@lexical/table 0.1.20 → 0.2.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.dev.js +294 -106
- package/LexicalTable.prod.js +51 -42
- package/package.json +3 -3
package/LexicalTable.dev.js
CHANGED
@@ -25,7 +25,7 @@ class TableCellNode extends lexical.GridCellNode {
|
|
25
25
|
return new TableCellNode(node.__headerState, node.__colSpan, node.__width, node.__key);
|
26
26
|
}
|
27
27
|
|
28
|
-
static
|
28
|
+
static importDOM() {
|
29
29
|
return {
|
30
30
|
td: node => ({
|
31
31
|
conversion: convertTableCellNodeElement,
|
@@ -55,6 +55,29 @@ class TableCellNode extends lexical.GridCellNode {
|
|
55
55
|
return element;
|
56
56
|
}
|
57
57
|
|
58
|
+
exportDOM(editor) {
|
59
|
+
const {
|
60
|
+
element
|
61
|
+
} = super.exportDOM(editor);
|
62
|
+
|
63
|
+
if (element) {
|
64
|
+
const maxWidth = 700;
|
65
|
+
const colCount = this.getParentOrThrow().getChildrenSize();
|
66
|
+
element.style.border = '1px solid black';
|
67
|
+
element.style.width = `${this.getWidth() || Math.max(90, maxWidth / colCount)}px`;
|
68
|
+
element.style.verticalAlign = 'top';
|
69
|
+
element.style.textAlign = 'start';
|
70
|
+
|
71
|
+
if (this.hasHeader()) {
|
72
|
+
element.style.backgroundColor = '#f2f3f5';
|
73
|
+
}
|
74
|
+
}
|
75
|
+
|
76
|
+
return {
|
77
|
+
element
|
78
|
+
};
|
79
|
+
}
|
80
|
+
|
58
81
|
getTag() {
|
59
82
|
return this.hasHeader() ? 'th' : 'td';
|
60
83
|
}
|
@@ -141,6 +164,89 @@ function $isTableCellNode(node) {
|
|
141
164
|
return node instanceof TableCellNode;
|
142
165
|
}
|
143
166
|
|
167
|
+
/**
|
168
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
169
|
+
*
|
170
|
+
* This source code is licensed under the MIT license found in the
|
171
|
+
* LICENSE file in the root directory of this source tree.
|
172
|
+
*
|
173
|
+
*
|
174
|
+
*/
|
175
|
+
class TableRowNode extends lexical.GridRowNode {
|
176
|
+
static getType() {
|
177
|
+
return 'tablerow';
|
178
|
+
}
|
179
|
+
|
180
|
+
static clone(node) {
|
181
|
+
return new TableRowNode(node.__height, node.__key);
|
182
|
+
}
|
183
|
+
|
184
|
+
static importDOM() {
|
185
|
+
return {
|
186
|
+
tr: node => ({
|
187
|
+
conversion: convertTableRowElement,
|
188
|
+
priority: 0
|
189
|
+
})
|
190
|
+
};
|
191
|
+
}
|
192
|
+
|
193
|
+
constructor(height, key) {
|
194
|
+
super(key);
|
195
|
+
this.__height = height;
|
196
|
+
}
|
197
|
+
|
198
|
+
createDOM(config) {
|
199
|
+
const element = document.createElement('tr');
|
200
|
+
|
201
|
+
if (this.__height) {
|
202
|
+
element.style.height = `${this.__height}px`;
|
203
|
+
}
|
204
|
+
|
205
|
+
utils.addClassNamesToElement(element, config.theme.tableRow);
|
206
|
+
return element;
|
207
|
+
}
|
208
|
+
|
209
|
+
setHeight(height) {
|
210
|
+
const self = this.getWritable();
|
211
|
+
self.__height = height;
|
212
|
+
return this.__height;
|
213
|
+
}
|
214
|
+
|
215
|
+
getHeight() {
|
216
|
+
return this.getLatest().__height;
|
217
|
+
}
|
218
|
+
|
219
|
+
updateDOM(prevNode) {
|
220
|
+
return prevNode.__height !== this.__height;
|
221
|
+
}
|
222
|
+
|
223
|
+
canBeEmpty() {
|
224
|
+
return false;
|
225
|
+
}
|
226
|
+
|
227
|
+
}
|
228
|
+
function convertTableRowElement(domNode) {
|
229
|
+
return {
|
230
|
+
node: $createTableRowNode()
|
231
|
+
};
|
232
|
+
}
|
233
|
+
function $createTableRowNode(height) {
|
234
|
+
return new TableRowNode(height);
|
235
|
+
}
|
236
|
+
function $isTableRowNode(node) {
|
237
|
+
return node instanceof TableRowNode;
|
238
|
+
}
|
239
|
+
|
240
|
+
/**
|
241
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
242
|
+
*
|
243
|
+
* This source code is licensed under the MIT license found in the
|
244
|
+
* LICENSE file in the root directory of this source tree.
|
245
|
+
*
|
246
|
+
*
|
247
|
+
*/
|
248
|
+
const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
|
249
|
+
|
144
250
|
/**
|
145
251
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
146
252
|
*
|
@@ -161,8 +267,32 @@ var getDOMSelection = getSelection;
|
|
161
267
|
*
|
162
268
|
*
|
163
269
|
*/
|
164
|
-
|
165
|
-
|
270
|
+
|
271
|
+
if (CAN_USE_DOM) {
|
272
|
+
const disableNativeSelectionUi = document.createElement('style');
|
273
|
+
disableNativeSelectionUi.innerHTML = `
|
274
|
+
table.disable-selection {
|
275
|
+
-webkit-touch-callout: none;
|
276
|
+
-webkit-user-select: none;
|
277
|
+
-khtml-user-select: none;
|
278
|
+
-moz-user-select: none;
|
279
|
+
-ms-user-select: none;
|
280
|
+
user-select: none;
|
281
|
+
}
|
282
|
+
|
283
|
+
.disable-selection span::selection{
|
284
|
+
background-color: transparent;
|
285
|
+
}
|
286
|
+
.disable-selection br::selection{
|
287
|
+
background-color: transparent;
|
288
|
+
}
|
289
|
+
`;
|
290
|
+
|
291
|
+
if (document.body) {
|
292
|
+
document.body.append(disableNativeSelectionUi);
|
293
|
+
}
|
294
|
+
}
|
295
|
+
|
166
296
|
class TableSelection {
|
167
297
|
constructor(editor, tableNodeKey) {
|
168
298
|
this.isHighlightingCells = false;
|
@@ -183,6 +313,7 @@ class TableSelection {
|
|
183
313
|
this.focusCellNodeKey = null;
|
184
314
|
this.anchorCell = null;
|
185
315
|
this.focusCell = null;
|
316
|
+
this.hasHijackedSelectionStyles = false;
|
186
317
|
this.trackTableGrid();
|
187
318
|
}
|
188
319
|
|
@@ -263,14 +394,37 @@ class TableSelection {
|
|
263
394
|
this.focusCellNodeKey = null;
|
264
395
|
this.anchorCell = null;
|
265
396
|
this.focusCell = null;
|
397
|
+
this.hasHijackedSelectionStyles = false;
|
266
398
|
$updateDOMForSelection(grid, null);
|
267
399
|
lexical.$setSelection(null);
|
268
400
|
this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND);
|
269
|
-
|
401
|
+
this.enableHighlightStyle();
|
402
|
+
});
|
403
|
+
}
|
404
|
+
|
405
|
+
enableHighlightStyle() {
|
406
|
+
this.editor.update(() => {
|
407
|
+
const tableElement = this.editor.getElementByKey(this.tableNodeKey);
|
408
|
+
|
409
|
+
if (!tableElement) {
|
410
|
+
throw new Error('Expected to find TableElement in DOM');
|
411
|
+
}
|
412
|
+
|
413
|
+
tableElement.classList.remove('disable-selection');
|
414
|
+
this.hasHijackedSelectionStyles = false;
|
415
|
+
});
|
416
|
+
}
|
270
417
|
|
271
|
-
|
272
|
-
|
418
|
+
disableHighlightStyle() {
|
419
|
+
this.editor.update(() => {
|
420
|
+
const tableElement = this.editor.getElementByKey(this.tableNodeKey);
|
421
|
+
|
422
|
+
if (!tableElement) {
|
423
|
+
throw new Error('Expected to find TableElement in DOM');
|
273
424
|
}
|
425
|
+
|
426
|
+
tableElement.classList.add('disable-selection');
|
427
|
+
this.hasHijackedSelectionStyles = true;
|
274
428
|
});
|
275
429
|
}
|
276
430
|
|
@@ -300,10 +454,7 @@ class TableSelection {
|
|
300
454
|
|
301
455
|
if (!this.isHighlightingCells && (this.startX !== cellX || this.startY !== cellY || ignoreStart)) {
|
302
456
|
this.isHighlightingCells = true;
|
303
|
-
|
304
|
-
if (document.body) {
|
305
|
-
document.body.appendChild(removeHighlightStyle);
|
306
|
-
}
|
457
|
+
this.disableHighlightStyle();
|
307
458
|
} else if (cellX === this.currentX && cellY === this.currentY) {
|
308
459
|
return;
|
309
460
|
}
|
@@ -438,6 +589,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
438
589
|
const tableSelection = new TableSelection(editor, tableNode.getKey());
|
439
590
|
attachTableSelectionToTableElement(tableElement, tableSelection);
|
440
591
|
let isMouseDown = false;
|
592
|
+
let isRangeSelectionHijacked = false;
|
441
593
|
tableElement.addEventListener('dblclick', event => {
|
442
594
|
// $FlowFixMe: event.target is always a Node on the DOM
|
443
595
|
const cell = getCellFromTarget(event.target);
|
@@ -462,7 +614,6 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
462
614
|
const cell = getCellFromTarget(event.target);
|
463
615
|
|
464
616
|
if (cell !== null) {
|
465
|
-
isMouseDown = true;
|
466
617
|
tableSelection.setAnchorCellForSelection(cell);
|
467
618
|
document.addEventListener('mouseup', () => {
|
468
619
|
isMouseDown = false;
|
@@ -475,6 +626,12 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
475
626
|
}); // This is adjusting the focus of the selection.
|
476
627
|
|
477
628
|
tableElement.addEventListener('mousemove', event => {
|
629
|
+
if (isRangeSelectionHijacked) {
|
630
|
+
event.preventDefault();
|
631
|
+
event.stopPropagation();
|
632
|
+
event.stopImmediatePropagation();
|
633
|
+
}
|
634
|
+
|
478
635
|
if (isMouseDown) {
|
479
636
|
// $FlowFixMe: event.target is always a Node on the DOM
|
480
637
|
const cell = getCellFromTarget(event.target);
|
@@ -504,6 +661,8 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
504
661
|
}); // Clear selection when clicking outside of dom.
|
505
662
|
|
506
663
|
const mouseDownCallback = event => {
|
664
|
+
isMouseDown = true;
|
665
|
+
|
507
666
|
if (event.button !== 0) {
|
508
667
|
return;
|
509
668
|
}
|
@@ -519,6 +678,13 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
519
678
|
|
520
679
|
window.addEventListener('mousedown', mouseDownCallback);
|
521
680
|
tableSelection.listenersToRemove.add(() => window.removeEventListener('mousedown', mouseDownCallback));
|
681
|
+
|
682
|
+
const mouseUpCallback = event => {
|
683
|
+
isMouseDown = false;
|
684
|
+
};
|
685
|
+
|
686
|
+
window.addEventListener('mouseup', mouseUpCallback);
|
687
|
+
tableSelection.listenersToRemove.add(() => window.removeEventListener('mouseup', mouseUpCallback));
|
522
688
|
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_DOWN_COMMAND, payload => {
|
523
689
|
const selection = lexical.$getSelection();
|
524
690
|
const event = payload;
|
@@ -814,6 +980,40 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
814
980
|
|
815
981
|
return false;
|
816
982
|
}, CriticalPriority));
|
983
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.SELECTION_CHANGE_COMMAND, payload => {
|
984
|
+
const selection = lexical.$getSelection();
|
985
|
+
|
986
|
+
if (selection && lexical.$isRangeSelection(selection) && !selection.isCollapsed()) {
|
987
|
+
const anchorNode = selection.anchor.getNode();
|
988
|
+
const focusNode = selection.focus.getNode();
|
989
|
+
const isAnchorInside = tableNode.isParentOf(anchorNode);
|
990
|
+
const isFocusInside = tableNode.isParentOf(focusNode);
|
991
|
+
const containsPartialTable = isAnchorInside && !isFocusInside || isFocusInside && !isAnchorInside;
|
992
|
+
|
993
|
+
if (containsPartialTable) {
|
994
|
+
const isBackward = selection.isBackward();
|
995
|
+
const startNode = isBackward ? focusNode : anchorNode;
|
996
|
+
const modifiedSelection = lexical.$createRangeSelection();
|
997
|
+
const tableIndex = tableNode.getIndexWithinParent();
|
998
|
+
const parentKey = tableNode.getParentOrThrow().getKey();
|
999
|
+
isRangeSelectionHijacked = true;
|
1000
|
+
(isBackward ? modifiedSelection.focus : modifiedSelection.anchor).set(startNode.getKey(), (isBackward ? selection.focus : selection.anchor).offset, lexical.$isTextNode(startNode) ? 'text' : 'element');
|
1001
|
+
(isBackward ? modifiedSelection.anchor : modifiedSelection.focus).set(parentKey, isBackward ? tableIndex - 1 : tableIndex + 1, 'element');
|
1002
|
+
lexical.$setSelection(modifiedSelection);
|
1003
|
+
$addHighlightStyleToTable(tableSelection);
|
1004
|
+
return true;
|
1005
|
+
}
|
1006
|
+
}
|
1007
|
+
|
1008
|
+
if (tableSelection.hasHijackedSelectionStyles && !tableNode.isSelected()) {
|
1009
|
+
$removeHighlightStyleToTable(tableSelection);
|
1010
|
+
isRangeSelectionHijacked = false;
|
1011
|
+
} else if (!tableSelection.hasHijackedSelectionStyles && tableNode.isSelected()) {
|
1012
|
+
$addHighlightStyleToTable(tableSelection);
|
1013
|
+
}
|
1014
|
+
|
1015
|
+
return false;
|
1016
|
+
}, CriticalPriority));
|
817
1017
|
return tableSelection;
|
818
1018
|
}
|
819
1019
|
function attachTableSelectionToTableElement(tableElement, tableSelection) {
|
@@ -914,39 +1114,71 @@ function getTableGrid(tableElement) {
|
|
914
1114
|
grid.rows = y + 1;
|
915
1115
|
return grid;
|
916
1116
|
}
|
917
|
-
function $updateDOMForSelection(grid,
|
1117
|
+
function $updateDOMForSelection(grid, selection) {
|
918
1118
|
const highlightedCells = [];
|
1119
|
+
const selectedCellNodes = new Set(selection ? selection.getNodes() : []);
|
1120
|
+
$forEachGridCell(grid, (cell, lexicalNode) => {
|
1121
|
+
const elem = cell.elem;
|
1122
|
+
|
1123
|
+
if (selectedCellNodes.has(lexicalNode)) {
|
1124
|
+
cell.highlighted = true;
|
1125
|
+
elem.style.setProperty('background-color', 'rgb(172, 206, 247)');
|
1126
|
+
elem.style.setProperty('caret-color', 'transparent');
|
1127
|
+
highlightedCells.push(cell);
|
1128
|
+
} else {
|
1129
|
+
cell.highlighted = false;
|
1130
|
+
elem.style.removeProperty('background-color');
|
1131
|
+
elem.style.removeProperty('caret-color');
|
1132
|
+
|
1133
|
+
if (!elem.getAttribute('style')) {
|
1134
|
+
elem.removeAttribute('style');
|
1135
|
+
}
|
1136
|
+
}
|
1137
|
+
});
|
1138
|
+
return highlightedCells;
|
1139
|
+
}
|
1140
|
+
function $forEachGridCell(grid, cb) {
|
919
1141
|
const {
|
920
1142
|
cells
|
921
1143
|
} = grid;
|
922
|
-
const selectedCellNodes = new Set(gridSelection ? gridSelection.getNodes() : []);
|
923
1144
|
|
924
1145
|
for (let y = 0; y < cells.length; y++) {
|
925
1146
|
const row = cells[y];
|
926
1147
|
|
927
1148
|
for (let x = 0; x < row.length; x++) {
|
928
1149
|
const cell = row[x];
|
929
|
-
const elemStyle = cell.elem.style;
|
930
1150
|
const lexicalNode = lexical.$getNearestNodeFromDOMNode(cell.elem);
|
931
1151
|
|
932
|
-
if (lexicalNode
|
933
|
-
cell
|
934
|
-
|
935
|
-
|
936
|
-
|
937
|
-
} else {
|
938
|
-
cell.highlighted = false;
|
939
|
-
elemStyle.removeProperty('background-color');
|
940
|
-
elemStyle.removeProperty('caret-color');
|
941
|
-
|
942
|
-
if (!cell.elem.getAttribute('style')) {
|
943
|
-
cell.elem.removeAttribute('style');
|
944
|
-
}
|
1152
|
+
if (lexicalNode !== null) {
|
1153
|
+
cb(cell, lexicalNode, {
|
1154
|
+
x,
|
1155
|
+
y
|
1156
|
+
});
|
945
1157
|
}
|
946
1158
|
}
|
947
1159
|
}
|
948
|
-
|
949
|
-
|
1160
|
+
}
|
1161
|
+
function $addHighlightStyleToTable(tableSelection) {
|
1162
|
+
tableSelection.disableHighlightStyle();
|
1163
|
+
$forEachGridCell(tableSelection.grid, cell => {
|
1164
|
+
const elem = cell.elem;
|
1165
|
+
cell.highlighted = true;
|
1166
|
+
elem.style.setProperty('background-color', 'rgb(172, 206, 247)');
|
1167
|
+
elem.style.setProperty('caret-color', 'transparent');
|
1168
|
+
});
|
1169
|
+
}
|
1170
|
+
function $removeHighlightStyleToTable(tableSelection) {
|
1171
|
+
tableSelection.enableHighlightStyle();
|
1172
|
+
$forEachGridCell(tableSelection.grid, cell => {
|
1173
|
+
const elem = cell.elem;
|
1174
|
+
cell.highlighted = false;
|
1175
|
+
elem.style.removeProperty('background-color');
|
1176
|
+
elem.style.removeProperty('caret-color');
|
1177
|
+
|
1178
|
+
if (!elem.getAttribute('style')) {
|
1179
|
+
elem.removeAttribute('style');
|
1180
|
+
}
|
1181
|
+
});
|
950
1182
|
}
|
951
1183
|
|
952
1184
|
const selectGridNodeInDirection = (tableSelection, tableNode, x, y, direction) => {
|
@@ -1062,7 +1294,7 @@ class TableNode extends lexical.GridNode {
|
|
1062
1294
|
return new TableNode(node.__key);
|
1063
1295
|
}
|
1064
1296
|
|
1065
|
-
static
|
1297
|
+
static importDOM() {
|
1066
1298
|
return {
|
1067
1299
|
table: node => ({
|
1068
1300
|
conversion: convertTableElement,
|
@@ -1076,15 +1308,44 @@ class TableNode extends lexical.GridNode {
|
|
1076
1308
|
}
|
1077
1309
|
|
1078
1310
|
createDOM(config, editor) {
|
1079
|
-
const
|
1080
|
-
utils.addClassNamesToElement(
|
1081
|
-
return
|
1311
|
+
const tableElement = document.createElement('table');
|
1312
|
+
utils.addClassNamesToElement(tableElement, config.theme.table);
|
1313
|
+
return tableElement;
|
1082
1314
|
}
|
1083
1315
|
|
1084
1316
|
updateDOM() {
|
1085
1317
|
return false;
|
1086
1318
|
}
|
1087
1319
|
|
1320
|
+
exportDOM(editor) {
|
1321
|
+
return { ...super.exportDOM(editor),
|
1322
|
+
after: tableElement => {
|
1323
|
+
if (tableElement) {
|
1324
|
+
const newElement = tableElement.cloneNode();
|
1325
|
+
const colGroup = document.createElement('colgroup');
|
1326
|
+
const tBody = document.createElement('tbody');
|
1327
|
+
tBody.append(...tableElement.children);
|
1328
|
+
const firstRow = this.getFirstChildOrThrow();
|
1329
|
+
|
1330
|
+
if (!$isTableRowNode(firstRow)) {
|
1331
|
+
throw new Error('Expected to find row node.');
|
1332
|
+
}
|
1333
|
+
|
1334
|
+
const colCount = firstRow.getChildrenSize();
|
1335
|
+
|
1336
|
+
for (let i = 0; i < colCount; i++) {
|
1337
|
+
const col = document.createElement('col');
|
1338
|
+
colGroup.append(col);
|
1339
|
+
} //$FlowFixMe This function does exist and is supported by major browsers.
|
1340
|
+
|
1341
|
+
|
1342
|
+
newElement.replaceChildren(colGroup, tBody);
|
1343
|
+
return newElement;
|
1344
|
+
}
|
1345
|
+
}
|
1346
|
+
};
|
1347
|
+
}
|
1348
|
+
|
1088
1349
|
canExtractContents() {
|
1089
1350
|
return false;
|
1090
1351
|
}
|
@@ -1213,79 +1474,6 @@ function $isTableNode(node) {
|
|
1213
1474
|
return node instanceof TableNode;
|
1214
1475
|
}
|
1215
1476
|
|
1216
|
-
/**
|
1217
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
1218
|
-
*
|
1219
|
-
* This source code is licensed under the MIT license found in the
|
1220
|
-
* LICENSE file in the root directory of this source tree.
|
1221
|
-
*
|
1222
|
-
*
|
1223
|
-
*/
|
1224
|
-
class TableRowNode extends lexical.GridRowNode {
|
1225
|
-
static getType() {
|
1226
|
-
return 'tablerow';
|
1227
|
-
}
|
1228
|
-
|
1229
|
-
static clone(node) {
|
1230
|
-
return new TableRowNode(node.__height, node.__key);
|
1231
|
-
}
|
1232
|
-
|
1233
|
-
static convertDOM() {
|
1234
|
-
return {
|
1235
|
-
tr: node => ({
|
1236
|
-
conversion: convertTableRowElement,
|
1237
|
-
priority: 0
|
1238
|
-
})
|
1239
|
-
};
|
1240
|
-
}
|
1241
|
-
|
1242
|
-
constructor(height, key) {
|
1243
|
-
super(key);
|
1244
|
-
this.__height = height;
|
1245
|
-
}
|
1246
|
-
|
1247
|
-
createDOM(config) {
|
1248
|
-
const element = document.createElement('tr');
|
1249
|
-
|
1250
|
-
if (this.__height) {
|
1251
|
-
element.style.height = `${this.__height}px`;
|
1252
|
-
}
|
1253
|
-
|
1254
|
-
utils.addClassNamesToElement(element, config.theme.tableRow);
|
1255
|
-
return element;
|
1256
|
-
}
|
1257
|
-
|
1258
|
-
setHeight(height) {
|
1259
|
-
const self = this.getWritable();
|
1260
|
-
self.__height = height;
|
1261
|
-
return this.__height;
|
1262
|
-
}
|
1263
|
-
|
1264
|
-
getHeight() {
|
1265
|
-
return this.getLatest().__height;
|
1266
|
-
}
|
1267
|
-
|
1268
|
-
updateDOM(prevNode) {
|
1269
|
-
return prevNode.__height !== this.__height;
|
1270
|
-
}
|
1271
|
-
|
1272
|
-
canBeEmpty() {
|
1273
|
-
return false;
|
1274
|
-
}
|
1275
|
-
|
1276
|
-
}
|
1277
|
-
function convertTableRowElement(domNode) {
|
1278
|
-
return {
|
1279
|
-
node: $createTableRowNode()
|
1280
|
-
};
|
1281
|
-
}
|
1282
|
-
function $createTableRowNode(height) {
|
1283
|
-
return new TableRowNode(height);
|
1284
|
-
}
|
1285
|
-
function $isTableRowNode(node) {
|
1286
|
-
return node instanceof TableRowNode;
|
1287
|
-
}
|
1288
|
-
|
1289
1477
|
/**
|
1290
1478
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
1291
1479
|
*
|
package/LexicalTable.prod.js
CHANGED
@@ -4,45 +4,54 @@
|
|
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 g=require("lexical"),
|
8
|
-
class
|
9
|
-
return b}
|
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}}
|
11
|
-
function w(a){return
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
{this.
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
b
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
d
|
36
|
-
|
37
|
-
exports
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
d
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
7
|
+
var g=require("lexical"),p=require("@lexical/utils");const q={NO_STATUS:0,ROW:1,COLUMN:2,BOTH:3};
|
8
|
+
class u extends g.GridCellNode{static getType(){return"tablecell"}static clone(a){return new u(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,h){super(b,h);this.__headerState=a;this.__width=d}createDOM(a){const b=document.createElement(this.getTag());this.__width&&(b.style.width=`${this.__width}px`);p.addClassNamesToElement(b,a.theme.tableCell,this.hasHeader()&&a.theme.tableCellHeader);
|
9
|
+
return b}exportDOM(a){({element:a}=super.exportDOM(a));if(a){const 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}}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=
|
10
|
+
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()&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}}
|
11
|
+
function w(a){a=a.nodeName.toLowerCase();return{node:x("th"===a?q.ROW:q.NO_STATUS),forChild:(b,d)=>{if(y(d)&&!g.$isElementNode(b)){d=g.$createParagraphNode();if(g.$isLineBreakNode(b)&&"\n"===b.getTextContent())return null;d.append(b);return d}return b}}}function x(a,b=1,d){return new u(a,b,d)}function y(a){return a instanceof u}
|
12
|
+
function A(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.");}
|
13
|
+
class B extends g.GridRowNode{static getType(){return"tablerow"}static clone(a){return new B(a.__height,a.__key)}static importDOM(){return{tr:()=>({conversion:C,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`);p.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!==
|
14
|
+
this.__height}canBeEmpty(){return!1}}function C(){return{node:D()}}function D(a){return new B(a)}function E(a){return a instanceof B}
|
15
|
+
if("undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement){const 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 ";document.body&&
|
16
|
+
document.body.append(a)}
|
17
|
+
class F{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(){const a=new MutationObserver(b=>
|
18
|
+
{this.editor.update(()=>{var d=!1;for(let h=0;h<b.length;h++){const c=b[h].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=G(d)}})});this.editor.update(()=>{const b=this.editor.getElementByKey(this.tableNodeKey);if(!b)throw Error("Expected to find TableElement in DOM");this.grid=G(b);a.observe(b,{childList:!0,subtree:!0})})}clearHighlight(){this.editor.update(()=>{var a=
|
19
|
+
g.$getNodeByKey(this.tableNodeKey);if(!H(a))throw Error("Expected TableNode.");a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a=G(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;I(a,null);g.$setSelection(null);this.editor.dispatchCommand(g.SELECTION_CHANGE_COMMAND);this.enableHighlightStyle()})}enableHighlightStyle(){this.editor.update(()=>
|
20
|
+
{const 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(()=>{const a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a.classList.add("disable-selection");this.hasHijackedSelectionStyles=!0})}adjustFocusCellForSelection(a,b=!1){this.editor.update(()=>{var d=g.$getNodeByKey(this.tableNodeKey);
|
21
|
+
if(!H(d))throw Error("Expected TableNode.");if(!this.editor.getElementByKey(this.tableNodeKey))throw Error("Expected to find TableElement in DOM");d=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!==d||this.startY!==h||b))this.isHighlightingCells=!0,this.disableHighlightStyle();else if(d===this.currentX&&h===this.currentY)return;this.currentX=d;this.currentY=h;this.isHighlightingCells&&
|
22
|
+
(d=g.$getNearestNodeFromDOMNode(a.elem),null!=this.gridSelection&&null!=this.anchorCellNodeKey&&y(d)&&(d=d.getKey(),this.gridSelection=g.$createGridSelection(),this.focusCellNodeKey=d,this.gridSelection.set(this.tableNodeKey,this.anchorCellNodeKey,this.focusCellNodeKey),g.$setSelection(this.gridSelection),this.editor.dispatchCommand(g.SELECTION_CHANGE_COMMAND),I(this.grid,this.gridSelection)))})}setAnchorCellForSelection(a){this.editor.update(()=>{this.anchorCell=a;this.startX=a.x;this.startY=a.y;
|
23
|
+
window.getSelection().setBaseAndExtent(a.elem,0,a.elem,0);var b=g.$getNearestNodeFromDOMNode(a.elem);y(b)&&(b=b.getKey(),this.gridSelection=g.$createGridSelection(),this.anchorCellNodeKey=b)})}formatCells(a){this.editor.update(()=>{const b=g.$getSelection();g.$isGridSelection(b)||A(79);const d=g.$createRangeSelection(),h=d.anchor,c=d.focus;b.getNodes().forEach(k=>{y(k)&&0!==k.getTextContentSize()&&(h.set(k.getKey(),0,"element"),c.set(k.getKey(),k.getChildrenSize(),"element"),d.formatText(a))});g.$setSelection(b);
|
24
|
+
this.editor.dispatchCommand(g.SELECTION_CHANGE_COMMAND)})}clearText(){this.editor.update(()=>{const a=g.$getNodeByKey(this.tableNodeKey);if(!H(a))throw Error("Expected TableNode.");var b=g.$getSelection();g.$isGridSelection(b)||A(79);b=b.getNodes().filter(y);b.length===this.grid.columns*this.grid.rows?(a.selectPrevious(),a.remove(),this.clearHighlight()):(b.forEach(d=>{if(g.$isElementNode(d)){const h=g.$createParagraphNode(),c=g.$createTextNode();h.append(c);d.append(h);d.getChildren().forEach(k=>
|
25
|
+
{k!==h&&k.remove()})}}),I(this.grid,null),g.$setSelection(null),this.editor.dispatchCommand(g.SELECTION_CHANGE_COMMAND))})}}function J(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}
|
26
|
+
function G(a){const b=[],d={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}}d.columns=a+1;d.rows=c+1;return d}
|
27
|
+
function I(a,b){const d=[],h=new Set(b?b.getNodes():[]);M(a,(c,k)=>{const n=c.elem;h.has(k)?(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 M(a,b){({cells:a}=a);for(let d=0;d<a.length;d++){const h=a[d];for(let c=0;c<h.length;c++){const k=h[c],n=g.$getNearestNodeFromDOMNode(k.elem);null!==n&&b(k,n,{x:c,y:d})}}}function N(a){a.disableHighlightStyle();M(a.grid,b=>{const 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();M(a.grid,b=>{const d=b.elem;b.highlighted=!1;d.style.removeProperty("background-color");d.style.removeProperty("caret-color");d.getAttribute("style")||d.removeAttribute("style")})}
|
30
|
+
const Q=(a,b,d,h,c)=>{switch(c){case "backward":case "forward":return c="forward"===c,d!==(c?a.grid.columns-1:0)?P(b.getCellNodeFromCordsOrThrow(d+(c?1:-1),h,a.grid)):h!==(c?a.grid.rows-1:0)?P(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?P(b.getCellNodeFromCordsOrThrow(d,h-1,a.grid)):b.selectPrevious(),!0;case "down":return h!==a.grid.rows-1?P(b.getCellNodeFromCordsOrThrow(d,h+1,a.grid)):b.selectNext(),!0}return!1},
|
31
|
+
R=(a,b,d,h,c)=>{switch(c){case "backward":case "forward":return c="forward"===c,d!==(c?a.grid.columns-1:0)&&a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(d+(c?1:-1),h,a.grid)),!0;case "up":if(0!==h)return a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(d,h-1,a.grid)),!0;break;case "down":if(h!==a.grid.rows-1)return a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(d,h+1,a.grid)),!0}return!1};
|
32
|
+
function P(a){const b=a.getChildren().find(d=>g.$isParagraphNode(d));g.$isParagraphNode(b)?b.selectEnd():a.selectEnd()}
|
33
|
+
class S extends g.GridNode{static getType(){return"table"}static clone(a){return new S(a.__key)}static importDOM(){return{table:()=>({conversion:T,priority:0})}}constructor(a){super(a)}createDOM(a){const 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){const d=b.cloneNode(),h=document.createElement("colgroup"),c=document.createElement("tbody");c.append(...b.children);b=this.getFirstChildOrThrow();
|
34
|
+
if(!E(b))throw Error("Expected to find row node.");b=b.getChildrenSize();for(let k=0;k<b;k++){const n=document.createElement("col");h.append(n)}d.replaceChildren(h,c);return d}}}}canExtractContents(){return!1}canBeEmpty(){return!1}getCordsFromCellNode(a,b){b||A(55);const {rows:d,cells:h}=b;for(b=0;b<d;b++){var c=h[b];if(null==c)throw Error(`Row not found at y:${b}`);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,
|
35
|
+
b,d){d||A(55);({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=g.$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.");return a}canSelectBefore(){return!0}}
|
36
|
+
function T(){return{node:U()}}function U(){return new S}function H(a){return a instanceof S}function V(a){a=p.$findMatchingParent(a,b=>E(b));if(E(a))return a;throw Error("Expected table cell to be inside of table row.");}function W(a){a=p.$findMatchingParent(a,b=>H(b));if(H(a))return a;throw Error("Expected table cell to be inside of table.");}const X=g.createCommand();exports.$createTableCellNode=x;exports.$createTableNode=U;
|
37
|
+
exports.$createTableNodeWithDimensions=function(a,b,d=!0){const h=U();for(let k=0;k<a;k++){const n=D();for(let r=0;r<b;r++){var c=q.NO_STATUS;d&&(0===k&&(c|=q.ROW),0===r&&(c|=q.COLUMN));c=x(c);const v=g.$createParagraphNode();v.append(g.$createTextNode());c.append(v);n.append(c)}h.append(n)}return h};exports.$createTableRowNode=D;
|
38
|
+
exports.$deleteTableColumn=function(a,b){const d=a.getChildren();for(let c=0;c<d.length;c++){var h=d[c];if(E(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 G(a)};exports.$getTableCellNodeFromLexicalNode=function(a){a=p.$findMatchingParent(a,b=>y(b));return y(a)?a:null};
|
39
|
+
exports.$getTableColumnIndexFromTableCellNode=function(a){return V(a).getChildren().findIndex(b=>b.is(a))};exports.$getTableNodeFromLexicalNodeOrThrow=W;exports.$getTableRowIndexFromTableCellNode=function(a){const b=V(a);return W(b).getChildren().findIndex(d=>d.is(b))};exports.$getTableRowNodeFromTableCellNodeOrThrow=V;
|
40
|
+
exports.$insertTableColumn=function(a,b,d=!0,h){const c=a.getChildren();for(let r=0;r<c.length;r++){const v=c[r];if(E(v))for(let e=0;e<h;e++){var k=q.NO_STATUS;0===r&&(k|=q.ROW);k=x(k);k.append(g.$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(k):n.insertBefore(k)}}return a};
|
41
|
+
exports.$insertTableRow=function(a,b,d=!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(E(b))for(k=0;k<h;k++){const v=b.getChildren(),e=v.length,f=D();for(let l=0;l<e;l++){var n=v[l];y(n)||A(73);var r=c;const m=W(n),{x:t,y:z}=m.getCordsFromCellNode(n,r);n={above:m.getCellNodeFromCords(t,z-1,r),below:m.getCellNodeFromCords(t,z+1,r),left:m.getCellNodeFromCords(t-1,z,r),right:m.getCellNodeFromCords(t+1,z,r)};const {above:K,below:L}=n;n=q.NO_STATUS;
|
42
|
+
r=K&&K.getWidth()||L&&L.getWidth()||null;if(K&&K.hasHeaderState(q.COLUMN)||L&&L.hasHeaderState(q.COLUMN))n|=q.COLUMN;n=x(n,1,r);n.append(g.$createParagraphNode());f.append(n)}d?b.insertAfter(f):b.insertBefore(f)}else throw Error("Row before insertion index does not exist.");return a};exports.$isTableCellNode=y;exports.$isTableNode=H;exports.$isTableRowNode=E;
|
43
|
+
exports.$removeTableRowAtIndex=function(a,b){const 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=X;exports.TableCellHeaderStates=q;exports.TableCellNode=u;exports.TableNode=S;exports.TableRowNode=B;exports.TableSelection=F;
|
44
|
+
exports.applyTableHandlers=function(a,b,d){const h=d.getRootElement();if(null===h)throw Error("No root element.");const c=new F(d,a.getKey());b.__lexicalTableSelection=c;let k=!1,n=!1;b.addEventListener("dblclick",e=>{const f=J(e.target);null!==f&&(e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),c.setAnchorCellForSelection(f),c.adjustFocusCellForSelection(f,!0),k=!1)});b.addEventListener("mousedown",e=>{setTimeout(()=>{if(0===e.button){var f=J(e.target);null!==f&&(c.setAnchorCellForSelection(f),
|
45
|
+
document.addEventListener("mouseup",()=>{k=!1},{capture:!0,once:!0}))}},0)});b.addEventListener("mousemove",e=>{n&&(e.preventDefault(),e.stopPropagation(),e.stopImmediatePropagation());if(k){const f=J(e.target);if(null!==f){const l=f.x,m=f.y;k&&(c.startX!==l||c.startY!==m||c.isHighlightingCells)&&(e.preventDefault(),k=!0,c.adjustFocusCellForSelection(f))}}});b.addEventListener("mouseup",()=>{k&&(k=!1)});b.addEventListener("mouseleave",()=>{});const r=e=>{k=!0;0===e.button&&d.update(()=>{const f=g.$getSelection();
|
46
|
+
if(g.$isGridSelection(f)&&f.gridKey===c.tableNodeKey&&h.contains(e.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(g.KEY_ARROW_DOWN_COMMAND,e=>{var f=g.$getSelection();if(g.$isRangeSelection(f)){if(f.isCollapsed()){var l=p.$findMatchingParent(f.anchor.getNode(),
|
47
|
+
t=>y(t));if(!y(l))return!1;var m=a.getCordsFromCellNode(l,c.grid);f=p.$findMatchingParent(f.anchor.getNode(),t=>g.$isElementNode(t));if(null==f)throw Error("Expected BlockNode Parent");if((l=l.getLastChild())&&f.isParentOf(l)||f===l||e.shiftKey)return e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),e.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(g.$isGridSelection(f)&&e.shiftKey){m=f.focus.getNode();
|
48
|
+
if(!y(m))return!1;m=a.getCordsFromCellNode(m,c.grid);e.preventDefault();e.stopImmediatePropagation();e.stopPropagation();return R(c,a,m.x,m.y,"down")}return!1},4));c.listenersToRemove.add(d.registerCommand(g.KEY_ARROW_UP_COMMAND,e=>{var f=g.$getSelection();if(g.$isRangeSelection(f)){if(f.isCollapsed()){var l=p.$findMatchingParent(f.anchor.getNode(),t=>y(t));if(!y(l))return!1;var m=a.getCordsFromCellNode(l,c.grid);f=p.$findMatchingParent(f.anchor.getNode(),t=>g.$isElementNode(t));if(null==f)throw Error("Expected BlockNode Parent");
|
49
|
+
if((l=l.getLastChild())&&f.isParentOf(l)||f===l||e.shiftKey)return e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),e.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(g.$isGridSelection(f)&&e.shiftKey){m=f.focus.getNode();if(!y(m))return!1;m=a.getCordsFromCellNode(m,c.grid);e.preventDefault();e.stopImmediatePropagation();e.stopPropagation();return R(c,a,m.x,m.y,"up")}return!1},4));c.listenersToRemove.add(d.registerCommand(g.KEY_ARROW_LEFT_COMMAND,
|
50
|
+
e=>{var f=g.$getSelection();if(g.$isRangeSelection(f)){if(f.isCollapsed()){var l=p.$findMatchingParent(f.anchor.getNode(),m=>y(m));if(!y(l))return!1;l=a.getCordsFromCellNode(l,c.grid);if(null==p.$findMatchingParent(f.anchor.getNode(),m=>g.$isElementNode(m)))throw Error("Expected BlockNode Parent");if(0===f.anchor.offset||e.shiftKey)return e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),e.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(l.x,l.y,c.grid)),R(c,a,l.x,
|
51
|
+
l.y,"backward")):Q(c,a,l.x,l.y,"backward")}}else if(g.$isGridSelection(f)&&e.shiftKey){f=f.focus.getNode();if(!y(f))return!1;f=a.getCordsFromCellNode(f,c.grid);e.preventDefault();e.stopImmediatePropagation();e.stopPropagation();return R(c,a,f.x,f.y,"backward")}return!1},4));c.listenersToRemove.add(d.registerCommand(g.KEY_ARROW_RIGHT_COMMAND,e=>{var f=g.$getSelection();if(g.$isRangeSelection(f)){if(f.isCollapsed()){var l=p.$findMatchingParent(f.anchor.getNode(),m=>y(m));if(!y(l))return!1;l=a.getCordsFromCellNode(l,
|
52
|
+
c.grid);if(null==p.$findMatchingParent(f.anchor.getNode(),m=>g.$isElementNode(m)))throw Error("Expected BlockNode Parent");if(f.anchor.offset===f.anchor.getNode().getTextContentSize()||e.shiftKey)return e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),e.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(l.x,l.y,c.grid)),R(c,a,l.x,l.y,"forward")):Q(c,a,l.x,l.y,"forward")}}else if(g.$isGridSelection(f)&&e.shiftKey){f=f.focus.getNode();if(!y(f))return!1;f=a.getCordsFromCellNode(f,
|
53
|
+
c.grid);e.preventDefault();e.stopImmediatePropagation();e.stopPropagation();return R(c,a,f.x,f.y,"forward")}return!1},4));c.listenersToRemove.add(d.registerCommand(g.DELETE_CHARACTER_COMMAND,()=>{const e=g.$getSelection();if(g.$isGridSelection(e))return c.clearText(),!0;if(g.$isRangeSelection(e)){const f=p.$findMatchingParent(e.anchor.getNode(),l=>y(l));if(!y(f))return!1;if(e.isCollapsed()&&0===e.anchor.offset&&0===e.anchor.getNode().getPreviousSiblings().length)return!0}return!1},4));c.listenersToRemove.add(d.registerCommand(g.KEY_BACKSPACE_COMMAND,
|
54
|
+
e=>{const f=g.$getSelection();if(g.$isGridSelection(f))return e.preventDefault(),e.stopPropagation(),c.clearText(),!0;g.$isRangeSelection(f)&&(e=p.$findMatchingParent(f.anchor.getNode(),l=>y(l)),y(e));return!1},4));c.listenersToRemove.add(d.registerCommand(g.FORMAT_TEXT_COMMAND,e=>{const f=g.$getSelection();if(g.$isGridSelection(f))return c.formatCells(e),!0;g.$isRangeSelection(f)&&(e=p.$findMatchingParent(f.anchor.getNode(),l=>y(l)),y(e));return!1},4));c.listenersToRemove.add(d.registerCommand(g.INSERT_TEXT_COMMAND,
|
55
|
+
()=>{var e=g.$getSelection();g.$isGridSelection(e)?c.clearHighlight():g.$isRangeSelection(e)&&(e=p.$findMatchingParent(e.anchor.getNode(),f=>y(f)),y(e));return!1},4));c.listenersToRemove.add(d.registerCommand(g.KEY_TAB_COMMAND,e=>{var f=g.$getSelection();if(g.$isRangeSelection(f)){const l=p.$findMatchingParent(f.anchor.getNode(),m=>y(m));if(!y(l))return!1;if(f.isCollapsed())return f=a.getCordsFromCellNode(l,c.grid),e.preventDefault(),Q(c,a,f.x,f.y,e.shiftKey?"backward":"forward"),!0}return!1},4));
|
56
|
+
c.listenersToRemove.add(d.registerCommand(g.SELECTION_CHANGE_COMMAND,()=>{const e=g.$getSelection();if(e&&g.$isRangeSelection(e)&&!e.isCollapsed()){var f=e.anchor.getNode(),l=e.focus.getNode(),m=a.isParentOf(f),t=a.isParentOf(l);if(m&&!t||t&&!m){f=(m=e.isBackward())?l:f;l=g.$createRangeSelection();t=a.getIndexWithinParent();const z=a.getParentOrThrow().getKey();n=!0;(m?l.focus:l.anchor).set(f.getKey(),(m?e.focus:e.anchor).offset,g.$isTextNode(f)?"text":"element");(m?l.anchor:l.focus).set(z,m?t-1:
|
57
|
+
t+1,"element");g.$setSelection(l);N(c);return!0}}c.hasHijackedSelectionStyles&&!a.isSelected()?(O(c),n=!1):!c.hasHijackedSelectionStyles&&a.isSelected()&&N(c);return!1},4));return c};exports.getCellFromTarget=J;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.1
|
11
|
+
"version": "0.2.1",
|
12
12
|
"main": "LexicalTable.js",
|
13
13
|
"peerDependencies": {
|
14
|
-
"lexical": "0.1
|
14
|
+
"lexical": "0.2.1"
|
15
15
|
},
|
16
16
|
"dependencies": {
|
17
|
-
"@lexical/utils": "0.1
|
17
|
+
"@lexical/utils": "0.2.1"
|
18
18
|
},
|
19
19
|
"repository": {
|
20
20
|
"type": "git",
|