@@ -2,8 +2,7 @@ import "../index.scss";
2
2
3
3
import { Trigger } from "@arco-design/web-react" ;
4
4
import type { EditorSchema , EditorSuite } from "doc-editor-core" ;
5
- import type { Path } from "doc-editor-delta" ;
6
- import type { RenderElementProps } from "doc-editor-delta" ;
5
+ import type { Path , RenderElementProps } from "doc-editor-delta" ;
7
6
import { Transforms } from "doc-editor-delta" ;
8
7
import { ReactEditor } from "doc-editor-delta" ;
9
8
import { cs , isTextBlock } from "doc-editor-utils" ;
@@ -25,44 +24,43 @@ export const DocMenu: React.FC<{
25
24
const [ iconVisible , setIconVisible ] = useState ( false ) ;
26
25
const [ menuVisible , setMenuVisible ] = useState ( false ) ;
27
26
28
- const visible = iconVisible || menuVisible ;
29
-
30
27
const onClose = ( ) => {
31
28
setIconVisible ( false ) ;
32
29
setMenuVisible ( false ) ;
33
30
} ;
34
31
35
32
const path = useMemo ( ( ) => {
36
33
let path : Path = [ ] ;
37
- if ( visible ) {
34
+ try {
38
35
// `Slate`没有直接维护状态 需要主动查找
36
+ // 注意即使在非`visible`的状态下也需要查找 否则会导致节点闪烁问题
39
37
// `findPath`不需要遍历所有`Descendant`而是根据两个`WeakMap`查找
40
- // slate-react/src/plugin/react-editor.ts
38
+ // https://github.com/ianstormtaylor/ slate/blob/25be3b/packages/slate -react/src/plugin/react-editor.ts#L100
41
39
path = ReactEditor . findPath ( props . editor , props . element ) ;
40
+ } catch ( error ) {
41
+ props . editor . logger . warning ( "ToolBar FindPath Error" , error ) ;
42
42
}
43
43
return path ;
44
- } , [ visible , props . editor , props . element ] ) ;
44
+ } , [ props . editor , props . element ] ) ;
45
45
46
46
const state = useMemo ( ( ) => {
47
47
let isInCodeBlock = false ;
48
48
let isInReactLive = false ;
49
49
let isInHighLightBlock = false ;
50
- if ( visible ) {
51
- withinIterator ( props . editor , path , node => {
52
- if ( node [ CODE_BLOCK_KEY ] ) isInCodeBlock = true ;
53
- if ( node [ REACT_LIVE_KEY ] ) isInReactLive = true ;
54
- if ( node [ HIGHLIGHT_BLOCK_KEY ] ) isInHighLightBlock = true ;
55
- } ) ;
56
- }
50
+ withinIterator ( props . editor , path , node => {
51
+ if ( node [ CODE_BLOCK_KEY ] ) isInCodeBlock = true ;
52
+ if ( node [ REACT_LIVE_KEY ] ) isInReactLive = true ;
53
+ if ( node [ HIGHLIGHT_BLOCK_KEY ] ) isInHighLightBlock = true ;
54
+ } ) ;
57
55
const state : DocToolBarState = {
58
56
path : path ,
59
57
schema : props . schema ,
60
58
editor : props . editor ,
61
59
element : props . element ,
62
60
status : {
63
- isBlock : visible && isBlockNode ( props . schema , props . element ) ,
64
- isTextBlock : visible && isTextBlock ( props . editor , props . element ) ,
65
- isEmptyLine : visible && isEmptyLine ( props . element ) ,
61
+ isBlock : isBlockNode ( props . schema , props . element ) ,
62
+ isTextBlock : isTextBlock ( props . editor , props . element ) ,
63
+ isEmptyLine : isEmptyLine ( props . element ) ,
66
64
isInCodeBlock : isInCodeBlock ,
67
65
isInReactLive : isInReactLive ,
68
66
isInHighLightBlock : isInHighLightBlock ,
@@ -71,7 +69,7 @@ export const DocMenu: React.FC<{
71
69
close : onClose ,
72
70
} ;
73
71
return state ;
74
- } , [ path , visible , props . editor , props . element , props . schema ] ) ;
72
+ } , [ path , props . editor , props . element , props . schema ] ) ;
75
73
76
74
const HoverIconConfig = useMemo ( ( ) => {
77
75
let HoverIconConfig : ReturnType < DocToolbarPlugin [ "renderIcon" ] > = null ;
0 commit comments