@webiny/lexical-nodes 6.3.0 → 6.4.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/FontColorNode.js +94 -119
  2. package/FontColorNode.js.map +1 -1
  3. package/HeadingNode.js +160 -183
  4. package/HeadingNode.js.map +1 -1
  5. package/ImageNode.js +101 -131
  6. package/ImageNode.js.map +1 -1
  7. package/LinkNode.js +228 -315
  8. package/LinkNode.js.map +1 -1
  9. package/ListItemNode.js +249 -320
  10. package/ListItemNode.js.map +1 -1
  11. package/ListNode.js +174 -223
  12. package/ListNode.js.map +1 -1
  13. package/ParagraphNode.js +119 -148
  14. package/ParagraphNode.js.map +1 -1
  15. package/QuoteNode.js +97 -102
  16. package/QuoteNode.js.map +1 -1
  17. package/components/ImageNode/ImageComponent.js +117 -147
  18. package/components/ImageNode/ImageComponent.js.map +1 -1
  19. package/components/ImageNode/ImageResizer.js +167 -194
  20. package/components/ImageNode/ImageResizer.js.map +1 -1
  21. package/generateInitialLexicalValue.js +20 -23
  22. package/generateInitialLexicalValue.js.map +1 -1
  23. package/index.js +38 -26
  24. package/index.js.map +1 -1
  25. package/package.json +4 -4
  26. package/prepareLexicalState.js +30 -43
  27. package/prepareLexicalState.js.map +1 -1
  28. package/types.js +0 -3
  29. package/utils/clearNodeFormating.js +14 -15
  30. package/utils/clearNodeFormating.js.map +1 -1
  31. package/utils/formatList.js +277 -368
  32. package/utils/formatList.js.map +1 -1
  33. package/utils/formatToHeading.js +6 -13
  34. package/utils/formatToHeading.js.map +1 -1
  35. package/utils/formatToParagraph.js +6 -7
  36. package/utils/formatToParagraph.js.map +1 -1
  37. package/utils/formatToQuote.js +6 -13
  38. package/utils/formatToQuote.js.map +1 -1
  39. package/utils/getStyleId.js +6 -11
  40. package/utils/getStyleId.js.map +1 -1
  41. package/utils/listNode.js +60 -84
  42. package/utils/listNode.js.map +1 -1
  43. package/utils/toggleLink.js +67 -118
  44. package/utils/toggleLink.js.map +1 -1
  45. package/types.js.map +0 -1
@@ -6,407 +6,316 @@ import { $createParagraphNode } from "../ParagraphNode.js";
6
6
  import { $getAllListItems, $getTopListNode, $removeHighestEmptyListParent, findNearestListItemNode, getUniqueListItemNodes, isNestedListNode } from "./listNode.js";
7
7
  const DEFAULT_LIST_START_NUMBER = 1;
8
8
  function $isSelectingEmptyListItem(anchorNode, nodes) {
9
- return $isListItemNode(anchorNode) && (nodes.length === 0 || nodes.length === 1 && anchorNode.is(nodes[0]) && anchorNode.getChildrenSize() === 0);
9
+ return $isListItemNode(anchorNode) && (0 === nodes.length || 1 === nodes.length && anchorNode.is(nodes[0]) && 0 === anchorNode.getChildrenSize());
10
10
  }
11
11
  function $getListItemValue(listItem) {
12
- const list = listItem.getParent();
13
- let value = 1;
14
- if (list !== null) {
15
- if (!$isListNode(list)) {
16
- console.log("$getListItemValue: webiny list node is not parent of webiny list item node");
17
- return DEFAULT_LIST_START_NUMBER;
18
- } else {
19
- value = list.getStart();
12
+ const list = listItem.getParent();
13
+ let value = 1;
14
+ if (null !== list) if ($isListNode(list)) value = list.getStart();
15
+ else {
16
+ console.log("$getListItemValue: webiny list node is not parent of webiny list item node");
17
+ return DEFAULT_LIST_START_NUMBER;
20
18
  }
21
- }
22
- const siblings = listItem.getPreviousSiblings();
23
- for (let i = 0; i < siblings.length; i++) {
24
- const sibling = siblings[i];
25
- if ($isListItemNode(sibling) && !$isListNode(sibling.getFirstChild())) {
26
- value++;
19
+ const siblings = listItem.getPreviousSiblings();
20
+ for(let i = 0; i < siblings.length; i++){
21
+ const sibling = siblings[i];
22
+ if ($isListItemNode(sibling) && !$isListNode(sibling.getFirstChild())) value++;
27
23
  }
28
- }
29
- return value;
24
+ return value;
30
25
  }
31
- export function insertList(editor, listType, styleId) {
32
- editor.update(() => {
33
- const selection = $getSelection();
34
- if (selection && $isRangeSelection(selection)) {
35
- const nodes = selection.getNodes();
36
- const anchor = selection.anchor;
37
- const anchorNode = anchor.getNode();
38
- const anchorNodeParent = anchorNode.getParent();
39
- if ($isSelectingEmptyListItem(anchorNode, nodes)) {
40
- const list = $createListNode(listType, styleId);
41
- if ($isRootOrShadowRoot(anchorNodeParent)) {
42
- anchorNode.replace(list);
43
- const listItem = $createListItemNode();
44
- if ($isElementNode(anchorNode)) {
45
- listItem.setFormat(anchorNode.getFormatType());
46
- listItem.setIndent(anchorNode.getIndent());
47
- }
48
- list.append(listItem);
49
- } else if ($isListItemNode(anchorNode)) {
50
- const parent = anchorNode.getParentOrThrow();
51
- append(list, parent.getChildren());
52
- parent.replace(list);
53
- }
54
- return;
55
- } else {
56
- const handled = new Set();
57
- for (let i = 0; i < nodes.length; i++) {
58
- const node = nodes[i];
59
- if ($isElementNode(node) && node.isEmpty() && !handled.has(node.getKey())) {
60
- createListOrMerge(node, listType, styleId);
61
- continue;
62
- }
63
- if ($isLeafNode(node)) {
64
- let parent = node.getParent();
65
- while (parent != null) {
66
- const parentKey = parent.getKey();
67
- if ($isListNode(parent)) {
68
- if (!handled.has(parentKey)) {
69
- const newListNode = $createListNode(listType, styleId);
70
- append(newListNode, parent.getChildren());
71
- parent.replace(newListNode);
72
- updateChildrenListItemValue(newListNode);
73
- handled.add(parentKey);
26
+ function insertList(editor, listType, styleId) {
27
+ editor.update(()=>{
28
+ const selection = $getSelection();
29
+ if (selection && $isRangeSelection(selection)) {
30
+ const nodes = selection.getNodes();
31
+ const anchor = selection.anchor;
32
+ const anchorNode = anchor.getNode();
33
+ const anchorNodeParent = anchorNode.getParent();
34
+ if ($isSelectingEmptyListItem(anchorNode, nodes)) {
35
+ const list = $createListNode(listType, styleId);
36
+ if ($isRootOrShadowRoot(anchorNodeParent)) {
37
+ anchorNode.replace(list);
38
+ const listItem = $createListItemNode();
39
+ if ($isElementNode(anchorNode)) {
40
+ listItem.setFormat(anchorNode.getFormatType());
41
+ listItem.setIndent(anchorNode.getIndent());
42
+ }
43
+ list.append(listItem);
44
+ } else if ($isListItemNode(anchorNode)) {
45
+ const parent = anchorNode.getParentOrThrow();
46
+ append(list, parent.getChildren());
47
+ parent.replace(list);
74
48
  }
75
- break;
76
- } else {
77
- const nextParent = parent.getParent();
78
- if ($isRootOrShadowRoot(nextParent) && !handled.has(parentKey)) {
79
- handled.add(parentKey);
80
- createListOrMerge(parent, listType, styleId);
81
- break;
49
+ return;
50
+ }
51
+ {
52
+ const handled = new Set();
53
+ for(let i = 0; i < nodes.length; i++){
54
+ const node = nodes[i];
55
+ if ($isElementNode(node) && node.isEmpty() && !handled.has(node.getKey())) {
56
+ createListOrMerge(node, listType, styleId);
57
+ continue;
58
+ }
59
+ if ($isLeafNode(node)) {
60
+ let parent = node.getParent();
61
+ while(null != parent){
62
+ const parentKey = parent.getKey();
63
+ if ($isListNode(parent)) {
64
+ if (!handled.has(parentKey)) {
65
+ const newListNode = $createListNode(listType, styleId);
66
+ append(newListNode, parent.getChildren());
67
+ parent.replace(newListNode);
68
+ updateChildrenListItemValue(newListNode);
69
+ handled.add(parentKey);
70
+ }
71
+ break;
72
+ }
73
+ {
74
+ const nextParent = parent.getParent();
75
+ if ($isRootOrShadowRoot(nextParent) && !handled.has(parentKey)) {
76
+ handled.add(parentKey);
77
+ createListOrMerge(parent, listType, styleId);
78
+ break;
79
+ }
80
+ parent = nextParent;
81
+ }
82
+ }
83
+ }
82
84
  }
83
- parent = nextParent;
84
- }
85
85
  }
86
- }
87
86
  }
88
- }
89
- }
90
- });
87
+ });
91
88
  }
92
89
  function append(node, nodesToAppend) {
93
- node.splice(node.getChildrenSize(), 0, nodesToAppend);
90
+ node.splice(node.getChildrenSize(), 0, nodesToAppend);
94
91
  }
95
92
  function createListOrMerge(node, listType, styleId) {
96
- if ($isListNode(node)) {
97
- return node;
98
- }
99
- const previousSibling = node.getPreviousSibling();
100
- const nextSibling = node.getNextSibling();
101
- const listItem = $createListItemNode();
102
- listItem.setFormat(node.getFormatType());
103
- listItem.setIndent(node.getIndent());
104
- append(listItem, node.getChildren());
105
- if ($isListNode(previousSibling) && listType === previousSibling.getListType()) {
106
- previousSibling.append(listItem);
107
- node.remove();
108
- // if the same type of list is on both sides, merge them.
109
-
93
+ if ($isListNode(node)) return node;
94
+ const previousSibling = node.getPreviousSibling();
95
+ const nextSibling = node.getNextSibling();
96
+ const listItem = $createListItemNode();
97
+ listItem.setFormat(node.getFormatType());
98
+ listItem.setIndent(node.getIndent());
99
+ append(listItem, node.getChildren());
100
+ if ($isListNode(previousSibling) && listType === previousSibling.getListType()) {
101
+ previousSibling.append(listItem);
102
+ node.remove();
103
+ if ($isListNode(nextSibling) && listType === nextSibling.getListType()) {
104
+ append(previousSibling, nextSibling.getChildren());
105
+ nextSibling.remove();
106
+ }
107
+ return previousSibling;
108
+ }
110
109
  if ($isListNode(nextSibling) && listType === nextSibling.getListType()) {
111
- append(previousSibling, nextSibling.getChildren());
112
- nextSibling.remove();
110
+ nextSibling.getFirstChildOrThrow().insertBefore(listItem);
111
+ node.remove();
112
+ return nextSibling;
113
+ }
114
+ {
115
+ const list = $createListNode(listType, styleId);
116
+ list.append(listItem);
117
+ node.replace(list);
118
+ updateChildrenListItemValue(list);
119
+ return list;
113
120
  }
114
- return previousSibling;
115
- } else if ($isListNode(nextSibling) && listType === nextSibling.getListType()) {
116
- nextSibling.getFirstChildOrThrow().insertBefore(listItem);
117
- node.remove();
118
- return nextSibling;
119
- } else {
120
- const list = $createListNode(listType, styleId);
121
- list.append(listItem);
122
- node.replace(list);
123
- updateChildrenListItemValue(list);
124
- return list;
125
- }
126
121
  }
127
-
128
- /**
129
- * A recursive function that goes through each list and their children, including nested lists,
130
- * appending list2 children after list1 children and updating ListItemNode values.
131
- * @param list1 - The first list to be merged.
132
- * @param list2 - The second list to be merged.
133
- */
134
- export function mergeLists(list1, list2) {
135
- const listItem1 = list1.getLastChild();
136
- const listItem2 = list2.getFirstChild();
137
- if (listItem1 && listItem2 && isNestedListNode(listItem1) && isNestedListNode(listItem2)) {
138
- mergeLists(listItem1.getFirstChild(), listItem2.getFirstChild());
139
- listItem2.remove();
140
- }
141
- const toMerge = list2.getChildren();
142
- if (toMerge.length > 0) {
143
- list1.append(...toMerge);
144
- updateChildrenListItemValue(list1);
145
- }
146
- list2.remove();
122
+ function mergeLists(list1, list2) {
123
+ const listItem1 = list1.getLastChild();
124
+ const listItem2 = list2.getFirstChild();
125
+ if (listItem1 && listItem2 && isNestedListNode(listItem1) && isNestedListNode(listItem2)) {
126
+ mergeLists(listItem1.getFirstChild(), listItem2.getFirstChild());
127
+ listItem2.remove();
128
+ }
129
+ const toMerge = list2.getChildren();
130
+ if (toMerge.length > 0) {
131
+ list1.append(...toMerge);
132
+ updateChildrenListItemValue(list1);
133
+ }
134
+ list2.remove();
147
135
  }
148
- export function removeList(editor) {
149
- editor.update(() => {
150
- const selection = $getSelection();
151
- if ($isRangeSelection(selection)) {
152
- const listNodes = new Set();
153
- const nodes = selection.getNodes();
154
- const anchorNode = selection.anchor.getNode();
155
- if ($isSelectingEmptyListItem(anchorNode, nodes)) {
156
- listNodes.add($getTopListNode(anchorNode));
157
- } else {
158
- for (let i = 0; i < nodes.length; i++) {
159
- const node = nodes[i];
160
- if ($isLeafNode(node)) {
161
- const listItemNode = $getNearestNodeOfType(node, ListItemNode);
162
- if (listItemNode != null) {
163
- listNodes.add($getTopListNode(listItemNode));
136
+ function removeList(editor) {
137
+ editor.update(()=>{
138
+ const selection = $getSelection();
139
+ if ($isRangeSelection(selection)) {
140
+ const listNodes = new Set();
141
+ const nodes = selection.getNodes();
142
+ const anchorNode = selection.anchor.getNode();
143
+ if ($isSelectingEmptyListItem(anchorNode, nodes)) listNodes.add($getTopListNode(anchorNode));
144
+ else for(let i = 0; i < nodes.length; i++){
145
+ const node = nodes[i];
146
+ if ($isLeafNode(node)) {
147
+ const listItemNode = $getNearestNodeOfType(node, ListItemNode);
148
+ if (null != listItemNode) listNodes.add($getTopListNode(listItemNode));
149
+ }
150
+ }
151
+ for (const listNode of listNodes){
152
+ let insertionPoint = listNode;
153
+ const listItems = $getAllListItems(listNode);
154
+ for (const listItemNode of listItems){
155
+ const paragraph = $createParagraphNode();
156
+ append(paragraph, listItemNode.getChildren());
157
+ insertionPoint.insertAfter(paragraph);
158
+ insertionPoint = paragraph;
159
+ if (listItemNode.__key === selection.anchor.key) selection.anchor.set(paragraph.getKey(), 0, "element");
160
+ if (listItemNode.__key === selection.focus.key) selection.focus.set(paragraph.getKey(), 0, "element");
161
+ listItemNode.remove();
162
+ }
163
+ listNode.remove();
164
164
  }
165
- }
166
- }
167
- }
168
- for (const listNode of listNodes) {
169
- let insertionPoint = listNode;
170
- const listItems = $getAllListItems(listNode);
171
- for (const listItemNode of listItems) {
172
- const paragraph = $createParagraphNode();
173
- append(paragraph, listItemNode.getChildren());
174
- insertionPoint.insertAfter(paragraph);
175
- insertionPoint = paragraph;
176
-
177
- // When the anchor and focus fall on the textNode
178
- // we don't have to change the selection because the textNode will be appended to
179
- // the newly generated paragraph.
180
- // When selection is in empty nested list item, selection is actually on the listItemNode.
181
- // When the corresponding listItemNode is deleted and replaced by the newly generated paragraph
182
- // we should manually set the selection's focus and anchor to the newly generated paragraph.
183
- if (listItemNode.__key === selection.anchor.key) {
184
- selection.anchor.set(paragraph.getKey(), 0, "element");
185
- }
186
- if (listItemNode.__key === selection.focus.key) {
187
- selection.focus.set(paragraph.getKey(), 0, "element");
188
- }
189
- listItemNode.remove();
190
165
  }
191
- listNode.remove();
192
- }
193
- }
194
- });
166
+ });
195
167
  }
196
- export function updateChildrenListItemValue(list, children) {
197
- const childrenOrExisting = children || list.getChildren();
198
- if (childrenOrExisting !== undefined) {
199
- for (let i = 0; i < childrenOrExisting.length; i++) {
200
- const child = childrenOrExisting[i];
201
- if ($isListItemNode(child)) {
202
- const prevValue = child.getValue();
203
- const nextValue = $getListItemValue(child);
204
- if (prevValue !== nextValue) {
205
- child.setValue(nextValue);
168
+ function updateChildrenListItemValue(list, children) {
169
+ const childrenOrExisting = children || list.getChildren();
170
+ if (void 0 !== childrenOrExisting) for(let i = 0; i < childrenOrExisting.length; i++){
171
+ const child = childrenOrExisting[i];
172
+ if ($isListItemNode(child)) {
173
+ const prevValue = child.getValue();
174
+ const nextValue = $getListItemValue(child);
175
+ if (prevValue !== nextValue) child.setValue(nextValue);
206
176
  }
207
- }
208
177
  }
209
- }
210
178
  }
211
- export function $handleIndent(WebinyListItemNodes) {
212
- // go through each node and decide where to move it.
213
- const removed = new Set();
214
- WebinyListItemNodes.forEach(WebinyListItemNode => {
215
- if (isNestedListNode(WebinyListItemNode) || removed.has(WebinyListItemNode.getKey())) {
216
- return;
217
- }
218
- const parent = WebinyListItemNode.getParent();
219
-
220
- // We can cast both of the below `isNestedListNode` only returns a boolean type instead of a user-defined type guards
221
- const nextSibling = WebinyListItemNode.getNextSibling();
222
- const previousSibling = WebinyListItemNode.getPreviousSibling();
223
- // if there are nested lists on either side, merge them all together.
224
-
225
- if (isNestedListNode(nextSibling) && isNestedListNode(previousSibling)) {
226
- const innerList = previousSibling.getFirstChild();
227
- if ($isListNode(innerList)) {
228
- innerList.append(WebinyListItemNode);
229
- const nextInnerList = nextSibling.getFirstChild();
230
- if ($isListNode(nextInnerList)) {
231
- const children = nextInnerList.getChildren();
232
- append(innerList, children);
233
- nextSibling.remove();
234
- removed.add(nextSibling.getKey());
235
- }
236
- updateChildrenListItemValue(innerList);
237
- }
238
- } else if (isNestedListNode(nextSibling)) {
239
- // if the WebinyListItemNode is next to a nested ListNode, merge them
240
- const innerList = nextSibling.getFirstChild();
241
- if ($isListNode(innerList)) {
242
- const firstChild = innerList.getFirstChild();
243
- if (firstChild !== null) {
244
- firstChild.insertBefore(WebinyListItemNode);
245
- }
246
- updateChildrenListItemValue(innerList);
247
- }
248
- } else if (isNestedListNode(previousSibling)) {
249
- const innerList = previousSibling.getFirstChild();
250
- if ($isListNode(innerList)) {
251
- innerList.append(WebinyListItemNode);
252
- updateChildrenListItemValue(innerList);
253
- }
254
- } else {
255
- // otherwise, we need to create a new nested ListNode
256
-
257
- if ($isListNode(parent)) {
258
- const newListItem = $createListItemNode();
259
- const newList = $createListNode(parent.getListType(), parent.getStyleId());
260
- newListItem.append(newList);
261
- newList.append(WebinyListItemNode);
262
- if (previousSibling) {
263
- previousSibling.insertAfter(newListItem);
264
- } else if (nextSibling) {
265
- nextSibling.insertBefore(newListItem);
266
- } else {
267
- parent.append(newListItem);
179
+ function $handleIndent(WebinyListItemNodes) {
180
+ const removed = new Set();
181
+ WebinyListItemNodes.forEach((WebinyListItemNode)=>{
182
+ if (isNestedListNode(WebinyListItemNode) || removed.has(WebinyListItemNode.getKey())) return;
183
+ const parent = WebinyListItemNode.getParent();
184
+ const nextSibling = WebinyListItemNode.getNextSibling();
185
+ const previousSibling = WebinyListItemNode.getPreviousSibling();
186
+ if (isNestedListNode(nextSibling) && isNestedListNode(previousSibling)) {
187
+ const innerList = previousSibling.getFirstChild();
188
+ if ($isListNode(innerList)) {
189
+ innerList.append(WebinyListItemNode);
190
+ const nextInnerList = nextSibling.getFirstChild();
191
+ if ($isListNode(nextInnerList)) {
192
+ const children = nextInnerList.getChildren();
193
+ append(innerList, children);
194
+ nextSibling.remove();
195
+ removed.add(nextSibling.getKey());
196
+ }
197
+ updateChildrenListItemValue(innerList);
198
+ }
199
+ } else if (isNestedListNode(nextSibling)) {
200
+ const innerList = nextSibling.getFirstChild();
201
+ if ($isListNode(innerList)) {
202
+ const firstChild = innerList.getFirstChild();
203
+ if (null !== firstChild) firstChild.insertBefore(WebinyListItemNode);
204
+ updateChildrenListItemValue(innerList);
205
+ }
206
+ } else if (isNestedListNode(previousSibling)) {
207
+ const innerList = previousSibling.getFirstChild();
208
+ if ($isListNode(innerList)) {
209
+ innerList.append(WebinyListItemNode);
210
+ updateChildrenListItemValue(innerList);
211
+ }
212
+ } else if ($isListNode(parent)) {
213
+ const newListItem = $createListItemNode();
214
+ const newList = $createListNode(parent.getListType(), parent.getStyleId());
215
+ newListItem.append(newList);
216
+ newList.append(WebinyListItemNode);
217
+ if (previousSibling) previousSibling.insertAfter(newListItem);
218
+ else if (nextSibling) nextSibling.insertBefore(newListItem);
219
+ else parent.append(newListItem);
268
220
  }
269
- }
270
- }
271
- if ($isListNode(parent)) {
272
- updateChildrenListItemValue(parent);
273
- }
274
- });
221
+ if ($isListNode(parent)) updateChildrenListItemValue(parent);
222
+ });
275
223
  }
276
- export function $handleOutdent(WebinyListItemNodes) {
277
- // go through each node and decide where to move it.
278
-
279
- WebinyListItemNodes.forEach(WebinyListItemNode => {
280
- if (isNestedListNode(WebinyListItemNode)) {
281
- return;
282
- }
283
- const parentList = WebinyListItemNode.getParent();
284
- const grandparentListItem = parentList ? parentList.getParent() : undefined;
285
- const greatGrandparentList = grandparentListItem ? grandparentListItem.getParent() : undefined;
286
- // If it doesn't have these ancestors, it's not indented.
287
-
288
- if ($isListNode(greatGrandparentList) && $isListItemNode(grandparentListItem) && $isListNode(parentList)) {
289
- // if it's the first child in it's parent list, insert it into the
290
- // great grandparent list before the grandparent
291
- const firstChild = parentList ? parentList.getFirstChild() : undefined;
292
- const lastChild = parentList ? parentList.getLastChild() : undefined;
293
- if (WebinyListItemNode.is(firstChild)) {
294
- grandparentListItem.insertBefore(WebinyListItemNode);
295
- if (parentList.isEmpty()) {
296
- grandparentListItem.remove();
297
- }
298
- // if it's the last child in it's parent list, insert it into the
299
- // great grandparent list after the grandparent.
300
- } else if (WebinyListItemNode.is(lastChild)) {
301
- grandparentListItem.insertAfter(WebinyListItemNode);
302
- if (parentList.isEmpty()) {
303
- grandparentListItem.remove();
224
+ function $handleOutdent(WebinyListItemNodes) {
225
+ WebinyListItemNodes.forEach((WebinyListItemNode)=>{
226
+ if (isNestedListNode(WebinyListItemNode)) return;
227
+ const parentList = WebinyListItemNode.getParent();
228
+ const grandparentListItem = parentList ? parentList.getParent() : void 0;
229
+ const greatGrandparentList = grandparentListItem ? grandparentListItem.getParent() : void 0;
230
+ if ($isListNode(greatGrandparentList) && $isListItemNode(grandparentListItem) && $isListNode(parentList)) {
231
+ const firstChild = parentList ? parentList.getFirstChild() : void 0;
232
+ const lastChild = parentList ? parentList.getLastChild() : void 0;
233
+ if (WebinyListItemNode.is(firstChild)) {
234
+ grandparentListItem.insertBefore(WebinyListItemNode);
235
+ if (parentList.isEmpty()) grandparentListItem.remove();
236
+ } else if (WebinyListItemNode.is(lastChild)) {
237
+ grandparentListItem.insertAfter(WebinyListItemNode);
238
+ if (parentList.isEmpty()) grandparentListItem.remove();
239
+ } else {
240
+ const listType = parentList.getListType();
241
+ const themeStyleId = parentList.getStyleId();
242
+ const previousSiblingsListItem = $createListItemNode();
243
+ const previousSiblingsList = $createListNode(listType, themeStyleId);
244
+ previousSiblingsListItem.append(previousSiblingsList);
245
+ WebinyListItemNode.getPreviousSiblings().forEach((sibling)=>previousSiblingsList.append(sibling));
246
+ const nextSiblingsListItem = $createListItemNode();
247
+ const nextSiblingsList = $createListNode(listType, themeStyleId);
248
+ nextSiblingsListItem.append(nextSiblingsList);
249
+ append(nextSiblingsList, WebinyListItemNode.getNextSiblings());
250
+ grandparentListItem.insertBefore(previousSiblingsListItem);
251
+ grandparentListItem.insertAfter(nextSiblingsListItem);
252
+ grandparentListItem.replace(WebinyListItemNode);
253
+ }
254
+ updateChildrenListItemValue(parentList);
255
+ updateChildrenListItemValue(greatGrandparentList);
304
256
  }
305
- } else {
306
- // otherwise, we need to split the siblings into two new nested lists
307
- const listType = parentList.getListType();
308
- const themeStyleId = parentList.getStyleId();
309
- const previousSiblingsListItem = $createListItemNode();
310
- const previousSiblingsList = $createListNode(listType, themeStyleId);
311
- previousSiblingsListItem.append(previousSiblingsList);
312
- WebinyListItemNode.getPreviousSiblings().forEach(sibling => previousSiblingsList.append(sibling));
313
- const nextSiblingsListItem = $createListItemNode();
314
- const nextSiblingsList = $createListNode(listType, themeStyleId);
315
- nextSiblingsListItem.append(nextSiblingsList);
316
- append(nextSiblingsList, WebinyListItemNode.getNextSiblings());
317
- // put the sibling nested lists on either side of the grandparent list item in the great grandparent.
318
- grandparentListItem.insertBefore(previousSiblingsListItem);
319
- grandparentListItem.insertAfter(nextSiblingsListItem);
320
- // replace the grandparent list item (now between the siblings) with the outdented list item.
321
- grandparentListItem.replace(WebinyListItemNode);
322
- }
323
- updateChildrenListItemValue(parentList);
324
- updateChildrenListItemValue(greatGrandparentList);
325
- }
326
- });
257
+ });
327
258
  }
328
259
  function maybeIndentOrOutdent(direction) {
329
- const selection = $getSelection();
330
- if (!$isRangeSelection(selection)) {
331
- return;
332
- }
333
- const selectedNodes = selection.getNodes();
334
- let webinyListItemNodes = [];
335
- if (selectedNodes.length === 0) {
336
- selectedNodes.push(selection.anchor.getNode());
337
- }
338
- if (selectedNodes.length === 1) {
339
- // Only 1 node selected. Selection may not contain the ListNodeItem so we traverse the tree to
340
- // find whether this is part of a WebinyListItemNode
341
- const nearestWebinyListItemNode = findNearestListItemNode(selectedNodes[0]);
342
- if (nearestWebinyListItemNode !== null) {
343
- webinyListItemNodes = [nearestWebinyListItemNode];
344
- }
345
- } else {
346
- webinyListItemNodes = getUniqueListItemNodes(selectedNodes);
347
- }
348
- if (webinyListItemNodes.length > 0) {
349
- if (direction === "indent") {
350
- $handleIndent(webinyListItemNodes);
351
- } else {
352
- $handleOutdent(webinyListItemNodes);
353
- }
354
- }
260
+ const selection = $getSelection();
261
+ if (!$isRangeSelection(selection)) return;
262
+ const selectedNodes = selection.getNodes();
263
+ let webinyListItemNodes = [];
264
+ if (0 === selectedNodes.length) selectedNodes.push(selection.anchor.getNode());
265
+ if (1 === selectedNodes.length) {
266
+ const nearestWebinyListItemNode = findNearestListItemNode(selectedNodes[0]);
267
+ if (null !== nearestWebinyListItemNode) webinyListItemNodes = [
268
+ nearestWebinyListItemNode
269
+ ];
270
+ } else webinyListItemNodes = getUniqueListItemNodes(selectedNodes);
271
+ if (webinyListItemNodes.length > 0) if ("indent" === direction) $handleIndent(webinyListItemNodes);
272
+ else $handleOutdent(webinyListItemNodes);
355
273
  }
356
- export function indentList() {
357
- maybeIndentOrOutdent("indent");
274
+ function indentList() {
275
+ maybeIndentOrOutdent("indent");
358
276
  }
359
- export function outdentList() {
360
- maybeIndentOrOutdent("outdent");
277
+ function outdentList() {
278
+ maybeIndentOrOutdent("outdent");
361
279
  }
362
- export function $handleListInsertParagraph() {
363
- const selection = $getSelection();
364
- if (!$isRangeSelection(selection) || !selection.isCollapsed()) {
365
- return false;
366
- }
367
-
368
- // Only run this code on empty list items
369
- const anchor = selection.anchor.getNode();
370
- if (!$isListItemNode(anchor) || anchor.getTextContent() !== "") {
371
- return false;
372
- }
373
- const topListNode = $getTopListNode(anchor);
374
- const parent = anchor.getParent();
375
- if (!$isListNode(parent)) {
376
- console.log("A WebinyListItemNode must have a WebinyListNode for a parent.");
377
- return false;
378
- }
379
- const grandparent = parent?.getParent() || null;
380
- let replacementNode;
381
- if ($isRootOrShadowRoot(grandparent)) {
382
- replacementNode = $createParagraphNode();
383
- topListNode.insertAfter(replacementNode);
384
- } else if ($isListItemNode(grandparent)) {
385
- replacementNode = $createListItemNode();
386
- grandparent.insertAfter(replacementNode);
387
- } else {
388
- return false;
389
- }
390
- replacementNode.select();
391
- const nextSiblings = anchor.getNextSiblings();
392
- if (nextSiblings.length > 0) {
393
- const newList = $createListNode(parent?.getListType(), parent?.getStyleId());
394
- if ($isParagraphNode(replacementNode)) {
395
- replacementNode.insertAfter(newList);
280
+ function $handleListInsertParagraph() {
281
+ const selection = $getSelection();
282
+ if (!$isRangeSelection(selection) || !selection.isCollapsed()) return false;
283
+ const anchor = selection.anchor.getNode();
284
+ if (!$isListItemNode(anchor) || "" !== anchor.getTextContent()) return false;
285
+ const topListNode = $getTopListNode(anchor);
286
+ const parent = anchor.getParent();
287
+ if (!$isListNode(parent)) {
288
+ console.log("A WebinyListItemNode must have a WebinyListNode for a parent.");
289
+ return false;
290
+ }
291
+ const grandparent = parent?.getParent() || null;
292
+ let replacementNode;
293
+ if ($isRootOrShadowRoot(grandparent)) {
294
+ replacementNode = $createParagraphNode();
295
+ topListNode.insertAfter(replacementNode);
396
296
  } else {
397
- const newListItem = $createListItemNode();
398
- newListItem.append(newList);
399
- replacementNode.insertAfter(newListItem);
297
+ if (!$isListItemNode(grandparent)) return false;
298
+ replacementNode = $createListItemNode();
299
+ grandparent.insertAfter(replacementNode);
400
300
  }
401
- nextSiblings.forEach(sibling => {
402
- sibling.remove();
403
- newList.append(sibling);
404
- });
405
- }
406
-
407
- // Don't leave hanging nested empty lists
408
- $removeHighestEmptyListParent(anchor);
409
- return true;
301
+ replacementNode.select();
302
+ const nextSiblings = anchor.getNextSiblings();
303
+ if (nextSiblings.length > 0) {
304
+ const newList = $createListNode(parent?.getListType(), parent?.getStyleId());
305
+ if ($isParagraphNode(replacementNode)) replacementNode.insertAfter(newList);
306
+ else {
307
+ const newListItem = $createListItemNode();
308
+ newListItem.append(newList);
309
+ replacementNode.insertAfter(newListItem);
310
+ }
311
+ nextSiblings.forEach((sibling)=>{
312
+ sibling.remove();
313
+ newList.append(sibling);
314
+ });
315
+ }
316
+ $removeHighestEmptyListParent(anchor);
317
+ return true;
410
318
  }
319
+ export { $handleIndent, $handleListInsertParagraph, $handleOutdent, indentList, insertList, mergeLists, outdentList, removeList, updateChildrenListItemValue };
411
320
 
412
321
  //# sourceMappingURL=formatList.js.map