@lexical/devtools-core 0.14.4
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/LICENSE +21 -0
- package/LexicalDevtoolsCore.dev.js +500 -0
- package/LexicalDevtoolsCore.dev.mjs +496 -0
- package/LexicalDevtoolsCore.js +9 -0
- package/LexicalDevtoolsCore.js.flow +8 -0
- package/LexicalDevtoolsCore.mjs +13 -0
- package/LexicalDevtoolsCore.node.mjs +11 -0
- package/LexicalDevtoolsCore.prod.js +25 -0
- package/LexicalDevtoolsCore.prod.mjs +7 -0
- package/README.md +5 -0
- package/TreeView.d.ts +21 -0
- package/generateContent.d.ts +12 -0
- package/index.d.ts +10 -0
- package/package.json +50 -0
- package/useLexicalCommandsLog.d.ts +14 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,500 @@
|
|
|
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 html = require('@lexical/html');
|
|
10
|
+
var link = require('@lexical/link');
|
|
11
|
+
var mark = require('@lexical/mark');
|
|
12
|
+
var table = require('@lexical/table');
|
|
13
|
+
var lexical = require('lexical');
|
|
14
|
+
var React = require('react');
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
18
|
+
*
|
|
19
|
+
* This source code is licensed under the MIT license found in the
|
|
20
|
+
* LICENSE file in the root directory of this source tree.
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
const NON_SINGLE_WIDTH_CHARS_REPLACEMENT = Object.freeze({
|
|
24
|
+
'\t': '\\t',
|
|
25
|
+
'\n': '\\n'
|
|
26
|
+
});
|
|
27
|
+
const NON_SINGLE_WIDTH_CHARS_REGEX = new RegExp(Object.keys(NON_SINGLE_WIDTH_CHARS_REPLACEMENT).join('|'), 'g');
|
|
28
|
+
const SYMBOLS = Object.freeze({
|
|
29
|
+
ancestorHasNextSibling: '|',
|
|
30
|
+
ancestorIsLastChild: ' ',
|
|
31
|
+
hasNextSibling: '├',
|
|
32
|
+
isLastChild: '└',
|
|
33
|
+
selectedChar: '^',
|
|
34
|
+
selectedLine: '>'
|
|
35
|
+
});
|
|
36
|
+
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'];
|
|
37
|
+
const FORMAT_PREDICATES_PARAGRAPH = [node => node.hasTextFormat('bold') && 'Bold', node => node.hasTextFormat('code') && 'Code', node => node.hasTextFormat('italic') && 'Italic', node => node.hasTextFormat('strikethrough') && 'Strikethrough', node => node.hasTextFormat('subscript') && 'Subscript', node => node.hasTextFormat('superscript') && 'Superscript', node => node.hasTextFormat('underline') && 'Underline'];
|
|
38
|
+
const DETAIL_PREDICATES = [node => node.isDirectionless() && 'Directionless', node => node.isUnmergeable() && 'Unmergeable'];
|
|
39
|
+
const MODE_PREDICATES = [node => node.isToken() && 'Token', node => node.isSegmented() && 'Segmented'];
|
|
40
|
+
function generateContent(editor, commandsLog, exportDOM) {
|
|
41
|
+
const editorState = editor.getEditorState();
|
|
42
|
+
const editorConfig = editor._config;
|
|
43
|
+
const compositionKey = editor._compositionKey;
|
|
44
|
+
const editable = editor._editable;
|
|
45
|
+
if (exportDOM) {
|
|
46
|
+
let htmlString = '';
|
|
47
|
+
editorState.read(() => {
|
|
48
|
+
htmlString = printPrettyHTML(html.$generateHtmlFromNodes(editor));
|
|
49
|
+
});
|
|
50
|
+
return htmlString;
|
|
51
|
+
}
|
|
52
|
+
let res = ' root\n';
|
|
53
|
+
const selectionString = editorState.read(() => {
|
|
54
|
+
const selection = lexical.$getSelection();
|
|
55
|
+
visitTree(lexical.$getRoot(), (node, indent) => {
|
|
56
|
+
const nodeKey = node.getKey();
|
|
57
|
+
const nodeKeyDisplay = `(${nodeKey})`;
|
|
58
|
+
const typeDisplay = node.getType() || '';
|
|
59
|
+
const isSelected = node.isSelected();
|
|
60
|
+
const idsDisplay = mark.$isMarkNode(node) ? ` id: [ ${node.getIDs().join(', ')} ] ` : '';
|
|
61
|
+
res += `${isSelected ? SYMBOLS.selectedLine : ' '} ${indent.join(' ')} ${nodeKeyDisplay} ${typeDisplay} ${idsDisplay} ${printNode(node)}\n`;
|
|
62
|
+
res += printSelectedCharsLine({
|
|
63
|
+
indent,
|
|
64
|
+
isSelected,
|
|
65
|
+
node,
|
|
66
|
+
nodeKeyDisplay,
|
|
67
|
+
selection,
|
|
68
|
+
typeDisplay
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
return selection === null ? ': null' : lexical.$isRangeSelection(selection) ? printRangeSelection(selection) : table.$isTableSelection(selection) ? printTableSelection(selection) : printNodeSelection(selection);
|
|
72
|
+
});
|
|
73
|
+
res += '\n selection' + selectionString;
|
|
74
|
+
res += '\n\n commands:';
|
|
75
|
+
if (commandsLog.length) {
|
|
76
|
+
for (const {
|
|
77
|
+
type,
|
|
78
|
+
payload
|
|
79
|
+
} of commandsLog) {
|
|
80
|
+
res += `\n └ { type: ${type}, payload: ${payload instanceof Event ? payload.constructor.name : payload} }`;
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
res += '\n └ None dispatched.';
|
|
84
|
+
}
|
|
85
|
+
res += '\n\n editor:';
|
|
86
|
+
res += `\n └ namespace ${editorConfig.namespace}`;
|
|
87
|
+
if (compositionKey !== null) {
|
|
88
|
+
res += `\n └ compositionKey ${compositionKey}`;
|
|
89
|
+
}
|
|
90
|
+
res += `\n └ editable ${String(editable)}`;
|
|
91
|
+
return res;
|
|
92
|
+
}
|
|
93
|
+
function printRangeSelection(selection) {
|
|
94
|
+
let res = '';
|
|
95
|
+
const formatText = printFormatProperties(selection);
|
|
96
|
+
res += `: range ${formatText !== '' ? `{ ${formatText} }` : ''} ${selection.style !== '' ? `{ style: ${selection.style} } ` : ''}`;
|
|
97
|
+
const anchor = selection.anchor;
|
|
98
|
+
const focus = selection.focus;
|
|
99
|
+
const anchorOffset = anchor.offset;
|
|
100
|
+
const focusOffset = focus.offset;
|
|
101
|
+
res += `\n ├ anchor { key: ${anchor.key}, offset: ${anchorOffset === null ? 'null' : anchorOffset}, type: ${anchor.type} }`;
|
|
102
|
+
res += `\n └ focus { key: ${focus.key}, offset: ${focusOffset === null ? 'null' : focusOffset}, type: ${focus.type} }`;
|
|
103
|
+
return res;
|
|
104
|
+
}
|
|
105
|
+
function printNodeSelection(selection) {
|
|
106
|
+
if (!lexical.$isNodeSelection(selection)) {
|
|
107
|
+
return '';
|
|
108
|
+
}
|
|
109
|
+
return `: node\n └ [${Array.from(selection._nodes).join(', ')}]`;
|
|
110
|
+
}
|
|
111
|
+
function printTableSelection(selection) {
|
|
112
|
+
return `: table\n └ { table: ${selection.tableKey}, anchorCell: ${selection.anchor.key}, focusCell: ${selection.focus.key} }`;
|
|
113
|
+
}
|
|
114
|
+
function visitTree(currentNode, visitor, indent = []) {
|
|
115
|
+
const childNodes = currentNode.getChildren();
|
|
116
|
+
const childNodesLength = childNodes.length;
|
|
117
|
+
childNodes.forEach((childNode, i) => {
|
|
118
|
+
visitor(childNode, indent.concat(i === childNodesLength - 1 ? SYMBOLS.isLastChild : SYMBOLS.hasNextSibling));
|
|
119
|
+
if (lexical.$isElementNode(childNode)) {
|
|
120
|
+
visitTree(childNode, visitor, indent.concat(i === childNodesLength - 1 ? SYMBOLS.ancestorIsLastChild : SYMBOLS.ancestorHasNextSibling));
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
function normalize(text) {
|
|
125
|
+
return Object.entries(NON_SINGLE_WIDTH_CHARS_REPLACEMENT).reduce((acc, [key, value]) => acc.replace(new RegExp(key, 'g'), String(value)), text);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// TODO Pass via props to allow customizability
|
|
129
|
+
function printNode(node) {
|
|
130
|
+
if (lexical.$isTextNode(node)) {
|
|
131
|
+
const text = node.getTextContent();
|
|
132
|
+
const title = text.length === 0 ? '(empty)' : `"${normalize(text)}"`;
|
|
133
|
+
const properties = printAllTextNodeProperties(node);
|
|
134
|
+
return [title, properties.length !== 0 ? `{ ${properties} }` : null].filter(Boolean).join(' ').trim();
|
|
135
|
+
} else if (link.$isLinkNode(node)) {
|
|
136
|
+
const link = node.getURL();
|
|
137
|
+
const title = link.length === 0 ? '(empty)' : `"${normalize(link)}"`;
|
|
138
|
+
const properties = printAllLinkNodeProperties(node);
|
|
139
|
+
return [title, properties.length !== 0 ? `{ ${properties} }` : null].filter(Boolean).join(' ').trim();
|
|
140
|
+
} else if (lexical.$isParagraphNode(node)) {
|
|
141
|
+
const formatText = printTextFormatProperties(node);
|
|
142
|
+
return formatText !== '' ? `{ ${formatText} }` : '';
|
|
143
|
+
} else {
|
|
144
|
+
return '';
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function printTextFormatProperties(nodeOrSelection) {
|
|
148
|
+
let str = FORMAT_PREDICATES_PARAGRAPH.map(predicate => predicate(nodeOrSelection)).filter(Boolean).join(', ').toLocaleLowerCase();
|
|
149
|
+
if (str !== '') {
|
|
150
|
+
str = 'format: ' + str;
|
|
151
|
+
}
|
|
152
|
+
return str;
|
|
153
|
+
}
|
|
154
|
+
function printAllTextNodeProperties(node) {
|
|
155
|
+
return [printFormatProperties(node), printDetailProperties(node), printModeProperties(node)].filter(Boolean).join(', ');
|
|
156
|
+
}
|
|
157
|
+
function printAllLinkNodeProperties(node) {
|
|
158
|
+
return [printTargetProperties(node), printRelProperties(node), printTitleProperties(node)].filter(Boolean).join(', ');
|
|
159
|
+
}
|
|
160
|
+
function printDetailProperties(nodeOrSelection) {
|
|
161
|
+
let str = DETAIL_PREDICATES.map(predicate => predicate(nodeOrSelection)).filter(Boolean).join(', ').toLocaleLowerCase();
|
|
162
|
+
if (str !== '') {
|
|
163
|
+
str = 'detail: ' + str;
|
|
164
|
+
}
|
|
165
|
+
return str;
|
|
166
|
+
}
|
|
167
|
+
function printModeProperties(nodeOrSelection) {
|
|
168
|
+
let str = MODE_PREDICATES.map(predicate => predicate(nodeOrSelection)).filter(Boolean).join(', ').toLocaleLowerCase();
|
|
169
|
+
if (str !== '') {
|
|
170
|
+
str = 'mode: ' + str;
|
|
171
|
+
}
|
|
172
|
+
return str;
|
|
173
|
+
}
|
|
174
|
+
function printFormatProperties(nodeOrSelection) {
|
|
175
|
+
let str = FORMAT_PREDICATES.map(predicate => predicate(nodeOrSelection)).filter(Boolean).join(', ').toLocaleLowerCase();
|
|
176
|
+
if (str !== '') {
|
|
177
|
+
str = 'format: ' + str;
|
|
178
|
+
}
|
|
179
|
+
return str;
|
|
180
|
+
}
|
|
181
|
+
function printTargetProperties(node) {
|
|
182
|
+
let str = node.getTarget();
|
|
183
|
+
// TODO Fix nullish on LinkNode
|
|
184
|
+
if (str != null) {
|
|
185
|
+
str = 'target: ' + str;
|
|
186
|
+
}
|
|
187
|
+
return str;
|
|
188
|
+
}
|
|
189
|
+
function printRelProperties(node) {
|
|
190
|
+
let str = node.getRel();
|
|
191
|
+
// TODO Fix nullish on LinkNode
|
|
192
|
+
if (str != null) {
|
|
193
|
+
str = 'rel: ' + str;
|
|
194
|
+
}
|
|
195
|
+
return str;
|
|
196
|
+
}
|
|
197
|
+
function printTitleProperties(node) {
|
|
198
|
+
let str = node.getTitle();
|
|
199
|
+
// TODO Fix nullish on LinkNode
|
|
200
|
+
if (str != null) {
|
|
201
|
+
str = 'title: ' + str;
|
|
202
|
+
}
|
|
203
|
+
return str;
|
|
204
|
+
}
|
|
205
|
+
function printSelectedCharsLine({
|
|
206
|
+
indent,
|
|
207
|
+
isSelected,
|
|
208
|
+
node,
|
|
209
|
+
nodeKeyDisplay,
|
|
210
|
+
selection,
|
|
211
|
+
typeDisplay
|
|
212
|
+
}) {
|
|
213
|
+
// No selection or node is not selected.
|
|
214
|
+
if (!lexical.$isTextNode(node) || !lexical.$isRangeSelection(selection) || !isSelected || lexical.$isElementNode(node)) {
|
|
215
|
+
return '';
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// No selected characters.
|
|
219
|
+
const anchor = selection.anchor;
|
|
220
|
+
const focus = selection.focus;
|
|
221
|
+
if (node.getTextContent() === '' || anchor.getNode() === selection.focus.getNode() && anchor.offset === focus.offset) {
|
|
222
|
+
return '';
|
|
223
|
+
}
|
|
224
|
+
const [start, end] = $getSelectionStartEnd(node, selection);
|
|
225
|
+
if (start === end) {
|
|
226
|
+
return '';
|
|
227
|
+
}
|
|
228
|
+
const selectionLastIndent = indent[indent.length - 1] === SYMBOLS.hasNextSibling ? SYMBOLS.ancestorHasNextSibling : SYMBOLS.ancestorIsLastChild;
|
|
229
|
+
const indentionChars = [...indent.slice(0, indent.length - 1), selectionLastIndent];
|
|
230
|
+
const unselectedChars = Array(start + 1).fill(' ');
|
|
231
|
+
const selectedChars = Array(end - start).fill(SYMBOLS.selectedChar);
|
|
232
|
+
const paddingLength = typeDisplay.length + 3; // 2 for the spaces around + 1 for the double quote.
|
|
233
|
+
|
|
234
|
+
const nodePrintSpaces = Array(nodeKeyDisplay.length + paddingLength).fill(' ');
|
|
235
|
+
return [SYMBOLS.selectedLine, indentionChars.join(' '), [...nodePrintSpaces, ...unselectedChars, ...selectedChars].join('')].join(' ') + '\n';
|
|
236
|
+
}
|
|
237
|
+
function printPrettyHTML(str) {
|
|
238
|
+
const div = document.createElement('div');
|
|
239
|
+
div.innerHTML = str.trim();
|
|
240
|
+
return prettifyHTML(div, 0).innerHTML;
|
|
241
|
+
}
|
|
242
|
+
function prettifyHTML(node, level) {
|
|
243
|
+
const indentBefore = new Array(level++ + 1).join(' ');
|
|
244
|
+
const indentAfter = new Array(level - 1).join(' ');
|
|
245
|
+
let textNode;
|
|
246
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
247
|
+
textNode = document.createTextNode('\n' + indentBefore);
|
|
248
|
+
node.insertBefore(textNode, node.children[i]);
|
|
249
|
+
prettifyHTML(node.children[i], level);
|
|
250
|
+
if (node.lastElementChild === node.children[i]) {
|
|
251
|
+
textNode = document.createTextNode('\n' + indentAfter);
|
|
252
|
+
node.appendChild(textNode);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return node;
|
|
256
|
+
}
|
|
257
|
+
function $getSelectionStartEnd(node, selection) {
|
|
258
|
+
const anchorAndFocus = selection.getStartEndPoints();
|
|
259
|
+
if (lexical.$isNodeSelection(selection) || anchorAndFocus === null) {
|
|
260
|
+
return [-1, -1];
|
|
261
|
+
}
|
|
262
|
+
const [anchor, focus] = anchorAndFocus;
|
|
263
|
+
const textContent = node.getTextContent();
|
|
264
|
+
const textLength = textContent.length;
|
|
265
|
+
let start = -1;
|
|
266
|
+
let end = -1;
|
|
267
|
+
|
|
268
|
+
// Only one node is being selected.
|
|
269
|
+
if (anchor.type === 'text' && focus.type === 'text') {
|
|
270
|
+
const anchorNode = anchor.getNode();
|
|
271
|
+
const focusNode = focus.getNode();
|
|
272
|
+
if (anchorNode === focusNode && node === anchorNode && anchor.offset !== focus.offset) {
|
|
273
|
+
[start, end] = anchor.offset < focus.offset ? [anchor.offset, focus.offset] : [focus.offset, anchor.offset];
|
|
274
|
+
} else if (node === anchorNode) {
|
|
275
|
+
[start, end] = anchorNode.isBefore(focusNode) ? [anchor.offset, textLength] : [0, anchor.offset];
|
|
276
|
+
} else if (node === focusNode) {
|
|
277
|
+
[start, end] = focusNode.isBefore(anchorNode) ? [focus.offset, textLength] : [0, focus.offset];
|
|
278
|
+
} else {
|
|
279
|
+
// Node is within selection but not the anchor nor focus.
|
|
280
|
+
[start, end] = [0, textLength];
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Account for non-single width characters.
|
|
285
|
+
const numNonSingleWidthCharBeforeSelection = (textContent.slice(0, start).match(NON_SINGLE_WIDTH_CHARS_REGEX) || []).length;
|
|
286
|
+
const numNonSingleWidthCharInSelection = (textContent.slice(start, end).match(NON_SINGLE_WIDTH_CHARS_REGEX) || []).length;
|
|
287
|
+
return [start + numNonSingleWidthCharBeforeSelection, end + numNonSingleWidthCharBeforeSelection + numNonSingleWidthCharInSelection];
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
292
|
+
*
|
|
293
|
+
* This source code is licensed under the MIT license found in the
|
|
294
|
+
* LICENSE file in the root directory of this source tree.
|
|
295
|
+
*
|
|
296
|
+
*/
|
|
297
|
+
const LARGE_EDITOR_STATE_SIZE = 1000;
|
|
298
|
+
const TreeView = /*#__PURE__*/React.forwardRef(function TreeViewWrapped({
|
|
299
|
+
treeTypeButtonClassName,
|
|
300
|
+
timeTravelButtonClassName,
|
|
301
|
+
timeTravelPanelSliderClassName,
|
|
302
|
+
timeTravelPanelButtonClassName,
|
|
303
|
+
viewClassName,
|
|
304
|
+
timeTravelPanelClassName,
|
|
305
|
+
editorState,
|
|
306
|
+
setEditorState,
|
|
307
|
+
setEditorReadOnly,
|
|
308
|
+
generateContent
|
|
309
|
+
}, ref) {
|
|
310
|
+
const [timeStampedEditorStates, setTimeStampedEditorStates] = React.useState([]);
|
|
311
|
+
const [content, setContent] = React.useState('');
|
|
312
|
+
const [timeTravelEnabled, setTimeTravelEnabled] = React.useState(false);
|
|
313
|
+
const [showExportDOM, setShowExportDOM] = React.useState(false);
|
|
314
|
+
const playingIndexRef = React.useRef(0);
|
|
315
|
+
const inputRef = React.useRef(null);
|
|
316
|
+
const [isPlaying, setIsPlaying] = React.useState(false);
|
|
317
|
+
const [isLimited, setIsLimited] = React.useState(false);
|
|
318
|
+
const [showLimited, setShowLimited] = React.useState(false);
|
|
319
|
+
const lastEditorStateRef = React.useRef();
|
|
320
|
+
const lastGenerationID = React.useRef(0);
|
|
321
|
+
const generateTree = React.useCallback(exportDOM => {
|
|
322
|
+
const myID = ++lastGenerationID.current;
|
|
323
|
+
generateContent(exportDOM).then(treeText => {
|
|
324
|
+
if (myID === lastGenerationID.current) {
|
|
325
|
+
setContent(treeText);
|
|
326
|
+
}
|
|
327
|
+
}).catch(err => {
|
|
328
|
+
if (myID === lastGenerationID.current) {
|
|
329
|
+
setContent(`Error rendering tree: ${err.message}\n\nStack:\n${err.stack}`);
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
}, [generateContent]);
|
|
333
|
+
React.useEffect(() => {
|
|
334
|
+
if (!showLimited && editorState._nodeMap.size > LARGE_EDITOR_STATE_SIZE) {
|
|
335
|
+
setIsLimited(true);
|
|
336
|
+
if (!showLimited) {
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// Prevent re-rendering if the editor state hasn't changed
|
|
342
|
+
if (lastEditorStateRef.current !== editorState) {
|
|
343
|
+
lastEditorStateRef.current = editorState;
|
|
344
|
+
generateTree(showExportDOM);
|
|
345
|
+
if (!timeTravelEnabled) {
|
|
346
|
+
setTimeStampedEditorStates(currentEditorStates => [...currentEditorStates, [Date.now(), editorState]]);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}, [editorState, generateTree, showExportDOM, showLimited, timeTravelEnabled]);
|
|
350
|
+
const totalEditorStates = timeStampedEditorStates.length;
|
|
351
|
+
React.useEffect(() => {
|
|
352
|
+
if (isPlaying) {
|
|
353
|
+
let timeoutId;
|
|
354
|
+
const play = () => {
|
|
355
|
+
const currentIndex = playingIndexRef.current;
|
|
356
|
+
if (currentIndex === totalEditorStates - 1) {
|
|
357
|
+
setIsPlaying(false);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
const currentTime = timeStampedEditorStates[currentIndex][0];
|
|
361
|
+
const nextTime = timeStampedEditorStates[currentIndex + 1][0];
|
|
362
|
+
const timeDiff = nextTime - currentTime;
|
|
363
|
+
timeoutId = setTimeout(() => {
|
|
364
|
+
playingIndexRef.current++;
|
|
365
|
+
const index = playingIndexRef.current;
|
|
366
|
+
const input = inputRef.current;
|
|
367
|
+
if (input !== null) {
|
|
368
|
+
input.value = String(index);
|
|
369
|
+
}
|
|
370
|
+
setEditorState(timeStampedEditorStates[index][1]);
|
|
371
|
+
play();
|
|
372
|
+
}, timeDiff);
|
|
373
|
+
};
|
|
374
|
+
play();
|
|
375
|
+
return () => {
|
|
376
|
+
clearTimeout(timeoutId);
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
}, [timeStampedEditorStates, isPlaying, totalEditorStates, setEditorState]);
|
|
380
|
+
const handleExportModeToggleClick = () => {
|
|
381
|
+
generateTree(!showExportDOM);
|
|
382
|
+
setShowExportDOM(!showExportDOM);
|
|
383
|
+
};
|
|
384
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
385
|
+
className: viewClassName
|
|
386
|
+
}, !showLimited && isLimited ? /*#__PURE__*/React.createElement("div", {
|
|
387
|
+
style: {
|
|
388
|
+
padding: 20
|
|
389
|
+
}
|
|
390
|
+
}, /*#__PURE__*/React.createElement("span", {
|
|
391
|
+
style: {
|
|
392
|
+
marginRight: 20
|
|
393
|
+
}
|
|
394
|
+
}, "Detected large EditorState, this can impact debugging performance."), /*#__PURE__*/React.createElement("button", {
|
|
395
|
+
onClick: () => {
|
|
396
|
+
setShowLimited(true);
|
|
397
|
+
},
|
|
398
|
+
style: {
|
|
399
|
+
background: 'transparent',
|
|
400
|
+
border: '1px solid white',
|
|
401
|
+
color: 'white',
|
|
402
|
+
cursor: 'pointer',
|
|
403
|
+
padding: 5
|
|
404
|
+
}
|
|
405
|
+
}, "Show full tree")) : null, !showLimited ? /*#__PURE__*/React.createElement("button", {
|
|
406
|
+
onClick: () => handleExportModeToggleClick(),
|
|
407
|
+
className: treeTypeButtonClassName,
|
|
408
|
+
type: "button"
|
|
409
|
+
}, showExportDOM ? 'Tree' : 'Export DOM') : null, !timeTravelEnabled && (showLimited || !isLimited) && totalEditorStates > 2 && /*#__PURE__*/React.createElement("button", {
|
|
410
|
+
onClick: () => {
|
|
411
|
+
setEditorReadOnly(true);
|
|
412
|
+
playingIndexRef.current = totalEditorStates - 1;
|
|
413
|
+
setTimeTravelEnabled(true);
|
|
414
|
+
},
|
|
415
|
+
className: timeTravelButtonClassName,
|
|
416
|
+
type: "button"
|
|
417
|
+
}, "Time Travel"), (showLimited || !isLimited) && /*#__PURE__*/React.createElement("pre", {
|
|
418
|
+
ref: ref
|
|
419
|
+
}, content), timeTravelEnabled && (showLimited || !isLimited) && /*#__PURE__*/React.createElement("div", {
|
|
420
|
+
className: timeTravelPanelClassName
|
|
421
|
+
}, /*#__PURE__*/React.createElement("button", {
|
|
422
|
+
className: timeTravelPanelButtonClassName,
|
|
423
|
+
onClick: () => {
|
|
424
|
+
if (playingIndexRef.current === totalEditorStates - 1) {
|
|
425
|
+
playingIndexRef.current = 1;
|
|
426
|
+
}
|
|
427
|
+
setIsPlaying(!isPlaying);
|
|
428
|
+
},
|
|
429
|
+
type: "button"
|
|
430
|
+
}, isPlaying ? 'Pause' : 'Play'), /*#__PURE__*/React.createElement("input", {
|
|
431
|
+
className: timeTravelPanelSliderClassName,
|
|
432
|
+
ref: inputRef,
|
|
433
|
+
onChange: event => {
|
|
434
|
+
const editorStateIndex = Number(event.target.value);
|
|
435
|
+
const timeStampedEditorState = timeStampedEditorStates[editorStateIndex];
|
|
436
|
+
if (timeStampedEditorState) {
|
|
437
|
+
playingIndexRef.current = editorStateIndex;
|
|
438
|
+
setEditorState(timeStampedEditorState[1]);
|
|
439
|
+
}
|
|
440
|
+
},
|
|
441
|
+
type: "range",
|
|
442
|
+
min: "1",
|
|
443
|
+
max: totalEditorStates - 1
|
|
444
|
+
}), /*#__PURE__*/React.createElement("button", {
|
|
445
|
+
className: timeTravelPanelButtonClassName,
|
|
446
|
+
onClick: () => {
|
|
447
|
+
setEditorReadOnly(false);
|
|
448
|
+
const index = timeStampedEditorStates.length - 1;
|
|
449
|
+
const timeStampedEditorState = timeStampedEditorStates[index];
|
|
450
|
+
setEditorState(timeStampedEditorState[1]);
|
|
451
|
+
const input = inputRef.current;
|
|
452
|
+
if (input !== null) {
|
|
453
|
+
input.value = String(index);
|
|
454
|
+
}
|
|
455
|
+
setTimeTravelEnabled(false);
|
|
456
|
+
setIsPlaying(false);
|
|
457
|
+
},
|
|
458
|
+
type: "button"
|
|
459
|
+
}, "Exit")));
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
464
|
+
*
|
|
465
|
+
* This source code is licensed under the MIT license found in the
|
|
466
|
+
* LICENSE file in the root directory of this source tree.
|
|
467
|
+
*
|
|
468
|
+
*/
|
|
469
|
+
function registerLexicalCommandLogger(editor, setLoggedCommands) {
|
|
470
|
+
const unregisterCommandListeners = new Set();
|
|
471
|
+
for (const [command] of editor._commands) {
|
|
472
|
+
unregisterCommandListeners.add(editor.registerCommand(command, payload => {
|
|
473
|
+
setLoggedCommands(state => {
|
|
474
|
+
const newState = [...state];
|
|
475
|
+
newState.push({
|
|
476
|
+
payload,
|
|
477
|
+
type: command.type ? command.type : 'UNKNOWN'
|
|
478
|
+
});
|
|
479
|
+
if (newState.length > 10) {
|
|
480
|
+
newState.shift();
|
|
481
|
+
}
|
|
482
|
+
return newState;
|
|
483
|
+
});
|
|
484
|
+
return false;
|
|
485
|
+
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
|
486
|
+
}
|
|
487
|
+
return () => unregisterCommandListeners.forEach(unregister => unregister());
|
|
488
|
+
}
|
|
489
|
+
function useLexicalCommandsLog(editor) {
|
|
490
|
+
const [loggedCommands, setLoggedCommands] = React.useState([]);
|
|
491
|
+
React.useEffect(() => {
|
|
492
|
+
return registerLexicalCommandLogger(editor, setLoggedCommands);
|
|
493
|
+
}, [editor]);
|
|
494
|
+
return React.useMemo(() => loggedCommands, [loggedCommands]);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
exports.TreeView = TreeView;
|
|
498
|
+
exports.generateContent = generateContent;
|
|
499
|
+
exports.registerLexicalCommandLogger = registerLexicalCommandLogger;
|
|
500
|
+
exports.useLexicalCommandsLog = useLexicalCommandsLog;
|
|
@@ -0,0 +1,496 @@
|
|
|
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
|
+
import { $generateHtmlFromNodes } from '@lexical/html';
|
|
8
|
+
import { $isLinkNode } from '@lexical/link';
|
|
9
|
+
import { $isMarkNode } from '@lexical/mark';
|
|
10
|
+
import { $isTableSelection } from '@lexical/table';
|
|
11
|
+
import { $getSelection, $getRoot, $isRangeSelection, $isNodeSelection, $isElementNode, $isTextNode, $isParagraphNode, COMMAND_PRIORITY_CRITICAL } from 'lexical';
|
|
12
|
+
import * as React from 'react';
|
|
13
|
+
import { forwardRef, useState, useRef, useCallback, useEffect, useMemo } from 'react';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Copyright (c) Meta Platforms, Inc. and 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
|
+
const NON_SINGLE_WIDTH_CHARS_REPLACEMENT = Object.freeze({
|
|
23
|
+
'\t': '\\t',
|
|
24
|
+
'\n': '\\n'
|
|
25
|
+
});
|
|
26
|
+
const NON_SINGLE_WIDTH_CHARS_REGEX = new RegExp(Object.keys(NON_SINGLE_WIDTH_CHARS_REPLACEMENT).join('|'), 'g');
|
|
27
|
+
const SYMBOLS = Object.freeze({
|
|
28
|
+
ancestorHasNextSibling: '|',
|
|
29
|
+
ancestorIsLastChild: ' ',
|
|
30
|
+
hasNextSibling: '├',
|
|
31
|
+
isLastChild: '└',
|
|
32
|
+
selectedChar: '^',
|
|
33
|
+
selectedLine: '>'
|
|
34
|
+
});
|
|
35
|
+
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'];
|
|
36
|
+
const FORMAT_PREDICATES_PARAGRAPH = [node => node.hasTextFormat('bold') && 'Bold', node => node.hasTextFormat('code') && 'Code', node => node.hasTextFormat('italic') && 'Italic', node => node.hasTextFormat('strikethrough') && 'Strikethrough', node => node.hasTextFormat('subscript') && 'Subscript', node => node.hasTextFormat('superscript') && 'Superscript', node => node.hasTextFormat('underline') && 'Underline'];
|
|
37
|
+
const DETAIL_PREDICATES = [node => node.isDirectionless() && 'Directionless', node => node.isUnmergeable() && 'Unmergeable'];
|
|
38
|
+
const MODE_PREDICATES = [node => node.isToken() && 'Token', node => node.isSegmented() && 'Segmented'];
|
|
39
|
+
function generateContent(editor, commandsLog, exportDOM) {
|
|
40
|
+
const editorState = editor.getEditorState();
|
|
41
|
+
const editorConfig = editor._config;
|
|
42
|
+
const compositionKey = editor._compositionKey;
|
|
43
|
+
const editable = editor._editable;
|
|
44
|
+
if (exportDOM) {
|
|
45
|
+
let htmlString = '';
|
|
46
|
+
editorState.read(() => {
|
|
47
|
+
htmlString = printPrettyHTML($generateHtmlFromNodes(editor));
|
|
48
|
+
});
|
|
49
|
+
return htmlString;
|
|
50
|
+
}
|
|
51
|
+
let res = ' root\n';
|
|
52
|
+
const selectionString = editorState.read(() => {
|
|
53
|
+
const selection = $getSelection();
|
|
54
|
+
visitTree($getRoot(), (node, indent) => {
|
|
55
|
+
const nodeKey = node.getKey();
|
|
56
|
+
const nodeKeyDisplay = `(${nodeKey})`;
|
|
57
|
+
const typeDisplay = node.getType() || '';
|
|
58
|
+
const isSelected = node.isSelected();
|
|
59
|
+
const idsDisplay = $isMarkNode(node) ? ` id: [ ${node.getIDs().join(', ')} ] ` : '';
|
|
60
|
+
res += `${isSelected ? SYMBOLS.selectedLine : ' '} ${indent.join(' ')} ${nodeKeyDisplay} ${typeDisplay} ${idsDisplay} ${printNode(node)}\n`;
|
|
61
|
+
res += printSelectedCharsLine({
|
|
62
|
+
indent,
|
|
63
|
+
isSelected,
|
|
64
|
+
node,
|
|
65
|
+
nodeKeyDisplay,
|
|
66
|
+
selection,
|
|
67
|
+
typeDisplay
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
return selection === null ? ': null' : $isRangeSelection(selection) ? printRangeSelection(selection) : $isTableSelection(selection) ? printTableSelection(selection) : printNodeSelection(selection);
|
|
71
|
+
});
|
|
72
|
+
res += '\n selection' + selectionString;
|
|
73
|
+
res += '\n\n commands:';
|
|
74
|
+
if (commandsLog.length) {
|
|
75
|
+
for (const {
|
|
76
|
+
type,
|
|
77
|
+
payload
|
|
78
|
+
} of commandsLog) {
|
|
79
|
+
res += `\n └ { type: ${type}, payload: ${payload instanceof Event ? payload.constructor.name : payload} }`;
|
|
80
|
+
}
|
|
81
|
+
} else {
|
|
82
|
+
res += '\n └ None dispatched.';
|
|
83
|
+
}
|
|
84
|
+
res += '\n\n editor:';
|
|
85
|
+
res += `\n └ namespace ${editorConfig.namespace}`;
|
|
86
|
+
if (compositionKey !== null) {
|
|
87
|
+
res += `\n └ compositionKey ${compositionKey}`;
|
|
88
|
+
}
|
|
89
|
+
res += `\n └ editable ${String(editable)}`;
|
|
90
|
+
return res;
|
|
91
|
+
}
|
|
92
|
+
function printRangeSelection(selection) {
|
|
93
|
+
let res = '';
|
|
94
|
+
const formatText = printFormatProperties(selection);
|
|
95
|
+
res += `: range ${formatText !== '' ? `{ ${formatText} }` : ''} ${selection.style !== '' ? `{ style: ${selection.style} } ` : ''}`;
|
|
96
|
+
const anchor = selection.anchor;
|
|
97
|
+
const focus = selection.focus;
|
|
98
|
+
const anchorOffset = anchor.offset;
|
|
99
|
+
const focusOffset = focus.offset;
|
|
100
|
+
res += `\n ├ anchor { key: ${anchor.key}, offset: ${anchorOffset === null ? 'null' : anchorOffset}, type: ${anchor.type} }`;
|
|
101
|
+
res += `\n └ focus { key: ${focus.key}, offset: ${focusOffset === null ? 'null' : focusOffset}, type: ${focus.type} }`;
|
|
102
|
+
return res;
|
|
103
|
+
}
|
|
104
|
+
function printNodeSelection(selection) {
|
|
105
|
+
if (!$isNodeSelection(selection)) {
|
|
106
|
+
return '';
|
|
107
|
+
}
|
|
108
|
+
return `: node\n └ [${Array.from(selection._nodes).join(', ')}]`;
|
|
109
|
+
}
|
|
110
|
+
function printTableSelection(selection) {
|
|
111
|
+
return `: table\n └ { table: ${selection.tableKey}, anchorCell: ${selection.anchor.key}, focusCell: ${selection.focus.key} }`;
|
|
112
|
+
}
|
|
113
|
+
function visitTree(currentNode, visitor, indent = []) {
|
|
114
|
+
const childNodes = currentNode.getChildren();
|
|
115
|
+
const childNodesLength = childNodes.length;
|
|
116
|
+
childNodes.forEach((childNode, i) => {
|
|
117
|
+
visitor(childNode, indent.concat(i === childNodesLength - 1 ? SYMBOLS.isLastChild : SYMBOLS.hasNextSibling));
|
|
118
|
+
if ($isElementNode(childNode)) {
|
|
119
|
+
visitTree(childNode, visitor, indent.concat(i === childNodesLength - 1 ? SYMBOLS.ancestorIsLastChild : SYMBOLS.ancestorHasNextSibling));
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
function normalize(text) {
|
|
124
|
+
return Object.entries(NON_SINGLE_WIDTH_CHARS_REPLACEMENT).reduce((acc, [key, value]) => acc.replace(new RegExp(key, 'g'), String(value)), text);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// TODO Pass via props to allow customizability
|
|
128
|
+
function printNode(node) {
|
|
129
|
+
if ($isTextNode(node)) {
|
|
130
|
+
const text = node.getTextContent();
|
|
131
|
+
const title = text.length === 0 ? '(empty)' : `"${normalize(text)}"`;
|
|
132
|
+
const properties = printAllTextNodeProperties(node);
|
|
133
|
+
return [title, properties.length !== 0 ? `{ ${properties} }` : null].filter(Boolean).join(' ').trim();
|
|
134
|
+
} else if ($isLinkNode(node)) {
|
|
135
|
+
const link = node.getURL();
|
|
136
|
+
const title = link.length === 0 ? '(empty)' : `"${normalize(link)}"`;
|
|
137
|
+
const properties = printAllLinkNodeProperties(node);
|
|
138
|
+
return [title, properties.length !== 0 ? `{ ${properties} }` : null].filter(Boolean).join(' ').trim();
|
|
139
|
+
} else if ($isParagraphNode(node)) {
|
|
140
|
+
const formatText = printTextFormatProperties(node);
|
|
141
|
+
return formatText !== '' ? `{ ${formatText} }` : '';
|
|
142
|
+
} else {
|
|
143
|
+
return '';
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function printTextFormatProperties(nodeOrSelection) {
|
|
147
|
+
let str = FORMAT_PREDICATES_PARAGRAPH.map(predicate => predicate(nodeOrSelection)).filter(Boolean).join(', ').toLocaleLowerCase();
|
|
148
|
+
if (str !== '') {
|
|
149
|
+
str = 'format: ' + str;
|
|
150
|
+
}
|
|
151
|
+
return str;
|
|
152
|
+
}
|
|
153
|
+
function printAllTextNodeProperties(node) {
|
|
154
|
+
return [printFormatProperties(node), printDetailProperties(node), printModeProperties(node)].filter(Boolean).join(', ');
|
|
155
|
+
}
|
|
156
|
+
function printAllLinkNodeProperties(node) {
|
|
157
|
+
return [printTargetProperties(node), printRelProperties(node), printTitleProperties(node)].filter(Boolean).join(', ');
|
|
158
|
+
}
|
|
159
|
+
function printDetailProperties(nodeOrSelection) {
|
|
160
|
+
let str = DETAIL_PREDICATES.map(predicate => predicate(nodeOrSelection)).filter(Boolean).join(', ').toLocaleLowerCase();
|
|
161
|
+
if (str !== '') {
|
|
162
|
+
str = 'detail: ' + str;
|
|
163
|
+
}
|
|
164
|
+
return str;
|
|
165
|
+
}
|
|
166
|
+
function printModeProperties(nodeOrSelection) {
|
|
167
|
+
let str = MODE_PREDICATES.map(predicate => predicate(nodeOrSelection)).filter(Boolean).join(', ').toLocaleLowerCase();
|
|
168
|
+
if (str !== '') {
|
|
169
|
+
str = 'mode: ' + str;
|
|
170
|
+
}
|
|
171
|
+
return str;
|
|
172
|
+
}
|
|
173
|
+
function printFormatProperties(nodeOrSelection) {
|
|
174
|
+
let str = FORMAT_PREDICATES.map(predicate => predicate(nodeOrSelection)).filter(Boolean).join(', ').toLocaleLowerCase();
|
|
175
|
+
if (str !== '') {
|
|
176
|
+
str = 'format: ' + str;
|
|
177
|
+
}
|
|
178
|
+
return str;
|
|
179
|
+
}
|
|
180
|
+
function printTargetProperties(node) {
|
|
181
|
+
let str = node.getTarget();
|
|
182
|
+
// TODO Fix nullish on LinkNode
|
|
183
|
+
if (str != null) {
|
|
184
|
+
str = 'target: ' + str;
|
|
185
|
+
}
|
|
186
|
+
return str;
|
|
187
|
+
}
|
|
188
|
+
function printRelProperties(node) {
|
|
189
|
+
let str = node.getRel();
|
|
190
|
+
// TODO Fix nullish on LinkNode
|
|
191
|
+
if (str != null) {
|
|
192
|
+
str = 'rel: ' + str;
|
|
193
|
+
}
|
|
194
|
+
return str;
|
|
195
|
+
}
|
|
196
|
+
function printTitleProperties(node) {
|
|
197
|
+
let str = node.getTitle();
|
|
198
|
+
// TODO Fix nullish on LinkNode
|
|
199
|
+
if (str != null) {
|
|
200
|
+
str = 'title: ' + str;
|
|
201
|
+
}
|
|
202
|
+
return str;
|
|
203
|
+
}
|
|
204
|
+
function printSelectedCharsLine({
|
|
205
|
+
indent,
|
|
206
|
+
isSelected,
|
|
207
|
+
node,
|
|
208
|
+
nodeKeyDisplay,
|
|
209
|
+
selection,
|
|
210
|
+
typeDisplay
|
|
211
|
+
}) {
|
|
212
|
+
// No selection or node is not selected.
|
|
213
|
+
if (!$isTextNode(node) || !$isRangeSelection(selection) || !isSelected || $isElementNode(node)) {
|
|
214
|
+
return '';
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// No selected characters.
|
|
218
|
+
const anchor = selection.anchor;
|
|
219
|
+
const focus = selection.focus;
|
|
220
|
+
if (node.getTextContent() === '' || anchor.getNode() === selection.focus.getNode() && anchor.offset === focus.offset) {
|
|
221
|
+
return '';
|
|
222
|
+
}
|
|
223
|
+
const [start, end] = $getSelectionStartEnd(node, selection);
|
|
224
|
+
if (start === end) {
|
|
225
|
+
return '';
|
|
226
|
+
}
|
|
227
|
+
const selectionLastIndent = indent[indent.length - 1] === SYMBOLS.hasNextSibling ? SYMBOLS.ancestorHasNextSibling : SYMBOLS.ancestorIsLastChild;
|
|
228
|
+
const indentionChars = [...indent.slice(0, indent.length - 1), selectionLastIndent];
|
|
229
|
+
const unselectedChars = Array(start + 1).fill(' ');
|
|
230
|
+
const selectedChars = Array(end - start).fill(SYMBOLS.selectedChar);
|
|
231
|
+
const paddingLength = typeDisplay.length + 3; // 2 for the spaces around + 1 for the double quote.
|
|
232
|
+
|
|
233
|
+
const nodePrintSpaces = Array(nodeKeyDisplay.length + paddingLength).fill(' ');
|
|
234
|
+
return [SYMBOLS.selectedLine, indentionChars.join(' '), [...nodePrintSpaces, ...unselectedChars, ...selectedChars].join('')].join(' ') + '\n';
|
|
235
|
+
}
|
|
236
|
+
function printPrettyHTML(str) {
|
|
237
|
+
const div = document.createElement('div');
|
|
238
|
+
div.innerHTML = str.trim();
|
|
239
|
+
return prettifyHTML(div, 0).innerHTML;
|
|
240
|
+
}
|
|
241
|
+
function prettifyHTML(node, level) {
|
|
242
|
+
const indentBefore = new Array(level++ + 1).join(' ');
|
|
243
|
+
const indentAfter = new Array(level - 1).join(' ');
|
|
244
|
+
let textNode;
|
|
245
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
246
|
+
textNode = document.createTextNode('\n' + indentBefore);
|
|
247
|
+
node.insertBefore(textNode, node.children[i]);
|
|
248
|
+
prettifyHTML(node.children[i], level);
|
|
249
|
+
if (node.lastElementChild === node.children[i]) {
|
|
250
|
+
textNode = document.createTextNode('\n' + indentAfter);
|
|
251
|
+
node.appendChild(textNode);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return node;
|
|
255
|
+
}
|
|
256
|
+
function $getSelectionStartEnd(node, selection) {
|
|
257
|
+
const anchorAndFocus = selection.getStartEndPoints();
|
|
258
|
+
if ($isNodeSelection(selection) || anchorAndFocus === null) {
|
|
259
|
+
return [-1, -1];
|
|
260
|
+
}
|
|
261
|
+
const [anchor, focus] = anchorAndFocus;
|
|
262
|
+
const textContent = node.getTextContent();
|
|
263
|
+
const textLength = textContent.length;
|
|
264
|
+
let start = -1;
|
|
265
|
+
let end = -1;
|
|
266
|
+
|
|
267
|
+
// Only one node is being selected.
|
|
268
|
+
if (anchor.type === 'text' && focus.type === 'text') {
|
|
269
|
+
const anchorNode = anchor.getNode();
|
|
270
|
+
const focusNode = focus.getNode();
|
|
271
|
+
if (anchorNode === focusNode && node === anchorNode && anchor.offset !== focus.offset) {
|
|
272
|
+
[start, end] = anchor.offset < focus.offset ? [anchor.offset, focus.offset] : [focus.offset, anchor.offset];
|
|
273
|
+
} else if (node === anchorNode) {
|
|
274
|
+
[start, end] = anchorNode.isBefore(focusNode) ? [anchor.offset, textLength] : [0, anchor.offset];
|
|
275
|
+
} else if (node === focusNode) {
|
|
276
|
+
[start, end] = focusNode.isBefore(anchorNode) ? [focus.offset, textLength] : [0, focus.offset];
|
|
277
|
+
} else {
|
|
278
|
+
// Node is within selection but not the anchor nor focus.
|
|
279
|
+
[start, end] = [0, textLength];
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Account for non-single width characters.
|
|
284
|
+
const numNonSingleWidthCharBeforeSelection = (textContent.slice(0, start).match(NON_SINGLE_WIDTH_CHARS_REGEX) || []).length;
|
|
285
|
+
const numNonSingleWidthCharInSelection = (textContent.slice(start, end).match(NON_SINGLE_WIDTH_CHARS_REGEX) || []).length;
|
|
286
|
+
return [start + numNonSingleWidthCharBeforeSelection, end + numNonSingleWidthCharBeforeSelection + numNonSingleWidthCharInSelection];
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
291
|
+
*
|
|
292
|
+
* This source code is licensed under the MIT license found in the
|
|
293
|
+
* LICENSE file in the root directory of this source tree.
|
|
294
|
+
*
|
|
295
|
+
*/
|
|
296
|
+
const LARGE_EDITOR_STATE_SIZE = 1000;
|
|
297
|
+
const TreeView = /*#__PURE__*/forwardRef(function TreeViewWrapped({
|
|
298
|
+
treeTypeButtonClassName,
|
|
299
|
+
timeTravelButtonClassName,
|
|
300
|
+
timeTravelPanelSliderClassName,
|
|
301
|
+
timeTravelPanelButtonClassName,
|
|
302
|
+
viewClassName,
|
|
303
|
+
timeTravelPanelClassName,
|
|
304
|
+
editorState,
|
|
305
|
+
setEditorState,
|
|
306
|
+
setEditorReadOnly,
|
|
307
|
+
generateContent
|
|
308
|
+
}, ref) {
|
|
309
|
+
const [timeStampedEditorStates, setTimeStampedEditorStates] = useState([]);
|
|
310
|
+
const [content, setContent] = useState('');
|
|
311
|
+
const [timeTravelEnabled, setTimeTravelEnabled] = useState(false);
|
|
312
|
+
const [showExportDOM, setShowExportDOM] = useState(false);
|
|
313
|
+
const playingIndexRef = useRef(0);
|
|
314
|
+
const inputRef = useRef(null);
|
|
315
|
+
const [isPlaying, setIsPlaying] = useState(false);
|
|
316
|
+
const [isLimited, setIsLimited] = useState(false);
|
|
317
|
+
const [showLimited, setShowLimited] = useState(false);
|
|
318
|
+
const lastEditorStateRef = useRef();
|
|
319
|
+
const lastGenerationID = useRef(0);
|
|
320
|
+
const generateTree = useCallback(exportDOM => {
|
|
321
|
+
const myID = ++lastGenerationID.current;
|
|
322
|
+
generateContent(exportDOM).then(treeText => {
|
|
323
|
+
if (myID === lastGenerationID.current) {
|
|
324
|
+
setContent(treeText);
|
|
325
|
+
}
|
|
326
|
+
}).catch(err => {
|
|
327
|
+
if (myID === lastGenerationID.current) {
|
|
328
|
+
setContent(`Error rendering tree: ${err.message}\n\nStack:\n${err.stack}`);
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
}, [generateContent]);
|
|
332
|
+
useEffect(() => {
|
|
333
|
+
if (!showLimited && editorState._nodeMap.size > LARGE_EDITOR_STATE_SIZE) {
|
|
334
|
+
setIsLimited(true);
|
|
335
|
+
if (!showLimited) {
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Prevent re-rendering if the editor state hasn't changed
|
|
341
|
+
if (lastEditorStateRef.current !== editorState) {
|
|
342
|
+
lastEditorStateRef.current = editorState;
|
|
343
|
+
generateTree(showExportDOM);
|
|
344
|
+
if (!timeTravelEnabled) {
|
|
345
|
+
setTimeStampedEditorStates(currentEditorStates => [...currentEditorStates, [Date.now(), editorState]]);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}, [editorState, generateTree, showExportDOM, showLimited, timeTravelEnabled]);
|
|
349
|
+
const totalEditorStates = timeStampedEditorStates.length;
|
|
350
|
+
useEffect(() => {
|
|
351
|
+
if (isPlaying) {
|
|
352
|
+
let timeoutId;
|
|
353
|
+
const play = () => {
|
|
354
|
+
const currentIndex = playingIndexRef.current;
|
|
355
|
+
if (currentIndex === totalEditorStates - 1) {
|
|
356
|
+
setIsPlaying(false);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
const currentTime = timeStampedEditorStates[currentIndex][0];
|
|
360
|
+
const nextTime = timeStampedEditorStates[currentIndex + 1][0];
|
|
361
|
+
const timeDiff = nextTime - currentTime;
|
|
362
|
+
timeoutId = setTimeout(() => {
|
|
363
|
+
playingIndexRef.current++;
|
|
364
|
+
const index = playingIndexRef.current;
|
|
365
|
+
const input = inputRef.current;
|
|
366
|
+
if (input !== null) {
|
|
367
|
+
input.value = String(index);
|
|
368
|
+
}
|
|
369
|
+
setEditorState(timeStampedEditorStates[index][1]);
|
|
370
|
+
play();
|
|
371
|
+
}, timeDiff);
|
|
372
|
+
};
|
|
373
|
+
play();
|
|
374
|
+
return () => {
|
|
375
|
+
clearTimeout(timeoutId);
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
}, [timeStampedEditorStates, isPlaying, totalEditorStates, setEditorState]);
|
|
379
|
+
const handleExportModeToggleClick = () => {
|
|
380
|
+
generateTree(!showExportDOM);
|
|
381
|
+
setShowExportDOM(!showExportDOM);
|
|
382
|
+
};
|
|
383
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
384
|
+
className: viewClassName
|
|
385
|
+
}, !showLimited && isLimited ? /*#__PURE__*/React.createElement("div", {
|
|
386
|
+
style: {
|
|
387
|
+
padding: 20
|
|
388
|
+
}
|
|
389
|
+
}, /*#__PURE__*/React.createElement("span", {
|
|
390
|
+
style: {
|
|
391
|
+
marginRight: 20
|
|
392
|
+
}
|
|
393
|
+
}, "Detected large EditorState, this can impact debugging performance."), /*#__PURE__*/React.createElement("button", {
|
|
394
|
+
onClick: () => {
|
|
395
|
+
setShowLimited(true);
|
|
396
|
+
},
|
|
397
|
+
style: {
|
|
398
|
+
background: 'transparent',
|
|
399
|
+
border: '1px solid white',
|
|
400
|
+
color: 'white',
|
|
401
|
+
cursor: 'pointer',
|
|
402
|
+
padding: 5
|
|
403
|
+
}
|
|
404
|
+
}, "Show full tree")) : null, !showLimited ? /*#__PURE__*/React.createElement("button", {
|
|
405
|
+
onClick: () => handleExportModeToggleClick(),
|
|
406
|
+
className: treeTypeButtonClassName,
|
|
407
|
+
type: "button"
|
|
408
|
+
}, showExportDOM ? 'Tree' : 'Export DOM') : null, !timeTravelEnabled && (showLimited || !isLimited) && totalEditorStates > 2 && /*#__PURE__*/React.createElement("button", {
|
|
409
|
+
onClick: () => {
|
|
410
|
+
setEditorReadOnly(true);
|
|
411
|
+
playingIndexRef.current = totalEditorStates - 1;
|
|
412
|
+
setTimeTravelEnabled(true);
|
|
413
|
+
},
|
|
414
|
+
className: timeTravelButtonClassName,
|
|
415
|
+
type: "button"
|
|
416
|
+
}, "Time Travel"), (showLimited || !isLimited) && /*#__PURE__*/React.createElement("pre", {
|
|
417
|
+
ref: ref
|
|
418
|
+
}, content), timeTravelEnabled && (showLimited || !isLimited) && /*#__PURE__*/React.createElement("div", {
|
|
419
|
+
className: timeTravelPanelClassName
|
|
420
|
+
}, /*#__PURE__*/React.createElement("button", {
|
|
421
|
+
className: timeTravelPanelButtonClassName,
|
|
422
|
+
onClick: () => {
|
|
423
|
+
if (playingIndexRef.current === totalEditorStates - 1) {
|
|
424
|
+
playingIndexRef.current = 1;
|
|
425
|
+
}
|
|
426
|
+
setIsPlaying(!isPlaying);
|
|
427
|
+
},
|
|
428
|
+
type: "button"
|
|
429
|
+
}, isPlaying ? 'Pause' : 'Play'), /*#__PURE__*/React.createElement("input", {
|
|
430
|
+
className: timeTravelPanelSliderClassName,
|
|
431
|
+
ref: inputRef,
|
|
432
|
+
onChange: event => {
|
|
433
|
+
const editorStateIndex = Number(event.target.value);
|
|
434
|
+
const timeStampedEditorState = timeStampedEditorStates[editorStateIndex];
|
|
435
|
+
if (timeStampedEditorState) {
|
|
436
|
+
playingIndexRef.current = editorStateIndex;
|
|
437
|
+
setEditorState(timeStampedEditorState[1]);
|
|
438
|
+
}
|
|
439
|
+
},
|
|
440
|
+
type: "range",
|
|
441
|
+
min: "1",
|
|
442
|
+
max: totalEditorStates - 1
|
|
443
|
+
}), /*#__PURE__*/React.createElement("button", {
|
|
444
|
+
className: timeTravelPanelButtonClassName,
|
|
445
|
+
onClick: () => {
|
|
446
|
+
setEditorReadOnly(false);
|
|
447
|
+
const index = timeStampedEditorStates.length - 1;
|
|
448
|
+
const timeStampedEditorState = timeStampedEditorStates[index];
|
|
449
|
+
setEditorState(timeStampedEditorState[1]);
|
|
450
|
+
const input = inputRef.current;
|
|
451
|
+
if (input !== null) {
|
|
452
|
+
input.value = String(index);
|
|
453
|
+
}
|
|
454
|
+
setTimeTravelEnabled(false);
|
|
455
|
+
setIsPlaying(false);
|
|
456
|
+
},
|
|
457
|
+
type: "button"
|
|
458
|
+
}, "Exit")));
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
463
|
+
*
|
|
464
|
+
* This source code is licensed under the MIT license found in the
|
|
465
|
+
* LICENSE file in the root directory of this source tree.
|
|
466
|
+
*
|
|
467
|
+
*/
|
|
468
|
+
function registerLexicalCommandLogger(editor, setLoggedCommands) {
|
|
469
|
+
const unregisterCommandListeners = new Set();
|
|
470
|
+
for (const [command] of editor._commands) {
|
|
471
|
+
unregisterCommandListeners.add(editor.registerCommand(command, payload => {
|
|
472
|
+
setLoggedCommands(state => {
|
|
473
|
+
const newState = [...state];
|
|
474
|
+
newState.push({
|
|
475
|
+
payload,
|
|
476
|
+
type: command.type ? command.type : 'UNKNOWN'
|
|
477
|
+
});
|
|
478
|
+
if (newState.length > 10) {
|
|
479
|
+
newState.shift();
|
|
480
|
+
}
|
|
481
|
+
return newState;
|
|
482
|
+
});
|
|
483
|
+
return false;
|
|
484
|
+
}, COMMAND_PRIORITY_CRITICAL));
|
|
485
|
+
}
|
|
486
|
+
return () => unregisterCommandListeners.forEach(unregister => unregister());
|
|
487
|
+
}
|
|
488
|
+
function useLexicalCommandsLog(editor) {
|
|
489
|
+
const [loggedCommands, setLoggedCommands] = useState([]);
|
|
490
|
+
useEffect(() => {
|
|
491
|
+
return registerLexicalCommandLogger(editor, setLoggedCommands);
|
|
492
|
+
}, [editor]);
|
|
493
|
+
return useMemo(() => loggedCommands, [loggedCommands]);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
export { TreeView, generateContent, registerLexicalCommandLogger, useLexicalCommandsLog };
|
|
@@ -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 LexicalDevtoolsCore = process.env.NODE_ENV === 'development' ? require('./LexicalDevtoolsCore.dev.js') : require('./LexicalDevtoolsCore.prod.js');
|
|
9
|
+
module.exports = LexicalDevtoolsCore;
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
import * as modDev from './LexicalDevtoolsCore.dev.mjs';
|
|
8
|
+
import * as modProd from './LexicalDevtoolsCore.prod.mjs';
|
|
9
|
+
const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
|
|
10
|
+
export const TreeView = mod.TreeView;
|
|
11
|
+
export const generateContent = mod.generateContent;
|
|
12
|
+
export const registerLexicalCommandLogger = mod.registerLexicalCommandLogger;
|
|
13
|
+
export const useLexicalCommandsLog = mod.useLexicalCommandsLog;
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
const mod = await (process.env.NODE_ENV === 'development' ? import('./LexicalDevtoolsCore.dev.mjs') : import('./LexicalDevtoolsCore.prod.mjs'));
|
|
8
|
+
export const TreeView = mod.TreeView;
|
|
9
|
+
export const generateContent = mod.generateContent;
|
|
10
|
+
export const registerLexicalCommandLogger = mod.registerLexicalCommandLogger;
|
|
11
|
+
export const useLexicalCommandsLog = mod.useLexicalCommandsLog;
|
|
@@ -0,0 +1,25 @@
|
|
|
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 e=require("@lexical/html"),q=require("@lexical/link"),u=require("@lexical/mark"),K=require("@lexical/table"),L=require("lexical"),M=require("react");
|
|
8
|
+
let N=Object.freeze({"\t":"\\t","\n":"\\n"}),O=new RegExp(Object.keys(N).join("|"),"g"),P=Object.freeze({ancestorHasNextSibling:"|",ancestorIsLastChild:" ",hasNextSibling:"\u251c",isLastChild:"\u2514",selectedChar:"^",selectedLine:">"}),U=[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")&&
|
|
9
|
+
"Underline"],aa=[a=>a.hasTextFormat("bold")&&"Bold",a=>a.hasTextFormat("code")&&"Code",a=>a.hasTextFormat("italic")&&"Italic",a=>a.hasTextFormat("strikethrough")&&"Strikethrough",a=>a.hasTextFormat("subscript")&&"Subscript",a=>a.hasTextFormat("superscript")&&"Superscript",a=>a.hasTextFormat("underline")&&"Underline"],ba=[a=>a.isDirectionless()&&"Directionless",a=>a.isUnmergeable()&&"Unmergeable"],ca=[a=>a.isToken()&&"Token",a=>a.isSegmented()&&"Segmented"];
|
|
10
|
+
function da(a){let b="";var c=V(a);b+=`: range ${""!==c?`{ ${c} }`:""} ${""!==a.style?`{ style: ${a.style} } `:""}`;c=a.anchor;a=a.focus;let f=c.offset,d=a.offset;b+=`\n \u251c anchor { key: ${c.key}, offset: ${null===f?"null":f}, type: ${c.type} }`;return b+=`\n \u2514 focus { key: ${a.key}, offset: ${null===d?"null":d}, type: ${a.type} }`}function ea(a){return L.$isNodeSelection(a)?`: node\n \u2514 [${Array.from(a._nodes).join(", ")}]`:""}
|
|
11
|
+
function W(a,b,c=[]){a=a.getChildren();let f=a.length;a.forEach((d,k)=>{b(d,c.concat(k===f-1?P.isLastChild:P.hasNextSibling));L.$isElementNode(d)&&W(d,b,c.concat(k===f-1?P.ancestorIsLastChild:P.ancestorHasNextSibling))})}function X(a){return Object.entries(N).reduce((b,[c,f])=>b.replace(new RegExp(c,"g"),String(f)),a)}
|
|
12
|
+
function fa(a){if(L.$isTextNode(a)){var b=a.getTextContent();b=0===b.length?"(empty)":`"${X(b)}"`;a=[V(a),ha(a),ia(a)].filter(Boolean).join(", ");return[b,0!==a.length?`{ ${a} }`:null].filter(Boolean).join(" ").trim()}if(q.$isLinkNode(a)){b=a.getURL();b=0===b.length?"(empty)":`"${X(b)}"`;var c=a.getTarget();null!=c&&(c="target: "+c);var f=Boolean;var d=a.getRel();null!=d&&(d="rel: "+d);a=a.getTitle();null!=a&&(a="title: "+a);a=[c,d,a].filter(f).join(", ");return[b,0!==a.length?`{ ${a} }`:null].filter(Boolean).join(" ").trim()}return L.$isParagraphNode(a)?
|
|
13
|
+
(a=ja(a),""!==a?`{ ${a} }`:""):""}function ja(a){let b=aa.map(c=>c(a)).filter(Boolean).join(", ").toLocaleLowerCase();""!==b&&(b="format: "+b);return b}function ha(a){let b=ba.map(c=>c(a)).filter(Boolean).join(", ").toLocaleLowerCase();""!==b&&(b="detail: "+b);return b}function ia(a){let b=ca.map(c=>c(a)).filter(Boolean).join(", ").toLocaleLowerCase();""!==b&&(b="mode: "+b);return b}
|
|
14
|
+
function V(a){let b=U.map(c=>c(a)).filter(Boolean).join(", ").toLocaleLowerCase();""!==b&&(b="format: "+b);return b}
|
|
15
|
+
function ka({indent:a,isSelected:b,node:c,nodeKeyDisplay:f,selection:d,typeDisplay:k}){if(!L.$isTextNode(c)||!L.$isRangeSelection(d)||!b||L.$isElementNode(c))return"";b=d.anchor;var m=d.focus;if(""===c.getTextContent()||b.getNode()===d.focus.getNode()&&b.offset===m.offset)return"";b=d.getStartEndPoints();if(L.$isNodeSelection(d)||null===b)c=[-1,-1];else{var [h,g]=b;m=c.getTextContent();var l=m.length;b=d=-1;if("text"===h.type&&"text"===g.type){let v=h.getNode(),w=g.getNode();v===w&&c===v&&h.offset!==
|
|
16
|
+
g.offset?[d,b]=h.offset<g.offset?[h.offset,g.offset]:[g.offset,h.offset]:c===v?[d,b]=v.isBefore(w)?[h.offset,l]:[0,h.offset]:c===w?[d,b]=w.isBefore(v)?[g.offset,l]:[0,g.offset]:[d,b]=[0,l]}c=(m.slice(0,d).match(O)||[]).length;m=(m.slice(d,b).match(O)||[]).length;c=[d+c,b+c+m]}let [r,n]=c;if(r===n)return"";c=a[a.length-1]===P.hasNextSibling?P.ancestorHasNextSibling:P.ancestorIsLastChild;a=[...a.slice(0,a.length-1),c];c=Array(r+1).fill(" ");d=Array(n-r).fill(P.selectedChar);f=Array(f.length+(k.length+
|
|
17
|
+
3)).fill(" ");return[P.selectedLine,a.join(" "),[...f,...c,...d].join("")].join(" ")+"\n"}function Y(a,b){let c=Array(b++ +1).join(" "),f=Array(b-1).join(" "),d;for(let k=0;k<a.children.length;k++)d=document.createTextNode("\n"+c),a.insertBefore(d,a.children[k]),Y(a.children[k],b),a.lastElementChild===a.children[k]&&(d=document.createTextNode("\n"+f),a.appendChild(d));return a}
|
|
18
|
+
let oa=M.forwardRef(function({treeTypeButtonClassName:a,timeTravelButtonClassName:b,timeTravelPanelSliderClassName:c,timeTravelPanelButtonClassName:f,viewClassName:d,timeTravelPanelClassName:k,editorState:m,setEditorState:h,setEditorReadOnly:g,generateContent:l},r){const [n,v]=M.useState([]),[w,C]=M.useState(""),[D,Q]=M.useState(!1),[B,la]=M.useState(!1),z=M.useRef(0),G=M.useRef(null),[E,H]=M.useState(!1),[F,ma]=M.useState(!1),[x,na]=M.useState(!1),R=M.useRef(),I=M.useRef(0),J=M.useCallback(p=>{const t=
|
|
19
|
+
++I.current;l(p).then(y=>{t===I.current&&C(y)}).catch(y=>{t===I.current&&C(`Error rendering tree: ${y.message}\n\nStack:\n${y.stack}`)})},[l]);M.useEffect(()=>{if(!x&&1E3<m._nodeMap.size&&(ma(!0),!x))return;R.current!==m&&(R.current=m,J(B),D||v(p=>[...p,[Date.now(),m]]))},[m,J,B,x,D]);const A=n.length;M.useEffect(()=>{if(E){let p;const t=()=>{const y=z.current;y===A-1?H(!1):p=setTimeout(()=>{z.current++;const S=z.current,T=G.current;null!==T&&(T.value=String(S));h(n[S][1]);t()},n[y+1][0]-n[y][0])};
|
|
20
|
+
t();return()=>{clearTimeout(p)}}},[n,E,A,h]);return M.createElement("div",{className:d},!x&&F?M.createElement("div",{style:{padding:20}},M.createElement("span",{style:{marginRight:20}},"Detected large EditorState, this can impact debugging performance."),M.createElement("button",{onClick:()=>{na(!0)},style:{background:"transparent",border:"1px solid white",color:"white",cursor:"pointer",padding:5}},"Show full tree")):null,x?null:M.createElement("button",{onClick:()=>{J(!B);la(!B)},className:a,type:"button"},
|
|
21
|
+
B?"Tree":"Export DOM"),!D&&(x||!F)&&2<A&&M.createElement("button",{onClick:()=>{g(!0);z.current=A-1;Q(!0)},className:b,type:"button"},"Time Travel"),(x||!F)&&M.createElement("pre",{ref:r},w),D&&(x||!F)&&M.createElement("div",{className:k},M.createElement("button",{className:f,onClick:()=>{z.current===A-1&&(z.current=1);H(!E)},type:"button"},E?"Pause":"Play"),M.createElement("input",{className:c,ref:G,onChange:p=>{p=Number(p.target.value);const t=n[p];t&&(z.current=p,h(t[1]))},type:"range",min:"1",
|
|
22
|
+
max:A-1}),M.createElement("button",{className:f,onClick:()=>{g(!1);const p=n.length-1;h(n[p][1]);const t=G.current;null!==t&&(t.value=String(p));Q(!1);H(!1)},type:"button"},"Exit")))});function Z(a,b){let c=new Set;for(let [f]of a._commands)c.add(a.registerCommand(f,d=>{b(k=>{k=[...k];k.push({payload:d,type:f.type?f.type:"UNKNOWN"});10<k.length&&k.shift();return k});return!1},L.COMMAND_PRIORITY_CRITICAL));return()=>c.forEach(f=>f())}exports.TreeView=oa;
|
|
23
|
+
exports.generateContent=function(a,b,c){let f=a.getEditorState(),d=a._config,k=a._compositionKey,m=a._editable;if(c){let g="";f.read(()=>{var l=e.$generateHtmlFromNodes(a);let r=document.createElement("div");r.innerHTML=l.trim();g=Y(r,0).innerHTML});return g}let h=" root\n";c=f.read(()=>{const g=L.$getSelection();W(L.$getRoot(),(l,r)=>{const n=`(${l.getKey()})`,v=l.getType()||"",w=l.isSelected(),C=u.$isMarkNode(l)?` id: [ ${l.getIDs().join(", ")} ] `:"";h+=`${w?P.selectedLine:" "} ${r.join(" ")} ${n} ${v} ${C} ${fa(l)}\n`;
|
|
24
|
+
h+=ka({indent:r,isSelected:w,node:l,nodeKeyDisplay:n,selection:g,typeDisplay:v})});return null===g?": null":L.$isRangeSelection(g)?da(g):K.$isTableSelection(g)?`: table\n \u2514 { table: ${g.tableKey}, anchorCell: ${g.anchor.key}, focusCell: ${g.focus.key} }`:ea(g)});h+="\n selection"+c;h+="\n\n commands:";if(b.length)for(let {type:g,payload:l}of b)h+=`\n \u2514 { type: ${g}, payload: ${l instanceof Event?l.constructor.name:l} }`;else h+="\n \u2514 None dispatched.";h+="\n\n editor:";h+=`\n \u2514 namespace ${d.namespace}`;
|
|
25
|
+
null!==k&&(h+=`\n \u2514 compositionKey ${k}`);return h+=`\n \u2514 editable ${String(m)}`};exports.registerLexicalCommandLogger=Z;exports.useLexicalCommandsLog=function(a){let [b,c]=M.useState([]);M.useEffect(()=>Z(a,c),[a]);return M.useMemo(()=>b,[b])}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import{$generateHtmlFromNodes as e}from"@lexical/html";import{$isLinkNode as t}from"@lexical/link";import{$isMarkNode as n}from"@lexical/mark";import{$isTableSelection as r}from"@lexical/table";import{$getSelection as o,$getRoot as l,$isRangeSelection as i,$isNodeSelection as a,$isElementNode as s,$isTextNode as c,$isParagraphNode as u,COMMAND_PRIORITY_CRITICAL as f}from"lexical";import*as m from"react";import{forwardRef as d,useState as h,useRef as g,useCallback as p,useEffect as y,useMemo as b}from"react";const C=Object.freeze({"\t":"\\t","\n":"\\n"}),$=new RegExp(Object.keys(C).join("|"),"g"),x=Object.freeze({ancestorHasNextSibling:"|",ancestorIsLastChild:" ",hasNextSibling:"├",isLastChild:"└",selectedChar:"^",selectedLine:">"}),N=[e=>e.hasFormat("bold")&&"Bold",e=>e.hasFormat("code")&&"Code",e=>e.hasFormat("italic")&&"Italic",e=>e.hasFormat("strikethrough")&&"Strikethrough",e=>e.hasFormat("subscript")&&"Subscript",e=>e.hasFormat("superscript")&&"Superscript",e=>e.hasFormat("underline")&&"Underline"],S=[e=>e.hasTextFormat("bold")&&"Bold",e=>e.hasTextFormat("code")&&"Code",e=>e.hasTextFormat("italic")&&"Italic",e=>e.hasTextFormat("strikethrough")&&"Strikethrough",e=>e.hasTextFormat("subscript")&&"Subscript",e=>e.hasTextFormat("superscript")&&"Superscript",e=>e.hasTextFormat("underline")&&"Underline"],T=[e=>e.isDirectionless()&&"Directionless",e=>e.isUnmergeable()&&"Unmergeable"],E=[e=>e.isToken()&&"Token",e=>e.isSegmented()&&"Segmented"];function k(f,m,d){const h=f.getEditorState(),g=f._config,p=f._compositionKey,y=f._editable;if(d){let t="";return h.read((()=>{t=function(e){const t=document.createElement("div");return t.innerHTML=e.trim(),O(t,0).innerHTML}(e(f))})),t}let b=" root\n";const C=h.read((()=>{const e=o();return j(l(),((r,o)=>{const l=`(${r.getKey()})`,f=r.getType()||"",m=r.isSelected(),d=n(r)?` id: [ ${r.getIDs().join(", ")} ] `:"";b+=`${m?x.selectedLine:" "} ${o.join(" ")} ${l} ${f} ${d} ${function(e){if(c(e)){const t=e.getTextContent(),n=0===t.length?"(empty)":`"${L(t)}"`,r=function(e){return[w(e),v(e),B(e)].filter(Boolean).join(", ")}(e);return[n,0!==r.length?`{ ${r} }`:null].filter(Boolean).join(" ").trim()}if(t(e)){const t=e.getURL(),n=0===t.length?"(empty)":`"${L(t)}"`,r=function(e){return[F(e),D(e),K(e)].filter(Boolean).join(", ")}(e);return[n,0!==r.length?`{ ${r} }`:null].filter(Boolean).join(" ").trim()}if(u(e)){const t=function(e){let t=S.map((t=>t(e))).filter(Boolean).join(", ").toLocaleLowerCase();""!==t&&(t="format: "+t);return t}(e);return""!==t?`{ ${t} }`:""}return""}(r)}\n`,b+=function({indent:e,isSelected:t,node:n,nodeKeyDisplay:r,selection:o,typeDisplay:l}){if(!c(n)||!i(o)||!t||s(n))return"";const u=o.anchor,f=o.focus;if(""===n.getTextContent()||u.getNode()===o.focus.getNode()&&u.offset===f.offset)return"";const[m,d]=function(e,t){const n=t.getStartEndPoints();if(a(t)||null===n)return[-1,-1];const[r,o]=n,l=e.getTextContent(),i=l.length;let s=-1,c=-1;if("text"===r.type&&"text"===o.type){const t=r.getNode(),n=o.getNode();t===n&&e===t&&r.offset!==o.offset?[s,c]=r.offset<o.offset?[r.offset,o.offset]:[o.offset,r.offset]:[s,c]=e===t?t.isBefore(n)?[r.offset,i]:[0,r.offset]:e===n?n.isBefore(t)?[o.offset,i]:[0,o.offset]:[0,i]}const u=(l.slice(0,s).match($)||[]).length,f=(l.slice(s,c).match($)||[]).length;return[s+u,c+u+f]}(n,o);if(m===d)return"";const h=e[e.length-1]===x.hasNextSibling?x.ancestorHasNextSibling:x.ancestorIsLastChild,g=[...e.slice(0,e.length-1),h],p=Array(m+1).fill(" "),y=Array(d-m).fill(x.selectedChar),b=l.length+3,C=Array(r.length+b).fill(" ");return[x.selectedLine,g.join(" "),[...C,...p,...y].join("")].join(" ")+"\n"}({indent:o,isSelected:m,node:r,nodeKeyDisplay:l,selection:e,typeDisplay:f})})),null===e?": null":i(e)?function(e){let t="";const n=w(e);t+=`: range ${""!==n?`{ ${n} }`:""} ${""!==e.style?`{ style: ${e.style} } `:""}`;const r=e.anchor,o=e.focus,l=r.offset,i=o.offset;return t+=`\n ├ anchor { key: ${r.key}, offset: ${null===l?"null":l}, type: ${r.type} }`,t+=`\n └ focus { key: ${o.key}, offset: ${null===i?"null":i}, type: ${o.type} }`,t}(e):r(e)?function(e){return`: table\n └ { table: ${e.tableKey}, anchorCell: ${e.anchor.key}, focusCell: ${e.focus.key} }`}(e):function(e){if(!a(e))return"";return`: node\n └ [${Array.from(e._nodes).join(", ")}]`}(e)}));if(b+="\n selection"+C,b+="\n\n commands:",m.length)for(const{type:e,payload:t}of m)b+=`\n └ { type: ${e}, payload: ${t instanceof Event?t.constructor.name:t} }`;else b+="\n └ None dispatched.";return b+="\n\n editor:",b+=`\n └ namespace ${g.namespace}`,null!==p&&(b+=`\n └ compositionKey ${p}`),b+=`\n └ editable ${String(y)}`,b}function j(e,t,n=[]){const r=e.getChildren(),o=r.length;r.forEach(((e,r)=>{t(e,n.concat(r===o-1?x.isLastChild:x.hasNextSibling)),s(e)&&j(e,t,n.concat(r===o-1?x.ancestorIsLastChild:x.ancestorHasNextSibling))}))}function L(e){return Object.entries(C).reduce(((e,[t,n])=>e.replace(new RegExp(t,"g"),String(n))),e)}function v(e){let t=T.map((t=>t(e))).filter(Boolean).join(", ").toLocaleLowerCase();return""!==t&&(t="detail: "+t),t}function B(e){let t=E.map((t=>t(e))).filter(Boolean).join(", ").toLocaleLowerCase();return""!==t&&(t="mode: "+t),t}function w(e){let t=N.map((t=>t(e))).filter(Boolean).join(", ").toLocaleLowerCase();return""!==t&&(t="format: "+t),t}function F(e){let t=e.getTarget();return null!=t&&(t="target: "+t),t}function D(e){let t=e.getRel();return null!=t&&(t="rel: "+t),t}function K(e){let t=e.getTitle();return null!=t&&(t="title: "+t),t}function O(e,t){const n=new Array(1+t++).join(" "),r=new Array(t-1).join(" ");let o;for(let l=0;l<e.children.length;l++)o=document.createTextNode("\n"+n),e.insertBefore(o,e.children[l]),O(e.children[l],t),e.lastElementChild===e.children[l]&&(o=document.createTextNode("\n"+r),e.appendChild(o));return e}const A=d((function({treeTypeButtonClassName:e,timeTravelButtonClassName:t,timeTravelPanelSliderClassName:n,timeTravelPanelButtonClassName:r,viewClassName:o,timeTravelPanelClassName:l,editorState:i,setEditorState:a,setEditorReadOnly:s,generateContent:c},u){const[f,d]=h([]),[b,C]=h(""),[$,x]=h(!1),[N,S]=h(!1),T=g(0),E=g(null),[k,j]=h(!1),[L,v]=h(!1),[B,w]=h(!1),F=g(),D=g(0),K=p((e=>{const t=++D.current;c(e).then((e=>{t===D.current&&C(e)})).catch((e=>{t===D.current&&C(`Error rendering tree: ${e.message}\n\nStack:\n${e.stack}`)}))}),[c]);y((()=>{!B&&i._nodeMap.size>1e3&&(v(!0),!B)||F.current!==i&&(F.current=i,K(N),$||d((e=>[...e,[Date.now(),i]])))}),[i,K,N,B,$]);const O=f.length;y((()=>{if(k){let e;const t=()=>{const n=T.current;if(n===O-1)return void j(!1);const r=f[n][0],o=f[n+1][0];e=setTimeout((()=>{T.current++;const e=T.current,n=E.current;null!==n&&(n.value=String(e)),a(f[e][1]),t()}),o-r)};return t(),()=>{clearTimeout(e)}}}),[f,k,O,a]);return m.createElement("div",{className:o},!B&&L?m.createElement("div",{style:{padding:20}},m.createElement("span",{style:{marginRight:20}},"Detected large EditorState, this can impact debugging performance."),m.createElement("button",{onClick:()=>{w(!0)},style:{background:"transparent",border:"1px solid white",color:"white",cursor:"pointer",padding:5}},"Show full tree")):null,B?null:m.createElement("button",{onClick:()=>(K(!N),void S(!N)),className:e,type:"button"},N?"Tree":"Export DOM"),!$&&(B||!L)&&O>2&&m.createElement("button",{onClick:()=>{s(!0),T.current=O-1,x(!0)},className:t,type:"button"},"Time Travel"),(B||!L)&&m.createElement("pre",{ref:u},b),$&&(B||!L)&&m.createElement("div",{className:l},m.createElement("button",{className:r,onClick:()=>{T.current===O-1&&(T.current=1),j(!k)},type:"button"},k?"Pause":"Play"),m.createElement("input",{className:n,ref:E,onChange:e=>{const t=Number(e.target.value),n=f[t];n&&(T.current=t,a(n[1]))},type:"range",min:"1",max:O-1}),m.createElement("button",{className:r,onClick:()=>{s(!1);const e=f.length-1,t=f[e];a(t[1]);const n=E.current;null!==n&&(n.value=String(e)),x(!1),j(!1)},type:"button"},"Exit")))}));function I(e,t){const n=new Set;for(const[r]of e._commands)n.add(e.registerCommand(r,(e=>(t((t=>{const n=[...t];return n.push({payload:e,type:r.type?r.type:"UNKNOWN"}),n.length>10&&n.shift(),n})),!1)),f));return()=>n.forEach((e=>e()))}function P(e){const[t,n]=h([]);return y((()=>I(e,n)),[e]),b((()=>t),[t])}export{A as TreeView,k as generateContent,I as registerLexicalCommandLogger,P as useLexicalCommandsLog};
|
package/README.md
ADDED
package/TreeView.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
*/
|
|
8
|
+
import type { EditorSetOptions, EditorState } from 'lexical';
|
|
9
|
+
import * as React from 'react';
|
|
10
|
+
export declare const TreeView: React.ForwardRefExoticComponent<{
|
|
11
|
+
editorState: EditorState;
|
|
12
|
+
treeTypeButtonClassName: string;
|
|
13
|
+
timeTravelButtonClassName: string;
|
|
14
|
+
timeTravelPanelButtonClassName: string;
|
|
15
|
+
timeTravelPanelClassName: string;
|
|
16
|
+
timeTravelPanelSliderClassName: string;
|
|
17
|
+
viewClassName: string;
|
|
18
|
+
generateContent: (exportDOM: boolean) => Promise<string>;
|
|
19
|
+
setEditorState: (state: EditorState, options?: EditorSetOptions) => void;
|
|
20
|
+
setEditorReadOnly: (isReadonly: boolean) => void;
|
|
21
|
+
} & React.RefAttributes<HTMLPreElement>>;
|
|
@@ -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
|
+
*/
|
|
8
|
+
import type { LexicalEditor } from 'lexical';
|
|
9
|
+
import { LexicalCommand } from 'lexical';
|
|
10
|
+
export declare function generateContent(editor: LexicalEditor, commandsLog: ReadonlyArray<LexicalCommand<unknown> & {
|
|
11
|
+
payload: unknown;
|
|
12
|
+
}>, exportDOM: boolean): string;
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
export * from './generateContent';
|
|
9
|
+
export * from './TreeView';
|
|
10
|
+
export * from './useLexicalCommandsLog';
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lexical/devtools-core",
|
|
3
|
+
"description": "This package contains tools necessary to debug and develop Lexical.",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"lexical",
|
|
6
|
+
"editor",
|
|
7
|
+
"rich-text",
|
|
8
|
+
"utils"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"version": "0.14.4",
|
|
12
|
+
"main": "LexicalDevtoolsCore.js",
|
|
13
|
+
"types": "index.d.ts",
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@lexical/html": "0.14.4",
|
|
16
|
+
"@lexical/link": "0.14.4",
|
|
17
|
+
"@lexical/mark": "0.14.4",
|
|
18
|
+
"@lexical/table": "0.14.4",
|
|
19
|
+
"@lexical/utils": "0.14.4",
|
|
20
|
+
"lexical": "0.14.4"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"react": ">=17.x",
|
|
24
|
+
"react-dom": ">=17.x"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/facebook/lexical",
|
|
29
|
+
"directory": "packages/lexical-devtools-core"
|
|
30
|
+
},
|
|
31
|
+
"module": "LexicalDevtoolsCore.mjs",
|
|
32
|
+
"sideEffects": false,
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"import": {
|
|
36
|
+
"types": "./index.d.ts",
|
|
37
|
+
"development": "./LexicalDevtoolsCore.dev.mjs",
|
|
38
|
+
"production": "./LexicalDevtoolsCore.prod.mjs",
|
|
39
|
+
"node": "./LexicalDevtoolsCore.node.mjs",
|
|
40
|
+
"default": "./LexicalDevtoolsCore.mjs"
|
|
41
|
+
},
|
|
42
|
+
"require": {
|
|
43
|
+
"types": "./index.d.ts",
|
|
44
|
+
"development": "./LexicalDevtoolsCore.dev.js",
|
|
45
|
+
"production": "./LexicalDevtoolsCore.prod.js",
|
|
46
|
+
"default": "./LexicalDevtoolsCore.js"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
import type { LexicalEditor } from 'lexical';
|
|
9
|
+
import { LexicalCommand } from 'lexical';
|
|
10
|
+
export type LexicalCommandLog = ReadonlyArray<LexicalCommand<unknown> & {
|
|
11
|
+
payload: unknown;
|
|
12
|
+
}>;
|
|
13
|
+
export declare function registerLexicalCommandLogger(editor: LexicalEditor, setLoggedCommands: (v: (oldValue: LexicalCommandLog) => LexicalCommandLog) => void): () => void;
|
|
14
|
+
export declare function useLexicalCommandsLog(editor: LexicalEditor): LexicalCommandLog;
|