@react-fabric/lexical 1.0.0-beta.6 → 1.0.0-beta.60

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 (76) hide show
  1. package/{src/editor/Lexical.module.css → css/_lexical.css} +8 -4
  2. package/css/styles.css +24 -0
  3. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +62 -20
  4. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +1 -1
  5. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +11 -11
  6. package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +643 -245
  7. package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +1 -1
  8. package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +29 -22
  9. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +13 -6
  10. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +1 -1
  11. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +3 -3
  12. package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +242 -141
  13. package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +1 -1
  14. package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +11 -10
  15. package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +594 -255
  16. package/dist/cjs/node_modules/@lexical/list/LexicalList.js +1 -1
  17. package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +23 -16
  18. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +102 -53
  19. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +1 -1
  20. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +10 -10
  21. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +275 -194
  22. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +5 -1
  23. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +28 -18
  24. package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +2293 -878
  25. package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +1 -1
  26. package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +53 -40
  27. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +471 -193
  28. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +13 -1
  29. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +47 -32
  30. package/dist/cjs/node_modules/prismjs/prism.js +1 -1
  31. package/dist/cjs/packages/lexical/src/components/StickyComponent.js +1 -1
  32. package/dist/cjs/packages/lexical/src/editor/LexicalEditor.js +30 -5
  33. package/dist/cjs/packages/lexical/src/hooks/useFormatWatcher.js +86 -83
  34. package/dist/cjs/packages/lexical/src/plugins/ToolbarPlugin.js +25 -17
  35. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +62 -20
  36. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +1 -1
  37. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +3 -3
  38. package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +637 -246
  39. package/dist/esm/node_modules/@lexical/code/LexicalCode.js +1 -1
  40. package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +4 -4
  41. package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +14 -7
  42. package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +1 -1
  43. package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +3 -3
  44. package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +244 -144
  45. package/dist/esm/node_modules/@lexical/link/LexicalLink.js +1 -1
  46. package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +4 -4
  47. package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +590 -257
  48. package/dist/esm/node_modules/@lexical/list/LexicalList.js +1 -1
  49. package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +5 -4
  50. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +104 -55
  51. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +1 -1
  52. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +4 -4
  53. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +267 -196
  54. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +4 -2
  55. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +4 -4
  56. package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +2283 -881
  57. package/dist/esm/node_modules/@lexical/table/LexicalTable.js +1 -1
  58. package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +4 -4
  59. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +457 -194
  60. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +8 -2
  61. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +3 -3
  62. package/dist/esm/node_modules/prismjs/prism.js +1 -1
  63. package/dist/esm/packages/lexical/src/components/StickyComponent.js +1 -1
  64. package/dist/esm/packages/lexical/src/editor/LexicalEditor.js +30 -5
  65. package/dist/esm/packages/lexical/src/hooks/useFormatWatcher.js +88 -85
  66. package/dist/esm/packages/lexical/src/plugins/ToolbarPlugin.js +25 -17
  67. package/dist/types/editor/LexicalEditor.d.ts +25 -0
  68. package/dist/types/hooks/useFormatWatcher.d.ts +2 -2
  69. package/dist/types/nodes/StickyNode.d.ts +0 -1
  70. package/package.json +14 -13
  71. package/src/components/StickyComponent.tsx +2 -2
  72. package/src/editor/LexicalEditor.tsx +57 -26
  73. package/src/hooks/useFormatWatcher.ts +35 -31
  74. package/src/plugins/ToolbarPlugin.tsx +27 -17
  75. package/dist/cjs/editor/Lexical.module.css +0 -1
  76. package/dist/esm/editor/Lexical.module.css +0 -1
@@ -1,7 +1,8 @@
1
1
  'use client';
2
2
 
3
- import { $getSelection, $isRangeSelection, $isRootOrShadowRoot, $isElementNode, $isLeafNode, $createParagraphNode, $isParagraphNode, ElementNode, $applyNodeReplacement, $createTextNode, createCommand } from 'lexical';
4
- import { $getNearestNodeOfType, removeClassNamesFromElement, addClassNamesToElement, isHTMLElement } from '../utils/LexicalUtils.js';
3
+ import { $getNearestNodeOfType, removeClassNamesFromElement, addClassNamesToElement, isHTMLElement, mergeRegister, $findMatchingParent, calculateZoomLevel } from '../utils/LexicalUtils.js';
4
+ import { $getSelection, $isRangeSelection, $isRootOrShadowRoot, $createParagraphNode, $isElementNode, $isLeafNode, $setPointFromCaret, $normalizeCaret, $getChildCaret, ElementNode, buildImportMap, $isParagraphNode, $applyNodeReplacement, $createTextNode, createCommand, COMMAND_PRIORITY_LOW, KEY_ARROW_DOWN_COMMAND, KEY_ARROW_UP_COMMAND, KEY_ESCAPE_COMMAND, KEY_SPACE_COMMAND, $getNearestNodeFromDOMNode, KEY_ARROW_LEFT_COMMAND, $getNodeByKey, INSERT_PARAGRAPH_COMMAND, $isTextNode, TextNode, getNearestEditorFromDOMNode } from 'lexical';
5
+ import { getStyleObjectFromCSS } from '../selection/LexicalSelection.js';
5
6
 
6
7
  /**
7
8
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -20,6 +21,11 @@ import { $getNearestNodeOfType, removeClassNamesFromElement, addClassNamesToElem
20
21
  *
21
22
  */
22
23
 
24
+ // Do not require this module directly! Use normal `invariant` calls.
25
+
26
+ function formatDevErrorMessage(message) {
27
+ throw new Error(message);
28
+ }
23
29
 
24
30
  /**
25
31
  * Checks the depth of listNode from the root node.
@@ -38,7 +44,7 @@ function $getListDepth(listNode) {
38
44
  continue;
39
45
  }
40
46
  {
41
- throw Error(`A ListItemNode must have a ListNode for a parent.`);
47
+ formatDevErrorMessage(`A ListItemNode must have a ListNode for a parent.`);
42
48
  }
43
49
  }
44
50
  return depth;
@@ -55,7 +61,7 @@ function $getTopListNode(listItem) {
55
61
  let list = listItem.getParent();
56
62
  if (!$isListNode(list)) {
57
63
  {
58
- throw Error(`A ListItemNode must have a ListNode for a parent.`);
64
+ formatDevErrorMessage(`A ListItemNode must have a ListNode for a parent.`);
59
65
  }
60
66
  }
61
67
  let parent = list;
@@ -116,7 +122,7 @@ function $removeHighestEmptyListParent(sublist) {
116
122
  let emptyListPtr = sublist;
117
123
  while (emptyListPtr.getNextSibling() == null && emptyListPtr.getPreviousSibling() == null) {
118
124
  const parent = emptyListPtr.getParent();
119
- if (parent == null || !($isListItemNode(emptyListPtr) || $isListNode(emptyListPtr))) {
125
+ if (parent == null || !($isListItemNode(parent) || $isListNode(parent))) {
120
126
  break;
121
127
  }
122
128
  emptyListPtr = parent;
@@ -134,14 +140,6 @@ function $wrapInListItem(node) {
134
140
  return listItemWrapper.append(node);
135
141
  }
136
142
 
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
143
  function $isSelectingEmptyListItem(anchorNode, nodes) {
146
144
  return $isListItemNode(anchorNode) && (nodes.length === 0 || nodes.length === 1 && anchorNode.is(nodes[0]) && anchorNode.getChildrenSize() === 0);
147
145
  }
@@ -153,73 +151,77 @@ function $isSelectingEmptyListItem(anchorNode, nodes) {
153
151
  * If the selection's anchor node is not an empty ListItemNode, it will add a new ListNode or merge an existing ListNode,
154
152
  * 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
153
  * a new ListNode, or create a new ListNode at the nearest root/shadow root.
156
- * @param editor - The lexical editor.
157
154
  * @param listType - The type of list, "number" | "bullet" | "check".
158
155
  */
159
- function insertList(editor, listType) {
160
- editor.update(() => {
161
- const selection = $getSelection();
162
- if (selection !== null) {
163
- const nodes = selection.getNodes();
164
- if ($isRangeSelection(selection)) {
165
- const anchorAndFocus = selection.getStartEndPoints();
166
- if (!(anchorAndFocus !== null)) {
167
- throw Error(`insertList: anchor should be defined`);
156
+ function $insertList(listType) {
157
+ const selection = $getSelection();
158
+ if (selection !== null) {
159
+ let nodes = selection.getNodes();
160
+ if ($isRangeSelection(selection)) {
161
+ const anchorAndFocus = selection.getStartEndPoints();
162
+ if (!(anchorAndFocus !== null)) {
163
+ formatDevErrorMessage(`insertList: anchor should be defined`);
164
+ }
165
+ const [anchor] = anchorAndFocus;
166
+ const anchorNode = anchor.getNode();
167
+ const anchorNodeParent = anchorNode.getParent();
168
+ if ($isRootOrShadowRoot(anchorNode)) {
169
+ const firstChild = anchorNode.getFirstChild();
170
+ if (firstChild) {
171
+ nodes = firstChild.selectStart().getNodes();
172
+ } else {
173
+ const paragraph = $createParagraphNode();
174
+ anchorNode.append(paragraph);
175
+ nodes = paragraph.select().getNodes();
168
176
  }
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 ($isRootOrShadowRoot(anchorNodeParent)) {
175
- anchorNode.replace(list);
176
- const listItem = $createListItemNode();
177
- if ($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);
177
+ } else if ($isSelectingEmptyListItem(anchorNode, nodes)) {
178
+ const list = $createListNode(listType);
179
+ if ($isRootOrShadowRoot(anchorNodeParent)) {
180
+ anchorNode.replace(list);
181
+ const listItem = $createListItemNode();
182
+ if ($isElementNode(anchorNode)) {
183
+ listItem.setFormat(anchorNode.getFormatType());
184
+ listItem.setIndent(anchorNode.getIndent());
186
185
  }
187
- return;
186
+ list.append(listItem);
187
+ } else if ($isListItemNode(anchorNode)) {
188
+ const parent = anchorNode.getParentOrThrow();
189
+ append(list, parent.getChildren());
190
+ parent.replace(list);
188
191
  }
192
+ return;
189
193
  }
190
- const handled = new Set();
191
- for (let i = 0; i < nodes.length; i++) {
192
- const node = nodes[i];
193
- if ($isElementNode(node) && node.isEmpty() && !$isListItemNode(node) && !handled.has(node.getKey())) {
194
- $createListOrMerge(node, listType);
195
- continue;
196
- }
197
- if ($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
- }
208
- break;
209
- } else {
210
- const nextParent = parent.getParent();
211
- if ($isRootOrShadowRoot(nextParent) && !handled.has(parentKey)) {
212
- handled.add(parentKey);
213
- $createListOrMerge(parent, listType);
214
- break;
215
- }
216
- parent = nextParent;
217
- }
194
+ }
195
+ const handled = new Set();
196
+ for (let i = 0; i < nodes.length; i++) {
197
+ const node = nodes[i];
198
+ if ($isElementNode(node) && node.isEmpty() && !$isListItemNode(node) && !handled.has(node.getKey())) {
199
+ $createListOrMerge(node, listType);
200
+ continue;
201
+ }
202
+ let parent = $isLeafNode(node) ? node.getParent() : $isListItemNode(node) && node.isEmpty() ? node : null;
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 ($isRootOrShadowRoot(nextParent) && !handled.has(parentKey)) {
216
+ handled.add(parentKey);
217
+ $createListOrMerge(parent, listType);
218
+ break;
218
219
  }
220
+ parent = nextParent;
219
221
  }
220
222
  }
221
223
  }
222
- });
224
+ }
223
225
  }
224
226
  function append(node, nodesToAppend) {
225
227
  node.splice(node.getChildrenSize(), 0, nodesToAppend);
@@ -231,29 +233,30 @@ function $createListOrMerge(node, listType) {
231
233
  const previousSibling = node.getPreviousSibling();
232
234
  const nextSibling = node.getNextSibling();
233
235
  const listItem = $createListItemNode();
234
- listItem.setFormat(node.getFormatType());
235
- listItem.setIndent(node.getIndent());
236
236
  append(listItem, node.getChildren());
237
+ let targetList;
237
238
  if ($isListNode(previousSibling) && listType === previousSibling.getListType()) {
238
239
  previousSibling.append(listItem);
239
- node.remove();
240
240
  // if the same type of list is on both sides, merge them.
241
-
242
241
  if ($isListNode(nextSibling) && listType === nextSibling.getListType()) {
243
242
  append(previousSibling, nextSibling.getChildren());
244
243
  nextSibling.remove();
245
244
  }
246
- return previousSibling;
245
+ targetList = previousSibling;
247
246
  } else if ($isListNode(nextSibling) && listType === nextSibling.getListType()) {
248
247
  nextSibling.getFirstChildOrThrow().insertBefore(listItem);
249
- node.remove();
250
- return nextSibling;
248
+ targetList = nextSibling;
251
249
  } else {
252
250
  const list = $createListNode(listType);
253
251
  list.append(listItem);
254
252
  node.replace(list);
255
- return list;
253
+ targetList = list;
256
254
  }
255
+ // listItem needs to be attached to root prior to setting indent
256
+ listItem.setFormat(node.getFormatType());
257
+ listItem.setIndent(node.getIndent());
258
+ node.remove();
259
+ return targetList;
257
260
  }
258
261
 
259
262
  /**
@@ -281,55 +284,52 @@ function mergeLists(list1, list2) {
281
284
  * it will remove the whole list, including the ListItemNode. For each ListItemNode in the ListNode,
282
285
  * removeList will also generate new ParagraphNodes in the removed ListNode's place. Any child node
283
286
  * inside a ListItemNode will be appended to the new ParagraphNodes.
284
- * @param editor - The lexical editor.
285
287
  */
286
- function removeList(editor) {
287
- editor.update(() => {
288
- const selection = $getSelection();
289
- if ($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 ($isLeafNode(node)) {
299
- const listItemNode = $getNearestNodeOfType(node, ListItemNode);
300
- if (listItemNode != null) {
301
- listNodes.add($getTopListNode(listItemNode));
302
- }
288
+ function $removeList() {
289
+ const selection = $getSelection();
290
+ if ($isRangeSelection(selection)) {
291
+ const listNodes = new Set();
292
+ const nodes = selection.getNodes();
293
+ const anchorNode = selection.anchor.getNode();
294
+ if ($isSelectingEmptyListItem(anchorNode, nodes)) {
295
+ listNodes.add($getTopListNode(anchorNode));
296
+ } else {
297
+ for (let i = 0; i < nodes.length; i++) {
298
+ const node = nodes[i];
299
+ if ($isLeafNode(node)) {
300
+ const listItemNode = $getNearestNodeOfType(node, ListItemNode);
301
+ if (listItemNode != null) {
302
+ listNodes.add($getTopListNode(listItemNode));
303
303
  }
304
304
  }
305
305
  }
306
- for (const listNode of listNodes) {
307
- let insertionPoint = listNode;
308
- const listItems = $getAllListItems(listNode);
309
- for (const listItemNode of listItems) {
310
- const paragraph = $createParagraphNode();
311
- append(paragraph, listItemNode.getChildren());
312
- insertionPoint.insertAfter(paragraph);
313
- insertionPoint = paragraph;
306
+ }
307
+ for (const listNode of listNodes) {
308
+ let insertionPoint = listNode;
309
+ const listItems = $getAllListItems(listNode);
310
+ for (const listItemNode of listItems) {
311
+ const paragraph = $createParagraphNode().setTextStyle(selection.style).setTextFormat(selection.format);
312
+ append(paragraph, listItemNode.getChildren());
313
+ insertionPoint.insertAfter(paragraph);
314
+ insertionPoint = paragraph;
314
315
 
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();
316
+ // When the anchor and focus fall on the textNode
317
+ // we don't have to change the selection because the textNode will be appended to
318
+ // the newly generated paragraph.
319
+ // When selection is in empty nested list item, selection is actually on the listItemNode.
320
+ // When the corresponding listItemNode is deleted and replaced by the newly generated paragraph
321
+ // we should manually set the selection's focus and anchor to the newly generated paragraph.
322
+ if (listItemNode.__key === selection.anchor.key) {
323
+ $setPointFromCaret(selection.anchor, $normalizeCaret($getChildCaret(paragraph, 'next')));
328
324
  }
329
- listNode.remove();
325
+ if (listItemNode.__key === selection.focus.key) {
326
+ $setPointFromCaret(selection.focus, $normalizeCaret($getChildCaret(paragraph, 'next')));
327
+ }
328
+ listItemNode.remove();
330
329
  }
330
+ listNode.remove();
331
331
  }
332
- });
332
+ }
333
333
  }
334
334
 
335
335
  /**
@@ -417,8 +417,8 @@ function $handleIndent(listItemNode) {
417
417
  // otherwise, we need to create a new nested ListNode
418
418
 
419
419
  if ($isListNode(parent)) {
420
- const newListItem = $createListItemNode();
421
- const newList = $createListNode(parent.getListType());
420
+ const newListItem = $createListItemNode().setTextFormat(listItemNode.getTextFormat()).setTextStyle(listItemNode.getTextStyle());
421
+ const newList = $createListNode(parent.getListType()).setTextFormat(parent.getTextFormat()).setTextStyle(parent.getTextStyle());
422
422
  newListItem.append(newList);
423
423
  newList.append(listItemNode);
424
424
  if (previousSibling) {
@@ -492,7 +492,7 @@ function $handleOutdent(listItemNode) {
492
492
  * (which should be the parent node) and insert the ParagraphNode as a sibling to the ListNode. If the ListNode is
493
493
  * nested in a ListItemNode instead, it will add the ParagraphNode after the grandparent ListItemNode.
494
494
  * Throws an invariant if the selection is not a child of a ListNode.
495
- * @returns true if a ParagraphNode was inserted succesfully, false if there is no selection
495
+ * @returns true if a ParagraphNode was inserted successfully, false if there is no selection
496
496
  * or the selection does not contain a ListItemNode or the node already holds text.
497
497
  */
498
498
  function $handleListInsertParagraph() {
@@ -508,7 +508,7 @@ function $handleListInsertParagraph() {
508
508
  const topListNode = $getTopListNode(anchor);
509
509
  const parent = anchor.getParent();
510
510
  if (!$isListNode(parent)) {
511
- throw Error(`A ListItemNode must have a ListNode for a parent.`);
511
+ formatDevErrorMessage(`A ListItemNode must have a ListNode for a parent.`);
512
512
  }
513
513
  const grandparent = parent.getParent();
514
514
  let replacementNode;
@@ -521,21 +521,18 @@ function $handleListInsertParagraph() {
521
521
  } else {
522
522
  return false;
523
523
  }
524
- replacementNode.select();
524
+ replacementNode.setTextStyle(selection.style).setTextFormat(selection.format).select();
525
525
  const nextSiblings = anchor.getNextSiblings();
526
526
  if (nextSiblings.length > 0) {
527
527
  const newList = $createListNode(parent.getListType());
528
- if ($isParagraphNode(replacementNode)) {
529
- replacementNode.insertAfter(newList);
530
- } else {
528
+ if ($isListItemNode(replacementNode)) {
531
529
  const newListItem = $createListItemNode();
532
530
  newListItem.append(newList);
533
531
  replacementNode.insertAfter(newListItem);
532
+ } else {
533
+ replacementNode.insertAfter(newList);
534
534
  }
535
- nextSiblings.forEach(sibling => {
536
- sibling.remove();
537
- newList.append(sibling);
538
- });
535
+ newList.append(...nextSiblings);
539
536
  }
540
537
 
541
538
  // Don't leave hanging nested empty lists
@@ -563,13 +560,19 @@ function normalizeClassNames(...classNames) {
563
560
  return rval;
564
561
  }
565
562
 
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
- */
563
+ function applyMarkerStyles(dom, node, prevNode) {
564
+ const styles = getStyleObjectFromCSS(node.__textStyle);
565
+ for (const k in styles) {
566
+ dom.style.setProperty(`--listitem-marker-${k}`, styles[k]);
567
+ }
568
+ if (prevNode) {
569
+ for (const k in getStyleObjectFromCSS(prevNode.__textStyle)) {
570
+ if (!(k in styles)) {
571
+ dom.style.removeProperty(`--listitem-marker-${k}`);
572
+ }
573
+ }
574
+ }
575
+ }
573
576
 
574
577
  /** @noInheritDoc */
575
578
  class ListItemNode extends ElementNode {
@@ -577,72 +580,81 @@ class ListItemNode extends ElementNode {
577
580
 
578
581
  /** @internal */
579
582
 
580
- static getType() {
581
- return 'listitem';
582
- }
583
- static clone(node) {
584
- return new ListItemNode(node.__value, node.__checked, node.__key);
583
+ /** @internal */
584
+ $config() {
585
+ return this.config('listitem', {
586
+ $transform: node => {
587
+ if (node.__checked == null) {
588
+ return;
589
+ }
590
+ const parent = node.getParent();
591
+ if ($isListNode(parent)) {
592
+ if (parent.getListType() !== 'check' && node.getChecked() != null) {
593
+ node.setChecked(undefined);
594
+ }
595
+ }
596
+ },
597
+ extends: ElementNode,
598
+ importDOM: buildImportMap({
599
+ li: () => ({
600
+ conversion: $convertListItemElement,
601
+ priority: 0
602
+ })
603
+ })
604
+ });
585
605
  }
586
- constructor(value, checked, key) {
606
+ constructor(value = 1, checked = undefined, key) {
587
607
  super(key);
588
608
  this.__value = value === undefined ? 1 : value;
589
609
  this.__checked = checked;
590
610
  }
611
+ afterCloneFrom(prevNode) {
612
+ super.afterCloneFrom(prevNode);
613
+ this.__value = prevNode.__value;
614
+ this.__checked = prevNode.__checked;
615
+ }
591
616
  createDOM(config) {
592
617
  const element = document.createElement('li');
593
- const parent = this.getParent();
594
- if ($isListNode(parent) && parent.getListType() === 'check') {
595
- updateListItemChecked(element, this, null);
596
- }
597
- element.value = this.__value;
598
- $setListItemThemeClassNames(element, config.theme, this);
618
+ this.updateListItemDOM(null, element, config);
599
619
  return element;
600
620
  }
601
- updateDOM(prevNode, dom, config) {
621
+ updateListItemDOM(prevNode, dom, config) {
602
622
  const parent = this.getParent();
603
623
  if ($isListNode(parent) && parent.getListType() === 'check') {
604
624
  updateListItemChecked(dom, this, prevNode);
605
625
  }
606
- // @ts-expect-error - this is always HTMLListItemElement
607
626
  dom.value = this.__value;
608
627
  $setListItemThemeClassNames(dom, config.theme, this);
609
- return false;
610
- }
611
- static transform() {
612
- return node => {
613
- if (!$isListItemNode(node)) {
614
- throw Error(`node is not a ListItemNode`);
615
- }
616
- if (node.__checked == null) {
617
- return;
618
- }
619
- const parent = node.getParent();
620
- if ($isListNode(parent)) {
621
- if (parent.getListType() !== 'check' && node.getChecked() != null) {
622
- node.setChecked(undefined);
623
- }
628
+ const prevStyle = prevNode ? prevNode.__style : '';
629
+ const nextStyle = this.__style;
630
+ if (prevStyle !== nextStyle) {
631
+ if (nextStyle === '') {
632
+ dom.removeAttribute('style');
633
+ } else {
634
+ dom.style.cssText = nextStyle;
624
635
  }
625
- };
636
+ }
637
+ applyMarkerStyles(dom, this, prevNode);
626
638
  }
627
- static importDOM() {
628
- return {
629
- li: () => ({
630
- conversion: $convertListItemElement,
631
- priority: 0
632
- })
633
- };
639
+ updateDOM(prevNode, dom, config) {
640
+ // @ts-expect-error - this is always HTMLListItemElement
641
+ const element = dom;
642
+ this.updateListItemDOM(prevNode, element, config);
643
+ return false;
634
644
  }
635
- 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;
645
+ updateFromJSON(serializedNode) {
646
+ return super.updateFromJSON(serializedNode).setValue(serializedNode.value).setChecked(serializedNode.checked);
642
647
  }
643
648
  exportDOM(editor) {
644
649
  const element = this.createDOM(editor._config);
645
- element.style.textAlign = this.getFormatType();
650
+ const formatType = this.getFormatType();
651
+ if (formatType) {
652
+ element.style.textAlign = formatType;
653
+ }
654
+ const direction = this.getDirection();
655
+ if (direction) {
656
+ element.dir = direction;
657
+ }
646
658
  return {
647
659
  element
648
660
  };
@@ -651,9 +663,7 @@ class ListItemNode extends ElementNode {
651
663
  return {
652
664
  ...super.exportJSON(),
653
665
  checked: this.getChecked(),
654
- type: 'listitem',
655
- value: this.getValue(),
656
- version: 1
666
+ value: this.getValue()
657
667
  };
658
668
  }
659
669
  append(...nodes) {
@@ -696,7 +706,7 @@ class ListItemNode extends ElementNode {
696
706
  }
697
707
  if (includeChildren) {
698
708
  if (!$isElementNode(replaceWithNode)) {
699
- throw Error(`includeChildren should only be true for ElementNodes`);
709
+ formatDevErrorMessage(`includeChildren should only be true for ElementNodes`);
700
710
  }
701
711
  this.getChildren().forEach(child => {
702
712
  replaceWithNode.append(child);
@@ -712,7 +722,7 @@ class ListItemNode extends ElementNode {
712
722
  const listNode = this.getParentOrThrow();
713
723
  if (!$isListNode(listNode)) {
714
724
  {
715
- throw Error(`insertAfter: list node is not parent of list item node`);
725
+ formatDevErrorMessage(`insertAfter: list node is not parent of list item node`);
716
726
  }
717
727
  }
718
728
  if ($isListItemNode(node)) {
@@ -739,7 +749,7 @@ class ListItemNode extends ElementNode {
739
749
  }
740
750
  }
741
751
  insertNewAfter(_, restoreSelection = true) {
742
- const newElement = $createListItemNode(this.__checked == null ? undefined : false);
752
+ const newElement = $createListItemNode().updateFromJSON(this.exportJSON()).setChecked(this.getChecked() ? false : undefined);
743
753
  this.insertAfter(newElement, restoreSelection);
744
754
  return newElement;
745
755
  }
@@ -784,6 +794,7 @@ class ListItemNode extends ElementNode {
784
794
  setValue(value) {
785
795
  const self = this.getWritable();
786
796
  self.__value = value;
797
+ return self;
787
798
  }
788
799
  getChecked() {
789
800
  const self = this.getLatest();
@@ -797,14 +808,16 @@ class ListItemNode extends ElementNode {
797
808
  setChecked(checked) {
798
809
  const self = this.getWritable();
799
810
  self.__checked = checked;
811
+ return self;
800
812
  }
801
813
  toggleChecked() {
802
- this.setChecked(!this.__checked);
814
+ const self = this.getWritable();
815
+ return self.setChecked(!self.__checked);
803
816
  }
804
817
  getIndent() {
805
818
  // If we don't have a parent, we are likely serializing
806
819
  const parent = this.getParent();
807
- if (parent === null) {
820
+ if (parent === null || !this.isAttached()) {
808
821
  return this.getLatest().__indent;
809
822
  }
810
823
  // ListItemNode should always have a ListNode for a parent.
@@ -818,11 +831,11 @@ class ListItemNode extends ElementNode {
818
831
  }
819
832
  setIndent(indent) {
820
833
  if (!(typeof indent === 'number')) {
821
- throw Error(`Invalid indent value.`);
834
+ formatDevErrorMessage(`Invalid indent value.`);
822
835
  }
823
836
  indent = Math.floor(indent);
824
837
  if (!(indent >= 0)) {
825
- throw Error(`Indent value must be non-negative.`);
838
+ formatDevErrorMessage(`Indent value must be non-negative.`);
826
839
  }
827
840
  let currentIndent = this.getIndent();
828
841
  while (currentIndent !== indent) {
@@ -847,7 +860,7 @@ class ListItemNode extends ElementNode {
847
860
  return $isListItemNode(replacement);
848
861
  }
849
862
  canMergeWith(node) {
850
- return $isParagraphNode(node) || $isListItemNode(node);
863
+ return $isListItemNode(node) || $isParagraphNode(node);
851
864
  }
852
865
  extractWithChild(child, selection) {
853
866
  if (!$isRangeSelection(selection)) {
@@ -984,33 +997,58 @@ class ListNode extends ElementNode {
984
997
 
985
998
  /** @internal */
986
999
 
987
- static getType() {
988
- return 'list';
989
- }
990
- static clone(node) {
991
- const listType = node.__listType || TAG_TO_LIST_TYPE[node.__tag];
992
- return new ListNode(listType, node.__start, node.__key);
1000
+ /** @internal */
1001
+ $config() {
1002
+ return this.config('list', {
1003
+ $transform: node => {
1004
+ mergeNextSiblingListIfSameType(node);
1005
+ updateChildrenListItemValue(node);
1006
+ },
1007
+ extends: ElementNode,
1008
+ importDOM: buildImportMap({
1009
+ ol: () => ({
1010
+ conversion: $convertListNode,
1011
+ priority: 0
1012
+ }),
1013
+ ul: () => ({
1014
+ conversion: $convertListNode,
1015
+ priority: 0
1016
+ })
1017
+ })
1018
+ });
993
1019
  }
994
- constructor(listType, start, key) {
1020
+ constructor(listType = 'number', start = 1, key) {
995
1021
  super(key);
996
1022
  const _listType = TAG_TO_LIST_TYPE[listType] || listType;
997
1023
  this.__listType = _listType;
998
1024
  this.__tag = _listType === 'number' ? 'ol' : 'ul';
999
1025
  this.__start = start;
1000
1026
  }
1027
+ afterCloneFrom(prevNode) {
1028
+ super.afterCloneFrom(prevNode);
1029
+ this.__listType = prevNode.__listType;
1030
+ this.__tag = prevNode.__tag;
1031
+ this.__start = prevNode.__start;
1032
+ }
1001
1033
  getTag() {
1002
- return this.__tag;
1034
+ return this.getLatest().__tag;
1003
1035
  }
1004
1036
  setListType(type) {
1005
1037
  const writable = this.getWritable();
1006
1038
  writable.__listType = type;
1007
1039
  writable.__tag = type === 'number' ? 'ol' : 'ul';
1040
+ return writable;
1008
1041
  }
1009
1042
  getListType() {
1010
- return this.__listType;
1043
+ return this.getLatest().__listType;
1011
1044
  }
1012
1045
  getStart() {
1013
- return this.__start;
1046
+ return this.getLatest().__start;
1047
+ }
1048
+ setStart(start) {
1049
+ const self = this.getWritable();
1050
+ self.__start = start;
1051
+ return self;
1014
1052
  }
1015
1053
 
1016
1054
  // View
@@ -1033,39 +1071,12 @@ class ListNode extends ElementNode {
1033
1071
  $setListThemeClassNames(dom, config.theme, this);
1034
1072
  return false;
1035
1073
  }
1036
- static transform() {
1037
- return node => {
1038
- if (!$isListNode(node)) {
1039
- throw Error(`node is not a ListNode`);
1040
- }
1041
- mergeNextSiblingListIfSameType(node);
1042
- updateChildrenListItemValue(node);
1043
- };
1044
- }
1045
- static importDOM() {
1046
- return {
1047
- ol: () => ({
1048
- conversion: $convertListNode,
1049
- priority: 0
1050
- }),
1051
- ul: () => ({
1052
- conversion: $convertListNode,
1053
- priority: 0
1054
- })
1055
- };
1056
- }
1057
- 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;
1074
+ updateFromJSON(serializedNode) {
1075
+ return super.updateFromJSON(serializedNode).setListType(serializedNode.listType).setStart(serializedNode.start);
1063
1076
  }
1064
1077
  exportDOM(editor) {
1065
- const {
1066
- element
1067
- } = super.exportDOM(editor);
1068
- if (element && isHTMLElement(element)) {
1078
+ const element = this.createDOM(editor._config, editor);
1079
+ if (isHTMLElement(element)) {
1069
1080
  if (this.__start !== 1) {
1070
1081
  element.setAttribute('start', String(this.__start));
1071
1082
  }
@@ -1082,9 +1093,7 @@ class ListNode extends ElementNode {
1082
1093
  ...super.exportJSON(),
1083
1094
  listType: this.getListType(),
1084
1095
  start: this.getStart(),
1085
- tag: this.getTag(),
1086
- type: 'list',
1087
- version: 1
1096
+ tag: this.getTag()
1088
1097
  };
1089
1098
  }
1090
1099
  canBeEmpty() {
@@ -1093,25 +1102,18 @@ class ListNode extends ElementNode {
1093
1102
  canIndent() {
1094
1103
  return false;
1095
1104
  }
1096
- append(...nodesToAppend) {
1097
- for (let i = 0; i < nodesToAppend.length; i++) {
1098
- const currentNode = nodesToAppend[i];
1099
- if ($isListItemNode(currentNode)) {
1100
- super.append(currentNode);
1101
- } else {
1102
- const listItemNode = $createListItemNode();
1103
- if ($isListNode(currentNode)) {
1104
- listItemNode.append(currentNode);
1105
- } else if ($isElementNode(currentNode)) {
1106
- const textNode = $createTextNode(currentNode.getTextContent());
1107
- listItemNode.append(textNode);
1108
- } else {
1109
- listItemNode.append(currentNode);
1105
+ splice(start, deleteCount, nodesToInsert) {
1106
+ let listItemNodesToInsert = nodesToInsert;
1107
+ for (let i = 0; i < nodesToInsert.length; i++) {
1108
+ const node = nodesToInsert[i];
1109
+ if (!$isListItemNode(node)) {
1110
+ if (listItemNodesToInsert === nodesToInsert) {
1111
+ listItemNodesToInsert = [...nodesToInsert];
1110
1112
  }
1111
- super.append(listItemNode);
1113
+ listItemNodesToInsert[i] = $createListItemNode().append($isElementNode(node) && !($isListNode(node) || node.isInline()) ? $createTextNode(node.getTextContent()) : node);
1112
1114
  }
1113
1115
  }
1114
- return this;
1116
+ return super.splice(start, deleteCount, listItemNodesToInsert);
1115
1117
  }
1116
1118
  extractWithChild(child) {
1117
1119
  return $isListItemNode(child);
@@ -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 $applyNodeReplacement(new ListNode(listType, start));
1238
1240
  }
1239
1241
 
@@ -1254,9 +1256,340 @@ function $isListNode(node) {
1254
1256
  *
1255
1257
  */
1256
1258
 
1259
+ const INSERT_CHECK_LIST_COMMAND = createCommand('INSERT_CHECK_LIST_COMMAND');
1260
+ function registerCheckList(editor) {
1261
+ return mergeRegister(editor.registerCommand(INSERT_CHECK_LIST_COMMAND, () => {
1262
+ $insertList('check');
1263
+ return true;
1264
+ }, COMMAND_PRIORITY_LOW), editor.registerCommand(KEY_ARROW_DOWN_COMMAND, event => {
1265
+ return handleArrowUpOrDown(event, editor, false);
1266
+ }, COMMAND_PRIORITY_LOW), editor.registerCommand(KEY_ARROW_UP_COMMAND, event => {
1267
+ return handleArrowUpOrDown(event, editor, true);
1268
+ }, COMMAND_PRIORITY_LOW), editor.registerCommand(KEY_ESCAPE_COMMAND, () => {
1269
+ const activeItem = getActiveCheckListItem();
1270
+ if (activeItem != null) {
1271
+ const rootElement = editor.getRootElement();
1272
+ if (rootElement != null) {
1273
+ rootElement.focus();
1274
+ }
1275
+ return true;
1276
+ }
1277
+ return false;
1278
+ }, COMMAND_PRIORITY_LOW), editor.registerCommand(KEY_SPACE_COMMAND, event => {
1279
+ const activeItem = getActiveCheckListItem();
1280
+ if (activeItem != null && editor.isEditable()) {
1281
+ editor.update(() => {
1282
+ const listItemNode = $getNearestNodeFromDOMNode(activeItem);
1283
+ if ($isListItemNode(listItemNode)) {
1284
+ event.preventDefault();
1285
+ listItemNode.toggleChecked();
1286
+ }
1287
+ });
1288
+ return true;
1289
+ }
1290
+ return false;
1291
+ }, COMMAND_PRIORITY_LOW), editor.registerCommand(KEY_ARROW_LEFT_COMMAND, event => {
1292
+ return editor.getEditorState().read(() => {
1293
+ const selection = $getSelection();
1294
+ if ($isRangeSelection(selection) && selection.isCollapsed()) {
1295
+ const {
1296
+ anchor
1297
+ } = selection;
1298
+ const isElement = anchor.type === 'element';
1299
+ if (isElement || anchor.offset === 0) {
1300
+ const anchorNode = anchor.getNode();
1301
+ const elementNode = $findMatchingParent(anchorNode, node => $isElementNode(node) && !node.isInline());
1302
+ if ($isListItemNode(elementNode)) {
1303
+ const parent = elementNode.getParent();
1304
+ if ($isListNode(parent) && parent.getListType() === 'check' && (isElement || elementNode.getFirstDescendant() === anchorNode)) {
1305
+ const domNode = editor.getElementByKey(elementNode.__key);
1306
+ if (domNode != null && document.activeElement !== domNode) {
1307
+ domNode.focus();
1308
+ event.preventDefault();
1309
+ return true;
1310
+ }
1311
+ }
1312
+ }
1313
+ }
1314
+ }
1315
+ return false;
1316
+ });
1317
+ }, COMMAND_PRIORITY_LOW), editor.registerRootListener((rootElement, prevElement) => {
1318
+ if (rootElement !== null) {
1319
+ rootElement.addEventListener('click', handleClick);
1320
+ rootElement.addEventListener('pointerdown', handlePointerDown);
1321
+ }
1322
+ if (prevElement !== null) {
1323
+ prevElement.removeEventListener('click', handleClick);
1324
+ prevElement.removeEventListener('pointerdown', handlePointerDown);
1325
+ }
1326
+ }));
1327
+ }
1328
+ function handleCheckItemEvent(event, callback) {
1329
+ const target = event.target;
1330
+ if (!isHTMLElement(target)) {
1331
+ return;
1332
+ }
1333
+
1334
+ // Ignore clicks on LI that have nested lists
1335
+ const firstChild = target.firstChild;
1336
+ if (isHTMLElement(firstChild) && (firstChild.tagName === 'UL' || firstChild.tagName === 'OL')) {
1337
+ return;
1338
+ }
1339
+ const parentNode = target.parentNode;
1340
+
1341
+ // @ts-ignore internal field
1342
+ if (!parentNode || parentNode.__lexicalListType !== 'check') {
1343
+ return;
1344
+ }
1345
+ const rect = target.getBoundingClientRect();
1346
+ const zoom = calculateZoomLevel(target);
1347
+ const clientX = event.clientX / zoom;
1348
+
1349
+ // Use getComputedStyle if available, otherwise fallback to 0px width
1350
+ const beforeStyles = window.getComputedStyle ? window.getComputedStyle(target, '::before') : {
1351
+ width: '0px'
1352
+ };
1353
+ const beforeWidthInPixels = parseFloat(beforeStyles.width);
1354
+
1355
+ // Make click area slightly larger for touch devices to improve accessibility
1356
+ const isTouchEvent = event.pointerType === 'touch';
1357
+ const clickAreaPadding = isTouchEvent ? 32 : 0; // Add 32px padding for touch events
1358
+
1359
+ if (target.dir === 'rtl' ? clientX < rect.right + clickAreaPadding && clientX > rect.right - beforeWidthInPixels - clickAreaPadding : clientX > rect.left - clickAreaPadding && clientX < rect.left + beforeWidthInPixels + clickAreaPadding) {
1360
+ callback();
1361
+ }
1362
+ }
1363
+ function handleClick(event) {
1364
+ handleCheckItemEvent(event, () => {
1365
+ if (isHTMLElement(event.target)) {
1366
+ const domNode = event.target;
1367
+ const editor = getNearestEditorFromDOMNode(domNode);
1368
+ if (editor != null && editor.isEditable()) {
1369
+ editor.update(() => {
1370
+ const node = $getNearestNodeFromDOMNode(domNode);
1371
+ if ($isListItemNode(node)) {
1372
+ domNode.focus();
1373
+ node.toggleChecked();
1374
+ }
1375
+ });
1376
+ }
1377
+ }
1378
+ });
1379
+ }
1380
+ function handlePointerDown(event) {
1381
+ handleCheckItemEvent(event, () => {
1382
+ // Prevents caret moving when clicking on check mark
1383
+ event.preventDefault();
1384
+ });
1385
+ }
1386
+ function getActiveCheckListItem() {
1387
+ const activeElement = document.activeElement;
1388
+ return isHTMLElement(activeElement) && activeElement.tagName === 'LI' && activeElement.parentNode != null &&
1389
+ // @ts-ignore internal field
1390
+ activeElement.parentNode.__lexicalListType === 'check' ? activeElement : null;
1391
+ }
1392
+ function findCheckListItemSibling(node, backward) {
1393
+ let sibling = backward ? node.getPreviousSibling() : node.getNextSibling();
1394
+ let parent = node;
1395
+
1396
+ // Going up in a tree to get non-null sibling
1397
+ while (sibling == null && $isListItemNode(parent)) {
1398
+ // Get li -> parent ul/ol -> parent li
1399
+ parent = parent.getParentOrThrow().getParent();
1400
+ if (parent != null) {
1401
+ sibling = backward ? parent.getPreviousSibling() : parent.getNextSibling();
1402
+ }
1403
+ }
1404
+
1405
+ // Going down in a tree to get first non-nested list item
1406
+ while ($isListItemNode(sibling)) {
1407
+ const firstChild = backward ? sibling.getLastChild() : sibling.getFirstChild();
1408
+ if (!$isListNode(firstChild)) {
1409
+ return sibling;
1410
+ }
1411
+ sibling = backward ? firstChild.getLastChild() : firstChild.getFirstChild();
1412
+ }
1413
+ return null;
1414
+ }
1415
+ function handleArrowUpOrDown(event, editor, backward) {
1416
+ const activeItem = getActiveCheckListItem();
1417
+ if (activeItem != null) {
1418
+ editor.update(() => {
1419
+ const listItem = $getNearestNodeFromDOMNode(activeItem);
1420
+ if (!$isListItemNode(listItem)) {
1421
+ return;
1422
+ }
1423
+ const nextListItem = findCheckListItemSibling(listItem, backward);
1424
+ if (nextListItem != null) {
1425
+ nextListItem.selectStart();
1426
+ const dom = editor.getElementByKey(nextListItem.__key);
1427
+ if (dom != null) {
1428
+ event.preventDefault();
1429
+ setTimeout(() => {
1430
+ dom.focus();
1431
+ }, 0);
1432
+ }
1433
+ }
1434
+ });
1435
+ }
1436
+ return false;
1437
+ }
1438
+
1439
+ /**
1440
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
1441
+ *
1442
+ * This source code is licensed under the MIT license found in the
1443
+ * LICENSE file in the root directory of this source tree.
1444
+ *
1445
+ */
1446
+
1447
+ const UPDATE_LIST_START_COMMAND = createCommand('UPDATE_LIST_START_COMMAND');
1257
1448
  const INSERT_UNORDERED_LIST_COMMAND = createCommand('INSERT_UNORDERED_LIST_COMMAND');
1258
1449
  const INSERT_ORDERED_LIST_COMMAND = createCommand('INSERT_ORDERED_LIST_COMMAND');
1259
- const INSERT_CHECK_LIST_COMMAND = createCommand('INSERT_CHECK_LIST_COMMAND');
1260
1450
  const REMOVE_LIST_COMMAND = createCommand('REMOVE_LIST_COMMAND');
1451
+ function registerList(editor) {
1452
+ const removeListener = mergeRegister(editor.registerCommand(INSERT_ORDERED_LIST_COMMAND, () => {
1453
+ $insertList('number');
1454
+ return true;
1455
+ }, COMMAND_PRIORITY_LOW), editor.registerCommand(UPDATE_LIST_START_COMMAND, payload => {
1456
+ const {
1457
+ listNodeKey,
1458
+ newStart
1459
+ } = payload;
1460
+ const listNode = $getNodeByKey(listNodeKey);
1461
+ if (!$isListNode(listNode)) {
1462
+ return false;
1463
+ }
1464
+ if (listNode.getListType() === 'number') {
1465
+ listNode.setStart(newStart);
1466
+ updateChildrenListItemValue(listNode);
1467
+ }
1468
+ return true;
1469
+ }, COMMAND_PRIORITY_LOW), editor.registerCommand(INSERT_UNORDERED_LIST_COMMAND, () => {
1470
+ $insertList('bullet');
1471
+ return true;
1472
+ }, COMMAND_PRIORITY_LOW), editor.registerCommand(REMOVE_LIST_COMMAND, () => {
1473
+ $removeList();
1474
+ return true;
1475
+ }, COMMAND_PRIORITY_LOW), editor.registerCommand(INSERT_PARAGRAPH_COMMAND, () => $handleListInsertParagraph(), COMMAND_PRIORITY_LOW), editor.registerNodeTransform(ListItemNode, node => {
1476
+ const firstChild = node.getFirstChild();
1477
+ if (firstChild) {
1478
+ if ($isTextNode(firstChild)) {
1479
+ const style = firstChild.getStyle();
1480
+ const format = firstChild.getFormat();
1481
+ if (node.getTextStyle() !== style) {
1482
+ node.setTextStyle(style);
1483
+ }
1484
+ if (node.getTextFormat() !== format) {
1485
+ node.setTextFormat(format);
1486
+ }
1487
+ }
1488
+ } else {
1489
+ // If it's empty, check the selection
1490
+ const selection = $getSelection();
1491
+ if ($isRangeSelection(selection) && (selection.style !== node.getTextStyle() || selection.format !== node.getTextFormat()) && selection.isCollapsed() && node.is(selection.anchor.getNode())) {
1492
+ node.setTextStyle(selection.style).setTextFormat(selection.format);
1493
+ }
1494
+ }
1495
+ }), editor.registerNodeTransform(TextNode, node => {
1496
+ const listItemParentNode = node.getParent();
1497
+ if ($isListItemNode(listItemParentNode) && node.is(listItemParentNode.getFirstChild())) {
1498
+ const style = node.getStyle();
1499
+ const format = node.getFormat();
1500
+ if (style !== listItemParentNode.getTextStyle() || format !== listItemParentNode.getTextFormat()) {
1501
+ listItemParentNode.setTextStyle(style).setTextFormat(format);
1502
+ }
1503
+ }
1504
+ }));
1505
+ return removeListener;
1506
+ }
1507
+ function registerListStrictIndentTransform(editor) {
1508
+ const $formatListIndentStrict = listItemNode => {
1509
+ const listNode = listItemNode.getParent();
1510
+ if ($isListNode(listItemNode.getFirstChild()) || !$isListNode(listNode)) {
1511
+ return;
1512
+ }
1513
+ const startingListItemNode = $findMatchingParent(listItemNode, node => $isListItemNode(node) && $isListNode(node.getParent()) && $isListItemNode(node.getPreviousSibling()));
1514
+ if (startingListItemNode === null && listItemNode.getIndent() > 0) {
1515
+ listItemNode.setIndent(0);
1516
+ } else if ($isListItemNode(startingListItemNode)) {
1517
+ const prevListItemNode = startingListItemNode.getPreviousSibling();
1518
+ if ($isListItemNode(prevListItemNode)) {
1519
+ const endListItemNode = $findChildrenEndListItemNode(prevListItemNode);
1520
+ const endListNode = endListItemNode.getParent();
1521
+ if ($isListNode(endListNode)) {
1522
+ const prevDepth = $getListDepth(endListNode);
1523
+ const depth = $getListDepth(listNode);
1524
+ if (prevDepth + 1 < depth) {
1525
+ listItemNode.setIndent(prevDepth);
1526
+ }
1527
+ }
1528
+ }
1529
+ }
1530
+ };
1531
+ const $processListWithStrictIndent = listNode => {
1532
+ const queue = [listNode];
1533
+ while (queue.length > 0) {
1534
+ const node = queue.shift();
1535
+ if (!$isListNode(node)) {
1536
+ continue;
1537
+ }
1538
+ for (const child of node.getChildren()) {
1539
+ if ($isListItemNode(child)) {
1540
+ $formatListIndentStrict(child);
1541
+ const firstChild = child.getFirstChild();
1542
+ if ($isListNode(firstChild)) {
1543
+ queue.push(firstChild);
1544
+ }
1545
+ }
1546
+ }
1547
+ }
1548
+ };
1549
+ return editor.registerNodeTransform(ListNode, $processListWithStrictIndent);
1550
+ }
1551
+ function $findChildrenEndListItemNode(listItemNode) {
1552
+ let current = listItemNode;
1553
+ let firstChild = current.getFirstChild();
1554
+ while ($isListNode(firstChild)) {
1555
+ const lastChild = firstChild.getLastChild();
1556
+ if ($isListItemNode(lastChild)) {
1557
+ current = lastChild;
1558
+ firstChild = current.getFirstChild();
1559
+ } else {
1560
+ break;
1561
+ }
1562
+ }
1563
+ return current;
1564
+ }
1565
+
1566
+ /**
1567
+ * @deprecated use {@link $insertList} from an update or command listener.
1568
+ *
1569
+ * Inserts a new ListNode. If the selection's anchor node is an empty ListItemNode and is a child of
1570
+ * the root/shadow root, it will replace the ListItemNode with a ListNode and the old ListItemNode.
1571
+ * Otherwise it will replace its parent with a new ListNode and re-insert the ListItemNode and any previous children.
1572
+ * If the selection's anchor node is not an empty ListItemNode, it will add a new ListNode or merge an existing ListNode,
1573
+ * 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
1574
+ * a new ListNode, or create a new ListNode at the nearest root/shadow root.
1575
+ * @param editor - The lexical editor.
1576
+ * @param listType - The type of list, "number" | "bullet" | "check".
1577
+ */
1578
+ function insertList(editor, listType) {
1579
+ editor.update(() => $insertList(listType));
1580
+ }
1581
+
1582
+ /**
1583
+ * @deprecated use {@link $removeList} from an update or command listener.
1584
+ *
1585
+ * Searches for the nearest ancestral ListNode and removes it. If selection is an empty ListItemNode
1586
+ * it will remove the whole list, including the ListItemNode. For each ListItemNode in the ListNode,
1587
+ * removeList will also generate new ParagraphNodes in the removed ListNode's place. Any child node
1588
+ * inside a ListItemNode will be appended to the new ParagraphNodes.
1589
+ * @param editor - The lexical editor.
1590
+ */
1591
+ function removeList(editor) {
1592
+ editor.update(() => $removeList());
1593
+ }
1261
1594
 
1262
- export { $createListItemNode, $createListNode, $getListDepth, $handleListInsertParagraph, $isListItemNode, $isListNode, INSERT_CHECK_LIST_COMMAND, INSERT_ORDERED_LIST_COMMAND, INSERT_UNORDERED_LIST_COMMAND, ListItemNode, ListNode, REMOVE_LIST_COMMAND, insertList, removeList };
1595
+ export { $createListItemNode, $createListNode, $getListDepth, $handleListInsertParagraph, $insertList, $isListItemNode, $isListNode, $removeList, INSERT_CHECK_LIST_COMMAND, INSERT_ORDERED_LIST_COMMAND, INSERT_UNORDERED_LIST_COMMAND, ListItemNode, ListNode, REMOVE_LIST_COMMAND, UPDATE_LIST_START_COMMAND, insertList, registerCheckList, registerList, registerListStrictIndentTransform, removeList };