Skip to content

Commit 295a82c

Browse files
committed
fix: doc-toolbar rerender
1 parent 484a08e commit 295a82c

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

packages/plugin/src/doc-toolbar/components/doc-menu.tsx

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import "../index.scss";
22

33
import { Trigger } from "@arco-design/web-react";
44
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";
76
import { Transforms } from "doc-editor-delta";
87
import { ReactEditor } from "doc-editor-delta";
98
import { cs, isTextBlock } from "doc-editor-utils";
@@ -25,44 +24,43 @@ export const DocMenu: React.FC<{
2524
const [iconVisible, setIconVisible] = useState(false);
2625
const [menuVisible, setMenuVisible] = useState(false);
2726

28-
const visible = iconVisible || menuVisible;
29-
3027
const onClose = () => {
3128
setIconVisible(false);
3229
setMenuVisible(false);
3330
};
3431

3532
const path = useMemo(() => {
3633
let path: Path = [];
37-
if (visible) {
34+
try {
3835
// `Slate`没有直接维护状态 需要主动查找
36+
// 注意即使在非`visible`的状态下也需要查找 否则会导致节点闪烁问题
3937
// `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
4139
path = ReactEditor.findPath(props.editor, props.element);
40+
} catch (error) {
41+
props.editor.logger.warning("ToolBar FindPath Error", error);
4242
}
4343
return path;
44-
}, [visible, props.editor, props.element]);
44+
}, [props.editor, props.element]);
4545

4646
const state = useMemo(() => {
4747
let isInCodeBlock = false;
4848
let isInReactLive = false;
4949
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+
});
5755
const state: DocToolBarState = {
5856
path: path,
5957
schema: props.schema,
6058
editor: props.editor,
6159
element: props.element,
6260
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),
6664
isInCodeBlock: isInCodeBlock,
6765
isInReactLive: isInReactLive,
6866
isInHighLightBlock: isInHighLightBlock,
@@ -71,7 +69,7 @@ export const DocMenu: React.FC<{
7169
close: onClose,
7270
};
7371
return state;
74-
}, [path, visible, props.editor, props.element, props.schema]);
72+
}, [path, props.editor, props.element, props.schema]);
7573

7674
const HoverIconConfig = useMemo(() => {
7775
let HoverIconConfig: ReturnType<DocToolbarPlugin["renderIcon"]> = null;

0 commit comments

Comments
 (0)