@lexical/react 0.8.1 → 0.9.1

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