@lexical/table 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/LexicalTable.dev.js +909 -0
- package/LexicalTable.js +9 -0
- package/LexicalTable.prod.js +29 -0
- package/README.md +5 -0
- package/package.json +25 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2020 Dominic Gannaway
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
@@ -0,0 +1,909 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*/
|
7
|
+
'use strict';
|
8
|
+
|
9
|
+
var lexical = require('lexical');
|
10
|
+
|
11
|
+
/**
|
12
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
13
|
+
*
|
14
|
+
* This source code is licensed under the MIT license found in the
|
15
|
+
* LICENSE file in the root directory of this source tree.
|
16
|
+
*
|
17
|
+
*
|
18
|
+
*/
|
19
|
+
function addClassNamesToElement(element, ...classNames) {
|
20
|
+
classNames.forEach(className => {
|
21
|
+
if (className != null && typeof className === 'string') {
|
22
|
+
element.classList.add(...className.split(' '));
|
23
|
+
}
|
24
|
+
});
|
25
|
+
}
|
26
|
+
|
27
|
+
/**
|
28
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
29
|
+
*
|
30
|
+
* This source code is licensed under the MIT license found in the
|
31
|
+
* LICENSE file in the root directory of this source tree.
|
32
|
+
*
|
33
|
+
*
|
34
|
+
*/
|
35
|
+
class TableCellNode extends lexical.ElementNode {
|
36
|
+
static getType() {
|
37
|
+
return 'table-cell';
|
38
|
+
}
|
39
|
+
|
40
|
+
static clone(node) {
|
41
|
+
return new TableCellNode(false, node.__key);
|
42
|
+
}
|
43
|
+
|
44
|
+
constructor(isHeader = false, key) {
|
45
|
+
super(key);
|
46
|
+
this.__isHeader = isHeader;
|
47
|
+
}
|
48
|
+
|
49
|
+
getTag() {
|
50
|
+
return this.__isHeader ? 'th' : 'td';
|
51
|
+
}
|
52
|
+
|
53
|
+
createDOM(config) {
|
54
|
+
const element = document.createElement(this.getTag());
|
55
|
+
addClassNamesToElement(element, config.theme.tableCell, this.__isHeader === true && config.theme.tableCellHeader);
|
56
|
+
return element;
|
57
|
+
}
|
58
|
+
|
59
|
+
updateDOM() {
|
60
|
+
return false;
|
61
|
+
}
|
62
|
+
|
63
|
+
collapseAtStart() {
|
64
|
+
return true;
|
65
|
+
}
|
66
|
+
|
67
|
+
canBeEmpty() {
|
68
|
+
return false;
|
69
|
+
}
|
70
|
+
|
71
|
+
}
|
72
|
+
function $createTableCellNode(isHeader) {
|
73
|
+
return new TableCellNode(isHeader);
|
74
|
+
}
|
75
|
+
function $isTableCellNode(node) {
|
76
|
+
return node instanceof TableCellNode;
|
77
|
+
}
|
78
|
+
|
79
|
+
/**
|
80
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
81
|
+
*
|
82
|
+
* This source code is licensed under the MIT license found in the
|
83
|
+
* LICENSE file in the root directory of this source tree.
|
84
|
+
*
|
85
|
+
*
|
86
|
+
*/
|
87
|
+
function $findMatchingParent(startingNode, findFn) {
|
88
|
+
let curr = startingNode;
|
89
|
+
|
90
|
+
while (curr !== lexical.$getRoot() && curr != null) {
|
91
|
+
if (findFn(curr)) {
|
92
|
+
return curr;
|
93
|
+
}
|
94
|
+
|
95
|
+
curr = curr.getParent();
|
96
|
+
}
|
97
|
+
|
98
|
+
return null;
|
99
|
+
}
|
100
|
+
|
101
|
+
/**
|
102
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
103
|
+
*
|
104
|
+
* This source code is licensed under the MIT license found in the
|
105
|
+
* LICENSE file in the root directory of this source tree.
|
106
|
+
*
|
107
|
+
*
|
108
|
+
*/
|
109
|
+
const LowPriority = 1;
|
110
|
+
const CriticalPriority = 4;
|
111
|
+
const removeHighlightStyle = document.createElement('style');
|
112
|
+
removeHighlightStyle.appendChild(document.createTextNode('::selection{background-color: transparent}'));
|
113
|
+
|
114
|
+
function getCellFromTarget(node) {
|
115
|
+
let currentNode = node;
|
116
|
+
|
117
|
+
while (currentNode != null) {
|
118
|
+
const nodeName = currentNode.nodeName;
|
119
|
+
|
120
|
+
if (nodeName === 'TD' || nodeName === 'TH') {
|
121
|
+
// $FlowFixMe: internal field
|
122
|
+
const cell = currentNode._cell;
|
123
|
+
|
124
|
+
if (cell === undefined) {
|
125
|
+
return null;
|
126
|
+
}
|
127
|
+
|
128
|
+
return cell;
|
129
|
+
}
|
130
|
+
|
131
|
+
currentNode = currentNode.parentNode;
|
132
|
+
}
|
133
|
+
|
134
|
+
return null;
|
135
|
+
}
|
136
|
+
|
137
|
+
function trackTableGrid(tableNode, tableElement, editor) {
|
138
|
+
const cells = [];
|
139
|
+
const grid = {
|
140
|
+
cells,
|
141
|
+
columns: 0,
|
142
|
+
rows: 0
|
143
|
+
};
|
144
|
+
const observer = new MutationObserver(records => {
|
145
|
+
editor.update(() => {
|
146
|
+
let currentNode = tableElement.firstChild;
|
147
|
+
let x = 0;
|
148
|
+
let y = 0;
|
149
|
+
let gridNeedsRedraw = false;
|
150
|
+
|
151
|
+
for (let i = 0; i < records.length; i++) {
|
152
|
+
const record = records[i];
|
153
|
+
const target = record.target;
|
154
|
+
const nodeName = target.nodeName;
|
155
|
+
|
156
|
+
if (nodeName === 'TABLE' || nodeName === 'TR') {
|
157
|
+
gridNeedsRedraw = true;
|
158
|
+
break;
|
159
|
+
}
|
160
|
+
}
|
161
|
+
|
162
|
+
if (!gridNeedsRedraw) {
|
163
|
+
return;
|
164
|
+
}
|
165
|
+
|
166
|
+
cells.length = 0;
|
167
|
+
|
168
|
+
while (currentNode != null) {
|
169
|
+
const nodeMame = currentNode.nodeName;
|
170
|
+
|
171
|
+
if (nodeMame === 'TD' || nodeMame === 'TH') {
|
172
|
+
// $FlowFixMe: TD is always an HTMLElement
|
173
|
+
const elem = currentNode;
|
174
|
+
const cell = {
|
175
|
+
elem,
|
176
|
+
highlighted: false,
|
177
|
+
x,
|
178
|
+
y
|
179
|
+
}; // $FlowFixMe: internal field
|
180
|
+
|
181
|
+
currentNode._cell = cell;
|
182
|
+
|
183
|
+
if (cells[y] === undefined) {
|
184
|
+
cells[y] = [];
|
185
|
+
}
|
186
|
+
|
187
|
+
cells[y][x] = cell;
|
188
|
+
} else {
|
189
|
+
const child = currentNode.firstChild;
|
190
|
+
|
191
|
+
if (child != null) {
|
192
|
+
currentNode = child;
|
193
|
+
continue;
|
194
|
+
}
|
195
|
+
}
|
196
|
+
|
197
|
+
const sibling = currentNode.nextSibling;
|
198
|
+
|
199
|
+
if (sibling != null) {
|
200
|
+
x++;
|
201
|
+
currentNode = sibling;
|
202
|
+
continue;
|
203
|
+
}
|
204
|
+
|
205
|
+
const parent = currentNode.parentNode;
|
206
|
+
|
207
|
+
if (parent != null) {
|
208
|
+
const parentSibling = parent.nextSibling;
|
209
|
+
|
210
|
+
if (parentSibling == null) {
|
211
|
+
break;
|
212
|
+
}
|
213
|
+
|
214
|
+
y++;
|
215
|
+
x = 0;
|
216
|
+
currentNode = parentSibling;
|
217
|
+
}
|
218
|
+
}
|
219
|
+
|
220
|
+
grid.columns = x + 1;
|
221
|
+
grid.rows = y + 1;
|
222
|
+
tableNode.setGrid(grid);
|
223
|
+
});
|
224
|
+
});
|
225
|
+
observer.observe(tableElement, {
|
226
|
+
childList: true,
|
227
|
+
subtree: true
|
228
|
+
});
|
229
|
+
tableNode.setGrid(grid);
|
230
|
+
return grid;
|
231
|
+
}
|
232
|
+
|
233
|
+
function updateCells(fromX, toX, fromY, toY, cells) {
|
234
|
+
const highlighted = [];
|
235
|
+
|
236
|
+
for (let y = 0; y < cells.length; y++) {
|
237
|
+
const row = cells[y];
|
238
|
+
|
239
|
+
for (let x = 0; x < row.length; x++) {
|
240
|
+
const cell = row[x];
|
241
|
+
const elemStyle = cell.elem.style;
|
242
|
+
|
243
|
+
if (x >= fromX && x <= toX && y >= fromY && y <= toY) {
|
244
|
+
if (!cell.highlighted) {
|
245
|
+
cell.highlighted = true;
|
246
|
+
elemStyle.setProperty('background-color', 'rgb(163, 187, 255)');
|
247
|
+
elemStyle.setProperty('caret-color', 'transparent');
|
248
|
+
}
|
249
|
+
|
250
|
+
highlighted.push(cell);
|
251
|
+
} else if (cell.highlighted) {
|
252
|
+
cell.highlighted = false;
|
253
|
+
elemStyle.removeProperty('background-color');
|
254
|
+
elemStyle.removeProperty('caret-color');
|
255
|
+
}
|
256
|
+
}
|
257
|
+
}
|
258
|
+
|
259
|
+
return highlighted;
|
260
|
+
}
|
261
|
+
|
262
|
+
function applyCustomTableHandlers(tableNode, tableElement, editor) {
|
263
|
+
const rootElement = editor.getRootElement();
|
264
|
+
|
265
|
+
if (rootElement === null) {
|
266
|
+
return;
|
267
|
+
}
|
268
|
+
|
269
|
+
trackTableGrid(tableNode, tableElement, editor);
|
270
|
+
const grid = tableNode.getGrid();
|
271
|
+
let isSelected = false;
|
272
|
+
let isHighlightingCells = false;
|
273
|
+
let startX = -1;
|
274
|
+
let startY = -1;
|
275
|
+
let currentX = -1;
|
276
|
+
let currentY = -1;
|
277
|
+
let highlightedCells = [];
|
278
|
+
|
279
|
+
if (grid == null) {
|
280
|
+
throw new Error('Table grid not found.');
|
281
|
+
}
|
282
|
+
|
283
|
+
tableElement.addEventListener('mousemove', event => {
|
284
|
+
if (isSelected) {
|
285
|
+
// $FlowFixMe: event.target is always a Node on the DOM
|
286
|
+
const cell = getCellFromTarget(event.target);
|
287
|
+
|
288
|
+
if (cell !== null) {
|
289
|
+
const cellX = cell.x;
|
290
|
+
const cellY = cell.y;
|
291
|
+
|
292
|
+
if (!isHighlightingCells && (startX !== cellX || startY !== cellY)) {
|
293
|
+
event.preventDefault();
|
294
|
+
const windowSelection = window.getSelection(); // Collapse the selection
|
295
|
+
|
296
|
+
windowSelection.setBaseAndExtent(windowSelection.anchorNode, 0, windowSelection.anchorNode, 0);
|
297
|
+
isHighlightingCells = true;
|
298
|
+
|
299
|
+
if (document.body) {
|
300
|
+
document.body.appendChild(removeHighlightStyle);
|
301
|
+
}
|
302
|
+
|
303
|
+
if (deleteCharacterListener === null) {
|
304
|
+
deleteCharacterListener = editor.addListener('command', (type, payload) => {
|
305
|
+
if (type === 'deleteCharacter') {
|
306
|
+
if (highlightedCells.length === grid.columns * grid.rows) {
|
307
|
+
tableNode.selectPrevious(); // Delete entire table
|
308
|
+
|
309
|
+
tableNode.remove();
|
310
|
+
clearHighlight();
|
311
|
+
return true;
|
312
|
+
}
|
313
|
+
|
314
|
+
highlightedCells.forEach(({
|
315
|
+
elem
|
316
|
+
}) => {
|
317
|
+
const cellNode = lexical.$getNearestNodeFromDOMNode(elem);
|
318
|
+
|
319
|
+
if (lexical.$isElementNode(cellNode)) {
|
320
|
+
cellNode.clear();
|
321
|
+
const paragraphNode = lexical.$createParagraphNode();
|
322
|
+
const textNode = lexical.$createTextNode();
|
323
|
+
paragraphNode.append(textNode);
|
324
|
+
cellNode.append(paragraphNode);
|
325
|
+
}
|
326
|
+
});
|
327
|
+
tableNode.setSelectionState(null);
|
328
|
+
lexical.$setSelection(null);
|
329
|
+
return true;
|
330
|
+
} else if (type === 'formatText') {
|
331
|
+
formatCells(payload);
|
332
|
+
return true;
|
333
|
+
} else if (type === 'insertText') {
|
334
|
+
clearHighlight();
|
335
|
+
return false;
|
336
|
+
}
|
337
|
+
|
338
|
+
return false;
|
339
|
+
}, LowPriority);
|
340
|
+
}
|
341
|
+
} else if (cellX === currentX && cellY === currentY) {
|
342
|
+
return;
|
343
|
+
}
|
344
|
+
|
345
|
+
currentX = cellX;
|
346
|
+
currentY = cellY;
|
347
|
+
|
348
|
+
if (isHighlightingCells) {
|
349
|
+
const fromX = Math.min(startX, currentX);
|
350
|
+
const toX = Math.max(startX, currentX);
|
351
|
+
const fromY = Math.min(startY, currentY);
|
352
|
+
const toY = Math.max(startY, currentY);
|
353
|
+
editor.update(() => {
|
354
|
+
highlightedCells = tableNode.setSelectionState({
|
355
|
+
fromX,
|
356
|
+
fromY,
|
357
|
+
toX,
|
358
|
+
toY
|
359
|
+
});
|
360
|
+
});
|
361
|
+
}
|
362
|
+
}
|
363
|
+
}
|
364
|
+
});
|
365
|
+
|
366
|
+
const clearHighlight = () => {
|
367
|
+
editor.update(() => {
|
368
|
+
isHighlightingCells = false;
|
369
|
+
isSelected = false;
|
370
|
+
startX = -1;
|
371
|
+
startY = -1;
|
372
|
+
currentX = -1;
|
373
|
+
currentY = -1;
|
374
|
+
editor.update(() => {
|
375
|
+
tableNode.setSelectionState(null);
|
376
|
+
});
|
377
|
+
highlightedCells = [];
|
378
|
+
|
379
|
+
if (deleteCharacterListener !== null) {
|
380
|
+
deleteCharacterListener();
|
381
|
+
deleteCharacterListener = null;
|
382
|
+
}
|
383
|
+
|
384
|
+
const parent = removeHighlightStyle.parentNode;
|
385
|
+
|
386
|
+
if (parent != null) {
|
387
|
+
parent.removeChild(removeHighlightStyle);
|
388
|
+
}
|
389
|
+
});
|
390
|
+
};
|
391
|
+
|
392
|
+
tableElement.addEventListener('mouseleave', event => {
|
393
|
+
if (isSelected) {
|
394
|
+
return;
|
395
|
+
}
|
396
|
+
});
|
397
|
+
|
398
|
+
const formatCells = type => {
|
399
|
+
let selection = lexical.$getSelection();
|
400
|
+
|
401
|
+
if (selection === null) {
|
402
|
+
selection = lexical.$createRangeSelection();
|
403
|
+
} // This is to make Flow play ball.
|
404
|
+
|
405
|
+
|
406
|
+
const formatSelection = selection;
|
407
|
+
const anchor = formatSelection.anchor;
|
408
|
+
const focus = formatSelection.focus;
|
409
|
+
highlightedCells.forEach(highlightedCell => {
|
410
|
+
const cellNode = lexical.$getNearestNodeFromDOMNode(highlightedCell.elem);
|
411
|
+
|
412
|
+
if (lexical.$isElementNode(cellNode)) {
|
413
|
+
anchor.set(cellNode.getKey(), 0, 'element');
|
414
|
+
focus.set(cellNode.getKey(), cellNode.getChildrenSize(), 'element');
|
415
|
+
formatSelection.formatText(type);
|
416
|
+
}
|
417
|
+
}); // Collapse selection
|
418
|
+
|
419
|
+
selection.anchor.set(selection.anchor.key, selection.anchor.offset, selection.anchor.type);
|
420
|
+
selection.focus.set(selection.anchor.key, selection.anchor.offset, selection.anchor.type);
|
421
|
+
lexical.$setSelection(selection);
|
422
|
+
};
|
423
|
+
|
424
|
+
let deleteCharacterListener = null;
|
425
|
+
tableElement.addEventListener('mousedown', event => {
|
426
|
+
if (isSelected) {
|
427
|
+
if (isHighlightingCells) {
|
428
|
+
clearHighlight();
|
429
|
+
}
|
430
|
+
|
431
|
+
return;
|
432
|
+
}
|
433
|
+
|
434
|
+
setTimeout(() => {
|
435
|
+
if (isHighlightingCells) {
|
436
|
+
clearHighlight();
|
437
|
+
} // $FlowFixMe: event.target is always a Node on the DOM
|
438
|
+
|
439
|
+
|
440
|
+
const cell = getCellFromTarget(event.target);
|
441
|
+
|
442
|
+
if (cell !== null) {
|
443
|
+
isSelected = true;
|
444
|
+
startX = cell.x;
|
445
|
+
startY = cell.y;
|
446
|
+
document.addEventListener('mouseup', () => {
|
447
|
+
isSelected = false;
|
448
|
+
}, {
|
449
|
+
capture: true,
|
450
|
+
once: true
|
451
|
+
});
|
452
|
+
}
|
453
|
+
}, 0);
|
454
|
+
});
|
455
|
+
window.addEventListener('click', e => {
|
456
|
+
if (highlightedCells.length > 0 && !tableElement.contains(e.target)) {
|
457
|
+
editor.update(() => {
|
458
|
+
tableNode.setSelectionState(null);
|
459
|
+
});
|
460
|
+
}
|
461
|
+
});
|
462
|
+
|
463
|
+
const selectGridNodeInDirection = (x, y, direction) => {
|
464
|
+
switch (direction) {
|
465
|
+
case 'backward':
|
466
|
+
case 'forward':
|
467
|
+
{
|
468
|
+
const isForward = direction === 'forward';
|
469
|
+
|
470
|
+
if (y !== (isForward ? grid.columns - 1 : 0)) {
|
471
|
+
tableNode.getCellNodeFromCords(x, y + (isForward ? 1 : -1)).select();
|
472
|
+
} else {
|
473
|
+
if (x !== (isForward ? grid.rows - 1 : 0)) {
|
474
|
+
tableNode.getCellNodeFromCords(x + (isForward ? 1 : -1), isForward ? 0 : grid.columns - 1).select();
|
475
|
+
} else if (!isForward) {
|
476
|
+
tableNode.selectPrevious();
|
477
|
+
} else {
|
478
|
+
tableNode.selectNext();
|
479
|
+
}
|
480
|
+
}
|
481
|
+
|
482
|
+
return true;
|
483
|
+
}
|
484
|
+
|
485
|
+
case 'up':
|
486
|
+
{
|
487
|
+
if (x !== 0) {
|
488
|
+
tableNode.getCellNodeFromCords(x - 1, y).select();
|
489
|
+
} else {
|
490
|
+
tableNode.selectPrevious();
|
491
|
+
}
|
492
|
+
|
493
|
+
return true;
|
494
|
+
}
|
495
|
+
|
496
|
+
case 'down':
|
497
|
+
{
|
498
|
+
if (x !== grid.rows - 1) {
|
499
|
+
tableNode.getCellNodeFromCords(x + 1, y).select();
|
500
|
+
} else {
|
501
|
+
tableNode.selectNext();
|
502
|
+
}
|
503
|
+
|
504
|
+
return true;
|
505
|
+
}
|
506
|
+
}
|
507
|
+
|
508
|
+
return false;
|
509
|
+
};
|
510
|
+
|
511
|
+
editor.addListener('command', (type, payload) => {
|
512
|
+
const selection = lexical.$getSelection();
|
513
|
+
|
514
|
+
if (selection == null) {
|
515
|
+
return false;
|
516
|
+
}
|
517
|
+
|
518
|
+
const tableCellNode = $findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
|
519
|
+
|
520
|
+
if (!$isTableCellNode(tableCellNode)) {
|
521
|
+
return false;
|
522
|
+
}
|
523
|
+
|
524
|
+
if (type === 'deleteCharacter') {
|
525
|
+
if (highlightedCells.length === 0 && selection.isCollapsed() && selection.anchor.offset === 0) {
|
526
|
+
return true;
|
527
|
+
}
|
528
|
+
}
|
529
|
+
|
530
|
+
if (type === 'indentContent' || type === 'outdentContent') {
|
531
|
+
if (selection.isCollapsed() && highlightedCells.length === 0) {
|
532
|
+
const currentCords = tableNode.getCordsFromCellNode(tableCellNode);
|
533
|
+
selectGridNodeInDirection(currentCords.x, currentCords.y, type === 'indentContent' ? 'forward' : 'backward');
|
534
|
+
return true;
|
535
|
+
}
|
536
|
+
}
|
537
|
+
|
538
|
+
if (type === 'keyArrowDown' || type === 'keyArrowUp') {
|
539
|
+
const event = payload;
|
540
|
+
|
541
|
+
if (selection.isCollapsed() && highlightedCells.length === 0) {
|
542
|
+
const currentCords = tableNode.getCordsFromCellNode(tableCellNode);
|
543
|
+
const elementParentNode = $findMatchingParent(selection.anchor.getNode(), n => lexical.$isElementNode(n));
|
544
|
+
|
545
|
+
if (type === 'keyArrowUp' && elementParentNode === tableCellNode.getFirstChild() || type === 'keyArrowDown' && elementParentNode === tableCellNode.getLastChild()) {
|
546
|
+
event.preventDefault();
|
547
|
+
event.stopImmediatePropagation();
|
548
|
+
selectGridNodeInDirection(currentCords.x, currentCords.y, type === 'keyArrowUp' ? 'up' : 'down');
|
549
|
+
return true;
|
550
|
+
}
|
551
|
+
}
|
552
|
+
}
|
553
|
+
|
554
|
+
return false;
|
555
|
+
}, CriticalPriority);
|
556
|
+
}
|
557
|
+
|
558
|
+
class TableNode extends lexical.ElementNode {
|
559
|
+
static getType() {
|
560
|
+
return 'table';
|
561
|
+
}
|
562
|
+
|
563
|
+
static clone(node, selectionShape, grid) {
|
564
|
+
return new TableNode(node.__key, node.__selectionShape, node.__grid);
|
565
|
+
}
|
566
|
+
|
567
|
+
constructor(key, selectionShape, grid) {
|
568
|
+
super(key);
|
569
|
+
this.__selectionShape = selectionShape;
|
570
|
+
this.__grid = grid;
|
571
|
+
}
|
572
|
+
|
573
|
+
createDOM(config, editor) {
|
574
|
+
const element = document.createElement('table');
|
575
|
+
addClassNamesToElement(element, config.theme.table);
|
576
|
+
applyCustomTableHandlers(this, element, editor);
|
577
|
+
return element;
|
578
|
+
}
|
579
|
+
|
580
|
+
updateDOM() {
|
581
|
+
return false;
|
582
|
+
}
|
583
|
+
|
584
|
+
canExtractContents() {
|
585
|
+
return false;
|
586
|
+
}
|
587
|
+
|
588
|
+
canBeEmpty() {
|
589
|
+
return false;
|
590
|
+
}
|
591
|
+
|
592
|
+
setSelectionState(selectionShape) {
|
593
|
+
const self = this.getWritable();
|
594
|
+
self.__selectionShape = selectionShape;
|
595
|
+
if (this.__grid == null) return [];
|
596
|
+
|
597
|
+
if (!selectionShape) {
|
598
|
+
return updateCells(-1, -1, -1, -1, this.__grid.cells);
|
599
|
+
}
|
600
|
+
|
601
|
+
return updateCells(selectionShape.fromX, selectionShape.toX, selectionShape.fromY, selectionShape.toY, this.__grid.cells);
|
602
|
+
}
|
603
|
+
|
604
|
+
getSelectionState() {
|
605
|
+
return this.__selectionShape;
|
606
|
+
}
|
607
|
+
|
608
|
+
getCordsFromCellNode(tableCellNode) {
|
609
|
+
if (!this.__grid) {
|
610
|
+
throw Error(`Grid not found.`);
|
611
|
+
}
|
612
|
+
|
613
|
+
const {
|
614
|
+
rows,
|
615
|
+
cells
|
616
|
+
} = this.__grid;
|
617
|
+
|
618
|
+
for (let x = 0; x < rows; x++) {
|
619
|
+
const row = cells[x];
|
620
|
+
|
621
|
+
if (row == null) {
|
622
|
+
throw new Error(`Row not found at x:${x}`);
|
623
|
+
}
|
624
|
+
|
625
|
+
const y = row.findIndex(({
|
626
|
+
elem
|
627
|
+
}) => {
|
628
|
+
const cellNode = lexical.$getNearestNodeFromDOMNode(elem);
|
629
|
+
return cellNode === tableCellNode;
|
630
|
+
});
|
631
|
+
|
632
|
+
if (y !== -1) {
|
633
|
+
return {
|
634
|
+
x,
|
635
|
+
y
|
636
|
+
};
|
637
|
+
}
|
638
|
+
}
|
639
|
+
|
640
|
+
throw new Error('Cell not found in table.');
|
641
|
+
}
|
642
|
+
|
643
|
+
getCellNodeFromCords(x, y) {
|
644
|
+
if (!this.__grid) {
|
645
|
+
throw Error(`Grid not found.`);
|
646
|
+
}
|
647
|
+
|
648
|
+
const {
|
649
|
+
cells
|
650
|
+
} = this.__grid;
|
651
|
+
const row = cells[x];
|
652
|
+
|
653
|
+
if (row == null) {
|
654
|
+
throw new Error(`Table row x:"${x}" not found.`);
|
655
|
+
}
|
656
|
+
|
657
|
+
const cell = row[y];
|
658
|
+
|
659
|
+
if (cell == null) {
|
660
|
+
throw new Error(`Table cell y:"${y}" in row x:"${x}" not found.`);
|
661
|
+
}
|
662
|
+
|
663
|
+
const node = lexical.$getNearestNodeFromDOMNode(cell.elem);
|
664
|
+
|
665
|
+
if ($isTableCellNode(node)) {
|
666
|
+
return node;
|
667
|
+
}
|
668
|
+
|
669
|
+
throw new Error('Node at cords not TableCellNode.');
|
670
|
+
}
|
671
|
+
|
672
|
+
setGrid(grid) {
|
673
|
+
const self = this.getWritable();
|
674
|
+
self.__grid = grid;
|
675
|
+
}
|
676
|
+
|
677
|
+
getGrid() {
|
678
|
+
return this.__grid;
|
679
|
+
}
|
680
|
+
|
681
|
+
canSelectBefore() {
|
682
|
+
return true;
|
683
|
+
}
|
684
|
+
|
685
|
+
}
|
686
|
+
function $createTableNode() {
|
687
|
+
return new TableNode();
|
688
|
+
}
|
689
|
+
function $isTableNode(node) {
|
690
|
+
return node instanceof TableNode;
|
691
|
+
}
|
692
|
+
|
693
|
+
/**
|
694
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
695
|
+
*
|
696
|
+
* This source code is licensed under the MIT license found in the
|
697
|
+
* LICENSE file in the root directory of this source tree.
|
698
|
+
*
|
699
|
+
*
|
700
|
+
*/
|
701
|
+
class TableRowNode extends lexical.ElementNode {
|
702
|
+
static getType() {
|
703
|
+
return 'table-row';
|
704
|
+
}
|
705
|
+
|
706
|
+
static clone(node) {
|
707
|
+
return new TableRowNode(node.__key);
|
708
|
+
}
|
709
|
+
|
710
|
+
constructor(key) {
|
711
|
+
super(key);
|
712
|
+
}
|
713
|
+
|
714
|
+
createDOM(config) {
|
715
|
+
const element = document.createElement('tr');
|
716
|
+
addClassNamesToElement(element, config.theme.tableRow);
|
717
|
+
return element;
|
718
|
+
}
|
719
|
+
|
720
|
+
updateDOM() {
|
721
|
+
return false;
|
722
|
+
}
|
723
|
+
|
724
|
+
canBeEmpty() {
|
725
|
+
return false;
|
726
|
+
}
|
727
|
+
|
728
|
+
}
|
729
|
+
function $createTableRowNode() {
|
730
|
+
return new TableRowNode();
|
731
|
+
}
|
732
|
+
function $isTableRowNode(node) {
|
733
|
+
return node instanceof TableRowNode;
|
734
|
+
}
|
735
|
+
|
736
|
+
/**
|
737
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
738
|
+
*
|
739
|
+
* This source code is licensed under the MIT license found in the
|
740
|
+
* LICENSE file in the root directory of this source tree.
|
741
|
+
*
|
742
|
+
*
|
743
|
+
*/
|
744
|
+
function $createTableNodeWithDimensions(rowCount, columnCount, includeHeader = true) {
|
745
|
+
const tableNode = $createTableNode();
|
746
|
+
|
747
|
+
for (let iRow = 0; iRow < rowCount; iRow++) {
|
748
|
+
const tableRowNode = $createTableRowNode();
|
749
|
+
|
750
|
+
for (let iColumn = 0; iColumn < columnCount; iColumn++) {
|
751
|
+
const tableCellNode = $createTableCellNode(iRow === 0 && includeHeader);
|
752
|
+
const paragraphNode = lexical.$createParagraphNode();
|
753
|
+
paragraphNode.append(lexical.$createTextNode());
|
754
|
+
tableCellNode.append(paragraphNode);
|
755
|
+
tableRowNode.append(tableCellNode);
|
756
|
+
}
|
757
|
+
|
758
|
+
tableNode.append(tableRowNode);
|
759
|
+
}
|
760
|
+
|
761
|
+
return tableNode;
|
762
|
+
}
|
763
|
+
function $getTableCellNodeFromLexicalNode(startingNode) {
|
764
|
+
const node = $findMatchingParent(startingNode, n => $isTableCellNode(n));
|
765
|
+
|
766
|
+
if ($isTableCellNode(node)) {
|
767
|
+
return node;
|
768
|
+
}
|
769
|
+
|
770
|
+
return null;
|
771
|
+
}
|
772
|
+
function $getTableRowNodeFromTableCellNodeOrThrow(startingNode) {
|
773
|
+
const node = $findMatchingParent(startingNode, n => $isTableRowNode(n));
|
774
|
+
|
775
|
+
if ($isTableRowNode(node)) {
|
776
|
+
return node;
|
777
|
+
}
|
778
|
+
|
779
|
+
throw new Error('Expected table cell to be inside of table row.');
|
780
|
+
}
|
781
|
+
function $getTableNodeFromLexicalNodeOrThrow(startingNode) {
|
782
|
+
const node = $findMatchingParent(startingNode, n => $isTableNode(n));
|
783
|
+
|
784
|
+
if ($isTableNode(node)) {
|
785
|
+
return node;
|
786
|
+
}
|
787
|
+
|
788
|
+
throw new Error('Expected table cell to be inside of table.');
|
789
|
+
}
|
790
|
+
function $getTableRowIndexFromTableCellNode(tableCellNode) {
|
791
|
+
const tableRowNode = $getTableRowNodeFromTableCellNodeOrThrow(tableCellNode);
|
792
|
+
const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableRowNode);
|
793
|
+
return tableNode.getChildren().findIndex(n => n.is(tableRowNode));
|
794
|
+
}
|
795
|
+
function $getTableColumnIndexFromTableCellNode(tableCellNode) {
|
796
|
+
const tableRowNode = $getTableRowNodeFromTableCellNodeOrThrow(tableCellNode);
|
797
|
+
return tableRowNode.getChildren().findIndex(n => n.is(tableCellNode));
|
798
|
+
}
|
799
|
+
function $removeTableRowAtIndex(tableNode, indexToDelete) {
|
800
|
+
const tableRows = tableNode.getChildren();
|
801
|
+
|
802
|
+
if (indexToDelete >= tableRows.length || indexToDelete < 0) {
|
803
|
+
throw new Error('Expected table cell to be inside of table row.');
|
804
|
+
}
|
805
|
+
|
806
|
+
const targetRowNode = tableRows[indexToDelete];
|
807
|
+
targetRowNode.remove();
|
808
|
+
return tableNode;
|
809
|
+
}
|
810
|
+
function $insertTableRow(tableNode, targetIndex, shouldInsertAfter = true, rowCount) {
|
811
|
+
const tableRows = tableNode.getChildren();
|
812
|
+
|
813
|
+
if (targetIndex >= tableRows.length || targetIndex < 0) {
|
814
|
+
throw new Error('Table row target index out of range');
|
815
|
+
}
|
816
|
+
|
817
|
+
const targetRowNode = tableRows[targetIndex];
|
818
|
+
|
819
|
+
if ($isTableRowNode(targetRowNode)) {
|
820
|
+
for (let i = 0; i < rowCount; i++) {
|
821
|
+
const tableColumnCount = targetRowNode.getChildren().length;
|
822
|
+
const newTableRowNode = $createTableRowNode();
|
823
|
+
|
824
|
+
for (let j = 0; j < tableColumnCount; j++) {
|
825
|
+
const tableCellNode = $createTableCellNode(false);
|
826
|
+
tableCellNode.append(lexical.$createTextNode());
|
827
|
+
newTableRowNode.append(tableCellNode);
|
828
|
+
}
|
829
|
+
|
830
|
+
if (shouldInsertAfter) {
|
831
|
+
targetRowNode.insertAfter(newTableRowNode);
|
832
|
+
} else {
|
833
|
+
targetRowNode.insertBefore(newTableRowNode);
|
834
|
+
}
|
835
|
+
}
|
836
|
+
} else {
|
837
|
+
throw new Error('Row before insertion index does not exist.');
|
838
|
+
}
|
839
|
+
|
840
|
+
return tableNode;
|
841
|
+
}
|
842
|
+
function $insertTableColumn(tableNode, targetIndex, shouldInsertAfter = true, columnCount) {
|
843
|
+
const tableRows = tableNode.getChildren();
|
844
|
+
|
845
|
+
for (let i = 0; i < tableRows.length; i++) {
|
846
|
+
const currentTableRowNode = tableRows[i];
|
847
|
+
|
848
|
+
if ($isTableRowNode(currentTableRowNode)) {
|
849
|
+
for (let j = 0; j < columnCount; j++) {
|
850
|
+
const newTableCell = $createTableCellNode(i === 0);
|
851
|
+
newTableCell.append(lexical.$createTextNode());
|
852
|
+
const tableRowChildren = currentTableRowNode.getChildren();
|
853
|
+
|
854
|
+
if (targetIndex >= tableRowChildren.length || targetIndex < 0) {
|
855
|
+
throw new Error('Table column target index out of range');
|
856
|
+
}
|
857
|
+
|
858
|
+
const targetCell = tableRowChildren[targetIndex];
|
859
|
+
|
860
|
+
if (shouldInsertAfter) {
|
861
|
+
targetCell.insertAfter(newTableCell);
|
862
|
+
} else {
|
863
|
+
targetCell.insertBefore(newTableCell);
|
864
|
+
}
|
865
|
+
}
|
866
|
+
}
|
867
|
+
}
|
868
|
+
|
869
|
+
return tableNode;
|
870
|
+
}
|
871
|
+
function $deleteTableColumn(tableNode, targetIndex) {
|
872
|
+
const tableRows = tableNode.getChildren();
|
873
|
+
|
874
|
+
for (let i = 0; i < tableRows.length; i++) {
|
875
|
+
const currentTableRowNode = tableRows[i];
|
876
|
+
|
877
|
+
if ($isTableRowNode(currentTableRowNode)) {
|
878
|
+
const tableRowChildren = currentTableRowNode.getChildren();
|
879
|
+
|
880
|
+
if (targetIndex >= tableRowChildren.length || targetIndex < 0) {
|
881
|
+
throw new Error('Table column target index out of range');
|
882
|
+
}
|
883
|
+
|
884
|
+
tableRowChildren[targetIndex].remove();
|
885
|
+
}
|
886
|
+
}
|
887
|
+
|
888
|
+
return tableNode;
|
889
|
+
}
|
890
|
+
|
891
|
+
exports.$createTableCellNode = $createTableCellNode;
|
892
|
+
exports.$createTableNode = $createTableNode;
|
893
|
+
exports.$createTableNodeWithDimensions = $createTableNodeWithDimensions;
|
894
|
+
exports.$createTableRowNode = $createTableRowNode;
|
895
|
+
exports.$deleteTableColumn = $deleteTableColumn;
|
896
|
+
exports.$getTableCellNodeFromLexicalNode = $getTableCellNodeFromLexicalNode;
|
897
|
+
exports.$getTableColumnIndexFromTableCellNode = $getTableColumnIndexFromTableCellNode;
|
898
|
+
exports.$getTableNodeFromLexicalNodeOrThrow = $getTableNodeFromLexicalNodeOrThrow;
|
899
|
+
exports.$getTableRowIndexFromTableCellNode = $getTableRowIndexFromTableCellNode;
|
900
|
+
exports.$getTableRowNodeFromTableCellNodeOrThrow = $getTableRowNodeFromTableCellNodeOrThrow;
|
901
|
+
exports.$insertTableColumn = $insertTableColumn;
|
902
|
+
exports.$insertTableRow = $insertTableRow;
|
903
|
+
exports.$isTableCellNode = $isTableCellNode;
|
904
|
+
exports.$isTableNode = $isTableNode;
|
905
|
+
exports.$isTableRowNode = $isTableRowNode;
|
906
|
+
exports.$removeTableRowAtIndex = $removeTableRowAtIndex;
|
907
|
+
exports.TableCellNode = TableCellNode;
|
908
|
+
exports.TableNode = TableNode;
|
909
|
+
exports.TableRowNode = TableRowNode;
|
package/LexicalTable.js
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*/
|
7
|
+
'use strict'
|
8
|
+
const LexicalTable = process.env.NODE_ENV === 'development' ? require('./LexicalTable.dev.js') : require('./LexicalTable.prod.js')
|
9
|
+
module.exports = LexicalTable;
|
@@ -0,0 +1,29 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*/
|
7
|
+
var q=require("lexical");function x(a,...b){b.forEach(c=>{null!=c&&"string"===typeof c&&a.classList.add(...c.split(" "))})}
|
8
|
+
class y extends q.ElementNode{static getType(){return"table-cell"}static clone(a){return new y(!1,a.__key)}constructor(a=!1,b){super(b);this.__isHeader=a}getTag(){return this.__isHeader?"th":"td"}createDOM(a){const b=document.createElement(this.getTag());x(b,a.theme.tableCell,!0===this.__isHeader&&a.theme.tableCellHeader);return b}updateDOM(){return!1}collapseAtStart(){return!0}canBeEmpty(){return!1}}function z(a){return new y(a)}function B(a){return a instanceof y}
|
9
|
+
function C(a){throw Error(`Minified Lexical error #${a}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}function D(a,b){for(;a!==q.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null}const F=document.createElement("style");F.appendChild(document.createTextNode("::selection{background-color: transparent}"));
|
10
|
+
function G(a){for(;null!=a;){const b=a.nodeName;if("TD"===b||"TH"===b){a=a._cell;if(void 0===a)break;return a}a=a.parentNode}return null}
|
11
|
+
function H(a,b,c){const g=[],d={cells:g,columns:0,rows:0};(new MutationObserver(n=>{c.update(()=>{var e=b.firstChild;let p=0,m=0;var h=!1;for(let r=0;r<n.length;r++){const w=n[r].target.nodeName;if("TABLE"===w||"TR"===w){h=!0;break}}if(h){for(g.length=0;null!=e;){h=e.nodeName;if("TD"===h||"TH"===h)h={elem:e,highlighted:!1,x:p,y:m},e._cell=h,void 0===g[m]&&(g[m]=[]),g[m][p]=h;else if(h=e.firstChild,null!=h){e=h;continue}h=e.nextSibling;if(null!=h)p++,e=h;else if(h=e.parentNode,null!=h){e=h.nextSibling;
|
12
|
+
if(null==e)break;m++;p=0}}d.columns=p+1;d.rows=m+1;a.setGrid(d)}})})).observe(b,{childList:!0,subtree:!0});a.setGrid(d);return d}
|
13
|
+
function I(a,b,c,g,d){const n=[];for(let e=0;e<d.length;e++){const p=d[e];for(let m=0;m<p.length;m++){const h=p[m],r=h.elem.style;m>=a&&m<=b&&e>=c&&e<=g?(h.highlighted||(h.highlighted=!0,r.setProperty("background-color","rgb(163, 187, 255)"),r.setProperty("caret-color","transparent")),n.push(h)):h.highlighted&&(h.highlighted=!1,r.removeProperty("background-color"),r.removeProperty("caret-color"))}}return n}
|
14
|
+
function J(a,b,c){if(null!==c.getRootElement()){H(a,b,c);var g=a.getGrid(),d=!1,n=!1,e=-1,p=-1,m=-1,h=-1,r=[];if(null==g)throw Error("Table grid not found.");b.addEventListener("mousemove",k=>{if(d){var f=G(k.target);if(null!==f){const l=f.x;f=f.y;if(!n&&(e!==l||p!==f))k.preventDefault(),k=window.getSelection(),k.setBaseAndExtent(k.anchorNode,0,k.anchorNode,0),n=!0,document.body&&document.body.appendChild(F),null===A&&(A=c.addListener("command",(u,v)=>{if("deleteCharacter"===u){if(r.length===g.columns*
|
15
|
+
g.rows)return a.selectPrevious(),a.remove(),w(),!0;r.forEach(({elem:t})=>{t=q.$getNearestNodeFromDOMNode(t);if(q.$isElementNode(t)){t.clear();const E=q.$createParagraphNode(),T=q.$createTextNode();E.append(T);t.append(E)}});a.setSelectionState(null);q.$setSelection(null);return!0}if("formatText"===u)return U(v),!0;"insertText"===u&&w();return!1},1));else if(l===m&&f===h)return;m=l;h=f;if(n){const u=Math.min(e,m),v=Math.max(e,m),t=Math.min(p,h),E=Math.max(p,h);c.update(()=>{r=a.setSelectionState({fromX:u,
|
16
|
+
fromY:t,toX:v,toY:E})})}}}});var w=()=>{c.update(()=>{d=n=!1;h=m=p=e=-1;c.update(()=>{a.setSelectionState(null)});r=[];null!==A&&(A(),A=null);const k=F.parentNode;null!=k&&k.removeChild(F)})};b.addEventListener("mouseleave",()=>{});var U=k=>{let f=q.$getSelection();null===f&&(f=q.$createRangeSelection());const l=f,u=l.anchor,v=l.focus;r.forEach(t=>{t=q.$getNearestNodeFromDOMNode(t.elem);q.$isElementNode(t)&&(u.set(t.getKey(),0,"element"),v.set(t.getKey(),t.getChildrenSize(),"element"),l.formatText(k))});
|
17
|
+
f.anchor.set(f.anchor.key,f.anchor.offset,f.anchor.type);f.focus.set(f.anchor.key,f.anchor.offset,f.anchor.type);q.$setSelection(f)},A=null;b.addEventListener("mousedown",k=>{d?n&&w():setTimeout(()=>{n&&w();const f=G(k.target);null!==f&&(d=!0,e=f.x,p=f.y,document.addEventListener("mouseup",()=>{d=!1},{capture:!0,once:!0}))},0)});window.addEventListener("click",k=>{0<r.length&&!b.contains(k.target)&&c.update(()=>{a.setSelectionState(null)})});var O=(k,f,l)=>{switch(l){case "backward":case "forward":return l=
|
18
|
+
"forward"===l,f!==(l?g.columns-1:0)?a.getCellNodeFromCords(k,f+(l?1:-1)).select():k!==(l?g.rows-1:0)?a.getCellNodeFromCords(k+(l?1:-1),l?0:g.columns-1).select():l?a.selectNext():a.selectPrevious(),!0;case "up":return 0!==k?a.getCellNodeFromCords(k-1,f).select():a.selectPrevious(),!0;case "down":return k!==g.rows-1?a.getCellNodeFromCords(k+1,f).select():a.selectNext(),!0}return!1};c.addListener("command",(k,f)=>{var l=q.$getSelection();if(null==l)return!1;const u=D(l.anchor.getNode(),v=>B(v));if(!B(u))return!1;
|
19
|
+
if("deleteCharacter"===k&&0===r.length&&l.isCollapsed()&&0===l.anchor.offset)return!0;if(("indentContent"===k||"outdentContent"===k)&&l.isCollapsed()&&0===r.length)return f=a.getCordsFromCellNode(u),O(f.x,f.y,"indentContent"===k?"forward":"backward"),!0;if(("keyArrowDown"===k||"keyArrowUp"===k)&&l.isCollapsed()&&0===r.length){const v=a.getCordsFromCellNode(u);l=D(l.anchor.getNode(),t=>q.$isElementNode(t));if("keyArrowUp"===k&&l===u.getFirstChild()||"keyArrowDown"===k&&l===u.getLastChild())return f.preventDefault(),
|
20
|
+
f.stopImmediatePropagation(),O(v.x,v.y,"keyArrowUp"===k?"up":"down"),!0}return!1},4)}}
|
21
|
+
class K extends q.ElementNode{static getType(){return"table"}static clone(a){return new K(a.__key,a.__selectionShape,a.__grid)}constructor(a,b,c){super(a);this.__selectionShape=b;this.__grid=c}createDOM(a,b){const c=document.createElement("table");x(c,a.theme.table);J(this,c,b);return c}updateDOM(){return!1}canExtractContents(){return!1}canBeEmpty(){return!1}setSelectionState(a){this.getWritable().__selectionShape=a;return null==this.__grid?[]:a?I(a.fromX,a.toX,a.fromY,a.toY,this.__grid.cells):I(-1,
|
22
|
+
-1,-1,-1,this.__grid.cells)}getSelectionState(){return this.__selectionShape}getCordsFromCellNode(a){this.__grid||C(55);const {rows:b,cells:c}=this.__grid;for(let d=0;d<b;d++){var g=c[d];if(null==g)throw Error(`Row not found at x:${d}`);g=g.findIndex(({elem:n})=>q.$getNearestNodeFromDOMNode(n)===a);if(-1!==g)return{x:d,y:g}}throw Error("Cell not found in table.");}getCellNodeFromCords(a,b){this.__grid||C(55);var {cells:c}=this.__grid;c=c[a];if(null==c)throw Error(`Table row x:"${a}" not found.`);
|
23
|
+
c=c[b];if(null==c)throw Error(`Table cell y:"${b}" in row x:"${a}" not found.`);a=q.$getNearestNodeFromDOMNode(c.elem);if(B(a))return a;throw Error("Node at cords not TableCellNode.");}setGrid(a){this.getWritable().__grid=a}getGrid(){return this.__grid}canSelectBefore(){return!0}}function L(){return new K}function M(a){return a instanceof K}
|
24
|
+
class N extends q.ElementNode{static getType(){return"table-row"}static clone(a){return new N(a.__key)}constructor(a){super(a)}createDOM(a){const b=document.createElement("tr");x(b,a.theme.tableRow);return b}updateDOM(){return!1}canBeEmpty(){return!1}}function P(){return new N}function Q(a){return a instanceof N}function R(a){a=D(a,b=>Q(b));if(Q(a))return a;throw Error("Expected table cell to be inside of table row.");}
|
25
|
+
function S(a){a=D(a,b=>M(b));if(M(a))return a;throw Error("Expected table cell to be inside of table.");}exports.$createTableCellNode=z;exports.$createTableNode=L;exports.$createTableNodeWithDimensions=function(a,b,c=!0){const g=L();for(let d=0;d<a;d++){const n=P();for(let e=0;e<b;e++){const p=z(0===d&&c),m=q.$createParagraphNode();m.append(q.$createTextNode());p.append(m);n.append(p)}g.append(n)}return g};exports.$createTableRowNode=P;
|
26
|
+
exports.$deleteTableColumn=function(a,b){const c=a.getChildren();for(let d=0;d<c.length;d++){var g=c[d];if(Q(g)){g=g.getChildren();if(b>=g.length||0>b)throw Error("Table column target index out of range");g[b].remove()}}return a};exports.$getTableCellNodeFromLexicalNode=function(a){a=D(a,b=>B(b));return B(a)?a:null};exports.$getTableColumnIndexFromTableCellNode=function(a){return R(a).getChildren().findIndex(b=>b.is(a))};exports.$getTableNodeFromLexicalNodeOrThrow=S;
|
27
|
+
exports.$getTableRowIndexFromTableCellNode=function(a){const b=R(a);return S(b).getChildren().findIndex(c=>c.is(b))};exports.$getTableRowNodeFromTableCellNodeOrThrow=R;exports.$insertTableColumn=function(a,b,c=!0,g){const d=a.getChildren();for(let e=0;e<d.length;e++){const p=d[e];if(Q(p))for(let m=0;m<g;m++){const h=z(0===e);h.append(q.$createTextNode());var n=p.getChildren();if(b>=n.length||0>b)throw Error("Table column target index out of range");n=n[b];c?n.insertAfter(h):n.insertBefore(h)}}return a};
|
28
|
+
exports.$insertTableRow=function(a,b,c=!0,g){var d=a.getChildren();if(b>=d.length||0>b)throw Error("Table row target index out of range");b=d[b];if(Q(b))for(d=0;d<g;d++){const n=b.getChildren().length,e=P();for(let p=0;p<n;p++){const m=z(!1);m.append(q.$createTextNode());e.append(m)}c?b.insertAfter(e):b.insertBefore(e)}else throw Error("Row before insertion index does not exist.");return a};exports.$isTableCellNode=B;exports.$isTableNode=M;exports.$isTableRowNode=Q;
|
29
|
+
exports.$removeTableRowAtIndex=function(a,b){const c=a.getChildren();if(b>=c.length||0>b)throw Error("Expected table cell to be inside of table row.");c[b].remove();return a};exports.TableCellNode=y;exports.TableNode=K;exports.TableRowNode=N;
|
package/README.md
ADDED
package/package.json
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
{
|
2
|
+
"name": "@lexical/table",
|
3
|
+
"author": {
|
4
|
+
"name": "Dominic Gannaway",
|
5
|
+
"email": "dg@domgan.com"
|
6
|
+
},
|
7
|
+
"description": "This package provides the Table feature for Lexical.",
|
8
|
+
"keywords": [
|
9
|
+
"lexical",
|
10
|
+
"editor",
|
11
|
+
"rich-text",
|
12
|
+
"table"
|
13
|
+
],
|
14
|
+
"license": "MIT",
|
15
|
+
"version": "0.1.9",
|
16
|
+
"main": "LexicalTable.js",
|
17
|
+
"peerDependencies": {
|
18
|
+
"lexical": "0.1.9"
|
19
|
+
},
|
20
|
+
"repository": {
|
21
|
+
"type": "git",
|
22
|
+
"url": "https://github.com/facebook/lexical",
|
23
|
+
"directory": "packages/lexical-table"
|
24
|
+
}
|
25
|
+
}
|