@lexical/react 0.13.0 → 0.14.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.dev.esm.js +131 -0
- package/LexicalAutoEmbedPlugin.esm.js +13 -0
- package/LexicalAutoEmbedPlugin.js +1 -1
- package/LexicalAutoEmbedPlugin.prod.esm.js +7 -0
- package/LexicalAutoFocusPlugin.dev.esm.js +42 -0
- package/LexicalAutoFocusPlugin.dev.js +0 -5
- package/LexicalAutoFocusPlugin.esm.js +10 -0
- package/LexicalAutoFocusPlugin.js +1 -1
- package/LexicalAutoFocusPlugin.prod.esm.js +7 -0
- package/LexicalAutoFocusPlugin.prod.js +1 -1
- package/LexicalAutoLinkPlugin.dev.esm.js +312 -0
- package/LexicalAutoLinkPlugin.dev.js +3 -1
- package/LexicalAutoLinkPlugin.esm.js +11 -0
- package/LexicalAutoLinkPlugin.js +1 -1
- package/LexicalAutoLinkPlugin.prod.esm.js +7 -0
- package/LexicalBlockWithAlignableContents.dev.esm.js +86 -0
- package/LexicalBlockWithAlignableContents.dev.js +1 -0
- package/LexicalBlockWithAlignableContents.esm.js +10 -0
- package/LexicalBlockWithAlignableContents.js +1 -1
- package/LexicalBlockWithAlignableContents.prod.esm.js +7 -0
- package/LexicalBlockWithAlignableContents.prod.js +1 -1
- package/LexicalCharacterLimitPlugin.d.ts +4 -1
- package/LexicalCharacterLimitPlugin.dev.esm.js +272 -0
- package/LexicalCharacterLimitPlugin.dev.js +12 -4
- package/LexicalCharacterLimitPlugin.esm.js +10 -0
- package/LexicalCharacterLimitPlugin.js +1 -1
- package/LexicalCharacterLimitPlugin.prod.esm.js +7 -0
- package/LexicalCharacterLimitPlugin.prod.js +9 -9
- package/LexicalCheckListPlugin.dev.esm.js +203 -0
- package/LexicalCheckListPlugin.esm.js +10 -0
- package/LexicalCheckListPlugin.js +1 -1
- package/LexicalCheckListPlugin.prod.esm.js +7 -0
- package/LexicalClearEditorPlugin.dev.esm.js +64 -0
- package/LexicalClearEditorPlugin.esm.js +10 -0
- package/LexicalClearEditorPlugin.js +1 -1
- package/LexicalClearEditorPlugin.prod.esm.js +7 -0
- package/LexicalClickableLinkPlugin.dev.esm.js +95 -0
- package/LexicalClickableLinkPlugin.esm.js +10 -0
- package/LexicalClickableLinkPlugin.js +1 -1
- package/LexicalClickableLinkPlugin.prod.esm.js +7 -0
- package/LexicalCollaborationContext.dev.esm.js +36 -0
- package/LexicalCollaborationContext.esm.js +11 -0
- package/LexicalCollaborationContext.js +1 -1
- package/LexicalCollaborationContext.prod.esm.js +7 -0
- package/LexicalCollaborationPlugin.dev.esm.js +308 -0
- package/LexicalCollaborationPlugin.esm.js +10 -0
- package/LexicalCollaborationPlugin.js +1 -1
- package/LexicalCollaborationPlugin.prod.esm.js +7 -0
- package/LexicalComposer.dev.esm.js +129 -0
- package/LexicalComposer.esm.js +10 -0
- package/LexicalComposer.js +1 -1
- package/LexicalComposer.prod.esm.js +7 -0
- package/LexicalComposerContext.dev.esm.js +42 -0
- package/LexicalComposerContext.esm.js +12 -0
- package/LexicalComposerContext.js +1 -1
- package/LexicalComposerContext.prod.esm.js +7 -0
- package/LexicalContentEditable.dev.esm.js +107 -0
- package/LexicalContentEditable.dev.js +5 -1
- package/LexicalContentEditable.esm.js +10 -0
- package/LexicalContentEditable.js +1 -1
- package/LexicalContentEditable.prod.esm.js +7 -0
- package/LexicalContentEditable.prod.js +3 -3
- package/LexicalContextMenuPlugin.dev.esm.js +455 -0
- package/LexicalContextMenuPlugin.dev.js +3 -1
- package/LexicalContextMenuPlugin.esm.js +11 -0
- package/LexicalContextMenuPlugin.js +1 -1
- package/LexicalContextMenuPlugin.prod.esm.js +7 -0
- package/LexicalDecoratorBlockNode.dev.esm.js +46 -0
- package/LexicalDecoratorBlockNode.esm.js +11 -0
- package/LexicalDecoratorBlockNode.js +1 -1
- package/LexicalDecoratorBlockNode.prod.esm.js +7 -0
- package/LexicalEditorRefPlugin.dev.esm.js +40 -0
- package/LexicalEditorRefPlugin.dev.js +10 -5
- package/LexicalEditorRefPlugin.esm.js +10 -0
- package/LexicalEditorRefPlugin.js +1 -1
- package/LexicalEditorRefPlugin.prod.esm.js +7 -0
- package/LexicalEditorRefPlugin.prod.js +1 -1
- package/LexicalErrorBoundary.dev.esm.js +157 -0
- package/LexicalErrorBoundary.esm.js +10 -0
- package/LexicalErrorBoundary.js +1 -1
- package/LexicalErrorBoundary.prod.esm.js +7 -0
- package/LexicalHashtagPlugin.dev.esm.js +163 -0
- package/LexicalHashtagPlugin.esm.js +10 -0
- package/LexicalHashtagPlugin.js +1 -1
- package/LexicalHashtagPlugin.prod.esm.js +7 -0
- package/LexicalHistoryPlugin.dev.esm.js +41 -0
- package/LexicalHistoryPlugin.esm.js +11 -0
- package/LexicalHistoryPlugin.js +1 -1
- package/LexicalHistoryPlugin.prod.esm.js +7 -0
- package/LexicalHorizontalRuleNode.dev.esm.js +118 -0
- package/LexicalHorizontalRuleNode.dev.js +1 -0
- package/LexicalHorizontalRuleNode.esm.js +13 -0
- package/LexicalHorizontalRuleNode.js +1 -1
- package/LexicalHorizontalRuleNode.prod.esm.js +7 -0
- package/LexicalHorizontalRuleNode.prod.js +1 -1
- package/LexicalHorizontalRulePlugin.dev.esm.js +39 -0
- package/LexicalHorizontalRulePlugin.esm.js +10 -0
- package/LexicalHorizontalRulePlugin.js +1 -1
- package/LexicalHorizontalRulePlugin.prod.esm.js +7 -0
- package/LexicalLinkPlugin.dev.esm.js +79 -0
- package/LexicalLinkPlugin.dev.js +6 -2
- package/LexicalLinkPlugin.esm.js +10 -0
- package/LexicalLinkPlugin.js +1 -1
- package/LexicalLinkPlugin.prod.esm.js +7 -0
- package/LexicalLinkPlugin.prod.js +1 -1
- package/LexicalListPlugin.dev.esm.js +59 -0
- package/LexicalListPlugin.esm.js +10 -0
- package/LexicalListPlugin.js +1 -1
- package/LexicalListPlugin.prod.esm.js +7 -0
- package/LexicalMarkdownShortcutPlugin.dev.esm.js +49 -0
- package/LexicalMarkdownShortcutPlugin.esm.js +11 -0
- package/LexicalMarkdownShortcutPlugin.js +1 -1
- package/LexicalMarkdownShortcutPlugin.prod.esm.js +7 -0
- package/LexicalNestedComposer.dev.esm.js +105 -0
- package/LexicalNestedComposer.esm.js +10 -0
- package/LexicalNestedComposer.js +1 -1
- package/LexicalNestedComposer.prod.esm.js +7 -0
- package/LexicalNodeEventPlugin.dev.esm.js +56 -0
- package/LexicalNodeEventPlugin.esm.js +10 -0
- package/LexicalNodeEventPlugin.js +1 -1
- package/LexicalNodeEventPlugin.prod.esm.js +7 -0
- package/LexicalNodeMenuPlugin.dev.esm.js +466 -0
- package/LexicalNodeMenuPlugin.dev.js +3 -1
- package/LexicalNodeMenuPlugin.esm.js +11 -0
- package/LexicalNodeMenuPlugin.js +1 -1
- package/LexicalNodeMenuPlugin.prod.esm.js +7 -0
- package/LexicalOnChangePlugin.dev.esm.js +62 -0
- package/LexicalOnChangePlugin.esm.js +10 -0
- package/LexicalOnChangePlugin.js +1 -1
- package/LexicalOnChangePlugin.prod.esm.js +7 -0
- package/LexicalPlainTextPlugin.dev.esm.js +161 -0
- package/LexicalPlainTextPlugin.esm.js +10 -0
- package/LexicalPlainTextPlugin.js +1 -1
- package/LexicalPlainTextPlugin.prod.esm.js +7 -0
- package/LexicalRichTextPlugin.dev.esm.js +161 -0
- package/LexicalRichTextPlugin.esm.js +10 -0
- package/LexicalRichTextPlugin.js +1 -1
- package/LexicalRichTextPlugin.prod.esm.js +7 -0
- package/LexicalTabIndentationPlugin.dev.esm.js +76 -0
- package/LexicalTabIndentationPlugin.esm.js +11 -0
- package/LexicalTabIndentationPlugin.js +1 -1
- package/LexicalTabIndentationPlugin.prod.esm.js +7 -0
- package/LexicalTableOfContents.dev.esm.js +157 -0
- package/LexicalTableOfContents.esm.js +10 -0
- package/LexicalTableOfContents.js +1 -1
- package/LexicalTableOfContents.prod.esm.js +7 -0
- package/LexicalTablePlugin.dev.esm.js +163 -0
- package/LexicalTablePlugin.esm.js +10 -0
- package/LexicalTablePlugin.js +1 -1
- package/LexicalTablePlugin.prod.esm.js +7 -0
- package/LexicalTreeView.dev.esm.js +483 -0
- package/LexicalTreeView.dev.js +3 -1
- package/LexicalTreeView.esm.js +10 -0
- package/LexicalTreeView.js +1 -1
- package/LexicalTreeView.prod.esm.js +7 -0
- package/LexicalTypeaheadMenuPlugin.dev.esm.js +569 -0
- package/LexicalTypeaheadMenuPlugin.dev.js +3 -1
- package/LexicalTypeaheadMenuPlugin.esm.js +16 -0
- package/LexicalTypeaheadMenuPlugin.js +1 -1
- package/LexicalTypeaheadMenuPlugin.prod.esm.js +7 -0
- package/package.json +583 -20
- package/useLexicalEditable.dev.esm.js +82 -0
- package/useLexicalEditable.esm.js +10 -0
- package/useLexicalEditable.js +1 -1
- package/useLexicalEditable.prod.esm.js +7 -0
- package/useLexicalIsTextContentEmpty.dev.esm.js +51 -0
- package/useLexicalIsTextContentEmpty.esm.js +10 -0
- package/useLexicalIsTextContentEmpty.js +1 -1
- package/useLexicalIsTextContentEmpty.prod.esm.js +7 -0
- package/useLexicalNodeSelection.dev.esm.js +69 -0
- package/useLexicalNodeSelection.esm.js +10 -0
- package/useLexicalNodeSelection.js +1 -1
- package/useLexicalNodeSelection.prod.esm.js +7 -0
- package/useLexicalSubscription.dev.esm.js +63 -0
- package/useLexicalSubscription.esm.js +10 -0
- package/useLexicalSubscription.js +1 -1
- package/useLexicalSubscription.prod.esm.js +7 -0
- package/useLexicalTextEntity.dev.esm.js +26 -0
- package/useLexicalTextEntity.esm.js +10 -0
- package/useLexicalTextEntity.js +1 -1
- package/useLexicalTextEntity.prod.esm.js +7 -0
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
import * as modDev from './LexicalNestedComposer.dev.esm.js';
|
|
8
|
+
import * as modProd from './LexicalNestedComposer.prod.esm.js';
|
|
9
|
+
const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
|
|
10
|
+
export const LexicalNestedComposer = mod.LexicalNestedComposer;
|
package/LexicalNestedComposer.js
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
'use strict'
|
|
8
|
-
const LexicalNestedComposer = process.env.NODE_ENV === 'development' ? require('./LexicalNestedComposer.dev.js') : require('./LexicalNestedComposer.prod.js')
|
|
8
|
+
const LexicalNestedComposer = process.env.NODE_ENV === 'development' ? require('./LexicalNestedComposer.dev.js') : require('./LexicalNestedComposer.prod.js');
|
|
9
9
|
module.exports = LexicalNestedComposer;
|
|
@@ -0,0 +1,7 @@
|
|
|
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
|
+
import{useCollaborationContext as e}from"@lexical/react/LexicalCollaborationContext";import{LexicalComposerContext as t,createLexicalComposerContext as r}from"@lexical/react/LexicalComposerContext";import*as o from"react";import{useRef as n,useContext as i,useMemo as l,useEffect as a}from"react";var s=function(e){const t=new URLSearchParams;t.append("code",e);for(let e=1;e<arguments.length;e++)t.append("v",arguments[e]);throw Error(`Minified Lexical error #${e}; visit https://lexical.dev/docs/error?${t} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)};function c({initialEditor:c,children:p,initialNodes:d,initialTheme:f,skipCollabChecks:m}){const h=n(!1),u=i(t);null==u&&s(9);const[x,{getTheme:g}]=u,v=l((()=>{const e=f||g()||void 0,t=r(u,e);if(void 0!==e&&(c._config.theme=e),c._parentEditor=x,d)for(let e of d){let t=null,r=null;if("function"!=typeof e){const o=e;e=o.replace,t=o.with,r=o.withKlass||null}const o=c._nodes.get(e.getType());c._nodes.set(e.getType(),{exportDOM:o?o.exportDOM:void 0,klass:e,replace:t,replaceWithKlass:r,transforms:new Set})}else{const e=c._nodes=new Map(x._nodes);for(const[t,r]of e)c._nodes.set(t,{exportDOM:r.exportDOM,klass:r.klass,replace:r.replace,replaceWithKlass:r.replaceWithKlass,transforms:new Set})}return c._config.namespace=x._config.namespace,c._editable=x._editable,[c,t]}),[]),{isCollabActive:_,yjsDocMap:w}=e(),b=m||h.current||w.has(c.getKey());return a((()=>{b&&(h.current=!0)}),[b]),a((()=>x.registerEditableListener((e=>{c.setEditable(e)}))),[c,x]),o.createElement(t.Provider,{value:v},!_||b?p:null)}export{c as LexicalNestedComposer};
|
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
8
|
+
import { $findMatchingParent } from '@lexical/utils';
|
|
9
|
+
import { $getNearestNodeFromDOMNode } from 'lexical';
|
|
10
|
+
import { useRef, useEffect } from 'react';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
14
|
+
*
|
|
15
|
+
* This source code is licensed under the MIT license found in the
|
|
16
|
+
* LICENSE file in the root directory of this source tree.
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
const capturedEvents = new Set(['mouseenter', 'mouseleave']);
|
|
20
|
+
function NodeEventPlugin({
|
|
21
|
+
nodeType,
|
|
22
|
+
eventType,
|
|
23
|
+
eventListener
|
|
24
|
+
}) {
|
|
25
|
+
const [editor] = useLexicalComposerContext();
|
|
26
|
+
const listenerRef = useRef(eventListener);
|
|
27
|
+
listenerRef.current = eventListener;
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
const isCaptured = capturedEvents.has(eventType);
|
|
30
|
+
const onEvent = event => {
|
|
31
|
+
editor.update(() => {
|
|
32
|
+
const nearestNode = $getNearestNodeFromDOMNode(event.target);
|
|
33
|
+
if (nearestNode !== null) {
|
|
34
|
+
const targetNode = isCaptured ? nearestNode instanceof nodeType ? nearestNode : null : $findMatchingParent(nearestNode, node => node instanceof nodeType);
|
|
35
|
+
if (targetNode !== null) {
|
|
36
|
+
listenerRef.current(event, editor, targetNode.getKey());
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
return editor.registerRootListener((rootElement, prevRootElement) => {
|
|
43
|
+
if (rootElement) {
|
|
44
|
+
rootElement.addEventListener(eventType, onEvent, isCaptured);
|
|
45
|
+
}
|
|
46
|
+
if (prevRootElement) {
|
|
47
|
+
prevRootElement.removeEventListener(eventType, onEvent, isCaptured);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
// We intentionally don't respect changes to eventType.
|
|
51
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
52
|
+
}, [editor, nodeType]);
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export { NodeEventPlugin };
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
import * as modDev from './LexicalNodeEventPlugin.dev.esm.js';
|
|
8
|
+
import * as modProd from './LexicalNodeEventPlugin.prod.esm.js';
|
|
9
|
+
const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
|
|
10
|
+
export const NodeEventPlugin = mod.NodeEventPlugin;
|
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
'use strict'
|
|
8
|
-
const LexicalNodeEventPlugin = process.env.NODE_ENV === 'development' ? require('./LexicalNodeEventPlugin.dev.js') : require('./LexicalNodeEventPlugin.prod.js')
|
|
8
|
+
const LexicalNodeEventPlugin = process.env.NODE_ENV === 'development' ? require('./LexicalNodeEventPlugin.dev.js') : require('./LexicalNodeEventPlugin.prod.js');
|
|
9
9
|
module.exports = LexicalNodeEventPlugin;
|
|
@@ -0,0 +1,7 @@
|
|
|
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
|
+
import{useLexicalComposerContext as e}from"@lexical/react/LexicalComposerContext";import{$findMatchingParent as t}from"@lexical/utils";import{$getNearestNodeFromDOMNode as n}from"lexical";import{useRef as r,useEffect as o}from"react";const i=new Set(["mouseenter","mouseleave"]);function l({nodeType:l,eventType:s,eventListener:c}){const[u]=e(),a=r(c);return a.current=c,o((()=>{const e=i.has(s),r=r=>{u.update((()=>{const o=n(r.target);if(null!==o){const n=e?o instanceof l?o:null:t(o,(e=>e instanceof l));if(null!==n)return void a.current(r,u,n.getKey())}}))};return u.registerRootListener(((t,n)=>{t&&t.addEventListener(s,r,e),n&&n.removeEventListener(s,r,e)}))}),[u,l]),null}export{l as NodeEventPlugin};
|
|
@@ -0,0 +1,466 @@
|
|
|
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
|
+
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
8
|
+
import { createCommand, KEY_ARROW_DOWN_COMMAND, KEY_ARROW_UP_COMMAND, KEY_ESCAPE_COMMAND, KEY_TAB_COMMAND, KEY_ENTER_COMMAND, COMMAND_PRIORITY_LOW, $getSelection, $isRangeSelection, $getNodeByKey } from 'lexical';
|
|
9
|
+
import * as React from 'react';
|
|
10
|
+
import { useLayoutEffect as useLayoutEffect$1, useEffect, useState, useCallback, useMemo, useRef } from 'react';
|
|
11
|
+
import { mergeRegister } from '@lexical/utils';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
15
|
+
*
|
|
16
|
+
* This source code is licensed under the MIT license found in the
|
|
17
|
+
* LICENSE file in the root directory of this source tree.
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
25
|
+
*
|
|
26
|
+
* This source code is licensed under the MIT license found in the
|
|
27
|
+
* LICENSE file in the root directory of this source tree.
|
|
28
|
+
*
|
|
29
|
+
*/
|
|
30
|
+
const useLayoutEffectImpl = CAN_USE_DOM ? useLayoutEffect$1 : useEffect;
|
|
31
|
+
var useLayoutEffect = useLayoutEffectImpl;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
35
|
+
*
|
|
36
|
+
* This source code is licensed under the MIT license found in the
|
|
37
|
+
* LICENSE file in the root directory of this source tree.
|
|
38
|
+
*
|
|
39
|
+
*/
|
|
40
|
+
class MenuOption {
|
|
41
|
+
constructor(key) {
|
|
42
|
+
this.key = key;
|
|
43
|
+
this.ref = {
|
|
44
|
+
current: null
|
|
45
|
+
};
|
|
46
|
+
this.setRefElement = this.setRefElement.bind(this);
|
|
47
|
+
}
|
|
48
|
+
setRefElement(element) {
|
|
49
|
+
this.ref = {
|
|
50
|
+
current: element
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const scrollIntoViewIfNeeded = target => {
|
|
55
|
+
const typeaheadContainerNode = document.getElementById('typeahead-menu');
|
|
56
|
+
if (!typeaheadContainerNode) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const typeaheadRect = typeaheadContainerNode.getBoundingClientRect();
|
|
60
|
+
if (typeaheadRect.top + typeaheadRect.height > window.innerHeight) {
|
|
61
|
+
typeaheadContainerNode.scrollIntoView({
|
|
62
|
+
block: 'center'
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
if (typeaheadRect.top < 0) {
|
|
66
|
+
typeaheadContainerNode.scrollIntoView({
|
|
67
|
+
block: 'center'
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
target.scrollIntoView({
|
|
71
|
+
block: 'nearest'
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Walk backwards along user input and forward through entity title to try
|
|
77
|
+
* and replace more of the user's text with entity.
|
|
78
|
+
*/
|
|
79
|
+
function getFullMatchOffset(documentText, entryText, offset) {
|
|
80
|
+
let triggerOffset = offset;
|
|
81
|
+
for (let i = triggerOffset; i <= entryText.length; i++) {
|
|
82
|
+
if (documentText.substr(-i) === entryText.substr(0, i)) {
|
|
83
|
+
triggerOffset = i;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return triggerOffset;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Split Lexical TextNode and return a new TextNode only containing matched text.
|
|
91
|
+
* Common use cases include: removing the node, replacing with a new node.
|
|
92
|
+
*/
|
|
93
|
+
function $splitNodeContainingQuery(match) {
|
|
94
|
+
const selection = $getSelection();
|
|
95
|
+
if (!$isRangeSelection(selection) || !selection.isCollapsed()) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
const anchor = selection.anchor;
|
|
99
|
+
if (anchor.type !== 'text') {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
const anchorNode = anchor.getNode();
|
|
103
|
+
if (!anchorNode.isSimpleText()) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
const selectionOffset = anchor.offset;
|
|
107
|
+
const textContent = anchorNode.getTextContent().slice(0, selectionOffset);
|
|
108
|
+
const characterOffset = match.replaceableString.length;
|
|
109
|
+
const queryOffset = getFullMatchOffset(textContent, match.matchingString, characterOffset);
|
|
110
|
+
const startOffset = selectionOffset - queryOffset;
|
|
111
|
+
if (startOffset < 0) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
let newNode;
|
|
115
|
+
if (startOffset === 0) {
|
|
116
|
+
[newNode] = anchorNode.splitText(selectionOffset);
|
|
117
|
+
} else {
|
|
118
|
+
[, newNode] = anchorNode.splitText(startOffset, selectionOffset);
|
|
119
|
+
}
|
|
120
|
+
return newNode;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Got from https://stackoverflow.com/a/42543908/2013580
|
|
124
|
+
function getScrollParent(element, includeHidden) {
|
|
125
|
+
let style = getComputedStyle(element);
|
|
126
|
+
const excludeStaticParent = style.position === 'absolute';
|
|
127
|
+
const overflowRegex = includeHidden ? /(auto|scroll|hidden)/ : /(auto|scroll)/;
|
|
128
|
+
if (style.position === 'fixed') {
|
|
129
|
+
return document.body;
|
|
130
|
+
}
|
|
131
|
+
for (let parent = element; parent = parent.parentElement;) {
|
|
132
|
+
style = getComputedStyle(parent);
|
|
133
|
+
if (excludeStaticParent && style.position === 'static') {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (overflowRegex.test(style.overflow + style.overflowY + style.overflowX)) {
|
|
137
|
+
return parent;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return document.body;
|
|
141
|
+
}
|
|
142
|
+
function isTriggerVisibleInNearestScrollContainer(targetElement, containerElement) {
|
|
143
|
+
const tRect = targetElement.getBoundingClientRect();
|
|
144
|
+
const cRect = containerElement.getBoundingClientRect();
|
|
145
|
+
return tRect.top > cRect.top && tRect.top < cRect.bottom;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Reposition the menu on scroll, window resize, and element resize.
|
|
149
|
+
function useDynamicPositioning(resolution, targetElement, onReposition, onVisibilityChange) {
|
|
150
|
+
const [editor] = useLexicalComposerContext();
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
if (targetElement != null && resolution != null) {
|
|
153
|
+
const rootElement = editor.getRootElement();
|
|
154
|
+
const rootScrollParent = rootElement != null ? getScrollParent(rootElement, false) : document.body;
|
|
155
|
+
let ticking = false;
|
|
156
|
+
let previousIsInView = isTriggerVisibleInNearestScrollContainer(targetElement, rootScrollParent);
|
|
157
|
+
const handleScroll = function () {
|
|
158
|
+
if (!ticking) {
|
|
159
|
+
window.requestAnimationFrame(function () {
|
|
160
|
+
onReposition();
|
|
161
|
+
ticking = false;
|
|
162
|
+
});
|
|
163
|
+
ticking = true;
|
|
164
|
+
}
|
|
165
|
+
const isInView = isTriggerVisibleInNearestScrollContainer(targetElement, rootScrollParent);
|
|
166
|
+
if (isInView !== previousIsInView) {
|
|
167
|
+
previousIsInView = isInView;
|
|
168
|
+
if (onVisibilityChange != null) {
|
|
169
|
+
onVisibilityChange(isInView);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
const resizeObserver = new ResizeObserver(onReposition);
|
|
174
|
+
window.addEventListener('resize', onReposition);
|
|
175
|
+
document.addEventListener('scroll', handleScroll, {
|
|
176
|
+
capture: true,
|
|
177
|
+
passive: true
|
|
178
|
+
});
|
|
179
|
+
resizeObserver.observe(targetElement);
|
|
180
|
+
return () => {
|
|
181
|
+
resizeObserver.unobserve(targetElement);
|
|
182
|
+
window.removeEventListener('resize', onReposition);
|
|
183
|
+
document.removeEventListener('scroll', handleScroll, true);
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}, [targetElement, editor, onVisibilityChange, onReposition, resolution]);
|
|
187
|
+
}
|
|
188
|
+
const SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND = createCommand('SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND');
|
|
189
|
+
function LexicalMenu({
|
|
190
|
+
close,
|
|
191
|
+
editor,
|
|
192
|
+
anchorElementRef,
|
|
193
|
+
resolution,
|
|
194
|
+
options,
|
|
195
|
+
menuRenderFn,
|
|
196
|
+
onSelectOption,
|
|
197
|
+
shouldSplitNodeWithQuery = false,
|
|
198
|
+
commandPriority = COMMAND_PRIORITY_LOW
|
|
199
|
+
}) {
|
|
200
|
+
const [selectedIndex, setHighlightedIndex] = useState(null);
|
|
201
|
+
const matchingString = resolution.match && resolution.match.matchingString;
|
|
202
|
+
useEffect(() => {
|
|
203
|
+
setHighlightedIndex(0);
|
|
204
|
+
}, [matchingString]);
|
|
205
|
+
const selectOptionAndCleanUp = useCallback(selectedEntry => {
|
|
206
|
+
editor.update(() => {
|
|
207
|
+
const textNodeContainingQuery = resolution.match != null && shouldSplitNodeWithQuery ? $splitNodeContainingQuery(resolution.match) : null;
|
|
208
|
+
onSelectOption(selectedEntry, textNodeContainingQuery, close, resolution.match ? resolution.match.matchingString : '');
|
|
209
|
+
});
|
|
210
|
+
}, [editor, shouldSplitNodeWithQuery, resolution.match, onSelectOption, close]);
|
|
211
|
+
const updateSelectedIndex = useCallback(index => {
|
|
212
|
+
const rootElem = editor.getRootElement();
|
|
213
|
+
if (rootElem !== null) {
|
|
214
|
+
rootElem.setAttribute('aria-activedescendant', 'typeahead-item-' + index);
|
|
215
|
+
setHighlightedIndex(index);
|
|
216
|
+
}
|
|
217
|
+
}, [editor]);
|
|
218
|
+
useEffect(() => {
|
|
219
|
+
return () => {
|
|
220
|
+
const rootElem = editor.getRootElement();
|
|
221
|
+
if (rootElem !== null) {
|
|
222
|
+
rootElem.removeAttribute('aria-activedescendant');
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
}, [editor]);
|
|
226
|
+
useLayoutEffect(() => {
|
|
227
|
+
if (options === null) {
|
|
228
|
+
setHighlightedIndex(null);
|
|
229
|
+
} else if (selectedIndex === null) {
|
|
230
|
+
updateSelectedIndex(0);
|
|
231
|
+
}
|
|
232
|
+
}, [options, selectedIndex, updateSelectedIndex]);
|
|
233
|
+
useEffect(() => {
|
|
234
|
+
return mergeRegister(editor.registerCommand(SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND, ({
|
|
235
|
+
option
|
|
236
|
+
}) => {
|
|
237
|
+
if (option.ref && option.ref.current != null) {
|
|
238
|
+
scrollIntoViewIfNeeded(option.ref.current);
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
241
|
+
return false;
|
|
242
|
+
}, commandPriority));
|
|
243
|
+
}, [editor, updateSelectedIndex, commandPriority]);
|
|
244
|
+
useEffect(() => {
|
|
245
|
+
return mergeRegister(editor.registerCommand(KEY_ARROW_DOWN_COMMAND, payload => {
|
|
246
|
+
const event = payload;
|
|
247
|
+
if (options !== null && options.length && selectedIndex !== null) {
|
|
248
|
+
const newSelectedIndex = selectedIndex !== options.length - 1 ? selectedIndex + 1 : 0;
|
|
249
|
+
updateSelectedIndex(newSelectedIndex);
|
|
250
|
+
const option = options[newSelectedIndex];
|
|
251
|
+
if (option.ref != null && option.ref.current) {
|
|
252
|
+
editor.dispatchCommand(SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND, {
|
|
253
|
+
index: newSelectedIndex,
|
|
254
|
+
option
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
event.preventDefault();
|
|
258
|
+
event.stopImmediatePropagation();
|
|
259
|
+
}
|
|
260
|
+
return true;
|
|
261
|
+
}, commandPriority), editor.registerCommand(KEY_ARROW_UP_COMMAND, payload => {
|
|
262
|
+
const event = payload;
|
|
263
|
+
if (options !== null && options.length && selectedIndex !== null) {
|
|
264
|
+
const newSelectedIndex = selectedIndex !== 0 ? selectedIndex - 1 : options.length - 1;
|
|
265
|
+
updateSelectedIndex(newSelectedIndex);
|
|
266
|
+
const option = options[newSelectedIndex];
|
|
267
|
+
if (option.ref != null && option.ref.current) {
|
|
268
|
+
scrollIntoViewIfNeeded(option.ref.current);
|
|
269
|
+
}
|
|
270
|
+
event.preventDefault();
|
|
271
|
+
event.stopImmediatePropagation();
|
|
272
|
+
}
|
|
273
|
+
return true;
|
|
274
|
+
}, commandPriority), editor.registerCommand(KEY_ESCAPE_COMMAND, payload => {
|
|
275
|
+
const event = payload;
|
|
276
|
+
event.preventDefault();
|
|
277
|
+
event.stopImmediatePropagation();
|
|
278
|
+
close();
|
|
279
|
+
return true;
|
|
280
|
+
}, commandPriority), editor.registerCommand(KEY_TAB_COMMAND, payload => {
|
|
281
|
+
const event = payload;
|
|
282
|
+
if (options === null || selectedIndex === null || options[selectedIndex] == null) {
|
|
283
|
+
return false;
|
|
284
|
+
}
|
|
285
|
+
event.preventDefault();
|
|
286
|
+
event.stopImmediatePropagation();
|
|
287
|
+
selectOptionAndCleanUp(options[selectedIndex]);
|
|
288
|
+
return true;
|
|
289
|
+
}, commandPriority), editor.registerCommand(KEY_ENTER_COMMAND, event => {
|
|
290
|
+
if (options === null || selectedIndex === null || options[selectedIndex] == null) {
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
if (event !== null) {
|
|
294
|
+
event.preventDefault();
|
|
295
|
+
event.stopImmediatePropagation();
|
|
296
|
+
}
|
|
297
|
+
selectOptionAndCleanUp(options[selectedIndex]);
|
|
298
|
+
return true;
|
|
299
|
+
}, commandPriority));
|
|
300
|
+
}, [selectOptionAndCleanUp, close, editor, options, selectedIndex, updateSelectedIndex, commandPriority]);
|
|
301
|
+
const listItemProps = useMemo(() => ({
|
|
302
|
+
options,
|
|
303
|
+
selectOptionAndCleanUp,
|
|
304
|
+
selectedIndex,
|
|
305
|
+
setHighlightedIndex
|
|
306
|
+
}), [selectOptionAndCleanUp, selectedIndex, options]);
|
|
307
|
+
return menuRenderFn(anchorElementRef, listItemProps, resolution.match ? resolution.match.matchingString : '');
|
|
308
|
+
}
|
|
309
|
+
function useMenuAnchorRef(resolution, setResolution, className, parent = document.body) {
|
|
310
|
+
const [editor] = useLexicalComposerContext();
|
|
311
|
+
const anchorElementRef = useRef(document.createElement('div'));
|
|
312
|
+
const positionMenu = useCallback(() => {
|
|
313
|
+
anchorElementRef.current.style.top = anchorElementRef.current.style.bottom;
|
|
314
|
+
const rootElement = editor.getRootElement();
|
|
315
|
+
const containerDiv = anchorElementRef.current;
|
|
316
|
+
const menuEle = containerDiv.firstChild;
|
|
317
|
+
if (rootElement !== null && resolution !== null) {
|
|
318
|
+
const {
|
|
319
|
+
left,
|
|
320
|
+
top,
|
|
321
|
+
width,
|
|
322
|
+
height
|
|
323
|
+
} = resolution.getRect();
|
|
324
|
+
const anchorHeight = anchorElementRef.current.offsetHeight; // use to position under anchor
|
|
325
|
+
containerDiv.style.top = `${top + window.pageYOffset + anchorHeight + 3}px`;
|
|
326
|
+
containerDiv.style.left = `${left + window.pageXOffset}px`;
|
|
327
|
+
containerDiv.style.height = `${height}px`;
|
|
328
|
+
containerDiv.style.width = `${width}px`;
|
|
329
|
+
if (menuEle !== null) {
|
|
330
|
+
menuEle.style.top = `${top}`;
|
|
331
|
+
const menuRect = menuEle.getBoundingClientRect();
|
|
332
|
+
const menuHeight = menuRect.height;
|
|
333
|
+
const menuWidth = menuRect.width;
|
|
334
|
+
const rootElementRect = rootElement.getBoundingClientRect();
|
|
335
|
+
if (left + menuWidth > rootElementRect.right) {
|
|
336
|
+
containerDiv.style.left = `${rootElementRect.right - menuWidth + window.pageXOffset}px`;
|
|
337
|
+
}
|
|
338
|
+
if ((top + menuHeight > window.innerHeight || top + menuHeight > rootElementRect.bottom) && top - rootElementRect.top > menuHeight) {
|
|
339
|
+
containerDiv.style.top = `${top - menuHeight + window.pageYOffset - height}px`;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
if (!containerDiv.isConnected) {
|
|
343
|
+
if (className != null) {
|
|
344
|
+
containerDiv.className = className;
|
|
345
|
+
}
|
|
346
|
+
containerDiv.setAttribute('aria-label', 'Typeahead menu');
|
|
347
|
+
containerDiv.setAttribute('id', 'typeahead-menu');
|
|
348
|
+
containerDiv.setAttribute('role', 'listbox');
|
|
349
|
+
containerDiv.style.display = 'block';
|
|
350
|
+
containerDiv.style.position = 'absolute';
|
|
351
|
+
parent.append(containerDiv);
|
|
352
|
+
}
|
|
353
|
+
anchorElementRef.current = containerDiv;
|
|
354
|
+
rootElement.setAttribute('aria-controls', 'typeahead-menu');
|
|
355
|
+
}
|
|
356
|
+
}, [editor, resolution, className, parent]);
|
|
357
|
+
useEffect(() => {
|
|
358
|
+
const rootElement = editor.getRootElement();
|
|
359
|
+
if (resolution !== null) {
|
|
360
|
+
positionMenu();
|
|
361
|
+
return () => {
|
|
362
|
+
if (rootElement !== null) {
|
|
363
|
+
rootElement.removeAttribute('aria-controls');
|
|
364
|
+
}
|
|
365
|
+
const containerDiv = anchorElementRef.current;
|
|
366
|
+
if (containerDiv !== null && containerDiv.isConnected) {
|
|
367
|
+
containerDiv.remove();
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
}, [editor, positionMenu, resolution]);
|
|
372
|
+
const onVisibilityChange = useCallback(isInView => {
|
|
373
|
+
if (resolution !== null) {
|
|
374
|
+
if (!isInView) {
|
|
375
|
+
setResolution(null);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}, [resolution, setResolution]);
|
|
379
|
+
useDynamicPositioning(resolution, anchorElementRef.current, positionMenu, onVisibilityChange);
|
|
380
|
+
return anchorElementRef;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
385
|
+
*
|
|
386
|
+
* This source code is licensed under the MIT license found in the
|
|
387
|
+
* LICENSE file in the root directory of this source tree.
|
|
388
|
+
*
|
|
389
|
+
*/
|
|
390
|
+
function startTransition(callback) {
|
|
391
|
+
if (React.startTransition) {
|
|
392
|
+
React.startTransition(callback);
|
|
393
|
+
} else {
|
|
394
|
+
callback();
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
function LexicalNodeMenuPlugin({
|
|
398
|
+
options,
|
|
399
|
+
nodeKey,
|
|
400
|
+
onClose,
|
|
401
|
+
onOpen,
|
|
402
|
+
onSelectOption,
|
|
403
|
+
menuRenderFn,
|
|
404
|
+
anchorClassName,
|
|
405
|
+
commandPriority = COMMAND_PRIORITY_LOW,
|
|
406
|
+
parent
|
|
407
|
+
}) {
|
|
408
|
+
const [editor] = useLexicalComposerContext();
|
|
409
|
+
const [resolution, setResolution] = useState(null);
|
|
410
|
+
const anchorElementRef = useMenuAnchorRef(resolution, setResolution, anchorClassName, parent);
|
|
411
|
+
const closeNodeMenu = useCallback(() => {
|
|
412
|
+
setResolution(null);
|
|
413
|
+
if (onClose != null && resolution !== null) {
|
|
414
|
+
onClose();
|
|
415
|
+
}
|
|
416
|
+
}, [onClose, resolution]);
|
|
417
|
+
const openNodeMenu = useCallback(res => {
|
|
418
|
+
setResolution(res);
|
|
419
|
+
if (onOpen != null && resolution === null) {
|
|
420
|
+
onOpen(res);
|
|
421
|
+
}
|
|
422
|
+
}, [onOpen, resolution]);
|
|
423
|
+
const positionOrCloseMenu = useCallback(() => {
|
|
424
|
+
if (nodeKey) {
|
|
425
|
+
editor.update(() => {
|
|
426
|
+
const node = $getNodeByKey(nodeKey);
|
|
427
|
+
const domElement = editor.getElementByKey(nodeKey);
|
|
428
|
+
if (node != null && domElement != null) {
|
|
429
|
+
if (resolution == null) {
|
|
430
|
+
startTransition(() => openNodeMenu({
|
|
431
|
+
getRect: () => domElement.getBoundingClientRect()
|
|
432
|
+
}));
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
} else if (nodeKey == null && resolution != null) {
|
|
437
|
+
closeNodeMenu();
|
|
438
|
+
}
|
|
439
|
+
}, [closeNodeMenu, editor, nodeKey, openNodeMenu, resolution]);
|
|
440
|
+
useEffect(() => {
|
|
441
|
+
positionOrCloseMenu();
|
|
442
|
+
}, [positionOrCloseMenu, nodeKey]);
|
|
443
|
+
useEffect(() => {
|
|
444
|
+
if (nodeKey != null) {
|
|
445
|
+
return editor.registerUpdateListener(({
|
|
446
|
+
dirtyElements
|
|
447
|
+
}) => {
|
|
448
|
+
if (dirtyElements.get(nodeKey)) {
|
|
449
|
+
positionOrCloseMenu();
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
}, [editor, positionOrCloseMenu, nodeKey]);
|
|
454
|
+
return resolution === null || editor === null ? null : /*#__PURE__*/React.createElement(LexicalMenu, {
|
|
455
|
+
close: closeNodeMenu,
|
|
456
|
+
resolution: resolution,
|
|
457
|
+
editor: editor,
|
|
458
|
+
anchorElementRef: anchorElementRef,
|
|
459
|
+
options: options,
|
|
460
|
+
menuRenderFn: menuRenderFn,
|
|
461
|
+
onSelectOption: onSelectOption,
|
|
462
|
+
commandPriority: commandPriority
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
export { LexicalNodeMenuPlugin, MenuOption };
|
|
@@ -54,7 +54,9 @@ class MenuOption {
|
|
|
54
54
|
}
|
|
55
55
|
const scrollIntoViewIfNeeded = target => {
|
|
56
56
|
const typeaheadContainerNode = document.getElementById('typeahead-menu');
|
|
57
|
-
if (!typeaheadContainerNode)
|
|
57
|
+
if (!typeaheadContainerNode) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
58
60
|
const typeaheadRect = typeaheadContainerNode.getBoundingClientRect();
|
|
59
61
|
if (typeaheadRect.top + typeaheadRect.height > window.innerHeight) {
|
|
60
62
|
typeaheadContainerNode.scrollIntoView({
|
|
@@ -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
|
+
import * as modDev from './LexicalNodeMenuPlugin.dev.esm.js';
|
|
8
|
+
import * as modProd from './LexicalNodeMenuPlugin.prod.esm.js';
|
|
9
|
+
const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
|
|
10
|
+
export const LexicalNodeMenuPlugin = mod.LexicalNodeMenuPlugin;
|
|
11
|
+
export const MenuOption = mod.MenuOption;
|
package/LexicalNodeMenuPlugin.js
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
'use strict'
|
|
8
|
-
const LexicalNodeMenuPlugin = process.env.NODE_ENV === 'development' ? require('./LexicalNodeMenuPlugin.dev.js') : require('./LexicalNodeMenuPlugin.prod.js')
|
|
8
|
+
const LexicalNodeMenuPlugin = process.env.NODE_ENV === 'development' ? require('./LexicalNodeMenuPlugin.dev.js') : require('./LexicalNodeMenuPlugin.prod.js');
|
|
9
9
|
module.exports = LexicalNodeMenuPlugin;
|
|
@@ -0,0 +1,7 @@
|
|
|
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
|
+
import{useLexicalComposerContext as t}from"@lexical/react/LexicalComposerContext";import{createCommand as e,KEY_ARROW_DOWN_COMMAND as n,KEY_ARROW_UP_COMMAND as o,KEY_ESCAPE_COMMAND as l,KEY_TAB_COMMAND as r,KEY_ENTER_COMMAND as i,COMMAND_PRIORITY_LOW as s,$getSelection as u,$isRangeSelection as c,$getNodeByKey as a}from"lexical";import*as m from"react";import{useLayoutEffect as d,useEffect as p,useState as f,useCallback as g,useMemo as h,useRef as y}from"react";import{mergeRegister as w}from"@lexical/utils";var b="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement?d:p;class v{constructor(t){this.key=t,this.ref={current:null},this.setRefElement=this.setRefElement.bind(this)}setRefElement(t){this.ref={current:t}}}const C=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 E(t,e){const n=t.getBoundingClientRect(),o=e.getBoundingClientRect();return n.top>o.top&&n.top<o.bottom}function R(e,n,o,l){const[r]=t();p((()=>{if(null!=n&&null!=e){const t=r.getRootElement(),e=null!=t?function(t,e){let n=getComputedStyle(t);const o="absolute"===n.position,l=e?/(auto|scroll|hidden)/:/(auto|scroll)/;if("fixed"===n.position)return document.body;for(let e=t;e=e.parentElement;)if(n=getComputedStyle(e),(!o||"static"!==n.position)&&l.test(n.overflow+n.overflowY+n.overflowX))return e;return document.body}(t,!1):document.body;let i=!1,s=E(n,e);const u=function(){i||(window.requestAnimationFrame((function(){o(),i=!1})),i=!0);const t=E(n,e);t!==s&&(s=t,null!=l&&l(t))},c=new ResizeObserver(o);return window.addEventListener("resize",o),document.addEventListener("scroll",u,{capture:!0,passive:!0}),c.observe(n),()=>{c.unobserve(n),window.removeEventListener("resize",o),document.removeEventListener("scroll",u,!0)}}}),[n,r,l,o,e])}const x=e("SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND");function O({close:t,editor:e,anchorElementRef:a,resolution:m,options:d,menuRenderFn:y,onSelectOption:v,shouldSplitNodeWithQuery:E=!1,commandPriority:R=s}){const[O,I]=f(null),S=m.match&&m.match.matchingString;p((()=>{I(0)}),[S]);const A=g((n=>{e.update((()=>{const e=null!=m.match&&E?function(t){const e=u();if(!c(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,s=l-function(t,e,n){let o=n;for(let n=o;n<=e.length;n++)t.substr(-n)===e.substr(0,n)&&(o=n);return o}(r,t.matchingString,i);if(s<0)return null;let a;return 0===s?[a]=o.splitText(l):[,a]=o.splitText(s,l),a}(m.match):null;v(n,e,t,m.match?m.match.matchingString:"")}))}),[e,E,m.match,v,t]),P=g((t=>{const n=e.getRootElement();null!==n&&(n.setAttribute("aria-activedescendant","typeahead-item-"+t),I(t))}),[e]);p((()=>()=>{const t=e.getRootElement();null!==t&&t.removeAttribute("aria-activedescendant")}),[e]),b((()=>{null===d?I(null):null===O&&P(0)}),[d,O,P]),p((()=>w(e.registerCommand(x,(({option:t})=>!(!t.ref||null==t.ref.current)&&(C(t.ref.current),!0)),R))),[e,P,R]),p((()=>w(e.registerCommand(n,(t=>{const n=t;if(null!==d&&d.length&&null!==O){const t=O!==d.length-1?O+1:0;P(t);const o=d[t];null!=o.ref&&o.ref.current&&e.dispatchCommand(x,{index:t,option:o}),n.preventDefault(),n.stopImmediatePropagation()}return!0}),R),e.registerCommand(o,(t=>{const e=t;if(null!==d&&d.length&&null!==O){const t=0!==O?O-1:d.length-1;P(t);const n=d[t];null!=n.ref&&n.ref.current&&C(n.ref.current),e.preventDefault(),e.stopImmediatePropagation()}return!0}),R),e.registerCommand(l,(e=>{const n=e;return n.preventDefault(),n.stopImmediatePropagation(),t(),!0}),R),e.registerCommand(r,(t=>{const e=t;return null!==d&&null!==O&&null!=d[O]&&(e.preventDefault(),e.stopImmediatePropagation(),A(d[O]),!0)}),R),e.registerCommand(i,(t=>null!==d&&null!==O&&null!=d[O]&&(null!==t&&(t.preventDefault(),t.stopImmediatePropagation()),A(d[O]),!0)),R))),[A,t,e,d,O,P,R]);return y(a,h((()=>({options:d,selectOptionAndCleanUp:A,selectedIndex:O,setHighlightedIndex:I})),[A,O,d]),m.match?m.match.matchingString:"")}function I({options:e,nodeKey:n,onClose:o,onOpen:l,onSelectOption:r,menuRenderFn:i,anchorClassName:u,commandPriority:c=s,parent:d}){const[h]=t(),[w,b]=f(null),v=function(e,n,o,l=document.body){const[r]=t(),i=y(document.createElement("div")),s=g((()=>{i.current.style.top=i.current.style.bottom;const t=r.getRootElement(),n=i.current,s=n.firstChild;if(null!==t&&null!==e){const{left:r,top:u,width:c,height:a}=e.getRect(),m=i.current.offsetHeight;if(n.style.top=`${u+window.pageYOffset+m+3}px`,n.style.left=`${r+window.pageXOffset}px`,n.style.height=`${a}px`,n.style.width=`${c}px`,null!==s){s.style.top=`${u}`;const e=s.getBoundingClientRect(),o=e.height,l=e.width,i=t.getBoundingClientRect();r+l>i.right&&(n.style.left=`${i.right-l+window.pageXOffset}px`),(u+o>window.innerHeight||u+o>i.bottom)&&u-i.top>o&&(n.style.top=u-o+window.pageYOffset-a+"px")}n.isConnected||(null!=o&&(n.className=o),n.setAttribute("aria-label","Typeahead menu"),n.setAttribute("id","typeahead-menu"),n.setAttribute("role","listbox"),n.style.display="block",n.style.position="absolute",l.append(n)),i.current=n,t.setAttribute("aria-controls","typeahead-menu")}}),[r,e,o,l]);p((()=>{const t=r.getRootElement();if(null!==e)return s(),()=>{null!==t&&t.removeAttribute("aria-controls");const e=i.current;null!==e&&e.isConnected&&e.remove()}}),[r,s,e]);const u=g((t=>{null!==e&&(t||n(null))}),[e,n]);return R(e,i.current,s,u),i}(w,b,u,d),C=g((()=>{b(null),null!=o&&null!==w&&o()}),[o,w]),E=g((t=>{b(t),null!=l&&null===w&&l(t)}),[l,w]),x=g((()=>{n?h.update((()=>{const t=a(n),e=h.getElementByKey(n);var o;null!=t&&null!=e&&null==w&&(o=()=>E({getRect:()=>e.getBoundingClientRect()}),m.startTransition?m.startTransition(o):o())})):null==n&&null!=w&&C()}),[C,h,n,E,w]);return p((()=>{x()}),[x,n]),p((()=>{if(null!=n)return h.registerUpdateListener((({dirtyElements:t})=>{t.get(n)&&x()}))}),[h,x,n]),null===w||null===h?null:m.createElement(O,{close:C,resolution:w,editor:h,anchorElementRef:v,options:e,menuRenderFn:i,onSelectOption:r,commandPriority:c})}export{I as LexicalNodeMenuPlugin,v as MenuOption};
|