-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
LexicalNodeMenuPlugin.tsx
134 lines (121 loc) · 3.47 KB
/
LexicalNodeMenuPlugin.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import type {MenuRenderFn, MenuResolution} from './shared/LexicalMenu';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {
$getNodeByKey,
COMMAND_PRIORITY_LOW,
CommandListenerPriority,
NodeKey,
TextNode,
} from 'lexical';
import {useCallback, useEffect, useState} from 'react';
import * as React from 'react';
import {LexicalMenu, MenuOption, useMenuAnchorRef} from './shared/LexicalMenu';
function startTransition(callback: () => void) {
if (React.startTransition) {
React.startTransition(callback);
} else {
callback();
}
}
export type NodeMenuPluginProps<TOption extends MenuOption> = {
onSelectOption: (
option: TOption,
textNodeContainingQuery: TextNode | null,
closeMenu: () => void,
matchingString: string,
) => void;
options: Array<TOption>;
nodeKey: NodeKey | null;
onClose?: () => void;
onOpen?: (resolution: MenuResolution) => void;
menuRenderFn: MenuRenderFn<TOption>;
anchorClassName?: string;
commandPriority?: CommandListenerPriority;
parent?: HTMLElement;
};
export function LexicalNodeMenuPlugin<TOption extends MenuOption>({
options,
nodeKey,
onClose,
onOpen,
onSelectOption,
menuRenderFn,
anchorClassName,
commandPriority = COMMAND_PRIORITY_LOW,
parent,
}: NodeMenuPluginProps<TOption>): JSX.Element | null {
const [editor] = useLexicalComposerContext();
const [resolution, setResolution] = useState<MenuResolution | null>(null);
const anchorElementRef = useMenuAnchorRef(
resolution,
setResolution,
anchorClassName,
parent,
);
const closeNodeMenu = useCallback(() => {
setResolution(null);
if (onClose != null && resolution !== null) {
onClose();
}
}, [onClose, resolution]);
const openNodeMenu = useCallback(
(res: MenuResolution) => {
setResolution(res);
if (onOpen != null && resolution === null) {
onOpen(res);
}
},
[onOpen, resolution],
);
const positionOrCloseMenu = useCallback(() => {
if (nodeKey) {
editor.update(() => {
const node = $getNodeByKey(nodeKey);
const domElement = editor.getElementByKey(nodeKey);
if (node != null && domElement != null) {
if (resolution == null) {
startTransition(() =>
openNodeMenu({
getRect: () => domElement.getBoundingClientRect(),
}),
);
}
}
});
} else if (nodeKey == null && resolution != null) {
closeNodeMenu();
}
}, [closeNodeMenu, editor, nodeKey, openNodeMenu, resolution]);
useEffect(() => {
positionOrCloseMenu();
}, [positionOrCloseMenu, nodeKey]);
useEffect(() => {
if (nodeKey != null) {
return editor.registerUpdateListener(({dirtyElements}) => {
if (dirtyElements.get(nodeKey)) {
positionOrCloseMenu();
}
});
}
}, [editor, positionOrCloseMenu, nodeKey]);
return resolution === null || editor === null ? null : (
<LexicalMenu
close={closeNodeMenu}
resolution={resolution}
editor={editor}
anchorElementRef={anchorElementRef}
options={options}
menuRenderFn={menuRenderFn}
onSelectOption={onSelectOption}
commandPriority={commandPriority}
/>
);
}
export {MenuOption, MenuRenderFn, MenuResolution};