@lexical/table 0.2.8 → 0.3.1
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.d.ts +4 -10
- package/LexicalTable.dev.js +113 -115
- package/LexicalTable.js.flow +1 -0
- package/LexicalTable.prod.js +47 -48
- package/package.json +3 -3
package/LexicalTable.d.ts
CHANGED
@@ -4,7 +4,6 @@
|
|
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
|
-
*
|
8
7
|
*/
|
9
8
|
|
10
9
|
import type {
|
@@ -18,7 +17,6 @@ import type {
|
|
18
17
|
TextFormatType,
|
19
18
|
LexicalCommand,
|
20
19
|
} from 'lexical';
|
21
|
-
import {$Values} from 'utility-types';
|
22
20
|
|
23
21
|
export enum TableCellHeaderStates {
|
24
22
|
NO_STATUS = 0,
|
@@ -30,7 +28,6 @@ export enum TableCellHeaderStates {
|
|
30
28
|
/**
|
31
29
|
* LexicalTableCellNode
|
32
30
|
*/
|
33
|
-
|
34
31
|
export declare class TableCellNode extends ElementNode {
|
35
32
|
static getType(): string;
|
36
33
|
static clone(node: TableCellNode): TableCellNode;
|
@@ -71,7 +68,6 @@ export declare function $isTableCellNode(
|
|
71
68
|
/**
|
72
69
|
* LexicalTableNode
|
73
70
|
*/
|
74
|
-
|
75
71
|
export declare class TableNode extends ElementNode {
|
76
72
|
static getType(): string;
|
77
73
|
static clone(node: TableNode): TableNode;
|
@@ -115,16 +111,13 @@ declare function $isTableRowNode(node?: LexicalNode): node is TableRowNode;
|
|
115
111
|
/**
|
116
112
|
* LexicalTableSelectionHelpers
|
117
113
|
*/
|
118
|
-
|
119
114
|
export type Cell = {
|
120
115
|
elem: HTMLElement;
|
121
116
|
highlighted: boolean;
|
122
117
|
x: number;
|
123
118
|
y: number;
|
124
119
|
};
|
125
|
-
|
126
120
|
export type Cells = Array<Array<Cell>>;
|
127
|
-
|
128
121
|
export type Grid = {
|
129
122
|
cells: Cells;
|
130
123
|
columns: number;
|
@@ -228,8 +221,9 @@ export declare class TableSelection {
|
|
228
221
|
formatCells(type: TextFormatType): void;
|
229
222
|
clearText(): void;
|
230
223
|
}
|
231
|
-
|
232
|
-
export var INSERT_TABLE_COMMAND: LexicalCommand<{
|
233
|
-
rows: string;
|
224
|
+
export type InsertTableCommandPayload = Readonly<{
|
234
225
|
columns: string;
|
226
|
+
rows: string;
|
227
|
+
includeHeaders?: boolean;
|
235
228
|
}>;
|
229
|
+
export const INSERT_TABLE_COMMAND: LexicalCommand<InsertTableCommandPayload>;
|
package/LexicalTable.dev.js
CHANGED
@@ -9,12 +9,18 @@
|
|
9
9
|
var lexical = require('lexical');
|
10
10
|
var utils = require('@lexical/utils');
|
11
11
|
|
12
|
-
|
12
|
+
/**
|
13
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
14
|
+
*
|
15
|
+
* This source code is licensed under the MIT license found in the
|
16
|
+
* LICENSE file in the root directory of this source tree.
|
17
|
+
*
|
18
|
+
*/
|
13
19
|
const TableCellHeaderStates = {
|
14
|
-
|
15
|
-
ROW: 1,
|
20
|
+
BOTH: 3,
|
16
21
|
COLUMN: 2,
|
17
|
-
|
22
|
+
NO_STATUS: 0,
|
23
|
+
ROW: 1
|
18
24
|
};
|
19
25
|
class TableCellNode extends lexical.GridCellNode {
|
20
26
|
static getType() {
|
@@ -38,6 +44,10 @@ class TableCellNode extends lexical.GridCellNode {
|
|
38
44
|
};
|
39
45
|
}
|
40
46
|
|
47
|
+
static importJSON(serializedNode) {
|
48
|
+
return $createTableCellNode(serializedNode.headerState, serializedNode.colSpan, serializedNode.width);
|
49
|
+
}
|
50
|
+
|
41
51
|
constructor(headerState = TableCellHeaderStates.NO_STATUS, colSpan = 1, width, key) {
|
42
52
|
super(colSpan, key);
|
43
53
|
this.__headerState = headerState;
|
@@ -78,6 +88,15 @@ class TableCellNode extends lexical.GridCellNode {
|
|
78
88
|
};
|
79
89
|
}
|
80
90
|
|
91
|
+
exportJSON() {
|
92
|
+
return { ...super.exportJSON(),
|
93
|
+
colSpan: super.__colSpan,
|
94
|
+
headerState: this.__headerState,
|
95
|
+
type: 'tablecell',
|
96
|
+
width: this.getWidth()
|
97
|
+
};
|
98
|
+
}
|
99
|
+
|
81
100
|
getTag() {
|
82
101
|
return this.hasHeader() ? 'th' : 'td';
|
83
102
|
}
|
@@ -111,7 +130,6 @@ class TableCellNode extends lexical.GridCellNode {
|
|
111
130
|
self.__headerState += headerStateToToggle;
|
112
131
|
}
|
113
132
|
|
114
|
-
self.__headerState = self.__headerState;
|
115
133
|
return self;
|
116
134
|
}
|
117
135
|
|
@@ -144,7 +162,6 @@ function convertTableCellNodeElement(domNode) {
|
|
144
162
|
const nodeName = domNode.nodeName.toLowerCase();
|
145
163
|
const tableCellNode = $createTableCellNode(nodeName === 'th' ? TableCellHeaderStates.ROW : TableCellHeaderStates.NO_STATUS);
|
146
164
|
return {
|
147
|
-
node: tableCellNode,
|
148
165
|
forChild: (lexicalNode, parentLexicalNode) => {
|
149
166
|
if ($isTableCellNode(parentLexicalNode) && !lexical.$isElementNode(lexicalNode)) {
|
150
167
|
const paragraphNode = lexical.$createParagraphNode();
|
@@ -158,7 +175,8 @@ function convertTableCellNodeElement(domNode) {
|
|
158
175
|
}
|
159
176
|
|
160
177
|
return lexicalNode;
|
161
|
-
}
|
178
|
+
},
|
179
|
+
node: tableCellNode
|
162
180
|
};
|
163
181
|
}
|
164
182
|
function $createTableCellNode(headerState, colSpan = 1, width) {
|
@@ -174,7 +192,6 @@ function $isTableCellNode(node) {
|
|
174
192
|
* This source code is licensed under the MIT license found in the
|
175
193
|
* LICENSE file in the root directory of this source tree.
|
176
194
|
*
|
177
|
-
*
|
178
195
|
*/
|
179
196
|
class TableRowNode extends lexical.GridRowNode {
|
180
197
|
static getType() {
|
@@ -194,11 +211,22 @@ class TableRowNode extends lexical.GridRowNode {
|
|
194
211
|
};
|
195
212
|
}
|
196
213
|
|
214
|
+
static importJSON(serializedNode) {
|
215
|
+
return $createTableRowNode(serializedNode.height);
|
216
|
+
}
|
217
|
+
|
197
218
|
constructor(height, key) {
|
198
219
|
super(key);
|
199
220
|
this.__height = height;
|
200
221
|
}
|
201
222
|
|
223
|
+
exportJSON() {
|
224
|
+
return { ...super.exportJSON(),
|
225
|
+
type: 'tablerow',
|
226
|
+
version: 1
|
227
|
+
};
|
228
|
+
}
|
229
|
+
|
202
230
|
createDOM(config) {
|
203
231
|
const element = document.createElement('tr');
|
204
232
|
|
@@ -251,7 +279,6 @@ function $isTableRowNode(node) {
|
|
251
279
|
* This source code is licensed under the MIT license found in the
|
252
280
|
* LICENSE file in the root directory of this source tree.
|
253
281
|
*
|
254
|
-
*
|
255
282
|
*/
|
256
283
|
const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
|
257
284
|
|
@@ -261,7 +288,6 @@ const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !==
|
|
261
288
|
* This source code is licensed under the MIT license found in the
|
262
289
|
* LICENSE file in the root directory of this source tree.
|
263
290
|
*
|
264
|
-
*
|
265
291
|
*/
|
266
292
|
const getSelection = () => window.getSelection();
|
267
293
|
|
@@ -273,7 +299,6 @@ var getDOMSelection = getSelection;
|
|
273
299
|
* This source code is licensed under the MIT license found in the
|
274
300
|
* LICENSE file in the root directory of this source tree.
|
275
301
|
*
|
276
|
-
*
|
277
302
|
*/
|
278
303
|
|
279
304
|
if (CAN_USE_DOM) {
|
@@ -405,7 +430,7 @@ class TableSelection {
|
|
405
430
|
this.hasHijackedSelectionStyles = false;
|
406
431
|
$updateDOMForSelection(grid, null);
|
407
432
|
lexical.$setSelection(null);
|
408
|
-
this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND);
|
433
|
+
this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND, undefined);
|
409
434
|
this.enableHighlightStyle();
|
410
435
|
});
|
411
436
|
}
|
@@ -496,10 +521,9 @@ class TableSelection {
|
|
496
521
|
const focusNodeKey = focusTableCellNode.getKey();
|
497
522
|
this.gridSelection = lexical.$createGridSelection();
|
498
523
|
this.focusCellNodeKey = focusNodeKey;
|
499
|
-
this.gridSelection.set(this.tableNodeKey,
|
500
|
-
this.anchorCellNodeKey, this.focusCellNodeKey);
|
524
|
+
this.gridSelection.set(this.tableNodeKey, this.anchorCellNodeKey, this.focusCellNodeKey);
|
501
525
|
lexical.$setSelection(this.gridSelection);
|
502
|
-
this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND);
|
526
|
+
this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND, undefined);
|
503
527
|
$updateDOMForSelection(this.grid, this.gridSelection);
|
504
528
|
}
|
505
529
|
}
|
@@ -531,8 +555,7 @@ class TableSelection {
|
|
531
555
|
{
|
532
556
|
throw Error(`Expected grid selection`);
|
533
557
|
}
|
534
|
-
}
|
535
|
-
|
558
|
+
}
|
536
559
|
|
537
560
|
const formatSelection = lexical.$createRangeSelection();
|
538
561
|
const anchor = formatSelection.anchor;
|
@@ -545,7 +568,7 @@ class TableSelection {
|
|
545
568
|
}
|
546
569
|
});
|
547
570
|
lexical.$setSelection(selection);
|
548
|
-
this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND);
|
571
|
+
this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND, undefined);
|
549
572
|
});
|
550
573
|
}
|
551
574
|
|
@@ -590,7 +613,7 @@ class TableSelection {
|
|
590
613
|
});
|
591
614
|
$updateDOMForSelection(this.grid, null);
|
592
615
|
lexical.$setSelection(null);
|
593
|
-
this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND);
|
616
|
+
this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND, undefined);
|
594
617
|
});
|
595
618
|
}
|
596
619
|
|
@@ -602,7 +625,6 @@ class TableSelection {
|
|
602
625
|
* This source code is licensed under the MIT license found in the
|
603
626
|
* LICENSE file in the root directory of this source tree.
|
604
627
|
*
|
605
|
-
*
|
606
628
|
*/
|
607
629
|
const LEXICAL_ELEMENT_KEY = '__lexicalTableSelection';
|
608
630
|
function applyTableHandlers(tableNode, tableElement, editor) {
|
@@ -617,7 +639,6 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
617
639
|
let isMouseDown = false;
|
618
640
|
let isRangeSelectionHijacked = false;
|
619
641
|
tableElement.addEventListener('dblclick', event => {
|
620
|
-
// $FlowFixMe: event.target is always a Node on the DOM
|
621
642
|
const cell = getCellFromTarget(event.target);
|
622
643
|
|
623
644
|
if (cell !== null) {
|
@@ -634,8 +655,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
634
655
|
setTimeout(() => {
|
635
656
|
if (event.button !== 0) {
|
636
657
|
return;
|
637
|
-
}
|
638
|
-
|
658
|
+
}
|
639
659
|
|
640
660
|
const cell = getCellFromTarget(event.target);
|
641
661
|
|
@@ -659,7 +679,6 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
659
679
|
}
|
660
680
|
|
661
681
|
if (isMouseDown) {
|
662
|
-
// $FlowFixMe: event.target is always a Node on the DOM
|
663
682
|
const cell = getCellFromTarget(event.target);
|
664
683
|
|
665
684
|
if (cell !== null) {
|
@@ -674,13 +693,13 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
674
693
|
}
|
675
694
|
}
|
676
695
|
});
|
677
|
-
tableElement.addEventListener('mouseup',
|
696
|
+
tableElement.addEventListener('mouseup', () => {
|
678
697
|
if (isMouseDown) {
|
679
698
|
isMouseDown = false;
|
680
699
|
}
|
681
700
|
}); // Select entire table at this point, when grid selection is ready.
|
682
701
|
|
683
|
-
tableElement.addEventListener('mouseleave',
|
702
|
+
tableElement.addEventListener('mouseleave', () => {
|
684
703
|
if (isMouseDown) {
|
685
704
|
return;
|
686
705
|
}
|
@@ -705,20 +724,19 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
705
724
|
window.addEventListener('mousedown', mouseDownCallback);
|
706
725
|
tableSelection.listenersToRemove.add(() => window.removeEventListener('mousedown', mouseDownCallback));
|
707
726
|
|
708
|
-
const mouseUpCallback =
|
727
|
+
const mouseUpCallback = () => {
|
709
728
|
isMouseDown = false;
|
710
729
|
};
|
711
730
|
|
712
731
|
window.addEventListener('mouseup', mouseUpCallback);
|
713
732
|
tableSelection.listenersToRemove.add(() => window.removeEventListener('mouseup', mouseUpCallback));
|
714
|
-
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_DOWN_COMMAND,
|
733
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_DOWN_COMMAND, event => {
|
715
734
|
const selection = lexical.$getSelection();
|
716
735
|
|
717
736
|
if (!$isSelectionInTable(selection, tableNode)) {
|
718
737
|
return false;
|
719
738
|
}
|
720
739
|
|
721
|
-
const event = payload;
|
722
740
|
const direction = 'down';
|
723
741
|
|
724
742
|
if (lexical.$isRangeSelection(selection)) {
|
@@ -768,14 +786,13 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
768
786
|
|
769
787
|
return false;
|
770
788
|
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
771
|
-
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_UP_COMMAND,
|
789
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_UP_COMMAND, event => {
|
772
790
|
const selection = lexical.$getSelection();
|
773
791
|
|
774
792
|
if (!$isSelectionInTable(selection, tableNode)) {
|
775
793
|
return false;
|
776
794
|
}
|
777
795
|
|
778
|
-
const event = payload;
|
779
796
|
const direction = 'up';
|
780
797
|
|
781
798
|
if (lexical.$isRangeSelection(selection)) {
|
@@ -825,14 +842,13 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
825
842
|
|
826
843
|
return false;
|
827
844
|
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
828
|
-
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_LEFT_COMMAND,
|
845
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_LEFT_COMMAND, event => {
|
829
846
|
const selection = lexical.$getSelection();
|
830
847
|
|
831
848
|
if (!$isSelectionInTable(selection, tableNode)) {
|
832
849
|
return false;
|
833
850
|
}
|
834
851
|
|
835
|
-
const event = payload;
|
836
852
|
const direction = 'backward';
|
837
853
|
|
838
854
|
if (lexical.$isRangeSelection(selection)) {
|
@@ -879,14 +895,13 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
879
895
|
|
880
896
|
return false;
|
881
897
|
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
882
|
-
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_RIGHT_COMMAND,
|
898
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_RIGHT_COMMAND, event => {
|
883
899
|
const selection = lexical.$getSelection();
|
884
900
|
|
885
901
|
if (!$isSelectionInTable(selection, tableNode)) {
|
886
902
|
return false;
|
887
903
|
}
|
888
904
|
|
889
|
-
const event = payload;
|
890
905
|
const direction = 'forward';
|
891
906
|
|
892
907
|
if (lexical.$isRangeSelection(selection)) {
|
@@ -963,7 +978,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
963
978
|
|
964
979
|
return false;
|
965
980
|
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
966
|
-
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_BACKSPACE_COMMAND,
|
981
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_BACKSPACE_COMMAND, event => {
|
967
982
|
const selection = lexical.$getSelection();
|
968
983
|
|
969
984
|
if (!$isSelectionInTable(selection, tableNode)) {
|
@@ -971,7 +986,6 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
971
986
|
}
|
972
987
|
|
973
988
|
if (lexical.$isGridSelection(selection)) {
|
974
|
-
const event = payload;
|
975
989
|
event.preventDefault();
|
976
990
|
event.stopPropagation();
|
977
991
|
tableSelection.clearText();
|
@@ -1006,7 +1020,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
1006
1020
|
|
1007
1021
|
return false;
|
1008
1022
|
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
1009
|
-
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.
|
1023
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.CONTROLLED_TEXT_INSERTION_COMMAND, payload => {
|
1010
1024
|
const selection = lexical.$getSelection();
|
1011
1025
|
|
1012
1026
|
if (!$isSelectionInTable(selection, tableNode)) {
|
@@ -1026,7 +1040,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
1026
1040
|
|
1027
1041
|
return false;
|
1028
1042
|
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
1029
|
-
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_TAB_COMMAND,
|
1043
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_TAB_COMMAND, event => {
|
1030
1044
|
const selection = lexical.$getSelection();
|
1031
1045
|
|
1032
1046
|
if (!$isSelectionInTable(selection, tableNode)) {
|
@@ -1040,8 +1054,6 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
1040
1054
|
return false;
|
1041
1055
|
}
|
1042
1056
|
|
1043
|
-
const event = payload;
|
1044
|
-
|
1045
1057
|
if (selection.isCollapsed()) {
|
1046
1058
|
const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableSelection.grid);
|
1047
1059
|
event.preventDefault();
|
@@ -1098,11 +1110,9 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
1098
1110
|
return tableSelection;
|
1099
1111
|
}
|
1100
1112
|
function attachTableSelectionToTableElement(tableElement, tableSelection) {
|
1101
|
-
// $FlowFixMe
|
1102
1113
|
tableElement[LEXICAL_ELEMENT_KEY] = tableSelection;
|
1103
1114
|
}
|
1104
1115
|
function getTableSelectionFromTableElement(tableElement) {
|
1105
|
-
// $FlowFixMe
|
1106
1116
|
return tableElement[LEXICAL_ELEMENT_KEY];
|
1107
1117
|
}
|
1108
1118
|
function getCellFromTarget(node) {
|
@@ -1112,7 +1122,7 @@ function getCellFromTarget(node) {
|
|
1112
1122
|
const nodeName = currentNode.nodeName;
|
1113
1123
|
|
1114
1124
|
if (nodeName === 'TD' || nodeName === 'TH') {
|
1115
|
-
//
|
1125
|
+
// @ts-expect-error: internal field
|
1116
1126
|
const cell = currentNode._cell;
|
1117
1127
|
|
1118
1128
|
if (cell === undefined) {
|
@@ -1143,14 +1153,13 @@ function getTableGrid(tableElement) {
|
|
1143
1153
|
const nodeMame = currentNode.nodeName;
|
1144
1154
|
|
1145
1155
|
if (nodeMame === 'TD' || nodeMame === 'TH') {
|
1146
|
-
// $FlowFixMe: TD is always an HTMLElement
|
1147
1156
|
const elem = currentNode;
|
1148
1157
|
const cell = {
|
1149
1158
|
elem,
|
1150
1159
|
highlighted: false,
|
1151
1160
|
x,
|
1152
1161
|
y
|
1153
|
-
}; //
|
1162
|
+
}; // @ts-expect-error: internal field
|
1154
1163
|
|
1155
1164
|
currentNode._cell = cell;
|
1156
1165
|
|
@@ -1263,89 +1272,79 @@ function $removeHighlightStyleToTable(tableSelection) {
|
|
1263
1272
|
}
|
1264
1273
|
|
1265
1274
|
const selectGridNodeInDirection = (tableSelection, tableNode, x, y, direction) => {
|
1275
|
+
const isForward = direction === 'forward';
|
1276
|
+
|
1266
1277
|
switch (direction) {
|
1267
1278
|
case 'backward':
|
1268
1279
|
case 'forward':
|
1269
|
-
{
|
1270
|
-
|
1271
|
-
|
1272
|
-
if (
|
1273
|
-
selectTableCellNode(tableNode.getCellNodeFromCordsOrThrow(
|
1280
|
+
if (x !== (isForward ? tableSelection.grid.columns - 1 : 0)) {
|
1281
|
+
selectTableCellNode(tableNode.getCellNodeFromCordsOrThrow(x + (isForward ? 1 : -1), y, tableSelection.grid));
|
1282
|
+
} else {
|
1283
|
+
if (y !== (isForward ? tableSelection.grid.rows - 1 : 0)) {
|
1284
|
+
selectTableCellNode(tableNode.getCellNodeFromCordsOrThrow(isForward ? 0 : tableSelection.grid.columns - 1, y + (isForward ? 1 : -1), tableSelection.grid));
|
1285
|
+
} else if (!isForward) {
|
1286
|
+
tableNode.selectPrevious();
|
1274
1287
|
} else {
|
1275
|
-
|
1276
|
-
selectTableCellNode(tableNode.getCellNodeFromCordsOrThrow(isForward ? 0 : tableSelection.grid.columns - 1, y + (isForward ? 1 : -1), tableSelection.grid));
|
1277
|
-
} else if (!isForward) {
|
1278
|
-
tableNode.selectPrevious();
|
1279
|
-
} else {
|
1280
|
-
tableNode.selectNext();
|
1281
|
-
}
|
1288
|
+
tableNode.selectNext();
|
1282
1289
|
}
|
1283
|
-
|
1284
|
-
return true;
|
1285
1290
|
}
|
1286
1291
|
|
1287
|
-
|
1288
|
-
{
|
1289
|
-
if (y !== 0) {
|
1290
|
-
selectTableCellNode(tableNode.getCellNodeFromCordsOrThrow(x, y - 1, tableSelection.grid));
|
1291
|
-
} else {
|
1292
|
-
tableNode.selectPrevious();
|
1293
|
-
}
|
1292
|
+
return true;
|
1294
1293
|
|
1295
|
-
|
1294
|
+
case 'up':
|
1295
|
+
if (y !== 0) {
|
1296
|
+
selectTableCellNode(tableNode.getCellNodeFromCordsOrThrow(x, y - 1, tableSelection.grid));
|
1297
|
+
} else {
|
1298
|
+
tableNode.selectPrevious();
|
1296
1299
|
}
|
1297
1300
|
|
1298
|
-
|
1299
|
-
{
|
1300
|
-
if (y !== tableSelection.grid.rows - 1) {
|
1301
|
-
selectTableCellNode(tableNode.getCellNodeFromCordsOrThrow(x, y + 1, tableSelection.grid));
|
1302
|
-
} else {
|
1303
|
-
tableNode.selectNext();
|
1304
|
-
}
|
1301
|
+
return true;
|
1305
1302
|
|
1306
|
-
|
1303
|
+
case 'down':
|
1304
|
+
if (y !== tableSelection.grid.rows - 1) {
|
1305
|
+
selectTableCellNode(tableNode.getCellNodeFromCordsOrThrow(x, y + 1, tableSelection.grid));
|
1306
|
+
} else {
|
1307
|
+
tableNode.selectNext();
|
1307
1308
|
}
|
1308
|
-
}
|
1309
1309
|
|
1310
|
-
|
1310
|
+
return true;
|
1311
|
+
|
1312
|
+
default:
|
1313
|
+
return false;
|
1314
|
+
}
|
1311
1315
|
};
|
1312
1316
|
|
1313
1317
|
const adjustFocusNodeInDirection = (tableSelection, tableNode, x, y, direction) => {
|
1318
|
+
const isForward = direction === 'forward';
|
1319
|
+
|
1314
1320
|
switch (direction) {
|
1315
1321
|
case 'backward':
|
1316
1322
|
case 'forward':
|
1317
|
-
{
|
1318
|
-
|
1319
|
-
|
1320
|
-
if (x !== (isForward ? tableSelection.grid.columns - 1 : 0)) {
|
1321
|
-
tableSelection.adjustFocusCellForSelection(tableNode.getCellFromCordsOrThrow(x + (isForward ? 1 : -1), y, tableSelection.grid));
|
1322
|
-
}
|
1323
|
-
|
1324
|
-
return true;
|
1323
|
+
if (x !== (isForward ? tableSelection.grid.columns - 1 : 0)) {
|
1324
|
+
tableSelection.adjustFocusCellForSelection(tableNode.getCellFromCordsOrThrow(x + (isForward ? 1 : -1), y, tableSelection.grid));
|
1325
1325
|
}
|
1326
1326
|
|
1327
|
+
return true;
|
1328
|
+
|
1327
1329
|
case 'up':
|
1328
|
-
{
|
1329
|
-
|
1330
|
-
|
1331
|
-
|
1332
|
-
|
1333
|
-
return false;
|
1334
|
-
}
|
1330
|
+
if (y !== 0) {
|
1331
|
+
tableSelection.adjustFocusCellForSelection(tableNode.getCellFromCordsOrThrow(x, y - 1, tableSelection.grid));
|
1332
|
+
return true;
|
1333
|
+
} else {
|
1334
|
+
return false;
|
1335
1335
|
}
|
1336
1336
|
|
1337
1337
|
case 'down':
|
1338
|
-
{
|
1339
|
-
|
1340
|
-
|
1341
|
-
|
1342
|
-
|
1343
|
-
return false;
|
1344
|
-
}
|
1338
|
+
if (y !== tableSelection.grid.rows - 1) {
|
1339
|
+
tableSelection.adjustFocusCellForSelection(tableNode.getCellFromCordsOrThrow(x, y + 1, tableSelection.grid));
|
1340
|
+
return true;
|
1341
|
+
} else {
|
1342
|
+
return false;
|
1345
1343
|
}
|
1346
|
-
}
|
1347
1344
|
|
1348
|
-
|
1345
|
+
default:
|
1346
|
+
return false;
|
1347
|
+
}
|
1349
1348
|
};
|
1350
1349
|
|
1351
1350
|
function $isSelectionInTable(selection, tableNode) {
|
@@ -1374,7 +1373,6 @@ function selectTableCellNode(tableCell) {
|
|
1374
1373
|
* This source code is licensed under the MIT license found in the
|
1375
1374
|
* LICENSE file in the root directory of this source tree.
|
1376
1375
|
*
|
1377
|
-
*
|
1378
1376
|
*/
|
1379
1377
|
class TableNode extends lexical.GridNode {
|
1380
1378
|
static getType() {
|
@@ -1394,10 +1392,21 @@ class TableNode extends lexical.GridNode {
|
|
1394
1392
|
};
|
1395
1393
|
}
|
1396
1394
|
|
1395
|
+
static importJSON(serializedNode) {
|
1396
|
+
return $createTableNode();
|
1397
|
+
}
|
1398
|
+
|
1397
1399
|
constructor(key) {
|
1398
1400
|
super(key);
|
1399
1401
|
}
|
1400
1402
|
|
1403
|
+
exportJSON() {
|
1404
|
+
return { ...super.exportJSON(),
|
1405
|
+
type: 'table',
|
1406
|
+
version: 1
|
1407
|
+
};
|
1408
|
+
}
|
1409
|
+
|
1401
1410
|
createDOM(config, editor) {
|
1402
1411
|
const tableElement = document.createElement('table');
|
1403
1412
|
utils.addClassNamesToElement(tableElement, config.theme.table);
|
@@ -1427,8 +1436,7 @@ class TableNode extends lexical.GridNode {
|
|
1427
1436
|
for (let i = 0; i < colCount; i++) {
|
1428
1437
|
const col = document.createElement('col');
|
1429
1438
|
colGroup.append(col);
|
1430
|
-
}
|
1431
|
-
|
1439
|
+
}
|
1432
1440
|
|
1433
1441
|
newElement.replaceChildren(colGroup, tBody);
|
1434
1442
|
return newElement;
|
@@ -1446,10 +1454,6 @@ class TableNode extends lexical.GridNode {
|
|
1446
1454
|
}
|
1447
1455
|
|
1448
1456
|
getCordsFromCellNode(tableCellNode, grid) {
|
1449
|
-
if (!grid) {
|
1450
|
-
throw Error(`Grid not found.`);
|
1451
|
-
}
|
1452
|
-
|
1453
1457
|
const {
|
1454
1458
|
rows,
|
1455
1459
|
cells
|
@@ -1481,10 +1485,6 @@ class TableNode extends lexical.GridNode {
|
|
1481
1485
|
}
|
1482
1486
|
|
1483
1487
|
getCellFromCords(x, y, grid) {
|
1484
|
-
if (!grid) {
|
1485
|
-
throw Error(`Grid not found.`);
|
1486
|
-
}
|
1487
|
-
|
1488
1488
|
const {
|
1489
1489
|
cells
|
1490
1490
|
} = grid;
|
@@ -1575,7 +1575,6 @@ function $isTableNode(node) {
|
|
1575
1575
|
* This source code is licensed under the MIT license found in the
|
1576
1576
|
* LICENSE file in the root directory of this source tree.
|
1577
1577
|
*
|
1578
|
-
*
|
1579
1578
|
*/
|
1580
1579
|
function $createTableNodeWithDimensions(rowCount, columnCount, includeHeaders = true) {
|
1581
1580
|
const tableNode = $createTableNode();
|
@@ -1774,7 +1773,6 @@ function $deleteTableColumn(tableNode, targetIndex) {
|
|
1774
1773
|
* This source code is licensed under the MIT license found in the
|
1775
1774
|
* LICENSE file in the root directory of this source tree.
|
1776
1775
|
*
|
1777
|
-
*
|
1778
1776
|
*/
|
1779
1777
|
const INSERT_TABLE_COMMAND = lexical.createCommand();
|
1780
1778
|
|
package/LexicalTable.js.flow
CHANGED
package/LexicalTable.prod.js
CHANGED
@@ -4,57 +4,56 @@
|
|
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 f=require("lexical"),p=require("@lexical/utils");
|
8
|
-
class t extends f.GridCellNode{static getType(){return"tablecell"}static clone(a){return new t(a.__headerState,a.__colSpan,a.__width,a.__key)}static importDOM(){return{td:()=>({conversion:w,priority:0}),th:()=>({conversion:w,priority:0})}}constructor(a=q.NO_STATUS,b=1,d,
|
9
|
-
return b}exportDOM(a){({element:a}=super.exportDOM(a));if(a){
|
10
|
-
a;return this.__width}getWidth(){return this.getLatest().__width}toggleHeaderStyle(a){
|
11
|
-
function w(a){a=a.nodeName.toLowerCase();return{
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
this.
|
23
|
-
this.
|
24
|
-
|
25
|
-
|
26
|
-
function
|
27
|
-
function
|
28
|
-
function
|
29
|
-
function
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
if(
|
36
|
-
|
37
|
-
function
|
38
|
-
exports.$
|
39
|
-
exports.$
|
40
|
-
exports.$
|
41
|
-
exports.$
|
42
|
-
|
43
|
-
|
44
|
-
exports
|
45
|
-
|
46
|
-
|
47
|
-
if(f.$isGridSelection(e)&&e.gridKey===c.tableNodeKey&&h.contains(g.target))return c.clearHighlight()})};window.addEventListener("mousedown",r);c.listenersToRemove.add(()=>window.removeEventListener("mousedown",r));const v=()=>{k=!1};window.addEventListener("mouseup",v);c.listenersToRemove.add(()=>window.removeEventListener("mouseup",v));c.listenersToRemove.add(d.registerCommand(f.KEY_ARROW_DOWN_COMMAND,g=>{var e=f.$getSelection();if(!S(e,a))return!1;if(f.$isRangeSelection(e)){if(e.isCollapsed()){var l=
|
7
|
+
'use strict';var f=require("lexical"),p=require("@lexical/utils");let q={BOTH:3,COLUMN:2,NO_STATUS:0,ROW:1};
|
8
|
+
class t extends f.GridCellNode{static getType(){return"tablecell"}static clone(a){return new t(a.__headerState,a.__colSpan,a.__width,a.__key)}static importDOM(){return{td:()=>({conversion:w,priority:0}),th:()=>({conversion:w,priority:0})}}static importJSON(a){return x(a.headerState,a.colSpan,a.width)}constructor(a=q.NO_STATUS,b=1,d,k){super(b,k);this.__headerState=a;this.__width=d}createDOM(a){let b=document.createElement(this.getTag());this.__width&&(b.style.width=`${this.__width}px`);p.addClassNamesToElement(b,
|
9
|
+
a.theme.tableCell,this.hasHeader()&&a.theme.tableCellHeader);return b}exportDOM(a){({element:a}=super.exportDOM(a));if(a){let b=this.getParentOrThrow().getChildrenSize();a.style.border="1px solid black";a.style.width=`${this.getWidth()||Math.max(90,700/b)}px`;a.style.verticalAlign="top";a.style.textAlign="start";this.hasHeader()&&(a.style.backgroundColor="#f2f3f5")}return{element:a}}exportJSON(){return{...super.exportJSON(),colSpan:super.__colSpan,headerState:this.__headerState,type:"tablecell",width:this.getWidth()}}getTag(){return this.hasHeader()?
|
10
|
+
"th":"td"}setHeaderStyles(a){this.getWritable().__headerState=a;return this.__headerState}getHeaderStyles(){return this.getLatest().__headerState}setWidth(a){this.getWritable().__width=a;return this.__width}getWidth(){return this.getLatest().__width}toggleHeaderStyle(a){let b=this.getWritable();b.__headerState=(b.__headerState&a)===a?b.__headerState-a:b.__headerState+a;return b}hasHeaderState(a){return(this.getHeaderStyles()&a)===a}hasHeader(){return this.getLatest().__headerState!==q.NO_STATUS}updateDOM(a){return a.__headerState!==
|
11
|
+
this.__headerState||a.__width!==this.__width}collapseAtStart(){return!0}canBeEmpty(){return!1}canIndent(){return!1}}function w(a){a=a.nodeName.toLowerCase();return{forChild:(b,d)=>{if(y(d)&&!f.$isElementNode(b)){d=f.$createParagraphNode();if(f.$isLineBreakNode(b)&&"\n"===b.getTextContent())return null;d.append(b);return d}return b},node:x("th"===a?q.ROW:q.NO_STATUS)}}function x(a,b=1,d){return new t(a,b,d)}function y(a){return a instanceof t}
|
12
|
+
class z extends f.GridRowNode{static getType(){return"tablerow"}static clone(a){return new z(a.__height,a.__key)}static importDOM(){return{tr:()=>({conversion:A,priority:0})}}static importJSON(a){return B(a.height)}constructor(a,b){super(b);this.__height=a}exportJSON(){return{...super.exportJSON(),type:"tablerow",version:1}}createDOM(a){let b=document.createElement("tr");this.__height&&(b.style.height=`${this.__height}px`);p.addClassNamesToElement(b,a.theme.tableRow);return b}setHeight(a){this.getWritable().__height=
|
13
|
+
a;return this.__height}getHeight(){return this.getLatest().__height}updateDOM(a){return a.__height!==this.__height}canBeEmpty(){return!1}canIndent(){return!1}}function A(){return{node:B()}}function B(a){return new z(a)}function C(a){return a instanceof z}function D(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
|
+
if("undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement){let a=document.createElement("style");a.innerHTML="\n table.disable-selection {\n -webkit-touch-callout: none;\n -webkit-user-select: none; \n -khtml-user-select: none; \n -moz-user-select: none; \n -ms-user-select: none; \n user-select: none;\n }\n \n .disable-selection span::selection{\n background-color: transparent;\n }\n .disable-selection br::selection{\n background-color: transparent;\n }\n ";
|
15
|
+
document.body&&document.body.append(a)}
|
16
|
+
class E{constructor(a,b){this.isHighlightingCells=!1;this.currentY=this.currentX=this.startY=this.startX=-1;this.listenersToRemove=new Set;this.tableNodeKey=b;this.editor=a;this.grid={cells:[],columns:0,rows:0};this.focusCell=this.anchorCell=this.focusCellNodeKey=this.anchorCellNodeKey=this.gridSelection=null;this.hasHijackedSelectionStyles=!1;this.trackTableGrid()}getGrid(){return this.grid}removeListeners(){Array.from(this.listenersToRemove).forEach(a=>a())}trackTableGrid(){let a=new MutationObserver(b=>
|
17
|
+
{this.editor.update(()=>{var d=!1;for(let k=0;k<b.length;k++){const c=b[k].target.nodeName;if("TABLE"===c||"TR"===c){d=!0;break}}if(d){d=this.editor.getElementByKey(this.tableNodeKey);if(!d)throw Error("Expected to find TableElement in DOM");this.grid=F(d)}})});this.editor.update(()=>{let b=this.editor.getElementByKey(this.tableNodeKey);if(!b)throw Error("Expected to find TableElement in DOM");this.grid=F(b);a.observe(b,{childList:!0,subtree:!0})})}clearHighlight(){this.editor.update(()=>{var a=f.$getNodeByKey(this.tableNodeKey);
|
18
|
+
if(!G(a))throw Error("Expected TableNode.");a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a=F(a);this.isHighlightingCells=!1;this.currentY=this.currentX=this.startY=this.startX=-1;this.focusCell=this.anchorCell=this.focusCellNodeKey=this.anchorCellNodeKey=this.gridSelection=null;this.hasHijackedSelectionStyles=!1;H(a,null);f.$setSelection(null);this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND,void 0);this.enableHighlightStyle()})}enableHighlightStyle(){this.editor.update(()=>
|
19
|
+
{let a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a.classList.remove("disable-selection");this.hasHijackedSelectionStyles=!1})}disableHighlightStyle(){this.editor.update(()=>{let a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a.classList.add("disable-selection");this.hasHijackedSelectionStyles=!0})}updateTableGridSelection(a){if(null!=a){this.gridSelection=a;this.isHighlightingCells=
|
20
|
+
!0;this.disableHighlightStyle();let b=this.editor.getElementByKey(a.anchor.key);a=this.editor.getElementByKey(a.focus.key);b&&a&&window.getSelection().setBaseAndExtent(b,0,a,0);H(this.grid,this.gridSelection)}else this.clearHighlight()}adjustFocusCellForSelection(a,b=!1){this.editor.update(()=>{var d=f.$getNodeByKey(this.tableNodeKey);if(!G(d))throw Error("Expected TableNode.");if(!this.editor.getElementByKey(this.tableNodeKey))throw Error("Expected to find TableElement in DOM");d=a.x;let k=a.y;this.focusCell=
|
21
|
+
a;let c=window.getSelection();null!==this.anchorCell&&c.setBaseAndExtent(this.anchorCell.elem,0,a.elem,0);if(!this.isHighlightingCells&&(this.startX!==d||this.startY!==k||b))this.isHighlightingCells=!0,this.disableHighlightStyle();else if(d===this.currentX&&k===this.currentY)return;this.currentX=d;this.currentY=k;this.isHighlightingCells&&(d=f.$getNearestNodeFromDOMNode(a.elem),null!=this.gridSelection&&null!=this.anchorCellNodeKey&&y(d)&&(d=d.getKey(),this.gridSelection=f.$createGridSelection(),
|
22
|
+
this.focusCellNodeKey=d,this.gridSelection.set(this.tableNodeKey,this.anchorCellNodeKey,this.focusCellNodeKey),f.$setSelection(this.gridSelection),this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND,void 0),H(this.grid,this.gridSelection)))})}setAnchorCellForSelection(a){this.editor.update(()=>{this.anchorCell=a;this.startX=a.x;this.startY=a.y;window.getSelection().setBaseAndExtent(a.elem,0,a.elem,0);var b=f.$getNearestNodeFromDOMNode(a.elem);y(b)&&(b=b.getKey(),this.gridSelection=f.$createGridSelection(),
|
23
|
+
this.anchorCellNodeKey=b)})}formatCells(a){this.editor.update(()=>{let b=f.$getSelection();f.$isGridSelection(b)||D(11);let d=f.$createRangeSelection(),k=d.anchor,c=d.focus;b.getNodes().forEach(h=>{y(h)&&0!==h.getTextContentSize()&&(k.set(h.getKey(),0,"element"),c.set(h.getKey(),h.getChildrenSize(),"element"),d.formatText(a))});f.$setSelection(b);this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND,void 0)})}clearText(){this.editor.update(()=>{let a=f.$getNodeByKey(this.tableNodeKey);if(!G(a))throw Error("Expected TableNode.");
|
24
|
+
var b=f.$getSelection();f.$isGridSelection(b)||D(11);b=b.getNodes().filter(y);b.length===this.grid.columns*this.grid.rows?(a.selectPrevious(),a.remove(),this.clearHighlight()):(b.forEach(d=>{if(f.$isElementNode(d)){let k=f.$createParagraphNode(),c=f.$createTextNode();k.append(c);d.append(k);d.getChildren().forEach(h=>{h!==k&&h.remove()})}}),H(this.grid,null),f.$setSelection(null),this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND,void 0))})}}
|
25
|
+
function I(a){for(;null!=a;){let b=a.nodeName;if("TD"===b||"TH"===b){a=a._cell;if(void 0===a)break;return a}a=a.parentNode}return null}
|
26
|
+
function F(a){let b=[],d={cells:b,columns:0,rows:0};var k=a.firstChild;let c=a=0;for(b.length=0;null!=k;){var h=k.nodeName;if("TD"===h||"TH"===h)h={elem:k,highlighted:!1,x:a,y:c},k._cell=h,void 0===b[c]&&(b[c]=[]),b[c][a]=h;else if(h=k.firstChild,null!=h){k=h;continue}h=k.nextSibling;if(null!=h)a++,k=h;else if(h=k.parentNode,null!=h){k=h.nextSibling;if(null==k)break;c++;a=0}}d.columns=a+1;d.rows=c+1;return d}
|
27
|
+
function H(a,b){let d=[],k=new Set(b?b.getNodes():[]);J(a,(c,h)=>{let n=c.elem;k.has(h)?(c.highlighted=!0,n.style.setProperty("background-color","rgb(172, 206, 247)"),n.style.setProperty("caret-color","transparent"),d.push(c)):(c.highlighted=!1,n.style.removeProperty("background-color"),n.style.removeProperty("caret-color"),n.getAttribute("style")||n.removeAttribute("style"))});return d}
|
28
|
+
function J(a,b){({cells:a}=a);for(let d=0;d<a.length;d++){let k=a[d];for(let c=0;c<k.length;c++){let h=k[c],n=f.$getNearestNodeFromDOMNode(h.elem);null!==n&&b(h,n,{x:c,y:d})}}}function K(a){a.disableHighlightStyle();J(a.grid,b=>{let d=b.elem;b.highlighted=!0;d.style.setProperty("background-color","rgb(172, 206, 247)");d.style.setProperty("caret-color","transparent")})}
|
29
|
+
function O(a){a.enableHighlightStyle();J(a.grid,b=>{let d=b.elem;b.highlighted=!1;d.style.removeProperty("background-color");d.style.removeProperty("caret-color");d.getAttribute("style")||d.removeAttribute("style")})}
|
30
|
+
let Q=(a,b,d,k,c)=>{const h="forward"===c;switch(c){case "backward":case "forward":return d!==(h?a.grid.columns-1:0)?P(b.getCellNodeFromCordsOrThrow(d+(h?1:-1),k,a.grid)):k!==(h?a.grid.rows-1:0)?P(b.getCellNodeFromCordsOrThrow(h?0:a.grid.columns-1,k+(h?1:-1),a.grid)):h?b.selectNext():b.selectPrevious(),!0;case "up":return 0!==k?P(b.getCellNodeFromCordsOrThrow(d,k-1,a.grid)):b.selectPrevious(),!0;case "down":return k!==a.grid.rows-1?P(b.getCellNodeFromCordsOrThrow(d,k+1,a.grid)):b.selectNext(),!0;
|
31
|
+
default:return!1}},R=(a,b,d,k,c)=>{const h="forward"===c;switch(c){case "backward":case "forward":return d!==(h?a.grid.columns-1:0)&&a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(d+(h?1:-1),k,a.grid)),!0;case "up":return 0!==k?(a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(d,k-1,a.grid)),!0):!1;case "down":return k!==a.grid.rows-1?(a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(d,k+1,a.grid)),!0):!1;default:return!1}};
|
32
|
+
function S(a,b){if(f.$isRangeSelection(a)||f.$isGridSelection(a)){let d=b.isParentOf(a.anchor.getNode());a=b.isParentOf(a.focus.getNode());return d&&a}return!1}function P(a){let b=a.getChildren().find(d=>f.$isParagraphNode(d));f.$isParagraphNode(b)?b.selectEnd():a.selectEnd()}
|
33
|
+
class T extends f.GridNode{static getType(){return"table"}static clone(a){return new T(a.__key)}static importDOM(){return{table:()=>({conversion:U,priority:0})}}static importJSON(){return V()}constructor(a){super(a)}exportJSON(){return{...super.exportJSON(),type:"table",version:1}}createDOM(a){let b=document.createElement("table");p.addClassNamesToElement(b,a.theme.table);return b}updateDOM(){return!1}exportDOM(a){return{...super.exportDOM(a),after:b=>{if(b){let d=b.cloneNode(),k=document.createElement("colgroup"),
|
34
|
+
c=document.createElement("tbody");c.append(...b.children);b=this.getFirstChildOrThrow();if(!C(b))throw Error("Expected to find row node.");b=b.getChildrenSize();for(let h=0;h<b;h++){let n=document.createElement("col");k.append(n)}d.replaceChildren(k,c);return d}}}}canExtractContents(){return!1}canBeEmpty(){return!1}getCordsFromCellNode(a,b){let {rows:d,cells:k}=b;for(b=0;b<d;b++){var c=k[b];if(null==c)throw Error(`Row not found at y:${b}`);c=c.findIndex(({elem:h})=>f.$getNearestNodeFromDOMNode(h)===
|
35
|
+
a);if(-1!==c)return{x:c,y:b}}throw Error("Cell not found in table.");}getCellFromCords(a,b,d){({cells:d}=d);b=d[b];if(null==b)return null;a=b[a];return null==a?null:a}getCellFromCordsOrThrow(a,b,d){a=this.getCellFromCords(a,b,d);if(!a)throw Error("Cell not found at cords.");return a}getCellNodeFromCords(a,b,d){a=this.getCellFromCords(a,b,d);if(null==a)return null;a=f.$getNearestNodeFromDOMNode(a.elem);return y(a)?a:null}getCellNodeFromCordsOrThrow(a,b,d){a=this.getCellNodeFromCords(a,b,d);if(!a)throw Error("Node at cords not TableCellNode.");
|
36
|
+
return a}canSelectBefore(){return!0}canIndent(){return!1}}function U(){return{node:V()}}function V(){return new T}function G(a){return a instanceof T}function W(a){a=p.$findMatchingParent(a,b=>C(b));if(C(a))return a;throw Error("Expected table cell to be inside of table row.");}function X(a){a=p.$findMatchingParent(a,b=>G(b));if(G(a))return a;throw Error("Expected table cell to be inside of table.");}let Y=f.createCommand();exports.$createTableCellNode=x;exports.$createTableNode=V;
|
37
|
+
exports.$createTableNodeWithDimensions=function(a,b,d=!0){let k=V();for(let h=0;h<a;h++){let n=B();for(let r=0;r<b;r++){var c=q.NO_STATUS;d&&(0===h&&(c|=q.ROW),0===r&&(c|=q.COLUMN));c=x(c);let v=f.$createParagraphNode();v.append(f.$createTextNode());c.append(v);n.append(c)}k.append(n)}return k};exports.$createTableRowNode=B;
|
38
|
+
exports.$deleteTableColumn=function(a,b){let d=a.getChildren();for(let c=0;c<d.length;c++){var k=d[c];if(C(k)){k=k.getChildren();if(b>=k.length||0>b)throw Error("Table column target index out of range");k[b].remove()}}return a};exports.$getElementGridForTableNode=function(a,b){a=a.getElementByKey(b.getKey());if(null==a)throw Error("Table Element Not Found");return F(a)};exports.$getTableCellNodeFromLexicalNode=function(a){a=p.$findMatchingParent(a,b=>y(b));return y(a)?a:null};
|
39
|
+
exports.$getTableColumnIndexFromTableCellNode=function(a){return W(a).getChildren().findIndex(b=>b.is(a))};exports.$getTableNodeFromLexicalNodeOrThrow=X;exports.$getTableRowIndexFromTableCellNode=function(a){let b=W(a);return X(b).getChildren().findIndex(d=>d.is(b))};exports.$getTableRowNodeFromTableCellNodeOrThrow=W;
|
40
|
+
exports.$insertTableColumn=function(a,b,d=!0,k){let c=a.getChildren();for(let r=0;r<c.length;r++){let v=c[r];if(C(v))for(let g=0;g<k;g++){var h=q.NO_STATUS;0===r&&(h|=q.ROW);h=x(h);h.append(f.$createParagraphNode());var n=v.getChildren();if(b>=n.length||0>b)throw Error("Table column target index out of range");n=n[b];d?n.insertAfter(h):n.insertBefore(h)}}return a};
|
41
|
+
exports.$insertTableRow=function(a,b,d=!0,k,c){var h=a.getChildren();if(b>=h.length||0>b)throw Error("Table row target index out of range");b=h[b];if(C(b))for(h=0;h<k;h++){let v=b.getChildren(),g=v.length,e=B();for(let l=0;l<g;l++){var n=v[l];y(n)||D(12);var r=c;let m=X(n),{x:u,y:L}=m.getCordsFromCellNode(n,r);n={above:m.getCellNodeFromCords(u,L-1,r),below:m.getCellNodeFromCords(u,L+1,r),left:m.getCellNodeFromCords(u-1,L,r),right:m.getCellNodeFromCords(u+1,L,r)};let {above:M,below:N}=n;n=q.NO_STATUS;
|
42
|
+
r=M&&M.getWidth()||N&&N.getWidth()||null;if(M&&M.hasHeaderState(q.COLUMN)||N&&N.hasHeaderState(q.COLUMN))n|=q.COLUMN;n=x(n,1,r);n.append(f.$createParagraphNode());e.append(n)}d?b.insertAfter(e):b.insertBefore(e)}else throw Error("Row before insertion index does not exist.");return a};exports.$isTableCellNode=y;exports.$isTableNode=G;exports.$isTableRowNode=C;
|
43
|
+
exports.$removeTableRowAtIndex=function(a,b){let d=a.getChildren();if(b>=d.length||0>b)throw Error("Expected table cell to be inside of table row.");d[b].remove();return a};exports.INSERT_TABLE_COMMAND=Y;exports.TableCellHeaderStates=q;exports.TableCellNode=t;exports.TableNode=T;exports.TableRowNode=z;exports.TableSelection=E;
|
44
|
+
exports.applyTableHandlers=function(a,b,d){let k=d.getRootElement();if(null===k)throw Error("No root element.");let c=new E(d,a.getKey());b.__lexicalTableSelection=c;let h=!1,n=!1;b.addEventListener("dblclick",g=>{let e=I(g.target);null!==e&&(g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),c.setAnchorCellForSelection(e),c.adjustFocusCellForSelection(e,!0),h=!1)});b.addEventListener("mousedown",g=>{setTimeout(()=>{if(0===g.button){var e=I(g.target);null!==e&&(c.setAnchorCellForSelection(e),
|
45
|
+
document.addEventListener("mouseup",()=>{h=!1},{capture:!0,once:!0}))}},0)});b.addEventListener("mousemove",g=>{n&&(g.preventDefault(),g.stopPropagation(),g.stopImmediatePropagation());if(h){let e=I(g.target);if(null!==e){let l=e.x,m=e.y;h&&(c.startX!==l||c.startY!==m||c.isHighlightingCells)&&(g.preventDefault(),h=!0,c.adjustFocusCellForSelection(e))}}});b.addEventListener("mouseup",()=>{h&&(h=!1)});b.addEventListener("mouseleave",()=>{});let r=g=>{h=!0;0===g.button&&d.update(()=>{const e=f.$getSelection();
|
46
|
+
if(f.$isGridSelection(e)&&e.gridKey===c.tableNodeKey&&k.contains(g.target))return c.clearHighlight()})};window.addEventListener("mousedown",r);c.listenersToRemove.add(()=>window.removeEventListener("mousedown",r));let v=()=>{h=!1};window.addEventListener("mouseup",v);c.listenersToRemove.add(()=>window.removeEventListener("mouseup",v));c.listenersToRemove.add(d.registerCommand(f.KEY_ARROW_DOWN_COMMAND,g=>{var e=f.$getSelection();if(!S(e,a))return!1;if(f.$isRangeSelection(e)){if(e.isCollapsed()){var l=
|
48
47
|
p.$findMatchingParent(e.anchor.getNode(),u=>y(u));if(!y(l))return!1;var m=a.getCordsFromCellNode(l,c.grid);e=p.$findMatchingParent(e.anchor.getNode(),u=>f.$isElementNode(u));if(null==e)throw Error("Expected BlockNode Parent");if((l=l.getLastChild())&&e.isParentOf(l)||e===l||g.shiftKey)return g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(m.x,m.y,c.grid)),R(c,a,m.x,m.y,"down")):Q(c,a,m.x,m.y,"down")}}else if(f.$isGridSelection(e)&&
|
49
48
|
g.shiftKey){m=e.focus.getNode();if(!y(m))return!1;m=a.getCordsFromCellNode(m,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();return R(c,a,m.x,m.y,"down")}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.KEY_ARROW_UP_COMMAND,g=>{var e=f.$getSelection();if(!S(e,a))return!1;if(f.$isRangeSelection(e)){if(e.isCollapsed()){var l=p.$findMatchingParent(e.anchor.getNode(),u=>y(u));if(!y(l))return!1;var m=a.getCordsFromCellNode(l,c.grid);e=p.$findMatchingParent(e.anchor.getNode(),
|
50
49
|
u=>f.$isElementNode(u));if(null==e)throw Error("Expected BlockNode Parent");if((l=l.getLastChild())&&e.isParentOf(l)||e===l||g.shiftKey)return g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(m.x,m.y,c.grid)),R(c,a,m.x,m.y,"up")):Q(c,a,m.x,m.y,"up")}}else if(f.$isGridSelection(e)&&g.shiftKey){m=e.focus.getNode();if(!y(m))return!1;m=a.getCordsFromCellNode(m,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();
|
51
50
|
return R(c,a,m.x,m.y,"up")}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.KEY_ARROW_LEFT_COMMAND,g=>{var e=f.$getSelection();if(!S(e,a))return!1;if(f.$isRangeSelection(e)){if(e.isCollapsed()){var l=p.$findMatchingParent(e.anchor.getNode(),m=>y(m));if(!y(l))return!1;l=a.getCordsFromCellNode(l,c.grid);if(null==p.$findMatchingParent(e.anchor.getNode(),m=>f.$isElementNode(m)))throw Error("Expected BlockNode Parent");if(0===e.anchor.offset||g.shiftKey)return g.preventDefault(),
|
52
51
|
g.stopImmediatePropagation(),g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(l.x,l.y,c.grid)),R(c,a,l.x,l.y,"backward")):Q(c,a,l.x,l.y,"backward")}}else if(f.$isGridSelection(e)&&g.shiftKey){e=e.focus.getNode();if(!y(e))return!1;e=a.getCordsFromCellNode(e,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();return R(c,a,e.x,e.y,"backward")}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.KEY_ARROW_RIGHT_COMMAND,
|
53
52
|
g=>{var e=f.$getSelection();if(!S(e,a))return!1;if(f.$isRangeSelection(e)){if(e.isCollapsed()){var l=p.$findMatchingParent(e.anchor.getNode(),m=>y(m));if(!y(l))return!1;l=a.getCordsFromCellNode(l,c.grid);if(null==p.$findMatchingParent(e.anchor.getNode(),m=>f.$isElementNode(m)))throw Error("Expected BlockNode Parent");if(e.anchor.offset===e.anchor.getNode().getTextContentSize()||g.shiftKey)return g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(l.x,
|
54
|
-
l.y,c.grid)),R(c,a,l.x,l.y,"forward")):Q(c,a,l.x,l.y,"forward")}}else if(f.$isGridSelection(e)&&g.shiftKey){e=e.focus.getNode();if(!y(e))return!1;e=a.getCordsFromCellNode(e,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();return R(c,a,e.x,e.y,"forward")}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.DELETE_CHARACTER_COMMAND,()=>{
|
55
|
-
p.$findMatchingParent(g.anchor.getNode(),l=>y(l));if(!y(e))return!1;e=p.$findMatchingParent(g.anchor.getNode(),l=>f.$isParagraphNode(l));if(!f.$isParagraphNode(e))return!1;if(g.isCollapsed()&&0===g.anchor.offset&&0===e.getPreviousSiblings().length)return!0}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.KEY_BACKSPACE_COMMAND,g=>{
|
56
|
-
|
57
|
-
|
58
|
-
"forward"),!0}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.FOCUS_COMMAND,()=>a.isSelected(),f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.SELECTION_CHANGE_COMMAND,()=>{
|
59
|
-
g.anchor.getNode();e=g.focus.getNode();l=a.isParentOf(l);e=a.isParentOf(e);if(l&&!e||e&&!l){e=g.isBackward();l=f.$createRangeSelection();
|
60
|
-
exports.getCellFromTarget=I;exports.getTableSelectionFromTableElement=function(a){return a.__lexicalTableSelection}
|
53
|
+
l.y,c.grid)),R(c,a,l.x,l.y,"forward")):Q(c,a,l.x,l.y,"forward")}}else if(f.$isGridSelection(e)&&g.shiftKey){e=e.focus.getNode();if(!y(e))return!1;e=a.getCordsFromCellNode(e,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();return R(c,a,e.x,e.y,"forward")}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.DELETE_CHARACTER_COMMAND,()=>{let g=f.$getSelection();if(!S(g,a))return!1;if(f.$isGridSelection(g))return c.clearText(),!0;if(f.$isRangeSelection(g)){var e=
|
54
|
+
p.$findMatchingParent(g.anchor.getNode(),l=>y(l));if(!y(e))return!1;e=p.$findMatchingParent(g.anchor.getNode(),l=>f.$isParagraphNode(l));if(!f.$isParagraphNode(e))return!1;if(g.isCollapsed()&&0===g.anchor.offset&&0===e.getPreviousSiblings().length)return!0}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.KEY_BACKSPACE_COMMAND,g=>{let e=f.$getSelection();if(!S(e,a))return!1;if(f.$isGridSelection(e))return g.preventDefault(),g.stopPropagation(),c.clearText(),!0;f.$isRangeSelection(e)&&
|
55
|
+
(g=p.$findMatchingParent(e.anchor.getNode(),l=>y(l)),y(g));return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.FORMAT_TEXT_COMMAND,g=>{let e=f.$getSelection();if(!S(e,a))return!1;if(f.$isGridSelection(e))return c.formatCells(g),!0;f.$isRangeSelection(e)&&(g=p.$findMatchingParent(e.anchor.getNode(),l=>y(l)),y(g));return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.CONTROLLED_TEXT_INSERTION_COMMAND,()=>{var g=f.$getSelection();if(!S(g,
|
56
|
+
a))return!1;f.$isGridSelection(g)?c.clearHighlight():f.$isRangeSelection(g)&&(g=p.$findMatchingParent(g.anchor.getNode(),e=>y(e)),y(g));return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.KEY_TAB_COMMAND,g=>{var e=f.$getSelection();if(!S(e,a))return!1;if(f.$isRangeSelection(e)){let l=p.$findMatchingParent(e.anchor.getNode(),m=>y(m));if(!y(l))return!1;if(e.isCollapsed())return e=a.getCordsFromCellNode(l,c.grid),g.preventDefault(),Q(c,a,e.x,e.y,g.shiftKey?"backward":
|
57
|
+
"forward"),!0}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.FOCUS_COMMAND,()=>a.isSelected(),f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.SELECTION_CHANGE_COMMAND,()=>{let g=f.$getSelection();var e=f.$getPreviousSelection();if(g!==e&&(f.$isGridSelection(g)||f.$isGridSelection(e))&&c.gridSelection!==g)return c.updateTableGridSelection(f.$isGridSelection(g)&&a.isSelected()?g:null),!1;if(g&&f.$isRangeSelection(g)&&!g.isCollapsed()){var l=
|
58
|
+
g.anchor.getNode();e=g.focus.getNode();l=a.isParentOf(l);e=a.isParentOf(e);if(l&&!e||e&&!l){e=g.isBackward();l=f.$createRangeSelection();let m=a.getIndexWithinParent(),u=a.getParentOrThrow().getKey();l.anchor.set(g.anchor.key,g.anchor.offset,g.anchor.type);l.focus.set(u,e?m-1:m+1,"element");n=!0;f.$setSelection(l);K(c);return!0}}c.hasHijackedSelectionStyles&&!a.isSelected()?(O(c),n=!1):!c.hasHijackedSelectionStyles&&a.isSelected()&&K(c);return!1},f.COMMAND_PRIORITY_CRITICAL));return c};
|
59
|
+
exports.getCellFromTarget=I;exports.getTableSelectionFromTableElement=function(a){return a.__lexicalTableSelection}
|
package/package.json
CHANGED
@@ -8,13 +8,13 @@
|
|
8
8
|
"table"
|
9
9
|
],
|
10
10
|
"license": "MIT",
|
11
|
-
"version": "0.
|
11
|
+
"version": "0.3.1",
|
12
12
|
"main": "LexicalTable.js",
|
13
13
|
"peerDependencies": {
|
14
|
-
"lexical": "0.
|
14
|
+
"lexical": "0.3.1"
|
15
15
|
},
|
16
16
|
"dependencies": {
|
17
|
-
"@lexical/utils": "0.
|
17
|
+
"@lexical/utils": "0.3.1"
|
18
18
|
},
|
19
19
|
"repository": {
|
20
20
|
"type": "git",
|