@lexical/react 0.1.10 → 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 (59) hide show
  1. package/DEPRECATED_useLexicalAutoFormatter.dev.js +69 -28
  2. package/DEPRECATED_useLexicalAutoFormatter.prod.js +19 -18
  3. package/DEPRECATED_useLexicalCharacterLimit.dev.js +20 -17
  4. package/DEPRECATED_useLexicalCharacterLimit.prod.js +8 -8
  5. package/DEPRECATED_useLexicalList.dev.js +6 -0
  6. package/DEPRECATED_useLexicalList.prod.js +1 -1
  7. package/DEPRECATED_useLexicalPlainText.dev.js +61 -58
  8. package/DEPRECATED_useLexicalPlainText.prod.js +15 -15
  9. package/DEPRECATED_useLexicalRichText.dev.js +64 -61
  10. package/DEPRECATED_useLexicalRichText.prod.js +24 -25
  11. package/LexicalAutoFormatterPlugin.dev.js +69 -28
  12. package/LexicalAutoFormatterPlugin.js.flow +10 -0
  13. package/LexicalAutoFormatterPlugin.prod.js +20 -19
  14. package/LexicalAutoLinkPlugin.js.flow +23 -0
  15. package/LexicalCharacterLimitPlugin.dev.js +20 -17
  16. package/LexicalCharacterLimitPlugin.js.flow +12 -0
  17. package/LexicalCharacterLimitPlugin.prod.js +9 -8
  18. package/LexicalClearEditorPlugin.dev.js +52 -0
  19. package/LexicalClearEditorPlugin.js +9 -0
  20. package/LexicalClearEditorPlugin.js.flow +14 -0
  21. package/LexicalClearEditorPlugin.prod.js +7 -0
  22. package/LexicalCollaborationPlugin.dev.js +21 -11
  23. package/LexicalCollaborationPlugin.js.flow +55 -0
  24. package/LexicalCollaborationPlugin.prod.js +7 -7
  25. package/LexicalComposer.dev.js +7 -0
  26. package/LexicalComposer.js.flow +23 -0
  27. package/LexicalComposer.prod.js +3 -2
  28. package/LexicalComposerContext.js.flow +27 -0
  29. package/LexicalContentEditable.dev.js +14 -8
  30. package/LexicalContentEditable.js.flow +35 -0
  31. package/LexicalContentEditable.prod.js +3 -3
  32. package/LexicalHashtagPlugin.js.flow +20 -0
  33. package/LexicalHistoryPlugin.js.flow +34 -0
  34. package/LexicalHorizontalRuleNode.js.flow +25 -0
  35. package/LexicalLinkPlugin.js.flow +10 -0
  36. package/LexicalListPlugin.dev.js +6 -0
  37. package/LexicalListPlugin.js.flow +10 -0
  38. package/LexicalListPlugin.prod.js +2 -2
  39. package/LexicalNestedComposer.js.flow +21 -0
  40. package/LexicalOnChangePlugin.js.flow +14 -0
  41. package/LexicalPlainTextPlugin.dev.js +64 -60
  42. package/LexicalPlainTextPlugin.js.flow +18 -0
  43. package/LexicalPlainTextPlugin.prod.js +12 -12
  44. package/LexicalRichTextPlugin.dev.js +65 -61
  45. package/LexicalRichTextPlugin.js.flow +18 -0
  46. package/LexicalRichTextPlugin.prod.js +19 -20
  47. package/LexicalTablePlugin.dev.js +22 -0
  48. package/LexicalTablePlugin.js.flow +10 -0
  49. package/LexicalTablePlugin.prod.js +4 -3
  50. package/LexicalTreeView.js.flow +19 -0
  51. package/README.md +0 -1
  52. package/package.json +5 -5
  53. package/useLexicalDecoratorMap.js.flow +16 -0
  54. package/useLexicalIsTextContentEmpty.js.flow +15 -0
  55. package/useLexicalNodeSelection.js.flow +14 -0
  56. package/withSubscriptions.js.flow +13 -0
  57. package/LexicalBootstrapPlugin.dev.js +0 -122
  58. package/LexicalBootstrapPlugin.js +0 -9
  59. package/LexicalBootstrapPlugin.prod.js +0 -8
@@ -542,55 +542,6 @@ function $moveCharacter(selection, isHoldingShift, isBackward) {
542
542
  $moveCaretSelection(selection, isHoldingShift, isBackward ? !isRTL : isRTL, 'character');
543
543
  }
544
544
 
545
- /**
546
- * Copyright (c) Meta Platforms, Inc. and affiliates.
547
- *
548
- * This source code is licensed under the MIT license found in the
549
- * LICENSE file in the root directory of this source tree.
550
- *
551
- *
552
- */
553
- function resolveElement(element, isBackward, focusOffset) {
554
- const parent = element.getParent();
555
- let offset = focusOffset;
556
- let block = element;
557
-
558
- if (parent !== null) {
559
- if (isBackward && focusOffset === 0) {
560
- offset = block.getIndexWithinParent();
561
- block = parent;
562
- } else if (!isBackward && focusOffset === block.getChildrenSize()) {
563
- offset = block.getIndexWithinParent() + 1;
564
- block = parent;
565
- }
566
- }
567
-
568
- return block.getChildAtIndex(isBackward ? offset - 1 : offset);
569
- }
570
-
571
- function getPossibleDecoratorNode(focus, isBackward) {
572
- const focusOffset = focus.offset;
573
-
574
- if (focus.type === 'element') {
575
- const block = focus.getNode();
576
- return resolveElement(block, isBackward, focusOffset);
577
- } else {
578
- const focusNode = focus.getNode();
579
-
580
- if (isBackward && focusOffset === 0 || !isBackward && focusOffset === focusNode.getTextContentSize()) {
581
- const possibleNode = isBackward ? focusNode.getPreviousSibling() : focusNode.getNextSibling();
582
-
583
- if (possibleNode === null) {
584
- return resolveElement(focusNode.getParentOrThrow(), isBackward, focusNode.getIndexWithinParent() + (isBackward ? 0 : 1));
585
- }
586
-
587
- return possibleNode;
588
- }
589
- }
590
-
591
- return null;
592
- }
593
-
594
545
  /**
595
546
  * Copyright (c) Meta Platforms, Inc. and affiliates.
596
547
  *
@@ -776,7 +727,7 @@ function $insertDataTransferForPlainText(dataTransfer, selection) {
776
727
  }
777
728
  }
778
729
  function $shouldOverrideDefaultCharacterSelection(selection, isBackward) {
779
- const possibleNode = getPossibleDecoratorNode(selection.focus, isBackward);
730
+ const possibleNode = lexical.$getDecoratorNode(selection.focus, isBackward);
780
731
  return lexical.$isDecoratorNode(possibleNode) && !possibleNode.isIsolated();
781
732
  }
782
733
  function onPasteForRichText(event, editor) {
@@ -834,6 +785,62 @@ function onCopyForRichText(event, editor) {
834
785
  });
835
786
  }
836
787
 
788
+ /**
789
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
790
+ *
791
+ * This source code is licensed under the MIT license found in the
792
+ * LICENSE file in the root directory of this source tree.
793
+ *
794
+ *
795
+ */
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
+ }
828
+
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
+ }
843
+
837
844
  /**
838
845
  * Copyright (c) Meta Platforms, Inc. and affiliates.
839
846
  *
@@ -930,8 +937,8 @@ function useLexicalDragonSupport(editor) {
930
937
  *
931
938
  *
932
939
  */
933
- function useRichTextSetup(editor) {
934
- react.useEffect(() => {
940
+ function useRichTextSetup(editor, initialEditorState) {
941
+ react.useLayoutEffect(() => {
935
942
  const removeListener = editor.addListener('command', (type, payload) => {
936
943
  const selection = lexical.$getSelection();
937
944
 
@@ -1170,13 +1177,9 @@ function useRichTextSetup(editor) {
1170
1177
 
1171
1178
  return false;
1172
1179
  }, 0);
1173
- const bootstrapCommandHandled = editor.execCommand('bootstrapEditor');
1174
-
1175
- if (!bootstrapCommandHandled) {
1176
- console.warn('bootstrapEditor command was not handled. Did you forget to add <BootstrapPlugin />?');
1177
- }
1178
-
1179
- return removeListener;
1180
+ initializeEditor(editor, initialEditorState);
1181
+ return removeListener; // We only do this for init
1182
+ // eslint-disable-next-line react-hooks/exhaustive-deps
1180
1183
  }, [editor]);
1181
1184
  useLexicalDragonSupport(editor);
1182
1185
  }
@@ -1189,8 +1192,8 @@ function useRichTextSetup(editor) {
1189
1192
  *
1190
1193
  *
1191
1194
  */
1192
- function useLexicalRichText(editor, externalHistoryState) {
1193
- useRichTextSetup(editor);
1195
+ function useLexicalRichText(editor, externalHistoryState, initialEditorState) {
1196
+ useRichTextSetup(editor, initialEditorState);
1194
1197
  useLexicalHistory(editor, externalHistoryState);
1195
1198
  }
1196
1199
 
@@ -5,28 +5,27 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  var k=require("@lexical/react/withSubscriptions"),w=require("lexical"),y=require("react");
8
- function z(a,d,e,b,c){if(null===a||0===e.size&&0===b.size)return 0;var f=d._selection,g=a._selection;if(c)return 1;if(!(w.$isRangeSelection(f)&&w.$isRangeSelection(g)&&g.isCollapsed()&&f.isCollapsed()))return 0;var h=Array.from(e);b=Array.from(b);e=d._nodeMap;c=[];for(var l=0;l<h.length;l++){const n=e.get(h[l]);void 0!==n&&c.push(n)}for(h=0;h<b.length;h++)b[h][1]&&(l=e.get(b[h][0]),void 0===l||w.$isRootNode(l)||c.push(l));if(0===c.length)return 0;if(1<c.length)return b=d._nodeMap,d=b.get(f.anchor.key),
9
- g=b.get(g.anchor.key),d&&g&&!a._nodeMap.has(d.__key)&&w.$isTextNode(d)&&1===d.__text.length&&1===f.anchor.offset?2:0;d=c[0];a=a._nodeMap.get(d.__key);if(!w.$isTextNode(a)||!w.$isTextNode(d)||a.__mode!==d.__mode)return 0;a=a.__text;d=d.__text;if(a===d)return 0;f=f.anchor;g=g.anchor;if(f.key!==g.key||"text"!==f.type)return 0;f=f.offset;g=g.offset;a=d.length-a.length;return 1===a&&g===f-1?2:-1===a&&g===f+1?3:-1===a&&g===f?4:0}
10
- function A(a,d){let e=Date.now(),b=0;return(c,f,g,h,l,n)=>{const p=Date.now();if(n.has("historic"))return b=0,e=p,2;const m=z(c,f,h,l,a.isComposing()),t=(()=>{const q=n.has("history-push");if(!q&&n.has("history-merge"))return 0;if(null===c)return 1;var r=f._selection;const u=c._selection;if(!(0<h.size||0<l.size))return null===u&&null!==r?0:2;r=null===g||g.editor===a;return!1===q&&0!==m&&m===b&&p<e+d&&r?0:1})();e=p;b=m;return t}}
11
- function B(a,d,e=1E3){const b=y.useMemo(()=>d||{current:null,redoStack:[],undoStack:[]},[d]),c=y.useCallback(()=>{b.undoStack=[];b.redoStack=[];b.current=null},[b]);y.useEffect(()=>{const f=A(a,e);return k(a.addListener("command",g=>{switch(g){case "undo":g=b.redoStack;var h=b.undoStack;if(0!==h.length){var l=b.current;const n=h.pop();null!==l&&(g.push(l),a.execCommand("canRedo",!0));0===h.length&&a.execCommand("canUndo",!1);b.current=n;n.editor.setEditorState(n.editorState.clone(n.undoSelection),
12
- {tag:"historic"})}return!0;case "redo":return g=b.redoStack,h=b.undoStack,0!==g.length&&(l=b.current,null!==l&&(h.push(l),a.execCommand("canUndo",!0)),h=g.pop(),0===g.length&&a.execCommand("canRedo",!1),b.current=h,h.editor.setEditorState(h.editorState,{tag:"historic"})),!0;case "clearEditor":return c(),!1;case "clearHistory":return c(),!0;default:return!1}},0),a.addListener("update",({editorState:g,prevEditorState:h,dirtyLeaves:l,dirtyElements:n,tags:p})=>{const m=b.current,t=b.redoStack,q=b.undoStack,
13
- r=null===m?null:m.editorState;if(null===m||g!==r){l=f(h,g,m,l,n,p);if(1===l)0!==t.length&&(b.redoStack=[]),null!==m&&(q.push({...m,undoSelection:h.read(w.$getSelection)}),a.execCommand("canUndo",!0));else if(2===l)return;b.current={editor:a,editorState:g}}}))},[c,e,a,b])}function C(a,d,e=1E3){return B(a,d,e)}
14
- function D(a){a=a.getLatest();const d=a.constructor.clone(a);d.__parent=a.__parent;w.$isElementNode(a)&&w.$isElementNode(d)?(d.__children=Array.from(a.__children),d.__format=a.__format,d.__indent=a.__indent,d.__dir=a.__dir):w.$isTextNode(a)&&w.$isTextNode(d)?(d.__format=a.__format,d.__style=a.__style,d.__mode=a.__mode,d.__detail=a.__detail):w.$isDecoratorNode(a)&&w.$isDecoratorNode(d)&&(d.__state=a.__state);return d}
15
- function E(a,d,e,b,c){for(var f=d;null!==a;){for(d=a.getParent();null!==d&&d.excludeFromCopy();)d=d.getParent();if(null===d)break;if(!w.$isElementNode(a)||!a.excludeFromCopy()){const g=a.getKey();let h=c.get(g);const l=void 0===h;l&&(h=D(a),c.set(g,h));!w.$isTextNode(h)||h.isSegmented()||h.isToken()?w.$isElementNode(h)&&(h.__children=h.__children.slice(e?f:0,e?void 0:f+1)):h.__text=h.__text.slice(e?f:0,e?void 0:f);if(w.$isRootNode(d)){l&&b.push(g);break}}f=c.get(d.getKey());f=w.$isElementNode(f)?
16
- f.__children.indexOf(a.getKey()):a.getIndexWithinParent();a=d}}function F(a){a=a.anchor.getNode();return"rtl"===(w.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}function G(a,d,e){const b=a.getParent();let c=e;null!==b&&(d&&0===e?(c=a.getIndexWithinParent(),a=b):d||e!==a.getChildrenSize()||(c=a.getIndexWithinParent()+1,a=b));return a.getChildAtIndex(d?c-1:c)}
17
- function H(a,d){const {nodeName:e}=a;d=d._htmlConversions.get(e.toLowerCase());let b=null;void 0!==d&&d.forEach(c=>{c=c(a);null!==c&&(null===b||b.priority<c.priority)&&(b=c)});return null!==b?b.conversion:null}
18
- function I(a,d,e=new Map){let b=[],c=null;var f=H(a,d),g=f?f(a):null;f=null;if(null!==g){f=g.after;c=g.node;if(null!==c){b.push(c);var h=Array.from(e.values());for(let l=0;l<h.length;l++)h[l](c)}null!=g.forChild&&e.set(a.nodeName,g.forChild)}a=a.childNodes;g=[];for(h=0;h<a.length;h++)g.push(...I(a[h],d,e));null!=f&&(g=f(g));null==c?b=b.concat(g):w.$isElementNode(c)&&c.append(...g);return b}
19
- function J(a,d,e){var b=a.getData("application/x-lexical-editor");if(b){var c=e._config.namespace;try{const l=JSON.parse(b);if(l.namespace===c){const {range:n,nodeMap:p}=l.state;var f=new Map(p);b=[];for(c=0;c<n.length;c++){var g=f.get(n[c]);if(void 0!==g){var h=w.$createNodeFromParse(g,f);b.push(h)}}d.insertNodes(b);return}}catch(l){}}if(f=a.getData("text/html")){f=(new DOMParser).parseFromString(f,"text/html");a=[];f=f.body?Array.from(f.body.childNodes):[];g=f.length;for(h=0;h<g;h++)b=I(f[h],e),
20
- null!==b&&(a=a.concat(b));e=a;a=[];f=null;for(g=0;g<e.length;g++)h=e[g],!w.$isElementNode(h)||h.isInline()?(null===f&&(f=w.$createParagraphNode(),a.push(f)),null!==f&&f.append(h)):(a.push(h),f=null);d.insertNodes(a)}else e=a.getData("text/plain"),null!=e&&d.insertRawText(e)}
21
- function K(a,d){var e=a.focus;a=e.offset;"element"===e.type?(e=e.getNode(),d=G(e,d,a)):(e=e.getNode(),d&&0===a||!d&&a===e.getTextContentSize()?(a=d?e.getPreviousSibling():e.getNextSibling(),d=null===a?G(e.getParentOrThrow(),d,e.getIndexWithinParent()+(d?0:1)):a):d=null);return w.$isDecoratorNode(d)&&!d.isIsolated()}function L(a,d){a.preventDefault();d.update(()=>{const e=w.$getSelection(),b=a.clipboardData;null!=b&&w.$isRangeSelection(e)&&J(b,e,d)})}
22
- function M(a,d){N(a,d);d.update(()=>{const e=w.$getSelection();w.$isRangeSelection(e)&&e.removeText()})}
23
- function N(a,d){a.preventDefault();d.update(()=>{const e=a.clipboardData;var b=w.$getSelection();if(null!==b&&null!=e){var c=window.getSelection();if(!c.isCollapsed){var f=c.getRangeAt(0);f&&(c=document.createElement("div"),f=f.cloneContents(),c.appendChild(f),e.setData("text/html",c.innerHTML));e.setData("text/plain",b.getTextContent());c=d._config.namespace;f=e.setData;var g=JSON,h=g.stringify;{if(!w.$isRangeSelection(b))throw Error("Minified Lexical error #68; see codes.json for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");
24
- var l=b.anchor,n=b.focus;var p=l.getCharacterOffset();const x=n.getCharacterOffset();var m=l.getNode(),t=n.getNode(),q=m.getParentOrThrow();if(m===t&&w.$isTextNode(m)&&(q.canBeEmpty()||1<q.getChildrenSize()))b=D(m),m=x>p,b.__text=b.__text.slice(m?p:x,m?x:p),p=b.getKey(),p={nodeMap:[[p,b]],range:[p]};else if(b=b.getNodes(),0===b.length)p={nodeMap:[],range:[]};else{m=b.length;t=b[0];q=t.getParent();if(null!==q&&(!q.canBeEmpty()||w.$isRootNode(q))){var r=q.__children;if(r.length===m){var u=!0;for(var v=
25
- 0;v<r.length;v++)if(r[v]!==b[v].__key){u=!1;break}u&&(m++,b.push(q))}}q=b[m-1];l=l.isBefore(n);n=new Map;r=[];E(t,l?p:x,!0,r,n);for(t=0;t<m;t++)if(u=b[t],v=u.getKey(),!(n.has(v)||w.$isElementNode(u)&&u.excludeFromCopy())){const O=D(u);w.$isRootNode(u.getParent())&&r.push(u.getKey());n.set(v,O)}E(q,l?x:p,!1,r,n);p={nodeMap:Array.from(n.entries()),range:r}}}f.call(e,"application/x-lexical-editor",h.call(g,{namespace:c,state:p}))}}})}
26
- function P(a){y.useEffect(()=>{const d=e=>{var b=a.getRootElement();if(document.activeElement===b&&(b=e.data,"string"===typeof b)){try{var c=JSON.parse(b)}catch(f){return}if(c&&"nuanria_messaging"===c.protocol&&"request"===c.type&&(c=c.payload)&&"makeChanges"===c.functionId&&(c=c.args)){const [f,g,h,l,n]=c;a.update(()=>{const p=w.$getSelection();if(w.$isRangeSelection(p)){var m=p.anchor;let t=m.getNode(),q=0,r=0;w.$isTextNode(t)&&0<=f&&0<=g&&(q=f,r=f+g,p.setTextNodeRange(t,q,t,r));if(q!==r||""!==
27
- h)p.insertRawText(h),t=m.getNode();w.$isTextNode(t)&&(q=l,r=l+n,m=t.getTextContentSize(),q=q>m?m:q,r=r>m?m:r,p.setTextNodeRange(t,q,t,r));e.stopImmediatePropagation()}})}}};window.addEventListener("message",d,!0);return()=>{window.removeEventListener("message",d,!0)}},[a])}
28
- function Q(a){y.useEffect(()=>{const d=a.addListener("command",(e,b)=>{var c=w.$getSelection();if("click"===e&&w.$isNodeSelection(c))return c.clear(),!0;if(!w.$isRangeSelection(c))return!1;switch(e){case "deleteCharacter":return c.deleteCharacter(b),!0;case "deleteWord":return c.deleteWord(b),!0;case "deleteLine":return c.deleteLine(b),!0;case "insertText":return"string"===typeof b?c.insertText(b):(e=b.dataTransfer,null!=e?J(e,c,a):(b=b.data)&&c.insertText(b)),!0;case "removeText":return c.removeText(),
29
- !0;case "formatText":return c.formatText(b),!0;case "formatElement":return c=c.anchor.getNode(),(w.$isElementNode(c)?c:c.getParentOrThrow()).setFormat(b),!0;case "insertLineBreak":return c.insertLineBreak(b),!0;case "insertParagraph":return c.insertParagraph(),!0;case "indentContent":return c=c.anchor,c="element"===c.type?c.getNode():c.getNode().getParentOrThrow(),c.canInsertTab()?a.execCommand("insertText","\t"):10!==c.getIndent()&&c.setIndent(c.getIndent()+1),!0;case "outdentContent":return c=c.anchor,
30
- b=c.getNode(),e="element"===c.type?c.getNode():c.getNode().getParentOrThrow(),e.canInsertTab()?"\t"===b.getTextContent()[c.offset-1]&&a.execCommand("deleteCharacter",!0):0!==e.getIndent()&&e.setIndent(e.getIndent()-1),!0;case "keyArrowLeft":e=b.shiftKey;if(K(c,!0))return b.preventDefault(),b=e,e=F(c),c.modify(b?"extend":"move",!e,"character"),!0;break;case "keyArrowRight":e=b.shiftKey;if(K(c,!1))return b.preventDefault(),b=e,e=F(c),c.modify(b?"extend":"move",e,"character"),!0;break;case "keyBackspace":return b.preventDefault(),
31
- {anchor:b}=c,c.isCollapsed()&&0===b.offset&&0<("element"===b.type?b.getNode():b.getNode().getParentOrThrow()).getIndent()?a.execCommand("outdentContent"):a.execCommand("deleteCharacter",!0);case "keyDelete":return b.preventDefault(),a.execCommand("deleteCharacter",!1);case "keyEnter":return b.preventDefault(),b.shiftKey?a.execCommand("insertLineBreak"):a.execCommand("insertParagraph");case "keyTab":return b.preventDefault(),a.execCommand(b.shiftKey?"outdentContent":"indentContent");case "keyEscape":return a.blur(),
32
- !0;case "copy":return N(b,a),!0;case "cut":return M(b,a),!0;case "paste":return L(b,a),!0;case "drop":case "dragstart":return b.preventDefault(),!0}return!1},0);a.execCommand("bootstrapEditor");return d},[a]);P(a)}module.exports=function(a,d){Q(a);C(a,d)};
8
+ function z(a,e,f,c,d){if(null===a||0===f.size&&0===c.size)return 0;var b=e._selection,g=a._selection;if(d)return 1;if(!(w.$isRangeSelection(b)&&w.$isRangeSelection(g)&&g.isCollapsed()&&b.isCollapsed()))return 0;var h=Array.from(f);c=Array.from(c);f=e._nodeMap;d=[];for(var l=0;l<h.length;l++){const n=f.get(h[l]);void 0!==n&&d.push(n)}for(h=0;h<c.length;h++)c[h][1]&&(l=f.get(c[h][0]),void 0===l||w.$isRootNode(l)||d.push(l));if(0===d.length)return 0;if(1<d.length)return c=e._nodeMap,e=c.get(b.anchor.key),
9
+ g=c.get(g.anchor.key),e&&g&&!a._nodeMap.has(e.__key)&&w.$isTextNode(e)&&1===e.__text.length&&1===b.anchor.offset?2:0;e=d[0];a=a._nodeMap.get(e.__key);if(!w.$isTextNode(a)||!w.$isTextNode(e)||a.__mode!==e.__mode)return 0;a=a.__text;e=e.__text;if(a===e)return 0;b=b.anchor;g=g.anchor;if(b.key!==g.key||"text"!==b.type)return 0;b=b.offset;g=g.offset;a=e.length-a.length;return 1===a&&g===b-1?2:-1===a&&g===b+1?3:-1===a&&g===b?4:0}
10
+ function A(a,e){let f=Date.now(),c=0;return(d,b,g,h,l,n)=>{const p=Date.now();if(n.has("historic"))return c=0,f=p,2;const m=z(d,b,h,l,a.isComposing()),t=(()=>{const q=n.has("history-push");if(!q&&n.has("history-merge"))return 0;if(null===d)return 1;var r=b._selection;const u=d._selection;if(!(0<h.size||0<l.size))return null===u&&null!==r?0:2;r=null===g||g.editor===a;return!1===q&&0!==m&&m===c&&p<f+e&&r?0:1})();f=p;c=m;return t}}
11
+ function B(a,e,f=1E3){const c=y.useMemo(()=>e||{current:null,redoStack:[],undoStack:[]},[e]),d=y.useCallback(()=>{c.undoStack=[];c.redoStack=[];c.current=null},[c]);y.useEffect(()=>{const b=A(a,f);return k(a.addListener("command",g=>{switch(g){case "undo":g=c.redoStack;var h=c.undoStack;if(0!==h.length){var l=c.current;const n=h.pop();null!==l&&(g.push(l),a.execCommand("canRedo",!0));0===h.length&&a.execCommand("canUndo",!1);c.current=n;n.editor.setEditorState(n.editorState.clone(n.undoSelection),
12
+ {tag:"historic"})}return!0;case "redo":return g=c.redoStack,h=c.undoStack,0!==g.length&&(l=c.current,null!==l&&(h.push(l),a.execCommand("canUndo",!0)),h=g.pop(),0===g.length&&a.execCommand("canRedo",!1),c.current=h,h.editor.setEditorState(h.editorState,{tag:"historic"})),!0;case "clearEditor":return d(),!1;case "clearHistory":return d(),!0;default:return!1}},0),a.addListener("update",({editorState:g,prevEditorState:h,dirtyLeaves:l,dirtyElements:n,tags:p})=>{const m=c.current,t=c.redoStack,q=c.undoStack,
13
+ r=null===m?null:m.editorState;if(null===m||g!==r){l=b(h,g,m,l,n,p);if(1===l)0!==t.length&&(c.redoStack=[]),null!==m&&(q.push({...m,undoSelection:h.read(w.$getSelection)}),a.execCommand("canUndo",!0));else if(2===l)return;c.current={editor:a,editorState:g}}}))},[d,f,a,c])}function C(a,e,f=1E3){return B(a,e,f)}
14
+ function D(a){a=a.getLatest();const e=a.constructor.clone(a);e.__parent=a.__parent;w.$isElementNode(a)&&w.$isElementNode(e)?(e.__children=Array.from(a.__children),e.__format=a.__format,e.__indent=a.__indent,e.__dir=a.__dir):w.$isTextNode(a)&&w.$isTextNode(e)?(e.__format=a.__format,e.__style=a.__style,e.__mode=a.__mode,e.__detail=a.__detail):w.$isDecoratorNode(a)&&w.$isDecoratorNode(e)&&(e.__state=a.__state);return e}
15
+ function E(a,e,f,c,d){for(var b=e;null!==a;){for(e=a.getParent();null!==e&&e.excludeFromCopy();)e=e.getParent();if(null===e)break;if(!w.$isElementNode(a)||!a.excludeFromCopy()){const g=a.getKey();let h=d.get(g);const l=void 0===h;l&&(h=D(a),d.set(g,h));!w.$isTextNode(h)||h.isSegmented()||h.isToken()?w.$isElementNode(h)&&(h.__children=h.__children.slice(f?b:0,f?void 0:b+1)):h.__text=h.__text.slice(f?b:0,f?void 0:b);if(w.$isRootNode(e)){l&&c.push(g);break}}b=d.get(e.getKey());b=w.$isElementNode(b)?
16
+ b.__children.indexOf(a.getKey()):a.getIndexWithinParent();a=e}}function F(a){a=a.anchor.getNode();return"rtl"===(w.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}function G(a,e){const {nodeName:f}=a;e=e._htmlConversions.get(f.toLowerCase());let c=null;void 0!==e&&e.forEach(d=>{d=d(a);null!==d&&(null===c||c.priority<d.priority)&&(c=d)});return null!==c?c.conversion:null}
17
+ function H(a,e,f=new Map){let c=[],d=null;var b=G(a,e),g=b?b(a):null;b=null;if(null!==g){b=g.after;d=g.node;if(null!==d){c.push(d);var h=Array.from(f.values());for(let l=0;l<h.length;l++)h[l](d)}null!=g.forChild&&f.set(a.nodeName,g.forChild)}a=a.childNodes;g=[];for(h=0;h<a.length;h++)g.push(...H(a[h],e,f));null!=b&&(g=b(g));null==d?c=c.concat(g):w.$isElementNode(d)&&d.append(...g);return c}
18
+ function I(a,e,f){var c=a.getData("application/x-lexical-editor");if(c){var d=f._config.namespace;try{const l=JSON.parse(c);if(l.namespace===d){const {range:n,nodeMap:p}=l.state;var b=new Map(p);c=[];for(d=0;d<n.length;d++){var g=b.get(n[d]);if(void 0!==g){var h=w.$createNodeFromParse(g,b);c.push(h)}}e.insertNodes(c);return}}catch(l){}}if(b=a.getData("text/html")){b=(new DOMParser).parseFromString(b,"text/html");a=[];b=b.body?Array.from(b.body.childNodes):[];g=b.length;for(h=0;h<g;h++)c=H(b[h],f),
19
+ null!==c&&(a=a.concat(c));f=a;a=[];b=null;for(g=0;g<f.length;g++)h=f[g],!w.$isElementNode(h)||h.isInline()?(null===b&&(b=w.$createParagraphNode(),a.push(b)),null!==b&&b.append(h)):(a.push(h),b=null);e.insertNodes(a)}else f=a.getData("text/plain"),null!=f&&e.insertRawText(f)}function J(a,e){a=w.$getDecoratorNode(a.focus,e);return w.$isDecoratorNode(a)&&!a.isIsolated()}
20
+ function K(a,e){a.preventDefault();e.update(()=>{const f=w.$getSelection(),c=a.clipboardData;null!=c&&w.$isRangeSelection(f)&&I(c,f,e)})}function L(a,e){M(a,e);e.update(()=>{const f=w.$getSelection();w.$isRangeSelection(f)&&f.removeText()})}
21
+ function M(a,e){a.preventDefault();e.update(()=>{const f=a.clipboardData;var c=w.$getSelection();if(null!==c&&null!=f){var d=window.getSelection();if(!d.isCollapsed){var b=d.getRangeAt(0);b&&(d=document.createElement("div"),b=b.cloneContents(),d.appendChild(b),f.setData("text/html",d.innerHTML));f.setData("text/plain",c.getTextContent());d=e._config.namespace;b=f.setData;var g=JSON,h=g.stringify;{if(!w.$isRangeSelection(c))throw Error("Minified Lexical error #68; see codes.json for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");
22
+ var l=c.anchor,n=c.focus;var p=l.getCharacterOffset();const x=n.getCharacterOffset();var m=l.getNode(),t=n.getNode(),q=m.getParentOrThrow();if(m===t&&w.$isTextNode(m)&&(q.canBeEmpty()||1<q.getChildrenSize()))c=D(m),m=x>p,c.__text=c.__text.slice(m?p:x,m?x:p),p=c.getKey(),p={nodeMap:[[p,c]],range:[p]};else if(c=c.getNodes(),0===c.length)p={nodeMap:[],range:[]};else{m=c.length;t=c[0];q=t.getParent();if(null!==q&&(!q.canBeEmpty()||w.$isRootNode(q))){var r=q.__children;if(r.length===m){var u=!0;for(var v=
23
+ 0;v<r.length;v++)if(r[v]!==c[v].__key){u=!1;break}u&&(m++,c.push(q))}}q=c[m-1];l=l.isBefore(n);n=new Map;r=[];E(t,l?p:x,!0,r,n);for(t=0;t<m;t++)if(u=c[t],v=u.getKey(),!(n.has(v)||w.$isElementNode(u)&&u.excludeFromCopy())){const O=D(u);w.$isRootNode(u.getParent())&&r.push(u.getKey());n.set(v,O)}E(q,l?x:p,!1,r,n);p={nodeMap:Array.from(n.entries()),range:r}}}b.call(f,"application/x-lexical-editor",h.call(g,{namespace:d,state:p}))}}})}const N={tag:"history-merge"};
24
+ function P(a,e){if(null!==e)if(void 0===e)a.update(()=>{var f=w.$getRoot();if(null===f.getFirstChild()){const c=w.$createParagraphNode();f.append(c);f=document.activeElement;(null!==w.$getSelection()||null!==f&&f===a.getRootElement())&&c.select()}},N);else if(null!==e)switch(typeof e){case "string":e=a.parseEditorState(e);a.setEditorState(e,N);break;case "object":a.setEditorState(e,N);break;case "function":a.update(e,N)}}
25
+ function Q(a){y.useEffect(()=>{const e=f=>{var c=a.getRootElement();if(document.activeElement===c&&(c=f.data,"string"===typeof c)){try{var d=JSON.parse(c)}catch(b){return}if(d&&"nuanria_messaging"===d.protocol&&"request"===d.type&&(d=d.payload)&&"makeChanges"===d.functionId&&(d=d.args)){const [b,g,h,l,n]=d;a.update(()=>{const p=w.$getSelection();if(w.$isRangeSelection(p)){var m=p.anchor;let t=m.getNode(),q=0,r=0;w.$isTextNode(t)&&0<=b&&0<=g&&(q=b,r=b+g,p.setTextNodeRange(t,q,t,r));if(q!==r||""!==
26
+ h)p.insertRawText(h),t=m.getNode();w.$isTextNode(t)&&(q=l,r=l+n,m=t.getTextContentSize(),q=q>m?m:q,r=r>m?m:r,p.setTextNodeRange(t,q,t,r));f.stopImmediatePropagation()}})}}};window.addEventListener("message",e,!0);return()=>{window.removeEventListener("message",e,!0)}},[a])}
27
+ function R(a,e){y.useLayoutEffect(()=>{const f=a.addListener("command",(c,d)=>{var b=w.$getSelection();if("click"===c&&w.$isNodeSelection(b))return b.clear(),!0;if(!w.$isRangeSelection(b))return!1;switch(c){case "deleteCharacter":return b.deleteCharacter(d),!0;case "deleteWord":return b.deleteWord(d),!0;case "deleteLine":return b.deleteLine(d),!0;case "insertText":return"string"===typeof d?b.insertText(d):(c=d.dataTransfer,null!=c?I(c,b,a):(d=d.data)&&b.insertText(d)),!0;case "removeText":return b.removeText(),
28
+ !0;case "formatText":return b.formatText(d),!0;case "formatElement":return b=b.anchor.getNode(),(w.$isElementNode(b)?b:b.getParentOrThrow()).setFormat(d),!0;case "insertLineBreak":return b.insertLineBreak(d),!0;case "insertParagraph":return b.insertParagraph(),!0;case "indentContent":return b=b.anchor,b="element"===b.type?b.getNode():b.getNode().getParentOrThrow(),b.canInsertTab()?a.execCommand("insertText","\t"):10!==b.getIndent()&&b.setIndent(b.getIndent()+1),!0;case "outdentContent":return b=b.anchor,
29
+ d=b.getNode(),c="element"===b.type?b.getNode():b.getNode().getParentOrThrow(),c.canInsertTab()?"\t"===d.getTextContent()[b.offset-1]&&a.execCommand("deleteCharacter",!0):0!==c.getIndent()&&c.setIndent(c.getIndent()-1),!0;case "keyArrowLeft":c=d.shiftKey;if(J(b,!0))return d.preventDefault(),d=c,c=F(b),b.modify(d?"extend":"move",!c,"character"),!0;break;case "keyArrowRight":c=d.shiftKey;if(J(b,!1))return d.preventDefault(),d=c,c=F(b),b.modify(d?"extend":"move",c,"character"),!0;break;case "keyBackspace":return d.preventDefault(),
30
+ {anchor:d}=b,b.isCollapsed()&&0===d.offset&&0<("element"===d.type?d.getNode():d.getNode().getParentOrThrow()).getIndent()?a.execCommand("outdentContent"):a.execCommand("deleteCharacter",!0);case "keyDelete":return d.preventDefault(),a.execCommand("deleteCharacter",!1);case "keyEnter":return d.preventDefault(),d.shiftKey?a.execCommand("insertLineBreak"):a.execCommand("insertParagraph");case "keyTab":return d.preventDefault(),a.execCommand(d.shiftKey?"outdentContent":"indentContent");case "keyEscape":return a.blur(),
31
+ !0;case "copy":return M(d,a),!0;case "cut":return L(d,a),!0;case "paste":return K(d,a),!0;case "drop":case "dragstart":return d.preventDefault(),!0}return!1},0);P(a,e);return f},[a]);Q(a)}module.exports=function(a,e,f){R(a,f);C(a,e)};
@@ -171,14 +171,33 @@ const markdownHorizontalRuleUsingDashes = { ...paragraphStartBase,
171
171
  nodeTransformationKind: 'horizontalRule',
172
172
  regEx: /(?:--- )/
173
173
  };
174
+ const markdownItalic = { ...autoFormatBase,
175
+ nodeTransformationKind: 'italic',
176
+ regEx: /(\*)(\s*\b)([^\*]*)(\b\s*)(\*\s)$/
177
+ };
174
178
  const markdownBold = { ...autoFormatBase,
175
- nodeTransformationKind: 'textBold',
176
- // regEx: /(\*)(?:\s*\b)(?:[^\*]*)(?:\b\s*)(\*\s)$/, // The $ will find the target at the end of the string.
177
- regEx: /(\*)(\s*\b)([^\*]*)(\b\s*)(\*\s)$/ // Remove the first and last capture groups. Remeber, the 0th capture group is the entire string.
178
- // e.g. "*Hello* " requires removing both "*" as well as bolding "Hello".
179
-
179
+ nodeTransformationKind: 'bold',
180
+ regEx: /(\*\*)(\s*\b)([^\*\*]*)(\b\s*)(\*\*\s)$/
181
+ };
182
+ const markdownBoldWithUnderlines = { ...autoFormatBase,
183
+ nodeTransformationKind: 'bold',
184
+ regEx: /(__)(\s*)([^__]*)(\s*)(__\s)$/
185
+ };
186
+ const markdownBoldItalic = { ...autoFormatBase,
187
+ nodeTransformationKind: 'bold_italic',
188
+ regEx: /(\*\*\*)(\s*\b)([^\*\*\*]*)(\b\s*)(\*\*\*\s)$/
189
+ }; // Markdown does not support underline, but we can allow folks to use
190
+ // the HTML tags for underline.
191
+
192
+ const fakeMarkdownUnderline = { ...autoFormatBase,
193
+ nodeTransformationKind: 'underline',
194
+ regEx: /(\<u\>)(\s*\b)([^\<]*)(\b\s*)(\<\/u\>\s)$/
180
195
  };
181
- const allAutoFormatCriteriaForTextNodes = [markdownBold];
196
+ const markdownStrikethrough = { ...autoFormatBase,
197
+ nodeTransformationKind: 'strikethrough',
198
+ regEx: /(~~)(\s*\b)([^~~]*)(\b\s*)(~~\s)$/
199
+ };
200
+ const allAutoFormatCriteriaForTextNodes = [markdownBoldItalic, markdownItalic, markdownBold, markdownBoldWithUnderlines, fakeMarkdownUnderline, markdownStrikethrough];
182
201
  const allAutoFormatCriteria = [markdownHeader1, markdownHeader2, markdownHeader3, markdownBlockQuote, markdownUnorderedListDash, markdownUnorderedListAsterisk, markdownOrderedList, markdownCodeBlock, markdownHorizontalRule, markdownHorizontalRuleUsingDashes, ...allAutoFormatCriteriaForTextNodes];
183
202
  function getAllAutoFormatCriteriaForTextNodes() {
184
203
  return allAutoFormatCriteriaForTextNodes;
@@ -259,17 +278,14 @@ function getMatchResultContextForText(autoFormatCriteria, scanningContext) {
259
278
  // Lazy calculate the text to search.
260
279
  scanningContext.joinedText = $joinTextNodesInElementNode(parentNode, SEPARATOR_BETWEEN_TEXT_AND_NON_TEXT_NODES, scanningContext.textNodeWithOffset);
261
280
  }
262
-
263
- return getMatchResultContextWithRegEx(scanningContext.joinedText, false, true, autoFormatCriteria.regEx);
264
281
  } else {
265
282
  {
266
283
  throw Error(`Expected node ${parentNode.__key} to to be a ElementNode.`);
267
284
  }
268
285
  }
269
- } // This is a placeholder function for following PR's related to character based transformations.
270
-
286
+ }
271
287
 
272
- return null;
288
+ return getMatchResultContextWithRegEx(scanningContext.joinedText, false, true, autoFormatCriteria.regEx);
273
289
  }
274
290
 
275
291
  function getMatchResultContextForCriteria(autoFormatCriteria, scanningContext) {
@@ -391,28 +407,50 @@ function transformTextNodeForParagraphs(scanningContext) {
391
407
  }
392
408
  }
393
409
 
410
+ function getTextFormatType(nodeTransformationKind) {
411
+ switch (nodeTransformationKind) {
412
+ case 'italic':
413
+ case 'bold':
414
+ case 'underline':
415
+ case 'strikethrough':
416
+ return [nodeTransformationKind];
417
+
418
+ case 'bold_italic':
419
+ {
420
+ return ['bold', 'italic'];
421
+ }
422
+ }
423
+
424
+ return null;
425
+ }
426
+
394
427
  function transformTextNodeForText(scanningContext) {
395
428
  const autoFormatCriteria = scanningContext.autoFormatCriteria;
396
429
  const matchResultContext = scanningContext.matchResultContext;
397
430
 
398
431
  if (autoFormatCriteria.nodeTransformationKind != null) {
399
- switch (autoFormatCriteria.nodeTransformationKind) {
400
- case 'textBold':
401
- {
402
- if (matchResultContext.regExCaptureGroups.length !== 6) {
403
- // The expected reg ex pattern for bold should have 6 groups.
404
- // If it does not, then break and fail silently.
405
- // e2e tests validate the regEx pattern.
406
- break;
407
- }
432
+ if (matchResultContext.regExCaptureGroups.length !== 6) {
433
+ // For BIUS and other formatts which have a pattern + text + pattern,
434
+ // the expected reg ex pattern should have 6 groups.
435
+ // If it does not, then break and fail silently.
436
+ // e2e tests validate the regEx pattern.
437
+ return;
438
+ }
408
439
 
409
- matchResultContext.regExCaptureGroups = getCaptureGroupsByResolvingAllDetails(scanningContext); // Remove unwanted text in reg ex pattern.
440
+ const formatting = getTextFormatType(autoFormatCriteria.nodeTransformationKind);
410
441
 
411
- removeTextInCaptureGroups([1, 5], matchResultContext);
412
- formatTextInCaptureGroupIndex('bold', 3, matchResultContext);
413
- makeCollapsedSelectionAtOffsetInJoinedText(matchResultContext.offsetInJoinedTextForCollapsedSelection, matchResultContext.offsetInJoinedTextForCollapsedSelection + 1, scanningContext.textNodeWithOffset.node.getParentOrThrow());
414
- break;
415
- }
442
+ if (formatting != null) {
443
+ const captureGroupsToDelete = [1, 5];
444
+ const formatCaptureGroup = 3;
445
+ matchResultContext.regExCaptureGroups = getCaptureGroupsByResolvingAllDetails(scanningContext);
446
+
447
+ if (captureGroupsToDelete.length > 0) {
448
+ // Remove unwanted text in reg ex pattern.
449
+ removeTextInCaptureGroups(captureGroupsToDelete, matchResultContext);
450
+ }
451
+
452
+ formatTextInCaptureGroupIndex(formatting, formatCaptureGroup, matchResultContext);
453
+ makeCollapsedSelectionAtOffsetInJoinedText(matchResultContext.offsetInJoinedTextForCollapsedSelection, matchResultContext.offsetInJoinedTextForCollapsedSelection + 1, scanningContext.textNodeWithOffset.node.getParentOrThrow());
416
454
  }
417
455
  }
418
456
  } // Some Capture Group Details were left lazily unresolved as their calculation
@@ -516,7 +554,7 @@ function shiftCaptureGroupOffsets(delta, applyAtOrAfterOffset, node, startingCap
516
554
  }
517
555
  }
518
556
 
519
- function formatTextInCaptureGroupIndex(formatType, captureGroupIndex, matchResultContext) {
557
+ function formatTextInCaptureGroupIndex(formatTypes, captureGroupIndex, matchResultContext) {
520
558
  const regExCaptureGroups = matchResultContext.regExCaptureGroups;
521
559
  const regExCaptureGroupsCount = regExCaptureGroups.length;
522
560
 
@@ -536,7 +574,10 @@ function formatTextInCaptureGroupIndex(formatType, captureGroupIndex, matchResul
536
574
  const currentSelection = lexical.$getSelection();
537
575
 
538
576
  if (lexical.$isRangeSelection(currentSelection)) {
539
- currentSelection.formatText(formatType);
577
+ for (let i = 0; i < formatTypes.length; i++) {
578
+ currentSelection.formatText(formatTypes[i]);
579
+ }
580
+
540
581
  const finalSelection = lexical.$createRangeSelection();
541
582
  finalSelection.anchor.set(focusTextNodeWithOffset.node.getKey(), focusTextNodeWithOffset.offset + 1, 'text');
542
583
  finalSelection.focus.set(focusTextNodeWithOffset.node.getKey(), focusTextNodeWithOffset.offset + 1, 'text');
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ */
9
+
10
+ declare export default function LexicalAutoFormatterPlugin(): React$Node;
@@ -4,23 +4,24 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- var l=require("@lexical/react/LexicalComposerContext"),p=require("@lexical/list"),q=require("lexical"),r=require("lexical/CodeNode"),v=require("react"),w=require("@lexical/react/LexicalHorizontalRuleNode"),A=require("lexical/HeadingNode"),B=require("lexical/QuoteNode");function C(g){throw Error(`Minified Lexical error #${g}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
8
- function D(g,f,a,b){g=g.getChildren();const c=g.length;let d=0,e=!1;for(let n=0;n<c&&!(d>f);++n){const h=g[n],m=q.$isTextNode(h);var k=m?h.getTextContent().length:b;k=d+k;if((!1===e&&d===a||0===d&&d===a||d<a&&a<=k)&&q.$isTextNode(h))return{node:h,offset:a-d};d=k;e=m}return null}
9
- const E={nodeTransformationKind:null,regEx:/(?:)/,requiresParagraphStart:!1},G={...E,requiresParagraphStart:!0},H=[{...E,nodeTransformationKind:"textBold",regEx:/(\*)(\s*\b)([^\*]*)(\b\s*)(\*\s)$/}],I=[{...G,nodeTransformationKind:"paragraphH1",regEx:/(?:# )/},{...G,nodeTransformationKind:"paragraphH2",regEx:/(?:## )/},{...G,nodeTransformationKind:"paragraphH2",regEx:/(?:### )/},{...G,nodeTransformationKind:"paragraphBlockQuote",regEx:/(?:> )/},{...G,nodeTransformationKind:"paragraphUnorderedList",
10
- regEx:/(?:- )/},{...G,nodeTransformationKind:"paragraphUnorderedList",regEx:/(?:\* )/},{...G,nodeTransformationKind:"paragraphOrderedList",regEx:/^(\d+)\.\s/},{...G,nodeTransformationKind:"paragraphCodeBlock",regEx:/(?:``` )/},{...G,nodeTransformationKind:"horizontalRule",regEx:/(?:\*\*\* )/},{...G,nodeTransformationKind:"horizontalRule",regEx:/(?:--- )/},...H];
11
- function J(g,f,a,b){const c={offsetInJoinedTextForCollapsedSelection:0,regExCaptureGroups:[]};b=g.match(b);if(null!==b&&0<b.length&&(!1===f||0===b.index)&&(!1===a||b.index+b[0].length===g.length)){c.offsetInJoinedTextForCollapsedSelection=g.length;g=b.length;f=b.index;for(a=0;a<g;a++){const d=b[a];c.regExCaptureGroups.push({anchorTextNodeWithOffset:null,focusTextNodeWithOffset:null,offsetInParent:f,text:d,textLength:d.length-(a+1===g?1:0)});0<a&&(f+=d.length)}return c}return null}
12
- function K(g,f,a,b,c){c.offsetInJoinedTextForCollapsedSelection+=g;0<c.offsetInJoinedTextForCollapsedSelection||C(67);c=c.regExCaptureGroups;const d=c.length;for(b+=1;b<d;b++){const e=c[b];null!=e.anchorTextNodeWithOffset&&e.anchorTextNodeWithOffset.offset>=f&&e.anchorTextNodeWithOffset.node.is(a)&&(e.anchorTextNodeWithOffset.offset+=g);null!=e.focusTextNodeWithOffset&&e.focusTextNodeWithOffset.offset>=f&&e.focusTextNodeWithOffset.node.is(a)&&(e.focusTextNodeWithOffset.offset+=g)}}
13
- function L(g,f){g.update(()=>{if(f.autoFormatCriteria.requiresParagraphStart){var a=f.textNodeWithOffset,b=a.node.getParentOrThrow();a=a.node.spliceText(0,f.matchResultContext.regExCaptureGroups[0].text.length,"",!0);""===a.getTextContent()&&(a.selectPrevious(),a.remove());var c=b;a=null;var d=c.getChildren(),e=f.autoFormatCriteria,k=f.matchResultContext;if(null!=e.nodeTransformationKind)switch(e.nodeTransformationKind){case "paragraphH1":a=A.$createHeadingNode("h1");a.append(...d);break;case "paragraphH2":a=
14
- A.$createHeadingNode("h2");a.append(...d);break;case "paragraphH3":a=A.$createHeadingNode("h3");a.append(...d);break;case "paragraphBlockQuote":a=B.$createQuoteNode();a.append(...d);break;case "paragraphUnorderedList":a=p.$createListNode("ul");c=p.$createListItemNode();c.append(...d);a.append(c);break;case "paragraphOrderedList":a=parseInt(1<k.regExCaptureGroups.length?k.regExCaptureGroups[k.regExCaptureGroups.length-1].text:"1",10);a=p.$createListNode("ol",a);c=p.$createListItemNode();c.append(...d);
15
- a.append(c);break;case "paragraphCodeBlock":a=null!=f.triggerState&&f.triggerState.isCodeBlock?q.$createParagraphNode():r.$createCodeNode();a.append(...d);break;case "horizontalRule":d=w.$createHorizontalRuleNode(),c.insertBefore(d)}null!==a&&b.replace(a)}else if(a=f.autoFormatCriteria,b=f.matchResultContext,null!=a.nodeTransformationKind)switch(a.nodeTransformationKind){case "textBold":if(6===b.regExCaptureGroups.length){a=f.autoFormatCriteria;d=f.matchResultContext.regExCaptureGroups;c=d.length;
16
- e=f.textNodeWithOffset.node.getParentOrThrow();null==f.joinedText&&C(62);k=f.joinedText.length;for(var n=1;n<c;n++){var h=d[n];h.anchorTextNodeWithOffset=D(e,k,h.offsetInParent,1);h.focusTextNodeWithOffset=D(e,k,h.offsetInParent+h.textLength,1);0>h.textLength&&C(63,a.nodeTransformationKind)}b.regExCaptureGroups=d;a=[1,5];d=b.regExCaptureGroups;c=d.length;for(e=a.length-1;0<=e;e--)if(e<c){k=a[e];var m=d[k];h=m.anchorTextNodeWithOffset;n=m.focusTextNodeWithOffset;null!=h&&null!=n&&0<m.textLength&&(m=
17
- q.$createRangeSelection(),m.anchor.set(h.node.getKey(),h.offset,"text"),m.focus.set(n.node.getKey(),n.offset,"text"),q.$setSelection(m),m=q.$getSelection(),q.$isRangeSelection(m)&&(m.removeText(),h.node.getKey()===n.node.getKey()?(m=n.offset-h.offset,0<m||C(64),K(-m,n.offset,h.node,k,b)):(h=n.offset,0<h&&K(-h,h,n.node,k,b))))}a=b.regExCaptureGroups;3<a.length||C(65);c=a[3];d=c.anchorTextNodeWithOffset;a=c.focusTextNodeWithOffset;null!=d&&null!=a&&0<c.textLength&&(c=q.$createRangeSelection(),c.anchor.set(d.node.getKey(),
18
- d.offset,"text"),c.focus.set(a.node.getKey(),a.offset,"text"),q.$setSelection(c),d=q.$getSelection(),q.$isRangeSelection(d)&&(d.formatText("bold"),d=q.$createRangeSelection(),d.anchor.set(a.node.getKey(),a.offset+1,"text"),d.focus.set(a.node.getKey(),a.offset+1,"text"),q.$setSelection(d)));a=f.textNodeWithOffset.node.getParentOrThrow();b=D(a,b.offsetInJoinedTextForCollapsedSelection+1,b.offsetInJoinedTextForCollapsedSelection,1);null!=b&&(a=q.$createRangeSelection(),a.anchor.set(b.node.getKey(),b.offset,
19
- "text"),a.focus.set(b.node.getKey(),b.offset,"text"),q.$setSelection(a))}}},{tag:"history-push"})}
20
- function M(g,f){let a=null;g.read(()=>{var b=q.$getSelection();if(q.$isRangeSelection(b)){var c=b.anchor.getNode();b=q.$isTextNode(c)?{node:c,offset:b.anchor.offset}:null}else b=null;if(null!==b){b={autoFormatCriteria:{nodeTransformationKind:"noTransformation",regEx:/(?:)/,requiresParagraphStart:null},joinedText:null,matchResultContext:{offsetInJoinedTextForCollapsedSelection:0,regExCaptureGroups:[]},textNodeWithOffset:b,triggerState:f};a:{c=!1===f.isParentAListItemNode?I:H;const m=b.triggerState,
21
- N=c.length;for(let x=0;x<N;x++){const y=c[x];if(null!=m&&!1===m.isCodeBlock||"paragraphCodeBlock"===y.nodeTransformationKind){var d=y,e=b;if(null!==d.requiresParagraphStart&&!0===d.requiresParagraphStart)null===e.textNodeWithOffset.node.getPreviousSibling()?(e=e.textNodeWithOffset.node.getTextContent(),d=J(e,!0,!1,d.regEx)):d=null;else b:{if(null==e.joinedText){var k=e.textNodeWithOffset.node.getParentOrThrow();if(q.$isElementNode(k)){if(null==e.joinedText){var n=e,h=e.textNodeWithOffset;let u="";
22
- k=k.getChildren();const O=k.length;for(let z=0;z<O;++z){const t=k[z];if(q.$isTextNode(t)){const F=t.getTextContent();if(t.is(h.node)){h.offset>F.length&&C(50,t.__key);u+=t.getTextContent().substr(0,h.offset);break}else u+=F}else u+="\u0004"}n.joinedText=u}d=J(e.joinedText,!1,!0,d.regEx);break b}C(52,k.__key)}d=null}if(null!=d){c={autoFormatCriteria:y,matchResultContext:d};break a}}}c={autoFormatCriteria:null,matchResultContext:null}}null!==c.autoFormatCriteria&&null!==c.matchResultContext&&(a=b,a.autoFormatCriteria=
7
+ var k=require("@lexical/react/LexicalComposerContext"),q=require("@lexical/list"),r=require("lexical"),t=require("lexical/CodeNode"),u=require("react"),x=require("@lexical/react/LexicalHorizontalRuleNode"),y=require("lexical/HeadingNode"),C=require("lexical/QuoteNode");function D(g){throw Error(`Minified Lexical error #${g}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
8
+ function E(g,f,a,b){g=g.getChildren();const c=g.length;let d=0,e=!1;for(let p=0;p<c&&!(d>f);++p){const m=g[p],n=r.$isTextNode(m);var h=n?m.getTextContent().length:b;h=d+h;if((!1===e&&d===a||0===d&&d===a||d<a&&a<=h)&&r.$isTextNode(m))return{node:m,offset:a-d};d=h;e=n}return null}
9
+ const F={nodeTransformationKind:null,regEx:/(?:)/,requiresParagraphStart:!1},H={...F,requiresParagraphStart:!0},I=[{...F,nodeTransformationKind:"bold_italic",regEx:/(\*\*\*)(\s*\b)([^\*\*\*]*)(\b\s*)(\*\*\*\s)$/},{...F,nodeTransformationKind:"italic",regEx:/(\*)(\s*\b)([^\*]*)(\b\s*)(\*\s)$/},{...F,nodeTransformationKind:"bold",regEx:/(\*\*)(\s*\b)([^\*\*]*)(\b\s*)(\*\*\s)$/},{...F,nodeTransformationKind:"bold",regEx:/(__)(\s*)([^__]*)(\s*)(__\s)$/},{...F,nodeTransformationKind:"underline",regEx:/(<u>)(\s*\b)([^<]*)(\b\s*)(<\/u>\s)$/},
10
+ {...F,nodeTransformationKind:"strikethrough",regEx:/(~~)(\s*\b)([^~~]*)(\b\s*)(~~\s)$/}],J=[{...H,nodeTransformationKind:"paragraphH1",regEx:/(?:# )/},{...H,nodeTransformationKind:"paragraphH2",regEx:/(?:## )/},{...H,nodeTransformationKind:"paragraphH2",regEx:/(?:### )/},{...H,nodeTransformationKind:"paragraphBlockQuote",regEx:/(?:> )/},{...H,nodeTransformationKind:"paragraphUnorderedList",regEx:/(?:- )/},{...H,nodeTransformationKind:"paragraphUnorderedList",regEx:/(?:\* )/},{...H,nodeTransformationKind:"paragraphOrderedList",
11
+ regEx:/^(\d+)\.\s/},{...H,nodeTransformationKind:"paragraphCodeBlock",regEx:/(?:``` )/},{...H,nodeTransformationKind:"horizontalRule",regEx:/(?:\*\*\* )/},{...H,nodeTransformationKind:"horizontalRule",regEx:/(?:--- )/},...I];
12
+ function K(g,f,a,b){const c={offsetInJoinedTextForCollapsedSelection:0,regExCaptureGroups:[]};b=g.match(b);if(null!==b&&0<b.length&&(!1===f||0===b.index)&&(!1===a||b.index+b[0].length===g.length)){c.offsetInJoinedTextForCollapsedSelection=g.length;g=b.length;f=b.index;for(a=0;a<g;a++){const d=b[a];c.regExCaptureGroups.push({anchorTextNodeWithOffset:null,focusTextNodeWithOffset:null,offsetInParent:f,text:d,textLength:d.length-(a+1===g?1:0)});0<a&&(f+=d.length)}return c}return null}
13
+ function L(g,f,a,b,c){c.offsetInJoinedTextForCollapsedSelection+=g;0<c.offsetInJoinedTextForCollapsedSelection||D(67);c=c.regExCaptureGroups;const d=c.length;for(b+=1;b<d;b++){const e=c[b];null!=e.anchorTextNodeWithOffset&&e.anchorTextNodeWithOffset.offset>=f&&e.anchorTextNodeWithOffset.node.is(a)&&(e.anchorTextNodeWithOffset.offset+=g);null!=e.focusTextNodeWithOffset&&e.focusTextNodeWithOffset.offset>=f&&e.focusTextNodeWithOffset.node.is(a)&&(e.focusTextNodeWithOffset.offset+=g)}}
14
+ function M(g,f){g.update(()=>{if(f.autoFormatCriteria.requiresParagraphStart){var a=f.textNodeWithOffset,b=a.node.getParentOrThrow();a=a.node.spliceText(0,f.matchResultContext.regExCaptureGroups[0].text.length,"",!0);""===a.getTextContent()&&(a.selectPrevious(),a.remove());var c=b;a=null;var d=c.getChildren(),e=f.autoFormatCriteria,h=f.matchResultContext;if(null!=e.nodeTransformationKind)switch(e.nodeTransformationKind){case "paragraphH1":a=y.$createHeadingNode("h1");a.append(...d);break;case "paragraphH2":a=
15
+ y.$createHeadingNode("h2");a.append(...d);break;case "paragraphH3":a=y.$createHeadingNode("h3");a.append(...d);break;case "paragraphBlockQuote":a=C.$createQuoteNode();a.append(...d);break;case "paragraphUnorderedList":a=q.$createListNode("ul");c=q.$createListItemNode();c.append(...d);a.append(c);break;case "paragraphOrderedList":a=parseInt(1<h.regExCaptureGroups.length?h.regExCaptureGroups[h.regExCaptureGroups.length-1].text:"1",10);a=q.$createListNode("ol",a);c=q.$createListItemNode();c.append(...d);
16
+ a.append(c);break;case "paragraphCodeBlock":a=null!=f.triggerState&&f.triggerState.isCodeBlock?r.$createParagraphNode():t.$createCodeNode();a.append(...d);break;case "horizontalRule":d=x.$createHorizontalRuleNode(),c.insertBefore(d)}null!==a&&b.replace(a)}else if(a=f.autoFormatCriteria,b=f.matchResultContext,null!=a.nodeTransformationKind&&6===b.regExCaptureGroups.length){a:{a=a.nodeTransformationKind;switch(a){case "italic":case "bold":case "underline":case "strikethrough":a=[a];break a;case "bold_italic":a=
17
+ ["bold","italic"];break a}a=null}if(null!=a){d=[1,5];c=f.autoFormatCriteria;e=f.matchResultContext.regExCaptureGroups;h=e.length;var p=f.textNodeWithOffset.node.getParentOrThrow();null==f.joinedText&&D(62);var m=f.joinedText.length;for(var n=1;n<h;n++){var l=e[n];l.anchorTextNodeWithOffset=E(p,m,l.offsetInParent,1);l.focusTextNodeWithOffset=E(p,m,l.offsetInParent+l.textLength,1);0>l.textLength&&D(63,c.nodeTransformationKind)}b.regExCaptureGroups=e;if(0<d.length)for(c=b.regExCaptureGroups,e=c.length,
18
+ h=d.length-1;0<=h;h--)h<e&&(p=d[h],l=c[p],n=l.anchorTextNodeWithOffset,m=l.focusTextNodeWithOffset,null!=n&&null!=m&&0<l.textLength&&(l=r.$createRangeSelection(),l.anchor.set(n.node.getKey(),n.offset,"text"),l.focus.set(m.node.getKey(),m.offset,"text"),r.$setSelection(l),l=r.$getSelection(),r.$isRangeSelection(l)&&(l.removeText(),n.node.getKey()===m.node.getKey()?(l=m.offset-n.offset,0<l||D(64),L(-l,m.offset,n.node,p,b)):(n=m.offset,0<n&&L(-n,n,m.node,p,b)))));d=b.regExCaptureGroups;3<d.length||D(65);
19
+ e=d[3];c=e.anchorTextNodeWithOffset;d=e.focusTextNodeWithOffset;if(null!=c&&null!=d&&0<e.textLength&&(e=r.$createRangeSelection(),e.anchor.set(c.node.getKey(),c.offset,"text"),e.focus.set(d.node.getKey(),d.offset,"text"),r.$setSelection(e),c=r.$getSelection(),r.$isRangeSelection(c))){for(e=0;e<a.length;e++)c.formatText(a[e]);a=r.$createRangeSelection();a.anchor.set(d.node.getKey(),d.offset+1,"text");a.focus.set(d.node.getKey(),d.offset+1,"text");r.$setSelection(a)}a=f.textNodeWithOffset.node.getParentOrThrow();
20
+ b=E(a,b.offsetInJoinedTextForCollapsedSelection+1,b.offsetInJoinedTextForCollapsedSelection,1);null!=b&&(a=r.$createRangeSelection(),a.anchor.set(b.node.getKey(),b.offset,"text"),a.focus.set(b.node.getKey(),b.offset,"text"),r.$setSelection(a))}}},{tag:"history-push"})}
21
+ function N(g,f){let a=null;g.read(()=>{var b=r.$getSelection();if(r.$isRangeSelection(b)){var c=b.anchor.getNode();b=r.$isTextNode(c)?{node:c,offset:b.anchor.offset}:null}else b=null;if(null!==b){b={autoFormatCriteria:{nodeTransformationKind:"noTransformation",regEx:/(?:)/,requiresParagraphStart:null},joinedText:null,matchResultContext:{offsetInJoinedTextForCollapsedSelection:0,regExCaptureGroups:[]},textNodeWithOffset:b,triggerState:f};a:{c=!1===f.isParentAListItemNode?J:I;const n=b.triggerState,
22
+ l=c.length;for(let z=0;z<l;z++){const A=c[z];if(null!=n&&!1===n.isCodeBlock||"paragraphCodeBlock"===A.nodeTransformationKind){var d=A,e=b;if(null!==d.requiresParagraphStart&&!0===d.requiresParagraphStart)null===e.textNodeWithOffset.node.getPreviousSibling()?(e=e.textNodeWithOffset.node.getTextContent(),d=K(e,!0,!1,d.regEx)):d=null;else{if(null==e.joinedText){var h=e.textNodeWithOffset.node.getParentOrThrow();if(r.$isElementNode(h)){if(null==e.joinedText){var p=e,m=e.textNodeWithOffset;let w="";h=
23
+ h.getChildren();const O=h.length;for(let B=0;B<O;++B){const v=h[B];if(r.$isTextNode(v)){const G=v.getTextContent();if(v.is(m.node)){m.offset>G.length&&D(50,v.__key);w+=v.getTextContent().substr(0,m.offset);break}else w+=G}else w+="\u0004"}p.joinedText=w}}else D(52,h.__key)}d=K(e.joinedText,!1,!0,d.regEx)}if(null!=d){c={autoFormatCriteria:A,matchResultContext:d};break a}}}c={autoFormatCriteria:null,matchResultContext:null}}null!==c.autoFormatCriteria&&null!==c.matchResultContext&&(a=b,a.autoFormatCriteria=
23
24
  c.autoFormatCriteria,a.matchResultContext=c.matchResultContext)}});return a}
24
- function P(g){let f=null;g.read(()=>{const a=q.$getSelection();if(q.$isRangeSelection(a)&&a.isCollapsed()){var b=a.anchor.getNode(),c=b.getParent(),d=null!==c&&p.$isListItemNode(c);f={anchorOffset:a.anchor.offset,hasParentNode:null!==c,isCodeBlock:r.$isCodeNode(b),isParentAListItemNode:d,isSelectionCollapsed:a.isCollapsed(),isSimpleText:q.$isTextNode(b)&&b.isSimpleText(),nodeKey:b.getKey(),textContent:b.getTextContent()}}});return f}
25
- function Q(g){v.useEffect(()=>{let f=null;return g.addListener("update",({tags:a})=>{if(!1===a.has("historic")){var b=g.getEditorState();a=P(b);if(null==a)b=null;else{var c=a,d=f;if(null==c||null==d)b=null;else{var e=c.textContent.length,k=c.anchorOffset-1;b=!1===(!0===c.hasParentNode&&c.isSimpleText&&c.isSelectionCollapsed&&c.nodeKey===d.nodeKey&&c.anchorOffset!==d.anchorOffset&&0<=k&&k+1<=e&&" "===c.textContent.substr(k,1)&&c.textContent!==d.textContent)?null:M(b,c)}}null!=b&&L(g,b);f=a}else f=
26
- null})},[g])}module.exports=function(){const [g]=l.useLexicalComposerContext();Q(g);return null};
25
+ function P(g){let f=null;g.read(()=>{const a=r.$getSelection();if(r.$isRangeSelection(a)&&a.isCollapsed()){var b=a.anchor.getNode(),c=b.getParent(),d=null!==c&&q.$isListItemNode(c);f={anchorOffset:a.anchor.offset,hasParentNode:null!==c,isCodeBlock:t.$isCodeNode(b),isParentAListItemNode:d,isSelectionCollapsed:a.isCollapsed(),isSimpleText:r.$isTextNode(b)&&b.isSimpleText(),nodeKey:b.getKey(),textContent:b.getTextContent()}}});return f}
26
+ function Q(g){u.useEffect(()=>{let f=null;return g.addListener("update",({tags:a})=>{if(!1===a.has("historic")){var b=g.getEditorState();a=P(b);if(null==a)b=null;else{var c=a,d=f;if(null==c||null==d)b=null;else{var e=c.textContent.length,h=c.anchorOffset-1;b=!1===(!0===c.hasParentNode&&c.isSimpleText&&c.isSelectionCollapsed&&c.nodeKey===d.nodeKey&&c.anchorOffset!==d.anchorOffset&&0<=h&&h+1<=e&&" "===c.textContent.substr(h,1)&&c.textContent!==d.textContent)?null:N(b,c)}}null!=b&&M(g,b);f=a}else f=
27
+ null})},[g])}module.exports=function(){const [g]=k.useLexicalComposerContext();Q(g);return null};
@@ -0,0 +1,23 @@
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
+ * @flow strict
8
+ */
9
+
10
+ type ChangeHandler = (url: string | null, prevUrl: string | null) => void;
11
+
12
+ type LinkMatcherResult = {
13
+ text: string,
14
+ url: string,
15
+ length: number,
16
+ index: number,
17
+ };
18
+ export type LinkMatcher = (text: string) => LinkMatcherResult | null;
19
+
20
+ declare export default function LexicalAutoLinkPlugin(props: {
21
+ matchers: Array<LinkMatcher>,
22
+ onChange?: ChangeHandler,
23
+ }): React$Node;