@lexical/react 0.1.9 → 0.1.10
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 +3 -38
- package/DEPRECATED_useLexical.prod.js +1 -2
- package/DEPRECATED_useLexicalAutoFormatter.dev.js +4 -4
- package/DEPRECATED_useLexicalAutoFormatter.prod.js +8 -7
- package/DEPRECATED_useLexicalCharacterLimit.dev.js +2 -2
- package/DEPRECATED_useLexicalCharacterLimit.prod.js +3 -3
- package/DEPRECATED_useLexicalEditor.dev.js +1 -25
- package/DEPRECATED_useLexicalEditor.prod.js +1 -1
- package/DEPRECATED_useLexicalHistory.dev.js +1 -1
- package/DEPRECATED_useLexicalHistory.prod.js +2 -2
- package/DEPRECATED_useLexicalPlainText.dev.js +15 -32
- package/DEPRECATED_useLexicalPlainText.prod.js +14 -15
- package/DEPRECATED_useLexicalRichText.dev.js +57 -209
- package/DEPRECATED_useLexicalRichText.prod.js +26 -29
- package/LexicalAutoFormatterPlugin.dev.js +4 -4
- package/LexicalAutoFormatterPlugin.prod.js +8 -7
- package/LexicalCharacterLimitPlugin.dev.js +2 -2
- package/LexicalCharacterLimitPlugin.prod.js +3 -3
- package/LexicalComposer.dev.js +1 -6
- package/LexicalComposer.prod.js +2 -3
- package/LexicalHistoryPlugin.dev.js +1 -1
- package/LexicalHistoryPlugin.prod.js +2 -2
- package/LexicalPlainTextPlugin.dev.js +12 -8
- package/LexicalPlainTextPlugin.prod.js +7 -7
- package/LexicalRichTextPlugin.dev.js +54 -185
- package/LexicalRichTextPlugin.prod.js +22 -25
- package/LexicalTablePlugin.dev.js +1 -1
- package/LexicalTablePlugin.prod.js +1 -1
- package/LexicalTreeView.dev.js +10 -2
- package/LexicalTreeView.prod.js +9 -8
- package/package.json +5 -5
- package/useLexicalNodeSelection.dev.js +70 -0
- package/useLexicalNodeSelection.js +9 -0
- package/useLexicalNodeSelection.prod.js +8 -0
package/LexicalTreeView.dev.js
CHANGED
|
@@ -181,6 +181,14 @@ function printRangeSelection(selection) {
|
|
|
181
181
|
return res;
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
+
function printObjectSelection(selection) {
|
|
185
|
+
return `: node\n └ [${Array.from(selection._nodes).join(', ')}]`;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function printGridSelection(selection) {
|
|
189
|
+
return `: grid\n └ { grid: ${selection.gridKey}, anchorCell: ${selection.anchorCellKey}, focusCell: ${selection.focusCellKey} }`;
|
|
190
|
+
}
|
|
191
|
+
|
|
184
192
|
function generateContent(editorState) {
|
|
185
193
|
let res = ' root\n';
|
|
186
194
|
const selectionString = editorState.read(() => {
|
|
@@ -200,7 +208,7 @@ function generateContent(editorState) {
|
|
|
200
208
|
typeDisplay
|
|
201
209
|
});
|
|
202
210
|
});
|
|
203
|
-
return selection === null ? ': null' : printRangeSelection(selection);
|
|
211
|
+
return selection === null ? ': null' : lexical.$isRangeSelection(selection) ? printRangeSelection(selection) : lexical.$isGridSelection(selection) ? printGridSelection(selection) : printObjectSelection(selection);
|
|
204
212
|
});
|
|
205
213
|
return res + '\n selection' + selectionString;
|
|
206
214
|
}
|
|
@@ -279,7 +287,7 @@ function printSelectedCharsLine({
|
|
|
279
287
|
typeDisplay
|
|
280
288
|
}) {
|
|
281
289
|
// No selection or node is not selected.
|
|
282
|
-
if (!lexical.$isTextNode(node) || selection
|
|
290
|
+
if (!lexical.$isTextNode(node) || !lexical.$isRangeSelection(selection) || !isSelected || lexical.$isElementNode(node)) {
|
|
283
291
|
return '';
|
|
284
292
|
} // No selected characters.
|
|
285
293
|
|
package/LexicalTreeView.prod.js
CHANGED
|
@@ -4,16 +4,17 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
var k=require("lexical"),q=require("react");const
|
|
7
|
+
var k=require("lexical"),q=require("react");const t=Object.freeze({"\t":"\\t","\n":"\\n"}),D=new RegExp(Object.keys(t).join("|"),"g"),E=Object.freeze({ancestorHasNextSibling:"|",ancestorIsLastChild:" ",hasNextSibling:"\u251c",isLastChild:"\u2514",selectedChar:"^",selectedLine:">"});
|
|
8
8
|
function F(a){let b="";const c=G(a),e=a.anchor;a=a.focus;const d=e.offset,f=a.offset;b=b+`: range ${""!==c?`{ ${c} }`:""}`+`\n \u251c anchor { key: ${e.key}, offset: ${null===d?"null":d}, type: ${e.type} }`;return b+=`\n \u2514 focus { key: ${a.key}, offset: ${null===f?"null":f}, type: ${a.type} }`}
|
|
9
|
-
function H(a){let b=" root\n";a=a.read(()=>{const c=k.$getSelection();J(k.$getRoot(),(e,d)=>{const f=`(${e.getKey()})`,g=e.getType()||"",m=e.isSelected();var
|
|
10
|
-
typeDisplay:g})});return null===c?": null":F(c)
|
|
9
|
+
function H(a){let b=" root\n";a=a.read(()=>{const c=k.$getSelection();J(k.$getRoot(),(e,d)=>{const f=`(${e.getKey()})`,g=e.getType()||"",m=e.isSelected();var v=b,u=m?E.selectedLine:" ",r=d.join(" ");if(k.$isTextNode(e)){var l=e.getTextContent(!0);const n=0===l.length?"(empty)":`"${K(l)}"`;l=[G(e),L(e),M(e)].filter(Boolean).join(", ");l=[n,0!==l.length?`{ ${l} }`:null].filter(Boolean).join(" ").trim()}else l="";b=v+`${u} ${r} ${f} ${g} ${l}\n`;b+=N({indent:d,isSelected:m,node:e,nodeKeyDisplay:f,selection:c,
|
|
10
|
+
typeDisplay:g})});return null===c?": null":k.$isRangeSelection(c)?F(c):k.$isGridSelection(c)?`: grid\n \u2514 { grid: ${c.gridKey}, anchorCell: ${c.anchorCellKey}, focusCell: ${c.focusCellKey} }`:`: node\n \u2514 [${Array.from(c._nodes).join(", ")}]`});return b+"\n selection"+a}
|
|
11
|
+
function J(a,b,c=[]){a=a.getChildren();const e=a.length;a.forEach((d,f)=>{b(d,c.concat(f===e-1?E.isLastChild:E.hasNextSibling));k.$isElementNode(d)&&J(d,b,c.concat(f===e-1?E.ancestorIsLastChild:E.ancestorHasNextSibling))})}function K(a){return Object.entries(t).reduce((b,[c,e])=>b.replace(new RegExp(c,"g"),String(e)),a)}
|
|
11
12
|
const O=[a=>a.hasFormat("bold")&&"Bold",a=>a.hasFormat("code")&&"Code",a=>a.hasFormat("italic")&&"Italic",a=>a.hasFormat("strikethrough")&&"Strikethrough",a=>a.hasFormat("underline")&&"Underline"],P=[a=>a.isDirectionless()&&"Directionless",a=>a.isUnmergeable()&&"Unmergeable"],Q=[a=>a.isToken()&&"Token",a=>a.isSegmented()&&"Segmented",a=>a.isInert()&&"Inert"];function L(a){let b=P.map(c=>c(a)).filter(Boolean).join(", ").toLocaleLowerCase();""!==b&&(b="detail: "+b);return b}
|
|
12
13
|
function M(a){let b=Q.map(c=>c(a)).filter(Boolean).join(", ").toLocaleLowerCase();""!==b&&(b="mode: "+b);return b}function G(a){let b=O.map(c=>c(a)).filter(Boolean).join(", ").toLocaleLowerCase();""!==b&&(b="format: "+b);return b}
|
|
13
|
-
function N({indent:a,isSelected:b,node:c,nodeKeyDisplay:e,selection:d,typeDisplay:f}){if(!k.$isTextNode(c)
|
|
14
|
-
c===n?[d,b]=n.isBefore(
|
|
14
|
+
function N({indent:a,isSelected:b,node:c,nodeKeyDisplay:e,selection:d,typeDisplay:f}){if(!k.$isTextNode(c)||!k.$isRangeSelection(d)||!b||k.$isElementNode(c))return"";b=d.anchor;var g=d.focus;if(""===c.getTextContent()||b.getNode()===d.focus.getNode()&&b.offset===g.offset)return"";g=d.anchor;const m=d.focus,v=c.getTextContent(!0),u=v.length;b=d=-1;if("text"===g.type&&"text"===m.type){const n=g.getNode(),w=m.getNode();n===w&&c===n&&g.offset!==m.offset?[d,b]=g.offset<m.offset?[g.offset,m.offset]:[m.offset,
|
|
15
|
+
g.offset]:c===n?[d,b]=n.isBefore(w)?[g.offset,u]:[0,g.offset]:c===w?[d,b]=w.isBefore(n)?[m.offset,u]:[0,m.offset]:[d,b]=[0,u]}c=(v.slice(0,d).match(D)||[]).length;g=(v.slice(d,b).match(D)||[]).length;const [r,l]=[d+c,b+c+g];if(r===l)return"";c=a[a.length-1]===E.hasNextSibling?E.ancestorHasNextSibling:E.ancestorIsLastChild;a=[...a.slice(0,a.length-1),c];c=Array(r).fill(" ");d=Array(l-r).fill(E.selectedChar);e=Array(e.length+(f.length+3)).fill(" ");return[E.selectedLine,a.join(" "),[...e,...c,...d].join("")].join(" ")+
|
|
15
16
|
"\n"}
|
|
16
|
-
module.exports=function({timeTravelButtonClassName:a,timeTravelPanelSliderClassName:b,timeTravelPanelButtonClassName:c,viewClassName:e,timeTravelPanelClassName:d,editor:f}){const [g,m]=q.useState([]),[u
|
|
17
|
-
m(A=>[...A,[Date.now(),h]])})},[r,f]);const y=g.length;q.useEffect(()=>{if(z){let h;const p=()=>{const x=n.current;x===y-1?C(!1):h=setTimeout(()=>{n.current++;const A=n.current,I=B.current;null!==I&&(I.value=String(A));f.setEditorState(g[A][1]);p()},g[x+1][0]-g[x][0])};p();return()=>{window.clearTimeout(h)}}},[g,z,f,y]);q.useEffect(()=>{const h=
|
|
18
|
-
{onClick:()=>{const h=f.getRootElement();null!==h&&(h.contentEditable="false",n.current=y-1,l(!0))},className:a},"Time Travel"),q.createElement("pre",{ref:
|
|
17
|
+
module.exports=function({timeTravelButtonClassName:a,timeTravelPanelSliderClassName:b,timeTravelPanelButtonClassName:c,viewClassName:e,timeTravelPanelClassName:d,editor:f}){const [g,m]=q.useState([]),[v,u]=q.useState(""),[r,l]=q.useState(!1),n=q.useRef(0),w=q.useRef(null),B=q.useRef(null),[z,C]=q.useState(!1);q.useEffect(()=>{u(H(f.getEditorState()));return f.addListener("update",({editorState:h})=>{const p=f._compositionKey,x=H(f.getEditorState());u([x,null!==p&&`Composition key: ${p}`].filter(Boolean).join("\n\n"));r||
|
|
18
|
+
m(A=>[...A,[Date.now(),h]])})},[r,f]);const y=g.length;q.useEffect(()=>{if(z){let h;const p=()=>{const x=n.current;x===y-1?C(!1):h=setTimeout(()=>{n.current++;const A=n.current,I=B.current;null!==I&&(I.value=String(A));f.setEditorState(g[A][1]);p()},g[x+1][0]-g[x][0])};p();return()=>{window.clearTimeout(h)}}},[g,z,f,y]);q.useEffect(()=>{const h=w.current;if(null!==h)return h.__lexicalEditor=f,()=>{h.__lexicalEditor=null}},[f]);return q.createElement("div",{className:e},!r&&2<y&&q.createElement("button",
|
|
19
|
+
{onClick:()=>{const h=f.getRootElement();null!==h&&(h.contentEditable="false",n.current=y-1,l(!0))},className:a},"Time Travel"),q.createElement("pre",{ref:w},v),r&&q.createElement("div",{className:d},q.createElement("button",{className:c,onClick:()=>{C(!z)}},z?"Pause":"Play"),q.createElement("input",{className:b,ref:B,onChange:h=>{h=Number(h.target.value);const p=g[h];p&&(n.current=h,f.setEditorState(p[1]))},type:"range",min:"1",max:y-1}),q.createElement("button",{className:c,onClick:()=>{var h=f.getRootElement();
|
|
19
20
|
if(null!==h){h.contentEditable="true";h=g.length-1;f.setEditorState(g[h][1]);const p=B.current;null!==p&&(p.value=String(h));l(!1);C(!1)}}},"Exit")))};
|
package/package.json
CHANGED
|
@@ -12,12 +12,12 @@
|
|
|
12
12
|
"rich-text"
|
|
13
13
|
],
|
|
14
14
|
"license": "MIT",
|
|
15
|
-
"version": "0.1.
|
|
15
|
+
"version": "0.1.10",
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"lexical": "0.1.
|
|
18
|
-
"@lexical/helpers": "0.1.
|
|
19
|
-
"@lexical/table": "0.1.
|
|
20
|
-
"@lexical/yjs": "0.1.
|
|
17
|
+
"lexical": "0.1.10",
|
|
18
|
+
"@lexical/helpers": "0.1.10",
|
|
19
|
+
"@lexical/table": "0.1.10",
|
|
20
|
+
"@lexical/yjs": "0.1.10",
|
|
21
21
|
"react": ">=17.x",
|
|
22
22
|
"react-dom": ">=17.x"
|
|
23
23
|
},
|
|
@@ -0,0 +1,70 @@
|
|
|
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
|
+
|
|
22
|
+
function isNodeSelected(editor, key) {
|
|
23
|
+
return editor.getEditorState().read(() => {
|
|
24
|
+
const node = lexical.$getNodeByKey(key);
|
|
25
|
+
|
|
26
|
+
if (node === null) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return node.isSelected();
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function useLexicalNodeSelection(key) {
|
|
35
|
+
const [editor] = LexicalComposerContext.useLexicalComposerContext();
|
|
36
|
+
const [isSelected, setIsSelected] = react.useState(() => isNodeSelected(editor, key));
|
|
37
|
+
react.useEffect(() => {
|
|
38
|
+
return editor.addListener('update', () => {
|
|
39
|
+
setIsSelected(isNodeSelected(editor, key));
|
|
40
|
+
});
|
|
41
|
+
}, [editor, key]);
|
|
42
|
+
const setSelected = react.useCallback(selected => {
|
|
43
|
+
editor.update(() => {
|
|
44
|
+
let selection = lexical.$getSelection();
|
|
45
|
+
|
|
46
|
+
if (!lexical.$isNodeSelection(selection)) {
|
|
47
|
+
selection = lexical.$createNodeSelection();
|
|
48
|
+
lexical.$setSelection(selection);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (selected) {
|
|
52
|
+
selection.add(key);
|
|
53
|
+
} else {
|
|
54
|
+
selection.delete(key);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}, [editor, key]);
|
|
58
|
+
const clearSelected = react.useCallback(() => {
|
|
59
|
+
editor.update(() => {
|
|
60
|
+
const selection = lexical.$getSelection();
|
|
61
|
+
|
|
62
|
+
if (lexical.$isNodeSelection(selection)) {
|
|
63
|
+
selection.clear();
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}, [editor]);
|
|
67
|
+
return [isSelected, setSelected, clearSelected];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
module.exports = useLexicalNodeSelection;
|
|
@@ -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 useLexicalNodeSelection = process.env.NODE_ENV === 'development' ? require('./useLexicalNodeSelection.dev.js') : require('./useLexicalNodeSelection.prod.js')
|
|
9
|
+
module.exports = useLexicalNodeSelection;
|
|
@@ -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
|
+
var c=require("@lexical/react/LexicalComposerContext"),e=require("lexical"),h=require("react");function k(b,a){return b.getEditorState().read(()=>{const f=e.$getNodeByKey(a);return null===f?!1:f.isSelected()})}
|
|
8
|
+
module.exports=function(b){const [a]=c.useLexicalComposerContext(),[f,l]=h.useState(()=>k(a,b));h.useEffect(()=>a.addListener("update",()=>{l(k(a,b))}),[a,b]);const m=h.useCallback(g=>{a.update(()=>{let d=e.$getSelection();e.$isNodeSelection(d)||(d=e.$createNodeSelection(),e.$setSelection(d));g?d.add(b):d.delete(b)})},[a,b]),n=h.useCallback(()=>{a.update(()=>{const g=e.$getSelection();e.$isNodeSelection(g)&&g.clear()})},[a]);return[f,m,n]};
|