@lexical/react 0.1.8 → 0.1.9
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.
- package/DEPRECATED_useLexical.prod.js +1 -1
- package/DEPRECATED_useLexicalAutoFormatter.dev.js +142 -55
- package/DEPRECATED_useLexicalAutoFormatter.prod.js +19 -16
- package/DEPRECATED_useLexicalCanShowPlaceholder.prod.js +1 -1
- package/DEPRECATED_useLexicalCharacterLimit.dev.js +1 -2
- package/DEPRECATED_useLexicalCharacterLimit.prod.js +3 -3
- package/DEPRECATED_useLexicalDecorators.prod.js +1 -1
- package/DEPRECATED_useLexicalEditor.prod.js +1 -1
- package/DEPRECATED_useLexicalEditorEvents.prod.js +1 -1
- package/DEPRECATED_useLexicalHistory.dev.js +17 -14
- package/DEPRECATED_useLexicalHistory.prod.js +7 -7
- package/DEPRECATED_useLexicalList.prod.js +1 -1
- package/DEPRECATED_useLexicalPlainText.dev.js +46 -23
- package/DEPRECATED_useLexicalPlainText.prod.js +16 -16
- package/DEPRECATED_useLexicalRichText.dev.js +47 -24
- package/DEPRECATED_useLexicalRichText.prod.js +27 -27
- package/LexicalAutoFormatterPlugin.dev.js +142 -55
- package/LexicalAutoFormatterPlugin.prod.js +19 -17
- package/LexicalAutoLinkPlugin.prod.js +4 -4
- package/LexicalBootstrapPlugin.dev.js +0 -2
- package/LexicalBootstrapPlugin.prod.js +2 -2
- package/LexicalCharacterLimitPlugin.dev.js +1 -2
- package/LexicalCharacterLimitPlugin.prod.js +3 -3
- package/LexicalCollaborationPlugin.dev.js +11 -26
- package/LexicalCollaborationPlugin.prod.js +7 -8
- package/LexicalComposer.prod.js +1 -1
- package/LexicalComposerContext.prod.js +1 -1
- package/LexicalContentEditable.prod.js +1 -1
- package/LexicalHashtagPlugin.prod.js +1 -1
- package/LexicalHistoryPlugin.dev.js +17 -14
- package/LexicalHistoryPlugin.prod.js +6 -6
- package/LexicalHorizontalRuleNode.dev.js +66 -0
- package/LexicalHorizontalRuleNode.js +9 -0
- package/LexicalHorizontalRuleNode.prod.js +8 -0
- package/LexicalLinkPlugin.dev.js +0 -1
- package/LexicalLinkPlugin.prod.js +3 -3
- package/LexicalListPlugin.prod.js +1 -1
- package/LexicalNestedComposer.prod.js +1 -1
- package/LexicalOnChangePlugin.prod.js +1 -1
- package/LexicalPlainTextPlugin.dev.js +29 -9
- package/LexicalPlainTextPlugin.prod.js +12 -11
- package/LexicalRichTextPlugin.dev.js +30 -10
- package/LexicalRichTextPlugin.prod.js +16 -16
- package/LexicalTablePlugin.dev.js +20 -38
- package/LexicalTablePlugin.prod.js +3 -3
- package/LexicalTreeView.prod.js +1 -1
- package/package.json +5 -4
- package/useLexicalDecoratorMap.prod.js +1 -1
- package/useLexicalIsTextContentEmpty.prod.js +1 -1
- package/withSubscriptions.prod.js +1 -1
- package/LexicalHorizontalRulePlugin.dev.js +0 -51
- package/LexicalHorizontalRulePlugin.js +0 -9
- package/LexicalHorizontalRulePlugin.prod.js +0 -7
|
@@ -4,32 +4,32 @@
|
|
|
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
|
-
|
|
8
|
-
function D(a,
|
|
9
|
-
h&&!a._nodeMap.has(
|
|
10
|
-
function E(a,
|
|
11
|
-
function F(a,
|
|
12
|
-
{tag:"historic"})}return!0;case "redo":return h=b.redoStack,g=b.undoStack,0!==h.length&&(
|
|
13
|
-
r=null===
|
|
14
|
-
function H(a){a=a.getLatest();const
|
|
15
|
-
function I(a,
|
|
16
|
-
f.__children.indexOf(a.getKey()):a.getIndexWithinParent();a=d}
|
|
17
|
-
const
|
|
7
|
+
var l=require("@lexical/react/withSubscriptions"),w=require("lexical"),x=require("react"),z=require("@lexical/list"),A=require("lexical/CodeNode"),B=require("lexical/HeadingNode"),C=require("lexical/LinkNode");
|
|
8
|
+
function D(a,c,e,b,d){if(null===a||0===e.size&&0===b.size)return 0;var f=c._selection,h=a._selection;if(d)return 1;if(null===f||null===h||!h.isCollapsed()||!f.isCollapsed())return 0;var g=Array.from(e);b=Array.from(b);e=c._nodeMap;d=[];for(var k=0;k<g.length;k++){const m=e.get(g[k]);void 0!==m&&d.push(m)}for(g=0;g<b.length;g++)b[g][1]&&(k=e.get(b[g][0]),void 0===k||w.$isRootNode(k)||d.push(k));if(0===d.length)return 0;if(1<d.length)return b=c._nodeMap,c=b.get(f.anchor.key),h=b.get(h.anchor.key),c&&
|
|
9
|
+
h&&!a._nodeMap.has(c.__key)&&w.$isTextNode(c)&&1===c.__text.length&&1===f.anchor.offset?2:0;c=d[0];a=a._nodeMap.get(c.__key);if(!w.$isTextNode(a)||!w.$isTextNode(c)||a.__mode!==c.__mode)return 0;a=a.__text;c=c.__text;if(a===c)return 0;f=f.anchor;h=h.anchor;if(f.key!==h.key||"text"!==f.type)return 0;f=f.offset;h=h.offset;a=c.length-a.length;return 1===a&&h===f-1?2:-1===a&&h===f+1?3:-1===a&&h===f?4:0}
|
|
10
|
+
function E(a,c){let e=Date.now(),b=0;return(d,f,h,g,k,m)=>{const p=Date.now();if(m.has("historic"))return b=0,e=p,2;const n=D(d,f,g,k,a.isComposing()),t=(()=>{const q=m.has("history-push");if(!q&&m.has("history-merge"))return 0;if(null===d)return 1;var r=f._selection;const u=d._selection;if(!(0<g.size||0<k.size))return null===u&&null!==r?0:2;r=null===h||h.editor===a;return!1===q&&0!==n&&n===b&&p<e+c&&r?0:1})();e=p;b=n;return t}}
|
|
11
|
+
function F(a,c,e=1E3){const b=x.useMemo(()=>c||{current:null,redoStack:[],undoStack:[]},[c]),d=x.useCallback(()=>{b.undoStack=[];b.redoStack=[];b.current=null},[b]);x.useEffect(()=>{const f=E(a,e);return l(a.addListener("command",h=>{switch(h){case "undo":h=b.redoStack;var g=b.undoStack;if(0!==g.length){var k=b.current;const m=g.pop();null!==k&&(h.push(k),a.execCommand("canRedo",!0));0===g.length&&a.execCommand("canUndo",!1);b.current=m;m.editor.setEditorState(m.editorState.clone(m.undoSelection),
|
|
12
|
+
{tag:"historic"})}return!0;case "redo":return h=b.redoStack,g=b.undoStack,0!==h.length&&(k=b.current,null!==k&&(g.push(k),a.execCommand("canUndo",!0)),g=h.pop(),0===h.length&&a.execCommand("canRedo",!1),b.current=g,g.editor.setEditorState(g.editorState,{tag:"historic"})),!0;case "clearEditor":return d(),!1;case "clearHistory":return d(),!0;default:return!1}},0),a.addListener("update",({editorState:h,prevEditorState:g,dirtyLeaves:k,dirtyElements:m,tags:p})=>{const n=b.current,t=b.redoStack,q=b.undoStack,
|
|
13
|
+
r=null===n?null:n.editorState;if(null===n||h!==r){k=f(g,h,n,k,m,p);if(1===k)0!==t.length&&(b.redoStack=[]),null!==n&&(q.push({...n,undoSelection:g.read(w.$getSelection)}),a.execCommand("canUndo",!0));else if(2===k)return;b.current={editor:a,editorState:h}}}))},[d,e,a,b])}function G(a,c,e=1E3){return F(a,c,e)}
|
|
14
|
+
function H(a){a=a.getLatest();const c=a.constructor.clone(a);c.__parent=a.__parent;w.$isElementNode(a)&&w.$isElementNode(c)?(c.__children=Array.from(a.__children),c.__format=a.__format,c.__indent=a.__indent,c.__dir=a.__dir):w.$isTextNode(a)&&w.$isTextNode(c)?(c.__format=a.__format,c.__style=a.__style,c.__mode=a.__mode,c.__detail=a.__detail):w.$isDecoratorNode(a)&&w.$isDecoratorNode(c)&&(c.__state=a.__state);return c}
|
|
15
|
+
function I(a,c,e,b,d){for(var f=c;null!==a;){for(c=a.getParent();null!==c&&c.excludeFromCopy();)c=c.getParent();if(null===c)break;if(!w.$isElementNode(a)||!a.excludeFromCopy()){const h=a.getKey();let g=d.get(h);const k=void 0===g;k&&(g=H(a),d.set(h,g));!w.$isTextNode(g)||g.isSegmented()||g.isToken()?w.$isElementNode(g)&&(g.__children=g.__children.slice(e?f:0,e?void 0:f+1)):g.__text=g.__text.slice(e?f:0,e?void 0:f);if(w.$isRootNode(c)){k&&b.push(h);break}}f=d.get(c.getKey());f=w.$isElementNode(f)?
|
|
16
|
+
f.__children.indexOf(a.getKey()):a.getIndexWithinParent();a=c}}function J(a){a=a.anchor.getNode();return"rtl"===(w.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}function K(a,c,e){const b=a.getParent();let d=e;null!==b&&(c&&0===e?(d=a.getIndexWithinParent(),a=b):c||e!==a.getChildrenSize()||(d=a.getIndexWithinParent()+1,a=b));return a.getChildAtIndex(c?d-1:d)}
|
|
17
|
+
const L={"#text":a=>({node:w.$createTextNode(a.textContent)}),a:a=>({node:a instanceof HTMLAnchorElement?C.$createLinkNode(a.href):w.$createTextNode(a.textContent)}),b:a=>{const c="normal"===a.style.fontWeight;return{forChild:e=>{w.$isTextNode(e)&&!c&&e.toggleFormat("bold")},node:null}},br:()=>({node:w.$createLineBreakNode()}),div:a=>({after:c=>{const e=a.parentNode;null!=e&&a!==e.lastChild&&c.push(w.$createLineBreakNode());return c},node:null!==a.style.fontFamily.match("monospace")?A.$createCodeNode():
|
|
18
18
|
null}),em:()=>({forChild:a=>{w.$isTextNode(a)&&a.toggleFormat("italic")},node:null}),h1:()=>({node:B.$createHeadingNode("h1")}),h2:()=>({node:B.$createHeadingNode("h2")}),h3:()=>({node:B.$createHeadingNode("h3")}),h4:()=>({node:B.$createHeadingNode("h4")}),h5:()=>({node:B.$createHeadingNode("h5")}),i:()=>({forChild:a=>{w.$isTextNode(a)&&a.toggleFormat("italic")},node:null}),li:()=>({node:z.$createListItemNode()}),ol:()=>({node:z.$createListNode("ol")}),p:()=>({node:w.$createParagraphNode()}),pre:()=>
|
|
19
|
-
({node:A.$createCodeNode()}),span:a=>{const
|
|
19
|
+
({node:A.$createCodeNode()}),span:a=>{const c="700"===a.style.fontWeight;return{forChild:e=>{w.$isTextNode(e)&&c&&e.toggleFormat("bold")},node:null}},strong:()=>({forChild:a=>{w.$isTextNode(a)&&a.toggleFormat("bold")},node:null}),table:a=>({node:a.classList.contains("js-file-line-container")?A.$createCodeNode():null}),td:a=>{const c=a.classList.contains("js-file-line");return{after:e=>{c&&a.parentNode&&a.parentNode.nextSibling&&e.push(w.$createLineBreakNode());return e},node:null}},u:()=>({forChild:a=>
|
|
20
20
|
{w.$isTextNode(a)&&a.toggleFormat("underline")},node:null}),ul:()=>({node:z.$createListNode("ul")})};
|
|
21
|
-
function
|
|
22
|
-
function
|
|
23
|
-
e),null!==b&&(a=a.concat(b));e=a;a=[];f=null;for(h=0;h<e.length;h++)g=e[h],!w.$isElementNode(g)||g.isInline()?(null===f&&(f=w.$createParagraphNode(),a.push(f)),null!==f&&f.append(g)):(a.push(g),f=null);
|
|
24
|
-
function
|
|
25
|
-
function
|
|
26
|
-
function
|
|
27
|
-
|
|
28
|
-
r=[];I(t,
|
|
29
|
-
function
|
|
30
|
-
|
|
31
|
-
function
|
|
32
|
-
|
|
33
|
-
e.canInsertTab()?"\t"===b.getTextContent()[
|
|
34
|
-
|
|
35
|
-
|
|
21
|
+
function M(a,c,e,b=new Map){let d=[],f=null;var h=a.nodeName.toLowerCase(),g=(e._config.htmlTransforms||{})[h]||c[h],k=g?g(a):null;g=null;if(null!==k){g=k.after;f=k.node;if(null!==f){d.push(f);const m=Array.from(b.values());for(let p=0;p<m.length;p++)m[p](f)}null!=k.forChild&&b.set(h,k.forChild)}a=a.childNodes;h=[];for(k=0;k<a.length;k++)h.push(...M(a[k],c,e,b));null!=g&&(h=g(h));null==f?d=d.concat(h):w.$isElementNode(f)&&f.append(...h);return d}
|
|
22
|
+
function N(a,c,e){var b=a.getData("application/x-lexical-editor");if(b){var d=e._config.namespace;try{const k=JSON.parse(b);if(k.namespace===d){const {range:m,nodeMap:p}=k.state;var f=new Map(p);b=[];for(d=0;d<m.length;d++){var h=f.get(m[d]);if(void 0!==h){var g=w.$createNodeFromParse(h,f);b.push(g)}}c.insertNodes(b);return}}catch(k){}}if(f=a.getData("text/html")){f=(new DOMParser).parseFromString(f,"text/html");a=[];f=f.body?Array.from(f.body.childNodes):[];h=f.length;for(g=0;g<h;g++)b=M(f[g],L,
|
|
23
|
+
e),null!==b&&(a=a.concat(b));e=a;a=[];f=null;for(h=0;h<e.length;h++)g=e[h],!w.$isElementNode(g)||g.isInline()?(null===f&&(f=w.$createParagraphNode(),a.push(f)),null!==f&&f.append(g)):(a.push(g),f=null);c.insertNodes(a)}else e=a.getData("text/plain"),null!=e&&c.insertRawText(e)}
|
|
24
|
+
function O(a,c){var e=a.focus;a=e.offset;"element"===e.type?(e=e.getNode(),c=K(e,c,a)):(e=e.getNode(),c&&0===a||!c&&a===e.getTextContentSize()?(a=c?e.getPreviousSibling():e.getNextSibling(),c=null===a?K(e.getParentOrThrow(),c,e.getIndexWithinParent()+(c?0:1)):a):c=null);return w.$isDecoratorNode(c)&&!c.isIsolated()}function P(a,c){a.preventDefault();c.update(()=>{const e=w.$getSelection(),b=a.clipboardData;null!=b&&null!==e&&N(b,e,c)})}
|
|
25
|
+
function Q(a,c){R(a,c);c.update(()=>{const e=w.$getSelection();null!==e&&e.removeText()})}
|
|
26
|
+
function R(a,c){a.preventDefault();c.update(()=>{const e=a.clipboardData;var b=w.$getSelection();if(null!==b&&null!=e){var d=window.getSelection();if(!d.isCollapsed){var f=d.getRangeAt(0);f&&(d=document.createElement("div"),f=f.cloneContents(),d.appendChild(f),e.setData("text/html",d.innerHTML));e.setData("text/plain",b.getTextContent());d=c._config.namespace;f=e.setData;var h=JSON,g=h.stringify;{var k=b.anchor,m=b.focus;var p=k.getCharacterOffset();const y=m.getCharacterOffset();var n=k.getNode(),
|
|
27
|
+
t=m.getNode(),q=n.getParentOrThrow();if(n===t&&w.$isTextNode(n)&&(q.canBeEmpty()||1<q.getChildrenSize()))b=H(n),n=y>p,b.__text=b.__text.slice(n?p:y,n?y:p),p=b.getKey(),p={nodeMap:[[p,b]],range:[p]};else if(b=b.getNodes(),0===b.length)p={nodeMap:[],range:[]};else{n=b.length;t=b[0];q=t.getParent();if(null!==q&&(!q.canBeEmpty()||w.$isRootNode(q))){var r=q.__children;if(r.length===n){var u=!0;for(var v=0;v<r.length;v++)if(r[v]!==b[v].__key){u=!1;break}u&&(n++,b.push(q))}}q=b[n-1];k=k.isBefore(m);m=new Map;
|
|
28
|
+
r=[];I(t,k?p:y,!0,r,m);for(t=0;t<n;t++)if(u=b[t],v=u.getKey(),!(m.has(v)||w.$isElementNode(u)&&u.excludeFromCopy())){const S=H(u);w.$isRootNode(u.getParent())&&r.push(u.getKey());m.set(v,S)}I(q,k?y:p,!1,r,m);p={nodeMap:Array.from(m.entries()),range:r}}}f.call(e,"application/x-lexical-editor",g.call(h,{namespace:d,state:p}))}}})}var T="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement?x.useLayoutEffect:x.useEffect;
|
|
29
|
+
function U(a){x.useEffect(()=>{const c=e=>{var b=a.getRootElement();if(document.activeElement===b&&(b=e.data,"string"===typeof b)){try{var d=JSON.parse(b)}catch(f){return}if(d&&"nuanria_messaging"===d.protocol&&"request"===d.type&&(d=d.payload)&&"makeChanges"===d.functionId&&(d=d.args)){const [f,h,g,k,m]=d;a.update(()=>{const p=w.$getSelection();if(null!==p){var n=p.anchor;let t=n.getNode(),q=0,r=0;w.$isTextNode(t)&&0<=f&&0<=h&&(q=f,r=f+h,p.setTextNodeRange(t,q,t,r));if(q!==r||""!==g)p.insertRawText(g),
|
|
30
|
+
t=n.getNode();w.$isTextNode(t)&&(q=k,r=k+m,n=t.getTextContentSize(),q=q>n?n:q,r=r>n?n:r,p.setTextNodeRange(t,q,t,r));e.stopImmediatePropagation()}})}}};window.addEventListener("message",c,!0);return()=>{window.removeEventListener("message",c,!0)}},[a])}
|
|
31
|
+
function V(a){T(()=>{const c=a.addListener("command",(e,b)=>{var d=w.$getSelection();if(null===d)return!1;switch(e){case "deleteCharacter":return d.deleteCharacter(b),!0;case "deleteWord":return d.deleteWord(b),!0;case "deleteLine":return d.deleteLine(b),!0;case "insertText":return"string"===typeof b?d.insertText(b):(e=b.dataTransfer,null!=e?N(e,d,a):(b=b.data)&&d.insertText(b)),!0;case "removeText":return d.removeText(),!0;case "formatText":return d.formatText(b),!0;case "formatElement":return d=
|
|
32
|
+
d.anchor.getNode(),(w.$isElementNode(d)?d:d.getParentOrThrow()).setFormat(b),!0;case "insertLineBreak":return d.insertLineBreak(b),!0;case "insertParagraph":return d.insertParagraph(),!0;case "indentContent":return d=d.anchor,d="element"===d.type?d.getNode():d.getNode().getParentOrThrow(),d.canInsertTab()?a.execCommand("insertText","\t"):10!==d.getIndent()&&d.setIndent(d.getIndent()+1),!0;case "outdentContent":return d=d.anchor,b=d.getNode(),e="element"===d.type?d.getNode():d.getNode().getParentOrThrow(),
|
|
33
|
+
e.canInsertTab()?"\t"===b.getTextContent()[d.offset-1]&&a.execCommand("deleteCharacter",!0):0!==e.getIndent()&&e.setIndent(e.getIndent()-1),!0;case "keyArrowLeft":e=b.shiftKey;if(O(d,!0))return b.preventDefault(),b=e,e=J(d),d.modify(b?"extend":"move",!e,"character"),!0;break;case "keyArrowRight":e=b.shiftKey;if(O(d,!1))return b.preventDefault(),b=e,e=J(d),d.modify(b?"extend":"move",e,"character"),!0;break;case "keyBackspace":return b.preventDefault(),{anchor:b}=d,d.isCollapsed()&&0===b.offset&&0<
|
|
34
|
+
("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(),!0;case "copy":return R(b,a),
|
|
35
|
+
!0;case "cut":return Q(b,a),!0;case "paste":return P(b,a),!0;case "drop":case "dragstart":return b.preventDefault(),!0}return!1},0);a.execCommand("bootstrapEditor");return c},[a]);U(a)}module.exports=function(a,c){V(a);G(a,c)};
|
|
@@ -11,6 +11,7 @@ var list = require('@lexical/list');
|
|
|
11
11
|
var lexical = require('lexical');
|
|
12
12
|
var CodeNode = require('lexical/CodeNode');
|
|
13
13
|
var react = require('react');
|
|
14
|
+
var LexicalHorizontalRuleNode = require('@lexical/react/LexicalHorizontalRuleNode');
|
|
14
15
|
var HeadingNode = require('lexical/HeadingNode');
|
|
15
16
|
var QuoteNode = require('lexical/QuoteNode');
|
|
16
17
|
|
|
@@ -73,17 +74,26 @@ function $findNodeWithOffsetFromJoinedText(elementNode, joinedTextLength, offset
|
|
|
73
74
|
const children = elementNode.getChildren();
|
|
74
75
|
const childrenLength = children.length;
|
|
75
76
|
let runningLength = 0;
|
|
77
|
+
let isPriorNodeTextNode = false;
|
|
76
78
|
|
|
77
79
|
for (let i = 0; i < childrenLength; ++i) {
|
|
78
|
-
|
|
80
|
+
// We must examine the offsetInJoinedText that is located
|
|
81
|
+
// at the length of the string.
|
|
82
|
+
// For example, given "hello", the length is 5, yet
|
|
83
|
+
// the caller still wants the node + offset at the
|
|
84
|
+
// right edge of the "o".
|
|
85
|
+
if (runningLength > joinedTextLength) {
|
|
79
86
|
break;
|
|
80
87
|
}
|
|
81
88
|
|
|
82
89
|
const child = children[i];
|
|
83
|
-
const
|
|
90
|
+
const isChildNodeTestNode = lexical.$isTextNode(child);
|
|
91
|
+
const childContentLength = isChildNodeTestNode ? child.getTextContent().length : separatorLength;
|
|
84
92
|
const newRunningLength = runningLength + childContentLength;
|
|
93
|
+
const isJoinedOffsetWithinNode = isPriorNodeTextNode === false && runningLength === offsetInJoinedText || runningLength === 0 && runningLength === offsetInJoinedText || runningLength < offsetInJoinedText && offsetInJoinedText <= newRunningLength;
|
|
85
94
|
|
|
86
|
-
if (
|
|
95
|
+
if (isJoinedOffsetWithinNode && lexical.$isTextNode(child)) {
|
|
96
|
+
// Check isTextNode again for flow.
|
|
87
97
|
return {
|
|
88
98
|
node: child,
|
|
89
99
|
offset: offsetInJoinedText - runningLength
|
|
@@ -91,6 +101,7 @@ function $findNodeWithOffsetFromJoinedText(elementNode, joinedTextLength, offset
|
|
|
91
101
|
}
|
|
92
102
|
|
|
93
103
|
runningLength = newRunningLength;
|
|
104
|
+
isPriorNodeTextNode = isChildNodeTestNode;
|
|
94
105
|
}
|
|
95
106
|
|
|
96
107
|
return null;
|
|
@@ -115,8 +126,6 @@ const SEPARATOR_BETWEEN_TEXT_AND_NON_TEXT_NODES = '\u0004'; // Select an unused
|
|
|
115
126
|
const autoFormatBase = {
|
|
116
127
|
nodeTransformationKind: null,
|
|
117
128
|
regEx: /(?:)/,
|
|
118
|
-
regExCaptureGroupsToDelete: null,
|
|
119
|
-
regExExpectedCaptureGroupCount: 1,
|
|
120
129
|
requiresParagraphStart: false
|
|
121
130
|
};
|
|
122
131
|
const paragraphStartBase = { ...autoFormatBase,
|
|
@@ -152,38 +161,58 @@ const markdownCodeBlock = { ...paragraphStartBase,
|
|
|
152
161
|
};
|
|
153
162
|
const markdownOrderedList = { ...paragraphStartBase,
|
|
154
163
|
nodeTransformationKind: 'paragraphOrderedList',
|
|
155
|
-
regEx: /^(\d+)\.\s
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
164
|
+
regEx: /^(\d+)\.\s/
|
|
165
|
+
};
|
|
166
|
+
const markdownHorizontalRule = { ...paragraphStartBase,
|
|
167
|
+
nodeTransformationKind: 'horizontalRule',
|
|
168
|
+
regEx: /(?:\*\*\* )/
|
|
169
|
+
};
|
|
170
|
+
const markdownHorizontalRuleUsingDashes = { ...paragraphStartBase,
|
|
171
|
+
nodeTransformationKind: 'horizontalRule',
|
|
172
|
+
regEx: /(?:--- )/
|
|
159
173
|
};
|
|
160
174
|
const markdownBold = { ...autoFormatBase,
|
|
161
175
|
nodeTransformationKind: 'textBold',
|
|
162
176
|
// regEx: /(\*)(?:\s*\b)(?:[^\*]*)(?:\b\s*)(\*\s)$/, // The $ will find the target at the end of the string.
|
|
163
|
-
regEx: /(\*)(\s*\b)([^\*]*)(\b\s*)(\*\s)
|
|
164
|
-
// Remove the first and last capture groups. Remeber, the 0th capture group is the entire string.
|
|
177
|
+
regEx: /(\*)(\s*\b)([^\*]*)(\b\s*)(\*\s)$/ // Remove the first and last capture groups. Remeber, the 0th capture group is the entire string.
|
|
165
178
|
// e.g. "*Hello* " requires removing both "*" as well as bolding "Hello".
|
|
166
|
-
|
|
167
|
-
// The $ will find the target at the end of the string.
|
|
168
|
-
regExExpectedCaptureGroupCount: 6
|
|
179
|
+
|
|
169
180
|
};
|
|
170
181
|
const allAutoFormatCriteriaForTextNodes = [markdownBold];
|
|
171
|
-
const allAutoFormatCriteria = [markdownHeader1, markdownHeader2, markdownHeader3, markdownBlockQuote, markdownUnorderedListDash, markdownUnorderedListAsterisk, markdownOrderedList, markdownCodeBlock, ...allAutoFormatCriteriaForTextNodes];
|
|
182
|
+
const allAutoFormatCriteria = [markdownHeader1, markdownHeader2, markdownHeader3, markdownBlockQuote, markdownUnorderedListDash, markdownUnorderedListAsterisk, markdownOrderedList, markdownCodeBlock, markdownHorizontalRule, markdownHorizontalRuleUsingDashes, ...allAutoFormatCriteriaForTextNodes];
|
|
172
183
|
function getAllAutoFormatCriteriaForTextNodes() {
|
|
173
184
|
return allAutoFormatCriteriaForTextNodes;
|
|
174
185
|
}
|
|
175
186
|
function getAllAutoFormatCriteria() {
|
|
176
187
|
return allAutoFormatCriteria;
|
|
177
188
|
}
|
|
189
|
+
function getInitialScanningContext(textNodeWithOffset, triggerState) {
|
|
190
|
+
return {
|
|
191
|
+
autoFormatCriteria: {
|
|
192
|
+
nodeTransformationKind: 'noTransformation',
|
|
193
|
+
regEx: /(?:)/,
|
|
194
|
+
// Empty reg ex will do until the precise criteria is discovered.
|
|
195
|
+
requiresParagraphStart: null
|
|
196
|
+
},
|
|
197
|
+
joinedText: null,
|
|
198
|
+
matchResultContext: {
|
|
199
|
+
offsetInJoinedTextForCollapsedSelection: 0,
|
|
200
|
+
regExCaptureGroups: []
|
|
201
|
+
},
|
|
202
|
+
textNodeWithOffset,
|
|
203
|
+
triggerState
|
|
204
|
+
};
|
|
205
|
+
}
|
|
178
206
|
|
|
179
|
-
function getMatchResultContextWithRegEx(textToSearch, matchMustAppearAtStartOfString, matchMustAppearAtEndOfString, regEx
|
|
207
|
+
function getMatchResultContextWithRegEx(textToSearch, matchMustAppearAtStartOfString, matchMustAppearAtEndOfString, regEx) {
|
|
180
208
|
const matchResultContext = {
|
|
181
|
-
|
|
182
|
-
|
|
209
|
+
offsetInJoinedTextForCollapsedSelection: 0,
|
|
210
|
+
regExCaptureGroups: []
|
|
183
211
|
};
|
|
184
212
|
const regExMatches = textToSearch.match(regEx);
|
|
185
213
|
|
|
186
|
-
if (regExMatches !== null && regExMatches.length > 0 &&
|
|
214
|
+
if (regExMatches !== null && regExMatches.length > 0 && (matchMustAppearAtStartOfString === false || regExMatches.index === 0) && (matchMustAppearAtEndOfString === false || regExMatches.index + regExMatches[0].length === textToSearch.length)) {
|
|
215
|
+
matchResultContext.offsetInJoinedTextForCollapsedSelection = textToSearch.length;
|
|
187
216
|
const captureGroupsCount = regExMatches.length;
|
|
188
217
|
let runningLength = regExMatches.index;
|
|
189
218
|
|
|
@@ -215,7 +244,7 @@ function getMatchResultContextForParagraphs(autoFormatCriteria, scanningContext)
|
|
|
215
244
|
|
|
216
245
|
if (textNodeWithOffset.node.getPreviousSibling() === null) {
|
|
217
246
|
const textToSearch = scanningContext.textNodeWithOffset.node.getTextContent();
|
|
218
|
-
return getMatchResultContextWithRegEx(textToSearch, true, false, autoFormatCriteria.regEx
|
|
247
|
+
return getMatchResultContextWithRegEx(textToSearch, true, false, autoFormatCriteria.regEx);
|
|
219
248
|
}
|
|
220
249
|
|
|
221
250
|
return null;
|
|
@@ -231,7 +260,7 @@ function getMatchResultContextForText(autoFormatCriteria, scanningContext) {
|
|
|
231
260
|
scanningContext.joinedText = $joinTextNodesInElementNode(parentNode, SEPARATOR_BETWEEN_TEXT_AND_NON_TEXT_NODES, scanningContext.textNodeWithOffset);
|
|
232
261
|
}
|
|
233
262
|
|
|
234
|
-
return getMatchResultContextWithRegEx(scanningContext.joinedText, false, true, autoFormatCriteria.regEx
|
|
263
|
+
return getMatchResultContextWithRegEx(scanningContext.joinedText, false, true, autoFormatCriteria.regEx);
|
|
235
264
|
} else {
|
|
236
265
|
{
|
|
237
266
|
throw Error(`Expected node ${parentNode.__key} to to be a ElementNode.`);
|
|
@@ -251,8 +280,11 @@ function getMatchResultContextForCriteria(autoFormatCriteria, scanningContext) {
|
|
|
251
280
|
return getMatchResultContextForText(autoFormatCriteria, scanningContext);
|
|
252
281
|
}
|
|
253
282
|
|
|
254
|
-
function getNewNodeForCriteria(
|
|
283
|
+
function getNewNodeForCriteria(scanningContext, element) {
|
|
255
284
|
let newNode = null;
|
|
285
|
+
const children = element.getChildren();
|
|
286
|
+
const autoFormatCriteria = scanningContext.autoFormatCriteria;
|
|
287
|
+
const matchResultContext = scanningContext.matchResultContext;
|
|
256
288
|
|
|
257
289
|
if (autoFormatCriteria.nodeTransformationKind != null) {
|
|
258
290
|
switch (autoFormatCriteria.nodeTransformationKind) {
|
|
@@ -307,7 +339,7 @@ function getNewNodeForCriteria(autoFormatCriteria, matchResultContext, children)
|
|
|
307
339
|
case 'paragraphCodeBlock':
|
|
308
340
|
{
|
|
309
341
|
// Toggle code and paragraph nodes.
|
|
310
|
-
if (
|
|
342
|
+
if (scanningContext.triggerState != null && scanningContext.triggerState.isCodeBlock) {
|
|
311
343
|
newNode = lexical.$createParagraphNode();
|
|
312
344
|
} else {
|
|
313
345
|
newNode = CodeNode.$createCodeNode();
|
|
@@ -316,6 +348,14 @@ function getNewNodeForCriteria(autoFormatCriteria, matchResultContext, children)
|
|
|
316
348
|
newNode.append(...children);
|
|
317
349
|
return newNode;
|
|
318
350
|
}
|
|
351
|
+
|
|
352
|
+
case 'horizontalRule':
|
|
353
|
+
{
|
|
354
|
+
// return null for newNode. Insert the HR here.
|
|
355
|
+
const horizontalRuleNode = LexicalHorizontalRuleNode.$createHorizontalRuleNode();
|
|
356
|
+
element.insertBefore(horizontalRuleNode);
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
319
359
|
}
|
|
320
360
|
}
|
|
321
361
|
|
|
@@ -331,39 +371,46 @@ function updateTextNode(node, count) {
|
|
|
331
371
|
}
|
|
332
372
|
}
|
|
333
373
|
|
|
334
|
-
function transformTextNodeForAutoFormatCriteria(scanningContext
|
|
335
|
-
if (autoFormatCriteria.requiresParagraphStart) {
|
|
336
|
-
transformTextNodeForParagraphs(scanningContext
|
|
374
|
+
function transformTextNodeForAutoFormatCriteria(scanningContext) {
|
|
375
|
+
if (scanningContext.autoFormatCriteria.requiresParagraphStart) {
|
|
376
|
+
transformTextNodeForParagraphs(scanningContext);
|
|
337
377
|
} else {
|
|
338
|
-
transformTextNodeForText(scanningContext
|
|
378
|
+
transformTextNodeForText(scanningContext);
|
|
339
379
|
}
|
|
340
380
|
}
|
|
341
381
|
|
|
342
|
-
function transformTextNodeForParagraphs(scanningContext
|
|
382
|
+
function transformTextNodeForParagraphs(scanningContext) {
|
|
343
383
|
const textNodeWithOffset = scanningContext.textNodeWithOffset;
|
|
344
384
|
const element = textNodeWithOffset.node.getParentOrThrow();
|
|
345
|
-
const text = matchResultContext.regExCaptureGroups[0].text;
|
|
385
|
+
const text = scanningContext.matchResultContext.regExCaptureGroups[0].text;
|
|
346
386
|
updateTextNode(textNodeWithOffset.node, text.length);
|
|
347
|
-
const elementNode = getNewNodeForCriteria(
|
|
387
|
+
const elementNode = getNewNodeForCriteria(scanningContext, element);
|
|
348
388
|
|
|
349
389
|
if (elementNode !== null) {
|
|
350
390
|
element.replace(elementNode);
|
|
351
391
|
}
|
|
352
392
|
}
|
|
353
393
|
|
|
354
|
-
function transformTextNodeForText(scanningContext
|
|
394
|
+
function transformTextNodeForText(scanningContext) {
|
|
395
|
+
const autoFormatCriteria = scanningContext.autoFormatCriteria;
|
|
396
|
+
const matchResultContext = scanningContext.matchResultContext;
|
|
397
|
+
|
|
355
398
|
if (autoFormatCriteria.nodeTransformationKind != null) {
|
|
356
399
|
switch (autoFormatCriteria.nodeTransformationKind) {
|
|
357
400
|
case 'textBold':
|
|
358
401
|
{
|
|
359
|
-
matchResultContext.regExCaptureGroups
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
//
|
|
363
|
-
|
|
364
|
-
formatTextInCaptureGroupIndex('bold', 3, matchResultContext);
|
|
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;
|
|
365
407
|
}
|
|
366
408
|
|
|
409
|
+
matchResultContext.regExCaptureGroups = getCaptureGroupsByResolvingAllDetails(scanningContext); // Remove unwanted text in reg ex pattern.
|
|
410
|
+
|
|
411
|
+
removeTextInCaptureGroups([1, 5], matchResultContext);
|
|
412
|
+
formatTextInCaptureGroupIndex('bold', 3, matchResultContext);
|
|
413
|
+
makeCollapsedSelectionAtOffsetInJoinedText(matchResultContext.offsetInJoinedTextForCollapsedSelection, matchResultContext.offsetInJoinedTextForCollapsedSelection + 1, scanningContext.textNodeWithOffset.node.getParentOrThrow());
|
|
367
414
|
break;
|
|
368
415
|
}
|
|
369
416
|
}
|
|
@@ -373,7 +420,9 @@ function transformTextNodeForText(scanningContext, autoFormatCriteria, matchResu
|
|
|
373
420
|
// known, the details may be fully resolved without incurring unwasted performance cost.
|
|
374
421
|
|
|
375
422
|
|
|
376
|
-
function getCaptureGroupsByResolvingAllDetails(scanningContext
|
|
423
|
+
function getCaptureGroupsByResolvingAllDetails(scanningContext) {
|
|
424
|
+
const autoFormatCriteria = scanningContext.autoFormatCriteria;
|
|
425
|
+
const matchResultContext = scanningContext.matchResultContext;
|
|
377
426
|
const textNodeWithOffset = scanningContext.textNodeWithOffset;
|
|
378
427
|
const regExCaptureGroups = matchResultContext.regExCaptureGroups;
|
|
379
428
|
const captureGroupsCount = regExCaptureGroups.length;
|
|
@@ -445,6 +494,12 @@ function removeTextInCaptureGroups(regExCaptureGroupsToDelete, matchResultContex
|
|
|
445
494
|
}
|
|
446
495
|
|
|
447
496
|
function shiftCaptureGroupOffsets(delta, applyAtOrAfterOffset, node, startingCaptureGroupIndex, matchResultContext) {
|
|
497
|
+
matchResultContext.offsetInJoinedTextForCollapsedSelection += delta;
|
|
498
|
+
|
|
499
|
+
if (!(matchResultContext.offsetInJoinedTextForCollapsedSelection > 0)) {
|
|
500
|
+
throw Error(`The text content string length does not correlate with insertions/deletions of new text.`);
|
|
501
|
+
}
|
|
502
|
+
|
|
448
503
|
const regExCaptureGroups = matchResultContext.regExCaptureGroups;
|
|
449
504
|
const regExCaptureGroupsCount = regExCaptureGroups.length;
|
|
450
505
|
|
|
@@ -482,10 +537,25 @@ function formatTextInCaptureGroupIndex(formatType, captureGroupIndex, matchResul
|
|
|
482
537
|
|
|
483
538
|
if (currentSelection != null) {
|
|
484
539
|
currentSelection.formatText(formatType);
|
|
540
|
+
const finalSelection = lexical.$createRangeSelection();
|
|
541
|
+
finalSelection.anchor.set(focusTextNodeWithOffset.node.getKey(), focusTextNodeWithOffset.offset + 1, 'text');
|
|
542
|
+
finalSelection.focus.set(focusTextNodeWithOffset.node.getKey(), focusTextNodeWithOffset.offset + 1, 'text');
|
|
543
|
+
lexical.$setSelection(finalSelection);
|
|
485
544
|
}
|
|
486
545
|
}
|
|
487
546
|
}
|
|
488
547
|
|
|
548
|
+
function makeCollapsedSelectionAtOffsetInJoinedText(offsetInJoinedText, joinedTextLength, parentElementNode) {
|
|
549
|
+
const textNodeWithOffset = $findNodeWithOffsetFromJoinedText(parentElementNode, joinedTextLength, offsetInJoinedText, TRIGGER_STRING_LENGTH);
|
|
550
|
+
|
|
551
|
+
if (textNodeWithOffset != null) {
|
|
552
|
+
const newSelection = lexical.$createRangeSelection();
|
|
553
|
+
newSelection.anchor.set(textNodeWithOffset.node.getKey(), textNodeWithOffset.offset, 'text');
|
|
554
|
+
newSelection.focus.set(textNodeWithOffset.node.getKey(), textNodeWithOffset.offset, 'text');
|
|
555
|
+
lexical.$setSelection(newSelection);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
489
559
|
/**
|
|
490
560
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
491
561
|
*
|
|
@@ -495,17 +565,17 @@ function formatTextInCaptureGroupIndex(formatType, captureGroupIndex, matchResul
|
|
|
495
565
|
*
|
|
496
566
|
*/
|
|
497
567
|
|
|
498
|
-
function getCriteriaWithMatchResultContext(autoFormatCriteriaArray,
|
|
568
|
+
function getCriteriaWithMatchResultContext(autoFormatCriteriaArray, scanningContext) {
|
|
569
|
+
const currentTriggerState = scanningContext.triggerState;
|
|
499
570
|
const count = autoFormatCriteriaArray.length;
|
|
500
571
|
|
|
501
572
|
for (let i = 0; i < count; i++) {
|
|
502
573
|
const autoFormatCriteria = autoFormatCriteriaArray[i]; // Skip code block nodes, unless the nodeTransformationKind calls for toggling the code block.
|
|
503
574
|
|
|
504
|
-
if (currentTriggerState.isCodeBlock === false || autoFormatCriteria.nodeTransformationKind === 'paragraphCodeBlock') {
|
|
575
|
+
if (currentTriggerState != null && currentTriggerState.isCodeBlock === false || autoFormatCriteria.nodeTransformationKind === 'paragraphCodeBlock') {
|
|
505
576
|
const matchResultContext = getMatchResultContextForCriteria(autoFormatCriteria, scanningContext);
|
|
506
577
|
|
|
507
578
|
if (matchResultContext != null) {
|
|
508
|
-
matchResultContext.triggerState = currentTriggerState;
|
|
509
579
|
return {
|
|
510
580
|
autoFormatCriteria: autoFormatCriteria,
|
|
511
581
|
matchResultContext
|
|
@@ -537,8 +607,17 @@ function getTextNodeForAutoFormatting(selection) {
|
|
|
537
607
|
};
|
|
538
608
|
}
|
|
539
609
|
|
|
540
|
-
function updateAutoFormatting(editor,
|
|
610
|
+
function updateAutoFormatting(editor, scanningContext) {
|
|
541
611
|
editor.update(() => {
|
|
612
|
+
transformTextNodeForAutoFormatCriteria(scanningContext);
|
|
613
|
+
}, {
|
|
614
|
+
tag: 'history-push'
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
function findScanningContextWithValidMatch(editorState, currentTriggerState) {
|
|
619
|
+
let scanningContext = null;
|
|
620
|
+
editorState.read(() => {
|
|
542
621
|
const textNodeWithOffset = getTextNodeForAutoFormatting(lexical.$getSelection());
|
|
543
622
|
|
|
544
623
|
if (textNodeWithOffset === null) {
|
|
@@ -546,24 +625,25 @@ function updateAutoFormatting(editor, currentTriggerState) {
|
|
|
546
625
|
} // Please see the declaration of ScanningContext for a detailed explanation.
|
|
547
626
|
|
|
548
627
|
|
|
549
|
-
const
|
|
550
|
-
joinedText: null,
|
|
551
|
-
textNodeWithOffset
|
|
552
|
-
};
|
|
628
|
+
const initialScanningContext = getInitialScanningContext(textNodeWithOffset, currentTriggerState);
|
|
553
629
|
const criteriaWithMatchResultContext = getCriteriaWithMatchResultContext( // Do not apply paragraph node changes like blockQuote or H1 to listNodes. Also, do not attempt to transform a list into a list using * or -.
|
|
554
|
-
currentTriggerState.isParentAListItemNode === false ? getAllAutoFormatCriteria() : getAllAutoFormatCriteriaForTextNodes(),
|
|
630
|
+
currentTriggerState.isParentAListItemNode === false ? getAllAutoFormatCriteria() : getAllAutoFormatCriteriaForTextNodes(), initialScanningContext);
|
|
555
631
|
|
|
556
632
|
if (criteriaWithMatchResultContext.autoFormatCriteria === null || criteriaWithMatchResultContext.matchResultContext === null) {
|
|
557
633
|
return;
|
|
558
634
|
}
|
|
559
635
|
|
|
560
|
-
|
|
636
|
+
scanningContext = initialScanningContext; // Lazy fill-in the particular format criteria and any matching result information.
|
|
637
|
+
|
|
638
|
+
scanningContext.autoFormatCriteria = criteriaWithMatchResultContext.autoFormatCriteria;
|
|
639
|
+
scanningContext.matchResultContext = criteriaWithMatchResultContext.matchResultContext;
|
|
561
640
|
});
|
|
641
|
+
return scanningContext;
|
|
562
642
|
}
|
|
563
643
|
|
|
564
|
-
function
|
|
644
|
+
function findScanningContext(editorState, currentTriggerState, priorTriggerState) {
|
|
565
645
|
if (currentTriggerState == null || priorTriggerState == null) {
|
|
566
|
-
return
|
|
646
|
+
return null;
|
|
567
647
|
} // The below checks needs to execute relativey quickly, so perform the light-weight ones first.
|
|
568
648
|
// The substr check is a quick way to avoid autoformat parsing in that it looks for the autoformat
|
|
569
649
|
// trigger which is the trigger string (" ").
|
|
@@ -572,7 +652,12 @@ function shouldAttemptToAutoFormat(currentTriggerState, priorTriggerState) {
|
|
|
572
652
|
const triggerStringLength = TRIGGER_STRING.length;
|
|
573
653
|
const currentTextContentLength = currentTriggerState.textContent.length;
|
|
574
654
|
const triggerOffset = currentTriggerState.anchorOffset - triggerStringLength;
|
|
575
|
-
|
|
655
|
+
|
|
656
|
+
if ((currentTriggerState.hasParentNode === true && currentTriggerState.isSimpleText && currentTriggerState.isSelectionCollapsed && currentTriggerState.nodeKey === priorTriggerState.nodeKey && currentTriggerState.anchorOffset !== priorTriggerState.anchorOffset && triggerOffset >= 0 && triggerOffset + triggerStringLength <= currentTextContentLength && currentTriggerState.textContent.substr(triggerOffset, triggerStringLength) === TRIGGER_STRING && currentTriggerState.textContent !== priorTriggerState.textContent) === false) {
|
|
657
|
+
return null;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
return findScanningContextWithValidMatch(editorState, currentTriggerState);
|
|
576
661
|
}
|
|
577
662
|
|
|
578
663
|
function getTriggerState(editorState) {
|
|
@@ -609,15 +694,17 @@ function useAutoFormatter(editor) {
|
|
|
609
694
|
// For example, typing "#" and then " ", shoud trigger an format.
|
|
610
695
|
// However, given "#A B", where the user delets "A" should not.
|
|
611
696
|
let priorTriggerState = null;
|
|
612
|
-
editor.addListener('update', ({
|
|
697
|
+
return editor.addListener('update', ({
|
|
613
698
|
tags
|
|
614
699
|
}) => {
|
|
615
700
|
// Examine historic so that we are not running autoformatting within markdown.
|
|
616
701
|
if (tags.has('historic') === false) {
|
|
617
|
-
const
|
|
702
|
+
const editorState = editor.getEditorState();
|
|
703
|
+
const currentTriggerState = getTriggerState(editorState);
|
|
704
|
+
const scanningContext = currentTriggerState == null ? null : findScanningContext(editorState, currentTriggerState, priorTriggerState);
|
|
618
705
|
|
|
619
|
-
if (
|
|
620
|
-
updateAutoFormatting(editor,
|
|
706
|
+
if (scanningContext != null) {
|
|
707
|
+
updateAutoFormatting(editor, scanningContext);
|
|
621
708
|
}
|
|
622
709
|
|
|
623
710
|
priorTriggerState = currentTriggerState;
|
|
@@ -4,20 +4,22 @@
|
|
|
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
|
-
|
|
8
|
-
function
|
|
9
|
-
const
|
|
10
|
-
{...
|
|
11
|
-
function
|
|
12
|
-
function
|
|
13
|
-
function
|
|
14
|
-
(
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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(),null!=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(),null!=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,"text"),
|
|
19
|
+
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(null==b)b=null;else{var c=b.anchor.getNode();b=q.$isTextNode(c)?{node:c,offset:b.anchor.offset}: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,N=c.length;for(let x=
|
|
21
|
+
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="";k=k.getChildren();const O=
|
|
22
|
+
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=c.autoFormatCriteria,
|
|
23
|
+
a.matchResultContext=c.matchResultContext)}});return a}function P(g){let f=null;g.read(()=>{const a=q.$getSelection();if(null!=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}
|
|
24
|
+
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=
|
|
25
|
+
null})},[g])}module.exports=function(){const [g]=l.useLexicalComposerContext();Q(g);return null};
|