@react-fabric/lexical 1.0.0-beta.9 → 1.0.2

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 (148) hide show
  1. package/{src/editor/Lexical.module.css → css/_lexical.css} +79 -13
  2. package/css/styles.css +24 -0
  3. package/dist/cjs/{packages/lexical/src/editor → editor}/LexicalEditor.js +98 -45
  4. package/dist/cjs/{packages/lexical/src/hooks → hooks}/useFormatWatcher.js +147 -140
  5. package/dist/cjs/i18n/ar/lexical.json.js +30 -0
  6. package/dist/cjs/i18n/en/lexical.json.js +30 -0
  7. package/{src/plugins/StickyPlugin.ts → dist/cjs/i18n/index.js} +10 -15
  8. package/dist/cjs/index.js +36 -0
  9. package/dist/cjs/plugins/CommentPlugin.js +481 -0
  10. package/dist/cjs/{packages/lexical/src/plugins → plugins}/DraggableBlockPlugin.js +17 -20
  11. package/dist/cjs/{packages/lexical/src/plugins → plugins}/ToolbarPlugin.js +58 -37
  12. package/dist/esm/{packages/lexical/src/editor → editor}/LexicalEditor.js +101 -48
  13. package/dist/esm/{packages/lexical/src/hooks → hooks}/useFormatWatcher.js +95 -88
  14. package/dist/esm/i18n/ar/lexical.json.js +25 -0
  15. package/dist/esm/i18n/en/lexical.json.js +25 -0
  16. package/dist/esm/i18n/index.js +33 -0
  17. package/dist/esm/index.js +34 -0
  18. package/dist/esm/plugins/CommentPlugin.js +481 -0
  19. package/dist/esm/{packages/lexical/src/plugins → plugins}/DraggableBlockPlugin.js +15 -18
  20. package/dist/esm/{packages/lexical/src/plugins → plugins}/ToolbarPlugin.js +59 -38
  21. package/dist/types/editor/LexicalEditor.d.ts +33 -4
  22. package/dist/types/hooks/useFormatWatcher.d.ts +7 -4
  23. package/dist/types/i18n/index.d.ts +1 -0
  24. package/dist/types/index.d.ts +27 -0
  25. package/dist/types/plugins/CommentPlugin.d.ts +5 -0
  26. package/dist/types/plugins/DraggableBlockPlugin.d.ts +3 -2
  27. package/dist/types/plugins/ToolbarPlugin.d.ts +1 -1
  28. package/dist/types/utils/index.d.ts +3 -6
  29. package/package.json +18 -18
  30. package/src/editor/LexicalEditor.tsx +95 -55
  31. package/src/hooks/useFormatWatcher.ts +50 -74
  32. package/src/i18n/ar/lexical.json +18 -0
  33. package/src/i18n/en/lexical.json +18 -0
  34. package/src/i18n/index.ts +28 -0
  35. package/src/index.ts +24 -17
  36. package/src/plugins/CommentPlugin.tsx +523 -0
  37. package/src/plugins/DraggableBlockPlugin.tsx +23 -68
  38. package/src/plugins/ToolbarPlugin.tsx +83 -131
  39. package/src/utils/index.ts +12 -108
  40. package/dist/cjs/_virtual/prism.js +0 -5
  41. package/dist/cjs/editor/Lexical.module.css +0 -1
  42. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +0 -432
  43. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +0 -21
  44. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +0 -27
  45. package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +0 -1139
  46. package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +0 -25
  47. package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +0 -50
  48. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +0 -245
  49. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +0 -19
  50. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +0 -18
  51. package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +0 -471
  52. package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +0 -17
  53. package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +0 -24
  54. package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +0 -1275
  55. package/dist/cjs/node_modules/@lexical/list/LexicalList.js +0 -29
  56. package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +0 -29
  57. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +0 -789
  58. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +0 -29
  59. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +0 -26
  60. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +0 -976
  61. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +0 -35
  62. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +0 -35
  63. package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +0 -2792
  64. package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +0 -23
  65. package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +0 -55
  66. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +0 -888
  67. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +0 -35
  68. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +0 -66
  69. package/dist/cjs/node_modules/@mdi/js/mdi.js +0 -8
  70. package/dist/cjs/node_modules/prismjs/components/prism-c.js +0 -82
  71. package/dist/cjs/node_modules/prismjs/components/prism-clike.js +0 -33
  72. package/dist/cjs/node_modules/prismjs/components/prism-cpp.js +0 -101
  73. package/dist/cjs/node_modules/prismjs/components/prism-css.js +0 -66
  74. package/dist/cjs/node_modules/prismjs/components/prism-java.js +0 -126
  75. package/dist/cjs/node_modules/prismjs/components/prism-javascript.js +0 -174
  76. package/dist/cjs/node_modules/prismjs/components/prism-markdown.js +0 -417
  77. package/dist/cjs/node_modules/prismjs/components/prism-markup.js +0 -188
  78. package/dist/cjs/node_modules/prismjs/components/prism-objectivec.js +0 -14
  79. package/dist/cjs/node_modules/prismjs/components/prism-powershell.js +0 -60
  80. package/dist/cjs/node_modules/prismjs/components/prism-python.js +0 -67
  81. package/dist/cjs/node_modules/prismjs/components/prism-rust.js +0 -130
  82. package/dist/cjs/node_modules/prismjs/components/prism-sql.js +0 -34
  83. package/dist/cjs/node_modules/prismjs/components/prism-swift.js +0 -150
  84. package/dist/cjs/node_modules/prismjs/components/prism-typescript.js +0 -62
  85. package/dist/cjs/node_modules/prismjs/prism.js +0 -1951
  86. package/dist/cjs/packages/lexical/src/components/StickyComponent.js +0 -221
  87. package/dist/cjs/packages/lexical/src/index.js +0 -7
  88. package/dist/cjs/packages/lexical/src/nodes/StickyNode.js +0 -218
  89. package/dist/cjs/packages/lexical/src/plugins/StickyPlugin.js +0 -20
  90. package/dist/esm/_virtual/prism.js +0 -5
  91. package/dist/esm/editor/Lexical.module.css +0 -1
  92. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +0 -423
  93. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +0 -19
  94. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +0 -18
  95. package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +0 -1121
  96. package/dist/esm/node_modules/@lexical/code/LexicalCode.js +0 -21
  97. package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +0 -32
  98. package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +0 -244
  99. package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +0 -18
  100. package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +0 -17
  101. package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +0 -463
  102. package/dist/esm/node_modules/@lexical/link/LexicalLink.js +0 -17
  103. package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +0 -16
  104. package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +0 -1262
  105. package/dist/esm/node_modules/@lexical/list/LexicalList.js +0 -23
  106. package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +0 -16
  107. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +0 -781
  108. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +0 -23
  109. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +0 -18
  110. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +0 -957
  111. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +0 -26
  112. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +0 -16
  113. package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +0 -2754
  114. package/dist/esm/node_modules/@lexical/table/LexicalTable.js +0 -20
  115. package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +0 -17
  116. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +0 -839
  117. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +0 -26
  118. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +0 -17
  119. package/dist/esm/node_modules/@mdi/js/mdi.js +0 -7
  120. package/dist/esm/node_modules/prismjs/components/prism-c.js +0 -82
  121. package/dist/esm/node_modules/prismjs/components/prism-clike.js +0 -33
  122. package/dist/esm/node_modules/prismjs/components/prism-cpp.js +0 -101
  123. package/dist/esm/node_modules/prismjs/components/prism-css.js +0 -66
  124. package/dist/esm/node_modules/prismjs/components/prism-java.js +0 -126
  125. package/dist/esm/node_modules/prismjs/components/prism-javascript.js +0 -174
  126. package/dist/esm/node_modules/prismjs/components/prism-markdown.js +0 -417
  127. package/dist/esm/node_modules/prismjs/components/prism-markup.js +0 -188
  128. package/dist/esm/node_modules/prismjs/components/prism-objectivec.js +0 -14
  129. package/dist/esm/node_modules/prismjs/components/prism-powershell.js +0 -60
  130. package/dist/esm/node_modules/prismjs/components/prism-python.js +0 -67
  131. package/dist/esm/node_modules/prismjs/components/prism-rust.js +0 -130
  132. package/dist/esm/node_modules/prismjs/components/prism-sql.js +0 -34
  133. package/dist/esm/node_modules/prismjs/components/prism-swift.js +0 -150
  134. package/dist/esm/node_modules/prismjs/components/prism-typescript.js +0 -62
  135. package/dist/esm/node_modules/prismjs/prism.js +0 -1951
  136. package/dist/esm/packages/lexical/src/components/StickyComponent.js +0 -221
  137. package/dist/esm/packages/lexical/src/index.js +0 -3
  138. package/dist/esm/packages/lexical/src/nodes/StickyNode.js +0 -216
  139. package/dist/esm/packages/lexical/src/plugins/StickyPlugin.js +0 -20
  140. package/dist/types/components/StickyComponent.d.ts +0 -9
  141. package/dist/types/nodes/StickyNode.d.ts +0 -35
  142. package/dist/types/plugins/StickyPlugin.d.ts +0 -1
  143. package/src/components/StickyComponent.tsx +0 -271
  144. package/src/nodes/StickyNode.tsx +0 -249
  145. /package/dist/cjs/{packages/lexical/src/editor → editor}/theme.js +0 -0
  146. /package/dist/cjs/{packages/lexical/src/utils → utils}/index.js +0 -0
  147. /package/dist/esm/{packages/lexical/src/editor → editor}/theme.js +0 -0
  148. /package/dist/esm/{packages/lexical/src/utils → utils}/index.js +0 -0
@@ -1,1275 +0,0 @@
1
- 'use strict';
2
-
3
- var lexical = require('lexical');
4
- var LexicalUtils = require('../utils/LexicalUtils.js');
5
-
6
- /**
7
- * Copyright (c) Meta Platforms, Inc. and affiliates.
8
- *
9
- * This source code is licensed under the MIT license found in the
10
- * LICENSE file in the root directory of this source tree.
11
- *
12
- */
13
-
14
-
15
- /**
16
- * Copyright (c) Meta Platforms, Inc. and affiliates.
17
- *
18
- * This source code is licensed under the MIT license found in the
19
- * LICENSE file in the root directory of this source tree.
20
- *
21
- */
22
-
23
-
24
- /**
25
- * Checks the depth of listNode from the root node.
26
- * @param listNode - The ListNode to be checked.
27
- * @returns The depth of the ListNode.
28
- */
29
- function $getListDepth(listNode) {
30
- let depth = 1;
31
- let parent = listNode.getParent();
32
- while (parent != null) {
33
- if ($isListItemNode(parent)) {
34
- const parentList = parent.getParent();
35
- if ($isListNode(parentList)) {
36
- depth++;
37
- parent = parentList.getParent();
38
- continue;
39
- }
40
- {
41
- throw Error(`A ListItemNode must have a ListNode for a parent.`);
42
- }
43
- }
44
- return depth;
45
- }
46
- return depth;
47
- }
48
-
49
- /**
50
- * Finds the nearest ancestral ListNode and returns it, throws an invariant if listItem is not a ListItemNode.
51
- * @param listItem - The node to be checked.
52
- * @returns The ListNode found.
53
- */
54
- function $getTopListNode(listItem) {
55
- let list = listItem.getParent();
56
- if (!$isListNode(list)) {
57
- {
58
- throw Error(`A ListItemNode must have a ListNode for a parent.`);
59
- }
60
- }
61
- let parent = list;
62
- while (parent !== null) {
63
- parent = parent.getParent();
64
- if ($isListNode(parent)) {
65
- list = parent;
66
- }
67
- }
68
- return list;
69
- }
70
-
71
- /**
72
- * A recursive Depth-First Search (Postorder Traversal) that finds all of a node's children
73
- * that are of type ListItemNode and returns them in an array.
74
- * @param node - The ListNode to start the search.
75
- * @returns An array containing all nodes of type ListItemNode found.
76
- */
77
- // This should probably be $getAllChildrenOfType
78
- function $getAllListItems(node) {
79
- let listItemNodes = [];
80
- const listChildren = node.getChildren().filter($isListItemNode);
81
- for (let i = 0; i < listChildren.length; i++) {
82
- const listItemNode = listChildren[i];
83
- const firstChild = listItemNode.getFirstChild();
84
- if ($isListNode(firstChild)) {
85
- listItemNodes = listItemNodes.concat($getAllListItems(firstChild));
86
- } else {
87
- listItemNodes.push(listItemNode);
88
- }
89
- }
90
- return listItemNodes;
91
- }
92
-
93
- /**
94
- * Checks to see if the passed node is a ListItemNode and has a ListNode as a child.
95
- * @param node - The node to be checked.
96
- * @returns true if the node is a ListItemNode and has a ListNode child, false otherwise.
97
- */
98
- function isNestedListNode(node) {
99
- return $isListItemNode(node) && $isListNode(node.getFirstChild());
100
- }
101
-
102
- /**
103
- * Takes a deeply nested ListNode or ListItemNode and traverses up the branch to delete the first
104
- * ancestral ListNode (which could be the root ListNode) or ListItemNode with siblings, essentially
105
- * bringing the deeply nested node up the branch once. Would remove sublist if it has siblings.
106
- * Should not break ListItem -> List -> ListItem chain as empty List/ItemNodes should be removed on .remove().
107
- * @param sublist - The nested ListNode or ListItemNode to be brought up the branch.
108
- */
109
- function $removeHighestEmptyListParent(sublist) {
110
- // Nodes may be repeatedly indented, to create deeply nested lists that each
111
- // contain just one bullet.
112
- // Our goal is to remove these (empty) deeply nested lists. The easiest
113
- // way to do that is crawl back up the tree until we find a node that has siblings
114
- // (e.g. is actually part of the list contents) and delete that, or delete
115
- // the root of the list (if no list nodes have siblings.)
116
- let emptyListPtr = sublist;
117
- while (emptyListPtr.getNextSibling() == null && emptyListPtr.getPreviousSibling() == null) {
118
- const parent = emptyListPtr.getParent();
119
- if (parent == null || !($isListItemNode(emptyListPtr) || $isListNode(emptyListPtr))) {
120
- break;
121
- }
122
- emptyListPtr = parent;
123
- }
124
- emptyListPtr.remove();
125
- }
126
-
127
- /**
128
- * Wraps a node into a ListItemNode.
129
- * @param node - The node to be wrapped into a ListItemNode
130
- * @returns The ListItemNode which the passed node is wrapped in.
131
- */
132
- function $wrapInListItem(node) {
133
- const listItemWrapper = $createListItemNode();
134
- return listItemWrapper.append(node);
135
- }
136
-
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
- function $isSelectingEmptyListItem(anchorNode, nodes) {
146
- return $isListItemNode(anchorNode) && (nodes.length === 0 || nodes.length === 1 && anchorNode.is(nodes[0]) && anchorNode.getChildrenSize() === 0);
147
- }
148
-
149
- /**
150
- * Inserts a new ListNode. If the selection's anchor node is an empty ListItemNode and is a child of
151
- * the root/shadow root, it will replace the ListItemNode with a ListNode and the old ListItemNode.
152
- * Otherwise it will replace its parent with a new ListNode and re-insert the ListItemNode and any previous children.
153
- * If the selection's anchor node is not an empty ListItemNode, it will add a new ListNode or merge an existing ListNode,
154
- * 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
- * a new ListNode, or create a new ListNode at the nearest root/shadow root.
156
- * @param editor - The lexical editor.
157
- * @param listType - The type of list, "number" | "bullet" | "check".
158
- */
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`);
168
- }
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);
186
- }
187
- return;
188
- }
189
- }
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
- }
208
- 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
- }
218
- }
219
- }
220
- }
221
- }
222
- });
223
- }
224
- function append(node, nodesToAppend) {
225
- node.splice(node.getChildrenSize(), 0, nodesToAppend);
226
- }
227
- function $createListOrMerge(node, listType) {
228
- if ($isListNode(node)) {
229
- return node;
230
- }
231
- const previousSibling = node.getPreviousSibling();
232
- const nextSibling = node.getNextSibling();
233
- const listItem = $createListItemNode();
234
- listItem.setFormat(node.getFormatType());
235
- listItem.setIndent(node.getIndent());
236
- append(listItem, node.getChildren());
237
- if ($isListNode(previousSibling) && listType === previousSibling.getListType()) {
238
- previousSibling.append(listItem);
239
- node.remove();
240
- // if the same type of list is on both sides, merge them.
241
-
242
- if ($isListNode(nextSibling) && listType === nextSibling.getListType()) {
243
- append(previousSibling, nextSibling.getChildren());
244
- nextSibling.remove();
245
- }
246
- return previousSibling;
247
- } else if ($isListNode(nextSibling) && listType === nextSibling.getListType()) {
248
- nextSibling.getFirstChildOrThrow().insertBefore(listItem);
249
- node.remove();
250
- return nextSibling;
251
- } else {
252
- const list = $createListNode(listType);
253
- list.append(listItem);
254
- node.replace(list);
255
- return list;
256
- }
257
- }
258
-
259
- /**
260
- * A recursive function that goes through each list and their children, including nested lists,
261
- * appending list2 children after list1 children and updating ListItemNode values.
262
- * @param list1 - The first list to be merged.
263
- * @param list2 - The second list to be merged.
264
- */
265
- function mergeLists(list1, list2) {
266
- const listItem1 = list1.getLastChild();
267
- const listItem2 = list2.getFirstChild();
268
- if (listItem1 && listItem2 && isNestedListNode(listItem1) && isNestedListNode(listItem2)) {
269
- mergeLists(listItem1.getFirstChild(), listItem2.getFirstChild());
270
- listItem2.remove();
271
- }
272
- const toMerge = list2.getChildren();
273
- if (toMerge.length > 0) {
274
- list1.append(...toMerge);
275
- }
276
- list2.remove();
277
- }
278
-
279
- /**
280
- * Searches for the nearest ancestral ListNode and removes it. If selection is an empty ListItemNode
281
- * it will remove the whole list, including the ListItemNode. For each ListItemNode in the ListNode,
282
- * removeList will also generate new ParagraphNodes in the removed ListNode's place. Any child node
283
- * inside a ListItemNode will be appended to the new ParagraphNodes.
284
- * @param editor - The lexical editor.
285
- */
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
- }
303
- }
304
- }
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 = lexical.$createParagraphNode();
311
- append(paragraph, listItemNode.getChildren());
312
- insertionPoint.insertAfter(paragraph);
313
- insertionPoint = paragraph;
314
-
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();
328
- }
329
- listNode.remove();
330
- }
331
- }
332
- });
333
- }
334
-
335
- /**
336
- * Takes the value of a child ListItemNode and makes it the value the ListItemNode
337
- * should be if it isn't already. Also ensures that checked is undefined if the
338
- * parent does not have a list type of 'check'.
339
- * @param list - The list whose children are updated.
340
- */
341
- function updateChildrenListItemValue(list) {
342
- const isNotChecklist = list.getListType() !== 'check';
343
- let value = list.getStart();
344
- for (const child of list.getChildren()) {
345
- if ($isListItemNode(child)) {
346
- if (child.getValue() !== value) {
347
- child.setValue(value);
348
- }
349
- if (isNotChecklist && child.getLatest().__checked != null) {
350
- child.setChecked(undefined);
351
- }
352
- if (!$isListNode(child.getFirstChild())) {
353
- value++;
354
- }
355
- }
356
- }
357
- }
358
-
359
- /**
360
- * Merge the next sibling list if same type.
361
- * <ul> will merge with <ul>, but NOT <ul> with <ol>.
362
- * @param list - The list whose next sibling should be potentially merged
363
- */
364
- function mergeNextSiblingListIfSameType(list) {
365
- const nextSibling = list.getNextSibling();
366
- if ($isListNode(nextSibling) && list.getListType() === nextSibling.getListType()) {
367
- mergeLists(list, nextSibling);
368
- }
369
- }
370
-
371
- /**
372
- * Adds an empty ListNode/ListItemNode chain at listItemNode, so as to
373
- * create an indent effect. Won't indent ListItemNodes that have a ListNode as
374
- * a child, but does merge sibling ListItemNodes if one has a nested ListNode.
375
- * @param listItemNode - The ListItemNode to be indented.
376
- */
377
- function $handleIndent(listItemNode) {
378
- // go through each node and decide where to move it.
379
- const removed = new Set();
380
- if (isNestedListNode(listItemNode) || removed.has(listItemNode.getKey())) {
381
- return;
382
- }
383
- const parent = listItemNode.getParent();
384
-
385
- // We can cast both of the below `isNestedListNode` only returns a boolean type instead of a user-defined type guards
386
- const nextSibling = listItemNode.getNextSibling();
387
- const previousSibling = listItemNode.getPreviousSibling();
388
- // if there are nested lists on either side, merge them all together.
389
-
390
- if (isNestedListNode(nextSibling) && isNestedListNode(previousSibling)) {
391
- const innerList = previousSibling.getFirstChild();
392
- if ($isListNode(innerList)) {
393
- innerList.append(listItemNode);
394
- const nextInnerList = nextSibling.getFirstChild();
395
- if ($isListNode(nextInnerList)) {
396
- const children = nextInnerList.getChildren();
397
- append(innerList, children);
398
- nextSibling.remove();
399
- removed.add(nextSibling.getKey());
400
- }
401
- }
402
- } else if (isNestedListNode(nextSibling)) {
403
- // if the ListItemNode is next to a nested ListNode, merge them
404
- const innerList = nextSibling.getFirstChild();
405
- if ($isListNode(innerList)) {
406
- const firstChild = innerList.getFirstChild();
407
- if (firstChild !== null) {
408
- firstChild.insertBefore(listItemNode);
409
- }
410
- }
411
- } else if (isNestedListNode(previousSibling)) {
412
- const innerList = previousSibling.getFirstChild();
413
- if ($isListNode(innerList)) {
414
- innerList.append(listItemNode);
415
- }
416
- } else {
417
- // otherwise, we need to create a new nested ListNode
418
-
419
- if ($isListNode(parent)) {
420
- const newListItem = $createListItemNode();
421
- const newList = $createListNode(parent.getListType());
422
- newListItem.append(newList);
423
- newList.append(listItemNode);
424
- if (previousSibling) {
425
- previousSibling.insertAfter(newListItem);
426
- } else if (nextSibling) {
427
- nextSibling.insertBefore(newListItem);
428
- } else {
429
- parent.append(newListItem);
430
- }
431
- }
432
- }
433
- }
434
-
435
- /**
436
- * Removes an indent by removing an empty ListNode/ListItemNode chain. An indented ListItemNode
437
- * has a great grandparent node of type ListNode, which is where the ListItemNode will reside
438
- * within as a child.
439
- * @param listItemNode - The ListItemNode to remove the indent (outdent).
440
- */
441
- function $handleOutdent(listItemNode) {
442
- // go through each node and decide where to move it.
443
-
444
- if (isNestedListNode(listItemNode)) {
445
- return;
446
- }
447
- const parentList = listItemNode.getParent();
448
- const grandparentListItem = parentList ? parentList.getParent() : undefined;
449
- const greatGrandparentList = grandparentListItem ? grandparentListItem.getParent() : undefined;
450
- // If it doesn't have these ancestors, it's not indented.
451
-
452
- if ($isListNode(greatGrandparentList) && $isListItemNode(grandparentListItem) && $isListNode(parentList)) {
453
- // if it's the first child in it's parent list, insert it into the
454
- // great grandparent list before the grandparent
455
- const firstChild = parentList ? parentList.getFirstChild() : undefined;
456
- const lastChild = parentList ? parentList.getLastChild() : undefined;
457
- if (listItemNode.is(firstChild)) {
458
- grandparentListItem.insertBefore(listItemNode);
459
- if (parentList.isEmpty()) {
460
- grandparentListItem.remove();
461
- }
462
- // if it's the last child in it's parent list, insert it into the
463
- // great grandparent list after the grandparent.
464
- } else if (listItemNode.is(lastChild)) {
465
- grandparentListItem.insertAfter(listItemNode);
466
- if (parentList.isEmpty()) {
467
- grandparentListItem.remove();
468
- }
469
- } else {
470
- // otherwise, we need to split the siblings into two new nested lists
471
- const listType = parentList.getListType();
472
- const previousSiblingsListItem = $createListItemNode();
473
- const previousSiblingsList = $createListNode(listType);
474
- previousSiblingsListItem.append(previousSiblingsList);
475
- listItemNode.getPreviousSiblings().forEach(sibling => previousSiblingsList.append(sibling));
476
- const nextSiblingsListItem = $createListItemNode();
477
- const nextSiblingsList = $createListNode(listType);
478
- nextSiblingsListItem.append(nextSiblingsList);
479
- append(nextSiblingsList, listItemNode.getNextSiblings());
480
- // put the sibling nested lists on either side of the grandparent list item in the great grandparent.
481
- grandparentListItem.insertBefore(previousSiblingsListItem);
482
- grandparentListItem.insertAfter(nextSiblingsListItem);
483
- // replace the grandparent list item (now between the siblings) with the outdented list item.
484
- grandparentListItem.replace(listItemNode);
485
- }
486
- }
487
- }
488
-
489
- /**
490
- * Attempts to insert a ParagraphNode at selection and selects the new node. The selection must contain a ListItemNode
491
- * or a node that does not already contain text. If its grandparent is the root/shadow root, it will get the ListNode
492
- * (which should be the parent node) and insert the ParagraphNode as a sibling to the ListNode. If the ListNode is
493
- * nested in a ListItemNode instead, it will add the ParagraphNode after the grandparent ListItemNode.
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
496
- * or the selection does not contain a ListItemNode or the node already holds text.
497
- */
498
- function $handleListInsertParagraph() {
499
- const selection = lexical.$getSelection();
500
- if (!lexical.$isRangeSelection(selection) || !selection.isCollapsed()) {
501
- return false;
502
- }
503
- // Only run this code on empty list items
504
- const anchor = selection.anchor.getNode();
505
- if (!$isListItemNode(anchor) || anchor.getChildrenSize() !== 0) {
506
- return false;
507
- }
508
- const topListNode = $getTopListNode(anchor);
509
- const parent = anchor.getParent();
510
- if (!$isListNode(parent)) {
511
- throw Error(`A ListItemNode must have a ListNode for a parent.`);
512
- }
513
- const grandparent = parent.getParent();
514
- let replacementNode;
515
- if (lexical.$isRootOrShadowRoot(grandparent)) {
516
- replacementNode = lexical.$createParagraphNode();
517
- topListNode.insertAfter(replacementNode);
518
- } else if ($isListItemNode(grandparent)) {
519
- replacementNode = $createListItemNode();
520
- grandparent.insertAfter(replacementNode);
521
- } else {
522
- return false;
523
- }
524
- replacementNode.select();
525
- const nextSiblings = anchor.getNextSiblings();
526
- if (nextSiblings.length > 0) {
527
- const newList = $createListNode(parent.getListType());
528
- if (lexical.$isParagraphNode(replacementNode)) {
529
- replacementNode.insertAfter(newList);
530
- } else {
531
- const newListItem = $createListItemNode();
532
- newListItem.append(newList);
533
- replacementNode.insertAfter(newListItem);
534
- }
535
- nextSiblings.forEach(sibling => {
536
- sibling.remove();
537
- newList.append(sibling);
538
- });
539
- }
540
-
541
- // Don't leave hanging nested empty lists
542
- $removeHighestEmptyListParent(anchor);
543
- return true;
544
- }
545
-
546
- /**
547
- * Copyright (c) Meta Platforms, Inc. and affiliates.
548
- *
549
- * This source code is licensed under the MIT license found in the
550
- * LICENSE file in the root directory of this source tree.
551
- *
552
- */
553
-
554
- function normalizeClassNames(...classNames) {
555
- const rval = [];
556
- for (const className of classNames) {
557
- if (className && typeof className === 'string') {
558
- for (const [s] of className.matchAll(/\S+/g)) {
559
- rval.push(s);
560
- }
561
- }
562
- }
563
- return rval;
564
- }
565
-
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
- /** @noInheritDoc */
575
- class ListItemNode extends lexical.ElementNode {
576
- /** @internal */
577
-
578
- /** @internal */
579
-
580
- static getType() {
581
- return 'listitem';
582
- }
583
- static clone(node) {
584
- return new ListItemNode(node.__value, node.__checked, node.__key);
585
- }
586
- constructor(value, checked, key) {
587
- super(key);
588
- this.__value = value === undefined ? 1 : value;
589
- this.__checked = checked;
590
- }
591
- createDOM(config) {
592
- 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);
599
- return element;
600
- }
601
- updateDOM(prevNode, dom, config) {
602
- const parent = this.getParent();
603
- if ($isListNode(parent) && parent.getListType() === 'check') {
604
- updateListItemChecked(dom, this, prevNode);
605
- }
606
- // @ts-expect-error - this is always HTMLListItemElement
607
- dom.value = this.__value;
608
- $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
- }
624
- }
625
- };
626
- }
627
- static importDOM() {
628
- return {
629
- li: () => ({
630
- conversion: $convertListItemElement,
631
- priority: 0
632
- })
633
- };
634
- }
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;
642
- }
643
- exportDOM(editor) {
644
- const element = this.createDOM(editor._config);
645
- element.style.textAlign = this.getFormatType();
646
- return {
647
- element
648
- };
649
- }
650
- exportJSON() {
651
- return {
652
- ...super.exportJSON(),
653
- checked: this.getChecked(),
654
- type: 'listitem',
655
- value: this.getValue(),
656
- version: 1
657
- };
658
- }
659
- append(...nodes) {
660
- for (let i = 0; i < nodes.length; i++) {
661
- const node = nodes[i];
662
- if (lexical.$isElementNode(node) && this.canMergeWith(node)) {
663
- const children = node.getChildren();
664
- this.append(...children);
665
- node.remove();
666
- } else {
667
- super.append(node);
668
- }
669
- }
670
- return this;
671
- }
672
- replace(replaceWithNode, includeChildren) {
673
- if ($isListItemNode(replaceWithNode)) {
674
- return super.replace(replaceWithNode);
675
- }
676
- this.setIndent(0);
677
- const list = this.getParentOrThrow();
678
- if (!$isListNode(list)) {
679
- return replaceWithNode;
680
- }
681
- if (list.__first === this.getKey()) {
682
- list.insertBefore(replaceWithNode);
683
- } else if (list.__last === this.getKey()) {
684
- list.insertAfter(replaceWithNode);
685
- } else {
686
- // Split the list
687
- const newList = $createListNode(list.getListType());
688
- let nextSibling = this.getNextSibling();
689
- while (nextSibling) {
690
- const nodeToAppend = nextSibling;
691
- nextSibling = nextSibling.getNextSibling();
692
- newList.append(nodeToAppend);
693
- }
694
- list.insertAfter(replaceWithNode);
695
- replaceWithNode.insertAfter(newList);
696
- }
697
- if (includeChildren) {
698
- if (!lexical.$isElementNode(replaceWithNode)) {
699
- throw Error(`includeChildren should only be true for ElementNodes`);
700
- }
701
- this.getChildren().forEach(child => {
702
- replaceWithNode.append(child);
703
- });
704
- }
705
- this.remove();
706
- if (list.getChildrenSize() === 0) {
707
- list.remove();
708
- }
709
- return replaceWithNode;
710
- }
711
- insertAfter(node, restoreSelection = true) {
712
- const listNode = this.getParentOrThrow();
713
- if (!$isListNode(listNode)) {
714
- {
715
- throw Error(`insertAfter: list node is not parent of list item node`);
716
- }
717
- }
718
- if ($isListItemNode(node)) {
719
- return super.insertAfter(node, restoreSelection);
720
- }
721
- const siblings = this.getNextSiblings();
722
-
723
- // Split the lists and insert the node in between them
724
- listNode.insertAfter(node, restoreSelection);
725
- if (siblings.length !== 0) {
726
- const newListNode = $createListNode(listNode.getListType());
727
- siblings.forEach(sibling => newListNode.append(sibling));
728
- node.insertAfter(newListNode, restoreSelection);
729
- }
730
- return node;
731
- }
732
- remove(preserveEmptyParent) {
733
- const prevSibling = this.getPreviousSibling();
734
- const nextSibling = this.getNextSibling();
735
- super.remove(preserveEmptyParent);
736
- if (prevSibling && nextSibling && isNestedListNode(prevSibling) && isNestedListNode(nextSibling)) {
737
- mergeLists(prevSibling.getFirstChild(), nextSibling.getFirstChild());
738
- nextSibling.remove();
739
- }
740
- }
741
- insertNewAfter(_, restoreSelection = true) {
742
- const newElement = $createListItemNode(this.__checked == null ? undefined : false);
743
- this.insertAfter(newElement, restoreSelection);
744
- return newElement;
745
- }
746
- collapseAtStart(selection) {
747
- const paragraph = lexical.$createParagraphNode();
748
- const children = this.getChildren();
749
- children.forEach(child => paragraph.append(child));
750
- const listNode = this.getParentOrThrow();
751
- const listNodeParent = listNode.getParentOrThrow();
752
- const isIndented = $isListItemNode(listNodeParent);
753
- if (listNode.getChildrenSize() === 1) {
754
- if (isIndented) {
755
- // if the list node is nested, we just want to remove it,
756
- // effectively unindenting it.
757
- listNode.remove();
758
- listNodeParent.select();
759
- } else {
760
- listNode.insertBefore(paragraph);
761
- listNode.remove();
762
- // If we have selection on the list item, we'll need to move it
763
- // to the paragraph
764
- const anchor = selection.anchor;
765
- const focus = selection.focus;
766
- const key = paragraph.getKey();
767
- if (anchor.type === 'element' && anchor.getNode().is(this)) {
768
- anchor.set(key, anchor.offset, 'element');
769
- }
770
- if (focus.type === 'element' && focus.getNode().is(this)) {
771
- focus.set(key, focus.offset, 'element');
772
- }
773
- }
774
- } else {
775
- listNode.insertBefore(paragraph);
776
- this.remove();
777
- }
778
- return true;
779
- }
780
- getValue() {
781
- const self = this.getLatest();
782
- return self.__value;
783
- }
784
- setValue(value) {
785
- const self = this.getWritable();
786
- self.__value = value;
787
- }
788
- getChecked() {
789
- const self = this.getLatest();
790
- let listType;
791
- const parent = this.getParent();
792
- if ($isListNode(parent)) {
793
- listType = parent.getListType();
794
- }
795
- return listType === 'check' ? Boolean(self.__checked) : undefined;
796
- }
797
- setChecked(checked) {
798
- const self = this.getWritable();
799
- self.__checked = checked;
800
- }
801
- toggleChecked() {
802
- this.setChecked(!this.__checked);
803
- }
804
- getIndent() {
805
- // If we don't have a parent, we are likely serializing
806
- const parent = this.getParent();
807
- if (parent === null) {
808
- return this.getLatest().__indent;
809
- }
810
- // ListItemNode should always have a ListNode for a parent.
811
- let listNodeParent = parent.getParentOrThrow();
812
- let indentLevel = 0;
813
- while ($isListItemNode(listNodeParent)) {
814
- listNodeParent = listNodeParent.getParentOrThrow().getParentOrThrow();
815
- indentLevel++;
816
- }
817
- return indentLevel;
818
- }
819
- setIndent(indent) {
820
- if (!(typeof indent === 'number')) {
821
- throw Error(`Invalid indent value.`);
822
- }
823
- indent = Math.floor(indent);
824
- if (!(indent >= 0)) {
825
- throw Error(`Indent value must be non-negative.`);
826
- }
827
- let currentIndent = this.getIndent();
828
- while (currentIndent !== indent) {
829
- if (currentIndent < indent) {
830
- $handleIndent(this);
831
- currentIndent++;
832
- } else {
833
- $handleOutdent(this);
834
- currentIndent--;
835
- }
836
- }
837
- return this;
838
- }
839
-
840
- /** @deprecated @internal */
841
- canInsertAfter(node) {
842
- return $isListItemNode(node);
843
- }
844
-
845
- /** @deprecated @internal */
846
- canReplaceWith(replacement) {
847
- return $isListItemNode(replacement);
848
- }
849
- canMergeWith(node) {
850
- return lexical.$isParagraphNode(node) || $isListItemNode(node);
851
- }
852
- extractWithChild(child, selection) {
853
- if (!lexical.$isRangeSelection(selection)) {
854
- return false;
855
- }
856
- const anchorNode = selection.anchor.getNode();
857
- const focusNode = selection.focus.getNode();
858
- return this.isParentOf(anchorNode) && this.isParentOf(focusNode) && this.getTextContent().length === selection.getTextContent().length;
859
- }
860
- isParentRequired() {
861
- return true;
862
- }
863
- createParentElementNode() {
864
- return $createListNode('bullet');
865
- }
866
- canMergeWhenEmpty() {
867
- return true;
868
- }
869
- }
870
- function $setListItemThemeClassNames(dom, editorThemeClasses, node) {
871
- const classesToAdd = [];
872
- const classesToRemove = [];
873
- const listTheme = editorThemeClasses.list;
874
- const listItemClassName = listTheme ? listTheme.listitem : undefined;
875
- let nestedListItemClassName;
876
- if (listTheme && listTheme.nested) {
877
- nestedListItemClassName = listTheme.nested.listitem;
878
- }
879
- if (listItemClassName !== undefined) {
880
- classesToAdd.push(...normalizeClassNames(listItemClassName));
881
- }
882
- if (listTheme) {
883
- const parentNode = node.getParent();
884
- const isCheckList = $isListNode(parentNode) && parentNode.getListType() === 'check';
885
- const checked = node.getChecked();
886
- if (!isCheckList || checked) {
887
- classesToRemove.push(listTheme.listitemUnchecked);
888
- }
889
- if (!isCheckList || !checked) {
890
- classesToRemove.push(listTheme.listitemChecked);
891
- }
892
- if (isCheckList) {
893
- classesToAdd.push(checked ? listTheme.listitemChecked : listTheme.listitemUnchecked);
894
- }
895
- }
896
- if (nestedListItemClassName !== undefined) {
897
- const nestedListItemClasses = normalizeClassNames(nestedListItemClassName);
898
- if (node.getChildren().some(child => $isListNode(child))) {
899
- classesToAdd.push(...nestedListItemClasses);
900
- } else {
901
- classesToRemove.push(...nestedListItemClasses);
902
- }
903
- }
904
- if (classesToRemove.length > 0) {
905
- LexicalUtils.removeClassNamesFromElement(dom, ...classesToRemove);
906
- }
907
- if (classesToAdd.length > 0) {
908
- LexicalUtils.addClassNamesToElement(dom, ...classesToAdd);
909
- }
910
- }
911
- function updateListItemChecked(dom, listItemNode, prevListItemNode, listNode) {
912
- // Only add attributes for leaf list items
913
- if ($isListNode(listItemNode.getFirstChild())) {
914
- dom.removeAttribute('role');
915
- dom.removeAttribute('tabIndex');
916
- dom.removeAttribute('aria-checked');
917
- } else {
918
- dom.setAttribute('role', 'checkbox');
919
- dom.setAttribute('tabIndex', '-1');
920
- if (!prevListItemNode || listItemNode.__checked !== prevListItemNode.__checked) {
921
- dom.setAttribute('aria-checked', listItemNode.getChecked() ? 'true' : 'false');
922
- }
923
- }
924
- }
925
- function $convertListItemElement(domNode) {
926
- const isGitHubCheckList = domNode.classList.contains('task-list-item');
927
- if (isGitHubCheckList) {
928
- for (const child of domNode.children) {
929
- if (child.tagName === 'INPUT') {
930
- return $convertCheckboxInput(child);
931
- }
932
- }
933
- }
934
- const ariaCheckedAttr = domNode.getAttribute('aria-checked');
935
- const checked = ariaCheckedAttr === 'true' ? true : ariaCheckedAttr === 'false' ? false : undefined;
936
- return {
937
- node: $createListItemNode(checked)
938
- };
939
- }
940
- function $convertCheckboxInput(domNode) {
941
- const isCheckboxInput = domNode.getAttribute('type') === 'checkbox';
942
- if (!isCheckboxInput) {
943
- return {
944
- node: null
945
- };
946
- }
947
- const checked = domNode.hasAttribute('checked');
948
- return {
949
- node: $createListItemNode(checked)
950
- };
951
- }
952
-
953
- /**
954
- * Creates a new List Item node, passing true/false will convert it to a checkbox input.
955
- * @param checked - Is the List Item a checkbox and, if so, is it checked? undefined/null: not a checkbox, true/false is a checkbox and checked/unchecked, respectively.
956
- * @returns The new List Item.
957
- */
958
- function $createListItemNode(checked) {
959
- return lexical.$applyNodeReplacement(new ListItemNode(undefined, checked));
960
- }
961
-
962
- /**
963
- * Checks to see if the node is a ListItemNode.
964
- * @param node - The node to be checked.
965
- * @returns true if the node is a ListItemNode, false otherwise.
966
- */
967
- function $isListItemNode(node) {
968
- return node instanceof ListItemNode;
969
- }
970
-
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
- /** @noInheritDoc */
980
- class ListNode extends lexical.ElementNode {
981
- /** @internal */
982
-
983
- /** @internal */
984
-
985
- /** @internal */
986
-
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);
993
- }
994
- constructor(listType, start, key) {
995
- super(key);
996
- const _listType = TAG_TO_LIST_TYPE[listType] || listType;
997
- this.__listType = _listType;
998
- this.__tag = _listType === 'number' ? 'ol' : 'ul';
999
- this.__start = start;
1000
- }
1001
- getTag() {
1002
- return this.__tag;
1003
- }
1004
- setListType(type) {
1005
- const writable = this.getWritable();
1006
- writable.__listType = type;
1007
- writable.__tag = type === 'number' ? 'ol' : 'ul';
1008
- }
1009
- getListType() {
1010
- return this.__listType;
1011
- }
1012
- getStart() {
1013
- return this.__start;
1014
- }
1015
-
1016
- // View
1017
-
1018
- createDOM(config, _editor) {
1019
- const tag = this.__tag;
1020
- const dom = document.createElement(tag);
1021
- if (this.__start !== 1) {
1022
- dom.setAttribute('start', String(this.__start));
1023
- }
1024
- // @ts-expect-error Internal field.
1025
- dom.__lexicalListType = this.__listType;
1026
- $setListThemeClassNames(dom, config.theme, this);
1027
- return dom;
1028
- }
1029
- updateDOM(prevNode, dom, config) {
1030
- if (prevNode.__tag !== this.__tag) {
1031
- return true;
1032
- }
1033
- $setListThemeClassNames(dom, config.theme, this);
1034
- return false;
1035
- }
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;
1063
- }
1064
- exportDOM(editor) {
1065
- const {
1066
- element
1067
- } = super.exportDOM(editor);
1068
- if (element && LexicalUtils.isHTMLElement(element)) {
1069
- if (this.__start !== 1) {
1070
- element.setAttribute('start', String(this.__start));
1071
- }
1072
- if (this.__listType === 'check') {
1073
- element.setAttribute('__lexicalListType', 'check');
1074
- }
1075
- }
1076
- return {
1077
- element
1078
- };
1079
- }
1080
- exportJSON() {
1081
- return {
1082
- ...super.exportJSON(),
1083
- listType: this.getListType(),
1084
- start: this.getStart(),
1085
- tag: this.getTag(),
1086
- type: 'list',
1087
- version: 1
1088
- };
1089
- }
1090
- canBeEmpty() {
1091
- return false;
1092
- }
1093
- canIndent() {
1094
- return false;
1095
- }
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 (lexical.$isElementNode(currentNode)) {
1106
- const textNode = lexical.$createTextNode(currentNode.getTextContent());
1107
- listItemNode.append(textNode);
1108
- } else {
1109
- listItemNode.append(currentNode);
1110
- }
1111
- super.append(listItemNode);
1112
- }
1113
- }
1114
- return this;
1115
- }
1116
- extractWithChild(child) {
1117
- return $isListItemNode(child);
1118
- }
1119
- }
1120
- function $setListThemeClassNames(dom, editorThemeClasses, node) {
1121
- const classesToAdd = [];
1122
- const classesToRemove = [];
1123
- const listTheme = editorThemeClasses.list;
1124
- if (listTheme !== undefined) {
1125
- const listLevelsClassNames = listTheme[`${node.__tag}Depth`] || [];
1126
- const listDepth = $getListDepth(node) - 1;
1127
- const normalizedListDepth = listDepth % listLevelsClassNames.length;
1128
- const listLevelClassName = listLevelsClassNames[normalizedListDepth];
1129
- const listClassName = listTheme[node.__tag];
1130
- let nestedListClassName;
1131
- const nestedListTheme = listTheme.nested;
1132
- const checklistClassName = listTheme.checklist;
1133
- if (nestedListTheme !== undefined && nestedListTheme.list) {
1134
- nestedListClassName = nestedListTheme.list;
1135
- }
1136
- if (listClassName !== undefined) {
1137
- classesToAdd.push(listClassName);
1138
- }
1139
- if (checklistClassName !== undefined && node.__listType === 'check') {
1140
- classesToAdd.push(checklistClassName);
1141
- }
1142
- if (listLevelClassName !== undefined) {
1143
- classesToAdd.push(...normalizeClassNames(listLevelClassName));
1144
- for (let i = 0; i < listLevelsClassNames.length; i++) {
1145
- if (i !== normalizedListDepth) {
1146
- classesToRemove.push(node.__tag + i);
1147
- }
1148
- }
1149
- }
1150
- if (nestedListClassName !== undefined) {
1151
- const nestedListItemClasses = normalizeClassNames(nestedListClassName);
1152
- if (listDepth > 1) {
1153
- classesToAdd.push(...nestedListItemClasses);
1154
- } else {
1155
- classesToRemove.push(...nestedListItemClasses);
1156
- }
1157
- }
1158
- }
1159
- if (classesToRemove.length > 0) {
1160
- LexicalUtils.removeClassNamesFromElement(dom, ...classesToRemove);
1161
- }
1162
- if (classesToAdd.length > 0) {
1163
- LexicalUtils.addClassNamesToElement(dom, ...classesToAdd);
1164
- }
1165
- }
1166
-
1167
- /*
1168
- * This function normalizes the children of a ListNode after the conversion from HTML,
1169
- * ensuring that they are all ListItemNodes and contain either a single nested ListNode
1170
- * or some other inline content.
1171
- */
1172
- function $normalizeChildren(nodes) {
1173
- const normalizedListItems = [];
1174
- for (let i = 0; i < nodes.length; i++) {
1175
- const node = nodes[i];
1176
- if ($isListItemNode(node)) {
1177
- normalizedListItems.push(node);
1178
- const children = node.getChildren();
1179
- if (children.length > 1) {
1180
- children.forEach(child => {
1181
- if ($isListNode(child)) {
1182
- normalizedListItems.push($wrapInListItem(child));
1183
- }
1184
- });
1185
- }
1186
- } else {
1187
- normalizedListItems.push($wrapInListItem(node));
1188
- }
1189
- }
1190
- return normalizedListItems;
1191
- }
1192
- function isDomChecklist(domNode) {
1193
- if (domNode.getAttribute('__lexicallisttype') === 'check' ||
1194
- // is github checklist
1195
- domNode.classList.contains('contains-task-list')) {
1196
- return true;
1197
- }
1198
- // if children are checklist items, the node is a checklist ul. Applicable for googledoc checklist pasting.
1199
- for (const child of domNode.childNodes) {
1200
- if (LexicalUtils.isHTMLElement(child) && child.hasAttribute('aria-checked')) {
1201
- return true;
1202
- }
1203
- }
1204
- return false;
1205
- }
1206
- function $convertListNode(domNode) {
1207
- const nodeName = domNode.nodeName.toLowerCase();
1208
- let node = null;
1209
- if (nodeName === 'ol') {
1210
- // @ts-ignore
1211
- const start = domNode.start;
1212
- node = $createListNode('number', start);
1213
- } else if (nodeName === 'ul') {
1214
- if (isDomChecklist(domNode)) {
1215
- node = $createListNode('check');
1216
- } else {
1217
- node = $createListNode('bullet');
1218
- }
1219
- }
1220
- return {
1221
- after: $normalizeChildren,
1222
- node
1223
- };
1224
- }
1225
- const TAG_TO_LIST_TYPE = {
1226
- ol: 'number',
1227
- ul: 'bullet'
1228
- };
1229
-
1230
- /**
1231
- * Creates a ListNode of listType.
1232
- * @param listType - The type of list to be created. Can be 'number', 'bullet', or 'check'.
1233
- * @param start - Where an ordered list starts its count, start = 1 if left undefined.
1234
- * @returns The new ListNode
1235
- */
1236
- function $createListNode(listType, start = 1) {
1237
- return lexical.$applyNodeReplacement(new ListNode(listType, start));
1238
- }
1239
-
1240
- /**
1241
- * Checks to see if the node is a ListNode.
1242
- * @param node - The node to be checked.
1243
- * @returns true if the node is a ListNode, false otherwise.
1244
- */
1245
- function $isListNode(node) {
1246
- return node instanceof ListNode;
1247
- }
1248
-
1249
- /**
1250
- * Copyright (c) Meta Platforms, Inc. and affiliates.
1251
- *
1252
- * This source code is licensed under the MIT license found in the
1253
- * LICENSE file in the root directory of this source tree.
1254
- *
1255
- */
1256
-
1257
- const INSERT_UNORDERED_LIST_COMMAND = lexical.createCommand('INSERT_UNORDERED_LIST_COMMAND');
1258
- const INSERT_ORDERED_LIST_COMMAND = lexical.createCommand('INSERT_ORDERED_LIST_COMMAND');
1259
- const INSERT_CHECK_LIST_COMMAND = lexical.createCommand('INSERT_CHECK_LIST_COMMAND');
1260
- const REMOVE_LIST_COMMAND = lexical.createCommand('REMOVE_LIST_COMMAND');
1261
-
1262
- exports.$createListItemNode = $createListItemNode;
1263
- exports.$createListNode = $createListNode;
1264
- exports.$getListDepth = $getListDepth;
1265
- exports.$handleListInsertParagraph = $handleListInsertParagraph;
1266
- exports.$isListItemNode = $isListItemNode;
1267
- exports.$isListNode = $isListNode;
1268
- exports.INSERT_CHECK_LIST_COMMAND = INSERT_CHECK_LIST_COMMAND;
1269
- exports.INSERT_ORDERED_LIST_COMMAND = INSERT_ORDERED_LIST_COMMAND;
1270
- exports.INSERT_UNORDERED_LIST_COMMAND = INSERT_UNORDERED_LIST_COMMAND;
1271
- exports.ListItemNode = ListItemNode;
1272
- exports.ListNode = ListNode;
1273
- exports.REMOVE_LIST_COMMAND = REMOVE_LIST_COMMAND;
1274
- exports.insertList = insertList;
1275
- exports.removeList = removeList;