@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,1136 @@
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 LexicalComposerContext = require('@lexical/react/LexicalComposerContext');
10
+ var React = require('react');
11
+ var lexical = require('lexical');
12
+ var reactDom = require('react-dom');
13
+ var list = require('@lexical/list');
14
+ var CodeNode = require('lexical/CodeNode');
15
+ var HeadingNode = require('lexical/HeadingNode');
16
+ var LinkNode = require('lexical/LinkNode');
17
+
18
+ /**
19
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
20
+ *
21
+ * This source code is licensed under the MIT license found in the
22
+ * LICENSE file in the root directory of this source tree.
23
+ *
24
+ *
25
+ */
26
+ function $textContent() {
27
+ const root = lexical.$getRoot();
28
+ return root.getTextContent();
29
+ }
30
+ function $isTextContentEmpty(isEditorComposing, trim = true) {
31
+ if (isEditorComposing) {
32
+ return false;
33
+ }
34
+
35
+ let text = $textContent();
36
+
37
+ if (trim) {
38
+ text = text.trim();
39
+ }
40
+
41
+ return text === '';
42
+ }
43
+ function $canShowPlaceholder(isComposing) {
44
+ if (!$isTextContentEmpty(isComposing, false)) {
45
+ return false;
46
+ }
47
+
48
+ const root = lexical.$getRoot();
49
+ const children = root.getChildren();
50
+ const childrenLength = children.length;
51
+
52
+ if (childrenLength > 1) {
53
+ return false;
54
+ }
55
+
56
+ for (let i = 0; i < childrenLength; i++) {
57
+ const topBlock = children[i];
58
+
59
+ if (lexical.$isElementNode(topBlock)) {
60
+ if (topBlock.__type !== 'paragraph') {
61
+ return false;
62
+ }
63
+
64
+ if (topBlock.__indent !== 0) {
65
+ return false;
66
+ }
67
+
68
+ const topBlockChildren = topBlock.getChildren();
69
+ const topBlockChildrenLength = topBlockChildren.length;
70
+
71
+ for (let s = 0; s < topBlockChildrenLength; s++) {
72
+ const child = topBlockChildren[i];
73
+
74
+ if (!lexical.$isTextNode(child)) {
75
+ return false;
76
+ }
77
+ }
78
+ }
79
+ }
80
+
81
+ return true;
82
+ }
83
+ function $canShowPlaceholderCurry(isEditorComposing) {
84
+ return () => $canShowPlaceholder(isEditorComposing);
85
+ }
86
+
87
+ /**
88
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
89
+ *
90
+ * This source code is licensed under the MIT license found in the
91
+ * LICENSE file in the root directory of this source tree.
92
+ *
93
+ *
94
+ */
95
+ const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
96
+
97
+ /**
98
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
99
+ *
100
+ * This source code is licensed under the MIT license found in the
101
+ * LICENSE file in the root directory of this source tree.
102
+ *
103
+ *
104
+ */
105
+ const useLayoutEffectImpl = CAN_USE_DOM ? React.useLayoutEffect : React.useEffect;
106
+ var useLayoutEffect = useLayoutEffectImpl;
107
+
108
+ /**
109
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
110
+ *
111
+ * This source code is licensed under the MIT license found in the
112
+ * LICENSE file in the root directory of this source tree.
113
+ *
114
+ *
115
+ */
116
+ function useLexicalCanShowPlaceholder(editor) {
117
+ const [canShowPlaceholder, setCanShowPlaceholder] = React.useState(editor.getEditorState().read($canShowPlaceholderCurry(editor.isComposing())));
118
+ useLayoutEffect(() => {
119
+ return editor.addListener('update', ({
120
+ editorState
121
+ }) => {
122
+ const isComposing = editor.isComposing();
123
+ const currentCanShowPlaceholder = editorState.read($canShowPlaceholderCurry(isComposing));
124
+ setCanShowPlaceholder(currentCanShowPlaceholder);
125
+ });
126
+ }, [editor]);
127
+ return canShowPlaceholder;
128
+ }
129
+
130
+ /**
131
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
132
+ *
133
+ * This source code is licensed under the MIT license found in the
134
+ * LICENSE file in the root directory of this source tree.
135
+ *
136
+ *
137
+ */
138
+ function useDecorators(editor) {
139
+ const [decorators, setDecorators] = React.useState(() => editor.getDecorators()); // Subscribe to changes
140
+
141
+ useLayoutEffect(() => {
142
+ return editor.addListener('decorator', nextDecorators => {
143
+ reactDom.flushSync(() => {
144
+ setDecorators(nextDecorators);
145
+ });
146
+ });
147
+ }, [editor]); // Return decorators defined as React Portals
148
+
149
+ return React.useMemo(() => {
150
+ const decoratedPortals = [];
151
+ const decoratorKeys = Object.keys(decorators);
152
+
153
+ for (let i = 0; i < decoratorKeys.length; i++) {
154
+ const nodeKey = decoratorKeys[i];
155
+ const reactDecorator = decorators[nodeKey];
156
+ const element = editor.getElementByKey(nodeKey);
157
+
158
+ if (element !== null) {
159
+ decoratedPortals.push( /*#__PURE__*/reactDom.createPortal(reactDecorator, element));
160
+ }
161
+ }
162
+
163
+ return decoratedPortals;
164
+ }, [decorators, editor]);
165
+ }
166
+
167
+ /**
168
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
169
+ *
170
+ * This source code is licensed under the MIT license found in the
171
+ * LICENSE file in the root directory of this source tree.
172
+ *
173
+ *
174
+ */
175
+
176
+ function $cloneWithProperties(node) {
177
+ const latest = node.getLatest();
178
+ const constructor = latest.constructor;
179
+ const clone = constructor.clone(latest);
180
+ clone.__parent = latest.__parent;
181
+
182
+ if (lexical.$isElementNode(latest) && lexical.$isElementNode(clone)) {
183
+ clone.__children = Array.from(latest.__children);
184
+ clone.__format = latest.__format;
185
+ clone.__indent = latest.__indent;
186
+ clone.__dir = latest.__dir;
187
+ } else if (lexical.$isTextNode(latest) && lexical.$isTextNode(clone)) {
188
+ clone.__format = latest.__format;
189
+ clone.__style = latest.__style;
190
+ clone.__mode = latest.__mode;
191
+ clone.__detail = latest.__detail;
192
+ } else if (lexical.$isDecoratorNode(latest) && lexical.$isDecoratorNode(clone)) {
193
+ clone.__state = latest.__state;
194
+ } // $FlowFixMe
195
+
196
+
197
+ return clone;
198
+ }
199
+
200
+ function $getIndexFromPossibleClone(node, parent, nodeMap) {
201
+ const parentClone = nodeMap.get(parent.getKey());
202
+
203
+ if (lexical.$isElementNode(parentClone)) {
204
+ return parentClone.__children.indexOf(node.getKey());
205
+ }
206
+
207
+ return node.getIndexWithinParent();
208
+ }
209
+
210
+ function $getParentAvoidingExcludedElements(node) {
211
+ let parent = node.getParent();
212
+
213
+ while (parent !== null && parent.excludeFromCopy()) {
214
+ parent = parent.getParent();
215
+ }
216
+
217
+ return parent;
218
+ }
219
+
220
+ function $copyLeafNodeBranchToRoot(leaf, startingOffset, isLeftSide, range, nodeMap) {
221
+ let node = leaf;
222
+ let offset = startingOffset;
223
+
224
+ while (node !== null) {
225
+ const parent = $getParentAvoidingExcludedElements(node);
226
+
227
+ if (parent === null) {
228
+ break;
229
+ }
230
+
231
+ if (!lexical.$isElementNode(node) || !node.excludeFromCopy()) {
232
+ const key = node.getKey();
233
+ let clone = nodeMap.get(key);
234
+ const needsClone = clone === undefined;
235
+
236
+ if (needsClone) {
237
+ clone = $cloneWithProperties(node);
238
+ nodeMap.set(key, clone);
239
+ }
240
+
241
+ if (lexical.$isTextNode(clone) && !clone.isSegmented() && !clone.isToken()) {
242
+ clone.__text = clone.__text.slice(isLeftSide ? offset : 0, isLeftSide ? undefined : offset);
243
+ } else if (lexical.$isElementNode(clone)) {
244
+ clone.__children = clone.__children.slice(isLeftSide ? offset : 0, isLeftSide ? undefined : offset + 1);
245
+ }
246
+
247
+ if (lexical.$isRootNode(parent)) {
248
+ if (needsClone) {
249
+ // We only want to collect a range of top level nodes.
250
+ // So if the parent is the root, we know this is a top level.
251
+ range.push(key);
252
+ }
253
+
254
+ break;
255
+ }
256
+ }
257
+
258
+ offset = $getIndexFromPossibleClone(node, parent, nodeMap);
259
+ node = parent;
260
+ }
261
+ }
262
+
263
+ function $cloneContents(selection) {
264
+ const anchor = selection.anchor;
265
+ const focus = selection.focus;
266
+ const anchorOffset = anchor.getCharacterOffset();
267
+ const focusOffset = focus.getCharacterOffset();
268
+ const anchorNode = anchor.getNode();
269
+ const focusNode = focus.getNode();
270
+ const anchorNodeParent = anchorNode.getParentOrThrow(); // Handle a single text node extraction
271
+
272
+ if (anchorNode === focusNode && lexical.$isTextNode(anchorNode) && (anchorNodeParent.canBeEmpty() || anchorNodeParent.getChildrenSize() > 1)) {
273
+ const clonedFirstNode = $cloneWithProperties(anchorNode);
274
+ const isBefore = focusOffset > anchorOffset;
275
+ const startOffset = isBefore ? anchorOffset : focusOffset;
276
+ const endOffset = isBefore ? focusOffset : anchorOffset;
277
+ clonedFirstNode.__text = clonedFirstNode.__text.slice(startOffset, endOffset);
278
+ const key = clonedFirstNode.getKey();
279
+ return {
280
+ nodeMap: [[key, clonedFirstNode]],
281
+ range: [key]
282
+ };
283
+ }
284
+
285
+ const nodes = selection.getNodes();
286
+
287
+ if (nodes.length === 0) {
288
+ return {
289
+ nodeMap: [],
290
+ range: []
291
+ };
292
+ } // Check if we can use the parent of the nodes, if the
293
+ // parent can't be empty, then it's important that we
294
+ // also copy that element node along with its children.
295
+
296
+
297
+ let nodesLength = nodes.length;
298
+ const firstNode = nodes[0];
299
+ const firstNodeParent = firstNode.getParent();
300
+
301
+ if (firstNodeParent !== null && !firstNodeParent.canBeEmpty()) {
302
+ const parentChildren = firstNodeParent.__children;
303
+ const parentChildrenLength = parentChildren.length;
304
+
305
+ if (parentChildrenLength === nodesLength) {
306
+ let areTheSame = true;
307
+
308
+ for (let i = 0; i < parentChildren.length; i++) {
309
+ if (parentChildren[i] !== nodes[i].__key) {
310
+ areTheSame = false;
311
+ break;
312
+ }
313
+ }
314
+
315
+ if (areTheSame) {
316
+ nodesLength++;
317
+ nodes.push(firstNodeParent);
318
+ }
319
+ }
320
+ }
321
+
322
+ const lastNode = nodes[nodesLength - 1];
323
+ const isBefore = anchor.isBefore(focus);
324
+ const nodeMap = new Map();
325
+ const range = []; // Do first node to root
326
+
327
+ $copyLeafNodeBranchToRoot(firstNode, isBefore ? anchorOffset : focusOffset, true, range, nodeMap); // Copy all nodes between
328
+
329
+ for (let i = 0; i < nodesLength; i++) {
330
+ const node = nodes[i];
331
+ const key = node.getKey();
332
+
333
+ if (!nodeMap.has(key) && (!lexical.$isElementNode(node) || !node.excludeFromCopy())) {
334
+ const clone = $cloneWithProperties(node);
335
+
336
+ if (lexical.$isRootNode(node.getParent())) {
337
+ range.push(node.getKey());
338
+ }
339
+
340
+ nodeMap.set(key, clone);
341
+ }
342
+ } // Do last node to root
343
+
344
+
345
+ $copyLeafNodeBranchToRoot(lastNode, isBefore ? focusOffset : anchorOffset, false, range, nodeMap);
346
+ return {
347
+ nodeMap: Array.from(nodeMap.entries()),
348
+ range
349
+ };
350
+ }
351
+
352
+ function $moveCaretSelection(selection, isHoldingShift, isBackward, granularity) {
353
+ selection.modify(isHoldingShift ? 'extend' : 'move', isBackward, granularity);
354
+ }
355
+ function $isParentElementRTL(selection) {
356
+ const anchorNode = selection.anchor.getNode();
357
+ const parent = anchorNode.getParentOrThrow();
358
+ return parent.getDirection() === 'rtl';
359
+ }
360
+ function $moveCharacter(selection, isHoldingShift, isBackward) {
361
+ const isRTL = $isParentElementRTL(selection);
362
+ $moveCaretSelection(selection, isHoldingShift, isBackward ? !isRTL : isRTL, 'character');
363
+ }
364
+
365
+ /**
366
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
367
+ *
368
+ * This source code is licensed under the MIT license found in the
369
+ * LICENSE file in the root directory of this source tree.
370
+ *
371
+ *
372
+ */
373
+ function getPossibleDecoratorNode(focus, isBackward) {
374
+ const focusOffset = focus.offset;
375
+
376
+ if (focus.type === 'element') {
377
+ const block = focus.getNode();
378
+ return block.getChildAtIndex(isBackward ? focusOffset - 1 : focusOffset);
379
+ } else {
380
+ const focusNode = focus.getNode();
381
+
382
+ if (isBackward && focusOffset === 0 || !isBackward && focusOffset === focusNode.getTextContentSize()) {
383
+ return isBackward ? focusNode.getPreviousSibling() : focusNode.getNextSibling();
384
+ }
385
+ }
386
+
387
+ return null;
388
+ }
389
+
390
+ /**
391
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
392
+ *
393
+ * This source code is licensed under the MIT license found in the
394
+ * LICENSE file in the root directory of this source tree.
395
+ *
396
+ *
397
+ */
398
+
399
+ const isCodeElement = div => {
400
+ return div.style.fontFamily.match('monospace') !== null;
401
+ };
402
+
403
+ const DOM_NODE_NAME_TO_LEXICAL_NODE = {
404
+ '#text': domNode => ({
405
+ node: lexical.$createTextNode(domNode.textContent)
406
+ }),
407
+ a: domNode => {
408
+ let node;
409
+
410
+ if (domNode instanceof HTMLAnchorElement) {
411
+ node = LinkNode.$createLinkNode(domNode.href);
412
+ } else {
413
+ node = lexical.$createTextNode(domNode.textContent);
414
+ }
415
+
416
+ return {
417
+ node
418
+ };
419
+ },
420
+ b: domNode => {
421
+ // $FlowFixMe[incompatible-type] domNode is a <b> since we matched it by nodeName
422
+ const b = domNode; // Google Docs wraps all copied HTML in a <b> with font-weight normal
423
+
424
+ const hasNormalFontWeight = b.style.fontWeight === 'normal';
425
+ return {
426
+ forChild: lexicalNode => {
427
+ if (lexical.$isTextNode(lexicalNode) && !hasNormalFontWeight) {
428
+ lexicalNode.toggleFormat('bold');
429
+ }
430
+ },
431
+ node: null
432
+ };
433
+ },
434
+ br: () => ({
435
+ node: lexical.$createLineBreakNode()
436
+ }),
437
+ div: domNode => {
438
+ // $FlowFixMe[incompatible-type] domNode is a <div> since we matched it by nodeName
439
+ const div = domNode;
440
+ return {
441
+ after: childLexicalNodes => {
442
+ const domParent = domNode.parentNode;
443
+
444
+ if (domParent != null && domNode !== domParent.lastChild) {
445
+ childLexicalNodes.push(lexical.$createLineBreakNode());
446
+ }
447
+
448
+ return childLexicalNodes;
449
+ },
450
+ node: isCodeElement(div) ? CodeNode.$createCodeNode() : null
451
+ };
452
+ },
453
+ em: domNode => {
454
+ return {
455
+ forChild: lexicalNode => {
456
+ if (lexical.$isTextNode(lexicalNode)) {
457
+ lexicalNode.toggleFormat('italic');
458
+ }
459
+ },
460
+ node: null
461
+ };
462
+ },
463
+ h1: () => ({
464
+ node: HeadingNode.$createHeadingNode('h1')
465
+ }),
466
+ h2: () => ({
467
+ node: HeadingNode.$createHeadingNode('h2')
468
+ }),
469
+ h3: () => ({
470
+ node: HeadingNode.$createHeadingNode('h3')
471
+ }),
472
+ h4: () => ({
473
+ node: HeadingNode.$createHeadingNode('h4')
474
+ }),
475
+ h5: () => ({
476
+ node: HeadingNode.$createHeadingNode('h5')
477
+ }),
478
+ i: domNode => {
479
+ return {
480
+ forChild: lexicalNode => {
481
+ if (lexical.$isTextNode(lexicalNode)) {
482
+ lexicalNode.toggleFormat('italic');
483
+ }
484
+ },
485
+ node: null
486
+ };
487
+ },
488
+ li: () => ({
489
+ node: list.$createListItemNode()
490
+ }),
491
+ ol: () => ({
492
+ node: list.$createListNode('ol')
493
+ }),
494
+ p: () => ({
495
+ node: lexical.$createParagraphNode()
496
+ }),
497
+ pre: domNode => ({
498
+ node: CodeNode.$createCodeNode()
499
+ }),
500
+ span: domNode => {
501
+ // $FlowFixMe[incompatible-type] domNode is a <span> since we matched it by nodeName
502
+ const span = domNode; // Google Docs uses span tags + font-weight for bold text
503
+
504
+ const hasBoldFontWeight = span.style.fontWeight === '700';
505
+ return {
506
+ forChild: lexicalNode => {
507
+ if (lexical.$isTextNode(lexicalNode) && hasBoldFontWeight) {
508
+ lexicalNode.toggleFormat('bold');
509
+ }
510
+ },
511
+ node: null
512
+ };
513
+ },
514
+ strong: domNode => {
515
+ return {
516
+ forChild: lexicalNode => {
517
+ if (lexical.$isTextNode(lexicalNode)) {
518
+ lexicalNode.toggleFormat('bold');
519
+ }
520
+ },
521
+ node: null
522
+ };
523
+ },
524
+ table: domNode => {
525
+ // $FlowFixMe[incompatible-type] domNode is a <table> since we matched it by nodeName
526
+ const table = domNode;
527
+ const isGitHubCodeTable = table.classList.contains('js-file-line-container');
528
+ return {
529
+ node: isGitHubCodeTable ? CodeNode.$createCodeNode() : null
530
+ };
531
+ },
532
+ td: domNode => {
533
+ // $FlowFixMe[incompatible-type] domNode is a <table> since we matched it by nodeName
534
+ const cell = domNode;
535
+ const isGitHubCodeCell = cell.classList.contains('js-file-line');
536
+ return {
537
+ after: childLexicalNodes => {
538
+ if (isGitHubCodeCell && cell.parentNode && cell.parentNode.nextSibling) {
539
+ // Append newline between code lines
540
+ childLexicalNodes.push(lexical.$createLineBreakNode());
541
+ }
542
+
543
+ return childLexicalNodes;
544
+ },
545
+ node: null
546
+ };
547
+ },
548
+ u: domNode => {
549
+ return {
550
+ forChild: lexicalNode => {
551
+ if (lexical.$isTextNode(lexicalNode)) {
552
+ lexicalNode.toggleFormat('underline');
553
+ }
554
+ },
555
+ node: null
556
+ };
557
+ },
558
+ ul: () => ({
559
+ node: list.$createListNode('ul')
560
+ })
561
+ };
562
+
563
+ function $generateNodes(nodeRange) {
564
+ const {
565
+ range,
566
+ nodeMap
567
+ } = nodeRange;
568
+ const parsedNodeMap = new Map(nodeMap);
569
+ const nodes = [];
570
+
571
+ for (let i = 0; i < range.length; i++) {
572
+ const key = range[i];
573
+ const parsedNode = parsedNodeMap.get(key);
574
+
575
+ if (parsedNode !== undefined) {
576
+ const node = lexical.$createNodeFromParse(parsedNode, parsedNodeMap);
577
+ nodes.push(node);
578
+ }
579
+ }
580
+
581
+ return nodes;
582
+ }
583
+
584
+ function $createNodesFromDOM(node, conversionMap, editor, forChildMap = new Map()) {
585
+ let lexicalNodes = [];
586
+ let currentLexicalNode = null;
587
+ const nodeName = node.nodeName.toLowerCase();
588
+ const customHtmlTransforms = editor._config.htmlTransforms || {};
589
+ const transformFunction = customHtmlTransforms[nodeName] || conversionMap[nodeName];
590
+ const transformOutput = transformFunction ? transformFunction(node) : null;
591
+ let postTransform = null;
592
+
593
+ if (transformOutput !== null) {
594
+ postTransform = transformOutput.after;
595
+ currentLexicalNode = transformOutput.node;
596
+
597
+ if (currentLexicalNode !== null) {
598
+ lexicalNodes.push(currentLexicalNode);
599
+ const forChildFunctions = Array.from(forChildMap.values());
600
+
601
+ for (let i = 0; i < forChildFunctions.length; i++) {
602
+ forChildFunctions[i](currentLexicalNode);
603
+ }
604
+ }
605
+
606
+ if (transformOutput.forChild != null) {
607
+ forChildMap.set(nodeName, transformOutput.forChild);
608
+ }
609
+ } // If the DOM node doesn't have a transformer, we don't know what
610
+ // to do with it but we still need to process any childNodes.
611
+
612
+
613
+ const children = node.childNodes;
614
+ let childLexicalNodes = [];
615
+
616
+ for (let i = 0; i < children.length; i++) {
617
+ childLexicalNodes.push(...$createNodesFromDOM(children[i], conversionMap, editor, forChildMap));
618
+ }
619
+
620
+ if (postTransform != null) {
621
+ childLexicalNodes = postTransform(childLexicalNodes);
622
+ }
623
+
624
+ if (currentLexicalNode == null) {
625
+ // If it hasn't been converted to a LexicalNode, we hoist its children
626
+ // up to the same level as it.
627
+ lexicalNodes = lexicalNodes.concat(childLexicalNodes);
628
+ } else {
629
+ if (lexical.$isElementNode(currentLexicalNode)) {
630
+ // If the current node is a ElementNode after conversion,
631
+ // we can append all the children to it.
632
+ currentLexicalNode.append(...childLexicalNodes);
633
+ }
634
+ }
635
+
636
+ return lexicalNodes;
637
+ }
638
+
639
+ function $generateNodesFromDOM(dom, conversionMap, editor) {
640
+ let lexicalNodes = [];
641
+ const elements = dom.body ? Array.from(dom.body.childNodes) : [];
642
+ const elementsLength = elements.length;
643
+
644
+ for (let i = 0; i < elementsLength; i++) {
645
+ const lexicalNode = $createNodesFromDOM(elements[i], conversionMap, editor);
646
+
647
+ if (lexicalNode !== null) {
648
+ lexicalNodes = lexicalNodes.concat(lexicalNode);
649
+ }
650
+ }
651
+
652
+ return lexicalNodes;
653
+ }
654
+
655
+ function $insertDataTransferForRichText(dataTransfer, selection, editor) {
656
+ const lexicalNodesString = dataTransfer.getData('application/x-lexical-editor');
657
+
658
+ if (lexicalNodesString) {
659
+ const namespace = editor._config.namespace;
660
+
661
+ try {
662
+ const lexicalClipboardData = JSON.parse(lexicalNodesString);
663
+
664
+ if (lexicalClipboardData.namespace === namespace) {
665
+ const nodeRange = lexicalClipboardData.state;
666
+ const nodes = $generateNodes(nodeRange);
667
+ selection.insertNodes(nodes);
668
+ return;
669
+ }
670
+ } catch (e) {// Malformed, missing nodes..
671
+ }
672
+ }
673
+
674
+ const textHtmlMimeType = 'text/html';
675
+ const htmlString = dataTransfer.getData(textHtmlMimeType);
676
+
677
+ if (htmlString) {
678
+ const parser = new DOMParser();
679
+ const dom = parser.parseFromString(htmlString, textHtmlMimeType);
680
+ const nodes = $generateNodesFromDOM(dom, DOM_NODE_NAME_TO_LEXICAL_NODE, editor); // Wrap text and inline nodes in paragraph nodes so we have all blocks at the top-level
681
+
682
+ const topLevelBlocks = [];
683
+ let currentBlock = null;
684
+
685
+ for (let i = 0; i < nodes.length; i++) {
686
+ const node = nodes[i];
687
+
688
+ if (!lexical.$isElementNode(node) || node.isInline()) {
689
+ if (currentBlock === null) {
690
+ currentBlock = lexical.$createParagraphNode();
691
+ topLevelBlocks.push(currentBlock);
692
+ }
693
+
694
+ if (currentBlock !== null) {
695
+ currentBlock.append(node);
696
+ }
697
+ } else {
698
+ topLevelBlocks.push(node);
699
+ currentBlock = null;
700
+ }
701
+ }
702
+
703
+ selection.insertNodes(topLevelBlocks);
704
+ return;
705
+ }
706
+
707
+ $insertDataTransferForPlainText(dataTransfer, selection);
708
+ }
709
+ function $insertDataTransferForPlainText(dataTransfer, selection) {
710
+ const text = dataTransfer.getData('text/plain');
711
+
712
+ if (text != null) {
713
+ selection.insertRawText(text);
714
+ }
715
+ }
716
+ function $shouldOverrideDefaultCharacterSelection(selection, isBackward) {
717
+ const possibleDecoratorNode = getPossibleDecoratorNode(selection.focus, isBackward);
718
+ return lexical.$isDecoratorNode(possibleDecoratorNode);
719
+ }
720
+ function onPasteForRichText(event, editor) {
721
+ event.preventDefault();
722
+ editor.update(() => {
723
+ lexical.$log('onPasteForRichText');
724
+ const selection = lexical.$getSelection();
725
+ const clipboardData = event.clipboardData;
726
+
727
+ if (clipboardData != null && selection !== null) {
728
+ $insertDataTransferForRichText(clipboardData, selection, editor);
729
+ }
730
+ });
731
+ }
732
+ function onCutForRichText(event, editor) {
733
+ onCopyForRichText(event, editor);
734
+ editor.update(() => {
735
+ lexical.$log('onCutForRichText');
736
+ const selection = lexical.$getSelection();
737
+
738
+ if (selection !== null) {
739
+ selection.removeText();
740
+ }
741
+ });
742
+ }
743
+ function onCopyForRichText(event, editor) {
744
+ event.preventDefault();
745
+ editor.update(() => {
746
+ lexical.$log('onCopyForRichText');
747
+ const clipboardData = event.clipboardData;
748
+ const selection = lexical.$getSelection();
749
+
750
+ if (selection !== null) {
751
+ if (clipboardData != null) {
752
+ const domSelection = window.getSelection(); // If we haven't selected a range, then don't copy anything
753
+
754
+ if (domSelection.isCollapsed) {
755
+ return;
756
+ }
757
+
758
+ const range = domSelection.getRangeAt(0);
759
+
760
+ if (range) {
761
+ const container = document.createElement('div');
762
+ const frag = range.cloneContents();
763
+ container.appendChild(frag);
764
+ clipboardData.setData('text/html', container.innerHTML);
765
+ }
766
+
767
+ clipboardData.setData('text/plain', selection.getTextContent());
768
+ const namespace = editor._config.namespace;
769
+ clipboardData.setData('application/x-lexical-editor', JSON.stringify({
770
+ namespace,
771
+ state: $cloneContents(selection)
772
+ }));
773
+ }
774
+ }
775
+ });
776
+ }
777
+
778
+ /**
779
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
780
+ *
781
+ * This source code is licensed under the MIT license found in the
782
+ * LICENSE file in the root directory of this source tree.
783
+ *
784
+ *
785
+ */
786
+ function useLexicalDragonSupport(editor) {
787
+ React.useEffect(() => {
788
+ const handler = event => {
789
+ const rootElement = editor.getRootElement();
790
+
791
+ if (document.activeElement !== rootElement) {
792
+ return;
793
+ }
794
+
795
+ const data = event.data;
796
+
797
+ if (typeof data === 'string') {
798
+ let parsedData;
799
+
800
+ try {
801
+ parsedData = JSON.parse(data);
802
+ } catch (e) {
803
+ return;
804
+ }
805
+
806
+ if (parsedData && parsedData.protocol === 'nuanria_messaging' && parsedData.type === 'request') {
807
+ const payload = parsedData.payload;
808
+
809
+ if (payload && payload.functionId === 'makeChanges') {
810
+ const args = payload.args;
811
+
812
+ if (args) {
813
+ const [elementStart, elementLength, text, selStart, selLength, formatCommand] = args; // TODO: we should probably handle formatCommand somehow?
814
+ editor.update(() => {
815
+ lexical.$log('useLexicalDragonSupport');
816
+ const selection = lexical.$getSelection();
817
+
818
+ if (selection !== null) {
819
+ const anchor = selection.anchor;
820
+ let anchorNode = anchor.getNode();
821
+ let setSelStart = 0;
822
+ let setSelEnd = 0;
823
+
824
+ if (lexical.$isTextNode(anchorNode)) {
825
+ // set initial selection
826
+ if (elementStart >= 0 && elementLength >= 0) {
827
+ setSelStart = elementStart;
828
+ setSelEnd = elementStart + elementLength; // If the offset is more than the end, make it the end
829
+
830
+ selection.setTextNodeRange(anchorNode, setSelStart, anchorNode, setSelEnd);
831
+ }
832
+ }
833
+
834
+ if (setSelStart !== setSelEnd || text !== '') {
835
+ selection.insertRawText(text);
836
+ anchorNode = anchor.getNode();
837
+ }
838
+
839
+ if (lexical.$isTextNode(anchorNode)) {
840
+ // set final selection
841
+ setSelStart = selStart;
842
+ setSelEnd = selStart + selLength;
843
+ const anchorNodeTextLength = anchorNode.getTextContentSize(); // If the offset is more than the end, make it the end
844
+
845
+ setSelStart = setSelStart > anchorNodeTextLength ? anchorNodeTextLength : setSelStart;
846
+ setSelEnd = setSelEnd > anchorNodeTextLength ? anchorNodeTextLength : setSelEnd;
847
+ selection.setTextNodeRange(anchorNode, setSelStart, anchorNode, setSelEnd);
848
+ } // block the chrome extension from handling this event
849
+
850
+
851
+ event.stopImmediatePropagation();
852
+ }
853
+ });
854
+ }
855
+ }
856
+ }
857
+ }
858
+ };
859
+
860
+ window.addEventListener('message', handler, true);
861
+ return () => {
862
+ window.removeEventListener('message', handler, true);
863
+ };
864
+ }, [editor]);
865
+ }
866
+
867
+ /**
868
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
869
+ *
870
+ * This source code is licensed under the MIT license found in the
871
+ * LICENSE file in the root directory of this source tree.
872
+ *
873
+ *
874
+ */
875
+ const EditorPriority = 0;
876
+ function useRichTextSetup(editor) {
877
+ useLayoutEffect(() => {
878
+ const removeListener = editor.addListener('command', (type, payload) => {
879
+ const selection = lexical.$getSelection();
880
+
881
+ if (selection === null) {
882
+ return false;
883
+ }
884
+
885
+ switch (type) {
886
+ case 'deleteCharacter':
887
+ {
888
+ const isBackward = payload;
889
+ selection.deleteCharacter(isBackward);
890
+ return true;
891
+ }
892
+
893
+ case 'deleteWord':
894
+ {
895
+ const isBackward = payload;
896
+ selection.deleteWord(isBackward);
897
+ return true;
898
+ }
899
+
900
+ case 'deleteLine':
901
+ {
902
+ const isBackward = payload;
903
+ selection.deleteLine(isBackward);
904
+ return true;
905
+ }
906
+
907
+ case 'insertText':
908
+ {
909
+ const eventOrText = payload;
910
+
911
+ if (typeof eventOrText === 'string') {
912
+ selection.insertText(eventOrText);
913
+ } else {
914
+ const dataTransfer = eventOrText.dataTransfer;
915
+
916
+ if (dataTransfer != null) {
917
+ $insertDataTransferForRichText(dataTransfer, selection, editor);
918
+ } else {
919
+ const data = eventOrText.data;
920
+
921
+ if (data) {
922
+ selection.insertText(data);
923
+ }
924
+ }
925
+ }
926
+
927
+ return true;
928
+ }
929
+
930
+ case 'removeText':
931
+ selection.removeText();
932
+ return true;
933
+
934
+ case 'formatText':
935
+ {
936
+ const format = payload;
937
+ selection.formatText(format);
938
+ return true;
939
+ }
940
+
941
+ case 'formatElement':
942
+ {
943
+ const format = payload;
944
+ const node = selection.anchor.getNode();
945
+ const element = lexical.$isElementNode(node) ? node : node.getParentOrThrow();
946
+ element.setFormat(format);
947
+ return true;
948
+ }
949
+
950
+ case 'insertLineBreak':
951
+ const selectStart = payload;
952
+ selection.insertLineBreak(selectStart);
953
+ return true;
954
+
955
+ case 'insertParagraph':
956
+ selection.insertParagraph();
957
+ return true;
958
+
959
+ case 'indentContent':
960
+ {
961
+ // Handle code blocks
962
+ const anchor = selection.anchor;
963
+ const parentBlock = anchor.type === 'element' ? anchor.getNode() : anchor.getNode().getParentOrThrow();
964
+
965
+ if (parentBlock.canInsertTab()) {
966
+ editor.execCommand('insertText', '\t');
967
+ } else {
968
+ if (parentBlock.getIndent() !== 10) {
969
+ parentBlock.setIndent(parentBlock.getIndent() + 1);
970
+ }
971
+ }
972
+
973
+ return true;
974
+ }
975
+
976
+ case 'outdentContent':
977
+ {
978
+ // Handle code blocks
979
+ const anchor = selection.anchor;
980
+ const anchorNode = anchor.getNode();
981
+ const parentBlock = anchor.type === 'element' ? anchor.getNode() : anchor.getNode().getParentOrThrow();
982
+
983
+ if (parentBlock.canInsertTab()) {
984
+ const textContent = anchorNode.getTextContent();
985
+ const character = textContent[anchor.offset - 1];
986
+
987
+ if (character === '\t') {
988
+ editor.execCommand('deleteCharacter', true);
989
+ }
990
+ } else {
991
+ if (parentBlock.getIndent() !== 0) {
992
+ parentBlock.setIndent(parentBlock.getIndent() - 1);
993
+ }
994
+ }
995
+
996
+ return true;
997
+ }
998
+
999
+ case 'keyArrowLeft':
1000
+ {
1001
+ const event = payload;
1002
+ const isHoldingShift = event.shiftKey;
1003
+
1004
+ if ($shouldOverrideDefaultCharacterSelection(selection, true)) {
1005
+ event.preventDefault();
1006
+ $moveCharacter(selection, isHoldingShift, true);
1007
+ return true;
1008
+ }
1009
+
1010
+ return false;
1011
+ }
1012
+
1013
+ case 'keyArrowRight':
1014
+ {
1015
+ const event = payload;
1016
+ const isHoldingShift = event.shiftKey;
1017
+
1018
+ if ($shouldOverrideDefaultCharacterSelection(selection, false)) {
1019
+ event.preventDefault();
1020
+ $moveCharacter(selection, isHoldingShift, false);
1021
+ return true;
1022
+ }
1023
+
1024
+ return false;
1025
+ }
1026
+
1027
+ case 'keyBackspace':
1028
+ {
1029
+ const event = payload;
1030
+ event.preventDefault();
1031
+ const {
1032
+ anchor
1033
+ } = selection;
1034
+
1035
+ if (selection.isCollapsed() && anchor.offset === 0) {
1036
+ const element = anchor.type === 'element' ? anchor.getNode() : anchor.getNode().getParentOrThrow();
1037
+
1038
+ if (element.getIndent() > 0) {
1039
+ return editor.execCommand('outdentContent');
1040
+ }
1041
+ }
1042
+
1043
+ return editor.execCommand('deleteCharacter', true);
1044
+ }
1045
+
1046
+ case 'keyDelete':
1047
+ {
1048
+ const event = payload;
1049
+ event.preventDefault();
1050
+ return editor.execCommand('deleteCharacter', false);
1051
+ }
1052
+
1053
+ case 'keyEnter':
1054
+ {
1055
+ const event = payload;
1056
+ event.preventDefault();
1057
+
1058
+ if (event.shiftKey) {
1059
+ return editor.execCommand('insertLineBreak');
1060
+ }
1061
+
1062
+ return editor.execCommand('insertParagraph');
1063
+ }
1064
+
1065
+ case 'keyTab':
1066
+ {
1067
+ const event = payload;
1068
+ event.preventDefault();
1069
+ return editor.execCommand(event.shiftKey ? 'outdentContent' : 'indentContent');
1070
+ }
1071
+
1072
+ case 'keyEscape':
1073
+ {
1074
+ editor.blur();
1075
+ return true;
1076
+ }
1077
+
1078
+ case 'copy':
1079
+ {
1080
+ const event = payload;
1081
+ onCopyForRichText(event, editor);
1082
+ return true;
1083
+ }
1084
+
1085
+ case 'cut':
1086
+ {
1087
+ const event = payload;
1088
+ onCutForRichText(event, editor);
1089
+ return true;
1090
+ }
1091
+
1092
+ case 'paste':
1093
+ {
1094
+ const event = payload;
1095
+ onPasteForRichText(event, editor);
1096
+ return true;
1097
+ }
1098
+
1099
+ case 'drop':
1100
+ case 'dragstart':
1101
+ {
1102
+ // TODO: Make drag and drop work at some point.
1103
+ const event = payload;
1104
+ event.preventDefault();
1105
+ return true;
1106
+ }
1107
+ }
1108
+
1109
+ return false;
1110
+ }, EditorPriority);
1111
+ editor.execCommand('bootstrapEditor');
1112
+ return removeListener;
1113
+ }, [editor]);
1114
+ useLexicalDragonSupport(editor);
1115
+ }
1116
+
1117
+ /**
1118
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
1119
+ *
1120
+ * This source code is licensed under the MIT license found in the
1121
+ * LICENSE file in the root directory of this source tree.
1122
+ *
1123
+ *
1124
+ */
1125
+ function RichTextPlugin({
1126
+ contentEditable,
1127
+ placeholder
1128
+ }) {
1129
+ const [editor] = LexicalComposerContext.useLexicalComposerContext();
1130
+ const showPlaceholder = useLexicalCanShowPlaceholder(editor);
1131
+ useRichTextSetup(editor);
1132
+ const decorators = useDecorators(editor);
1133
+ return /*#__PURE__*/React.createElement(React.Fragment, null, contentEditable, showPlaceholder && placeholder, decorators);
1134
+ }
1135
+
1136
+ module.exports = RichTextPlugin;