@lexical/table 0.8.1 → 0.9.1

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.
@@ -22,18 +22,22 @@ const TableCellHeaderStates = {
22
22
  NO_STATUS: 0,
23
23
  ROW: 1
24
24
  };
25
+
25
26
  /** @noInheritDoc */
26
27
  class TableCellNode extends lexical.DEPRECATED_GridCellNode {
27
28
  /** @internal */
28
29
 
29
30
  /** @internal */
30
-
31
31
  static getType() {
32
32
  return 'tablecell';
33
33
  }
34
+
34
35
  static clone(node) {
35
- return new TableCellNode(node.__headerState, node.__colSpan, node.__width, node.__key);
36
+ const tableNode = new TableCellNode(node.__headerState, node.__colSpan, node.__width, node.__key);
37
+ tableNode.__rowSpan = node.__rowSpan;
38
+ return tableNode;
36
39
  }
40
+
37
41
  static importDOM() {
38
42
  return {
39
43
  td: node => ({
@@ -46,113 +50,162 @@ class TableCellNode extends lexical.DEPRECATED_GridCellNode {
46
50
  })
47
51
  };
48
52
  }
53
+
49
54
  static importJSON(serializedNode) {
50
55
  return $createTableCellNode(serializedNode.headerState, serializedNode.colSpan, serializedNode.width || undefined);
51
56
  }
57
+
52
58
  constructor(headerState = TableCellHeaderStates.NO_STATUS, colSpan = 1, width, key) {
53
59
  super(colSpan, key);
54
60
  this.__headerState = headerState;
55
61
  this.__width = width;
56
62
  }
63
+
57
64
  createDOM(config) {
58
65
  const element = document.createElement(this.getTag());
66
+
59
67
  if (this.__width) {
60
68
  element.style.width = `${this.__width}px`;
61
69
  }
70
+
71
+ if (this.__colSpan !== 1) {
72
+ element.colSpan = this.__colSpan;
73
+ }
74
+
75
+ if (this.__rowSpan !== 1) {
76
+ element.rowSpan = this.__rowSpan;
77
+ }
78
+
62
79
  utils.addClassNamesToElement(element, config.theme.tableCell, this.hasHeader() && config.theme.tableCellHeader);
63
80
  return element;
64
81
  }
82
+
65
83
  exportDOM(editor) {
66
84
  const {
67
85
  element
68
86
  } = super.exportDOM(editor);
87
+
69
88
  if (element) {
89
+ const element_ = element;
70
90
  const maxWidth = 700;
71
91
  const colCount = this.getParentOrThrow().getChildrenSize();
72
- element.style.border = '1px solid black';
73
- element.style.width = `${this.getWidth() || Math.max(90, maxWidth / colCount)}px`;
74
- element.style.verticalAlign = 'top';
75
- element.style.textAlign = 'start';
92
+ element_.style.border = '1px solid black';
93
+
94
+ if (this.__colSpan !== 1) {
95
+ element_.colSpan = this.__colSpan;
96
+ }
97
+
98
+ if (this.__rowSpan !== 1) {
99
+ element_.rowSpan = this.__rowSpan;
100
+ }
101
+
102
+ element_.style.width = `${this.getWidth() || Math.max(90, maxWidth / colCount)}px`;
103
+ element_.style.verticalAlign = 'top';
104
+ element_.style.textAlign = 'start';
105
+
76
106
  if (this.hasHeader()) {
77
- element.style.backgroundColor = '#f2f3f5';
107
+ element_.style.backgroundColor = '#f2f3f5';
78
108
  }
79
109
  }
110
+
80
111
  return {
81
112
  element
82
113
  };
83
114
  }
115
+
84
116
  exportJSON() {
85
- return {
86
- ...super.exportJSON(),
117
+ return { ...super.exportJSON(),
87
118
  colSpan: super.__colSpan,
88
119
  headerState: this.__headerState,
89
120
  type: 'tablecell',
90
121
  width: this.getWidth()
91
122
  };
92
123
  }
124
+
93
125
  getTag() {
94
126
  return this.hasHeader() ? 'th' : 'td';
95
127
  }
128
+
96
129
  setHeaderStyles(headerState) {
97
130
  const self = this.getWritable();
98
131
  self.__headerState = headerState;
99
132
  return this.__headerState;
100
133
  }
134
+
101
135
  getHeaderStyles() {
102
136
  return this.getLatest().__headerState;
103
137
  }
138
+
104
139
  setWidth(width) {
105
140
  const self = this.getWritable();
106
141
  self.__width = width;
107
142
  return this.__width;
108
143
  }
144
+
109
145
  getWidth() {
110
146
  return this.getLatest().__width;
111
147
  }
148
+
112
149
  toggleHeaderStyle(headerStateToToggle) {
113
150
  const self = this.getWritable();
151
+
114
152
  if ((self.__headerState & headerStateToToggle) === headerStateToToggle) {
115
153
  self.__headerState -= headerStateToToggle;
116
154
  } else {
117
155
  self.__headerState += headerStateToToggle;
118
156
  }
157
+
119
158
  return self;
120
159
  }
160
+
121
161
  hasHeaderState(headerState) {
122
162
  return (this.getHeaderStyles() & headerState) === headerState;
123
163
  }
164
+
124
165
  hasHeader() {
125
166
  return this.getLatest().__headerState !== TableCellHeaderStates.NO_STATUS;
126
167
  }
168
+
127
169
  updateDOM(prevNode) {
128
- return prevNode.__headerState !== this.__headerState || prevNode.__width !== this.__width;
170
+ return prevNode.__headerState !== this.__headerState || prevNode.__width !== this.__width || prevNode.__colSpan !== this.__colSpan || prevNode.__rowSpan !== this.__rowSpan;
129
171
  }
172
+
130
173
  isShadowRoot() {
131
174
  return true;
132
175
  }
176
+
133
177
  collapseAtStart() {
134
178
  return true;
135
179
  }
180
+
136
181
  canBeEmpty() {
137
182
  return false;
138
183
  }
184
+
139
185
  canIndent() {
140
186
  return false;
141
187
  }
188
+
142
189
  }
143
190
  function convertTableCellNodeElement(domNode) {
191
+ const domNode_ = domNode;
144
192
  const nodeName = domNode.nodeName.toLowerCase();
145
193
  const tableCellNode = $createTableCellNode(nodeName === 'th' ? TableCellHeaderStates.ROW : TableCellHeaderStates.NO_STATUS);
194
+ tableCellNode.__colSpan = domNode_.colSpan;
195
+ tableCellNode.__rowSpan = domNode_.rowSpan;
146
196
  return {
147
197
  forChild: (lexicalNode, parentLexicalNode) => {
148
198
  if ($isTableCellNode(parentLexicalNode) && !lexical.$isElementNode(lexicalNode)) {
149
199
  const paragraphNode = lexical.$createParagraphNode();
200
+
150
201
  if (lexical.$isLineBreakNode(lexicalNode) && lexicalNode.getTextContent() === '\n') {
151
202
  return null;
152
203
  }
204
+
153
205
  paragraphNode.append(lexicalNode);
154
206
  return paragraphNode;
155
207
  }
208
+
156
209
  return lexicalNode;
157
210
  },
158
211
  node: tableCellNode
@@ -172,16 +225,18 @@ function $isTableCellNode(node) {
172
225
  * LICENSE file in the root directory of this source tree.
173
226
  *
174
227
  */
228
+
175
229
  /** @noInheritDoc */
176
230
  class TableRowNode extends lexical.DEPRECATED_GridRowNode {
177
231
  /** @internal */
178
-
179
232
  static getType() {
180
233
  return 'tablerow';
181
234
  }
235
+
182
236
  static clone(node) {
183
237
  return new TableRowNode(node.__height, node.__key);
184
238
  }
239
+
185
240
  static importDOM() {
186
241
  return {
187
242
  tr: node => ({
@@ -190,48 +245,60 @@ class TableRowNode extends lexical.DEPRECATED_GridRowNode {
190
245
  })
191
246
  };
192
247
  }
248
+
193
249
  static importJSON(serializedNode) {
194
250
  return $createTableRowNode(serializedNode.height);
195
251
  }
252
+
196
253
  constructor(height, key) {
197
254
  super(key);
198
255
  this.__height = height;
199
256
  }
257
+
200
258
  exportJSON() {
201
- return {
202
- ...super.exportJSON(),
259
+ return { ...super.exportJSON(),
203
260
  type: 'tablerow',
204
261
  version: 1
205
262
  };
206
263
  }
264
+
207
265
  createDOM(config) {
208
266
  const element = document.createElement('tr');
267
+
209
268
  if (this.__height) {
210
269
  element.style.height = `${this.__height}px`;
211
270
  }
271
+
212
272
  utils.addClassNamesToElement(element, config.theme.tableRow);
213
273
  return element;
214
274
  }
275
+
215
276
  isShadowRoot() {
216
277
  return true;
217
278
  }
279
+
218
280
  setHeight(height) {
219
281
  const self = this.getWritable();
220
282
  self.__height = height;
221
283
  return this.__height;
222
284
  }
285
+
223
286
  getHeight() {
224
287
  return this.getLatest().__height;
225
288
  }
289
+
226
290
  updateDOM(prevNode) {
227
291
  return prevNode.__height !== this.__height;
228
292
  }
293
+
229
294
  canBeEmpty() {
230
295
  return false;
231
296
  }
297
+
232
298
  canIndent() {
233
299
  return false;
234
300
  }
301
+
235
302
  }
236
303
  function convertTableRowElement(domNode) {
237
304
  return {
@@ -252,7 +319,6 @@ function $isTableRowNode(node) {
252
319
  * LICENSE file in the root directory of this source tree.
253
320
  *
254
321
  */
255
-
256
322
  const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
257
323
 
258
324
  /**
@@ -262,7 +328,9 @@ const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !==
262
328
  * LICENSE file in the root directory of this source tree.
263
329
  *
264
330
  */
331
+
265
332
  const getDOMSelection = targetWindow => CAN_USE_DOM ? (targetWindow || window).getSelection() : null;
333
+
266
334
  class TableSelection {
267
335
  constructor(editor, tableNodeKey) {
268
336
  this.isHighlightingCells = false;
@@ -286,40 +354,51 @@ class TableSelection {
286
354
  this.hasHijackedSelectionStyles = false;
287
355
  this.trackTableGrid();
288
356
  }
357
+
289
358
  getGrid() {
290
359
  return this.grid;
291
360
  }
361
+
292
362
  removeListeners() {
293
363
  Array.from(this.listenersToRemove).forEach(removeListener => removeListener());
294
364
  }
365
+
295
366
  trackTableGrid() {
296
367
  const observer = new MutationObserver(records => {
297
368
  this.editor.update(() => {
298
369
  let gridNeedsRedraw = false;
370
+
299
371
  for (let i = 0; i < records.length; i++) {
300
372
  const record = records[i];
301
373
  const target = record.target;
302
374
  const nodeName = target.nodeName;
375
+
303
376
  if (nodeName === 'TABLE' || nodeName === 'TR') {
304
377
  gridNeedsRedraw = true;
305
378
  break;
306
379
  }
307
380
  }
381
+
308
382
  if (!gridNeedsRedraw) {
309
383
  return;
310
384
  }
385
+
311
386
  const tableElement = this.editor.getElementByKey(this.tableNodeKey);
387
+
312
388
  if (!tableElement) {
313
389
  throw new Error('Expected to find TableElement in DOM');
314
390
  }
391
+
315
392
  this.grid = getTableGrid(tableElement);
316
393
  });
317
394
  });
318
395
  this.editor.update(() => {
319
396
  const tableElement = this.editor.getElementByKey(this.tableNodeKey);
397
+
320
398
  if (!tableElement) {
321
399
  throw new Error('Expected to find TableElement in DOM');
322
400
  }
401
+
323
402
  this.grid = getTableGrid(tableElement);
324
403
  observer.observe(tableElement, {
325
404
  childList: true,
@@ -327,6 +406,7 @@ class TableSelection {
327
406
  });
328
407
  });
329
408
  }
409
+
330
410
  clearHighlight() {
331
411
  this.isHighlightingCells = false;
332
412
  this.anchorX = -1;
@@ -342,39 +422,50 @@ class TableSelection {
342
422
  this.enableHighlightStyle();
343
423
  this.editor.update(() => {
344
424
  const tableNode = lexical.$getNodeByKey(this.tableNodeKey);
425
+
345
426
  if (!$isTableNode(tableNode)) {
346
427
  throw new Error('Expected TableNode.');
347
428
  }
429
+
348
430
  const tableElement = this.editor.getElementByKey(this.tableNodeKey);
431
+
349
432
  if (!tableElement) {
350
433
  throw new Error('Expected to find TableElement in DOM');
351
434
  }
435
+
352
436
  const grid = getTableGrid(tableElement);
353
437
  $updateDOMForSelection(grid, null);
354
438
  lexical.$setSelection(null);
355
439
  this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND, undefined);
356
440
  });
357
441
  }
442
+
358
443
  enableHighlightStyle() {
359
444
  this.editor.update(() => {
360
445
  const tableElement = this.editor.getElementByKey(this.tableNodeKey);
446
+
361
447
  if (!tableElement) {
362
448
  throw new Error('Expected to find TableElement in DOM');
363
449
  }
450
+
364
451
  tableElement.classList.remove('disable-selection');
365
452
  this.hasHijackedSelectionStyles = false;
366
453
  });
367
454
  }
455
+
368
456
  disableHighlightStyle() {
369
457
  this.editor.update(() => {
370
458
  const tableElement = this.editor.getElementByKey(this.tableNodeKey);
459
+
371
460
  if (!tableElement) {
372
461
  throw new Error('Expected to find TableElement in DOM');
373
462
  }
463
+
374
464
  tableElement.classList.add('disable-selection');
375
465
  this.hasHijackedSelectionStyles = true;
376
466
  });
377
467
  }
468
+
378
469
  updateTableGridSelection(selection) {
379
470
  if (selection != null && selection.gridKey === this.tableNodeKey) {
380
471
  this.gridSelection = selection;
@@ -385,36 +476,46 @@ class TableSelection {
385
476
  this.clearHighlight();
386
477
  }
387
478
  }
479
+
388
480
  setFocusCellForSelection(cell, ignoreStart = false) {
389
481
  this.editor.update(() => {
390
482
  const tableNode = lexical.$getNodeByKey(this.tableNodeKey);
483
+
391
484
  if (!$isTableNode(tableNode)) {
392
485
  throw new Error('Expected TableNode.');
393
486
  }
487
+
394
488
  const tableElement = this.editor.getElementByKey(this.tableNodeKey);
489
+
395
490
  if (!tableElement) {
396
491
  throw new Error('Expected to find TableElement in DOM');
397
492
  }
493
+
398
494
  const cellX = cell.x;
399
495
  const cellY = cell.y;
400
496
  this.focusCell = cell;
497
+
401
498
  if (this.anchorCell !== null) {
402
- const domSelection = getDOMSelection(this.editor._window);
403
- // Collapse the selection
499
+ const domSelection = getDOMSelection(this.editor._window); // Collapse the selection
500
+
404
501
  if (domSelection) {
405
502
  domSelection.setBaseAndExtent(this.anchorCell.elem, 0, this.focusCell.elem, 0);
406
503
  }
407
504
  }
505
+
408
506
  if (!this.isHighlightingCells && (this.anchorX !== cellX || this.anchorY !== cellY || ignoreStart)) {
409
507
  this.isHighlightingCells = true;
410
508
  this.disableHighlightStyle();
411
509
  } else if (cellX === this.focusX && cellY === this.focusY) {
412
510
  return;
413
511
  }
512
+
414
513
  this.focusX = cellX;
415
514
  this.focusY = cellY;
515
+
416
516
  if (this.isHighlightingCells) {
417
517
  const focusTableCellNode = lexical.$getNearestNodeFromDOMNode(cell.elem);
518
+
418
519
  if (this.gridSelection != null && this.anchorCellNodeKey != null && $isTableCellNode(focusTableCellNode)) {
419
520
  const focusNodeKey = focusTableCellNode.getKey();
420
521
  this.gridSelection = this.gridSelection.clone() || lexical.DEPRECATED_$createGridSelection();
@@ -427,6 +528,7 @@ class TableSelection {
427
528
  }
428
529
  });
429
530
  }
531
+
430
532
  setAnchorCellForSelection(cell) {
431
533
  this.isHighlightingCells = false;
432
534
  this.anchorCell = cell;
@@ -434,6 +536,7 @@ class TableSelection {
434
536
  this.anchorY = cell.y;
435
537
  this.editor.update(() => {
436
538
  const anchorTableCellNode = lexical.$getNearestNodeFromDOMNode(cell.elem);
539
+
437
540
  if ($isTableCellNode(anchorTableCellNode)) {
438
541
  const anchorNodeKey = anchorTableCellNode.getKey();
439
542
  this.gridSelection = lexical.DEPRECATED_$createGridSelection();
@@ -441,14 +544,17 @@ class TableSelection {
441
544
  }
442
545
  });
443
546
  }
547
+
444
548
  formatCells(type) {
445
549
  this.editor.update(() => {
446
550
  const selection = lexical.$getSelection();
551
+
447
552
  if (!lexical.DEPRECATED_$isGridSelection(selection)) {
448
553
  {
449
554
  throw Error(`Expected grid selection`);
450
555
  }
451
556
  }
557
+
452
558
  const formatSelection = lexical.$createRangeSelection();
453
559
  const anchor = formatSelection.anchor;
454
560
  const focus = formatSelection.focus;
@@ -463,27 +569,34 @@ class TableSelection {
463
569
  this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND, undefined);
464
570
  });
465
571
  }
572
+
466
573
  clearText() {
467
574
  this.editor.update(() => {
468
575
  const tableNode = lexical.$getNodeByKey(this.tableNodeKey);
576
+
469
577
  if (!$isTableNode(tableNode)) {
470
578
  throw new Error('Expected TableNode.');
471
579
  }
580
+
472
581
  const selection = lexical.$getSelection();
582
+
473
583
  if (!lexical.DEPRECATED_$isGridSelection(selection)) {
474
584
  {
475
585
  throw Error(`Expected grid selection`);
476
586
  }
477
587
  }
588
+
478
589
  const selectedNodes = selection.getNodes().filter($isTableCellNode);
590
+
479
591
  if (selectedNodes.length === this.grid.columns * this.grid.rows) {
480
- tableNode.selectPrevious();
481
- // Delete entire table
592
+ tableNode.selectPrevious(); // Delete entire table
593
+
482
594
  tableNode.remove();
483
595
  const rootNode = lexical.$getRoot();
484
596
  rootNode.selectStart();
485
597
  return;
486
598
  }
599
+
487
600
  selectedNodes.forEach(cellNode => {
488
601
  if (lexical.$isElementNode(cellNode)) {
489
602
  const paragraphNode = lexical.$createParagraphNode();
@@ -502,6 +615,7 @@ class TableSelection {
502
615
  this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND, undefined);
503
616
  });
504
617
  }
618
+
505
619
  }
506
620
 
507
621
  /**
@@ -514,15 +628,18 @@ class TableSelection {
514
628
  const LEXICAL_ELEMENT_KEY = '__lexicalTableSelection';
515
629
  function applyTableHandlers(tableNode, tableElement, editor) {
516
630
  const rootElement = editor.getRootElement();
631
+
517
632
  if (rootElement === null) {
518
633
  throw new Error('No root element.');
519
634
  }
635
+
520
636
  const tableSelection = new TableSelection(editor, tableNode.getKey());
521
637
  attachTableSelectionToTableElement(tableElement, tableSelection);
522
638
  let isMouseDown = false;
523
639
  let isRangeSelectionHijacked = false;
524
640
  tableElement.addEventListener('dblclick', event => {
525
641
  const cell = getCellFromTarget(event.target);
642
+
526
643
  if (cell !== null) {
527
644
  event.preventDefault();
528
645
  event.stopImmediatePropagation();
@@ -531,15 +648,16 @@ function applyTableHandlers(tableNode, tableElement, editor) {
531
648
  tableSelection.setFocusCellForSelection(cell, true);
532
649
  isMouseDown = false;
533
650
  }
534
- });
651
+ }); // This is the anchor of the selection.
535
652
 
536
- // This is the anchor of the selection.
537
653
  tableElement.addEventListener('mousedown', event => {
538
654
  setTimeout(() => {
539
655
  if (event.button !== 0) {
540
656
  return;
541
657
  }
658
+
542
659
  const cell = getCellFromTarget(event.target);
660
+
543
661
  if (cell !== null) {
544
662
  event.preventDefault();
545
663
  event.stopPropagation();
@@ -547,268 +665,335 @@ function applyTableHandlers(tableNode, tableElement, editor) {
547
665
  tableSelection.setAnchorCellForSelection(cell);
548
666
  }
549
667
  }, 0);
550
- });
668
+ }); // This is adjusting the focus of the selection.
551
669
 
552
- // This is adjusting the focus of the selection.
553
670
  tableElement.addEventListener('mousemove', event => {
554
671
  if (isRangeSelectionHijacked) {
555
672
  event.preventDefault();
556
673
  event.stopPropagation();
557
674
  event.stopImmediatePropagation();
558
675
  }
676
+
559
677
  if (isMouseDown) {
560
678
  const cell = getCellFromTarget(event.target);
679
+
561
680
  if (cell !== null) {
562
681
  const cellX = cell.x;
563
682
  const cellY = cell.y;
683
+
564
684
  if (isMouseDown && (tableSelection.anchorX !== cellX || tableSelection.anchorY !== cellY || tableSelection.isHighlightingCells)) {
565
685
  event.preventDefault();
566
686
  tableSelection.setFocusCellForSelection(cell);
567
687
  }
568
688
  }
569
689
  }
570
- });
690
+ }); // Select entire table at this point, when grid selection is ready.
571
691
 
572
- // Select entire table at this point, when grid selection is ready.
573
692
  tableElement.addEventListener('mouseleave', () => {
574
693
  if (isMouseDown) {
575
694
  return;
576
695
  }
577
- });
696
+ }); // Clear selection when clicking outside of dom.
578
697
 
579
- // Clear selection when clicking outside of dom.
580
698
  const mouseDownCallback = event => {
581
- isMouseDown = true;
582
699
  if (event.button !== 0) {
583
700
  return;
584
701
  }
702
+
585
703
  editor.update(() => {
586
704
  const selection = lexical.$getSelection();
587
- if (lexical.DEPRECATED_$isGridSelection(selection) && selection.gridKey === tableSelection.tableNodeKey && rootElement.contains(event.target)) {
588
- return tableSelection.clearHighlight();
705
+ const target = event.target;
706
+
707
+ if (target instanceof Node) {
708
+ if (lexical.DEPRECATED_$isGridSelection(selection) && selection.gridKey === tableSelection.tableNodeKey && rootElement.contains(target)) {
709
+ return tableSelection.clearHighlight();
710
+ } // TODO Revise this logic; the UX selection boundaries and nested editors
711
+
712
+
713
+ const node = lexical.$getNearestNodeFromDOMNode(target);
714
+
715
+ if (node !== null && utils.$findMatchingParent(node, lexical.DEPRECATED_$isGridNode)) {
716
+ isMouseDown = true;
717
+ }
589
718
  }
590
719
  });
591
720
  };
721
+
592
722
  window.addEventListener('mousedown', mouseDownCallback);
593
723
  tableSelection.listenersToRemove.add(() => window.removeEventListener('mousedown', mouseDownCallback));
724
+
594
725
  const mouseUpCallback = event => {
595
726
  if (isMouseDown) {
596
727
  event.preventDefault();
597
728
  event.stopPropagation();
598
729
  }
730
+
599
731
  isMouseDown = false;
600
732
  };
733
+
601
734
  window.addEventListener('mouseup', mouseUpCallback);
602
735
  tableSelection.listenersToRemove.add(() => window.removeEventListener('mouseup', mouseUpCallback));
603
- tableSelection.listenersToRemove.add(() => tableElement.addEventListener('mouseup', mouseUpCallback));
736
+ tableElement.addEventListener('mouseup', mouseUpCallback);
604
737
  tableSelection.listenersToRemove.add(() => tableElement.removeEventListener('mouseup', mouseUpCallback));
605
738
  tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_DOWN_COMMAND, event => {
606
739
  const selection = lexical.$getSelection();
740
+
607
741
  if (!$isSelectionInTable(selection, tableNode)) {
608
742
  return false;
609
743
  }
744
+
610
745
  const direction = 'down';
746
+
611
747
  if (lexical.$isRangeSelection(selection)) {
612
748
  if (selection.isCollapsed()) {
613
749
  const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
750
+
614
751
  if (!$isTableCellNode(tableCellNode)) {
615
752
  return false;
616
753
  }
754
+
617
755
  const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
618
756
  const elementParentNode = utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isElementNode(n));
757
+
619
758
  if (elementParentNode == null) {
620
759
  throw new Error('Expected BlockNode Parent');
621
760
  }
761
+
622
762
  const lastChild = tableCellNode.getLastChild();
623
763
  const isSelectionInLastBlock = lastChild && elementParentNode.isParentOf(lastChild) || elementParentNode === lastChild;
764
+
624
765
  if (isSelectionInLastBlock || event.shiftKey) {
625
766
  event.preventDefault();
626
767
  event.stopImmediatePropagation();
627
- event.stopPropagation();
768
+ event.stopPropagation(); // Start Selection
628
769
 
629
- // Start Selection
630
770
  if (event.shiftKey) {
631
771
  tableSelection.setAnchorCellForSelection(tableNode.getCellFromCordsOrThrow(currentCords.x, currentCords.y, tableSelection.grid));
632
772
  return adjustFocusNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
633
773
  }
774
+
634
775
  return selectGridNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
635
776
  }
636
777
  }
637
778
  } else if (lexical.DEPRECATED_$isGridSelection(selection) && event.shiftKey) {
638
779
  const tableCellNode = selection.focus.getNode();
780
+
639
781
  if (!$isTableCellNode(tableCellNode)) {
640
782
  return false;
641
783
  }
784
+
642
785
  const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
643
786
  event.preventDefault();
644
787
  event.stopImmediatePropagation();
645
788
  event.stopPropagation();
646
789
  return adjustFocusNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
647
790
  }
791
+
648
792
  return false;
649
793
  }, lexical.COMMAND_PRIORITY_HIGH));
650
794
  tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_UP_COMMAND, event => {
651
795
  const selection = lexical.$getSelection();
796
+
652
797
  if (!$isSelectionInTable(selection, tableNode)) {
653
798
  return false;
654
799
  }
800
+
655
801
  const direction = 'up';
802
+
656
803
  if (lexical.$isRangeSelection(selection)) {
657
804
  if (selection.isCollapsed()) {
658
805
  const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
806
+
659
807
  if (!$isTableCellNode(tableCellNode)) {
660
808
  return false;
661
809
  }
810
+
662
811
  const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
663
812
  const elementParentNode = utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isElementNode(n));
813
+
664
814
  if (elementParentNode == null) {
665
815
  throw new Error('Expected BlockNode Parent');
666
816
  }
817
+
667
818
  const lastChild = tableCellNode.getLastChild();
668
819
  const isSelectionInLastBlock = lastChild && elementParentNode.isParentOf(lastChild) || elementParentNode === lastChild;
820
+
669
821
  if (isSelectionInLastBlock || event.shiftKey) {
670
822
  event.preventDefault();
671
823
  event.stopImmediatePropagation();
672
- event.stopPropagation();
824
+ event.stopPropagation(); // Start Selection
673
825
 
674
- // Start Selection
675
826
  if (event.shiftKey) {
676
827
  tableSelection.setAnchorCellForSelection(tableNode.getCellFromCordsOrThrow(currentCords.x, currentCords.y, tableSelection.grid));
677
828
  return adjustFocusNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
678
829
  }
830
+
679
831
  return selectGridNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
680
832
  }
681
833
  }
682
834
  } else if (lexical.DEPRECATED_$isGridSelection(selection) && event.shiftKey) {
683
835
  const tableCellNode = selection.focus.getNode();
836
+
684
837
  if (!$isTableCellNode(tableCellNode)) {
685
838
  return false;
686
839
  }
840
+
687
841
  const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
688
842
  event.preventDefault();
689
843
  event.stopImmediatePropagation();
690
844
  event.stopPropagation();
691
845
  return adjustFocusNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
692
846
  }
847
+
693
848
  return false;
694
849
  }, lexical.COMMAND_PRIORITY_HIGH));
695
850
  tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_LEFT_COMMAND, event => {
696
851
  const selection = lexical.$getSelection();
852
+
697
853
  if (!$isSelectionInTable(selection, tableNode)) {
698
854
  return false;
699
855
  }
856
+
700
857
  const direction = 'backward';
858
+
701
859
  if (lexical.$isRangeSelection(selection)) {
702
860
  if (selection.isCollapsed()) {
703
861
  const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
862
+
704
863
  if (!$isTableCellNode(tableCellNode)) {
705
864
  return false;
706
865
  }
866
+
707
867
  const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
708
868
  const elementParentNode = utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isElementNode(n));
869
+
709
870
  if (elementParentNode == null) {
710
871
  throw new Error('Expected BlockNode Parent');
711
872
  }
873
+
712
874
  if (selection.anchor.offset === 0 || event.shiftKey) {
713
875
  event.preventDefault();
714
876
  event.stopImmediatePropagation();
715
- event.stopPropagation();
877
+ event.stopPropagation(); // Start Selection
716
878
 
717
- // Start Selection
718
879
  if (event.shiftKey) {
719
880
  tableSelection.setAnchorCellForSelection(tableNode.getCellFromCordsOrThrow(currentCords.x, currentCords.y, tableSelection.grid));
720
881
  return adjustFocusNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
721
882
  }
883
+
722
884
  return selectGridNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
723
885
  }
724
886
  }
725
887
  } else if (lexical.DEPRECATED_$isGridSelection(selection) && event.shiftKey) {
726
888
  const tableCellNode = selection.focus.getNode();
889
+
727
890
  if (!$isTableCellNode(tableCellNode)) {
728
891
  return false;
729
892
  }
893
+
730
894
  const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
731
895
  event.preventDefault();
732
896
  event.stopImmediatePropagation();
733
897
  event.stopPropagation();
734
898
  return adjustFocusNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
735
899
  }
900
+
736
901
  return false;
737
902
  }, lexical.COMMAND_PRIORITY_HIGH));
738
903
  tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_RIGHT_COMMAND, event => {
739
904
  const selection = lexical.$getSelection();
905
+
740
906
  if (!$isSelectionInTable(selection, tableNode)) {
741
907
  return false;
742
908
  }
909
+
743
910
  const direction = 'forward';
911
+
744
912
  if (lexical.$isRangeSelection(selection)) {
745
913
  if (selection.isCollapsed()) {
746
914
  const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
915
+
747
916
  if (!$isTableCellNode(tableCellNode)) {
748
917
  return false;
749
918
  }
919
+
750
920
  const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
751
921
  const elementParentNode = utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isElementNode(n));
922
+
752
923
  if (elementParentNode == null) {
753
924
  throw new Error('Expected BlockNode Parent');
754
925
  }
926
+
755
927
  if (selection.anchor.offset === selection.anchor.getNode().getTextContentSize() || event.shiftKey) {
756
928
  event.preventDefault();
757
929
  event.stopImmediatePropagation();
758
- event.stopPropagation();
930
+ event.stopPropagation(); // Start Selection
759
931
 
760
- // Start Selection
761
932
  if (event.shiftKey) {
762
933
  tableSelection.setAnchorCellForSelection(tableNode.getCellFromCordsOrThrow(currentCords.x, currentCords.y, tableSelection.grid));
763
934
  return adjustFocusNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
764
935
  }
936
+
765
937
  return selectGridNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
766
938
  }
767
939
  }
768
940
  } else if (lexical.DEPRECATED_$isGridSelection(selection) && event.shiftKey) {
769
941
  const tableCellNode = selection.focus.getNode();
942
+
770
943
  if (!$isTableCellNode(tableCellNode)) {
771
944
  return false;
772
945
  }
946
+
773
947
  const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
774
948
  event.preventDefault();
775
949
  event.stopImmediatePropagation();
776
950
  event.stopPropagation();
777
951
  return adjustFocusNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
778
952
  }
953
+
779
954
  return false;
780
955
  }, lexical.COMMAND_PRIORITY_HIGH));
956
+
781
957
  const deleteTextHandler = command => () => {
782
958
  const selection = lexical.$getSelection();
959
+
783
960
  if (!$isSelectionInTable(selection, tableNode)) {
784
961
  return false;
785
962
  }
963
+
786
964
  if (lexical.DEPRECATED_$isGridSelection(selection)) {
787
965
  tableSelection.clearText();
788
966
  return true;
789
967
  } else if (lexical.$isRangeSelection(selection)) {
790
968
  const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
969
+
791
970
  if (!$isTableCellNode(tableCellNode)) {
792
971
  return false;
793
972
  }
973
+
794
974
  const anchorNode = selection.anchor.getNode();
795
975
  const focusNode = selection.focus.getNode();
796
976
  const isAnchorInside = tableNode.isParentOf(anchorNode);
797
977
  const isFocusInside = tableNode.isParentOf(focusNode);
798
978
  const selectionContainsPartialTable = isAnchorInside && !isFocusInside || isFocusInside && !isAnchorInside;
979
+
799
980
  if (selectionContainsPartialTable) {
800
981
  tableSelection.clearText();
801
982
  return true;
802
983
  }
984
+
803
985
  const nearestElementNode = utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isElementNode(n));
804
986
  const topLevelCellElementNode = nearestElementNode && utils.$findMatchingParent(nearestElementNode, n => lexical.$isElementNode(n) && $isTableCellNode(n.getParent()));
987
+
805
988
  if (!lexical.$isElementNode(topLevelCellElementNode) || !lexical.$isElementNode(nearestElementNode)) {
806
989
  return false;
807
990
  }
991
+
808
992
  if (command === lexical.DELETE_LINE_COMMAND && topLevelCellElementNode.getPreviousSibling() === null) {
809
993
  // TODO: Fix Delete Line in Table Cells.
810
994
  return true;
811
995
  }
996
+
812
997
  if (command === lexical.DELETE_CHARACTER_COMMAND || command === lexical.DELETE_WORD_COMMAND) {
813
998
  if (selection.isCollapsed() && selection.anchor.offset === 0) {
814
999
  if (nearestElementNode !== topLevelCellElementNode) {
@@ -822,16 +1007,21 @@ function applyTableHandlers(tableNode, tableElement, editor) {
822
1007
  }
823
1008
  }
824
1009
  }
1010
+
825
1011
  return false;
826
1012
  };
1013
+
827
1014
  [lexical.DELETE_WORD_COMMAND, lexical.DELETE_LINE_COMMAND, lexical.DELETE_CHARACTER_COMMAND].forEach(command => {
828
1015
  tableSelection.listenersToRemove.add(editor.registerCommand(command, deleteTextHandler(command), lexical.COMMAND_PRIORITY_CRITICAL));
829
1016
  });
1017
+
830
1018
  const deleteCellHandler = event => {
831
1019
  const selection = lexical.$getSelection();
1020
+
832
1021
  if (!$isSelectionInTable(selection, tableNode)) {
833
1022
  return false;
834
1023
  }
1024
+
835
1025
  if (lexical.DEPRECATED_$isGridSelection(selection)) {
836
1026
  event.preventDefault();
837
1027
  event.stopPropagation();
@@ -839,56 +1029,71 @@ function applyTableHandlers(tableNode, tableElement, editor) {
839
1029
  return true;
840
1030
  } else if (lexical.$isRangeSelection(selection)) {
841
1031
  const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
1032
+
842
1033
  if (!$isTableCellNode(tableCellNode)) {
843
1034
  return false;
844
1035
  }
845
1036
  }
1037
+
846
1038
  return false;
847
1039
  };
1040
+
848
1041
  tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_BACKSPACE_COMMAND, deleteCellHandler, lexical.COMMAND_PRIORITY_CRITICAL));
849
1042
  tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_DELETE_COMMAND, deleteCellHandler, lexical.COMMAND_PRIORITY_CRITICAL));
850
1043
  tableSelection.listenersToRemove.add(editor.registerCommand(lexical.FORMAT_TEXT_COMMAND, payload => {
851
1044
  const selection = lexical.$getSelection();
1045
+
852
1046
  if (!$isSelectionInTable(selection, tableNode)) {
853
1047
  return false;
854
1048
  }
1049
+
855
1050
  if (lexical.DEPRECATED_$isGridSelection(selection)) {
856
1051
  tableSelection.formatCells(payload);
857
1052
  return true;
858
1053
  } else if (lexical.$isRangeSelection(selection)) {
859
1054
  const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
1055
+
860
1056
  if (!$isTableCellNode(tableCellNode)) {
861
1057
  return false;
862
1058
  }
863
1059
  }
1060
+
864
1061
  return false;
865
1062
  }, lexical.COMMAND_PRIORITY_CRITICAL));
866
1063
  tableSelection.listenersToRemove.add(editor.registerCommand(lexical.CONTROLLED_TEXT_INSERTION_COMMAND, payload => {
867
1064
  const selection = lexical.$getSelection();
1065
+
868
1066
  if (!$isSelectionInTable(selection, tableNode)) {
869
1067
  return false;
870
1068
  }
1069
+
871
1070
  if (lexical.DEPRECATED_$isGridSelection(selection)) {
872
1071
  tableSelection.clearHighlight();
873
1072
  return false;
874
1073
  } else if (lexical.$isRangeSelection(selection)) {
875
1074
  const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
1075
+
876
1076
  if (!$isTableCellNode(tableCellNode)) {
877
1077
  return false;
878
1078
  }
879
1079
  }
1080
+
880
1081
  return false;
881
1082
  }, lexical.COMMAND_PRIORITY_CRITICAL));
882
1083
  tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_TAB_COMMAND, event => {
883
1084
  const selection = lexical.$getSelection();
1085
+
884
1086
  if (!$isSelectionInTable(selection, tableNode)) {
885
1087
  return false;
886
1088
  }
1089
+
887
1090
  if (lexical.$isRangeSelection(selection)) {
888
1091
  const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
1092
+
889
1093
  if (!$isTableCellNode(tableCellNode)) {
890
1094
  return false;
891
1095
  }
1096
+
892
1097
  if (selection.isCollapsed()) {
893
1098
  const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
894
1099
  event.preventDefault();
@@ -896,6 +1101,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
896
1101
  return true;
897
1102
  }
898
1103
  }
1104
+
899
1105
  return false;
900
1106
  }, lexical.COMMAND_PRIORITY_HIGH));
901
1107
  tableSelection.listenersToRemove.add(editor.registerCommand(lexical.FOCUS_COMMAND, payload => {
@@ -904,6 +1110,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
904
1110
  tableSelection.listenersToRemove.add(editor.registerCommand(lexical.SELECTION_CHANGE_COMMAND, payload => {
905
1111
  const selection = lexical.$getSelection();
906
1112
  const prevSelection = lexical.$getPreviousSelection();
1113
+
907
1114
  if (selection && lexical.$isRangeSelection(selection) && !selection.isCollapsed()) {
908
1115
  const anchorNode = selection.anchor.getNode();
909
1116
  const focusNode = selection.focus.getNode();
@@ -911,6 +1118,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
911
1118
  const isFocusInside = tableNode.isParentOf(focusNode);
912
1119
  const selectionContainsPartialTable = isAnchorInside && !isFocusInside || isFocusInside && !isAnchorInside;
913
1120
  const selectionIsInsideTable = isAnchorInside && isFocusInside && !tableNode.isSelected();
1121
+
914
1122
  if (selectionContainsPartialTable) {
915
1123
  const isBackward = selection.isBackward();
916
1124
  const modifiedSelection = lexical.$createRangeSelection();
@@ -925,11 +1133,13 @@ function applyTableHandlers(tableNode, tableElement, editor) {
925
1133
  const {
926
1134
  grid
927
1135
  } = tableSelection;
1136
+
928
1137
  if (selection.getNodes().filter($isTableCellNode).length === grid.rows * grid.columns) {
929
1138
  const gridSelection = lexical.DEPRECATED_$createGridSelection();
930
1139
  const tableKey = tableNode.getKey();
931
1140
  const firstCell = tableNode.getFirstChildOrThrow().getFirstChild();
932
1141
  const lastCell = tableNode.getLastChildOrThrow().getLastChild();
1142
+
933
1143
  if (firstCell != null && lastCell != null) {
934
1144
  gridSelection.set(tableKey, firstCell.getKey(), lastCell.getKey());
935
1145
  lexical.$setSelection(gridSelection);
@@ -939,20 +1149,24 @@ function applyTableHandlers(tableNode, tableElement, editor) {
939
1149
  }
940
1150
  }
941
1151
  }
1152
+
942
1153
  if (selection && !selection.is(prevSelection) && (lexical.DEPRECATED_$isGridSelection(selection) || lexical.DEPRECATED_$isGridSelection(prevSelection)) && tableSelection.gridSelection && !tableSelection.gridSelection.is(prevSelection)) {
943
1154
  if (lexical.DEPRECATED_$isGridSelection(selection) && selection.gridKey === tableSelection.tableNodeKey) {
944
1155
  tableSelection.updateTableGridSelection(selection);
945
1156
  } else if (!lexical.DEPRECATED_$isGridSelection(selection) && lexical.DEPRECATED_$isGridSelection(prevSelection) && prevSelection.gridKey === tableSelection.tableNodeKey) {
946
1157
  tableSelection.updateTableGridSelection(null);
947
1158
  }
1159
+
948
1160
  return false;
949
1161
  }
1162
+
950
1163
  if (tableSelection.hasHijackedSelectionStyles && !tableNode.isSelected()) {
951
1164
  $removeHighlightStyleToTable(tableSelection);
952
1165
  isRangeSelectionHijacked = false;
953
1166
  } else if (!tableSelection.hasHijackedSelectionStyles && tableNode.isSelected()) {
954
1167
  $addHighlightStyleToTable(tableSelection);
955
1168
  }
1169
+
956
1170
  return false;
957
1171
  }, lexical.COMMAND_PRIORITY_CRITICAL));
958
1172
  return tableSelection;
@@ -965,18 +1179,24 @@ function getTableSelectionFromTableElement(tableElement) {
965
1179
  }
966
1180
  function getCellFromTarget(node) {
967
1181
  let currentNode = node;
1182
+
968
1183
  while (currentNode != null) {
969
1184
  const nodeName = currentNode.nodeName;
1185
+
970
1186
  if (nodeName === 'TD' || nodeName === 'TH') {
971
1187
  // @ts-expect-error: internal field
972
1188
  const cell = currentNode._cell;
1189
+
973
1190
  if (cell === undefined) {
974
1191
  return null;
975
1192
  }
1193
+
976
1194
  return cell;
977
1195
  }
1196
+
978
1197
  currentNode = currentNode.parentNode;
979
1198
  }
1199
+
980
1200
  return null;
981
1201
  }
982
1202
  function getTableGrid(tableElement) {
@@ -990,8 +1210,10 @@ function getTableGrid(tableElement) {
990
1210
  let x = 0;
991
1211
  let y = 0;
992
1212
  cells.length = 0;
1213
+
993
1214
  while (currentNode != null) {
994
1215
  const nodeMame = currentNode.nodeName;
1216
+
995
1217
  if (nodeMame === 'TD' || nodeMame === 'TH') {
996
1218
  const elem = currentNode;
997
1219
  const cell = {
@@ -999,38 +1221,47 @@ function getTableGrid(tableElement) {
999
1221
  highlighted: false,
1000
1222
  x,
1001
1223
  y
1002
- };
1224
+ }; // @ts-expect-error: internal field
1003
1225
 
1004
- // @ts-expect-error: internal field
1005
1226
  currentNode._cell = cell;
1227
+
1006
1228
  if (cells[y] === undefined) {
1007
1229
  cells[y] = [];
1008
1230
  }
1231
+
1009
1232
  cells[y][x] = cell;
1010
1233
  } else {
1011
1234
  const child = currentNode.firstChild;
1235
+
1012
1236
  if (child != null) {
1013
1237
  currentNode = child;
1014
1238
  continue;
1015
1239
  }
1016
1240
  }
1241
+
1017
1242
  const sibling = currentNode.nextSibling;
1243
+
1018
1244
  if (sibling != null) {
1019
1245
  x++;
1020
1246
  currentNode = sibling;
1021
1247
  continue;
1022
1248
  }
1249
+
1023
1250
  const parent = currentNode.parentNode;
1251
+
1024
1252
  if (parent != null) {
1025
1253
  const parentSibling = parent.nextSibling;
1254
+
1026
1255
  if (parentSibling == null) {
1027
1256
  break;
1028
1257
  }
1258
+
1029
1259
  y++;
1030
1260
  x = 0;
1031
1261
  currentNode = parentSibling;
1032
1262
  }
1033
1263
  }
1264
+
1034
1265
  grid.columns = x + 1;
1035
1266
  grid.rows = y + 1;
1036
1267
  return grid;
@@ -1040,6 +1271,7 @@ function $updateDOMForSelection(grid, selection) {
1040
1271
  const selectedCellNodes = new Set(selection ? selection.getNodes() : []);
1041
1272
  $forEachGridCell(grid, (cell, lexicalNode) => {
1042
1273
  const elem = cell.elem;
1274
+
1043
1275
  if (selectedCellNodes.has(lexicalNode)) {
1044
1276
  cell.highlighted = true;
1045
1277
  elem.style.setProperty('background-color', 'rgb(172, 206, 247)');
@@ -1049,6 +1281,7 @@ function $updateDOMForSelection(grid, selection) {
1049
1281
  cell.highlighted = false;
1050
1282
  elem.style.removeProperty('background-color');
1051
1283
  elem.style.removeProperty('caret-color');
1284
+
1052
1285
  if (!elem.getAttribute('style')) {
1053
1286
  elem.removeAttribute('style');
1054
1287
  }
@@ -1060,11 +1293,14 @@ function $forEachGridCell(grid, cb) {
1060
1293
  const {
1061
1294
  cells
1062
1295
  } = grid;
1296
+
1063
1297
  for (let y = 0; y < cells.length; y++) {
1064
1298
  const row = cells[y];
1299
+
1065
1300
  for (let x = 0; x < row.length; x++) {
1066
1301
  const cell = row[x];
1067
1302
  const lexicalNode = lexical.$getNearestNodeFromDOMNode(cell.elem);
1303
+
1068
1304
  if (lexicalNode !== null) {
1069
1305
  cb(cell, lexicalNode, {
1070
1306
  x,
@@ -1090,13 +1326,16 @@ function $removeHighlightStyleToTable(tableSelection) {
1090
1326
  cell.highlighted = false;
1091
1327
  elem.style.removeProperty('background-color');
1092
1328
  elem.style.removeProperty('caret-color');
1329
+
1093
1330
  if (!elem.getAttribute('style')) {
1094
1331
  elem.removeAttribute('style');
1095
1332
  }
1096
1333
  });
1097
1334
  }
1335
+
1098
1336
  const selectGridNodeInDirection = (tableSelection, tableNode, x, y, direction) => {
1099
1337
  const isForward = direction === 'forward';
1338
+
1100
1339
  switch (direction) {
1101
1340
  case 'backward':
1102
1341
  case 'forward':
@@ -1111,34 +1350,44 @@ const selectGridNodeInDirection = (tableSelection, tableNode, x, y, direction) =
1111
1350
  tableNode.selectNext();
1112
1351
  }
1113
1352
  }
1353
+
1114
1354
  return true;
1355
+
1115
1356
  case 'up':
1116
1357
  if (y !== 0) {
1117
1358
  selectTableCellNode(tableNode.getCellNodeFromCordsOrThrow(x, y - 1, tableSelection.grid));
1118
1359
  } else {
1119
1360
  tableNode.selectPrevious();
1120
1361
  }
1362
+
1121
1363
  return true;
1364
+
1122
1365
  case 'down':
1123
1366
  if (y !== tableSelection.grid.rows - 1) {
1124
1367
  selectTableCellNode(tableNode.getCellNodeFromCordsOrThrow(x, y + 1, tableSelection.grid));
1125
1368
  } else {
1126
1369
  tableNode.selectNext();
1127
1370
  }
1371
+
1128
1372
  return true;
1373
+
1129
1374
  default:
1130
1375
  return false;
1131
1376
  }
1132
1377
  };
1378
+
1133
1379
  const adjustFocusNodeInDirection = (tableSelection, tableNode, x, y, direction) => {
1134
1380
  const isForward = direction === 'forward';
1381
+
1135
1382
  switch (direction) {
1136
1383
  case 'backward':
1137
1384
  case 'forward':
1138
1385
  if (x !== (isForward ? tableSelection.grid.columns - 1 : 0)) {
1139
1386
  tableSelection.setFocusCellForSelection(tableNode.getCellFromCordsOrThrow(x + (isForward ? 1 : -1), y, tableSelection.grid));
1140
1387
  }
1388
+
1141
1389
  return true;
1390
+
1142
1391
  case 'up':
1143
1392
  if (y !== 0) {
1144
1393
  tableSelection.setFocusCellForSelection(tableNode.getCellFromCordsOrThrow(x, y - 1, tableSelection.grid));
@@ -1146,6 +1395,7 @@ const adjustFocusNodeInDirection = (tableSelection, tableNode, x, y, direction)
1146
1395
  } else {
1147
1396
  return false;
1148
1397
  }
1398
+
1149
1399
  case 'down':
1150
1400
  if (y !== tableSelection.grid.rows - 1) {
1151
1401
  tableSelection.setFocusCellForSelection(tableNode.getCellFromCordsOrThrow(x, y + 1, tableSelection.grid));
@@ -1153,20 +1403,25 @@ const adjustFocusNodeInDirection = (tableSelection, tableNode, x, y, direction)
1153
1403
  } else {
1154
1404
  return false;
1155
1405
  }
1406
+
1156
1407
  default:
1157
1408
  return false;
1158
1409
  }
1159
1410
  };
1411
+
1160
1412
  function $isSelectionInTable(selection, tableNode) {
1161
1413
  if (lexical.$isRangeSelection(selection) || lexical.DEPRECATED_$isGridSelection(selection)) {
1162
1414
  const isAnchorInside = tableNode.isParentOf(selection.anchor.getNode());
1163
1415
  const isFocusInside = tableNode.isParentOf(selection.focus.getNode());
1164
1416
  return isAnchorInside && isFocusInside;
1165
1417
  }
1418
+
1166
1419
  return false;
1167
1420
  }
1421
+
1168
1422
  function selectTableCellNode(tableCell) {
1169
1423
  const possibleParagraph = tableCell.getChildren().find(n => lexical.$isParagraphNode(n));
1424
+
1170
1425
  if (lexical.$isParagraphNode(possibleParagraph)) {
1171
1426
  possibleParagraph.selectEnd();
1172
1427
  } else {
@@ -1181,16 +1436,18 @@ function selectTableCellNode(tableCell) {
1181
1436
  * LICENSE file in the root directory of this source tree.
1182
1437
  *
1183
1438
  */
1439
+
1184
1440
  /** @noInheritDoc */
1185
1441
  class TableNode extends lexical.DEPRECATED_GridNode {
1186
1442
  /** @internal */
1187
-
1188
1443
  static getType() {
1189
1444
  return 'table';
1190
1445
  }
1446
+
1191
1447
  static clone(node) {
1192
1448
  return new TableNode(node.__key);
1193
1449
  }
1450
+
1194
1451
  static importDOM() {
1195
1452
  return {
1196
1453
  table: _node => ({
@@ -1199,30 +1456,34 @@ class TableNode extends lexical.DEPRECATED_GridNode {
1199
1456
  })
1200
1457
  };
1201
1458
  }
1459
+
1202
1460
  static importJSON(_serializedNode) {
1203
1461
  return $createTableNode();
1204
1462
  }
1463
+
1205
1464
  constructor(key) {
1206
1465
  super(key);
1207
1466
  }
1467
+
1208
1468
  exportJSON() {
1209
- return {
1210
- ...super.exportJSON(),
1469
+ return { ...super.exportJSON(),
1211
1470
  type: 'table',
1212
1471
  version: 1
1213
1472
  };
1214
1473
  }
1474
+
1215
1475
  createDOM(config, editor) {
1216
1476
  const tableElement = document.createElement('table');
1217
1477
  utils.addClassNamesToElement(tableElement, config.theme.table);
1218
1478
  return tableElement;
1219
1479
  }
1480
+
1220
1481
  updateDOM() {
1221
1482
  return false;
1222
1483
  }
1484
+
1223
1485
  exportDOM(editor) {
1224
- return {
1225
- ...super.exportDOM(editor),
1486
+ return { ...super.exportDOM(editor),
1226
1487
  after: tableElement => {
1227
1488
  if (tableElement) {
1228
1489
  const newElement = tableElement.cloneNode();
@@ -1230,45 +1491,57 @@ class TableNode extends lexical.DEPRECATED_GridNode {
1230
1491
  const tBody = document.createElement('tbody');
1231
1492
  tBody.append(...tableElement.children);
1232
1493
  const firstRow = this.getFirstChildOrThrow();
1494
+
1233
1495
  if (!$isTableRowNode(firstRow)) {
1234
1496
  throw new Error('Expected to find row node.');
1235
1497
  }
1498
+
1236
1499
  const colCount = firstRow.getChildrenSize();
1500
+
1237
1501
  for (let i = 0; i < colCount; i++) {
1238
1502
  const col = document.createElement('col');
1239
1503
  colGroup.append(col);
1240
1504
  }
1505
+
1241
1506
  newElement.replaceChildren(colGroup, tBody);
1242
1507
  return newElement;
1243
1508
  }
1244
1509
  }
1245
1510
  };
1246
1511
  }
1512
+
1247
1513
  canExtractContents() {
1248
1514
  return false;
1249
1515
  }
1516
+
1250
1517
  canBeEmpty() {
1251
1518
  return false;
1252
1519
  }
1520
+
1253
1521
  isShadowRoot() {
1254
1522
  return true;
1255
1523
  }
1524
+
1256
1525
  getCordsFromCellNode(tableCellNode, grid) {
1257
1526
  const {
1258
1527
  rows,
1259
1528
  cells
1260
1529
  } = grid;
1530
+
1261
1531
  for (let y = 0; y < rows; y++) {
1262
1532
  const row = cells[y];
1533
+
1263
1534
  if (row == null) {
1264
1535
  throw new Error(`Row not found at y:${y}`);
1265
1536
  }
1537
+
1266
1538
  const x = row.findIndex(({
1267
1539
  elem
1268
1540
  }) => {
1269
1541
  const cellNode = lexical.$getNearestNodeFromDOMNode(elem);
1270
1542
  return cellNode === tableCellNode;
1271
1543
  });
1544
+
1272
1545
  if (x !== -1) {
1273
1546
  return {
1274
1547
  x,
@@ -1276,59 +1549,81 @@ class TableNode extends lexical.DEPRECATED_GridNode {
1276
1549
  };
1277
1550
  }
1278
1551
  }
1552
+
1279
1553
  throw new Error('Cell not found in table.');
1280
1554
  }
1555
+
1281
1556
  getCellFromCords(x, y, grid) {
1282
1557
  const {
1283
1558
  cells
1284
1559
  } = grid;
1285
1560
  const row = cells[y];
1561
+
1286
1562
  if (row == null) {
1287
1563
  return null;
1288
1564
  }
1565
+
1289
1566
  const cell = row[x];
1567
+
1290
1568
  if (cell == null) {
1291
1569
  return null;
1292
1570
  }
1571
+
1293
1572
  return cell;
1294
1573
  }
1574
+
1295
1575
  getCellFromCordsOrThrow(x, y, grid) {
1296
1576
  const cell = this.getCellFromCords(x, y, grid);
1577
+
1297
1578
  if (!cell) {
1298
1579
  throw new Error('Cell not found at cords.');
1299
1580
  }
1581
+
1300
1582
  return cell;
1301
1583
  }
1584
+
1302
1585
  getCellNodeFromCords(x, y, grid) {
1303
1586
  const cell = this.getCellFromCords(x, y, grid);
1587
+
1304
1588
  if (cell == null) {
1305
1589
  return null;
1306
1590
  }
1591
+
1307
1592
  const node = lexical.$getNearestNodeFromDOMNode(cell.elem);
1593
+
1308
1594
  if ($isTableCellNode(node)) {
1309
1595
  return node;
1310
1596
  }
1597
+
1311
1598
  return null;
1312
1599
  }
1600
+
1313
1601
  getCellNodeFromCordsOrThrow(x, y, grid) {
1314
1602
  const node = this.getCellNodeFromCords(x, y, grid);
1603
+
1315
1604
  if (!node) {
1316
1605
  throw new Error('Node at cords not TableCellNode.');
1317
1606
  }
1607
+
1318
1608
  return node;
1319
1609
  }
1610
+
1320
1611
  canSelectBefore() {
1321
1612
  return true;
1322
1613
  }
1614
+
1323
1615
  canIndent() {
1324
1616
  return false;
1325
1617
  }
1618
+
1326
1619
  }
1327
1620
  function $getElementGridForTableNode(editor, tableNode) {
1328
1621
  const tableElement = editor.getElementByKey(tableNode.getKey());
1622
+
1329
1623
  if (tableElement == null) {
1330
1624
  throw new Error('Table Element Not Found');
1331
1625
  }
1626
+
1332
1627
  return getTableGrid(tableElement);
1333
1628
  }
1334
1629
  function convertTableElement(_domNode) {
@@ -1352,10 +1647,13 @@ function $isTableNode(node) {
1352
1647
  */
1353
1648
  function $createTableNodeWithDimensions(rowCount, columnCount, includeHeaders = true) {
1354
1649
  const tableNode = $createTableNode();
1650
+
1355
1651
  for (let iRow = 0; iRow < rowCount; iRow++) {
1356
1652
  const tableRowNode = $createTableRowNode();
1653
+
1357
1654
  for (let iColumn = 0; iColumn < columnCount; iColumn++) {
1358
1655
  let headerState = TableCellHeaderStates.NO_STATUS;
1656
+
1359
1657
  if (typeof includeHeaders === 'object') {
1360
1658
  if (iRow === 0 && includeHeaders.rows) headerState |= TableCellHeaderStates.ROW;
1361
1659
  if (iColumn === 0 && includeHeaders.columns) headerState |= TableCellHeaderStates.COLUMN;
@@ -1363,35 +1661,44 @@ function $createTableNodeWithDimensions(rowCount, columnCount, includeHeaders =
1363
1661
  if (iRow === 0) headerState |= TableCellHeaderStates.ROW;
1364
1662
  if (iColumn === 0) headerState |= TableCellHeaderStates.COLUMN;
1365
1663
  }
1664
+
1366
1665
  const tableCellNode = $createTableCellNode(headerState);
1367
1666
  const paragraphNode = lexical.$createParagraphNode();
1368
1667
  paragraphNode.append(lexical.$createTextNode());
1369
1668
  tableCellNode.append(paragraphNode);
1370
1669
  tableRowNode.append(tableCellNode);
1371
1670
  }
1671
+
1372
1672
  tableNode.append(tableRowNode);
1373
1673
  }
1674
+
1374
1675
  return tableNode;
1375
1676
  }
1376
1677
  function $getTableCellNodeFromLexicalNode(startingNode) {
1377
1678
  const node = utils.$findMatchingParent(startingNode, n => $isTableCellNode(n));
1679
+
1378
1680
  if ($isTableCellNode(node)) {
1379
1681
  return node;
1380
1682
  }
1683
+
1381
1684
  return null;
1382
1685
  }
1383
1686
  function $getTableRowNodeFromTableCellNodeOrThrow(startingNode) {
1384
1687
  const node = utils.$findMatchingParent(startingNode, n => $isTableRowNode(n));
1688
+
1385
1689
  if ($isTableRowNode(node)) {
1386
1690
  return node;
1387
1691
  }
1692
+
1388
1693
  throw new Error('Expected table cell to be inside of table row.');
1389
1694
  }
1390
1695
  function $getTableNodeFromLexicalNodeOrThrow(startingNode) {
1391
1696
  const node = utils.$findMatchingParent(startingNode, n => $isTableNode(n));
1697
+
1392
1698
  if ($isTableNode(node)) {
1393
1699
  return node;
1394
1700
  }
1701
+
1395
1702
  throw new Error('Expected table cell to be inside of table.');
1396
1703
  }
1397
1704
  function $getTableRowIndexFromTableCellNode(tableCellNode) {
@@ -1418,42 +1725,53 @@ function $getTableCellSiblingsFromTableCellNode(tableCellNode, grid) {
1418
1725
  }
1419
1726
  function $removeTableRowAtIndex(tableNode, indexToDelete) {
1420
1727
  const tableRows = tableNode.getChildren();
1728
+
1421
1729
  if (indexToDelete >= tableRows.length || indexToDelete < 0) {
1422
1730
  throw new Error('Expected table cell to be inside of table row.');
1423
1731
  }
1732
+
1424
1733
  const targetRowNode = tableRows[indexToDelete];
1425
1734
  targetRowNode.remove();
1426
1735
  return tableNode;
1427
1736
  }
1428
1737
  function $insertTableRow(tableNode, targetIndex, shouldInsertAfter = true, rowCount, grid) {
1429
1738
  const tableRows = tableNode.getChildren();
1739
+
1430
1740
  if (targetIndex >= tableRows.length || targetIndex < 0) {
1431
1741
  throw new Error('Table row target index out of range');
1432
1742
  }
1743
+
1433
1744
  const targetRowNode = tableRows[targetIndex];
1745
+
1434
1746
  if ($isTableRowNode(targetRowNode)) {
1435
1747
  for (let r = 0; r < rowCount; r++) {
1436
1748
  const tableRowCells = targetRowNode.getChildren();
1437
1749
  const tableColumnCount = tableRowCells.length;
1438
1750
  const newTableRowNode = $createTableRowNode();
1751
+
1439
1752
  for (let c = 0; c < tableColumnCount; c++) {
1440
1753
  const tableCellFromTargetRow = tableRowCells[c];
1754
+
1441
1755
  if (!$isTableCellNode(tableCellFromTargetRow)) {
1442
1756
  throw Error(`Expected table cell`);
1443
1757
  }
1758
+
1444
1759
  const {
1445
1760
  above,
1446
1761
  below
1447
1762
  } = $getTableCellSiblingsFromTableCellNode(tableCellFromTargetRow, grid);
1448
1763
  let headerState = TableCellHeaderStates.NO_STATUS;
1449
1764
  const width = above && above.getWidth() || below && below.getWidth() || undefined;
1765
+
1450
1766
  if (above && above.hasHeaderState(TableCellHeaderStates.COLUMN) || below && below.hasHeaderState(TableCellHeaderStates.COLUMN)) {
1451
1767
  headerState |= TableCellHeaderStates.COLUMN;
1452
1768
  }
1769
+
1453
1770
  const tableCellNode = $createTableCellNode(headerState, 1, width);
1454
1771
  tableCellNode.append(lexical.$createParagraphNode());
1455
1772
  newTableRowNode.append(tableCellNode);
1456
1773
  }
1774
+
1457
1775
  if (shouldInsertAfter) {
1458
1776
  targetRowNode.insertAfter(newTableRowNode);
1459
1777
  } else {
@@ -1463,32 +1781,108 @@ function $insertTableRow(tableNode, targetIndex, shouldInsertAfter = true, rowCo
1463
1781
  } else {
1464
1782
  throw new Error('Row before insertion index does not exist.');
1465
1783
  }
1784
+
1466
1785
  return tableNode;
1467
1786
  }
1787
+ function $insertTableRow__EXPERIMENTAL(insertAfter = true) {
1788
+ const selection = lexical.$getSelection();
1789
+
1790
+ if (!(lexical.$isRangeSelection(selection) || lexical.DEPRECATED_$isGridSelection(selection))) {
1791
+ throw Error(`Expected a RangeSelection or GridSelection`);
1792
+ }
1793
+
1794
+ const focus = selection.focus.getNode();
1795
+ const [focusCell,, grid] = lexical.DEPRECATED_$getNodeTriplet(focus);
1796
+ const [gridMap, focusCellMap] = lexical.DEPRECATED_$computeGridMap(grid, focusCell, focusCell);
1797
+ const columnCount = gridMap[0].length;
1798
+ const {
1799
+ startRow: focusStartRow
1800
+ } = focusCellMap;
1801
+
1802
+ if (insertAfter) {
1803
+ const focusEndRow = focusStartRow + focusCell.__rowSpan - 1;
1804
+ const focusEndRowMap = gridMap[focusEndRow];
1805
+ const newRow = $createTableRowNode();
1806
+
1807
+ for (let i = 0; i < columnCount; i++) {
1808
+ const {
1809
+ cell,
1810
+ startRow
1811
+ } = focusEndRowMap[i];
1812
+
1813
+ if (startRow + cell.__rowSpan - 1 <= focusEndRow) {
1814
+ newRow.append($createTableCellNode(TableCellHeaderStates.NO_STATUS));
1815
+ } else {
1816
+ cell.setRowSpan(cell.__rowSpan + 1);
1817
+ }
1818
+ }
1819
+
1820
+ const focusEndRowNode = grid.getChildAtIndex(focusEndRow);
1821
+
1822
+ if (!lexical.DEPRECATED_$isGridRowNode(focusEndRowNode)) {
1823
+ throw Error(`focusEndRow is not a GridRowNode`);
1824
+ }
1825
+
1826
+ focusEndRowNode.insertAfter(newRow);
1827
+ } else {
1828
+ const focusStartRowMap = gridMap[focusStartRow];
1829
+ const newRow = $createTableRowNode();
1830
+
1831
+ for (let i = 0; i < columnCount; i++) {
1832
+ const {
1833
+ cell,
1834
+ startRow
1835
+ } = focusStartRowMap[i];
1836
+
1837
+ if (startRow === focusStartRow) {
1838
+ newRow.append($createTableCellNode(TableCellHeaderStates.NO_STATUS));
1839
+ } else {
1840
+ cell.setRowSpan(cell.__rowSpan + 1);
1841
+ }
1842
+ }
1843
+
1844
+ const focusStartRowNode = grid.getChildAtIndex(focusStartRow);
1845
+
1846
+ if (!lexical.DEPRECATED_$isGridRowNode(focusStartRowNode)) {
1847
+ throw Error(`focusEndRow is not a GridRowNode`);
1848
+ }
1849
+
1850
+ focusStartRowNode.insertBefore(newRow);
1851
+ }
1852
+ }
1468
1853
  function $insertTableColumn(tableNode, targetIndex, shouldInsertAfter = true, columnCount, grid) {
1469
1854
  const tableRows = tableNode.getChildren();
1855
+
1470
1856
  for (let r = 0; r < tableRows.length; r++) {
1471
1857
  const currentTableRowNode = tableRows[r];
1858
+
1472
1859
  if ($isTableRowNode(currentTableRowNode)) {
1473
1860
  for (let c = 0; c < columnCount; c++) {
1474
1861
  const tableRowChildren = currentTableRowNode.getChildren();
1862
+
1475
1863
  if (targetIndex >= tableRowChildren.length || targetIndex < 0) {
1476
1864
  throw new Error('Table column target index out of range');
1477
1865
  }
1866
+
1478
1867
  const targetCell = tableRowChildren[targetIndex];
1868
+
1479
1869
  if (!$isTableCellNode(targetCell)) {
1480
1870
  throw Error(`Expected table cell`);
1481
1871
  }
1872
+
1482
1873
  const {
1483
1874
  left,
1484
1875
  right
1485
1876
  } = $getTableCellSiblingsFromTableCellNode(targetCell, grid);
1486
1877
  let headerState = TableCellHeaderStates.NO_STATUS;
1878
+
1487
1879
  if (left && left.hasHeaderState(TableCellHeaderStates.ROW) || right && right.hasHeaderState(TableCellHeaderStates.ROW)) {
1488
1880
  headerState |= TableCellHeaderStates.ROW;
1489
1881
  }
1882
+
1490
1883
  const newTableCell = $createTableCellNode(headerState);
1491
1884
  newTableCell.append(lexical.$createParagraphNode());
1885
+
1492
1886
  if (shouldInsertAfter) {
1493
1887
  targetCell.insertAfter(newTableCell);
1494
1888
  } else {
@@ -1497,22 +1891,257 @@ function $insertTableColumn(tableNode, targetIndex, shouldInsertAfter = true, co
1497
1891
  }
1498
1892
  }
1499
1893
  }
1894
+
1500
1895
  return tableNode;
1501
1896
  }
1897
+ function $insertTableColumn__EXPERIMENTAL(insertAfter = true) {
1898
+ const selection = lexical.$getSelection();
1899
+
1900
+ if (!(lexical.$isRangeSelection(selection) || lexical.DEPRECATED_$isGridSelection(selection))) {
1901
+ throw Error(`Expected a RangeSelection or GridSelection`);
1902
+ }
1903
+
1904
+ const focus = selection.focus.getNode();
1905
+ const [focusCell,, grid] = lexical.DEPRECATED_$getNodeTriplet(focus);
1906
+ const [gridMap, focusCellMap] = lexical.DEPRECATED_$computeGridMap(grid, focusCell, focusCell);
1907
+ const rowCount = gridMap.length;
1908
+ const {
1909
+ startColumn: focusStartColumn
1910
+ } = focusCellMap;
1911
+
1912
+ if (insertAfter) {
1913
+ const focusEndColumn = focusStartColumn + focusCell.__colSpan - 1;
1914
+
1915
+ for (let i = 0; i < rowCount; i++) {
1916
+ const {
1917
+ cell,
1918
+ startColumn
1919
+ } = gridMap[i][focusEndColumn];
1920
+
1921
+ if (startColumn + cell.__colSpan - 1 <= focusEndColumn) {
1922
+ cell.insertAfter($createTableCellNode(TableCellHeaderStates.NO_STATUS));
1923
+ } else {
1924
+ cell.setColSpan(cell.__colSpan + 1);
1925
+ }
1926
+ }
1927
+ } else {
1928
+ for (let i = 0; i < rowCount; i++) {
1929
+ const {
1930
+ cell,
1931
+ startColumn
1932
+ } = gridMap[i][focusStartColumn];
1933
+
1934
+ if (startColumn === focusStartColumn) {
1935
+ cell.insertBefore($createTableCellNode(TableCellHeaderStates.NO_STATUS));
1936
+ } else {
1937
+ cell.setColSpan(cell.__colSpan + 1);
1938
+ }
1939
+ }
1940
+ }
1941
+ }
1502
1942
  function $deleteTableColumn(tableNode, targetIndex) {
1503
1943
  const tableRows = tableNode.getChildren();
1944
+
1504
1945
  for (let i = 0; i < tableRows.length; i++) {
1505
1946
  const currentTableRowNode = tableRows[i];
1947
+
1506
1948
  if ($isTableRowNode(currentTableRowNode)) {
1507
1949
  const tableRowChildren = currentTableRowNode.getChildren();
1950
+
1508
1951
  if (targetIndex >= tableRowChildren.length || targetIndex < 0) {
1509
1952
  throw new Error('Table column target index out of range');
1510
1953
  }
1954
+
1511
1955
  tableRowChildren[targetIndex].remove();
1512
1956
  }
1513
1957
  }
1958
+
1514
1959
  return tableNode;
1515
1960
  }
1961
+ function $deleteTableRow__EXPERIMENTAL() {
1962
+ const selection = lexical.$getSelection();
1963
+
1964
+ if (!(lexical.$isRangeSelection(selection) || lexical.DEPRECATED_$isGridSelection(selection))) {
1965
+ throw Error(`Expected a RangeSelection or GridSelection`);
1966
+ }
1967
+
1968
+ const anchor = selection.anchor.getNode();
1969
+ const focus = selection.focus.getNode();
1970
+ const [anchorCell,, grid] = lexical.DEPRECATED_$getNodeTriplet(anchor);
1971
+ const [focusCell] = lexical.DEPRECATED_$getNodeTriplet(focus);
1972
+ const [gridMap, anchorCellMap, focusCellMap] = lexical.DEPRECATED_$computeGridMap(grid, anchorCell, focusCell);
1973
+ const {
1974
+ startRow: anchorStartRow
1975
+ } = anchorCellMap;
1976
+ const {
1977
+ startRow: focusStartRow
1978
+ } = focusCellMap;
1979
+ const focusEndRow = focusStartRow + focusCell.__rowSpan - 1;
1980
+
1981
+ if (gridMap.length === focusEndRow - anchorStartRow + 1) {
1982
+ // Empty grid
1983
+ grid.remove();
1984
+ return;
1985
+ }
1986
+
1987
+ const columnCount = gridMap[0].length;
1988
+ const nextRow = gridMap[focusEndRow + 1];
1989
+ const nextRowNode = grid.getChildAtIndex(focusEndRow + 1);
1990
+
1991
+ if (!lexical.DEPRECATED_$isGridRowNode(nextRowNode)) {
1992
+ throw Error(`Expected GridNode childAtIndex(${String(focusEndRow + 1)}) to be RowNode`);
1993
+ }
1994
+
1995
+ for (let row = focusEndRow; row >= anchorStartRow; row--) {
1996
+ for (let column = columnCount - 1; column >= 0; column--) {
1997
+ const {
1998
+ cell,
1999
+ startRow: cellStartRow,
2000
+ startColumn: cellStartColumn
2001
+ } = gridMap[row][column];
2002
+
2003
+ if (cellStartColumn !== column) {
2004
+ // Don't repeat work for the same Cell
2005
+ continue;
2006
+ } // Rows overflowing top have to be trimmed
2007
+
2008
+
2009
+ if (row === anchorStartRow && cellStartRow < anchorStartRow) {
2010
+ cell.setRowSpan(cell.__rowSpan - (cellStartRow - anchorStartRow));
2011
+ } // Rows overflowing bottom have to be trimmed and moved to the next row
2012
+
2013
+
2014
+ if (cellStartRow >= anchorStartRow && cellStartRow + cell.__rowSpan - 1 > focusEndRow) {
2015
+ cell.setRowSpan(cell.__rowSpan - (focusEndRow - cellStartRow + 1));
2016
+
2017
+ if (column === 0) {
2018
+ $insertFirst(nextRowNode, cell);
2019
+ } else {
2020
+ const {
2021
+ cell: previousCell
2022
+ } = nextRow[column - 1];
2023
+ previousCell.insertAfter(cell);
2024
+ }
2025
+ }
2026
+ }
2027
+
2028
+ const rowNode = grid.getChildAtIndex(row);
2029
+
2030
+ if (!lexical.DEPRECATED_$isGridRowNode(rowNode)) {
2031
+ throw Error(`Expected GridNode childAtIndex(${String(row)}) to be RowNode`);
2032
+ }
2033
+
2034
+ rowNode.remove();
2035
+ }
2036
+
2037
+ if (nextRow !== undefined) {
2038
+ const {
2039
+ cell
2040
+ } = nextRow[0];
2041
+ $moveSelectionToCell(cell);
2042
+ } else {
2043
+ const previousRow = gridMap[anchorStartRow - 1];
2044
+ const {
2045
+ cell
2046
+ } = previousRow[0];
2047
+ $moveSelectionToCell(cell);
2048
+ }
2049
+ }
2050
+ function $deleteTableColumn__EXPERIMENTAL() {
2051
+ const selection = lexical.$getSelection();
2052
+
2053
+ if (!(lexical.$isRangeSelection(selection) || lexical.DEPRECATED_$isGridSelection(selection))) {
2054
+ throw Error(`Expected a RangeSelection or GridSelection`);
2055
+ }
2056
+
2057
+ const anchor = selection.anchor.getNode();
2058
+ const focus = selection.focus.getNode();
2059
+ const [anchorCell,, grid] = lexical.DEPRECATED_$getNodeTriplet(anchor);
2060
+ const [focusCell] = lexical.DEPRECATED_$getNodeTriplet(focus);
2061
+ const [gridMap, anchorCellMap, focusCellMap] = lexical.DEPRECATED_$computeGridMap(grid, anchorCell, focusCell);
2062
+ const {
2063
+ startColumn: anchorStartColumn
2064
+ } = anchorCellMap;
2065
+ const {
2066
+ startRow: focusStartRow,
2067
+ startColumn: focusStartColumn
2068
+ } = focusCellMap;
2069
+ const startColumn = Math.min(anchorStartColumn, focusStartColumn);
2070
+ const endColumn = Math.max(anchorStartColumn + anchorCell.__colSpan - 1, focusStartColumn + focusCell.__colSpan - 1);
2071
+ const selectedColumnCount = endColumn - startColumn + 1;
2072
+ const columnCount = gridMap[0].length;
2073
+
2074
+ if (columnCount === endColumn - startColumn + 1) {
2075
+ // Empty grid
2076
+ grid.selectPrevious();
2077
+ grid.remove();
2078
+ return;
2079
+ }
2080
+
2081
+ const rowCount = gridMap.length;
2082
+
2083
+ for (let row = 0; row < rowCount; row++) {
2084
+ for (let column = startColumn; column <= endColumn; column++) {
2085
+ const {
2086
+ cell,
2087
+ startColumn: cellStartColumn
2088
+ } = gridMap[row][column];
2089
+
2090
+ if (cellStartColumn < startColumn) {
2091
+ if (column === startColumn) {
2092
+ const overflowLeft = startColumn - cellStartColumn; // Overflowing left
2093
+
2094
+ cell.setColSpan(cell.__colSpan - // Possible overflow right too
2095
+ Math.min(selectedColumnCount, cell.__colSpan - overflowLeft));
2096
+ }
2097
+ } else if (cellStartColumn + cell.__colSpan - 1 > endColumn) {
2098
+ if (column === endColumn) {
2099
+ // Overflowing right
2100
+ const inSelectedArea = endColumn - cellStartColumn + 1;
2101
+ cell.setColSpan(cell.__colSpan - inSelectedArea);
2102
+ }
2103
+ } else {
2104
+ cell.remove();
2105
+ }
2106
+ }
2107
+ }
2108
+
2109
+ const focusRowMap = gridMap[focusStartRow];
2110
+ const nextColumn = focusRowMap[focusStartColumn + focusCell.__colSpan];
2111
+
2112
+ if (nextColumn !== undefined) {
2113
+ const {
2114
+ cell
2115
+ } = nextColumn;
2116
+ $moveSelectionToCell(cell);
2117
+ } else {
2118
+ const previousRow = focusRowMap[focusStartColumn - 1];
2119
+ const {
2120
+ cell
2121
+ } = previousRow;
2122
+ $moveSelectionToCell(cell);
2123
+ }
2124
+ }
2125
+
2126
+ function $moveSelectionToCell(cell) {
2127
+ const firstDescendant = cell.getFirstDescendant();
2128
+
2129
+ if (!(firstDescendant !== null)) {
2130
+ throw Error(`Unexpected empty cell`);
2131
+ }
2132
+
2133
+ firstDescendant.getParentOrThrow().selectStart();
2134
+ }
2135
+
2136
+ function $insertFirst(parent, node) {
2137
+ const firstChild = parent.getFirstChild();
2138
+
2139
+ if (firstChild !== null) {
2140
+ parent.insertBefore(firstChild);
2141
+ } else {
2142
+ parent.append(node);
2143
+ }
2144
+ }
1516
2145
 
1517
2146
  /** @module @lexical/table */
1518
2147
  const INSERT_TABLE_COMMAND = lexical.createCommand('INSERT_TABLE_COMMAND');
@@ -1522,6 +2151,8 @@ exports.$createTableNode = $createTableNode;
1522
2151
  exports.$createTableNodeWithDimensions = $createTableNodeWithDimensions;
1523
2152
  exports.$createTableRowNode = $createTableRowNode;
1524
2153
  exports.$deleteTableColumn = $deleteTableColumn;
2154
+ exports.$deleteTableColumn__EXPERIMENTAL = $deleteTableColumn__EXPERIMENTAL;
2155
+ exports.$deleteTableRow__EXPERIMENTAL = $deleteTableRow__EXPERIMENTAL;
1525
2156
  exports.$getElementGridForTableNode = $getElementGridForTableNode;
1526
2157
  exports.$getTableCellNodeFromLexicalNode = $getTableCellNodeFromLexicalNode;
1527
2158
  exports.$getTableColumnIndexFromTableCellNode = $getTableColumnIndexFromTableCellNode;
@@ -1529,7 +2160,9 @@ exports.$getTableNodeFromLexicalNodeOrThrow = $getTableNodeFromLexicalNodeOrThro
1529
2160
  exports.$getTableRowIndexFromTableCellNode = $getTableRowIndexFromTableCellNode;
1530
2161
  exports.$getTableRowNodeFromTableCellNodeOrThrow = $getTableRowNodeFromTableCellNodeOrThrow;
1531
2162
  exports.$insertTableColumn = $insertTableColumn;
2163
+ exports.$insertTableColumn__EXPERIMENTAL = $insertTableColumn__EXPERIMENTAL;
1532
2164
  exports.$insertTableRow = $insertTableRow;
2165
+ exports.$insertTableRow__EXPERIMENTAL = $insertTableRow__EXPERIMENTAL;
1533
2166
  exports.$isTableCellNode = $isTableCellNode;
1534
2167
  exports.$isTableNode = $isTableNode;
1535
2168
  exports.$isTableRowNode = $isTableRowNode;