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