@lexical/react 0.38.3-nightly.20251120.0 → 0.38.3-nightly.20251124.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LexicalAutoEmbedPlugin.d.ts +3 -2
- package/LexicalAutoEmbedPlugin.dev.js +2 -0
- package/LexicalAutoEmbedPlugin.dev.mjs +2 -0
- package/LexicalAutoEmbedPlugin.js.flow +2 -0
- package/LexicalAutoEmbedPlugin.prod.js +1 -1
- package/LexicalAutoEmbedPlugin.prod.mjs +1 -1
- package/LexicalContextMenuPlugin.d.ts +36 -0
- package/LexicalContextMenuPlugin.dev.js +524 -0
- package/LexicalContextMenuPlugin.dev.mjs +509 -0
- package/LexicalContextMenuPlugin.js +11 -0
- package/LexicalContextMenuPlugin.mjs +13 -0
- package/LexicalContextMenuPlugin.node.mjs +11 -0
- package/LexicalContextMenuPlugin.prod.js +9 -0
- package/LexicalContextMenuPlugin.prod.mjs +9 -0
- package/LexicalNodeMenuPlugin.d.ts +4 -3
- package/LexicalNodeMenuPlugin.dev.js +10 -49
- package/LexicalNodeMenuPlugin.dev.mjs +12 -51
- package/LexicalNodeMenuPlugin.js.flow +12 -0
- package/LexicalNodeMenuPlugin.prod.js +1 -1
- package/LexicalNodeMenuPlugin.prod.mjs +1 -1
- package/LexicalTypeaheadMenuPlugin.d.ts +4 -3
- package/LexicalTypeaheadMenuPlugin.dev.js +10 -49
- package/LexicalTypeaheadMenuPlugin.dev.mjs +12 -51
- package/LexicalTypeaheadMenuPlugin.js.flow +12 -0
- package/LexicalTypeaheadMenuPlugin.prod.js +1 -1
- package/LexicalTypeaheadMenuPlugin.prod.mjs +1 -1
- package/package.json +48 -18
- package/shared/LexicalMenu.d.ts +10 -4
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
10
|
+
import { mergeRegister, calculateZoomLevel } from '@lexical/utils';
|
|
11
|
+
import { createCommand, COMMAND_PRIORITY_LOW, KEY_ARROW_DOWN_COMMAND, KEY_ARROW_UP_COMMAND, KEY_ESCAPE_COMMAND, KEY_TAB_COMMAND, KEY_ENTER_COMMAND, $getSelection, $isRangeSelection, isDOMNode } from 'lexical';
|
|
12
|
+
import * as React from 'react';
|
|
13
|
+
import { useLayoutEffect, useEffect, useRef, useCallback, useState, useMemo } from 'react';
|
|
14
|
+
import { jsx } from 'react/jsx-runtime';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
18
|
+
*
|
|
19
|
+
* This source code is licensed under the MIT license found in the
|
|
20
|
+
* LICENSE file in the root directory of this source tree.
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
28
|
+
*
|
|
29
|
+
* This source code is licensed under the MIT license found in the
|
|
30
|
+
* LICENSE file in the root directory of this source tree.
|
|
31
|
+
*
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
// This workaround is no longer necessary in React 19,
|
|
36
|
+
// but we currently support React >=17.x
|
|
37
|
+
// https://github.com/facebook/react/pull/26395
|
|
38
|
+
const useLayoutEffectImpl = CAN_USE_DOM ? useLayoutEffect : useEffect;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
42
|
+
*
|
|
43
|
+
* This source code is licensed under the MIT license found in the
|
|
44
|
+
* LICENSE file in the root directory of this source tree.
|
|
45
|
+
*
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
class MenuOption {
|
|
49
|
+
key;
|
|
50
|
+
ref;
|
|
51
|
+
constructor(key) {
|
|
52
|
+
this.key = key;
|
|
53
|
+
this.ref = {
|
|
54
|
+
current: null
|
|
55
|
+
};
|
|
56
|
+
this.setRefElement = this.setRefElement.bind(this);
|
|
57
|
+
}
|
|
58
|
+
setRefElement(element) {
|
|
59
|
+
this.ref = {
|
|
60
|
+
current: element
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const scrollIntoViewIfNeeded = target => {
|
|
65
|
+
const typeaheadContainerNode = document.getElementById('typeahead-menu');
|
|
66
|
+
if (!typeaheadContainerNode) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const typeaheadRect = typeaheadContainerNode.getBoundingClientRect();
|
|
70
|
+
if (typeaheadRect.top + typeaheadRect.height > window.innerHeight) {
|
|
71
|
+
typeaheadContainerNode.scrollIntoView({
|
|
72
|
+
block: 'center'
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
if (typeaheadRect.top < 0) {
|
|
76
|
+
typeaheadContainerNode.scrollIntoView({
|
|
77
|
+
block: 'center'
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
target.scrollIntoView({
|
|
81
|
+
block: 'nearest'
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Walk backwards along user input and forward through entity title to try
|
|
87
|
+
* and replace more of the user's text with entity.
|
|
88
|
+
*/
|
|
89
|
+
function getFullMatchOffset(documentText, entryText, offset) {
|
|
90
|
+
let triggerOffset = offset;
|
|
91
|
+
for (let i = triggerOffset; i <= entryText.length; i++) {
|
|
92
|
+
if (documentText.slice(-i) === entryText.substring(0, i)) {
|
|
93
|
+
triggerOffset = i;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return triggerOffset;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Split Lexical TextNode and return a new TextNode only containing matched text.
|
|
101
|
+
* Common use cases include: removing the node, replacing with a new node.
|
|
102
|
+
*/
|
|
103
|
+
function $splitNodeContainingQuery(match) {
|
|
104
|
+
const selection = $getSelection();
|
|
105
|
+
if (!$isRangeSelection(selection) || !selection.isCollapsed()) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
const anchor = selection.anchor;
|
|
109
|
+
if (anchor.type !== 'text') {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
const anchorNode = anchor.getNode();
|
|
113
|
+
if (!anchorNode.isSimpleText()) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
const selectionOffset = anchor.offset;
|
|
117
|
+
const textContent = anchorNode.getTextContent().slice(0, selectionOffset);
|
|
118
|
+
const characterOffset = match.replaceableString.length;
|
|
119
|
+
const queryOffset = getFullMatchOffset(textContent, match.matchingString, characterOffset);
|
|
120
|
+
const startOffset = selectionOffset - queryOffset;
|
|
121
|
+
if (startOffset < 0) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
let newNode;
|
|
125
|
+
if (startOffset === 0) {
|
|
126
|
+
[newNode] = anchorNode.splitText(selectionOffset);
|
|
127
|
+
} else {
|
|
128
|
+
[, newNode] = anchorNode.splitText(startOffset, selectionOffset);
|
|
129
|
+
}
|
|
130
|
+
return newNode;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Got from https://stackoverflow.com/a/42543908/2013580
|
|
134
|
+
function getScrollParent(element, includeHidden) {
|
|
135
|
+
let style = getComputedStyle(element);
|
|
136
|
+
const excludeStaticParent = style.position === 'absolute';
|
|
137
|
+
const overflowRegex = /(auto|scroll)/;
|
|
138
|
+
if (style.position === 'fixed') {
|
|
139
|
+
return document.body;
|
|
140
|
+
}
|
|
141
|
+
for (let parent = element; parent = parent.parentElement;) {
|
|
142
|
+
style = getComputedStyle(parent);
|
|
143
|
+
if (excludeStaticParent && style.position === 'static') {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (overflowRegex.test(style.overflow + style.overflowY + style.overflowX)) {
|
|
147
|
+
return parent;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return document.body;
|
|
151
|
+
}
|
|
152
|
+
function isTriggerVisibleInNearestScrollContainer(targetElement, containerElement) {
|
|
153
|
+
const tRect = targetElement.getBoundingClientRect();
|
|
154
|
+
const cRect = containerElement.getBoundingClientRect();
|
|
155
|
+
const VISIBILITY_MARGIN_PX = 6;
|
|
156
|
+
return tRect.top >= cRect.top - VISIBILITY_MARGIN_PX && tRect.top <= cRect.bottom + VISIBILITY_MARGIN_PX;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Reposition the menu on scroll, window resize, and element resize.
|
|
160
|
+
function useDynamicPositioning(resolution, targetElement, onReposition, onVisibilityChange) {
|
|
161
|
+
const [editor] = useLexicalComposerContext();
|
|
162
|
+
useEffect(() => {
|
|
163
|
+
if (targetElement != null && resolution != null) {
|
|
164
|
+
const rootElement = editor.getRootElement();
|
|
165
|
+
const rootScrollParent = rootElement != null ? getScrollParent(rootElement) : document.body;
|
|
166
|
+
let ticking = false;
|
|
167
|
+
let previousIsInView = isTriggerVisibleInNearestScrollContainer(targetElement, rootScrollParent);
|
|
168
|
+
const handleScroll = function () {
|
|
169
|
+
if (!ticking) {
|
|
170
|
+
window.requestAnimationFrame(function () {
|
|
171
|
+
onReposition();
|
|
172
|
+
ticking = false;
|
|
173
|
+
});
|
|
174
|
+
ticking = true;
|
|
175
|
+
}
|
|
176
|
+
const isInView = isTriggerVisibleInNearestScrollContainer(targetElement, rootScrollParent);
|
|
177
|
+
if (isInView !== previousIsInView) {
|
|
178
|
+
previousIsInView = isInView;
|
|
179
|
+
if (onVisibilityChange != null) {
|
|
180
|
+
onVisibilityChange(isInView);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
const resizeObserver = new ResizeObserver(onReposition);
|
|
185
|
+
window.addEventListener('resize', onReposition);
|
|
186
|
+
document.addEventListener('scroll', handleScroll, {
|
|
187
|
+
capture: true,
|
|
188
|
+
passive: true
|
|
189
|
+
});
|
|
190
|
+
resizeObserver.observe(targetElement);
|
|
191
|
+
return () => {
|
|
192
|
+
resizeObserver.unobserve(targetElement);
|
|
193
|
+
window.removeEventListener('resize', onReposition);
|
|
194
|
+
document.removeEventListener('scroll', handleScroll, true);
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
}, [targetElement, editor, onVisibilityChange, onReposition, resolution]);
|
|
198
|
+
}
|
|
199
|
+
const SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND = createCommand('SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND');
|
|
200
|
+
function LexicalMenu({
|
|
201
|
+
close,
|
|
202
|
+
editor,
|
|
203
|
+
anchorElementRef,
|
|
204
|
+
resolution,
|
|
205
|
+
options,
|
|
206
|
+
menuRenderFn,
|
|
207
|
+
onSelectOption,
|
|
208
|
+
shouldSplitNodeWithQuery = false,
|
|
209
|
+
commandPriority = COMMAND_PRIORITY_LOW,
|
|
210
|
+
preselectFirstItem = true
|
|
211
|
+
}) {
|
|
212
|
+
const [rawSelectedIndex, setHighlightedIndex] = useState(null);
|
|
213
|
+
// Clamp highlighted index if options list shrinks
|
|
214
|
+
const selectedIndex = rawSelectedIndex !== null ? Math.min(options.length - 1, rawSelectedIndex) : null;
|
|
215
|
+
const matchingString = resolution.match && resolution.match.matchingString;
|
|
216
|
+
useEffect(() => {
|
|
217
|
+
if (preselectFirstItem) {
|
|
218
|
+
setHighlightedIndex(0);
|
|
219
|
+
}
|
|
220
|
+
}, [matchingString, preselectFirstItem]);
|
|
221
|
+
const selectOptionAndCleanUp = useCallback(selectedEntry => {
|
|
222
|
+
editor.update(() => {
|
|
223
|
+
const textNodeContainingQuery = resolution.match != null && shouldSplitNodeWithQuery ? $splitNodeContainingQuery(resolution.match) : null;
|
|
224
|
+
onSelectOption(selectedEntry, textNodeContainingQuery, close, resolution.match ? resolution.match.matchingString : '');
|
|
225
|
+
});
|
|
226
|
+
}, [editor, shouldSplitNodeWithQuery, resolution.match, onSelectOption, close]);
|
|
227
|
+
const updateSelectedIndex = useCallback(index => {
|
|
228
|
+
const rootElem = editor.getRootElement();
|
|
229
|
+
if (rootElem !== null) {
|
|
230
|
+
rootElem.setAttribute('aria-activedescendant', 'typeahead-item-' + index);
|
|
231
|
+
setHighlightedIndex(index);
|
|
232
|
+
}
|
|
233
|
+
}, [editor]);
|
|
234
|
+
useEffect(() => {
|
|
235
|
+
return () => {
|
|
236
|
+
const rootElem = editor.getRootElement();
|
|
237
|
+
if (rootElem !== null) {
|
|
238
|
+
rootElem.removeAttribute('aria-activedescendant');
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
}, [editor]);
|
|
242
|
+
useLayoutEffectImpl(() => {
|
|
243
|
+
if (options === null) {
|
|
244
|
+
setHighlightedIndex(null);
|
|
245
|
+
} else if (selectedIndex === null && preselectFirstItem) {
|
|
246
|
+
updateSelectedIndex(0);
|
|
247
|
+
}
|
|
248
|
+
}, [options, selectedIndex, updateSelectedIndex, preselectFirstItem]);
|
|
249
|
+
useEffect(() => {
|
|
250
|
+
return mergeRegister(editor.registerCommand(SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND, ({
|
|
251
|
+
option
|
|
252
|
+
}) => {
|
|
253
|
+
if (option.ref && option.ref.current != null) {
|
|
254
|
+
scrollIntoViewIfNeeded(option.ref.current);
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
return false;
|
|
258
|
+
}, commandPriority));
|
|
259
|
+
}, [editor, updateSelectedIndex, commandPriority]);
|
|
260
|
+
useEffect(() => {
|
|
261
|
+
return mergeRegister(editor.registerCommand(KEY_ARROW_DOWN_COMMAND, payload => {
|
|
262
|
+
const event = payload;
|
|
263
|
+
if (options !== null && options.length) {
|
|
264
|
+
const newSelectedIndex = selectedIndex === null ? 0 : selectedIndex !== options.length - 1 ? selectedIndex + 1 : 0;
|
|
265
|
+
updateSelectedIndex(newSelectedIndex);
|
|
266
|
+
const option = options[newSelectedIndex];
|
|
267
|
+
if (!option) {
|
|
268
|
+
updateSelectedIndex(-1);
|
|
269
|
+
event.preventDefault();
|
|
270
|
+
event.stopImmediatePropagation();
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
if (option.ref && option.ref.current) {
|
|
274
|
+
editor.dispatchCommand(SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND, {
|
|
275
|
+
index: newSelectedIndex,
|
|
276
|
+
option
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
event.preventDefault();
|
|
280
|
+
event.stopImmediatePropagation();
|
|
281
|
+
}
|
|
282
|
+
return true;
|
|
283
|
+
}, commandPriority), editor.registerCommand(KEY_ARROW_UP_COMMAND, payload => {
|
|
284
|
+
const event = payload;
|
|
285
|
+
if (options !== null && options.length) {
|
|
286
|
+
const newSelectedIndex = selectedIndex === null ? options.length - 1 : selectedIndex !== 0 ? selectedIndex - 1 : options.length - 1;
|
|
287
|
+
updateSelectedIndex(newSelectedIndex);
|
|
288
|
+
const option = options[newSelectedIndex];
|
|
289
|
+
if (!option) {
|
|
290
|
+
updateSelectedIndex(-1);
|
|
291
|
+
event.preventDefault();
|
|
292
|
+
event.stopImmediatePropagation();
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
if (option.ref && option.ref.current) {
|
|
296
|
+
scrollIntoViewIfNeeded(option.ref.current);
|
|
297
|
+
}
|
|
298
|
+
event.preventDefault();
|
|
299
|
+
event.stopImmediatePropagation();
|
|
300
|
+
}
|
|
301
|
+
return true;
|
|
302
|
+
}, commandPriority), editor.registerCommand(KEY_ESCAPE_COMMAND, payload => {
|
|
303
|
+
const event = payload;
|
|
304
|
+
event.preventDefault();
|
|
305
|
+
event.stopImmediatePropagation();
|
|
306
|
+
close();
|
|
307
|
+
return true;
|
|
308
|
+
}, commandPriority), editor.registerCommand(KEY_TAB_COMMAND, payload => {
|
|
309
|
+
const event = payload;
|
|
310
|
+
if (options === null || selectedIndex === null || options[selectedIndex] == null) {
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
event.preventDefault();
|
|
314
|
+
event.stopImmediatePropagation();
|
|
315
|
+
selectOptionAndCleanUp(options[selectedIndex]);
|
|
316
|
+
return true;
|
|
317
|
+
}, commandPriority), editor.registerCommand(KEY_ENTER_COMMAND, event => {
|
|
318
|
+
if (options === null || selectedIndex === null || options[selectedIndex] == null) {
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
if (event !== null) {
|
|
322
|
+
event.preventDefault();
|
|
323
|
+
event.stopImmediatePropagation();
|
|
324
|
+
}
|
|
325
|
+
selectOptionAndCleanUp(options[selectedIndex]);
|
|
326
|
+
return true;
|
|
327
|
+
}, commandPriority));
|
|
328
|
+
}, [selectOptionAndCleanUp, close, editor, options, selectedIndex, updateSelectedIndex, commandPriority]);
|
|
329
|
+
const listItemProps = useMemo(() => ({
|
|
330
|
+
options,
|
|
331
|
+
selectOptionAndCleanUp,
|
|
332
|
+
selectedIndex,
|
|
333
|
+
setHighlightedIndex
|
|
334
|
+
}), [selectOptionAndCleanUp, selectedIndex, options]);
|
|
335
|
+
return menuRenderFn(anchorElementRef, listItemProps, resolution.match ? resolution.match.matchingString : '');
|
|
336
|
+
}
|
|
337
|
+
function setContainerDivAttributes(containerDiv, className) {
|
|
338
|
+
if (className != null) {
|
|
339
|
+
containerDiv.className = className;
|
|
340
|
+
}
|
|
341
|
+
containerDiv.setAttribute('aria-label', 'Typeahead menu');
|
|
342
|
+
containerDiv.setAttribute('role', 'listbox');
|
|
343
|
+
containerDiv.style.display = 'block';
|
|
344
|
+
containerDiv.style.position = 'absolute';
|
|
345
|
+
}
|
|
346
|
+
function useMenuAnchorRef(resolution, setResolution, className, parent = CAN_USE_DOM ? document.body : undefined, shouldIncludePageYOffset__EXPERIMENTAL = true) {
|
|
347
|
+
const [editor] = useLexicalComposerContext();
|
|
348
|
+
const initialAnchorElement = CAN_USE_DOM ? document.createElement('div') : null;
|
|
349
|
+
const anchorElementRef = useRef(initialAnchorElement);
|
|
350
|
+
const positionMenu = useCallback(() => {
|
|
351
|
+
if (anchorElementRef.current === null || parent === undefined) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
anchorElementRef.current.style.top = anchorElementRef.current.style.bottom;
|
|
355
|
+
const rootElement = editor.getRootElement();
|
|
356
|
+
const containerDiv = anchorElementRef.current;
|
|
357
|
+
const menuEle = containerDiv.firstChild;
|
|
358
|
+
if (rootElement !== null && resolution !== null) {
|
|
359
|
+
const {
|
|
360
|
+
left,
|
|
361
|
+
top,
|
|
362
|
+
width,
|
|
363
|
+
height
|
|
364
|
+
} = resolution.getRect();
|
|
365
|
+
const anchorHeight = anchorElementRef.current.offsetHeight; // use to position under anchor
|
|
366
|
+
containerDiv.style.top = `${top + anchorHeight + 3 + (shouldIncludePageYOffset__EXPERIMENTAL ? window.pageYOffset : 0)}px`;
|
|
367
|
+
containerDiv.style.left = `${left + window.pageXOffset}px`;
|
|
368
|
+
containerDiv.style.height = `${height}px`;
|
|
369
|
+
containerDiv.style.width = `${width}px`;
|
|
370
|
+
if (menuEle !== null) {
|
|
371
|
+
menuEle.style.top = `${top}`;
|
|
372
|
+
const menuRect = menuEle.getBoundingClientRect();
|
|
373
|
+
const menuHeight = menuRect.height;
|
|
374
|
+
const menuWidth = menuRect.width;
|
|
375
|
+
const rootElementRect = rootElement.getBoundingClientRect();
|
|
376
|
+
if (left + menuWidth > rootElementRect.right) {
|
|
377
|
+
containerDiv.style.left = `${rootElementRect.right - menuWidth + window.pageXOffset}px`;
|
|
378
|
+
}
|
|
379
|
+
if ((top + menuHeight > window.innerHeight || top + menuHeight > rootElementRect.bottom) && top - rootElementRect.top > menuHeight + height) {
|
|
380
|
+
containerDiv.style.top = `${top - menuHeight - height + (shouldIncludePageYOffset__EXPERIMENTAL ? window.pageYOffset : 0)}px`;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
if (!containerDiv.isConnected) {
|
|
384
|
+
setContainerDivAttributes(containerDiv, className);
|
|
385
|
+
parent.append(containerDiv);
|
|
386
|
+
}
|
|
387
|
+
containerDiv.setAttribute('id', 'typeahead-menu');
|
|
388
|
+
rootElement.setAttribute('aria-controls', 'typeahead-menu');
|
|
389
|
+
}
|
|
390
|
+
}, [editor, resolution, shouldIncludePageYOffset__EXPERIMENTAL, className, parent]);
|
|
391
|
+
useEffect(() => {
|
|
392
|
+
const rootElement = editor.getRootElement();
|
|
393
|
+
if (resolution !== null) {
|
|
394
|
+
positionMenu();
|
|
395
|
+
}
|
|
396
|
+
return () => {
|
|
397
|
+
if (rootElement !== null) {
|
|
398
|
+
rootElement.removeAttribute('aria-controls');
|
|
399
|
+
}
|
|
400
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
401
|
+
const containerDiv = anchorElementRef.current;
|
|
402
|
+
if (containerDiv !== null && containerDiv.isConnected) {
|
|
403
|
+
containerDiv.remove();
|
|
404
|
+
containerDiv.removeAttribute('id');
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
}, [editor, positionMenu, resolution]);
|
|
408
|
+
const onVisibilityChange = useCallback(isInView => {
|
|
409
|
+
if (resolution !== null) {
|
|
410
|
+
if (!isInView) {
|
|
411
|
+
setResolution(null);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}, [resolution, setResolution]);
|
|
415
|
+
useDynamicPositioning(resolution, anchorElementRef.current, positionMenu, onVisibilityChange);
|
|
416
|
+
|
|
417
|
+
// Append the context for the menu immediately
|
|
418
|
+
if (initialAnchorElement != null && initialAnchorElement === anchorElementRef.current) {
|
|
419
|
+
setContainerDivAttributes(initialAnchorElement, className);
|
|
420
|
+
if (parent != null) {
|
|
421
|
+
parent.append(initialAnchorElement);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return anchorElementRef;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
429
|
+
*
|
|
430
|
+
* This source code is licensed under the MIT license found in the
|
|
431
|
+
* LICENSE file in the root directory of this source tree.
|
|
432
|
+
*
|
|
433
|
+
*/
|
|
434
|
+
|
|
435
|
+
const PRE_PORTAL_DIV_SIZE = 1;
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* @deprecated Use LexicalNodeContextMenuPlugin instead.
|
|
439
|
+
*/
|
|
440
|
+
function LexicalContextMenuPlugin({
|
|
441
|
+
options,
|
|
442
|
+
onWillOpen,
|
|
443
|
+
onClose,
|
|
444
|
+
onOpen,
|
|
445
|
+
onSelectOption,
|
|
446
|
+
menuRenderFn: contextMenuRenderFn,
|
|
447
|
+
anchorClassName,
|
|
448
|
+
commandPriority = COMMAND_PRIORITY_LOW,
|
|
449
|
+
parent
|
|
450
|
+
}) {
|
|
451
|
+
const [editor] = useLexicalComposerContext();
|
|
452
|
+
const [resolution, setResolution] = useState(null);
|
|
453
|
+
const menuRef = React.useRef(null);
|
|
454
|
+
const anchorElementRef = useMenuAnchorRef(resolution, setResolution, anchorClassName, parent);
|
|
455
|
+
const closeNodeMenu = useCallback(() => {
|
|
456
|
+
setResolution(null);
|
|
457
|
+
if (onClose != null && resolution !== null) {
|
|
458
|
+
onClose();
|
|
459
|
+
}
|
|
460
|
+
}, [onClose, resolution]);
|
|
461
|
+
const openNodeMenu = useCallback(res => {
|
|
462
|
+
setResolution(res);
|
|
463
|
+
if (onOpen != null && resolution === null) {
|
|
464
|
+
onOpen(res);
|
|
465
|
+
}
|
|
466
|
+
}, [onOpen, resolution]);
|
|
467
|
+
const handleContextMenu = useCallback(event => {
|
|
468
|
+
event.preventDefault();
|
|
469
|
+
if (onWillOpen != null) {
|
|
470
|
+
onWillOpen(event);
|
|
471
|
+
}
|
|
472
|
+
const zoom = calculateZoomLevel(event.target);
|
|
473
|
+
openNodeMenu({
|
|
474
|
+
getRect: () => new DOMRect(event.clientX / zoom, event.clientY / zoom, PRE_PORTAL_DIV_SIZE, PRE_PORTAL_DIV_SIZE)
|
|
475
|
+
});
|
|
476
|
+
}, [openNodeMenu, onWillOpen]);
|
|
477
|
+
const handleClick = useCallback(event => {
|
|
478
|
+
if (resolution !== null && menuRef.current != null && event.target != null && isDOMNode(event.target) && !menuRef.current.contains(event.target)) {
|
|
479
|
+
closeNodeMenu();
|
|
480
|
+
}
|
|
481
|
+
}, [closeNodeMenu, resolution]);
|
|
482
|
+
useEffect(() => {
|
|
483
|
+
const editorElement = editor.getRootElement();
|
|
484
|
+
if (editorElement) {
|
|
485
|
+
editorElement.addEventListener('contextmenu', handleContextMenu);
|
|
486
|
+
return () => editorElement.removeEventListener('contextmenu', handleContextMenu);
|
|
487
|
+
}
|
|
488
|
+
}, [editor, handleContextMenu]);
|
|
489
|
+
useEffect(() => {
|
|
490
|
+
document.addEventListener('click', handleClick);
|
|
491
|
+
return () => document.removeEventListener('click', handleClick);
|
|
492
|
+
}, [editor, handleClick]);
|
|
493
|
+
return anchorElementRef.current === null || resolution === null || editor === null ? null : /*#__PURE__*/jsx(LexicalMenu, {
|
|
494
|
+
close: closeNodeMenu,
|
|
495
|
+
resolution: resolution,
|
|
496
|
+
editor: editor,
|
|
497
|
+
anchorElementRef: anchorElementRef,
|
|
498
|
+
options: options,
|
|
499
|
+
menuRenderFn: (anchorRef, itemProps) => contextMenuRenderFn(anchorRef, itemProps, {
|
|
500
|
+
setMenuRef: ref => {
|
|
501
|
+
menuRef.current = ref;
|
|
502
|
+
}
|
|
503
|
+
}),
|
|
504
|
+
onSelectOption: onSelectOption,
|
|
505
|
+
commandPriority: commandPriority
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
export { LexicalContextMenuPlugin, MenuOption };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
'use strict'
|
|
10
|
+
const LexicalContextMenuPlugin = process.env.NODE_ENV !== 'production' ? require('./LexicalContextMenuPlugin.dev.js') : require('./LexicalContextMenuPlugin.prod.js');
|
|
11
|
+
module.exports = LexicalContextMenuPlugin;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import * as modDev from './LexicalContextMenuPlugin.dev.mjs';
|
|
10
|
+
import * as modProd from './LexicalContextMenuPlugin.prod.mjs';
|
|
11
|
+
const mod = process.env.NODE_ENV !== 'production' ? modDev : modProd;
|
|
12
|
+
export const LexicalContextMenuPlugin = mod.LexicalContextMenuPlugin;
|
|
13
|
+
export const MenuOption = mod.MenuOption;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const mod = await (process.env.NODE_ENV !== 'production' ? import('./LexicalContextMenuPlugin.dev.mjs') : import('./LexicalContextMenuPlugin.prod.mjs'));
|
|
10
|
+
export const LexicalContextMenuPlugin = mod.LexicalContextMenuPlugin;
|
|
11
|
+
export const MenuOption = mod.MenuOption;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
"use strict";var e=require("@lexical/react/LexicalComposerContext"),t=require("@lexical/utils"),n=require("lexical"),o=require("react"),l=require("react/jsx-runtime");function r(e){var t=Object.create(null);if(e)for(var n in e)t[n]=e[n];return t.default=e,t}var i=r(o);const u="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement,s=u?o.useLayoutEffect:o.useEffect;const c=e=>{const t=document.getElementById("typeahead-menu");if(!t)return;const n=t.getBoundingClientRect();n.top+n.height>window.innerHeight&&t.scrollIntoView({block:"center"}),n.top<0&&t.scrollIntoView({block:"center"}),e.scrollIntoView({block:"nearest"})};function a(e,t){const n=e.getBoundingClientRect(),o=t.getBoundingClientRect();return n.top>=o.top-6&&n.top<=o.bottom+6}function m(t,n,l,r){const[i]=e.useLexicalComposerContext();o.useEffect(()=>{if(null!=n&&null!=t){const e=i.getRootElement(),t=null!=e?function(e){let t=getComputedStyle(e);const n="absolute"===t.position,o=/(auto|scroll)/;if("fixed"===t.position)return document.body;for(let l=e;l=l.parentElement;)if(t=getComputedStyle(l),(!n||"static"!==t.position)&&o.test(t.overflow+t.overflowY+t.overflowX))return l;return document.body}(e):document.body;let o=!1,u=a(n,t);const s=function(){o||(window.requestAnimationFrame(function(){l(),o=!1}),o=!0);const e=a(n,t);e!==u&&(u=e,null!=r&&r(e))},c=new ResizeObserver(l);return window.addEventListener("resize",l),document.addEventListener("scroll",s,{capture:!0,passive:!0}),c.observe(n),()=>{c.unobserve(n),window.removeEventListener("resize",l),document.removeEventListener("scroll",s,!0)}}},[n,i,r,l,t])}const d=n.createCommand("SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND");function f({close:e,editor:l,anchorElementRef:r,resolution:i,options:u,menuRenderFn:a,onSelectOption:m,shouldSplitNodeWithQuery:f=!1,commandPriority:p=n.COMMAND_PRIORITY_LOW,preselectFirstItem:g=!0}){const[h,C]=o.useState(null),E=null!==h?Math.min(u.length-1,h):null,v=i.match&&i.match.matchingString;o.useEffect(()=>{g&&C(0)},[v,g]);const R=o.useCallback(t=>{l.update(()=>{const o=null!=i.match&&f?function(e){const t=n.$getSelection();if(!n.$isRangeSelection(t)||!t.isCollapsed())return null;const o=t.anchor;if("text"!==o.type)return null;const l=o.getNode();if(!l.isSimpleText())return null;const r=o.offset,i=l.getTextContent().slice(0,r),u=e.replaceableString.length,s=r-function(e,t,n){let o=n;for(let n=o;n<=t.length;n++)e.slice(-n)===t.substring(0,n)&&(o=n);return o}(i,e.matchingString,u);if(s<0)return null;let c;return 0===s?[c]=l.splitText(r):[,c]=l.splitText(s,r),c}(i.match):null;m(t,o,e,i.match?i.match.matchingString:"")})},[l,f,i.match,m,e]),b=o.useCallback(e=>{const t=l.getRootElement();null!==t&&(t.setAttribute("aria-activedescendant","typeahead-item-"+e),C(e))},[l]);o.useEffect(()=>()=>{const e=l.getRootElement();null!==e&&e.removeAttribute("aria-activedescendant")},[l]),s(()=>{null===u?C(null):null===E&&g&&b(0)},[u,E,b,g]),o.useEffect(()=>t.mergeRegister(l.registerCommand(d,({option:e})=>!(!e.ref||null==e.ref.current)&&(c(e.ref.current),!0),p)),[l,b,p]),o.useEffect(()=>t.mergeRegister(l.registerCommand(n.KEY_ARROW_DOWN_COMMAND,e=>{const t=e;if(null!==u&&u.length){const e=null===E?0:E!==u.length-1?E+1:0;b(e);const n=u[e];if(!n)return b(-1),t.preventDefault(),t.stopImmediatePropagation(),!0;n.ref&&n.ref.current&&l.dispatchCommand(d,{index:e,option:n}),t.preventDefault(),t.stopImmediatePropagation()}return!0},p),l.registerCommand(n.KEY_ARROW_UP_COMMAND,e=>{const t=e;if(null!==u&&u.length){const e=null===E?u.length-1:0!==E?E-1:u.length-1;b(e);const n=u[e];if(!n)return b(-1),t.preventDefault(),t.stopImmediatePropagation(),!0;n.ref&&n.ref.current&&c(n.ref.current),t.preventDefault(),t.stopImmediatePropagation()}return!0},p),l.registerCommand(n.KEY_ESCAPE_COMMAND,t=>{const n=t;return n.preventDefault(),n.stopImmediatePropagation(),e(),!0},p),l.registerCommand(n.KEY_TAB_COMMAND,e=>{const t=e;return null!==u&&null!==E&&null!=u[E]&&(t.preventDefault(),t.stopImmediatePropagation(),R(u[E]),!0)},p),l.registerCommand(n.KEY_ENTER_COMMAND,e=>null!==u&&null!==E&&null!=u[E]&&(null!==e&&(e.preventDefault(),e.stopImmediatePropagation()),R(u[E]),!0),p)),[R,e,l,u,E,b,p]);return a(r,o.useMemo(()=>({options:u,selectOptionAndCleanUp:R,selectedIndex:E,setHighlightedIndex:C}),[R,E,u]),i.match?i.match.matchingString:"")}function p(e,t){null!=t&&(e.className=t),e.setAttribute("aria-label","Typeahead menu"),e.setAttribute("role","listbox"),e.style.display="block",e.style.position="absolute"}exports.LexicalContextMenuPlugin=function({options:r,onWillOpen:s,onClose:c,onOpen:a,onSelectOption:d,menuRenderFn:g,anchorClassName:h,commandPriority:C=n.COMMAND_PRIORITY_LOW,parent:E}){const[v]=e.useLexicalComposerContext(),[R,b]=o.useState(null),w=i.useRef(null),x=function(t,n,l,r=(u?document.body:void 0),i=!0){const[s]=e.useLexicalComposerContext(),c=u?document.createElement("div"):null,a=o.useRef(c),d=o.useCallback(()=>{if(null===a.current||void 0===r)return;a.current.style.top=a.current.style.bottom;const e=s.getRootElement(),n=a.current,o=n.firstChild;if(null!==e&&null!==t){const{left:u,top:s,width:c,height:m}=t.getRect(),d=a.current.offsetHeight;if(n.style.top=`${s+d+3+(i?window.pageYOffset:0)}px`,n.style.left=`${u+window.pageXOffset}px`,n.style.height=`${m}px`,n.style.width=`${c}px`,null!==o){o.style.top=`${s}`;const t=o.getBoundingClientRect(),l=t.height,r=t.width,c=e.getBoundingClientRect();u+r>c.right&&(n.style.left=`${c.right-r+window.pageXOffset}px`),(s+l>window.innerHeight||s+l>c.bottom)&&s-c.top>l+m&&(n.style.top=`${s-l-m+(i?window.pageYOffset:0)}px`)}n.isConnected||(p(n,l),r.append(n)),n.setAttribute("id","typeahead-menu"),e.setAttribute("aria-controls","typeahead-menu")}},[s,t,i,l,r]);o.useEffect(()=>{const e=s.getRootElement();return null!==t&&d(),()=>{null!==e&&e.removeAttribute("aria-controls");const t=a.current;null!==t&&t.isConnected&&(t.remove(),t.removeAttribute("id"))}},[s,d,t]);const f=o.useCallback(e=>{null!==t&&(e||n(null))},[t,n]);return m(t,a.current,d,f),null!=c&&c===a.current&&(p(c,l),null!=r&&r.append(c)),a}(R,b,h,E),O=o.useCallback(()=>{b(null),null!=c&&null!==R&&c()},[c,R]),y=o.useCallback(e=>{b(e),null!=a&&null===R&&a(e)},[a,R]),A=o.useCallback(e=>{e.preventDefault(),null!=s&&s(e);const n=t.calculateZoomLevel(e.target);y({getRect:()=>new DOMRect(e.clientX/n,e.clientY/n,1,1)})},[y,s]),M=o.useCallback(e=>{null!==R&&null!=w.current&&null!=e.target&&n.isDOMNode(e.target)&&!w.current.contains(e.target)&&O()},[O,R]);return o.useEffect(()=>{const e=v.getRootElement();if(e)return e.addEventListener("contextmenu",A),()=>e.removeEventListener("contextmenu",A)},[v,A]),o.useEffect(()=>(document.addEventListener("click",M),()=>document.removeEventListener("click",M)),[v,M]),null===x.current||null===R||null===v?null:l.jsx(f,{close:O,resolution:R,editor:v,anchorElementRef:x,options:r,menuRenderFn:(e,t)=>g(e,t,{setMenuRef:e=>{w.current=e}}),onSelectOption:d,commandPriority:C})},exports.MenuOption=class{key;ref;constructor(e){this.key=e,this.ref={current:null},this.setRefElement=this.setRefElement.bind(this)}setRefElement(e){this.ref={current:e}}};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import{useLexicalComposerContext as t}from"@lexical/react/LexicalComposerContext";import{mergeRegister as e,calculateZoomLevel as n}from"@lexical/utils";import{createCommand as o,COMMAND_PRIORITY_LOW as l,KEY_ARROW_DOWN_COMMAND as r,KEY_ARROW_UP_COMMAND as i,KEY_ESCAPE_COMMAND as u,KEY_TAB_COMMAND as c,KEY_ENTER_COMMAND as s,$getSelection as a,$isRangeSelection as m,isDOMNode as d}from"lexical";import*as p from"react";import{useLayoutEffect as f,useEffect as g,useRef as h,useCallback as v,useState as w,useMemo as y}from"react";import{jsx as b}from"react/jsx-runtime";const C="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement,E=C?f:g;class R{key;ref;constructor(t){this.key=t,this.ref={current:null},this.setRefElement=this.setRefElement.bind(this)}setRefElement(t){this.ref={current:t}}}const x=t=>{const e=document.getElementById("typeahead-menu");if(!e)return;const n=e.getBoundingClientRect();n.top+n.height>window.innerHeight&&e.scrollIntoView({block:"center"}),n.top<0&&e.scrollIntoView({block:"center"}),t.scrollIntoView({block:"nearest"})};function I(t,e){const n=t.getBoundingClientRect(),o=e.getBoundingClientRect();return n.top>=o.top-6&&n.top<=o.bottom+6}function O(e,n,o,l){const[r]=t();g(()=>{if(null!=n&&null!=e){const t=r.getRootElement(),e=null!=t?function(t){let e=getComputedStyle(t);const n="absolute"===e.position,o=/(auto|scroll)/;if("fixed"===e.position)return document.body;for(let l=t;l=l.parentElement;)if(e=getComputedStyle(l),(!n||"static"!==e.position)&&o.test(e.overflow+e.overflowY+e.overflowX))return l;return document.body}(t):document.body;let i=!1,u=I(n,e);const c=function(){i||(window.requestAnimationFrame(function(){o(),i=!1}),i=!0);const t=I(n,e);t!==u&&(u=t,null!=l&&l(t))},s=new ResizeObserver(o);return window.addEventListener("resize",o),document.addEventListener("scroll",c,{capture:!0,passive:!0}),s.observe(n),()=>{s.unobserve(n),window.removeEventListener("resize",o),document.removeEventListener("scroll",c,!0)}}},[n,r,l,o,e])}const A=o("SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND");function S({close:t,editor:n,anchorElementRef:o,resolution:d,options:p,menuRenderFn:f,onSelectOption:h,shouldSplitNodeWithQuery:b=!1,commandPriority:C=l,preselectFirstItem:R=!0}){const[I,O]=w(null),S=null!==I?Math.min(p.length-1,I):null,P=d.match&&d.match.matchingString;g(()=>{R&&O(0)},[P,R]);const D=v(e=>{n.update(()=>{const n=null!=d.match&&b?function(t){const e=a();if(!m(e)||!e.isCollapsed())return null;const n=e.anchor;if("text"!==n.type)return null;const o=n.getNode();if(!o.isSimpleText())return null;const l=n.offset,r=o.getTextContent().slice(0,l),i=t.replaceableString.length,u=l-function(t,e,n){let o=n;for(let n=o;n<=e.length;n++)t.slice(-n)===e.substring(0,n)&&(o=n);return o}(r,t.matchingString,i);if(u<0)return null;let c;return 0===u?[c]=o.splitText(l):[,c]=o.splitText(u,l),c}(d.match):null;h(e,n,t,d.match?d.match.matchingString:"")})},[n,b,d.match,h,t]),L=v(t=>{const e=n.getRootElement();null!==e&&(e.setAttribute("aria-activedescendant","typeahead-item-"+t),O(t))},[n]);g(()=>()=>{const t=n.getRootElement();null!==t&&t.removeAttribute("aria-activedescendant")},[n]),E(()=>{null===p?O(null):null===S&&R&&L(0)},[p,S,L,R]),g(()=>e(n.registerCommand(A,({option:t})=>!(!t.ref||null==t.ref.current)&&(x(t.ref.current),!0),C)),[n,L,C]),g(()=>e(n.registerCommand(r,t=>{const e=t;if(null!==p&&p.length){const t=null===S?0:S!==p.length-1?S+1:0;L(t);const o=p[t];if(!o)return L(-1),e.preventDefault(),e.stopImmediatePropagation(),!0;o.ref&&o.ref.current&&n.dispatchCommand(A,{index:t,option:o}),e.preventDefault(),e.stopImmediatePropagation()}return!0},C),n.registerCommand(i,t=>{const e=t;if(null!==p&&p.length){const t=null===S?p.length-1:0!==S?S-1:p.length-1;L(t);const n=p[t];if(!n)return L(-1),e.preventDefault(),e.stopImmediatePropagation(),!0;n.ref&&n.ref.current&&x(n.ref.current),e.preventDefault(),e.stopImmediatePropagation()}return!0},C),n.registerCommand(u,e=>{const n=e;return n.preventDefault(),n.stopImmediatePropagation(),t(),!0},C),n.registerCommand(c,t=>{const e=t;return null!==p&&null!==S&&null!=p[S]&&(e.preventDefault(),e.stopImmediatePropagation(),D(p[S]),!0)},C),n.registerCommand(s,t=>null!==p&&null!==S&&null!=p[S]&&(null!==t&&(t.preventDefault(),t.stopImmediatePropagation()),D(p[S]),!0),C)),[D,t,n,p,S,L,C]);return f(o,y(()=>({options:p,selectOptionAndCleanUp:D,selectedIndex:S,setHighlightedIndex:O}),[D,S,p]),d.match?d.match.matchingString:"")}function P(t,e){null!=e&&(t.className=e),t.setAttribute("aria-label","Typeahead menu"),t.setAttribute("role","listbox"),t.style.display="block",t.style.position="absolute"}function D({options:e,onWillOpen:o,onClose:r,onOpen:i,onSelectOption:u,menuRenderFn:c,anchorClassName:s,commandPriority:a=l,parent:m}){const[f]=t(),[y,E]=w(null),R=p.useRef(null),x=function(e,n,o,l=(C?document.body:void 0),r=!0){const[i]=t(),u=C?document.createElement("div"):null,c=h(u),s=v(()=>{if(null===c.current||void 0===l)return;c.current.style.top=c.current.style.bottom;const t=i.getRootElement(),n=c.current,u=n.firstChild;if(null!==t&&null!==e){const{left:i,top:s,width:a,height:m}=e.getRect(),d=c.current.offsetHeight;if(n.style.top=`${s+d+3+(r?window.pageYOffset:0)}px`,n.style.left=`${i+window.pageXOffset}px`,n.style.height=`${m}px`,n.style.width=`${a}px`,null!==u){u.style.top=`${s}`;const e=u.getBoundingClientRect(),o=e.height,l=e.width,c=t.getBoundingClientRect();i+l>c.right&&(n.style.left=`${c.right-l+window.pageXOffset}px`),(s+o>window.innerHeight||s+o>c.bottom)&&s-c.top>o+m&&(n.style.top=`${s-o-m+(r?window.pageYOffset:0)}px`)}n.isConnected||(P(n,o),l.append(n)),n.setAttribute("id","typeahead-menu"),t.setAttribute("aria-controls","typeahead-menu")}},[i,e,r,o,l]);g(()=>{const t=i.getRootElement();return null!==e&&s(),()=>{null!==t&&t.removeAttribute("aria-controls");const e=c.current;null!==e&&e.isConnected&&(e.remove(),e.removeAttribute("id"))}},[i,s,e]);const a=v(t=>{null!==e&&(t||n(null))},[e,n]);return O(e,c.current,s,a),null!=u&&u===c.current&&(P(u,o),null!=l&&l.append(u)),c}(y,E,s,m),I=v(()=>{E(null),null!=r&&null!==y&&r()},[r,y]),A=v(t=>{E(t),null!=i&&null===y&&i(t)},[i,y]),D=v(t=>{t.preventDefault(),null!=o&&o(t);const e=n(t.target);A({getRect:()=>new DOMRect(t.clientX/e,t.clientY/e,1,1)})},[A,o]),L=v(t=>{null!==y&&null!=R.current&&null!=t.target&&d(t.target)&&!R.current.contains(t.target)&&I()},[I,y]);return g(()=>{const t=f.getRootElement();if(t)return t.addEventListener("contextmenu",D),()=>t.removeEventListener("contextmenu",D)},[f,D]),g(()=>(document.addEventListener("click",L),()=>document.removeEventListener("click",L)),[f,L]),null===x.current||null===y||null===f?null:b(S,{close:I,resolution:y,editor:f,anchorElementRef:x,options:e,menuRenderFn:(t,e)=>c(t,e,{setMenuRef:t=>{R.current=t}}),onSelectOption:u,commandPriority:a})}export{D as LexicalContextMenuPlugin,R as MenuOption};
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
|
-
import type { MenuResolution } from './shared/LexicalMenu';
|
|
8
|
+
import type { MenuRenderFn, MenuResolution } from './shared/LexicalMenu';
|
|
9
9
|
import type { JSX } from 'react';
|
|
10
10
|
import { CommandListenerPriority, NodeKey, TextNode } from 'lexical';
|
|
11
11
|
import { MenuOption } from './shared/LexicalMenu';
|
|
@@ -15,9 +15,10 @@ export type NodeMenuPluginProps<TOption extends MenuOption> = {
|
|
|
15
15
|
nodeKey: NodeKey | null;
|
|
16
16
|
onClose?: () => void;
|
|
17
17
|
onOpen?: (resolution: MenuResolution) => void;
|
|
18
|
+
menuRenderFn: MenuRenderFn<TOption>;
|
|
18
19
|
anchorClassName?: string;
|
|
19
20
|
commandPriority?: CommandListenerPriority;
|
|
20
21
|
parent?: HTMLElement;
|
|
21
22
|
};
|
|
22
|
-
export declare function LexicalNodeMenuPlugin<TOption extends MenuOption>({ options, nodeKey, onClose, onOpen, onSelectOption, anchorClassName, commandPriority, parent, }: NodeMenuPluginProps<TOption>): JSX.Element | null;
|
|
23
|
-
export { MenuOption, MenuResolution };
|
|
23
|
+
export declare function LexicalNodeMenuPlugin<TOption extends MenuOption>({ options, nodeKey, onClose, onOpen, onSelectOption, menuRenderFn, anchorClassName, commandPriority, parent, }: NodeMenuPluginProps<TOption>): JSX.Element | null;
|
|
24
|
+
export { MenuOption, MenuRenderFn, MenuResolution };
|