@lexical/table 0.5.1-next.0 → 0.5.1-next.2

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.
@@ -346,10 +346,10 @@ if (CAN_USE_DOM) {
346
346
  class TableSelection {
347
347
  constructor(editor, tableNodeKey) {
348
348
  this.isHighlightingCells = false;
349
- this.startX = -1;
350
- this.startY = -1;
351
- this.currentX = -1;
352
- this.currentY = -1;
349
+ this.anchorX = -1;
350
+ this.anchorY = -1;
351
+ this.focusX = -1;
352
+ this.focusY = -1;
353
353
  this.listenersToRemove = new Set();
354
354
  this.tableNodeKey = tableNodeKey;
355
355
  this.editor = editor;
@@ -420,6 +420,18 @@ class TableSelection {
420
420
  }
421
421
 
422
422
  clearHighlight() {
423
+ this.isHighlightingCells = false;
424
+ this.anchorX = -1;
425
+ this.anchorY = -1;
426
+ this.focusX = -1;
427
+ this.focusY = -1;
428
+ this.gridSelection = null;
429
+ this.anchorCellNodeKey = null;
430
+ this.focusCellNodeKey = null;
431
+ this.anchorCell = null;
432
+ this.focusCell = null;
433
+ this.hasHijackedSelectionStyles = false;
434
+ this.enableHighlightStyle();
423
435
  this.editor.update(() => {
424
436
  const tableNode = lexical.$getNodeByKey(this.tableNodeKey);
425
437
 
@@ -434,21 +446,9 @@ class TableSelection {
434
446
  }
435
447
 
436
448
  const grid = getTableGrid(tableElement);
437
- this.isHighlightingCells = false;
438
- this.startX = -1;
439
- this.startY = -1;
440
- this.currentX = -1;
441
- this.currentY = -1;
442
- this.gridSelection = null;
443
- this.anchorCellNodeKey = null;
444
- this.focusCellNodeKey = null;
445
- this.anchorCell = null;
446
- this.focusCell = null;
447
- this.hasHijackedSelectionStyles = false;
448
449
  $updateDOMForSelection(grid, null);
449
450
  lexical.$setSelection(null);
450
451
  this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND, undefined);
451
- this.enableHighlightStyle();
452
452
  });
453
453
  }
454
454
 
@@ -484,14 +484,12 @@ class TableSelection {
484
484
  this.isHighlightingCells = true;
485
485
  this.disableHighlightStyle();
486
486
  $updateDOMForSelection(this.grid, this.gridSelection);
487
- }
488
-
489
- if (selection == null) {
487
+ } else if (selection == null) {
490
488
  this.clearHighlight();
491
489
  }
492
490
  }
493
491
 
494
- adjustFocusCellForSelection(cell, ignoreStart = false) {
492
+ setFocusCellForSelection(cell, ignoreStart = false) {
495
493
  this.editor.update(() => {
496
494
  const tableNode = lexical.$getNodeByKey(this.tableNodeKey);
497
495
 
@@ -513,26 +511,26 @@ class TableSelection {
513
511
  const domSelection = getDOMSelection(); // Collapse the selection
514
512
 
515
513
  if (domSelection) {
516
- domSelection.setBaseAndExtent(this.anchorCell.elem, 0, this.anchorCell.elem, 0);
514
+ domSelection.setBaseAndExtent(this.anchorCell.elem, 0, this.focusCell.elem, 0);
517
515
  }
518
516
  }
519
517
 
520
- if (!this.isHighlightingCells && (this.startX !== cellX || this.startY !== cellY || ignoreStart)) {
518
+ if (!this.isHighlightingCells && (this.anchorX !== cellX || this.anchorY !== cellY || ignoreStart)) {
521
519
  this.isHighlightingCells = true;
522
520
  this.disableHighlightStyle();
523
- } else if (cellX === this.currentX && cellY === this.currentY) {
521
+ } else if (cellX === this.focusX && cellY === this.focusY) {
524
522
  return;
525
523
  }
526
524
 
527
- this.currentX = cellX;
528
- this.currentY = cellY;
525
+ this.focusX = cellX;
526
+ this.focusY = cellY;
529
527
 
530
528
  if (this.isHighlightingCells) {
531
529
  const focusTableCellNode = lexical.$getNearestNodeFromDOMNode(cell.elem);
532
530
 
533
531
  if (this.gridSelection != null && this.anchorCellNodeKey != null && $isTableCellNode(focusTableCellNode)) {
534
532
  const focusNodeKey = focusTableCellNode.getKey();
535
- this.gridSelection = lexical.DEPRECATED_$createGridSelection();
533
+ this.gridSelection = this.gridSelection.clone() || lexical.DEPRECATED_$createGridSelection();
536
534
  this.focusCellNodeKey = focusNodeKey;
537
535
  this.gridSelection.set(this.tableNodeKey, this.anchorCellNodeKey, this.focusCellNodeKey);
538
536
  lexical.$setSelection(this.gridSelection);
@@ -544,18 +542,11 @@ class TableSelection {
544
542
  }
545
543
 
546
544
  setAnchorCellForSelection(cell) {
545
+ this.isHighlightingCells = false;
546
+ this.anchorCell = cell;
547
+ this.anchorX = cell.x;
548
+ this.anchorY = cell.y;
547
549
  this.editor.update(() => {
548
- if (this.anchorCell === cell && this.isHighlightingCells) {
549
- const domSelection = getDOMSelection(); // Collapse the selection
550
-
551
- if (domSelection) {
552
- domSelection.setBaseAndExtent(cell.elem, 0, cell.elem, 0);
553
- }
554
- }
555
-
556
- this.anchorCell = cell;
557
- this.startX = cell.x;
558
- this.startY = cell.y;
559
550
  const anchorTableCellNode = lexical.$getNearestNodeFromDOMNode(cell.elem);
560
551
 
561
552
  if ($isTableCellNode(anchorTableCellNode)) {
@@ -666,7 +657,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
666
657
  event.stopImmediatePropagation();
667
658
  event.stopPropagation();
668
659
  tableSelection.setAnchorCellForSelection(cell);
669
- tableSelection.adjustFocusCellForSelection(cell, true);
660
+ tableSelection.setFocusCellForSelection(cell, true);
670
661
  isMouseDown = false;
671
662
  }
672
663
  }); // This is the anchor of the selection.
@@ -702,10 +693,9 @@ function applyTableHandlers(tableNode, tableElement, editor) {
702
693
  const cellX = cell.x;
703
694
  const cellY = cell.y;
704
695
 
705
- if (isMouseDown && (tableSelection.startX !== cellX || tableSelection.startY !== cellY || tableSelection.isHighlightingCells)) {
696
+ if (isMouseDown && (tableSelection.anchorX !== cellX || tableSelection.anchorY !== cellY || tableSelection.isHighlightingCells)) {
706
697
  event.preventDefault();
707
- isMouseDown = true;
708
- tableSelection.adjustFocusCellForSelection(cell);
698
+ tableSelection.setFocusCellForSelection(cell);
709
699
  }
710
700
  }
711
701
  }
@@ -740,9 +730,9 @@ function applyTableHandlers(tableNode, tableElement, editor) {
740
730
  if (isMouseDown) {
741
731
  event.preventDefault();
742
732
  event.stopPropagation();
743
- event.stopImmediatePropagation();
744
- isMouseDown = false;
745
733
  }
734
+
735
+ isMouseDown = false;
746
736
  };
747
737
 
748
738
  window.addEventListener('mouseup', mouseUpCallback);
@@ -996,38 +986,28 @@ function applyTableHandlers(tableNode, tableElement, editor) {
996
986
  return true;
997
987
  }
998
988
 
999
- const parentElementNode = utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isElementNode(n) && $isTableCellNode(n.getParent()));
1000
989
  const nearestElementNode = utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isElementNode(n));
990
+ const topLevelCellElementNode = nearestElementNode && utils.$findMatchingParent(nearestElementNode, n => lexical.$isElementNode(n) && $isTableCellNode(n.getParent()));
1001
991
 
1002
- if (!lexical.$isElementNode(parentElementNode) || !lexical.$isElementNode(nearestElementNode)) {
992
+ if (!lexical.$isElementNode(topLevelCellElementNode) || !lexical.$isElementNode(nearestElementNode)) {
1003
993
  return false;
1004
994
  }
1005
995
 
1006
- const clearCell = () => {
1007
- const newParagraphNode = lexical.$createParagraphNode();
1008
- const textNode = lexical.$createTextNode();
1009
- newParagraphNode.append(textNode);
1010
- tableCellNode.append(newParagraphNode);
1011
- tableCellNode.getChildren().forEach(child => {
1012
- if (child !== newParagraphNode) {
1013
- child.remove();
1014
- }
1015
- });
1016
- };
1017
-
1018
- if (command === lexical.DELETE_LINE_COMMAND && parentElementNode.getPreviousSibling() === null) {
1019
- clearCell();
996
+ if (command === lexical.DELETE_LINE_COMMAND && topLevelCellElementNode.getPreviousSibling() === null) {
997
+ // TODO: Fix Delete Line in Table Cells.
1020
998
  return true;
1021
999
  }
1022
1000
 
1023
1001
  if (command === lexical.DELETE_CHARACTER_COMMAND || command === lexical.DELETE_WORD_COMMAND) {
1024
- if (selection.isCollapsed() && selection.anchor.offset === 0 && parentElementNode === nearestElementNode && parentElementNode.getPreviousSibling() === null) {
1025
- return true;
1026
- }
1027
-
1028
- if (!lexical.$isParagraphNode(parentElementNode) && parentElementNode.getTextContentSize() === 0) {
1029
- clearCell();
1030
- return true;
1002
+ if (selection.isCollapsed() && selection.anchor.offset === 0) {
1003
+ if (nearestElementNode !== topLevelCellElementNode) {
1004
+ const children = nearestElementNode.getChildren();
1005
+ const newParagraphNode = lexical.$createParagraphNode();
1006
+ children.forEach(child => newParagraphNode.append(child));
1007
+ nearestElementNode.replace(newParagraphNode);
1008
+ nearestElementNode.getWritable().__parent = tableCellNode.getKey();
1009
+ return true;
1010
+ }
1031
1011
  }
1032
1012
  }
1033
1013
  }
@@ -1407,14 +1387,14 @@ const adjustFocusNodeInDirection = (tableSelection, tableNode, x, y, direction)
1407
1387
  case 'backward':
1408
1388
  case 'forward':
1409
1389
  if (x !== (isForward ? tableSelection.grid.columns - 1 : 0)) {
1410
- tableSelection.adjustFocusCellForSelection(tableNode.getCellFromCordsOrThrow(x + (isForward ? 1 : -1), y, tableSelection.grid));
1390
+ tableSelection.setFocusCellForSelection(tableNode.getCellFromCordsOrThrow(x + (isForward ? 1 : -1), y, tableSelection.grid));
1411
1391
  }
1412
1392
 
1413
1393
  return true;
1414
1394
 
1415
1395
  case 'up':
1416
1396
  if (y !== 0) {
1417
- tableSelection.adjustFocusCellForSelection(tableNode.getCellFromCordsOrThrow(x, y - 1, tableSelection.grid));
1397
+ tableSelection.setFocusCellForSelection(tableNode.getCellFromCordsOrThrow(x, y - 1, tableSelection.grid));
1418
1398
  return true;
1419
1399
  } else {
1420
1400
  return false;
@@ -1422,7 +1402,7 @@ const adjustFocusNodeInDirection = (tableSelection, tableNode, x, y, direction)
1422
1402
 
1423
1403
  case 'down':
1424
1404
  if (y !== tableSelection.grid.rows - 1) {
1425
- tableSelection.adjustFocusCellForSelection(tableNode.getCellFromCordsOrThrow(x, y + 1, tableSelection.grid));
1405
+ tableSelection.setFocusCellForSelection(tableNode.getCellFromCordsOrThrow(x, y + 1, tableSelection.grid));
1426
1406
  return true;
1427
1407
  } else {
1428
1408
  return false;
@@ -231,7 +231,7 @@ declare export class TableSelection {
231
231
  removeListeners(): void;
232
232
  trackTableGrid(): void;
233
233
  clearHighlight(): void;
234
- adjustFocusCellForSelection(cell: Cell): void;
234
+ setFocusCellForSelection(cell: Cell): void;
235
235
  setAnchorCellForSelection(cell: Cell): void;
236
236
  formatCells(type: TextFormatType): void;
237
237
  clearText(): void;
@@ -11,53 +11,52 @@ type:"tablecell",width:this.getWidth()}}getTag(){return this.hasHeader()?"th":"t
11
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
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
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.");}
15
- let I="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement;
16
- if(I){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 ";document.body&&document.body.append(a)}
17
- class J{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=K(e)}})});this.editor.update(()=>{let b=this.editor.getElementByKey(this.tableNodeKey);if(!b)throw Error("Expected to find TableElement in DOM");this.grid=K(b);a.observe(b,{childList:!0,subtree:!0})})}clearHighlight(){this.editor.update(()=>{var a=d.$getNodeByKey(this.tableNodeKey);
19
- if(!L(a))throw Error("Expected TableNode.");a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a=K(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;M(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(),M(this.grid,this.gridSelection));null==a&&this.clearHighlight()}adjustFocusCellForSelection(a,b=!1){this.editor.update(()=>{var e=d.$getNodeByKey(this.tableNodeKey);if(!L(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=I?window.getSelection():null;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),M(this.grid,this.gridSelection)))})}setAnchorCellForSelection(a){this.editor.update(()=>{if(this.anchorCell===a&&this.isHighlightingCells){var b=I?window.getSelection():null;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(),
24
- this.anchorCellNodeKey=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);
25
- if(!L(a))throw Error("Expected TableNode.");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()})}}),M(this.grid,null),d.$setSelection(null),this.editor.dispatchCommand(d.SELECTION_CHANGE_COMMAND,void 0))})}}
26
- function N(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 K(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 M(a,b){let e=[],h=new Set(b?b.getNodes():[]);O(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 O(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 P(a){a.disableHighlightStyle();O(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 aa(a){a.enableHighlightStyle();O(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")})}
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 G(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.");}
15
+ let H="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement;
16
+ if(H){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 ";document.body&&document.body.append(a)}
17
+ class I{constructor(a,b){this.isHighlightingCells=!1;this.focusY=this.focusX=this.anchorY=this.anchorX=-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.isHighlightingCells=!1;this.focusY=
19
+ this.focusX=this.anchorY=this.anchorX=-1;this.focusCell=this.anchorCell=this.focusCellNodeKey=this.anchorCellNodeKey=this.gridSelection=null;this.hasHijackedSelectionStyles=!1;this.enableHighlightStyle();this.editor.update(()=>{var a=d.$getNodeByKey(this.tableNodeKey);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);L(a,null);d.$setSelection(null);this.editor.dispatchCommand(d.SELECTION_CHANGE_COMMAND,
20
+ void 0)})}enableHighlightStyle(){this.editor.update(()=>{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!=
21
+ a&&a.gridKey===this.tableNodeKey?(this.gridSelection=a,this.isHighlightingCells=!0,this.disableHighlightStyle(),L(this.grid,this.gridSelection)):null==a&&this.clearHighlight()}setFocusCellForSelection(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=H?window.getSelection():
22
+ null;c&&c.setBaseAndExtent(this.anchorCell.elem,0,this.focusCell.elem,0)}if(!this.isHighlightingCells&&(this.anchorX!==e||this.anchorY!==h||b))this.isHighlightingCells=!0,this.disableHighlightStyle();else if(e===this.focusX&&h===this.focusY)return;this.focusX=e;this.focusY=h;this.isHighlightingCells&&(e=d.$getNearestNodeFromDOMNode(a.elem),null!=this.gridSelection&&null!=this.anchorCellNodeKey&&B(e)&&(e=e.getKey(),this.gridSelection=this.gridSelection.clone()||d.DEPRECATED_$createGridSelection(),
23
+ this.focusCellNodeKey=e,this.gridSelection.set(this.tableNodeKey,this.anchorCellNodeKey,this.focusCellNodeKey),d.$setSelection(this.gridSelection),this.editor.dispatchCommand(d.SELECTION_CHANGE_COMMAND,void 0),L(this.grid,this.gridSelection)))})}setAnchorCellForSelection(a){this.isHighlightingCells=!1;this.anchorCell=a;this.anchorX=a.x;this.anchorY=a.y;this.editor.update(()=>{var 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)||G(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)||G(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
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}};
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.setFocusCellForSelection(b.getCellFromCordsOrThrow(e+(l?1:-1),h,a.grid)),!0;case "up":return 0!==h?(a.setFocusCellForSelection(b.getCellFromCordsOrThrow(e,h-1,a.grid)),!0):!1;case "down":return h!==a.grid.rows-1?(a.setFocusCellForSelection(b.getCellFromCordsOrThrow(e,h+1,a.grid)),!0):!1;default:return!1}};
33
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:ba,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"),
34
+ class U extends d.DEPRECATED_GridNode{static getType(){return"table"}static clone(a){return new U(a.__key)}static importDOM(){return{table:()=>({conversion:V,priority:1})}}static importJSON(){return W()}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
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
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 ba(){return{node:V()}}function V(){return new U}function L(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 X(a){a=n.$findMatchingParent(a,b=>L(b));if(L(a))return a;throw Error("Expected table cell to be inside of table.");}
38
- function Z(a,b){let e=X(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 ca=d.createCommand("INSERT_TABLE_COMMAND");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 K(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=X;exports.$getTableRowIndexFromTableCellNode=function(a){let b=W(a);return X(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=L;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=ca;exports.TableCellHeaderStates=q;exports.TableCellNode=t;exports.TableNode=U;exports.TableRowNode=C;exports.TableSelection=J;
45
- exports.applyTableHandlers=function(a,b,e){let h=e.getRootElement();if(null===h)throw Error("No root element.");let c=new J(e,a.getKey());b.__lexicalTableSelection=c;let l=!1,p=!1;b.addEventListener("dblclick",g=>{let f=N(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=N(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=N(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_HIGH));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_HIGH));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_HIGH));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=>d.$isElementNode(m)))throw Error("Expected BlockNode Parent");
54
- 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();g.stopPropagation();return S(c,a,f.x,f.y,"forward")}return!1},
55
- d.COMMAND_PRIORITY_HIGH));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)&&B(w.getParent()));k=n.$findMatchingParent(f.anchor.getNode(),w=>
56
- d.$isElementNode(w));if(!d.$isElementNode(m)||!d.$isElementNode(k))return!1;const G=()=>{const w=d.$createParagraphNode(),da=d.$createTextNode();w.append(da);r.append(w);r.getChildren().forEach(Y=>{Y!==w&&Y.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;if(!d.$isParagraphNode(m)&&0===m.getTextContentSize())return G(),
57
- !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,y,d.COMMAND_PRIORITY_CRITICAL));
58
- 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,()=>{var g=d.$getSelection();if(!T(g,
59
- 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(),R(c,a,f.x,f.y,g.shiftKey?
60
- "backward":"forward"),!0}return!1},d.COMMAND_PRIORITY_HIGH));c.listenersToRemove.add(e.registerCommand(d.FOCUS_COMMAND,()=>a.isSelected(),d.COMMAND_PRIORITY_HIGH));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=g.isBackward(),k=d.$createRangeSelection(),
61
- 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),P(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),!0}}if(g&&!g.is(f)&&(d.DEPRECATED_$isGridSelection(g)||
62
- 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()?(aa(c),p=!1):!c.hasHijackedSelectionStyles&&a.isSelected()&&P(c);return!1},d.COMMAND_PRIORITY_CRITICAL));return c};exports.getCellFromTarget=N;
63
- exports.getTableSelectionFromTableElement=function(a){return a.__lexicalTableSelection}
37
+ return a}canSelectBefore(){return!0}canIndent(){return!1}}function V(){return{node:W()}}function W(){return new U}function K(a){return a instanceof U}function X(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 aa=d.createCommand("INSERT_TABLE_COMMAND");exports.$createTableCellNode=z;exports.$createTableNode=W;
39
+ exports.$createTableNodeWithDimensions=function(a,b,e=!0){let h=W();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 X(a).getChildren().findIndex(b=>b.is(a))};exports.$getTableNodeFromLexicalNodeOrThrow=Y;exports.$getTableRowIndexFromTableCellNode=function(a){let b=X(a);return Y(b).getChildren().findIndex(e=>e.is(b))};exports.$getTableRowNodeFromTableCellNodeOrThrow=X;
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)||G(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)||G(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=aa;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.setFocusCellForSelection(f,!0),l=!1)});b.addEventListener("mousedown",g=>{setTimeout(()=>{if(0===g.button){var f=M(g.target);null!==f&&(g.preventDefault(),g.stopPropagation(),
46
+ 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.anchorX!==k||c.anchorY!==m||c.isHighlightingCells)&&(g.preventDefault(),c.setFocusCellForSelection(f))}}});b.addEventListener("mouseleave",()=>{});let u=g=>{l=!0;0===g.button&&e.update(()=>{const f=d.$getSelection();if(d.DEPRECATED_$isGridSelection(f)&&f.gridKey===c.tableNodeKey&&
47
+ 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());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",v));c.listenersToRemove.add(e.registerCommand(d.KEY_ARROW_DOWN_COMMAND,
48
+ 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(),g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(m.x,
49
+ 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_HIGH));c.listenersToRemove.add(e.registerCommand(d.KEY_ARROW_UP_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(),
50
+ 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,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=
51
+ 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_HIGH));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(),m=>B(m));if(!B(k))return!1;k=a.getCordsFromCellNode(k,c.grid);if(null==n.$findMatchingParent(f.anchor.getNode(),
52
+ 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;f=a.getCordsFromCellNode(f,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();
53
+ return S(c,a,f.x,f.y,"backward")}return!1},d.COMMAND_PRIORITY_HIGH));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=>d.$isElementNode(m)))throw Error("Expected BlockNode Parent");if(f.anchor.offset===f.anchor.getNode().getTextContentSize()||
54
+ 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();g.stopPropagation();return S(c,a,f.x,f.y,"forward")}return!1},d.COMMAND_PRIORITY_HIGH));let A=g=>()=>{var f=
55
+ 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;k=(m=n.$findMatchingParent(f.anchor.getNode(),w=>d.$isElementNode(w)))&&n.$findMatchingParent(m,w=>d.$isElementNode(w)&&B(w.getParent()));if(!d.$isElementNode(k)||!d.$isElementNode(m))return!1;
56
+ if(g===d.DELETE_LINE_COMMAND&&null===k.getPreviousSibling())return!0;if((g===d.DELETE_CHARACTER_COMMAND||g===d.DELETE_WORD_COMMAND)&&f.isCollapsed()&&0===f.anchor.offset&&m!==k){f=m.getChildren();const w=d.$createParagraphNode();f.forEach(ba=>w.append(ba));m.replace(w);m.getWritable().__parent=r.getKey();return!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=>
57
+ {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,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=
58
+ 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,()=>{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));
59
+ 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(),R(c,a,f.x,f.y,g.shiftKey?"backward":"forward"),!0}return!1},d.COMMAND_PRIORITY_HIGH));c.listenersToRemove.add(e.registerCommand(d.FOCUS_COMMAND,()=>a.isSelected(),d.COMMAND_PRIORITY_HIGH));c.listenersToRemove.add(e.registerCommand(d.SELECTION_CHANGE_COMMAND,
60
+ ()=>{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=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=
61
+ d.DEPRECATED_$createGridSelection();m=a.getKey();r=a.getFirstChildOrThrow().getFirstChild();let w=a.getLastChildOrThrow().getLastChild();if(null!=r&&null!=w)return k.set(m,r.getKey(),w.getKey()),d.$setSelection(k),c.updateTableGridSelection(k),!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)&&
62
+ 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));return c};exports.getCellFromTarget=M;exports.getTableSelectionFromTableElement=function(a){return a.__lexicalTableSelection}
@@ -19,13 +19,13 @@ export declare type Grid = {
19
19
  rows: number;
20
20
  };
21
21
  export declare class TableSelection {
22
- currentX: number;
23
- currentY: number;
22
+ focusX: number;
23
+ focusY: number;
24
24
  listenersToRemove: Set<() => void>;
25
25
  grid: Grid;
26
26
  isHighlightingCells: boolean;
27
- startX: number;
28
- startY: number;
27
+ anchorX: number;
28
+ anchorY: number;
29
29
  tableNodeKey: NodeKey;
30
30
  anchorCell: Cell | null;
31
31
  focusCell: Cell | null;
@@ -42,7 +42,7 @@ export declare class TableSelection {
42
42
  enableHighlightStyle(): void;
43
43
  disableHighlightStyle(): void;
44
44
  updateTableGridSelection(selection: GridSelection | null): void;
45
- adjustFocusCellForSelection(cell: Cell, ignoreStart?: boolean): void;
45
+ setFocusCellForSelection(cell: Cell, ignoreStart?: boolean): void;
46
46
  setAnchorCellForSelection(cell: Cell): void;
47
47
  formatCells(type: TextFormatType): void;
48
48
  clearText(): void;
package/package.json CHANGED
@@ -8,13 +8,13 @@
8
8
  "table"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.5.1-next.0",
11
+ "version": "0.5.1-next.2",
12
12
  "main": "LexicalTable.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.5.1-next.0"
14
+ "lexical": "0.5.1-next.2"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/utils": "0.5.1-next.0"
17
+ "@lexical/utils": "0.5.1-next.2"
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",