@lexical/react 0.1.8 → 0.1.11

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 (91) hide show
  1. package/DEPRECATED_useLexical.dev.js +3 -38
  2. package/DEPRECATED_useLexical.prod.js +1 -2
  3. package/DEPRECATED_useLexicalAutoFormatter.dev.js +202 -74
  4. package/DEPRECATED_useLexicalAutoFormatter.prod.js +21 -16
  5. package/DEPRECATED_useLexicalCanShowPlaceholder.prod.js +1 -1
  6. package/DEPRECATED_useLexicalCharacterLimit.dev.js +23 -21
  7. package/DEPRECATED_useLexicalCharacterLimit.prod.js +8 -8
  8. package/DEPRECATED_useLexicalDecorators.prod.js +1 -1
  9. package/DEPRECATED_useLexicalEditor.dev.js +1 -25
  10. package/DEPRECATED_useLexicalEditor.prod.js +1 -1
  11. package/DEPRECATED_useLexicalEditorEvents.prod.js +1 -1
  12. package/DEPRECATED_useLexicalHistory.dev.js +18 -15
  13. package/DEPRECATED_useLexicalHistory.prod.js +7 -7
  14. package/DEPRECATED_useLexicalList.dev.js +6 -0
  15. package/DEPRECATED_useLexicalList.prod.js +1 -1
  16. package/DEPRECATED_useLexicalPlainText.dev.js +79 -70
  17. package/DEPRECATED_useLexicalPlainText.prod.js +15 -16
  18. package/DEPRECATED_useLexicalRichText.dev.js +124 -250
  19. package/DEPRECATED_useLexicalRichText.prod.js +25 -29
  20. package/LexicalAutoFormatterPlugin.dev.js +202 -74
  21. package/LexicalAutoFormatterPlugin.js.flow +10 -0
  22. package/LexicalAutoFormatterPlugin.prod.js +21 -17
  23. package/LexicalAutoLinkPlugin.js.flow +23 -0
  24. package/LexicalAutoLinkPlugin.prod.js +4 -4
  25. package/LexicalCharacterLimitPlugin.dev.js +23 -21
  26. package/LexicalCharacterLimitPlugin.js.flow +12 -0
  27. package/LexicalCharacterLimitPlugin.prod.js +9 -8
  28. package/LexicalClearEditorPlugin.dev.js +52 -0
  29. package/LexicalClearEditorPlugin.js +9 -0
  30. package/LexicalClearEditorPlugin.js.flow +14 -0
  31. package/LexicalClearEditorPlugin.prod.js +7 -0
  32. package/LexicalCollaborationPlugin.dev.js +31 -36
  33. package/LexicalCollaborationPlugin.js.flow +55 -0
  34. package/LexicalCollaborationPlugin.prod.js +7 -8
  35. package/LexicalComposer.dev.js +8 -6
  36. package/LexicalComposer.js.flow +23 -0
  37. package/LexicalComposer.prod.js +3 -3
  38. package/LexicalComposerContext.js.flow +27 -0
  39. package/LexicalComposerContext.prod.js +1 -1
  40. package/LexicalContentEditable.dev.js +14 -8
  41. package/LexicalContentEditable.js.flow +35 -0
  42. package/LexicalContentEditable.prod.js +3 -3
  43. package/LexicalHashtagPlugin.js.flow +20 -0
  44. package/LexicalHashtagPlugin.prod.js +1 -1
  45. package/LexicalHistoryPlugin.dev.js +18 -15
  46. package/LexicalHistoryPlugin.js.flow +34 -0
  47. package/LexicalHistoryPlugin.prod.js +7 -7
  48. package/LexicalHorizontalRuleNode.dev.js +66 -0
  49. package/LexicalHorizontalRuleNode.js +9 -0
  50. package/LexicalHorizontalRuleNode.js.flow +25 -0
  51. package/LexicalHorizontalRuleNode.prod.js +8 -0
  52. package/LexicalLinkPlugin.dev.js +0 -1
  53. package/LexicalLinkPlugin.js.flow +10 -0
  54. package/LexicalLinkPlugin.prod.js +3 -3
  55. package/LexicalListPlugin.dev.js +6 -0
  56. package/LexicalListPlugin.js.flow +10 -0
  57. package/LexicalListPlugin.prod.js +2 -2
  58. package/LexicalNestedComposer.js.flow +21 -0
  59. package/LexicalNestedComposer.prod.js +1 -1
  60. package/LexicalOnChangePlugin.js.flow +14 -0
  61. package/LexicalOnChangePlugin.prod.js +1 -1
  62. package/LexicalPlainTextPlugin.dev.js +72 -44
  63. package/LexicalPlainTextPlugin.js.flow +18 -0
  64. package/LexicalPlainTextPlugin.prod.js +12 -11
  65. package/LexicalRichTextPlugin.dev.js +115 -222
  66. package/LexicalRichTextPlugin.js.flow +18 -0
  67. package/LexicalRichTextPlugin.prod.js +21 -25
  68. package/LexicalTablePlugin.dev.js +43 -39
  69. package/LexicalTablePlugin.js.flow +10 -0
  70. package/LexicalTablePlugin.prod.js +4 -3
  71. package/LexicalTreeView.dev.js +10 -2
  72. package/LexicalTreeView.js.flow +19 -0
  73. package/LexicalTreeView.prod.js +9 -8
  74. package/README.md +0 -1
  75. package/package.json +5 -4
  76. package/useLexicalDecoratorMap.js.flow +16 -0
  77. package/useLexicalDecoratorMap.prod.js +1 -1
  78. package/useLexicalIsTextContentEmpty.js.flow +15 -0
  79. package/useLexicalIsTextContentEmpty.prod.js +1 -1
  80. package/useLexicalNodeSelection.dev.js +70 -0
  81. package/useLexicalNodeSelection.js +9 -0
  82. package/useLexicalNodeSelection.js.flow +14 -0
  83. package/useLexicalNodeSelection.prod.js +8 -0
  84. package/withSubscriptions.js.flow +13 -0
  85. package/withSubscriptions.prod.js +1 -1
  86. package/LexicalBootstrapPlugin.dev.js +0 -124
  87. package/LexicalBootstrapPlugin.js +0 -9
  88. package/LexicalBootstrapPlugin.prod.js +0 -8
  89. package/LexicalHorizontalRulePlugin.dev.js +0 -51
  90. package/LexicalHorizontalRulePlugin.js +0 -9
  91. package/LexicalHorizontalRulePlugin.prod.js +0 -7
@@ -9,10 +9,6 @@
9
9
  var withSubscriptions = require('@lexical/react/withSubscriptions');
10
10
  var lexical = require('lexical');
11
11
  var react = require('react');
12
- var list = require('@lexical/list');
13
- var CodeNode = require('lexical/CodeNode');
14
- var HeadingNode = require('lexical/HeadingNode');
15
- var LinkNode = require('lexical/LinkNode');
16
12
 
17
13
  /**
18
14
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -22,15 +18,15 @@ var LinkNode = require('lexical/LinkNode');
22
18
  *
23
19
  *
24
20
  */
25
- const MERGE = 0;
26
- const NO_MERGE = 1;
27
- const DISCARD = 2;
21
+ const HISTORY_MERGE = 0;
22
+ const HISTORY_PUSH = 1;
23
+ const DISCARD_HISTORY_CANDIDATE = 2;
28
24
  const OTHER = 0;
29
25
  const COMPOSING_CHARACTER = 1;
30
26
  const INSERT_CHARACTER_AFTER_SELECTION = 2;
31
27
  const DELETE_CHARACTER_BEFORE_SELECTION = 3;
32
28
  const DELETE_CHARACTER_AFTER_SELECTION = 4;
33
- const EditorPriority$1 = 0;
29
+ const EditorPriority = 0;
34
30
 
35
31
  function getDirtyNodes(editorState, dirtyLeavesSet, dirtyElementsSet) {
36
32
  const dirtyLeaves = Array.from(dirtyLeavesSet);
@@ -77,7 +73,7 @@ function getChangeType(prevEditorState, nextEditorState, dirtyLeavesSet, dirtyEl
77
73
  return COMPOSING_CHARACTER;
78
74
  }
79
75
 
80
- if (nextSelection === null || prevSelection === null || !prevSelection.isCollapsed() || !nextSelection.isCollapsed()) {
76
+ if (!lexical.$isRangeSelection(nextSelection) || !lexical.$isRangeSelection(prevSelection) || !prevSelection.isCollapsed() || !nextSelection.isCollapsed()) {
81
77
  return OTHER;
82
78
  }
83
79
 
@@ -152,18 +148,21 @@ function createMergeActionGetter(editor, delay) {
152
148
  if (tags.has('historic')) {
153
149
  prevChangeType = OTHER;
154
150
  prevChangeTime = changeTime;
155
- return DISCARD;
151
+ return DISCARD_HISTORY_CANDIDATE;
156
152
  }
157
153
 
158
154
  const changeType = getChangeType(prevEditorState, nextEditorState, dirtyLeaves, dirtyElements, editor.isComposing());
159
155
 
160
156
  const mergeAction = (() => {
161
- if (tags.has('without-history')) {
162
- return MERGE;
157
+ const shouldPushHistory = tags.has('history-push');
158
+ const shouldMergeHistory = !shouldPushHistory && tags.has('history-merge');
159
+
160
+ if (shouldMergeHistory) {
161
+ return HISTORY_MERGE;
163
162
  }
164
163
 
165
164
  if (prevEditorState === null) {
166
- return NO_MERGE;
165
+ return HISTORY_PUSH;
167
166
  }
168
167
 
169
168
  const selection = nextEditorState._selection;
@@ -172,19 +171,19 @@ function createMergeActionGetter(editor, delay) {
172
171
 
173
172
  if (!hasDirtyNodes) {
174
173
  if (prevSelection === null && selection !== null) {
175
- return MERGE;
174
+ return HISTORY_MERGE;
176
175
  }
177
176
 
178
- return DISCARD;
177
+ return DISCARD_HISTORY_CANDIDATE;
179
178
  }
180
179
 
181
180
  const isSameEditor = currentHistoryEntry === null || currentHistoryEntry.editor === editor;
182
181
 
183
- if (changeType !== OTHER && changeType === prevChangeType && changeTime < prevChangeTime + delay && isSameEditor) {
184
- return MERGE;
182
+ if (shouldPushHistory === false && changeType !== OTHER && changeType === prevChangeType && changeTime < prevChangeTime + delay && isSameEditor) {
183
+ return HISTORY_MERGE;
185
184
  }
186
185
 
187
- return NO_MERGE;
186
+ return HISTORY_PUSH;
188
187
  })();
189
188
 
190
189
  prevChangeTime = changeTime;
@@ -221,7 +220,7 @@ function useHistory(editor, externalHistoryState, delay = 1000) {
221
220
 
222
221
  const mergeAction = getMergeAction(prevEditorState, editorState, current, dirtyLeaves, dirtyElements, tags);
223
222
 
224
- if (mergeAction === NO_MERGE) {
223
+ if (mergeAction === HISTORY_PUSH) {
225
224
  if (redoStack.length !== 0) {
226
225
  historyState.redoStack = [];
227
226
  }
@@ -232,7 +231,7 @@ function useHistory(editor, externalHistoryState, delay = 1000) {
232
231
  });
233
232
  editor.execCommand('canUndo', true);
234
233
  }
235
- } else if (mergeAction === DISCARD) {
234
+ } else if (mergeAction === DISCARD_HISTORY_CANDIDATE) {
236
235
  return;
237
236
  } // Else we merge
238
237
 
@@ -316,7 +315,7 @@ function useHistory(editor, externalHistoryState, delay = 1000) {
316
315
  }
317
316
  };
318
317
 
319
- return withSubscriptions(editor.addListener('command', applyCommand, EditorPriority$1), editor.addListener('update', applyChange));
318
+ return withSubscriptions(editor.addListener('command', applyCommand, EditorPriority), editor.addListener('update', applyChange));
320
319
  }, [clearHistory, delay, editor, historyState]);
321
320
  }
322
321
  function createEmptyHistoryState() {
@@ -436,6 +435,12 @@ function $copyLeafNodeBranchToRoot(leaf, startingOffset, isLeftSide, range, node
436
435
  }
437
436
 
438
437
  function $cloneContents(selection) {
438
+ if (!lexical.$isRangeSelection(selection)) {
439
+ {
440
+ throw Error(`TODO`);
441
+ }
442
+ }
443
+
439
444
  const anchor = selection.anchor;
440
445
  const focus = selection.focus;
441
446
  const anchorOffset = anchor.getCharacterOffset();
@@ -473,7 +478,7 @@ function $cloneContents(selection) {
473
478
  const firstNode = nodes[0];
474
479
  const firstNodeParent = firstNode.getParent();
475
480
 
476
- if (firstNodeParent !== null && !firstNodeParent.canBeEmpty()) {
481
+ if (firstNodeParent !== null && (!firstNodeParent.canBeEmpty() || lexical.$isRootNode(firstNodeParent))) {
477
482
  const parentChildren = firstNodeParent.__children;
478
483
  const parentChildrenLength = parentChildren.length;
479
484
 
@@ -529,7 +534,7 @@ function $moveCaretSelection(selection, isHoldingShift, isBackward, granularity)
529
534
  }
530
535
  function $isParentElementRTL(selection) {
531
536
  const anchorNode = selection.anchor.getNode();
532
- const parent = anchorNode.getParentOrThrow();
537
+ const parent = lexical.$isRootNode(anchorNode) ? anchorNode : anchorNode.getParentOrThrow();
533
538
  return parent.getDirection() === 'rtl';
534
539
  }
535
540
  function $moveCharacter(selection, isHoldingShift, isBackward) {
@@ -545,195 +550,6 @@ function $moveCharacter(selection, isHoldingShift, isBackward) {
545
550
  *
546
551
  *
547
552
  */
548
- function getPossibleDecoratorNode(focus, isBackward) {
549
- const focusOffset = focus.offset;
550
-
551
- if (focus.type === 'element') {
552
- const block = focus.getNode();
553
- return block.getChildAtIndex(isBackward ? focusOffset - 1 : focusOffset);
554
- } else {
555
- const focusNode = focus.getNode();
556
-
557
- if (isBackward && focusOffset === 0 || !isBackward && focusOffset === focusNode.getTextContentSize()) {
558
- return isBackward ? focusNode.getPreviousSibling() : focusNode.getNextSibling();
559
- }
560
- }
561
-
562
- return null;
563
- }
564
-
565
- /**
566
- * Copyright (c) Meta Platforms, Inc. and affiliates.
567
- *
568
- * This source code is licensed under the MIT license found in the
569
- * LICENSE file in the root directory of this source tree.
570
- *
571
- *
572
- */
573
-
574
- const isCodeElement = div => {
575
- return div.style.fontFamily.match('monospace') !== null;
576
- };
577
-
578
- const DOM_NODE_NAME_TO_LEXICAL_NODE = {
579
- '#text': domNode => ({
580
- node: lexical.$createTextNode(domNode.textContent)
581
- }),
582
- a: domNode => {
583
- let node;
584
-
585
- if (domNode instanceof HTMLAnchorElement) {
586
- node = LinkNode.$createLinkNode(domNode.href);
587
- } else {
588
- node = lexical.$createTextNode(domNode.textContent);
589
- }
590
-
591
- return {
592
- node
593
- };
594
- },
595
- b: domNode => {
596
- // $FlowFixMe[incompatible-type] domNode is a <b> since we matched it by nodeName
597
- const b = domNode; // Google Docs wraps all copied HTML in a <b> with font-weight normal
598
-
599
- const hasNormalFontWeight = b.style.fontWeight === 'normal';
600
- return {
601
- forChild: lexicalNode => {
602
- if (lexical.$isTextNode(lexicalNode) && !hasNormalFontWeight) {
603
- lexicalNode.toggleFormat('bold');
604
- }
605
- },
606
- node: null
607
- };
608
- },
609
- br: () => ({
610
- node: lexical.$createLineBreakNode()
611
- }),
612
- div: domNode => {
613
- // $FlowFixMe[incompatible-type] domNode is a <div> since we matched it by nodeName
614
- const div = domNode;
615
- return {
616
- after: childLexicalNodes => {
617
- const domParent = domNode.parentNode;
618
-
619
- if (domParent != null && domNode !== domParent.lastChild) {
620
- childLexicalNodes.push(lexical.$createLineBreakNode());
621
- }
622
-
623
- return childLexicalNodes;
624
- },
625
- node: isCodeElement(div) ? CodeNode.$createCodeNode() : null
626
- };
627
- },
628
- em: domNode => {
629
- return {
630
- forChild: lexicalNode => {
631
- if (lexical.$isTextNode(lexicalNode)) {
632
- lexicalNode.toggleFormat('italic');
633
- }
634
- },
635
- node: null
636
- };
637
- },
638
- h1: () => ({
639
- node: HeadingNode.$createHeadingNode('h1')
640
- }),
641
- h2: () => ({
642
- node: HeadingNode.$createHeadingNode('h2')
643
- }),
644
- h3: () => ({
645
- node: HeadingNode.$createHeadingNode('h3')
646
- }),
647
- h4: () => ({
648
- node: HeadingNode.$createHeadingNode('h4')
649
- }),
650
- h5: () => ({
651
- node: HeadingNode.$createHeadingNode('h5')
652
- }),
653
- i: domNode => {
654
- return {
655
- forChild: lexicalNode => {
656
- if (lexical.$isTextNode(lexicalNode)) {
657
- lexicalNode.toggleFormat('italic');
658
- }
659
- },
660
- node: null
661
- };
662
- },
663
- li: () => ({
664
- node: list.$createListItemNode()
665
- }),
666
- ol: () => ({
667
- node: list.$createListNode('ol')
668
- }),
669
- p: () => ({
670
- node: lexical.$createParagraphNode()
671
- }),
672
- pre: domNode => ({
673
- node: CodeNode.$createCodeNode()
674
- }),
675
- span: domNode => {
676
- // $FlowFixMe[incompatible-type] domNode is a <span> since we matched it by nodeName
677
- const span = domNode; // Google Docs uses span tags + font-weight for bold text
678
-
679
- const hasBoldFontWeight = span.style.fontWeight === '700';
680
- return {
681
- forChild: lexicalNode => {
682
- if (lexical.$isTextNode(lexicalNode) && hasBoldFontWeight) {
683
- lexicalNode.toggleFormat('bold');
684
- }
685
- },
686
- node: null
687
- };
688
- },
689
- strong: domNode => {
690
- return {
691
- forChild: lexicalNode => {
692
- if (lexical.$isTextNode(lexicalNode)) {
693
- lexicalNode.toggleFormat('bold');
694
- }
695
- },
696
- node: null
697
- };
698
- },
699
- table: domNode => {
700
- // $FlowFixMe[incompatible-type] domNode is a <table> since we matched it by nodeName
701
- const table = domNode;
702
- const isGitHubCodeTable = table.classList.contains('js-file-line-container');
703
- return {
704
- node: isGitHubCodeTable ? CodeNode.$createCodeNode() : null
705
- };
706
- },
707
- td: domNode => {
708
- // $FlowFixMe[incompatible-type] domNode is a <table> since we matched it by nodeName
709
- const cell = domNode;
710
- const isGitHubCodeCell = cell.classList.contains('js-file-line');
711
- return {
712
- after: childLexicalNodes => {
713
- if (isGitHubCodeCell && cell.parentNode && cell.parentNode.nextSibling) {
714
- // Append newline between code lines
715
- childLexicalNodes.push(lexical.$createLineBreakNode());
716
- }
717
-
718
- return childLexicalNodes;
719
- },
720
- node: null
721
- };
722
- },
723
- u: domNode => {
724
- return {
725
- forChild: lexicalNode => {
726
- if (lexical.$isTextNode(lexicalNode)) {
727
- lexicalNode.toggleFormat('underline');
728
- }
729
- },
730
- node: null
731
- };
732
- },
733
- ul: () => ({
734
- node: list.$createListNode('ul')
735
- })
736
- };
737
553
 
738
554
  function $generateNodes(nodeRange) {
739
555
  const {
@@ -756,12 +572,34 @@ function $generateNodes(nodeRange) {
756
572
  return nodes;
757
573
  }
758
574
 
759
- function $createNodesFromDOM(node, conversionMap, editor, forChildMap = new Map()) {
575
+ function getConversionFunction(domNode, editor) {
576
+ const {
577
+ nodeName
578
+ } = domNode;
579
+
580
+ const cachedConversions = editor._htmlConversions.get(nodeName.toLowerCase());
581
+
582
+ let currentConversion = null;
583
+
584
+ if (cachedConversions !== undefined) {
585
+ cachedConversions.forEach(cachedConversion => {
586
+ const domConversion = cachedConversion(domNode);
587
+
588
+ if (domConversion !== null) {
589
+ if (currentConversion === null || currentConversion.priority < domConversion.priority) {
590
+ currentConversion = domConversion;
591
+ }
592
+ }
593
+ });
594
+ }
595
+
596
+ return currentConversion !== null ? currentConversion.conversion : null;
597
+ }
598
+
599
+ function $createNodesFromDOM(node, editor, forChildMap = new Map()) {
760
600
  let lexicalNodes = [];
761
601
  let currentLexicalNode = null;
762
- const nodeName = node.nodeName.toLowerCase();
763
- const customHtmlTransforms = editor._config.htmlTransforms || {};
764
- const transformFunction = customHtmlTransforms[nodeName] || conversionMap[nodeName];
602
+ const transformFunction = getConversionFunction(node, editor);
765
603
  const transformOutput = transformFunction ? transformFunction(node) : null;
766
604
  let postTransform = null;
767
605
 
@@ -779,7 +617,7 @@ function $createNodesFromDOM(node, conversionMap, editor, forChildMap = new Map(
779
617
  }
780
618
 
781
619
  if (transformOutput.forChild != null) {
782
- forChildMap.set(nodeName, transformOutput.forChild);
620
+ forChildMap.set(node.nodeName, transformOutput.forChild);
783
621
  }
784
622
  } // If the DOM node doesn't have a transformer, we don't know what
785
623
  // to do with it but we still need to process any childNodes.
@@ -789,7 +627,7 @@ function $createNodesFromDOM(node, conversionMap, editor, forChildMap = new Map(
789
627
  let childLexicalNodes = [];
790
628
 
791
629
  for (let i = 0; i < children.length; i++) {
792
- childLexicalNodes.push(...$createNodesFromDOM(children[i], conversionMap, editor, forChildMap));
630
+ childLexicalNodes.push(...$createNodesFromDOM(children[i], editor, forChildMap));
793
631
  }
794
632
 
795
633
  if (postTransform != null) {
@@ -811,13 +649,13 @@ function $createNodesFromDOM(node, conversionMap, editor, forChildMap = new Map(
811
649
  return lexicalNodes;
812
650
  }
813
651
 
814
- function $generateNodesFromDOM(dom, conversionMap, editor) {
652
+ function $generateNodesFromDOM(dom, editor) {
815
653
  let lexicalNodes = [];
816
654
  const elements = dom.body ? Array.from(dom.body.childNodes) : [];
817
655
  const elementsLength = elements.length;
818
656
 
819
657
  for (let i = 0; i < elementsLength; i++) {
820
- const lexicalNode = $createNodesFromDOM(elements[i], conversionMap, editor);
658
+ const lexicalNode = $createNodesFromDOM(elements[i], editor);
821
659
 
822
660
  if (lexicalNode !== null) {
823
661
  lexicalNodes = lexicalNodes.concat(lexicalNode);
@@ -852,7 +690,7 @@ function $insertDataTransferForRichText(dataTransfer, selection, editor) {
852
690
  if (htmlString) {
853
691
  const parser = new DOMParser();
854
692
  const dom = parser.parseFromString(htmlString, textHtmlMimeType);
855
- 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
693
+ const nodes = $generateNodesFromDOM(dom, editor); // Wrap text and inline nodes in paragraph nodes so we have all blocks at the top-level
856
694
 
857
695
  const topLevelBlocks = [];
858
696
  let currentBlock = null;
@@ -889,17 +727,16 @@ function $insertDataTransferForPlainText(dataTransfer, selection) {
889
727
  }
890
728
  }
891
729
  function $shouldOverrideDefaultCharacterSelection(selection, isBackward) {
892
- const possibleDecoratorNode = getPossibleDecoratorNode(selection.focus, isBackward);
893
- return lexical.$isDecoratorNode(possibleDecoratorNode);
730
+ const possibleNode = lexical.$getDecoratorNode(selection.focus, isBackward);
731
+ return lexical.$isDecoratorNode(possibleNode) && !possibleNode.isIsolated();
894
732
  }
895
733
  function onPasteForRichText(event, editor) {
896
734
  event.preventDefault();
897
735
  editor.update(() => {
898
- lexical.$log('onPasteForRichText');
899
736
  const selection = lexical.$getSelection();
900
737
  const clipboardData = event.clipboardData;
901
738
 
902
- if (clipboardData != null && selection !== null) {
739
+ if (clipboardData != null && lexical.$isRangeSelection(selection)) {
903
740
  $insertDataTransferForRichText(clipboardData, selection, editor);
904
741
  }
905
742
  });
@@ -907,10 +744,9 @@ function onPasteForRichText(event, editor) {
907
744
  function onCutForRichText(event, editor) {
908
745
  onCopyForRichText(event, editor);
909
746
  editor.update(() => {
910
- lexical.$log('onCutForRichText');
911
747
  const selection = lexical.$getSelection();
912
748
 
913
- if (selection !== null) {
749
+ if (lexical.$isRangeSelection(selection)) {
914
750
  selection.removeText();
915
751
  }
916
752
  });
@@ -918,7 +754,6 @@ function onCutForRichText(event, editor) {
918
754
  function onCopyForRichText(event, editor) {
919
755
  event.preventDefault();
920
756
  editor.update(() => {
921
- lexical.$log('onCopyForRichText');
922
757
  const clipboardData = event.clipboardData;
923
758
  const selection = lexical.$getSelection();
924
759
 
@@ -958,18 +793,53 @@ function onCopyForRichText(event, editor) {
958
793
  *
959
794
  *
960
795
  */
961
- const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
796
+ // Convoluted logic to make this work with Flow. Order matters.
797
+ const options = {
798
+ tag: 'history-merge'
799
+ };
800
+ const setEditorOptions = options;
801
+ const updateOptions = options;
802
+ function initializeEditor(editor, initialEditorState) {
803
+ if (initialEditorState === null) {
804
+ return;
805
+ } else if (initialEditorState === undefined) {
806
+ editor.update(() => {
807
+ const root = lexical.$getRoot();
808
+ const firstChild = root.getFirstChild();
809
+
810
+ if (firstChild === null) {
811
+ const paragraph = lexical.$createParagraphNode();
812
+ root.append(paragraph);
813
+ const activeElement = document.activeElement;
814
+
815
+ if (lexical.$getSelection() !== null || activeElement !== null && activeElement === editor.getRootElement()) {
816
+ paragraph.select();
817
+ }
818
+ }
819
+ }, updateOptions);
820
+ } else if (initialEditorState !== null) {
821
+ switch (typeof initialEditorState) {
822
+ case 'string':
823
+ {
824
+ const parsedEditorState = editor.parseEditorState(initialEditorState);
825
+ editor.setEditorState(parsedEditorState, setEditorOptions);
826
+ break;
827
+ }
962
828
 
963
- /**
964
- * Copyright (c) Meta Platforms, Inc. and affiliates.
965
- *
966
- * This source code is licensed under the MIT license found in the
967
- * LICENSE file in the root directory of this source tree.
968
- *
969
- *
970
- */
971
- const useLayoutEffectImpl = CAN_USE_DOM ? react.useLayoutEffect : react.useEffect;
972
- var useLayoutEffect = useLayoutEffectImpl;
829
+ case 'object':
830
+ {
831
+ editor.setEditorState(initialEditorState, setEditorOptions);
832
+ break;
833
+ }
834
+
835
+ case 'function':
836
+ {
837
+ editor.update(initialEditorState, updateOptions);
838
+ break;
839
+ }
840
+ }
841
+ }
842
+ }
973
843
 
974
844
  /**
975
845
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -1008,10 +878,9 @@ function useLexicalDragonSupport(editor) {
1008
878
  if (args) {
1009
879
  const [elementStart, elementLength, text, selStart, selLength, formatCommand] = args; // TODO: we should probably handle formatCommand somehow?
1010
880
  editor.update(() => {
1011
- lexical.$log('useLexicalDragonSupport');
1012
881
  const selection = lexical.$getSelection();
1013
882
 
1014
- if (selection !== null) {
883
+ if (lexical.$isRangeSelection(selection)) {
1015
884
  const anchor = selection.anchor;
1016
885
  let anchorNode = anchor.getNode();
1017
886
  let setSelStart = 0;
@@ -1068,13 +937,17 @@ function useLexicalDragonSupport(editor) {
1068
937
  *
1069
938
  *
1070
939
  */
1071
- const EditorPriority = 0;
1072
- function useRichTextSetup(editor) {
1073
- useLayoutEffect(() => {
940
+ function useRichTextSetup(editor, initialEditorState) {
941
+ react.useLayoutEffect(() => {
1074
942
  const removeListener = editor.addListener('command', (type, payload) => {
1075
943
  const selection = lexical.$getSelection();
1076
944
 
1077
- if (selection === null) {
945
+ if (type === 'click' && lexical.$isNodeSelection(selection)) {
946
+ selection.clear();
947
+ return true;
948
+ }
949
+
950
+ if (!lexical.$isRangeSelection(selection)) {
1078
951
  return false;
1079
952
  }
1080
953
 
@@ -1303,9 +1176,10 @@ function useRichTextSetup(editor) {
1303
1176
  }
1304
1177
 
1305
1178
  return false;
1306
- }, EditorPriority);
1307
- editor.execCommand('bootstrapEditor');
1308
- return removeListener;
1179
+ }, 0);
1180
+ initializeEditor(editor, initialEditorState);
1181
+ return removeListener; // We only do this for init
1182
+ // eslint-disable-next-line react-hooks/exhaustive-deps
1309
1183
  }, [editor]);
1310
1184
  useLexicalDragonSupport(editor);
1311
1185
  }
@@ -1318,8 +1192,8 @@ function useRichTextSetup(editor) {
1318
1192
  *
1319
1193
  *
1320
1194
  */
1321
- function useLexicalRichText(editor, externalHistoryState) {
1322
- useRichTextSetup(editor);
1195
+ function useLexicalRichText(editor, externalHistoryState, initialEditorState) {
1196
+ useRichTextSetup(editor, initialEditorState);
1323
1197
  useLexicalHistory(editor, externalHistoryState);
1324
1198
  }
1325
1199