@lexical/react 0.8.0 → 0.9.0
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/LexicalAutoEmbedPlugin.d.ts +1 -0
- package/LexicalAutoEmbedPlugin.dev.js +11 -0
- package/LexicalAutoFocusPlugin.dev.js +1 -0
- package/LexicalAutoLinkPlugin.d.ts +1 -0
- package/LexicalAutoLinkPlugin.dev.js +57 -11
- package/LexicalAutoLinkPlugin.prod.js +7 -6
- package/LexicalBlockWithAlignableContents.dev.js +13 -2
- package/LexicalBlockWithAlignableContents.prod.js +4 -4
- package/LexicalCharacterLimitPlugin.d.ts +1 -0
- package/LexicalCharacterLimitPlugin.dev.js +46 -7
- package/LexicalCheckListPlugin.dev.js +50 -12
- package/LexicalCheckListPlugin.prod.js +1 -1
- package/LexicalClearEditorPlugin.d.ts +1 -0
- package/LexicalClearEditorPlugin.dev.js +1 -1
- package/LexicalCollaborationContext.dev.js +3 -0
- package/LexicalCollaborationPlugin.d.ts +3 -2
- package/LexicalCollaborationPlugin.dev.js +35 -6
- package/LexicalComposer.d.ts +1 -0
- package/LexicalComposer.dev.js +10 -6
- package/LexicalComposerContext.dev.js +6 -0
- package/LexicalContentEditable.dev.js +0 -1
- package/LexicalContentEditable.js.flow +1 -0
- package/LexicalDecoratorBlockNode.d.ts +1 -0
- package/LexicalDecoratorBlockNode.dev.js +5 -0
- package/LexicalErrorBoundary.d.ts +1 -0
- package/LexicalHashtagPlugin.d.ts +1 -0
- package/LexicalHashtagPlugin.dev.js +43 -73
- package/LexicalHorizontalRuleNode.d.ts +1 -0
- package/LexicalHorizontalRuleNode.dev.js +22 -0
- package/LexicalHorizontalRulePlugin.dev.js +4 -0
- package/LexicalLinkPlugin.dev.js +10 -4
- package/LexicalListPlugin.dev.js +2 -0
- package/LexicalMarkdownShortcutPlugin.dev.js +2 -2
- package/LexicalNestedComposer.dev.js +12 -6
- package/LexicalNestedComposer.prod.js +2 -2
- package/LexicalNodeEventPlugin.dev.js +3 -5
- package/LexicalOnChangePlugin.dev.js +1 -1
- package/LexicalPlainTextPlugin.d.ts +1 -0
- package/LexicalPlainTextPlugin.dev.js +12 -8
- package/LexicalRichTextPlugin.d.ts +1 -0
- package/LexicalRichTextPlugin.dev.js +12 -8
- package/LexicalTabIndentationPlugin.dev.js +3 -1
- package/{LexicalTableOfContents__EXPERIMENTAL.d.ts → LexicalTableOfContents.d.ts} +1 -0
- package/{LexicalTableOfContents__EXPERIMENTAL.dev.js → LexicalTableOfContents.dev.js} +33 -5
- package/{DEPRECATED_useLexical.js → LexicalTableOfContents.js} +2 -2
- package/LexicalTablePlugin.d.ts +1 -0
- package/LexicalTablePlugin.dev.js +19 -5
- package/LexicalTreeView.d.ts +1 -0
- package/LexicalTreeView.dev.js +113 -21
- package/LexicalTreeView.prod.js +16 -15
- package/LexicalTypeaheadMenuPlugin.dev.js +123 -17
- package/LexicalTypeaheadMenuPlugin.prod.js +18 -18
- package/package.json +19 -19
- package/shared/useYjsCollaboration.d.ts +3 -4
- package/useLexicalEditable.dev.js +5 -1
- package/useLexicalIsTextContentEmpty.dev.js +0 -1
- package/useLexicalNodeSelection.dev.js +7 -0
- package/useLexicalSubscription.dev.js +3 -1
- package/DEPRECATED_useLexical.d.ts +0 -18
- package/DEPRECATED_useLexical.dev.js +0 -104
- package/DEPRECATED_useLexical.js.flow +0 -25
- package/DEPRECATED_useLexical.prod.js +0 -8
- package/DEPRECATED_useLexicalCanShowPlaceholder.d.ts +0 -9
- package/DEPRECATED_useLexicalCanShowPlaceholder.dev.js +0 -72
- package/DEPRECATED_useLexicalCanShowPlaceholder.js +0 -9
- package/DEPRECATED_useLexicalCanShowPlaceholder.js.flow +0 -15
- package/DEPRECATED_useLexicalCanShowPlaceholder.prod.js +0 -8
- package/DEPRECATED_useLexicalCharacterLimit.d.ts +0 -8
- package/DEPRECATED_useLexicalCharacterLimit.dev.js +0 -213
- package/DEPRECATED_useLexicalCharacterLimit.js +0 -9
- package/DEPRECATED_useLexicalCharacterLimit.js.flow +0 -31
- package/DEPRECATED_useLexicalCharacterLimit.prod.js +0 -13
- package/DEPRECATED_useLexicalEditor.d.ts +0 -9
- package/DEPRECATED_useLexicalEditor.dev.js +0 -87
- package/DEPRECATED_useLexicalEditor.js +0 -9
- package/DEPRECATED_useLexicalEditor.prod.js +0 -8
- package/DEPRECATED_useLexicalHistory.d.ts +0 -12
- package/DEPRECATED_useLexicalHistory.dev.js +0 -38
- package/DEPRECATED_useLexicalHistory.js +0 -9
- package/DEPRECATED_useLexicalHistory.js.flow +0 -34
- package/DEPRECATED_useLexicalHistory.prod.js +0 -7
- package/DEPRECATED_useLexicalPlainText.d.ts +0 -10
- package/DEPRECATED_useLexicalPlainText.dev.js +0 -88
- package/DEPRECATED_useLexicalPlainText.js +0 -9
- package/DEPRECATED_useLexicalPlainText.js.flow +0 -17
- package/DEPRECATED_useLexicalPlainText.prod.js +0 -8
- package/DEPRECATED_useLexicalRichText.d.ts +0 -10
- package/DEPRECATED_useLexicalRichText.dev.js +0 -88
- package/DEPRECATED_useLexicalRichText.js +0 -9
- package/DEPRECATED_useLexicalRichText.js.flow +0 -17
- package/DEPRECATED_useLexicalRichText.prod.js +0 -8
- package/LexicalTableOfContents__EXPERIMENTAL.js +0 -9
- /package/{LexicalTableOfContents__EXPERIMENTAL.js.flow → LexicalTableOfContents.js.flow} +0 -0
- /package/{LexicalTableOfContents__EXPERIMENTAL.prod.js → LexicalTableOfContents.prod.js} +0 -0
package/LexicalTreeView.dev.js
CHANGED
|
@@ -50,19 +50,22 @@ function TreeView({
|
|
|
50
50
|
const [isLimited, setIsLimited] = React.useState(false);
|
|
51
51
|
const [showLimited, setShowLimited] = React.useState(false);
|
|
52
52
|
const lastEditorStateRef = React.useRef(null);
|
|
53
|
+
const commandsLog = useLexicalCommandsLog(editor);
|
|
53
54
|
const generateTree = React.useCallback(editorState => {
|
|
54
|
-
const treeText = generateContent(editor.getEditorState(), editor._config, editor._compositionKey, editor._editable);
|
|
55
|
+
const treeText = generateContent(editor.getEditorState(), editor._config, commandsLog, editor._compositionKey, editor._editable);
|
|
55
56
|
setContent(treeText);
|
|
57
|
+
|
|
56
58
|
if (!timeTravelEnabled) {
|
|
57
59
|
setTimeStampedEditorStates(currentEditorStates => [...currentEditorStates, [Date.now(), editorState]]);
|
|
58
60
|
}
|
|
59
|
-
}, [editor, timeTravelEnabled]);
|
|
61
|
+
}, [commandsLog, editor, timeTravelEnabled]);
|
|
60
62
|
React.useEffect(() => {
|
|
61
63
|
const editorState = editor.getEditorState();
|
|
64
|
+
|
|
62
65
|
if (!showLimited && editorState._nodeMap.size > 1000) {
|
|
63
|
-
setContent(generateContent(editorState, editor._config, editor._compositionKey, editor._editable));
|
|
66
|
+
setContent(generateContent(editorState, editor._config, commandsLog, editor._compositionKey, editor._editable));
|
|
64
67
|
}
|
|
65
|
-
}, [editor, showLimited]);
|
|
68
|
+
}, [commandsLog, editor, showLimited]);
|
|
66
69
|
React.useEffect(() => {
|
|
67
70
|
return utils.mergeRegister(editor.registerUpdateListener(({
|
|
68
71
|
editorState
|
|
@@ -70,26 +73,31 @@ function TreeView({
|
|
|
70
73
|
if (!showLimited && editorState._nodeMap.size > 1000) {
|
|
71
74
|
lastEditorStateRef.current = editorState;
|
|
72
75
|
setIsLimited(true);
|
|
76
|
+
|
|
73
77
|
if (!showLimited) {
|
|
74
78
|
return;
|
|
75
79
|
}
|
|
76
80
|
}
|
|
81
|
+
|
|
77
82
|
generateTree(editorState);
|
|
78
83
|
}), editor.registerEditableListener(() => {
|
|
79
|
-
const treeText = generateContent(editor.getEditorState(), editor._config, editor._compositionKey, editor._editable);
|
|
84
|
+
const treeText = generateContent(editor.getEditorState(), editor._config, commandsLog, editor._compositionKey, editor._editable);
|
|
80
85
|
setContent(treeText);
|
|
81
86
|
}));
|
|
82
|
-
}, [editor, isLimited, generateTree, showLimited]);
|
|
87
|
+
}, [commandsLog, editor, isLimited, generateTree, showLimited]);
|
|
83
88
|
const totalEditorStates = timeStampedEditorStates.length;
|
|
84
89
|
React.useEffect(() => {
|
|
85
90
|
if (isPlaying) {
|
|
86
91
|
let timeoutId;
|
|
92
|
+
|
|
87
93
|
const play = () => {
|
|
88
94
|
const currentIndex = playingIndexRef.current;
|
|
95
|
+
|
|
89
96
|
if (currentIndex === totalEditorStates - 1) {
|
|
90
97
|
setIsPlaying(false);
|
|
91
98
|
return;
|
|
92
99
|
}
|
|
100
|
+
|
|
93
101
|
const currentTime = timeStampedEditorStates[currentIndex][0];
|
|
94
102
|
const nextTime = timeStampedEditorStates[currentIndex + 1][0];
|
|
95
103
|
const timeDiff = nextTime - currentTime;
|
|
@@ -97,13 +105,16 @@ function TreeView({
|
|
|
97
105
|
playingIndexRef.current++;
|
|
98
106
|
const index = playingIndexRef.current;
|
|
99
107
|
const input = inputRef.current;
|
|
108
|
+
|
|
100
109
|
if (input !== null) {
|
|
101
110
|
input.value = String(index);
|
|
102
111
|
}
|
|
112
|
+
|
|
103
113
|
editor.setEditorState(timeStampedEditorStates[index][1]);
|
|
104
114
|
play();
|
|
105
115
|
}, timeDiff);
|
|
106
116
|
};
|
|
117
|
+
|
|
107
118
|
play();
|
|
108
119
|
return () => {
|
|
109
120
|
clearTimeout(timeoutId);
|
|
@@ -112,6 +123,7 @@ function TreeView({
|
|
|
112
123
|
}, [timeStampedEditorStates, isPlaying, editor, totalEditorStates]);
|
|
113
124
|
React.useEffect(() => {
|
|
114
125
|
const element = treeElementRef.current;
|
|
126
|
+
|
|
115
127
|
if (element !== null) {
|
|
116
128
|
// @ts-ignore Internal field
|
|
117
129
|
element.__lexicalEditor = editor;
|
|
@@ -135,6 +147,7 @@ function TreeView({
|
|
|
135
147
|
onClick: () => {
|
|
136
148
|
setShowLimited(true);
|
|
137
149
|
const editorState = lastEditorStateRef.current;
|
|
150
|
+
|
|
138
151
|
if (editorState !== null) {
|
|
139
152
|
lastEditorStateRef.current = null;
|
|
140
153
|
generateTree(editorState);
|
|
@@ -150,6 +163,7 @@ function TreeView({
|
|
|
150
163
|
}, "Show full tree")) : null, !timeTravelEnabled && (showLimited || !isLimited) && totalEditorStates > 2 && /*#__PURE__*/React.createElement("button", {
|
|
151
164
|
onClick: () => {
|
|
152
165
|
const rootElement = editor.getRootElement();
|
|
166
|
+
|
|
153
167
|
if (rootElement !== null) {
|
|
154
168
|
rootElement.contentEditable = 'false';
|
|
155
169
|
playingIndexRef.current = totalEditorStates - 1;
|
|
@@ -168,6 +182,7 @@ function TreeView({
|
|
|
168
182
|
if (playingIndexRef.current === totalEditorStates - 1) {
|
|
169
183
|
playingIndexRef.current = 1;
|
|
170
184
|
}
|
|
185
|
+
|
|
171
186
|
setIsPlaying(!isPlaying);
|
|
172
187
|
},
|
|
173
188
|
type: "button"
|
|
@@ -177,6 +192,7 @@ function TreeView({
|
|
|
177
192
|
onChange: event => {
|
|
178
193
|
const editorStateIndex = Number(event.target.value);
|
|
179
194
|
const timeStampedEditorState = timeStampedEditorStates[editorStateIndex];
|
|
195
|
+
|
|
180
196
|
if (timeStampedEditorState) {
|
|
181
197
|
playingIndexRef.current = editorStateIndex;
|
|
182
198
|
editor.setEditorState(timeStampedEditorState[1]);
|
|
@@ -189,15 +205,18 @@ function TreeView({
|
|
|
189
205
|
className: timeTravelPanelButtonClassName,
|
|
190
206
|
onClick: () => {
|
|
191
207
|
const rootElement = editor.getRootElement();
|
|
208
|
+
|
|
192
209
|
if (rootElement !== null) {
|
|
193
210
|
rootElement.contentEditable = 'true';
|
|
194
211
|
const index = timeStampedEditorStates.length - 1;
|
|
195
212
|
const timeStampedEditorState = timeStampedEditorStates[index];
|
|
196
213
|
editor.setEditorState(timeStampedEditorState[1]);
|
|
197
214
|
const input = inputRef.current;
|
|
215
|
+
|
|
198
216
|
if (input !== null) {
|
|
199
217
|
input.value = String(index);
|
|
200
218
|
}
|
|
219
|
+
|
|
201
220
|
setTimeTravelEnabled(false);
|
|
202
221
|
setIsPlaying(false);
|
|
203
222
|
}
|
|
@@ -205,6 +224,36 @@ function TreeView({
|
|
|
205
224
|
type: "button"
|
|
206
225
|
}, "Exit")));
|
|
207
226
|
}
|
|
227
|
+
|
|
228
|
+
function useLexicalCommandsLog(editor) {
|
|
229
|
+
const [loggedCommands, setLoggedCommands] = React.useState([]);
|
|
230
|
+
React.useEffect(() => {
|
|
231
|
+
const unregisterCommandListeners = new Set();
|
|
232
|
+
|
|
233
|
+
for (const [command] of editor._commands) {
|
|
234
|
+
unregisterCommandListeners.add(editor.registerCommand(command, payload => {
|
|
235
|
+
setLoggedCommands(state => {
|
|
236
|
+
const newState = [...state];
|
|
237
|
+
newState.push({
|
|
238
|
+
payload,
|
|
239
|
+
type: command.type ? command.type : 'UNKNOWN'
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
if (newState.length > 10) {
|
|
243
|
+
newState.shift();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return newState;
|
|
247
|
+
});
|
|
248
|
+
return false;
|
|
249
|
+
}, lexical.COMMAND_PRIORITY_HIGH));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return () => unregisterCommandListeners.forEach(unregister => unregister());
|
|
253
|
+
}, [editor]);
|
|
254
|
+
return React.useMemo(() => loggedCommands, [loggedCommands]);
|
|
255
|
+
}
|
|
256
|
+
|
|
208
257
|
function printRangeSelection(selection) {
|
|
209
258
|
let res = '';
|
|
210
259
|
const formatText = printFormatProperties(selection);
|
|
@@ -217,13 +266,16 @@ function printRangeSelection(selection) {
|
|
|
217
266
|
res += `\n └ focus { key: ${focus.key}, offset: ${focusOffset === null ? 'null' : focusOffset}, type: ${focus.type} }`;
|
|
218
267
|
return res;
|
|
219
268
|
}
|
|
220
|
-
|
|
269
|
+
|
|
270
|
+
function printNodeSelection(selection) {
|
|
221
271
|
return `: node\n └ [${Array.from(selection._nodes).join(', ')}]`;
|
|
222
272
|
}
|
|
273
|
+
|
|
223
274
|
function printGridSelection(selection) {
|
|
224
275
|
return `: grid\n └ { grid: ${selection.gridKey}, anchorCell: ${selection.anchor.key}, focusCell: ${selection.focus.key} }`;
|
|
225
276
|
}
|
|
226
|
-
|
|
277
|
+
|
|
278
|
+
function generateContent(editorState, editorConfig, commandsLog, compositionKey, editable) {
|
|
227
279
|
let res = ' root\n';
|
|
228
280
|
const selectionString = editorState.read(() => {
|
|
229
281
|
const selection = lexical.$getSelection();
|
|
@@ -243,32 +295,50 @@ function generateContent(editorState, editorConfig, compositionKey, editable) {
|
|
|
243
295
|
typeDisplay
|
|
244
296
|
});
|
|
245
297
|
});
|
|
246
|
-
return selection === null ? ': null' : lexical.$isRangeSelection(selection) ? printRangeSelection(selection) : lexical.DEPRECATED_$isGridSelection(selection) ? printGridSelection(selection) :
|
|
298
|
+
return selection === null ? ': null' : lexical.$isRangeSelection(selection) ? printRangeSelection(selection) : lexical.DEPRECATED_$isGridSelection(selection) ? printGridSelection(selection) : printNodeSelection(selection);
|
|
247
299
|
});
|
|
248
300
|
res += '\n selection' + selectionString;
|
|
301
|
+
res += '\n\n commands:';
|
|
302
|
+
|
|
303
|
+
if (commandsLog.length) {
|
|
304
|
+
for (const {
|
|
305
|
+
type,
|
|
306
|
+
payload
|
|
307
|
+
} of commandsLog) {
|
|
308
|
+
res += `\n └ { type: ${type}, payload: ${payload instanceof Event ? payload.constructor.name : payload} }`;
|
|
309
|
+
}
|
|
310
|
+
} else {
|
|
311
|
+
res += '\n └ None dispatched.';
|
|
312
|
+
}
|
|
313
|
+
|
|
249
314
|
res += '\n\n editor:';
|
|
250
315
|
res += `\n └ namespace ${editorConfig.namespace}`;
|
|
316
|
+
|
|
251
317
|
if (compositionKey !== null) {
|
|
252
318
|
res += `\n └ compositionKey ${compositionKey}`;
|
|
253
319
|
}
|
|
320
|
+
|
|
254
321
|
res += `\n └ editable ${String(editable)}`;
|
|
255
322
|
return res;
|
|
256
323
|
}
|
|
324
|
+
|
|
257
325
|
function visitTree(currentNode, visitor, indent = []) {
|
|
258
326
|
const childNodes = currentNode.getChildren();
|
|
259
327
|
const childNodesLength = childNodes.length;
|
|
260
328
|
childNodes.forEach((childNode, i) => {
|
|
261
329
|
visitor(childNode, indent.concat(i === childNodesLength - 1 ? SYMBOLS.isLastChild : SYMBOLS.hasNextSibling));
|
|
330
|
+
|
|
262
331
|
if (lexical.$isElementNode(childNode)) {
|
|
263
332
|
visitTree(childNode, visitor, indent.concat(i === childNodesLength - 1 ? SYMBOLS.ancestorIsLastChild : SYMBOLS.ancestorHasNextSibling));
|
|
264
333
|
}
|
|
265
334
|
});
|
|
266
335
|
}
|
|
336
|
+
|
|
267
337
|
function normalize(text) {
|
|
268
338
|
return Object.entries(NON_SINGLE_WIDTH_CHARS_REPLACEMENT).reduce((acc, [key, value]) => acc.replace(new RegExp(key, 'g'), String(value)), text);
|
|
269
|
-
}
|
|
339
|
+
} // TODO Pass via props to allow customizability
|
|
340
|
+
|
|
270
341
|
|
|
271
|
-
// TODO Pass via props to allow customizability
|
|
272
342
|
function printNode(node) {
|
|
273
343
|
if (lexical.$isTextNode(node)) {
|
|
274
344
|
const text = node.getTextContent();
|
|
@@ -284,52 +354,69 @@ function printNode(node) {
|
|
|
284
354
|
return '';
|
|
285
355
|
}
|
|
286
356
|
}
|
|
357
|
+
|
|
287
358
|
const FORMAT_PREDICATES = [node => node.hasFormat('bold') && 'Bold', node => node.hasFormat('code') && 'Code', node => node.hasFormat('italic') && 'Italic', node => node.hasFormat('strikethrough') && 'Strikethrough', node => node.hasFormat('subscript') && 'Subscript', node => node.hasFormat('superscript') && 'Superscript', node => node.hasFormat('underline') && 'Underline'];
|
|
288
359
|
const DETAIL_PREDICATES = [node => node.isDirectionless() && 'Directionless', node => node.isUnmergeable() && 'Unmergeable'];
|
|
289
360
|
const MODE_PREDICATES = [node => node.isToken() && 'Token', node => node.isSegmented() && 'Segmented'];
|
|
361
|
+
|
|
290
362
|
function printAllTextNodeProperties(node) {
|
|
291
363
|
return [printFormatProperties(node), printDetailProperties(node), printModeProperties(node)].filter(Boolean).join(', ');
|
|
292
364
|
}
|
|
365
|
+
|
|
293
366
|
function printAllLinkNodeProperties(node) {
|
|
294
367
|
return [printTargetProperties(node), printRelProperties(node)].filter(Boolean).join(', ');
|
|
295
368
|
}
|
|
369
|
+
|
|
296
370
|
function printDetailProperties(nodeOrSelection) {
|
|
297
371
|
let str = DETAIL_PREDICATES.map(predicate => predicate(nodeOrSelection)).filter(Boolean).join(', ').toLocaleLowerCase();
|
|
372
|
+
|
|
298
373
|
if (str !== '') {
|
|
299
374
|
str = 'detail: ' + str;
|
|
300
375
|
}
|
|
376
|
+
|
|
301
377
|
return str;
|
|
302
378
|
}
|
|
379
|
+
|
|
303
380
|
function printModeProperties(nodeOrSelection) {
|
|
304
381
|
let str = MODE_PREDICATES.map(predicate => predicate(nodeOrSelection)).filter(Boolean).join(', ').toLocaleLowerCase();
|
|
382
|
+
|
|
305
383
|
if (str !== '') {
|
|
306
384
|
str = 'mode: ' + str;
|
|
307
385
|
}
|
|
386
|
+
|
|
308
387
|
return str;
|
|
309
388
|
}
|
|
389
|
+
|
|
310
390
|
function printFormatProperties(nodeOrSelection) {
|
|
311
391
|
let str = FORMAT_PREDICATES.map(predicate => predicate(nodeOrSelection)).filter(Boolean).join(', ').toLocaleLowerCase();
|
|
392
|
+
|
|
312
393
|
if (str !== '') {
|
|
313
394
|
str = 'format: ' + str;
|
|
314
395
|
}
|
|
396
|
+
|
|
315
397
|
return str;
|
|
316
398
|
}
|
|
399
|
+
|
|
317
400
|
function printTargetProperties(node) {
|
|
318
|
-
let str = node.getTarget();
|
|
319
|
-
|
|
401
|
+
let str = node.getTarget(); // TODO Fix nullish on LinkNode
|
|
402
|
+
|
|
320
403
|
if (str != null) {
|
|
321
404
|
str = 'target: ' + str;
|
|
322
405
|
}
|
|
406
|
+
|
|
323
407
|
return str;
|
|
324
408
|
}
|
|
409
|
+
|
|
325
410
|
function printRelProperties(node) {
|
|
326
|
-
let str = node.getRel();
|
|
327
|
-
|
|
411
|
+
let str = node.getRel(); // TODO Fix nullish on LinkNode
|
|
412
|
+
|
|
328
413
|
if (str != null) {
|
|
329
414
|
str = 'rel: ' + str;
|
|
330
415
|
}
|
|
416
|
+
|
|
331
417
|
return str;
|
|
332
418
|
}
|
|
419
|
+
|
|
333
420
|
function printSelectedCharsLine({
|
|
334
421
|
indent,
|
|
335
422
|
isSelected,
|
|
@@ -341,18 +428,22 @@ function printSelectedCharsLine({
|
|
|
341
428
|
// No selection or node is not selected.
|
|
342
429
|
if (!lexical.$isTextNode(node) || !lexical.$isRangeSelection(selection) || !isSelected || lexical.$isElementNode(node)) {
|
|
343
430
|
return '';
|
|
344
|
-
}
|
|
431
|
+
} // No selected characters.
|
|
432
|
+
|
|
345
433
|
|
|
346
|
-
// No selected characters.
|
|
347
434
|
const anchor = selection.anchor;
|
|
348
435
|
const focus = selection.focus;
|
|
436
|
+
|
|
349
437
|
if (node.getTextContent() === '' || anchor.getNode() === selection.focus.getNode() && anchor.offset === focus.offset) {
|
|
350
438
|
return '';
|
|
351
439
|
}
|
|
440
|
+
|
|
352
441
|
const [start, end] = $getSelectionStartEnd(node, selection);
|
|
442
|
+
|
|
353
443
|
if (start === end) {
|
|
354
444
|
return '';
|
|
355
445
|
}
|
|
446
|
+
|
|
356
447
|
const selectionLastIndent = indent[indent.length - 1] === SYMBOLS.hasNextSibling ? SYMBOLS.ancestorHasNextSibling : SYMBOLS.ancestorIsLastChild;
|
|
357
448
|
const indentionChars = [...indent.slice(0, indent.length - 1), selectionLastIndent];
|
|
358
449
|
const unselectedChars = Array(start + 1).fill(' ');
|
|
@@ -362,18 +453,19 @@ function printSelectedCharsLine({
|
|
|
362
453
|
const nodePrintSpaces = Array(nodeKeyDisplay.length + paddingLength).fill(' ');
|
|
363
454
|
return [SYMBOLS.selectedLine, indentionChars.join(' '), [...nodePrintSpaces, ...unselectedChars, ...selectedChars].join('')].join(' ') + '\n';
|
|
364
455
|
}
|
|
456
|
+
|
|
365
457
|
function $getSelectionStartEnd(node, selection) {
|
|
366
458
|
const anchor = selection.anchor;
|
|
367
459
|
const focus = selection.focus;
|
|
368
460
|
const textContent = node.getTextContent();
|
|
369
461
|
const textLength = textContent.length;
|
|
370
462
|
let start = -1;
|
|
371
|
-
let end = -1;
|
|
463
|
+
let end = -1; // Only one node is being selected.
|
|
372
464
|
|
|
373
|
-
// Only one node is being selected.
|
|
374
465
|
if (anchor.type === 'text' && focus.type === 'text') {
|
|
375
466
|
const anchorNode = anchor.getNode();
|
|
376
467
|
const focusNode = focus.getNode();
|
|
468
|
+
|
|
377
469
|
if (anchorNode === focusNode && node === anchorNode && anchor.offset !== focus.offset) {
|
|
378
470
|
[start, end] = anchor.offset < focus.offset ? [anchor.offset, focus.offset] : [focus.offset, anchor.offset];
|
|
379
471
|
} else if (node === anchorNode) {
|
|
@@ -384,9 +476,9 @@ function $getSelectionStartEnd(node, selection) {
|
|
|
384
476
|
// Node is within selection but not the anchor nor focus.
|
|
385
477
|
[start, end] = [0, textLength];
|
|
386
478
|
}
|
|
387
|
-
}
|
|
479
|
+
} // Account for non-single width characters.
|
|
480
|
+
|
|
388
481
|
|
|
389
|
-
// Account for non-single width characters.
|
|
390
482
|
const numNonSingleWidthCharBeforeSelection = (textContent.slice(0, start).match(NON_SINGLE_WIDTH_CHARS_REGEX) || []).length;
|
|
391
483
|
const numNonSingleWidthCharInSelection = (textContent.slice(start, end).match(NON_SINGLE_WIDTH_CHARS_REGEX) || []).length;
|
|
392
484
|
return [start + numNonSingleWidthCharBeforeSelection, end + numNonSingleWidthCharBeforeSelection + numNonSingleWidthCharInSelection];
|
package/LexicalTreeView.prod.js
CHANGED
|
@@ -4,19 +4,20 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
'use strict';var
|
|
8
|
-
function
|
|
9
|
-
function Q(a
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
function V(
|
|
16
|
-
|
|
7
|
+
'use strict';var k=require("@lexical/link"),t=require("@lexical/mark"),u=require("@lexical/utils"),F=require("lexical"),J=require("react");let K=Object.freeze({"\t":"\\t","\n":"\\n"}),L=new RegExp(Object.keys(K).join("|"),"g"),M=Object.freeze({ancestorHasNextSibling:"|",ancestorIsLastChild:" ",hasNextSibling:"\u251c",isLastChild:"\u2514",selectedChar:"^",selectedLine:">"});
|
|
8
|
+
function N(a){let [c,d]=J.useState([]);J.useEffect(()=>{let l=new Set;for(let [g]of a._commands)l.add(a.registerCommand(g,b=>{d(e=>{e=[...e];e.push({payload:b,type:g.type?g.type:"UNKNOWN"});10<e.length&&e.shift();return e});return!1},F.COMMAND_PRIORITY_HIGH));return()=>l.forEach(g=>g())},[a]);return J.useMemo(()=>c,[c])}
|
|
9
|
+
function Q(a){let c="";var d=R(a);c+=`: range ${""!==d?`{ ${d} }`:""} ${""!==a.style?`{ style: ${a.style} } `:""}`;d=a.anchor;a=a.focus;let l=d.offset,g=a.offset;c+=`\n \u251c anchor { key: ${d.key}, offset: ${null===l?"null":l}, type: ${d.type} }`;return c+=`\n \u2514 focus { key: ${a.key}, offset: ${null===g?"null":g}, type: ${a.type} }`}
|
|
10
|
+
function S(a,c,d,l,g){let b=" root\n";a=a.read(()=>{const e=F.$getSelection();T(F.$getRoot(),(h,y)=>{const v=`(${h.getKey()})`,r=h.getType()||"",z=h.isSelected(),n=t.$isMarkNode(h)?` id: [ ${h.getIDs().join(", ")} ] `:"";var x=b,D=z?M.selectedLine:" ",A=y.join(" ");if(F.$isTextNode(h)){var m=h.getTextContent();var p=0===m.length?"(empty)":`"${U(m)}"`;m=[R(h),V(h),W(h)].filter(Boolean).join(", ");m=[p,0!==m.length?`{ ${m} }`:null].filter(Boolean).join(" ").trim()}else if(k.$isLinkNode(h)){m=h.getURL();
|
|
11
|
+
m=0===m.length?"(empty)":`"${U(m)}"`;p=h.getTarget();null!=p&&(p="target: "+p);var G=Boolean;let q=h.getRel();null!=q&&(q="rel: "+q);p=[p,q].filter(G).join(", ");m=[m,0!==p.length?`{ ${p} }`:null].filter(Boolean).join(" ").trim()}else m="";b=x+`${D} ${A} ${v} ${r} ${n} ${m}\n`;b+=X({indent:y,isSelected:z,node:h,nodeKeyDisplay:v,selection:e,typeDisplay:r})});return null===e?": null":F.$isRangeSelection(e)?Q(e):F.DEPRECATED_$isGridSelection(e)?`: grid\n \u2514 { grid: ${e.gridKey}, anchorCell: ${e.anchor.key}, focusCell: ${e.focus.key} }`:
|
|
12
|
+
`: node\n \u2514 [${Array.from(e._nodes).join(", ")}]`});b+="\n selection"+a;b+="\n\n commands:";if(d.length)for(let {type:e,payload:h}of d)b+=`\n \u2514 { type: ${e}, payload: ${h instanceof Event?h.constructor.name:h} }`;else b+="\n \u2514 None dispatched.";b+="\n\n editor:";b+=`\n \u2514 namespace ${c.namespace}`;null!==l&&(b+=`\n \u2514 compositionKey ${l}`);return b+=`\n \u2514 editable ${String(g)}`}
|
|
13
|
+
function T(a,c,d=[]){a=a.getChildren();let l=a.length;a.forEach((g,b)=>{c(g,d.concat(b===l-1?M.isLastChild:M.hasNextSibling));F.$isElementNode(g)&&T(g,c,d.concat(b===l-1?M.ancestorIsLastChild:M.ancestorHasNextSibling))})}function U(a){return Object.entries(K).reduce((c,[d,l])=>c.replace(new RegExp(d,"g"),String(l)),a)}
|
|
14
|
+
let Y=[a=>a.hasFormat("bold")&&"Bold",a=>a.hasFormat("code")&&"Code",a=>a.hasFormat("italic")&&"Italic",a=>a.hasFormat("strikethrough")&&"Strikethrough",a=>a.hasFormat("subscript")&&"Subscript",a=>a.hasFormat("superscript")&&"Superscript",a=>a.hasFormat("underline")&&"Underline"],Z=[a=>a.isDirectionless()&&"Directionless",a=>a.isUnmergeable()&&"Unmergeable"],aa=[a=>a.isToken()&&"Token",a=>a.isSegmented()&&"Segmented"];
|
|
15
|
+
function V(a){let c=Z.map(d=>d(a)).filter(Boolean).join(", ").toLocaleLowerCase();""!==c&&(c="detail: "+c);return c}function W(a){let c=aa.map(d=>d(a)).filter(Boolean).join(", ").toLocaleLowerCase();""!==c&&(c="mode: "+c);return c}function R(a){let c=Y.map(d=>d(a)).filter(Boolean).join(", ").toLocaleLowerCase();""!==c&&(c="format: "+c);return c}
|
|
16
|
+
function X({indent:a,isSelected:c,node:d,nodeKeyDisplay:l,selection:g,typeDisplay:b}){if(!F.$isTextNode(d)||!F.$isRangeSelection(g)||!c||F.$isElementNode(d))return"";c=g.anchor;var e=g.focus;if(""===d.getTextContent()||c.getNode()===g.focus.getNode()&&c.offset===e.offset)return"";e=g.anchor;let h=g.focus,y=d.getTextContent(),v=y.length;c=g=-1;if("text"===e.type&&"text"===h.type){let n=e.getNode(),x=h.getNode();n===x&&d===n&&e.offset!==h.offset?[g,c]=e.offset<h.offset?[e.offset,h.offset]:[h.offset,
|
|
17
|
+
e.offset]:d===n?[g,c]=n.isBefore(x)?[e.offset,v]:[0,e.offset]:d===x?[g,c]=x.isBefore(n)?[h.offset,v]:[0,h.offset]:[g,c]=[0,v]}d=(y.slice(0,g).match(L)||[]).length;e=(y.slice(g,c).match(L)||[]).length;let [r,z]=[g+d,c+d+e];if(r===z)return"";d=a[a.length-1]===M.hasNextSibling?M.ancestorHasNextSibling:M.ancestorIsLastChild;a=[...a.slice(0,a.length-1),d];d=Array(r+1).fill(" ");g=Array(z-r).fill(M.selectedChar);l=Array(l.length+(b.length+3)).fill(" ");return[M.selectedLine,a.join(" "),[...l,...d,...g].join("")].join(" ")+
|
|
17
18
|
"\n"}
|
|
18
|
-
exports.TreeView=function({timeTravelButtonClassName:a,timeTravelPanelSliderClassName:c,timeTravelPanelButtonClassName:d,viewClassName:l,timeTravelPanelClassName:
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
{
|
|
22
|
-
{
|
|
19
|
+
exports.TreeView=function({timeTravelButtonClassName:a,timeTravelPanelSliderClassName:c,timeTravelPanelButtonClassName:d,viewClassName:l,timeTravelPanelClassName:g,editor:b}){let [e,h]=J.useState([]),[y,v]=J.useState(""),[r,z]=J.useState(!1),n=J.useRef(0),x=J.useRef(null),D=J.useRef(null),[A,m]=J.useState(!1),[p,G]=J.useState(!1),[q,ba]=J.useState(!1),H=J.useRef(null),B=N(b),I=J.useCallback(f=>{const w=S(b.getEditorState(),b._config,B,b._compositionKey,b._editable);v(w);r||h(E=>[...E,[Date.now(),f]])},
|
|
20
|
+
[B,b,r]);J.useEffect(()=>{let f=b.getEditorState();!q&&1E3<f._nodeMap.size&&v(S(f,b._config,B,b._compositionKey,b._editable))},[B,b,q]);J.useEffect(()=>u.mergeRegister(b.registerUpdateListener(({editorState:f})=>{if(!q&&1E3<f._nodeMap.size&&(H.current=f,G(!0),!q))return;I(f)}),b.registerEditableListener(()=>{let f=S(b.getEditorState(),b._config,B,b._compositionKey,b._editable);v(f)})),[B,b,p,I,q]);let C=e.length;J.useEffect(()=>{if(A){let f,w=()=>{const E=n.current;E===C-1?m(!1):f=setTimeout(()=>
|
|
21
|
+
{n.current++;const O=n.current,P=D.current;null!==P&&(P.value=String(O));b.setEditorState(e[O][1]);w()},e[E+1][0]-e[E][0])};w();return()=>{clearTimeout(f)}}},[e,A,b,C]);J.useEffect(()=>{let f=x.current;if(null!==f)return f.__lexicalEditor=b,()=>{f.__lexicalEditor=null}},[b]);return J.createElement("div",{className:l},!q&&p?J.createElement("div",{style:{padding:20}},J.createElement("span",{style:{marginRight:20}},"Detected large EditorState, this can impact debugging performance."),J.createElement("button",
|
|
22
|
+
{onClick:()=>{ba(!0);let f=H.current;null!==f&&(H.current=null,I(f))},style:{background:"transparent",border:"1px solid white",color:"white",cursor:"pointer",padding:5}},"Show full tree")):null,!r&&(q||!p)&&2<C&&J.createElement("button",{onClick:()=>{let f=b.getRootElement();null!==f&&(f.contentEditable="false",n.current=C-1,z(!0))},className:a,type:"button"},"Time Travel"),(q||!p)&&J.createElement("pre",{ref:x},y),r&&(q||!p)&&J.createElement("div",{className:g},J.createElement("button",{className:d,
|
|
23
|
+
onClick:()=>{n.current===C-1&&(n.current=1);m(!A)},type:"button"},A?"Pause":"Play"),J.createElement("input",{className:c,ref:D,onChange:f=>{f=Number(f.target.value);let w=e[f];w&&(n.current=f,b.setEditorState(w[1]))},type:"range",min:"1",max:C-1}),J.createElement("button",{className:d,onClick:()=>{var f=b.getRootElement();if(null!==f){f.contentEditable="true";f=e.length-1;b.setEditorState(e[f][1]);let w=D.current;null!==w&&(w.value=String(f));z(!1);m(!1)}},type:"button"},"Exit")))}
|