@lexical/table 0.1.16 → 0.1.19
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/LICENSE +1 -1
- package/LexicalTable.d.ts +16 -0
- package/LexicalTable.dev.js +450 -83
- package/LexicalTable.js.flow +246 -0
- package/LexicalTable.prod.js +42 -32
- package/package.json +3 -7
package/LICENSE
CHANGED
package/LexicalTable.d.ts
CHANGED
@@ -1,3 +1,12 @@
|
|
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
|
+
|
1
10
|
import type {
|
2
11
|
EditorConfig,
|
3
12
|
LexicalNode,
|
@@ -73,6 +82,8 @@ export declare class TableNode extends ElementNode {
|
|
73
82
|
canInsertTab(): true;
|
74
83
|
collapseAtStart(): true;
|
75
84
|
getCordsFromCellNode(tableCellNode: TableCellNode): {x: number; y: number};
|
85
|
+
getCellFromCords(x: number, y: number, grid: Grid): ?Cell;
|
86
|
+
getCellFromCordsOrThrow(x: number, y: number, grid: Grid): Cell;
|
76
87
|
getCellNodeFromCords(x: number, y: number): TableCellNode | null;
|
77
88
|
getCellNodeFromCordsOrThrow(x: number, y: number): TableCellNode;
|
78
89
|
setGrid(grid?: Grid): TableNode;
|
@@ -219,3 +230,8 @@ declare class TableSelection {
|
|
219
230
|
formatCells(type: TextFormatType): void;
|
220
231
|
clearText(): void;
|
221
232
|
}
|
233
|
+
|
234
|
+
export var INSERT_TABLE_COMMAND: LexicalCommand<{
|
235
|
+
rows: string;
|
236
|
+
columns: string;
|
237
|
+
}>;
|
package/LexicalTable.dev.js
CHANGED
@@ -6,8 +6,8 @@
|
|
6
6
|
*/
|
7
7
|
'use strict';
|
8
8
|
|
9
|
-
var utils = require('@lexical/utils');
|
10
9
|
var lexical = require('lexical');
|
10
|
+
var utils = require('@lexical/utils');
|
11
11
|
|
12
12
|
/* eslint-disable sort-keys-fix/sort-keys-fix */
|
13
13
|
const TableCellHeaderStates = {
|
@@ -25,6 +25,19 @@ class TableCellNode extends lexical.GridCellNode {
|
|
25
25
|
return new TableCellNode(node.__headerState, node.__colSpan, node.__width, node.__key);
|
26
26
|
}
|
27
27
|
|
28
|
+
static convertDOM() {
|
29
|
+
return {
|
30
|
+
td: node => ({
|
31
|
+
conversion: convertTableCellNodeElement,
|
32
|
+
priority: 0
|
33
|
+
}),
|
34
|
+
th: node => ({
|
35
|
+
conversion: convertTableCellNodeElement,
|
36
|
+
priority: 0
|
37
|
+
})
|
38
|
+
};
|
39
|
+
}
|
40
|
+
|
28
41
|
constructor(headerState = TableCellHeaderStates.NO_STATUS, colSpan = 1, width, key) {
|
29
42
|
super(colSpan, key);
|
30
43
|
this.__headerState = headerState;
|
@@ -100,6 +113,27 @@ class TableCellNode extends lexical.GridCellNode {
|
|
100
113
|
}
|
101
114
|
|
102
115
|
}
|
116
|
+
function convertTableCellNodeElement(domNode) {
|
117
|
+
const nodeName = domNode.nodeName.toLowerCase();
|
118
|
+
const tableCellNode = $createTableCellNode(nodeName === 'th' ? TableCellHeaderStates.ROW : TableCellHeaderStates.NO_STATUS);
|
119
|
+
return {
|
120
|
+
node: tableCellNode,
|
121
|
+
forChild: (lexicalNode, parentLexicalNode) => {
|
122
|
+
if ($isTableCellNode(parentLexicalNode) && !lexical.$isElementNode(lexicalNode)) {
|
123
|
+
const paragraphNode = lexical.$createParagraphNode();
|
124
|
+
|
125
|
+
if (lexical.$isLineBreakNode(lexicalNode) && lexicalNode.getTextContent() === '\n') {
|
126
|
+
return null;
|
127
|
+
}
|
128
|
+
|
129
|
+
paragraphNode.append(lexicalNode);
|
130
|
+
return paragraphNode;
|
131
|
+
}
|
132
|
+
|
133
|
+
return lexicalNode;
|
134
|
+
}
|
135
|
+
};
|
136
|
+
}
|
103
137
|
function $createTableCellNode(headerState, colSpan = 1, width) {
|
104
138
|
return new TableCellNode(headerState, colSpan, width);
|
105
139
|
}
|
@@ -131,7 +165,6 @@ const removeHighlightStyle = document.createElement('style');
|
|
131
165
|
removeHighlightStyle.appendChild(document.createTextNode('::selection{background-color: transparent}'));
|
132
166
|
class TableSelection {
|
133
167
|
constructor(editor, tableNodeKey) {
|
134
|
-
this.isMouseDown = false;
|
135
168
|
this.isHighlightingCells = false;
|
136
169
|
this.startX = -1;
|
137
170
|
this.startY = -1;
|
@@ -148,6 +181,8 @@ class TableSelection {
|
|
148
181
|
this.gridSelection = null;
|
149
182
|
this.anchorCellNodeKey = null;
|
150
183
|
this.focusCellNodeKey = null;
|
184
|
+
this.anchorCell = null;
|
185
|
+
this.focusCell = null;
|
151
186
|
this.trackTableGrid();
|
152
187
|
}
|
153
188
|
|
@@ -219,14 +254,18 @@ class TableSelection {
|
|
219
254
|
|
220
255
|
const grid = getTableGrid(tableElement);
|
221
256
|
this.isHighlightingCells = false;
|
222
|
-
this.isMouseDown = false;
|
223
257
|
this.startX = -1;
|
224
258
|
this.startY = -1;
|
225
259
|
this.currentX = -1;
|
226
260
|
this.currentY = -1;
|
227
|
-
$updateDOMForSelection(grid, null);
|
228
261
|
this.gridSelection = null;
|
262
|
+
this.anchorCellNodeKey = null;
|
263
|
+
this.focusCellNodeKey = null;
|
264
|
+
this.anchorCell = null;
|
265
|
+
this.focusCell = null;
|
266
|
+
$updateDOMForSelection(grid, null);
|
229
267
|
lexical.$setSelection(null);
|
268
|
+
this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND);
|
230
269
|
const parent = removeHighlightStyle.parentNode;
|
231
270
|
|
232
271
|
if (parent != null) {
|
@@ -237,7 +276,6 @@ class TableSelection {
|
|
237
276
|
|
238
277
|
adjustFocusCellForSelection(cell, ignoreStart = false) {
|
239
278
|
this.editor.update(() => {
|
240
|
-
this.isMouseDown = true;
|
241
279
|
const tableNode = lexical.$getNodeByKey(this.tableNodeKey);
|
242
280
|
|
243
281
|
if (!$isTableNode(tableNode)) {
|
@@ -252,16 +290,15 @@ class TableSelection {
|
|
252
290
|
|
253
291
|
const cellX = cell.x;
|
254
292
|
const cellY = cell.y;
|
293
|
+
this.focusCell = cell;
|
294
|
+
const domSelection = getDOMSelection();
|
255
295
|
|
256
|
-
if (
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
if (anchorNode !== null) {
|
261
|
-
// Collapse the selection
|
262
|
-
domSelection.setBaseAndExtent(anchorNode, 0, anchorNode, 0);
|
263
|
-
}
|
296
|
+
if (this.anchorCell !== null) {
|
297
|
+
// Collapse the selection
|
298
|
+
domSelection.setBaseAndExtent(this.anchorCell.elem, 0, cell.elem, 0);
|
299
|
+
}
|
264
300
|
|
301
|
+
if (!this.isHighlightingCells && (this.startX !== cellX || this.startY !== cellY || ignoreStart)) {
|
265
302
|
this.isHighlightingCells = true;
|
266
303
|
|
267
304
|
if (document.body) {
|
@@ -284,6 +321,7 @@ class TableSelection {
|
|
284
321
|
this.gridSelection.set(this.tableNodeKey, // $FlowFixMe This is not null, as you can see in the statement above.
|
285
322
|
this.anchorCellNodeKey, this.focusCellNodeKey);
|
286
323
|
lexical.$setSelection(this.gridSelection);
|
324
|
+
this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND);
|
287
325
|
$updateDOMForSelection(this.grid, this.gridSelection);
|
288
326
|
}
|
289
327
|
}
|
@@ -292,9 +330,11 @@ class TableSelection {
|
|
292
330
|
|
293
331
|
setAnchorCellForSelection(cell) {
|
294
332
|
this.editor.update(() => {
|
333
|
+
this.anchorCell = cell;
|
295
334
|
this.startX = cell.x;
|
296
335
|
this.startY = cell.y;
|
297
|
-
|
336
|
+
const domSelection = getDOMSelection();
|
337
|
+
domSelection.setBaseAndExtent(cell.elem, 0, cell.elem, 0);
|
298
338
|
const anchorTableCellNode = lexical.$getNearestNodeFromDOMNode(cell.elem);
|
299
339
|
|
300
340
|
if ($isTableCellNode(anchorTableCellNode)) {
|
@@ -303,12 +343,6 @@ class TableSelection {
|
|
303
343
|
this.anchorCellNodeKey = anchorNodeKey;
|
304
344
|
}
|
305
345
|
});
|
306
|
-
document.addEventListener('mouseup', () => {
|
307
|
-
this.isMouseDown = false;
|
308
|
-
}, {
|
309
|
-
capture: true,
|
310
|
-
once: true
|
311
|
-
});
|
312
346
|
}
|
313
347
|
|
314
348
|
formatCells(type) {
|
@@ -326,13 +360,14 @@ class TableSelection {
|
|
326
360
|
const anchor = formatSelection.anchor;
|
327
361
|
const focus = formatSelection.focus;
|
328
362
|
selection.getNodes().forEach(cellNode => {
|
329
|
-
if (
|
363
|
+
if ($isTableCellNode(cellNode) && cellNode.getTextContentSize() !== 0) {
|
330
364
|
anchor.set(cellNode.getKey(), 0, 'element');
|
331
365
|
focus.set(cellNode.getKey(), cellNode.getChildrenSize(), 'element');
|
332
366
|
formatSelection.formatText(type);
|
333
367
|
}
|
334
368
|
});
|
335
369
|
lexical.$setSelection(selection);
|
370
|
+
this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND);
|
336
371
|
});
|
337
372
|
}
|
338
373
|
|
@@ -352,7 +387,7 @@ class TableSelection {
|
|
352
387
|
}
|
353
388
|
}
|
354
389
|
|
355
|
-
const selectedNodes = selection.getNodes();
|
390
|
+
const selectedNodes = selection.getNodes().filter($isTableCellNode);
|
356
391
|
|
357
392
|
if (selectedNodes.length === this.grid.columns * this.grid.rows) {
|
358
393
|
tableNode.selectPrevious(); // Delete entire table
|
@@ -377,6 +412,7 @@ class TableSelection {
|
|
377
412
|
});
|
378
413
|
$updateDOMForSelection(this.grid, null);
|
379
414
|
lexical.$setSelection(null);
|
415
|
+
this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND);
|
380
416
|
});
|
381
417
|
}
|
382
418
|
|
@@ -401,6 +437,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
401
437
|
|
402
438
|
const tableSelection = new TableSelection(editor, tableNode.getKey());
|
403
439
|
attachTableSelectionToTableElement(tableElement, tableSelection);
|
440
|
+
let isMouseDown = false;
|
404
441
|
tableElement.addEventListener('dblclick', event => {
|
405
442
|
// $FlowFixMe: event.target is always a Node on the DOM
|
406
443
|
const cell = getCellFromTarget(event.target);
|
@@ -411,23 +448,34 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
411
448
|
event.stopPropagation();
|
412
449
|
tableSelection.setAnchorCellForSelection(cell);
|
413
450
|
tableSelection.adjustFocusCellForSelection(cell, true);
|
414
|
-
|
451
|
+
isMouseDown = false;
|
415
452
|
}
|
416
453
|
}); // This is the anchor of the selection.
|
417
454
|
|
418
455
|
tableElement.addEventListener('mousedown', event => {
|
419
456
|
setTimeout(() => {
|
420
|
-
|
457
|
+
if (event.button !== 0) {
|
458
|
+
return;
|
459
|
+
} // $FlowFixMe: event.target is always a Node on the DOM
|
460
|
+
|
461
|
+
|
421
462
|
const cell = getCellFromTarget(event.target);
|
422
463
|
|
423
464
|
if (cell !== null) {
|
465
|
+
isMouseDown = true;
|
424
466
|
tableSelection.setAnchorCellForSelection(cell);
|
467
|
+
document.addEventListener('mouseup', () => {
|
468
|
+
isMouseDown = false;
|
469
|
+
}, {
|
470
|
+
capture: true,
|
471
|
+
once: true
|
472
|
+
});
|
425
473
|
}
|
426
474
|
}, 0);
|
427
475
|
}); // This is adjusting the focus of the selection.
|
428
476
|
|
429
477
|
tableElement.addEventListener('mousemove', event => {
|
430
|
-
if (
|
478
|
+
if (isMouseDown) {
|
431
479
|
// $FlowFixMe: event.target is always a Node on the DOM
|
432
480
|
const cell = getCellFromTarget(event.target);
|
433
481
|
|
@@ -435,30 +483,35 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
435
483
|
const cellX = cell.x;
|
436
484
|
const cellY = cell.y;
|
437
485
|
|
438
|
-
if (
|
486
|
+
if (isMouseDown && (tableSelection.startX !== cellX || tableSelection.startY !== cellY || tableSelection.isHighlightingCells)) {
|
439
487
|
event.preventDefault();
|
488
|
+
isMouseDown = true;
|
440
489
|
tableSelection.adjustFocusCellForSelection(cell);
|
441
490
|
}
|
442
491
|
}
|
443
492
|
}
|
444
493
|
});
|
445
494
|
tableElement.addEventListener('mouseup', event => {
|
446
|
-
if (
|
447
|
-
|
495
|
+
if (isMouseDown) {
|
496
|
+
isMouseDown = false;
|
448
497
|
}
|
449
498
|
}); // Select entire table at this point, when grid selection is ready.
|
450
499
|
|
451
500
|
tableElement.addEventListener('mouseleave', event => {
|
452
|
-
if (
|
501
|
+
if (isMouseDown) {
|
453
502
|
return;
|
454
503
|
}
|
455
504
|
}); // Clear selection when clicking outside of dom.
|
456
505
|
|
457
|
-
const mouseDownCallback =
|
506
|
+
const mouseDownCallback = event => {
|
507
|
+
if (event.button !== 0) {
|
508
|
+
return;
|
509
|
+
}
|
510
|
+
|
458
511
|
editor.update(() => {
|
459
512
|
const selection = lexical.$getSelection();
|
460
513
|
|
461
|
-
if (lexical.$isGridSelection(selection) && selection.gridKey === tableSelection.tableNodeKey && rootElement.contains(
|
514
|
+
if (lexical.$isGridSelection(selection) && selection.gridKey === tableSelection.tableNodeKey && rootElement.contains(event.target)) {
|
462
515
|
return tableSelection.clearHighlight();
|
463
516
|
}
|
464
517
|
});
|
@@ -466,80 +519,297 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
466
519
|
|
467
520
|
window.addEventListener('mousedown', mouseDownCallback);
|
468
521
|
tableSelection.listenersToRemove.add(() => window.removeEventListener('mousedown', mouseDownCallback));
|
469
|
-
tableSelection.listenersToRemove.add(editor.
|
522
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_DOWN_COMMAND, payload => {
|
470
523
|
const selection = lexical.$getSelection();
|
524
|
+
const event = payload;
|
525
|
+
const direction = 'down';
|
471
526
|
|
472
|
-
if (lexical.$
|
473
|
-
if (
|
474
|
-
const
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
tableSelection.
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
527
|
+
if (lexical.$isRangeSelection(selection)) {
|
528
|
+
if (selection.isCollapsed()) {
|
529
|
+
const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
|
530
|
+
|
531
|
+
if (!$isTableCellNode(tableCellNode)) {
|
532
|
+
return false;
|
533
|
+
}
|
534
|
+
|
535
|
+
const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
|
536
|
+
const elementParentNode = utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isElementNode(n));
|
537
|
+
|
538
|
+
if (elementParentNode == null) {
|
539
|
+
throw new Error('Expected BlockNode Parent');
|
540
|
+
}
|
541
|
+
|
542
|
+
const lastChild = tableCellNode.getLastChild();
|
543
|
+
const isSelectionInLastBlock = lastChild && elementParentNode.isParentOf(lastChild) || elementParentNode === lastChild;
|
544
|
+
|
545
|
+
if (isSelectionInLastBlock || event.shiftKey) {
|
546
|
+
event.preventDefault();
|
547
|
+
event.stopImmediatePropagation();
|
548
|
+
event.stopPropagation(); // Start Selection
|
549
|
+
|
550
|
+
if (event.shiftKey) {
|
551
|
+
tableSelection.setAnchorCellForSelection(tableNode.getCellFromCordsOrThrow(currentCords.x, currentCords.y, tableSelection.grid));
|
552
|
+
return adjustFocusNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
|
553
|
+
}
|
554
|
+
|
555
|
+
return selectGridNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
|
556
|
+
}
|
485
557
|
}
|
486
|
-
} else if (lexical.$
|
487
|
-
const tableCellNode =
|
558
|
+
} else if (lexical.$isGridSelection(selection) && event.shiftKey) {
|
559
|
+
const tableCellNode = selection.focus.getNode();
|
488
560
|
|
489
561
|
if (!$isTableCellNode(tableCellNode)) {
|
490
562
|
return false;
|
491
563
|
}
|
492
564
|
|
493
|
-
|
494
|
-
|
495
|
-
|
565
|
+
const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
|
566
|
+
event.preventDefault();
|
567
|
+
event.stopImmediatePropagation();
|
568
|
+
event.stopPropagation();
|
569
|
+
return adjustFocusNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
|
570
|
+
}
|
571
|
+
|
572
|
+
return false;
|
573
|
+
}, CriticalPriority));
|
574
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_UP_COMMAND, payload => {
|
575
|
+
const selection = lexical.$getSelection();
|
576
|
+
const event = payload;
|
577
|
+
const direction = 'up';
|
578
|
+
|
579
|
+
if (lexical.$isRangeSelection(selection)) {
|
580
|
+
if (selection.isCollapsed()) {
|
581
|
+
const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
|
582
|
+
|
583
|
+
if (!$isTableCellNode(tableCellNode)) {
|
584
|
+
return false;
|
496
585
|
}
|
497
|
-
}
|
498
586
|
|
499
|
-
|
500
|
-
const
|
587
|
+
const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
|
588
|
+
const elementParentNode = utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isElementNode(n));
|
589
|
+
|
590
|
+
if (elementParentNode == null) {
|
591
|
+
throw new Error('Expected BlockNode Parent');
|
592
|
+
}
|
593
|
+
|
594
|
+
const lastChild = tableCellNode.getLastChild();
|
595
|
+
const isSelectionInLastBlock = lastChild && elementParentNode.isParentOf(lastChild) || elementParentNode === lastChild;
|
501
596
|
|
502
|
-
if (
|
503
|
-
const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
|
597
|
+
if (isSelectionInLastBlock || event.shiftKey) {
|
504
598
|
event.preventDefault();
|
505
|
-
|
506
|
-
|
599
|
+
event.stopImmediatePropagation();
|
600
|
+
event.stopPropagation(); // Start Selection
|
601
|
+
|
602
|
+
if (event.shiftKey) {
|
603
|
+
tableSelection.setAnchorCellForSelection(tableNode.getCellFromCordsOrThrow(currentCords.x, currentCords.y, tableSelection.grid));
|
604
|
+
return adjustFocusNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
|
605
|
+
}
|
606
|
+
|
607
|
+
return selectGridNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
|
507
608
|
}
|
508
609
|
}
|
610
|
+
} else if (lexical.$isGridSelection(selection) && event.shiftKey) {
|
611
|
+
const tableCellNode = selection.focus.getNode();
|
509
612
|
|
510
|
-
if (
|
511
|
-
|
613
|
+
if (!$isTableCellNode(tableCellNode)) {
|
614
|
+
return false;
|
615
|
+
}
|
512
616
|
|
513
|
-
|
514
|
-
|
515
|
-
|
617
|
+
const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
|
618
|
+
event.preventDefault();
|
619
|
+
event.stopImmediatePropagation();
|
620
|
+
event.stopPropagation();
|
621
|
+
return adjustFocusNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
|
622
|
+
}
|
516
623
|
|
517
|
-
|
518
|
-
|
519
|
-
|
624
|
+
return false;
|
625
|
+
}, CriticalPriority));
|
626
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_LEFT_COMMAND, payload => {
|
627
|
+
const selection = lexical.$getSelection();
|
628
|
+
const event = payload;
|
629
|
+
const direction = 'backward';
|
630
|
+
|
631
|
+
if (lexical.$isRangeSelection(selection)) {
|
632
|
+
if (selection.isCollapsed()) {
|
633
|
+
const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
|
634
|
+
|
635
|
+
if (!$isTableCellNode(tableCellNode)) {
|
636
|
+
return false;
|
637
|
+
}
|
638
|
+
|
639
|
+
const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
|
640
|
+
const elementParentNode = utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isElementNode(n));
|
520
641
|
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
642
|
+
if (elementParentNode == null) {
|
643
|
+
throw new Error('Expected BlockNode Parent');
|
644
|
+
}
|
645
|
+
|
646
|
+
if (selection.anchor.offset === 0 || event.shiftKey) {
|
647
|
+
event.preventDefault();
|
648
|
+
event.stopImmediatePropagation();
|
649
|
+
event.stopPropagation(); // Start Selection
|
650
|
+
|
651
|
+
if (event.shiftKey) {
|
652
|
+
tableSelection.setAnchorCellForSelection(tableNode.getCellFromCordsOrThrow(currentCords.x, currentCords.y, tableSelection.grid));
|
653
|
+
return adjustFocusNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
|
532
654
|
}
|
533
655
|
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
656
|
+
return selectGridNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
|
657
|
+
}
|
658
|
+
}
|
659
|
+
} else if (lexical.$isGridSelection(selection) && event.shiftKey) {
|
660
|
+
const tableCellNode = selection.focus.getNode();
|
661
|
+
|
662
|
+
if (!$isTableCellNode(tableCellNode)) {
|
663
|
+
return false;
|
664
|
+
}
|
665
|
+
|
666
|
+
const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
|
667
|
+
event.preventDefault();
|
668
|
+
event.stopImmediatePropagation();
|
669
|
+
event.stopPropagation();
|
670
|
+
return adjustFocusNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
|
671
|
+
}
|
672
|
+
|
673
|
+
return false;
|
674
|
+
}, CriticalPriority));
|
675
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_RIGHT_COMMAND, payload => {
|
676
|
+
const selection = lexical.$getSelection();
|
677
|
+
const event = payload;
|
678
|
+
const direction = 'forward';
|
679
|
+
|
680
|
+
if (lexical.$isRangeSelection(selection)) {
|
681
|
+
if (selection.isCollapsed()) {
|
682
|
+
const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
|
683
|
+
|
684
|
+
if (!$isTableCellNode(tableCellNode)) {
|
685
|
+
return false;
|
686
|
+
}
|
687
|
+
|
688
|
+
const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
|
689
|
+
const elementParentNode = utils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isElementNode(n));
|
690
|
+
|
691
|
+
if (elementParentNode == null) {
|
692
|
+
throw new Error('Expected BlockNode Parent');
|
693
|
+
}
|
694
|
+
|
695
|
+
if (selection.anchor.offset === selection.anchor.getNode().getTextContentSize() || event.shiftKey) {
|
696
|
+
event.preventDefault();
|
697
|
+
event.stopImmediatePropagation();
|
698
|
+
event.stopPropagation(); // Start Selection
|
699
|
+
|
700
|
+
if (event.shiftKey) {
|
701
|
+
tableSelection.setAnchorCellForSelection(tableNode.getCellFromCordsOrThrow(currentCords.x, currentCords.y, tableSelection.grid));
|
702
|
+
return adjustFocusNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
|
540
703
|
}
|
704
|
+
|
705
|
+
return selectGridNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
|
541
706
|
}
|
542
707
|
}
|
708
|
+
} else if (lexical.$isGridSelection(selection) && event.shiftKey) {
|
709
|
+
const tableCellNode = selection.focus.getNode();
|
710
|
+
|
711
|
+
if (!$isTableCellNode(tableCellNode)) {
|
712
|
+
return false;
|
713
|
+
}
|
714
|
+
|
715
|
+
const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
|
716
|
+
event.preventDefault();
|
717
|
+
event.stopImmediatePropagation();
|
718
|
+
event.stopPropagation();
|
719
|
+
return adjustFocusNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, direction);
|
720
|
+
}
|
721
|
+
|
722
|
+
return false;
|
723
|
+
}, CriticalPriority));
|
724
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.DELETE_CHARACTER_COMMAND, () => {
|
725
|
+
const selection = lexical.$getSelection();
|
726
|
+
|
727
|
+
if (lexical.$isGridSelection(selection)) {
|
728
|
+
tableSelection.clearText();
|
729
|
+
return true;
|
730
|
+
} else if (lexical.$isRangeSelection(selection)) {
|
731
|
+
const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
|
732
|
+
|
733
|
+
if (!$isTableCellNode(tableCellNode)) {
|
734
|
+
return false;
|
735
|
+
}
|
736
|
+
|
737
|
+
if (selection.isCollapsed() && selection.anchor.offset === 0 && selection.anchor.getNode().getPreviousSiblings().length === 0) {
|
738
|
+
return true;
|
739
|
+
}
|
740
|
+
}
|
741
|
+
|
742
|
+
return false;
|
743
|
+
}, CriticalPriority));
|
744
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_BACKSPACE_COMMAND, payload => {
|
745
|
+
const selection = lexical.$getSelection();
|
746
|
+
|
747
|
+
if (lexical.$isGridSelection(selection)) {
|
748
|
+
const event = payload;
|
749
|
+
event.preventDefault();
|
750
|
+
event.stopPropagation();
|
751
|
+
tableSelection.clearText();
|
752
|
+
return true;
|
753
|
+
} else if (lexical.$isRangeSelection(selection)) {
|
754
|
+
const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
|
755
|
+
|
756
|
+
if (!$isTableCellNode(tableCellNode)) {
|
757
|
+
return false;
|
758
|
+
}
|
759
|
+
}
|
760
|
+
|
761
|
+
return false;
|
762
|
+
}, CriticalPriority));
|
763
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.FORMAT_TEXT_COMMAND, payload => {
|
764
|
+
const selection = lexical.$getSelection();
|
765
|
+
|
766
|
+
if (lexical.$isGridSelection(selection)) {
|
767
|
+
tableSelection.formatCells(payload);
|
768
|
+
return true;
|
769
|
+
} else if (lexical.$isRangeSelection(selection)) {
|
770
|
+
const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
|
771
|
+
|
772
|
+
if (!$isTableCellNode(tableCellNode)) {
|
773
|
+
return false;
|
774
|
+
}
|
775
|
+
}
|
776
|
+
|
777
|
+
return false;
|
778
|
+
}, CriticalPriority));
|
779
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.INSERT_TEXT_COMMAND, payload => {
|
780
|
+
const selection = lexical.$getSelection();
|
781
|
+
|
782
|
+
if (lexical.$isGridSelection(selection)) {
|
783
|
+
tableSelection.clearHighlight();
|
784
|
+
return false;
|
785
|
+
} else if (lexical.$isRangeSelection(selection)) {
|
786
|
+
const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
|
787
|
+
|
788
|
+
if (!$isTableCellNode(tableCellNode)) {
|
789
|
+
return false;
|
790
|
+
}
|
791
|
+
}
|
792
|
+
|
793
|
+
return false;
|
794
|
+
}, CriticalPriority));
|
795
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_TAB_COMMAND, payload => {
|
796
|
+
const selection = lexical.$getSelection();
|
797
|
+
|
798
|
+
if (lexical.$isRangeSelection(selection)) {
|
799
|
+
const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
|
800
|
+
|
801
|
+
if (!$isTableCellNode(tableCellNode)) {
|
802
|
+
return false;
|
803
|
+
}
|
804
|
+
|
805
|
+
const event = payload;
|
806
|
+
|
807
|
+
if (selection.isCollapsed()) {
|
808
|
+
const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
|
809
|
+
event.preventDefault();
|
810
|
+
selectGridNodeInDirection(tableSelection, tableNode, currentCords.x, currentCords.y, !event.shiftKey ? 'forward' : 'backward');
|
811
|
+
return true;
|
812
|
+
}
|
543
813
|
}
|
544
814
|
|
545
815
|
return false;
|
@@ -727,6 +997,44 @@ const selectGridNodeInDirection = (tableSelection, tableNode, x, y, direction) =
|
|
727
997
|
return false;
|
728
998
|
};
|
729
999
|
|
1000
|
+
const adjustFocusNodeInDirection = (tableSelection, tableNode, x, y, direction) => {
|
1001
|
+
switch (direction) {
|
1002
|
+
case 'backward':
|
1003
|
+
case 'forward':
|
1004
|
+
{
|
1005
|
+
const isForward = direction === 'forward';
|
1006
|
+
|
1007
|
+
if (x !== (isForward ? tableSelection.grid.columns - 1 : 0)) {
|
1008
|
+
tableSelection.adjustFocusCellForSelection(tableNode.getCellFromCordsOrThrow(x + (isForward ? 1 : -1), y, tableSelection.grid));
|
1009
|
+
}
|
1010
|
+
|
1011
|
+
return true;
|
1012
|
+
}
|
1013
|
+
|
1014
|
+
case 'up':
|
1015
|
+
{
|
1016
|
+
if (y !== 0) {
|
1017
|
+
tableSelection.adjustFocusCellForSelection(tableNode.getCellFromCordsOrThrow(x, y - 1, tableSelection.grid));
|
1018
|
+
return true;
|
1019
|
+
} else {
|
1020
|
+
return false;
|
1021
|
+
}
|
1022
|
+
}
|
1023
|
+
|
1024
|
+
case 'down':
|
1025
|
+
{
|
1026
|
+
if (y !== tableSelection.grid.rows - 1) {
|
1027
|
+
tableSelection.adjustFocusCellForSelection(tableNode.getCellFromCordsOrThrow(x, y + 1, tableSelection.grid));
|
1028
|
+
return true;
|
1029
|
+
} else {
|
1030
|
+
return false;
|
1031
|
+
}
|
1032
|
+
}
|
1033
|
+
}
|
1034
|
+
|
1035
|
+
return false;
|
1036
|
+
};
|
1037
|
+
|
730
1038
|
function selectTableCellNode(tableCell) {
|
731
1039
|
const possibleParagraph = tableCell.getChildren().find(n => lexical.$isParagraphNode(n));
|
732
1040
|
|
@@ -754,6 +1062,15 @@ class TableNode extends lexical.GridNode {
|
|
754
1062
|
return new TableNode(node.__key);
|
755
1063
|
}
|
756
1064
|
|
1065
|
+
static convertDOM() {
|
1066
|
+
return {
|
1067
|
+
table: node => ({
|
1068
|
+
conversion: convertTableElement,
|
1069
|
+
priority: 0
|
1070
|
+
})
|
1071
|
+
};
|
1072
|
+
}
|
1073
|
+
|
757
1074
|
constructor(key) {
|
758
1075
|
super(key);
|
759
1076
|
}
|
@@ -811,7 +1128,7 @@ class TableNode extends lexical.GridNode {
|
|
811
1128
|
throw new Error('Cell not found in table.');
|
812
1129
|
}
|
813
1130
|
|
814
|
-
|
1131
|
+
getCellFromCords(x, y, grid) {
|
815
1132
|
if (!grid) {
|
816
1133
|
throw Error(`Grid not found.`);
|
817
1134
|
}
|
@@ -831,6 +1148,26 @@ class TableNode extends lexical.GridNode {
|
|
831
1148
|
return null;
|
832
1149
|
}
|
833
1150
|
|
1151
|
+
return cell;
|
1152
|
+
}
|
1153
|
+
|
1154
|
+
getCellFromCordsOrThrow(x, y, grid) {
|
1155
|
+
const cell = this.getCellFromCords(x, y, grid);
|
1156
|
+
|
1157
|
+
if (!cell) {
|
1158
|
+
throw new Error('Cell not found at cords.');
|
1159
|
+
}
|
1160
|
+
|
1161
|
+
return cell;
|
1162
|
+
}
|
1163
|
+
|
1164
|
+
getCellNodeFromCords(x, y, grid) {
|
1165
|
+
const cell = this.getCellFromCords(x, y, grid);
|
1166
|
+
|
1167
|
+
if (cell == null) {
|
1168
|
+
return null;
|
1169
|
+
}
|
1170
|
+
|
834
1171
|
const node = lexical.$getNearestNodeFromDOMNode(cell.elem);
|
835
1172
|
|
836
1173
|
if ($isTableCellNode(node)) {
|
@@ -864,6 +1201,11 @@ function $getElementGridForTableNode(editor, tableNode) {
|
|
864
1201
|
|
865
1202
|
return getTableGrid(tableElement);
|
866
1203
|
}
|
1204
|
+
function convertTableElement(domNode) {
|
1205
|
+
return {
|
1206
|
+
node: $createTableNode()
|
1207
|
+
};
|
1208
|
+
}
|
867
1209
|
function $createTableNode() {
|
868
1210
|
return new TableNode();
|
869
1211
|
}
|
@@ -888,6 +1230,15 @@ class TableRowNode extends lexical.GridRowNode {
|
|
888
1230
|
return new TableRowNode(node.__height, node.__key);
|
889
1231
|
}
|
890
1232
|
|
1233
|
+
static convertDOM() {
|
1234
|
+
return {
|
1235
|
+
tr: node => ({
|
1236
|
+
conversion: convertTableRowElement,
|
1237
|
+
priority: 0
|
1238
|
+
})
|
1239
|
+
};
|
1240
|
+
}
|
1241
|
+
|
891
1242
|
constructor(height, key) {
|
892
1243
|
super(key);
|
893
1244
|
this.__height = height;
|
@@ -923,6 +1274,11 @@ class TableRowNode extends lexical.GridRowNode {
|
|
923
1274
|
}
|
924
1275
|
|
925
1276
|
}
|
1277
|
+
function convertTableRowElement(domNode) {
|
1278
|
+
return {
|
1279
|
+
node: $createTableRowNode()
|
1280
|
+
};
|
1281
|
+
}
|
926
1282
|
function $createTableRowNode(height) {
|
927
1283
|
return new TableRowNode(height);
|
928
1284
|
}
|
@@ -1129,6 +1485,16 @@ function $deleteTableColumn(tableNode, targetIndex) {
|
|
1129
1485
|
return tableNode;
|
1130
1486
|
}
|
1131
1487
|
|
1488
|
+
/**
|
1489
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
1490
|
+
*
|
1491
|
+
* This source code is licensed under the MIT license found in the
|
1492
|
+
* LICENSE file in the root directory of this source tree.
|
1493
|
+
*
|
1494
|
+
*
|
1495
|
+
*/
|
1496
|
+
const INSERT_TABLE_COMMAND = lexical.createCommand();
|
1497
|
+
|
1132
1498
|
exports.$createTableCellNode = $createTableCellNode;
|
1133
1499
|
exports.$createTableNode = $createTableNode;
|
1134
1500
|
exports.$createTableNodeWithDimensions = $createTableNodeWithDimensions;
|
@@ -1146,6 +1512,7 @@ exports.$isTableCellNode = $isTableCellNode;
|
|
1146
1512
|
exports.$isTableNode = $isTableNode;
|
1147
1513
|
exports.$isTableRowNode = $isTableRowNode;
|
1148
1514
|
exports.$removeTableRowAtIndex = $removeTableRowAtIndex;
|
1515
|
+
exports.INSERT_TABLE_COMMAND = INSERT_TABLE_COMMAND;
|
1149
1516
|
exports.TableCellHeaderStates = TableCellHeaderStates;
|
1150
1517
|
exports.TableCellNode = TableCellNode;
|
1151
1518
|
exports.TableNode = TableNode;
|
@@ -0,0 +1,246 @@
|
|
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
|
+
* @flow strict
|
8
|
+
*/
|
9
|
+
|
10
|
+
import type {
|
11
|
+
EditorConfig,
|
12
|
+
LexicalNode,
|
13
|
+
NodeKey,
|
14
|
+
ParagraphNode,
|
15
|
+
RangeSelection,
|
16
|
+
LexicalEditor,
|
17
|
+
TextFormatType,
|
18
|
+
LexicalCommand,
|
19
|
+
} from 'lexical';
|
20
|
+
|
21
|
+
import {ElementNode} from 'lexical';
|
22
|
+
|
23
|
+
/**
|
24
|
+
* LexicalTableCellNode
|
25
|
+
*/
|
26
|
+
|
27
|
+
export const TableCellHeaderStates = {
|
28
|
+
NO_STATUS: 0,
|
29
|
+
ROW: 1,
|
30
|
+
COLUMN: 2,
|
31
|
+
BOTH: 3,
|
32
|
+
};
|
33
|
+
|
34
|
+
export type TableCellHeaderState = $Values<typeof TableCellHeaderStates>;
|
35
|
+
|
36
|
+
declare export class TableCellNode extends ElementNode {
|
37
|
+
static getType(): string;
|
38
|
+
static clone(node: TableCellNode): TableCellNode;
|
39
|
+
constructor(
|
40
|
+
headerState?: TableCellHeaderState,
|
41
|
+
colSpan?: number,
|
42
|
+
width?: ?number,
|
43
|
+
key?: NodeKey,
|
44
|
+
): void;
|
45
|
+
__headerState: TableCellHeaderState;
|
46
|
+
createDOM<EditorContext>(config: EditorConfig<EditorContext>): HTMLElement;
|
47
|
+
updateDOM(prevNode: TableCellNode, dom: HTMLElement): boolean;
|
48
|
+
insertNewAfter(
|
49
|
+
selection: RangeSelection,
|
50
|
+
): null | ParagraphNode | TableCellNode;
|
51
|
+
canInsertTab(): true;
|
52
|
+
collapseAtStart(): true;
|
53
|
+
getTag(): string;
|
54
|
+
setHeaderStyles(headerState: TableCellHeaderState): TableCellHeaderState;
|
55
|
+
getHeaderStyles(): TableCellHeaderState;
|
56
|
+
setWidth(width: number): ?number;
|
57
|
+
getWidth(): ?number;
|
58
|
+
toggleHeaderStyle(headerState: TableCellHeaderState): TableCellNode;
|
59
|
+
hasHeader(): boolean;
|
60
|
+
updateDOM(prevNode: TableCellNode): boolean;
|
61
|
+
collapseAtStart(): true;
|
62
|
+
canBeEmpty(): false;
|
63
|
+
}
|
64
|
+
declare export function $createTableCellNode(
|
65
|
+
headerState: TableCellHeaderState,
|
66
|
+
colSpan?: number,
|
67
|
+
width?: ?number,
|
68
|
+
): TableCellNode;
|
69
|
+
declare export function $isTableCellNode(
|
70
|
+
node: ?LexicalNode,
|
71
|
+
): boolean %checks(node instanceof TableCellNode);
|
72
|
+
|
73
|
+
/**
|
74
|
+
* LexicalTableNode
|
75
|
+
*/
|
76
|
+
|
77
|
+
declare export class TableNode extends ElementNode {
|
78
|
+
static getType(): string;
|
79
|
+
static clone(node: TableNode): TableNode;
|
80
|
+
constructor(grid: ?Grid, key?: NodeKey): void;
|
81
|
+
createDOM<EditorContext>(config: EditorConfig<EditorContext>): HTMLElement;
|
82
|
+
updateDOM(prevNode: TableNode, dom: HTMLElement): boolean;
|
83
|
+
insertNewAfter(selection: RangeSelection): null | ParagraphNode | TableNode;
|
84
|
+
canInsertTab(): true;
|
85
|
+
collapseAtStart(): true;
|
86
|
+
getCordsFromCellNode(
|
87
|
+
tableCellNode: TableCellNode,
|
88
|
+
grid: Grid,
|
89
|
+
): {x: number, y: number};
|
90
|
+
getCellFromCords(x: number, y: number, grid: Grid): ?Cell;
|
91
|
+
getCellFromCordsOrThrow(x: number, y: number, grid: Grid): Cell;
|
92
|
+
getCellNodeFromCords(x: number, y: number, grid: Grid): ?TableCellNode;
|
93
|
+
getCellNodeFromCordsOrThrow(x: number, y: number, grid: Grid): TableCellNode;
|
94
|
+
setGrid(grid: ?Grid): TableNode;
|
95
|
+
getGrid(): ?Grid;
|
96
|
+
canSelectBefore(): true;
|
97
|
+
}
|
98
|
+
declare export function $createTableNode(): TableNode;
|
99
|
+
declare export function $isTableNode(
|
100
|
+
node: ?LexicalNode,
|
101
|
+
): boolean %checks(node instanceof TableNode);
|
102
|
+
|
103
|
+
/**
|
104
|
+
* LexicalTableRowNode
|
105
|
+
*/
|
106
|
+
|
107
|
+
declare export class TableRowNode extends ElementNode {
|
108
|
+
static getType(): string;
|
109
|
+
static clone(node: TableRowNode): TableRowNode;
|
110
|
+
constructor(height?: ?number, key?: NodeKey): void;
|
111
|
+
createDOM<EditorContext>(config: EditorConfig<EditorContext>): HTMLElement;
|
112
|
+
updateDOM(prevNode: TableRowNode, dom: HTMLElement): boolean;
|
113
|
+
setHeight(height: number): ?number;
|
114
|
+
getHeight(): ?number;
|
115
|
+
insertNewAfter(
|
116
|
+
selection: RangeSelection,
|
117
|
+
): null | ParagraphNode | TableRowNode;
|
118
|
+
canInsertTab(): true;
|
119
|
+
collapseAtStart(): true;
|
120
|
+
}
|
121
|
+
declare export function $createTableRowNode(): TableRowNode;
|
122
|
+
declare export function $isTableRowNode(
|
123
|
+
node: ?LexicalNode,
|
124
|
+
): boolean %checks(node instanceof TableRowNode);
|
125
|
+
|
126
|
+
/**
|
127
|
+
* LexicalTableSelectionHelpers
|
128
|
+
*/
|
129
|
+
|
130
|
+
export type Cell = {
|
131
|
+
elem: HTMLElement,
|
132
|
+
highlighted: boolean,
|
133
|
+
x: number,
|
134
|
+
y: number,
|
135
|
+
};
|
136
|
+
|
137
|
+
export type Cells = Array<Array<Cell>>;
|
138
|
+
|
139
|
+
export type Grid = {
|
140
|
+
cells: Cells,
|
141
|
+
columns: number,
|
142
|
+
rows: number,
|
143
|
+
};
|
144
|
+
|
145
|
+
declare export function applyTableHandlers(
|
146
|
+
tableNode: TableNode,
|
147
|
+
tableElement: HTMLElement,
|
148
|
+
editor: LexicalEditor,
|
149
|
+
): TableSelection;
|
150
|
+
|
151
|
+
declare export function $getElementGridForTableNode(
|
152
|
+
editor: LexicalEditor,
|
153
|
+
tableNode: TableNode,
|
154
|
+
): Grid;
|
155
|
+
|
156
|
+
declare export function getTableSelectionFromTableElement(
|
157
|
+
tableElement: HTMLElement,
|
158
|
+
): TableSelection;
|
159
|
+
|
160
|
+
declare export function getCellFromTarget(node: Node): Cell | null;
|
161
|
+
|
162
|
+
/**
|
163
|
+
* LexicalTableUtils
|
164
|
+
*/
|
165
|
+
|
166
|
+
declare export function $createTableNodeWithDimensions(
|
167
|
+
rowCount: number,
|
168
|
+
columnCount: number,
|
169
|
+
includeHeaders?: boolean,
|
170
|
+
): TableNode;
|
171
|
+
|
172
|
+
declare export function $getTableCellNodeFromLexicalNode(
|
173
|
+
startingNode: LexicalNode,
|
174
|
+
): TableCellNode | null;
|
175
|
+
|
176
|
+
declare export function $getTableRowNodeFromTableCellNodeOrThrow(
|
177
|
+
startingNode: LexicalNode,
|
178
|
+
): TableRowNode;
|
179
|
+
|
180
|
+
declare export function $getTableNodeFromLexicalNodeOrThrow(
|
181
|
+
startingNode: LexicalNode,
|
182
|
+
): TableNode;
|
183
|
+
|
184
|
+
declare export function $getTableRowIndexFromTableCellNode(
|
185
|
+
tableCellNode: TableCellNode,
|
186
|
+
): number;
|
187
|
+
|
188
|
+
declare export function $getTableColumnIndexFromTableCellNode(
|
189
|
+
tableCellNode: TableCellNode,
|
190
|
+
): number;
|
191
|
+
|
192
|
+
declare export function $removeTableRowAtIndex(
|
193
|
+
tableNode: TableNode,
|
194
|
+
indexToDelete: number,
|
195
|
+
): TableNode;
|
196
|
+
|
197
|
+
declare export function $insertTableRow(
|
198
|
+
tableNode: TableNode,
|
199
|
+
targetIndex: number,
|
200
|
+
shouldInsertAfter?: boolean,
|
201
|
+
rowCount: number,
|
202
|
+
grid: Grid,
|
203
|
+
): TableNode;
|
204
|
+
|
205
|
+
declare export function $insertTableColumn(
|
206
|
+
tableNode: TableNode,
|
207
|
+
targetIndex: number,
|
208
|
+
shouldInsertAfter?: boolean,
|
209
|
+
columnCount: number,
|
210
|
+
): TableNode;
|
211
|
+
|
212
|
+
declare export function $deleteTableColumn(
|
213
|
+
tableNode: TableNode,
|
214
|
+
targetIndex: number,
|
215
|
+
): TableNode;
|
216
|
+
|
217
|
+
/**
|
218
|
+
* LexicalTableSelection.js
|
219
|
+
*/
|
220
|
+
declare export class TableSelection {
|
221
|
+
currentX: number;
|
222
|
+
currentY: number;
|
223
|
+
listenersToRemove: Set<() => void>;
|
224
|
+
domListeners: Set<() => void>;
|
225
|
+
grid: Grid;
|
226
|
+
isHighlightingCells: boolean;
|
227
|
+
isMouseDown: boolean;
|
228
|
+
startX: number;
|
229
|
+
startY: number;
|
230
|
+
nodeKey: string;
|
231
|
+
editor: LexicalEditor;
|
232
|
+
constructor(editor: LexicalEditor, nodeKey: string): void;
|
233
|
+
getGrid(): Grid;
|
234
|
+
removeListeners(): void;
|
235
|
+
trackTableGrid(): void;
|
236
|
+
clearHighlight(): void;
|
237
|
+
adjustFocusCellForSelection(cell: Cell): void;
|
238
|
+
setAnchorCellForSelection(cell: Cell): void;
|
239
|
+
formatCells(type: TextFormatType): void;
|
240
|
+
clearText(): void;
|
241
|
+
}
|
242
|
+
|
243
|
+
declare export var INSERT_TABLE_COMMAND: LexicalCommand<{
|
244
|
+
rows: string,
|
245
|
+
columns: string,
|
246
|
+
}>;
|
package/LexicalTable.prod.js
CHANGED
@@ -4,35 +4,45 @@
|
|
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
|
-
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){const b=this.getWritable();b.__headerState=(b.__headerState&a)===a?b.__headerState-a:b.__headerState+a;b.__headerState=b.__headerState;return b}hasHeaderState(a){return(this.getHeaderStyles()&
|
10
|
-
this.__headerState||a.__width!==this.__width}collapseAtStart(){return!0}canBeEmpty(){return!1}}function
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
this.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
function
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
exports.$
|
31
|
-
const
|
32
|
-
exports.$
|
33
|
-
exports
|
34
|
-
0)
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
7
|
+
var g=require("lexical"),n=require("@lexical/utils");const q={NO_STATUS:0,ROW:1,COLUMN:2,BOTH:3};
|
8
|
+
class r extends g.GridCellNode{static getType(){return"tablecell"}static clone(a){return new r(a.__headerState,a.__colSpan,a.__width,a.__key)}static convertDOM(){return{td:()=>({conversion:u,priority:0}),th:()=>({conversion:u,priority:0})}}constructor(a=q.NO_STATUS,b=1,f,h){super(b,h);this.__headerState=a;this.__width=f}createDOM(a){const b=document.createElement(this.getTag());this.__width&&(b.style.width=`${this.__width}px`);n.addClassNamesToElement(b,a.theme.tableCell,this.hasHeader()&&a.theme.tableCellHeader);
|
9
|
+
return b}getTag(){return this.hasHeader()?"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){const b=this.getWritable();b.__headerState=(b.__headerState&a)===a?b.__headerState-a:b.__headerState+a;b.__headerState=b.__headerState;return b}hasHeaderState(a){return(this.getHeaderStyles()&
|
10
|
+
a)===a}hasHeader(){return this.getLatest().__headerState!==q.NO_STATUS}updateDOM(a){return a.__headerState!==this.__headerState||a.__width!==this.__width}collapseAtStart(){return!0}canBeEmpty(){return!1}}function u(a){a=a.nodeName.toLowerCase();return{node:v("th"===a?q.ROW:q.NO_STATUS),forChild:(b,f)=>{if(w(f)&&!g.$isElementNode(b)){f=g.$createParagraphNode();if(g.$isLineBreakNode(b)&&"\n"===b.getTextContent())return null;f.append(b);return f}return b}}}function v(a,b=1,f){return new r(a,b,f)}
|
11
|
+
function w(a){return a instanceof r}function x(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.");}const y=document.createElement("style");y.appendChild(document.createTextNode("::selection{background-color: transparent}"));
|
12
|
+
class z{constructor(a,b){this.isHighlightingCells=!1;this.currentY=this.currentX=this.startY=this.startX=-1;this.listenersToRemove=new Set;this.tableNodeKey=b;this.editor=a;this.grid={cells:[],columns:0,rows:0};this.focusCell=this.anchorCell=this.focusCellNodeKey=this.anchorCellNodeKey=this.gridSelection=null;this.trackTableGrid()}getGrid(){return this.grid}removeListeners(){Array.from(this.listenersToRemove).forEach(a=>a())}trackTableGrid(){const a=new MutationObserver(b=>{this.editor.update(()=>
|
13
|
+
{var f=!1;for(let h=0;h<b.length;h++){const c=b[h].target.nodeName;if("TABLE"===c||"TR"===c){f=!0;break}}if(f){f=this.editor.getElementByKey(this.tableNodeKey);if(!f)throw Error("Expected to find TableElement in DOM");this.grid=B(f)}})});this.editor.update(()=>{const b=this.editor.getElementByKey(this.tableNodeKey);if(!b)throw Error("Expected to find TableElement in DOM");this.grid=B(b);a.observe(b,{childList:!0,subtree:!0})})}clearHighlight(){this.editor.update(()=>{var a=g.$getNodeByKey(this.tableNodeKey);
|
14
|
+
if(!C(a))throw Error("Expected TableNode.");a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a=B(a);this.isHighlightingCells=!1;this.currentY=this.currentX=this.startY=this.startX=-1;this.focusCell=this.anchorCell=this.focusCellNodeKey=this.anchorCellNodeKey=this.gridSelection=null;D(a,null);g.$setSelection(null);this.editor.dispatchCommand(g.SELECTION_CHANGE_COMMAND);a=y.parentNode;null!=a&&a.removeChild(y)})}adjustFocusCellForSelection(a,
|
15
|
+
b=!1){this.editor.update(()=>{var f=g.$getNodeByKey(this.tableNodeKey);if(!C(f))throw Error("Expected TableNode.");if(!this.editor.getElementByKey(this.tableNodeKey))throw Error("Expected to find TableElement in DOM");f=a.x;const h=a.y;this.focusCell=a;const c=window.getSelection();null!==this.anchorCell&&c.setBaseAndExtent(this.anchorCell.elem,0,a.elem,0);if(!this.isHighlightingCells&&(this.startX!==f||this.startY!==h||b))this.isHighlightingCells=!0,document.body&&document.body.appendChild(y);else if(f===
|
16
|
+
this.currentX&&h===this.currentY)return;this.currentX=f;this.currentY=h;this.isHighlightingCells&&(f=g.$getNearestNodeFromDOMNode(a.elem),null!=this.gridSelection&&null!=this.anchorCellNodeKey&&w(f)&&(f=f.getKey(),this.gridSelection=g.$createGridSelection(),this.focusCellNodeKey=f,this.gridSelection.set(this.tableNodeKey,this.anchorCellNodeKey,this.focusCellNodeKey),g.$setSelection(this.gridSelection),this.editor.dispatchCommand(g.SELECTION_CHANGE_COMMAND),D(this.grid,this.gridSelection)))})}setAnchorCellForSelection(a){this.editor.update(()=>
|
17
|
+
{this.anchorCell=a;this.startX=a.x;this.startY=a.y;window.getSelection().setBaseAndExtent(a.elem,0,a.elem,0);var b=g.$getNearestNodeFromDOMNode(a.elem);w(b)&&(b=b.getKey(),this.gridSelection=g.$createGridSelection(),this.anchorCellNodeKey=b)})}formatCells(a){this.editor.update(()=>{const b=g.$getSelection();g.$isGridSelection(b)||x(79);const f=g.$createRangeSelection(),h=f.anchor,c=f.focus;b.getNodes().forEach(k=>{w(k)&&0!==k.getTextContentSize()&&(h.set(k.getKey(),0,"element"),c.set(k.getKey(),k.getChildrenSize(),
|
18
|
+
"element"),f.formatText(a))});g.$setSelection(b);this.editor.dispatchCommand(g.SELECTION_CHANGE_COMMAND)})}clearText(){this.editor.update(()=>{const a=g.$getNodeByKey(this.tableNodeKey);if(!C(a))throw Error("Expected TableNode.");var b=g.$getSelection();g.$isGridSelection(b)||x(79);b=b.getNodes().filter(w);b.length===this.grid.columns*this.grid.rows?(a.selectPrevious(),a.remove(),this.clearHighlight()):(b.forEach(f=>{if(g.$isElementNode(f)){const h=g.$createParagraphNode(),c=g.$createTextNode();h.append(c);
|
19
|
+
f.append(h);f.getChildren().forEach(k=>{k!==h&&k.remove()})}}),D(this.grid,null),g.$setSelection(null),this.editor.dispatchCommand(g.SELECTION_CHANGE_COMMAND))})}}function E(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}
|
20
|
+
function B(a){const b=[],f={cells:b,columns:0,rows:0};var h=a.firstChild;let c=a=0;for(b.length=0;null!=h;){var k=h.nodeName;if("TD"===k||"TH"===k)k={elem:h,highlighted:!1,x:a,y:c},h._cell=k,void 0===b[c]&&(b[c]=[]),b[c][a]=k;else if(k=h.firstChild,null!=k){h=k;continue}k=h.nextSibling;if(null!=k)a++,h=k;else if(k=h.parentNode,null!=k){h=k.nextSibling;if(null==h)break;c++;a=0}}f.columns=a+1;f.rows=c+1;return f}
|
21
|
+
function D(a,b){const f=[];({cells:a}=a);b=new Set(b?b.getNodes():[]);for(let h=0;h<a.length;h++){const c=a[h];for(let k=0;k<c.length;k++){const p=c[k],d=p.elem.style,e=g.$getNearestNodeFromDOMNode(p.elem);e&&b.has(e)?(p.highlighted=!0,d.setProperty("background-color","rgb(163, 187, 255)"),d.setProperty("caret-color","transparent"),f.push(p)):(p.highlighted=!1,d.removeProperty("background-color"),d.removeProperty("caret-color"),p.elem.getAttribute("style")||p.elem.removeAttribute("style"))}}return f}
|
22
|
+
const G=(a,b,f,h,c)=>{switch(c){case "backward":case "forward":return c="forward"===c,f!==(c?a.grid.columns-1:0)?F(b.getCellNodeFromCordsOrThrow(f+(c?1:-1),h,a.grid)):h!==(c?a.grid.rows-1:0)?F(b.getCellNodeFromCordsOrThrow(c?0:a.grid.columns-1,h+(c?1:-1),a.grid)):c?b.selectNext():b.selectPrevious(),!0;case "up":return 0!==h?F(b.getCellNodeFromCordsOrThrow(f,h-1,a.grid)):b.selectPrevious(),!0;case "down":return h!==a.grid.rows-1?F(b.getCellNodeFromCordsOrThrow(f,h+1,a.grid)):b.selectNext(),!0}return!1},
|
23
|
+
H=(a,b,f,h,c)=>{switch(c){case "backward":case "forward":return c="forward"===c,f!==(c?a.grid.columns-1:0)&&a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(f+(c?1:-1),h,a.grid)),!0;case "up":if(0!==h)return a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(f,h-1,a.grid)),!0;break;case "down":if(h!==a.grid.rows-1)return a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(f,h+1,a.grid)),!0}return!1};
|
24
|
+
function F(a){const b=a.getChildren().find(f=>g.$isParagraphNode(f));g.$isParagraphNode(b)?b.selectEnd():a.selectEnd()}
|
25
|
+
class I extends g.GridNode{static getType(){return"table"}static clone(a){return new I(a.__key)}static convertDOM(){return{table:()=>({conversion:N,priority:0})}}constructor(a){super(a)}createDOM(a){const b=document.createElement("table");n.addClassNamesToElement(b,a.theme.table);return b}updateDOM(){return!1}canExtractContents(){return!1}canBeEmpty(){return!1}getCordsFromCellNode(a,b){b||x(55);const {rows:f,cells:h}=b;for(b=0;b<f;b++){var c=h[b];if(null==c)throw Error(`Row not found at y:${b}`);
|
26
|
+
c=c.findIndex(({elem:k})=>g.$getNearestNodeFromDOMNode(k)===a);if(-1!==c)return{x:c,y:b}}throw Error("Cell not found in table.");}getCellFromCords(a,b,f){f||x(55);({cells:f}=f);b=f[b];if(null==b)return null;a=b[a];return null==a?null:a}getCellFromCordsOrThrow(a,b,f){a=this.getCellFromCords(a,b,f);if(!a)throw Error("Cell not found at cords.");return a}getCellNodeFromCords(a,b,f){a=this.getCellFromCords(a,b,f);if(null==a)return null;a=g.$getNearestNodeFromDOMNode(a.elem);return w(a)?a:null}getCellNodeFromCordsOrThrow(a,
|
27
|
+
b,f){a=this.getCellNodeFromCords(a,b,f);if(!a)throw Error("Node at cords not TableCellNode.");return a}canSelectBefore(){return!0}}function N(){return{node:O()}}function O(){return new I}function C(a){return a instanceof I}
|
28
|
+
class P extends g.GridRowNode{static getType(){return"tablerow"}static clone(a){return new P(a.__height,a.__key)}static convertDOM(){return{tr:()=>({conversion:Q,priority:0})}}constructor(a,b){super(b);this.__height=a}createDOM(a){const b=document.createElement("tr");this.__height&&(b.style.height=`${this.__height}px`);n.addClassNamesToElement(b,a.theme.tableRow);return b}setHeight(a){this.getWritable().__height=a;return this.__height}getHeight(){return this.getLatest().__height}updateDOM(a){return a.__height!==
|
29
|
+
this.__height}canBeEmpty(){return!1}}function Q(){return{node:R()}}function R(a){return new P(a)}function S(a){return a instanceof P}function T(a){a=n.$findMatchingParent(a,b=>S(b));if(S(a))return a;throw Error("Expected table cell to be inside of table row.");}function U(a){a=n.$findMatchingParent(a,b=>C(b));if(C(a))return a;throw Error("Expected table cell to be inside of table.");}const V=g.createCommand();exports.$createTableCellNode=v;exports.$createTableNode=O;
|
30
|
+
exports.$createTableNodeWithDimensions=function(a,b,f=!0){const h=O();for(let k=0;k<a;k++){const p=R();for(let d=0;d<b;d++){var c=q.NO_STATUS;f&&(0===k&&(c|=q.ROW),0===d&&(c|=q.COLUMN));c=v(c);const e=g.$createParagraphNode();e.append(g.$createTextNode());c.append(e);p.append(c)}h.append(p)}return h};exports.$createTableRowNode=R;
|
31
|
+
exports.$deleteTableColumn=function(a,b){const f=a.getChildren();for(let c=0;c<f.length;c++){var h=f[c];if(S(h)){h=h.getChildren();if(b>=h.length||0>b)throw Error("Table column target index out of range");h[b].remove()}}return a};exports.$getElementGridForTableNode=function(a,b){a=a.getElementByKey(b.getKey());if(null==a)throw Error("Table Element Not Found");return B(a)};exports.$getTableCellNodeFromLexicalNode=function(a){a=n.$findMatchingParent(a,b=>w(b));return w(a)?a:null};
|
32
|
+
exports.$getTableColumnIndexFromTableCellNode=function(a){return T(a).getChildren().findIndex(b=>b.is(a))};exports.$getTableNodeFromLexicalNodeOrThrow=U;exports.$getTableRowIndexFromTableCellNode=function(a){const b=T(a);return U(b).getChildren().findIndex(f=>f.is(b))};exports.$getTableRowNodeFromTableCellNodeOrThrow=T;
|
33
|
+
exports.$insertTableColumn=function(a,b,f=!0,h){const c=a.getChildren();for(let d=0;d<c.length;d++){const e=c[d];if(S(e))for(let l=0;l<h;l++){var k=q.NO_STATUS;0===d&&(k|=q.ROW);k=v(k);k.append(g.$createParagraphNode());var p=e.getChildren();if(b>=p.length||0>b)throw Error("Table column target index out of range");p=p[b];f?p.insertAfter(k):p.insertBefore(k)}}return a};
|
34
|
+
exports.$insertTableRow=function(a,b,f=!0,h,c){var k=a.getChildren();if(b>=k.length||0>b)throw Error("Table row target index out of range");b=k[b];if(S(b))for(k=0;k<h;k++){const e=b.getChildren(),l=e.length,m=R();for(let t=0;t<l;t++){var p=e[t];w(p)||x(73);var d=c;const A=U(p),{x:J,y:K}=A.getCordsFromCellNode(p,d);p={above:A.getCellNodeFromCords(J,K-1,d),below:A.getCellNodeFromCords(J,K+1,d),left:A.getCellNodeFromCords(J-1,K,d),right:A.getCellNodeFromCords(J+1,K,d)};const {above:L,below:M}=p;p=q.NO_STATUS;
|
35
|
+
d=L&&L.getWidth()||M&&M.getWidth()||null;if(L&&L.hasHeaderState(q.COLUMN)||M&&M.hasHeaderState(q.COLUMN))p|=q.COLUMN;p=v(p,1,d);p.append(g.$createParagraphNode());m.append(p)}f?b.insertAfter(m):b.insertBefore(m)}else throw Error("Row before insertion index does not exist.");return a};exports.$isTableCellNode=w;exports.$isTableNode=C;exports.$isTableRowNode=S;
|
36
|
+
exports.$removeTableRowAtIndex=function(a,b){const f=a.getChildren();if(b>=f.length||0>b)throw Error("Expected table cell to be inside of table row.");f[b].remove();return a};exports.INSERT_TABLE_COMMAND=V;exports.TableCellHeaderStates=q;exports.TableCellNode=r;exports.TableNode=I;exports.TableRowNode=P;exports.TableSelection=z;
|
37
|
+
exports.applyTableHandlers=function(a,b,f){const h=f.getRootElement();if(null===h)throw Error("No root element.");const c=new z(f,a.getKey());b.__lexicalTableSelection=c;let k=!1;b.addEventListener("dblclick",d=>{const e=E(d.target);null!==e&&(d.preventDefault(),d.stopImmediatePropagation(),d.stopPropagation(),c.setAnchorCellForSelection(e),c.adjustFocusCellForSelection(e,!0),k=!1)});b.addEventListener("mousedown",d=>{setTimeout(()=>{if(0===d.button){var e=E(d.target);null!==e&&(k=!0,c.setAnchorCellForSelection(e),
|
38
|
+
document.addEventListener("mouseup",()=>{k=!1},{capture:!0,once:!0}))}},0)});b.addEventListener("mousemove",d=>{if(k){const e=E(d.target);if(null!==e){const l=e.x,m=e.y;k&&(c.startX!==l||c.startY!==m||c.isHighlightingCells)&&(d.preventDefault(),k=!0,c.adjustFocusCellForSelection(e))}}});b.addEventListener("mouseup",()=>{k&&(k=!1)});b.addEventListener("mouseleave",()=>{});const p=d=>{0===d.button&&f.update(()=>{const e=g.$getSelection();if(g.$isGridSelection(e)&&e.gridKey===c.tableNodeKey&&h.contains(d.target))return c.clearHighlight()})};
|
39
|
+
window.addEventListener("mousedown",p);c.listenersToRemove.add(()=>window.removeEventListener("mousedown",p));c.listenersToRemove.add(f.registerCommand(g.KEY_ARROW_DOWN_COMMAND,d=>{var e=g.$getSelection();if(g.$isRangeSelection(e)){if(e.isCollapsed()){var l=n.$findMatchingParent(e.anchor.getNode(),t=>w(t));if(!w(l))return!1;var m=a.getCordsFromCellNode(l,c.grid);e=n.$findMatchingParent(e.anchor.getNode(),t=>g.$isElementNode(t));if(null==e)throw Error("Expected BlockNode Parent");if((l=l.getLastChild())&&
|
40
|
+
e.isParentOf(l)||e===l||d.shiftKey)return d.preventDefault(),d.stopImmediatePropagation(),d.stopPropagation(),d.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(m.x,m.y,c.grid)),H(c,a,m.x,m.y,"down")):G(c,a,m.x,m.y,"down")}}else if(g.$isGridSelection(e)&&d.shiftKey){m=e.focus.getNode();if(!w(m))return!1;m=a.getCordsFromCellNode(m,c.grid);d.preventDefault();d.stopImmediatePropagation();d.stopPropagation();return H(c,a,m.x,m.y,"down")}return!1},4));c.listenersToRemove.add(f.registerCommand(g.KEY_ARROW_UP_COMMAND,
|
41
|
+
d=>{var e=g.$getSelection();if(g.$isRangeSelection(e)){if(e.isCollapsed()){var l=n.$findMatchingParent(e.anchor.getNode(),t=>w(t));if(!w(l))return!1;var m=a.getCordsFromCellNode(l,c.grid);e=n.$findMatchingParent(e.anchor.getNode(),t=>g.$isElementNode(t));if(null==e)throw Error("Expected BlockNode Parent");if((l=l.getLastChild())&&e.isParentOf(l)||e===l||d.shiftKey)return d.preventDefault(),d.stopImmediatePropagation(),d.stopPropagation(),d.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(m.x,
|
42
|
+
m.y,c.grid)),H(c,a,m.x,m.y,"up")):G(c,a,m.x,m.y,"up")}}else if(g.$isGridSelection(e)&&d.shiftKey){m=e.focus.getNode();if(!w(m))return!1;m=a.getCordsFromCellNode(m,c.grid);d.preventDefault();d.stopImmediatePropagation();d.stopPropagation();return H(c,a,m.x,m.y,"up")}return!1},4));c.listenersToRemove.add(f.registerCommand(g.KEY_ARROW_LEFT_COMMAND,d=>{var e=g.$getSelection();if(g.$isRangeSelection(e)){if(e.isCollapsed()){var l=n.$findMatchingParent(e.anchor.getNode(),m=>w(m));if(!w(l))return!1;l=a.getCordsFromCellNode(l,
|
43
|
+
c.grid);if(null==n.$findMatchingParent(e.anchor.getNode(),m=>g.$isElementNode(m)))throw Error("Expected BlockNode Parent");if(0===e.anchor.offset||d.shiftKey)return d.preventDefault(),d.stopImmediatePropagation(),d.stopPropagation(),d.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(l.x,l.y,c.grid)),H(c,a,l.x,l.y,"backward")):G(c,a,l.x,l.y,"backward")}}else if(g.$isGridSelection(e)&&d.shiftKey){e=e.focus.getNode();if(!w(e))return!1;e=a.getCordsFromCellNode(e,c.grid);d.preventDefault();
|
44
|
+
d.stopImmediatePropagation();d.stopPropagation();return H(c,a,e.x,e.y,"backward")}return!1},4));c.listenersToRemove.add(f.registerCommand(g.KEY_ARROW_RIGHT_COMMAND,d=>{var e=g.$getSelection();if(g.$isRangeSelection(e)){if(e.isCollapsed()){var l=n.$findMatchingParent(e.anchor.getNode(),m=>w(m));if(!w(l))return!1;l=a.getCordsFromCellNode(l,c.grid);if(null==n.$findMatchingParent(e.anchor.getNode(),m=>g.$isElementNode(m)))throw Error("Expected BlockNode Parent");if(e.anchor.offset===e.anchor.getNode().getTextContentSize()||
|
45
|
+
d.shiftKey)return d.preventDefault(),d.stopImmediatePropagation(),d.stopPropagation(),d.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(l.x,l.y,c.grid)),H(c,a,l.x,l.y,"forward")):G(c,a,l.x,l.y,"forward")}}else if(g.$isGridSelection(e)&&d.shiftKey){e=e.focus.getNode();if(!w(e))return!1;e=a.getCordsFromCellNode(e,c.grid);d.preventDefault();d.stopImmediatePropagation();d.stopPropagation();return H(c,a,e.x,e.y,"forward")}return!1},4));c.listenersToRemove.add(f.registerCommand(g.DELETE_CHARACTER_COMMAND,
|
46
|
+
()=>{const d=g.$getSelection();if(g.$isGridSelection(d))return c.clearText(),!0;if(g.$isRangeSelection(d)){const e=n.$findMatchingParent(d.anchor.getNode(),l=>w(l));if(!w(e))return!1;if(d.isCollapsed()&&0===d.anchor.offset&&0===d.anchor.getNode().getPreviousSiblings().length)return!0}return!1},4));c.listenersToRemove.add(f.registerCommand(g.KEY_BACKSPACE_COMMAND,d=>{const e=g.$getSelection();if(g.$isGridSelection(e))return d.preventDefault(),d.stopPropagation(),c.clearText(),!0;g.$isRangeSelection(e)&&
|
47
|
+
(d=n.$findMatchingParent(e.anchor.getNode(),l=>w(l)),w(d));return!1},4));c.listenersToRemove.add(f.registerCommand(g.FORMAT_TEXT_COMMAND,d=>{const e=g.$getSelection();if(g.$isGridSelection(e))return c.formatCells(d),!0;g.$isRangeSelection(e)&&(d=n.$findMatchingParent(e.anchor.getNode(),l=>w(l)),w(d));return!1},4));c.listenersToRemove.add(f.registerCommand(g.INSERT_TEXT_COMMAND,()=>{var d=g.$getSelection();g.$isGridSelection(d)?c.clearHighlight():g.$isRangeSelection(d)&&(d=n.$findMatchingParent(d.anchor.getNode(),
|
48
|
+
e=>w(e)),w(d));return!1},4));c.listenersToRemove.add(f.registerCommand(g.KEY_TAB_COMMAND,d=>{var e=g.$getSelection();if(g.$isRangeSelection(e)){const l=n.$findMatchingParent(e.anchor.getNode(),m=>w(m));if(!w(l))return!1;if(e.isCollapsed())return e=a.getCordsFromCellNode(l,c.grid),d.preventDefault(),G(c,a,e.x,e.y,d.shiftKey?"backward":"forward"),!0}return!1},4));return c};exports.getCellFromTarget=E;exports.getTableSelectionFromTableElement=function(a){return a.__lexicalTableSelection};
|
package/package.json
CHANGED
@@ -1,9 +1,5 @@
|
|
1
1
|
{
|
2
2
|
"name": "@lexical/table",
|
3
|
-
"author": {
|
4
|
-
"name": "Dominic Gannaway",
|
5
|
-
"email": "dg@domgan.com"
|
6
|
-
},
|
7
3
|
"description": "This package provides the Table feature for Lexical.",
|
8
4
|
"keywords": [
|
9
5
|
"lexical",
|
@@ -12,13 +8,13 @@
|
|
12
8
|
"table"
|
13
9
|
],
|
14
10
|
"license": "MIT",
|
15
|
-
"version": "0.1.
|
11
|
+
"version": "0.1.19",
|
16
12
|
"main": "LexicalTable.js",
|
17
13
|
"peerDependencies": {
|
18
|
-
"lexical": "0.1.
|
14
|
+
"lexical": "0.1.19"
|
19
15
|
},
|
20
16
|
"dependencies": {
|
21
|
-
"@lexical/utils": "0.1.
|
17
|
+
"@lexical/utils": "0.1.19"
|
22
18
|
},
|
23
19
|
"repository": {
|
24
20
|
"type": "git",
|