@react-fabric/lexical 1.0.0-beta.47 → 1.0.0-beta.48

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 (62) hide show
  1. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +29 -19
  2. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +1 -1
  3. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +9 -9
  4. package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +142 -75
  5. package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +1 -1
  6. package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +25 -21
  7. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +10 -3
  8. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +1 -1
  9. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +3 -3
  10. package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +147 -103
  11. package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +1 -1
  12. package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +10 -10
  13. package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +254 -178
  14. package/dist/cjs/node_modules/@lexical/list/LexicalList.js +1 -1
  15. package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +19 -16
  16. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +53 -29
  17. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +1 -1
  18. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +10 -10
  19. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +176 -194
  20. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +1 -1
  21. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +25 -18
  22. package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +1925 -793
  23. package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +1 -1
  24. package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +48 -40
  25. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +426 -144
  26. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +11 -1
  27. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +46 -32
  28. package/dist/cjs/node_modules/prismjs/prism.js +1 -1
  29. package/dist/cjs/packages/lexical/src/editor/LexicalEditor.js +1 -1
  30. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +29 -19
  31. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +1 -1
  32. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +3 -3
  33. package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +139 -76
  34. package/dist/esm/node_modules/@lexical/code/LexicalCode.js +1 -1
  35. package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +4 -4
  36. package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +11 -4
  37. package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +1 -1
  38. package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +3 -3
  39. package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +149 -105
  40. package/dist/esm/node_modules/@lexical/link/LexicalLink.js +1 -1
  41. package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +4 -4
  42. package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +253 -180
  43. package/dist/esm/node_modules/@lexical/list/LexicalList.js +1 -1
  44. package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +4 -4
  45. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +54 -30
  46. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +1 -1
  47. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +3 -3
  48. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +171 -196
  49. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +1 -1
  50. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +4 -4
  51. package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +1920 -796
  52. package/dist/esm/node_modules/@lexical/table/LexicalTable.js +1 -1
  53. package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +4 -4
  54. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +413 -145
  55. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +7 -2
  56. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +3 -3
  57. package/dist/esm/node_modules/prismjs/prism.js +1 -1
  58. package/dist/esm/packages/lexical/src/editor/LexicalEditor.js +1 -1
  59. package/package.json +7 -8
  60. package/src/components/StickyComponent.tsx +1 -1
  61. package/src/editor/LexicalEditor.tsx +25 -21
  62. package/src/plugins/ToolbarPlugin.tsx +20 -4
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var lexical = require('lexical');
4
3
  var LexicalUtils = require('../utils/LexicalUtils.js');
4
+ var lexical = require('lexical');
5
5
 
6
6
  /**
7
7
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -20,6 +20,11 @@ var LexicalUtils = require('../utils/LexicalUtils.js');
20
20
  *
21
21
  */
22
22
 
23
+ // Do not require this module directly! Use normal `invariant` calls.
24
+
25
+ function formatDevErrorMessage(message) {
26
+ throw new Error(message);
27
+ }
23
28
 
24
29
  /**
25
30
  * Checks the depth of listNode from the root node.
@@ -38,7 +43,7 @@ function $getListDepth(listNode) {
38
43
  continue;
39
44
  }
40
45
  {
41
- throw Error(`A ListItemNode must have a ListNode for a parent.`);
46
+ formatDevErrorMessage(`A ListItemNode must have a ListNode for a parent.`);
42
47
  }
43
48
  }
44
49
  return depth;
@@ -55,7 +60,7 @@ function $getTopListNode(listItem) {
55
60
  let list = listItem.getParent();
56
61
  if (!$isListNode(list)) {
57
62
  {
58
- throw Error(`A ListItemNode must have a ListNode for a parent.`);
63
+ formatDevErrorMessage(`A ListItemNode must have a ListNode for a parent.`);
59
64
  }
60
65
  }
61
66
  let parent = list;
@@ -116,7 +121,7 @@ function $removeHighestEmptyListParent(sublist) {
116
121
  let emptyListPtr = sublist;
117
122
  while (emptyListPtr.getNextSibling() == null && emptyListPtr.getPreviousSibling() == null) {
118
123
  const parent = emptyListPtr.getParent();
119
- if (parent == null || !($isListItemNode(emptyListPtr) || $isListNode(emptyListPtr))) {
124
+ if (parent == null || !($isListItemNode(parent) || $isListNode(parent))) {
120
125
  break;
121
126
  }
122
127
  emptyListPtr = parent;
@@ -134,14 +139,6 @@ function $wrapInListItem(node) {
134
139
  return listItemWrapper.append(node);
135
140
  }
136
141
 
137
- /**
138
- * Copyright (c) Meta Platforms, Inc. and affiliates.
139
- *
140
- * This source code is licensed under the MIT license found in the
141
- * LICENSE file in the root directory of this source tree.
142
- *
143
- */
144
-
145
142
  function $isSelectingEmptyListItem(anchorNode, nodes) {
146
143
  return $isListItemNode(anchorNode) && (nodes.length === 0 || nodes.length === 1 && anchorNode.is(nodes[0]) && anchorNode.getChildrenSize() === 0);
147
144
  }
@@ -153,73 +150,79 @@ function $isSelectingEmptyListItem(anchorNode, nodes) {
153
150
  * If the selection's anchor node is not an empty ListItemNode, it will add a new ListNode or merge an existing ListNode,
154
151
  * unless the the node is a leaf node, in which case it will attempt to find a ListNode up the branch and replace it with
155
152
  * a new ListNode, or create a new ListNode at the nearest root/shadow root.
156
- * @param editor - The lexical editor.
157
153
  * @param listType - The type of list, "number" | "bullet" | "check".
158
154
  */
159
- function insertList(editor, listType) {
160
- editor.update(() => {
161
- const selection = lexical.$getSelection();
162
- if (selection !== null) {
163
- const nodes = selection.getNodes();
164
- if (lexical.$isRangeSelection(selection)) {
165
- const anchorAndFocus = selection.getStartEndPoints();
166
- if (!(anchorAndFocus !== null)) {
167
- throw Error(`insertList: anchor should be defined`);
155
+ function $insertList(listType) {
156
+ const selection = lexical.$getSelection();
157
+ if (selection !== null) {
158
+ let nodes = selection.getNodes();
159
+ if (lexical.$isRangeSelection(selection)) {
160
+ const anchorAndFocus = selection.getStartEndPoints();
161
+ if (!(anchorAndFocus !== null)) {
162
+ formatDevErrorMessage(`insertList: anchor should be defined`);
163
+ }
164
+ const [anchor] = anchorAndFocus;
165
+ const anchorNode = anchor.getNode();
166
+ const anchorNodeParent = anchorNode.getParent();
167
+ if (lexical.$isRootOrShadowRoot(anchorNode)) {
168
+ const firstChild = anchorNode.getFirstChild();
169
+ if (firstChild) {
170
+ nodes = firstChild.selectStart().getNodes();
171
+ } else {
172
+ const paragraph = lexical.$createParagraphNode();
173
+ anchorNode.append(paragraph);
174
+ nodes = paragraph.select().getNodes();
168
175
  }
169
- const [anchor] = anchorAndFocus;
170
- const anchorNode = anchor.getNode();
171
- const anchorNodeParent = anchorNode.getParent();
172
- if ($isSelectingEmptyListItem(anchorNode, nodes)) {
173
- const list = $createListNode(listType);
174
- if (lexical.$isRootOrShadowRoot(anchorNodeParent)) {
175
- anchorNode.replace(list);
176
- const listItem = $createListItemNode();
177
- if (lexical.$isElementNode(anchorNode)) {
178
- listItem.setFormat(anchorNode.getFormatType());
179
- listItem.setIndent(anchorNode.getIndent());
180
- }
181
- list.append(listItem);
182
- } else if ($isListItemNode(anchorNode)) {
183
- const parent = anchorNode.getParentOrThrow();
184
- append(list, parent.getChildren());
185
- parent.replace(list);
176
+ } else if ($isSelectingEmptyListItem(anchorNode, nodes)) {
177
+ const list = $createListNode(listType);
178
+ if (lexical.$isRootOrShadowRoot(anchorNodeParent)) {
179
+ anchorNode.replace(list);
180
+ const listItem = $createListItemNode();
181
+ if (lexical.$isElementNode(anchorNode)) {
182
+ listItem.setFormat(anchorNode.getFormatType());
183
+ listItem.setIndent(anchorNode.getIndent());
186
184
  }
187
- return;
185
+ list.append(listItem);
186
+ } else if ($isListItemNode(anchorNode)) {
187
+ const parent = anchorNode.getParentOrThrow();
188
+ append(list, parent.getChildren());
189
+ parent.replace(list);
188
190
  }
191
+ return;
189
192
  }
190
- const handled = new Set();
191
- for (let i = 0; i < nodes.length; i++) {
192
- const node = nodes[i];
193
- if (lexical.$isElementNode(node) && node.isEmpty() && !$isListItemNode(node) && !handled.has(node.getKey())) {
194
- $createListOrMerge(node, listType);
195
- continue;
196
- }
197
- if (lexical.$isLeafNode(node)) {
198
- let parent = node.getParent();
199
- while (parent != null) {
200
- const parentKey = parent.getKey();
201
- if ($isListNode(parent)) {
202
- if (!handled.has(parentKey)) {
203
- const newListNode = $createListNode(listType);
204
- append(newListNode, parent.getChildren());
205
- parent.replace(newListNode);
206
- handled.add(parentKey);
207
- }
193
+ }
194
+ const handled = new Set();
195
+ for (let i = 0; i < nodes.length; i++) {
196
+ const node = nodes[i];
197
+ if (lexical.$isElementNode(node) && node.isEmpty() && !$isListItemNode(node) && !handled.has(node.getKey())) {
198
+ $createListOrMerge(node, listType);
199
+ continue;
200
+ }
201
+ if (lexical.$isLeafNode(node)) {
202
+ let parent = node.getParent();
203
+ while (parent != null) {
204
+ const parentKey = parent.getKey();
205
+ if ($isListNode(parent)) {
206
+ if (!handled.has(parentKey)) {
207
+ const newListNode = $createListNode(listType);
208
+ append(newListNode, parent.getChildren());
209
+ parent.replace(newListNode);
210
+ handled.add(parentKey);
211
+ }
212
+ break;
213
+ } else {
214
+ const nextParent = parent.getParent();
215
+ if (lexical.$isRootOrShadowRoot(nextParent) && !handled.has(parentKey)) {
216
+ handled.add(parentKey);
217
+ $createListOrMerge(parent, listType);
208
218
  break;
209
- } else {
210
- const nextParent = parent.getParent();
211
- if (lexical.$isRootOrShadowRoot(nextParent) && !handled.has(parentKey)) {
212
- handled.add(parentKey);
213
- $createListOrMerge(parent, listType);
214
- break;
215
- }
216
- parent = nextParent;
217
219
  }
220
+ parent = nextParent;
218
221
  }
219
222
  }
220
223
  }
221
224
  }
222
- });
225
+ }
223
226
  }
224
227
  function append(node, nodesToAppend) {
225
228
  node.splice(node.getChildrenSize(), 0, nodesToAppend);
@@ -231,29 +234,30 @@ function $createListOrMerge(node, listType) {
231
234
  const previousSibling = node.getPreviousSibling();
232
235
  const nextSibling = node.getNextSibling();
233
236
  const listItem = $createListItemNode();
234
- listItem.setFormat(node.getFormatType());
235
- listItem.setIndent(node.getIndent());
236
237
  append(listItem, node.getChildren());
238
+ let targetList;
237
239
  if ($isListNode(previousSibling) && listType === previousSibling.getListType()) {
238
240
  previousSibling.append(listItem);
239
- node.remove();
240
241
  // if the same type of list is on both sides, merge them.
241
-
242
242
  if ($isListNode(nextSibling) && listType === nextSibling.getListType()) {
243
243
  append(previousSibling, nextSibling.getChildren());
244
244
  nextSibling.remove();
245
245
  }
246
- return previousSibling;
246
+ targetList = previousSibling;
247
247
  } else if ($isListNode(nextSibling) && listType === nextSibling.getListType()) {
248
248
  nextSibling.getFirstChildOrThrow().insertBefore(listItem);
249
- node.remove();
250
- return nextSibling;
249
+ targetList = nextSibling;
251
250
  } else {
252
251
  const list = $createListNode(listType);
253
252
  list.append(listItem);
254
253
  node.replace(list);
255
- return list;
254
+ targetList = list;
256
255
  }
256
+ // listItem needs to be attached to root prior to setting indent
257
+ listItem.setFormat(node.getFormatType());
258
+ listItem.setIndent(node.getIndent());
259
+ node.remove();
260
+ return targetList;
257
261
  }
258
262
 
259
263
  /**
@@ -281,55 +285,52 @@ function mergeLists(list1, list2) {
281
285
  * it will remove the whole list, including the ListItemNode. For each ListItemNode in the ListNode,
282
286
  * removeList will also generate new ParagraphNodes in the removed ListNode's place. Any child node
283
287
  * inside a ListItemNode will be appended to the new ParagraphNodes.
284
- * @param editor - The lexical editor.
285
288
  */
286
- function removeList(editor) {
287
- editor.update(() => {
288
- const selection = lexical.$getSelection();
289
- if (lexical.$isRangeSelection(selection)) {
290
- const listNodes = new Set();
291
- const nodes = selection.getNodes();
292
- const anchorNode = selection.anchor.getNode();
293
- if ($isSelectingEmptyListItem(anchorNode, nodes)) {
294
- listNodes.add($getTopListNode(anchorNode));
295
- } else {
296
- for (let i = 0; i < nodes.length; i++) {
297
- const node = nodes[i];
298
- if (lexical.$isLeafNode(node)) {
299
- const listItemNode = LexicalUtils.$getNearestNodeOfType(node, ListItemNode);
300
- if (listItemNode != null) {
301
- listNodes.add($getTopListNode(listItemNode));
302
- }
289
+ function $removeList() {
290
+ const selection = lexical.$getSelection();
291
+ if (lexical.$isRangeSelection(selection)) {
292
+ const listNodes = new Set();
293
+ const nodes = selection.getNodes();
294
+ const anchorNode = selection.anchor.getNode();
295
+ if ($isSelectingEmptyListItem(anchorNode, nodes)) {
296
+ listNodes.add($getTopListNode(anchorNode));
297
+ } else {
298
+ for (let i = 0; i < nodes.length; i++) {
299
+ const node = nodes[i];
300
+ if (lexical.$isLeafNode(node)) {
301
+ const listItemNode = LexicalUtils.$getNearestNodeOfType(node, ListItemNode);
302
+ if (listItemNode != null) {
303
+ listNodes.add($getTopListNode(listItemNode));
303
304
  }
304
305
  }
305
306
  }
306
- for (const listNode of listNodes) {
307
- let insertionPoint = listNode;
308
- const listItems = $getAllListItems(listNode);
309
- for (const listItemNode of listItems) {
310
- const paragraph = lexical.$createParagraphNode();
311
- append(paragraph, listItemNode.getChildren());
312
- insertionPoint.insertAfter(paragraph);
313
- insertionPoint = paragraph;
307
+ }
308
+ for (const listNode of listNodes) {
309
+ let insertionPoint = listNode;
310
+ const listItems = $getAllListItems(listNode);
311
+ for (const listItemNode of listItems) {
312
+ const paragraph = lexical.$createParagraphNode().setTextStyle(selection.style).setTextFormat(selection.format);
313
+ append(paragraph, listItemNode.getChildren());
314
+ insertionPoint.insertAfter(paragraph);
315
+ insertionPoint = paragraph;
314
316
 
315
- // When the anchor and focus fall on the textNode
316
- // we don't have to change the selection because the textNode will be appended to
317
- // the newly generated paragraph.
318
- // When selection is in empty nested list item, selection is actually on the listItemNode.
319
- // When the corresponding listItemNode is deleted and replaced by the newly generated paragraph
320
- // we should manually set the selection's focus and anchor to the newly generated paragraph.
321
- if (listItemNode.__key === selection.anchor.key) {
322
- selection.anchor.set(paragraph.getKey(), 0, 'element');
323
- }
324
- if (listItemNode.__key === selection.focus.key) {
325
- selection.focus.set(paragraph.getKey(), 0, 'element');
326
- }
327
- listItemNode.remove();
317
+ // When the anchor and focus fall on the textNode
318
+ // we don't have to change the selection because the textNode will be appended to
319
+ // the newly generated paragraph.
320
+ // When selection is in empty nested list item, selection is actually on the listItemNode.
321
+ // When the corresponding listItemNode is deleted and replaced by the newly generated paragraph
322
+ // we should manually set the selection's focus and anchor to the newly generated paragraph.
323
+ if (listItemNode.__key === selection.anchor.key) {
324
+ lexical.$setPointFromCaret(selection.anchor, lexical.$normalizeCaret(lexical.$getChildCaret(paragraph, 'next')));
328
325
  }
329
- listNode.remove();
326
+ if (listItemNode.__key === selection.focus.key) {
327
+ lexical.$setPointFromCaret(selection.focus, lexical.$normalizeCaret(lexical.$getChildCaret(paragraph, 'next')));
328
+ }
329
+ listItemNode.remove();
330
330
  }
331
+ listNode.remove();
331
332
  }
332
- });
333
+ }
333
334
  }
334
335
 
335
336
  /**
@@ -417,8 +418,8 @@ function $handleIndent(listItemNode) {
417
418
  // otherwise, we need to create a new nested ListNode
418
419
 
419
420
  if ($isListNode(parent)) {
420
- const newListItem = $createListItemNode();
421
- const newList = $createListNode(parent.getListType());
421
+ const newListItem = $createListItemNode().setTextFormat(parent.getTextFormat()).setTextStyle(parent.getTextStyle());
422
+ const newList = $createListNode(parent.getListType()).setTextFormat(parent.getTextFormat()).setTextStyle(parent.getTextStyle());
422
423
  newListItem.append(newList);
423
424
  newList.append(listItemNode);
424
425
  if (previousSibling) {
@@ -508,7 +509,7 @@ function $handleListInsertParagraph() {
508
509
  const topListNode = $getTopListNode(anchor);
509
510
  const parent = anchor.getParent();
510
511
  if (!$isListNode(parent)) {
511
- throw Error(`A ListItemNode must have a ListNode for a parent.`);
512
+ formatDevErrorMessage(`A ListItemNode must have a ListNode for a parent.`);
512
513
  }
513
514
  const grandparent = parent.getParent();
514
515
  let replacementNode;
@@ -521,21 +522,18 @@ function $handleListInsertParagraph() {
521
522
  } else {
522
523
  return false;
523
524
  }
524
- replacementNode.select();
525
+ replacementNode.setTextStyle(selection.style).setTextFormat(selection.format).select();
525
526
  const nextSiblings = anchor.getNextSiblings();
526
527
  if (nextSiblings.length > 0) {
527
528
  const newList = $createListNode(parent.getListType());
528
- if (lexical.$isParagraphNode(replacementNode)) {
529
- replacementNode.insertAfter(newList);
530
- } else {
529
+ if ($isListItemNode(replacementNode)) {
531
530
  const newListItem = $createListItemNode();
532
531
  newListItem.append(newList);
533
532
  replacementNode.insertAfter(newListItem);
533
+ } else {
534
+ replacementNode.insertAfter(newList);
534
535
  }
535
- nextSiblings.forEach(sibling => {
536
- sibling.remove();
537
- newList.append(sibling);
538
- });
536
+ newList.append(...nextSiblings);
539
537
  }
540
538
 
541
539
  // Don't leave hanging nested empty lists
@@ -563,14 +561,6 @@ function normalizeClassNames(...classNames) {
563
561
  return rval;
564
562
  }
565
563
 
566
- /**
567
- * Copyright (c) Meta Platforms, Inc. and affiliates.
568
- *
569
- * This source code is licensed under the MIT license found in the
570
- * LICENSE file in the root directory of this source tree.
571
- *
572
- */
573
-
574
564
  /** @noInheritDoc */
575
565
  class ListItemNode extends lexical.ElementNode {
576
566
  /** @internal */
@@ -596,6 +586,10 @@ class ListItemNode extends lexical.ElementNode {
596
586
  }
597
587
  element.value = this.__value;
598
588
  $setListItemThemeClassNames(element, config.theme, this);
589
+ const nextStyle = this.__style || this.__textStyle;
590
+ if (nextStyle) {
591
+ element.style.cssText = nextStyle;
592
+ }
599
593
  return element;
600
594
  }
601
595
  updateDOM(prevNode, dom, config) {
@@ -606,12 +600,20 @@ class ListItemNode extends lexical.ElementNode {
606
600
  // @ts-expect-error - this is always HTMLListItemElement
607
601
  dom.value = this.__value;
608
602
  $setListItemThemeClassNames(dom, config.theme, this);
603
+ const prevStyle = prevNode.__style || prevNode.__textStyle;
604
+ const nextStyle = this.__style || this.__textStyle;
605
+ if (prevStyle !== nextStyle) {
606
+ dom.style.cssText = nextStyle;
607
+ if (nextStyle === '') {
608
+ dom.removeAttribute('style');
609
+ }
610
+ }
609
611
  return false;
610
612
  }
611
613
  static transform() {
612
614
  return node => {
613
615
  if (!$isListItemNode(node)) {
614
- throw Error(`node is not a ListItemNode`);
616
+ formatDevErrorMessage(`node is not a ListItemNode`);
615
617
  }
616
618
  if (node.__checked == null) {
617
619
  return;
@@ -633,16 +635,18 @@ class ListItemNode extends lexical.ElementNode {
633
635
  };
634
636
  }
635
637
  static importJSON(serializedNode) {
636
- const node = $createListItemNode();
637
- node.setChecked(serializedNode.checked);
638
- node.setValue(serializedNode.value);
639
- node.setFormat(serializedNode.format);
640
- node.setDirection(serializedNode.direction);
641
- return node;
638
+ return $createListItemNode().updateFromJSON(serializedNode);
639
+ }
640
+ updateFromJSON(serializedNode) {
641
+ return super.updateFromJSON(serializedNode).setValue(serializedNode.value).setChecked(serializedNode.checked);
642
642
  }
643
643
  exportDOM(editor) {
644
644
  const element = this.createDOM(editor._config);
645
645
  element.style.textAlign = this.getFormatType();
646
+ const direction = this.getDirection();
647
+ if (direction) {
648
+ element.dir = direction;
649
+ }
646
650
  return {
647
651
  element
648
652
  };
@@ -651,9 +655,7 @@ class ListItemNode extends lexical.ElementNode {
651
655
  return {
652
656
  ...super.exportJSON(),
653
657
  checked: this.getChecked(),
654
- type: 'listitem',
655
- value: this.getValue(),
656
- version: 1
658
+ value: this.getValue()
657
659
  };
658
660
  }
659
661
  append(...nodes) {
@@ -696,7 +698,7 @@ class ListItemNode extends lexical.ElementNode {
696
698
  }
697
699
  if (includeChildren) {
698
700
  if (!lexical.$isElementNode(replaceWithNode)) {
699
- throw Error(`includeChildren should only be true for ElementNodes`);
701
+ formatDevErrorMessage(`includeChildren should only be true for ElementNodes`);
700
702
  }
701
703
  this.getChildren().forEach(child => {
702
704
  replaceWithNode.append(child);
@@ -712,7 +714,7 @@ class ListItemNode extends lexical.ElementNode {
712
714
  const listNode = this.getParentOrThrow();
713
715
  if (!$isListNode(listNode)) {
714
716
  {
715
- throw Error(`insertAfter: list node is not parent of list item node`);
717
+ formatDevErrorMessage(`insertAfter: list node is not parent of list item node`);
716
718
  }
717
719
  }
718
720
  if ($isListItemNode(node)) {
@@ -739,7 +741,7 @@ class ListItemNode extends lexical.ElementNode {
739
741
  }
740
742
  }
741
743
  insertNewAfter(_, restoreSelection = true) {
742
- const newElement = $createListItemNode(this.__checked == null ? undefined : false);
744
+ const newElement = $createListItemNode().updateFromJSON(this.exportJSON()).setChecked(this.getChecked() ? false : undefined);
743
745
  this.insertAfter(newElement, restoreSelection);
744
746
  return newElement;
745
747
  }
@@ -784,6 +786,7 @@ class ListItemNode extends lexical.ElementNode {
784
786
  setValue(value) {
785
787
  const self = this.getWritable();
786
788
  self.__value = value;
789
+ return self;
787
790
  }
788
791
  getChecked() {
789
792
  const self = this.getLatest();
@@ -797,14 +800,16 @@ class ListItemNode extends lexical.ElementNode {
797
800
  setChecked(checked) {
798
801
  const self = this.getWritable();
799
802
  self.__checked = checked;
803
+ return self;
800
804
  }
801
805
  toggleChecked() {
802
- this.setChecked(!this.__checked);
806
+ const self = this.getWritable();
807
+ return self.setChecked(!self.__checked);
803
808
  }
804
809
  getIndent() {
805
810
  // If we don't have a parent, we are likely serializing
806
811
  const parent = this.getParent();
807
- if (parent === null) {
812
+ if (parent === null || !this.isAttached()) {
808
813
  return this.getLatest().__indent;
809
814
  }
810
815
  // ListItemNode should always have a ListNode for a parent.
@@ -818,11 +823,11 @@ class ListItemNode extends lexical.ElementNode {
818
823
  }
819
824
  setIndent(indent) {
820
825
  if (!(typeof indent === 'number')) {
821
- throw Error(`Invalid indent value.`);
826
+ formatDevErrorMessage(`Invalid indent value.`);
822
827
  }
823
828
  indent = Math.floor(indent);
824
829
  if (!(indent >= 0)) {
825
- throw Error(`Indent value must be non-negative.`);
830
+ formatDevErrorMessage(`Indent value must be non-negative.`);
826
831
  }
827
832
  let currentIndent = this.getIndent();
828
833
  while (currentIndent !== indent) {
@@ -847,7 +852,7 @@ class ListItemNode extends lexical.ElementNode {
847
852
  return $isListItemNode(replacement);
848
853
  }
849
854
  canMergeWith(node) {
850
- return lexical.$isParagraphNode(node) || $isListItemNode(node);
855
+ return $isListItemNode(node) || lexical.$isParagraphNode(node);
851
856
  }
852
857
  extractWithChild(child, selection) {
853
858
  if (!lexical.$isRangeSelection(selection)) {
@@ -968,14 +973,6 @@ function $isListItemNode(node) {
968
973
  return node instanceof ListItemNode;
969
974
  }
970
975
 
971
- /**
972
- * Copyright (c) Meta Platforms, Inc. and affiliates.
973
- *
974
- * This source code is licensed under the MIT license found in the
975
- * LICENSE file in the root directory of this source tree.
976
- *
977
- */
978
-
979
976
  /** @noInheritDoc */
980
977
  class ListNode extends lexical.ElementNode {
981
978
  /** @internal */
@@ -991,7 +988,7 @@ class ListNode extends lexical.ElementNode {
991
988
  const listType = node.__listType || TAG_TO_LIST_TYPE[node.__tag];
992
989
  return new ListNode(listType, node.__start, node.__key);
993
990
  }
994
- constructor(listType, start, key) {
991
+ constructor(listType = 'number', start = 1, key) {
995
992
  super(key);
996
993
  const _listType = TAG_TO_LIST_TYPE[listType] || listType;
997
994
  this.__listType = _listType;
@@ -1005,6 +1002,7 @@ class ListNode extends lexical.ElementNode {
1005
1002
  const writable = this.getWritable();
1006
1003
  writable.__listType = type;
1007
1004
  writable.__tag = type === 'number' ? 'ol' : 'ul';
1005
+ return writable;
1008
1006
  }
1009
1007
  getListType() {
1010
1008
  return this.__listType;
@@ -1012,6 +1010,11 @@ class ListNode extends lexical.ElementNode {
1012
1010
  getStart() {
1013
1011
  return this.__start;
1014
1012
  }
1013
+ setStart(start) {
1014
+ const self = this.getWritable();
1015
+ self.__start = start;
1016
+ return self;
1017
+ }
1015
1018
 
1016
1019
  // View
1017
1020
 
@@ -1036,7 +1039,7 @@ class ListNode extends lexical.ElementNode {
1036
1039
  static transform() {
1037
1040
  return node => {
1038
1041
  if (!$isListNode(node)) {
1039
- throw Error(`node is not a ListNode`);
1042
+ formatDevErrorMessage(`node is not a ListNode`);
1040
1043
  }
1041
1044
  mergeNextSiblingListIfSameType(node);
1042
1045
  updateChildrenListItemValue(node);
@@ -1055,17 +1058,14 @@ class ListNode extends lexical.ElementNode {
1055
1058
  };
1056
1059
  }
1057
1060
  static importJSON(serializedNode) {
1058
- const node = $createListNode(serializedNode.listType, serializedNode.start);
1059
- node.setFormat(serializedNode.format);
1060
- node.setIndent(serializedNode.indent);
1061
- node.setDirection(serializedNode.direction);
1062
- return node;
1061
+ return $createListNode().updateFromJSON(serializedNode);
1062
+ }
1063
+ updateFromJSON(serializedNode) {
1064
+ return super.updateFromJSON(serializedNode).setListType(serializedNode.listType).setStart(serializedNode.start);
1063
1065
  }
1064
1066
  exportDOM(editor) {
1065
- const {
1066
- element
1067
- } = super.exportDOM(editor);
1068
- if (element && LexicalUtils.isHTMLElement(element)) {
1067
+ const element = this.createDOM(editor._config, editor);
1068
+ if (LexicalUtils.isHTMLElement(element)) {
1069
1069
  if (this.__start !== 1) {
1070
1070
  element.setAttribute('start', String(this.__start));
1071
1071
  }
@@ -1082,9 +1082,7 @@ class ListNode extends lexical.ElementNode {
1082
1082
  ...super.exportJSON(),
1083
1083
  listType: this.getListType(),
1084
1084
  start: this.getStart(),
1085
- tag: this.getTag(),
1086
- type: 'list',
1087
- version: 1
1085
+ tag: this.getTag()
1088
1086
  };
1089
1087
  }
1090
1088
  canBeEmpty() {
@@ -1103,8 +1101,12 @@ class ListNode extends lexical.ElementNode {
1103
1101
  if ($isListNode(currentNode)) {
1104
1102
  listItemNode.append(currentNode);
1105
1103
  } else if (lexical.$isElementNode(currentNode)) {
1106
- const textNode = lexical.$createTextNode(currentNode.getTextContent());
1107
- listItemNode.append(textNode);
1104
+ if (currentNode.isInline()) {
1105
+ listItemNode.append(currentNode);
1106
+ } else {
1107
+ const textNode = lexical.$createTextNode(currentNode.getTextContent());
1108
+ listItemNode.append(textNode);
1109
+ }
1108
1110
  } else {
1109
1111
  listItemNode.append(currentNode);
1110
1112
  }
@@ -1233,7 +1235,7 @@ const TAG_TO_LIST_TYPE = {
1233
1235
  * @param start - Where an ordered list starts its count, start = 1 if left undefined.
1234
1236
  * @returns The new ListNode
1235
1237
  */
1236
- function $createListNode(listType, start = 1) {
1238
+ function $createListNode(listType = 'number', start = 1) {
1237
1239
  return lexical.$applyNodeReplacement(new ListNode(listType, start));
1238
1240
  }
1239
1241
 
@@ -1258,13 +1260,86 @@ const INSERT_UNORDERED_LIST_COMMAND = lexical.createCommand('INSERT_UNORDERED_LI
1258
1260
  const INSERT_ORDERED_LIST_COMMAND = lexical.createCommand('INSERT_ORDERED_LIST_COMMAND');
1259
1261
  const INSERT_CHECK_LIST_COMMAND = lexical.createCommand('INSERT_CHECK_LIST_COMMAND');
1260
1262
  const REMOVE_LIST_COMMAND = lexical.createCommand('REMOVE_LIST_COMMAND');
1263
+ function registerList(editor) {
1264
+ const removeListener = LexicalUtils.mergeRegister(editor.registerCommand(INSERT_ORDERED_LIST_COMMAND, () => {
1265
+ $insertList('number');
1266
+ return true;
1267
+ }, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(INSERT_UNORDERED_LIST_COMMAND, () => {
1268
+ $insertList('bullet');
1269
+ return true;
1270
+ }, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(REMOVE_LIST_COMMAND, () => {
1271
+ $removeList();
1272
+ return true;
1273
+ }, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.INSERT_PARAGRAPH_COMMAND, () => $handleListInsertParagraph(), lexical.COMMAND_PRIORITY_LOW), editor.registerNodeTransform(ListItemNode, node => {
1274
+ const firstChild = node.getFirstChild();
1275
+ if (firstChild) {
1276
+ if (lexical.$isTextNode(firstChild)) {
1277
+ const style = firstChild.getStyle();
1278
+ const format = firstChild.getFormat();
1279
+ if (node.getTextStyle() !== style) {
1280
+ node.setTextStyle(style);
1281
+ }
1282
+ if (node.getTextFormat() !== format) {
1283
+ node.setTextFormat(format);
1284
+ }
1285
+ }
1286
+ } else {
1287
+ // If it's empty, check the selection
1288
+ const selection = lexical.$getSelection();
1289
+ if (lexical.$isRangeSelection(selection) && (selection.style !== node.getTextStyle() || selection.format !== node.getTextFormat()) && selection.isCollapsed() && node.is(selection.anchor.getNode())) {
1290
+ node.setTextStyle(selection.style).setTextFormat(selection.format);
1291
+ }
1292
+ }
1293
+ }), editor.registerNodeTransform(lexical.TextNode, node => {
1294
+ const listItemParentNode = node.getParent();
1295
+ if ($isListItemNode(listItemParentNode) && node.is(listItemParentNode.getFirstChild())) {
1296
+ const style = node.getStyle();
1297
+ const format = node.getFormat();
1298
+ if (style !== listItemParentNode.getTextStyle() || format !== listItemParentNode.getTextFormat()) {
1299
+ listItemParentNode.setTextStyle(style).setTextFormat(format);
1300
+ }
1301
+ }
1302
+ }));
1303
+ return removeListener;
1304
+ }
1305
+
1306
+ /**
1307
+ * @deprecated use {@link $insertList} from an update or command listener.
1308
+ *
1309
+ * Inserts a new ListNode. If the selection's anchor node is an empty ListItemNode and is a child of
1310
+ * the root/shadow root, it will replace the ListItemNode with a ListNode and the old ListItemNode.
1311
+ * Otherwise it will replace its parent with a new ListNode and re-insert the ListItemNode and any previous children.
1312
+ * If the selection's anchor node is not an empty ListItemNode, it will add a new ListNode or merge an existing ListNode,
1313
+ * unless the the node is a leaf node, in which case it will attempt to find a ListNode up the branch and replace it with
1314
+ * a new ListNode, or create a new ListNode at the nearest root/shadow root.
1315
+ * @param editor - The lexical editor.
1316
+ * @param listType - The type of list, "number" | "bullet" | "check".
1317
+ */
1318
+ function insertList(editor, listType) {
1319
+ editor.update(() => $insertList(listType));
1320
+ }
1321
+
1322
+ /**
1323
+ * @deprecated use {@link $removeList} from an update or command listener.
1324
+ *
1325
+ * Searches for the nearest ancestral ListNode and removes it. If selection is an empty ListItemNode
1326
+ * it will remove the whole list, including the ListItemNode. For each ListItemNode in the ListNode,
1327
+ * removeList will also generate new ParagraphNodes in the removed ListNode's place. Any child node
1328
+ * inside a ListItemNode will be appended to the new ParagraphNodes.
1329
+ * @param editor - The lexical editor.
1330
+ */
1331
+ function removeList(editor) {
1332
+ editor.update(() => $removeList());
1333
+ }
1261
1334
 
1262
1335
  exports.$createListItemNode = $createListItemNode;
1263
1336
  exports.$createListNode = $createListNode;
1264
1337
  exports.$getListDepth = $getListDepth;
1265
1338
  exports.$handleListInsertParagraph = $handleListInsertParagraph;
1339
+ exports.$insertList = $insertList;
1266
1340
  exports.$isListItemNode = $isListItemNode;
1267
1341
  exports.$isListNode = $isListNode;
1342
+ exports.$removeList = $removeList;
1268
1343
  exports.INSERT_CHECK_LIST_COMMAND = INSERT_CHECK_LIST_COMMAND;
1269
1344
  exports.INSERT_ORDERED_LIST_COMMAND = INSERT_ORDERED_LIST_COMMAND;
1270
1345
  exports.INSERT_UNORDERED_LIST_COMMAND = INSERT_UNORDERED_LIST_COMMAND;
@@ -1272,4 +1347,5 @@ exports.ListItemNode = ListItemNode;
1272
1347
  exports.ListNode = ListNode;
1273
1348
  exports.REMOVE_LIST_COMMAND = REMOVE_LIST_COMMAND;
1274
1349
  exports.insertList = insertList;
1350
+ exports.registerList = registerList;
1275
1351
  exports.removeList = removeList;