@lexical/devtools-core 0.44.1-nightly.20260519.0 → 0.45.1-dev.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/package.json +34 -20
- package/src/TreeView.tsx +264 -0
- package/src/generateContent.ts +576 -0
- package/src/index.ts +11 -0
- package/src/useLexicalCommandsLog.ts +61 -0
- /package/{LexicalDevtoolsCore.dev.js → dist/LexicalDevtoolsCore.dev.js} +0 -0
- /package/{LexicalDevtoolsCore.dev.mjs → dist/LexicalDevtoolsCore.dev.mjs} +0 -0
- /package/{LexicalDevtoolsCore.js → dist/LexicalDevtoolsCore.js} +0 -0
- /package/{LexicalDevtoolsCore.js.flow → dist/LexicalDevtoolsCore.js.flow} +0 -0
- /package/{LexicalDevtoolsCore.mjs → dist/LexicalDevtoolsCore.mjs} +0 -0
- /package/{LexicalDevtoolsCore.node.mjs → dist/LexicalDevtoolsCore.node.mjs} +0 -0
- /package/{LexicalDevtoolsCore.prod.js → dist/LexicalDevtoolsCore.prod.js} +0 -0
- /package/{LexicalDevtoolsCore.prod.mjs → dist/LexicalDevtoolsCore.prod.mjs} +0 -0
- /package/{TreeView.d.ts → dist/TreeView.d.ts} +0 -0
- /package/{generateContent.d.ts → dist/generateContent.d.ts} +0 -0
- /package/{index.d.ts → dist/index.d.ts} +0 -0
- /package/{useLexicalCommandsLog.d.ts → dist/useLexicalCommandsLog.d.ts} +0 -0
package/package.json
CHANGED
|
@@ -8,16 +8,16 @@
|
|
|
8
8
|
"utils"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"version": "0.
|
|
12
|
-
"main": "LexicalDevtoolsCore.js",
|
|
13
|
-
"types": "index.d.ts",
|
|
11
|
+
"version": "0.45.1-dev.0",
|
|
12
|
+
"main": "./dist/LexicalDevtoolsCore.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@lexical/html": "0.
|
|
16
|
-
"@lexical/
|
|
17
|
-
"@lexical/
|
|
18
|
-
"@lexical/table": "0.
|
|
19
|
-
"@lexical/utils": "0.
|
|
20
|
-
"lexical": "0.
|
|
15
|
+
"@lexical/html": "0.45.1-dev.0",
|
|
16
|
+
"@lexical/mark": "0.45.1-dev.0",
|
|
17
|
+
"@lexical/link": "0.45.1-dev.0",
|
|
18
|
+
"@lexical/table": "0.45.1-dev.0",
|
|
19
|
+
"@lexical/utils": "0.45.1-dev.0",
|
|
20
|
+
"lexical": "0.45.1-dev.0"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"react": ">=17.x",
|
|
@@ -28,23 +28,37 @@
|
|
|
28
28
|
"url": "git+https://github.com/facebook/lexical.git",
|
|
29
29
|
"directory": "packages/lexical-devtools-core"
|
|
30
30
|
},
|
|
31
|
-
"module": "LexicalDevtoolsCore.mjs",
|
|
31
|
+
"module": "./dist/LexicalDevtoolsCore.mjs",
|
|
32
32
|
"sideEffects": false,
|
|
33
33
|
"exports": {
|
|
34
34
|
".": {
|
|
35
|
+
"source": "./src/index.ts",
|
|
35
36
|
"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"
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"development": "./dist/LexicalDevtoolsCore.dev.mjs",
|
|
39
|
+
"production": "./dist/LexicalDevtoolsCore.prod.mjs",
|
|
40
|
+
"node": "./dist/LexicalDevtoolsCore.node.mjs",
|
|
41
|
+
"default": "./dist/LexicalDevtoolsCore.mjs"
|
|
41
42
|
},
|
|
42
43
|
"require": {
|
|
43
|
-
"types": "./index.d.ts",
|
|
44
|
-
"development": "./LexicalDevtoolsCore.dev.js",
|
|
45
|
-
"production": "./LexicalDevtoolsCore.prod.js",
|
|
46
|
-
"default": "./LexicalDevtoolsCore.js"
|
|
44
|
+
"types": "./dist/index.d.ts",
|
|
45
|
+
"development": "./dist/LexicalDevtoolsCore.dev.js",
|
|
46
|
+
"production": "./dist/LexicalDevtoolsCore.prod.js",
|
|
47
|
+
"default": "./dist/LexicalDevtoolsCore.js"
|
|
47
48
|
}
|
|
48
49
|
}
|
|
49
|
-
}
|
|
50
|
+
},
|
|
51
|
+
"files": [
|
|
52
|
+
"dist",
|
|
53
|
+
"src",
|
|
54
|
+
"!src/__tests__",
|
|
55
|
+
"!src/__bench__",
|
|
56
|
+
"!src/__mocks__",
|
|
57
|
+
"!src/**/*.test.ts",
|
|
58
|
+
"!src/**/*.test.tsx",
|
|
59
|
+
"!src/**/*.bench.ts",
|
|
60
|
+
"!src/**/*.bench.tsx",
|
|
61
|
+
"README.md",
|
|
62
|
+
"LICENSE"
|
|
63
|
+
]
|
|
50
64
|
}
|
package/src/TreeView.tsx
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
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
|
+
|
|
9
|
+
import type {LexicalCommandLog} from './useLexicalCommandsLog';
|
|
10
|
+
import type {EditorSetOptions, EditorState} from 'lexical';
|
|
11
|
+
import type {JSX} from 'react';
|
|
12
|
+
|
|
13
|
+
import * as React from 'react';
|
|
14
|
+
import {forwardRef, useCallback, useEffect, useRef, useState} from 'react';
|
|
15
|
+
|
|
16
|
+
const LARGE_EDITOR_STATE_SIZE = 1000;
|
|
17
|
+
|
|
18
|
+
export const TreeView = forwardRef<
|
|
19
|
+
HTMLPreElement,
|
|
20
|
+
{
|
|
21
|
+
editorState: EditorState;
|
|
22
|
+
treeTypeButtonClassName?: string;
|
|
23
|
+
timeTravelButtonClassName?: string;
|
|
24
|
+
timeTravelPanelButtonClassName?: string;
|
|
25
|
+
timeTravelPanelClassName?: string;
|
|
26
|
+
timeTravelPanelSliderClassName?: string;
|
|
27
|
+
viewClassName?: string;
|
|
28
|
+
generateContent: (exportDOM: boolean) => Promise<string>;
|
|
29
|
+
setEditorState: (state: EditorState, options?: EditorSetOptions) => void;
|
|
30
|
+
setEditorReadOnly: (isReadonly: boolean) => void;
|
|
31
|
+
commandsLog?: LexicalCommandLog;
|
|
32
|
+
}
|
|
33
|
+
>(function TreeViewWrapped(
|
|
34
|
+
{
|
|
35
|
+
treeTypeButtonClassName,
|
|
36
|
+
timeTravelButtonClassName,
|
|
37
|
+
timeTravelPanelSliderClassName,
|
|
38
|
+
timeTravelPanelButtonClassName,
|
|
39
|
+
viewClassName,
|
|
40
|
+
timeTravelPanelClassName,
|
|
41
|
+
editorState,
|
|
42
|
+
setEditorState,
|
|
43
|
+
setEditorReadOnly,
|
|
44
|
+
generateContent,
|
|
45
|
+
commandsLog = [],
|
|
46
|
+
},
|
|
47
|
+
ref,
|
|
48
|
+
): JSX.Element {
|
|
49
|
+
const [timeStampedEditorStates, setTimeStampedEditorStates] = useState<
|
|
50
|
+
Array<[number, EditorState]>
|
|
51
|
+
>([]);
|
|
52
|
+
const [content, setContent] = useState<string>('');
|
|
53
|
+
const [timeTravelEnabled, setTimeTravelEnabled] = useState(false);
|
|
54
|
+
const [showExportDOM, setShowExportDOM] = useState(false);
|
|
55
|
+
const playingIndexRef = useRef(0);
|
|
56
|
+
const inputRef = useRef<HTMLInputElement | null>(null);
|
|
57
|
+
const [isPlaying, setIsPlaying] = useState(false);
|
|
58
|
+
const [isLimited, setIsLimited] = useState(false);
|
|
59
|
+
const [showLimited, setShowLimited] = useState(false);
|
|
60
|
+
const lastEditorStateRef = useRef<null | EditorState>(null);
|
|
61
|
+
const lastCommandsLogRef = useRef<LexicalCommandLog>([]);
|
|
62
|
+
const lastGenerationID = useRef(0);
|
|
63
|
+
|
|
64
|
+
const generateTree = useCallback(
|
|
65
|
+
(exportDOM: boolean) => {
|
|
66
|
+
const myID = ++lastGenerationID.current;
|
|
67
|
+
generateContent(exportDOM)
|
|
68
|
+
.then(treeText => {
|
|
69
|
+
if (myID === lastGenerationID.current) {
|
|
70
|
+
setContent(treeText);
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
.catch(err => {
|
|
74
|
+
if (myID === lastGenerationID.current) {
|
|
75
|
+
setContent(
|
|
76
|
+
`Error rendering tree: ${err.message}\n\nStack:\n${err.stack}`,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
[generateContent],
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
if (!showLimited && editorState._nodeMap.size > LARGE_EDITOR_STATE_SIZE) {
|
|
86
|
+
setIsLimited(true);
|
|
87
|
+
if (!showLimited) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Update view when either editor state changes or new commands are logged
|
|
93
|
+
const shouldUpdate =
|
|
94
|
+
lastEditorStateRef.current !== editorState ||
|
|
95
|
+
lastCommandsLogRef.current !== commandsLog;
|
|
96
|
+
|
|
97
|
+
if (shouldUpdate) {
|
|
98
|
+
// Check if it's a real editor state change
|
|
99
|
+
const isEditorStateChange = lastEditorStateRef.current !== editorState;
|
|
100
|
+
|
|
101
|
+
lastEditorStateRef.current = editorState;
|
|
102
|
+
lastCommandsLogRef.current = commandsLog;
|
|
103
|
+
generateTree(showExportDOM);
|
|
104
|
+
|
|
105
|
+
// Only record in time travel if there was an actual editor state change
|
|
106
|
+
if (!timeTravelEnabled && isEditorStateChange) {
|
|
107
|
+
setTimeStampedEditorStates(currentEditorStates => [
|
|
108
|
+
...currentEditorStates,
|
|
109
|
+
[Date.now(), editorState],
|
|
110
|
+
]);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}, [
|
|
114
|
+
editorState,
|
|
115
|
+
generateTree,
|
|
116
|
+
showExportDOM,
|
|
117
|
+
showLimited,
|
|
118
|
+
timeTravelEnabled,
|
|
119
|
+
commandsLog,
|
|
120
|
+
]);
|
|
121
|
+
|
|
122
|
+
const totalEditorStates = timeStampedEditorStates.length;
|
|
123
|
+
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
if (isPlaying) {
|
|
126
|
+
let timeoutId: ReturnType<typeof setTimeout>;
|
|
127
|
+
|
|
128
|
+
const play = () => {
|
|
129
|
+
const currentIndex = playingIndexRef.current;
|
|
130
|
+
|
|
131
|
+
if (currentIndex === totalEditorStates - 1) {
|
|
132
|
+
setIsPlaying(false);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const currentTime = timeStampedEditorStates[currentIndex][0];
|
|
137
|
+
const nextTime = timeStampedEditorStates[currentIndex + 1][0];
|
|
138
|
+
const timeDiff = nextTime - currentTime;
|
|
139
|
+
timeoutId = setTimeout(() => {
|
|
140
|
+
playingIndexRef.current++;
|
|
141
|
+
const index = playingIndexRef.current;
|
|
142
|
+
const input = inputRef.current;
|
|
143
|
+
|
|
144
|
+
if (input !== null) {
|
|
145
|
+
input.value = String(index);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
setEditorState(timeStampedEditorStates[index][1]);
|
|
149
|
+
play();
|
|
150
|
+
}, timeDiff);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
play();
|
|
154
|
+
|
|
155
|
+
return () => {
|
|
156
|
+
clearTimeout(timeoutId);
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
}, [timeStampedEditorStates, isPlaying, totalEditorStates, setEditorState]);
|
|
160
|
+
|
|
161
|
+
const handleExportModeToggleClick = () => {
|
|
162
|
+
generateTree(!showExportDOM);
|
|
163
|
+
setShowExportDOM(!showExportDOM);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
return (
|
|
167
|
+
<div className={viewClassName}>
|
|
168
|
+
{!showLimited && isLimited ? (
|
|
169
|
+
<div style={{padding: 20}}>
|
|
170
|
+
<span style={{marginRight: 20}}>
|
|
171
|
+
Detected large EditorState, this can impact debugging performance.
|
|
172
|
+
</span>
|
|
173
|
+
<button
|
|
174
|
+
onClick={() => {
|
|
175
|
+
setShowLimited(true);
|
|
176
|
+
}}
|
|
177
|
+
style={{
|
|
178
|
+
background: 'transparent',
|
|
179
|
+
border: '1px solid white',
|
|
180
|
+
color: 'white',
|
|
181
|
+
cursor: 'pointer',
|
|
182
|
+
padding: 5,
|
|
183
|
+
}}>
|
|
184
|
+
Show full tree
|
|
185
|
+
</button>
|
|
186
|
+
</div>
|
|
187
|
+
) : null}
|
|
188
|
+
{!showLimited ? (
|
|
189
|
+
<button
|
|
190
|
+
onClick={() => handleExportModeToggleClick()}
|
|
191
|
+
className={treeTypeButtonClassName}
|
|
192
|
+
type="button">
|
|
193
|
+
{showExportDOM ? 'Tree' : 'Export DOM'}
|
|
194
|
+
</button>
|
|
195
|
+
) : null}
|
|
196
|
+
{!timeTravelEnabled &&
|
|
197
|
+
(showLimited || !isLimited) &&
|
|
198
|
+
totalEditorStates > 2 && (
|
|
199
|
+
<button
|
|
200
|
+
onClick={() => {
|
|
201
|
+
setEditorReadOnly(true);
|
|
202
|
+
playingIndexRef.current = totalEditorStates - 1;
|
|
203
|
+
setTimeTravelEnabled(true);
|
|
204
|
+
}}
|
|
205
|
+
className={timeTravelButtonClassName}
|
|
206
|
+
type="button">
|
|
207
|
+
Time Travel
|
|
208
|
+
</button>
|
|
209
|
+
)}
|
|
210
|
+
{(showLimited || !isLimited) && <pre ref={ref}>{content}</pre>}
|
|
211
|
+
{timeTravelEnabled && (showLimited || !isLimited) && (
|
|
212
|
+
<div className={timeTravelPanelClassName}>
|
|
213
|
+
<button
|
|
214
|
+
className={timeTravelPanelButtonClassName}
|
|
215
|
+
onClick={() => {
|
|
216
|
+
if (playingIndexRef.current === totalEditorStates - 1) {
|
|
217
|
+
playingIndexRef.current = 1;
|
|
218
|
+
}
|
|
219
|
+
setIsPlaying(!isPlaying);
|
|
220
|
+
}}
|
|
221
|
+
type="button">
|
|
222
|
+
{isPlaying ? 'Pause' : 'Play'}
|
|
223
|
+
</button>
|
|
224
|
+
<input
|
|
225
|
+
className={timeTravelPanelSliderClassName}
|
|
226
|
+
ref={inputRef}
|
|
227
|
+
onChange={event => {
|
|
228
|
+
const editorStateIndex = Number(event.target.value);
|
|
229
|
+
const timeStampedEditorState =
|
|
230
|
+
timeStampedEditorStates[editorStateIndex];
|
|
231
|
+
|
|
232
|
+
if (timeStampedEditorState) {
|
|
233
|
+
playingIndexRef.current = editorStateIndex;
|
|
234
|
+
setEditorState(timeStampedEditorState[1]);
|
|
235
|
+
}
|
|
236
|
+
}}
|
|
237
|
+
type="range"
|
|
238
|
+
min="1"
|
|
239
|
+
max={totalEditorStates - 1}
|
|
240
|
+
/>
|
|
241
|
+
<button
|
|
242
|
+
className={timeTravelPanelButtonClassName}
|
|
243
|
+
onClick={() => {
|
|
244
|
+
setEditorReadOnly(false);
|
|
245
|
+
const index = timeStampedEditorStates.length - 1;
|
|
246
|
+
const timeStampedEditorState = timeStampedEditorStates[index];
|
|
247
|
+
setEditorState(timeStampedEditorState[1]);
|
|
248
|
+
const input = inputRef.current;
|
|
249
|
+
|
|
250
|
+
if (input !== null) {
|
|
251
|
+
input.value = String(index);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
setTimeTravelEnabled(false);
|
|
255
|
+
setIsPlaying(false);
|
|
256
|
+
}}
|
|
257
|
+
type="button">
|
|
258
|
+
Exit
|
|
259
|
+
</button>
|
|
260
|
+
</div>
|
|
261
|
+
)}
|
|
262
|
+
</div>
|
|
263
|
+
);
|
|
264
|
+
});
|
|
@@ -0,0 +1,576 @@
|
|
|
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
|
+
|
|
9
|
+
import type {
|
|
10
|
+
BaseSelection,
|
|
11
|
+
ElementNode,
|
|
12
|
+
LexicalEditor,
|
|
13
|
+
LexicalNode,
|
|
14
|
+
ParagraphNode,
|
|
15
|
+
RangeSelection,
|
|
16
|
+
TextNode,
|
|
17
|
+
} from 'lexical';
|
|
18
|
+
|
|
19
|
+
import {$generateHtmlFromNodes} from '@lexical/html';
|
|
20
|
+
import {$isLinkNode, LinkNode} from '@lexical/link';
|
|
21
|
+
import {$isMarkNode} from '@lexical/mark';
|
|
22
|
+
import {$isTableSelection, TableSelection} from '@lexical/table';
|
|
23
|
+
import {
|
|
24
|
+
$getRoot,
|
|
25
|
+
$getSelection,
|
|
26
|
+
$isElementNode,
|
|
27
|
+
$isNodeSelection,
|
|
28
|
+
$isParagraphNode,
|
|
29
|
+
$isRangeSelection,
|
|
30
|
+
$isTextNode,
|
|
31
|
+
} from 'lexical';
|
|
32
|
+
|
|
33
|
+
import {LexicalCommandLog} from './useLexicalCommandsLog';
|
|
34
|
+
|
|
35
|
+
export type CustomPrintNodeFn = (
|
|
36
|
+
node: LexicalNode,
|
|
37
|
+
obfuscateText?: boolean,
|
|
38
|
+
) => string | undefined;
|
|
39
|
+
|
|
40
|
+
const NON_SINGLE_WIDTH_CHARS_REPLACEMENT: Readonly<Record<string, string>> =
|
|
41
|
+
Object.freeze({
|
|
42
|
+
'\t': '\\t',
|
|
43
|
+
'\n': '\\n',
|
|
44
|
+
});
|
|
45
|
+
const NON_SINGLE_WIDTH_CHARS_REGEX = new RegExp(
|
|
46
|
+
Object.keys(NON_SINGLE_WIDTH_CHARS_REPLACEMENT).join('|'),
|
|
47
|
+
'g',
|
|
48
|
+
);
|
|
49
|
+
const SYMBOLS: Record<string, string> = Object.freeze({
|
|
50
|
+
ancestorHasNextSibling: '|',
|
|
51
|
+
ancestorIsLastChild: ' ',
|
|
52
|
+
hasNextSibling: '├',
|
|
53
|
+
isLastChild: '└',
|
|
54
|
+
selectedChar: '^',
|
|
55
|
+
selectedLine: '>',
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const FORMAT_PREDICATES = [
|
|
59
|
+
(node: TextNode | RangeSelection) => node.hasFormat('bold') && 'Bold',
|
|
60
|
+
(node: TextNode | RangeSelection) => node.hasFormat('code') && 'Code',
|
|
61
|
+
(node: TextNode | RangeSelection) => node.hasFormat('italic') && 'Italic',
|
|
62
|
+
(node: TextNode | RangeSelection) =>
|
|
63
|
+
node.hasFormat('strikethrough') && 'Strikethrough',
|
|
64
|
+
(node: TextNode | RangeSelection) =>
|
|
65
|
+
node.hasFormat('subscript') && 'Subscript',
|
|
66
|
+
(node: TextNode | RangeSelection) =>
|
|
67
|
+
node.hasFormat('superscript') && 'Superscript',
|
|
68
|
+
(node: TextNode | RangeSelection) =>
|
|
69
|
+
node.hasFormat('underline') && 'Underline',
|
|
70
|
+
(node: TextNode | RangeSelection) =>
|
|
71
|
+
node.hasFormat('highlight') && 'Highlight',
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
const FORMAT_PREDICATES_PARAGRAPH = [
|
|
75
|
+
(node: ParagraphNode) => node.hasTextFormat('bold') && 'Bold',
|
|
76
|
+
(node: ParagraphNode) => node.hasTextFormat('code') && 'Code',
|
|
77
|
+
(node: ParagraphNode) => node.hasTextFormat('italic') && 'Italic',
|
|
78
|
+
(node: ParagraphNode) =>
|
|
79
|
+
node.hasTextFormat('strikethrough') && 'Strikethrough',
|
|
80
|
+
(node: ParagraphNode) => node.hasTextFormat('subscript') && 'Subscript',
|
|
81
|
+
(node: ParagraphNode) => node.hasTextFormat('superscript') && 'Superscript',
|
|
82
|
+
(node: ParagraphNode) => node.hasTextFormat('underline') && 'Underline',
|
|
83
|
+
(node: ParagraphNode) => node.hasTextFormat('highlight') && 'Highlight',
|
|
84
|
+
];
|
|
85
|
+
|
|
86
|
+
const DETAIL_PREDICATES = [
|
|
87
|
+
(node: TextNode) => node.isDirectionless() && 'Directionless',
|
|
88
|
+
(node: TextNode) => node.isUnmergeable() && 'Unmergeable',
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
const MODE_PREDICATES = [
|
|
92
|
+
(node: TextNode) => node.isToken() && 'Token',
|
|
93
|
+
(node: TextNode) => node.isSegmented() && 'Segmented',
|
|
94
|
+
];
|
|
95
|
+
|
|
96
|
+
export function generateContent(
|
|
97
|
+
editor: LexicalEditor,
|
|
98
|
+
commandsLog: LexicalCommandLog,
|
|
99
|
+
exportDOM: boolean,
|
|
100
|
+
customPrintNode?: CustomPrintNodeFn,
|
|
101
|
+
obfuscateText: boolean = false,
|
|
102
|
+
): string {
|
|
103
|
+
const editorState = editor.getEditorState();
|
|
104
|
+
const editorConfig = editor._config;
|
|
105
|
+
const compositionKey = editor._compositionKey;
|
|
106
|
+
const editable = editor._editable;
|
|
107
|
+
|
|
108
|
+
if (exportDOM) {
|
|
109
|
+
let htmlString = '';
|
|
110
|
+
editorState.read(
|
|
111
|
+
() => {
|
|
112
|
+
htmlString = printPrettyHTML($generateHtmlFromNodes(editor));
|
|
113
|
+
},
|
|
114
|
+
{editor},
|
|
115
|
+
);
|
|
116
|
+
return htmlString;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let res = ' root\n';
|
|
120
|
+
|
|
121
|
+
const selectionString = editorState.read(
|
|
122
|
+
() => {
|
|
123
|
+
const selection = $getSelection();
|
|
124
|
+
|
|
125
|
+
visitTree($getRoot(), (node: LexicalNode, indent: Array<string>) => {
|
|
126
|
+
const nodeKey = node.getKey();
|
|
127
|
+
const nodeKeyDisplay = `(${nodeKey})`;
|
|
128
|
+
const typeDisplay = node.getType() || '';
|
|
129
|
+
const isSelected = node.isSelected();
|
|
130
|
+
|
|
131
|
+
res += `${isSelected ? SYMBOLS.selectedLine : ' '} ${indent.join(
|
|
132
|
+
' ',
|
|
133
|
+
)} ${nodeKeyDisplay} ${typeDisplay} ${printNode(
|
|
134
|
+
node,
|
|
135
|
+
customPrintNode,
|
|
136
|
+
obfuscateText,
|
|
137
|
+
)}\n`;
|
|
138
|
+
|
|
139
|
+
res += $printSelectedCharsLine({
|
|
140
|
+
indent,
|
|
141
|
+
isSelected,
|
|
142
|
+
node,
|
|
143
|
+
nodeKeyDisplay,
|
|
144
|
+
selection,
|
|
145
|
+
typeDisplay,
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
return selection === null
|
|
150
|
+
? ': null'
|
|
151
|
+
: $isRangeSelection(selection)
|
|
152
|
+
? printRangeSelection(selection)
|
|
153
|
+
: $isTableSelection(selection)
|
|
154
|
+
? printTableSelection(selection)
|
|
155
|
+
: printNodeSelection(selection);
|
|
156
|
+
},
|
|
157
|
+
{editor},
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
res += '\n selection' + selectionString;
|
|
161
|
+
|
|
162
|
+
res += '\n\n commands:';
|
|
163
|
+
|
|
164
|
+
if (commandsLog.length) {
|
|
165
|
+
for (const {index, type, payload} of commandsLog) {
|
|
166
|
+
res += `\n └ ${index}. { type: ${type}, payload: ${
|
|
167
|
+
payload instanceof Event ? payload.constructor.name : payload
|
|
168
|
+
} }`;
|
|
169
|
+
}
|
|
170
|
+
} else {
|
|
171
|
+
res += '\n └ None dispatched.';
|
|
172
|
+
}
|
|
173
|
+
const {version} = editor.constructor;
|
|
174
|
+
res += `\n\n editor${version ? ` (v${version})` : ''}:`;
|
|
175
|
+
res += `\n └ namespace ${editorConfig.namespace}`;
|
|
176
|
+
if (compositionKey !== null) {
|
|
177
|
+
res += `\n └ compositionKey ${compositionKey}`;
|
|
178
|
+
}
|
|
179
|
+
res += `\n └ editable ${String(editable)}`;
|
|
180
|
+
|
|
181
|
+
return res;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function printRangeSelection(selection: RangeSelection): string {
|
|
185
|
+
let res = '';
|
|
186
|
+
|
|
187
|
+
const formatText = printFormatProperties(selection);
|
|
188
|
+
|
|
189
|
+
res += `: range ${formatText !== '' ? `{ ${formatText} }` : ''} ${
|
|
190
|
+
selection.style !== '' ? `{ style: ${selection.style} } ` : ''
|
|
191
|
+
}`;
|
|
192
|
+
|
|
193
|
+
const anchor = selection.anchor;
|
|
194
|
+
const focus = selection.focus;
|
|
195
|
+
const anchorOffset = anchor.offset;
|
|
196
|
+
const focusOffset = focus.offset;
|
|
197
|
+
|
|
198
|
+
res += `\n ├ anchor { key: ${anchor.key}, offset: ${
|
|
199
|
+
anchorOffset === null ? 'null' : anchorOffset
|
|
200
|
+
}, type: ${anchor.type} }`;
|
|
201
|
+
res += `\n └ focus { key: ${focus.key}, offset: ${
|
|
202
|
+
focusOffset === null ? 'null' : focusOffset
|
|
203
|
+
}, type: ${focus.type} }`;
|
|
204
|
+
|
|
205
|
+
return res;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function printNodeSelection(selection: BaseSelection): string {
|
|
209
|
+
if (!$isNodeSelection(selection)) {
|
|
210
|
+
return '';
|
|
211
|
+
}
|
|
212
|
+
return `: node\n └ [${Array.from(selection._nodes).join(', ')}]`;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function printTableSelection(selection: TableSelection): string {
|
|
216
|
+
return `: table\n └ { table: ${selection.tableKey}, anchorCell: ${selection.anchor.key}, focusCell: ${selection.focus.key} }`;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function visitTree(
|
|
220
|
+
currentNode: ElementNode,
|
|
221
|
+
visitor: (node: LexicalNode, indentArr: Array<string>) => void,
|
|
222
|
+
indent: Array<string> = [],
|
|
223
|
+
) {
|
|
224
|
+
const childNodes = currentNode.getChildren();
|
|
225
|
+
const childNodesLength = childNodes.length;
|
|
226
|
+
|
|
227
|
+
childNodes.forEach((childNode, i) => {
|
|
228
|
+
visitor(
|
|
229
|
+
childNode,
|
|
230
|
+
indent.concat(
|
|
231
|
+
i === childNodesLength - 1
|
|
232
|
+
? SYMBOLS.isLastChild
|
|
233
|
+
: SYMBOLS.hasNextSibling,
|
|
234
|
+
),
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
if ($isElementNode(childNode)) {
|
|
238
|
+
visitTree(
|
|
239
|
+
childNode,
|
|
240
|
+
visitor,
|
|
241
|
+
indent.concat(
|
|
242
|
+
i === childNodesLength - 1
|
|
243
|
+
? SYMBOLS.ancestorIsLastChild
|
|
244
|
+
: SYMBOLS.ancestorHasNextSibling,
|
|
245
|
+
),
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function normalize(text: string, obfuscateText: boolean = false) {
|
|
252
|
+
const textToPrint = Object.entries(NON_SINGLE_WIDTH_CHARS_REPLACEMENT).reduce(
|
|
253
|
+
(acc, [key, value]) => acc.replace(new RegExp(key, 'g'), String(value)),
|
|
254
|
+
text,
|
|
255
|
+
);
|
|
256
|
+
if (obfuscateText) {
|
|
257
|
+
return textToPrint.replace(/[^\s]/g, '*');
|
|
258
|
+
}
|
|
259
|
+
return textToPrint;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function printNode(
|
|
263
|
+
node: LexicalNode,
|
|
264
|
+
customPrintNode?: CustomPrintNodeFn,
|
|
265
|
+
obfuscateText: boolean = false,
|
|
266
|
+
) {
|
|
267
|
+
const customPrint: string | undefined = customPrintNode
|
|
268
|
+
? customPrintNode(node, obfuscateText)
|
|
269
|
+
: undefined;
|
|
270
|
+
if (customPrint !== undefined && customPrint.length > 0) {
|
|
271
|
+
return customPrint;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if ($isTextNode(node)) {
|
|
275
|
+
const text = node.getTextContent();
|
|
276
|
+
const title =
|
|
277
|
+
text.length === 0 ? '(empty)' : `"${normalize(text, obfuscateText)}"`;
|
|
278
|
+
const properties = printAllTextNodeProperties(node);
|
|
279
|
+
return [title, properties.length !== 0 ? `{ ${properties} }` : null]
|
|
280
|
+
.filter(Boolean)
|
|
281
|
+
.join(' ')
|
|
282
|
+
.trim();
|
|
283
|
+
} else if ($isLinkNode(node)) {
|
|
284
|
+
const link = node.getURL();
|
|
285
|
+
const title =
|
|
286
|
+
link.length === 0 ? '(empty)' : `"${normalize(link, obfuscateText)}"`;
|
|
287
|
+
const properties = printAllLinkNodeProperties(node);
|
|
288
|
+
return [title, properties.length !== 0 ? `{ ${properties} }` : null]
|
|
289
|
+
.filter(Boolean)
|
|
290
|
+
.join(' ')
|
|
291
|
+
.trim();
|
|
292
|
+
} else if ($isMarkNode(node)) {
|
|
293
|
+
return `ids: [ ${node.getIDs().join(', ')} ]`;
|
|
294
|
+
} else if ($isParagraphNode(node)) {
|
|
295
|
+
const formatText = printTextFormatProperties(node);
|
|
296
|
+
let paragraphData = formatText !== '' ? `{ ${formatText} }` : '';
|
|
297
|
+
paragraphData += node.__style ? `(${node.__style})` : '';
|
|
298
|
+
return paragraphData;
|
|
299
|
+
} else {
|
|
300
|
+
return '';
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function printTextFormatProperties(nodeOrSelection: ParagraphNode) {
|
|
305
|
+
let str = FORMAT_PREDICATES_PARAGRAPH.map(predicate =>
|
|
306
|
+
predicate(nodeOrSelection),
|
|
307
|
+
)
|
|
308
|
+
.filter(Boolean)
|
|
309
|
+
.join(', ')
|
|
310
|
+
.toLocaleLowerCase();
|
|
311
|
+
|
|
312
|
+
if (str !== '') {
|
|
313
|
+
str = 'format: ' + str;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return str;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function printAllTextNodeProperties(node: TextNode) {
|
|
320
|
+
return [
|
|
321
|
+
printFormatProperties(node),
|
|
322
|
+
printDetailProperties(node),
|
|
323
|
+
printModeProperties(node),
|
|
324
|
+
printStateProperties(node),
|
|
325
|
+
]
|
|
326
|
+
.filter(Boolean)
|
|
327
|
+
.join(', ');
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function printAllLinkNodeProperties(node: LinkNode) {
|
|
331
|
+
return [
|
|
332
|
+
printTargetProperties(node),
|
|
333
|
+
printRelProperties(node),
|
|
334
|
+
printTitleProperties(node),
|
|
335
|
+
printStateProperties(node),
|
|
336
|
+
]
|
|
337
|
+
.filter(Boolean)
|
|
338
|
+
.join(', ');
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function printDetailProperties(nodeOrSelection: TextNode) {
|
|
342
|
+
let str = DETAIL_PREDICATES.map(predicate => predicate(nodeOrSelection))
|
|
343
|
+
.filter(Boolean)
|
|
344
|
+
.join(', ')
|
|
345
|
+
.toLocaleLowerCase();
|
|
346
|
+
|
|
347
|
+
if (str !== '') {
|
|
348
|
+
str = 'detail: ' + str;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return str;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function printModeProperties(nodeOrSelection: TextNode) {
|
|
355
|
+
let str = MODE_PREDICATES.map(predicate => predicate(nodeOrSelection))
|
|
356
|
+
.filter(Boolean)
|
|
357
|
+
.join(', ')
|
|
358
|
+
.toLocaleLowerCase();
|
|
359
|
+
|
|
360
|
+
if (str !== '') {
|
|
361
|
+
str = 'mode: ' + str;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
return str;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function printFormatProperties(nodeOrSelection: TextNode | RangeSelection) {
|
|
368
|
+
let str = FORMAT_PREDICATES.map(predicate => predicate(nodeOrSelection))
|
|
369
|
+
.filter(Boolean)
|
|
370
|
+
.join(', ')
|
|
371
|
+
.toLocaleLowerCase();
|
|
372
|
+
|
|
373
|
+
if (str !== '') {
|
|
374
|
+
str = 'format: ' + str;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
return str;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function printTargetProperties(node: LinkNode) {
|
|
381
|
+
let str = node.getTarget();
|
|
382
|
+
// TODO Fix nullish on LinkNode
|
|
383
|
+
if (str != null) {
|
|
384
|
+
str = 'target: ' + str;
|
|
385
|
+
}
|
|
386
|
+
return str;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function printRelProperties(node: LinkNode) {
|
|
390
|
+
let str = node.getRel();
|
|
391
|
+
// TODO Fix nullish on LinkNode
|
|
392
|
+
if (str != null) {
|
|
393
|
+
str = 'rel: ' + str;
|
|
394
|
+
}
|
|
395
|
+
return str;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function printTitleProperties(node: LinkNode) {
|
|
399
|
+
let str = node.getTitle();
|
|
400
|
+
// TODO Fix nullish on LinkNode
|
|
401
|
+
if (str != null) {
|
|
402
|
+
str = 'title: ' + str;
|
|
403
|
+
}
|
|
404
|
+
return str;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function printStateProperties(node: LexicalNode) {
|
|
408
|
+
if (!node.__state) {
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
const states = [];
|
|
412
|
+
for (const [stateType, value] of node.__state.knownState.entries()) {
|
|
413
|
+
if (stateType.isEqual(value, stateType.defaultValue)) {
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
const textValue = JSON.stringify(stateType.unparse(value));
|
|
417
|
+
states.push(`[${stateType.key}: ${textValue}]`);
|
|
418
|
+
}
|
|
419
|
+
let str = states.join(',');
|
|
420
|
+
if (str !== '') {
|
|
421
|
+
str = 'state: ' + str;
|
|
422
|
+
}
|
|
423
|
+
return str;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function $printSelectedCharsLine({
|
|
427
|
+
indent,
|
|
428
|
+
isSelected,
|
|
429
|
+
node,
|
|
430
|
+
nodeKeyDisplay,
|
|
431
|
+
selection,
|
|
432
|
+
typeDisplay,
|
|
433
|
+
}: {
|
|
434
|
+
indent: Array<string>;
|
|
435
|
+
isSelected: boolean;
|
|
436
|
+
node: LexicalNode;
|
|
437
|
+
nodeKeyDisplay: string;
|
|
438
|
+
selection: BaseSelection | null;
|
|
439
|
+
typeDisplay: string;
|
|
440
|
+
}) {
|
|
441
|
+
// No selection or node is not selected.
|
|
442
|
+
if (
|
|
443
|
+
!$isTextNode(node) ||
|
|
444
|
+
!$isRangeSelection(selection) ||
|
|
445
|
+
!isSelected ||
|
|
446
|
+
$isElementNode(node)
|
|
447
|
+
) {
|
|
448
|
+
return '';
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// No selected characters.
|
|
452
|
+
const anchor = selection.anchor;
|
|
453
|
+
const focus = selection.focus;
|
|
454
|
+
|
|
455
|
+
if (
|
|
456
|
+
node.getTextContent() === '' ||
|
|
457
|
+
(anchor.getNode() === selection.focus.getNode() &&
|
|
458
|
+
anchor.offset === focus.offset)
|
|
459
|
+
) {
|
|
460
|
+
return '';
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const [start, end] = $getSelectionStartEnd(node, selection);
|
|
464
|
+
|
|
465
|
+
if (start === end) {
|
|
466
|
+
return '';
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const selectionLastIndent =
|
|
470
|
+
indent[indent.length - 1] === SYMBOLS.hasNextSibling
|
|
471
|
+
? SYMBOLS.ancestorHasNextSibling
|
|
472
|
+
: SYMBOLS.ancestorIsLastChild;
|
|
473
|
+
|
|
474
|
+
const indentionChars = [
|
|
475
|
+
...indent.slice(0, indent.length - 1),
|
|
476
|
+
selectionLastIndent,
|
|
477
|
+
];
|
|
478
|
+
const unselectedChars = Array(start + 1).fill(' ');
|
|
479
|
+
const selectedChars = Array(end - start).fill(SYMBOLS.selectedChar);
|
|
480
|
+
const paddingLength = typeDisplay.length + 2; // 1 for the space after + 1 for the double quote.
|
|
481
|
+
|
|
482
|
+
const nodePrintSpaces = Array(nodeKeyDisplay.length + paddingLength).fill(
|
|
483
|
+
' ',
|
|
484
|
+
);
|
|
485
|
+
|
|
486
|
+
return (
|
|
487
|
+
[
|
|
488
|
+
SYMBOLS.selectedLine,
|
|
489
|
+
indentionChars.join(' '),
|
|
490
|
+
[...nodePrintSpaces, ...unselectedChars, ...selectedChars].join(''),
|
|
491
|
+
].join(' ') + '\n'
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
function printPrettyHTML(str: string) {
|
|
496
|
+
const div = document.createElement('div');
|
|
497
|
+
div.innerHTML = str.trim();
|
|
498
|
+
return prettifyHTML(div, 0).innerHTML;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
function prettifyHTML(node: Element, level: number) {
|
|
502
|
+
const indentBefore = new Array(level++ + 1).join(' ');
|
|
503
|
+
const indentAfter = new Array(level - 1).join(' ');
|
|
504
|
+
let textNode;
|
|
505
|
+
|
|
506
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
507
|
+
textNode = document.createTextNode('\n' + indentBefore);
|
|
508
|
+
node.insertBefore(textNode, node.children[i]);
|
|
509
|
+
prettifyHTML(node.children[i], level);
|
|
510
|
+
if (node.lastElementChild === node.children[i]) {
|
|
511
|
+
textNode = document.createTextNode('\n' + indentAfter);
|
|
512
|
+
node.appendChild(textNode);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
return node;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function $getSelectionStartEnd(
|
|
520
|
+
node: LexicalNode,
|
|
521
|
+
selection: BaseSelection,
|
|
522
|
+
): [number, number] {
|
|
523
|
+
const anchorAndFocus = selection.getStartEndPoints();
|
|
524
|
+
if ($isNodeSelection(selection) || anchorAndFocus === null) {
|
|
525
|
+
return [-1, -1];
|
|
526
|
+
}
|
|
527
|
+
const [anchor, focus] = anchorAndFocus;
|
|
528
|
+
const textContent = node.getTextContent();
|
|
529
|
+
const textLength = textContent.length;
|
|
530
|
+
|
|
531
|
+
let start = -1;
|
|
532
|
+
let end = -1;
|
|
533
|
+
|
|
534
|
+
// Only one node is being selected.
|
|
535
|
+
if (anchor.type === 'text' && focus.type === 'text') {
|
|
536
|
+
const anchorNode = anchor.getNode();
|
|
537
|
+
const focusNode = focus.getNode();
|
|
538
|
+
|
|
539
|
+
if (
|
|
540
|
+
anchorNode === focusNode &&
|
|
541
|
+
node === anchorNode &&
|
|
542
|
+
anchor.offset !== focus.offset
|
|
543
|
+
) {
|
|
544
|
+
[start, end] =
|
|
545
|
+
anchor.offset < focus.offset
|
|
546
|
+
? [anchor.offset, focus.offset]
|
|
547
|
+
: [focus.offset, anchor.offset];
|
|
548
|
+
} else if (node === anchorNode) {
|
|
549
|
+
[start, end] = anchorNode.isBefore(focusNode)
|
|
550
|
+
? [anchor.offset, textLength]
|
|
551
|
+
: [0, anchor.offset];
|
|
552
|
+
} else if (node === focusNode) {
|
|
553
|
+
[start, end] = focusNode.isBefore(anchorNode)
|
|
554
|
+
? [focus.offset, textLength]
|
|
555
|
+
: [0, focus.offset];
|
|
556
|
+
} else {
|
|
557
|
+
// Node is within selection but not the anchor nor focus.
|
|
558
|
+
[start, end] = [0, textLength];
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// Account for non-single width characters.
|
|
563
|
+
const numNonSingleWidthCharBeforeSelection = (
|
|
564
|
+
textContent.slice(0, start).match(NON_SINGLE_WIDTH_CHARS_REGEX) || []
|
|
565
|
+
).length;
|
|
566
|
+
const numNonSingleWidthCharInSelection = (
|
|
567
|
+
textContent.slice(start, end).match(NON_SINGLE_WIDTH_CHARS_REGEX) || []
|
|
568
|
+
).length;
|
|
569
|
+
|
|
570
|
+
return [
|
|
571
|
+
start + numNonSingleWidthCharBeforeSelection,
|
|
572
|
+
end +
|
|
573
|
+
numNonSingleWidthCharBeforeSelection +
|
|
574
|
+
numNonSingleWidthCharInSelection,
|
|
575
|
+
];
|
|
576
|
+
}
|
package/src/index.ts
ADDED
|
@@ -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
|
+
*/
|
|
8
|
+
|
|
9
|
+
export * from './generateContent';
|
|
10
|
+
export * from './TreeView';
|
|
11
|
+
export * from './useLexicalCommandsLog';
|
|
@@ -0,0 +1,61 @@
|
|
|
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
|
+
|
|
9
|
+
import type {LexicalEditor} from 'lexical';
|
|
10
|
+
|
|
11
|
+
import {COMMAND_PRIORITY_CRITICAL, LexicalCommand} from 'lexical';
|
|
12
|
+
import {useEffect, useState} from 'react';
|
|
13
|
+
|
|
14
|
+
export type LexicalCommandEntry = {index: number} & LexicalCommand<unknown> & {
|
|
15
|
+
payload: unknown;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type LexicalCommandLog = ReadonlyArray<LexicalCommandEntry>;
|
|
19
|
+
|
|
20
|
+
export function registerLexicalCommandLogger(
|
|
21
|
+
editor: LexicalEditor,
|
|
22
|
+
setLoggedCommands: (
|
|
23
|
+
v: (oldValue: LexicalCommandLog) => LexicalCommandLog,
|
|
24
|
+
) => void,
|
|
25
|
+
): () => void {
|
|
26
|
+
const unregisterCommandListeners: (() => void)[] = [];
|
|
27
|
+
let index = 0;
|
|
28
|
+
for (const command of editor._commands.keys()) {
|
|
29
|
+
unregisterCommandListeners.push(
|
|
30
|
+
editor.registerCommand(
|
|
31
|
+
command,
|
|
32
|
+
payload => {
|
|
33
|
+
index += 1;
|
|
34
|
+
const entry: LexicalCommandEntry = {
|
|
35
|
+
index,
|
|
36
|
+
payload,
|
|
37
|
+
type: command.type ? command.type : 'UNKNOWN',
|
|
38
|
+
};
|
|
39
|
+
setLoggedCommands(state => [...state.slice(-9), entry]);
|
|
40
|
+
return false;
|
|
41
|
+
},
|
|
42
|
+
COMMAND_PRIORITY_CRITICAL,
|
|
43
|
+
),
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return () => unregisterCommandListeners.forEach(unregister => unregister());
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function useLexicalCommandsLog(
|
|
51
|
+
editor: LexicalEditor,
|
|
52
|
+
): LexicalCommandLog {
|
|
53
|
+
const [loggedCommands, setLoggedCommands] = useState<LexicalCommandLog>([]);
|
|
54
|
+
|
|
55
|
+
useEffect(
|
|
56
|
+
() => registerLexicalCommandLogger(editor, setLoggedCommands),
|
|
57
|
+
[editor],
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
return loggedCommands;
|
|
61
|
+
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|