@lexical/react 0.1.7 → 0.1.8

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 (101) hide show
  1. package/DEPRECATED_useLexical.dev.js +84 -0
  2. package/DEPRECATED_useLexical.js +9 -2
  3. package/DEPRECATED_useLexical.prod.js +8 -0
  4. package/DEPRECATED_useLexicalAutoFormatter.dev.js +642 -0
  5. package/DEPRECATED_useLexicalAutoFormatter.js +9 -11
  6. package/DEPRECATED_useLexicalAutoFormatter.prod.js +22 -0
  7. package/DEPRECATED_useLexicalCanShowPlaceholder.dev.js +136 -0
  8. package/DEPRECATED_useLexicalCanShowPlaceholder.js +9 -3
  9. package/DEPRECATED_useLexicalCanShowPlaceholder.prod.js +8 -0
  10. package/DEPRECATED_useLexicalCharacterLimit.dev.js +294 -0
  11. package/DEPRECATED_useLexicalCharacterLimit.js +9 -8
  12. package/DEPRECATED_useLexicalCharacterLimit.prod.js +14 -0
  13. package/DEPRECATED_useLexicalDecorators.dev.js +82 -0
  14. package/DEPRECATED_useLexicalDecorators.js +9 -2
  15. package/DEPRECATED_useLexicalDecorators.prod.js +8 -0
  16. package/DEPRECATED_useLexicalEditor.dev.js +52 -0
  17. package/DEPRECATED_useLexicalEditor.js +9 -1
  18. package/DEPRECATED_useLexicalEditor.prod.js +7 -0
  19. package/DEPRECATED_useLexicalEditorEvents.dev.js +96 -0
  20. package/DEPRECATED_useLexicalEditorEvents.js +9 -2
  21. package/DEPRECATED_useLexicalEditorEvents.prod.js +8 -0
  22. package/DEPRECATED_useLexicalHistory.dev.js +339 -0
  23. package/DEPRECATED_useLexicalHistory.js +9 -7
  24. package/DEPRECATED_useLexicalHistory.prod.js +13 -0
  25. package/DEPRECATED_useLexicalList.dev.js +64 -0
  26. package/DEPRECATED_useLexicalList.js +9 -1
  27. package/DEPRECATED_useLexicalList.prod.js +7 -0
  28. package/DEPRECATED_useLexicalPlainText.dev.js +755 -0
  29. package/DEPRECATED_useLexicalPlainText.js +9 -16
  30. package/DEPRECATED_useLexicalPlainText.prod.js +22 -0
  31. package/DEPRECATED_useLexicalRichText.dev.js +1326 -0
  32. package/DEPRECATED_useLexicalRichText.js +9 -30
  33. package/DEPRECATED_useLexicalRichText.prod.js +35 -0
  34. package/LexicalAutoFormatterPlugin.dev.js +645 -0
  35. package/LexicalAutoFormatterPlugin.js +9 -12
  36. package/LexicalAutoFormatterPlugin.prod.js +23 -0
  37. package/LexicalAutoLinkPlugin.dev.js +227 -0
  38. package/LexicalAutoLinkPlugin.js +9 -6
  39. package/LexicalAutoLinkPlugin.prod.js +12 -0
  40. package/LexicalBootstrapPlugin.dev.js +124 -0
  41. package/LexicalBootstrapPlugin.js +9 -0
  42. package/LexicalBootstrapPlugin.prod.js +8 -0
  43. package/LexicalCharacterLimitPlugin.dev.js +352 -0
  44. package/LexicalCharacterLimitPlugin.js +9 -8
  45. package/LexicalCharacterLimitPlugin.prod.js +14 -0
  46. package/LexicalCollaborationPlugin.dev.js +235 -0
  47. package/LexicalCollaborationPlugin.js +9 -8
  48. package/LexicalCollaborationPlugin.prod.js +14 -0
  49. package/LexicalComposer.dev.js +76 -0
  50. package/LexicalComposer.js +9 -3
  51. package/LexicalComposer.prod.js +9 -0
  52. package/LexicalComposerContext.dev.js +53 -0
  53. package/LexicalComposerContext.js +9 -1
  54. package/LexicalComposerContext.prod.js +7 -0
  55. package/LexicalContentEditable.dev.js +71 -0
  56. package/LexicalContentEditable.js +9 -3
  57. package/LexicalContentEditable.prod.js +9 -0
  58. package/LexicalHashtagPlugin.dev.js +152 -0
  59. package/LexicalHashtagPlugin.js +9 -4
  60. package/LexicalHashtagPlugin.prod.js +10 -0
  61. package/LexicalHistoryPlugin.dev.js +344 -0
  62. package/LexicalHistoryPlugin.js +9 -7
  63. package/LexicalHistoryPlugin.prod.js +13 -0
  64. package/LexicalHorizontalRulePlugin.dev.js +51 -0
  65. package/LexicalHorizontalRulePlugin.js +9 -1
  66. package/LexicalHorizontalRulePlugin.prod.js +7 -0
  67. package/LexicalLinkPlugin.dev.js +137 -0
  68. package/LexicalLinkPlugin.js +9 -3
  69. package/LexicalLinkPlugin.prod.js +9 -0
  70. package/LexicalListPlugin.dev.js +67 -0
  71. package/LexicalListPlugin.js +9 -2
  72. package/LexicalListPlugin.prod.js +8 -0
  73. package/LexicalNestedComposer.dev.js +60 -0
  74. package/LexicalNestedComposer.js +9 -2
  75. package/LexicalNestedComposer.prod.js +8 -0
  76. package/LexicalOnChangePlugin.dev.js +57 -0
  77. package/LexicalOnChangePlugin.js +9 -1
  78. package/LexicalOnChangePlugin.prod.js +7 -0
  79. package/LexicalPlainTextPlugin.dev.js +565 -0
  80. package/LexicalPlainTextPlugin.js +9 -13
  81. package/LexicalPlainTextPlugin.prod.js +17 -0
  82. package/LexicalRichTextPlugin.dev.js +1136 -0
  83. package/LexicalRichTextPlugin.js +9 -27
  84. package/LexicalRichTextPlugin.prod.js +31 -0
  85. package/LexicalTablePlugin.dev.js +95 -0
  86. package/LexicalTablePlugin.js +9 -3
  87. package/LexicalTablePlugin.prod.js +9 -0
  88. package/LexicalTreeView.dev.js +340 -0
  89. package/LexicalTreeView.js +9 -13
  90. package/LexicalTreeView.prod.js +19 -0
  91. package/README.md +1 -0
  92. package/package.json +4 -4
  93. package/useLexicalDecoratorMap.dev.js +83 -0
  94. package/useLexicalDecoratorMap.js +9 -2
  95. package/useLexicalDecoratorMap.prod.js +8 -0
  96. package/useLexicalIsTextContentEmpty.dev.js +84 -0
  97. package/useLexicalIsTextContentEmpty.js +9 -2
  98. package/useLexicalIsTextContentEmpty.prod.js +8 -0
  99. package/withSubscriptions.dev.js +23 -0
  100. package/withSubscriptions.js +9 -1
  101. package/withSubscriptions.prod.js +7 -0
@@ -0,0 +1,755 @@
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
+ 'use strict';
8
+
9
+ var withSubscriptions = require('@lexical/react/withSubscriptions');
10
+ var lexical = require('lexical');
11
+ var react = require('react');
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 MERGE = 0;
22
+ const NO_MERGE = 1;
23
+ const DISCARD = 2;
24
+ const OTHER = 0;
25
+ const COMPOSING_CHARACTER = 1;
26
+ const INSERT_CHARACTER_AFTER_SELECTION = 2;
27
+ const DELETE_CHARACTER_BEFORE_SELECTION = 3;
28
+ const DELETE_CHARACTER_AFTER_SELECTION = 4;
29
+ const EditorPriority$1 = 0;
30
+
31
+ function getDirtyNodes(editorState, dirtyLeavesSet, dirtyElementsSet) {
32
+ const dirtyLeaves = Array.from(dirtyLeavesSet);
33
+ const dirtyElements = Array.from(dirtyElementsSet);
34
+ const nodeMap = editorState._nodeMap;
35
+ const nodes = [];
36
+
37
+ for (let i = 0; i < dirtyLeaves.length; i++) {
38
+ const dirtyLeafKey = dirtyLeaves[i];
39
+ const dirtyLeaf = nodeMap.get(dirtyLeafKey);
40
+
41
+ if (dirtyLeaf !== undefined) {
42
+ nodes.push(dirtyLeaf);
43
+ }
44
+ }
45
+
46
+ for (let i = 0; i < dirtyElements.length; i++) {
47
+ const intentionallyMarkedAsDirty = dirtyElements[i][1];
48
+
49
+ if (!intentionallyMarkedAsDirty) {
50
+ continue;
51
+ }
52
+
53
+ const dirtyElementKey = dirtyElements[i][0];
54
+ const dirtyElement = nodeMap.get(dirtyElementKey);
55
+
56
+ if (dirtyElement !== undefined && !lexical.$isRootNode(dirtyElement)) {
57
+ nodes.push(dirtyElement);
58
+ }
59
+ }
60
+
61
+ return nodes;
62
+ }
63
+
64
+ function getChangeType(prevEditorState, nextEditorState, dirtyLeavesSet, dirtyElementsSet, isComposing) {
65
+ if (prevEditorState === null || dirtyLeavesSet.size === 0 && dirtyElementsSet.size === 0) {
66
+ return OTHER;
67
+ }
68
+
69
+ const nextSelection = nextEditorState._selection;
70
+ const prevSelection = prevEditorState._selection;
71
+
72
+ if (isComposing) {
73
+ return COMPOSING_CHARACTER;
74
+ }
75
+
76
+ if (nextSelection === null || prevSelection === null || !prevSelection.isCollapsed() || !nextSelection.isCollapsed()) {
77
+ return OTHER;
78
+ }
79
+
80
+ const dirtyNodes = getDirtyNodes(nextEditorState, dirtyLeavesSet, dirtyElementsSet);
81
+
82
+ if (dirtyNodes.length === 0) {
83
+ return OTHER;
84
+ } // Catching the case when inserting new text node into an element (e.g. first char in paragraph/list),
85
+ // or after existing node.
86
+
87
+
88
+ if (dirtyNodes.length > 1) {
89
+ const nextNodeMap = nextEditorState._nodeMap;
90
+ const nextAnchorNode = nextNodeMap.get(nextSelection.anchor.key);
91
+ const prevAnchorNode = nextNodeMap.get(prevSelection.anchor.key);
92
+
93
+ if (nextAnchorNode && prevAnchorNode && !prevEditorState._nodeMap.has(nextAnchorNode.__key) && lexical.$isTextNode(nextAnchorNode) && nextAnchorNode.__text.length === 1 && nextSelection.anchor.offset === 1) {
94
+ return INSERT_CHARACTER_AFTER_SELECTION;
95
+ }
96
+
97
+ return OTHER;
98
+ }
99
+
100
+ const nextDirtyNode = dirtyNodes[0];
101
+
102
+ const prevDirtyNode = prevEditorState._nodeMap.get(nextDirtyNode.__key);
103
+
104
+ if (!lexical.$isTextNode(prevDirtyNode) || !lexical.$isTextNode(nextDirtyNode) || prevDirtyNode.__mode !== nextDirtyNode.__mode) {
105
+ return OTHER;
106
+ }
107
+
108
+ const prevText = prevDirtyNode.__text;
109
+ const nextText = nextDirtyNode.__text;
110
+
111
+ if (prevText === nextText) {
112
+ return OTHER;
113
+ }
114
+
115
+ const nextAnchor = nextSelection.anchor;
116
+ const prevAnchor = prevSelection.anchor;
117
+
118
+ if (nextAnchor.key !== prevAnchor.key || nextAnchor.type !== 'text') {
119
+ return OTHER;
120
+ }
121
+
122
+ const nextAnchorOffset = nextAnchor.offset;
123
+ const prevAnchorOffset = prevAnchor.offset;
124
+ const textDiff = nextText.length - prevText.length;
125
+
126
+ if (textDiff === 1 && prevAnchorOffset === nextAnchorOffset - 1) {
127
+ return INSERT_CHARACTER_AFTER_SELECTION;
128
+ }
129
+
130
+ if (textDiff === -1 && prevAnchorOffset === nextAnchorOffset + 1) {
131
+ return DELETE_CHARACTER_BEFORE_SELECTION;
132
+ }
133
+
134
+ if (textDiff === -1 && prevAnchorOffset === nextAnchorOffset) {
135
+ return DELETE_CHARACTER_AFTER_SELECTION;
136
+ }
137
+
138
+ return OTHER;
139
+ }
140
+
141
+ function createMergeActionGetter(editor, delay) {
142
+ let prevChangeTime = Date.now();
143
+ let prevChangeType = OTHER;
144
+ return (prevEditorState, nextEditorState, currentHistoryEntry, dirtyLeaves, dirtyElements, tags) => {
145
+ const changeTime = Date.now(); // If applying changes from history stack there's no need
146
+ // to run history logic again, as history entries already calculated
147
+
148
+ if (tags.has('historic')) {
149
+ prevChangeType = OTHER;
150
+ prevChangeTime = changeTime;
151
+ return DISCARD;
152
+ }
153
+
154
+ const changeType = getChangeType(prevEditorState, nextEditorState, dirtyLeaves, dirtyElements, editor.isComposing());
155
+
156
+ const mergeAction = (() => {
157
+ if (tags.has('without-history')) {
158
+ return MERGE;
159
+ }
160
+
161
+ if (prevEditorState === null) {
162
+ return NO_MERGE;
163
+ }
164
+
165
+ const selection = nextEditorState._selection;
166
+ const prevSelection = prevEditorState._selection;
167
+ const hasDirtyNodes = dirtyLeaves.size > 0 || dirtyElements.size > 0;
168
+
169
+ if (!hasDirtyNodes) {
170
+ if (prevSelection === null && selection !== null) {
171
+ return MERGE;
172
+ }
173
+
174
+ return DISCARD;
175
+ }
176
+
177
+ const isSameEditor = currentHistoryEntry === null || currentHistoryEntry.editor === editor;
178
+
179
+ if (changeType !== OTHER && changeType === prevChangeType && changeTime < prevChangeTime + delay && isSameEditor) {
180
+ return MERGE;
181
+ }
182
+
183
+ return NO_MERGE;
184
+ })();
185
+
186
+ prevChangeTime = changeTime;
187
+ prevChangeType = changeType;
188
+ return mergeAction;
189
+ };
190
+ }
191
+
192
+ function useHistory(editor, externalHistoryState, delay = 1000) {
193
+ const historyState = react.useMemo(() => externalHistoryState || createEmptyHistoryState(), [externalHistoryState]);
194
+ const clearHistory = react.useCallback(() => {
195
+ historyState.undoStack = [];
196
+ historyState.redoStack = [];
197
+ historyState.current = null;
198
+ }, [historyState]);
199
+ react.useEffect(() => {
200
+ const getMergeAction = createMergeActionGetter(editor, delay);
201
+
202
+ const applyChange = ({
203
+ editorState,
204
+ prevEditorState,
205
+ dirtyLeaves,
206
+ dirtyElements,
207
+ tags
208
+ }) => {
209
+ const current = historyState.current;
210
+ const redoStack = historyState.redoStack;
211
+ const undoStack = historyState.undoStack;
212
+ const currentEditorState = current === null ? null : current.editorState;
213
+
214
+ if (current !== null && editorState === currentEditorState) {
215
+ return;
216
+ }
217
+
218
+ const mergeAction = getMergeAction(prevEditorState, editorState, current, dirtyLeaves, dirtyElements, tags);
219
+
220
+ if (mergeAction === NO_MERGE) {
221
+ if (redoStack.length !== 0) {
222
+ historyState.redoStack = [];
223
+ }
224
+
225
+ if (current !== null) {
226
+ undoStack.push({ ...current,
227
+ undoSelection: prevEditorState.read(lexical.$getSelection)
228
+ });
229
+ editor.execCommand('canUndo', true);
230
+ }
231
+ } else if (mergeAction === DISCARD) {
232
+ return;
233
+ } // Else we merge
234
+
235
+
236
+ historyState.current = {
237
+ editor,
238
+ editorState
239
+ };
240
+ };
241
+
242
+ const undo = () => {
243
+ const redoStack = historyState.redoStack;
244
+ const undoStack = historyState.undoStack;
245
+ const undoStackLength = undoStack.length;
246
+
247
+ if (undoStackLength !== 0) {
248
+ const current = historyState.current;
249
+ const historyStateEntry = undoStack.pop();
250
+
251
+ if (current !== null) {
252
+ redoStack.push(current);
253
+ editor.execCommand('canRedo', true);
254
+ }
255
+
256
+ if (undoStack.length === 0) {
257
+ editor.execCommand('canUndo', false);
258
+ }
259
+
260
+ historyState.current = historyStateEntry;
261
+ historyStateEntry.editor.setEditorState(historyStateEntry.editorState.clone(historyStateEntry.undoSelection), {
262
+ tag: 'historic'
263
+ });
264
+ }
265
+ };
266
+
267
+ const redo = () => {
268
+ const redoStack = historyState.redoStack;
269
+ const undoStack = historyState.undoStack;
270
+
271
+ if (redoStack.length !== 0) {
272
+ const current = historyState.current;
273
+
274
+ if (current !== null) {
275
+ undoStack.push(current);
276
+ editor.execCommand('canUndo', true);
277
+ }
278
+
279
+ const historyStateEntry = redoStack.pop();
280
+
281
+ if (redoStack.length === 0) {
282
+ editor.execCommand('canRedo', false);
283
+ }
284
+
285
+ historyState.current = historyStateEntry;
286
+ historyStateEntry.editor.setEditorState(historyStateEntry.editorState, {
287
+ tag: 'historic'
288
+ });
289
+ }
290
+ };
291
+
292
+ const applyCommand = type => {
293
+ switch (type) {
294
+ case 'undo':
295
+ undo();
296
+ return true;
297
+
298
+ case 'redo':
299
+ redo();
300
+ return true;
301
+
302
+ case 'clearEditor':
303
+ clearHistory();
304
+ return false;
305
+
306
+ case 'clearHistory':
307
+ clearHistory();
308
+ return true;
309
+
310
+ default:
311
+ return false;
312
+ }
313
+ };
314
+
315
+ return withSubscriptions(editor.addListener('command', applyCommand, EditorPriority$1), editor.addListener('update', applyChange));
316
+ }, [clearHistory, delay, editor, historyState]);
317
+ }
318
+ function createEmptyHistoryState() {
319
+ return {
320
+ current: null,
321
+ redoStack: [],
322
+ undoStack: []
323
+ };
324
+ }
325
+
326
+ /**
327
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
328
+ *
329
+ * This source code is licensed under the MIT license found in the
330
+ * LICENSE file in the root directory of this source tree.
331
+ *
332
+ *
333
+ */
334
+ function useLexicalHistory(editor, externalHistoryState, delay = 1000) {
335
+ return useHistory(editor, externalHistoryState, delay);
336
+ }
337
+
338
+ /**
339
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
340
+ *
341
+ * This source code is licensed under the MIT license found in the
342
+ * LICENSE file in the root directory of this source tree.
343
+ *
344
+ *
345
+ */
346
+
347
+ function $moveCaretSelection(selection, isHoldingShift, isBackward, granularity) {
348
+ selection.modify(isHoldingShift ? 'extend' : 'move', isBackward, granularity);
349
+ }
350
+ function $isParentElementRTL(selection) {
351
+ const anchorNode = selection.anchor.getNode();
352
+ const parent = anchorNode.getParentOrThrow();
353
+ return parent.getDirection() === 'rtl';
354
+ }
355
+ function $moveCharacter(selection, isHoldingShift, isBackward) {
356
+ const isRTL = $isParentElementRTL(selection);
357
+ $moveCaretSelection(selection, isHoldingShift, isBackward ? !isRTL : isRTL, 'character');
358
+ }
359
+
360
+ /**
361
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
362
+ *
363
+ * This source code is licensed under the MIT license found in the
364
+ * LICENSE file in the root directory of this source tree.
365
+ *
366
+ *
367
+ */
368
+ function getPossibleDecoratorNode(focus, isBackward) {
369
+ const focusOffset = focus.offset;
370
+
371
+ if (focus.type === 'element') {
372
+ const block = focus.getNode();
373
+ return block.getChildAtIndex(isBackward ? focusOffset - 1 : focusOffset);
374
+ } else {
375
+ const focusNode = focus.getNode();
376
+
377
+ if (isBackward && focusOffset === 0 || !isBackward && focusOffset === focusNode.getTextContentSize()) {
378
+ return isBackward ? focusNode.getPreviousSibling() : focusNode.getNextSibling();
379
+ }
380
+ }
381
+
382
+ return null;
383
+ }
384
+
385
+ /**
386
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
387
+ *
388
+ * This source code is licensed under the MIT license found in the
389
+ * LICENSE file in the root directory of this source tree.
390
+ *
391
+ *
392
+ */
393
+ function $insertDataTransferForPlainText(dataTransfer, selection) {
394
+ const text = dataTransfer.getData('text/plain');
395
+
396
+ if (text != null) {
397
+ selection.insertRawText(text);
398
+ }
399
+ }
400
+ function $shouldOverrideDefaultCharacterSelection(selection, isBackward) {
401
+ const possibleDecoratorNode = getPossibleDecoratorNode(selection.focus, isBackward);
402
+ return lexical.$isDecoratorNode(possibleDecoratorNode);
403
+ }
404
+ function onPasteForPlainText(event, editor) {
405
+ event.preventDefault();
406
+ editor.update(() => {
407
+ lexical.$log('onPasteForPlainText');
408
+ const selection = lexical.$getSelection();
409
+ const clipboardData = event.clipboardData;
410
+
411
+ if (clipboardData != null && selection !== null) {
412
+ $insertDataTransferForPlainText(clipboardData, selection);
413
+ }
414
+ });
415
+ }
416
+ function onCutForPlainText(event, editor) {
417
+ onCopyForPlainText(event, editor);
418
+ editor.update(() => {
419
+ lexical.$log('onCutForPlainText');
420
+ const selection = lexical.$getSelection();
421
+
422
+ if (selection !== null) {
423
+ selection.removeText();
424
+ }
425
+ });
426
+ }
427
+ function onCopyForPlainText(event, editor) {
428
+ event.preventDefault();
429
+ editor.update(() => {
430
+ lexical.$log('onCopyForPlainText');
431
+ const clipboardData = event.clipboardData;
432
+ const selection = lexical.$getSelection();
433
+
434
+ if (selection !== null) {
435
+ if (clipboardData != null) {
436
+ const domSelection = window.getSelection(); // If we haven't selected a range, then don't copy anything
437
+
438
+ if (domSelection.isCollapsed) {
439
+ return;
440
+ }
441
+
442
+ const range = domSelection.getRangeAt(0);
443
+
444
+ if (range) {
445
+ const container = document.createElement('div');
446
+ const frag = range.cloneContents();
447
+ container.appendChild(frag);
448
+ clipboardData.setData('text/html', container.innerHTML);
449
+ }
450
+
451
+ clipboardData.setData('text/plain', selection.getTextContent());
452
+ }
453
+ }
454
+ });
455
+ }
456
+
457
+ /**
458
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
459
+ *
460
+ * This source code is licensed under the MIT license found in the
461
+ * LICENSE file in the root directory of this source tree.
462
+ *
463
+ *
464
+ */
465
+ const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
466
+
467
+ /**
468
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
469
+ *
470
+ * This source code is licensed under the MIT license found in the
471
+ * LICENSE file in the root directory of this source tree.
472
+ *
473
+ *
474
+ */
475
+ const useLayoutEffectImpl = CAN_USE_DOM ? react.useLayoutEffect : react.useEffect;
476
+ var useLayoutEffect = useLayoutEffectImpl;
477
+
478
+ /**
479
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
480
+ *
481
+ * This source code is licensed under the MIT license found in the
482
+ * LICENSE file in the root directory of this source tree.
483
+ *
484
+ *
485
+ */
486
+ function useLexicalDragonSupport(editor) {
487
+ react.useEffect(() => {
488
+ const handler = event => {
489
+ const rootElement = editor.getRootElement();
490
+
491
+ if (document.activeElement !== rootElement) {
492
+ return;
493
+ }
494
+
495
+ const data = event.data;
496
+
497
+ if (typeof data === 'string') {
498
+ let parsedData;
499
+
500
+ try {
501
+ parsedData = JSON.parse(data);
502
+ } catch (e) {
503
+ return;
504
+ }
505
+
506
+ if (parsedData && parsedData.protocol === 'nuanria_messaging' && parsedData.type === 'request') {
507
+ const payload = parsedData.payload;
508
+
509
+ if (payload && payload.functionId === 'makeChanges') {
510
+ const args = payload.args;
511
+
512
+ if (args) {
513
+ const [elementStart, elementLength, text, selStart, selLength, formatCommand] = args; // TODO: we should probably handle formatCommand somehow?
514
+ editor.update(() => {
515
+ lexical.$log('useLexicalDragonSupport');
516
+ const selection = lexical.$getSelection();
517
+
518
+ if (selection !== null) {
519
+ const anchor = selection.anchor;
520
+ let anchorNode = anchor.getNode();
521
+ let setSelStart = 0;
522
+ let setSelEnd = 0;
523
+
524
+ if (lexical.$isTextNode(anchorNode)) {
525
+ // set initial selection
526
+ if (elementStart >= 0 && elementLength >= 0) {
527
+ setSelStart = elementStart;
528
+ setSelEnd = elementStart + elementLength; // If the offset is more than the end, make it the end
529
+
530
+ selection.setTextNodeRange(anchorNode, setSelStart, anchorNode, setSelEnd);
531
+ }
532
+ }
533
+
534
+ if (setSelStart !== setSelEnd || text !== '') {
535
+ selection.insertRawText(text);
536
+ anchorNode = anchor.getNode();
537
+ }
538
+
539
+ if (lexical.$isTextNode(anchorNode)) {
540
+ // set final selection
541
+ setSelStart = selStart;
542
+ setSelEnd = selStart + selLength;
543
+ const anchorNodeTextLength = anchorNode.getTextContentSize(); // If the offset is more than the end, make it the end
544
+
545
+ setSelStart = setSelStart > anchorNodeTextLength ? anchorNodeTextLength : setSelStart;
546
+ setSelEnd = setSelEnd > anchorNodeTextLength ? anchorNodeTextLength : setSelEnd;
547
+ selection.setTextNodeRange(anchorNode, setSelStart, anchorNode, setSelEnd);
548
+ } // block the chrome extension from handling this event
549
+
550
+
551
+ event.stopImmediatePropagation();
552
+ }
553
+ });
554
+ }
555
+ }
556
+ }
557
+ }
558
+ };
559
+
560
+ window.addEventListener('message', handler, true);
561
+ return () => {
562
+ window.removeEventListener('message', handler, true);
563
+ };
564
+ }, [editor]);
565
+ }
566
+
567
+ /**
568
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
569
+ *
570
+ * This source code is licensed under the MIT license found in the
571
+ * LICENSE file in the root directory of this source tree.
572
+ *
573
+ *
574
+ */
575
+ const EditorPriority = 0;
576
+ function usePlainTextSetup(editor) {
577
+ useLayoutEffect(() => {
578
+ const removeListener = editor.addListener('command', (type, payload) => {
579
+ const selection = lexical.$getSelection();
580
+
581
+ if (selection === null) {
582
+ return false;
583
+ }
584
+
585
+ switch (type) {
586
+ case 'deleteCharacter':
587
+ {
588
+ const isBackward = payload;
589
+ selection.deleteCharacter(isBackward);
590
+ return true;
591
+ }
592
+
593
+ case 'deleteWord':
594
+ {
595
+ const isBackward = payload;
596
+ selection.deleteWord(isBackward);
597
+ return true;
598
+ }
599
+
600
+ case 'deleteLine':
601
+ {
602
+ const isBackward = payload;
603
+ selection.deleteLine(isBackward);
604
+ return true;
605
+ }
606
+
607
+ case 'insertText':
608
+ {
609
+ const eventOrText = payload;
610
+
611
+ if (typeof eventOrText === 'string') {
612
+ selection.insertText(eventOrText);
613
+ } else {
614
+ const dataTransfer = eventOrText.dataTransfer;
615
+
616
+ if (dataTransfer != null) {
617
+ $insertDataTransferForPlainText(dataTransfer, selection);
618
+ } else {
619
+ const data = eventOrText.data;
620
+
621
+ if (data) {
622
+ selection.insertText(data);
623
+ }
624
+ }
625
+ }
626
+
627
+ return true;
628
+ }
629
+
630
+ case 'removeText':
631
+ selection.removeText();
632
+ return true;
633
+
634
+ case 'insertLineBreak':
635
+ const selectStart = payload;
636
+ selection.insertLineBreak(selectStart);
637
+ return true;
638
+
639
+ case 'insertParagraph':
640
+ selection.insertLineBreak();
641
+ return true;
642
+
643
+ case 'indentContent':
644
+ case 'outdentContent':
645
+ case 'insertHorizontalRule':
646
+ case 'insertImage':
647
+ case 'insertTable':
648
+ case 'formatElement':
649
+ case 'formatText':
650
+ {
651
+ return true;
652
+ }
653
+
654
+ case 'keyArrowLeft':
655
+ {
656
+ const event = payload;
657
+ const isHoldingShift = event.shiftKey;
658
+
659
+ if ($shouldOverrideDefaultCharacterSelection(selection, true)) {
660
+ event.preventDefault();
661
+ $moveCharacter(selection, isHoldingShift, true);
662
+ return true;
663
+ }
664
+
665
+ return false;
666
+ }
667
+
668
+ case 'keyArrowRight':
669
+ {
670
+ const event = payload;
671
+ const isHoldingShift = event.shiftKey;
672
+
673
+ if ($shouldOverrideDefaultCharacterSelection(selection, false)) {
674
+ event.preventDefault();
675
+ $moveCharacter(selection, isHoldingShift, false);
676
+ return true;
677
+ }
678
+
679
+ return false;
680
+ }
681
+
682
+ case 'keyBackspace':
683
+ {
684
+ const event = payload;
685
+ event.preventDefault();
686
+ return editor.execCommand('deleteCharacter', true);
687
+ }
688
+
689
+ case 'keyDelete':
690
+ {
691
+ const event = payload;
692
+ event.preventDefault();
693
+ return editor.execCommand('deleteCharacter', false);
694
+ }
695
+
696
+ case 'keyEnter':
697
+ {
698
+ const event = payload;
699
+ event.preventDefault();
700
+ return editor.execCommand('insertLineBreak');
701
+ }
702
+
703
+ case 'copy':
704
+ {
705
+ const event = payload;
706
+ onCopyForPlainText(event, editor);
707
+ return true;
708
+ }
709
+
710
+ case 'cut':
711
+ {
712
+ const event = payload;
713
+ onCutForPlainText(event, editor);
714
+ return true;
715
+ }
716
+
717
+ case 'paste':
718
+ {
719
+ const event = payload;
720
+ onPasteForPlainText(event, editor);
721
+ return true;
722
+ }
723
+
724
+ case 'drop':
725
+ case 'dragstart':
726
+ {
727
+ // TODO: Make drag and drop work at some point.
728
+ const event = payload;
729
+ event.preventDefault();
730
+ return true;
731
+ }
732
+ }
733
+
734
+ return false;
735
+ }, EditorPriority);
736
+ editor.execCommand('bootstrapEditor');
737
+ return removeListener;
738
+ }, [editor]);
739
+ useLexicalDragonSupport(editor);
740
+ }
741
+
742
+ /**
743
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
744
+ *
745
+ * This source code is licensed under the MIT license found in the
746
+ * LICENSE file in the root directory of this source tree.
747
+ *
748
+ *
749
+ */
750
+ function useLexicalPlainText(editor, externalHistoryState) {
751
+ usePlainTextSetup(editor);
752
+ useLexicalHistory(editor, externalHistoryState);
753
+ }
754
+
755
+ module.exports = useLexicalPlainText;