@lexical/table 0.1.9 → 0.1.12
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.
- package/LexicalTable.dev.js +147 -114
- package/LexicalTable.prod.js +24 -23
- package/package.json +2 -2
package/LexicalTable.dev.js
CHANGED
@@ -32,17 +32,17 @@ function addClassNamesToElement(element, ...classNames) {
|
|
32
32
|
*
|
33
33
|
*
|
34
34
|
*/
|
35
|
-
class TableCellNode extends lexical.
|
35
|
+
class TableCellNode extends lexical.GridCellNode {
|
36
36
|
static getType() {
|
37
|
-
return '
|
37
|
+
return 'tablecell';
|
38
38
|
}
|
39
39
|
|
40
40
|
static clone(node) {
|
41
|
-
return new TableCellNode(false, node.__key);
|
41
|
+
return new TableCellNode(false, node.__colSpan, node.__key);
|
42
42
|
}
|
43
43
|
|
44
|
-
constructor(isHeader = false, key) {
|
45
|
-
super(key);
|
44
|
+
constructor(isHeader = false, colSpan = 1, key) {
|
45
|
+
super(colSpan, key);
|
46
46
|
this.__isHeader = isHeader;
|
47
47
|
}
|
48
48
|
|
@@ -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
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
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
|
-
|
157
|
-
gridNeedsRedraw = true;
|
158
|
-
break;
|
159
|
-
}
|
160
|
-
}
|
162
|
+
currentNode._cell = cell;
|
161
163
|
|
162
|
-
|
163
|
-
|
164
|
-
|
164
|
+
if (cells[y] === undefined) {
|
165
|
+
cells[y] = [];
|
166
|
+
}
|
165
167
|
|
166
|
-
|
168
|
+
cells[y][x] = cell;
|
169
|
+
} else {
|
170
|
+
const child = currentNode.firstChild;
|
167
171
|
|
168
|
-
|
169
|
-
|
172
|
+
if (child != null) {
|
173
|
+
currentNode = child;
|
174
|
+
continue;
|
175
|
+
}
|
176
|
+
}
|
170
177
|
|
171
|
-
|
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
|
-
|
180
|
+
if (sibling != null) {
|
181
|
+
x++;
|
182
|
+
currentNode = sibling;
|
183
|
+
continue;
|
184
|
+
}
|
182
185
|
|
183
|
-
|
184
|
-
cells[y] = [];
|
185
|
-
}
|
186
|
+
const parent = currentNode.parentNode;
|
186
187
|
|
187
|
-
|
188
|
-
|
189
|
-
const child = currentNode.firstChild;
|
188
|
+
if (parent != null) {
|
189
|
+
const parentSibling = parent.nextSibling;
|
190
190
|
|
191
|
-
|
192
|
-
|
193
|
-
continue;
|
194
|
-
}
|
191
|
+
if (parentSibling == null) {
|
192
|
+
break;
|
195
193
|
}
|
196
194
|
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
continue;
|
203
|
-
}
|
195
|
+
y++;
|
196
|
+
x = 0;
|
197
|
+
currentNode = parentSibling;
|
198
|
+
}
|
199
|
+
}
|
204
200
|
|
205
|
-
|
201
|
+
grid.columns = x + 1;
|
202
|
+
grid.rows = y + 1;
|
203
|
+
tableNode.setGrid(grid);
|
204
|
+
};
|
206
205
|
|
207
|
-
|
208
|
-
|
206
|
+
const observer = new MutationObserver(records => {
|
207
|
+
editor.update(() => {
|
208
|
+
let gridNeedsRedraw = false;
|
209
209
|
|
210
|
-
|
211
|
-
|
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
|
-
|
215
|
-
|
216
|
-
|
215
|
+
if (nodeName === 'TABLE' || nodeName === 'TR') {
|
216
|
+
gridNeedsRedraw = true;
|
217
|
+
break;
|
217
218
|
}
|
218
219
|
}
|
219
220
|
|
220
|
-
|
221
|
-
|
222
|
-
|
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
|
-
|
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
|
-
|
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;
|
@@ -398,7 +411,7 @@ function applyCustomTableHandlers(tableNode, tableElement, editor) {
|
|
398
411
|
const formatCells = type => {
|
399
412
|
let selection = lexical.$getSelection();
|
400
413
|
|
401
|
-
if (selection
|
414
|
+
if (!lexical.$isRangeSelection(selection)) {
|
402
415
|
selection = lexical.$createRangeSelection();
|
403
416
|
} // This is to make Flow play ball.
|
404
417
|
|
@@ -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 (
|
471
|
-
tableNode.getCellNodeFromCords(x
|
482
|
+
if (x !== (isForward ? grid.columns - 1 : 0)) {
|
483
|
+
tableNode.getCellNodeFromCords(x + (isForward ? 1 : -1), y).select();
|
472
484
|
} else {
|
473
|
-
if (
|
474
|
-
tableNode.getCellNodeFromCords(
|
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 (
|
488
|
-
tableNode.getCellNodeFromCords(x - 1
|
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 (
|
499
|
-
tableNode.getCellNodeFromCords(x + 1
|
510
|
+
if (y !== grid.rows - 1) {
|
511
|
+
tableNode.getCellNodeFromCords(x, y + 1).select();
|
500
512
|
} else {
|
501
513
|
tableNode.selectNext();
|
502
514
|
}
|
@@ -508,10 +520,10 @@ 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
|
-
if (selection
|
526
|
+
if (!lexical.$isRangeSelection(selection)) {
|
515
527
|
return false;
|
516
528
|
}
|
517
529
|
|
@@ -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 === '
|
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
|
-
|
547
|
+
event.preventDefault();
|
548
|
+
selectGridNodeInDirection(currentCords.x, currentCords.y, !event.shiftKey && type === 'keyTab' ? 'forward' : 'backward');
|
534
549
|
return true;
|
535
550
|
}
|
536
551
|
}
|
@@ -553,14 +568,26 @@ 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
|
|
558
|
-
|
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
|
+
*/
|
583
|
+
class TableNode extends lexical.GridNode {
|
559
584
|
static getType() {
|
560
585
|
return 'table';
|
561
586
|
}
|
562
587
|
|
563
588
|
static clone(node, selectionShape, grid) {
|
589
|
+
// TODO: selectionShape and grid aren't being deeply cloned?
|
590
|
+
// They shouldn't really be on the table node IMO.
|
564
591
|
return new TableNode(node.__key, node.__selectionShape, node.__grid);
|
565
592
|
}
|
566
593
|
|
@@ -573,7 +600,6 @@ class TableNode extends lexical.ElementNode {
|
|
573
600
|
createDOM(config, editor) {
|
574
601
|
const element = document.createElement('table');
|
575
602
|
addClassNamesToElement(element, config.theme.table);
|
576
|
-
applyCustomTableHandlers(this, element, editor);
|
577
603
|
return element;
|
578
604
|
}
|
579
605
|
|
@@ -592,44 +618,47 @@ class TableNode extends lexical.ElementNode {
|
|
592
618
|
setSelectionState(selectionShape) {
|
593
619
|
const self = this.getWritable();
|
594
620
|
self.__selectionShape = selectionShape;
|
595
|
-
|
621
|
+
const grid = this.getGrid();
|
622
|
+
if (grid == null) return [];
|
596
623
|
|
597
624
|
if (!selectionShape) {
|
598
|
-
return updateCells(-1, -1, -1, -1,
|
625
|
+
return updateCells(-1, -1, -1, -1, grid.cells);
|
599
626
|
}
|
600
627
|
|
601
|
-
return updateCells(selectionShape.fromX, selectionShape.toX, selectionShape.fromY, selectionShape.toY,
|
628
|
+
return updateCells(selectionShape.fromX, selectionShape.toX, selectionShape.fromY, selectionShape.toY, grid.cells);
|
602
629
|
}
|
603
630
|
|
604
631
|
getSelectionState() {
|
605
|
-
return this.__selectionShape;
|
632
|
+
return this.getLatest().__selectionShape;
|
606
633
|
}
|
607
634
|
|
608
635
|
getCordsFromCellNode(tableCellNode) {
|
609
|
-
|
636
|
+
const grid = this.getGrid();
|
637
|
+
|
638
|
+
if (!grid) {
|
610
639
|
throw Error(`Grid not found.`);
|
611
640
|
}
|
612
641
|
|
613
642
|
const {
|
614
643
|
rows,
|
615
644
|
cells
|
616
|
-
} =
|
645
|
+
} = grid;
|
617
646
|
|
618
|
-
for (let
|
619
|
-
const row = cells[
|
647
|
+
for (let y = 0; y < rows; y++) {
|
648
|
+
const row = cells[y];
|
620
649
|
|
621
650
|
if (row == null) {
|
622
|
-
throw new Error(`Row not found at
|
651
|
+
throw new Error(`Row not found at y:${y}`);
|
623
652
|
}
|
624
653
|
|
625
|
-
const
|
654
|
+
const x = row.findIndex(({
|
626
655
|
elem
|
627
656
|
}) => {
|
628
657
|
const cellNode = lexical.$getNearestNodeFromDOMNode(elem);
|
629
658
|
return cellNode === tableCellNode;
|
630
659
|
});
|
631
660
|
|
632
|
-
if (
|
661
|
+
if (x !== -1) {
|
633
662
|
return {
|
634
663
|
x,
|
635
664
|
y
|
@@ -641,23 +670,25 @@ class TableNode extends lexical.ElementNode {
|
|
641
670
|
}
|
642
671
|
|
643
672
|
getCellNodeFromCords(x, y) {
|
644
|
-
|
673
|
+
const grid = this.getGrid();
|
674
|
+
|
675
|
+
if (!grid) {
|
645
676
|
throw Error(`Grid not found.`);
|
646
677
|
}
|
647
678
|
|
648
679
|
const {
|
649
680
|
cells
|
650
|
-
} =
|
651
|
-
const row = cells[
|
681
|
+
} = grid;
|
682
|
+
const row = cells[y];
|
652
683
|
|
653
684
|
if (row == null) {
|
654
|
-
throw new Error(`Table row x:"${
|
685
|
+
throw new Error(`Table row x:"${y}" not found.`);
|
655
686
|
}
|
656
687
|
|
657
|
-
const cell = row[
|
688
|
+
const cell = row[x];
|
658
689
|
|
659
690
|
if (cell == null) {
|
660
|
-
throw new Error(`Table cell y:"${
|
691
|
+
throw new Error(`Table cell y:"${x}" in row x:"${y}" not found.`);
|
661
692
|
}
|
662
693
|
|
663
694
|
const node = lexical.$getNearestNodeFromDOMNode(cell.elem);
|
@@ -672,10 +703,11 @@ class TableNode extends lexical.ElementNode {
|
|
672
703
|
setGrid(grid) {
|
673
704
|
const self = this.getWritable();
|
674
705
|
self.__grid = grid;
|
706
|
+
return self;
|
675
707
|
}
|
676
708
|
|
677
709
|
getGrid() {
|
678
|
-
return this.__grid;
|
710
|
+
return this.getLatest().__grid;
|
679
711
|
}
|
680
712
|
|
681
713
|
canSelectBefore() {
|
@@ -698,9 +730,9 @@ function $isTableNode(node) {
|
|
698
730
|
*
|
699
731
|
*
|
700
732
|
*/
|
701
|
-
class TableRowNode extends lexical.
|
733
|
+
class TableRowNode extends lexical.GridRowNode {
|
702
734
|
static getType() {
|
703
|
-
return '
|
735
|
+
return 'tablerow';
|
704
736
|
}
|
705
737
|
|
706
738
|
static clone(node) {
|
@@ -823,7 +855,7 @@ function $insertTableRow(tableNode, targetIndex, shouldInsertAfter = true, rowCo
|
|
823
855
|
|
824
856
|
for (let j = 0; j < tableColumnCount; j++) {
|
825
857
|
const tableCellNode = $createTableCellNode(false);
|
826
|
-
tableCellNode.append(lexical.$
|
858
|
+
tableCellNode.append(lexical.$createParagraphNode());
|
827
859
|
newTableRowNode.append(tableCellNode);
|
828
860
|
}
|
829
861
|
|
@@ -848,7 +880,7 @@ function $insertTableColumn(tableNode, targetIndex, shouldInsertAfter = true, co
|
|
848
880
|
if ($isTableRowNode(currentTableRowNode)) {
|
849
881
|
for (let j = 0; j < columnCount; j++) {
|
850
882
|
const newTableCell = $createTableCellNode(i === 0);
|
851
|
-
newTableCell.append(lexical.$
|
883
|
+
newTableCell.append(lexical.$createParagraphNode());
|
852
884
|
const tableRowChildren = currentTableRowNode.getChildren();
|
853
885
|
|
854
886
|
if (targetIndex >= tableRowChildren.length || targetIndex < 0) {
|
@@ -888,6 +920,7 @@ function $deleteTableColumn(tableNode, targetIndex) {
|
|
888
920
|
return tableNode;
|
889
921
|
}
|
890
922
|
|
923
|
+
exports.$applyCustomTableHandlers = $applyCustomTableHandlers;
|
891
924
|
exports.$createTableCellNode = $createTableCellNode;
|
892
925
|
exports.$createTableNode = $createTableNode;
|
893
926
|
exports.$createTableNodeWithDimensions = $createTableNodeWithDimensions;
|
package/LexicalTable.prod.js
CHANGED
@@ -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
|
8
|
-
class
|
9
|
-
function
|
10
|
-
function G(a){for(;null!=
|
11
|
-
|
12
|
-
|
13
|
-
function
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
exports.$
|
27
|
-
exports.$
|
28
|
-
exports.$
|
29
|
-
exports.$
|
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