@react-fabric/lexical 1.0.0-beta.56 → 1.0.0-beta.58
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/css/_lexical.css +4 -0
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +33 -1
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +11 -11
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +525 -194
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +26 -23
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +3 -3
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +3 -3
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +95 -38
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +11 -10
- package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +387 -124
- package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +22 -18
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +54 -29
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +10 -10
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +107 -8
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +4 -0
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +23 -20
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +420 -137
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +50 -45
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +61 -65
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +2 -0
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +47 -46
- package/dist/cjs/packages/lexical/src/hooks/useFormatWatcher.js +3 -1
- package/dist/cjs/packages/lexical/src/plugins/ToolbarPlugin.js +20 -10
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +34 -2
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +524 -196
- package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +4 -4
- package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +96 -40
- package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +387 -127
- package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +5 -4
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +56 -31
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +4 -4
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +106 -10
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +3 -1
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +417 -139
- package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +62 -67
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +2 -1
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +3 -3
- package/dist/esm/packages/lexical/src/hooks/useFormatWatcher.js +3 -1
- package/dist/esm/packages/lexical/src/plugins/ToolbarPlugin.js +20 -10
- package/package.json +12 -12
- package/src/hooks/useFormatWatcher.ts +3 -1
- package/src/plugins/ToolbarPlugin.tsx +5 -10
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var LexicalUtils = require('../utils/LexicalUtils.js');
|
|
4
4
|
var lexical = require('lexical');
|
|
5
|
+
var LexicalSelection = require('../selection/LexicalSelection.js');
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
@@ -198,27 +199,25 @@ function $insertList(listType) {
|
|
|
198
199
|
$createListOrMerge(node, listType);
|
|
199
200
|
continue;
|
|
200
201
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
if (
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
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);
|
|
212
218
|
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
219
|
}
|
|
220
|
+
parent = nextParent;
|
|
222
221
|
}
|
|
223
222
|
}
|
|
224
223
|
}
|
|
@@ -418,7 +417,7 @@ function $handleIndent(listItemNode) {
|
|
|
418
417
|
// otherwise, we need to create a new nested ListNode
|
|
419
418
|
|
|
420
419
|
if ($isListNode(parent)) {
|
|
421
|
-
const newListItem = $createListItemNode().setTextFormat(
|
|
420
|
+
const newListItem = $createListItemNode().setTextFormat(listItemNode.getTextFormat()).setTextStyle(listItemNode.getTextStyle());
|
|
422
421
|
const newList = $createListNode(parent.getListType()).setTextFormat(parent.getTextFormat()).setTextStyle(parent.getTextStyle());
|
|
423
422
|
newListItem.append(newList);
|
|
424
423
|
newList.append(listItemNode);
|
|
@@ -493,7 +492,7 @@ function $handleOutdent(listItemNode) {
|
|
|
493
492
|
* (which should be the parent node) and insert the ParagraphNode as a sibling to the ListNode. If the ListNode is
|
|
494
493
|
* nested in a ListItemNode instead, it will add the ParagraphNode after the grandparent ListItemNode.
|
|
495
494
|
* Throws an invariant if the selection is not a child of a ListNode.
|
|
496
|
-
* @returns true if a ParagraphNode was inserted
|
|
495
|
+
* @returns true if a ParagraphNode was inserted successfully, false if there is no selection
|
|
497
496
|
* or the selection does not contain a ListItemNode or the node already holds text.
|
|
498
497
|
*/
|
|
499
498
|
function $handleListInsertParagraph() {
|
|
@@ -561,88 +560,97 @@ function normalizeClassNames(...classNames) {
|
|
|
561
560
|
return rval;
|
|
562
561
|
}
|
|
563
562
|
|
|
563
|
+
function applyMarkerStyles(dom, node, prevNode) {
|
|
564
|
+
const styles = LexicalSelection.getStyleObjectFromCSS(node.__textStyle);
|
|
565
|
+
for (const k in styles) {
|
|
566
|
+
dom.style.setProperty(`--listitem-marker-${k}`, styles[k]);
|
|
567
|
+
}
|
|
568
|
+
if (prevNode) {
|
|
569
|
+
for (const k in LexicalSelection.getStyleObjectFromCSS(prevNode.__textStyle)) {
|
|
570
|
+
if (!(k in styles)) {
|
|
571
|
+
dom.style.removeProperty(`--listitem-marker-${k}`);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
564
577
|
/** @noInheritDoc */
|
|
565
578
|
class ListItemNode extends lexical.ElementNode {
|
|
566
579
|
/** @internal */
|
|
567
580
|
|
|
568
581
|
/** @internal */
|
|
569
582
|
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
583
|
+
/** @internal */
|
|
584
|
+
$config() {
|
|
585
|
+
return this.config('listitem', {
|
|
586
|
+
$transform: node => {
|
|
587
|
+
if (node.__checked == null) {
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
const parent = node.getParent();
|
|
591
|
+
if ($isListNode(parent)) {
|
|
592
|
+
if (parent.getListType() !== 'check' && node.getChecked() != null) {
|
|
593
|
+
node.setChecked(undefined);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
},
|
|
597
|
+
extends: lexical.ElementNode,
|
|
598
|
+
importDOM: lexical.buildImportMap({
|
|
599
|
+
li: () => ({
|
|
600
|
+
conversion: $convertListItemElement,
|
|
601
|
+
priority: 0
|
|
602
|
+
})
|
|
603
|
+
})
|
|
604
|
+
});
|
|
575
605
|
}
|
|
576
|
-
constructor(value, checked, key) {
|
|
606
|
+
constructor(value = 1, checked = undefined, key) {
|
|
577
607
|
super(key);
|
|
578
608
|
this.__value = value === undefined ? 1 : value;
|
|
579
609
|
this.__checked = checked;
|
|
580
610
|
}
|
|
611
|
+
afterCloneFrom(prevNode) {
|
|
612
|
+
super.afterCloneFrom(prevNode);
|
|
613
|
+
this.__value = prevNode.__value;
|
|
614
|
+
this.__checked = prevNode.__checked;
|
|
615
|
+
}
|
|
581
616
|
createDOM(config) {
|
|
582
617
|
const element = document.createElement('li');
|
|
583
|
-
|
|
584
|
-
if ($isListNode(parent) && parent.getListType() === 'check') {
|
|
585
|
-
updateListItemChecked(element, this, null);
|
|
586
|
-
}
|
|
587
|
-
element.value = this.__value;
|
|
588
|
-
$setListItemThemeClassNames(element, config.theme, this);
|
|
589
|
-
const nextStyle = this.__style || this.__textStyle;
|
|
590
|
-
if (nextStyle) {
|
|
591
|
-
element.style.cssText = nextStyle;
|
|
592
|
-
}
|
|
618
|
+
this.updateListItemDOM(null, element, config);
|
|
593
619
|
return element;
|
|
594
620
|
}
|
|
595
|
-
|
|
621
|
+
updateListItemDOM(prevNode, dom, config) {
|
|
596
622
|
const parent = this.getParent();
|
|
597
623
|
if ($isListNode(parent) && parent.getListType() === 'check') {
|
|
598
624
|
updateListItemChecked(dom, this, prevNode);
|
|
599
625
|
}
|
|
600
|
-
// @ts-expect-error - this is always HTMLListItemElement
|
|
601
626
|
dom.value = this.__value;
|
|
602
627
|
$setListItemThemeClassNames(dom, config.theme, this);
|
|
603
|
-
const prevStyle = prevNode.__style
|
|
604
|
-
const nextStyle = this.__style
|
|
628
|
+
const prevStyle = prevNode ? prevNode.__style : '';
|
|
629
|
+
const nextStyle = this.__style;
|
|
605
630
|
if (prevStyle !== nextStyle) {
|
|
606
|
-
dom.style.cssText = nextStyle;
|
|
607
631
|
if (nextStyle === '') {
|
|
608
632
|
dom.removeAttribute('style');
|
|
633
|
+
} else {
|
|
634
|
+
dom.style.cssText = nextStyle;
|
|
609
635
|
}
|
|
610
636
|
}
|
|
611
|
-
|
|
612
|
-
}
|
|
613
|
-
static transform() {
|
|
614
|
-
return node => {
|
|
615
|
-
if (!$isListItemNode(node)) {
|
|
616
|
-
formatDevErrorMessage(`node is not a ListItemNode`);
|
|
617
|
-
}
|
|
618
|
-
if (node.__checked == null) {
|
|
619
|
-
return;
|
|
620
|
-
}
|
|
621
|
-
const parent = node.getParent();
|
|
622
|
-
if ($isListNode(parent)) {
|
|
623
|
-
if (parent.getListType() !== 'check' && node.getChecked() != null) {
|
|
624
|
-
node.setChecked(undefined);
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
};
|
|
628
|
-
}
|
|
629
|
-
static importDOM() {
|
|
630
|
-
return {
|
|
631
|
-
li: () => ({
|
|
632
|
-
conversion: $convertListItemElement,
|
|
633
|
-
priority: 0
|
|
634
|
-
})
|
|
635
|
-
};
|
|
637
|
+
applyMarkerStyles(dom, this, prevNode);
|
|
636
638
|
}
|
|
637
|
-
|
|
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;
|
|
639
644
|
}
|
|
640
645
|
updateFromJSON(serializedNode) {
|
|
641
646
|
return super.updateFromJSON(serializedNode).setValue(serializedNode.value).setChecked(serializedNode.checked);
|
|
642
647
|
}
|
|
643
648
|
exportDOM(editor) {
|
|
644
649
|
const element = this.createDOM(editor._config);
|
|
645
|
-
|
|
650
|
+
const formatType = this.getFormatType();
|
|
651
|
+
if (formatType) {
|
|
652
|
+
element.style.textAlign = formatType;
|
|
653
|
+
}
|
|
646
654
|
const direction = this.getDirection();
|
|
647
655
|
if (direction) {
|
|
648
656
|
element.dir = direction;
|
|
@@ -973,6 +981,14 @@ function $isListItemNode(node) {
|
|
|
973
981
|
return node instanceof ListItemNode;
|
|
974
982
|
}
|
|
975
983
|
|
|
984
|
+
/**
|
|
985
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
986
|
+
*
|
|
987
|
+
* This source code is licensed under the MIT license found in the
|
|
988
|
+
* LICENSE file in the root directory of this source tree.
|
|
989
|
+
*
|
|
990
|
+
*/
|
|
991
|
+
|
|
976
992
|
/** @noInheritDoc */
|
|
977
993
|
class ListNode extends lexical.ElementNode {
|
|
978
994
|
/** @internal */
|
|
@@ -981,12 +997,25 @@ class ListNode extends lexical.ElementNode {
|
|
|
981
997
|
|
|
982
998
|
/** @internal */
|
|
983
999
|
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
1000
|
+
/** @internal */
|
|
1001
|
+
$config() {
|
|
1002
|
+
return this.config('list', {
|
|
1003
|
+
$transform: node => {
|
|
1004
|
+
mergeNextSiblingListIfSameType(node);
|
|
1005
|
+
updateChildrenListItemValue(node);
|
|
1006
|
+
},
|
|
1007
|
+
extends: lexical.ElementNode,
|
|
1008
|
+
importDOM: lexical.buildImportMap({
|
|
1009
|
+
ol: () => ({
|
|
1010
|
+
conversion: $convertListNode,
|
|
1011
|
+
priority: 0
|
|
1012
|
+
}),
|
|
1013
|
+
ul: () => ({
|
|
1014
|
+
conversion: $convertListNode,
|
|
1015
|
+
priority: 0
|
|
1016
|
+
})
|
|
1017
|
+
})
|
|
1018
|
+
});
|
|
990
1019
|
}
|
|
991
1020
|
constructor(listType = 'number', start = 1, key) {
|
|
992
1021
|
super(key);
|
|
@@ -995,8 +1024,14 @@ class ListNode extends lexical.ElementNode {
|
|
|
995
1024
|
this.__tag = _listType === 'number' ? 'ol' : 'ul';
|
|
996
1025
|
this.__start = start;
|
|
997
1026
|
}
|
|
1027
|
+
afterCloneFrom(prevNode) {
|
|
1028
|
+
super.afterCloneFrom(prevNode);
|
|
1029
|
+
this.__listType = prevNode.__listType;
|
|
1030
|
+
this.__tag = prevNode.__tag;
|
|
1031
|
+
this.__start = prevNode.__start;
|
|
1032
|
+
}
|
|
998
1033
|
getTag() {
|
|
999
|
-
return this.__tag;
|
|
1034
|
+
return this.getLatest().__tag;
|
|
1000
1035
|
}
|
|
1001
1036
|
setListType(type) {
|
|
1002
1037
|
const writable = this.getWritable();
|
|
@@ -1005,10 +1040,10 @@ class ListNode extends lexical.ElementNode {
|
|
|
1005
1040
|
return writable;
|
|
1006
1041
|
}
|
|
1007
1042
|
getListType() {
|
|
1008
|
-
return this.__listType;
|
|
1043
|
+
return this.getLatest().__listType;
|
|
1009
1044
|
}
|
|
1010
1045
|
getStart() {
|
|
1011
|
-
return this.__start;
|
|
1046
|
+
return this.getLatest().__start;
|
|
1012
1047
|
}
|
|
1013
1048
|
setStart(start) {
|
|
1014
1049
|
const self = this.getWritable();
|
|
@@ -1036,30 +1071,6 @@ class ListNode extends lexical.ElementNode {
|
|
|
1036
1071
|
$setListThemeClassNames(dom, config.theme, this);
|
|
1037
1072
|
return false;
|
|
1038
1073
|
}
|
|
1039
|
-
static transform() {
|
|
1040
|
-
return node => {
|
|
1041
|
-
if (!$isListNode(node)) {
|
|
1042
|
-
formatDevErrorMessage(`node is not a ListNode`);
|
|
1043
|
-
}
|
|
1044
|
-
mergeNextSiblingListIfSameType(node);
|
|
1045
|
-
updateChildrenListItemValue(node);
|
|
1046
|
-
};
|
|
1047
|
-
}
|
|
1048
|
-
static importDOM() {
|
|
1049
|
-
return {
|
|
1050
|
-
ol: () => ({
|
|
1051
|
-
conversion: $convertListNode,
|
|
1052
|
-
priority: 0
|
|
1053
|
-
}),
|
|
1054
|
-
ul: () => ({
|
|
1055
|
-
conversion: $convertListNode,
|
|
1056
|
-
priority: 0
|
|
1057
|
-
})
|
|
1058
|
-
};
|
|
1059
|
-
}
|
|
1060
|
-
static importJSON(serializedNode) {
|
|
1061
|
-
return $createListNode().updateFromJSON(serializedNode);
|
|
1062
|
-
}
|
|
1063
1074
|
updateFromJSON(serializedNode) {
|
|
1064
1075
|
return super.updateFromJSON(serializedNode).setListType(serializedNode.listType).setStart(serializedNode.start);
|
|
1065
1076
|
}
|
|
@@ -1091,29 +1102,18 @@ class ListNode extends lexical.ElementNode {
|
|
|
1091
1102
|
canIndent() {
|
|
1092
1103
|
return false;
|
|
1093
1104
|
}
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
if ($isListNode(currentNode)) {
|
|
1102
|
-
listItemNode.append(currentNode);
|
|
1103
|
-
} else if (lexical.$isElementNode(currentNode)) {
|
|
1104
|
-
if (currentNode.isInline()) {
|
|
1105
|
-
listItemNode.append(currentNode);
|
|
1106
|
-
} else {
|
|
1107
|
-
const textNode = lexical.$createTextNode(currentNode.getTextContent());
|
|
1108
|
-
listItemNode.append(textNode);
|
|
1109
|
-
}
|
|
1110
|
-
} else {
|
|
1111
|
-
listItemNode.append(currentNode);
|
|
1105
|
+
splice(start, deleteCount, nodesToInsert) {
|
|
1106
|
+
let listItemNodesToInsert = nodesToInsert;
|
|
1107
|
+
for (let i = 0; i < nodesToInsert.length; i++) {
|
|
1108
|
+
const node = nodesToInsert[i];
|
|
1109
|
+
if (!$isListItemNode(node)) {
|
|
1110
|
+
if (listItemNodesToInsert === nodesToInsert) {
|
|
1111
|
+
listItemNodesToInsert = [...nodesToInsert];
|
|
1112
1112
|
}
|
|
1113
|
-
|
|
1113
|
+
listItemNodesToInsert[i] = $createListItemNode().append(lexical.$isElementNode(node) && !($isListNode(node) || node.isInline()) ? lexical.$createTextNode(node.getTextContent()) : node);
|
|
1114
1114
|
}
|
|
1115
1115
|
}
|
|
1116
|
-
return
|
|
1116
|
+
return super.splice(start, deleteCount, listItemNodesToInsert);
|
|
1117
1117
|
}
|
|
1118
1118
|
extractWithChild(child) {
|
|
1119
1119
|
return $isListItemNode(child);
|
|
@@ -1256,14 +1256,216 @@ function $isListNode(node) {
|
|
|
1256
1256
|
*
|
|
1257
1257
|
*/
|
|
1258
1258
|
|
|
1259
|
+
const INSERT_CHECK_LIST_COMMAND = lexical.createCommand('INSERT_CHECK_LIST_COMMAND');
|
|
1260
|
+
function registerCheckList(editor) {
|
|
1261
|
+
return LexicalUtils.mergeRegister(editor.registerCommand(INSERT_CHECK_LIST_COMMAND, () => {
|
|
1262
|
+
$insertList('check');
|
|
1263
|
+
return true;
|
|
1264
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.KEY_ARROW_DOWN_COMMAND, event => {
|
|
1265
|
+
return handleArrowUpOrDown(event, editor, false);
|
|
1266
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.KEY_ARROW_UP_COMMAND, event => {
|
|
1267
|
+
return handleArrowUpOrDown(event, editor, true);
|
|
1268
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.KEY_ESCAPE_COMMAND, () => {
|
|
1269
|
+
const activeItem = getActiveCheckListItem();
|
|
1270
|
+
if (activeItem != null) {
|
|
1271
|
+
const rootElement = editor.getRootElement();
|
|
1272
|
+
if (rootElement != null) {
|
|
1273
|
+
rootElement.focus();
|
|
1274
|
+
}
|
|
1275
|
+
return true;
|
|
1276
|
+
}
|
|
1277
|
+
return false;
|
|
1278
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.KEY_SPACE_COMMAND, event => {
|
|
1279
|
+
const activeItem = getActiveCheckListItem();
|
|
1280
|
+
if (activeItem != null && editor.isEditable()) {
|
|
1281
|
+
editor.update(() => {
|
|
1282
|
+
const listItemNode = lexical.$getNearestNodeFromDOMNode(activeItem);
|
|
1283
|
+
if ($isListItemNode(listItemNode)) {
|
|
1284
|
+
event.preventDefault();
|
|
1285
|
+
listItemNode.toggleChecked();
|
|
1286
|
+
}
|
|
1287
|
+
});
|
|
1288
|
+
return true;
|
|
1289
|
+
}
|
|
1290
|
+
return false;
|
|
1291
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical.KEY_ARROW_LEFT_COMMAND, event => {
|
|
1292
|
+
return editor.getEditorState().read(() => {
|
|
1293
|
+
const selection = lexical.$getSelection();
|
|
1294
|
+
if (lexical.$isRangeSelection(selection) && selection.isCollapsed()) {
|
|
1295
|
+
const {
|
|
1296
|
+
anchor
|
|
1297
|
+
} = selection;
|
|
1298
|
+
const isElement = anchor.type === 'element';
|
|
1299
|
+
if (isElement || anchor.offset === 0) {
|
|
1300
|
+
const anchorNode = anchor.getNode();
|
|
1301
|
+
const elementNode = LexicalUtils.$findMatchingParent(anchorNode, node => lexical.$isElementNode(node) && !node.isInline());
|
|
1302
|
+
if ($isListItemNode(elementNode)) {
|
|
1303
|
+
const parent = elementNode.getParent();
|
|
1304
|
+
if ($isListNode(parent) && parent.getListType() === 'check' && (isElement || elementNode.getFirstDescendant() === anchorNode)) {
|
|
1305
|
+
const domNode = editor.getElementByKey(elementNode.__key);
|
|
1306
|
+
if (domNode != null && document.activeElement !== domNode) {
|
|
1307
|
+
domNode.focus();
|
|
1308
|
+
event.preventDefault();
|
|
1309
|
+
return true;
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
return false;
|
|
1316
|
+
});
|
|
1317
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerRootListener((rootElement, prevElement) => {
|
|
1318
|
+
if (rootElement !== null) {
|
|
1319
|
+
rootElement.addEventListener('click', handleClick);
|
|
1320
|
+
rootElement.addEventListener('pointerdown', handlePointerDown);
|
|
1321
|
+
}
|
|
1322
|
+
if (prevElement !== null) {
|
|
1323
|
+
prevElement.removeEventListener('click', handleClick);
|
|
1324
|
+
prevElement.removeEventListener('pointerdown', handlePointerDown);
|
|
1325
|
+
}
|
|
1326
|
+
}));
|
|
1327
|
+
}
|
|
1328
|
+
function handleCheckItemEvent(event, callback) {
|
|
1329
|
+
const target = event.target;
|
|
1330
|
+
if (!LexicalUtils.isHTMLElement(target)) {
|
|
1331
|
+
return;
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
// Ignore clicks on LI that have nested lists
|
|
1335
|
+
const firstChild = target.firstChild;
|
|
1336
|
+
if (LexicalUtils.isHTMLElement(firstChild) && (firstChild.tagName === 'UL' || firstChild.tagName === 'OL')) {
|
|
1337
|
+
return;
|
|
1338
|
+
}
|
|
1339
|
+
const parentNode = target.parentNode;
|
|
1340
|
+
|
|
1341
|
+
// @ts-ignore internal field
|
|
1342
|
+
if (!parentNode || parentNode.__lexicalListType !== 'check') {
|
|
1343
|
+
return;
|
|
1344
|
+
}
|
|
1345
|
+
const rect = target.getBoundingClientRect();
|
|
1346
|
+
const zoom = LexicalUtils.calculateZoomLevel(target);
|
|
1347
|
+
const clientX = event.clientX / zoom;
|
|
1348
|
+
|
|
1349
|
+
// Use getComputedStyle if available, otherwise fallback to 0px width
|
|
1350
|
+
const beforeStyles = window.getComputedStyle ? window.getComputedStyle(target, '::before') : {
|
|
1351
|
+
width: '0px'
|
|
1352
|
+
};
|
|
1353
|
+
const beforeWidthInPixels = parseFloat(beforeStyles.width);
|
|
1354
|
+
|
|
1355
|
+
// Make click area slightly larger for touch devices to improve accessibility
|
|
1356
|
+
const isTouchEvent = event.pointerType === 'touch';
|
|
1357
|
+
const clickAreaPadding = isTouchEvent ? 32 : 0; // Add 32px padding for touch events
|
|
1358
|
+
|
|
1359
|
+
if (target.dir === 'rtl' ? clientX < rect.right + clickAreaPadding && clientX > rect.right - beforeWidthInPixels - clickAreaPadding : clientX > rect.left - clickAreaPadding && clientX < rect.left + beforeWidthInPixels + clickAreaPadding) {
|
|
1360
|
+
callback();
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
function handleClick(event) {
|
|
1364
|
+
handleCheckItemEvent(event, () => {
|
|
1365
|
+
if (LexicalUtils.isHTMLElement(event.target)) {
|
|
1366
|
+
const domNode = event.target;
|
|
1367
|
+
const editor = lexical.getNearestEditorFromDOMNode(domNode);
|
|
1368
|
+
if (editor != null && editor.isEditable()) {
|
|
1369
|
+
editor.update(() => {
|
|
1370
|
+
const node = lexical.$getNearestNodeFromDOMNode(domNode);
|
|
1371
|
+
if ($isListItemNode(node)) {
|
|
1372
|
+
domNode.focus();
|
|
1373
|
+
node.toggleChecked();
|
|
1374
|
+
}
|
|
1375
|
+
});
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
});
|
|
1379
|
+
}
|
|
1380
|
+
function handlePointerDown(event) {
|
|
1381
|
+
handleCheckItemEvent(event, () => {
|
|
1382
|
+
// Prevents caret moving when clicking on check mark
|
|
1383
|
+
event.preventDefault();
|
|
1384
|
+
});
|
|
1385
|
+
}
|
|
1386
|
+
function getActiveCheckListItem() {
|
|
1387
|
+
const activeElement = document.activeElement;
|
|
1388
|
+
return LexicalUtils.isHTMLElement(activeElement) && activeElement.tagName === 'LI' && activeElement.parentNode != null &&
|
|
1389
|
+
// @ts-ignore internal field
|
|
1390
|
+
activeElement.parentNode.__lexicalListType === 'check' ? activeElement : null;
|
|
1391
|
+
}
|
|
1392
|
+
function findCheckListItemSibling(node, backward) {
|
|
1393
|
+
let sibling = backward ? node.getPreviousSibling() : node.getNextSibling();
|
|
1394
|
+
let parent = node;
|
|
1395
|
+
|
|
1396
|
+
// Going up in a tree to get non-null sibling
|
|
1397
|
+
while (sibling == null && $isListItemNode(parent)) {
|
|
1398
|
+
// Get li -> parent ul/ol -> parent li
|
|
1399
|
+
parent = parent.getParentOrThrow().getParent();
|
|
1400
|
+
if (parent != null) {
|
|
1401
|
+
sibling = backward ? parent.getPreviousSibling() : parent.getNextSibling();
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1405
|
+
// Going down in a tree to get first non-nested list item
|
|
1406
|
+
while ($isListItemNode(sibling)) {
|
|
1407
|
+
const firstChild = backward ? sibling.getLastChild() : sibling.getFirstChild();
|
|
1408
|
+
if (!$isListNode(firstChild)) {
|
|
1409
|
+
return sibling;
|
|
1410
|
+
}
|
|
1411
|
+
sibling = backward ? firstChild.getLastChild() : firstChild.getFirstChild();
|
|
1412
|
+
}
|
|
1413
|
+
return null;
|
|
1414
|
+
}
|
|
1415
|
+
function handleArrowUpOrDown(event, editor, backward) {
|
|
1416
|
+
const activeItem = getActiveCheckListItem();
|
|
1417
|
+
if (activeItem != null) {
|
|
1418
|
+
editor.update(() => {
|
|
1419
|
+
const listItem = lexical.$getNearestNodeFromDOMNode(activeItem);
|
|
1420
|
+
if (!$isListItemNode(listItem)) {
|
|
1421
|
+
return;
|
|
1422
|
+
}
|
|
1423
|
+
const nextListItem = findCheckListItemSibling(listItem, backward);
|
|
1424
|
+
if (nextListItem != null) {
|
|
1425
|
+
nextListItem.selectStart();
|
|
1426
|
+
const dom = editor.getElementByKey(nextListItem.__key);
|
|
1427
|
+
if (dom != null) {
|
|
1428
|
+
event.preventDefault();
|
|
1429
|
+
setTimeout(() => {
|
|
1430
|
+
dom.focus();
|
|
1431
|
+
}, 0);
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
});
|
|
1435
|
+
}
|
|
1436
|
+
return false;
|
|
1437
|
+
}
|
|
1438
|
+
|
|
1439
|
+
/**
|
|
1440
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1441
|
+
*
|
|
1442
|
+
* This source code is licensed under the MIT license found in the
|
|
1443
|
+
* LICENSE file in the root directory of this source tree.
|
|
1444
|
+
*
|
|
1445
|
+
*/
|
|
1446
|
+
|
|
1447
|
+
const UPDATE_LIST_START_COMMAND = lexical.createCommand('UPDATE_LIST_START_COMMAND');
|
|
1259
1448
|
const INSERT_UNORDERED_LIST_COMMAND = lexical.createCommand('INSERT_UNORDERED_LIST_COMMAND');
|
|
1260
1449
|
const INSERT_ORDERED_LIST_COMMAND = lexical.createCommand('INSERT_ORDERED_LIST_COMMAND');
|
|
1261
|
-
const INSERT_CHECK_LIST_COMMAND = lexical.createCommand('INSERT_CHECK_LIST_COMMAND');
|
|
1262
1450
|
const REMOVE_LIST_COMMAND = lexical.createCommand('REMOVE_LIST_COMMAND');
|
|
1263
1451
|
function registerList(editor) {
|
|
1264
1452
|
const removeListener = LexicalUtils.mergeRegister(editor.registerCommand(INSERT_ORDERED_LIST_COMMAND, () => {
|
|
1265
1453
|
$insertList('number');
|
|
1266
1454
|
return true;
|
|
1455
|
+
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(UPDATE_LIST_START_COMMAND, payload => {
|
|
1456
|
+
const {
|
|
1457
|
+
listNodeKey,
|
|
1458
|
+
newStart
|
|
1459
|
+
} = payload;
|
|
1460
|
+
const listNode = lexical.$getNodeByKey(listNodeKey);
|
|
1461
|
+
if (!$isListNode(listNode)) {
|
|
1462
|
+
return false;
|
|
1463
|
+
}
|
|
1464
|
+
if (listNode.getListType() === 'number') {
|
|
1465
|
+
listNode.setStart(newStart);
|
|
1466
|
+
updateChildrenListItemValue(listNode);
|
|
1467
|
+
}
|
|
1468
|
+
return true;
|
|
1267
1469
|
}, lexical.COMMAND_PRIORITY_LOW), editor.registerCommand(INSERT_UNORDERED_LIST_COMMAND, () => {
|
|
1268
1470
|
$insertList('bullet');
|
|
1269
1471
|
return true;
|
|
@@ -1302,6 +1504,64 @@ function registerList(editor) {
|
|
|
1302
1504
|
}));
|
|
1303
1505
|
return removeListener;
|
|
1304
1506
|
}
|
|
1507
|
+
function registerListStrictIndentTransform(editor) {
|
|
1508
|
+
const $formatListIndentStrict = listItemNode => {
|
|
1509
|
+
const listNode = listItemNode.getParent();
|
|
1510
|
+
if ($isListNode(listItemNode.getFirstChild()) || !$isListNode(listNode)) {
|
|
1511
|
+
return;
|
|
1512
|
+
}
|
|
1513
|
+
const startingListItemNode = LexicalUtils.$findMatchingParent(listItemNode, node => $isListItemNode(node) && $isListNode(node.getParent()) && $isListItemNode(node.getPreviousSibling()));
|
|
1514
|
+
if (startingListItemNode === null && listItemNode.getIndent() > 0) {
|
|
1515
|
+
listItemNode.setIndent(0);
|
|
1516
|
+
} else if ($isListItemNode(startingListItemNode)) {
|
|
1517
|
+
const prevListItemNode = startingListItemNode.getPreviousSibling();
|
|
1518
|
+
if ($isListItemNode(prevListItemNode)) {
|
|
1519
|
+
const endListItemNode = $findChildrenEndListItemNode(prevListItemNode);
|
|
1520
|
+
const endListNode = endListItemNode.getParent();
|
|
1521
|
+
if ($isListNode(endListNode)) {
|
|
1522
|
+
const prevDepth = $getListDepth(endListNode);
|
|
1523
|
+
const depth = $getListDepth(listNode);
|
|
1524
|
+
if (prevDepth + 1 < depth) {
|
|
1525
|
+
listItemNode.setIndent(prevDepth);
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
};
|
|
1531
|
+
const $processListWithStrictIndent = listNode => {
|
|
1532
|
+
const queue = [listNode];
|
|
1533
|
+
while (queue.length > 0) {
|
|
1534
|
+
const node = queue.shift();
|
|
1535
|
+
if (!$isListNode(node)) {
|
|
1536
|
+
continue;
|
|
1537
|
+
}
|
|
1538
|
+
for (const child of node.getChildren()) {
|
|
1539
|
+
if ($isListItemNode(child)) {
|
|
1540
|
+
$formatListIndentStrict(child);
|
|
1541
|
+
const firstChild = child.getFirstChild();
|
|
1542
|
+
if ($isListNode(firstChild)) {
|
|
1543
|
+
queue.push(firstChild);
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
};
|
|
1549
|
+
return editor.registerNodeTransform(ListNode, $processListWithStrictIndent);
|
|
1550
|
+
}
|
|
1551
|
+
function $findChildrenEndListItemNode(listItemNode) {
|
|
1552
|
+
let current = listItemNode;
|
|
1553
|
+
let firstChild = current.getFirstChild();
|
|
1554
|
+
while ($isListNode(firstChild)) {
|
|
1555
|
+
const lastChild = firstChild.getLastChild();
|
|
1556
|
+
if ($isListItemNode(lastChild)) {
|
|
1557
|
+
current = lastChild;
|
|
1558
|
+
firstChild = current.getFirstChild();
|
|
1559
|
+
} else {
|
|
1560
|
+
break;
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
return current;
|
|
1564
|
+
}
|
|
1305
1565
|
|
|
1306
1566
|
/**
|
|
1307
1567
|
* @deprecated use {@link $insertList} from an update or command listener.
|
|
@@ -1346,6 +1606,9 @@ exports.INSERT_UNORDERED_LIST_COMMAND = INSERT_UNORDERED_LIST_COMMAND;
|
|
|
1346
1606
|
exports.ListItemNode = ListItemNode;
|
|
1347
1607
|
exports.ListNode = ListNode;
|
|
1348
1608
|
exports.REMOVE_LIST_COMMAND = REMOVE_LIST_COMMAND;
|
|
1609
|
+
exports.UPDATE_LIST_START_COMMAND = UPDATE_LIST_START_COMMAND;
|
|
1349
1610
|
exports.insertList = insertList;
|
|
1611
|
+
exports.registerCheckList = registerCheckList;
|
|
1350
1612
|
exports.registerList = registerList;
|
|
1613
|
+
exports.registerListStrictIndentTransform = registerListStrictIndentTransform;
|
|
1351
1614
|
exports.removeList = removeList;
|