@lexical/react 0.1.4 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DEPRECATED_useLexical.dev.js +84 -0
- package/DEPRECATED_useLexical.js +9 -2
- package/DEPRECATED_useLexical.prod.js +8 -0
- package/DEPRECATED_useLexicalAutoFormatter.dev.js +642 -0
- package/DEPRECATED_useLexicalAutoFormatter.js +9 -12
- package/DEPRECATED_useLexicalAutoFormatter.prod.js +22 -0
- package/DEPRECATED_useLexicalCanShowPlaceholder.dev.js +136 -0
- package/DEPRECATED_useLexicalCanShowPlaceholder.js +9 -3
- package/DEPRECATED_useLexicalCanShowPlaceholder.prod.js +8 -0
- package/DEPRECATED_useLexicalCharacterLimit.dev.js +294 -0
- package/DEPRECATED_useLexicalCharacterLimit.js +9 -9
- package/DEPRECATED_useLexicalCharacterLimit.prod.js +14 -0
- package/DEPRECATED_useLexicalDecorators.dev.js +82 -0
- package/DEPRECATED_useLexicalDecorators.js +9 -2
- package/DEPRECATED_useLexicalDecorators.prod.js +8 -0
- package/DEPRECATED_useLexicalEditor.dev.js +52 -0
- package/DEPRECATED_useLexicalEditor.js +9 -1
- package/DEPRECATED_useLexicalEditor.prod.js +7 -0
- package/DEPRECATED_useLexicalEditorEvents.dev.js +96 -0
- package/DEPRECATED_useLexicalEditorEvents.js +9 -2
- package/DEPRECATED_useLexicalEditorEvents.prod.js +8 -0
- package/DEPRECATED_useLexicalHistory.dev.js +339 -0
- package/DEPRECATED_useLexicalHistory.js +9 -7
- package/DEPRECATED_useLexicalHistory.prod.js +13 -0
- package/DEPRECATED_useLexicalList.dev.js +64 -0
- package/DEPRECATED_useLexicalList.js +9 -12
- package/DEPRECATED_useLexicalList.prod.js +7 -0
- package/DEPRECATED_useLexicalPlainText.dev.js +755 -0
- package/DEPRECATED_useLexicalPlainText.js +9 -17
- package/DEPRECATED_useLexicalPlainText.prod.js +22 -0
- package/DEPRECATED_useLexicalRichText.dev.js +1326 -0
- package/DEPRECATED_useLexicalRichText.js +9 -30
- package/DEPRECATED_useLexicalRichText.prod.js +35 -0
- package/LexicalAutoFormatterPlugin.dev.js +645 -0
- package/LexicalAutoFormatterPlugin.js +9 -13
- package/LexicalAutoFormatterPlugin.prod.js +23 -0
- package/LexicalAutoLinkPlugin.dev.js +227 -0
- package/LexicalAutoLinkPlugin.js +9 -5
- package/LexicalAutoLinkPlugin.prod.js +12 -0
- package/LexicalBootstrapPlugin.dev.js +124 -0
- package/LexicalBootstrapPlugin.js +9 -0
- package/LexicalBootstrapPlugin.prod.js +8 -0
- package/LexicalCharacterLimitPlugin.dev.js +352 -0
- package/LexicalCharacterLimitPlugin.js +9 -10
- package/LexicalCharacterLimitPlugin.prod.js +14 -0
- package/LexicalCollaborationPlugin.dev.js +235 -0
- package/LexicalCollaborationPlugin.js +9 -8
- package/LexicalCollaborationPlugin.prod.js +14 -0
- package/LexicalComposer.dev.js +76 -0
- package/LexicalComposer.js +9 -2
- package/LexicalComposer.prod.js +9 -0
- package/LexicalComposerContext.dev.js +53 -0
- package/LexicalComposerContext.js +9 -1
- package/LexicalComposerContext.prod.js +7 -0
- package/LexicalContentEditable.dev.js +71 -0
- package/LexicalContentEditable.js +9 -3
- package/LexicalContentEditable.prod.js +9 -0
- package/LexicalHashtagPlugin.dev.js +152 -0
- package/LexicalHashtagPlugin.js +9 -4
- package/LexicalHashtagPlugin.prod.js +10 -0
- package/LexicalHistoryPlugin.dev.js +344 -0
- package/LexicalHistoryPlugin.js +9 -7
- package/LexicalHistoryPlugin.prod.js +13 -0
- package/LexicalHorizontalRulePlugin.dev.js +51 -0
- package/LexicalHorizontalRulePlugin.js +9 -1
- package/LexicalHorizontalRulePlugin.prod.js +7 -0
- package/LexicalLinkPlugin.dev.js +137 -0
- package/LexicalLinkPlugin.js +9 -3
- package/LexicalLinkPlugin.prod.js +9 -0
- package/LexicalListPlugin.dev.js +67 -0
- package/LexicalListPlugin.js +9 -12
- package/LexicalListPlugin.prod.js +8 -0
- package/LexicalNestedComposer.dev.js +60 -0
- package/LexicalNestedComposer.js +9 -1
- package/LexicalNestedComposer.prod.js +8 -0
- package/LexicalOnChangePlugin.dev.js +57 -0
- package/LexicalOnChangePlugin.js +9 -1
- package/LexicalOnChangePlugin.prod.js +7 -0
- package/LexicalPlainTextPlugin.dev.js +565 -0
- package/LexicalPlainTextPlugin.js +9 -13
- package/LexicalPlainTextPlugin.prod.js +17 -0
- package/LexicalRichTextPlugin.dev.js +1136 -0
- package/LexicalRichTextPlugin.js +9 -26
- package/LexicalRichTextPlugin.prod.js +31 -0
- package/LexicalTablePlugin.dev.js +95 -0
- package/LexicalTablePlugin.js +9 -4
- package/LexicalTablePlugin.prod.js +9 -0
- package/LexicalTreeView.dev.js +340 -0
- package/LexicalTreeView.js +9 -12
- package/LexicalTreeView.prod.js +19 -0
- package/README.md +11 -6
- package/package.json +4 -4
- package/useLexicalDecoratorMap.dev.js +83 -0
- package/useLexicalDecoratorMap.js +9 -1
- package/useLexicalDecoratorMap.prod.js +8 -0
- package/useLexicalIsTextContentEmpty.dev.js +84 -0
- package/useLexicalIsTextContentEmpty.js +9 -2
- package/useLexicalIsTextContentEmpty.prod.js +8 -0
- package/withSubscriptions.dev.js +23 -0
- package/withSubscriptions.js +9 -1
- package/withSubscriptions.prod.js +7 -0
|
@@ -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
|
+
'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};
|
|
@@ -0,0 +1,227 @@
|
|
|
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 withSubscriptions = require('@lexical/react/withSubscriptions');
|
|
11
|
+
var lexical = require('lexical');
|
|
12
|
+
var AutoLinkNode = require('lexical/AutoLinkNode');
|
|
13
|
+
var react = require('react');
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
17
|
+
*
|
|
18
|
+
* This source code is licensed under the MIT license found in the
|
|
19
|
+
* LICENSE file in the root directory of this source tree.
|
|
20
|
+
*
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
function findFirstMatch(text, matchers) {
|
|
25
|
+
for (let i = 0; i < matchers.length; i++) {
|
|
26
|
+
const match = matchers[i](text);
|
|
27
|
+
|
|
28
|
+
if (match) {
|
|
29
|
+
return match;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function isPreviousNodeValid(node) {
|
|
37
|
+
let previousNode = node.getPreviousSibling();
|
|
38
|
+
|
|
39
|
+
if (lexical.$isElementNode(previousNode)) {
|
|
40
|
+
previousNode = previousNode.getLastDescendant();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return previousNode === null || lexical.$isLineBreakNode(previousNode) || lexical.$isTextNode(previousNode) && previousNode.getTextContent().endsWith(' ');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isNextNodeValid(node) {
|
|
47
|
+
let nextNode = node.getNextSibling();
|
|
48
|
+
|
|
49
|
+
if (lexical.$isElementNode(nextNode)) {
|
|
50
|
+
nextNode = nextNode.getFirstDescendant();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return nextNode === null || lexical.$isLineBreakNode(nextNode) || lexical.$isTextNode(nextNode) && nextNode.getTextContent().startsWith(' ');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function handleLinkCreation(node, matchers, onChange) {
|
|
57
|
+
const nodeText = node.getTextContent();
|
|
58
|
+
const nodeTextLength = nodeText.length;
|
|
59
|
+
let text = nodeText;
|
|
60
|
+
let textOffset = 0;
|
|
61
|
+
let lastNode = node;
|
|
62
|
+
let lastNodeOffset = 0;
|
|
63
|
+
let match;
|
|
64
|
+
|
|
65
|
+
while ((match = findFirstMatch(text, matchers)) && match !== null) {
|
|
66
|
+
const matchOffset = match.index;
|
|
67
|
+
const offset = textOffset + matchOffset;
|
|
68
|
+
const matchLength = match.length; // Previous node is valid if any of:
|
|
69
|
+
// 1. Space before same node
|
|
70
|
+
// 2. Space in previous simple text node
|
|
71
|
+
// 3. Previous node is LineBreakNode
|
|
72
|
+
|
|
73
|
+
let contentBeforeMatchIsValid;
|
|
74
|
+
|
|
75
|
+
if (offset > 0) {
|
|
76
|
+
contentBeforeMatchIsValid = nodeText[offset - 1] === ' ';
|
|
77
|
+
} else {
|
|
78
|
+
contentBeforeMatchIsValid = isPreviousNodeValid(node);
|
|
79
|
+
} // Next node is valid if any of:
|
|
80
|
+
// 1. Space after same node
|
|
81
|
+
// 2. Space in next simple text node
|
|
82
|
+
// 3. Next node is LineBreakNode
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
let contentAfterMatchIsValid;
|
|
86
|
+
|
|
87
|
+
if (offset + matchLength < nodeTextLength) {
|
|
88
|
+
contentAfterMatchIsValid = nodeText[offset + matchLength] === ' ';
|
|
89
|
+
} else {
|
|
90
|
+
contentAfterMatchIsValid = isNextNodeValid(node);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (contentBeforeMatchIsValid && contentAfterMatchIsValid) {
|
|
94
|
+
let middleNode;
|
|
95
|
+
const lastNodeMatchOffset = offset - lastNodeOffset;
|
|
96
|
+
|
|
97
|
+
if (lastNodeMatchOffset === 0) {
|
|
98
|
+
[middleNode, lastNode] = lastNode.splitText(matchLength);
|
|
99
|
+
} else {
|
|
100
|
+
[, middleNode, lastNode] = lastNode.splitText(lastNodeMatchOffset, lastNodeMatchOffset + matchLength);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const linkNode = AutoLinkNode.$createAutoLinkNode(match.url);
|
|
104
|
+
linkNode.append(lexical.$createTextNode(match.text));
|
|
105
|
+
middleNode.replace(linkNode);
|
|
106
|
+
lastNodeOffset = lastNodeMatchOffset + matchLength;
|
|
107
|
+
onChange(match.url, null);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const iterationOffset = matchOffset + matchLength;
|
|
111
|
+
text = text.substring(iterationOffset);
|
|
112
|
+
textOffset += iterationOffset;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function handleLinkEdit(linkNode, matchers, onChange) {
|
|
117
|
+
// Check children are simple text
|
|
118
|
+
const children = linkNode.getChildren();
|
|
119
|
+
const childrenLength = children.length;
|
|
120
|
+
|
|
121
|
+
for (let i = 0; i < childrenLength; i++) {
|
|
122
|
+
const child = children[i];
|
|
123
|
+
|
|
124
|
+
if (!lexical.$isTextNode(child) || !child.isSimpleText()) {
|
|
125
|
+
replaceWithChildren(linkNode);
|
|
126
|
+
onChange(null, linkNode.getURL());
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
} // Check text content fully matches
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
const text = linkNode.getTextContent();
|
|
133
|
+
const match = findFirstMatch(text, matchers);
|
|
134
|
+
|
|
135
|
+
if (match === null || match.text !== text) {
|
|
136
|
+
replaceWithChildren(linkNode);
|
|
137
|
+
onChange(null, linkNode.getURL());
|
|
138
|
+
return;
|
|
139
|
+
} // Check neighbors
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
if (!isPreviousNodeValid(linkNode) || !isNextNodeValid(linkNode)) {
|
|
143
|
+
replaceWithChildren(linkNode);
|
|
144
|
+
onChange(null, linkNode.getURL());
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const url = linkNode.getURL();
|
|
149
|
+
|
|
150
|
+
if (match !== null && url !== match.url) {
|
|
151
|
+
linkNode.setURL(match.url);
|
|
152
|
+
onChange(match.url, url);
|
|
153
|
+
}
|
|
154
|
+
} // Bad neighbours are edits in neighbor nodes that make AutoLinks incompatible.
|
|
155
|
+
// Given the creation preconditions, these can only be simple text nodes.
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
function handleBadNeighbors(textNode, onChange) {
|
|
159
|
+
const previousSibling = textNode.getPreviousSibling();
|
|
160
|
+
const nextSibling = textNode.getNextSibling();
|
|
161
|
+
const text = textNode.getTextContent();
|
|
162
|
+
|
|
163
|
+
if (AutoLinkNode.$isAutoLinkNode(previousSibling) && !text.startsWith(' ')) {
|
|
164
|
+
replaceWithChildren(previousSibling);
|
|
165
|
+
onChange(null, previousSibling.getURL());
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (AutoLinkNode.$isAutoLinkNode(nextSibling) && !text.endsWith(' ')) {
|
|
169
|
+
replaceWithChildren(nextSibling);
|
|
170
|
+
onChange(null, nextSibling.getURL());
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function replaceWithChildren(node) {
|
|
175
|
+
const children = node.getChildren();
|
|
176
|
+
const childrenLength = children.length;
|
|
177
|
+
|
|
178
|
+
for (let j = childrenLength - 1; j >= 0; j--) {
|
|
179
|
+
node.insertAfter(children[j]);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
node.remove();
|
|
183
|
+
return children.map(child => child.getLatest());
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function useAutoLink(editor, matchers, onChange) {
|
|
187
|
+
react.useEffect(() => {
|
|
188
|
+
if (!editor.hasNodes([AutoLinkNode.AutoLinkNode])) {
|
|
189
|
+
{
|
|
190
|
+
throw Error(`LexicalAutoLinkPlugin: AutoLinkNode, TableCellNode or TableRowNode not registered on editor`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const onChangeWrapped = (...args) => {
|
|
195
|
+
if (onChange) {
|
|
196
|
+
onChange(...args);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
return withSubscriptions(editor.addTransform(lexical.TextNode, textNode => {
|
|
201
|
+
const parent = textNode.getParentOrThrow();
|
|
202
|
+
|
|
203
|
+
if (AutoLinkNode.$isAutoLinkNode(parent)) {
|
|
204
|
+
handleLinkEdit(parent, matchers, onChangeWrapped);
|
|
205
|
+
} else {
|
|
206
|
+
if (textNode.isSimpleText()) {
|
|
207
|
+
handleLinkCreation(textNode, matchers, onChangeWrapped);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
handleBadNeighbors(textNode, onChangeWrapped);
|
|
211
|
+
}
|
|
212
|
+
}), editor.addTransform(AutoLinkNode.AutoLinkNode, linkNode => {
|
|
213
|
+
handleLinkEdit(linkNode, matchers, onChangeWrapped);
|
|
214
|
+
}));
|
|
215
|
+
}, [editor, matchers, onChange]);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function AutoLinkPlugin({
|
|
219
|
+
matchers,
|
|
220
|
+
onChange
|
|
221
|
+
}) {
|
|
222
|
+
const [editor] = LexicalComposerContext.useLexicalComposerContext();
|
|
223
|
+
useAutoLink(editor, matchers, onChange);
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
module.exports = AutoLinkPlugin;
|
package/LexicalAutoLinkPlugin.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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 LexicalAutoLinkPlugin = process.env.NODE_ENV === 'development' ? require('./LexicalAutoLinkPlugin.dev.js') : require('./LexicalAutoLinkPlugin.prod.js')
|
|
9
|
+
module.exports = LexicalAutoLinkPlugin;
|
|
@@ -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
|
+
'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(" ")}
|
|
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
|
+
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};
|
|
@@ -0,0 +1,124 @@
|
|
|
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
|
+
const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
25
|
+
*
|
|
26
|
+
* This source code is licensed under the MIT license found in the
|
|
27
|
+
* LICENSE file in the root directory of this source tree.
|
|
28
|
+
*
|
|
29
|
+
*
|
|
30
|
+
*/
|
|
31
|
+
const useLayoutEffectImpl = CAN_USE_DOM ? react.useLayoutEffect : react.useEffect;
|
|
32
|
+
var useLayoutEffect = useLayoutEffectImpl;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
36
|
+
*
|
|
37
|
+
* This source code is licensed under the MIT license found in the
|
|
38
|
+
* LICENSE file in the root directory of this source tree.
|
|
39
|
+
*
|
|
40
|
+
*
|
|
41
|
+
*/
|
|
42
|
+
const BootstrapPriority = 0;
|
|
43
|
+
|
|
44
|
+
function shouldSelectParagraph(editor) {
|
|
45
|
+
const activeElement = document.activeElement;
|
|
46
|
+
return lexical.$getSelection() !== null || activeElement !== null && activeElement === editor.getRootElement();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function initParagraph(root, editor) {
|
|
50
|
+
const paragraph = lexical.$createParagraphNode();
|
|
51
|
+
root.append(paragraph);
|
|
52
|
+
|
|
53
|
+
if (shouldSelectParagraph(editor)) {
|
|
54
|
+
paragraph.select();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function defaultInitEditor(editor) {
|
|
59
|
+
const root = lexical.$getRoot();
|
|
60
|
+
const firstChild = root.getFirstChild();
|
|
61
|
+
|
|
62
|
+
if (firstChild === null) {
|
|
63
|
+
initParagraph(root, editor);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function defaultClearEditor(editor) {
|
|
68
|
+
const root = lexical.$getRoot();
|
|
69
|
+
root.clear();
|
|
70
|
+
initParagraph(root, editor);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function initEditor(editor, initialPayloadFn) {
|
|
74
|
+
editor.update(() => {
|
|
75
|
+
lexical.$log('initEditor');
|
|
76
|
+
initialPayloadFn(editor);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function clearEditor(editor, clearEditorFn, callbackFn) {
|
|
81
|
+
editor.update(() => {
|
|
82
|
+
lexical.$log('clearEditor');
|
|
83
|
+
clearEditorFn(editor);
|
|
84
|
+
}, {
|
|
85
|
+
onUpdate: callbackFn
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function useBootstrapEditor(editor, initialPayloadFn, clearEditorFn) {
|
|
90
|
+
useLayoutEffect(() => {
|
|
91
|
+
return editor.addListener('command', (type, payload) => {
|
|
92
|
+
if (type === 'bootstrapEditor') {
|
|
93
|
+
initEditor(editor, initialPayloadFn != null ? initialPayloadFn : defaultInitEditor);
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (type === 'clearEditor') {
|
|
98
|
+
clearEditor(editor, clearEditorFn != null ? clearEditorFn : defaultClearEditor);
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return false;
|
|
103
|
+
}, BootstrapPriority);
|
|
104
|
+
}, [clearEditorFn, editor, initialPayloadFn]);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
109
|
+
*
|
|
110
|
+
* This source code is licensed under the MIT license found in the
|
|
111
|
+
* LICENSE file in the root directory of this source tree.
|
|
112
|
+
*
|
|
113
|
+
*
|
|
114
|
+
*/
|
|
115
|
+
function LexicalBootstrapPlugin({
|
|
116
|
+
initialPayloadFn,
|
|
117
|
+
clearEditorFn
|
|
118
|
+
}) {
|
|
119
|
+
const [editor] = LexicalComposerContext.useLexicalComposerContext();
|
|
120
|
+
useBootstrapEditor(editor, initialPayloadFn, clearEditorFn);
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
module.exports = LexicalBootstrapPlugin;
|
|
@@ -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 LexicalBootstrapPlugin = process.env.NODE_ENV === 'development' ? require('./LexicalBootstrapPlugin.dev.js') : require('./LexicalBootstrapPlugin.prod.js')
|
|
9
|
+
module.exports = LexicalBootstrapPlugin;
|
|
@@ -0,0 +1,8 @@
|
|
|
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';var d=require("@lexical/react/LexicalComposerContext"),e=require("lexical"),f=require("react"),h="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement?f.useLayoutEffect:f.useEffect;function k(a,b){const c=e.$createParagraphNode();a.append(c);a=document.activeElement;(null!==e.$getSelection()||null!==a&&a===b.getRootElement())&&c.select()}function l(a){const b=e.$getRoot();null===b.getFirstChild()&&k(b,a)}
|
|
8
|
+
function m(a){const b=e.$getRoot();b.clear();k(b,a)}function n(a,b){a.update(()=>{e.$log("initEditor");b(a)})}function p(a,b,c){a.update(()=>{e.$log("clearEditor");b(a)},{onUpdate:c})}function q(a,b,c){h(()=>a.addListener("command",g=>"bootstrapEditor"===g?(n(a,null!=b?b:l),!0):"clearEditor"===g?(p(a,null!=c?c:m),!0):!1,0),[c,a,b])}module.exports=function({initialPayloadFn:a,clearEditorFn:b}){const [c]=d.useLexicalComposerContext();q(c,a,b);return null};
|