@lexical/list 0.1.16 → 0.1.17
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/LICENSE +1 -1
- package/LexicalList.d.ts +50 -0
- package/LexicalList.dev.js +92 -73
- package/LexicalList.js.flow +64 -0
- package/LexicalList.prod.js +22 -24
- package/package.json +3 -7
package/LICENSE
CHANGED
package/LexicalList.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
import {ListNodeTagType} from './src/LexicalListNode';
|
|
10
|
+
import {
|
|
11
|
+
ElementNode,
|
|
12
|
+
LexicalNode,
|
|
13
|
+
LexicalEditor,
|
|
14
|
+
ParagraphNode,
|
|
15
|
+
RangeSelection,
|
|
16
|
+
LexicalCommand,
|
|
17
|
+
} from 'lexical';
|
|
18
|
+
|
|
19
|
+
export function $createListItemNode(): ListItemNode;
|
|
20
|
+
export function $createListNode(tag: ListNodeTagType, start?: number): ListNode;
|
|
21
|
+
export function $getListDepth(listNode: ListNode): number;
|
|
22
|
+
export function $handleListInsertParagraph(): boolean;
|
|
23
|
+
export function $isListItemNode(node?: LexicalNode): boolean;
|
|
24
|
+
export function $isListNode(node?: LexicalNode): boolean;
|
|
25
|
+
export function indentList(): boolean;
|
|
26
|
+
export function insertList(editor: LexicalEditor, listType: 'ul' | 'ol'): void;
|
|
27
|
+
export declare class ListItemNode extends ElementNode {
|
|
28
|
+
append(...nodes: LexicalNode[]): ListItemNode;
|
|
29
|
+
replace<N extends LexicalNode>(replaceWithNode: N): N;
|
|
30
|
+
insertAfter(node: LexicalNode): LexicalNode;
|
|
31
|
+
insertNewAfter(): ListItemNode | ParagraphNode;
|
|
32
|
+
collapseAtStart(selection: RangeSelection): true;
|
|
33
|
+
getIndent(): number;
|
|
34
|
+
setIndent(indent: number): this;
|
|
35
|
+
insertBefore(nodeToInsert: LexicalNode): LexicalNode;
|
|
36
|
+
canInsertAfter(node: LexicalNode): boolean;
|
|
37
|
+
canReplaceWith(replacement: LexicalNode): boolean;
|
|
38
|
+
canMergeWith(node: LexicalNode): boolean;
|
|
39
|
+
}
|
|
40
|
+
export declare class ListNode extends ElementNode {
|
|
41
|
+
canBeEmpty(): false;
|
|
42
|
+
append(...nodesToAppend: LexicalNode[]): ListNode;
|
|
43
|
+
getTag(): ListNodeTagType;
|
|
44
|
+
}
|
|
45
|
+
export function outdentList(): boolean;
|
|
46
|
+
export function removeList(editor: LexicalEditor): boolean;
|
|
47
|
+
|
|
48
|
+
export var INSERT_UNORDERED_LIST_COMMAND: LexicalCommand<void>;
|
|
49
|
+
export var INSERT_ORDERED_LIST_COMMAND: LexicalCommand<void>;
|
|
50
|
+
export var REMOVE_LIST_COMMAND: LexicalCommand<void>;
|
package/LexicalList.dev.js
CHANGED
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
*/
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
|
-
var list = require('@lexical/list');
|
|
10
|
-
var utils = require('@lexical/utils');
|
|
11
9
|
var lexical = require('lexical');
|
|
10
|
+
var utils = require('@lexical/utils');
|
|
11
|
+
var list = require('@lexical/list');
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
@@ -23,10 +23,10 @@ function $getListDepth(listNode) {
|
|
|
23
23
|
let parent = listNode.getParent();
|
|
24
24
|
|
|
25
25
|
while (parent != null) {
|
|
26
|
-
if (
|
|
26
|
+
if ($isListItemNode(parent)) {
|
|
27
27
|
const parentList = parent.getParent();
|
|
28
28
|
|
|
29
|
-
if (
|
|
29
|
+
if ($isListNode(parentList)) {
|
|
30
30
|
depth++;
|
|
31
31
|
parent = parentList.getParent();
|
|
32
32
|
continue;
|
|
@@ -43,37 +43,37 @@ function $getListDepth(listNode) {
|
|
|
43
43
|
return depth;
|
|
44
44
|
}
|
|
45
45
|
function $getTopListNode(listItem) {
|
|
46
|
-
let list
|
|
46
|
+
let list = listItem.getParent();
|
|
47
47
|
|
|
48
|
-
if (
|
|
48
|
+
if (!$isListNode(list)) {
|
|
49
49
|
{
|
|
50
50
|
throw Error(`A ListItemNode must have a ListNode for a parent.`);
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
let parent = list
|
|
54
|
+
let parent = list;
|
|
55
55
|
|
|
56
56
|
while (parent !== null) {
|
|
57
57
|
parent = parent.getParent();
|
|
58
58
|
|
|
59
|
-
if (
|
|
60
|
-
list
|
|
59
|
+
if ($isListNode(parent)) {
|
|
60
|
+
list = parent;
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
return list
|
|
64
|
+
return list;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
function $getAllListItems(node) {
|
|
68
68
|
let listItemNodes = []; //$FlowFixMe - the result of this will always be an array of ListItemNodes.
|
|
69
69
|
|
|
70
|
-
const listChildren = node.getChildren().filter(
|
|
70
|
+
const listChildren = node.getChildren().filter($isListItemNode);
|
|
71
71
|
|
|
72
72
|
for (let i = 0; i < listChildren.length; i++) {
|
|
73
73
|
const listItemNode = listChildren[i];
|
|
74
74
|
const firstChild = listItemNode.getFirstChild();
|
|
75
75
|
|
|
76
|
-
if (
|
|
76
|
+
if ($isListNode(firstChild)) {
|
|
77
77
|
listItemNodes = listItemNodes.concat($getAllListItems(firstChild));
|
|
78
78
|
} else {
|
|
79
79
|
listItemNodes.push(listItemNode);
|
|
@@ -83,13 +83,13 @@ function $getAllListItems(node) {
|
|
|
83
83
|
return listItemNodes;
|
|
84
84
|
}
|
|
85
85
|
function isNestedListNode(node) {
|
|
86
|
-
return
|
|
86
|
+
return $isListItemNode(node) && $isListNode(node.getFirstChild());
|
|
87
87
|
}
|
|
88
88
|
function findNearestListItemNode(node) {
|
|
89
89
|
let currentNode = node;
|
|
90
90
|
|
|
91
91
|
while (currentNode !== null) {
|
|
92
|
-
if (
|
|
92
|
+
if ($isListItemNode(currentNode)) {
|
|
93
93
|
return currentNode;
|
|
94
94
|
}
|
|
95
95
|
|
|
@@ -104,7 +104,7 @@ function getUniqueListItemNodes(nodeList) {
|
|
|
104
104
|
for (let i = 0; i < nodeList.length; i++) {
|
|
105
105
|
const node = nodeList[i];
|
|
106
106
|
|
|
107
|
-
if (
|
|
107
|
+
if ($isListItemNode(node)) {
|
|
108
108
|
keys.add(node);
|
|
109
109
|
}
|
|
110
110
|
}
|
|
@@ -123,7 +123,7 @@ function $removeHighestEmptyListParent(sublist) {
|
|
|
123
123
|
while (emptyListPtr.getNextSibling() == null && emptyListPtr.getPreviousSibling() == null) {
|
|
124
124
|
const parent = emptyListPtr.getParent();
|
|
125
125
|
|
|
126
|
-
if (parent == null || !(
|
|
126
|
+
if (parent == null || !($isListItemNode(emptyListPtr) || $isListNode(emptyListPtr))) {
|
|
127
127
|
break;
|
|
128
128
|
}
|
|
129
129
|
|
|
@@ -152,16 +152,16 @@ function insertList(editor, listType) {
|
|
|
152
152
|
const anchorNodeParent = anchorNode.getParent(); // This is a special case for when there's nothing selected
|
|
153
153
|
|
|
154
154
|
if (nodes.length === 0) {
|
|
155
|
-
const list
|
|
155
|
+
const list = $createListNode(listType);
|
|
156
156
|
|
|
157
157
|
if (lexical.$isRootNode(anchorNodeParent)) {
|
|
158
|
-
anchorNode.replace(list
|
|
159
|
-
const listItem =
|
|
160
|
-
list
|
|
161
|
-
} else if (
|
|
158
|
+
anchorNode.replace(list);
|
|
159
|
+
const listItem = $createListItemNode();
|
|
160
|
+
list.append(listItem);
|
|
161
|
+
} else if ($isListItemNode(anchorNode)) {
|
|
162
162
|
const parent = anchorNode.getParentOrThrow();
|
|
163
|
-
list
|
|
164
|
-
parent.replace(list
|
|
163
|
+
list.append(...parent.getChildren());
|
|
164
|
+
parent.replace(list);
|
|
165
165
|
}
|
|
166
166
|
|
|
167
167
|
return;
|
|
@@ -182,9 +182,9 @@ function insertList(editor, listType) {
|
|
|
182
182
|
while (parent != null) {
|
|
183
183
|
const parentKey = parent.getKey();
|
|
184
184
|
|
|
185
|
-
if (
|
|
185
|
+
if ($isListNode(parent)) {
|
|
186
186
|
if (!handled.has(parentKey)) {
|
|
187
|
-
const newListNode =
|
|
187
|
+
const newListNode = $createListNode(listType);
|
|
188
188
|
newListNode.append(...parent.getChildren());
|
|
189
189
|
parent.replace(newListNode);
|
|
190
190
|
handled.add(parentKey);
|
|
@@ -211,34 +211,34 @@ function insertList(editor, listType) {
|
|
|
211
211
|
}
|
|
212
212
|
|
|
213
213
|
function createListOrMerge(node, listType) {
|
|
214
|
-
if (
|
|
214
|
+
if ($isListNode(node)) {
|
|
215
215
|
return node;
|
|
216
216
|
}
|
|
217
217
|
|
|
218
218
|
const previousSibling = node.getPreviousSibling();
|
|
219
219
|
const nextSibling = node.getNextSibling();
|
|
220
|
-
const listItem =
|
|
220
|
+
const listItem = $createListItemNode();
|
|
221
221
|
|
|
222
|
-
if (
|
|
222
|
+
if ($isListNode(previousSibling) && listType === previousSibling.getTag()) {
|
|
223
223
|
listItem.append(node);
|
|
224
224
|
previousSibling.append(listItem); // if the same type of list is on both sides, merge them.
|
|
225
225
|
|
|
226
|
-
if (
|
|
226
|
+
if ($isListNode(nextSibling) && listType === nextSibling.getTag()) {
|
|
227
227
|
previousSibling.append(...nextSibling.getChildren());
|
|
228
228
|
nextSibling.remove();
|
|
229
229
|
}
|
|
230
230
|
|
|
231
231
|
return previousSibling;
|
|
232
|
-
} else if (
|
|
232
|
+
} else if ($isListNode(nextSibling) && listType === nextSibling.getTag()) {
|
|
233
233
|
listItem.append(node);
|
|
234
234
|
nextSibling.getFirstChildOrThrow().insertBefore(listItem);
|
|
235
235
|
return nextSibling;
|
|
236
236
|
} else {
|
|
237
|
-
const list
|
|
238
|
-
list
|
|
239
|
-
node.replace(list
|
|
237
|
+
const list = $createListNode(listType);
|
|
238
|
+
list.append(listItem);
|
|
239
|
+
node.replace(list);
|
|
240
240
|
listItem.append(node);
|
|
241
|
-
return list
|
|
241
|
+
return list;
|
|
242
242
|
}
|
|
243
243
|
}
|
|
244
244
|
|
|
@@ -251,14 +251,14 @@ function removeList(editor) {
|
|
|
251
251
|
const nodes = selection.getNodes();
|
|
252
252
|
const anchorNode = selection.anchor.getNode();
|
|
253
253
|
|
|
254
|
-
if (nodes.length === 0 &&
|
|
254
|
+
if (nodes.length === 0 && $isListItemNode(anchorNode)) {
|
|
255
255
|
listNodes.add($getTopListNode(anchorNode));
|
|
256
256
|
} else {
|
|
257
257
|
for (let i = 0; i < nodes.length; i++) {
|
|
258
258
|
const node = nodes[i];
|
|
259
259
|
|
|
260
260
|
if (lexical.$isLeafNode(node)) {
|
|
261
|
-
const listItemNode = utils.$getNearestNodeOfType(node,
|
|
261
|
+
const listItemNode = utils.$getNearestNodeOfType(node, ListItemNode);
|
|
262
262
|
|
|
263
263
|
if (listItemNode != null) {
|
|
264
264
|
listNodes.add($getTopListNode(listItemNode));
|
|
@@ -298,11 +298,11 @@ function $handleIndent(listItemNodes) {
|
|
|
298
298
|
if (isNestedListNode(nextSibling) && isNestedListNode(previousSibling)) {
|
|
299
299
|
const innerList = previousSibling.getFirstChild();
|
|
300
300
|
|
|
301
|
-
if (
|
|
301
|
+
if ($isListNode(innerList)) {
|
|
302
302
|
innerList.append(listItemNode);
|
|
303
303
|
const nextInnerList = nextSibling.getFirstChild();
|
|
304
304
|
|
|
305
|
-
if (
|
|
305
|
+
if ($isListNode(nextInnerList)) {
|
|
306
306
|
const children = nextInnerList.getChildren();
|
|
307
307
|
innerList.append(...children);
|
|
308
308
|
nextInnerList.remove();
|
|
@@ -314,7 +314,7 @@ function $handleIndent(listItemNodes) {
|
|
|
314
314
|
// if the ListItemNode is next to a nested ListNode, merge them
|
|
315
315
|
const innerList = nextSibling.getFirstChild();
|
|
316
316
|
|
|
317
|
-
if (
|
|
317
|
+
if ($isListNode(innerList)) {
|
|
318
318
|
const firstChild = innerList.getFirstChild();
|
|
319
319
|
|
|
320
320
|
if (firstChild !== null) {
|
|
@@ -326,15 +326,15 @@ function $handleIndent(listItemNodes) {
|
|
|
326
326
|
} else if (isNestedListNode(previousSibling)) {
|
|
327
327
|
const innerList = previousSibling.getFirstChild();
|
|
328
328
|
|
|
329
|
-
if (
|
|
329
|
+
if ($isListNode(innerList)) {
|
|
330
330
|
innerList.append(listItemNode);
|
|
331
331
|
innerList.getChildren().forEach(child => child.markDirty());
|
|
332
332
|
}
|
|
333
333
|
} else {
|
|
334
334
|
// otherwise, we need to create a new nested ListNode
|
|
335
|
-
if (
|
|
336
|
-
const newListItem =
|
|
337
|
-
const newList =
|
|
335
|
+
if ($isListNode(parent)) {
|
|
336
|
+
const newListItem = $createListItemNode();
|
|
337
|
+
const newList = $createListNode(parent.getTag());
|
|
338
338
|
newListItem.append(newList);
|
|
339
339
|
newList.append(listItemNode);
|
|
340
340
|
|
|
@@ -348,7 +348,7 @@ function $handleIndent(listItemNodes) {
|
|
|
348
348
|
}
|
|
349
349
|
}
|
|
350
350
|
|
|
351
|
-
if (
|
|
351
|
+
if ($isListNode(parent)) {
|
|
352
352
|
parent.getChildren().forEach(child => child.markDirty());
|
|
353
353
|
}
|
|
354
354
|
});
|
|
@@ -364,7 +364,7 @@ function $handleOutdent(listItemNodes) {
|
|
|
364
364
|
const grandparentListItem = parentList ? parentList.getParent() : undefined;
|
|
365
365
|
const greatGrandparentList = grandparentListItem ? grandparentListItem.getParent() : undefined; // If it doesn't have these ancestors, it's not indented.
|
|
366
366
|
|
|
367
|
-
if (
|
|
367
|
+
if ($isListNode(greatGrandparentList) && $isListItemNode(grandparentListItem) && $isListNode(parentList)) {
|
|
368
368
|
// if it's the first child in it's parent list, insert it into the
|
|
369
369
|
// great grandparent list before the grandparent
|
|
370
370
|
const firstChild = parentList ? parentList.getFirstChild() : undefined;
|
|
@@ -387,12 +387,12 @@ function $handleOutdent(listItemNodes) {
|
|
|
387
387
|
} else {
|
|
388
388
|
// otherwise, we need to split the siblings into two new nested lists
|
|
389
389
|
const tag = parentList.getTag();
|
|
390
|
-
const previousSiblingsListItem =
|
|
391
|
-
const previousSiblingsList =
|
|
390
|
+
const previousSiblingsListItem = $createListItemNode();
|
|
391
|
+
const previousSiblingsList = $createListNode(tag);
|
|
392
392
|
previousSiblingsListItem.append(previousSiblingsList);
|
|
393
393
|
listItemNode.getPreviousSiblings().forEach(sibling => previousSiblingsList.append(sibling));
|
|
394
|
-
const nextSiblingsListItem =
|
|
395
|
-
const nextSiblingsList =
|
|
394
|
+
const nextSiblingsListItem = $createListItemNode();
|
|
395
|
+
const nextSiblingsList = $createListNode(tag);
|
|
396
396
|
nextSiblingsListItem.append(nextSiblingsList);
|
|
397
397
|
nextSiblingsList.append(...listItemNode.getNextSiblings()); // put the sibling nested lists on either side of the grandparent list item in the great grandparent.
|
|
398
398
|
|
|
@@ -463,14 +463,14 @@ function $handleListInsertParagraph() {
|
|
|
463
463
|
|
|
464
464
|
const anchor = selection.anchor.getNode();
|
|
465
465
|
|
|
466
|
-
if (
|
|
466
|
+
if (!$isListItemNode(anchor) || anchor.getTextContent() !== '') {
|
|
467
467
|
return false;
|
|
468
468
|
}
|
|
469
469
|
|
|
470
470
|
const topListNode = $getTopListNode(anchor);
|
|
471
471
|
const parent = anchor.getParent();
|
|
472
472
|
|
|
473
|
-
if (
|
|
473
|
+
if (!$isListNode(parent)) {
|
|
474
474
|
throw Error(`A ListItemNode must have a ListNode for a parent.`);
|
|
475
475
|
}
|
|
476
476
|
|
|
@@ -480,8 +480,8 @@ function $handleListInsertParagraph() {
|
|
|
480
480
|
if (lexical.$isRootNode(grandparent)) {
|
|
481
481
|
replacementNode = lexical.$createParagraphNode();
|
|
482
482
|
topListNode.insertAfter(replacementNode);
|
|
483
|
-
} else if (
|
|
484
|
-
replacementNode =
|
|
483
|
+
} else if ($isListItemNode(grandparent)) {
|
|
484
|
+
replacementNode = $createListItemNode();
|
|
485
485
|
grandparent.insertAfter(replacementNode);
|
|
486
486
|
} else {
|
|
487
487
|
return false;
|
|
@@ -491,12 +491,12 @@ function $handleListInsertParagraph() {
|
|
|
491
491
|
const nextSiblings = anchor.getNextSiblings();
|
|
492
492
|
|
|
493
493
|
if (nextSiblings.length > 0) {
|
|
494
|
-
const newList =
|
|
494
|
+
const newList = $createListNode(parent.getTag());
|
|
495
495
|
|
|
496
496
|
if (lexical.$isParagraphNode(replacementNode)) {
|
|
497
497
|
replacementNode.insertAfter(newList);
|
|
498
498
|
} else {
|
|
499
|
-
const newListItem =
|
|
499
|
+
const newListItem = $createListItemNode();
|
|
500
500
|
newListItem.append(newList);
|
|
501
501
|
replacementNode.insertAfter(newListItem);
|
|
502
502
|
}
|
|
@@ -579,35 +579,35 @@ class ListItemNode extends lexical.ElementNode {
|
|
|
579
579
|
return super.replace(replaceWithNode);
|
|
580
580
|
}
|
|
581
581
|
|
|
582
|
-
const list
|
|
582
|
+
const list = this.getParentOrThrow();
|
|
583
583
|
|
|
584
|
-
if (
|
|
585
|
-
const childrenKeys = list
|
|
584
|
+
if ($isListNode(list)) {
|
|
585
|
+
const childrenKeys = list.__children;
|
|
586
586
|
const childrenLength = childrenKeys.length;
|
|
587
587
|
const index = childrenKeys.indexOf(this.__key);
|
|
588
588
|
|
|
589
589
|
if (index === 0) {
|
|
590
|
-
list
|
|
590
|
+
list.insertBefore(replaceWithNode);
|
|
591
591
|
} else if (index === childrenLength - 1) {
|
|
592
|
-
list
|
|
592
|
+
list.insertAfter(replaceWithNode);
|
|
593
593
|
} else {
|
|
594
594
|
// Split the list
|
|
595
|
-
const newList =
|
|
596
|
-
const children = list
|
|
595
|
+
const newList = $createListNode(list.getTag());
|
|
596
|
+
const children = list.getChildren();
|
|
597
597
|
|
|
598
598
|
for (let i = index + 1; i < childrenLength; i++) {
|
|
599
599
|
const child = children[i];
|
|
600
600
|
newList.append(child);
|
|
601
601
|
}
|
|
602
602
|
|
|
603
|
-
list
|
|
603
|
+
list.insertAfter(replaceWithNode);
|
|
604
604
|
replaceWithNode.insertAfter(newList);
|
|
605
605
|
}
|
|
606
606
|
|
|
607
607
|
this.remove();
|
|
608
608
|
|
|
609
609
|
if (childrenLength === 1) {
|
|
610
|
-
list
|
|
610
|
+
list.remove();
|
|
611
611
|
}
|
|
612
612
|
}
|
|
613
613
|
|
|
@@ -625,14 +625,14 @@ class ListItemNode extends lexical.ElementNode {
|
|
|
625
625
|
|
|
626
626
|
const listNode = this.getParentOrThrow();
|
|
627
627
|
|
|
628
|
-
if (
|
|
628
|
+
if (!$isListNode(listNode)) {
|
|
629
629
|
{
|
|
630
630
|
throw Error(`insertAfter: list node is not parent of list item node`);
|
|
631
631
|
}
|
|
632
632
|
} // Attempt to merge tables if the list is of the same type.
|
|
633
633
|
|
|
634
634
|
|
|
635
|
-
if (
|
|
635
|
+
if ($isListNode(node) && node.getTag() === listNode.getTag()) {
|
|
636
636
|
let child = node;
|
|
637
637
|
const children = node.getChildren();
|
|
638
638
|
|
|
@@ -649,7 +649,7 @@ class ListItemNode extends lexical.ElementNode {
|
|
|
649
649
|
listNode.insertAfter(node);
|
|
650
650
|
|
|
651
651
|
if (siblings.length !== 0) {
|
|
652
|
-
const newListNode =
|
|
652
|
+
const newListNode = $createListNode(listNode.getTag());
|
|
653
653
|
siblings.forEach(sibling => newListNode.append(sibling));
|
|
654
654
|
node.insertAfter(newListNode);
|
|
655
655
|
}
|
|
@@ -756,16 +756,16 @@ class ListItemNode extends lexical.ElementNode {
|
|
|
756
756
|
}
|
|
757
757
|
|
|
758
758
|
function getListItemValue(listItem) {
|
|
759
|
-
const list
|
|
759
|
+
const list = listItem.getParent();
|
|
760
760
|
let value = 1;
|
|
761
761
|
|
|
762
|
-
if (list
|
|
763
|
-
if (
|
|
762
|
+
if (list != null) {
|
|
763
|
+
if (!$isListNode(list)) {
|
|
764
764
|
{
|
|
765
765
|
throw Error(`getListItemValue: list node is not parent of list item node`);
|
|
766
766
|
}
|
|
767
767
|
} else {
|
|
768
|
-
value = list
|
|
768
|
+
value = list.getStart();
|
|
769
769
|
}
|
|
770
770
|
}
|
|
771
771
|
|
|
@@ -774,7 +774,7 @@ function getListItemValue(listItem) {
|
|
|
774
774
|
for (let i = 0; i < siblings.length; i++) {
|
|
775
775
|
const sibling = siblings[i];
|
|
776
776
|
|
|
777
|
-
if ($isListItemNode(sibling) &&
|
|
777
|
+
if ($isListItemNode(sibling) && !$isListNode(sibling.getFirstChild())) {
|
|
778
778
|
value++;
|
|
779
779
|
}
|
|
780
780
|
}
|
|
@@ -801,7 +801,7 @@ function $setListItemThemeClassNames(dom, editorThemeClasses, node) {
|
|
|
801
801
|
if (nestedListItemClassName !== undefined) {
|
|
802
802
|
const nestedListItemClasses = nestedListItemClassName.split(' ');
|
|
803
803
|
|
|
804
|
-
if (node.getChildren().some(child =>
|
|
804
|
+
if (node.getChildren().some(child => $isListNode(child))) {
|
|
805
805
|
classesToAdd.push(...nestedListItemClasses);
|
|
806
806
|
} else {
|
|
807
807
|
classesToRemove.push(...nestedListItemClasses);
|
|
@@ -855,6 +855,10 @@ class ListNode extends lexical.ElementNode {
|
|
|
855
855
|
|
|
856
856
|
getTag() {
|
|
857
857
|
return this.__tag;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
getStart() {
|
|
861
|
+
return this.__start;
|
|
858
862
|
} // View
|
|
859
863
|
|
|
860
864
|
|
|
@@ -994,14 +998,29 @@ function $isListNode(node) {
|
|
|
994
998
|
return node instanceof ListNode;
|
|
995
999
|
}
|
|
996
1000
|
|
|
1001
|
+
/**
|
|
1002
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1003
|
+
*
|
|
1004
|
+
* This source code is licensed under the MIT license found in the
|
|
1005
|
+
* LICENSE file in the root directory of this source tree.
|
|
1006
|
+
*
|
|
1007
|
+
*
|
|
1008
|
+
*/
|
|
1009
|
+
const INSERT_UNORDERED_LIST_COMMAND = lexical.createCommand();
|
|
1010
|
+
const INSERT_ORDERED_LIST_COMMAND = lexical.createCommand();
|
|
1011
|
+
const REMOVE_LIST_COMMAND = lexical.createCommand();
|
|
1012
|
+
|
|
997
1013
|
exports.$createListItemNode = $createListItemNode;
|
|
998
1014
|
exports.$createListNode = $createListNode;
|
|
999
1015
|
exports.$getListDepth = $getListDepth;
|
|
1000
1016
|
exports.$handleListInsertParagraph = $handleListInsertParagraph;
|
|
1001
1017
|
exports.$isListItemNode = $isListItemNode;
|
|
1002
1018
|
exports.$isListNode = $isListNode;
|
|
1019
|
+
exports.INSERT_ORDERED_LIST_COMMAND = INSERT_ORDERED_LIST_COMMAND;
|
|
1020
|
+
exports.INSERT_UNORDERED_LIST_COMMAND = INSERT_UNORDERED_LIST_COMMAND;
|
|
1003
1021
|
exports.ListItemNode = ListItemNode;
|
|
1004
1022
|
exports.ListNode = ListNode;
|
|
1023
|
+
exports.REMOVE_LIST_COMMAND = REMOVE_LIST_COMMAND;
|
|
1005
1024
|
exports.indentList = indentList;
|
|
1006
1025
|
exports.insertList = insertList;
|
|
1007
1026
|
exports.outdentList = outdentList;
|
|
@@ -0,0 +1,64 @@
|
|
|
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
|
+
* @flow strict
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type {
|
|
11
|
+
LexicalNode,
|
|
12
|
+
LexicalEditor,
|
|
13
|
+
ParagraphNode,
|
|
14
|
+
RangeSelection,
|
|
15
|
+
LexicalCommand,
|
|
16
|
+
} from 'lexical';
|
|
17
|
+
import {ElementNode} from 'lexical';
|
|
18
|
+
|
|
19
|
+
type ListNodeTagType = 'ul' | 'ol';
|
|
20
|
+
declare export function $createListItemNode(): ListItemNode;
|
|
21
|
+
declare export function $createListNode(
|
|
22
|
+
tag: ListNodeTagType,
|
|
23
|
+
start?: number,
|
|
24
|
+
): ListNode;
|
|
25
|
+
declare export function $getListDepth(listNode: ListNode): number;
|
|
26
|
+
declare export function $handleListInsertParagraph(): boolean;
|
|
27
|
+
declare export function $isListItemNode(
|
|
28
|
+
node: ?LexicalNode,
|
|
29
|
+
): boolean %checks(node instanceof ListItemNode);
|
|
30
|
+
declare export function $isListNode(
|
|
31
|
+
node: ?LexicalNode,
|
|
32
|
+
): boolean %checks(node instanceof ListNode);
|
|
33
|
+
declare export function indentList(): boolean;
|
|
34
|
+
declare export function insertList(
|
|
35
|
+
editor: LexicalEditor,
|
|
36
|
+
listType: 'ul' | 'ol',
|
|
37
|
+
): void;
|
|
38
|
+
declare export class ListItemNode extends ElementNode {
|
|
39
|
+
append(...nodes: LexicalNode[]): ListItemNode;
|
|
40
|
+
replace<N: LexicalNode>(replaceWithNode: N): N;
|
|
41
|
+
insertAfter(node: LexicalNode): LexicalNode;
|
|
42
|
+
insertNewAfter(): ListItemNode | ParagraphNode;
|
|
43
|
+
collapseAtStart(selection: RangeSelection): true;
|
|
44
|
+
getIndent(): number;
|
|
45
|
+
setIndent(indent: number): this;
|
|
46
|
+
insertBefore(nodeToInsert: LexicalNode): LexicalNode;
|
|
47
|
+
canInsertAfter(node: LexicalNode): boolean;
|
|
48
|
+
canReplaceWith(replacement: LexicalNode): boolean;
|
|
49
|
+
canMergeWith(node: LexicalNode): boolean;
|
|
50
|
+
}
|
|
51
|
+
declare export class ListNode extends ElementNode {
|
|
52
|
+
__tag: ListNodeTagType;
|
|
53
|
+
__start: number;
|
|
54
|
+
canBeEmpty(): false;
|
|
55
|
+
append(...nodesToAppend: LexicalNode[]): ListNode;
|
|
56
|
+
getTag(): ListNodeTagType;
|
|
57
|
+
getStart(): number;
|
|
58
|
+
}
|
|
59
|
+
declare export function outdentList(): boolean;
|
|
60
|
+
declare export function removeList(editor: LexicalEditor): boolean;
|
|
61
|
+
|
|
62
|
+
declare export var INSERT_UNORDERED_LIST_COMMAND: LexicalCommand<void>;
|
|
63
|
+
declare export var INSERT_ORDERED_LIST_COMMAND: LexicalCommand<void>;
|
|
64
|
+
declare export var REMOVE_LIST_COMMAND: LexicalCommand<void>;
|
package/LexicalList.prod.js
CHANGED
|
@@ -4,27 +4,25 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
var
|
|
8
|
-
function
|
|
9
|
-
function
|
|
10
|
-
function
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
function
|
|
25
|
-
|
|
26
|
-
exports
|
|
27
|
-
|
|
28
|
-
exports.
|
|
29
|
-
f;){const l=f.getKey();if(g.$isListNode(f)){if(!c.has(l)){var h=g.$createListNode(a);h.append(...f.getChildren());f.replace(h);c.add(l)}break}else{h=f.getParent();if(n.$isRootNode(h)&&!c.has(l)){c.add(l);x(f,a);break}f=h}}}}})};exports.outdentList=function(){return B("outdent")};
|
|
30
|
-
exports.removeList=function(b){b.update(()=>{var a=n.$getSelection();if(n.$isRangeSelection(a)){const d=new Set,e=a.getNodes();a=a.anchor.getNode();if(0===e.length&&g.$isListItemNode(a))d.add(r(a));else for(a=0;a<e.length;a++){var c=e[a];n.$isLeafNode(c)&&(c=k.$getNearestNodeOfType(c,g.ListItemNode),null!=c&&d.add(r(c)))}d.forEach(f=>{let h=f;u(f).forEach(l=>{if(null!=l){const m=n.$createParagraphNode();m.append(...l.getChildren());h.insertAfter(m);h=m;l.remove()}});f.remove()})}})};
|
|
7
|
+
var h=require("lexical"),k=require("@lexical/utils"),l=require("@lexical/list");function p(b){throw Error(`Minified Lexical error #${b}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}function q(b){let a=1;for(b=b.getParent();null!=b;){if(r(b)){b=b.getParent();if(t(b)){a++;b=b.getParent();continue}p(2)}break}return a}
|
|
8
|
+
function u(b){b=b.getParent();t(b)||p(2);let a=b;for(;null!==a;)a=a.getParent(),t(a)&&(b=a);return b}function v(b){let a=[];b=b.getChildren().filter(r);for(let c=0;c<b.length;c++){const d=b[c],e=d.getFirstChild();t(e)?a=a.concat(v(e)):a.push(d)}return a}function x(b){return r(b)&&t(b.getFirstChild())}function y(b){for(;null==b.getNextSibling()&&null==b.getPreviousSibling();){const a=b.getParent();if(null==a||!r(b)&&!t(b))break;b=a}b.remove()}
|
|
9
|
+
function A(b,a){if(t(b))return b;const c=b.getPreviousSibling(),d=b.getNextSibling(),e=B();if(t(c)&&a===c.getTag())return e.append(b),c.append(e),t(d)&&a===d.getTag()&&(c.append(...d.getChildren()),d.remove()),c;if(t(d)&&a===d.getTag())return e.append(b),d.getFirstChildOrThrow().insertBefore(e),d;a=C(a);a.append(e);b.replace(a);e.append(b);return a}
|
|
10
|
+
function D(b){b.forEach(a=>{if(!x(a)){var c=a.getParent(),d=a.getNextSibling(),e=a.getPreviousSibling();if(x(d)&&x(e))e=e.getFirstChild(),t(e)&&(e.append(a),a=d.getFirstChild(),t(a)&&(d=a.getChildren(),e.append(...d),a.remove()),e.getChildren().forEach(f=>f.markDirty()));else if(x(d))d=d.getFirstChild(),t(d)&&(e=d.getFirstChild(),null!==e&&e.insertBefore(a),d.getChildren().forEach(f=>f.markDirty()));else if(x(e))d=e.getFirstChild(),t(d)&&(d.append(a),d.getChildren().forEach(f=>f.markDirty()));else if(t(c)){const f=
|
|
11
|
+
B(),g=C(c.getTag());f.append(g);g.append(a);e?e.insertAfter(f):d?d.insertBefore(f):c.append(f)}t(c)&&c.getChildren().forEach(f=>f.markDirty())}})}
|
|
12
|
+
function E(b){b.forEach(a=>{if(!x(a)){var c=a.getParent(),d=c?c.getParent():void 0,e=d?d.getParent():void 0;if(t(e)&&r(d)&&t(c)){var f=c?c.getFirstChild():void 0,g=c?c.getLastChild():void 0;if(a.is(f))d.insertBefore(a),c.isEmpty()&&d.remove();else if(a.is(g))d.insertAfter(a),c.isEmpty()&&d.remove();else{var m=c.getTag();f=B();const n=C(m);f.append(n);a.getPreviousSiblings().forEach(w=>n.append(w));g=B();m=C(m);g.append(m);m.append(...a.getNextSiblings());d.insertBefore(f);d.insertAfter(g);d.replace(a)}c.getChildren().forEach(n=>
|
|
13
|
+
n.markDirty());e.getChildren().forEach(n=>n.markDirty())}}})}function F(b){var a=h.$getSelection();if(!h.$isRangeSelection(a))return!1;var c=a.getNodes(),d=[];0===c.length&&c.push(a.anchor.getNode());if(1===c.length){a:{for(c=c[0];null!==c;){if(r(c))break a;c=c.getParent()}c=null}null!==c&&(d=[c])}else{d=new Set;for(a=0;a<c.length;a++){const e=c[a];r(e)&&d.add(e)}d=Array.from(d)}return 0<d.length?("indent"===b?D(d):E(d),!0):!1}
|
|
14
|
+
class G extends h.ElementNode{static getType(){return"listitem"}static clone(b){return new G(b.__key)}constructor(b){super(b)}createDOM(b){const a=document.createElement("li");a.value=H(this);I(a,b.theme,this);return a}updateDOM(b,a,c){a.value=H(this);I(a,c.theme,this);return!1}static convertDOM(){return{li:()=>({conversion:J,priority:0})}}append(...b){for(let a=0;a<b.length;a++){const c=b[a];if(h.$isElementNode(c)&&this.canMergeWith(c)){const d=c.getChildren();this.append(...d);c.remove()}else super.append(c)}return this}replace(b){if(r(b))return super.replace(b);
|
|
15
|
+
const a=this.getParentOrThrow();if(t(a)){var c=a.__children;const e=c.length;var d=c.indexOf(this.__key);if(0===d)a.insertBefore(b);else if(d===e-1)a.insertAfter(b);else{c=C(a.getTag());const f=a.getChildren();for(d+=1;d<e;d++)c.append(f[d]);a.insertAfter(b);b.insertAfter(c)}this.remove();1===e&&a.remove()}return b}insertAfter(b){var a=this.getNextSiblings();if(r(b))return a.forEach(d=>d.markDirty()),super.insertAfter(b);var c=this.getParentOrThrow();t(c)||p(1);if(t(b)&&b.getTag()===c.getTag()){a=
|
|
16
|
+
b;b=b.getChildren();for(c=b.length-1;0<=c;c--)a=b[c],this.insertAfter(a);return a}c.insertAfter(b);if(0!==a.length){const d=C(c.getTag());a.forEach(e=>d.append(e));b.insertAfter(d)}return b}insertNewAfter(){const b=B();this.insertAfter(b);return b}collapseAtStart(b){const a=h.$createParagraphNode();this.getChildren().forEach(f=>a.append(f));var c=this.getParentOrThrow(),d=c.getParentOrThrow();const e=r(d);1===c.getChildrenSize()?e?(c.remove(),d.select()):(c.replace(a),c=b.anchor,b=b.focus,d=a.getKey(),
|
|
17
|
+
"element"===c.type&&c.getNode().is(this)&&c.set(d,c.offset,"element"),"element"===b.type&&b.getNode().is(this)&&b.set(d,b.offset,"element")):(c.insertBefore(a),this.remove());return!0}getIndent(){let b=this.getParentOrThrow().getParentOrThrow(),a=0;for(;r(b);)b=b.getParentOrThrow().getParentOrThrow(),a++;return a}setIndent(b){let a=this.getIndent();for(;a!==b;)a<b?(D([this]),a++):(E([this]),a--);return this}insertBefore(b){const a=this.getNextSiblings();r(b)&&a.forEach(c=>c.markDirty());return super.insertBefore(b)}canInsertAfter(b){return r(b)}canReplaceWith(b){return r(b)}canMergeWith(b){return h.$isParagraphNode(b)||
|
|
18
|
+
r(b)}}function H(b){var a=b.getParent();let c=1;null!=a&&(t(a)?c=a.getStart():p(47));b=b.getPreviousSiblings();for(a=0;a<b.length;a++){const d=b[a];r(d)&&!t(d.getFirstChild())&&c++}return c}
|
|
19
|
+
function I(b,a,c){const d=[],e=[],f=(a=a.list)?a.listitem:void 0;if(a&&a.nested)var g=a.nested.listitem;void 0!==f&&(a=f.split(" "),d.push(...a));void 0!==g&&(g=g.split(" "),c.getChildren().some(m=>t(m))?d.push(...g):e.push(...g));0<d.length&&k.addClassNamesToElement(b,...d);0<e.length&&k.removeClassNamesFromElement(b,...e)}function J(){return{node:B()}}function B(){return new G}function r(b){return b instanceof G}
|
|
20
|
+
class K extends h.ElementNode{static getType(){return"list"}static clone(b){return new K(b.__tag,b.__start,b.__key)}constructor(b,a,c){super(c);this.__tag=b;this.__start=a}getTag(){return this.__tag}getStart(){return this.__start}createDOM(b){const a=document.createElement(this.__tag);1!==this.__start&&a.setAttribute("start",String(this.__start));L(a,b.theme,this);return a}updateDOM(b,a,c){if(b.__tag!==this.__tag)return!0;L(a,c.theme,this);return!1}static convertDOM(){return{ol:()=>({conversion:M,
|
|
21
|
+
priority:0}),ul:()=>({conversion:M,priority:0})}}canBeEmpty(){return!1}append(...b){for(let c=0;c<b.length;c++){var a=b[c];if(l.$isListItemNode(a))super.append(a);else{const d=l.$createListItemNode();t(a)?d.append(a):(a=h.$createTextNode(a.getTextContent()),d.append(a));super.append(d)}}return this}}
|
|
22
|
+
function L(b,a,c){const d=[],e=[];var f=a.list;if(void 0!==f){const m=f[c.__tag+"Depth"]||[];a=q(c)-1;const n=a%m.length;var g=m[n];const w=f[c.__tag];let z;f=f.nested;void 0!==f&&f.list&&(z=f.list);void 0!==w&&d.push(w);if(void 0!==g)for(g=g.split(" "),d.push(...g),g=0;g<m.length;g++)g!==n&&e.push(c.__tag+g);void 0!==z&&(c=z.split(" "),1<a?d.push(...c):e.push(...c))}0<d.length&&k.addClassNamesToElement(b,...d);0<e.length&&k.removeClassNamesFromElement(b,...e)}
|
|
23
|
+
function M(b){b=b.nodeName.toLowerCase();let a=null;if("ol"===b||"ul"===b)a=C(b);return{node:a}}function C(b,a=1){return new K(b,a)}function t(b){return b instanceof K}const N=h.createCommand(),O=h.createCommand(),P=h.createCommand();exports.$createListItemNode=B;exports.$createListNode=C;exports.$getListDepth=q;
|
|
24
|
+
exports.$handleListInsertParagraph=function(){var b=h.$getSelection();if(!h.$isRangeSelection(b)||!b.isCollapsed())return!1;b=b.anchor.getNode();if(!r(b)||""!==b.getTextContent())return!1;var a=u(b),c=b.getParent();t(c)||p(2);const d=c.getParent();let e;if(h.$isRootNode(d))e=h.$createParagraphNode(),a.insertAfter(e);else if(r(d))e=B(),d.insertAfter(e);else return!1;e.select();a=b.getNextSiblings();if(0<a.length){const f=C(c.getTag());h.$isParagraphNode(e)?e.insertAfter(f):(c=B(),c.append(f),e.insertAfter(c));
|
|
25
|
+
a.forEach(g=>{g.remove();f.append(g)})}y(b);return!0};exports.$isListItemNode=r;exports.$isListNode=t;exports.INSERT_ORDERED_LIST_COMMAND=O;exports.INSERT_UNORDERED_LIST_COMMAND=N;exports.ListItemNode=G;exports.ListNode=K;exports.REMOVE_LIST_COMMAND=P;exports.indentList=function(){return F("indent")};
|
|
26
|
+
exports.insertList=function(b,a){b.update(()=>{var c=h.$getSelection();if(h.$isRangeSelection(c)){var d=c.getNodes();c=c.anchor.getNode();var e=c.getParent();if(0===d.length)d=C(a),h.$isRootNode(e)?(c.replace(d),c=B(),d.append(c)):r(c)&&(c=c.getParentOrThrow(),d.append(...c.getChildren()),c.replace(d));else for(c=new Set,e=0;e<d.length;e++){var f=d[e];if(h.$isElementNode(f)&&f.isEmpty()&&!c.has(f.getKey()))A(f,a);else if(h.$isLeafNode(f))for(f=f.getParent();null!=f;){const m=f.getKey();if(t(f)){if(!c.has(m)){var g=
|
|
27
|
+
C(a);g.append(...f.getChildren());f.replace(g);c.add(m)}break}else{g=f.getParent();if(h.$isRootNode(g)&&!c.has(m)){c.add(m);A(f,a);break}f=g}}}}})};exports.outdentList=function(){return F("outdent")};
|
|
28
|
+
exports.removeList=function(b){b.update(()=>{var a=h.$getSelection();if(h.$isRangeSelection(a)){const d=new Set,e=a.getNodes();a=a.anchor.getNode();if(0===e.length&&r(a))d.add(u(a));else for(a=0;a<e.length;a++){var c=e[a];h.$isLeafNode(c)&&(c=k.$getNearestNodeOfType(c,G),null!=c&&d.add(u(c)))}d.forEach(f=>{let g=f;v(f).forEach(m=>{if(null!=m){const n=h.$createParagraphNode();n.append(...m.getChildren());g.insertAfter(n);g=n;m.remove()}});f.remove()})}})};
|
package/package.json
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lexical/list",
|
|
3
|
-
"author": {
|
|
4
|
-
"name": "Dominic Gannaway",
|
|
5
|
-
"email": "dg@domgan.com"
|
|
6
|
-
},
|
|
7
3
|
"description": "This package provides the list feature for Lexical.",
|
|
8
4
|
"keywords": [
|
|
9
5
|
"lexical",
|
|
@@ -12,13 +8,13 @@
|
|
|
12
8
|
"list"
|
|
13
9
|
],
|
|
14
10
|
"license": "MIT",
|
|
15
|
-
"version": "0.1.
|
|
11
|
+
"version": "0.1.17",
|
|
16
12
|
"main": "LexicalList.js",
|
|
17
13
|
"peerDependencies": {
|
|
18
|
-
"lexical": "0.1.
|
|
14
|
+
"lexical": "0.1.17"
|
|
19
15
|
},
|
|
20
16
|
"dependencies": {
|
|
21
|
-
"@lexical/utils": "0.1.
|
|
17
|
+
"@lexical/utils": "0.1.17"
|
|
22
18
|
},
|
|
23
19
|
"repository": {
|
|
24
20
|
"type": "git",
|