@ekz/lexical-list 0.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/EkzLexicalList.dev.js +1661 -0
- package/EkzLexicalList.dev.mjs +1638 -0
- package/EkzLexicalList.js +11 -0
- package/EkzLexicalList.mjs +33 -0
- package/EkzLexicalList.node.mjs +31 -0
- package/EkzLexicalList.prod.js +9 -0
- package/EkzLexicalList.prod.mjs +9 -0
- package/LICENSE +21 -0
- package/LexicalList.js.flow +118 -0
- package/LexicalListItemNode.d.ts +68 -0
- package/LexicalListNode.d.ts +59 -0
- package/README.md +88 -0
- package/checkList.d.ts +10 -0
- package/formatList.d.ts +70 -0
- package/index.d.ts +68 -0
- package/package.json +44 -0
- package/utils.d.ts +65 -0
|
@@ -0,0 +1,1661 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
'use strict';
|
|
10
|
+
|
|
11
|
+
var lexicalExtension = require('@ekz/lexical-extension');
|
|
12
|
+
var lexicalUtils = require('@ekz/lexical-utils');
|
|
13
|
+
var lexical = require('@ekz/lexical');
|
|
14
|
+
var lexicalSelection = require('@ekz/lexical-selection');
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
18
|
+
*
|
|
19
|
+
* This source code is licensed under the MIT license found in the
|
|
20
|
+
* LICENSE file in the root directory of this source tree.
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
// Do not require this module directly! Use normal `invariant` calls.
|
|
25
|
+
|
|
26
|
+
function formatDevErrorMessage(message) {
|
|
27
|
+
throw new Error(message);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Checks the depth of listNode from the root node.
|
|
32
|
+
* @param listNode - The ListNode to be checked.
|
|
33
|
+
* @returns The depth of the ListNode.
|
|
34
|
+
*/
|
|
35
|
+
function $getListDepth(listNode) {
|
|
36
|
+
let depth = 1;
|
|
37
|
+
let parent = listNode.getParent();
|
|
38
|
+
while (parent != null) {
|
|
39
|
+
if ($isListItemNode(parent)) {
|
|
40
|
+
const parentList = parent.getParent();
|
|
41
|
+
if ($isListNode(parentList)) {
|
|
42
|
+
depth++;
|
|
43
|
+
parent = parentList.getParent();
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
{
|
|
47
|
+
formatDevErrorMessage(`A ListItemNode must have a ListNode for a parent.`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return depth;
|
|
51
|
+
}
|
|
52
|
+
return depth;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Finds the nearest ancestral ListNode and returns it, throws an invariant if listItem is not a ListItemNode.
|
|
57
|
+
* @param listItem - The node to be checked.
|
|
58
|
+
* @returns The ListNode found.
|
|
59
|
+
*/
|
|
60
|
+
function $getTopListNode(listItem) {
|
|
61
|
+
let list = listItem.getParent();
|
|
62
|
+
if (!$isListNode(list)) {
|
|
63
|
+
{
|
|
64
|
+
formatDevErrorMessage(`A ListItemNode must have a ListNode for a parent.`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
let parent = list;
|
|
68
|
+
while (parent !== null) {
|
|
69
|
+
parent = parent.getParent();
|
|
70
|
+
if ($isListNode(parent)) {
|
|
71
|
+
list = parent;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return list;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A recursive Depth-First Search (Postorder Traversal) that finds all of a node's children
|
|
79
|
+
* that are of type ListItemNode and returns them in an array.
|
|
80
|
+
* @param node - The ListNode to start the search.
|
|
81
|
+
* @returns An array containing all nodes of type ListItemNode found.
|
|
82
|
+
*/
|
|
83
|
+
// This should probably be $getAllChildrenOfType
|
|
84
|
+
function $getAllListItems(node) {
|
|
85
|
+
let listItemNodes = [];
|
|
86
|
+
const listChildren = node.getChildren().filter($isListItemNode);
|
|
87
|
+
for (let i = 0; i < listChildren.length; i++) {
|
|
88
|
+
const listItemNode = listChildren[i];
|
|
89
|
+
const firstChild = listItemNode.getFirstChild();
|
|
90
|
+
if ($isListNode(firstChild)) {
|
|
91
|
+
listItemNodes = listItemNodes.concat($getAllListItems(firstChild));
|
|
92
|
+
} else {
|
|
93
|
+
listItemNodes.push(listItemNode);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return listItemNodes;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Checks to see if the passed node is a ListItemNode and has a ListNode as a child.
|
|
101
|
+
* @param node - The node to be checked.
|
|
102
|
+
* @returns true if the node is a ListItemNode and has a ListNode child, false otherwise.
|
|
103
|
+
*/
|
|
104
|
+
function isNestedListNode(node) {
|
|
105
|
+
return $isListItemNode(node) && $isListNode(node.getFirstChild());
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Takes a deeply nested ListNode or ListItemNode and traverses up the branch to delete the first
|
|
110
|
+
* ancestral ListNode (which could be the root ListNode) or ListItemNode with siblings, essentially
|
|
111
|
+
* bringing the deeply nested node up the branch once. Would remove sublist if it has siblings.
|
|
112
|
+
* Should not break ListItem -> List -> ListItem chain as empty List/ItemNodes should be removed on .remove().
|
|
113
|
+
* @param sublist - The nested ListNode or ListItemNode to be brought up the branch.
|
|
114
|
+
*/
|
|
115
|
+
function $removeHighestEmptyListParent(sublist) {
|
|
116
|
+
// Nodes may be repeatedly indented, to create deeply nested lists that each
|
|
117
|
+
// contain just one bullet.
|
|
118
|
+
// Our goal is to remove these (empty) deeply nested lists. The easiest
|
|
119
|
+
// way to do that is crawl back up the tree until we find a node that has siblings
|
|
120
|
+
// (e.g. is actually part of the list contents) and delete that, or delete
|
|
121
|
+
// the root of the list (if no list nodes have siblings.)
|
|
122
|
+
let emptyListPtr = sublist;
|
|
123
|
+
while (emptyListPtr.getNextSibling() == null && emptyListPtr.getPreviousSibling() == null) {
|
|
124
|
+
const parent = emptyListPtr.getParent();
|
|
125
|
+
if (parent == null || !($isListItemNode(parent) || $isListNode(parent))) {
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
emptyListPtr = parent;
|
|
129
|
+
}
|
|
130
|
+
emptyListPtr.remove();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Wraps a node into a ListItemNode.
|
|
135
|
+
* @param node - The node to be wrapped into a ListItemNode
|
|
136
|
+
* @returns The ListItemNode which the passed node is wrapped in.
|
|
137
|
+
*/
|
|
138
|
+
function $wrapInListItem(node) {
|
|
139
|
+
const listItemWrapper = $createListItemNode();
|
|
140
|
+
return listItemWrapper.append(node);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function $isSelectingEmptyListItem(anchorNode, nodes) {
|
|
144
|
+
return $isListItemNode(anchorNode) && (nodes.length === 0 || nodes.length === 1 && anchorNode.is(nodes[0]) && anchorNode.getChildrenSize() === 0);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Inserts a new ListNode. If the selection's anchor node is an empty ListItemNode and is a child of
|
|
149
|
+
* the root/shadow root, it will replace the ListItemNode with a ListNode and the old ListItemNode.
|
|
150
|
+
* Otherwise it will replace its parent with a new ListNode and re-insert the ListItemNode and any previous children.
|
|
151
|
+
* If the selection's anchor node is not an empty ListItemNode, it will add a new ListNode or merge an existing ListNode,
|
|
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
|
|
153
|
+
* a new ListNode, or create a new ListNode at the nearest root/shadow root.
|
|
154
|
+
* @param listType - The type of list, "number" | "bullet" | "check".
|
|
155
|
+
*/
|
|
156
|
+
function $insertList(listType) {
|
|
157
|
+
const selection = lexical.$getSelection();
|
|
158
|
+
if (selection !== null) {
|
|
159
|
+
let nodes = selection.getNodes();
|
|
160
|
+
if (lexical.$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 (lexical.$isRootOrShadowRoot(anchorNode)) {
|
|
169
|
+
const firstChild = anchorNode.getFirstChild();
|
|
170
|
+
if (firstChild) {
|
|
171
|
+
nodes = firstChild.selectStart().getNodes();
|
|
172
|
+
} else {
|
|
173
|
+
const paragraph = lexical.$createParagraphNode();
|
|
174
|
+
anchorNode.append(paragraph);
|
|
175
|
+
nodes = paragraph.select().getNodes();
|
|
176
|
+
}
|
|
177
|
+
} else if ($isSelectingEmptyListItem(anchorNode, nodes)) {
|
|
178
|
+
const list = $createListNode(listType);
|
|
179
|
+
if (lexical.$isRootOrShadowRoot(anchorNodeParent)) {
|
|
180
|
+
anchorNode.replace(list);
|
|
181
|
+
const listItem = $createListItemNode();
|
|
182
|
+
if (lexical.$isElementNode(anchorNode)) {
|
|
183
|
+
listItem.setFormat(anchorNode.getFormatType());
|
|
184
|
+
listItem.setIndent(anchorNode.getIndent());
|
|
185
|
+
}
|
|
186
|
+
list.append(listItem);
|
|
187
|
+
} else if ($isListItemNode(anchorNode)) {
|
|
188
|
+
const parent = anchorNode.getParentOrThrow();
|
|
189
|
+
append(list, parent.getChildren());
|
|
190
|
+
parent.replace(list);
|
|
191
|
+
}
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const handled = new Set();
|
|
196
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
197
|
+
const node = nodes[i];
|
|
198
|
+
if (lexical.$isElementNode(node) && node.isEmpty() && !$isListItemNode(node) && !handled.has(node.getKey())) {
|
|
199
|
+
$createListOrMerge(node, listType);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
let parent = lexical.$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 (lexical.$isRootOrShadowRoot(nextParent) && !handled.has(parentKey)) {
|
|
216
|
+
handled.add(parentKey);
|
|
217
|
+
$createListOrMerge(parent, listType);
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
parent = nextParent;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
function append(node, nodesToAppend) {
|
|
227
|
+
node.splice(node.getChildrenSize(), 0, nodesToAppend);
|
|
228
|
+
}
|
|
229
|
+
function $createListOrMerge(node, listType) {
|
|
230
|
+
if ($isListNode(node)) {
|
|
231
|
+
return node;
|
|
232
|
+
}
|
|
233
|
+
const previousSibling = node.getPreviousSibling();
|
|
234
|
+
const nextSibling = node.getNextSibling();
|
|
235
|
+
const listItem = $createListItemNode();
|
|
236
|
+
append(listItem, node.getChildren());
|
|
237
|
+
let targetList;
|
|
238
|
+
if ($isListNode(previousSibling) && listType === previousSibling.getListType()) {
|
|
239
|
+
previousSibling.append(listItem);
|
|
240
|
+
// if the same type of list is on both sides, merge them.
|
|
241
|
+
if ($isListNode(nextSibling) && listType === nextSibling.getListType()) {
|
|
242
|
+
append(previousSibling, nextSibling.getChildren());
|
|
243
|
+
nextSibling.remove();
|
|
244
|
+
}
|
|
245
|
+
targetList = previousSibling;
|
|
246
|
+
} else if ($isListNode(nextSibling) && listType === nextSibling.getListType()) {
|
|
247
|
+
nextSibling.getFirstChildOrThrow().insertBefore(listItem);
|
|
248
|
+
targetList = nextSibling;
|
|
249
|
+
} else {
|
|
250
|
+
const list = $createListNode(listType);
|
|
251
|
+
list.append(listItem);
|
|
252
|
+
node.replace(list);
|
|
253
|
+
targetList = list;
|
|
254
|
+
}
|
|
255
|
+
// listItem needs to be attached to root prior to setting indent
|
|
256
|
+
listItem.setFormat(node.getFormatType());
|
|
257
|
+
listItem.setIndent(node.getIndent());
|
|
258
|
+
|
|
259
|
+
// Preserve element-anchored selections by updating them to anchor to the listItem instead of the listNode.
|
|
260
|
+
const selection = lexical.$getSelection();
|
|
261
|
+
if (lexical.$isRangeSelection(selection)) {
|
|
262
|
+
if (targetList.getKey() === selection.anchor.key) {
|
|
263
|
+
selection.anchor.set(listItem.getKey(), selection.anchor.offset, 'element');
|
|
264
|
+
}
|
|
265
|
+
if (targetList.getKey() === selection.focus.key) {
|
|
266
|
+
selection.focus.set(listItem.getKey(), selection.focus.offset, 'element');
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
node.remove();
|
|
270
|
+
return targetList;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* A recursive function that goes through each list and their children, including nested lists,
|
|
275
|
+
* appending list2 children after list1 children and updating ListItemNode values.
|
|
276
|
+
* @param list1 - The first list to be merged.
|
|
277
|
+
* @param list2 - The second list to be merged.
|
|
278
|
+
*/
|
|
279
|
+
function mergeLists(list1, list2) {
|
|
280
|
+
const listItem1 = list1.getLastChild();
|
|
281
|
+
const listItem2 = list2.getFirstChild();
|
|
282
|
+
if (listItem1 && listItem2 && isNestedListNode(listItem1) && isNestedListNode(listItem2)) {
|
|
283
|
+
mergeLists(listItem1.getFirstChild(), listItem2.getFirstChild());
|
|
284
|
+
listItem2.remove();
|
|
285
|
+
}
|
|
286
|
+
const toMerge = list2.getChildren();
|
|
287
|
+
if (toMerge.length > 0) {
|
|
288
|
+
list1.append(...toMerge);
|
|
289
|
+
}
|
|
290
|
+
list2.remove();
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Searches for the nearest ancestral ListNode and removes it. If selection is an empty ListItemNode
|
|
295
|
+
* it will remove the whole list, including the ListItemNode. For each ListItemNode in the ListNode,
|
|
296
|
+
* removeList will also generate new ParagraphNodes in the removed ListNode's place. Any child node
|
|
297
|
+
* inside a ListItemNode will be appended to the new ParagraphNodes.
|
|
298
|
+
*/
|
|
299
|
+
function $removeList() {
|
|
300
|
+
const selection = lexical.$getSelection();
|
|
301
|
+
if (lexical.$isRangeSelection(selection)) {
|
|
302
|
+
const listNodes = new Set();
|
|
303
|
+
const nodes = selection.getNodes();
|
|
304
|
+
const anchorNode = selection.anchor.getNode();
|
|
305
|
+
if ($isSelectingEmptyListItem(anchorNode, nodes)) {
|
|
306
|
+
listNodes.add($getTopListNode(anchorNode));
|
|
307
|
+
} else {
|
|
308
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
309
|
+
const node = nodes[i];
|
|
310
|
+
if (lexical.$isLeafNode(node)) {
|
|
311
|
+
const listItemNode = lexicalUtils.$getNearestNodeOfType(node, ListItemNode);
|
|
312
|
+
if (listItemNode != null) {
|
|
313
|
+
listNodes.add($getTopListNode(listItemNode));
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
for (const listNode of listNodes) {
|
|
319
|
+
let insertionPoint = listNode;
|
|
320
|
+
const listItems = $getAllListItems(listNode);
|
|
321
|
+
for (const listItemNode of listItems) {
|
|
322
|
+
const paragraph = lexical.$createParagraphNode().setTextStyle(selection.style).setTextFormat(selection.format);
|
|
323
|
+
append(paragraph, listItemNode.getChildren());
|
|
324
|
+
insertionPoint.insertAfter(paragraph);
|
|
325
|
+
insertionPoint = paragraph;
|
|
326
|
+
|
|
327
|
+
// When the anchor and focus fall on the textNode
|
|
328
|
+
// we don't have to change the selection because the textNode will be appended to
|
|
329
|
+
// the newly generated paragraph.
|
|
330
|
+
// When selection is in empty nested list item, selection is actually on the listItemNode.
|
|
331
|
+
// When the corresponding listItemNode is deleted and replaced by the newly generated paragraph
|
|
332
|
+
// we should manually set the selection's focus and anchor to the newly generated paragraph.
|
|
333
|
+
if (listItemNode.__key === selection.anchor.key) {
|
|
334
|
+
lexical.$setPointFromCaret(selection.anchor, lexical.$normalizeCaret(lexical.$getChildCaret(paragraph, 'next')));
|
|
335
|
+
}
|
|
336
|
+
if (listItemNode.__key === selection.focus.key) {
|
|
337
|
+
lexical.$setPointFromCaret(selection.focus, lexical.$normalizeCaret(lexical.$getChildCaret(paragraph, 'next')));
|
|
338
|
+
}
|
|
339
|
+
listItemNode.remove();
|
|
340
|
+
}
|
|
341
|
+
listNode.remove();
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Takes the value of a child ListItemNode and makes it the value the ListItemNode
|
|
348
|
+
* should be if it isn't already. Also ensures that checked is undefined if the
|
|
349
|
+
* parent does not have a list type of 'check'.
|
|
350
|
+
* @param list - The list whose children are updated.
|
|
351
|
+
*/
|
|
352
|
+
function updateChildrenListItemValue(list) {
|
|
353
|
+
const isNotChecklist = list.getListType() !== 'check';
|
|
354
|
+
let value = list.getStart();
|
|
355
|
+
for (const child of list.getChildren()) {
|
|
356
|
+
if ($isListItemNode(child)) {
|
|
357
|
+
if (child.getValue() !== value) {
|
|
358
|
+
child.setValue(value);
|
|
359
|
+
}
|
|
360
|
+
if (isNotChecklist && child.getLatest().__checked != null) {
|
|
361
|
+
child.setChecked(undefined);
|
|
362
|
+
}
|
|
363
|
+
if (!$isListNode(child.getFirstChild())) {
|
|
364
|
+
value++;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Merge the next sibling list if same type.
|
|
372
|
+
* <ul> will merge with <ul>, but NOT <ul> with <ol>.
|
|
373
|
+
* @param list - The list whose next sibling should be potentially merged
|
|
374
|
+
*/
|
|
375
|
+
function mergeNextSiblingListIfSameType(list) {
|
|
376
|
+
const nextSibling = list.getNextSibling();
|
|
377
|
+
if ($isListNode(nextSibling) && list.getListType() === nextSibling.getListType()) {
|
|
378
|
+
mergeLists(list, nextSibling);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Adds an empty ListNode/ListItemNode chain at listItemNode, so as to
|
|
384
|
+
* create an indent effect. Won't indent ListItemNodes that have a ListNode as
|
|
385
|
+
* a child, but does merge sibling ListItemNodes if one has a nested ListNode.
|
|
386
|
+
* @param listItemNode - The ListItemNode to be indented.
|
|
387
|
+
*/
|
|
388
|
+
function $handleIndent(listItemNode) {
|
|
389
|
+
// go through each node and decide where to move it.
|
|
390
|
+
const removed = new Set();
|
|
391
|
+
if (isNestedListNode(listItemNode) || removed.has(listItemNode.getKey())) {
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
const parent = listItemNode.getParent();
|
|
395
|
+
|
|
396
|
+
// We can cast both of the below `isNestedListNode` only returns a boolean type instead of a user-defined type guards
|
|
397
|
+
const nextSibling = listItemNode.getNextSibling();
|
|
398
|
+
const previousSibling = listItemNode.getPreviousSibling();
|
|
399
|
+
// if there are nested lists on either side, merge them all together.
|
|
400
|
+
|
|
401
|
+
if (isNestedListNode(nextSibling) && isNestedListNode(previousSibling)) {
|
|
402
|
+
const innerList = previousSibling.getFirstChild();
|
|
403
|
+
if ($isListNode(innerList)) {
|
|
404
|
+
innerList.append(listItemNode);
|
|
405
|
+
const nextInnerList = nextSibling.getFirstChild();
|
|
406
|
+
if ($isListNode(nextInnerList)) {
|
|
407
|
+
const children = nextInnerList.getChildren();
|
|
408
|
+
append(innerList, children);
|
|
409
|
+
nextSibling.remove();
|
|
410
|
+
removed.add(nextSibling.getKey());
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
} else if (isNestedListNode(nextSibling)) {
|
|
414
|
+
// if the ListItemNode is next to a nested ListNode, merge them
|
|
415
|
+
const innerList = nextSibling.getFirstChild();
|
|
416
|
+
if ($isListNode(innerList)) {
|
|
417
|
+
const firstChild = innerList.getFirstChild();
|
|
418
|
+
if (firstChild !== null) {
|
|
419
|
+
firstChild.insertBefore(listItemNode);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
} else if (isNestedListNode(previousSibling)) {
|
|
423
|
+
const innerList = previousSibling.getFirstChild();
|
|
424
|
+
if ($isListNode(innerList)) {
|
|
425
|
+
innerList.append(listItemNode);
|
|
426
|
+
}
|
|
427
|
+
} else {
|
|
428
|
+
// otherwise, we need to create a new nested ListNode
|
|
429
|
+
|
|
430
|
+
if ($isListNode(parent)) {
|
|
431
|
+
const newListItem = $createListItemNode().setTextFormat(listItemNode.getTextFormat()).setTextStyle(listItemNode.getTextStyle());
|
|
432
|
+
const newList = $createListNode(parent.getListType()).setTextFormat(parent.getTextFormat()).setTextStyle(parent.getTextStyle());
|
|
433
|
+
newListItem.append(newList);
|
|
434
|
+
newList.append(listItemNode);
|
|
435
|
+
if (previousSibling) {
|
|
436
|
+
previousSibling.insertAfter(newListItem);
|
|
437
|
+
} else if (nextSibling) {
|
|
438
|
+
nextSibling.insertBefore(newListItem);
|
|
439
|
+
} else {
|
|
440
|
+
parent.append(newListItem);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Removes an indent by removing an empty ListNode/ListItemNode chain. An indented ListItemNode
|
|
448
|
+
* has a great grandparent node of type ListNode, which is where the ListItemNode will reside
|
|
449
|
+
* within as a child.
|
|
450
|
+
* @param listItemNode - The ListItemNode to remove the indent (outdent).
|
|
451
|
+
*/
|
|
452
|
+
function $handleOutdent(listItemNode) {
|
|
453
|
+
// go through each node and decide where to move it.
|
|
454
|
+
|
|
455
|
+
if (isNestedListNode(listItemNode)) {
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
const parentList = listItemNode.getParent();
|
|
459
|
+
const grandparentListItem = parentList ? parentList.getParent() : undefined;
|
|
460
|
+
const greatGrandparentList = grandparentListItem ? grandparentListItem.getParent() : undefined;
|
|
461
|
+
// If it doesn't have these ancestors, it's not indented.
|
|
462
|
+
|
|
463
|
+
if ($isListNode(greatGrandparentList) && $isListItemNode(grandparentListItem) && $isListNode(parentList)) {
|
|
464
|
+
// if it's the first child in it's parent list, insert it into the
|
|
465
|
+
// great grandparent list before the grandparent
|
|
466
|
+
const firstChild = parentList ? parentList.getFirstChild() : undefined;
|
|
467
|
+
const lastChild = parentList ? parentList.getLastChild() : undefined;
|
|
468
|
+
if (listItemNode.is(firstChild)) {
|
|
469
|
+
grandparentListItem.insertBefore(listItemNode);
|
|
470
|
+
if (parentList.isEmpty()) {
|
|
471
|
+
grandparentListItem.remove();
|
|
472
|
+
}
|
|
473
|
+
// if it's the last child in it's parent list, insert it into the
|
|
474
|
+
// great grandparent list after the grandparent.
|
|
475
|
+
} else if (listItemNode.is(lastChild)) {
|
|
476
|
+
grandparentListItem.insertAfter(listItemNode);
|
|
477
|
+
if (parentList.isEmpty()) {
|
|
478
|
+
grandparentListItem.remove();
|
|
479
|
+
}
|
|
480
|
+
} else {
|
|
481
|
+
// otherwise, we need to split the siblings into two new nested lists
|
|
482
|
+
const listType = parentList.getListType();
|
|
483
|
+
const previousSiblingsListItem = $createListItemNode();
|
|
484
|
+
const previousSiblingsList = $createListNode(listType);
|
|
485
|
+
previousSiblingsListItem.append(previousSiblingsList);
|
|
486
|
+
listItemNode.getPreviousSiblings().forEach(sibling => previousSiblingsList.append(sibling));
|
|
487
|
+
const nextSiblingsListItem = $createListItemNode();
|
|
488
|
+
const nextSiblingsList = $createListNode(listType);
|
|
489
|
+
nextSiblingsListItem.append(nextSiblingsList);
|
|
490
|
+
append(nextSiblingsList, listItemNode.getNextSiblings());
|
|
491
|
+
// put the sibling nested lists on either side of the grandparent list item in the great grandparent.
|
|
492
|
+
grandparentListItem.insertBefore(previousSiblingsListItem);
|
|
493
|
+
grandparentListItem.insertAfter(nextSiblingsListItem);
|
|
494
|
+
// replace the grandparent list item (now between the siblings) with the outdented list item.
|
|
495
|
+
grandparentListItem.replace(listItemNode);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Attempts to insert a ParagraphNode at selection and selects the new node. The selection must contain a ListItemNode
|
|
502
|
+
* or a node that does not already contain text. If its grandparent is the root/shadow root, it will get the ListNode
|
|
503
|
+
* (which should be the parent node) and insert the ParagraphNode as a sibling to the ListNode. If the ListNode is
|
|
504
|
+
* nested in a ListItemNode instead, it will add the ParagraphNode after the grandparent ListItemNode.
|
|
505
|
+
* Throws an invariant if the selection is not a child of a ListNode.
|
|
506
|
+
* @returns true if a ParagraphNode was inserted successfully, false if there is no selection
|
|
507
|
+
* or the selection does not contain a ListItemNode or the node already holds text.
|
|
508
|
+
*/
|
|
509
|
+
function $handleListInsertParagraph() {
|
|
510
|
+
const selection = lexical.$getSelection();
|
|
511
|
+
if (!lexical.$isRangeSelection(selection) || !selection.isCollapsed()) {
|
|
512
|
+
return false;
|
|
513
|
+
}
|
|
514
|
+
// Only run this code on empty list items (including whitespace-only)
|
|
515
|
+
const anchor = selection.anchor.getNode();
|
|
516
|
+
let listItem = null;
|
|
517
|
+
if ($isListItemNode(anchor) && anchor.getChildrenSize() === 0) {
|
|
518
|
+
// Truly empty list item (element selection)
|
|
519
|
+
listItem = anchor;
|
|
520
|
+
} else if (lexical.$isTextNode(anchor)) {
|
|
521
|
+
// Check if the entire list item contains only whitespace text nodes
|
|
522
|
+
const parentListItem = anchor.getParent();
|
|
523
|
+
if ($isListItemNode(parentListItem) && parentListItem.getChildren().every(node => lexical.$isTextNode(node) && node.getTextContent().trim() === '')) {
|
|
524
|
+
listItem = parentListItem;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
if (listItem === null) {
|
|
528
|
+
return false;
|
|
529
|
+
}
|
|
530
|
+
const topListNode = $getTopListNode(listItem);
|
|
531
|
+
const parent = listItem.getParent();
|
|
532
|
+
if (!$isListNode(parent)) {
|
|
533
|
+
formatDevErrorMessage(`A ListItemNode must have a ListNode for a parent.`);
|
|
534
|
+
}
|
|
535
|
+
const grandparent = parent.getParent();
|
|
536
|
+
let replacementNode;
|
|
537
|
+
if (lexical.$isRootOrShadowRoot(grandparent)) {
|
|
538
|
+
replacementNode = lexical.$createParagraphNode();
|
|
539
|
+
topListNode.insertAfter(replacementNode);
|
|
540
|
+
} else if ($isListItemNode(grandparent)) {
|
|
541
|
+
replacementNode = $createListItemNode();
|
|
542
|
+
grandparent.insertAfter(replacementNode);
|
|
543
|
+
} else {
|
|
544
|
+
return false;
|
|
545
|
+
}
|
|
546
|
+
replacementNode.setTextStyle(selection.style).setTextFormat(selection.format).select();
|
|
547
|
+
const nextSiblings = listItem.getNextSiblings();
|
|
548
|
+
if (nextSiblings.length > 0) {
|
|
549
|
+
const newList = $createListNode(parent.getListType());
|
|
550
|
+
if ($isListItemNode(replacementNode)) {
|
|
551
|
+
const newListItem = $createListItemNode();
|
|
552
|
+
newListItem.append(newList);
|
|
553
|
+
replacementNode.insertAfter(newListItem);
|
|
554
|
+
} else {
|
|
555
|
+
replacementNode.insertAfter(newList);
|
|
556
|
+
}
|
|
557
|
+
newList.append(...nextSiblings);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// Don't leave hanging nested empty lists
|
|
561
|
+
$removeHighestEmptyListParent(listItem);
|
|
562
|
+
return true;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
function applyMarkerStyles(dom, node, prevNode) {
|
|
566
|
+
const styles = lexicalSelection.getStyleObjectFromCSS(node.__textStyle);
|
|
567
|
+
for (const k in styles) {
|
|
568
|
+
dom.style.setProperty(`--listitem-marker-${k}`, styles[k]);
|
|
569
|
+
}
|
|
570
|
+
if (prevNode) {
|
|
571
|
+
for (const k in lexicalSelection.getStyleObjectFromCSS(prevNode.__textStyle)) {
|
|
572
|
+
if (!(k in styles)) {
|
|
573
|
+
dom.style.removeProperty(`--listitem-marker-${k}`);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/** @noInheritDoc */
|
|
580
|
+
class ListItemNode extends lexical.ElementNode {
|
|
581
|
+
/** @internal */
|
|
582
|
+
__value;
|
|
583
|
+
/** @internal */
|
|
584
|
+
__checked;
|
|
585
|
+
|
|
586
|
+
/** @internal */
|
|
587
|
+
$config() {
|
|
588
|
+
return this.config('listitem', {
|
|
589
|
+
$transform: node => {
|
|
590
|
+
if (node.__checked == null) {
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
const parent = node.getParent();
|
|
594
|
+
if ($isListNode(parent)) {
|
|
595
|
+
if (parent.getListType() !== 'check' && node.getChecked() != null) {
|
|
596
|
+
node.setChecked(undefined);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
},
|
|
600
|
+
extends: lexical.ElementNode,
|
|
601
|
+
importDOM: lexical.buildImportMap({
|
|
602
|
+
li: () => ({
|
|
603
|
+
conversion: $convertListItemElement,
|
|
604
|
+
priority: 0
|
|
605
|
+
})
|
|
606
|
+
})
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
constructor(value = 1, checked = undefined, key) {
|
|
610
|
+
super(key);
|
|
611
|
+
this.__value = value === undefined ? 1 : value;
|
|
612
|
+
this.__checked = checked;
|
|
613
|
+
}
|
|
614
|
+
afterCloneFrom(prevNode) {
|
|
615
|
+
super.afterCloneFrom(prevNode);
|
|
616
|
+
this.__value = prevNode.__value;
|
|
617
|
+
this.__checked = prevNode.__checked;
|
|
618
|
+
}
|
|
619
|
+
createDOM(config) {
|
|
620
|
+
const element = document.createElement('li');
|
|
621
|
+
this.updateListItemDOM(null, element, config);
|
|
622
|
+
return element;
|
|
623
|
+
}
|
|
624
|
+
updateListItemDOM(prevNode, dom, config) {
|
|
625
|
+
updateListItemChecked(dom, this, prevNode);
|
|
626
|
+
dom.value = this.__value;
|
|
627
|
+
$setListItemThemeClassNames(dom, config.theme, this);
|
|
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;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
applyMarkerStyles(dom, this, prevNode);
|
|
638
|
+
}
|
|
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;
|
|
644
|
+
}
|
|
645
|
+
updateFromJSON(serializedNode) {
|
|
646
|
+
return super.updateFromJSON(serializedNode).setValue(serializedNode.value).setChecked(serializedNode.checked);
|
|
647
|
+
}
|
|
648
|
+
exportDOM(editor) {
|
|
649
|
+
const element = this.createDOM(editor._config);
|
|
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
|
+
}
|
|
658
|
+
return {
|
|
659
|
+
element
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
exportJSON() {
|
|
663
|
+
return {
|
|
664
|
+
...super.exportJSON(),
|
|
665
|
+
checked: this.getChecked(),
|
|
666
|
+
value: this.getValue()
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
append(...nodes) {
|
|
670
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
671
|
+
const node = nodes[i];
|
|
672
|
+
if (lexical.$isElementNode(node) && this.canMergeWith(node)) {
|
|
673
|
+
const children = node.getChildren();
|
|
674
|
+
this.append(...children);
|
|
675
|
+
node.remove();
|
|
676
|
+
} else {
|
|
677
|
+
super.append(node);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
return this;
|
|
681
|
+
}
|
|
682
|
+
replace(replaceWithNode, includeChildren) {
|
|
683
|
+
if ($isListItemNode(replaceWithNode)) {
|
|
684
|
+
return super.replace(replaceWithNode);
|
|
685
|
+
}
|
|
686
|
+
this.setIndent(0);
|
|
687
|
+
const list = this.getParentOrThrow();
|
|
688
|
+
if (!$isListNode(list)) {
|
|
689
|
+
return replaceWithNode;
|
|
690
|
+
}
|
|
691
|
+
if (list.__first === this.getKey()) {
|
|
692
|
+
list.insertBefore(replaceWithNode);
|
|
693
|
+
} else if (list.__last === this.getKey()) {
|
|
694
|
+
list.insertAfter(replaceWithNode);
|
|
695
|
+
} else {
|
|
696
|
+
// Split the list
|
|
697
|
+
const newList = $createListNode(list.getListType());
|
|
698
|
+
let nextSibling = this.getNextSibling();
|
|
699
|
+
while (nextSibling) {
|
|
700
|
+
const nodeToAppend = nextSibling;
|
|
701
|
+
nextSibling = nextSibling.getNextSibling();
|
|
702
|
+
newList.append(nodeToAppend);
|
|
703
|
+
}
|
|
704
|
+
list.insertAfter(replaceWithNode);
|
|
705
|
+
replaceWithNode.insertAfter(newList);
|
|
706
|
+
}
|
|
707
|
+
if (includeChildren) {
|
|
708
|
+
if (!lexical.$isElementNode(replaceWithNode)) {
|
|
709
|
+
formatDevErrorMessage(`includeChildren should only be true for ElementNodes`);
|
|
710
|
+
}
|
|
711
|
+
this.getChildren().forEach(child => {
|
|
712
|
+
replaceWithNode.append(child);
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
this.remove();
|
|
716
|
+
if (list.getChildrenSize() === 0) {
|
|
717
|
+
list.remove();
|
|
718
|
+
}
|
|
719
|
+
return replaceWithNode;
|
|
720
|
+
}
|
|
721
|
+
insertAfter(node, restoreSelection = true) {
|
|
722
|
+
const listNode = this.getParentOrThrow();
|
|
723
|
+
if (!$isListNode(listNode)) {
|
|
724
|
+
{
|
|
725
|
+
formatDevErrorMessage(`insertAfter: list node is not parent of list item node`);
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
if ($isListItemNode(node)) {
|
|
729
|
+
return super.insertAfter(node, restoreSelection);
|
|
730
|
+
}
|
|
731
|
+
const siblings = this.getNextSiblings();
|
|
732
|
+
|
|
733
|
+
// Split the lists and insert the node in between them
|
|
734
|
+
listNode.insertAfter(node, restoreSelection);
|
|
735
|
+
if (siblings.length !== 0) {
|
|
736
|
+
const newListNode = $createListNode(listNode.getListType());
|
|
737
|
+
siblings.forEach(sibling => newListNode.append(sibling));
|
|
738
|
+
node.insertAfter(newListNode, restoreSelection);
|
|
739
|
+
}
|
|
740
|
+
return node;
|
|
741
|
+
}
|
|
742
|
+
remove(preserveEmptyParent) {
|
|
743
|
+
const prevSibling = this.getPreviousSibling();
|
|
744
|
+
const nextSibling = this.getNextSibling();
|
|
745
|
+
super.remove(preserveEmptyParent);
|
|
746
|
+
if (prevSibling && nextSibling && isNestedListNode(prevSibling) && isNestedListNode(nextSibling)) {
|
|
747
|
+
mergeLists(prevSibling.getFirstChild(), nextSibling.getFirstChild());
|
|
748
|
+
nextSibling.remove();
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
insertNewAfter(_, restoreSelection = true) {
|
|
752
|
+
const newElement = $createListItemNode().updateFromJSON(this.exportJSON()).setChecked(this.getChecked() ? false : undefined);
|
|
753
|
+
this.insertAfter(newElement, restoreSelection);
|
|
754
|
+
return newElement;
|
|
755
|
+
}
|
|
756
|
+
collapseAtStart(selection) {
|
|
757
|
+
const paragraph = lexical.$createParagraphNode();
|
|
758
|
+
const children = this.getChildren();
|
|
759
|
+
children.forEach(child => paragraph.append(child));
|
|
760
|
+
const listNode = this.getParentOrThrow();
|
|
761
|
+
const listNodeParent = listNode.getParentOrThrow();
|
|
762
|
+
const isIndented = $isListItemNode(listNodeParent);
|
|
763
|
+
if (listNode.getChildrenSize() === 1) {
|
|
764
|
+
if (isIndented) {
|
|
765
|
+
// if the list node is nested, we just want to remove it,
|
|
766
|
+
// effectively unindenting it.
|
|
767
|
+
listNode.remove();
|
|
768
|
+
listNodeParent.select();
|
|
769
|
+
} else {
|
|
770
|
+
listNode.insertBefore(paragraph);
|
|
771
|
+
listNode.remove();
|
|
772
|
+
// If we have selection on the list item, we'll need to move it
|
|
773
|
+
// to the paragraph
|
|
774
|
+
const anchor = selection.anchor;
|
|
775
|
+
const focus = selection.focus;
|
|
776
|
+
const key = paragraph.getKey();
|
|
777
|
+
if (anchor.type === 'element' && anchor.getNode().is(this)) {
|
|
778
|
+
anchor.set(key, anchor.offset, 'element');
|
|
779
|
+
}
|
|
780
|
+
if (focus.type === 'element' && focus.getNode().is(this)) {
|
|
781
|
+
focus.set(key, focus.offset, 'element');
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
} else {
|
|
785
|
+
listNode.insertBefore(paragraph);
|
|
786
|
+
this.remove();
|
|
787
|
+
}
|
|
788
|
+
return true;
|
|
789
|
+
}
|
|
790
|
+
getValue() {
|
|
791
|
+
const self = this.getLatest();
|
|
792
|
+
return self.__value;
|
|
793
|
+
}
|
|
794
|
+
setValue(value) {
|
|
795
|
+
const self = this.getWritable();
|
|
796
|
+
self.__value = value;
|
|
797
|
+
return self;
|
|
798
|
+
}
|
|
799
|
+
getChecked() {
|
|
800
|
+
const self = this.getLatest();
|
|
801
|
+
let listType;
|
|
802
|
+
const parent = this.getParent();
|
|
803
|
+
if ($isListNode(parent)) {
|
|
804
|
+
listType = parent.getListType();
|
|
805
|
+
}
|
|
806
|
+
return listType === 'check' ? Boolean(self.__checked) : undefined;
|
|
807
|
+
}
|
|
808
|
+
setChecked(checked) {
|
|
809
|
+
const self = this.getWritable();
|
|
810
|
+
self.__checked = checked;
|
|
811
|
+
return self;
|
|
812
|
+
}
|
|
813
|
+
toggleChecked() {
|
|
814
|
+
const self = this.getWritable();
|
|
815
|
+
return self.setChecked(!self.__checked);
|
|
816
|
+
}
|
|
817
|
+
getIndent() {
|
|
818
|
+
// If we don't have a parent, we are likely serializing
|
|
819
|
+
const parent = this.getParent();
|
|
820
|
+
if (parent === null || !this.isAttached()) {
|
|
821
|
+
return this.getLatest().__indent;
|
|
822
|
+
}
|
|
823
|
+
// ListItemNode should always have a ListNode for a parent.
|
|
824
|
+
let listNodeParent = parent.getParentOrThrow();
|
|
825
|
+
let indentLevel = 0;
|
|
826
|
+
while ($isListItemNode(listNodeParent)) {
|
|
827
|
+
listNodeParent = listNodeParent.getParentOrThrow().getParentOrThrow();
|
|
828
|
+
indentLevel++;
|
|
829
|
+
}
|
|
830
|
+
return indentLevel;
|
|
831
|
+
}
|
|
832
|
+
setIndent(indent) {
|
|
833
|
+
if (!(typeof indent === 'number')) {
|
|
834
|
+
formatDevErrorMessage(`Invalid indent value.`);
|
|
835
|
+
}
|
|
836
|
+
indent = Math.floor(indent);
|
|
837
|
+
if (!(indent >= 0)) {
|
|
838
|
+
formatDevErrorMessage(`Indent value must be non-negative.`);
|
|
839
|
+
}
|
|
840
|
+
let currentIndent = this.getIndent();
|
|
841
|
+
while (currentIndent !== indent) {
|
|
842
|
+
if (currentIndent < indent) {
|
|
843
|
+
$handleIndent(this);
|
|
844
|
+
currentIndent++;
|
|
845
|
+
} else {
|
|
846
|
+
$handleOutdent(this);
|
|
847
|
+
currentIndent--;
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
return this;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/** @deprecated @internal */
|
|
854
|
+
canInsertAfter(node) {
|
|
855
|
+
return $isListItemNode(node);
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/** @deprecated @internal */
|
|
859
|
+
canReplaceWith(replacement) {
|
|
860
|
+
return $isListItemNode(replacement);
|
|
861
|
+
}
|
|
862
|
+
canMergeWith(node) {
|
|
863
|
+
return $isListItemNode(node) || lexical.$isParagraphNode(node);
|
|
864
|
+
}
|
|
865
|
+
extractWithChild(child, selection) {
|
|
866
|
+
if (!lexical.$isRangeSelection(selection)) {
|
|
867
|
+
return false;
|
|
868
|
+
}
|
|
869
|
+
const anchorNode = selection.anchor.getNode();
|
|
870
|
+
const focusNode = selection.focus.getNode();
|
|
871
|
+
return this.isParentOf(anchorNode) && this.isParentOf(focusNode) && this.getTextContent().length === selection.getTextContent().length;
|
|
872
|
+
}
|
|
873
|
+
isParentRequired() {
|
|
874
|
+
return true;
|
|
875
|
+
}
|
|
876
|
+
createParentElementNode() {
|
|
877
|
+
return $createListNode('bullet');
|
|
878
|
+
}
|
|
879
|
+
canMergeWhenEmpty() {
|
|
880
|
+
return true;
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
function $setListItemThemeClassNames(dom, editorThemeClasses, node) {
|
|
884
|
+
const classesToAdd = [];
|
|
885
|
+
const classesToRemove = [];
|
|
886
|
+
const listTheme = editorThemeClasses.list;
|
|
887
|
+
const listItemClassName = listTheme ? listTheme.listitem : undefined;
|
|
888
|
+
let nestedListItemClassName;
|
|
889
|
+
if (listTheme && listTheme.nested) {
|
|
890
|
+
nestedListItemClassName = listTheme.nested.listitem;
|
|
891
|
+
}
|
|
892
|
+
if (listItemClassName !== undefined) {
|
|
893
|
+
classesToAdd.push(...lexical.normalizeClassNames(listItemClassName));
|
|
894
|
+
}
|
|
895
|
+
if (listTheme) {
|
|
896
|
+
const parentNode = node.getParent();
|
|
897
|
+
const isCheckList = $isListNode(parentNode) && parentNode.getListType() === 'check';
|
|
898
|
+
const checked = node.getChecked();
|
|
899
|
+
if (!isCheckList || checked) {
|
|
900
|
+
classesToRemove.push(listTheme.listitemUnchecked);
|
|
901
|
+
}
|
|
902
|
+
if (!isCheckList || !checked) {
|
|
903
|
+
classesToRemove.push(listTheme.listitemChecked);
|
|
904
|
+
}
|
|
905
|
+
if (isCheckList) {
|
|
906
|
+
classesToAdd.push(checked ? listTheme.listitemChecked : listTheme.listitemUnchecked);
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
if (nestedListItemClassName !== undefined) {
|
|
910
|
+
const nestedListItemClasses = lexical.normalizeClassNames(nestedListItemClassName);
|
|
911
|
+
if (node.getChildren().some(child => $isListNode(child))) {
|
|
912
|
+
classesToAdd.push(...nestedListItemClasses);
|
|
913
|
+
} else {
|
|
914
|
+
classesToRemove.push(...nestedListItemClasses);
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
if (classesToRemove.length > 0) {
|
|
918
|
+
lexicalUtils.removeClassNamesFromElement(dom, ...classesToRemove);
|
|
919
|
+
}
|
|
920
|
+
if (classesToAdd.length > 0) {
|
|
921
|
+
lexicalUtils.addClassNamesToElement(dom, ...classesToAdd);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
function updateListItemChecked(dom, listItemNode, prevListItemNode) {
|
|
925
|
+
const parent = listItemNode.getParent();
|
|
926
|
+
const isCheckbox = $isListNode(parent) && parent.getListType() === 'check' &&
|
|
927
|
+
// Only add attributes for leaf list items
|
|
928
|
+
!$isListNode(listItemNode.getFirstChild());
|
|
929
|
+
if (!isCheckbox) {
|
|
930
|
+
dom.removeAttribute('role');
|
|
931
|
+
dom.removeAttribute('tabIndex');
|
|
932
|
+
dom.removeAttribute('aria-checked');
|
|
933
|
+
} else {
|
|
934
|
+
dom.setAttribute('role', 'checkbox');
|
|
935
|
+
dom.setAttribute('tabIndex', '-1');
|
|
936
|
+
if (!prevListItemNode || listItemNode.__checked !== prevListItemNode.__checked) {
|
|
937
|
+
dom.setAttribute('aria-checked', listItemNode.getChecked() ? 'true' : 'false');
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
function $convertListItemElement(domNode) {
|
|
942
|
+
const isGitHubCheckList = domNode.classList.contains('task-list-item');
|
|
943
|
+
if (isGitHubCheckList) {
|
|
944
|
+
for (const child of domNode.children) {
|
|
945
|
+
if (child.tagName === 'INPUT') {
|
|
946
|
+
return $convertCheckboxInput(child);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
const isJoplinCheckList = domNode.classList.contains('joplin-checkbox');
|
|
951
|
+
if (isJoplinCheckList) {
|
|
952
|
+
for (const child of domNode.children) {
|
|
953
|
+
if (child.classList.contains('checkbox-wrapper') && child.children.length > 0 && child.children[0].tagName === 'INPUT') {
|
|
954
|
+
return $convertCheckboxInput(child.children[0]);
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
const ariaCheckedAttr = domNode.getAttribute('aria-checked');
|
|
959
|
+
const checked = ariaCheckedAttr === 'true' ? true : ariaCheckedAttr === 'false' ? false : undefined;
|
|
960
|
+
return {
|
|
961
|
+
node: $createListItemNode(checked)
|
|
962
|
+
};
|
|
963
|
+
}
|
|
964
|
+
function $convertCheckboxInput(domNode) {
|
|
965
|
+
const isCheckboxInput = domNode.getAttribute('type') === 'checkbox';
|
|
966
|
+
if (!isCheckboxInput) {
|
|
967
|
+
return {
|
|
968
|
+
node: null
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
const checked = domNode.hasAttribute('checked');
|
|
972
|
+
return {
|
|
973
|
+
node: $createListItemNode(checked)
|
|
974
|
+
};
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
/**
|
|
978
|
+
* Creates a new List Item node, passing true/false will convert it to a checkbox input.
|
|
979
|
+
* @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.
|
|
980
|
+
* @returns The new List Item.
|
|
981
|
+
*/
|
|
982
|
+
function $createListItemNode(checked) {
|
|
983
|
+
return lexical.$applyNodeReplacement(new ListItemNode(undefined, checked));
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
/**
|
|
987
|
+
* Checks to see if the node is a ListItemNode.
|
|
988
|
+
* @param node - The node to be checked.
|
|
989
|
+
* @returns true if the node is a ListItemNode, false otherwise.
|
|
990
|
+
*/
|
|
991
|
+
function $isListItemNode(node) {
|
|
992
|
+
return node instanceof ListItemNode;
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
/**
|
|
996
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
997
|
+
*
|
|
998
|
+
* This source code is licensed under the MIT license found in the
|
|
999
|
+
* LICENSE file in the root directory of this source tree.
|
|
1000
|
+
*
|
|
1001
|
+
*/
|
|
1002
|
+
|
|
1003
|
+
/** @noInheritDoc */
|
|
1004
|
+
class ListNode extends lexical.ElementNode {
|
|
1005
|
+
/** @internal */
|
|
1006
|
+
__tag;
|
|
1007
|
+
/** @internal */
|
|
1008
|
+
__start;
|
|
1009
|
+
/** @internal */
|
|
1010
|
+
__listType;
|
|
1011
|
+
|
|
1012
|
+
/** @internal */
|
|
1013
|
+
$config() {
|
|
1014
|
+
return this.config('list', {
|
|
1015
|
+
$transform: node => {
|
|
1016
|
+
mergeNextSiblingListIfSameType(node);
|
|
1017
|
+
updateChildrenListItemValue(node);
|
|
1018
|
+
},
|
|
1019
|
+
extends: lexical.ElementNode,
|
|
1020
|
+
importDOM: lexical.buildImportMap({
|
|
1021
|
+
ol: () => ({
|
|
1022
|
+
conversion: $convertListNode,
|
|
1023
|
+
priority: 0
|
|
1024
|
+
}),
|
|
1025
|
+
ul: () => ({
|
|
1026
|
+
conversion: $convertListNode,
|
|
1027
|
+
priority: 0
|
|
1028
|
+
})
|
|
1029
|
+
})
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
constructor(listType = 'number', start = 1, key) {
|
|
1033
|
+
super(key);
|
|
1034
|
+
const _listType = TAG_TO_LIST_TYPE[listType] || listType;
|
|
1035
|
+
this.__listType = _listType;
|
|
1036
|
+
this.__tag = _listType === 'number' ? 'ol' : 'ul';
|
|
1037
|
+
this.__start = start;
|
|
1038
|
+
}
|
|
1039
|
+
afterCloneFrom(prevNode) {
|
|
1040
|
+
super.afterCloneFrom(prevNode);
|
|
1041
|
+
this.__listType = prevNode.__listType;
|
|
1042
|
+
this.__tag = prevNode.__tag;
|
|
1043
|
+
this.__start = prevNode.__start;
|
|
1044
|
+
}
|
|
1045
|
+
getTag() {
|
|
1046
|
+
return this.getLatest().__tag;
|
|
1047
|
+
}
|
|
1048
|
+
setListType(type) {
|
|
1049
|
+
const writable = this.getWritable();
|
|
1050
|
+
writable.__listType = type;
|
|
1051
|
+
writable.__tag = type === 'number' ? 'ol' : 'ul';
|
|
1052
|
+
return writable;
|
|
1053
|
+
}
|
|
1054
|
+
getListType() {
|
|
1055
|
+
return this.getLatest().__listType;
|
|
1056
|
+
}
|
|
1057
|
+
getStart() {
|
|
1058
|
+
return this.getLatest().__start;
|
|
1059
|
+
}
|
|
1060
|
+
setStart(start) {
|
|
1061
|
+
const self = this.getWritable();
|
|
1062
|
+
self.__start = start;
|
|
1063
|
+
return self;
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
// View
|
|
1067
|
+
|
|
1068
|
+
createDOM(config, _editor) {
|
|
1069
|
+
const tag = this.__tag;
|
|
1070
|
+
const dom = document.createElement(tag);
|
|
1071
|
+
if (this.__start !== 1) {
|
|
1072
|
+
dom.setAttribute('start', String(this.__start));
|
|
1073
|
+
}
|
|
1074
|
+
// @ts-expect-error Internal field.
|
|
1075
|
+
dom.__lexicalListType = this.__listType;
|
|
1076
|
+
$setListThemeClassNames(dom, config.theme, this);
|
|
1077
|
+
return dom;
|
|
1078
|
+
}
|
|
1079
|
+
updateDOM(prevNode, dom, config) {
|
|
1080
|
+
if (prevNode.__tag !== this.__tag || prevNode.__listType !== this.__listType) {
|
|
1081
|
+
return true;
|
|
1082
|
+
}
|
|
1083
|
+
$setListThemeClassNames(dom, config.theme, this);
|
|
1084
|
+
return false;
|
|
1085
|
+
}
|
|
1086
|
+
updateFromJSON(serializedNode) {
|
|
1087
|
+
return super.updateFromJSON(serializedNode).setListType(serializedNode.listType).setStart(serializedNode.start);
|
|
1088
|
+
}
|
|
1089
|
+
exportDOM(editor) {
|
|
1090
|
+
const element = this.createDOM(editor._config, editor);
|
|
1091
|
+
if (lexicalUtils.isHTMLElement(element)) {
|
|
1092
|
+
if (this.__start !== 1) {
|
|
1093
|
+
element.setAttribute('start', String(this.__start));
|
|
1094
|
+
}
|
|
1095
|
+
if (this.__listType === 'check') {
|
|
1096
|
+
element.setAttribute('__lexicalListType', 'check');
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
return {
|
|
1100
|
+
element
|
|
1101
|
+
};
|
|
1102
|
+
}
|
|
1103
|
+
exportJSON() {
|
|
1104
|
+
return {
|
|
1105
|
+
...super.exportJSON(),
|
|
1106
|
+
listType: this.getListType(),
|
|
1107
|
+
start: this.getStart(),
|
|
1108
|
+
tag: this.getTag()
|
|
1109
|
+
};
|
|
1110
|
+
}
|
|
1111
|
+
canBeEmpty() {
|
|
1112
|
+
return false;
|
|
1113
|
+
}
|
|
1114
|
+
canIndent() {
|
|
1115
|
+
return false;
|
|
1116
|
+
}
|
|
1117
|
+
splice(start, deleteCount, nodesToInsert) {
|
|
1118
|
+
let listItemNodesToInsert = nodesToInsert;
|
|
1119
|
+
for (let i = 0; i < nodesToInsert.length; i++) {
|
|
1120
|
+
const node = nodesToInsert[i];
|
|
1121
|
+
if (!$isListItemNode(node)) {
|
|
1122
|
+
if (listItemNodesToInsert === nodesToInsert) {
|
|
1123
|
+
listItemNodesToInsert = [...nodesToInsert];
|
|
1124
|
+
}
|
|
1125
|
+
listItemNodesToInsert[i] = $createListItemNode().append(lexical.$isElementNode(node) && !($isListNode(node) || node.isInline()) ? lexical.$createTextNode(node.getTextContent()) : node);
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
return super.splice(start, deleteCount, listItemNodesToInsert);
|
|
1129
|
+
}
|
|
1130
|
+
extractWithChild(child) {
|
|
1131
|
+
return $isListItemNode(child);
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
function $setListThemeClassNames(dom, editorThemeClasses, node) {
|
|
1135
|
+
const classesToAdd = [];
|
|
1136
|
+
const classesToRemove = [];
|
|
1137
|
+
const listTheme = editorThemeClasses.list;
|
|
1138
|
+
if (listTheme !== undefined) {
|
|
1139
|
+
const listLevelsClassNames = listTheme[`${node.__tag}Depth`] || [];
|
|
1140
|
+
const listDepth = $getListDepth(node) - 1;
|
|
1141
|
+
const normalizedListDepth = listDepth % listLevelsClassNames.length;
|
|
1142
|
+
const listLevelClassName = listLevelsClassNames[normalizedListDepth];
|
|
1143
|
+
const listClassName = listTheme[node.__tag];
|
|
1144
|
+
let nestedListClassName;
|
|
1145
|
+
const nestedListTheme = listTheme.nested;
|
|
1146
|
+
const checklistClassName = listTheme.checklist;
|
|
1147
|
+
if (nestedListTheme !== undefined && nestedListTheme.list) {
|
|
1148
|
+
nestedListClassName = nestedListTheme.list;
|
|
1149
|
+
}
|
|
1150
|
+
if (listClassName !== undefined) {
|
|
1151
|
+
classesToAdd.push(listClassName);
|
|
1152
|
+
}
|
|
1153
|
+
if (checklistClassName !== undefined && node.__listType === 'check') {
|
|
1154
|
+
classesToAdd.push(checklistClassName);
|
|
1155
|
+
}
|
|
1156
|
+
if (listLevelClassName !== undefined) {
|
|
1157
|
+
classesToAdd.push(...lexical.normalizeClassNames(listLevelClassName));
|
|
1158
|
+
for (let i = 0; i < listLevelsClassNames.length; i++) {
|
|
1159
|
+
if (i !== normalizedListDepth) {
|
|
1160
|
+
classesToRemove.push(node.__tag + i);
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
if (nestedListClassName !== undefined) {
|
|
1165
|
+
const nestedListItemClasses = lexical.normalizeClassNames(nestedListClassName);
|
|
1166
|
+
if (listDepth > 1) {
|
|
1167
|
+
classesToAdd.push(...nestedListItemClasses);
|
|
1168
|
+
} else {
|
|
1169
|
+
classesToRemove.push(...nestedListItemClasses);
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
if (classesToRemove.length > 0) {
|
|
1174
|
+
lexicalUtils.removeClassNamesFromElement(dom, ...classesToRemove);
|
|
1175
|
+
}
|
|
1176
|
+
if (classesToAdd.length > 0) {
|
|
1177
|
+
lexicalUtils.addClassNamesToElement(dom, ...classesToAdd);
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
/*
|
|
1182
|
+
* This function normalizes the children of a ListNode after the conversion from HTML,
|
|
1183
|
+
* ensuring that they are all ListItemNodes and contain either a single nested ListNode
|
|
1184
|
+
* or some other inline content.
|
|
1185
|
+
*/
|
|
1186
|
+
function $normalizeChildren(nodes) {
|
|
1187
|
+
const normalizedListItems = [];
|
|
1188
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
1189
|
+
const node = nodes[i];
|
|
1190
|
+
if ($isListItemNode(node)) {
|
|
1191
|
+
normalizedListItems.push(node);
|
|
1192
|
+
const children = node.getChildren();
|
|
1193
|
+
if (children.length > 1) {
|
|
1194
|
+
children.forEach(child => {
|
|
1195
|
+
if ($isListNode(child)) {
|
|
1196
|
+
normalizedListItems.push($wrapInListItem(child));
|
|
1197
|
+
}
|
|
1198
|
+
});
|
|
1199
|
+
}
|
|
1200
|
+
} else {
|
|
1201
|
+
normalizedListItems.push($wrapInListItem(node));
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
return normalizedListItems;
|
|
1205
|
+
}
|
|
1206
|
+
function isDomChecklist(domNode) {
|
|
1207
|
+
if (domNode.getAttribute('__lexicallisttype') === 'check' ||
|
|
1208
|
+
// is github checklist
|
|
1209
|
+
domNode.classList.contains('contains-task-list') ||
|
|
1210
|
+
// is joplin checklist
|
|
1211
|
+
domNode.getAttribute('data-is-checklist') === '1') {
|
|
1212
|
+
return true;
|
|
1213
|
+
}
|
|
1214
|
+
// if children are checklist items, the node is a checklist ul. Applicable for googledoc checklist pasting.
|
|
1215
|
+
for (const child of domNode.childNodes) {
|
|
1216
|
+
if (lexicalUtils.isHTMLElement(child) && child.hasAttribute('aria-checked')) {
|
|
1217
|
+
return true;
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
return false;
|
|
1221
|
+
}
|
|
1222
|
+
function $convertListNode(domNode) {
|
|
1223
|
+
const nodeName = domNode.nodeName.toLowerCase();
|
|
1224
|
+
let node = null;
|
|
1225
|
+
if (nodeName === 'ol') {
|
|
1226
|
+
// @ts-ignore
|
|
1227
|
+
const start = domNode.start;
|
|
1228
|
+
node = $createListNode('number', start);
|
|
1229
|
+
} else if (nodeName === 'ul') {
|
|
1230
|
+
if (isDomChecklist(domNode)) {
|
|
1231
|
+
node = $createListNode('check');
|
|
1232
|
+
} else {
|
|
1233
|
+
node = $createListNode('bullet');
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
return {
|
|
1237
|
+
after: $normalizeChildren,
|
|
1238
|
+
node
|
|
1239
|
+
};
|
|
1240
|
+
}
|
|
1241
|
+
const TAG_TO_LIST_TYPE = {
|
|
1242
|
+
ol: 'number',
|
|
1243
|
+
ul: 'bullet'
|
|
1244
|
+
};
|
|
1245
|
+
|
|
1246
|
+
/**
|
|
1247
|
+
* Creates a ListNode of listType.
|
|
1248
|
+
* @param listType - The type of list to be created. Can be 'number', 'bullet', or 'check'.
|
|
1249
|
+
* @param start - Where an ordered list starts its count, start = 1 if left undefined.
|
|
1250
|
+
* @returns The new ListNode
|
|
1251
|
+
*/
|
|
1252
|
+
function $createListNode(listType = 'number', start = 1) {
|
|
1253
|
+
return lexical.$applyNodeReplacement(new ListNode(listType, start));
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
/**
|
|
1257
|
+
* Checks to see if the node is a ListNode.
|
|
1258
|
+
* @param node - The node to be checked.
|
|
1259
|
+
* @returns true if the node is a ListNode, false otherwise.
|
|
1260
|
+
*/
|
|
1261
|
+
function $isListNode(node) {
|
|
1262
|
+
return node instanceof ListNode;
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
/**
|
|
1266
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1267
|
+
*
|
|
1268
|
+
* This source code is licensed under the MIT license found in the
|
|
1269
|
+
* LICENSE file in the root directory of this source tree.
|
|
1270
|
+
*
|
|
1271
|
+
*/
|
|
1272
|
+
|
|
1273
|
+
const INSERT_CHECK_LIST_COMMAND = lexical.createCommand('INSERT_CHECK_LIST_COMMAND');
|
|
1274
|
+
function registerCheckList(editor) {
|
|
1275
|
+
return lexicalUtils.mergeRegister(editor.registerCommand(INSERT_CHECK_LIST_COMMAND, () => {
|
|
1276
|
+
$insertList('check');
|
|
1277
|
+
return true;
|
|
1278
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.KEY_ARROW_DOWN_COMMAND, event => {
|
|
1279
|
+
return handleArrowUpOrDown(event, editor, false);
|
|
1280
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.KEY_ARROW_UP_COMMAND, event => {
|
|
1281
|
+
return handleArrowUpOrDown(event, editor, true);
|
|
1282
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.KEY_ESCAPE_COMMAND, () => {
|
|
1283
|
+
const activeItem = getActiveCheckListItem();
|
|
1284
|
+
if (activeItem != null) {
|
|
1285
|
+
const rootElement = editor.getRootElement();
|
|
1286
|
+
if (rootElement != null) {
|
|
1287
|
+
rootElement.focus();
|
|
1288
|
+
}
|
|
1289
|
+
return true;
|
|
1290
|
+
}
|
|
1291
|
+
return false;
|
|
1292
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.KEY_SPACE_COMMAND, event => {
|
|
1293
|
+
const activeItem = getActiveCheckListItem();
|
|
1294
|
+
if (activeItem != null && editor.isEditable()) {
|
|
1295
|
+
editor.update(() => {
|
|
1296
|
+
const listItemNode = lexical.$getNearestNodeFromDOMNode(activeItem);
|
|
1297
|
+
if ($isListItemNode(listItemNode)) {
|
|
1298
|
+
event.preventDefault();
|
|
1299
|
+
listItemNode.toggleChecked();
|
|
1300
|
+
}
|
|
1301
|
+
});
|
|
1302
|
+
return true;
|
|
1303
|
+
}
|
|
1304
|
+
return false;
|
|
1305
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.KEY_ARROW_LEFT_COMMAND, event => {
|
|
1306
|
+
return editor.getEditorState().read(() => {
|
|
1307
|
+
const selection = lexical.$getSelection();
|
|
1308
|
+
if (lexical.$isRangeSelection(selection) && selection.isCollapsed()) {
|
|
1309
|
+
const {
|
|
1310
|
+
anchor
|
|
1311
|
+
} = selection;
|
|
1312
|
+
const isElement = anchor.type === 'element';
|
|
1313
|
+
if (isElement || anchor.offset === 0) {
|
|
1314
|
+
const anchorNode = anchor.getNode();
|
|
1315
|
+
const elementNode = lexicalUtils.$findMatchingParent(anchorNode, node => lexical.$isElementNode(node) && !node.isInline());
|
|
1316
|
+
if ($isListItemNode(elementNode)) {
|
|
1317
|
+
const parent = elementNode.getParent();
|
|
1318
|
+
if ($isListNode(parent) && parent.getListType() === 'check' && (isElement || elementNode.getFirstDescendant() === anchorNode)) {
|
|
1319
|
+
const domNode = editor.getElementByKey(elementNode.__key);
|
|
1320
|
+
if (domNode != null && document.activeElement !== domNode) {
|
|
1321
|
+
domNode.focus();
|
|
1322
|
+
event.preventDefault();
|
|
1323
|
+
return true;
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
return false;
|
|
1330
|
+
});
|
|
1331
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerRootListener((rootElement, prevElement) => {
|
|
1332
|
+
if (rootElement !== null) {
|
|
1333
|
+
rootElement.addEventListener('click', handleClick);
|
|
1334
|
+
rootElement.addEventListener('pointerdown', handlePointerDown);
|
|
1335
|
+
}
|
|
1336
|
+
if (prevElement !== null) {
|
|
1337
|
+
prevElement.removeEventListener('click', handleClick);
|
|
1338
|
+
prevElement.removeEventListener('pointerdown', handlePointerDown);
|
|
1339
|
+
}
|
|
1340
|
+
}));
|
|
1341
|
+
}
|
|
1342
|
+
function handleCheckItemEvent(event, callback) {
|
|
1343
|
+
const target = event.target;
|
|
1344
|
+
if (!lexicalUtils.isHTMLElement(target)) {
|
|
1345
|
+
return;
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
// Ignore clicks on LI that have nested lists
|
|
1349
|
+
const firstChild = target.firstChild;
|
|
1350
|
+
if (lexicalUtils.isHTMLElement(firstChild) && (firstChild.tagName === 'UL' || firstChild.tagName === 'OL')) {
|
|
1351
|
+
return;
|
|
1352
|
+
}
|
|
1353
|
+
const parentNode = target.parentNode;
|
|
1354
|
+
|
|
1355
|
+
// @ts-ignore internal field
|
|
1356
|
+
if (!parentNode || parentNode.__lexicalListType !== 'check') {
|
|
1357
|
+
return;
|
|
1358
|
+
}
|
|
1359
|
+
const rect = target.getBoundingClientRect();
|
|
1360
|
+
const zoom = lexicalUtils.calculateZoomLevel(target);
|
|
1361
|
+
const clientX = event.clientX / zoom;
|
|
1362
|
+
|
|
1363
|
+
// Use getComputedStyle if available, otherwise fallback to 0px width
|
|
1364
|
+
const beforeStyles = window.getComputedStyle ? window.getComputedStyle(target, '::before') : {
|
|
1365
|
+
width: '0px'
|
|
1366
|
+
};
|
|
1367
|
+
const beforeWidthInPixels = parseFloat(beforeStyles.width);
|
|
1368
|
+
|
|
1369
|
+
// Make click area slightly larger for touch devices to improve accessibility
|
|
1370
|
+
const isTouchEvent = event.pointerType === 'touch';
|
|
1371
|
+
const clickAreaPadding = isTouchEvent ? 32 : 0; // Add 32px padding for touch events
|
|
1372
|
+
|
|
1373
|
+
if (target.dir === 'rtl' ? clientX < rect.right + clickAreaPadding && clientX > rect.right - beforeWidthInPixels - clickAreaPadding : clientX > rect.left - clickAreaPadding && clientX < rect.left + beforeWidthInPixels + clickAreaPadding) {
|
|
1374
|
+
callback();
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
function handleClick(event) {
|
|
1378
|
+
handleCheckItemEvent(event, () => {
|
|
1379
|
+
if (lexicalUtils.isHTMLElement(event.target)) {
|
|
1380
|
+
const domNode = event.target;
|
|
1381
|
+
const editor = lexical.getNearestEditorFromDOMNode(domNode);
|
|
1382
|
+
if (editor != null && editor.isEditable()) {
|
|
1383
|
+
editor.update(() => {
|
|
1384
|
+
const node = lexical.$getNearestNodeFromDOMNode(domNode);
|
|
1385
|
+
if ($isListItemNode(node)) {
|
|
1386
|
+
domNode.focus();
|
|
1387
|
+
node.toggleChecked();
|
|
1388
|
+
}
|
|
1389
|
+
});
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
});
|
|
1393
|
+
}
|
|
1394
|
+
function handlePointerDown(event) {
|
|
1395
|
+
handleCheckItemEvent(event, () => {
|
|
1396
|
+
// Prevents caret moving when clicking on check mark
|
|
1397
|
+
event.preventDefault();
|
|
1398
|
+
});
|
|
1399
|
+
}
|
|
1400
|
+
function getActiveCheckListItem() {
|
|
1401
|
+
const activeElement = document.activeElement;
|
|
1402
|
+
return lexicalUtils.isHTMLElement(activeElement) && activeElement.tagName === 'LI' && activeElement.parentNode != null &&
|
|
1403
|
+
// @ts-ignore internal field
|
|
1404
|
+
activeElement.parentNode.__lexicalListType === 'check' ? activeElement : null;
|
|
1405
|
+
}
|
|
1406
|
+
function findCheckListItemSibling(node, backward) {
|
|
1407
|
+
let sibling = backward ? node.getPreviousSibling() : node.getNextSibling();
|
|
1408
|
+
let parent = node;
|
|
1409
|
+
|
|
1410
|
+
// Going up in a tree to get non-null sibling
|
|
1411
|
+
while (sibling == null && $isListItemNode(parent)) {
|
|
1412
|
+
// Get li -> parent ul/ol -> parent li
|
|
1413
|
+
parent = parent.getParentOrThrow().getParent();
|
|
1414
|
+
if (parent != null) {
|
|
1415
|
+
sibling = backward ? parent.getPreviousSibling() : parent.getNextSibling();
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
// Going down in a tree to get first non-nested list item
|
|
1420
|
+
while ($isListItemNode(sibling)) {
|
|
1421
|
+
const firstChild = backward ? sibling.getLastChild() : sibling.getFirstChild();
|
|
1422
|
+
if (!$isListNode(firstChild)) {
|
|
1423
|
+
return sibling;
|
|
1424
|
+
}
|
|
1425
|
+
sibling = backward ? firstChild.getLastChild() : firstChild.getFirstChild();
|
|
1426
|
+
}
|
|
1427
|
+
return null;
|
|
1428
|
+
}
|
|
1429
|
+
function handleArrowUpOrDown(event, editor, backward) {
|
|
1430
|
+
const activeItem = getActiveCheckListItem();
|
|
1431
|
+
if (activeItem != null) {
|
|
1432
|
+
editor.update(() => {
|
|
1433
|
+
const listItem = lexical.$getNearestNodeFromDOMNode(activeItem);
|
|
1434
|
+
if (!$isListItemNode(listItem)) {
|
|
1435
|
+
return;
|
|
1436
|
+
}
|
|
1437
|
+
const nextListItem = findCheckListItemSibling(listItem, backward);
|
|
1438
|
+
if (nextListItem != null) {
|
|
1439
|
+
nextListItem.selectStart();
|
|
1440
|
+
const dom = editor.getElementByKey(nextListItem.__key);
|
|
1441
|
+
if (dom != null) {
|
|
1442
|
+
event.preventDefault();
|
|
1443
|
+
setTimeout(() => {
|
|
1444
|
+
dom.focus();
|
|
1445
|
+
}, 0);
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
});
|
|
1449
|
+
}
|
|
1450
|
+
return false;
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
/**
|
|
1454
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1455
|
+
*
|
|
1456
|
+
* This source code is licensed under the MIT license found in the
|
|
1457
|
+
* LICENSE file in the root directory of this source tree.
|
|
1458
|
+
*
|
|
1459
|
+
*/
|
|
1460
|
+
|
|
1461
|
+
const UPDATE_LIST_START_COMMAND = lexical.createCommand('UPDATE_LIST_START_COMMAND');
|
|
1462
|
+
const INSERT_UNORDERED_LIST_COMMAND = lexical.createCommand('INSERT_UNORDERED_LIST_COMMAND');
|
|
1463
|
+
const INSERT_ORDERED_LIST_COMMAND = lexical.createCommand('INSERT_ORDERED_LIST_COMMAND');
|
|
1464
|
+
const REMOVE_LIST_COMMAND = lexical.createCommand('REMOVE_LIST_COMMAND');
|
|
1465
|
+
function registerList(editor) {
|
|
1466
|
+
const removeListener = lexicalUtils.mergeRegister(editor.registerCommand(INSERT_ORDERED_LIST_COMMAND, () => {
|
|
1467
|
+
$insertList('number');
|
|
1468
|
+
return true;
|
|
1469
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(UPDATE_LIST_START_COMMAND, payload => {
|
|
1470
|
+
const {
|
|
1471
|
+
listNodeKey,
|
|
1472
|
+
newStart
|
|
1473
|
+
} = payload;
|
|
1474
|
+
const listNode = lexical.$getNodeByKey(listNodeKey);
|
|
1475
|
+
if (!$isListNode(listNode)) {
|
|
1476
|
+
return false;
|
|
1477
|
+
}
|
|
1478
|
+
if (listNode.getListType() === 'number') {
|
|
1479
|
+
listNode.setStart(newStart);
|
|
1480
|
+
updateChildrenListItemValue(listNode);
|
|
1481
|
+
}
|
|
1482
|
+
return true;
|
|
1483
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(INSERT_UNORDERED_LIST_COMMAND, () => {
|
|
1484
|
+
$insertList('bullet');
|
|
1485
|
+
return true;
|
|
1486
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(REMOVE_LIST_COMMAND, () => {
|
|
1487
|
+
$removeList();
|
|
1488
|
+
return true;
|
|
1489
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.INSERT_PARAGRAPH_COMMAND, () => $handleListInsertParagraph(), lexical.COMMAND_PRIORITY_LOW), editor.registerNodeTransform(ListItemNode, node => {
|
|
1490
|
+
const firstChild = node.getFirstChild();
|
|
1491
|
+
if (firstChild) {
|
|
1492
|
+
if (lexical.$isTextNode(firstChild)) {
|
|
1493
|
+
const style = firstChild.getStyle();
|
|
1494
|
+
const format = firstChild.getFormat();
|
|
1495
|
+
if (node.getTextStyle() !== style) {
|
|
1496
|
+
node.setTextStyle(style);
|
|
1497
|
+
}
|
|
1498
|
+
if (node.getTextFormat() !== format) {
|
|
1499
|
+
node.setTextFormat(format);
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
} else {
|
|
1503
|
+
// If it's empty, check the selection
|
|
1504
|
+
const selection = lexical.$getSelection();
|
|
1505
|
+
if (lexical.$isRangeSelection(selection) && (selection.style !== node.getTextStyle() || selection.format !== node.getTextFormat()) && selection.isCollapsed() && node.is(selection.anchor.getNode())) {
|
|
1506
|
+
node.setTextStyle(selection.style).setTextFormat(selection.format);
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
}), editor.registerNodeTransform(lexical.TextNode, node => {
|
|
1510
|
+
const listItemParentNode = node.getParent();
|
|
1511
|
+
if ($isListItemNode(listItemParentNode) && node.is(listItemParentNode.getFirstChild())) {
|
|
1512
|
+
const style = node.getStyle();
|
|
1513
|
+
const format = node.getFormat();
|
|
1514
|
+
if (style !== listItemParentNode.getTextStyle() || format !== listItemParentNode.getTextFormat()) {
|
|
1515
|
+
listItemParentNode.setTextStyle(style).setTextFormat(format);
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
}));
|
|
1519
|
+
return removeListener;
|
|
1520
|
+
}
|
|
1521
|
+
function registerListStrictIndentTransform(editor) {
|
|
1522
|
+
const $formatListIndentStrict = listItemNode => {
|
|
1523
|
+
const listNode = listItemNode.getParent();
|
|
1524
|
+
if ($isListNode(listItemNode.getFirstChild()) || !$isListNode(listNode)) {
|
|
1525
|
+
return;
|
|
1526
|
+
}
|
|
1527
|
+
const startingListItemNode = lexicalUtils.$findMatchingParent(listItemNode, node => $isListItemNode(node) && $isListNode(node.getParent()) && $isListItemNode(node.getPreviousSibling()));
|
|
1528
|
+
if (startingListItemNode === null && listItemNode.getIndent() > 0) {
|
|
1529
|
+
listItemNode.setIndent(0);
|
|
1530
|
+
} else if ($isListItemNode(startingListItemNode)) {
|
|
1531
|
+
const prevListItemNode = startingListItemNode.getPreviousSibling();
|
|
1532
|
+
if ($isListItemNode(prevListItemNode)) {
|
|
1533
|
+
const endListItemNode = $findChildrenEndListItemNode(prevListItemNode);
|
|
1534
|
+
const endListNode = endListItemNode.getParent();
|
|
1535
|
+
if ($isListNode(endListNode)) {
|
|
1536
|
+
const prevDepth = $getListDepth(endListNode);
|
|
1537
|
+
const depth = $getListDepth(listNode);
|
|
1538
|
+
if (prevDepth + 1 < depth) {
|
|
1539
|
+
listItemNode.setIndent(prevDepth);
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
}
|
|
1544
|
+
};
|
|
1545
|
+
const $processListWithStrictIndent = listNode => {
|
|
1546
|
+
const queue = [listNode];
|
|
1547
|
+
while (queue.length > 0) {
|
|
1548
|
+
const node = queue.shift();
|
|
1549
|
+
if (!$isListNode(node)) {
|
|
1550
|
+
continue;
|
|
1551
|
+
}
|
|
1552
|
+
for (const child of node.getChildren()) {
|
|
1553
|
+
if ($isListItemNode(child)) {
|
|
1554
|
+
$formatListIndentStrict(child);
|
|
1555
|
+
const firstChild = child.getFirstChild();
|
|
1556
|
+
if ($isListNode(firstChild)) {
|
|
1557
|
+
queue.push(firstChild);
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
1562
|
+
};
|
|
1563
|
+
return editor.registerNodeTransform(ListNode, $processListWithStrictIndent);
|
|
1564
|
+
}
|
|
1565
|
+
function $findChildrenEndListItemNode(listItemNode) {
|
|
1566
|
+
let current = listItemNode;
|
|
1567
|
+
let firstChild = current.getFirstChild();
|
|
1568
|
+
while ($isListNode(firstChild)) {
|
|
1569
|
+
const lastChild = firstChild.getLastChild();
|
|
1570
|
+
if ($isListItemNode(lastChild)) {
|
|
1571
|
+
current = lastChild;
|
|
1572
|
+
firstChild = current.getFirstChild();
|
|
1573
|
+
} else {
|
|
1574
|
+
break;
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
return current;
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
/**
|
|
1581
|
+
* @deprecated use {@link $insertList} from an update or command listener.
|
|
1582
|
+
*
|
|
1583
|
+
* Inserts a new ListNode. If the selection's anchor node is an empty ListItemNode and is a child of
|
|
1584
|
+
* the root/shadow root, it will replace the ListItemNode with a ListNode and the old ListItemNode.
|
|
1585
|
+
* Otherwise it will replace its parent with a new ListNode and re-insert the ListItemNode and any previous children.
|
|
1586
|
+
* If the selection's anchor node is not an empty ListItemNode, it will add a new ListNode or merge an existing ListNode,
|
|
1587
|
+
* 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
|
|
1588
|
+
* a new ListNode, or create a new ListNode at the nearest root/shadow root.
|
|
1589
|
+
* @param editor - The lexical editor.
|
|
1590
|
+
* @param listType - The type of list, "number" | "bullet" | "check".
|
|
1591
|
+
*/
|
|
1592
|
+
function insertList(editor, listType) {
|
|
1593
|
+
editor.update(() => $insertList(listType));
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1596
|
+
/**
|
|
1597
|
+
* @deprecated use {@link $removeList} from an update or command listener.
|
|
1598
|
+
*
|
|
1599
|
+
* Searches for the nearest ancestral ListNode and removes it. If selection is an empty ListItemNode
|
|
1600
|
+
* it will remove the whole list, including the ListItemNode. For each ListItemNode in the ListNode,
|
|
1601
|
+
* removeList will also generate new ParagraphNodes in the removed ListNode's place. Any child node
|
|
1602
|
+
* inside a ListItemNode will be appended to the new ParagraphNodes.
|
|
1603
|
+
* @param editor - The lexical editor.
|
|
1604
|
+
*/
|
|
1605
|
+
function removeList(editor) {
|
|
1606
|
+
editor.update(() => $removeList());
|
|
1607
|
+
}
|
|
1608
|
+
/**
|
|
1609
|
+
* Configures {@link ListNode}, {@link ListItemNode} and registers
|
|
1610
|
+
* the strict indent transform if `hasStrictIndent` is true (default false).
|
|
1611
|
+
*/
|
|
1612
|
+
const ListExtension = lexical.defineExtension({
|
|
1613
|
+
build(editor, config, state) {
|
|
1614
|
+
return lexicalExtension.namedSignals(config);
|
|
1615
|
+
},
|
|
1616
|
+
config: lexical.safeCast({
|
|
1617
|
+
hasStrictIndent: false
|
|
1618
|
+
}),
|
|
1619
|
+
name: '@ekz/lexical-list/List',
|
|
1620
|
+
nodes: () => [ListNode, ListItemNode],
|
|
1621
|
+
register(editor, config, state) {
|
|
1622
|
+
const stores = state.getOutput();
|
|
1623
|
+
return lexicalUtils.mergeRegister(registerList(editor), lexicalExtension.effect(() => stores.hasStrictIndent.value ? registerListStrictIndentTransform(editor) : undefined));
|
|
1624
|
+
}
|
|
1625
|
+
});
|
|
1626
|
+
|
|
1627
|
+
/**
|
|
1628
|
+
* Registers checklist functionality for {@link ListNode} and
|
|
1629
|
+
* {@link ListItemNode} with a
|
|
1630
|
+
* {@link INSERT_CHECK_LIST_COMMAND} listener and
|
|
1631
|
+
* the expected keyboard and mouse interactions for
|
|
1632
|
+
* checkboxes.
|
|
1633
|
+
*/
|
|
1634
|
+
const CheckListExtension = lexical.defineExtension({
|
|
1635
|
+
dependencies: [ListExtension],
|
|
1636
|
+
name: '@ekz/lexical-list/CheckList',
|
|
1637
|
+
register: registerCheckList
|
|
1638
|
+
});
|
|
1639
|
+
|
|
1640
|
+
exports.$createListItemNode = $createListItemNode;
|
|
1641
|
+
exports.$createListNode = $createListNode;
|
|
1642
|
+
exports.$getListDepth = $getListDepth;
|
|
1643
|
+
exports.$handleListInsertParagraph = $handleListInsertParagraph;
|
|
1644
|
+
exports.$insertList = $insertList;
|
|
1645
|
+
exports.$isListItemNode = $isListItemNode;
|
|
1646
|
+
exports.$isListNode = $isListNode;
|
|
1647
|
+
exports.$removeList = $removeList;
|
|
1648
|
+
exports.CheckListExtension = CheckListExtension;
|
|
1649
|
+
exports.INSERT_CHECK_LIST_COMMAND = INSERT_CHECK_LIST_COMMAND;
|
|
1650
|
+
exports.INSERT_ORDERED_LIST_COMMAND = INSERT_ORDERED_LIST_COMMAND;
|
|
1651
|
+
exports.INSERT_UNORDERED_LIST_COMMAND = INSERT_UNORDERED_LIST_COMMAND;
|
|
1652
|
+
exports.ListExtension = ListExtension;
|
|
1653
|
+
exports.ListItemNode = ListItemNode;
|
|
1654
|
+
exports.ListNode = ListNode;
|
|
1655
|
+
exports.REMOVE_LIST_COMMAND = REMOVE_LIST_COMMAND;
|
|
1656
|
+
exports.UPDATE_LIST_START_COMMAND = UPDATE_LIST_START_COMMAND;
|
|
1657
|
+
exports.insertList = insertList;
|
|
1658
|
+
exports.registerCheckList = registerCheckList;
|
|
1659
|
+
exports.registerList = registerList;
|
|
1660
|
+
exports.registerListStrictIndentTransform = registerListStrictIndentTransform;
|
|
1661
|
+
exports.removeList = removeList;
|