@lexical/table 0.13.0 → 0.14.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.
@@ -222,6 +222,12 @@ function convertTableCellNodeElement(domNode) {
222
222
  tableCellNode.__backgroundColor = backgroundColor;
223
223
  }
224
224
  return {
225
+ after: childLexicalNodes => {
226
+ if (childLexicalNodes.length === 0) {
227
+ childLexicalNodes.push(lexical.$createParagraphNode());
228
+ }
229
+ return childLexicalNodes;
230
+ },
225
231
  forChild: (lexicalNode, parentLexicalNode) => {
226
232
  if ($isTableCellNode(parentLexicalNode) && !lexical.$isElementNode(lexicalNode)) {
227
233
  const paragraphNode = lexical.$createParagraphNode();
@@ -287,6 +293,9 @@ class TableRowNode extends lexical.ElementNode {
287
293
  exportJSON() {
288
294
  return {
289
295
  ...super.exportJSON(),
296
+ ...(this.getHeight() && {
297
+ height: this.getHeight()
298
+ }),
290
299
  type: 'tablerow',
291
300
  version: 1
292
301
  };
@@ -361,11 +370,19 @@ function $createTableNodeWithDimensions(rowCount, columnCount, includeHeaders =
361
370
  for (let iColumn = 0; iColumn < columnCount; iColumn++) {
362
371
  let headerState = TableCellHeaderStates.NO_STATUS;
363
372
  if (typeof includeHeaders === 'object') {
364
- if (iRow === 0 && includeHeaders.rows) headerState |= TableCellHeaderStates.ROW;
365
- if (iColumn === 0 && includeHeaders.columns) headerState |= TableCellHeaderStates.COLUMN;
373
+ if (iRow === 0 && includeHeaders.rows) {
374
+ headerState |= TableCellHeaderStates.ROW;
375
+ }
376
+ if (iColumn === 0 && includeHeaders.columns) {
377
+ headerState |= TableCellHeaderStates.COLUMN;
378
+ }
366
379
  } else if (includeHeaders) {
367
- if (iRow === 0) headerState |= TableCellHeaderStates.ROW;
368
- if (iColumn === 0) headerState |= TableCellHeaderStates.COLUMN;
380
+ if (iRow === 0) {
381
+ headerState |= TableCellHeaderStates.ROW;
382
+ }
383
+ if (iColumn === 0) {
384
+ headerState |= TableCellHeaderStates.COLUMN;
385
+ }
369
386
  }
370
387
  const tableCellNode = $createTableCellNode(headerState);
371
388
  const paragraphNode = lexical.$createParagraphNode();
@@ -469,6 +486,12 @@ function $insertTableRow(tableNode, targetIndex, shouldInsertAfter = true, rowCo
469
486
  }
470
487
  return tableNode;
471
488
  }
489
+ const getHeaderState = (currentState, possibleState) => {
490
+ if (currentState === TableCellHeaderStates.BOTH || currentState === possibleState) {
491
+ return possibleState;
492
+ }
493
+ return TableCellHeaderStates.NO_STATUS;
494
+ };
472
495
  function $insertTableRow__EXPERIMENTAL(insertAfter = true) {
473
496
  const selection = lexical.$getSelection();
474
497
  if (!(lexical.$isRangeSelection(selection) || $isTableSelection(selection))) {
@@ -491,7 +514,10 @@ function $insertTableRow__EXPERIMENTAL(insertAfter = true) {
491
514
  startRow
492
515
  } = focusEndRowMap[i];
493
516
  if (startRow + cell.__rowSpan - 1 <= focusEndRow) {
494
- newRow.append($createTableCellNode(TableCellHeaderStates.NO_STATUS).append(lexical.$createParagraphNode()));
517
+ const currentCell = focusEndRowMap[i].cell;
518
+ const currentCellHeaderState = currentCell.__headerState;
519
+ const headerState = getHeaderState(currentCellHeaderState, TableCellHeaderStates.COLUMN);
520
+ newRow.append($createTableCellNode(headerState).append(lexical.$createParagraphNode()));
495
521
  } else {
496
522
  cell.setRowSpan(cell.__rowSpan + 1);
497
523
  }
@@ -510,7 +536,10 @@ function $insertTableRow__EXPERIMENTAL(insertAfter = true) {
510
536
  startRow
511
537
  } = focusStartRowMap[i];
512
538
  if (startRow === focusStartRow) {
513
- newRow.append($createTableCellNode(TableCellHeaderStates.NO_STATUS).append(lexical.$createParagraphNode()));
539
+ const currentCell = focusStartRowMap[i].cell;
540
+ const currentCellHeaderState = currentCell.__headerState;
541
+ const headerState = getHeaderState(currentCellHeaderState, TableCellHeaderStates.COLUMN);
542
+ newRow.append($createTableCellNode(headerState).append(lexical.$createParagraphNode()));
514
543
  } else {
515
544
  cell.setRowSpan(cell.__rowSpan + 1);
516
545
  }
@@ -584,8 +613,8 @@ function $insertTableColumn__EXPERIMENTAL(insertAfter = true) {
584
613
  throw Error(`Expected firstTable child to be a row`);
585
614
  }
586
615
  let firstInsertedCell = null;
587
- function $createTableCellNodeForInsertTableColumn() {
588
- const cell = $createTableCellNode(TableCellHeaderStates.NO_STATUS).append(lexical.$createParagraphNode());
616
+ function $createTableCellNodeForInsertTableColumn(headerState = TableCellHeaderStates.NO_STATUS) {
617
+ const cell = $createTableCellNode(headerState).append(lexical.$createParagraphNode());
589
618
  if (firstInsertedCell === null) {
590
619
  firstInsertedCell = cell;
591
620
  }
@@ -601,8 +630,10 @@ function $insertTableColumn__EXPERIMENTAL(insertAfter = true) {
601
630
  loopRow = currentRow;
602
631
  }
603
632
  const rowMap = gridMap[i];
633
+ const currentCellHeaderState = rowMap[insertAfterColumn < 0 ? 0 : insertAfterColumn].cell.__headerState;
634
+ const headerState = getHeaderState(currentCellHeaderState, TableCellHeaderStates.ROW);
604
635
  if (insertAfterColumn < 0) {
605
- $insertFirst(loopRow, $createTableCellNodeForInsertTableColumn());
636
+ $insertFirst(loopRow, $createTableCellNodeForInsertTableColumn(headerState));
606
637
  continue;
607
638
  }
608
639
  const {
@@ -624,11 +655,11 @@ function $insertTableColumn__EXPERIMENTAL(insertAfter = true) {
624
655
  insertAfterCell = cell_;
625
656
  insertAfterCellRowStart = startRow_;
626
657
  } else {
627
- loopRow.append($createTableCellNodeForInsertTableColumn());
658
+ loopRow.append($createTableCellNodeForInsertTableColumn(headerState));
628
659
  continue rowLoop;
629
660
  }
630
661
  }
631
- insertAfterCell.insertAfter($createTableCellNodeForInsertTableColumn());
662
+ insertAfterCell.insertAfter($createTableCellNodeForInsertTableColumn(headerState));
632
663
  } else {
633
664
  currentCell.setColSpan(currentCell.__colSpan + 1);
634
665
  }
@@ -1678,6 +1709,36 @@ function applyTableHandlers(tableNode, tableElement, editor, hasTabHandler) {
1678
1709
  }
1679
1710
  return false;
1680
1711
  }, lexical.COMMAND_PRIORITY_CRITICAL));
1712
+ tableObserver.listenersToRemove.add(editor.registerCommand(lexical.FORMAT_ELEMENT_COMMAND, formatType => {
1713
+ const selection = lexical.$getSelection();
1714
+ if (!$isTableSelection(selection) || !$isSelectionInTable(selection, tableNode)) {
1715
+ return false;
1716
+ }
1717
+ const anchorNode = selection.anchor.getNode();
1718
+ const focusNode = selection.focus.getNode();
1719
+ if (!$isTableCellNode(anchorNode) || !$isTableCellNode(focusNode)) {
1720
+ return false;
1721
+ }
1722
+ const [tableMap, anchorCell, focusCell] = $computeTableMap(tableNode, anchorNode, focusNode);
1723
+ const maxRow = Math.max(anchorCell.startRow, focusCell.startRow);
1724
+ const maxColumn = Math.max(anchorCell.startColumn, focusCell.startColumn);
1725
+ const minRow = Math.min(anchorCell.startRow, focusCell.startRow);
1726
+ const minColumn = Math.min(anchorCell.startColumn, focusCell.startColumn);
1727
+ for (let i = minRow; i <= maxRow; i++) {
1728
+ for (let j = minColumn; j <= maxColumn; j++) {
1729
+ const cell = tableMap[i][j].cell;
1730
+ cell.setFormat(formatType);
1731
+ const cellChildren = cell.getChildren();
1732
+ for (let k = 0; k < cellChildren.length; k++) {
1733
+ const child = cellChildren[k];
1734
+ if (lexical.$isElementNode(child) && !child.isInline()) {
1735
+ child.setFormat(formatType);
1736
+ }
1737
+ }
1738
+ }
1739
+ }
1740
+ return true;
1741
+ }, lexical.COMMAND_PRIORITY_CRITICAL));
1681
1742
  tableObserver.listenersToRemove.add(editor.registerCommand(lexical.CONTROLLED_TEXT_INSERTION_COMMAND, payload => {
1682
1743
  const selection = lexical.$getSelection();
1683
1744
  if (!$isSelectionInTable(selection, tableNode)) {
@@ -1814,8 +1875,8 @@ function applyTableHandlers(tableNode, tableElement, editor, hasTabHandler) {
1814
1875
  // as in that case we'll leave selection resolving to that table
1815
1876
  const anchorCellNode = $findCellNode(anchorNode);
1816
1877
  const focusCellNode = $findCellNode(focusNode);
1817
- const isAnchorInside = anchorCellNode && tableNode.is($findTableNode(anchorCellNode));
1818
- const isFocusInside = focusCellNode && tableNode.is($findTableNode(focusCellNode));
1878
+ const isAnchorInside = !!(anchorCellNode && tableNode.is($findTableNode(anchorCellNode)));
1879
+ const isFocusInside = !!(focusCellNode && tableNode.is($findTableNode(focusCellNode)));
1819
1880
  const isPartialyWithinTable = isAnchorInside !== isFocusInside;
1820
1881
  const isWithinTable = isAnchorInside && isFocusInside;
1821
1882
  const isBackward = selection.isBackward();
@@ -2271,11 +2332,6 @@ class TableNode extends lexical.ElementNode {
2271
2332
  }
2272
2333
  };
2273
2334
  }
2274
-
2275
- // TODO 0.10 deprecate
2276
- canExtractContents() {
2277
- return false;
2278
- }
2279
2335
  canBeEmpty() {
2280
2336
  return false;
2281
2337
  }
@@ -2293,7 +2349,9 @@ class TableNode extends lexical.ElementNode {
2293
2349
  continue;
2294
2350
  }
2295
2351
  const x = row.findIndex(cell => {
2296
- if (!cell) return;
2352
+ if (!cell) {
2353
+ return;
2354
+ }
2297
2355
  const {
2298
2356
  elem
2299
2357
  } = cell;
@@ -0,0 +1,45 @@
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
+ import * as modDev from './LexicalTable.dev.esm.js';
8
+ import * as modProd from './LexicalTable.prod.esm.js';
9
+ const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
10
+ export const $computeTableMap = mod.$computeTableMap;
11
+ export const $createTableCellNode = mod.$createTableCellNode;
12
+ export const $createTableNode = mod.$createTableNode;
13
+ export const $createTableNodeWithDimensions = mod.$createTableNodeWithDimensions;
14
+ export const $createTableRowNode = mod.$createTableRowNode;
15
+ export const $createTableSelection = mod.$createTableSelection;
16
+ export const $deleteTableColumn = mod.$deleteTableColumn;
17
+ export const $deleteTableColumn__EXPERIMENTAL = mod.$deleteTableColumn__EXPERIMENTAL;
18
+ export const $deleteTableRow__EXPERIMENTAL = mod.$deleteTableRow__EXPERIMENTAL;
19
+ export const $getElementForTableNode = mod.$getElementForTableNode;
20
+ export const $getNodeTriplet = mod.$getNodeTriplet;
21
+ export const $getTableCellNodeFromLexicalNode = mod.$getTableCellNodeFromLexicalNode;
22
+ export const $getTableCellNodeRect = mod.$getTableCellNodeRect;
23
+ export const $getTableColumnIndexFromTableCellNode = mod.$getTableColumnIndexFromTableCellNode;
24
+ export const $getTableNodeFromLexicalNodeOrThrow = mod.$getTableNodeFromLexicalNodeOrThrow;
25
+ export const $getTableRowIndexFromTableCellNode = mod.$getTableRowIndexFromTableCellNode;
26
+ export const $getTableRowNodeFromTableCellNodeOrThrow = mod.$getTableRowNodeFromTableCellNodeOrThrow;
27
+ export const $insertTableColumn = mod.$insertTableColumn;
28
+ export const $insertTableColumn__EXPERIMENTAL = mod.$insertTableColumn__EXPERIMENTAL;
29
+ export const $insertTableRow = mod.$insertTableRow;
30
+ export const $insertTableRow__EXPERIMENTAL = mod.$insertTableRow__EXPERIMENTAL;
31
+ export const $isTableCellNode = mod.$isTableCellNode;
32
+ export const $isTableNode = mod.$isTableNode;
33
+ export const $isTableRowNode = mod.$isTableRowNode;
34
+ export const $isTableSelection = mod.$isTableSelection;
35
+ export const $removeTableRowAtIndex = mod.$removeTableRowAtIndex;
36
+ export const $unmergeCell = mod.$unmergeCell;
37
+ export const INSERT_TABLE_COMMAND = mod.INSERT_TABLE_COMMAND;
38
+ export const TableCellHeaderStates = mod.TableCellHeaderStates;
39
+ export const TableCellNode = mod.TableCellNode;
40
+ export const TableNode = mod.TableNode;
41
+ export const TableObserver = mod.TableObserver;
42
+ export const TableRowNode = mod.TableRowNode;
43
+ export const applyTableHandlers = mod.applyTableHandlers;
44
+ export const getDOMCellFromTarget = mod.getDOMCellFromTarget;
45
+ export const getTableObserverFromTableElement = mod.getTableObserverFromTableElement;
package/LexicalTable.js CHANGED
@@ -5,5 +5,5 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  'use strict'
8
- const LexicalTable = process.env.NODE_ENV === 'development' ? require('./LexicalTable.dev.js') : require('./LexicalTable.prod.js')
8
+ const LexicalTable = process.env.NODE_ENV === 'development' ? require('./LexicalTable.dev.js') : require('./LexicalTable.prod.js');
9
9
  module.exports = LexicalTable;
@@ -245,7 +245,7 @@ declare export function $deleteTableColumn__EXPERIMENTAL(): void;
245
245
 
246
246
  declare export function $unmergeCell(): void;
247
247
 
248
- declare export function $computeGridMap(
248
+ declare export function $computeTableMap(
249
249
  table: TableNode,
250
250
  cellA: TableCellNode,
251
251
  cellB: TableCellNode,
@@ -0,0 +1,7 @@
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
+ import{addClassNamesToElement as e,$findMatchingParent as t,removeClassNamesFromElement as n,isHTMLElement as o}from"@lexical/utils";import{ElementNode as r,$applyNodeReplacement as l,$createParagraphNode as s,$isElementNode as i,$isLineBreakNode as a,createCommand as c,$createTextNode as h,$getSelection as d,$isRangeSelection as u,$normalizeSelection__EXPERIMENTAL as g,$getNodeByKey as f,isCurrentlyReadOnlyMode as m,$createPoint as p,$setSelection as _,SELECTION_CHANGE_COMMAND as C,$getNearestNodeFromDOMNode as S,$createRangeSelection as w,$getRoot as b,KEY_ARROW_DOWN_COMMAND as y,COMMAND_PRIORITY_HIGH as N,KEY_ARROW_UP_COMMAND as T,KEY_ARROW_LEFT_COMMAND as x,KEY_ARROW_RIGHT_COMMAND as E,KEY_ESCAPE_COMMAND as O,DELETE_WORD_COMMAND as v,DELETE_LINE_COMMAND as M,DELETE_CHARACTER_COMMAND as R,COMMAND_PRIORITY_CRITICAL as K,KEY_BACKSPACE_COMMAND as F,KEY_DELETE_COMMAND as k,FORMAT_TEXT_COMMAND as H,FORMAT_ELEMENT_COMMAND as A,CONTROLLED_TEXT_INSERTION_COMMAND as B,KEY_TAB_COMMAND as D,FOCUS_COMMAND as P,SELECTION_INSERT_CLIPBOARD_NODES_COMMAND as L,$isTextNode as I,$getPreviousSelection as W}from"lexical";const U=/^(\d+(?:\.\d+)?)px$/,X={BOTH:3,COLUMN:2,NO_STATUS:0,ROW:1};class Y extends r{static getType(){return"tablecell"}static clone(e){const t=new Y(e.__headerState,e.__colSpan,e.__width,e.__key);return t.__rowSpan=e.__rowSpan,t.__backgroundColor=e.__backgroundColor,t}static importDOM(){return{td:e=>({conversion:z,priority:0}),th:e=>({conversion:z,priority:0})}}static importJSON(e){const t=e.colSpan||1,n=e.rowSpan||1,o=J(e.headerState,t,e.width||void 0);return o.__rowSpan=n,o.__backgroundColor=e.backgroundColor||null,o}constructor(e=X.NO_STATUS,t=1,n,o){super(o),this.__colSpan=t,this.__rowSpan=1,this.__headerState=e,this.__width=n,this.__backgroundColor=null}createDOM(t){const n=document.createElement(this.getTag());return this.__width&&(n.style.width=`${this.__width}px`),this.__colSpan>1&&(n.colSpan=this.__colSpan),this.__rowSpan>1&&(n.rowSpan=this.__rowSpan),null!==this.__backgroundColor&&(n.style.backgroundColor=this.__backgroundColor),e(n,t.theme.tableCell,this.hasHeader()&&t.theme.tableCellHeader),n}exportDOM(e){const{element:t}=super.exportDOM(e);if(t){const e=t,n=700,o=this.getParentOrThrow().getChildrenSize();e.style.border="1px solid black",this.__colSpan>1&&(e.colSpan=this.__colSpan),this.__rowSpan>1&&(e.rowSpan=this.__rowSpan),e.style.width=`${this.getWidth()||Math.max(90,n/o)}px`,e.style.verticalAlign="top",e.style.textAlign="start";const r=this.getBackgroundColor();null!==r?e.style.backgroundColor=r:this.hasHeader()&&(e.style.backgroundColor="#f2f3f5")}return{element:t}}exportJSON(){return{...super.exportJSON(),backgroundColor:this.getBackgroundColor(),colSpan:this.__colSpan,headerState:this.__headerState,rowSpan:this.__rowSpan,type:"tablecell",width:this.getWidth()}}getColSpan(){return this.__colSpan}setColSpan(e){return this.getWritable().__colSpan=e,this}getRowSpan(){return this.__rowSpan}setRowSpan(e){return this.getWritable().__rowSpan=e,this}getTag(){return this.hasHeader()?"th":"td"}setHeaderStyles(e){return this.getWritable().__headerState=e,this.__headerState}getHeaderStyles(){return this.getLatest().__headerState}setWidth(e){return this.getWritable().__width=e,this.__width}getWidth(){return this.getLatest().__width}getBackgroundColor(){return this.getLatest().__backgroundColor}setBackgroundColor(e){this.getWritable().__backgroundColor=e}toggleHeaderStyle(e){const t=this.getWritable();return(t.__headerState&e)===e?t.__headerState-=e:t.__headerState+=e,t}hasHeaderState(e){return(this.getHeaderStyles()&e)===e}hasHeader(){return this.getLatest().__headerState!==X.NO_STATUS}updateDOM(e){return e.__headerState!==this.__headerState||e.__width!==this.__width||e.__colSpan!==this.__colSpan||e.__rowSpan!==this.__rowSpan||e.__backgroundColor!==this.__backgroundColor}isShadowRoot(){return!0}collapseAtStart(){return!0}canBeEmpty(){return!1}canIndent(){return!1}}function z(e){const t=e,n=e.nodeName.toLowerCase();let o;U.test(t.style.width)&&(o=parseFloat(t.style.width));const r=J("th"===n?X.ROW:X.NO_STATUS,t.colSpan,o);r.__rowSpan=t.rowSpan;const l=t.style.backgroundColor;return""!==l&&(r.__backgroundColor=l),{after:e=>(0===e.length&&e.push(s()),e),forChild:(e,t)=>{if($(t)&&!i(e)){const t=s();return a(e)&&"\n"===e.getTextContent()?null:(t.append(e),t)}return e},node:r}}function J(e,t=1,n){return l(new Y(e,t,n))}function $(e){return e instanceof Y}const j=c("INSERT_TABLE_COMMAND");class q extends r{static getType(){return"tablerow"}static clone(e){return new q(e.__height,e.__key)}static importDOM(){return{tr:e=>({conversion:G,priority:0})}}static importJSON(e){return Q(e.height)}constructor(e,t){super(t),this.__height=e}exportJSON(){return{...super.exportJSON(),...this.getHeight()&&{height:this.getHeight()},type:"tablerow",version:1}}createDOM(t){const n=document.createElement("tr");return this.__height&&(n.style.height=`${this.__height}px`),e(n,t.theme.tableRow),n}isShadowRoot(){return!0}setHeight(e){return this.getWritable().__height=e,this.__height}getHeight(){return this.getLatest().__height}updateDOM(e){return e.__height!==this.__height}canBeEmpty(){return!1}canIndent(){return!1}}function G(e){const t=e;let n;return U.test(t.style.height)&&(n=parseFloat(t.style.height)),{node:Q(n)}}function Q(e){return l(new q(e))}function V(e){return e instanceof q}var Z=function(e){const t=new URLSearchParams;t.append("code",e);for(let e=1;e<arguments.length;e++)t.append("v",arguments[e]);throw Error(`Minified Lexical error #${e}; visit https://lexical.dev/docs/error?${t} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)};const ee="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement;function te(e,t,n=!0){const o=Ge();for(let r=0;r<e;r++){const e=Q();for(let o=0;o<t;o++){let t=X.NO_STATUS;"object"==typeof n?(0===r&&n.rows&&(t|=X.ROW),0===o&&n.columns&&(t|=X.COLUMN)):n&&(0===r&&(t|=X.ROW),0===o&&(t|=X.COLUMN));const l=J(t),i=s();i.append(h()),l.append(i),e.append(l)}o.append(e)}return o}function ne(e){const n=t(e,(e=>$(e)));return $(n)?n:null}function oe(e){const n=t(e,(e=>V(e)));if(V(n))return n;throw new Error("Expected table cell to be inside of table row.")}function re(e){const n=t(e,(e=>Qe(e)));if(Qe(n))return n;throw new Error("Expected table cell to be inside of table.")}function le(e){const t=oe(e);return re(t).getChildren().findIndex((e=>e.is(t)))}function se(e){return oe(e).getChildren().findIndex((t=>t.is(e)))}function ie(e,t){const n=re(e),{x:o,y:r}=n.getCordsFromCellNode(e,t);return{above:n.getCellNodeFromCords(o,r-1,t),below:n.getCellNodeFromCords(o,r+1,t),left:n.getCellNodeFromCords(o-1,r,t),right:n.getCellNodeFromCords(o+1,r,t)}}function ae(e,t){const n=e.getChildren();if(t>=n.length||t<0)throw new Error("Expected table cell to be inside of table row.");return n[t].remove(),e}function ce(e,t,n=!0,o,r){const l=e.getChildren();if(t>=l.length||t<0)throw new Error("Table row target index out of range");const i=l[t];if(!V(i))throw new Error("Row before insertion index does not exist.");for(let e=0;e<o;e++){const e=i.getChildren(),t=e.length,o=Q();for(let n=0;n<t;n++){const t=e[n];$(t)||Z(12);const{above:l,below:i}=ie(t,r);let a=X.NO_STATUS;const c=l&&l.getWidth()||i&&i.getWidth()||void 0;(l&&l.hasHeaderState(X.COLUMN)||i&&i.hasHeaderState(X.COLUMN))&&(a|=X.COLUMN);const h=J(a,1,c);h.append(s()),o.append(h)}n?i.insertAfter(o):i.insertBefore(o)}return e}const he=(e,t)=>e===X.BOTH||e===t?t:X.NO_STATUS;function de(e=!0){const t=d();u(t)||Te(t)||Z(118);const n=t.focus.getNode(),[o,,r]=be(n),[l,i]=we(r,o,o),a=l[0].length,{startRow:c}=i;if(e){const e=c+o.__rowSpan-1,t=l[e],n=Q();for(let o=0;o<a;o++){const{cell:r,startRow:l}=t[o];if(l+r.__rowSpan-1<=e){const e=t[o].cell.__headerState,r=he(e,X.COLUMN);n.append(J(r).append(s()))}else r.setRowSpan(r.__rowSpan+1)}const i=r.getChildAtIndex(e);V(i)||Z(145),i.insertAfter(n)}else{const e=l[c],t=Q();for(let n=0;n<a;n++){const{cell:o,startRow:r}=e[n];if(r===c){const o=e[n].cell.__headerState,r=he(o,X.COLUMN);t.append(J(r).append(s()))}else o.setRowSpan(o.__rowSpan+1)}const n=r.getChildAtIndex(c);V(n)||Z(145),n.insertBefore(t)}}function ue(e,t,n=!0,o,r){const l=e.getChildren(),i=[];for(let e=0;e<l.length;e++){const n=l[e];if(V(n))for(let e=0;e<o;e++){const e=n.getChildren();if(t>=e.length||t<0)throw new Error("Table column target index out of range");const o=e[t];$(o)||Z(12);const{left:l,right:a}=ie(o,r);let c=X.NO_STATUS;(l&&l.hasHeaderState(X.ROW)||a&&a.hasHeaderState(X.ROW))&&(c|=X.ROW);const h=J(c);h.append(s()),i.push({newTableCell:h,targetCell:o})}}return i.forEach((({newTableCell:e,targetCell:t})=>{n?t.insertAfter(e):t.insertBefore(e)})),e}function ge(e=!0){const t=d();u(t)||Te(t)||Z(118);const n=t.anchor.getNode(),o=t.focus.getNode(),[r]=be(n),[l,,i]=be(o),[a,c,h]=we(i,l,r),g=a.length,f=e?Math.max(c.startColumn,h.startColumn):Math.min(c.startColumn,h.startColumn),m=e?f+l.__colSpan-1:f-1,p=i.getFirstChild();V(p)||Z(120);let _=null;function C(e=X.NO_STATUS){const t=J(e).append(s());return null===_&&(_=t),t}let S=p;e:for(let e=0;e<g;e++){if(0!==e){const e=S.getNextSibling();V(e)||Z(121),S=e}const t=a[e],n=t[m<0?0:m].cell.__headerState,o=he(n,X.ROW);if(m<0){Ce(S,C(o));continue}const{cell:r,startColumn:l,startRow:s}=t[m];if(l+r.__colSpan-1<=m){let n=r,l=s,i=m;for(;l!==e&&n.__rowSpan>1;){if(i-=r.__colSpan,!(i>=0)){S.append(C(o));continue e}{const{cell:e,startRow:o}=t[i];n=e,l=o}}n.insertAfter(C(o))}else r.setColSpan(r.__colSpan+1)}null!==_&&_e(_)}function fe(e,t){const n=e.getChildren();for(let e=0;e<n.length;e++){const o=n[e];if(V(o)){const e=o.getChildren();if(t>=e.length||t<0)throw new Error("Table column target index out of range");e[t].remove()}}return e}function me(){const e=d();u(e)||Te(e)||Z(118);const t=e.anchor.getNode(),n=e.focus.getNode(),[o,,r]=be(t),[l]=be(n),[s,i,a]=we(r,o,l),{startRow:c}=i,{startRow:h}=a,g=h+l.__rowSpan-1;if(s.length===g-c+1)return void r.remove();const f=s[0].length,m=s[g+1],p=r.getChildAtIndex(g+1);for(let e=g;e>=c;e--){for(let t=f-1;t>=0;t--){const{cell:n,startRow:o,startColumn:r}=s[e][t];if(r===t&&(e===c&&o<c&&n.setRowSpan(n.__rowSpan-(o-c)),o>=c&&o+n.__rowSpan-1>g))if(n.setRowSpan(n.__rowSpan-(g-o+1)),null===p&&Z(122),0===t)Ce(p,n);else{const{cell:e}=m[t-1];e.insertAfter(n)}}const t=r.getChildAtIndex(e);V(t)||Z(123,String(e)),t.remove()}if(void 0!==m){const{cell:e}=m[0];_e(e)}else{const e=s[c-1],{cell:t}=e[0];_e(t)}}function pe(){const e=d();u(e)||Te(e)||Z(118);const t=e.anchor.getNode(),n=e.focus.getNode(),[o,,r]=be(t),[l]=be(n),[s,i,a]=we(r,o,l),{startColumn:c}=i,{startRow:h,startColumn:g}=a,f=Math.min(c,g),m=Math.max(c+o.__colSpan-1,g+l.__colSpan-1),p=m-f+1;if(s[0].length===m-f+1)return r.selectPrevious(),void r.remove();const _=s.length;for(let e=0;e<_;e++)for(let t=f;t<=m;t++){const{cell:n,startColumn:o}=s[e][t];if(o<f){if(t===f){const e=f-o;n.setColSpan(n.__colSpan-Math.min(p,n.__colSpan-e))}}else if(o+n.__colSpan-1>m){if(t===m){const e=m-o+1;n.setColSpan(n.__colSpan-e)}}else n.remove()}const C=s[h],S=C[g+l.__colSpan];if(void 0!==S){const{cell:e}=S;_e(e)}else{const e=C[g-1],{cell:t}=e;_e(t)}}function _e(e){const t=e.getFirstDescendant();null==t?e.selectStart():t.getParentOrThrow().selectStart()}function Ce(e,t){const n=e.getFirstChild();null!==n?n.insertBefore(t):e.append(t)}function Se(){const e=d();u(e)||Te(e)||Z(118);const t=e.anchor.getNode(),[n,o,r]=be(t),l=n.__colSpan,s=n.__rowSpan;if(l>1){for(let e=1;e<l;e++)n.insertAfter(J(X.NO_STATUS));n.setColSpan(1)}if(s>1){const[e,t]=we(r,n,n),{startColumn:i,startRow:a}=t;let c;for(let t=1;t<s;t++){const n=a+t,r=e[n];c=(c||o).getNextSibling(),V(c)||Z(125);let s=null;for(let e=0;e<i;e++){const t=r[e],o=t.cell;t.startRow===n&&(s=o),o.__colSpan>1&&(e+=o.__colSpan-1)}if(null===s)for(let e=0;e<l;e++)Ce(c,J(X.NO_STATUS));else for(let e=0;e<l;e++)s.insertAfter(J(X.NO_STATUS))}n.setRowSpan(1)}}function we(e,t,n){const o=[];let r=null,l=null;function s(e,s,i){const a={cell:i,startColumn:s,startRow:e},c=i.__rowSpan,h=i.__colSpan;for(let t=0;t<c;t++){void 0===o[e+t]&&(o[e+t]=[]);for(let n=0;n<h;n++)o[e+t][s+n]=a}t.is(i)&&(r=a),n.is(i)&&(l=a)}function i(e,t){return void 0===o[e]||void 0===o[e][t]}const a=e.getChildren();for(let e=0;e<a.length;e++){const t=a[e];V(t)||Z(146);const n=t.getChildren();let o=0;for(const t of n){for($(t)||Z(147);!i(e,o);)o++;s(e,o,t),o+=t.__colSpan}}return null===r&&Z(110),null===l&&Z(111),[o,r,l]}function be(e){let n;if(e instanceof Y)n=e;else if("__type"in e){const o=t(e,$);$(o)||Z(148),n=o}else{const o=t(e.getNode(),$);$(o)||Z(148),n=o}const o=n.getParent();V(o)||Z(149);const r=o.getParent();return Qe(r)||Z(150),[n,o,r]}function ye(e){const[t,,n]=be(e),o=n.getChildren(),r=o.length,l=o[0].getChildren().length,s=new Array(r);for(let e=0;e<r;e++)s[e]=new Array(l);for(let e=0;e<r;e++){const n=o[e].getChildren();let r=0;for(let o=0;o<n.length;o++){for(;s[e][r];)r++;const l=n[o],i=l.__rowSpan||1,a=l.__colSpan||1;for(let t=0;t<i;t++)for(let n=0;n<a;n++)s[e+t][r+n]=l;if(t===l)return{colSpan:a,columnIndex:r,rowIndex:e,rowSpan:i};r+=a}}return null}class Ne{constructor(e,t,n){this.anchor=t,this.focus=n,t._selection=this,n._selection=this,this._cachedNodes=null,this.dirty=!1,this.tableKey=e}getStartEndPoints(){return[this.anchor,this.focus]}isBackward(){return this.focus.isBefore(this.anchor)}getCachedNodes(){return this._cachedNodes}setCachedNodes(e){this._cachedNodes=e}is(e){return!!Te(e)&&(this.tableKey===e.tableKey&&this.anchor.is(e.anchor)&&this.focus.is(e.focus))}set(e,t,n){this.dirty=!0,this.tableKey=e,this.anchor.key=t,this.focus.key=n,this._cachedNodes=null}clone(){return new Ne(this.tableKey,this.anchor,this.focus)}isCollapsed(){return!1}extract(){return this.getNodes()}insertRawText(e){}insertText(){}insertNodes(e){const t=this.focus.getNode();i(t)||Z(151);g(t.select(0,t.getChildrenSize())).insertNodes(e)}getShape(){const e=f(this.anchor.key);$(e)||Z(152);const t=ye(e);null===t&&Z(153);const n=f(this.focus.key);$(n)||Z(154);const o=ye(n);null===o&&Z(155);const r=Math.min(t.columnIndex,o.columnIndex),l=Math.max(t.columnIndex,o.columnIndex),s=Math.min(t.rowIndex,o.rowIndex),i=Math.max(t.rowIndex,o.rowIndex);return{fromX:Math.min(r,l),fromY:Math.min(s,i),toX:Math.max(r,l),toY:Math.max(s,i)}}getNodes(){const e=this._cachedNodes;if(null!==e)return e;const n=this.anchor.getNode(),o=this.focus.getNode(),r=t(n,$),l=t(o,$);$(r)||Z(152),$(l)||Z(154);const s=r.getParent();V(s)||Z(156);const i=s.getParent();Qe(i)||Z(157);const a=l.getParents()[1];if(a!==i){if(i.isParentOf(l)){const e=a.getParent();null==e&&Z(159),this.set(this.tableKey,l.getKey(),e.getKey())}else{const e=i.getParent();null==e&&Z(158),this.set(this.tableKey,e.getKey(),l.getKey())}return this.getNodes()}const[c,h,d]=we(i,r,l);let u=Math.min(h.startColumn,d.startColumn),g=Math.min(h.startRow,d.startRow),f=Math.max(h.startColumn+h.cell.__colSpan-1,d.startColumn+d.cell.__colSpan-1),p=Math.max(h.startRow+h.cell.__rowSpan-1,d.startRow+d.cell.__rowSpan-1),_=u,C=g,S=u,w=g;function b(e){const{cell:t,startColumn:n,startRow:o}=e;u=Math.min(u,n),g=Math.min(g,o),f=Math.max(f,n+t.__colSpan-1),p=Math.max(p,o+t.__rowSpan-1)}for(;u<_||g<C||f>S||p>w;){if(u<_){const e=w-C,t=_-1;for(let n=0;n<=e;n++)b(c[C+n][t]);_=t}if(g<C){const e=S-_,t=C-1;for(let n=0;n<=e;n++)b(c[t][_+n]);C=t}if(f>S){const e=w-C,t=S+1;for(let n=0;n<=e;n++)b(c[C+n][t]);S=t}if(p>w){const e=S-_,t=w+1;for(let n=0;n<=e;n++)b(c[t][_+n]);w=t}}const y=[i];let N=null;for(let e=g;e<=p;e++)for(let t=u;t<=f;t++){const{cell:n}=c[e][t],o=n.getParent();V(o)||Z(160),o!==N&&y.push(o),y.push(n,...Ee(n)),N=o}return m()||(this._cachedNodes=y),y}getTextContent(){const e=this.getNodes();let t="";for(let n=0;n<e.length;n++)t+=e[n].getTextContent();return t}}function Te(e){return e instanceof Ne}function xe(){const e=p("root",0,"element"),t=p("root",0,"element");return new Ne("root",e,t)}function Ee(e){const t=[],n=[e];for(;n.length>0;){const o=n.pop();void 0===o&&Z(112),i(o)&&n.unshift(...o.getChildren()),o!==e&&t.push(o)}return t}class Oe{constructor(e,t){this.isHighlightingCells=!1,this.anchorX=-1,this.anchorY=-1,this.focusX=-1,this.focusY=-1,this.listenersToRemove=new Set,this.tableNodeKey=t,this.editor=e,this.table={columns:0,domRows:[],rows:0},this.tableSelection=null,this.anchorCellNodeKey=null,this.focusCellNodeKey=null,this.anchorCell=null,this.focusCell=null,this.hasHijackedSelectionStyles=!1,this.trackTable()}getTable(){return this.table}removeListeners(){Array.from(this.listenersToRemove).forEach((e=>e()))}trackTable(){const e=new MutationObserver((e=>{this.editor.update((()=>{let t=!1;for(let n=0;n<e.length;n++){const o=e[n].target.nodeName;if("TABLE"===o||"TR"===o){t=!0;break}}if(!t)return;const n=this.editor.getElementByKey(this.tableNodeKey);if(!n)throw new Error("Expected to find TableElement in DOM");this.table=Fe(n)}))}));this.editor.update((()=>{const t=this.editor.getElementByKey(this.tableNodeKey);if(!t)throw new Error("Expected to find TableElement in DOM");this.table=Fe(t),e.observe(t,{childList:!0,subtree:!0})}))}clearHighlight(){const e=this.editor;this.isHighlightingCells=!1,this.anchorX=-1,this.anchorY=-1,this.focusX=-1,this.focusY=-1,this.tableSelection=null,this.anchorCellNodeKey=null,this.focusCellNodeKey=null,this.anchorCell=null,this.focusCell=null,this.hasHijackedSelectionStyles=!1,this.enableHighlightStyle(),e.update((()=>{if(!Qe(f(this.tableNodeKey)))throw new Error("Expected TableNode.");const t=e.getElementByKey(this.tableNodeKey);if(!t)throw new Error("Expected to find TableElement in DOM");const n=Fe(t);ke(e,n,null),_(null),e.dispatchCommand(C,void 0)}))}enableHighlightStyle(){const e=this.editor;e.update((()=>{const t=e.getElementByKey(this.tableNodeKey);if(!t)throw new Error("Expected to find TableElement in DOM");n(t,e._config.theme.tableSelection),t.classList.remove("disable-selection"),this.hasHijackedSelectionStyles=!1}))}disableHighlightStyle(){const t=this.editor;t.update((()=>{const n=t.getElementByKey(this.tableNodeKey);if(!n)throw new Error("Expected to find TableElement in DOM");e(n,t._config.theme.tableSelection),this.hasHijackedSelectionStyles=!0}))}updateTableTableSelection(e){if(null!==e&&e.tableKey===this.tableNodeKey){const t=this.editor;this.tableSelection=e,this.isHighlightingCells=!0,this.disableHighlightStyle(),ke(t,this.table,this.tableSelection)}else null==e?this.clearHighlight():(this.tableNodeKey=e.tableKey,this.updateTableTableSelection(e))}setFocusCellForSelection(e,t=!1){const n=this.editor;n.update((()=>{if(!Qe(f(this.tableNodeKey)))throw new Error("Expected TableNode.");if(!n.getElementByKey(this.tableNodeKey))throw new Error("Expected to find TableElement in DOM");const o=e.x,r=e.y;if(this.focusCell=e,null!==this.anchorCell){const e=(l=n._window,ee?(l||window).getSelection():null);e&&e.setBaseAndExtent(this.anchorCell.elem,0,this.focusCell.elem,0)}var l;if(this.isHighlightingCells||this.anchorX===o&&this.anchorY===r&&!t){if(o===this.focusX&&r===this.focusY)return}else this.isHighlightingCells=!0,this.disableHighlightStyle();if(this.focusX=o,this.focusY=r,this.isHighlightingCells){const t=S(e.elem);if(null!=this.tableSelection&&null!=this.anchorCellNodeKey&&$(t)){const e=t.getKey();this.tableSelection=this.tableSelection.clone()||xe(),this.focusCellNodeKey=e,this.tableSelection.set(this.tableNodeKey,this.anchorCellNodeKey,this.focusCellNodeKey),_(this.tableSelection),n.dispatchCommand(C,void 0),ke(n,this.table,this.tableSelection)}}}))}setAnchorCellForSelection(e){this.isHighlightingCells=!1,this.anchorCell=e,this.anchorX=e.x,this.anchorY=e.y,this.editor.update((()=>{const t=S(e.elem);if($(t)){const e=t.getKey();this.tableSelection=null!=this.tableSelection?this.tableSelection.clone():xe(),this.anchorCellNodeKey=e}}))}formatCells(e){this.editor.update((()=>{const t=d();Te(t)||Z(11);const n=w(),o=n.anchor,r=n.focus;t.getNodes().forEach((t=>{$(t)&&0!==t.getTextContentSize()&&(o.set(t.getKey(),0,"element"),r.set(t.getKey(),t.getChildrenSize(),"element"),n.formatText(e))})),_(t),this.editor.dispatchCommand(C,void 0)}))}clearText(){const e=this.editor;e.update((()=>{const t=f(this.tableNodeKey);if(!Qe(t))throw new Error("Expected TableNode.");const n=d();Te(n)||Z(11);const o=n.getNodes().filter($);if(o.length!==this.table.columns*this.table.rows)o.forEach((e=>{if(i(e)){const t=s(),n=h();t.append(n),e.append(t),e.getChildren().forEach((e=>{e!==t&&e.remove()}))}})),ke(e,this.table,null),_(null),e.dispatchCommand(C,void 0);else{t.selectPrevious(),t.remove();b().selectStart()}}))}}const ve="__lexicalTableSelection";function Me(e,n,o,r){const l=o.getRootElement();if(null===l)throw new Error("No root element.");const a=new Oe(o,e.getKey()),c=o._window||window;!function(e,t){e[ve]=t}(n,a),n.addEventListener("mousedown",(e=>{setTimeout((()=>{if(0!==e.button)return;if(!c)return;const t=Ke(e.target);null!==t&&(Je(e),a.setAnchorCellForSelection(t));const n=()=>{c.removeEventListener("mouseup",n),c.removeEventListener("mousemove",o)},o=e=>{const t=Ke(e.target);null===t||a.anchorX===t.x&&a.anchorY===t.y||(e.preventDefault(),a.setFocusCellForSelection(t))};c.addEventListener("mouseup",n),c.addEventListener("mousemove",o)}),0)}));const h=e=>{0===e.button&&o.update((()=>{const t=d(),n=e.target;Te(t)&&t.tableKey===a.tableNodeKey&&l.contains(n)&&a.clearHighlight()}))};c.addEventListener("mousedown",h),a.listenersToRemove.add((()=>c.removeEventListener("mousedown",h))),a.listenersToRemove.add(o.registerCommand(y,(t=>ze(o,t,"down",e,a)),N)),a.listenersToRemove.add(o.registerCommand(T,(t=>ze(o,t,"up",e,a)),N)),a.listenersToRemove.add(o.registerCommand(x,(t=>ze(o,t,"backward",e,a)),N)),a.listenersToRemove.add(o.registerCommand(E,(t=>ze(o,t,"forward",e,a)),N)),a.listenersToRemove.add(o.registerCommand(O,(e=>{const n=d();if(Te(n)){const o=t(n.focus.getNode(),$);if($(o))return Je(e),o.selectEnd(),!0}return!1}),N));[v,M,R].forEach((n=>{a.listenersToRemove.add(o.registerCommand(n,(n=>()=>{const o=d();if(!Pe(o,e))return!1;if(Te(o))return a.clearText(),!0;if(u(o)){const r=t(o.anchor.getNode(),(e=>$(e)));if(!$(r))return!1;const l=o.anchor.getNode(),c=o.focus.getNode(),h=e.isParentOf(l),d=e.isParentOf(c);if(h&&!d||d&&!h)return a.clearText(),!0;const u=t(o.anchor.getNode(),(e=>i(e))),g=u&&t(u,(e=>i(e)&&$(e.getParent())));if(!i(g)||!i(u))return!1;if(n===M&&null===g.getPreviousSibling())return!0;if((n===R||n===v)&&o.isCollapsed()&&0===o.anchor.offset&&u!==g){const e=u.getChildren(),t=s();return e.forEach((e=>t.append(e))),u.replace(t),u.getWritable().__parent=r.getKey(),!0}}return!1})(n),K))}));const g=n=>{const o=d();if(!Pe(o,e))return!1;if(Te(o))return n.preventDefault(),n.stopPropagation(),a.clearText(),!0;if(u(o)){const e=t(o.anchor.getNode(),(e=>$(e)));if(!$(e))return!1}return!1};function f(t){const n=e.getCordsFromCellNode(t,a.table);return e.getDOMCellFromCordsOrThrow(n.x,n.y,a.table)}return a.listenersToRemove.add(o.registerCommand(F,g,K)),a.listenersToRemove.add(o.registerCommand(k,g,K)),a.listenersToRemove.add(o.registerCommand(H,(n=>{const o=d();if(!Pe(o,e))return!1;if(Te(o))return a.formatCells(n),!0;if(u(o)){const e=t(o.anchor.getNode(),(e=>$(e)));if(!$(e))return!1}return!1}),K)),a.listenersToRemove.add(o.registerCommand(A,(t=>{const n=d();if(!Te(n)||!Pe(n,e))return!1;const o=n.anchor.getNode(),r=n.focus.getNode();if(!$(o)||!$(r))return!1;const[l,s,a]=we(e,o,r),c=Math.max(s.startRow,a.startRow),h=Math.max(s.startColumn,a.startColumn),u=Math.min(s.startRow,a.startRow),g=Math.min(s.startColumn,a.startColumn);for(let e=u;e<=c;e++)for(let n=g;n<=h;n++){const o=l[e][n].cell;o.setFormat(t);const r=o.getChildren();for(let e=0;e<r.length;e++){const n=r[e];i(n)&&!n.isInline()&&n.setFormat(t)}}return!0}),K)),a.listenersToRemove.add(o.registerCommand(B,(n=>{const o=d();if(!Pe(o,e))return!1;if(Te(o))return a.clearHighlight(),!1;if(u(o)){const e=t(o.anchor.getNode(),(e=>$(e)));if(!$(e))return!1}return!1}),K)),r&&a.listenersToRemove.add(o.registerCommand(D,(t=>{const n=d();if(!u(n)||!n.isCollapsed()||!Pe(n,e))return!1;const o=Xe(n.anchor.getNode());if(null===o)return!1;Je(t);const r=e.getCordsFromCellNode(o,a.table);return Be(a,e,r.x,r.y,t.shiftKey?"backward":"forward"),!0}),K)),a.listenersToRemove.add(o.registerCommand(P,(t=>e.isSelected()),N)),a.listenersToRemove.add(o.registerCommand(L,(e=>{const{nodes:n,selection:o}=e,r=o.getStartEndPoints(),l=Te(o),i=u(o)&&null!==t(o.anchor.getNode(),(e=>$(e)))&&null!==t(o.focus.getNode(),(e=>$(e)))||l;if(1!==n.length||!Qe(n[0])||!i||null===r)return!1;const[a]=r,c=n[0],h=c.getChildren(),d=c.getFirstChildOrThrow().getChildrenSize(),g=c.getChildrenSize(),f=t(a.getNode(),(e=>$(e))),m=f&&t(f,(e=>V(e))),p=m&&t(m,(e=>Qe(e)));if(!$(f)||!V(m)||!Qe(p))return!1;const C=m.getIndexWithinParent(),S=Math.min(p.getChildrenSize()-1,C+g-1),w=f.getIndexWithinParent(),b=Math.min(m.getChildrenSize()-1,w+d-1),y=Math.min(w,b),N=Math.min(C,S),T=Math.max(w,b),x=Math.max(C,S),E=p.getChildren();let O,v,M=0;for(let e=N;e<=x;e++){const t=E[e];if(!V(t))return!1;const n=h[M];if(!V(n))return!1;const o=t.getChildren(),r=n.getChildren();let l=0;for(let t=y;t<=T;t++){const n=o[t];if(!$(n))return!1;const i=r[l];if(!$(i))return!1;e===N&&t===y?O=n.getKey():e===x&&t===T&&(v=n.getKey());const a=n.getChildren();i.getChildren().forEach((e=>{if(I(e)){s().append(e),n.append(e)}else n.append(e)})),a.forEach((e=>e.remove())),l++}M++}if(O&&v){const e=xe();e.set(n[0].getKey(),O,v),_(e)}return!0}),K)),a.listenersToRemove.add(o.registerCommand(C,(()=>{const t=d(),n=W();if(u(t)){const{anchor:n,focus:r}=t,l=n.getNode(),s=r.getNode(),i=Xe(l),c=Xe(s),h=!(!i||!e.is(Ye(i))),d=!(!c||!e.is(Ye(c))),u=h!==d,g=h&&d,m=t.isBackward();if(u){const n=t.clone();n.focus.set(e.getKey(),m?0:e.getChildrenSize(),"element"),_(n),Ae(o,a)}else g&&(i.is(c)||(a.setAnchorCellForSelection(f(i)),a.setFocusCellForSelection(f(c),!0)))}return t&&!t.is(n)&&(Te(t)||Te(n))&&a.tableSelection&&!a.tableSelection.is(n)?(Te(t)&&t.tableKey===a.tableNodeKey?a.updateTableTableSelection(t):!Te(t)&&Te(n)&&n.tableKey===a.tableNodeKey&&a.updateTableTableSelection(null),!1):(a.hasHijackedSelectionStyles&&!e.isSelected()?function(e,t){t.enableHighlightStyle(),He(t.table,(t=>{const n=t.elem;t.highlighted=!1,Ue(e,t),n.getAttribute("style")||n.removeAttribute("style")}))}(o,a):!a.hasHijackedSelectionStyles&&e.isSelected()&&Ae(o,a),!1)}),K)),a}function Re(e){return e[ve]}function Ke(e){let t=e;for(;null!=t;){const e=t.nodeName;if("TD"===e||"TH"===e){const e=t._cell;return void 0===e?null:e}t=t.parentNode}return null}function Fe(e){const t=[],n={columns:0,domRows:t,rows:0};let o=e.firstChild,r=0,l=0;for(t.length=0;null!=o;){const e=o.nodeName;if("TD"===e||"TH"===e){const e={elem:o,hasBackgroundColor:""!==o.style.backgroundColor,highlighted:!1,x:r,y:l};o._cell=e;let n=t[l];void 0===n&&(n=t[l]=[]),n[r]=e}else{const e=o.firstChild;if(null!=e){o=e;continue}}const n=o.nextSibling;if(null!=n){r++,o=n;continue}const s=o.parentNode;if(null!=s){const e=s.nextSibling;if(null==e)break;l++,r=0,o=e}}return n.columns=r+1,n.rows=l+1,n}function ke(e,t,n){const o=new Set(n?n.getNodes():[]);He(t,((t,n)=>{const r=t.elem;o.has(n)?(t.highlighted=!0,We(e,t)):(t.highlighted=!1,Ue(e,t),r.getAttribute("style")||r.removeAttribute("style"))}))}function He(e,t){const{domRows:n}=e;for(let e=0;e<n.length;e++){const o=n[e];if(o)for(let n=0;n<o.length;n++){const r=o[n];if(!r)continue;const l=S(r.elem);null!==l&&t(r,l,{x:n,y:e})}}}function Ae(e,t){t.disableHighlightStyle(),He(t.table,(t=>{t.highlighted=!0,We(e,t)}))}const Be=(e,t,n,o,r)=>{const l="forward"===r;switch(r){case"backward":case"forward":return n!==(l?e.table.columns-1:0)?Le(t.getCellNodeFromCordsOrThrow(n+(l?1:-1),o,e.table),l):o!==(l?e.table.rows-1:0)?Le(t.getCellNodeFromCordsOrThrow(l?0:e.table.columns-1,o+(l?1:-1),e.table),l):l?t.selectNext():t.selectPrevious(),!0;case"up":return 0!==o?Le(t.getCellNodeFromCordsOrThrow(n,o-1,e.table),!1):t.selectPrevious(),!0;case"down":return o!==e.table.rows-1?Le(t.getCellNodeFromCordsOrThrow(n,o+1,e.table),!0):t.selectNext(),!0;default:return!1}},De=(e,t,n,o,r)=>{const l="forward"===r;switch(r){case"backward":case"forward":return n!==(l?e.table.columns-1:0)&&e.setFocusCellForSelection(t.getDOMCellFromCordsOrThrow(n+(l?1:-1),o,e.table)),!0;case"up":return 0!==o&&(e.setFocusCellForSelection(t.getDOMCellFromCordsOrThrow(n,o-1,e.table)),!0);case"down":return o!==e.table.rows-1&&(e.setFocusCellForSelection(t.getDOMCellFromCordsOrThrow(n,o+1,e.table)),!0);default:return!1}};function Pe(e,t){if(u(e)||Te(e)){const n=t.isParentOf(e.anchor.getNode()),o=t.isParentOf(e.focus.getNode());return n&&o}return!1}function Le(e,t){t?e.selectStart():e.selectEnd()}const Ie="172,206,247";function We(e,t){const n=t.elem,o=S(n);$(o)||Z(131);null===o.getBackgroundColor()?n.style.setProperty("background-color",`rgb(${Ie})`):n.style.setProperty("background-image",`linear-gradient(to right, rgba(${Ie},0.85), rgba(${Ie},0.85))`),n.style.setProperty("caret-color","transparent")}function Ue(e,t){const n=t.elem,o=S(n);$(o)||Z(131);null===o.getBackgroundColor()&&n.style.removeProperty("background-color"),n.style.removeProperty("background-image"),n.style.removeProperty("caret-color")}function Xe(e){const n=t(e,$);return $(n)?n:null}function Ye(e){const n=t(e,Qe);return Qe(n)?n:null}function ze(e,n,o,r,l){const s=d();if(!Pe(s,r))return!1;if(u(s)&&s.isCollapsed()){if("backward"===o||"forward"===o)return!1;const{anchor:i,focus:a}=s,c=t(i.getNode(),$),h=t(a.getNode(),$);if(!$(c)||!c.is(h))return!1;const d=Ye(c);if(d!==r&&null!=d){const t=e.getElementByKey(d.getKey());if(null!=t)return l.table=Fe(t),ze(e,n,o,d,l)}const u=e.getElementByKey(c.__key),g=e.getElementByKey(i.key);if(null==g||null==u)return!1;let f;if("element"===i.type)f=g.getBoundingClientRect();else{const e=window.getSelection();if(null===e||0===e.rangeCount)return!1;f=e.getRangeAt(0).getBoundingClientRect()}const m="up"===o?c.getFirstChild():c.getLastChild();if(null==m)return!1;const p=e.getElementByKey(m.__key);if(null==p)return!1;const _=p.getBoundingClientRect();if("up"===o?_.top>f.top-f.height:f.bottom+f.height>_.bottom){Je(n);const e=r.getCordsFromCellNode(c,l.table);if(!n.shiftKey)return Be(l,r,e.x,e.y,o);{const t=r.getDOMCellFromCordsOrThrow(e.x,e.y,l.table);l.setAnchorCellForSelection(t),l.setFocusCellForSelection(t,!0)}return!0}}else if(Te(s)){const{anchor:i,focus:a}=s,c=t(i.getNode(),$),h=t(a.getNode(),$),[d]=s.getNodes(),u=e.getElementByKey(d.getKey());if(!$(c)||!$(h)||!Qe(d)||null==u)return!1;l.updateTableTableSelection(s);const g=Fe(u),f=r.getCordsFromCellNode(c,g),m=r.getDOMCellFromCordsOrThrow(f.x,f.y,g);if(l.setAnchorCellForSelection(m),Je(n),n.shiftKey){const e=r.getCordsFromCellNode(h,g);return De(l,d,e.x,e.y,o)}return h.selectEnd(),!0}return!1}function Je(e){e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation()}class $e extends r{static getType(){return"table"}static clone(e){return new $e(e.__key)}static importDOM(){return{table:e=>({conversion:qe,priority:1})}}static importJSON(e){return Ge()}constructor(e){super(e)}exportJSON(){return{...super.exportJSON(),type:"table",version:1}}createDOM(t,n){const o=document.createElement("table");return e(o,t.theme.table),o}updateDOM(){return!1}exportDOM(e){return{...super.exportDOM(e),after:e=>{if(e){const t=e.cloneNode(),n=document.createElement("colgroup"),r=document.createElement("tbody");o(e)&&r.append(...e.children);const l=this.getFirstChildOrThrow();if(!V(l))throw new Error("Expected to find row node.");const s=l.getChildrenSize();for(let e=0;e<s;e++){const e=document.createElement("col");n.append(e)}return t.replaceChildren(n,r),t}}}}canBeEmpty(){return!1}isShadowRoot(){return!0}getCordsFromCellNode(e,t){const{rows:n,domRows:o}=t;for(let t=0;t<n;t++){const n=o[t];if(null==n)continue;const r=n.findIndex((t=>{if(!t)return;const{elem:n}=t;return S(n)===e}));if(-1!==r)return{x:r,y:t}}throw new Error("Cell not found in table.")}getDOMCellFromCords(e,t,n){const{domRows:o}=n,r=o[t];if(null==r)return null;const l=r[e];return null==l?null:l}getDOMCellFromCordsOrThrow(e,t,n){const o=this.getDOMCellFromCords(e,t,n);if(!o)throw new Error("Cell not found at cords.");return o}getCellNodeFromCords(e,t,n){const o=this.getDOMCellFromCords(e,t,n);if(null==o)return null;const r=S(o.elem);return $(r)?r:null}getCellNodeFromCordsOrThrow(e,t,n){const o=this.getCellNodeFromCords(e,t,n);if(!o)throw new Error("Node at cords not TableCellNode.");return o}canSelectBefore(){return!0}canIndent(){return!1}}function je(e,t){const n=e.getElementByKey(t.getKey());if(null==n)throw new Error("Table Element Not Found");return Fe(n)}function qe(e){return{node:Ge()}}function Ge(){return l(new $e)}function Qe(e){return e instanceof $e}export{we as $computeTableMap,J as $createTableCellNode,Ge as $createTableNode,te as $createTableNodeWithDimensions,Q as $createTableRowNode,xe as $createTableSelection,fe as $deleteTableColumn,pe as $deleteTableColumn__EXPERIMENTAL,me as $deleteTableRow__EXPERIMENTAL,je as $getElementForTableNode,be as $getNodeTriplet,ne as $getTableCellNodeFromLexicalNode,ye as $getTableCellNodeRect,se as $getTableColumnIndexFromTableCellNode,re as $getTableNodeFromLexicalNodeOrThrow,le as $getTableRowIndexFromTableCellNode,oe as $getTableRowNodeFromTableCellNodeOrThrow,ue as $insertTableColumn,ge as $insertTableColumn__EXPERIMENTAL,ce as $insertTableRow,de as $insertTableRow__EXPERIMENTAL,$ as $isTableCellNode,Qe as $isTableNode,V as $isTableRowNode,Te as $isTableSelection,ae as $removeTableRowAtIndex,Se as $unmergeCell,j as INSERT_TABLE_COMMAND,X as TableCellHeaderStates,Y as TableCellNode,$e as TableNode,Oe as TableObserver,q as TableRowNode,Me as applyTableHandlers,Ke as getDOMCellFromTarget,Re as getTableObserverFromTableElement};