@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
@@ -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,20 +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
- 'use strict';var n=require("@lexical/react/LexicalComposerContext"),p=require("@lexical/list"),r=require("lexical"),v=require("lexical/CodeNode"),x=require("react"),y=require("lexical/HeadingNode"),z=require("lexical/QuoteNode");function A(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 B(g,k,b,a){g=g.getChildren();const d=g.length;let e=0;for(let h=0;h<d&&!(e>=k);++h){const f=g[h];var c=r.$isTextNode(f)?f.getTextContent().length:a;c=e+c;if(e<=b&&b<c&&r.$isTextNode(f))return{node:f,offset:b-e};e=c}return null}
9
- const C={nodeTransformationKind:null,regEx:/(?:)/,regExCaptureGroupsToDelete:null,regExExpectedCaptureGroupCount:1,requiresParagraphStart:!1},D={...C,requiresParagraphStart:!0},F=[{...C,nodeTransformationKind:"textBold",regEx:/(\*)(\s*\b)([^\*]*)(\b\s*)(\*\s)$/,regExCaptureGroupsToDelete:[1,5],regExExpectedCaptureGroupCount:6}],G=[{...D,nodeTransformationKind:"paragraphH1",regEx:/(?:# )/},{...D,nodeTransformationKind:"paragraphH2",regEx:/(?:## )/},{...D,nodeTransformationKind:"paragraphH2",regEx:/(?:### )/},
10
- {...D,nodeTransformationKind:"paragraphBlockQuote",regEx:/(?:> )/},{...D,nodeTransformationKind:"paragraphUnorderedList",regEx:/(?:- )/},{...D,nodeTransformationKind:"paragraphUnorderedList",regEx:/(?:\* )/},{...D,nodeTransformationKind:"paragraphOrderedList",regEx:/^(\d+)\.\s/,regExExpectedCaptureGroupCount:2},{...D,nodeTransformationKind:"paragraphCodeBlock",regEx:/(?:``` )/},...F];
11
- function H(g,k,b,a,d){const e={regExCaptureGroups:[],triggerState:null};a=g.match(a);if(null!==a&&0<a.length&&a.length===d&&(!1===k||0===a.index)&&(!1===b||a.index+a[0].length===g.length)){g=a.length;k=a.index;for(b=0;b<g;b++)d=a[b],e.regExCaptureGroups.push({anchorTextNodeWithOffset:null,focusTextNodeWithOffset:null,offsetInParent:k,text:d,textLength:d.length-(b+1===g?1:0)}),0<b&&(k+=d.length);return e}return null}
12
- function I(g,k,b,a,d){d=d.regExCaptureGroups;const e=d.length;for(a+=1;a<e;a++){const c=d[a];null!=c.anchorTextNodeWithOffset&&c.anchorTextNodeWithOffset.offset>=k&&c.anchorTextNodeWithOffset.node.is(b)&&(c.anchorTextNodeWithOffset.offset+=g);null!=c.focusTextNodeWithOffset&&c.focusTextNodeWithOffset.offset>=k&&c.focusTextNodeWithOffset.node.is(b)&&(c.focusTextNodeWithOffset.offset+=g)}}
13
- function J(g,k){g.update(()=>{var b=r.$getSelection();if(null==b)b=null;else{var a=b.anchor.getNode();b=r.$isTextNode(a)?{node:a,offset:b.anchor.offset}:null}if(null!==b){b={joinedText:null,textNodeWithOffset:b};a:{a=!1===k.isParentAListItemNode?G:F;var d=a.length;for(var e=0;e<d;e++){var c=a[e];if(!1===k.isCodeBlock||"paragraphCodeBlock"===c.nodeTransformationKind){var h=c,f=b;if(null!==h.requiresParagraphStart&&!0===h.requiresParagraphStart)null===f.textNodeWithOffset.node.getPreviousSibling()?
14
- (f=f.textNodeWithOffset.node.getTextContent(),h=H(f,!0,!1,h.regEx,h.regExExpectedCaptureGroupCount)):h=null;else b:{if(null==f.joinedText){var q=f.textNodeWithOffset.node.getParentOrThrow();if(r.$isElementNode(q)){if(null==f.joinedText){var l=f,m=f.textNodeWithOffset;let u="";q=q.getChildren();const K=q.length;for(let w=0;w<K;++w){const t=q[w];if(r.$isTextNode(t)){const E=t.getTextContent();if(t.is(m.node)){m.offset>E.length&&A(50,t.__key);u+=t.getTextContent().substr(0,m.offset);break}else u+=E}else u+=
15
- "\u0004"}l.joinedText=u}h=H(f.joinedText,!1,!0,h.regEx,h.regExExpectedCaptureGroupCount);break b}A(52,q.__key)}h=null}if(null!=h){h.triggerState=k;a={autoFormatCriteria:c,matchResultContext:h};break a}}}a={autoFormatCriteria:null,matchResultContext:null}}if(null!==a.autoFormatCriteria&&null!==a.matchResultContext)if(d=a.autoFormatCriteria,a=a.matchResultContext,d.requiresParagraphStart){e=b.textNodeWithOffset;b=e.node.getParentOrThrow();e=e.node.spliceText(0,a.regExCaptureGroups[0].text.length,"",
16
- !0);""===e.getTextContent()&&(e.selectPrevious(),e.remove());e=b.getChildren();c=null;if(null!=d.nodeTransformationKind)switch(d.nodeTransformationKind){case "paragraphH1":c=y.$createHeadingNode("h1");c.append(...e);break;case "paragraphH2":c=y.$createHeadingNode("h2");c.append(...e);break;case "paragraphH3":c=y.$createHeadingNode("h3");c.append(...e);break;case "paragraphBlockQuote":c=z.$createQuoteNode();c.append(...e);break;case "paragraphUnorderedList":c=p.$createListNode("ul");a=p.$createListItemNode();
17
- a.append(...e);c.append(a);break;case "paragraphOrderedList":a=parseInt(1<a.regExCaptureGroups.length?a.regExCaptureGroups[a.regExCaptureGroups.length-1].text:"1",10);c=p.$createListNode("ol",a);a=p.$createListItemNode();a.append(...e);c.append(a);break;case "paragraphCodeBlock":c=null!=a.triggerState&&a.triggerState.isCodeBlock?r.$createParagraphNode():v.$createCodeNode(),c.append(...e)}a=c;null!==a&&b.replace(a)}else if(null!=d.nodeTransformationKind)switch(d.nodeTransformationKind){case "textBold":e=
18
- a.regExCaptureGroups;c=e.length;h=b.textNodeWithOffset.node.getParentOrThrow();if(null==b.joinedText)throw Error("joinedText was not calculated");b=b.joinedText.length;for(f=1;f<c;f++)if(l=e[f],l.anchorTextNodeWithOffset=B(h,b,l.offsetInParent,1),l.focusTextNodeWithOffset=B(h,b,l.offsetInParent+l.textLength,1),0>l.textLength)throw Error(`Bad regEx pattern found for ${d.nodeTransformationKind}`);a.regExCaptureGroups=e;if(null!=d.regExCaptureGroupsToDelete){b=d.regExCaptureGroupsToDelete;d=a.regExCaptureGroups;
19
- e=d.length;for(c=b.length-1;0<=c;c--)if(c<e&&(h=b[c],m=d[h],l=m.anchorTextNodeWithOffset,f=m.focusTextNodeWithOffset,null!=l&&null!=f&&0<m.textLength&&(m=r.$createRangeSelection(),m.anchor.set(l.node.getKey(),l.offset,"text"),m.focus.set(f.node.getKey(),f.offset,"text"),r.$setSelection(m),m=r.$getSelection(),null!=m)))if(m.removeText(),l.node.getKey()===f.node.getKey()){m=f.offset-l.offset;if(!(0<m))throw Error("Expected anchor and focus offsets to have ascending character order.");I(-m,f.offset,
20
- l.node,h,a)}else l=f.offset,0<l&&I(-l,l,f.node,h,a);b=a.regExCaptureGroups;if(!(3<b.length))throw Error("The capture group count in the RegEx does match the actual capture group count.");d=b[3];b=d.anchorTextNodeWithOffset;a=d.focusTextNodeWithOffset;null!=b&&null!=a&&0<d.textLength&&(d=r.$createRangeSelection(),d.anchor.set(b.node.getKey(),b.offset,"text"),d.focus.set(a.node.getKey(),a.offset,"text"),r.$setSelection(d),b=r.$getSelection(),null!=b&&b.formatText("bold"))}}}})}
21
- function L(g){let k=null;g.read(()=>{const b=r.$getSelection();if(null!=b&&b.isCollapsed()){var a=b.anchor.getNode(),d=a.getParent(),e=null!==d&&p.$isListItemNode(d);k={anchorOffset:b.anchor.offset,hasParentNode:null!==d,isCodeBlock:v.$isCodeNode(a),isParentAListItemNode:e,isSelectionCollapsed:b.isCollapsed(),isSimpleText:r.$isTextNode(a)&&a.isSimpleText(),nodeKey:a.getKey(),textContent:a.getTextContent()}}});return k}
22
- function M(g){x.useEffect(()=>{let k=null;g.addListener("update",({tags:b})=>{if(!1===b.has("historic")){b=L(g.getEditorState());var a=b;var d=k;if(null==a||null==d)a=!1;else{var e=a.textContent.length,c=a.anchorOffset-1;a=!0===a.hasParentNode&&a.isSimpleText&&a.isSelectionCollapsed&&a.nodeKey===d.nodeKey&&a.anchorOffset!==d.anchorOffset&&0<=c&&c+1<=e&&" "===a.textContent.substr(c,1)&&a.textContent!==d.textContent}a&&null!=b&&J(g,b);k=b}else k=null})},[g])}
23
- module.exports=function(){const [g]=n.useLexicalComposerContext();M(g);return null};
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=
24
+ c.autoFormatCriteria,a.matchResultContext=c.matchResultContext)}});return a}
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;
@@ -4,9 +4,9 @@
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
- 'use strict';var h=require("@lexical/react/LexicalComposerContext"),n=require("@lexical/react/withSubscriptions"),q=require("lexical"),v=require("lexical/AutoLinkNode"),w=require("react");function x(a,b){for(let c=0;c<b.length;c++){const d=b[c](a);if(d)return d}return null}function y(a){a=a.getPreviousSibling();q.$isElementNode(a)&&(a=a.getLastDescendant());return null===a||q.$isLineBreakNode(a)||q.$isTextNode(a)&&a.getTextContent().endsWith(" ")}
7
+ var h=require("@lexical/react/LexicalComposerContext"),n=require("@lexical/react/withSubscriptions"),q=require("lexical"),v=require("lexical/AutoLinkNode"),w=require("react");function x(a,b){for(let c=0;c<b.length;c++){const d=b[c](a);if(d)return d}return null}function y(a){a=a.getPreviousSibling();q.$isElementNode(a)&&(a=a.getLastDescendant());return null===a||q.$isLineBreakNode(a)||q.$isTextNode(a)&&a.getTextContent().endsWith(" ")}
8
8
  function C(a){a=a.getNextSibling();q.$isElementNode(a)&&(a=a.getFirstDescendant());return null===a||q.$isLineBreakNode(a)||q.$isTextNode(a)&&a.getTextContent().startsWith(" ")}
9
9
  function D(a,b,c){var d=a.getChildren();const e=d.length;for(let f=0;f<e;f++){const k=d[f];if(!q.$isTextNode(k)||!k.isSimpleText()){E(a);c(null,a.getURL());return}}d=a.getTextContent();b=x(d,b);null===b||b.text!==d?(E(a),c(null,a.getURL())):y(a)&&C(a)?(d=a.getURL(),null!==b&&d!==b.url&&(a.setURL(b.url),c(b.url,d))):(E(a),c(null,a.getURL()))}function E(a){const b=a.getChildren();var c=b.length;for(--c;0<=c;c--)a.insertAfter(b[c]);a.remove();return b.map(d=>d.getLatest())}
10
- function F(a,b,c){w.useEffect(()=>{if(!a.hasNodes([v.AutoLinkNode]))throw Error("LexicalAutoLinkPlugin: AutoLinkNode, TableCellNode or TableRowNode not registered on editor");const d=(...e)=>{c&&c(...e)};return n(a.addTransform(q.TextNode,e=>{var f=e.getParentOrThrow();if(v.$isAutoLinkNode(f))D(f,b,d);else{if(e.isSimpleText()){f=e.getTextContent();const G=f.length;let u=f,z=0,r=e;for(var k=0,g;(g=x(u,b))&&null!==g;){var t=g.index,l=z+t;const p=g.length;let A;A=0<l?" "===f[l-1]:y(e);let B;B=l+p<G?
11
- " "===f[l+p]:C(e);if(A&&B){var m=void 0;k=l-k;0===k?[m,r]=r.splitText(p):[,m,r]=r.splitText(k,k+p);l=v.$createAutoLinkNode(g.url);l.append(q.$createTextNode(g.text));m.replace(l);k+=p;d(g.url,null)}t+=p;u=u.substring(t);z+=t}}g=e.getPreviousSibling();m=e.getNextSibling();e=e.getTextContent();v.$isAutoLinkNode(g)&&!e.startsWith(" ")&&(E(g),d(null,g.getURL()));v.$isAutoLinkNode(m)&&!e.endsWith(" ")&&(E(m),d(null,m.getURL()))}}),a.addTransform(v.AutoLinkNode,e=>{D(e,b,d)}))},[a,b,c])}
12
- module.exports=function({matchers:a,onChange:b}){const [c]=h.useLexicalComposerContext();F(c,a,b);return null};
10
+ function F(a,b,c){w.useEffect(()=>{if(!a.hasNodes([v.AutoLinkNode]))throw Error("Minified Lexical error #53; see codes.json for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");const d=(...e)=>{c&&c(...e)};return n(a.addTransform(q.TextNode,e=>{var f=e.getParentOrThrow();if(v.$isAutoLinkNode(f))D(f,b,d);else{if(e.isSimpleText()){f=e.getTextContent();const G=f.length;let u=f,z=0,r=e;for(var k=0,g;(g=x(u,b))&&null!==g;){var t=g.index,l=z+t;
11
+ const p=g.length;let A;A=0<l?" "===f[l-1]:y(e);let B;B=l+p<G?" "===f[l+p]:C(e);if(A&&B){var m=void 0;k=l-k;0===k?[m,r]=r.splitText(p):[,m,r]=r.splitText(k,k+p);l=v.$createAutoLinkNode(g.url);l.append(q.$createTextNode(g.text));m.replace(l);k+=p;d(g.url,null)}t+=p;u=u.substring(t);z+=t}}g=e.getPreviousSibling();m=e.getNextSibling();e=e.getTextContent();v.$isAutoLinkNode(g)&&!e.startsWith(" ")&&(E(g),d(null,g.getURL()));v.$isAutoLinkNode(m)&&!e.endsWith(" ")&&(E(m),d(null,m.getURL()))}}),a.addTransform(v.AutoLinkNode,
12
+ e=>{D(e,b,d)}))},[a,b,c])}module.exports=function({matchers:a,onChange:b}){const [c]=h.useLexicalComposerContext();F(c,a,b);return null};
@@ -20,11 +20,15 @@ var OverflowNode = require('lexical/OverflowNode');
20
20
  *
21
21
  *
22
22
  */
23
- function $dfs__DEPRECATED(startingNode, nextNode) {
24
- let node = startingNode;
25
- nextNode(node);
23
+ function $dfs(startingNode, endingNode) {
24
+ const nodes = [];
25
+ const start = (startingNode || lexical.$getRoot()).getLatest();
26
+ const end = endingNode || (lexical.$isElementNode(start) ? start.getLastDescendant() : start);
27
+ let node = start;
28
+
29
+ while (node !== null && !node.is(end)) {
30
+ nodes.push(node);
26
31
 
27
- while (node !== null) {
28
32
  if (lexical.$isElementNode(node) && node.getChildrenSize() > 0) {
29
33
  node = node.getFirstChild();
30
34
  } else {
@@ -41,11 +45,13 @@ function $dfs__DEPRECATED(startingNode, nextNode) {
41
45
  }
42
46
  }
43
47
  }
48
+ }
44
49
 
45
- if (node !== null) {
46
- node = nextNode(node);
47
- }
50
+ if (node !== null && node.is(end)) {
51
+ nodes.push(node);
48
52
  }
53
+
54
+ return nodes;
49
55
  }
50
56
 
51
57
  /**
@@ -106,10 +112,9 @@ function useCharacterLimit(editor, maxCharacters, optional = Object.freeze({}))
106
112
  if (lastComputedTextLength === null || textLengthAboveThreshold) {
107
113
  const offset = findOffset(text, maxCharacters, strlen);
108
114
  editor.update(() => {
109
- lexical.$log('CharacterLimit');
110
115
  $wrapOverflowedNodes(offset);
111
116
  }, {
112
- tag: 'without-history'
117
+ tag: 'history-merge'
113
118
  });
114
119
  }
115
120
 
@@ -160,10 +165,13 @@ function findOffset(text, maxCharacters, strlen) {
160
165
  }
161
166
 
162
167
  function $wrapOverflowedNodes(offset) {
163
- const root = lexical.$getRoot();
168
+ const dfsNodes = $dfs();
169
+ const dfsNodesLength = dfsNodes.length;
164
170
  let accumulatedLength = 0;
165
- let previousNode = root;
166
- $dfs__DEPRECATED(root, node => {
171
+
172
+ for (let i = 0; i < dfsNodesLength; i += 1) {
173
+ const node = dfsNodes[i];
174
+
167
175
  if (OverflowNode.$isOverflowNode(node)) {
168
176
  const previousLength = accumulatedLength;
169
177
  const nextLength = accumulatedLength + node.getTextContentSize();
@@ -175,7 +183,7 @@ function $wrapOverflowedNodes(offset) {
175
183
  $unwrapNode(node);
176
184
  const selection = lexical.$getSelection(); // Restore selection when the overflow children are removed
177
185
 
178
- if (selection !== null && (!selection.anchor.getNode().isAttached() || !selection.focus.getNode().isAttached())) {
186
+ if (lexical.$isRangeSelection(selection) && (!selection.anchor.getNode().isAttached() || !selection.focus.getNode().isAttached())) {
179
187
  if (lexical.$isTextNode(previousSibling)) {
180
188
  previousSibling.select();
181
189
  } else if (lexical.$isTextNode(nextSibling)) {
@@ -184,8 +192,6 @@ function $wrapOverflowedNodes(offset) {
184
192
  parent.select();
185
193
  }
186
194
  }
187
-
188
- return previousNode;
189
195
  } else if (previousLength < offset) {
190
196
  const descendant = node.getFirstDescendant();
191
197
  const descendantLength = descendant !== null ? descendant.getTextContentSize() : 0;
@@ -197,7 +203,6 @@ function $wrapOverflowedNodes(offset) {
197
203
 
198
204
  if (firstDescendantIsSimpleText || firstDescendantDoesNotOverflow) {
199
205
  $unwrapNode(node);
200
- return previousNode;
201
206
  }
202
207
  }
203
208
  } else if (lexical.$isLeafNode(node)) {
@@ -223,10 +228,7 @@ function $wrapOverflowedNodes(offset) {
223
228
  mergePrevious(overflowNode);
224
229
  }
225
230
  }
226
-
227
- previousNode = node;
228
- return node;
229
- });
231
+ }
230
232
  }
231
233
 
232
234
  function $wrapNode(node) {
@@ -269,7 +271,7 @@ function mergePrevious(overflowNode) {
269
271
 
270
272
  const selection = lexical.$getSelection();
271
273
 
272
- if (selection !== null) {
274
+ if (lexical.$isRangeSelection(selection)) {
273
275
  const anchor = selection.anchor;
274
276
  const anchorNode = anchor.getNode();
275
277
  const focus = selection.focus;
@@ -0,0 +1,12 @@
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 LexicalCharacterLimitPlugin(props: {
11
+ charset: 'UTF-8' | 'UTF-16',
12
+ }): React$Node;
@@ -4,11 +4,12 @@
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
- 'use strict';var l=require("@lexical/react/LexicalComposerContext"),p=require("react"),q=require("lexical"),r=require("@lexical/react/withSubscriptions"),t=require("lexical/OverflowNode");function u(a,e){for(e(a);null!==a;){if(q.$isElementNode(a)&&0<a.getChildrenSize())a=a.getFirstChild();else{let f=null;for(;null===f&&null!==a;)f=a.getNextSibling(),a=null===f?a.getParent():f}null!==a&&(a=e(a))}}function w(){return q.$getRoot().getTextContent()}
8
- function x(a,e,f=Object.freeze({})){const {strlen:h=c=>c.length,remainingCharacters:d=()=>{}}=f;p.useEffect(()=>{if(!a.hasNodes([t.OverflowNode]))throw Error("useCharacterLimit: OverflowNode not registered on editor");},[a]);p.useEffect(()=>{let c=a.getEditorState().read(w),b=0;return r(a.addListener("textcontent",g=>{c=g}),a.addListener("update",({dirtyLeaves:g})=>{var k=a.isComposing();g=0<g.size;if(!k&&g){k=h(c);g=k>e||null!==b&&b>e;d(e-k);if(null===b||g){const n=y(c,e,h);a.update(()=>{q.$log("CharacterLimit");
9
- z(n)},{tag:"without-history"})}b=k}}))},[a,e,d,h])}function y(a,e,f){var h=Intl.Segmenter;let d=0;var c=0;if("function"===typeof h){a=(new h).segment(a);for(var {segment:b}of a){c+=f(b);if(c>e)break;d+=b.length}}else for(b=Array.from(a),a=b.length,h=0;h<a;h++){const g=b[h];c+=f(g);if(c>e)break;d+=g.length}return d}
10
- function z(a){const e=q.$getRoot();let f=0,h=e;u(e,d=>{if(t.$isOverflowNode(d)){var c=f;if(f+d.getTextContentSize()<=a){var b=d.getParent();c=d.getPreviousSibling();var g=d.getNextSibling();A(d);d=q.$getSelection();null===d||d.anchor.getNode().isAttached()&&d.focus.getNode().isAttached()||(q.$isTextNode(c)?c.select():q.$isTextNode(g)?g.select():null!==b&&b.select());return h}if(c<a&&(b=d.getFirstDescendant(),g=null!==b?b.getTextContentSize():0,c+=g,b=q.$isTextNode(b)&&b.isSimpleText(),c=c<=a,b||c))return A(d),
11
- h}else if(q.$isLeafNode(d)&&(c=f,f+=d.getTextContentSize(),f>a&&!t.$isOverflowNode(d.getParent())&&(b=q.$getSelection(),c<a&&q.$isTextNode(d)&&d.isSimpleText()?([,c]=d.splitText(a-c),c=B(c)):c=B(d),null!==b&&q.$setSelection(b),b=c,g=b.getPreviousSibling(),t.$isOverflowNode(g)))){var k=b.getFirstChild(),n=g.getChildren();c=n.length;if(null===k)b.append(...n);else for(var m=0;m<c;m++)k.insertBefore(n[m]);m=q.$getSelection();if(null!==m){k=m.anchor;n=k.getNode();m=m.focus;const v=k.getNode();n.is(g)?
12
- k.set(b.getKey(),k.offset,"element"):n.is(b)&&k.set(b.getKey(),c+k.offset,"element");v.is(g)?m.set(b.getKey(),m.offset,"element"):v.is(b)&&m.set(b.getKey(),c+m.offset,"element")}g.remove()}return h=d})}function B(a){const e=t.$createOverflowNode();a.insertBefore(e);e.append(a);return e}function A(a){const e=a.getChildren(),f=e.length;for(let h=0;h<f;h++)a.insertBefore(e[h]);a.remove();return 0<f?e[f-1]:null}let C=null;
13
- module.exports=function({charset:a="UTF-16"}){const [e]=l.useLexicalComposerContext(),[f,h]=p.useState(0),d=p.useMemo(()=>({remainingCharacters:h,strlen:c=>{if("UTF-8"===a){if(void 0===window.TextEncoder)var b=null;else null===C&&(C=new window.TextEncoder),b=C;null===b?(b=encodeURIComponent(c).match(/%[89ABab]/g),c=c.length+(b?b.length:0)):c=b.encode(c).length;return c}if("UTF-16"===a)return c.length;throw Error("Unrecognized charset");}}),[a]);x(e,5,d);return p.createElement("span",{className:`characters-limit ${0>
14
- f?"characters-limit-exceeded":""}`},f)};
7
+ var l=require("@lexical/react/LexicalComposerContext"),w=require("react"),x=require("lexical"),B=require("@lexical/react/withSubscriptions"),C=require("lexical/OverflowNode");function D(){return x.$getRoot().getTextContent()}
8
+ function F(b,d,p=Object.freeze({})){const {strlen:h=e=>e.length,remainingCharacters:r=()=>{}}=p;w.useEffect(()=>{if(!b.hasNodes([C.OverflowNode]))throw Error("Minified Lexical error #58; see codes.json for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");},[b]);w.useEffect(()=>{let e=b.getEditorState().read(D),f=0;return B(b.addListener("textcontent",q=>{e=q}),b.addListener("update",({dirtyLeaves:q})=>{var y=b.isComposing();q=0<q.size;if(!y&&
9
+ q){y=h(e);q=y>d||null!==f&&f>d;r(d-y);if(null===f||q){const u=G(e,d,h);b.update(()=>{const z=[];for(var c=x.$getRoot().getLatest(),A=x.$isElementNode(c)?c.getLastDescendant():c;null!==c&&!c.is(A);)if(z.push(c),x.$isElementNode(c)&&0<c.getChildrenSize())c=c.getFirstChild();else for(var t=null;null===t&&null!==c;)t=c.getNextSibling(),c=null===t?c.getParent():t;null!==c&&c.is(A)&&z.push(c);A=z.length;c=0;for(t=0;t<A;t+=1){var a=z[t];if(C.$isOverflowNode(a)){var g=c;if(c+a.getTextContentSize()<=u){var k=
10
+ a.getParent();g=a.getPreviousSibling();var m=a.getNextSibling();H(a);a=x.$getSelection();!x.$isRangeSelection(a)||a.anchor.getNode().isAttached()&&a.focus.getNode().isAttached()||(x.$isTextNode(g)?g.select():x.$isTextNode(m)?m.select():null!==k&&k.select())}else g<u&&(k=a.getFirstDescendant(),m=null!==k?k.getTextContentSize():0,g+=m,k=x.$isTextNode(k)&&k.isSimpleText(),g=g<=u,(k||g)&&H(a))}else if(x.$isLeafNode(a)&&(g=c,c+=a.getTextContentSize(),c>u&&!C.$isOverflowNode(a.getParent())&&(k=x.$getSelection(),
11
+ g<u&&x.$isTextNode(a)&&a.isSimpleText()?([,a]=a.splitText(u-g),a=I(a)):a=I(a),null!==k&&x.$setSelection(k),g=a.getPreviousSibling(),C.$isOverflowNode(g)))){m=a.getFirstChild();var v=g.getChildren();k=v.length;if(null===m)a.append(...v);else for(var n=0;n<k;n++)m.insertBefore(v[n]);n=x.$getSelection();if(x.$isRangeSelection(n)){m=n.anchor;v=m.getNode();n=n.focus;const E=m.getNode();v.is(g)?m.set(a.getKey(),m.offset,"element"):v.is(a)&&m.set(a.getKey(),k+m.offset,"element");E.is(g)?n.set(a.getKey(),
12
+ n.offset,"element"):E.is(a)&&n.set(a.getKey(),k+n.offset,"element")}g.remove()}}},{tag:"history-merge"})}f=y}}))},[b,d,r,h])}function G(b,d,p){var h=Intl.Segmenter;let r=0;var e=0;if("function"===typeof h){b=(new h).segment(b);for(var {segment:f}of b){e+=p(f);if(e>d)break;r+=f.length}}else for(f=Array.from(b),b=f.length,h=0;h<b;h++){const q=f[h];e+=p(q);if(e>d)break;r+=q.length}return r}function I(b){const d=C.$createOverflowNode();b.insertBefore(d);d.append(b);return d}
13
+ function H(b){const d=b.getChildren(),p=d.length;for(let h=0;h<p;h++)b.insertBefore(d[h]);b.remove();return 0<p?d[p-1]:null}let J=null;
14
+ module.exports=function({charset:b="UTF-16"}){const [d]=l.useLexicalComposerContext(),[p,h]=w.useState(0),r=w.useMemo(()=>({remainingCharacters:h,strlen:e=>{if("UTF-8"===b){if(void 0===window.TextEncoder)var f=null;else null===J&&(J=new window.TextEncoder),f=J;null===f?(f=encodeURIComponent(e).match(/%[89ABab]/g),e=e.length+(f?f.length:0)):e=f.encode(e).length;return e}if("UTF-16"===b)return e.length;throw Error("Unrecognized charset");}}),[b]);F(d,5,r);return w.createElement("span",{className:`characters-limit ${0>
15
+ p?"characters-limit-exceeded":""}`},p)};
@@ -0,0 +1,52 @@
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 lexical = require('lexical');
11
+ var react = require('react');
12
+
13
+ /**
14
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
15
+ *
16
+ * This source code is licensed under the MIT license found in the
17
+ * LICENSE file in the root directory of this source tree.
18
+ *
19
+ *
20
+ */
21
+ function LexicalClearEditorPlugin({
22
+ onClear
23
+ }) {
24
+ const [editor] = LexicalComposerContext.useLexicalComposerContext();
25
+ react.useLayoutEffect(() => {
26
+ return editor.addListener('command', (type, payload) => {
27
+ if (type === 'clearEditor') {
28
+ editor.update(() => {
29
+ if (onClear == null) {
30
+ const root = lexical.$getRoot();
31
+ const selection = lexical.$getSelection();
32
+ const paragraph = lexical.$createParagraphNode();
33
+ root.clear();
34
+ root.append(paragraph);
35
+
36
+ if (selection !== null) {
37
+ paragraph.select();
38
+ }
39
+ } else {
40
+ onClear();
41
+ }
42
+ });
43
+ return true;
44
+ }
45
+
46
+ return false;
47
+ }, 0);
48
+ }, [editor, onClear]);
49
+ return null;
50
+ }
51
+
52
+ module.exports = LexicalClearEditorPlugin;
@@ -0,0 +1,9 @@
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
+ const LexicalClearEditorPlugin = process.env.NODE_ENV === 'development' ? require('./LexicalClearEditorPlugin.dev.js') : require('./LexicalClearEditorPlugin.prod.js')
9
+ module.exports = LexicalClearEditorPlugin;
@@ -0,0 +1,14 @@
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 Props = $ReadOnly<{
11
+ onClear?: () => void,
12
+ }>;
13
+
14
+ declare export default function LexicalClearEditorPlugin(Props): React$Node;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ var a=require("@lexical/react/LexicalComposerContext"),f=require("lexical"),g=require("react");module.exports=function({onClear:b}){const [c]=a.useLexicalComposerContext();g.useLayoutEffect(()=>c.addListener("command",h=>"clearEditor"===h?(c.update(()=>{if(null==b){const d=f.$getRoot(),k=f.$getSelection(),e=f.$createParagraphNode();d.clear();d.append(e);null!==k&&e.select()}else b()}),!0):!1,0),[c,b]);return null};
@@ -9,6 +9,7 @@
9
9
  var LexicalComposerContext = require('@lexical/react/LexicalComposerContext');
10
10
  var React = require('react');
11
11
  var yjs = require('@lexical/yjs');
12
+ var lexical = require('lexical');
12
13
  var reactDom = require('react-dom');
13
14
 
14
15
  /**
@@ -20,7 +21,6 @@ var reactDom = require('react-dom');
20
21
  *
21
22
  */
22
23
  const EditorPriority = 0;
23
- const BootstrapPriority = 1;
24
24
  function useYjsCollaboration(editor, id, provider, docMap, name, color, shouldBootstrap) {
25
25
  const binding = React.useMemo(() => yjs.createBinding(editor, provider, id, docMap), [editor, provider, id, docMap]);
26
26
  const connect = React.useCallback(() => {
@@ -32,16 +32,7 @@ function useYjsCollaboration(editor, id, provider, docMap, name, color, shouldBo
32
32
  } catch (e) {// Do nothing
33
33
  }
34
34
  }, [provider]);
35
- const isInitialized = React.useRef(false);
36
- React.useLayoutEffect(() => {
37
- return editor.addListener('command', type => {
38
- if (type === 'bootstrapEditor') {
39
- return !isInitialized.current;
40
- }
41
-
42
- return false;
43
- }, BootstrapPriority);
44
- }, [editor]);
35
+ const bootstrapPropagationRef = React.useRef(true);
45
36
  React.useEffect(() => {
46
37
  const {
47
38
  root
@@ -58,8 +49,8 @@ function useYjsCollaboration(editor, id, provider, docMap, name, color, shouldBo
58
49
 
59
50
  const onSync = isSynced => {
60
51
  if (shouldBootstrap && isSynced && root.isEmpty() && root._xmlText._length === 0) {
61
- isInitialized.current = true;
62
- editor.execCommand('bootstrapEditor');
52
+ bootstrapPropagationRef.current = false;
53
+ initializeEditor(editor);
63
54
  }
64
55
  };
65
56
 
@@ -132,31 +123,16 @@ function useYjsCollaboration(editor, id, provider, docMap, name, color, shouldBo
132
123
  }
133
124
  function useYjsFocusTracking(editor, provider) {
134
125
  React.useEffect(() => {
135
- const onBlur = () => {
136
- yjs.setLocalStateFocus(provider, false);
137
- };
138
-
139
- const onFocus = () => {
140
- yjs.setLocalStateFocus(provider, true);
141
- };
142
-
143
- return editor.addListener('root', (rootElement, prevRootElement) => {
144
- // Clear our old listener if the root element changes
145
- if (prevRootElement !== null) {
146
- prevRootElement.removeEventListener('blur', onBlur);
147
- prevRootElement.removeEventListener('focus', onFocus);
126
+ return editor.addListener('command', (type, payload) => {
127
+ if (type === 'focus') {
128
+ yjs.setLocalStateFocus(provider, true);
129
+ } else if (type === 'blur') {
130
+ yjs.setLocalStateFocus(provider, false);
148
131
  }
149
132
 
150
- if (rootElement !== null) {
151
- if (document.activeElement === rootElement) {
152
- onFocus();
153
- }
154
-
155
- rootElement.addEventListener('blur', onBlur);
156
- rootElement.addEventListener('focus', onFocus);
157
- }
158
- });
159
- }, [editor, provider, provider.awareness]);
133
+ return false;
134
+ }, EditorPriority);
135
+ }, [editor, provider]);
160
136
  }
161
137
  function useYjsHistory(editor, binding) {
162
138
  const undoManager = React.useMemo(() => yjs.createUndoManager(binding, binding.root.getSharedType()), [binding]);
@@ -191,6 +167,25 @@ function useYjsHistory(editor, binding) {
191
167
  return clearHistory;
192
168
  }
193
169
 
170
+ function initializeEditor(editor) {
171
+ editor.update(() => {
172
+ const root = lexical.$getRoot();
173
+ const firstChild = root.getFirstChild();
174
+
175
+ if (firstChild === null) {
176
+ const paragraph = lexical.$createParagraphNode();
177
+ root.append(paragraph);
178
+ const activeElement = document.activeElement;
179
+
180
+ if (lexical.$getSelection() !== null || activeElement !== null && activeElement === editor.getRootElement()) {
181
+ paragraph.select();
182
+ }
183
+ }
184
+ }, {
185
+ tag: 'history-merge'
186
+ });
187
+ }
188
+
194
189
  /**
195
190
  * Copyright (c) Meta Platforms, Inc. and affiliates.
196
191
  *
@@ -0,0 +1,55 @@
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
+ import type {Doc, RelativePosition} from 'yjs';
11
+
12
+ export type UserState = {
13
+ anchorPos: null | RelativePosition,
14
+ focusPos: null | RelativePosition,
15
+ name: string,
16
+ color: string,
17
+ focusing: boolean,
18
+ };
19
+
20
+ export type ProviderAwareness = {
21
+ getLocalState: () => UserState,
22
+ setLocalState: (UserState) => void,
23
+ getStates: () => Array<UserState>,
24
+ on: (type: 'update', cb: () => void) => void,
25
+ off: (type: 'update', cb: () => void) => void,
26
+ };
27
+
28
+ export interface Provider {
29
+ connect(): void | Promise<void>;
30
+ disconnect(): void;
31
+ awareness: ProviderAwareness;
32
+ on(type: 'sync', cb: (isSynced: boolean) => void): void;
33
+ on(type: 'status', cb: ({status: string}) => void): void;
34
+ // $FlowFixMe: temp
35
+ on(type: 'update', cb: (any) => void): void;
36
+ off(type: 'sync', cb: (isSynced: boolean) => void): void;
37
+ // $FlowFixMe: temp
38
+ off(type: 'update', cb: (any) => void): void;
39
+ off(type: 'status', cb: ({status: string}) => void): void;
40
+ }
41
+
42
+ type CollaborationContextType = {
43
+ clientID: number,
44
+ color: string,
45
+ name: string,
46
+ yjsDocMap: Map<string, Doc>,
47
+ };
48
+
49
+ declare export function CollaborationPlugin(arg0: {
50
+ id: string,
51
+ providerFactory: (id: string, yjsDocMap: Map<string, Doc>) => Provider,
52
+ shouldBootstrap: boolean,
53
+ }): React$Node;
54
+ declare export var CollaborationContext: React$Context<CollaborationContextType>;
55
+ declare export function useCollaborationContext(): CollaborationContextType;
@@ -4,11 +4,10 @@
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
- 'use strict';var c=require("@lexical/react/LexicalComposerContext"),k=require("react"),z=require("@lexical/yjs"),A=require("react-dom");
8
- function B(b,d,a,f,g,l,r){const e=k.useMemo(()=>z.createBinding(b,a,d,f),[b,a,d,f]),m=k.useCallback(()=>{a.connect()},[a]),n=k.useCallback(()=>{try{a.disconnect()}catch(h){}},[a]),q=k.useRef(!1);k.useLayoutEffect(()=>b.addListener("command",h=>"bootstrapEditor"===h?!q.current:!1,1),[b]);k.useEffect(()=>{const {root:h}=e,{awareness:t}=a,v=({status:p})=>{b.execCommand("connected","connected"===p)},w=p=>{r&&p&&h.isEmpty()&&0===h._xmlText._length&&(q.current=!0,b.execCommand("bootstrapEditor"))},x=()=>
9
- {z.syncCursorPositions(e,a)},y=(p,u)=>{u.origin!==e&&z.syncYjsChangesToLexical(e,a,p)};z.initLocalState(a,g,l,document.activeElement===b.getRootElement());a.on("status",v);a.on("sync",w);t.on("update",x);h.getSharedType().observeDeep(y);const K=b.addListener("update",({prevEditorState:p,editorState:u,dirtyLeaves:G,dirtyElements:H,normalizedNodes:I,tags:J})=>{z.syncLexicalUpdateToYjs(e,a,p,u,H,G,I,J)});m();return()=>{n();a.off("sync",w);a.off("status",v);t.off("update",x);h.getSharedType().unobserveDeep(y);
10
- K()}},[e,l,m,n,b,g,a,r]);const L=k.useMemo(()=>A.createPortal(k.createElement("div",{ref:h=>{e.cursorsContainer=h}}),document.body),[e]);k.useEffect(()=>b.addListener("command",(h,t)=>{"toggleConnect"===h&&void 0!==m&&void 0!==n&&(t?(console.log("Collaboration connected!"),m()):(console.log("Collaboration disconnected!"),n()));return!1},0),[m,n,b]);return[L,e]}
11
- function C(b,d){k.useEffect(()=>{const a=()=>{z.setLocalStateFocus(d,!1)},f=()=>{z.setLocalStateFocus(d,!0)};return b.addListener("root",(g,l)=>{null!==l&&(l.removeEventListener("blur",a),l.removeEventListener("focus",f));null!==g&&(document.activeElement===g&&z.setLocalStateFocus(d,!0),g.addEventListener("blur",a),g.addEventListener("focus",f))})},[b,d,d.awareness])}
12
- function D(b,d){const a=k.useMemo(()=>z.createUndoManager(d,d.root.getSharedType()),[d]);k.useEffect(()=>b.addListener("command",f=>"undo"===f?(a.undo(),!0):"redo"===f?(a.redo(),!0):!1,0));return k.useCallback(()=>{a.clear()},[a])}
13
- const E=[["Cat","255,165,0"],["Dog","0,200,55"],["Rabbit","160,0,200"],["Frog","0,172,200"],["Fox","197,200,0"],["Hedgehog","31,200,0"],["Pigeon","200,0,0"],["Squirrel","200,0,148"],["Bear","255,235,0"],["Tiger","86,255,0"],["Leopard","0,255,208"],["Zebra","0,243,255"],["Wolf","0,102,255"],["Owl","147,0,255"],["Gull","255,0,153"],["Squid","0,220,255"]],F=E[Math.floor(Math.random()*(E.length-1+1))],M=k.createContext({clientID:0,color:F[1],name:F[0],yjsDocMap:new Map});
14
- function N(){return k.useContext(M)}exports.CollaborationContext=M;exports.CollaborationPlugin=function({id:b,providerFactory:d,shouldBootstrap:a}){const f=N(),{yjsDocMap:g,name:l,color:r}=f,[e]=c.useLexicalComposerContext(),m=k.useMemo(()=>d(b,g),[b,d,g]),[n,q]=B(e,b,m,g,l,r,a);f.clientID=q.clientID;D(e,q);C(e,m);return n};exports.useCollaborationContext=N;
7
+ var d=require("@lexical/react/LexicalComposerContext"),g=require("react"),u=require("@lexical/yjs"),A=require("lexical"),B=require("react-dom");
8
+ function C(b,c,a,h,n,p,q){const e=g.useMemo(()=>u.createBinding(b,a,c,h),[b,a,c,h]),k=g.useCallback(()=>{a.connect()},[a]),l=g.useCallback(()=>{try{a.disconnect()}catch(f){}},[a]),r=g.useRef(!0);g.useEffect(()=>{const {root:f}=e,{awareness:t}=a,w=({status:m})=>{b.execCommand("connected","connected"===m)},x=m=>{q&&m&&f.isEmpty()&&0===f._xmlText._length&&(r.current=!1,D(b))},y=()=>{u.syncCursorPositions(e,a)},z=(m,v)=>{v.origin!==e&&u.syncYjsChangesToLexical(e,a,m)};u.initLocalState(a,n,p,document.activeElement===
9
+ b.getRootElement());a.on("status",w);a.on("sync",x);t.on("update",y);f.getSharedType().observeDeep(z);const M=b.addListener("update",({prevEditorState:m,editorState:v,dirtyLeaves:I,dirtyElements:J,normalizedNodes:K,tags:L})=>{u.syncLexicalUpdateToYjs(e,a,m,v,J,I,K,L)});k();return()=>{l();a.off("sync",x);a.off("status",w);t.off("update",y);f.getSharedType().unobserveDeep(z);M()}},[e,p,k,l,b,n,a,q]);const N=g.useMemo(()=>B.createPortal(g.createElement("div",{ref:f=>{e.cursorsContainer=f}}),document.body),
10
+ [e]);g.useEffect(()=>b.addListener("command",(f,t)=>{"toggleConnect"===f&&void 0!==k&&void 0!==l&&(t?(console.log("Collaboration connected!"),k()):(console.log("Collaboration disconnected!"),l()));return!1},0),[k,l,b]);return[N,e]}function E(b,c){g.useEffect(()=>b.addListener("command",a=>{"focus"===a?u.setLocalStateFocus(c,!0):"blur"===a&&u.setLocalStateFocus(c,!1);return!1},0),[b,c])}
11
+ function F(b,c){const a=g.useMemo(()=>u.createUndoManager(c,c.root.getSharedType()),[c]);g.useEffect(()=>b.addListener("command",h=>"undo"===h?(a.undo(),!0):"redo"===h?(a.redo(),!0):!1,0));return g.useCallback(()=>{a.clear()},[a])}function D(b){b.update(()=>{var c=A.$getRoot();if(null===c.getFirstChild()){const a=A.$createParagraphNode();c.append(a);c=document.activeElement;(null!==A.$getSelection()||null!==c&&c===b.getRootElement())&&a.select()}},{tag:"history-merge"})}
12
+ const G=[["Cat","255,165,0"],["Dog","0,200,55"],["Rabbit","160,0,200"],["Frog","0,172,200"],["Fox","197,200,0"],["Hedgehog","31,200,0"],["Pigeon","200,0,0"],["Squirrel","200,0,148"],["Bear","255,235,0"],["Tiger","86,255,0"],["Leopard","0,255,208"],["Zebra","0,243,255"],["Wolf","0,102,255"],["Owl","147,0,255"],["Gull","255,0,153"],["Squid","0,220,255"]],H=G[Math.floor(Math.random()*(G.length-1+1))],O=g.createContext({clientID:0,color:H[1],name:H[0],yjsDocMap:new Map});
13
+ function P(){return g.useContext(O)}exports.CollaborationContext=O;exports.CollaborationPlugin=function({id:b,providerFactory:c,shouldBootstrap:a}){const h=P(),{yjsDocMap:n,name:p,color:q}=h,[e]=d.useLexicalComposerContext(),k=g.useMemo(()=>c(b,n),[b,c,n]),[l,r]=C(e,b,k,n,p,q,a);h.clientID=r.clientID;F(e,r);E(e,k);return l};exports.useCollaborationContext=P;