@lexical/table 0.4.0 → 0.5.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.
@@ -24,7 +24,7 @@ const TableCellHeaderStates = {
24
24
  };
25
25
 
26
26
  /** @noInheritDoc */
27
- class TableCellNode extends lexical.GridCellNode {
27
+ class TableCellNode extends lexical.DEPRECATED_GridCellNode {
28
28
  /** @internal */
29
29
 
30
30
  /** @internal */
@@ -150,6 +150,10 @@ class TableCellNode extends lexical.GridCellNode {
150
150
  return prevNode.__headerState !== this.__headerState || prevNode.__width !== this.__width;
151
151
  }
152
152
 
153
+ isShadowRoot() {
154
+ return true;
155
+ }
156
+
153
157
  collapseAtStart() {
154
158
  return true;
155
159
  }
@@ -200,7 +204,7 @@ function $isTableCellNode(node) {
200
204
  */
201
205
 
202
206
  /** @noInheritDoc */
203
- class TableRowNode extends lexical.GridRowNode {
207
+ class TableRowNode extends lexical.DEPRECATED_GridRowNode {
204
208
  /** @internal */
205
209
  static getType() {
206
210
  return 'tablerow';
@@ -246,6 +250,10 @@ class TableRowNode extends lexical.GridRowNode {
246
250
  return element;
247
251
  }
248
252
 
253
+ isShadowRoot() {
254
+ return true;
255
+ }
256
+
249
257
  setHeight(height) {
250
258
  const self = this.getWritable();
251
259
  self.__height = height;
@@ -470,23 +478,14 @@ class TableSelection {
470
478
  }
471
479
 
472
480
  updateTableGridSelection(selection) {
473
- if (selection != null) {
481
+ if (selection != null && selection.gridKey === this.tableNodeKey) {
474
482
  this.gridSelection = selection;
475
483
  this.isHighlightingCells = true;
476
484
  this.disableHighlightStyle();
477
- const anchorElement = this.editor.getElementByKey(selection.anchor.key);
478
- const focusElement = this.editor.getElementByKey(selection.focus.key);
479
-
480
- if (anchorElement && focusElement) {
481
- const domSelection = getDOMSelection();
482
-
483
- if (domSelection) {
484
- domSelection.setBaseAndExtent(anchorElement, 0, focusElement, 0);
485
- }
486
- }
487
-
488
485
  $updateDOMForSelection(this.grid, this.gridSelection);
489
- } else {
486
+ }
487
+
488
+ if (selection == null) {
490
489
  this.clearHighlight();
491
490
  }
492
491
  }
@@ -508,12 +507,12 @@ class TableSelection {
508
507
  const cellX = cell.x;
509
508
  const cellY = cell.y;
510
509
  this.focusCell = cell;
511
- const domSelection = getDOMSelection();
512
510
 
513
511
  if (this.anchorCell !== null) {
514
- // Collapse the selection
512
+ const domSelection = getDOMSelection(); // Collapse the selection
513
+
515
514
  if (domSelection) {
516
- domSelection.setBaseAndExtent(this.anchorCell.elem, 0, cell.elem, 0);
515
+ domSelection.setBaseAndExtent(this.anchorCell.elem, 0, this.anchorCell.elem, 0);
517
516
  }
518
517
  }
519
518
 
@@ -532,7 +531,7 @@ class TableSelection {
532
531
 
533
532
  if (this.gridSelection != null && this.anchorCellNodeKey != null && $isTableCellNode(focusTableCellNode)) {
534
533
  const focusNodeKey = focusTableCellNode.getKey();
535
- this.gridSelection = lexical.$createGridSelection();
534
+ this.gridSelection = lexical.DEPRECATED_$createGridSelection();
536
535
  this.focusCellNodeKey = focusNodeKey;
537
536
  this.gridSelection.set(this.tableNodeKey, this.anchorCellNodeKey, this.focusCellNodeKey);
538
537
  lexical.$setSelection(this.gridSelection);
@@ -545,20 +544,22 @@ class TableSelection {
545
544
 
546
545
  setAnchorCellForSelection(cell) {
547
546
  this.editor.update(() => {
548
- this.anchorCell = cell;
549
- this.startX = cell.x;
550
- this.startY = cell.y;
551
- const domSelection = getDOMSelection();
547
+ if (this.anchorCell === cell && this.isHighlightingCells) {
548
+ const domSelection = getDOMSelection(); // Collapse the selection
552
549
 
553
- if (domSelection) {
554
- domSelection.setBaseAndExtent(cell.elem, 0, cell.elem, 0);
550
+ if (domSelection) {
551
+ domSelection.setBaseAndExtent(cell.elem, 0, cell.elem, 0);
552
+ }
555
553
  }
556
554
 
555
+ this.anchorCell = cell;
556
+ this.startX = cell.x;
557
+ this.startY = cell.y;
557
558
  const anchorTableCellNode = lexical.$getNearestNodeFromDOMNode(cell.elem);
558
559
 
559
560
  if ($isTableCellNode(anchorTableCellNode)) {
560
561
  const anchorNodeKey = anchorTableCellNode.getKey();
561
- this.gridSelection = lexical.$createGridSelection();
562
+ this.gridSelection = lexical.DEPRECATED_$createGridSelection();
562
563
  this.anchorCellNodeKey = anchorNodeKey;
563
564
  }
564
565
  });
@@ -568,7 +569,7 @@ class TableSelection {
568
569
  this.editor.update(() => {
569
570
  const selection = lexical.$getSelection();
570
571
 
571
- if (!lexical.$isGridSelection(selection)) {
572
+ if (!lexical.DEPRECATED_$isGridSelection(selection)) {
572
573
  {
573
574
  throw Error(`Expected grid selection`);
574
575
  }
@@ -599,7 +600,7 @@ class TableSelection {
599
600
 
600
601
  const selection = lexical.$getSelection();
601
602
 
602
- if (!lexical.$isGridSelection(selection)) {
603
+ if (!lexical.DEPRECATED_$isGridSelection(selection)) {
603
604
  {
604
605
  throw Error(`Expected grid selection`);
605
606
  }
@@ -611,7 +612,8 @@ class TableSelection {
611
612
  tableNode.selectPrevious(); // Delete entire table
612
613
 
613
614
  tableNode.remove();
614
- this.clearHighlight();
615
+ const rootNode = lexical.$getRoot();
616
+ rootNode.selectStart();
615
617
  return;
616
618
  }
617
619
 
@@ -677,13 +679,10 @@ function applyTableHandlers(tableNode, tableElement, editor) {
677
679
  const cell = getCellFromTarget(event.target);
678
680
 
679
681
  if (cell !== null) {
682
+ event.preventDefault();
683
+ event.stopPropagation();
684
+ event.stopImmediatePropagation();
680
685
  tableSelection.setAnchorCellForSelection(cell);
681
- document.addEventListener('mouseup', () => {
682
- isMouseDown = false;
683
- }, {
684
- capture: true,
685
- once: true
686
- });
687
686
  }
688
687
  }, 0);
689
688
  }); // This is adjusting the focus of the selection.
@@ -709,11 +708,6 @@ function applyTableHandlers(tableNode, tableElement, editor) {
709
708
  }
710
709
  }
711
710
  }
712
- });
713
- tableElement.addEventListener('mouseup', () => {
714
- if (isMouseDown) {
715
- isMouseDown = false;
716
- }
717
711
  }); // Select entire table at this point, when grid selection is ready.
718
712
 
719
713
  tableElement.addEventListener('mouseleave', () => {
@@ -732,7 +726,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
732
726
  editor.update(() => {
733
727
  const selection = lexical.$getSelection();
734
728
 
735
- if (lexical.$isGridSelection(selection) && selection.gridKey === tableSelection.tableNodeKey && rootElement.contains(event.target)) {
729
+ if (lexical.DEPRECATED_$isGridSelection(selection) && selection.gridKey === tableSelection.tableNodeKey && rootElement.contains(event.target)) {
736
730
  return tableSelection.clearHighlight();
737
731
  }
738
732
  });
@@ -741,12 +735,19 @@ function applyTableHandlers(tableNode, tableElement, editor) {
741
735
  window.addEventListener('mousedown', mouseDownCallback);
742
736
  tableSelection.listenersToRemove.add(() => window.removeEventListener('mousedown', mouseDownCallback));
743
737
 
744
- const mouseUpCallback = () => {
745
- isMouseDown = false;
738
+ const mouseUpCallback = event => {
739
+ if (isMouseDown) {
740
+ event.preventDefault();
741
+ event.stopPropagation();
742
+ event.stopImmediatePropagation();
743
+ isMouseDown = false;
744
+ }
746
745
  };
747
746
 
748
747
  window.addEventListener('mouseup', mouseUpCallback);
749
748
  tableSelection.listenersToRemove.add(() => window.removeEventListener('mouseup', mouseUpCallback));
749
+ tableSelection.listenersToRemove.add(() => tableElement.addEventListener('mouseup', mouseUpCallback));
750
+ tableSelection.listenersToRemove.add(() => tableElement.removeEventListener('mouseup', mouseUpCallback));
750
751
  tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_DOWN_COMMAND, event => {
751
752
  const selection = lexical.$getSelection();
752
753
 
@@ -787,7 +788,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
787
788
  return selectGridNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
788
789
  }
789
790
  }
790
- } else if (lexical.$isGridSelection(selection) && event.shiftKey) {
791
+ } else if (lexical.DEPRECATED_$isGridSelection(selection) && event.shiftKey) {
791
792
  const tableCellNode = selection.focus.getNode();
792
793
 
793
794
  if (!$isTableCellNode(tableCellNode)) {
@@ -843,7 +844,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
843
844
  return selectGridNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
844
845
  }
845
846
  }
846
- } else if (lexical.$isGridSelection(selection) && event.shiftKey) {
847
+ } else if (lexical.DEPRECATED_$isGridSelection(selection) && event.shiftKey) {
847
848
  const tableCellNode = selection.focus.getNode();
848
849
 
849
850
  if (!$isTableCellNode(tableCellNode)) {
@@ -896,7 +897,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
896
897
  return selectGridNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
897
898
  }
898
899
  }
899
- } else if (lexical.$isGridSelection(selection) && event.shiftKey) {
900
+ } else if (lexical.DEPRECATED_$isGridSelection(selection) && event.shiftKey) {
900
901
  const tableCellNode = selection.focus.getNode();
901
902
 
902
903
  if (!$isTableCellNode(tableCellNode)) {
@@ -949,7 +950,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
949
950
  return selectGridNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
950
951
  }
951
952
  }
952
- } else if (lexical.$isGridSelection(selection) && event.shiftKey) {
953
+ } else if (lexical.DEPRECATED_$isGridSelection(selection) && event.shiftKey) {
953
954
  const tableCellNode = selection.focus.getNode();
954
955
 
955
956
  if (!$isTableCellNode(tableCellNode)) {
@@ -965,14 +966,15 @@ function applyTableHandlers(tableNode, tableElement, editor) {
965
966
 
966
967
  return false;
967
968
  }, lexical.COMMAND_PRIORITY_CRITICAL));
968
- tableSelection.listenersToRemove.add(editor.registerCommand(lexical.DELETE_CHARACTER_COMMAND, () => {
969
+
970
+ const deleteTextHandler = command => () => {
969
971
  const selection = lexical.$getSelection();
970
972
 
971
973
  if (!$isSelectionInTable(selection, tableNode)) {
972
974
  return false;
973
975
  }
974
976
 
975
- if (lexical.$isGridSelection(selection)) {
977
+ if (lexical.DEPRECATED_$isGridSelection(selection)) {
976
978
  tableSelection.clearText();
977
979
  return true;
978
980
  } else if (lexical.$isRangeSelection(selection)) {
@@ -982,27 +984,68 @@ function applyTableHandlers(tableNode, tableElement, editor) {
982
984
  return false;
983
985
  }
984
986
 
985
- const paragraphNode = utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isParagraphNode(n));
987
+ const anchorNode = selection.anchor.getNode();
988
+ const focusNode = selection.focus.getNode();
989
+ const isAnchorInside = tableNode.isParentOf(anchorNode);
990
+ const isFocusInside = tableNode.isParentOf(focusNode);
991
+ const selectionContainsPartialTable = isAnchorInside && !isFocusInside || isFocusInside && !isAnchorInside;
992
+
993
+ if (selectionContainsPartialTable) {
994
+ tableSelection.clearText();
995
+ return true;
996
+ }
997
+
998
+ const parentElementNode = utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isElementNode(n) && $isTableCellNode(n.getParent()));
999
+ const nearestElementNode = utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isElementNode(n));
986
1000
 
987
- if (!lexical.$isParagraphNode(paragraphNode)) {
1001
+ if (!lexical.$isElementNode(parentElementNode) || !lexical.$isElementNode(nearestElementNode)) {
988
1002
  return false;
989
1003
  }
990
1004
 
991
- if (selection.isCollapsed() && selection.anchor.offset === 0 && paragraphNode.getPreviousSiblings().length === 0) {
1005
+ const clearCell = () => {
1006
+ const newParagraphNode = lexical.$createParagraphNode();
1007
+ const textNode = lexical.$createTextNode();
1008
+ newParagraphNode.append(textNode);
1009
+ tableCellNode.append(newParagraphNode);
1010
+ tableCellNode.getChildren().forEach(child => {
1011
+ if (child !== newParagraphNode) {
1012
+ child.remove();
1013
+ }
1014
+ });
1015
+ };
1016
+
1017
+ if (command === lexical.DELETE_LINE_COMMAND && parentElementNode.getPreviousSibling() === null) {
1018
+ clearCell();
992
1019
  return true;
993
1020
  }
1021
+
1022
+ if (command === lexical.DELETE_CHARACTER_COMMAND || command === lexical.DELETE_WORD_COMMAND) {
1023
+ if (selection.isCollapsed() && selection.anchor.offset === 0 && parentElementNode === nearestElementNode && parentElementNode.getPreviousSibling() === null) {
1024
+ return true;
1025
+ }
1026
+
1027
+ if (!lexical.$isParagraphNode(parentElementNode) && parentElementNode.getTextContentSize() === 0) {
1028
+ clearCell();
1029
+ return true;
1030
+ }
1031
+ }
994
1032
  }
995
1033
 
996
1034
  return false;
997
- }, lexical.COMMAND_PRIORITY_CRITICAL));
998
- tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_BACKSPACE_COMMAND, event => {
1035
+ };
1036
+
1037
+ [lexical.DELETE_WORD_COMMAND, lexical.DELETE_LINE_COMMAND, lexical.DELETE_CHARACTER_COMMAND].forEach(command => {
1038
+ tableSelection.listenersToRemove.add(editor.registerCommand(command, deleteTextHandler(command), lexical.COMMAND_PRIORITY_CRITICAL));
1039
+ });
1040
+
1041
+ const deleteCellHandler = event => {
999
1042
  const selection = lexical.$getSelection();
1000
1043
 
1001
1044
  if (!$isSelectionInTable(selection, tableNode)) {
1002
1045
  return false;
1003
1046
  }
1004
1047
 
1005
- if (lexical.$isGridSelection(selection)) {
1048
+ if (lexical.DEPRECATED_$isGridSelection(selection)) {
1006
1049
  event.preventDefault();
1007
1050
  event.stopPropagation();
1008
1051
  tableSelection.clearText();
@@ -1016,7 +1059,10 @@ function applyTableHandlers(tableNode, tableElement, editor) {
1016
1059
  }
1017
1060
 
1018
1061
  return false;
1019
- }, lexical.COMMAND_PRIORITY_CRITICAL));
1062
+ };
1063
+
1064
+ tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_BACKSPACE_COMMAND, deleteCellHandler, lexical.COMMAND_PRIORITY_CRITICAL));
1065
+ tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_DELETE_COMMAND, deleteCellHandler, lexical.COMMAND_PRIORITY_CRITICAL));
1020
1066
  tableSelection.listenersToRemove.add(editor.registerCommand(lexical.FORMAT_TEXT_COMMAND, payload => {
1021
1067
  const selection = lexical.$getSelection();
1022
1068
 
@@ -1024,7 +1070,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
1024
1070
  return false;
1025
1071
  }
1026
1072
 
1027
- if (lexical.$isGridSelection(selection)) {
1073
+ if (lexical.DEPRECATED_$isGridSelection(selection)) {
1028
1074
  tableSelection.formatCells(payload);
1029
1075
  return true;
1030
1076
  } else if (lexical.$isRangeSelection(selection)) {
@@ -1044,7 +1090,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
1044
1090
  return false;
1045
1091
  }
1046
1092
 
1047
- if (lexical.$isGridSelection(selection)) {
1093
+ if (lexical.DEPRECATED_$isGridSelection(selection)) {
1048
1094
  tableSelection.clearHighlight();
1049
1095
  return false;
1050
1096
  } else if (lexical.$isRangeSelection(selection)) {
@@ -1088,31 +1134,53 @@ function applyTableHandlers(tableNode, tableElement, editor) {
1088
1134
  const selection = lexical.$getSelection();
1089
1135
  const prevSelection = lexical.$getPreviousSelection();
1090
1136
 
1091
- if (selection !== prevSelection && (lexical.$isGridSelection(selection) || lexical.$isGridSelection(prevSelection)) && tableSelection.gridSelection !== selection) {
1092
- tableSelection.updateTableGridSelection(lexical.$isGridSelection(selection) && tableNode.isSelected() ? selection : null);
1093
- return false;
1094
- }
1095
-
1096
1137
  if (selection && lexical.$isRangeSelection(selection) && !selection.isCollapsed()) {
1097
1138
  const anchorNode = selection.anchor.getNode();
1098
1139
  const focusNode = selection.focus.getNode();
1099
1140
  const isAnchorInside = tableNode.isParentOf(anchorNode);
1100
1141
  const isFocusInside = tableNode.isParentOf(focusNode);
1101
- const containsPartialTable = isAnchorInside && !isFocusInside || isFocusInside && !isAnchorInside;
1142
+ const selectionContainsPartialTable = isAnchorInside && !isFocusInside || isFocusInside && !isAnchorInside;
1143
+ const selectionIsInsideTable = isAnchorInside && isFocusInside && !tableNode.isSelected();
1102
1144
 
1103
- if (containsPartialTable) {
1145
+ if (selectionContainsPartialTable) {
1104
1146
  const isBackward = selection.isBackward();
1105
1147
  const modifiedSelection = lexical.$createRangeSelection();
1106
- const tableIndex = tableNode.getIndexWithinParent();
1107
- const parentKey = tableNode.getParentOrThrow().getKey();
1108
- modifiedSelection.anchor.set(selection.anchor.key, selection.anchor.offset, selection.anchor.type); // Set selection to before or after table on the root node.
1109
-
1110
- modifiedSelection.focus.set(parentKey, isBackward ? tableIndex - 1 : tableIndex + 1, 'element');
1148
+ const tableKey = tableNode.getKey();
1149
+ modifiedSelection.anchor.set(selection.anchor.key, selection.anchor.offset, selection.anchor.type);
1150
+ modifiedSelection.focus.set(tableKey, isBackward ? 0 : tableNode.getChildrenSize(), 'element');
1111
1151
  isRangeSelectionHijacked = true;
1112
1152
  lexical.$setSelection(modifiedSelection);
1113
1153
  $addHighlightStyleToTable(tableSelection);
1114
1154
  return true;
1155
+ } else if (selectionIsInsideTable) {
1156
+ const {
1157
+ grid
1158
+ } = tableSelection;
1159
+
1160
+ if (selection.getNodes().filter($isTableCellNode).length === grid.rows * grid.columns) {
1161
+ const gridSelection = lexical.DEPRECATED_$createGridSelection();
1162
+ const tableKey = tableNode.getKey();
1163
+ const firstCell = tableNode.getFirstChildOrThrow().getFirstChild();
1164
+ const lastCell = tableNode.getLastChildOrThrow().getLastChild();
1165
+
1166
+ if (firstCell != null && lastCell != null) {
1167
+ gridSelection.set(tableKey, firstCell.getKey(), lastCell.getKey());
1168
+ lexical.$setSelection(gridSelection);
1169
+ tableSelection.updateTableGridSelection(gridSelection);
1170
+ return true;
1171
+ }
1172
+ }
1173
+ }
1174
+ }
1175
+
1176
+ if (selection && !selection.is(prevSelection) && (lexical.DEPRECATED_$isGridSelection(selection) || lexical.DEPRECATED_$isGridSelection(prevSelection)) && tableSelection.gridSelection && !tableSelection.gridSelection.is(prevSelection)) {
1177
+ if (lexical.DEPRECATED_$isGridSelection(selection) && selection.gridKey === tableSelection.tableNodeKey) {
1178
+ tableSelection.updateTableGridSelection(selection);
1179
+ } else if (!lexical.DEPRECATED_$isGridSelection(selection) && lexical.DEPRECATED_$isGridSelection(prevSelection) && prevSelection.gridKey === tableSelection.tableNodeKey) {
1180
+ tableSelection.updateTableGridSelection(null);
1115
1181
  }
1182
+
1183
+ return false;
1116
1184
  }
1117
1185
 
1118
1186
  if (tableSelection.hasHijackedSelectionStyles && !tableNode.isSelected()) {
@@ -1365,7 +1433,7 @@ const adjustFocusNodeInDirection = (tableSelection, tableNode, x, y, direction)
1365
1433
  };
1366
1434
 
1367
1435
  function $isSelectionInTable(selection, tableNode) {
1368
- if (lexical.$isRangeSelection(selection) || lexical.$isGridSelection(selection)) {
1436
+ if (lexical.$isRangeSelection(selection) || lexical.DEPRECATED_$isGridSelection(selection)) {
1369
1437
  const isAnchorInside = tableNode.isParentOf(selection.anchor.getNode());
1370
1438
  const isFocusInside = tableNode.isParentOf(selection.focus.getNode());
1371
1439
  return isAnchorInside && isFocusInside;
@@ -1393,7 +1461,7 @@ function selectTableCellNode(tableCell) {
1393
1461
  */
1394
1462
 
1395
1463
  /** @noInheritDoc */
1396
- class TableNode extends lexical.GridNode {
1464
+ class TableNode extends lexical.DEPRECATED_GridNode {
1397
1465
  /** @internal */
1398
1466
  static getType() {
1399
1467
  return 'table';
@@ -1407,7 +1475,7 @@ class TableNode extends lexical.GridNode {
1407
1475
  return {
1408
1476
  table: _node => ({
1409
1477
  conversion: convertTableElement,
1410
- priority: 0
1478
+ priority: 1
1411
1479
  })
1412
1480
  };
1413
1481
  }
@@ -1473,6 +1541,10 @@ class TableNode extends lexical.GridNode {
1473
1541
  return false;
1474
1542
  }
1475
1543
 
1544
+ isShadowRoot() {
1545
+ return true;
1546
+ }
1547
+
1476
1548
  getCordsFromCellNode(tableCellNode, grid) {
1477
1549
  const {
1478
1550
  rows,
@@ -1732,7 +1804,7 @@ function $insertTableRow(tableNode, targetIndex, shouldInsertAfter = true, rowCo
1732
1804
 
1733
1805
  return tableNode;
1734
1806
  }
1735
- function $insertTableColumn(tableNode, targetIndex, shouldInsertAfter = true, columnCount) {
1807
+ function $insertTableColumn(tableNode, targetIndex, shouldInsertAfter = true, columnCount, grid) {
1736
1808
  const tableRows = tableNode.getChildren();
1737
1809
 
1738
1810
  for (let r = 0; r < tableRows.length; r++) {
@@ -1740,14 +1812,6 @@ function $insertTableColumn(tableNode, targetIndex, shouldInsertAfter = true, co
1740
1812
 
1741
1813
  if ($isTableRowNode(currentTableRowNode)) {
1742
1814
  for (let c = 0; c < columnCount; c++) {
1743
- let headerState = TableCellHeaderStates.NO_STATUS;
1744
-
1745
- if (r === 0) {
1746
- headerState |= TableCellHeaderStates.ROW;
1747
- }
1748
-
1749
- const newTableCell = $createTableCellNode(headerState);
1750
- newTableCell.append(lexical.$createParagraphNode());
1751
1815
  const tableRowChildren = currentTableRowNode.getChildren();
1752
1816
 
1753
1817
  if (targetIndex >= tableRowChildren.length || targetIndex < 0) {
@@ -1756,6 +1820,23 @@ function $insertTableColumn(tableNode, targetIndex, shouldInsertAfter = true, co
1756
1820
 
1757
1821
  const targetCell = tableRowChildren[targetIndex];
1758
1822
 
1823
+ if (!$isTableCellNode(targetCell)) {
1824
+ throw Error(`Expected table cell`);
1825
+ }
1826
+
1827
+ const {
1828
+ left,
1829
+ right
1830
+ } = $getTableCellSiblingsFromTableCellNode(targetCell, grid);
1831
+ let headerState = TableCellHeaderStates.NO_STATUS;
1832
+
1833
+ if (left && left.hasHeaderState(TableCellHeaderStates.ROW) || right && right.hasHeaderState(TableCellHeaderStates.ROW)) {
1834
+ headerState |= TableCellHeaderStates.ROW;
1835
+ }
1836
+
1837
+ const newTableCell = $createTableCellNode(headerState);
1838
+ newTableCell.append(lexical.$createParagraphNode());
1839
+
1759
1840
  if (shouldInsertAfter) {
1760
1841
  targetCell.insertAfter(newTableCell);
1761
1842
  } else {
@@ -204,6 +204,7 @@ declare export function $insertTableColumn(
204
204
  targetIndex: number,
205
205
  shouldInsertAfter?: boolean,
206
206
  columnCount: number,
207
+ grid: Grid,
207
208
  ): TableNode;
208
209
 
209
210
  declare export function $deleteTableColumn(
@@ -4,56 +4,60 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- 'use strict';var f=require("lexical"),p=require("@lexical/utils");let q={BOTH:3,COLUMN:2,NO_STATUS:0,ROW:1};
8
- class t extends f.GridCellNode{static getType(){return"tablecell"}static clone(a){return new t(a.__headerState,a.__colSpan,a.__width,a.__key)}static importDOM(){return{td:()=>({conversion:w,priority:0}),th:()=>({conversion:w,priority:0})}}static importJSON(a){return x(a.headerState,a.colSpan,a.width||void 0)}constructor(a=q.NO_STATUS,b=1,d,k){super(b,k);this.__headerState=a;this.__width=d}createDOM(a){let b=document.createElement(this.getTag());this.__width&&(b.style.width=`${this.__width}px`);p.addClassNamesToElement(b,
9
- a.theme.tableCell,this.hasHeader()&&a.theme.tableCellHeader);return b}exportDOM(a){({element:a}=super.exportDOM(a));if(a){let b=this.getParentOrThrow().getChildrenSize();a.style.border="1px solid black";a.style.width=`${this.getWidth()||Math.max(90,700/b)}px`;a.style.verticalAlign="top";a.style.textAlign="start";this.hasHeader()&&(a.style.backgroundColor="#f2f3f5")}return{element:a}}exportJSON(){return{...super.exportJSON(),colSpan:super.__colSpan,headerState:this.__headerState,type:"tablecell",width:this.getWidth()}}getTag(){return this.hasHeader()?
10
- "th":"td"}setHeaderStyles(a){this.getWritable().__headerState=a;return this.__headerState}getHeaderStyles(){return this.getLatest().__headerState}setWidth(a){this.getWritable().__width=a;return this.__width}getWidth(){return this.getLatest().__width}toggleHeaderStyle(a){let b=this.getWritable();b.__headerState=(b.__headerState&a)===a?b.__headerState-a:b.__headerState+a;return b}hasHeaderState(a){return(this.getHeaderStyles()&a)===a}hasHeader(){return this.getLatest().__headerState!==q.NO_STATUS}updateDOM(a){return a.__headerState!==
11
- this.__headerState||a.__width!==this.__width}collapseAtStart(){return!0}canBeEmpty(){return!1}canIndent(){return!1}}function w(a){a=a.nodeName.toLowerCase();return{forChild:(b,d)=>{if(y(d)&&!f.$isElementNode(b)){d=f.$createParagraphNode();if(f.$isLineBreakNode(b)&&"\n"===b.getTextContent())return null;d.append(b);return d}return b},node:x("th"===a?q.ROW:q.NO_STATUS)}}function x(a,b=1,d){return new t(a,b,d)}function y(a){return a instanceof t}
12
- class z extends f.GridRowNode{static getType(){return"tablerow"}static clone(a){return new z(a.__height,a.__key)}static importDOM(){return{tr:()=>({conversion:A,priority:0})}}static importJSON(a){return B(a.height)}constructor(a,b){super(b);this.__height=a}exportJSON(){return{...super.exportJSON(),type:"tablerow",version:1}}createDOM(a){let b=document.createElement("tr");this.__height&&(b.style.height=`${this.__height}px`);p.addClassNamesToElement(b,a.theme.tableRow);return b}setHeight(a){this.getWritable().__height=
13
- a;return this.__height}getHeight(){return this.getLatest().__height}updateDOM(a){return a.__height!==this.__height}canBeEmpty(){return!1}canIndent(){return!1}}function A(){return{node:B()}}function B(a){return new z(a)}function C(a){return a instanceof z}function D(a){throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?code=${a} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
7
+ 'use strict';var d=require("lexical"),n=require("@lexical/utils");let q={BOTH:3,COLUMN:2,NO_STATUS:0,ROW:1};
8
+ class t extends d.DEPRECATED_GridCellNode{static getType(){return"tablecell"}static clone(a){return new t(a.__headerState,a.__colSpan,a.__width,a.__key)}static importDOM(){return{td:()=>({conversion:x,priority:0}),th:()=>({conversion:x,priority:0})}}static importJSON(a){return z(a.headerState,a.colSpan,a.width||void 0)}constructor(a=q.NO_STATUS,b=1,e,h){super(b,h);this.__headerState=a;this.__width=e}createDOM(a){let b=document.createElement(this.getTag());this.__width&&(b.style.width=`${this.__width}px`);
9
+ n.addClassNamesToElement(b,a.theme.tableCell,this.hasHeader()&&a.theme.tableCellHeader);return b}exportDOM(a){({element:a}=super.exportDOM(a));if(a){let b=this.getParentOrThrow().getChildrenSize();a.style.border="1px solid black";a.style.width=`${this.getWidth()||Math.max(90,700/b)}px`;a.style.verticalAlign="top";a.style.textAlign="start";this.hasHeader()&&(a.style.backgroundColor="#f2f3f5")}return{element:a}}exportJSON(){return{...super.exportJSON(),colSpan:super.__colSpan,headerState:this.__headerState,
10
+ type:"tablecell",width:this.getWidth()}}getTag(){return this.hasHeader()?"th":"td"}setHeaderStyles(a){this.getWritable().__headerState=a;return this.__headerState}getHeaderStyles(){return this.getLatest().__headerState}setWidth(a){this.getWritable().__width=a;return this.__width}getWidth(){return this.getLatest().__width}toggleHeaderStyle(a){let b=this.getWritable();b.__headerState=(b.__headerState&a)===a?b.__headerState-a:b.__headerState+a;return b}hasHeaderState(a){return(this.getHeaderStyles()&
11
+ a)===a}hasHeader(){return this.getLatest().__headerState!==q.NO_STATUS}updateDOM(a){return a.__headerState!==this.__headerState||a.__width!==this.__width}isShadowRoot(){return!0}collapseAtStart(){return!0}canBeEmpty(){return!1}canIndent(){return!1}}
12
+ function x(a){a=a.nodeName.toLowerCase();return{forChild:(b,e)=>{if(B(e)&&!d.$isElementNode(b)){e=d.$createParagraphNode();if(d.$isLineBreakNode(b)&&"\n"===b.getTextContent())return null;e.append(b);return e}return b},node:z("th"===a?q.ROW:q.NO_STATUS)}}function z(a,b=1,e){return new t(a,b,e)}function B(a){return a instanceof t}
13
+ class C extends d.DEPRECATED_GridRowNode{static getType(){return"tablerow"}static clone(a){return new C(a.__height,a.__key)}static importDOM(){return{tr:()=>({conversion:D,priority:0})}}static importJSON(a){return E(a.height)}constructor(a,b){super(b);this.__height=a}exportJSON(){return{...super.exportJSON(),type:"tablerow",version:1}}createDOM(a){let b=document.createElement("tr");this.__height&&(b.style.height=`${this.__height}px`);n.addClassNamesToElement(b,a.theme.tableRow);return b}isShadowRoot(){return!0}setHeight(a){this.getWritable().__height=
14
+ a;return this.__height}getHeight(){return this.getLatest().__height}updateDOM(a){return a.__height!==this.__height}canBeEmpty(){return!1}canIndent(){return!1}}function D(){return{node:E()}}function E(a){return new C(a)}function F(a){return a instanceof C}function H(a){throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?code=${a} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
14
15
  if("undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement){let a=document.createElement("style");a.innerHTML="\n table.disable-selection {\n -webkit-touch-callout: none;\n -webkit-user-select: none; \n -khtml-user-select: none; \n -moz-user-select: none; \n -ms-user-select: none; \n user-select: none;\n }\n \n .disable-selection span::selection{\n background-color: transparent;\n }\n .disable-selection br::selection{\n background-color: transparent;\n }\n ";
15
16
  document.body&&document.body.append(a)}
16
- class E{constructor(a,b){this.isHighlightingCells=!1;this.currentY=this.currentX=this.startY=this.startX=-1;this.listenersToRemove=new Set;this.tableNodeKey=b;this.editor=a;this.grid={cells:[],columns:0,rows:0};this.focusCell=this.anchorCell=this.focusCellNodeKey=this.anchorCellNodeKey=this.gridSelection=null;this.hasHijackedSelectionStyles=!1;this.trackTableGrid()}getGrid(){return this.grid}removeListeners(){Array.from(this.listenersToRemove).forEach(a=>a())}trackTableGrid(){let a=new MutationObserver(b=>
17
- {this.editor.update(()=>{var d=!1;for(let k=0;k<b.length;k++){const c=b[k].target.nodeName;if("TABLE"===c||"TR"===c){d=!0;break}}if(d){d=this.editor.getElementByKey(this.tableNodeKey);if(!d)throw Error("Expected to find TableElement in DOM");this.grid=F(d)}})});this.editor.update(()=>{let b=this.editor.getElementByKey(this.tableNodeKey);if(!b)throw Error("Expected to find TableElement in DOM");this.grid=F(b);a.observe(b,{childList:!0,subtree:!0})})}clearHighlight(){this.editor.update(()=>{var a=f.$getNodeByKey(this.tableNodeKey);
18
- if(!G(a))throw Error("Expected TableNode.");a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a=F(a);this.isHighlightingCells=!1;this.currentY=this.currentX=this.startY=this.startX=-1;this.focusCell=this.anchorCell=this.focusCellNodeKey=this.anchorCellNodeKey=this.gridSelection=null;this.hasHijackedSelectionStyles=!1;H(a,null);f.$setSelection(null);this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND,void 0);this.enableHighlightStyle()})}enableHighlightStyle(){this.editor.update(()=>
19
- {let a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a.classList.remove("disable-selection");this.hasHijackedSelectionStyles=!1})}disableHighlightStyle(){this.editor.update(()=>{let a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a.classList.add("disable-selection");this.hasHijackedSelectionStyles=!0})}updateTableGridSelection(a){if(null!=a){this.gridSelection=a;this.isHighlightingCells=
20
- !0;this.disableHighlightStyle();let b=this.editor.getElementByKey(a.anchor.key);a=this.editor.getElementByKey(a.focus.key);if(b&&a){let d=window.getSelection();d&&d.setBaseAndExtent(b,0,a,0)}H(this.grid,this.gridSelection)}else this.clearHighlight()}adjustFocusCellForSelection(a,b=!1){this.editor.update(()=>{var d=f.$getNodeByKey(this.tableNodeKey);if(!G(d))throw Error("Expected TableNode.");if(!this.editor.getElementByKey(this.tableNodeKey))throw Error("Expected to find TableElement in DOM");d=a.x;
21
- let k=a.y;this.focusCell=a;let c=window.getSelection();null!==this.anchorCell&&c&&c.setBaseAndExtent(this.anchorCell.elem,0,a.elem,0);if(!this.isHighlightingCells&&(this.startX!==d||this.startY!==k||b))this.isHighlightingCells=!0,this.disableHighlightStyle();else if(d===this.currentX&&k===this.currentY)return;this.currentX=d;this.currentY=k;this.isHighlightingCells&&(d=f.$getNearestNodeFromDOMNode(a.elem),null!=this.gridSelection&&null!=this.anchorCellNodeKey&&y(d)&&(d=d.getKey(),this.gridSelection=
22
- f.$createGridSelection(),this.focusCellNodeKey=d,this.gridSelection.set(this.tableNodeKey,this.anchorCellNodeKey,this.focusCellNodeKey),f.$setSelection(this.gridSelection),this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND,void 0),H(this.grid,this.gridSelection)))})}setAnchorCellForSelection(a){this.editor.update(()=>{this.anchorCell=a;this.startX=a.x;this.startY=a.y;var b=window.getSelection();b&&b.setBaseAndExtent(a.elem,0,a.elem,0);b=f.$getNearestNodeFromDOMNode(a.elem);y(b)&&(b=b.getKey(),
23
- this.gridSelection=f.$createGridSelection(),this.anchorCellNodeKey=b)})}formatCells(a){this.editor.update(()=>{let b=f.$getSelection();f.$isGridSelection(b)||D(11);let d=f.$createRangeSelection(),k=d.anchor,c=d.focus;b.getNodes().forEach(h=>{y(h)&&0!==h.getTextContentSize()&&(k.set(h.getKey(),0,"element"),c.set(h.getKey(),h.getChildrenSize(),"element"),d.formatText(a))});f.$setSelection(b);this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND,void 0)})}clearText(){this.editor.update(()=>{let a=f.$getNodeByKey(this.tableNodeKey);
24
- if(!G(a))throw Error("Expected TableNode.");var b=f.$getSelection();f.$isGridSelection(b)||D(11);b=b.getNodes().filter(y);b.length===this.grid.columns*this.grid.rows?(a.selectPrevious(),a.remove(),this.clearHighlight()):(b.forEach(d=>{if(f.$isElementNode(d)){let k=f.$createParagraphNode(),c=f.$createTextNode();k.append(c);d.append(k);d.getChildren().forEach(h=>{h!==k&&h.remove()})}}),H(this.grid,null),f.$setSelection(null),this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND,void 0))})}}
25
- function I(a){for(;null!=a;){let b=a.nodeName;if("TD"===b||"TH"===b){a=a._cell;if(void 0===a)break;return a}a=a.parentNode}return null}
26
- function F(a){let b=[],d={cells:b,columns:0,rows:0};var k=a.firstChild;let c=a=0;for(b.length=0;null!=k;){var h=k.nodeName;if("TD"===h||"TH"===h)h={elem:k,highlighted:!1,x:a,y:c},k._cell=h,void 0===b[c]&&(b[c]=[]),b[c][a]=h;else if(h=k.firstChild,null!=h){k=h;continue}h=k.nextSibling;if(null!=h)a++,k=h;else if(h=k.parentNode,null!=h){k=h.nextSibling;if(null==k)break;c++;a=0}}d.columns=a+1;d.rows=c+1;return d}
27
- function H(a,b){let d=[],k=new Set(b?b.getNodes():[]);J(a,(c,h)=>{let n=c.elem;k.has(h)?(c.highlighted=!0,n.style.setProperty("background-color","rgb(172, 206, 247)"),n.style.setProperty("caret-color","transparent"),d.push(c)):(c.highlighted=!1,n.style.removeProperty("background-color"),n.style.removeProperty("caret-color"),n.getAttribute("style")||n.removeAttribute("style"))});return d}
28
- function J(a,b){({cells:a}=a);for(let d=0;d<a.length;d++){let k=a[d];for(let c=0;c<k.length;c++){let h=k[c],n=f.$getNearestNodeFromDOMNode(h.elem);null!==n&&b(h,n,{x:c,y:d})}}}function K(a){a.disableHighlightStyle();J(a.grid,b=>{let d=b.elem;b.highlighted=!0;d.style.setProperty("background-color","rgb(172, 206, 247)");d.style.setProperty("caret-color","transparent")})}
29
- function O(a){a.enableHighlightStyle();J(a.grid,b=>{let d=b.elem;b.highlighted=!1;d.style.removeProperty("background-color");d.style.removeProperty("caret-color");d.getAttribute("style")||d.removeAttribute("style")})}
30
- let Q=(a,b,d,k,c)=>{const h="forward"===c;switch(c){case "backward":case "forward":return d!==(h?a.grid.columns-1:0)?P(b.getCellNodeFromCordsOrThrow(d+(h?1:-1),k,a.grid)):k!==(h?a.grid.rows-1:0)?P(b.getCellNodeFromCordsOrThrow(h?0:a.grid.columns-1,k+(h?1:-1),a.grid)):h?b.selectNext():b.selectPrevious(),!0;case "up":return 0!==k?P(b.getCellNodeFromCordsOrThrow(d,k-1,a.grid)):b.selectPrevious(),!0;case "down":return k!==a.grid.rows-1?P(b.getCellNodeFromCordsOrThrow(d,k+1,a.grid)):b.selectNext(),!0;
31
- default:return!1}},R=(a,b,d,k,c)=>{const h="forward"===c;switch(c){case "backward":case "forward":return d!==(h?a.grid.columns-1:0)&&a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(d+(h?1:-1),k,a.grid)),!0;case "up":return 0!==k?(a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(d,k-1,a.grid)),!0):!1;case "down":return k!==a.grid.rows-1?(a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(d,k+1,a.grid)),!0):!1;default:return!1}};
32
- function S(a,b){if(f.$isRangeSelection(a)||f.$isGridSelection(a)){let d=b.isParentOf(a.anchor.getNode());a=b.isParentOf(a.focus.getNode());return d&&a}return!1}function P(a){let b=a.getChildren().find(d=>f.$isParagraphNode(d));f.$isParagraphNode(b)?b.selectEnd():a.selectEnd()}
33
- class T extends f.GridNode{static getType(){return"table"}static clone(a){return new T(a.__key)}static importDOM(){return{table:()=>({conversion:U,priority:0})}}static importJSON(){return V()}constructor(a){super(a)}exportJSON(){return{...super.exportJSON(),type:"table",version:1}}createDOM(a){let b=document.createElement("table");p.addClassNamesToElement(b,a.theme.table);return b}updateDOM(){return!1}exportDOM(a){return{...super.exportDOM(a),after:b=>{if(b){let d=b.cloneNode(),k=document.createElement("colgroup"),
34
- c=document.createElement("tbody");c.append(...b.children);b=this.getFirstChildOrThrow();if(!C(b))throw Error("Expected to find row node.");b=b.getChildrenSize();for(let h=0;h<b;h++){let n=document.createElement("col");k.append(n)}d.replaceChildren(k,c);return d}}}}canExtractContents(){return!1}canBeEmpty(){return!1}getCordsFromCellNode(a,b){let {rows:d,cells:k}=b;for(b=0;b<d;b++){var c=k[b];if(null==c)throw Error(`Row not found at y:${b}`);c=c.findIndex(({elem:h})=>f.$getNearestNodeFromDOMNode(h)===
35
- a);if(-1!==c)return{x:c,y:b}}throw Error("Cell not found in table.");}getCellFromCords(a,b,d){({cells:d}=d);b=d[b];if(null==b)return null;a=b[a];return null==a?null:a}getCellFromCordsOrThrow(a,b,d){a=this.getCellFromCords(a,b,d);if(!a)throw Error("Cell not found at cords.");return a}getCellNodeFromCords(a,b,d){a=this.getCellFromCords(a,b,d);if(null==a)return null;a=f.$getNearestNodeFromDOMNode(a.elem);return y(a)?a:null}getCellNodeFromCordsOrThrow(a,b,d){a=this.getCellNodeFromCords(a,b,d);if(!a)throw Error("Node at cords not TableCellNode.");
36
- return a}canSelectBefore(){return!0}canIndent(){return!1}}function U(){return{node:V()}}function V(){return new T}function G(a){return a instanceof T}function W(a){a=p.$findMatchingParent(a,b=>C(b));if(C(a))return a;throw Error("Expected table cell to be inside of table row.");}function X(a){a=p.$findMatchingParent(a,b=>G(b));if(G(a))return a;throw Error("Expected table cell to be inside of table.");}let Y=f.createCommand();exports.$createTableCellNode=x;exports.$createTableNode=V;
37
- exports.$createTableNodeWithDimensions=function(a,b,d=!0){let k=V();for(let h=0;h<a;h++){let n=B();for(let r=0;r<b;r++){var c=q.NO_STATUS;d&&(0===h&&(c|=q.ROW),0===r&&(c|=q.COLUMN));c=x(c);let v=f.$createParagraphNode();v.append(f.$createTextNode());c.append(v);n.append(c)}k.append(n)}return k};exports.$createTableRowNode=B;
38
- exports.$deleteTableColumn=function(a,b){let d=a.getChildren();for(let c=0;c<d.length;c++){var k=d[c];if(C(k)){k=k.getChildren();if(b>=k.length||0>b)throw Error("Table column target index out of range");k[b].remove()}}return a};exports.$getElementGridForTableNode=function(a,b){a=a.getElementByKey(b.getKey());if(null==a)throw Error("Table Element Not Found");return F(a)};exports.$getTableCellNodeFromLexicalNode=function(a){a=p.$findMatchingParent(a,b=>y(b));return y(a)?a:null};
39
- exports.$getTableColumnIndexFromTableCellNode=function(a){return W(a).getChildren().findIndex(b=>b.is(a))};exports.$getTableNodeFromLexicalNodeOrThrow=X;exports.$getTableRowIndexFromTableCellNode=function(a){let b=W(a);return X(b).getChildren().findIndex(d=>d.is(b))};exports.$getTableRowNodeFromTableCellNodeOrThrow=W;
40
- exports.$insertTableColumn=function(a,b,d=!0,k){let c=a.getChildren();for(let r=0;r<c.length;r++){let v=c[r];if(C(v))for(let g=0;g<k;g++){var h=q.NO_STATUS;0===r&&(h|=q.ROW);h=x(h);h.append(f.$createParagraphNode());var n=v.getChildren();if(b>=n.length||0>b)throw Error("Table column target index out of range");n=n[b];d?n.insertAfter(h):n.insertBefore(h)}}return a};
41
- exports.$insertTableRow=function(a,b,d=!0,k,c){var h=a.getChildren();if(b>=h.length||0>b)throw Error("Table row target index out of range");b=h[b];if(C(b))for(h=0;h<k;h++){let v=b.getChildren(),g=v.length,e=B();for(let l=0;l<g;l++){var n=v[l];y(n)||D(12);var r=c;let m=X(n),{x:u,y:L}=m.getCordsFromCellNode(n,r);n={above:m.getCellNodeFromCords(u,L-1,r),below:m.getCellNodeFromCords(u,L+1,r),left:m.getCellNodeFromCords(u-1,L,r),right:m.getCellNodeFromCords(u+1,L,r)};let {above:M,below:N}=n;n=q.NO_STATUS;
42
- r=M&&M.getWidth()||N&&N.getWidth()||void 0;if(M&&M.hasHeaderState(q.COLUMN)||N&&N.hasHeaderState(q.COLUMN))n|=q.COLUMN;n=x(n,1,r);n.append(f.$createParagraphNode());e.append(n)}d?b.insertAfter(e):b.insertBefore(e)}else throw Error("Row before insertion index does not exist.");return a};exports.$isTableCellNode=y;exports.$isTableNode=G;exports.$isTableRowNode=C;
43
- exports.$removeTableRowAtIndex=function(a,b){let d=a.getChildren();if(b>=d.length||0>b)throw Error("Expected table cell to be inside of table row.");d[b].remove();return a};exports.INSERT_TABLE_COMMAND=Y;exports.TableCellHeaderStates=q;exports.TableCellNode=t;exports.TableNode=T;exports.TableRowNode=z;exports.TableSelection=E;
44
- exports.applyTableHandlers=function(a,b,d){let k=d.getRootElement();if(null===k)throw Error("No root element.");let c=new E(d,a.getKey());b.__lexicalTableSelection=c;let h=!1,n=!1;b.addEventListener("dblclick",g=>{let e=I(g.target);null!==e&&(g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),c.setAnchorCellForSelection(e),c.adjustFocusCellForSelection(e,!0),h=!1)});b.addEventListener("mousedown",g=>{setTimeout(()=>{if(0===g.button){var e=I(g.target);null!==e&&(c.setAnchorCellForSelection(e),
45
- document.addEventListener("mouseup",()=>{h=!1},{capture:!0,once:!0}))}},0)});b.addEventListener("mousemove",g=>{n&&(g.preventDefault(),g.stopPropagation(),g.stopImmediatePropagation());if(h){let e=I(g.target);if(null!==e){let l=e.x,m=e.y;h&&(c.startX!==l||c.startY!==m||c.isHighlightingCells)&&(g.preventDefault(),h=!0,c.adjustFocusCellForSelection(e))}}});b.addEventListener("mouseup",()=>{h&&(h=!1)});b.addEventListener("mouseleave",()=>{});let r=g=>{h=!0;0===g.button&&d.update(()=>{const e=f.$getSelection();
46
- if(f.$isGridSelection(e)&&e.gridKey===c.tableNodeKey&&k.contains(g.target))return c.clearHighlight()})};window.addEventListener("mousedown",r);c.listenersToRemove.add(()=>window.removeEventListener("mousedown",r));let v=()=>{h=!1};window.addEventListener("mouseup",v);c.listenersToRemove.add(()=>window.removeEventListener("mouseup",v));c.listenersToRemove.add(d.registerCommand(f.KEY_ARROW_DOWN_COMMAND,g=>{var e=f.$getSelection();if(!S(e,a))return!1;if(f.$isRangeSelection(e)){if(e.isCollapsed()){var l=
47
- p.$findMatchingParent(e.anchor.getNode(),u=>y(u));if(!y(l))return!1;var m=a.getCordsFromCellNode(l,c.grid);e=p.$findMatchingParent(e.anchor.getNode(),u=>f.$isElementNode(u));if(null==e)throw Error("Expected BlockNode Parent");if((l=l.getLastChild())&&e.isParentOf(l)||e===l||g.shiftKey)return g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(m.x,m.y,c.grid)),R(c,a,m.x,m.y,"down")):Q(c,a,m.x,m.y,"down")}}else if(f.$isGridSelection(e)&&
48
- g.shiftKey){m=e.focus.getNode();if(!y(m))return!1;m=a.getCordsFromCellNode(m,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();return R(c,a,m.x,m.y,"down")}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.KEY_ARROW_UP_COMMAND,g=>{var e=f.$getSelection();if(!S(e,a))return!1;if(f.$isRangeSelection(e)){if(e.isCollapsed()){var l=p.$findMatchingParent(e.anchor.getNode(),u=>y(u));if(!y(l))return!1;var m=a.getCordsFromCellNode(l,c.grid);e=p.$findMatchingParent(e.anchor.getNode(),
49
- u=>f.$isElementNode(u));if(null==e)throw Error("Expected BlockNode Parent");if((l=l.getLastChild())&&e.isParentOf(l)||e===l||g.shiftKey)return g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(m.x,m.y,c.grid)),R(c,a,m.x,m.y,"up")):Q(c,a,m.x,m.y,"up")}}else if(f.$isGridSelection(e)&&g.shiftKey){m=e.focus.getNode();if(!y(m))return!1;m=a.getCordsFromCellNode(m,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();
50
- return R(c,a,m.x,m.y,"up")}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.KEY_ARROW_LEFT_COMMAND,g=>{var e=f.$getSelection();if(!S(e,a))return!1;if(f.$isRangeSelection(e)){if(e.isCollapsed()){var l=p.$findMatchingParent(e.anchor.getNode(),m=>y(m));if(!y(l))return!1;l=a.getCordsFromCellNode(l,c.grid);if(null==p.$findMatchingParent(e.anchor.getNode(),m=>f.$isElementNode(m)))throw Error("Expected BlockNode Parent");if(0===e.anchor.offset||g.shiftKey)return g.preventDefault(),
51
- g.stopImmediatePropagation(),g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(l.x,l.y,c.grid)),R(c,a,l.x,l.y,"backward")):Q(c,a,l.x,l.y,"backward")}}else if(f.$isGridSelection(e)&&g.shiftKey){e=e.focus.getNode();if(!y(e))return!1;e=a.getCordsFromCellNode(e,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();return R(c,a,e.x,e.y,"backward")}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.KEY_ARROW_RIGHT_COMMAND,
52
- g=>{var e=f.$getSelection();if(!S(e,a))return!1;if(f.$isRangeSelection(e)){if(e.isCollapsed()){var l=p.$findMatchingParent(e.anchor.getNode(),m=>y(m));if(!y(l))return!1;l=a.getCordsFromCellNode(l,c.grid);if(null==p.$findMatchingParent(e.anchor.getNode(),m=>f.$isElementNode(m)))throw Error("Expected BlockNode Parent");if(e.anchor.offset===e.anchor.getNode().getTextContentSize()||g.shiftKey)return g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(l.x,
53
- l.y,c.grid)),R(c,a,l.x,l.y,"forward")):Q(c,a,l.x,l.y,"forward")}}else if(f.$isGridSelection(e)&&g.shiftKey){e=e.focus.getNode();if(!y(e))return!1;e=a.getCordsFromCellNode(e,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();return R(c,a,e.x,e.y,"forward")}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.DELETE_CHARACTER_COMMAND,()=>{let g=f.$getSelection();if(!S(g,a))return!1;if(f.$isGridSelection(g))return c.clearText(),!0;if(f.$isRangeSelection(g)){var e=
54
- p.$findMatchingParent(g.anchor.getNode(),l=>y(l));if(!y(e))return!1;e=p.$findMatchingParent(g.anchor.getNode(),l=>f.$isParagraphNode(l));if(!f.$isParagraphNode(e))return!1;if(g.isCollapsed()&&0===g.anchor.offset&&0===e.getPreviousSiblings().length)return!0}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.KEY_BACKSPACE_COMMAND,g=>{let e=f.$getSelection();if(!S(e,a))return!1;if(f.$isGridSelection(e))return g.preventDefault(),g.stopPropagation(),c.clearText(),!0;f.$isRangeSelection(e)&&
55
- (g=p.$findMatchingParent(e.anchor.getNode(),l=>y(l)),y(g));return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.FORMAT_TEXT_COMMAND,g=>{let e=f.$getSelection();if(!S(e,a))return!1;if(f.$isGridSelection(e))return c.formatCells(g),!0;f.$isRangeSelection(e)&&(g=p.$findMatchingParent(e.anchor.getNode(),l=>y(l)),y(g));return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.CONTROLLED_TEXT_INSERTION_COMMAND,()=>{var g=f.$getSelection();if(!S(g,
56
- a))return!1;f.$isGridSelection(g)?c.clearHighlight():f.$isRangeSelection(g)&&(g=p.$findMatchingParent(g.anchor.getNode(),e=>y(e)),y(g));return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.KEY_TAB_COMMAND,g=>{var e=f.$getSelection();if(!S(e,a))return!1;if(f.$isRangeSelection(e)){let l=p.$findMatchingParent(e.anchor.getNode(),m=>y(m));if(!y(l))return!1;if(e.isCollapsed())return e=a.getCordsFromCellNode(l,c.grid),g.preventDefault(),Q(c,a,e.x,e.y,g.shiftKey?"backward":
57
- "forward"),!0}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.FOCUS_COMMAND,()=>a.isSelected(),f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.SELECTION_CHANGE_COMMAND,()=>{let g=f.$getSelection();var e=f.$getPreviousSelection();if(g!==e&&(f.$isGridSelection(g)||f.$isGridSelection(e))&&c.gridSelection!==g)return c.updateTableGridSelection(f.$isGridSelection(g)&&a.isSelected()?g:null),!1;if(g&&f.$isRangeSelection(g)&&!g.isCollapsed()){var l=
58
- g.anchor.getNode();e=g.focus.getNode();l=a.isParentOf(l);e=a.isParentOf(e);if(l&&!e||e&&!l){e=g.isBackward();l=f.$createRangeSelection();let m=a.getIndexWithinParent(),u=a.getParentOrThrow().getKey();l.anchor.set(g.anchor.key,g.anchor.offset,g.anchor.type);l.focus.set(u,e?m-1:m+1,"element");n=!0;f.$setSelection(l);K(c);return!0}}c.hasHijackedSelectionStyles&&!a.isSelected()?(O(c),n=!1):!c.hasHijackedSelectionStyles&&a.isSelected()&&K(c);return!1},f.COMMAND_PRIORITY_CRITICAL));return c};
59
- exports.getCellFromTarget=I;exports.getTableSelectionFromTableElement=function(a){return a.__lexicalTableSelection}
17
+ class I{constructor(a,b){this.isHighlightingCells=!1;this.currentY=this.currentX=this.startY=this.startX=-1;this.listenersToRemove=new Set;this.tableNodeKey=b;this.editor=a;this.grid={cells:[],columns:0,rows:0};this.focusCell=this.anchorCell=this.focusCellNodeKey=this.anchorCellNodeKey=this.gridSelection=null;this.hasHijackedSelectionStyles=!1;this.trackTableGrid()}getGrid(){return this.grid}removeListeners(){Array.from(this.listenersToRemove).forEach(a=>a())}trackTableGrid(){let a=new MutationObserver(b=>
18
+ {this.editor.update(()=>{var e=!1;for(let h=0;h<b.length;h++){const c=b[h].target.nodeName;if("TABLE"===c||"TR"===c){e=!0;break}}if(e){e=this.editor.getElementByKey(this.tableNodeKey);if(!e)throw Error("Expected to find TableElement in DOM");this.grid=J(e)}})});this.editor.update(()=>{let b=this.editor.getElementByKey(this.tableNodeKey);if(!b)throw Error("Expected to find TableElement in DOM");this.grid=J(b);a.observe(b,{childList:!0,subtree:!0})})}clearHighlight(){this.editor.update(()=>{var a=d.$getNodeByKey(this.tableNodeKey);
19
+ if(!K(a))throw Error("Expected TableNode.");a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a=J(a);this.isHighlightingCells=!1;this.currentY=this.currentX=this.startY=this.startX=-1;this.focusCell=this.anchorCell=this.focusCellNodeKey=this.anchorCellNodeKey=this.gridSelection=null;this.hasHijackedSelectionStyles=!1;L(a,null);d.$setSelection(null);this.editor.dispatchCommand(d.SELECTION_CHANGE_COMMAND,void 0);this.enableHighlightStyle()})}enableHighlightStyle(){this.editor.update(()=>
20
+ {let a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a.classList.remove("disable-selection");this.hasHijackedSelectionStyles=!1})}disableHighlightStyle(){this.editor.update(()=>{let a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a.classList.add("disable-selection");this.hasHijackedSelectionStyles=!0})}updateTableGridSelection(a){null!=a&&a.gridKey===this.tableNodeKey&&(this.gridSelection=
21
+ a,this.isHighlightingCells=!0,this.disableHighlightStyle(),L(this.grid,this.gridSelection));null==a&&this.clearHighlight()}adjustFocusCellForSelection(a,b=!1){this.editor.update(()=>{var e=d.$getNodeByKey(this.tableNodeKey);if(!K(e))throw Error("Expected TableNode.");if(!this.editor.getElementByKey(this.tableNodeKey))throw Error("Expected to find TableElement in DOM");e=a.x;let h=a.y;this.focusCell=a;if(null!==this.anchorCell){let c=window.getSelection();c&&c.setBaseAndExtent(this.anchorCell.elem,
22
+ 0,this.anchorCell.elem,0)}if(!this.isHighlightingCells&&(this.startX!==e||this.startY!==h||b))this.isHighlightingCells=!0,this.disableHighlightStyle();else if(e===this.currentX&&h===this.currentY)return;this.currentX=e;this.currentY=h;this.isHighlightingCells&&(e=d.$getNearestNodeFromDOMNode(a.elem),null!=this.gridSelection&&null!=this.anchorCellNodeKey&&B(e)&&(e=e.getKey(),this.gridSelection=d.DEPRECATED_$createGridSelection(),this.focusCellNodeKey=e,this.gridSelection.set(this.tableNodeKey,this.anchorCellNodeKey,
23
+ this.focusCellNodeKey),d.$setSelection(this.gridSelection),this.editor.dispatchCommand(d.SELECTION_CHANGE_COMMAND,void 0),L(this.grid,this.gridSelection)))})}setAnchorCellForSelection(a){this.editor.update(()=>{if(this.anchorCell===a&&this.isHighlightingCells){var b=window.getSelection();b&&b.setBaseAndExtent(a.elem,0,a.elem,0)}this.anchorCell=a;this.startX=a.x;this.startY=a.y;b=d.$getNearestNodeFromDOMNode(a.elem);B(b)&&(b=b.getKey(),this.gridSelection=d.DEPRECATED_$createGridSelection(),this.anchorCellNodeKey=
24
+ b)})}formatCells(a){this.editor.update(()=>{let b=d.$getSelection();d.DEPRECATED_$isGridSelection(b)||H(11);let e=d.$createRangeSelection(),h=e.anchor,c=e.focus;b.getNodes().forEach(l=>{B(l)&&0!==l.getTextContentSize()&&(h.set(l.getKey(),0,"element"),c.set(l.getKey(),l.getChildrenSize(),"element"),e.formatText(a))});d.$setSelection(b);this.editor.dispatchCommand(d.SELECTION_CHANGE_COMMAND,void 0)})}clearText(){this.editor.update(()=>{let a=d.$getNodeByKey(this.tableNodeKey);if(!K(a))throw Error("Expected TableNode.");
25
+ var b=d.$getSelection();d.DEPRECATED_$isGridSelection(b)||H(11);b=b.getNodes().filter(B);b.length===this.grid.columns*this.grid.rows?(a.selectPrevious(),a.remove(),d.$getRoot().selectStart()):(b.forEach(e=>{if(d.$isElementNode(e)){let h=d.$createParagraphNode(),c=d.$createTextNode();h.append(c);e.append(h);e.getChildren().forEach(l=>{l!==h&&l.remove()})}}),L(this.grid,null),d.$setSelection(null),this.editor.dispatchCommand(d.SELECTION_CHANGE_COMMAND,void 0))})}}
26
+ function M(a){for(;null!=a;){let b=a.nodeName;if("TD"===b||"TH"===b){a=a._cell;if(void 0===a)break;return a}a=a.parentNode}return null}
27
+ function J(a){let b=[],e={cells:b,columns:0,rows:0};var h=a.firstChild;let c=a=0;for(b.length=0;null!=h;){var l=h.nodeName;if("TD"===l||"TH"===l)l={elem:h,highlighted:!1,x:a,y:c},h._cell=l,void 0===b[c]&&(b[c]=[]),b[c][a]=l;else if(l=h.firstChild,null!=l){h=l;continue}l=h.nextSibling;if(null!=l)a++,h=l;else if(l=h.parentNode,null!=l){h=l.nextSibling;if(null==h)break;c++;a=0}}e.columns=a+1;e.rows=c+1;return e}
28
+ function L(a,b){let e=[],h=new Set(b?b.getNodes():[]);N(a,(c,l)=>{let p=c.elem;h.has(l)?(c.highlighted=!0,p.style.setProperty("background-color","rgb(172, 206, 247)"),p.style.setProperty("caret-color","transparent"),e.push(c)):(c.highlighted=!1,p.style.removeProperty("background-color"),p.style.removeProperty("caret-color"),p.getAttribute("style")||p.removeAttribute("style"))});return e}
29
+ function N(a,b){({cells:a}=a);for(let e=0;e<a.length;e++){let h=a[e];for(let c=0;c<h.length;c++){let l=h[c],p=d.$getNearestNodeFromDOMNode(l.elem);null!==p&&b(l,p,{x:c,y:e})}}}function O(a){a.disableHighlightStyle();N(a.grid,b=>{let e=b.elem;b.highlighted=!0;e.style.setProperty("background-color","rgb(172, 206, 247)");e.style.setProperty("caret-color","transparent")})}
30
+ function P(a){a.enableHighlightStyle();N(a.grid,b=>{let e=b.elem;b.highlighted=!1;e.style.removeProperty("background-color");e.style.removeProperty("caret-color");e.getAttribute("style")||e.removeAttribute("style")})}
31
+ let R=(a,b,e,h,c)=>{const l="forward"===c;switch(c){case "backward":case "forward":return e!==(l?a.grid.columns-1:0)?Q(b.getCellNodeFromCordsOrThrow(e+(l?1:-1),h,a.grid)):h!==(l?a.grid.rows-1:0)?Q(b.getCellNodeFromCordsOrThrow(l?0:a.grid.columns-1,h+(l?1:-1),a.grid)):l?b.selectNext():b.selectPrevious(),!0;case "up":return 0!==h?Q(b.getCellNodeFromCordsOrThrow(e,h-1,a.grid)):b.selectPrevious(),!0;case "down":return h!==a.grid.rows-1?Q(b.getCellNodeFromCordsOrThrow(e,h+1,a.grid)):b.selectNext(),!0;
32
+ default:return!1}},S=(a,b,e,h,c)=>{const l="forward"===c;switch(c){case "backward":case "forward":return e!==(l?a.grid.columns-1:0)&&a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(e+(l?1:-1),h,a.grid)),!0;case "up":return 0!==h?(a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(e,h-1,a.grid)),!0):!1;case "down":return h!==a.grid.rows-1?(a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(e,h+1,a.grid)),!0):!1;default:return!1}};
33
+ function T(a,b){if(d.$isRangeSelection(a)||d.DEPRECATED_$isGridSelection(a)){let e=b.isParentOf(a.anchor.getNode());a=b.isParentOf(a.focus.getNode());return e&&a}return!1}function Q(a){let b=a.getChildren().find(e=>d.$isParagraphNode(e));d.$isParagraphNode(b)?b.selectEnd():a.selectEnd()}
34
+ class U extends d.DEPRECATED_GridNode{static getType(){return"table"}static clone(a){return new U(a.__key)}static importDOM(){return{table:()=>({conversion:aa,priority:1})}}static importJSON(){return V()}constructor(a){super(a)}exportJSON(){return{...super.exportJSON(),type:"table",version:1}}createDOM(a){let b=document.createElement("table");n.addClassNamesToElement(b,a.theme.table);return b}updateDOM(){return!1}exportDOM(a){return{...super.exportDOM(a),after:b=>{if(b){let e=b.cloneNode(),h=document.createElement("colgroup"),
35
+ c=document.createElement("tbody");c.append(...b.children);b=this.getFirstChildOrThrow();if(!F(b))throw Error("Expected to find row node.");b=b.getChildrenSize();for(let l=0;l<b;l++){let p=document.createElement("col");h.append(p)}e.replaceChildren(h,c);return e}}}}canExtractContents(){return!1}canBeEmpty(){return!1}isShadowRoot(){return!0}getCordsFromCellNode(a,b){let {rows:e,cells:h}=b;for(b=0;b<e;b++){var c=h[b];if(null==c)throw Error(`Row not found at y:${b}`);c=c.findIndex(({elem:l})=>d.$getNearestNodeFromDOMNode(l)===
36
+ a);if(-1!==c)return{x:c,y:b}}throw Error("Cell not found in table.");}getCellFromCords(a,b,e){({cells:e}=e);b=e[b];if(null==b)return null;a=b[a];return null==a?null:a}getCellFromCordsOrThrow(a,b,e){a=this.getCellFromCords(a,b,e);if(!a)throw Error("Cell not found at cords.");return a}getCellNodeFromCords(a,b,e){a=this.getCellFromCords(a,b,e);if(null==a)return null;a=d.$getNearestNodeFromDOMNode(a.elem);return B(a)?a:null}getCellNodeFromCordsOrThrow(a,b,e){a=this.getCellNodeFromCords(a,b,e);if(!a)throw Error("Node at cords not TableCellNode.");
37
+ return a}canSelectBefore(){return!0}canIndent(){return!1}}function aa(){return{node:V()}}function V(){return new U}function K(a){return a instanceof U}function W(a){a=n.$findMatchingParent(a,b=>F(b));if(F(a))return a;throw Error("Expected table cell to be inside of table row.");}function Y(a){a=n.$findMatchingParent(a,b=>K(b));if(K(a))return a;throw Error("Expected table cell to be inside of table.");}
38
+ function Z(a,b){let e=Y(a),{x:h,y:c}=e.getCordsFromCellNode(a,b);return{above:e.getCellNodeFromCords(h,c-1,b),below:e.getCellNodeFromCords(h,c+1,b),left:e.getCellNodeFromCords(h-1,c,b),right:e.getCellNodeFromCords(h+1,c,b)}}let ba=d.createCommand();exports.$createTableCellNode=z;exports.$createTableNode=V;
39
+ exports.$createTableNodeWithDimensions=function(a,b,e=!0){let h=V();for(let l=0;l<a;l++){let p=E();for(let u=0;u<b;u++){var c=q.NO_STATUS;e&&(0===l&&(c|=q.ROW),0===u&&(c|=q.COLUMN));c=z(c);let v=d.$createParagraphNode();v.append(d.$createTextNode());c.append(v);p.append(c)}h.append(p)}return h};exports.$createTableRowNode=E;
40
+ exports.$deleteTableColumn=function(a,b){let e=a.getChildren();for(let c=0;c<e.length;c++){var h=e[c];if(F(h)){h=h.getChildren();if(b>=h.length||0>b)throw Error("Table column target index out of range");h[b].remove()}}return a};exports.$getElementGridForTableNode=function(a,b){a=a.getElementByKey(b.getKey());if(null==a)throw Error("Table Element Not Found");return J(a)};exports.$getTableCellNodeFromLexicalNode=function(a){a=n.$findMatchingParent(a,b=>B(b));return B(a)?a:null};
41
+ exports.$getTableColumnIndexFromTableCellNode=function(a){return W(a).getChildren().findIndex(b=>b.is(a))};exports.$getTableNodeFromLexicalNodeOrThrow=Y;exports.$getTableRowIndexFromTableCellNode=function(a){let b=W(a);return Y(b).getChildren().findIndex(e=>e.is(b))};exports.$getTableRowNodeFromTableCellNodeOrThrow=W;
42
+ exports.$insertTableColumn=function(a,b,e=!0,h,c){let l=a.getChildren();for(let v=0;v<l.length;v++){let A=l[v];if(F(A))for(let y=0;y<h;y++){var p=A.getChildren();if(b>=p.length||0>b)throw Error("Table column target index out of range");p=p[b];B(p)||H(12);let {left:g,right:f}=Z(p,c);var u=q.NO_STATUS;if(g&&g.hasHeaderState(q.ROW)||f&&f.hasHeaderState(q.ROW))u|=q.ROW;u=z(u);u.append(d.$createParagraphNode());e?p.insertAfter(u):p.insertBefore(u)}}return a};
43
+ exports.$insertTableRow=function(a,b,e=!0,h,c){var l=a.getChildren();if(b>=l.length||0>b)throw Error("Table row target index out of range");b=l[b];if(F(b))for(l=0;l<h;l++){let u=b.getChildren(),v=u.length,A=E();for(let y=0;y<v;y++){var p=u[y];B(p)||H(12);let {above:g,below:f}=Z(p,c);p=q.NO_STATUS;let k=g&&g.getWidth()||f&&f.getWidth()||void 0;if(g&&g.hasHeaderState(q.COLUMN)||f&&f.hasHeaderState(q.COLUMN))p|=q.COLUMN;p=z(p,1,k);p.append(d.$createParagraphNode());A.append(p)}e?b.insertAfter(A):b.insertBefore(A)}else throw Error("Row before insertion index does not exist.");
44
+ return a};exports.$isTableCellNode=B;exports.$isTableNode=K;exports.$isTableRowNode=F;exports.$removeTableRowAtIndex=function(a,b){let e=a.getChildren();if(b>=e.length||0>b)throw Error("Expected table cell to be inside of table row.");e[b].remove();return a};exports.INSERT_TABLE_COMMAND=ba;exports.TableCellHeaderStates=q;exports.TableCellNode=t;exports.TableNode=U;exports.TableRowNode=C;exports.TableSelection=I;
45
+ exports.applyTableHandlers=function(a,b,e){let h=e.getRootElement();if(null===h)throw Error("No root element.");let c=new I(e,a.getKey());b.__lexicalTableSelection=c;let l=!1,p=!1;b.addEventListener("dblclick",g=>{let f=M(g.target);null!==f&&(g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),c.setAnchorCellForSelection(f),c.adjustFocusCellForSelection(f,!0),l=!1)});b.addEventListener("mousedown",g=>{setTimeout(()=>{if(0===g.button){var f=M(g.target);null!==f&&(g.preventDefault(),
46
+ g.stopPropagation(),g.stopImmediatePropagation(),c.setAnchorCellForSelection(f))}},0)});b.addEventListener("mousemove",g=>{p&&(g.preventDefault(),g.stopPropagation(),g.stopImmediatePropagation());if(l){let f=M(g.target);if(null!==f){let k=f.x,m=f.y;l&&(c.startX!==k||c.startY!==m||c.isHighlightingCells)&&(g.preventDefault(),l=!0,c.adjustFocusCellForSelection(f))}}});b.addEventListener("mouseleave",()=>{});let u=g=>{l=!0;0===g.button&&e.update(()=>{const f=d.$getSelection();if(d.DEPRECATED_$isGridSelection(f)&&
47
+ f.gridKey===c.tableNodeKey&&h.contains(g.target))return c.clearHighlight()})};window.addEventListener("mousedown",u);c.listenersToRemove.add(()=>window.removeEventListener("mousedown",u));let v=g=>{l&&(g.preventDefault(),g.stopPropagation(),g.stopImmediatePropagation(),l=!1)};window.addEventListener("mouseup",v);c.listenersToRemove.add(()=>window.removeEventListener("mouseup",v));c.listenersToRemove.add(()=>b.addEventListener("mouseup",v));c.listenersToRemove.add(()=>b.removeEventListener("mouseup",
48
+ v));c.listenersToRemove.add(e.registerCommand(d.KEY_ARROW_DOWN_COMMAND,g=>{var f=d.$getSelection();if(!T(f,a))return!1;if(d.$isRangeSelection(f)){if(f.isCollapsed()){var k=n.$findMatchingParent(f.anchor.getNode(),r=>B(r));if(!B(k))return!1;var m=a.getCordsFromCellNode(k,c.grid);f=n.$findMatchingParent(f.anchor.getNode(),r=>d.$isElementNode(r));if(null==f)throw Error("Expected BlockNode Parent");if((k=k.getLastChild())&&f.isParentOf(k)||f===k||g.shiftKey)return g.preventDefault(),g.stopImmediatePropagation(),
49
+ g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(m.x,m.y,c.grid)),S(c,a,m.x,m.y,"down")):R(c,a,m.x,m.y,"down")}}else if(d.DEPRECATED_$isGridSelection(f)&&g.shiftKey){m=f.focus.getNode();if(!B(m))return!1;m=a.getCordsFromCellNode(m,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();return S(c,a,m.x,m.y,"down")}return!1},d.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(e.registerCommand(d.KEY_ARROW_UP_COMMAND,g=>{var f=d.$getSelection();
50
+ if(!T(f,a))return!1;if(d.$isRangeSelection(f)){if(f.isCollapsed()){var k=n.$findMatchingParent(f.anchor.getNode(),r=>B(r));if(!B(k))return!1;var m=a.getCordsFromCellNode(k,c.grid);f=n.$findMatchingParent(f.anchor.getNode(),r=>d.$isElementNode(r));if(null==f)throw Error("Expected BlockNode Parent");if((k=k.getLastChild())&&f.isParentOf(k)||f===k||g.shiftKey)return g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(m.x,
51
+ m.y,c.grid)),S(c,a,m.x,m.y,"up")):R(c,a,m.x,m.y,"up")}}else if(d.DEPRECATED_$isGridSelection(f)&&g.shiftKey){m=f.focus.getNode();if(!B(m))return!1;m=a.getCordsFromCellNode(m,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();return S(c,a,m.x,m.y,"up")}return!1},d.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(e.registerCommand(d.KEY_ARROW_LEFT_COMMAND,g=>{var f=d.$getSelection();if(!T(f,a))return!1;if(d.$isRangeSelection(f)){if(f.isCollapsed()){var k=n.$findMatchingParent(f.anchor.getNode(),
52
+ m=>B(m));if(!B(k))return!1;k=a.getCordsFromCellNode(k,c.grid);if(null==n.$findMatchingParent(f.anchor.getNode(),m=>d.$isElementNode(m)))throw Error("Expected BlockNode Parent");if(0===f.anchor.offset||g.shiftKey)return g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(k.x,k.y,c.grid)),S(c,a,k.x,k.y,"backward")):R(c,a,k.x,k.y,"backward")}}else if(d.DEPRECATED_$isGridSelection(f)&&g.shiftKey){f=f.focus.getNode();if(!B(f))return!1;
53
+ f=a.getCordsFromCellNode(f,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();return S(c,a,f.x,f.y,"backward")}return!1},d.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(e.registerCommand(d.KEY_ARROW_RIGHT_COMMAND,g=>{var f=d.$getSelection();if(!T(f,a))return!1;if(d.$isRangeSelection(f)){if(f.isCollapsed()){var k=n.$findMatchingParent(f.anchor.getNode(),m=>B(m));if(!B(k))return!1;k=a.getCordsFromCellNode(k,c.grid);if(null==n.$findMatchingParent(f.anchor.getNode(),m=>
54
+ d.$isElementNode(m)))throw Error("Expected BlockNode Parent");if(f.anchor.offset===f.anchor.getNode().getTextContentSize()||g.shiftKey)return g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(k.x,k.y,c.grid)),S(c,a,k.x,k.y,"forward")):R(c,a,k.x,k.y,"forward")}}else if(d.DEPRECATED_$isGridSelection(f)&&g.shiftKey){f=f.focus.getNode();if(!B(f))return!1;f=a.getCordsFromCellNode(f,c.grid);g.preventDefault();g.stopImmediatePropagation();
55
+ g.stopPropagation();return S(c,a,f.x,f.y,"forward")}return!1},d.COMMAND_PRIORITY_CRITICAL));let A=g=>()=>{const f=d.$getSelection();if(!T(f,a))return!1;if(d.DEPRECATED_$isGridSelection(f))return c.clearText(),!0;if(d.$isRangeSelection(f)){const r=n.$findMatchingParent(f.anchor.getNode(),w=>B(w));if(!B(r))return!1;var k=f.anchor.getNode(),m=f.focus.getNode();k=a.isParentOf(k);m=a.isParentOf(m);if(k&&!m||m&&!k)return c.clearText(),!0;m=n.$findMatchingParent(f.anchor.getNode(),w=>d.$isElementNode(w)&&
56
+ B(w.getParent()));k=n.$findMatchingParent(f.anchor.getNode(),w=>d.$isElementNode(w));if(!d.$isElementNode(m)||!d.$isElementNode(k))return!1;const G=()=>{const w=d.$createParagraphNode(),ca=d.$createTextNode();w.append(ca);r.append(w);r.getChildren().forEach(X=>{X!==w&&X.remove()})};if(g===d.DELETE_LINE_COMMAND&&null===m.getPreviousSibling())return G(),!0;if(g===d.DELETE_CHARACTER_COMMAND||g===d.DELETE_WORD_COMMAND){if(f.isCollapsed()&&0===f.anchor.offset&&m===k&&null===m.getPreviousSibling())return!0;
57
+ if(!d.$isParagraphNode(m)&&0===m.getTextContentSize())return G(),!0}}return!1};[d.DELETE_WORD_COMMAND,d.DELETE_LINE_COMMAND,d.DELETE_CHARACTER_COMMAND].forEach(g=>{c.listenersToRemove.add(e.registerCommand(g,A(g),d.COMMAND_PRIORITY_CRITICAL))});let y=g=>{const f=d.$getSelection();if(!T(f,a))return!1;if(d.DEPRECATED_$isGridSelection(f))return g.preventDefault(),g.stopPropagation(),c.clearText(),!0;d.$isRangeSelection(f)&&(g=n.$findMatchingParent(f.anchor.getNode(),k=>B(k)),B(g));return!1};c.listenersToRemove.add(e.registerCommand(d.KEY_BACKSPACE_COMMAND,
58
+ y,d.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(e.registerCommand(d.KEY_DELETE_COMMAND,y,d.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(e.registerCommand(d.FORMAT_TEXT_COMMAND,g=>{let f=d.$getSelection();if(!T(f,a))return!1;if(d.DEPRECATED_$isGridSelection(f))return c.formatCells(g),!0;d.$isRangeSelection(f)&&(g=n.$findMatchingParent(f.anchor.getNode(),k=>B(k)),B(g));return!1},d.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(e.registerCommand(d.CONTROLLED_TEXT_INSERTION_COMMAND,
59
+ ()=>{var g=d.$getSelection();if(!T(g,a))return!1;d.DEPRECATED_$isGridSelection(g)?c.clearHighlight():d.$isRangeSelection(g)&&(g=n.$findMatchingParent(g.anchor.getNode(),f=>B(f)),B(g));return!1},d.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(e.registerCommand(d.KEY_TAB_COMMAND,g=>{var f=d.$getSelection();if(!T(f,a))return!1;if(d.$isRangeSelection(f)){let k=n.$findMatchingParent(f.anchor.getNode(),m=>B(m));if(!B(k))return!1;if(f.isCollapsed())return f=a.getCordsFromCellNode(k,c.grid),g.preventDefault(),
60
+ R(c,a,f.x,f.y,g.shiftKey?"backward":"forward"),!0}return!1},d.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(e.registerCommand(d.FOCUS_COMMAND,()=>a.isSelected(),d.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(e.registerCommand(d.SELECTION_CHANGE_COMMAND,()=>{let g=d.$getSelection();var f=d.$getPreviousSelection();if(g&&d.$isRangeSelection(g)&&!g.isCollapsed()){var k=g.anchor.getNode(),m=g.focus.getNode();k=a.isParentOf(k);var r=a.isParentOf(m);m=k&&!r||r&&!k;k=k&&r&&!a.isSelected();if(m)return f=
61
+ g.isBackward(),k=d.$createRangeSelection(),m=a.getKey(),k.anchor.set(g.anchor.key,g.anchor.offset,g.anchor.type),k.focus.set(m,f?0:a.getChildrenSize(),"element"),p=!0,d.$setSelection(k),O(c),!0;if(k&&({grid:k}=c,g.getNodes().filter(B).length===k.rows*k.columns)){k=d.DEPRECATED_$createGridSelection();m=a.getKey();r=a.getFirstChildOrThrow().getFirstChild();let G=a.getLastChildOrThrow().getLastChild();if(null!=r&&null!=G)return k.set(m,r.getKey(),G.getKey()),d.$setSelection(k),c.updateTableGridSelection(k),
62
+ !0}}if(g&&!g.is(f)&&(d.DEPRECATED_$isGridSelection(g)||d.DEPRECATED_$isGridSelection(f))&&c.gridSelection&&!c.gridSelection.is(f))return d.DEPRECATED_$isGridSelection(g)&&g.gridKey===c.tableNodeKey?c.updateTableGridSelection(g):!d.DEPRECATED_$isGridSelection(g)&&d.DEPRECATED_$isGridSelection(f)&&f.gridKey===c.tableNodeKey&&c.updateTableGridSelection(null),!1;c.hasHijackedSelectionStyles&&!a.isSelected()?(P(c),p=!1):!c.hasHijackedSelectionStyles&&a.isSelected()&&O(c);return!1},d.COMMAND_PRIORITY_CRITICAL));
63
+ return c};exports.getCellFromTarget=M;exports.getTableSelectionFromTableElement=function(a){return a.__lexicalTableSelection}
@@ -6,7 +6,7 @@
6
6
  *
7
7
  */
8
8
  import type { DOMConversionMap, DOMConversionOutput, DOMExportOutput, EditorConfig, LexicalEditor, LexicalNode, NodeKey, SerializedGridCellNode, Spread } from 'lexical';
9
- import { GridCellNode } from 'lexical';
9
+ import { DEPRECATED_GridCellNode } from 'lexical';
10
10
  export declare const TableCellHeaderStates: {
11
11
  BOTH: number;
12
12
  COLUMN: number;
@@ -20,7 +20,7 @@ export declare type SerializedTableCellNode = Spread<{
20
20
  width?: number;
21
21
  }, SerializedGridCellNode>;
22
22
  /** @noInheritDoc */
23
- export declare class TableCellNode extends GridCellNode {
23
+ export declare class TableCellNode extends DEPRECATED_GridCellNode {
24
24
  /** @internal */
25
25
  __headerState: TableCellHeaderState;
26
26
  /** @internal */
@@ -42,6 +42,7 @@ export declare class TableCellNode extends GridCellNode {
42
42
  hasHeaderState(headerState: TableCellHeaderState): boolean;
43
43
  hasHeader(): boolean;
44
44
  updateDOM(prevNode: TableCellNode): boolean;
45
+ isShadowRoot(): boolean;
45
46
  collapseAtStart(): true;
46
47
  canBeEmpty(): false;
47
48
  canIndent(): false;
@@ -8,13 +8,13 @@
8
8
  import type { TableCellNode } from './LexicalTableCellNode';
9
9
  import type { Cell, Grid } from './LexicalTableSelection';
10
10
  import type { DOMConversionMap, DOMConversionOutput, DOMExportOutput, EditorConfig, LexicalEditor, LexicalNode, NodeKey, SerializedElementNode, Spread } from 'lexical';
11
- import { GridNode } from 'lexical';
11
+ import { DEPRECATED_GridNode } from 'lexical';
12
12
  export declare type SerializedTableNode = Spread<{
13
13
  type: 'table';
14
14
  version: 1;
15
15
  }, SerializedElementNode>;
16
16
  /** @noInheritDoc */
17
- export declare class TableNode extends GridNode {
17
+ export declare class TableNode extends DEPRECATED_GridNode {
18
18
  /** @internal */
19
19
  __grid?: Grid;
20
20
  static getType(): 'table';
@@ -28,6 +28,7 @@ export declare class TableNode extends GridNode {
28
28
  exportDOM(editor: LexicalEditor): DOMExportOutput;
29
29
  canExtractContents(): false;
30
30
  canBeEmpty(): false;
31
+ isShadowRoot(): boolean;
31
32
  getCordsFromCellNode(tableCellNode: TableCellNode, grid: Grid): {
32
33
  x: number;
33
34
  y: number;
@@ -6,14 +6,14 @@
6
6
  *
7
7
  */
8
8
  import type { Spread } from 'lexical';
9
- import { DOMConversionMap, DOMConversionOutput, EditorConfig, GridRowNode, LexicalNode, NodeKey, SerializedElementNode } from 'lexical';
9
+ import { DEPRECATED_GridRowNode, DOMConversionMap, DOMConversionOutput, EditorConfig, LexicalNode, NodeKey, SerializedElementNode } from 'lexical';
10
10
  export declare type SerializedTableRowNode = Spread<{
11
11
  height: number;
12
12
  type: 'tablerow';
13
13
  version: 1;
14
14
  }, SerializedElementNode>;
15
15
  /** @noInheritDoc */
16
- export declare class TableRowNode extends GridRowNode {
16
+ export declare class TableRowNode extends DEPRECATED_GridRowNode {
17
17
  /** @internal */
18
18
  __height?: number;
19
19
  static getType(): 'tablerow';
@@ -23,6 +23,7 @@ export declare class TableRowNode extends GridRowNode {
23
23
  constructor(height?: number, key?: NodeKey);
24
24
  exportJSON(): SerializedElementNode;
25
25
  createDOM(config: EditorConfig): HTMLElement;
26
+ isShadowRoot(): boolean;
26
27
  setHeight(height: number): number | null | undefined;
27
28
  getHeight(): number | null | undefined;
28
29
  updateDOM(prevNode: TableRowNode): boolean;
@@ -25,5 +25,5 @@ export declare type TableCellSiblings = {
25
25
  export declare function $getTableCellSiblingsFromTableCellNode(tableCellNode: TableCellNode, grid: Grid): TableCellSiblings;
26
26
  export declare function $removeTableRowAtIndex(tableNode: TableNode, indexToDelete: number): TableNode;
27
27
  export declare function $insertTableRow(tableNode: TableNode, targetIndex: number, shouldInsertAfter: boolean | undefined, rowCount: number, grid: Grid): TableNode;
28
- export declare function $insertTableColumn(tableNode: TableNode, targetIndex: number, shouldInsertAfter: boolean | undefined, columnCount: number): TableNode;
28
+ export declare function $insertTableColumn(tableNode: TableNode, targetIndex: number, shouldInsertAfter: boolean | undefined, columnCount: number, grid: Grid): TableNode;
29
29
  export declare function $deleteTableColumn(tableNode: TableNode, targetIndex: number): TableNode;
package/package.json CHANGED
@@ -8,13 +8,13 @@
8
8
  "table"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.4.0",
11
+ "version": "0.5.0",
12
12
  "main": "LexicalTable.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.4.0"
14
+ "lexical": "0.5.0"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/utils": "0.4.0"
17
+ "@lexical/utils": "0.5.0"
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",
package/utils.d.ts DELETED
@@ -1,29 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
- import type { Grid } from './LexicalTableSelection';
9
- import type { LexicalNode } from 'lexical';
10
- import { TableCellNode } from './LexicalTableCellNode';
11
- import { TableNode } from './LexicalTableNode';
12
- import { TableRowNode } from './LexicalTableRowNode';
13
- export declare function $createTableNodeWithDimensions(rowCount: number, columnCount: number, includeHeaders?: boolean): TableNode;
14
- export declare function $getTableCellNodeFromLexicalNode(startingNode: LexicalNode): TableCellNode | null;
15
- export declare function $getTableRowNodeFromTableCellNodeOrThrow(startingNode: LexicalNode): TableRowNode;
16
- export declare function $getTableNodeFromLexicalNodeOrThrow(startingNode: LexicalNode): TableNode;
17
- export declare function $getTableRowIndexFromTableCellNode(tableCellNode: TableCellNode): number;
18
- export declare function $getTableColumnIndexFromTableCellNode(tableCellNode: TableCellNode): number;
19
- export declare type TableCellSiblings = {
20
- above: TableCellNode | null | undefined;
21
- below: TableCellNode | null | undefined;
22
- left: TableCellNode | null | undefined;
23
- right: TableCellNode | null | undefined;
24
- };
25
- export declare function $getTableCellSiblingsFromTableCellNode(tableCellNode: TableCellNode, grid: Grid): TableCellSiblings;
26
- export declare function $removeTableRowAtIndex(tableNode: TableNode, indexToDelete: number): TableNode;
27
- export declare function $insertTableRow(tableNode: TableNode, targetIndex: number, shouldInsertAfter: boolean | undefined, rowCount: number, grid: Grid): TableNode;
28
- export declare function $insertTableColumn(tableNode: TableNode, targetIndex: number, shouldInsertAfter: boolean | undefined, columnCount: number): TableNode;
29
- export declare function $deleteTableColumn(tableNode: TableNode, targetIndex: number): TableNode;