@lexical/table 0.3.3 → 0.3.6

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.
@@ -45,7 +45,7 @@ class TableCellNode extends lexical.GridCellNode {
45
45
  }
46
46
 
47
47
  static importJSON(serializedNode) {
48
- return $createTableCellNode(serializedNode.headerState, serializedNode.colSpan, serializedNode.width);
48
+ return $createTableCellNode(serializedNode.headerState, serializedNode.colSpan, serializedNode.width || undefined);
49
49
  }
50
50
 
51
51
  constructor(headerState = TableCellHeaderStates.NO_STATUS, colSpan = 1, width, key) {
@@ -471,7 +471,10 @@ class TableSelection {
471
471
 
472
472
  if (anchorElement && focusElement) {
473
473
  const domSelection = getDOMSelection();
474
- domSelection.setBaseAndExtent(anchorElement, 0, focusElement, 0);
474
+
475
+ if (domSelection) {
476
+ domSelection.setBaseAndExtent(anchorElement, 0, focusElement, 0);
477
+ }
475
478
  }
476
479
 
477
480
  $updateDOMForSelection(this.grid, this.gridSelection);
@@ -501,7 +504,9 @@ class TableSelection {
501
504
 
502
505
  if (this.anchorCell !== null) {
503
506
  // Collapse the selection
504
- domSelection.setBaseAndExtent(this.anchorCell.elem, 0, cell.elem, 0);
507
+ if (domSelection) {
508
+ domSelection.setBaseAndExtent(this.anchorCell.elem, 0, cell.elem, 0);
509
+ }
505
510
  }
506
511
 
507
512
  if (!this.isHighlightingCells && (this.startX !== cellX || this.startY !== cellY || ignoreStart)) {
@@ -536,7 +541,11 @@ class TableSelection {
536
541
  this.startX = cell.x;
537
542
  this.startY = cell.y;
538
543
  const domSelection = getDOMSelection();
539
- domSelection.setBaseAndExtent(cell.elem, 0, cell.elem, 0);
544
+
545
+ if (domSelection) {
546
+ domSelection.setBaseAndExtent(cell.elem, 0, cell.elem, 0);
547
+ }
548
+
540
549
  const anchorTableCellNode = lexical.$getNearestNodeFromDOMNode(cell.elem);
541
550
 
542
551
  if ($isTableCellNode(anchorTableCellNode)) {
@@ -1385,14 +1394,14 @@ class TableNode extends lexical.GridNode {
1385
1394
 
1386
1395
  static importDOM() {
1387
1396
  return {
1388
- table: node => ({
1397
+ table: _node => ({
1389
1398
  conversion: convertTableElement,
1390
1399
  priority: 0
1391
1400
  })
1392
1401
  };
1393
1402
  }
1394
1403
 
1395
- static importJSON(serializedNode) {
1404
+ static importJSON(_serializedNode) {
1396
1405
  return $createTableNode();
1397
1406
  }
1398
1407
 
@@ -1557,7 +1566,7 @@ function $getElementGridForTableNode(editor, tableNode) {
1557
1566
 
1558
1567
  return getTableGrid(tableElement);
1559
1568
  }
1560
- function convertTableElement(domNode) {
1569
+ function convertTableElement(_domNode) {
1561
1570
  return {
1562
1571
  node: $createTableNode()
1563
1572
  };
@@ -1689,7 +1698,7 @@ function $insertTableRow(tableNode, targetIndex, shouldInsertAfter = true, rowCo
1689
1698
  below
1690
1699
  } = $getTableCellSiblingsFromTableCellNode(tableCellFromTargetRow, grid);
1691
1700
  let headerState = TableCellHeaderStates.NO_STATUS;
1692
- const width = above && above.getWidth() || below && below.getWidth() || null;
1701
+ const width = above && above.getWidth() || below && below.getWidth() || undefined;
1693
1702
 
1694
1703
  if (above && above.hasHeaderState(TableCellHeaderStates.COLUMN) || below && below.hasHeaderState(TableCellHeaderStates.COLUMN)) {
1695
1704
  headerState |= TableCellHeaderStates.COLUMN;
@@ -218,7 +218,6 @@ declare export class TableSelection {
218
218
  currentX: number;
219
219
  currentY: number;
220
220
  listenersToRemove: Set<() => void>;
221
- domListeners: Set<() => void>;
222
221
  grid: Grid;
223
222
  isHighlightingCells: boolean;
224
223
  isMouseDown: boolean;
@@ -5,7 +5,7 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  'use strict';var f=require("lexical"),p=require("@lexical/utils");let q={BOTH:3,COLUMN:2,NO_STATUS:0,ROW:1};
8
- class t extends f.GridCellNode{static getType(){return"tablecell"}static clone(a){return new t(a.__headerState,a.__colSpan,a.__width,a.__key)}static importDOM(){return{td:()=>({conversion:w,priority:0}),th:()=>({conversion:w,priority:0})}}static importJSON(a){return x(a.headerState,a.colSpan,a.width)}constructor(a=q.NO_STATUS,b=1,d,k){super(b,k);this.__headerState=a;this.__width=d}createDOM(a){let b=document.createElement(this.getTag());this.__width&&(b.style.width=`${this.__width}px`);p.addClassNamesToElement(b,
8
+ class t extends f.GridCellNode{static getType(){return"tablecell"}static clone(a){return new t(a.__headerState,a.__colSpan,a.__width,a.__key)}static importDOM(){return{td:()=>({conversion:w,priority:0}),th:()=>({conversion:w,priority:0})}}static importJSON(a){return x(a.headerState,a.colSpan,a.width||void 0)}constructor(a=q.NO_STATUS,b=1,d,k){super(b,k);this.__headerState=a;this.__width=d}createDOM(a){let b=document.createElement(this.getTag());this.__width&&(b.style.width=`${this.__width}px`);p.addClassNamesToElement(b,
9
9
  a.theme.tableCell,this.hasHeader()&&a.theme.tableCellHeader);return b}exportDOM(a){({element:a}=super.exportDOM(a));if(a){let b=this.getParentOrThrow().getChildrenSize();a.style.border="1px solid black";a.style.width=`${this.getWidth()||Math.max(90,700/b)}px`;a.style.verticalAlign="top";a.style.textAlign="start";this.hasHeader()&&(a.style.backgroundColor="#f2f3f5")}return{element:a}}exportJSON(){return{...super.exportJSON(),colSpan:super.__colSpan,headerState:this.__headerState,type:"tablecell",width:this.getWidth()}}getTag(){return this.hasHeader()?
10
10
  "th":"td"}setHeaderStyles(a){this.getWritable().__headerState=a;return this.__headerState}getHeaderStyles(){return this.getLatest().__headerState}setWidth(a){this.getWritable().__width=a;return this.__width}getWidth(){return this.getLatest().__width}toggleHeaderStyle(a){let b=this.getWritable();b.__headerState=(b.__headerState&a)===a?b.__headerState-a:b.__headerState+a;return b}hasHeaderState(a){return(this.getHeaderStyles()&a)===a}hasHeader(){return this.getLatest().__headerState!==q.NO_STATUS}updateDOM(a){return a.__headerState!==
11
11
  this.__headerState||a.__width!==this.__width}collapseAtStart(){return!0}canBeEmpty(){return!1}canIndent(){return!1}}function w(a){a=a.nodeName.toLowerCase();return{forChild:(b,d)=>{if(y(d)&&!f.$isElementNode(b)){d=f.$createParagraphNode();if(f.$isLineBreakNode(b)&&"\n"===b.getTextContent())return null;d.append(b);return d}return b},node:x("th"===a?q.ROW:q.NO_STATUS)}}function x(a,b=1,d){return new t(a,b,d)}function y(a){return a instanceof t}
@@ -17,11 +17,11 @@ class E{constructor(a,b){this.isHighlightingCells=!1;this.currentY=this.currentX
17
17
  {this.editor.update(()=>{var d=!1;for(let k=0;k<b.length;k++){const c=b[k].target.nodeName;if("TABLE"===c||"TR"===c){d=!0;break}}if(d){d=this.editor.getElementByKey(this.tableNodeKey);if(!d)throw Error("Expected to find TableElement in DOM");this.grid=F(d)}})});this.editor.update(()=>{let b=this.editor.getElementByKey(this.tableNodeKey);if(!b)throw Error("Expected to find TableElement in DOM");this.grid=F(b);a.observe(b,{childList:!0,subtree:!0})})}clearHighlight(){this.editor.update(()=>{var a=f.$getNodeByKey(this.tableNodeKey);
18
18
  if(!G(a))throw Error("Expected TableNode.");a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a=F(a);this.isHighlightingCells=!1;this.currentY=this.currentX=this.startY=this.startX=-1;this.focusCell=this.anchorCell=this.focusCellNodeKey=this.anchorCellNodeKey=this.gridSelection=null;this.hasHijackedSelectionStyles=!1;H(a,null);f.$setSelection(null);this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND,void 0);this.enableHighlightStyle()})}enableHighlightStyle(){this.editor.update(()=>
19
19
  {let a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a.classList.remove("disable-selection");this.hasHijackedSelectionStyles=!1})}disableHighlightStyle(){this.editor.update(()=>{let a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a.classList.add("disable-selection");this.hasHijackedSelectionStyles=!0})}updateTableGridSelection(a){if(null!=a){this.gridSelection=a;this.isHighlightingCells=
20
- !0;this.disableHighlightStyle();let b=this.editor.getElementByKey(a.anchor.key);a=this.editor.getElementByKey(a.focus.key);b&&a&&window.getSelection().setBaseAndExtent(b,0,a,0);H(this.grid,this.gridSelection)}else this.clearHighlight()}adjustFocusCellForSelection(a,b=!1){this.editor.update(()=>{var d=f.$getNodeByKey(this.tableNodeKey);if(!G(d))throw Error("Expected TableNode.");if(!this.editor.getElementByKey(this.tableNodeKey))throw Error("Expected to find TableElement in DOM");d=a.x;let k=a.y;this.focusCell=
21
- a;let c=window.getSelection();null!==this.anchorCell&&c.setBaseAndExtent(this.anchorCell.elem,0,a.elem,0);if(!this.isHighlightingCells&&(this.startX!==d||this.startY!==k||b))this.isHighlightingCells=!0,this.disableHighlightStyle();else if(d===this.currentX&&k===this.currentY)return;this.currentX=d;this.currentY=k;this.isHighlightingCells&&(d=f.$getNearestNodeFromDOMNode(a.elem),null!=this.gridSelection&&null!=this.anchorCellNodeKey&&y(d)&&(d=d.getKey(),this.gridSelection=f.$createGridSelection(),
22
- this.focusCellNodeKey=d,this.gridSelection.set(this.tableNodeKey,this.anchorCellNodeKey,this.focusCellNodeKey),f.$setSelection(this.gridSelection),this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND,void 0),H(this.grid,this.gridSelection)))})}setAnchorCellForSelection(a){this.editor.update(()=>{this.anchorCell=a;this.startX=a.x;this.startY=a.y;window.getSelection().setBaseAndExtent(a.elem,0,a.elem,0);var b=f.$getNearestNodeFromDOMNode(a.elem);y(b)&&(b=b.getKey(),this.gridSelection=f.$createGridSelection(),
23
- this.anchorCellNodeKey=b)})}formatCells(a){this.editor.update(()=>{let b=f.$getSelection();f.$isGridSelection(b)||D(11);let d=f.$createRangeSelection(),k=d.anchor,c=d.focus;b.getNodes().forEach(h=>{y(h)&&0!==h.getTextContentSize()&&(k.set(h.getKey(),0,"element"),c.set(h.getKey(),h.getChildrenSize(),"element"),d.formatText(a))});f.$setSelection(b);this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND,void 0)})}clearText(){this.editor.update(()=>{let a=f.$getNodeByKey(this.tableNodeKey);if(!G(a))throw Error("Expected TableNode.");
24
- var b=f.$getSelection();f.$isGridSelection(b)||D(11);b=b.getNodes().filter(y);b.length===this.grid.columns*this.grid.rows?(a.selectPrevious(),a.remove(),this.clearHighlight()):(b.forEach(d=>{if(f.$isElementNode(d)){let k=f.$createParagraphNode(),c=f.$createTextNode();k.append(c);d.append(k);d.getChildren().forEach(h=>{h!==k&&h.remove()})}}),H(this.grid,null),f.$setSelection(null),this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND,void 0))})}}
20
+ !0;this.disableHighlightStyle();let b=this.editor.getElementByKey(a.anchor.key);a=this.editor.getElementByKey(a.focus.key);if(b&&a){let d=window.getSelection();d&&d.setBaseAndExtent(b,0,a,0)}H(this.grid,this.gridSelection)}else this.clearHighlight()}adjustFocusCellForSelection(a,b=!1){this.editor.update(()=>{var d=f.$getNodeByKey(this.tableNodeKey);if(!G(d))throw Error("Expected TableNode.");if(!this.editor.getElementByKey(this.tableNodeKey))throw Error("Expected to find TableElement in DOM");d=a.x;
21
+ let k=a.y;this.focusCell=a;let c=window.getSelection();null!==this.anchorCell&&c&&c.setBaseAndExtent(this.anchorCell.elem,0,a.elem,0);if(!this.isHighlightingCells&&(this.startX!==d||this.startY!==k||b))this.isHighlightingCells=!0,this.disableHighlightStyle();else if(d===this.currentX&&k===this.currentY)return;this.currentX=d;this.currentY=k;this.isHighlightingCells&&(d=f.$getNearestNodeFromDOMNode(a.elem),null!=this.gridSelection&&null!=this.anchorCellNodeKey&&y(d)&&(d=d.getKey(),this.gridSelection=
22
+ f.$createGridSelection(),this.focusCellNodeKey=d,this.gridSelection.set(this.tableNodeKey,this.anchorCellNodeKey,this.focusCellNodeKey),f.$setSelection(this.gridSelection),this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND,void 0),H(this.grid,this.gridSelection)))})}setAnchorCellForSelection(a){this.editor.update(()=>{this.anchorCell=a;this.startX=a.x;this.startY=a.y;var b=window.getSelection();b&&b.setBaseAndExtent(a.elem,0,a.elem,0);b=f.$getNearestNodeFromDOMNode(a.elem);y(b)&&(b=b.getKey(),
23
+ this.gridSelection=f.$createGridSelection(),this.anchorCellNodeKey=b)})}formatCells(a){this.editor.update(()=>{let b=f.$getSelection();f.$isGridSelection(b)||D(11);let d=f.$createRangeSelection(),k=d.anchor,c=d.focus;b.getNodes().forEach(h=>{y(h)&&0!==h.getTextContentSize()&&(k.set(h.getKey(),0,"element"),c.set(h.getKey(),h.getChildrenSize(),"element"),d.formatText(a))});f.$setSelection(b);this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND,void 0)})}clearText(){this.editor.update(()=>{let a=f.$getNodeByKey(this.tableNodeKey);
24
+ if(!G(a))throw Error("Expected TableNode.");var b=f.$getSelection();f.$isGridSelection(b)||D(11);b=b.getNodes().filter(y);b.length===this.grid.columns*this.grid.rows?(a.selectPrevious(),a.remove(),this.clearHighlight()):(b.forEach(d=>{if(f.$isElementNode(d)){let k=f.$createParagraphNode(),c=f.$createTextNode();k.append(c);d.append(k);d.getChildren().forEach(h=>{h!==k&&h.remove()})}}),H(this.grid,null),f.$setSelection(null),this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND,void 0))})}}
25
25
  function I(a){for(;null!=a;){let b=a.nodeName;if("TD"===b||"TH"===b){a=a._cell;if(void 0===a)break;return a}a=a.parentNode}return null}
26
26
  function F(a){let b=[],d={cells:b,columns:0,rows:0};var k=a.firstChild;let c=a=0;for(b.length=0;null!=k;){var h=k.nodeName;if("TD"===h||"TH"===h)h={elem:k,highlighted:!1,x:a,y:c},k._cell=h,void 0===b[c]&&(b[c]=[]),b[c][a]=h;else if(h=k.firstChild,null!=h){k=h;continue}h=k.nextSibling;if(null!=h)a++,k=h;else if(h=k.parentNode,null!=h){k=h.nextSibling;if(null==k)break;c++;a=0}}d.columns=a+1;d.rows=c+1;return d}
27
27
  function H(a,b){let d=[],k=new Set(b?b.getNodes():[]);J(a,(c,h)=>{let n=c.elem;k.has(h)?(c.highlighted=!0,n.style.setProperty("background-color","rgb(172, 206, 247)"),n.style.setProperty("caret-color","transparent"),d.push(c)):(c.highlighted=!1,n.style.removeProperty("background-color"),n.style.removeProperty("caret-color"),n.getAttribute("style")||n.removeAttribute("style"))});return d}
@@ -39,7 +39,7 @@ exports.$deleteTableColumn=function(a,b){let d=a.getChildren();for(let c=0;c<d.l
39
39
  exports.$getTableColumnIndexFromTableCellNode=function(a){return W(a).getChildren().findIndex(b=>b.is(a))};exports.$getTableNodeFromLexicalNodeOrThrow=X;exports.$getTableRowIndexFromTableCellNode=function(a){let b=W(a);return X(b).getChildren().findIndex(d=>d.is(b))};exports.$getTableRowNodeFromTableCellNodeOrThrow=W;
40
40
  exports.$insertTableColumn=function(a,b,d=!0,k){let c=a.getChildren();for(let r=0;r<c.length;r++){let v=c[r];if(C(v))for(let g=0;g<k;g++){var h=q.NO_STATUS;0===r&&(h|=q.ROW);h=x(h);h.append(f.$createParagraphNode());var n=v.getChildren();if(b>=n.length||0>b)throw Error("Table column target index out of range");n=n[b];d?n.insertAfter(h):n.insertBefore(h)}}return a};
41
41
  exports.$insertTableRow=function(a,b,d=!0,k,c){var h=a.getChildren();if(b>=h.length||0>b)throw Error("Table row target index out of range");b=h[b];if(C(b))for(h=0;h<k;h++){let v=b.getChildren(),g=v.length,e=B();for(let l=0;l<g;l++){var n=v[l];y(n)||D(12);var r=c;let m=X(n),{x:u,y:L}=m.getCordsFromCellNode(n,r);n={above:m.getCellNodeFromCords(u,L-1,r),below:m.getCellNodeFromCords(u,L+1,r),left:m.getCellNodeFromCords(u-1,L,r),right:m.getCellNodeFromCords(u+1,L,r)};let {above:M,below:N}=n;n=q.NO_STATUS;
42
- r=M&&M.getWidth()||N&&N.getWidth()||null;if(M&&M.hasHeaderState(q.COLUMN)||N&&N.hasHeaderState(q.COLUMN))n|=q.COLUMN;n=x(n,1,r);n.append(f.$createParagraphNode());e.append(n)}d?b.insertAfter(e):b.insertBefore(e)}else throw Error("Row before insertion index does not exist.");return a};exports.$isTableCellNode=y;exports.$isTableNode=G;exports.$isTableRowNode=C;
42
+ r=M&&M.getWidth()||N&&N.getWidth()||void 0;if(M&&M.hasHeaderState(q.COLUMN)||N&&N.hasHeaderState(q.COLUMN))n|=q.COLUMN;n=x(n,1,r);n.append(f.$createParagraphNode());e.append(n)}d?b.insertAfter(e):b.insertBefore(e)}else throw Error("Row before insertion index does not exist.");return a};exports.$isTableCellNode=y;exports.$isTableNode=G;exports.$isTableRowNode=C;
43
43
  exports.$removeTableRowAtIndex=function(a,b){let d=a.getChildren();if(b>=d.length||0>b)throw Error("Expected table cell to be inside of table row.");d[b].remove();return a};exports.INSERT_TABLE_COMMAND=Y;exports.TableCellHeaderStates=q;exports.TableCellNode=t;exports.TableNode=T;exports.TableRowNode=z;exports.TableSelection=E;
44
44
  exports.applyTableHandlers=function(a,b,d){let k=d.getRootElement();if(null===k)throw Error("No root element.");let c=new E(d,a.getKey());b.__lexicalTableSelection=c;let h=!1,n=!1;b.addEventListener("dblclick",g=>{let e=I(g.target);null!==e&&(g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),c.setAnchorCellForSelection(e),c.adjustFocusCellForSelection(e,!0),h=!1)});b.addEventListener("mousedown",g=>{setTimeout(()=>{if(0===g.button){var e=I(g.target);null!==e&&(c.setAnchorCellForSelection(e),
45
45
  document.addEventListener("mouseup",()=>{h=!1},{capture:!0,once:!0}))}},0)});b.addEventListener("mousemove",g=>{n&&(g.preventDefault(),g.stopPropagation(),g.stopImmediatePropagation());if(h){let e=I(g.target);if(null!==e){let l=e.x,m=e.y;h&&(c.startX!==l||c.startY!==m||c.isHighlightingCells)&&(g.preventDefault(),h=!0,c.adjustFocusCellForSelection(e))}}});b.addEventListener("mouseup",()=>{h&&(h=!1)});b.addEventListener("mouseleave",()=>{});let r=g=>{h=!0;0===g.button&&d.update(()=>{const e=f.$getSelection();
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import type { DOMConversionMap, DOMConversionOutput, DOMExportOutput, EditorConfig, LexicalEditor, LexicalNode, NodeKey, SerializedGridCellNode, Spread } from 'lexical';
9
+ import { GridCellNode } from 'lexical';
10
+ export declare const TableCellHeaderStates: {
11
+ BOTH: number;
12
+ COLUMN: number;
13
+ NO_STATUS: number;
14
+ ROW: number;
15
+ };
16
+ export declare type TableCellHeaderState = typeof TableCellHeaderStates[keyof typeof TableCellHeaderStates];
17
+ export declare type SerializedTableCellNode = Spread<{
18
+ headerState: TableCellHeaderState;
19
+ type: 'tablecell';
20
+ width?: number;
21
+ }, SerializedGridCellNode>;
22
+ export declare class TableCellNode extends GridCellNode {
23
+ __headerState: TableCellHeaderState;
24
+ __width?: number;
25
+ static getType(): 'tablecell';
26
+ static clone(node: TableCellNode): TableCellNode;
27
+ static importDOM(): DOMConversionMap | null;
28
+ static importJSON(serializedNode: SerializedTableCellNode): TableCellNode;
29
+ constructor(headerState?: number, colSpan?: number, width?: number, key?: NodeKey);
30
+ createDOM(config: EditorConfig): HTMLElement;
31
+ exportDOM(editor: LexicalEditor): DOMExportOutput;
32
+ exportJSON(): SerializedTableCellNode;
33
+ getTag(): string;
34
+ setHeaderStyles(headerState: TableCellHeaderState): TableCellHeaderState;
35
+ getHeaderStyles(): TableCellHeaderState;
36
+ setWidth(width: number): number | null | undefined;
37
+ getWidth(): number | undefined;
38
+ toggleHeaderStyle(headerStateToToggle: TableCellHeaderState): TableCellNode;
39
+ hasHeaderState(headerState: TableCellHeaderState): boolean;
40
+ hasHeader(): boolean;
41
+ updateDOM(prevNode: TableCellNode): boolean;
42
+ collapseAtStart(): true;
43
+ canBeEmpty(): false;
44
+ canIndent(): false;
45
+ }
46
+ export declare function convertTableCellNodeElement(domNode: Node): DOMConversionOutput;
47
+ export declare function $createTableCellNode(headerState: TableCellHeaderState, colSpan?: number, width?: number): TableCellNode;
48
+ export declare function $isTableCellNode(node: LexicalNode | null | undefined): node is TableCellNode;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import type { TableCellNode } from './LexicalTableCellNode';
9
+ import type { Cell, Grid } from './LexicalTableSelection';
10
+ import type { DOMConversionMap, DOMConversionOutput, DOMExportOutput, EditorConfig, LexicalEditor, LexicalNode, NodeKey, SerializedElementNode, Spread } from 'lexical';
11
+ import { GridNode } from 'lexical';
12
+ export declare type SerializedTableNode = Spread<{
13
+ type: 'table';
14
+ version: 1;
15
+ }, SerializedElementNode>;
16
+ export declare class TableNode extends GridNode {
17
+ __grid?: Grid;
18
+ static getType(): 'table';
19
+ static clone(node: TableNode): TableNode;
20
+ static importDOM(): DOMConversionMap | null;
21
+ static importJSON(_serializedNode: SerializedTableNode): TableNode;
22
+ constructor(key?: NodeKey);
23
+ exportJSON(): SerializedElementNode;
24
+ createDOM(config: EditorConfig, editor?: LexicalEditor): HTMLElement;
25
+ updateDOM(): boolean;
26
+ exportDOM(editor: LexicalEditor): DOMExportOutput;
27
+ canExtractContents(): false;
28
+ canBeEmpty(): false;
29
+ getCordsFromCellNode(tableCellNode: TableCellNode, grid: Grid): {
30
+ x: number;
31
+ y: number;
32
+ };
33
+ getCellFromCords(x: number, y: number, grid: Grid): Cell | null;
34
+ getCellFromCordsOrThrow(x: number, y: number, grid: Grid): Cell;
35
+ getCellNodeFromCords(x: number, y: number, grid: Grid): TableCellNode | null;
36
+ getCellNodeFromCordsOrThrow(x: number, y: number, grid: Grid): TableCellNode;
37
+ canSelectBefore(): true;
38
+ canIndent(): false;
39
+ }
40
+ export declare function $getElementGridForTableNode(editor: LexicalEditor, tableNode: TableNode): Grid;
41
+ export declare function convertTableElement(_domNode: Node): DOMConversionOutput;
42
+ export declare function $createTableNode(): TableNode;
43
+ export declare function $isTableNode(node: LexicalNode | null | undefined): node is TableNode;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import type { Spread } from 'lexical';
9
+ import { DOMConversionMap, DOMConversionOutput, EditorConfig, GridRowNode, LexicalNode, NodeKey, SerializedElementNode } from 'lexical';
10
+ export declare type SerializedTableRowNode = Spread<{
11
+ height: number;
12
+ type: 'tablerow';
13
+ version: 1;
14
+ }, SerializedElementNode>;
15
+ export declare class TableRowNode extends GridRowNode {
16
+ __height?: number;
17
+ static getType(): 'tablerow';
18
+ static clone(node: TableRowNode): TableRowNode;
19
+ static importDOM(): DOMConversionMap | null;
20
+ static importJSON(serializedNode: SerializedTableRowNode): TableRowNode;
21
+ constructor(height?: number, key?: NodeKey);
22
+ exportJSON(): SerializedElementNode;
23
+ createDOM(config: EditorConfig): HTMLElement;
24
+ setHeight(height: number): number | null | undefined;
25
+ getHeight(): number | null | undefined;
26
+ updateDOM(prevNode: TableRowNode): boolean;
27
+ canBeEmpty(): false;
28
+ canIndent(): false;
29
+ }
30
+ export declare function convertTableRowElement(domNode: Node): DOMConversionOutput;
31
+ export declare function $createTableRowNode(height?: number): TableRowNode;
32
+ export declare function $isTableRowNode(node: LexicalNode | null | undefined): node is TableRowNode;
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import type { GridSelection, LexicalEditor, NodeKey, TextFormatType } from 'lexical';
9
+ export declare type Cell = {
10
+ elem: HTMLElement;
11
+ highlighted: boolean;
12
+ x: number;
13
+ y: number;
14
+ };
15
+ export declare type Cells = Array<Array<Cell>>;
16
+ export declare type Grid = {
17
+ cells: Cells;
18
+ columns: number;
19
+ rows: number;
20
+ };
21
+ export declare class TableSelection {
22
+ currentX: number;
23
+ currentY: number;
24
+ listenersToRemove: Set<() => void>;
25
+ grid: Grid;
26
+ isHighlightingCells: boolean;
27
+ startX: number;
28
+ startY: number;
29
+ tableNodeKey: NodeKey;
30
+ anchorCell: Cell | null;
31
+ focusCell: Cell | null;
32
+ anchorCellNodeKey: NodeKey | null;
33
+ focusCellNodeKey: NodeKey | null;
34
+ editor: LexicalEditor;
35
+ gridSelection: GridSelection | null;
36
+ hasHijackedSelectionStyles: boolean;
37
+ constructor(editor: LexicalEditor, tableNodeKey: string);
38
+ getGrid(): Grid;
39
+ removeListeners(): void;
40
+ trackTableGrid(): void;
41
+ clearHighlight(): void;
42
+ enableHighlightStyle(): void;
43
+ disableHighlightStyle(): void;
44
+ updateTableGridSelection(selection: GridSelection | null): void;
45
+ adjustFocusCellForSelection(cell: Cell, ignoreStart?: boolean): void;
46
+ setAnchorCellForSelection(cell: Cell): void;
47
+ formatCells(type: TextFormatType): void;
48
+ clearText(): void;
49
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import type { TableNode } from './LexicalTableNode';
9
+ import type { Cell, Grid } from './LexicalTableSelection';
10
+ import type { GridSelection, LexicalEditor, LexicalNode, RangeSelection } from 'lexical';
11
+ import { TableSelection } from './LexicalTableSelection';
12
+ declare const LEXICAL_ELEMENT_KEY = "__lexicalTableSelection";
13
+ export declare function applyTableHandlers(tableNode: TableNode, tableElement: HTMLTableElementWithWithTableSelectionState, editor: LexicalEditor): TableSelection;
14
+ export declare type HTMLTableElementWithWithTableSelectionState = HTMLTableElement & Record<typeof LEXICAL_ELEMENT_KEY, TableSelection>;
15
+ export declare function attachTableSelectionToTableElement(tableElement: HTMLTableElementWithWithTableSelectionState, tableSelection: TableSelection): void;
16
+ export declare function getTableSelectionFromTableElement(tableElement: HTMLTableElementWithWithTableSelectionState): TableSelection | null;
17
+ export declare function getCellFromTarget(node: Node): Cell | null;
18
+ export declare function getTableGrid(tableElement: HTMLElement): Grid;
19
+ export declare function $updateDOMForSelection(grid: Grid, selection: GridSelection | RangeSelection | null): Array<Cell>;
20
+ export declare function $forEachGridCell(grid: Grid, cb: (cell: Cell, lexicalNode: LexicalNode, cords: {
21
+ x: number;
22
+ y: number;
23
+ }) => void): void;
24
+ export declare function $addHighlightStyleToTable(tableSelection: TableSelection): void;
25
+ export declare function $removeHighlightStyleToTable(tableSelection: TableSelection): void;
26
+ export {};
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import type { Grid } from './LexicalTableSelection';
9
+ import type { LexicalNode } from 'lexical';
10
+ import { TableCellNode } from './LexicalTableCellNode';
11
+ import { TableNode } from './LexicalTableNode';
12
+ import { TableRowNode } from './LexicalTableRowNode';
13
+ export declare function $createTableNodeWithDimensions(rowCount: number, columnCount: number, includeHeaders?: boolean): TableNode;
14
+ export declare function $getTableCellNodeFromLexicalNode(startingNode: LexicalNode): TableCellNode | null;
15
+ export declare function $getTableRowNodeFromTableCellNodeOrThrow(startingNode: LexicalNode): TableRowNode;
16
+ export declare function $getTableNodeFromLexicalNodeOrThrow(startingNode: LexicalNode): TableNode;
17
+ export declare function $getTableRowIndexFromTableCellNode(tableCellNode: TableCellNode): number;
18
+ export declare function $getTableColumnIndexFromTableCellNode(tableCellNode: TableCellNode): number;
19
+ export declare type TableCellSiblings = {
20
+ above: TableCellNode | null | undefined;
21
+ below: TableCellNode | null | undefined;
22
+ left: TableCellNode | null | undefined;
23
+ right: TableCellNode | null | undefined;
24
+ };
25
+ export declare function $getTableCellSiblingsFromTableCellNode(tableCellNode: TableCellNode, grid: Grid): TableCellSiblings;
26
+ export declare function $removeTableRowAtIndex(tableNode: TableNode, indexToDelete: number): TableNode;
27
+ export declare function $insertTableRow(tableNode: TableNode, targetIndex: number, shouldInsertAfter: boolean | undefined, rowCount: number, grid: Grid): TableNode;
28
+ export declare function $insertTableColumn(tableNode: TableNode, targetIndex: number, shouldInsertAfter: boolean | undefined, columnCount: number): TableNode;
29
+ export declare function $deleteTableColumn(tableNode: TableNode, targetIndex: number): TableNode;
package/index.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import type { Cell } from './LexicalTableSelection';
9
+ import type { HTMLTableElementWithWithTableSelectionState } from './LexicalTableSelectionHelpers';
10
+ import type { LexicalCommand } from 'lexical';
11
+ import { $createTableCellNode, $isTableCellNode, TableCellHeaderStates, TableCellNode } from './LexicalTableCellNode';
12
+ import { $createTableNode, $getElementGridForTableNode, $isTableNode, TableNode } from './LexicalTableNode';
13
+ import { $createTableRowNode, $isTableRowNode, TableRowNode } from './LexicalTableRowNode';
14
+ import { TableSelection } from './LexicalTableSelection';
15
+ import { applyTableHandlers, getCellFromTarget, getTableSelectionFromTableElement } from './LexicalTableSelectionHelpers';
16
+ import { $createTableNodeWithDimensions, $deleteTableColumn, $getTableCellNodeFromLexicalNode, $getTableColumnIndexFromTableCellNode, $getTableNodeFromLexicalNodeOrThrow, $getTableRowIndexFromTableCellNode, $getTableRowNodeFromTableCellNodeOrThrow, $insertTableColumn, $insertTableRow, $removeTableRowAtIndex } from './LexicalTableUtils';
17
+ export { $createTableCellNode, $createTableNode, $createTableNodeWithDimensions, $createTableRowNode, $deleteTableColumn, $getElementGridForTableNode, $getTableCellNodeFromLexicalNode, $getTableColumnIndexFromTableCellNode, $getTableNodeFromLexicalNodeOrThrow, $getTableRowIndexFromTableCellNode, $getTableRowNodeFromTableCellNodeOrThrow, $insertTableColumn, $insertTableRow, $isTableCellNode, $isTableNode, $isTableRowNode, $removeTableRowAtIndex, applyTableHandlers, Cell, getCellFromTarget, getTableSelectionFromTableElement, HTMLTableElementWithWithTableSelectionState, TableCellHeaderStates, TableCellNode, TableNode, TableRowNode, TableSelection, };
18
+ export declare type InsertTableCommandPayload = Readonly<{
19
+ columns: string;
20
+ rows: string;
21
+ includeHeaders?: boolean;
22
+ }>;
23
+ export declare const INSERT_TABLE_COMMAND: LexicalCommand<InsertTableCommandPayload>;
package/package.json CHANGED
@@ -8,13 +8,13 @@
8
8
  "table"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.3.3",
11
+ "version": "0.3.6",
12
12
  "main": "LexicalTable.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.3.3"
14
+ "lexical": "0.3.6"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/utils": "0.3.3"
17
+ "@lexical/utils": "0.3.6"
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",
package/utils.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import type { Grid } from './LexicalTableSelection';
9
+ import type { LexicalNode } from 'lexical';
10
+ import { TableCellNode } from './LexicalTableCellNode';
11
+ import { TableNode } from './LexicalTableNode';
12
+ import { TableRowNode } from './LexicalTableRowNode';
13
+ export declare function $createTableNodeWithDimensions(rowCount: number, columnCount: number, includeHeaders?: boolean): TableNode;
14
+ export declare function $getTableCellNodeFromLexicalNode(startingNode: LexicalNode): TableCellNode | null;
15
+ export declare function $getTableRowNodeFromTableCellNodeOrThrow(startingNode: LexicalNode): TableRowNode;
16
+ export declare function $getTableNodeFromLexicalNodeOrThrow(startingNode: LexicalNode): TableNode;
17
+ export declare function $getTableRowIndexFromTableCellNode(tableCellNode: TableCellNode): number;
18
+ export declare function $getTableColumnIndexFromTableCellNode(tableCellNode: TableCellNode): number;
19
+ export declare type TableCellSiblings = {
20
+ above: TableCellNode | null | undefined;
21
+ below: TableCellNode | null | undefined;
22
+ left: TableCellNode | null | undefined;
23
+ right: TableCellNode | null | undefined;
24
+ };
25
+ export declare function $getTableCellSiblingsFromTableCellNode(tableCellNode: TableCellNode, grid: Grid): TableCellSiblings;
26
+ export declare function $removeTableRowAtIndex(tableNode: TableNode, indexToDelete: number): TableNode;
27
+ export declare function $insertTableRow(tableNode: TableNode, targetIndex: number, shouldInsertAfter: boolean | undefined, rowCount: number, grid: Grid): TableNode;
28
+ export declare function $insertTableColumn(tableNode: TableNode, targetIndex: number, shouldInsertAfter: boolean | undefined, columnCount: number): TableNode;
29
+ export declare function $deleteTableColumn(tableNode: TableNode, targetIndex: number): TableNode;
package/LexicalTable.d.ts DELETED
@@ -1,229 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
-
9
- import type {
10
- EditorConfig,
11
- LexicalNode,
12
- NodeKey,
13
- ParagraphNode,
14
- RangeSelection,
15
- ElementNode,
16
- LexicalEditor,
17
- TextFormatType,
18
- LexicalCommand,
19
- } from 'lexical';
20
-
21
- export enum TableCellHeaderStates {
22
- NO_STATUS = 0,
23
- ROW = 1,
24
- COLUMN = 2,
25
- BOTH = 3,
26
- }
27
-
28
- /**
29
- * LexicalTableCellNode
30
- */
31
- export declare class TableCellNode extends ElementNode {
32
- static getType(): string;
33
- static clone(node: TableCellNode): TableCellNode;
34
- constructor(
35
- headerState?: TableCellHeaderStates,
36
- colSpan?: number,
37
- width?: number | null | undefined,
38
- key?: NodeKey,
39
- );
40
- __headerState: TableCellHeaderStates;
41
- createDOM(config: EditorConfig): HTMLElement;
42
- updateDOM(prevNode: TableCellNode, dom: HTMLElement): boolean;
43
- insertNewAfter(
44
- selection: RangeSelection,
45
- ): null | ParagraphNode | TableCellNode;
46
- collapseAtStart(): true;
47
- getTag(): string;
48
- setHeaderState(headerState: TableCellHeaderStates): TableCellHeaderStates;
49
- getHeaderState(): TableCellHeaderStates;
50
- toggleHeaderState(headerState: TableCellHeaderStates): TableCellNode;
51
- hasHeader(): boolean;
52
- setWidth(width: number): number | null | undefined;
53
- getWidth(): number | null | undefined;
54
- toggleHeaderStyle(headerState: TableCellHeaderStates): TableCellNode;
55
- updateDOM(prevNode: TableCellNode): boolean;
56
- collapseAtStart(): true;
57
- canBeEmpty(): false;
58
- }
59
- declare function $createTableCellNode(
60
- headerState: TableCellHeaderStates,
61
- colSpan?: number,
62
- width?: number | null | undefined,
63
- ): TableCellNode;
64
- export declare function $isTableCellNode(
65
- node?: LexicalNode,
66
- ): node is TableCellNode;
67
-
68
- /**
69
- * LexicalTableNode
70
- */
71
- export declare class TableNode extends ElementNode {
72
- static getType(): string;
73
- static clone(node: TableNode): TableNode;
74
- constructor(grid?: Grid, key?: NodeKey);
75
- createDOM(config: EditorConfig): HTMLElement;
76
- updateDOM(prevNode: TableNode, dom: HTMLElement): boolean;
77
- insertNewAfter(selection: RangeSelection): null | ParagraphNode | TableNode;
78
- collapseAtStart(): true;
79
- getCordsFromCellNode(tableCellNode: TableCellNode): {x: number; y: number};
80
- getCellFromCords(x: number, y: number, grid: Grid): Cell | null | undefined;
81
- getCellFromCordsOrThrow(x: number, y: number, grid: Grid): Cell;
82
- getCellNodeFromCords(x: number, y: number): TableCellNode | null;
83
- getCellNodeFromCordsOrThrow(x: number, y: number): TableCellNode;
84
- setGrid(grid?: Grid): TableNode;
85
- getGrid(): Grid | null;
86
- canSelectBefore(): true;
87
- }
88
- declare function $createTableNode(): TableNode;
89
- declare function $isTableNode(node?: LexicalNode): node is TableNode;
90
-
91
- /**
92
- * LexicalTableRowNode
93
- */
94
-
95
- declare class TableRowNode extends ElementNode {
96
- static getType(): string;
97
- static clone(node: TableRowNode): TableRowNode;
98
- constructor(key?: NodeKey, height?: number | null | undefined);
99
- createDOM(config: EditorConfig): HTMLElement;
100
- updateDOM(prevNode: TableRowNode, dom: HTMLElement): boolean;
101
- insertNewAfter(
102
- selection: RangeSelection,
103
- ): null | ParagraphNode | TableRowNode;
104
- setHeight(height: number): number | null | undefined;
105
- getHeight(): number | null | undefined;
106
- collapseAtStart(): true;
107
- }
108
- declare function $createTableRowNode(): TableRowNode;
109
- declare function $isTableRowNode(node?: LexicalNode): node is TableRowNode;
110
-
111
- /**
112
- * LexicalTableSelectionHelpers
113
- */
114
- export type Cell = {
115
- elem: HTMLElement;
116
- highlighted: boolean;
117
- x: number;
118
- y: number;
119
- };
120
- export type Cells = Array<Array<Cell>>;
121
- export type Grid = {
122
- cells: Cells;
123
- columns: number;
124
- rows: number;
125
- };
126
-
127
- declare function applyTableHandlers(
128
- tableNode: TableNode,
129
- tableElement: HTMLElement,
130
- editor: LexicalEditor,
131
- ): TableSelection;
132
-
133
- declare function $getElementGridForTableNode(
134
- editor: LexicalEditor,
135
- tableNode: TableNode,
136
- ): Grid;
137
-
138
- declare function getTableSelectionFromTableElement(
139
- tableElement: HTMLElement,
140
- ): TableSelection;
141
-
142
- declare function getCellFromTarget(node: Node): Cell | null;
143
-
144
- /**
145
- * LexicalTableUtils
146
- */
147
-
148
- declare function $createTableNodeWithDimensions(
149
- rowCount: number,
150
- columnCount: number,
151
- includeHeaders?: boolean,
152
- ): TableNode;
153
-
154
- declare function $getTableCellNodeFromLexicalNode(
155
- startingNode: LexicalNode,
156
- ): TableCellNode | null;
157
-
158
- declare function $getTableRowNodeFromTableCellNodeOrThrow(
159
- startingNode: LexicalNode,
160
- ): TableRowNode;
161
-
162
- declare function $getTableNodeFromLexicalNodeOrThrow(
163
- startingNode: LexicalNode,
164
- ): TableNode;
165
-
166
- declare function $getTableRowIndexFromTableCellNode(
167
- tableCellNode: TableCellNode,
168
- ): number;
169
-
170
- declare function $getTableColumnIndexFromTableCellNode(
171
- tableCellNode: TableCellNode,
172
- ): number;
173
-
174
- declare function $removeTableRowAtIndex(
175
- tableNode: TableNode,
176
- indexToDelete: number,
177
- ): TableNode;
178
-
179
- declare function $insertTableRow(
180
- tableNode: TableNode,
181
- targetIndex: number,
182
- shouldInsertAfter: boolean,
183
- rowCount: number,
184
- grid: Grid,
185
- ): TableNode;
186
-
187
- declare function $insertTableColumn(
188
- tableNode: TableNode,
189
- targetIndex: number,
190
- shouldInsertAfter: boolean,
191
- columnCount: number,
192
- ): TableNode;
193
-
194
- declare function $deleteTableColumn(
195
- tableNode: TableNode,
196
- targetIndex: number,
197
- ): TableNode;
198
-
199
- /**
200
- * LexicalTableSelection.js
201
- */
202
- export declare class TableSelection {
203
- currentX: number;
204
- currentY: number;
205
- listenersToRemove: Set<() => void>;
206
- domListeners: Set<() => void>;
207
- grid: Grid;
208
- isHighlightingCells: boolean;
209
- isMouseDown: boolean;
210
- startX: number;
211
- startY: number;
212
- nodeKey: string;
213
- editor: LexicalEditor;
214
- constructor(editor: LexicalEditor, nodeKey: string);
215
- getGrid(): Grid;
216
- removeListeners(): void;
217
- trackTableGrid(): void;
218
- clearHighlight(): void;
219
- adjustFocusCellForSelection(cell: Cell): void;
220
- setAnchorCellForSelection(cell: Cell): void;
221
- formatCells(type: TextFormatType): void;
222
- clearText(): void;
223
- }
224
- export type InsertTableCommandPayload = Readonly<{
225
- columns: string;
226
- rows: string;
227
- includeHeaders?: boolean;
228
- }>;
229
- export const INSERT_TABLE_COMMAND: LexicalCommand<InsertTableCommandPayload>;