@lexical/react 0.1.14 → 0.1.17

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.
Files changed (87) hide show
  1. package/DEPRECATED_useLexicalAutoFormatter.dev.js +5 -741
  2. package/DEPRECATED_useLexicalAutoFormatter.prod.js +1 -21
  3. package/DEPRECATED_useLexicalCanShowPlaceholder.dev.js +4 -73
  4. package/DEPRECATED_useLexicalCanShowPlaceholder.prod.js +1 -2
  5. package/DEPRECATED_useLexicalCharacterLimit.dev.js +19 -72
  6. package/DEPRECATED_useLexicalCharacterLimit.prod.js +7 -8
  7. package/DEPRECATED_useLexicalDecorators.dev.js +1 -1
  8. package/DEPRECATED_useLexicalDecorators.prod.js +1 -1
  9. package/DEPRECATED_useLexicalEditorEvents.dev.js +1 -1
  10. package/DEPRECATED_useLexicalEditorEvents.prod.js +1 -1
  11. package/DEPRECATED_useLexicalHistory.dev.js +5 -307
  12. package/DEPRECATED_useLexicalHistory.prod.js +1 -7
  13. package/DEPRECATED_useLexicalList.dev.js +29 -25
  14. package/DEPRECATED_useLexicalList.prod.js +3 -1
  15. package/DEPRECATED_useLexicalPlainText.dev.js +8 -687
  16. package/DEPRECATED_useLexicalPlainText.prod.js +2 -15
  17. package/DEPRECATED_useLexicalRichText.dev.js +8 -772
  18. package/DEPRECATED_useLexicalRichText.prod.js +2 -17
  19. package/LICENSE +1 -1
  20. package/{withSubscriptions.prod.js → LexicalAutoFocusPlugin.d.ts} +3 -1
  21. package/{withSubscriptions.dev.js → LexicalAutoFocusPlugin.dev.js} +10 -5
  22. package/LexicalAutoFocusPlugin.js +9 -0
  23. package/{LexicalAutoFormatterPlugin.js.flow → LexicalAutoFocusPlugin.js.flow} +1 -1
  24. package/LexicalAutoFocusPlugin.prod.js +7 -0
  25. package/LexicalAutoLinkPlugin.dev.js +12 -15
  26. package/LexicalAutoLinkPlugin.prod.js +6 -6
  27. package/LexicalAutoScrollPlugin.d.ts +13 -0
  28. package/LexicalAutoScrollPlugin.dev.js +82 -0
  29. package/LexicalAutoScrollPlugin.js +9 -0
  30. package/{withSubscriptions.d.ts → LexicalAutoScrollPlugin.js.flow} +5 -5
  31. package/LexicalAutoScrollPlugin.prod.js +8 -0
  32. package/LexicalCharacterLimitPlugin.dev.js +19 -72
  33. package/LexicalCharacterLimitPlugin.prod.js +8 -9
  34. package/LexicalClearEditorPlugin.dev.js +15 -19
  35. package/LexicalClearEditorPlugin.prod.js +1 -1
  36. package/LexicalCollaborationPlugin.d.ts +8 -3
  37. package/LexicalCollaborationPlugin.dev.js +70 -47
  38. package/LexicalCollaborationPlugin.js.flow +9 -3
  39. package/LexicalCollaborationPlugin.prod.js +10 -8
  40. package/LexicalComposer.d.ts +2 -2
  41. package/LexicalComposer.dev.js +3 -19
  42. package/LexicalComposer.js.flow +2 -2
  43. package/LexicalComposer.prod.js +2 -3
  44. package/LexicalContentEditable.dev.js +3 -1
  45. package/LexicalContentEditable.prod.js +2 -2
  46. package/LexicalHashtagPlugin.dev.js +21 -92
  47. package/LexicalHashtagPlugin.prod.js +4 -7
  48. package/LexicalHistoryPlugin.dev.js +5 -307
  49. package/LexicalHistoryPlugin.prod.js +1 -7
  50. package/LexicalHorizontalRuleNode.d.ts +3 -1
  51. package/LexicalHorizontalRuleNode.dev.js +2 -0
  52. package/LexicalHorizontalRuleNode.js.flow +6 -2
  53. package/LexicalHorizontalRuleNode.prod.js +2 -2
  54. package/LexicalLinkPlugin.dev.js +19 -20
  55. package/LexicalLinkPlugin.prod.js +4 -3
  56. package/LexicalListPlugin.dev.js +29 -25
  57. package/LexicalListPlugin.prod.js +3 -2
  58. package/{LexicalAutoFormatterPlugin.d.ts → LexicalMarkdownShortcutPlugin.d.ts} +1 -1
  59. package/LexicalMarkdownShortcutPlugin.dev.js +42 -0
  60. package/LexicalMarkdownShortcutPlugin.js +9 -0
  61. package/{withSubscriptions.js.flow → LexicalMarkdownShortcutPlugin.js.flow} +1 -4
  62. package/LexicalMarkdownShortcutPlugin.prod.js +7 -0
  63. package/LexicalNestedComposer.dev.js +20 -15
  64. package/LexicalNestedComposer.prod.js +3 -3
  65. package/LexicalOnChangePlugin.dev.js +16 -3
  66. package/LexicalOnChangePlugin.prod.js +2 -1
  67. package/LexicalPlainTextPlugin.dev.js +9 -455
  68. package/LexicalPlainTextPlugin.prod.js +4 -12
  69. package/LexicalRichTextPlugin.dev.js +9 -540
  70. package/LexicalRichTextPlugin.prod.js +4 -13
  71. package/LexicalTablePlugin.dev.js +36 -35
  72. package/LexicalTablePlugin.prod.js +3 -3
  73. package/LexicalTreeView.dev.js +1 -1
  74. package/LexicalTreeView.prod.js +1 -1
  75. package/package.json +17 -11
  76. package/useLexicalIsTextContentEmpty.dev.js +4 -33
  77. package/useLexicalIsTextContentEmpty.prod.js +1 -2
  78. package/useLexicalNodeSelection.dev.js +1 -1
  79. package/useLexicalNodeSelection.prod.js +1 -1
  80. package/useLexicalTextEntity.d.ts +19 -0
  81. package/useLexicalTextEntity.dev.js +29 -0
  82. package/{withSubscriptions.js → useLexicalTextEntity.js} +2 -2
  83. package/useLexicalTextEntity.js.flow +18 -0
  84. package/useLexicalTextEntity.prod.js +7 -0
  85. package/LexicalAutoFormatterPlugin.dev.js +0 -778
  86. package/LexicalAutoFormatterPlugin.js +0 -9
  87. package/LexicalAutoFormatterPlugin.prod.js +0 -27
@@ -7,7 +7,4 @@
7
7
  * @flow strict
8
8
  */
9
9
 
10
- type UnsubscribeFn = () => void;
11
- declare export default function withSubscriptions(
12
- ...unsubscribe: Array<UnsubscribeFn>
13
- ): () => void;
10
+ declare export default function LexicalMarkdownShortcutPlugin(): React$Node;
@@ -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
+ var b=require("@lexical/react/LexicalComposerContext"),c=require("@lexical/markdown"),d=require("@lexical/react/LexicalHorizontalRuleNode"),e=require("react");function f(a){e.useEffect(()=>c.registerMarkdownShortcuts(a,d.$createHorizontalRuleNode),[a])}module.exports=function(){const [a]=b.useLexicalComposerContext();f(a);return null};
@@ -6,9 +6,9 @@
6
6
  */
7
7
  'use strict';
8
8
 
9
- var LexicalComposer = require('@lexical/react/LexicalComposer');
10
9
  var LexicalComposerContext = require('@lexical/react/LexicalComposerContext');
11
10
  var LexicalOnChangePlugin = require('@lexical/react/LexicalOnChangePlugin');
11
+ var lexical = require('lexical');
12
12
  var React = require('react');
13
13
 
14
14
  /**
@@ -33,8 +33,7 @@ function LexicalNestedComposer({
33
33
 
34
34
  const [nestedEditor, setNestedEditor] = React.useState(null);
35
35
  const {
36
- decoratorEditor,
37
- theme
36
+ decoratorEditor
38
37
  } = initialConfig;
39
38
  React.useEffect(() => {
40
39
  if (!decoratorEditor.isEmpty() && nestedEditor !== null) {
@@ -42,6 +41,22 @@ function LexicalNestedComposer({
42
41
  } // eslint-disable-next-line react-hooks/exhaustive-deps
43
42
 
44
43
  }, [nestedEditor]);
44
+ const composerContext = React.useMemo(() => {
45
+ const [parentEditor, parentContextContext] = parentContext;
46
+ const composerTheme = initialConfig.theme || parentContextContext.getTheme() || undefined;
47
+ const context = LexicalComposerContext.createLexicalComposerContext(parentContext, composerTheme);
48
+ const editor = lexical.createEditor({
49
+ context,
50
+ namespace: parentEditor._config.namespace,
51
+ nodes: Array.from(parentEditor._nodes.values()).map(registeredNode => registeredNode.klass),
52
+ onError: parentEditor._onError,
53
+ parentEditor,
54
+ theme: composerTheme
55
+ });
56
+ return [editor, context];
57
+ }, // We only do this for init
58
+ // eslint-disable-next-line react-hooks/exhaustive-deps
59
+ []);
45
60
  const onChange = React.useCallback((editorState, nextNestedEditor) => {
46
61
  if (!editorState.isEmpty()) {
47
62
  decoratorEditor.set(nextNestedEditor);
@@ -50,18 +65,8 @@ function LexicalNestedComposer({
50
65
  }
51
66
  }, // eslint-disable-next-line react-hooks/exhaustive-deps
52
67
  []);
53
- return /*#__PURE__*/React.createElement(LexicalComposer, {
54
- initialConfig: React.useMemo(() => {
55
- const [parentEditor] = parentContext;
56
- return {
57
- editor: decoratorEditor.editor,
58
- namespace: parentEditor._config.namespace,
59
- nodes: Array.from(parentEditor._nodes.values()).map(registeredNode => registeredNode.klass),
60
- onError: parentEditor._onError,
61
- theme
62
- }; // We only do this for init
63
- // eslint-disable-next-line react-hooks/exhaustive-deps
64
- }, [])
68
+ return /*#__PURE__*/React.createElement(LexicalComposerContext.LexicalComposerContext.Provider, {
69
+ value: composerContext
65
70
  }, /*#__PURE__*/React.createElement(LexicalOnChangePlugin, {
66
71
  onChange: onChange
67
72
  }), children);
@@ -4,6 +4,6 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- var a=require("@lexical/react/LexicalComposer"),h=require("@lexical/react/LexicalComposerContext"),k=require("@lexical/react/LexicalOnChangePlugin"),l=require("react");
8
- module.exports=function({initialConfig:e={},children:m}){const g=l.useContext(h.LexicalComposerContext);if(null==g)throw Error("Unexpected parent context null on a nested composer");const [f,n]=l.useState(null),{decoratorEditor:c,theme:p}=e;l.useEffect(()=>{c.isEmpty()||null===f||c.init(f)},[f]);e=l.useCallback((b,d)=>{b.isEmpty()?n(d):c.set(d)},[]);return l.createElement(a,{initialConfig:l.useMemo(()=>{const [b]=g;return{editor:c.editor,namespace:b._config.namespace,nodes:Array.from(b._nodes.values()).map(d=>
9
- d.klass),onError:b._onError,theme:p}},[])},l.createElement(k,{onChange:e}),m)};
7
+ var a=require("@lexical/react/LexicalComposerContext"),d=require("@lexical/react/LexicalOnChangePlugin"),m=require("lexical"),n=require("react");
8
+ module.exports=function({initialConfig:h={},children:p}){const e=n.useContext(a.LexicalComposerContext);if(null==e)throw Error("Minified Lexical error #72; see codes.json for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");const [f,q]=n.useState(null),{decoratorEditor:g}=h;n.useEffect(()=>{g.isEmpty()||null===f||g.init(f)},[f]);const t=n.useMemo(()=>{const [b,c]=e,k=h.theme||c.getTheme()||void 0,l=a.createLexicalComposerContext(e,k);return[m.createEditor({context:l,
9
+ namespace:b._config.namespace,nodes:Array.from(b._nodes.values()).map(r=>r.klass),onError:b._onError,parentEditor:b,theme:k}),l]},[]),u=n.useCallback((b,c)=>{b.isEmpty()?q(c):g.set(c)},[]);return n.createElement(a.LexicalComposerContext.Provider,{value:t},n.createElement(d,{onChange:u}),p)};
@@ -39,18 +39,31 @@ var useLayoutEffect = useLayoutEffectImpl;
39
39
  *
40
40
  */
41
41
  function OnChangePlugin({
42
+ ignoreInitialChange = true,
43
+ ignoreSelectionChange = false,
42
44
  onChange
43
45
  }) {
44
46
  const [editor] = LexicalComposerContext.useLexicalComposerContext();
45
47
  useLayoutEffect(() => {
46
48
  if (onChange) {
47
- return editor.addListener('update', ({
48
- editorState
49
+ return editor.registerUpdateListener(({
50
+ editorState,
51
+ dirtyElements,
52
+ dirtyLeaves,
53
+ prevEditorState
49
54
  }) => {
55
+ if (ignoreSelectionChange && dirtyElements.size === 0 && dirtyLeaves.size === 0) {
56
+ return;
57
+ }
58
+
59
+ if (ignoreInitialChange && prevEditorState.isEmpty()) {
60
+ return;
61
+ }
62
+
50
63
  onChange(editorState, editor);
51
64
  });
52
65
  }
53
- }, [editor, onChange]);
66
+ }, [editor, ignoreInitialChange, ignoreSelectionChange, onChange]);
54
67
  return null;
55
68
  }
56
69
 
@@ -4,4 +4,5 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- var c=require("@lexical/react/LexicalComposerContext"),d=require("react"),e="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement?d.useLayoutEffect:d.useEffect;module.exports=function({onChange:a}){const [b]=c.useLexicalComposerContext();e(()=>{if(a)return b.addListener("update",({editorState:f})=>{a(f,b)})},[b,a]);return null};
7
+ var c=require("@lexical/react/LexicalComposerContext"),f=require("react"),g="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement?f.useLayoutEffect:f.useEffect;
8
+ module.exports=function({ignoreInitialChange:d=!0,ignoreSelectionChange:e=!1,onChange:a}){const [b]=c.useLexicalComposerContext();g(()=>{if(a)return b.registerUpdateListener(({editorState:h,dirtyElements:k,dirtyLeaves:l,prevEditorState:m})=>{e&&0===k.size&&0===l.size||d&&m.isEmpty()||a(h,b)})},[b,d,e,a]);return null};
@@ -8,78 +8,11 @@
8
8
 
9
9
  var LexicalComposerContext = require('@lexical/react/LexicalComposerContext');
10
10
  var React = require('react');
11
- var lexical = require('lexical');
11
+ var text = require('@lexical/text');
12
12
  var reactDom = require('react-dom');
13
- var clipboard = require('@lexical/clipboard');
14
-
15
- /**
16
- * Copyright (c) Meta Platforms, Inc. and affiliates.
17
- *
18
- * This source code is licensed under the MIT license found in the
19
- * LICENSE file in the root directory of this source tree.
20
- *
21
- *
22
- */
23
- function $textContent() {
24
- const root = lexical.$getRoot();
25
- return root.getTextContent();
26
- }
27
- function $isTextContentEmpty(isEditorComposing, trim = true) {
28
- if (isEditorComposing) {
29
- return false;
30
- }
31
-
32
- let text = $textContent();
33
-
34
- if (trim) {
35
- text = text.trim();
36
- }
37
-
38
- return text === '';
39
- }
40
- function $canShowPlaceholder(isComposing) {
41
- if (!$isTextContentEmpty(isComposing, false)) {
42
- return false;
43
- }
44
-
45
- const root = lexical.$getRoot();
46
- const children = root.getChildren();
47
- const childrenLength = children.length;
48
-
49
- if (childrenLength > 1) {
50
- return false;
51
- }
52
-
53
- for (let i = 0; i < childrenLength; i++) {
54
- const topBlock = children[i];
55
-
56
- if (lexical.$isElementNode(topBlock)) {
57
- if (topBlock.__type !== 'paragraph') {
58
- return false;
59
- }
60
-
61
- if (topBlock.__indent !== 0) {
62
- return false;
63
- }
64
-
65
- const topBlockChildren = topBlock.getChildren();
66
- const topBlockChildrenLength = topBlockChildren.length;
67
-
68
- for (let s = 0; s < topBlockChildrenLength; s++) {
69
- const child = topBlockChildren[i];
70
-
71
- if (!lexical.$isTextNode(child)) {
72
- return false;
73
- }
74
- }
75
- }
76
- }
77
-
78
- return true;
79
- }
80
- function $canShowPlaceholderCurry(isEditorComposing) {
81
- return () => $canShowPlaceholder(isEditorComposing);
82
- }
13
+ var dragon = require('@lexical/dragon');
14
+ var plainText = require('@lexical/plain-text');
15
+ var utils = require('@lexical/utils');
83
16
 
84
17
  /**
85
18
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -111,13 +44,13 @@ var useLayoutEffect = useLayoutEffectImpl;
111
44
  *
112
45
  */
113
46
  function useLexicalCanShowPlaceholder(editor) {
114
- const [canShowPlaceholder, setCanShowPlaceholder] = React.useState(editor.getEditorState().read($canShowPlaceholderCurry(editor.isComposing())));
47
+ const [canShowPlaceholder, setCanShowPlaceholder] = React.useState(editor.getEditorState().read(text.$canShowPlaceholderCurry(editor.isComposing())));
115
48
  useLayoutEffect(() => {
116
- return editor.addListener('update', ({
49
+ return editor.registerUpdateListener(({
117
50
  editorState
118
51
  }) => {
119
52
  const isComposing = editor.isComposing();
120
- const currentCanShowPlaceholder = editorState.read($canShowPlaceholderCurry(isComposing));
53
+ const currentCanShowPlaceholder = editorState.read(text.$canShowPlaceholderCurry(isComposing));
121
54
  setCanShowPlaceholder(currentCanShowPlaceholder);
122
55
  });
123
56
  }, [editor]);
@@ -136,7 +69,7 @@ function useDecorators(editor) {
136
69
  const [decorators, setDecorators] = React.useState(() => editor.getDecorators()); // Subscribe to changes
137
70
 
138
71
  useLayoutEffect(() => {
139
- return editor.addListener('decorator', nextDecorators => {
72
+ return editor.registerDecoratorListener(nextDecorators => {
140
73
  reactDom.flushSync(() => {
141
74
  setDecorators(nextDecorators);
142
75
  });
@@ -161,225 +94,6 @@ function useDecorators(editor) {
161
94
  }, [decorators, editor]);
162
95
  }
163
96
 
164
- /**
165
- * Copyright (c) Meta Platforms, Inc. and affiliates.
166
- *
167
- * This source code is licensed under the MIT license found in the
168
- * LICENSE file in the root directory of this source tree.
169
- *
170
- *
171
- */
172
-
173
- function $moveCaretSelection(selection, isHoldingShift, isBackward, granularity) {
174
- selection.modify(isHoldingShift ? 'extend' : 'move', isBackward, granularity);
175
- }
176
- function $isParentElementRTL(selection) {
177
- const anchorNode = selection.anchor.getNode();
178
- const parent = lexical.$isRootNode(anchorNode) ? anchorNode : anchorNode.getParentOrThrow();
179
- return parent.getDirection() === 'rtl';
180
- }
181
- function $moveCharacter(selection, isHoldingShift, isBackward) {
182
- const isRTL = $isParentElementRTL(selection);
183
- $moveCaretSelection(selection, isHoldingShift, isBackward ? !isRTL : isRTL, 'character');
184
- }
185
- function $shouldOverrideDefaultCharacterSelection(selection, isBackward) {
186
- const possibleNode = lexical.$getDecoratorNode(selection.focus, isBackward);
187
- return lexical.$isDecoratorNode(possibleNode) && !possibleNode.isIsolated();
188
- }
189
-
190
- /**
191
- * Copyright (c) Meta Platforms, Inc. and affiliates.
192
- *
193
- * This source code is licensed under the MIT license found in the
194
- * LICENSE file in the root directory of this source tree.
195
- *
196
- *
197
- */
198
- function onPasteForPlainText(event, editor) {
199
- event.preventDefault();
200
- editor.update(() => {
201
- const selection = lexical.$getSelection();
202
- const clipboardData = event.clipboardData;
203
-
204
- if (clipboardData != null && lexical.$isRangeSelection(selection)) {
205
- clipboard.$insertDataTransferForPlainText(clipboardData, selection);
206
- }
207
- });
208
- }
209
- function onCutForPlainText(event, editor) {
210
- onCopyForPlainText(event, editor);
211
- editor.update(() => {
212
- const selection = lexical.$getSelection();
213
-
214
- if (lexical.$isRangeSelection(selection)) {
215
- selection.removeText();
216
- }
217
- });
218
- }
219
- function onCopyForPlainText(event, editor) {
220
- event.preventDefault();
221
- editor.update(() => {
222
- const clipboardData = event.clipboardData;
223
- const selection = lexical.$getSelection();
224
-
225
- if (selection !== null) {
226
- if (clipboardData != null) {
227
- const htmlString = clipboard.getHtmlContent(editor);
228
-
229
- if (htmlString !== null) {
230
- clipboardData.setData('text/html', htmlString);
231
- }
232
-
233
- clipboardData.setData('text/plain', selection.getTextContent());
234
- }
235
- }
236
- });
237
- }
238
-
239
- /**
240
- * Copyright (c) Meta Platforms, Inc. and affiliates.
241
- *
242
- * This source code is licensed under the MIT license found in the
243
- * LICENSE file in the root directory of this source tree.
244
- *
245
- *
246
- */
247
- // Convoluted logic to make this work with Flow. Order matters.
248
- const options = {
249
- tag: 'history-merge'
250
- };
251
- const setEditorOptions = options;
252
- const updateOptions = options;
253
- function initializeEditor(editor, initialEditorState) {
254
- if (initialEditorState === null) {
255
- return;
256
- } else if (initialEditorState === undefined) {
257
- editor.update(() => {
258
- const root = lexical.$getRoot();
259
- const firstChild = root.getFirstChild();
260
-
261
- if (firstChild === null) {
262
- const paragraph = lexical.$createParagraphNode();
263
- root.append(paragraph);
264
- const activeElement = document.activeElement;
265
-
266
- if (lexical.$getSelection() !== null || activeElement !== null && activeElement === editor.getRootElement()) {
267
- paragraph.select();
268
- }
269
- }
270
- }, updateOptions);
271
- } else if (initialEditorState !== null) {
272
- switch (typeof initialEditorState) {
273
- case 'string':
274
- {
275
- const parsedEditorState = editor.parseEditorState(initialEditorState);
276
- editor.setEditorState(parsedEditorState, setEditorOptions);
277
- break;
278
- }
279
-
280
- case 'object':
281
- {
282
- editor.setEditorState(initialEditorState, setEditorOptions);
283
- break;
284
- }
285
-
286
- case 'function':
287
- {
288
- editor.update(initialEditorState, updateOptions);
289
- break;
290
- }
291
- }
292
- }
293
- }
294
-
295
- /**
296
- * Copyright (c) Meta Platforms, Inc. and affiliates.
297
- *
298
- * This source code is licensed under the MIT license found in the
299
- * LICENSE file in the root directory of this source tree.
300
- *
301
- *
302
- */
303
- function useLexicalDragonSupport(editor) {
304
- React.useEffect(() => {
305
- const handler = event => {
306
- const rootElement = editor.getRootElement();
307
-
308
- if (document.activeElement !== rootElement) {
309
- return;
310
- }
311
-
312
- const data = event.data;
313
-
314
- if (typeof data === 'string') {
315
- let parsedData;
316
-
317
- try {
318
- parsedData = JSON.parse(data);
319
- } catch (e) {
320
- return;
321
- }
322
-
323
- if (parsedData && parsedData.protocol === 'nuanria_messaging' && parsedData.type === 'request') {
324
- const payload = parsedData.payload;
325
-
326
- if (payload && payload.functionId === 'makeChanges') {
327
- const args = payload.args;
328
-
329
- if (args) {
330
- const [elementStart, elementLength, text, selStart, selLength, formatCommand] = args; // TODO: we should probably handle formatCommand somehow?
331
- editor.update(() => {
332
- const selection = lexical.$getSelection();
333
-
334
- if (lexical.$isRangeSelection(selection)) {
335
- const anchor = selection.anchor;
336
- let anchorNode = anchor.getNode();
337
- let setSelStart = 0;
338
- let setSelEnd = 0;
339
-
340
- if (lexical.$isTextNode(anchorNode)) {
341
- // set initial selection
342
- if (elementStart >= 0 && elementLength >= 0) {
343
- setSelStart = elementStart;
344
- setSelEnd = elementStart + elementLength; // If the offset is more than the end, make it the end
345
-
346
- selection.setTextNodeRange(anchorNode, setSelStart, anchorNode, setSelEnd);
347
- }
348
- }
349
-
350
- if (setSelStart !== setSelEnd || text !== '') {
351
- selection.insertRawText(text);
352
- anchorNode = anchor.getNode();
353
- }
354
-
355
- if (lexical.$isTextNode(anchorNode)) {
356
- // set final selection
357
- setSelStart = selStart;
358
- setSelEnd = selStart + selLength;
359
- const anchorNodeTextLength = anchorNode.getTextContentSize(); // If the offset is more than the end, make it the end
360
-
361
- setSelStart = setSelStart > anchorNodeTextLength ? anchorNodeTextLength : setSelStart;
362
- setSelEnd = setSelEnd > anchorNodeTextLength ? anchorNodeTextLength : setSelEnd;
363
- selection.setTextNodeRange(anchorNode, setSelStart, anchorNode, setSelEnd);
364
- } // block the chrome extension from handling this event
365
-
366
-
367
- event.stopImmediatePropagation();
368
- }
369
- });
370
- }
371
- }
372
- }
373
- }
374
- };
375
-
376
- window.addEventListener('message', handler, true);
377
- return () => {
378
- window.removeEventListener('message', handler, true);
379
- };
380
- }, [editor]);
381
- }
382
-
383
97
  /**
384
98
  * Copyright (c) Meta Platforms, Inc. and affiliates.
385
99
  *
@@ -390,169 +104,9 @@ function useLexicalDragonSupport(editor) {
390
104
  */
391
105
  function usePlainTextSetup(editor, initialEditorState) {
392
106
  useLayoutEffect(() => {
393
- const removeListener = editor.addListener('command', (type, payload) => {
394
- const selection = lexical.$getSelection();
395
-
396
- if (!lexical.$isRangeSelection(selection)) {
397
- return false;
398
- }
399
-
400
- switch (type) {
401
- case 'deleteCharacter':
402
- {
403
- const isBackward = payload;
404
- selection.deleteCharacter(isBackward);
405
- return true;
406
- }
407
-
408
- case 'deleteWord':
409
- {
410
- const isBackward = payload;
411
- selection.deleteWord(isBackward);
412
- return true;
413
- }
414
-
415
- case 'deleteLine':
416
- {
417
- const isBackward = payload;
418
- selection.deleteLine(isBackward);
419
- return true;
420
- }
421
-
422
- case 'insertText':
423
- {
424
- const eventOrText = payload;
425
-
426
- if (typeof eventOrText === 'string') {
427
- selection.insertText(eventOrText);
428
- } else {
429
- const dataTransfer = eventOrText.dataTransfer;
430
-
431
- if (dataTransfer != null) {
432
- clipboard.$insertDataTransferForPlainText(dataTransfer, selection);
433
- } else {
434
- const data = eventOrText.data;
435
-
436
- if (data) {
437
- selection.insertText(data);
438
- }
439
- }
440
- }
441
-
442
- return true;
443
- }
444
-
445
- case 'removeText':
446
- selection.removeText();
447
- return true;
448
-
449
- case 'insertLineBreak':
450
- const selectStart = payload;
451
- selection.insertLineBreak(selectStart);
452
- return true;
453
-
454
- case 'insertParagraph':
455
- selection.insertLineBreak();
456
- return true;
457
-
458
- case 'indentContent':
459
- case 'outdentContent':
460
- case 'insertHorizontalRule':
461
- case 'insertImage':
462
- case 'insertTable':
463
- case 'formatElement':
464
- case 'formatText':
465
- {
466
- return true;
467
- }
468
-
469
- case 'keyArrowLeft':
470
- {
471
- const event = payload;
472
- const isHoldingShift = event.shiftKey;
473
-
474
- if ($shouldOverrideDefaultCharacterSelection(selection, true)) {
475
- event.preventDefault();
476
- $moveCharacter(selection, isHoldingShift, true);
477
- return true;
478
- }
479
-
480
- return false;
481
- }
482
-
483
- case 'keyArrowRight':
484
- {
485
- const event = payload;
486
- const isHoldingShift = event.shiftKey;
487
-
488
- if ($shouldOverrideDefaultCharacterSelection(selection, false)) {
489
- event.preventDefault();
490
- $moveCharacter(selection, isHoldingShift, false);
491
- return true;
492
- }
493
-
494
- return false;
495
- }
496
-
497
- case 'keyBackspace':
498
- {
499
- const event = payload;
500
- event.preventDefault();
501
- return editor.execCommand('deleteCharacter', true);
502
- }
503
-
504
- case 'keyDelete':
505
- {
506
- const event = payload;
507
- event.preventDefault();
508
- return editor.execCommand('deleteCharacter', false);
509
- }
510
-
511
- case 'keyEnter':
512
- {
513
- const event = payload;
514
- event.preventDefault();
515
- return editor.execCommand('insertLineBreak');
516
- }
517
-
518
- case 'copy':
519
- {
520
- const event = payload;
521
- onCopyForPlainText(event, editor);
522
- return true;
523
- }
524
-
525
- case 'cut':
526
- {
527
- const event = payload;
528
- onCutForPlainText(event, editor);
529
- return true;
530
- }
531
-
532
- case 'paste':
533
- {
534
- const event = payload;
535
- onPasteForPlainText(event, editor);
536
- return true;
537
- }
538
-
539
- case 'drop':
540
- case 'dragstart':
541
- {
542
- // TODO: Make drag and drop work at some point.
543
- const event = payload;
544
- event.preventDefault();
545
- return true;
546
- }
547
- }
548
-
549
- return false;
550
- }, 0);
551
- initializeEditor(editor, initialEditorState);
552
- return removeListener; // We only do this for init
107
+ return utils.mergeRegister(plainText.registerPlainText(editor, initialEditorState), dragon.registerDragonSupport(editor)); // We only do this for init
553
108
  // eslint-disable-next-line react-hooks/exhaustive-deps
554
109
  }, [editor]);
555
- useLexicalDragonSupport(editor);
556
110
  }
557
111
 
558
112
  /**
@@ -4,15 +4,7 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- var g=require("@lexical/react/LexicalComposerContext"),h=require("react"),t=require("lexical"),u=require("react-dom"),v=require("@lexical/clipboard");function w(a,d=!0){if(a)return!1;a=t.$getRoot().getTextContent();d&&(a=a.trim());return""===a}
8
- function x(a){if(!w(a,!1))return!1;a=t.$getRoot().getChildren();const d=a.length;if(1<d)return!1;for(let c=0;c<d;c++){var e=a[c];if(t.$isElementNode(e)){if("paragraph"!==e.__type||0!==e.__indent)return!1;e=e.getChildren();const b=e.length;for(let f=0;f<b;f++)if(!t.$isTextNode(e[c]))return!1}}return!0}function y(a){return()=>x(a)}var z="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement?h.useLayoutEffect:h.useEffect;
9
- function A(a){const [d,e]=h.useState(a.getEditorState().read(y(a.isComposing())));z(()=>a.addListener("update",({editorState:c})=>{const b=a.isComposing();c=c.read(y(b));e(c)}),[a]);return d}function C(a){const [d,e]=h.useState(()=>a.getDecorators());z(()=>a.addListener("decorator",c=>{u.flushSync(()=>{e(c)})}),[a]);return h.useMemo(()=>{const c=[],b=Object.keys(d);for(let p=0;p<b.length;p++){var f=b[p];const r=d[f];f=a.getElementByKey(f);null!==f&&c.push(u.createPortal(r,f))}return c},[d,a])}
10
- function D(a){a=a.anchor.getNode();return"rtl"===(t.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}function E(a,d){a=t.$getDecoratorNode(a.focus,d);return t.$isDecoratorNode(a)&&!a.isIsolated()}function F(a,d){a.preventDefault();d.update(()=>{const e=t.$getSelection(),c=a.clipboardData;null!=c&&t.$isRangeSelection(e)&&v.$insertDataTransferForPlainText(c,e)})}function G(a,d){H(a,d);d.update(()=>{const e=t.$getSelection();t.$isRangeSelection(e)&&e.removeText()})}
11
- function H(a,d){a.preventDefault();d.update(()=>{const e=a.clipboardData,c=t.$getSelection();if(null!==c&&null!=e){const b=v.getHtmlContent(d);null!==b&&e.setData("text/html",b);e.setData("text/plain",c.getTextContent())}})}const I={tag:"history-merge"};
12
- function J(a,d){if(null!==d)if(void 0===d)a.update(()=>{var e=t.$getRoot();if(null===e.getFirstChild()){const c=t.$createParagraphNode();e.append(c);e=document.activeElement;(null!==t.$getSelection()||null!==e&&e===a.getRootElement())&&c.select()}},I);else if(null!==d)switch(typeof d){case "string":d=a.parseEditorState(d);a.setEditorState(d,I);break;case "object":a.setEditorState(d,I);break;case "function":a.update(d,I)}}
13
- function K(a){h.useEffect(()=>{const d=e=>{var c=a.getRootElement();if(document.activeElement===c&&(c=e.data,"string"===typeof c)){try{var b=JSON.parse(c)}catch(f){return}if(b&&"nuanria_messaging"===b.protocol&&"request"===b.type&&(b=b.payload)&&"makeChanges"===b.functionId&&(b=b.args)){const [f,p,r,B,L]=b;a.update(()=>{const q=t.$getSelection();if(t.$isRangeSelection(q)){var n=q.anchor;let k=n.getNode(),l=0,m=0;t.$isTextNode(k)&&0<=f&&0<=p&&(l=f,m=f+p,q.setTextNodeRange(k,l,k,m));if(l!==m||""!==
14
- r)q.insertRawText(r),k=n.getNode();t.$isTextNode(k)&&(l=B,m=B+L,n=k.getTextContentSize(),l=l>n?n:l,m=m>n?n:m,q.setTextNodeRange(k,l,k,m));e.stopImmediatePropagation()}})}}};window.addEventListener("message",d,!0);return()=>{window.removeEventListener("message",d,!0)}},[a])}
15
- function M(a,d){z(()=>{const e=a.addListener("command",(c,b)=>{const f=t.$getSelection();if(!t.$isRangeSelection(f))return!1;switch(c){case "deleteCharacter":return f.deleteCharacter(b),!0;case "deleteWord":return f.deleteWord(b),!0;case "deleteLine":return f.deleteLine(b),!0;case "insertText":return"string"===typeof b?f.insertText(b):(c=b.dataTransfer,null!=c?v.$insertDataTransferForPlainText(c,f):(b=b.data)&&f.insertText(b)),!0;case "removeText":return f.removeText(),!0;case "insertLineBreak":return f.insertLineBreak(b),
16
- !0;case "insertParagraph":return f.insertLineBreak(),!0;case "indentContent":case "outdentContent":case "insertHorizontalRule":case "insertImage":case "insertTable":case "formatElement":case "formatText":return!0;case "keyArrowLeft":c=b.shiftKey;if(E(f,!0))return b.preventDefault(),b=c,c=D(f),f.modify(b?"extend":"move",!c,"character"),!0;break;case "keyArrowRight":c=b.shiftKey;if(E(f,!1))return b.preventDefault(),b=c,c=D(f),f.modify(b?"extend":"move",c,"character"),!0;break;case "keyBackspace":return b.preventDefault(),
17
- a.execCommand("deleteCharacter",!0);case "keyDelete":return b.preventDefault(),a.execCommand("deleteCharacter",!1);case "keyEnter":return b.preventDefault(),a.execCommand("insertLineBreak");case "copy":return H(b,a),!0;case "cut":return G(b,a),!0;case "paste":return F(b,a),!0;case "drop":case "dragstart":return b.preventDefault(),!0}return!1},0);J(a,d);return e},[a]);K(a)}
18
- module.exports=function({contentEditable:a,placeholder:d,initialEditorState:e}){const [c]=g.useLexicalComposerContext(),b=A(c);M(c,e);e=C(c);return h.createElement(h.Fragment,null,a,b&&d,e)};
7
+ var d=require("@lexical/react/LexicalComposerContext"),h=require("react"),l=require("@lexical/text"),m=require("react-dom"),n=require("@lexical/dragon"),p=require("@lexical/plain-text"),q=require("@lexical/utils"),r="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement?h.useLayoutEffect:h.useEffect;
8
+ function t(a){const [c,e]=h.useState(a.getEditorState().read(l.$canShowPlaceholderCurry(a.isComposing())));r(()=>a.registerUpdateListener(({editorState:b})=>{const f=a.isComposing();b=b.read(l.$canShowPlaceholderCurry(f));e(b)}),[a]);return c}
9
+ function u(a){const [c,e]=h.useState(()=>a.getDecorators());r(()=>a.registerDecoratorListener(b=>{m.flushSync(()=>{e(b)})}),[a]);return h.useMemo(()=>{const b=[],f=Object.keys(c);for(let k=0;k<f.length;k++){var g=f[k];const v=c[g];g=a.getElementByKey(g);null!==g&&b.push(m.createPortal(v,g))}return b},[c,a])}function w(a,c){r(()=>q.mergeRegister(p.registerPlainText(a,c),n.registerDragonSupport(a)),[a])}
10
+ module.exports=function({contentEditable:a,placeholder:c,initialEditorState:e}){const [b]=d.useLexicalComposerContext(),f=t(b);w(b,e);e=u(b);return h.createElement(h.Fragment,null,a,f&&c,e)};