@lexical/table 0.1.10 → 0.1.11

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.
@@ -110,7 +110,6 @@ const LowPriority = 1;
110
110
  const CriticalPriority = 4;
111
111
  const removeHighlightStyle = document.createElement('style');
112
112
  removeHighlightStyle.appendChild(document.createTextNode('::selection{background-color: transparent}'));
113
-
114
113
  function getCellFromTarget(node) {
115
114
  let currentNode = node;
116
115
 
@@ -133,7 +132,6 @@ function getCellFromTarget(node) {
133
132
 
134
133
  return null;
135
134
  }
136
-
137
135
  function trackTableGrid(tableNode, tableElement, editor) {
138
136
  const cells = [];
139
137
  const grid = {
@@ -141,95 +139,99 @@ function trackTableGrid(tableNode, tableElement, editor) {
141
139
  columns: 0,
142
140
  rows: 0
143
141
  };
144
- const observer = new MutationObserver(records => {
145
- editor.update(() => {
146
- let currentNode = tableElement.firstChild;
147
- let x = 0;
148
- let y = 0;
149
- let gridNeedsRedraw = false;
150
142
 
151
- for (let i = 0; i < records.length; i++) {
152
- const record = records[i];
153
- const target = record.target;
154
- const nodeName = target.nodeName;
143
+ const calcSize = () => {
144
+ let currentNode = tableElement.firstChild;
145
+ let x = 0;
146
+ let y = 0;
147
+ cells.length = 0;
148
+
149
+ while (currentNode != null) {
150
+ const nodeMame = currentNode.nodeName;
151
+
152
+ if (nodeMame === 'TD' || nodeMame === 'TH') {
153
+ // $FlowFixMe: TD is always an HTMLElement
154
+ const elem = currentNode;
155
+ const cell = {
156
+ elem,
157
+ highlighted: false,
158
+ x,
159
+ y
160
+ }; // $FlowFixMe: internal field
155
161
 
156
- if (nodeName === 'TABLE' || nodeName === 'TR') {
157
- gridNeedsRedraw = true;
158
- break;
159
- }
160
- }
162
+ currentNode._cell = cell;
161
163
 
162
- if (!gridNeedsRedraw) {
163
- return;
164
- }
164
+ if (cells[y] === undefined) {
165
+ cells[y] = [];
166
+ }
165
167
 
166
- cells.length = 0;
168
+ cells[y][x] = cell;
169
+ } else {
170
+ const child = currentNode.firstChild;
167
171
 
168
- while (currentNode != null) {
169
- const nodeMame = currentNode.nodeName;
172
+ if (child != null) {
173
+ currentNode = child;
174
+ continue;
175
+ }
176
+ }
170
177
 
171
- if (nodeMame === 'TD' || nodeMame === 'TH') {
172
- // $FlowFixMe: TD is always an HTMLElement
173
- const elem = currentNode;
174
- const cell = {
175
- elem,
176
- highlighted: false,
177
- x,
178
- y
179
- }; // $FlowFixMe: internal field
178
+ const sibling = currentNode.nextSibling;
180
179
 
181
- currentNode._cell = cell;
180
+ if (sibling != null) {
181
+ x++;
182
+ currentNode = sibling;
183
+ continue;
184
+ }
182
185
 
183
- if (cells[y] === undefined) {
184
- cells[y] = [];
185
- }
186
+ const parent = currentNode.parentNode;
186
187
 
187
- cells[y][x] = cell;
188
- } else {
189
- const child = currentNode.firstChild;
188
+ if (parent != null) {
189
+ const parentSibling = parent.nextSibling;
190
190
 
191
- if (child != null) {
192
- currentNode = child;
193
- continue;
194
- }
191
+ if (parentSibling == null) {
192
+ break;
195
193
  }
196
194
 
197
- const sibling = currentNode.nextSibling;
198
-
199
- if (sibling != null) {
200
- x++;
201
- currentNode = sibling;
202
- continue;
203
- }
195
+ y++;
196
+ x = 0;
197
+ currentNode = parentSibling;
198
+ }
199
+ }
204
200
 
205
- const parent = currentNode.parentNode;
201
+ grid.columns = x + 1;
202
+ grid.rows = y + 1;
203
+ tableNode.setGrid(grid);
204
+ };
206
205
 
207
- if (parent != null) {
208
- const parentSibling = parent.nextSibling;
206
+ const observer = new MutationObserver(records => {
207
+ editor.update(() => {
208
+ let gridNeedsRedraw = false;
209
209
 
210
- if (parentSibling == null) {
211
- break;
212
- }
210
+ for (let i = 0; i < records.length; i++) {
211
+ const record = records[i];
212
+ const target = record.target;
213
+ const nodeName = target.nodeName;
213
214
 
214
- y++;
215
- x = 0;
216
- currentNode = parentSibling;
215
+ if (nodeName === 'TABLE' || nodeName === 'TR') {
216
+ gridNeedsRedraw = true;
217
+ break;
217
218
  }
218
219
  }
219
220
 
220
- grid.columns = x + 1;
221
- grid.rows = y + 1;
222
- tableNode.setGrid(grid);
221
+ if (!gridNeedsRedraw) {
222
+ return;
223
+ }
224
+
225
+ calcSize();
223
226
  });
224
227
  });
225
228
  observer.observe(tableElement, {
226
229
  childList: true,
227
230
  subtree: true
228
231
  });
229
- tableNode.setGrid(grid);
232
+ calcSize();
230
233
  return grid;
231
234
  }
232
-
233
235
  function updateCells(fromX, toX, fromY, toY, cells) {
234
236
  const highlighted = [];
235
237
 
@@ -252,18 +254,21 @@ function updateCells(fromX, toX, fromY, toY, cells) {
252
254
  cell.highlighted = false;
253
255
  elemStyle.removeProperty('background-color');
254
256
  elemStyle.removeProperty('caret-color');
257
+
258
+ if (!cell.elem.getAttribute('style')) {
259
+ cell.elem.removeAttribute('style');
260
+ }
255
261
  }
256
262
  }
257
263
  }
258
264
 
259
265
  return highlighted;
260
266
  }
261
-
262
- function applyCustomTableHandlers(tableNode, tableElement, editor) {
267
+ function $applyCustomTableHandlers(tableNode, tableElement, editor) {
263
268
  const rootElement = editor.getRootElement();
264
269
 
265
270
  if (rootElement === null) {
266
- return;
271
+ throw new Error('No root element.');
267
272
  }
268
273
 
269
274
  trackTableGrid(tableNode, tableElement, editor);
@@ -275,6 +280,8 @@ function applyCustomTableHandlers(tableNode, tableElement, editor) {
275
280
  let currentX = -1;
276
281
  let currentY = -1;
277
282
  let highlightedCells = [];
283
+ const editorListeners = new Set();
284
+ let deleteCharacterListener = null;
278
285
 
279
286
  if (grid == null) {
280
287
  throw new Error('Table grid not found.');
@@ -317,11 +324,15 @@ function applyCustomTableHandlers(tableNode, tableElement, editor) {
317
324
  const cellNode = lexical.$getNearestNodeFromDOMNode(elem);
318
325
 
319
326
  if (lexical.$isElementNode(cellNode)) {
320
- cellNode.clear();
321
327
  const paragraphNode = lexical.$createParagraphNode();
322
328
  const textNode = lexical.$createTextNode();
323
329
  paragraphNode.append(textNode);
324
330
  cellNode.append(paragraphNode);
331
+ cellNode.getChildren().forEach(child => {
332
+ if (child !== paragraphNode) {
333
+ child.remove();
334
+ }
335
+ });
325
336
  }
326
337
  });
327
338
  tableNode.setSelectionState(null);
@@ -337,6 +348,7 @@ function applyCustomTableHandlers(tableNode, tableElement, editor) {
337
348
 
338
349
  return false;
339
350
  }, LowPriority);
351
+ editorListeners.add(deleteCharacterListener);
340
352
  }
341
353
  } else if (cellX === currentX && cellY === currentY) {
342
354
  return;
@@ -379,6 +391,7 @@ function applyCustomTableHandlers(tableNode, tableElement, editor) {
379
391
  if (deleteCharacterListener !== null) {
380
392
  deleteCharacterListener();
381
393
  deleteCharacterListener = null;
394
+ editorListeners.delete(deleteCharacterListener);
382
395
  }
383
396
 
384
397
  const parent = removeHighlightStyle.parentNode;
@@ -409,7 +422,7 @@ function applyCustomTableHandlers(tableNode, tableElement, editor) {
409
422
  highlightedCells.forEach(highlightedCell => {
410
423
  const cellNode = lexical.$getNearestNodeFromDOMNode(highlightedCell.elem);
411
424
 
412
- if (lexical.$isElementNode(cellNode)) {
425
+ if (lexical.$isElementNode(cellNode) && cellNode.getTextContentSize() !== 0) {
413
426
  anchor.set(cellNode.getKey(), 0, 'element');
414
427
  focus.set(cellNode.getKey(), cellNode.getChildrenSize(), 'element');
415
428
  formatSelection.formatText(type);
@@ -421,7 +434,6 @@ function applyCustomTableHandlers(tableNode, tableElement, editor) {
421
434
  lexical.$setSelection(selection);
422
435
  };
423
436
 
424
- let deleteCharacterListener = null;
425
437
  tableElement.addEventListener('mousedown', event => {
426
438
  if (isSelected) {
427
439
  if (isHighlightingCells) {
@@ -453,7 +465,7 @@ function applyCustomTableHandlers(tableNode, tableElement, editor) {
453
465
  }, 0);
454
466
  });
455
467
  window.addEventListener('click', e => {
456
- if (highlightedCells.length > 0 && !tableElement.contains(e.target)) {
468
+ if (highlightedCells.length > 0 && !tableElement.contains(e.target) && rootElement.contains(e.target)) {
457
469
  editor.update(() => {
458
470
  tableNode.setSelectionState(null);
459
471
  });
@@ -467,11 +479,11 @@ function applyCustomTableHandlers(tableNode, tableElement, editor) {
467
479
  {
468
480
  const isForward = direction === 'forward';
469
481
 
470
- if (y !== (isForward ? grid.columns - 1 : 0)) {
471
- tableNode.getCellNodeFromCords(x, y + (isForward ? 1 : -1)).select();
482
+ if (x !== (isForward ? grid.columns - 1 : 0)) {
483
+ tableNode.getCellNodeFromCords(x + (isForward ? 1 : -1), y).select();
472
484
  } else {
473
- if (x !== (isForward ? grid.rows - 1 : 0)) {
474
- tableNode.getCellNodeFromCords(x + (isForward ? 1 : -1), isForward ? 0 : grid.columns - 1).select();
485
+ if (y !== (isForward ? grid.rows - 1 : 0)) {
486
+ tableNode.getCellNodeFromCords(isForward ? 0 : grid.columns - 1, y + (isForward ? 1 : -1)).select();
475
487
  } else if (!isForward) {
476
488
  tableNode.selectPrevious();
477
489
  } else {
@@ -484,8 +496,8 @@ function applyCustomTableHandlers(tableNode, tableElement, editor) {
484
496
 
485
497
  case 'up':
486
498
  {
487
- if (x !== 0) {
488
- tableNode.getCellNodeFromCords(x - 1, y).select();
499
+ if (y !== 0) {
500
+ tableNode.getCellNodeFromCords(x, y - 1).select();
489
501
  } else {
490
502
  tableNode.selectPrevious();
491
503
  }
@@ -495,8 +507,8 @@ function applyCustomTableHandlers(tableNode, tableElement, editor) {
495
507
 
496
508
  case 'down':
497
509
  {
498
- if (x !== grid.rows - 1) {
499
- tableNode.getCellNodeFromCords(x + 1, y).select();
510
+ if (y !== grid.rows - 1) {
511
+ tableNode.getCellNodeFromCords(x, y + 1).select();
500
512
  } else {
501
513
  tableNode.selectNext();
502
514
  }
@@ -508,7 +520,7 @@ function applyCustomTableHandlers(tableNode, tableElement, editor) {
508
520
  return false;
509
521
  };
510
522
 
511
- editor.addListener('command', (type, payload) => {
523
+ const genericCommandListener = editor.addListener('command', (type, payload) => {
512
524
  const selection = lexical.$getSelection();
513
525
 
514
526
  if (!lexical.$isRangeSelection(selection)) {
@@ -522,15 +534,18 @@ function applyCustomTableHandlers(tableNode, tableElement, editor) {
522
534
  }
523
535
 
524
536
  if (type === 'deleteCharacter') {
525
- if (highlightedCells.length === 0 && selection.isCollapsed() && selection.anchor.offset === 0) {
537
+ if (highlightedCells.length === 0 && selection.isCollapsed() && selection.anchor.offset === 0 && selection.anchor.getNode().getPreviousSiblings().length === 0) {
526
538
  return true;
527
539
  }
528
540
  }
529
541
 
530
- if (type === 'indentContent' || type === 'outdentContent') {
542
+ if (type === 'keyTab') {
543
+ const event = payload;
544
+
531
545
  if (selection.isCollapsed() && highlightedCells.length === 0) {
532
546
  const currentCords = tableNode.getCordsFromCellNode(tableCellNode);
533
- selectGridNodeInDirection(currentCords.x, currentCords.y, type === 'indentContent' ? 'forward' : 'backward');
547
+ event.preventDefault();
548
+ selectGridNodeInDirection(currentCords.x, currentCords.y, !event.shiftKey && type === 'keyTab' ? 'forward' : 'backward');
534
549
  return true;
535
550
  }
536
551
  }
@@ -553,8 +568,18 @@ function applyCustomTableHandlers(tableNode, tableElement, editor) {
553
568
 
554
569
  return false;
555
570
  }, CriticalPriority);
571
+ editorListeners.add(genericCommandListener);
572
+ return () => Array.from(editorListeners).forEach(removeListener => removeListener ? removeListener() : null);
556
573
  }
557
574
 
575
+ /**
576
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
577
+ *
578
+ * This source code is licensed under the MIT license found in the
579
+ * LICENSE file in the root directory of this source tree.
580
+ *
581
+ *
582
+ */
558
583
  class TableNode extends lexical.GridNode {
559
584
  static getType() {
560
585
  return 'table';
@@ -575,7 +600,6 @@ class TableNode extends lexical.GridNode {
575
600
  createDOM(config, editor) {
576
601
  const element = document.createElement('table');
577
602
  addClassNamesToElement(element, config.theme.table);
578
- applyCustomTableHandlers(this, element, editor);
579
603
  return element;
580
604
  }
581
605
 
@@ -594,44 +618,47 @@ class TableNode extends lexical.GridNode {
594
618
  setSelectionState(selectionShape) {
595
619
  const self = this.getWritable();
596
620
  self.__selectionShape = selectionShape;
597
- if (this.__grid == null) return [];
621
+ const grid = this.getGrid();
622
+ if (grid == null) return [];
598
623
 
599
624
  if (!selectionShape) {
600
- return updateCells(-1, -1, -1, -1, this.__grid.cells);
625
+ return updateCells(-1, -1, -1, -1, grid.cells);
601
626
  }
602
627
 
603
- return updateCells(selectionShape.fromX, selectionShape.toX, selectionShape.fromY, selectionShape.toY, this.__grid.cells);
628
+ return updateCells(selectionShape.fromX, selectionShape.toX, selectionShape.fromY, selectionShape.toY, grid.cells);
604
629
  }
605
630
 
606
631
  getSelectionState() {
607
- return this.__selectionShape;
632
+ return this.getLatest().__selectionShape;
608
633
  }
609
634
 
610
635
  getCordsFromCellNode(tableCellNode) {
611
- if (!this.__grid) {
636
+ const grid = this.getGrid();
637
+
638
+ if (!grid) {
612
639
  throw Error(`Grid not found.`);
613
640
  }
614
641
 
615
642
  const {
616
643
  rows,
617
644
  cells
618
- } = this.__grid;
645
+ } = grid;
619
646
 
620
- for (let x = 0; x < rows; x++) {
621
- const row = cells[x];
647
+ for (let y = 0; y < rows; y++) {
648
+ const row = cells[y];
622
649
 
623
650
  if (row == null) {
624
- throw new Error(`Row not found at x:${x}`);
651
+ throw new Error(`Row not found at y:${y}`);
625
652
  }
626
653
 
627
- const y = row.findIndex(({
654
+ const x = row.findIndex(({
628
655
  elem
629
656
  }) => {
630
657
  const cellNode = lexical.$getNearestNodeFromDOMNode(elem);
631
658
  return cellNode === tableCellNode;
632
659
  });
633
660
 
634
- if (y !== -1) {
661
+ if (x !== -1) {
635
662
  return {
636
663
  x,
637
664
  y
@@ -643,23 +670,25 @@ class TableNode extends lexical.GridNode {
643
670
  }
644
671
 
645
672
  getCellNodeFromCords(x, y) {
646
- if (!this.__grid) {
673
+ const grid = this.getGrid();
674
+
675
+ if (!grid) {
647
676
  throw Error(`Grid not found.`);
648
677
  }
649
678
 
650
679
  const {
651
680
  cells
652
- } = this.__grid;
653
- const row = cells[x];
681
+ } = grid;
682
+ const row = cells[y];
654
683
 
655
684
  if (row == null) {
656
- throw new Error(`Table row x:"${x}" not found.`);
685
+ throw new Error(`Table row x:"${y}" not found.`);
657
686
  }
658
687
 
659
- const cell = row[y];
688
+ const cell = row[x];
660
689
 
661
690
  if (cell == null) {
662
- throw new Error(`Table cell y:"${y}" in row x:"${x}" not found.`);
691
+ throw new Error(`Table cell y:"${x}" in row x:"${y}" not found.`);
663
692
  }
664
693
 
665
694
  const node = lexical.$getNearestNodeFromDOMNode(cell.elem);
@@ -674,10 +703,11 @@ class TableNode extends lexical.GridNode {
674
703
  setGrid(grid) {
675
704
  const self = this.getWritable();
676
705
  self.__grid = grid;
706
+ return self;
677
707
  }
678
708
 
679
709
  getGrid() {
680
- return this.__grid;
710
+ return this.getLatest().__grid;
681
711
  }
682
712
 
683
713
  canSelectBefore() {
@@ -825,7 +855,7 @@ function $insertTableRow(tableNode, targetIndex, shouldInsertAfter = true, rowCo
825
855
 
826
856
  for (let j = 0; j < tableColumnCount; j++) {
827
857
  const tableCellNode = $createTableCellNode(false);
828
- tableCellNode.append(lexical.$createTextNode());
858
+ tableCellNode.append(lexical.$createParagraphNode());
829
859
  newTableRowNode.append(tableCellNode);
830
860
  }
831
861
 
@@ -850,7 +880,7 @@ function $insertTableColumn(tableNode, targetIndex, shouldInsertAfter = true, co
850
880
  if ($isTableRowNode(currentTableRowNode)) {
851
881
  for (let j = 0; j < columnCount; j++) {
852
882
  const newTableCell = $createTableCellNode(i === 0);
853
- newTableCell.append(lexical.$createTextNode());
883
+ newTableCell.append(lexical.$createParagraphNode());
854
884
  const tableRowChildren = currentTableRowNode.getChildren();
855
885
 
856
886
  if (targetIndex >= tableRowChildren.length || targetIndex < 0) {
@@ -890,6 +920,7 @@ function $deleteTableColumn(tableNode, targetIndex) {
890
920
  return tableNode;
891
921
  }
892
922
 
923
+ exports.$applyCustomTableHandlers = $applyCustomTableHandlers;
893
924
  exports.$createTableCellNode = $createTableCellNode;
894
925
  exports.$createTableNode = $createTableNode;
895
926
  exports.$createTableNodeWithDimensions = $createTableNodeWithDimensions;
@@ -4,26 +4,27 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- var p=require("lexical");function x(a,...b){b.forEach(c=>{null!=c&&"string"===typeof c&&a.classList.add(...c.split(" "))})}
8
- class y extends p.GridCellNode{static getType(){return"tablecell"}static clone(a){return new y(!1,a.__colSpan,a.__key)}constructor(a=!1,b=1,c){super(b,c);this.__isHeader=a}getTag(){return this.__isHeader?"th":"td"}createDOM(a){const b=document.createElement(this.getTag());x(b,a.theme.tableCell,!0===this.__isHeader&&a.theme.tableCellHeader);return b}updateDOM(){return!1}collapseAtStart(){return!0}canBeEmpty(){return!1}}function z(a){return new y(a)}function B(a){return a instanceof y}
9
- function C(a){throw Error(`Minified Lexical error #${a}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}function D(a,b){for(;a!==p.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null}const F=document.createElement("style");F.appendChild(document.createTextNode("::selection{background-color: transparent}"));
10
- function G(a){for(;null!=a;){const b=a.nodeName;if("TD"===b||"TH"===b){a=a._cell;if(void 0===a)break;return a}a=a.parentNode}return null}
11
- function H(a,b,c){const g=[],d={cells:g,columns:0,rows:0};(new MutationObserver(n=>{c.update(()=>{var e=b.firstChild;let q=0,m=0;var h=!1;for(let r=0;r<n.length;r++){const w=n[r].target.nodeName;if("TABLE"===w||"TR"===w){h=!0;break}}if(h){for(g.length=0;null!=e;){h=e.nodeName;if("TD"===h||"TH"===h)h={elem:e,highlighted:!1,x:q,y:m},e._cell=h,void 0===g[m]&&(g[m]=[]),g[m][q]=h;else if(h=e.firstChild,null!=h){e=h;continue}h=e.nextSibling;if(null!=h)q++,e=h;else if(h=e.parentNode,null!=h){e=h.nextSibling;
12
- if(null==e)break;m++;q=0}}d.columns=q+1;d.rows=m+1;a.setGrid(d)}})})).observe(b,{childList:!0,subtree:!0});a.setGrid(d);return d}
13
- function I(a,b,c,g,d){const n=[];for(let e=0;e<d.length;e++){const q=d[e];for(let m=0;m<q.length;m++){const h=q[m],r=h.elem.style;m>=a&&m<=b&&e>=c&&e<=g?(h.highlighted||(h.highlighted=!0,r.setProperty("background-color","rgb(163, 187, 255)"),r.setProperty("caret-color","transparent")),n.push(h)):h.highlighted&&(h.highlighted=!1,r.removeProperty("background-color"),r.removeProperty("caret-color"))}}return n}
14
- function J(a,b,c){if(null!==c.getRootElement()){H(a,b,c);var g=a.getGrid(),d=!1,n=!1,e=-1,q=-1,m=-1,h=-1,r=[];if(null==g)throw Error("Table grid not found.");b.addEventListener("mousemove",k=>{if(d){var f=G(k.target);if(null!==f){const l=f.x;f=f.y;if(!n&&(e!==l||q!==f))k.preventDefault(),k=window.getSelection(),k.setBaseAndExtent(k.anchorNode,0,k.anchorNode,0),n=!0,document.body&&document.body.appendChild(F),null===A&&(A=c.addListener("command",(u,v)=>{if("deleteCharacter"===u){if(r.length===g.columns*
15
- g.rows)return a.selectPrevious(),a.remove(),w(),!0;r.forEach(({elem:t})=>{t=p.$getNearestNodeFromDOMNode(t);if(p.$isElementNode(t)){t.clear();const E=p.$createParagraphNode(),T=p.$createTextNode();E.append(T);t.append(E)}});a.setSelectionState(null);p.$setSelection(null);return!0}if("formatText"===u)return U(v),!0;"insertText"===u&&w();return!1},1));else if(l===m&&f===h)return;m=l;h=f;if(n){const u=Math.min(e,m),v=Math.max(e,m),t=Math.min(q,h),E=Math.max(q,h);c.update(()=>{r=a.setSelectionState({fromX:u,
16
- fromY:t,toX:v,toY:E})})}}}});var w=()=>{c.update(()=>{d=n=!1;h=m=q=e=-1;c.update(()=>{a.setSelectionState(null)});r=[];null!==A&&(A(),A=null);const k=F.parentNode;null!=k&&k.removeChild(F)})};b.addEventListener("mouseleave",()=>{});var U=k=>{let f=p.$getSelection();p.$isRangeSelection(f)||(f=p.$createRangeSelection());const l=f,u=l.anchor,v=l.focus;r.forEach(t=>{t=p.$getNearestNodeFromDOMNode(t.elem);p.$isElementNode(t)&&(u.set(t.getKey(),0,"element"),v.set(t.getKey(),t.getChildrenSize(),"element"),
17
- l.formatText(k))});f.anchor.set(f.anchor.key,f.anchor.offset,f.anchor.type);f.focus.set(f.anchor.key,f.anchor.offset,f.anchor.type);p.$setSelection(f)},A=null;b.addEventListener("mousedown",k=>{d?n&&w():setTimeout(()=>{n&&w();const f=G(k.target);null!==f&&(d=!0,e=f.x,q=f.y,document.addEventListener("mouseup",()=>{d=!1},{capture:!0,once:!0}))},0)});window.addEventListener("click",k=>{0<r.length&&!b.contains(k.target)&&c.update(()=>{a.setSelectionState(null)})});var O=(k,f,l)=>{switch(l){case "backward":case "forward":return l=
18
- "forward"===l,f!==(l?g.columns-1:0)?a.getCellNodeFromCords(k,f+(l?1:-1)).select():k!==(l?g.rows-1:0)?a.getCellNodeFromCords(k+(l?1:-1),l?0:g.columns-1).select():l?a.selectNext():a.selectPrevious(),!0;case "up":return 0!==k?a.getCellNodeFromCords(k-1,f).select():a.selectPrevious(),!0;case "down":return k!==g.rows-1?a.getCellNodeFromCords(k+1,f).select():a.selectNext(),!0}return!1};c.addListener("command",(k,f)=>{var l=p.$getSelection();if(!p.$isRangeSelection(l))return!1;const u=D(l.anchor.getNode(),
19
- v=>B(v));if(!B(u))return!1;if("deleteCharacter"===k&&0===r.length&&l.isCollapsed()&&0===l.anchor.offset)return!0;if(("indentContent"===k||"outdentContent"===k)&&l.isCollapsed()&&0===r.length)return f=a.getCordsFromCellNode(u),O(f.x,f.y,"indentContent"===k?"forward":"backward"),!0;if(("keyArrowDown"===k||"keyArrowUp"===k)&&l.isCollapsed()&&0===r.length){const v=a.getCordsFromCellNode(u);l=D(l.anchor.getNode(),t=>p.$isElementNode(t));if("keyArrowUp"===k&&l===u.getFirstChild()||"keyArrowDown"===k&&l===
20
- u.getLastChild())return f.preventDefault(),f.stopImmediatePropagation(),O(v.x,v.y,"keyArrowUp"===k?"up":"down"),!0}return!1},4)}}
21
- class K extends p.GridNode{static getType(){return"table"}static clone(a){return new K(a.__key,a.__selectionShape,a.__grid)}constructor(a,b,c){super(a);this.__selectionShape=b;this.__grid=c}createDOM(a,b){const c=document.createElement("table");x(c,a.theme.table);J(this,c,b);return c}updateDOM(){return!1}canExtractContents(){return!1}canBeEmpty(){return!1}setSelectionState(a){this.getWritable().__selectionShape=a;return null==this.__grid?[]:a?I(a.fromX,a.toX,a.fromY,a.toY,this.__grid.cells):I(-1,
22
- -1,-1,-1,this.__grid.cells)}getSelectionState(){return this.__selectionShape}getCordsFromCellNode(a){this.__grid||C(55);const {rows:b,cells:c}=this.__grid;for(let d=0;d<b;d++){var g=c[d];if(null==g)throw Error(`Row not found at x:${d}`);g=g.findIndex(({elem:n})=>p.$getNearestNodeFromDOMNode(n)===a);if(-1!==g)return{x:d,y:g}}throw Error("Cell not found in table.");}getCellNodeFromCords(a,b){this.__grid||C(55);var {cells:c}=this.__grid;c=c[a];if(null==c)throw Error(`Table row x:"${a}" not found.`);
23
- c=c[b];if(null==c)throw Error(`Table cell y:"${b}" in row x:"${a}" not found.`);a=p.$getNearestNodeFromDOMNode(c.elem);if(B(a))return a;throw Error("Node at cords not TableCellNode.");}setGrid(a){this.getWritable().__grid=a}getGrid(){return this.__grid}canSelectBefore(){return!0}}function L(){return new K}function M(a){return a instanceof K}
24
- class N extends p.GridRowNode{static getType(){return"tablerow"}static clone(a){return new N(a.__key)}constructor(a){super(a)}createDOM(a){const b=document.createElement("tr");x(b,a.theme.tableRow);return b}updateDOM(){return!1}canBeEmpty(){return!1}}function P(){return new N}function Q(a){return a instanceof N}function R(a){a=D(a,b=>Q(b));if(Q(a))return a;throw Error("Expected table cell to be inside of table row.");}
25
- function S(a){a=D(a,b=>M(b));if(M(a))return a;throw Error("Expected table cell to be inside of table.");}exports.$createTableCellNode=z;exports.$createTableNode=L;exports.$createTableNodeWithDimensions=function(a,b,c=!0){const g=L();for(let d=0;d<a;d++){const n=P();for(let e=0;e<b;e++){const q=z(0===d&&c),m=p.$createParagraphNode();m.append(p.$createTextNode());q.append(m);n.append(q)}g.append(n)}return g};exports.$createTableRowNode=P;
26
- exports.$deleteTableColumn=function(a,b){const c=a.getChildren();for(let d=0;d<c.length;d++){var g=c[d];if(Q(g)){g=g.getChildren();if(b>=g.length||0>b)throw Error("Table column target index out of range");g[b].remove()}}return a};exports.$getTableCellNodeFromLexicalNode=function(a){a=D(a,b=>B(b));return B(a)?a:null};exports.$getTableColumnIndexFromTableCellNode=function(a){return R(a).getChildren().findIndex(b=>b.is(a))};exports.$getTableNodeFromLexicalNodeOrThrow=S;
27
- exports.$getTableRowIndexFromTableCellNode=function(a){const b=R(a);return S(b).getChildren().findIndex(c=>c.is(b))};exports.$getTableRowNodeFromTableCellNodeOrThrow=R;exports.$insertTableColumn=function(a,b,c=!0,g){const d=a.getChildren();for(let e=0;e<d.length;e++){const q=d[e];if(Q(q))for(let m=0;m<g;m++){const h=z(0===e);h.append(p.$createTextNode());var n=q.getChildren();if(b>=n.length||0>b)throw Error("Table column target index out of range");n=n[b];c?n.insertAfter(h):n.insertBefore(h)}}return a};
28
- exports.$insertTableRow=function(a,b,c=!0,g){var d=a.getChildren();if(b>=d.length||0>b)throw Error("Table row target index out of range");b=d[b];if(Q(b))for(d=0;d<g;d++){const n=b.getChildren().length,e=P();for(let q=0;q<n;q++){const m=z(!1);m.append(p.$createTextNode());e.append(m)}c?b.insertAfter(e):b.insertBefore(e)}else throw Error("Row before insertion index does not exist.");return a};exports.$isTableCellNode=B;exports.$isTableNode=M;exports.$isTableRowNode=Q;
29
- exports.$removeTableRowAtIndex=function(a,b){const c=a.getChildren();if(b>=c.length||0>b)throw Error("Expected table cell to be inside of table row.");c[b].remove();return a};exports.TableCellNode=y;exports.TableNode=K;exports.TableRowNode=N;
7
+ var p=require("lexical");function y(a,...b){b.forEach(c=>{null!=c&&"string"===typeof c&&a.classList.add(...c.split(" "))})}
8
+ class z extends p.GridCellNode{static getType(){return"tablecell"}static clone(a){return new z(!1,a.__colSpan,a.__key)}constructor(a=!1,b=1,c){super(b,c);this.__isHeader=a}getTag(){return this.__isHeader?"th":"td"}createDOM(a){const b=document.createElement(this.getTag());y(b,a.theme.tableCell,!0===this.__isHeader&&a.theme.tableCellHeader);return b}updateDOM(){return!1}collapseAtStart(){return!0}canBeEmpty(){return!1}}function A(a){return new z(a)}function C(a){return a instanceof z}
9
+ function D(a,b){for(;a!==p.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null}const E=document.createElement("style");E.appendChild(document.createTextNode("::selection{background-color: transparent}"));function F(a){for(;null!=a;){const b=a.nodeName;if("TD"===b||"TH"===b){a=a._cell;if(void 0===a)break;return a}a=a.parentNode}return null}
10
+ function G(a,b,c){const n=[],e={cells:n,columns:0,rows:0},q=()=>{var d=b.firstChild;let m=0,k=0;for(n.length=0;null!=d;){var g=d.nodeName;if("TD"===g||"TH"===g)g={elem:d,highlighted:!1,x:m,y:k},d._cell=g,void 0===n[k]&&(n[k]=[]),n[k][m]=g;else if(g=d.firstChild,null!=g){d=g;continue}g=d.nextSibling;if(null!=g)m++,d=g;else if(g=d.parentNode,null!=g){d=g.nextSibling;if(null==d)break;k++;m=0}}e.columns=m+1;e.rows=k+1;a.setGrid(e)};(new MutationObserver(d=>{c.update(()=>{let m=!1;for(let k=0;k<d.length;k++){const g=
11
+ d[k].target.nodeName;if("TABLE"===g||"TR"===g){m=!0;break}}m&&q()})})).observe(b,{childList:!0,subtree:!0});q();return e}
12
+ function J(a,b,c,n,e){const q=[];for(let d=0;d<e.length;d++){const m=e[d];for(let k=0;k<m.length;k++){const g=m[k],u=g.elem.style;k>=a&&k<=b&&d>=c&&d<=n?(g.highlighted||(g.highlighted=!0,u.setProperty("background-color","rgb(163, 187, 255)"),u.setProperty("caret-color","transparent")),q.push(g)):g.highlighted&&(g.highlighted=!1,u.removeProperty("background-color"),u.removeProperty("caret-color"),g.elem.getAttribute("style")||g.elem.removeAttribute("style"))}}return q}
13
+ function K(a){throw Error(`Minified Lexical error #${a}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
14
+ class L extends p.GridNode{static getType(){return"table"}static clone(a){return new L(a.__key,a.__selectionShape,a.__grid)}constructor(a,b,c){super(a);this.__selectionShape=b;this.__grid=c}createDOM(a){const b=document.createElement("table");y(b,a.theme.table);return b}updateDOM(){return!1}canExtractContents(){return!1}canBeEmpty(){return!1}setSelectionState(a){this.getWritable().__selectionShape=a;const b=this.getGrid();return null==b?[]:a?J(a.fromX,a.toX,a.fromY,a.toY,b.cells):J(-1,-1,-1,-1,b.cells)}getSelectionState(){return this.getLatest().__selectionShape}getCordsFromCellNode(a){var b=
15
+ this.getGrid();b||K(55);const {rows:c,cells:n}=b;for(b=0;b<c;b++){var e=n[b];if(null==e)throw Error(`Row not found at y:${b}`);e=e.findIndex(({elem:q})=>p.$getNearestNodeFromDOMNode(q)===a);if(-1!==e)return{x:e,y:b}}throw Error("Cell not found in table.");}getCellNodeFromCords(a,b){var c=this.getGrid();c||K(55);({cells:c}=c);c=c[b];if(null==c)throw Error(`Table row x:"${b}" not found.`);c=c[a];if(null==c)throw Error(`Table cell y:"${a}" in row x:"${b}" not found.`);a=p.$getNearestNodeFromDOMNode(c.elem);
16
+ if(C(a))return a;throw Error("Node at cords not TableCellNode.");}setGrid(a){const b=this.getWritable();b.__grid=a;return b}getGrid(){return this.getLatest().__grid}canSelectBefore(){return!0}}function M(){return new L}function N(a){return a instanceof L}
17
+ class O extends p.GridRowNode{static getType(){return"tablerow"}static clone(a){return new O(a.__key)}constructor(a){super(a)}createDOM(a){const b=document.createElement("tr");y(b,a.theme.tableRow);return b}updateDOM(){return!1}canBeEmpty(){return!1}}function P(){return new O}function Q(a){return a instanceof O}function R(a){a=D(a,b=>Q(b));if(Q(a))return a;throw Error("Expected table cell to be inside of table row.");}
18
+ function U(a){a=D(a,b=>N(b));if(N(a))return a;throw Error("Expected table cell to be inside of table.");}
19
+ exports.$applyCustomTableHandlers=function(a,b,c){const n=c.getRootElement();if(null===n)throw Error("No root element.");G(a,b,c);const e=a.getGrid();let q=!1,d=!1,m=-1,k=-1,g=-1,u=-1,w=[];const H=new Set;let x=null;if(null==e)throw Error("Table grid not found.");b.addEventListener("mousemove",h=>{if(q){var f=F(h.target);if(null!==f){const l=f.x;f=f.y;if(!d&&(m!==l||k!==f))h.preventDefault(),h=window.getSelection(),h.setBaseAndExtent(h.anchorNode,0,h.anchorNode,0),d=!0,document.body&&document.body.appendChild(E),
20
+ null===x&&(x=c.addListener("command",(t,v)=>{if("deleteCharacter"===t){if(w.length===e.columns*e.rows)return a.selectPrevious(),a.remove(),I(),!0;w.forEach(({elem:r})=>{r=p.$getNearestNodeFromDOMNode(r);if(p.$isElementNode(r)){const B=p.$createParagraphNode(),V=p.$createTextNode();B.append(V);r.append(B);r.getChildren().forEach(S=>{S!==B&&S.remove()})}});a.setSelectionState(null);p.$setSelection(null);return!0}if("formatText"===t)return W(v),!0;"insertText"===t&&I();return!1},1),H.add(x));else if(l===
21
+ g&&f===u)return;g=l;u=f;if(d){const t=Math.min(m,g),v=Math.max(m,g),r=Math.min(k,u),B=Math.max(k,u);c.update(()=>{w=a.setSelectionState({fromX:t,fromY:r,toX:v,toY:B})})}}}});const I=()=>{c.update(()=>{q=d=!1;u=g=k=m=-1;c.update(()=>{a.setSelectionState(null)});w=[];null!==x&&(x(),x=null,H.delete(x));const h=E.parentNode;null!=h&&h.removeChild(E)})};b.addEventListener("mouseleave",()=>{});const W=h=>{let f=p.$getSelection();p.$isRangeSelection(f)||(f=p.$createRangeSelection());const l=f,t=l.anchor,
22
+ v=l.focus;w.forEach(r=>{r=p.$getNearestNodeFromDOMNode(r.elem);p.$isElementNode(r)&&0!==r.getTextContentSize()&&(t.set(r.getKey(),0,"element"),v.set(r.getKey(),r.getChildrenSize(),"element"),l.formatText(h))});f.anchor.set(f.anchor.key,f.anchor.offset,f.anchor.type);f.focus.set(f.anchor.key,f.anchor.offset,f.anchor.type);p.$setSelection(f)};b.addEventListener("mousedown",h=>{q?d&&I():setTimeout(()=>{d&&I();const f=F(h.target);null!==f&&(q=!0,m=f.x,k=f.y,document.addEventListener("mouseup",()=>{q=
23
+ !1},{capture:!0,once:!0}))},0)});window.addEventListener("click",h=>{0<w.length&&!b.contains(h.target)&&n.contains(h.target)&&c.update(()=>{a.setSelectionState(null)})});const T=(h,f,l)=>{switch(l){case "backward":case "forward":return l="forward"===l,h!==(l?e.columns-1:0)?a.getCellNodeFromCords(h+(l?1:-1),f).select():f!==(l?e.rows-1:0)?a.getCellNodeFromCords(l?0:e.columns-1,f+(l?1:-1)).select():l?a.selectNext():a.selectPrevious(),!0;case "up":return 0!==f?a.getCellNodeFromCords(h,f-1).select():a.selectPrevious(),
24
+ !0;case "down":return f!==e.rows-1?a.getCellNodeFromCords(h,f+1).select():a.selectNext(),!0}return!1},X=c.addListener("command",(h,f)=>{var l=p.$getSelection();if(!p.$isRangeSelection(l))return!1;var t=D(l.anchor.getNode(),v=>C(v));if(!C(t))return!1;if("deleteCharacter"===h&&0===w.length&&l.isCollapsed()&&0===l.anchor.offset&&0===l.anchor.getNode().getPreviousSiblings().length)return!0;if("keyTab"===h&&l.isCollapsed()&&0===w.length)return t=a.getCordsFromCellNode(t),f.preventDefault(),T(t.x,t.y,f.shiftKey||
25
+ "keyTab"!==h?"backward":"forward"),!0;if(("keyArrowDown"===h||"keyArrowUp"===h)&&l.isCollapsed()&&0===w.length){const v=a.getCordsFromCellNode(t);l=D(l.anchor.getNode(),r=>p.$isElementNode(r));if("keyArrowUp"===h&&l===t.getFirstChild()||"keyArrowDown"===h&&l===t.getLastChild())return f.preventDefault(),f.stopImmediatePropagation(),T(v.x,v.y,"keyArrowUp"===h?"up":"down"),!0}return!1},4);H.add(X);return()=>Array.from(H).forEach(h=>h?h():null)};exports.$createTableCellNode=A;
26
+ exports.$createTableNode=M;exports.$createTableNodeWithDimensions=function(a,b,c=!0){const n=M();for(let e=0;e<a;e++){const q=P();for(let d=0;d<b;d++){const m=A(0===e&&c),k=p.$createParagraphNode();k.append(p.$createTextNode());m.append(k);q.append(m)}n.append(q)}return n};exports.$createTableRowNode=P;
27
+ exports.$deleteTableColumn=function(a,b){const c=a.getChildren();for(let e=0;e<c.length;e++){var n=c[e];if(Q(n)){n=n.getChildren();if(b>=n.length||0>b)throw Error("Table column target index out of range");n[b].remove()}}return a};exports.$getTableCellNodeFromLexicalNode=function(a){a=D(a,b=>C(b));return C(a)?a:null};exports.$getTableColumnIndexFromTableCellNode=function(a){return R(a).getChildren().findIndex(b=>b.is(a))};exports.$getTableNodeFromLexicalNodeOrThrow=U;
28
+ exports.$getTableRowIndexFromTableCellNode=function(a){const b=R(a);return U(b).getChildren().findIndex(c=>c.is(b))};exports.$getTableRowNodeFromTableCellNodeOrThrow=R;exports.$insertTableColumn=function(a,b,c=!0,n){const e=a.getChildren();for(let d=0;d<e.length;d++){const m=e[d];if(Q(m))for(let k=0;k<n;k++){const g=A(0===d);g.append(p.$createParagraphNode());var q=m.getChildren();if(b>=q.length||0>b)throw Error("Table column target index out of range");q=q[b];c?q.insertAfter(g):q.insertBefore(g)}}return a};
29
+ exports.$insertTableRow=function(a,b,c=!0,n){var e=a.getChildren();if(b>=e.length||0>b)throw Error("Table row target index out of range");b=e[b];if(Q(b))for(e=0;e<n;e++){const q=b.getChildren().length,d=P();for(let m=0;m<q;m++){const k=A(!1);k.append(p.$createParagraphNode());d.append(k)}c?b.insertAfter(d):b.insertBefore(d)}else throw Error("Row before insertion index does not exist.");return a};exports.$isTableCellNode=C;exports.$isTableNode=N;exports.$isTableRowNode=Q;
30
+ exports.$removeTableRowAtIndex=function(a,b){const c=a.getChildren();if(b>=c.length||0>b)throw Error("Expected table cell to be inside of table row.");c[b].remove();return a};exports.TableCellNode=z;exports.TableNode=L;exports.TableRowNode=O;
package/package.json CHANGED
@@ -12,10 +12,10 @@
12
12
  "table"
13
13
  ],
14
14
  "license": "MIT",
15
- "version": "0.1.10",
15
+ "version": "0.1.11",
16
16
  "main": "LexicalTable.js",
17
17
  "peerDependencies": {
18
- "lexical": "0.1.10"
18
+ "lexical": "0.1.11"
19
19
  },
20
20
  "repository": {
21
21
  "type": "git",