@react-fabric/lexical 1.0.0-beta.47 → 1.0.0-beta.48
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/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +29 -19
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +1 -1
- package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +9 -9
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +142 -75
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +1 -1
- package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +25 -21
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +10 -3
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +1 -1
- package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +3 -3
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +147 -103
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +1 -1
- package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +10 -10
- package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +254 -178
- package/dist/cjs/node_modules/@lexical/list/LexicalList.js +1 -1
- package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +19 -16
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +53 -29
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +1 -1
- package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +10 -10
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +176 -194
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +1 -1
- package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +25 -18
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +1925 -793
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +1 -1
- package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +48 -40
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +426 -144
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +11 -1
- package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +46 -32
- package/dist/cjs/node_modules/prismjs/prism.js +1 -1
- package/dist/cjs/packages/lexical/src/editor/LexicalEditor.js +1 -1
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +29 -19
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +1 -1
- package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +139 -76
- package/dist/esm/node_modules/@lexical/code/LexicalCode.js +1 -1
- package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +4 -4
- package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +11 -4
- package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +1 -1
- package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +149 -105
- package/dist/esm/node_modules/@lexical/link/LexicalLink.js +1 -1
- package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +4 -4
- package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +253 -180
- package/dist/esm/node_modules/@lexical/list/LexicalList.js +1 -1
- package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +4 -4
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +54 -30
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +1 -1
- package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +3 -3
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +171 -196
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +1 -1
- package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +4 -4
- package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +1920 -796
- package/dist/esm/node_modules/@lexical/table/LexicalTable.js +1 -1
- package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +4 -4
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +413 -145
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +7 -2
- package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +3 -3
- package/dist/esm/node_modules/prismjs/prism.js +1 -1
- package/dist/esm/packages/lexical/src/editor/LexicalEditor.js +1 -1
- package/package.json +7 -8
- package/src/components/StickyComponent.tsx +1 -1
- package/src/editor/LexicalEditor.tsx +25 -21
- package/src/plugins/ToolbarPlugin.tsx +20 -4
|
@@ -53,14 +53,19 @@ class TableCellNode extends lexical.ElementNode {
|
|
|
53
53
|
|
|
54
54
|
/** @internal */
|
|
55
55
|
|
|
56
|
+
/** @internal */
|
|
57
|
+
|
|
56
58
|
static getType() {
|
|
57
59
|
return 'tablecell';
|
|
58
60
|
}
|
|
59
61
|
static clone(node) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
return new TableCellNode(node.__headerState, node.__colSpan, node.__width, node.__key);
|
|
63
|
+
}
|
|
64
|
+
afterCloneFrom(node) {
|
|
65
|
+
super.afterCloneFrom(node);
|
|
66
|
+
this.__rowSpan = node.__rowSpan;
|
|
67
|
+
this.__backgroundColor = node.__backgroundColor;
|
|
68
|
+
this.__verticalAlign = node.__verticalAlign;
|
|
64
69
|
}
|
|
65
70
|
static importDOM() {
|
|
66
71
|
return {
|
|
@@ -75,12 +80,10 @@ class TableCellNode extends lexical.ElementNode {
|
|
|
75
80
|
};
|
|
76
81
|
}
|
|
77
82
|
static importJSON(serializedNode) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
cellNode.__backgroundColor = serializedNode.backgroundColor || null;
|
|
83
|
-
return cellNode;
|
|
83
|
+
return $createTableCellNode().updateFromJSON(serializedNode);
|
|
84
|
+
}
|
|
85
|
+
updateFromJSON(serializedNode) {
|
|
86
|
+
return super.updateFromJSON(serializedNode).setHeaderStyles(serializedNode.headerState).setColSpan(serializedNode.colSpan || 1).setRowSpan(serializedNode.rowSpan || 1).setWidth(serializedNode.width || undefined).setBackgroundColor(serializedNode.backgroundColor || null).setVerticalAlign(serializedNode.verticalAlign || undefined);
|
|
84
87
|
}
|
|
85
88
|
constructor(headerState = TableCellHeaderStates.NO_STATUS, colSpan = 1, width, key) {
|
|
86
89
|
super(key);
|
|
@@ -89,6 +92,7 @@ class TableCellNode extends lexical.ElementNode {
|
|
|
89
92
|
this.__headerState = headerState;
|
|
90
93
|
this.__width = width;
|
|
91
94
|
this.__backgroundColor = null;
|
|
95
|
+
this.__verticalAlign = undefined;
|
|
92
96
|
}
|
|
93
97
|
createDOM(config) {
|
|
94
98
|
const element = document.createElement(this.getTag());
|
|
@@ -104,68 +108,69 @@ class TableCellNode extends lexical.ElementNode {
|
|
|
104
108
|
if (this.__backgroundColor !== null) {
|
|
105
109
|
element.style.backgroundColor = this.__backgroundColor;
|
|
106
110
|
}
|
|
111
|
+
if (isValidVerticalAlign(this.__verticalAlign)) {
|
|
112
|
+
element.style.verticalAlign = this.__verticalAlign;
|
|
113
|
+
}
|
|
107
114
|
LexicalUtils.addClassNamesToElement(element, config.theme.tableCell, this.hasHeader() && config.theme.tableCellHeader);
|
|
108
115
|
return element;
|
|
109
116
|
}
|
|
110
117
|
exportDOM(editor) {
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
element_.style.border = '1px solid black';
|
|
118
|
+
const output = super.exportDOM(editor);
|
|
119
|
+
if (lexical.isHTMLElement(output.element)) {
|
|
120
|
+
const element = output.element;
|
|
121
|
+
element.setAttribute('data-temporary-table-cell-lexical-key', this.getKey());
|
|
122
|
+
element.style.border = '1px solid black';
|
|
117
123
|
if (this.__colSpan > 1) {
|
|
118
|
-
|
|
124
|
+
element.colSpan = this.__colSpan;
|
|
119
125
|
}
|
|
120
126
|
if (this.__rowSpan > 1) {
|
|
121
|
-
|
|
127
|
+
element.rowSpan = this.__rowSpan;
|
|
122
128
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
element_.style.backgroundColor = backgroundColor;
|
|
129
|
-
} else if (this.hasHeader()) {
|
|
130
|
-
element_.style.backgroundColor = '#f2f3f5';
|
|
129
|
+
element.style.width = `${this.getWidth() || COLUMN_WIDTH}px`;
|
|
130
|
+
element.style.verticalAlign = this.getVerticalAlign() || 'top';
|
|
131
|
+
element.style.textAlign = 'start';
|
|
132
|
+
if (this.__backgroundColor === null && this.hasHeader()) {
|
|
133
|
+
element.style.backgroundColor = '#f2f3f5';
|
|
131
134
|
}
|
|
132
135
|
}
|
|
133
|
-
return
|
|
134
|
-
element
|
|
135
|
-
};
|
|
136
|
+
return output;
|
|
136
137
|
}
|
|
137
138
|
exportJSON() {
|
|
138
139
|
return {
|
|
139
140
|
...super.exportJSON(),
|
|
141
|
+
...(isValidVerticalAlign(this.__verticalAlign) && {
|
|
142
|
+
verticalAlign: this.__verticalAlign
|
|
143
|
+
}),
|
|
140
144
|
backgroundColor: this.getBackgroundColor(),
|
|
141
145
|
colSpan: this.__colSpan,
|
|
142
146
|
headerState: this.__headerState,
|
|
143
147
|
rowSpan: this.__rowSpan,
|
|
144
|
-
type: 'tablecell',
|
|
145
148
|
width: this.getWidth()
|
|
146
149
|
};
|
|
147
150
|
}
|
|
148
151
|
getColSpan() {
|
|
149
|
-
return this.__colSpan;
|
|
152
|
+
return this.getLatest().__colSpan;
|
|
150
153
|
}
|
|
151
154
|
setColSpan(colSpan) {
|
|
152
|
-
this.getWritable()
|
|
153
|
-
|
|
155
|
+
const self = this.getWritable();
|
|
156
|
+
self.__colSpan = colSpan;
|
|
157
|
+
return self;
|
|
154
158
|
}
|
|
155
159
|
getRowSpan() {
|
|
156
|
-
return this.__rowSpan;
|
|
160
|
+
return this.getLatest().__rowSpan;
|
|
157
161
|
}
|
|
158
162
|
setRowSpan(rowSpan) {
|
|
159
|
-
this.getWritable()
|
|
160
|
-
|
|
163
|
+
const self = this.getWritable();
|
|
164
|
+
self.__rowSpan = rowSpan;
|
|
165
|
+
return self;
|
|
161
166
|
}
|
|
162
167
|
getTag() {
|
|
163
168
|
return this.hasHeader() ? 'th' : 'td';
|
|
164
169
|
}
|
|
165
|
-
setHeaderStyles(headerState) {
|
|
170
|
+
setHeaderStyles(headerState, mask = TableCellHeaderStates.BOTH) {
|
|
166
171
|
const self = this.getWritable();
|
|
167
|
-
self.__headerState = headerState;
|
|
168
|
-
return
|
|
172
|
+
self.__headerState = headerState & mask | self.__headerState & ~mask;
|
|
173
|
+
return self;
|
|
169
174
|
}
|
|
170
175
|
getHeaderStyles() {
|
|
171
176
|
return this.getLatest().__headerState;
|
|
@@ -173,7 +178,7 @@ class TableCellNode extends lexical.ElementNode {
|
|
|
173
178
|
setWidth(width) {
|
|
174
179
|
const self = this.getWritable();
|
|
175
180
|
self.__width = width;
|
|
176
|
-
return
|
|
181
|
+
return self;
|
|
177
182
|
}
|
|
178
183
|
getWidth() {
|
|
179
184
|
return this.getLatest().__width;
|
|
@@ -182,7 +187,17 @@ class TableCellNode extends lexical.ElementNode {
|
|
|
182
187
|
return this.getLatest().__backgroundColor;
|
|
183
188
|
}
|
|
184
189
|
setBackgroundColor(newBackgroundColor) {
|
|
185
|
-
this.getWritable()
|
|
190
|
+
const self = this.getWritable();
|
|
191
|
+
self.__backgroundColor = newBackgroundColor;
|
|
192
|
+
return self;
|
|
193
|
+
}
|
|
194
|
+
getVerticalAlign() {
|
|
195
|
+
return this.getLatest().__verticalAlign;
|
|
196
|
+
}
|
|
197
|
+
setVerticalAlign(newVerticalAlign) {
|
|
198
|
+
const self = this.getWritable();
|
|
199
|
+
self.__verticalAlign = newVerticalAlign || undefined;
|
|
200
|
+
return self;
|
|
186
201
|
}
|
|
187
202
|
toggleHeaderStyle(headerStateToToggle) {
|
|
188
203
|
const self = this.getWritable();
|
|
@@ -200,7 +215,7 @@ class TableCellNode extends lexical.ElementNode {
|
|
|
200
215
|
return this.getLatest().__headerState !== TableCellHeaderStates.NO_STATUS;
|
|
201
216
|
}
|
|
202
217
|
updateDOM(prevNode) {
|
|
203
|
-
return prevNode.__headerState !== this.__headerState || prevNode.__width !== this.__width || prevNode.__colSpan !== this.__colSpan || prevNode.__rowSpan !== this.__rowSpan || prevNode.__backgroundColor !== this.__backgroundColor;
|
|
218
|
+
return prevNode.__headerState !== this.__headerState || prevNode.__width !== this.__width || prevNode.__colSpan !== this.__colSpan || prevNode.__rowSpan !== this.__rowSpan || prevNode.__backgroundColor !== this.__backgroundColor || prevNode.__verticalAlign !== this.__verticalAlign;
|
|
204
219
|
}
|
|
205
220
|
isShadowRoot() {
|
|
206
221
|
return true;
|
|
@@ -215,6 +230,9 @@ class TableCellNode extends lexical.ElementNode {
|
|
|
215
230
|
return false;
|
|
216
231
|
}
|
|
217
232
|
}
|
|
233
|
+
function isValidVerticalAlign(verticalAlign) {
|
|
234
|
+
return verticalAlign === 'middle' || verticalAlign === 'bottom';
|
|
235
|
+
}
|
|
218
236
|
function $convertTableCellNodeElement(domNode) {
|
|
219
237
|
const domNode_ = domNode;
|
|
220
238
|
const nodeName = domNode.nodeName.toLowerCase();
|
|
@@ -228,8 +246,12 @@ function $convertTableCellNodeElement(domNode) {
|
|
|
228
246
|
if (backgroundColor !== '') {
|
|
229
247
|
tableCellNode.__backgroundColor = backgroundColor;
|
|
230
248
|
}
|
|
249
|
+
const verticalAlign = domNode_.style.verticalAlign;
|
|
250
|
+
if (isValidVerticalAlign(verticalAlign)) {
|
|
251
|
+
tableCellNode.__verticalAlign = verticalAlign;
|
|
252
|
+
}
|
|
231
253
|
const style = domNode_.style;
|
|
232
|
-
const textDecoration = style.textDecoration.split(' ');
|
|
254
|
+
const textDecoration = (style && style.textDecoration || '').split(' ');
|
|
233
255
|
const hasBoldFontWeight = style.fontWeight === '700' || style.fontWeight === 'bold';
|
|
234
256
|
const hasLinethroughTextDecoration = textDecoration.includes('line-through');
|
|
235
257
|
const hasItalicFontStyle = style.fontStyle === 'italic';
|
|
@@ -269,7 +291,7 @@ function $convertTableCellNodeElement(domNode) {
|
|
|
269
291
|
node: tableCellNode
|
|
270
292
|
};
|
|
271
293
|
}
|
|
272
|
-
function $createTableCellNode(headerState, colSpan = 1, width) {
|
|
294
|
+
function $createTableCellNode(headerState = TableCellHeaderStates.NO_STATUS, colSpan = 1, width) {
|
|
273
295
|
return lexical.$applyNodeReplacement(new TableCellNode(headerState, colSpan, width));
|
|
274
296
|
}
|
|
275
297
|
function $isTableCellNode(node) {
|
|
@@ -286,6 +308,20 @@ function $isTableCellNode(node) {
|
|
|
286
308
|
|
|
287
309
|
const INSERT_TABLE_COMMAND = lexical.createCommand('INSERT_TABLE_COMMAND');
|
|
288
310
|
|
|
311
|
+
/**
|
|
312
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
313
|
+
*
|
|
314
|
+
* This source code is licensed under the MIT license found in the
|
|
315
|
+
* LICENSE file in the root directory of this source tree.
|
|
316
|
+
*
|
|
317
|
+
*/
|
|
318
|
+
|
|
319
|
+
// Do not require this module directly! Use normal `invariant` calls.
|
|
320
|
+
|
|
321
|
+
function formatDevErrorMessage(message) {
|
|
322
|
+
throw new Error(message);
|
|
323
|
+
}
|
|
324
|
+
|
|
289
325
|
/**
|
|
290
326
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
291
327
|
*
|
|
@@ -313,20 +349,22 @@ class TableRowNode extends lexical.ElementNode {
|
|
|
313
349
|
};
|
|
314
350
|
}
|
|
315
351
|
static importJSON(serializedNode) {
|
|
316
|
-
return $createTableRowNode(serializedNode
|
|
352
|
+
return $createTableRowNode().updateFromJSON(serializedNode);
|
|
353
|
+
}
|
|
354
|
+
updateFromJSON(serializedNode) {
|
|
355
|
+
return super.updateFromJSON(serializedNode).setHeight(serializedNode.height);
|
|
317
356
|
}
|
|
318
357
|
constructor(height, key) {
|
|
319
358
|
super(key);
|
|
320
359
|
this.__height = height;
|
|
321
360
|
}
|
|
322
361
|
exportJSON() {
|
|
362
|
+
const height = this.getHeight();
|
|
323
363
|
return {
|
|
324
364
|
...super.exportJSON(),
|
|
325
|
-
...(
|
|
326
|
-
height
|
|
327
|
-
})
|
|
328
|
-
type: 'tablerow',
|
|
329
|
-
version: 1
|
|
365
|
+
...(height === undefined ? undefined : {
|
|
366
|
+
height
|
|
367
|
+
})
|
|
330
368
|
};
|
|
331
369
|
}
|
|
332
370
|
createDOM(config) {
|
|
@@ -337,13 +375,16 @@ class TableRowNode extends lexical.ElementNode {
|
|
|
337
375
|
LexicalUtils.addClassNamesToElement(element, config.theme.tableRow);
|
|
338
376
|
return element;
|
|
339
377
|
}
|
|
378
|
+
extractWithChild(child, selection, destination) {
|
|
379
|
+
return destination === 'html';
|
|
380
|
+
}
|
|
340
381
|
isShadowRoot() {
|
|
341
382
|
return true;
|
|
342
383
|
}
|
|
343
384
|
setHeight(height) {
|
|
344
385
|
const self = this.getWritable();
|
|
345
386
|
self.__height = height;
|
|
346
|
-
return
|
|
387
|
+
return self;
|
|
347
388
|
}
|
|
348
389
|
getHeight() {
|
|
349
390
|
return this.getLatest().__height;
|
|
@@ -365,6 +406,7 @@ function $convertTableRowElement(domNode) {
|
|
|
365
406
|
height = parseFloat(domNode_.style.height);
|
|
366
407
|
}
|
|
367
408
|
return {
|
|
409
|
+
after: children => LexicalUtils.$descendantsMatching(children, $isTableCellNode),
|
|
368
410
|
node: $createTableRowNode(height)
|
|
369
411
|
};
|
|
370
412
|
}
|
|
@@ -393,6 +435,10 @@ const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !==
|
|
|
393
435
|
*
|
|
394
436
|
*/
|
|
395
437
|
|
|
438
|
+
const documentMode = CAN_USE_DOM && 'documentMode' in document ? document.documentMode : null;
|
|
439
|
+
const IS_FIREFOX = CAN_USE_DOM && /^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);
|
|
440
|
+
CAN_USE_DOM && 'InputEvent' in window && !documentMode ? 'getTargetRanges' in new window.InputEvent('input') : false;
|
|
441
|
+
|
|
396
442
|
function $createTableNodeWithDimensions(rowCount, columnCount, includeHeaders = true) {
|
|
397
443
|
const tableNode = $createTableNode();
|
|
398
444
|
for (let iRow = 0; iRow < rowCount; iRow++) {
|
|
@@ -490,7 +536,7 @@ function $insertTableRow(tableNode, targetIndex, shouldInsertAfter = true, rowCo
|
|
|
490
536
|
for (let c = 0; c < tableColumnCount; c++) {
|
|
491
537
|
const tableCellFromTargetRow = tableRowCells[c];
|
|
492
538
|
if (!$isTableCellNode(tableCellFromTargetRow)) {
|
|
493
|
-
|
|
539
|
+
formatDevErrorMessage(`Expected table cell`);
|
|
494
540
|
}
|
|
495
541
|
const {
|
|
496
542
|
above,
|
|
@@ -522,29 +568,41 @@ const getHeaderState = (currentState, possibleState) => {
|
|
|
522
568
|
}
|
|
523
569
|
return TableCellHeaderStates.NO_STATUS;
|
|
524
570
|
};
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Inserts a table row before or after the current focus cell node,
|
|
574
|
+
* taking into account any spans. If successful, returns the
|
|
575
|
+
* inserted table row node.
|
|
576
|
+
*/
|
|
525
577
|
function $insertTableRow__EXPERIMENTAL(insertAfter = true) {
|
|
526
578
|
const selection = lexical.$getSelection();
|
|
527
579
|
if (!(lexical.$isRangeSelection(selection) || $isTableSelection(selection))) {
|
|
528
|
-
|
|
580
|
+
formatDevErrorMessage(`Expected a RangeSelection or TableSelection`);
|
|
529
581
|
}
|
|
582
|
+
const anchor = selection.anchor.getNode();
|
|
530
583
|
const focus = selection.focus.getNode();
|
|
584
|
+
const [anchorCell] = $getNodeTriplet(anchor);
|
|
531
585
|
const [focusCell,, grid] = $getNodeTriplet(focus);
|
|
532
|
-
const [gridMap, focusCellMap] = $computeTableMap(grid, focusCell,
|
|
586
|
+
const [gridMap, focusCellMap, anchorCellMap] = $computeTableMap(grid, focusCell, anchorCell);
|
|
533
587
|
const columnCount = gridMap[0].length;
|
|
588
|
+
const {
|
|
589
|
+
startRow: anchorStartRow
|
|
590
|
+
} = anchorCellMap;
|
|
534
591
|
const {
|
|
535
592
|
startRow: focusStartRow
|
|
536
593
|
} = focusCellMap;
|
|
594
|
+
let insertedRow = null;
|
|
537
595
|
if (insertAfter) {
|
|
538
|
-
const
|
|
539
|
-
const
|
|
596
|
+
const insertAfterEndRow = Math.max(focusStartRow + focusCell.__rowSpan, anchorStartRow + anchorCell.__rowSpan) - 1;
|
|
597
|
+
const insertAfterEndRowMap = gridMap[insertAfterEndRow];
|
|
540
598
|
const newRow = $createTableRowNode();
|
|
541
599
|
for (let i = 0; i < columnCount; i++) {
|
|
542
600
|
const {
|
|
543
601
|
cell,
|
|
544
602
|
startRow
|
|
545
|
-
} =
|
|
546
|
-
if (startRow + cell.__rowSpan - 1 <=
|
|
547
|
-
const currentCell =
|
|
603
|
+
} = insertAfterEndRowMap[i];
|
|
604
|
+
if (startRow + cell.__rowSpan - 1 <= insertAfterEndRow) {
|
|
605
|
+
const currentCell = insertAfterEndRowMap[i].cell;
|
|
548
606
|
const currentCellHeaderState = currentCell.__headerState;
|
|
549
607
|
const headerState = getHeaderState(currentCellHeaderState, TableCellHeaderStates.COLUMN);
|
|
550
608
|
newRow.append($createTableCellNode(headerState).append(lexical.$createParagraphNode()));
|
|
@@ -552,21 +610,23 @@ function $insertTableRow__EXPERIMENTAL(insertAfter = true) {
|
|
|
552
610
|
cell.setRowSpan(cell.__rowSpan + 1);
|
|
553
611
|
}
|
|
554
612
|
}
|
|
555
|
-
const
|
|
556
|
-
if (!$isTableRowNode(
|
|
557
|
-
|
|
613
|
+
const insertAfterEndRowNode = grid.getChildAtIndex(insertAfterEndRow);
|
|
614
|
+
if (!$isTableRowNode(insertAfterEndRowNode)) {
|
|
615
|
+
formatDevErrorMessage(`insertAfterEndRow is not a TableRowNode`);
|
|
558
616
|
}
|
|
559
|
-
|
|
617
|
+
insertAfterEndRowNode.insertAfter(newRow);
|
|
618
|
+
insertedRow = newRow;
|
|
560
619
|
} else {
|
|
561
|
-
const
|
|
620
|
+
const insertBeforeStartRow = Math.min(focusStartRow, anchorStartRow);
|
|
621
|
+
const insertBeforeStartRowMap = gridMap[insertBeforeStartRow];
|
|
562
622
|
const newRow = $createTableRowNode();
|
|
563
623
|
for (let i = 0; i < columnCount; i++) {
|
|
564
624
|
const {
|
|
565
625
|
cell,
|
|
566
626
|
startRow
|
|
567
|
-
} =
|
|
568
|
-
if (startRow ===
|
|
569
|
-
const currentCell =
|
|
627
|
+
} = insertBeforeStartRowMap[i];
|
|
628
|
+
if (startRow === insertBeforeStartRow) {
|
|
629
|
+
const currentCell = insertBeforeStartRowMap[i].cell;
|
|
570
630
|
const currentCellHeaderState = currentCell.__headerState;
|
|
571
631
|
const headerState = getHeaderState(currentCellHeaderState, TableCellHeaderStates.COLUMN);
|
|
572
632
|
newRow.append($createTableCellNode(headerState).append(lexical.$createParagraphNode()));
|
|
@@ -574,12 +634,14 @@ function $insertTableRow__EXPERIMENTAL(insertAfter = true) {
|
|
|
574
634
|
cell.setRowSpan(cell.__rowSpan + 1);
|
|
575
635
|
}
|
|
576
636
|
}
|
|
577
|
-
const
|
|
578
|
-
if (!$isTableRowNode(
|
|
579
|
-
|
|
637
|
+
const insertBeforeStartRowNode = grid.getChildAtIndex(insertBeforeStartRow);
|
|
638
|
+
if (!$isTableRowNode(insertBeforeStartRowNode)) {
|
|
639
|
+
formatDevErrorMessage(`insertBeforeStartRow is not a TableRowNode`);
|
|
580
640
|
}
|
|
581
|
-
|
|
641
|
+
insertBeforeStartRowNode.insertBefore(newRow);
|
|
642
|
+
insertedRow = newRow;
|
|
582
643
|
}
|
|
644
|
+
return insertedRow;
|
|
583
645
|
}
|
|
584
646
|
function $insertTableColumn(tableNode, targetIndex, shouldInsertAfter = true, columnCount, table) {
|
|
585
647
|
const tableRows = tableNode.getChildren();
|
|
@@ -594,7 +656,7 @@ function $insertTableColumn(tableNode, targetIndex, shouldInsertAfter = true, co
|
|
|
594
656
|
}
|
|
595
657
|
const targetCell = tableRowChildren[targetIndex];
|
|
596
658
|
if (!$isTableCellNode(targetCell)) {
|
|
597
|
-
|
|
659
|
+
formatDevErrorMessage(`Expected table cell`);
|
|
598
660
|
}
|
|
599
661
|
const {
|
|
600
662
|
left,
|
|
@@ -625,10 +687,16 @@ function $insertTableColumn(tableNode, targetIndex, shouldInsertAfter = true, co
|
|
|
625
687
|
});
|
|
626
688
|
return tableNode;
|
|
627
689
|
}
|
|
690
|
+
|
|
691
|
+
/**
|
|
692
|
+
* Inserts a column before or after the current focus cell node,
|
|
693
|
+
* taking into account any spans. If successful, returns the
|
|
694
|
+
* first inserted cell node.
|
|
695
|
+
*/
|
|
628
696
|
function $insertTableColumn__EXPERIMENTAL(insertAfter = true) {
|
|
629
697
|
const selection = lexical.$getSelection();
|
|
630
698
|
if (!(lexical.$isRangeSelection(selection) || $isTableSelection(selection))) {
|
|
631
|
-
|
|
699
|
+
formatDevErrorMessage(`Expected a RangeSelection or TableSelection`);
|
|
632
700
|
}
|
|
633
701
|
const anchor = selection.anchor.getNode();
|
|
634
702
|
const focus = selection.focus.getNode();
|
|
@@ -640,7 +708,7 @@ function $insertTableColumn__EXPERIMENTAL(insertAfter = true) {
|
|
|
640
708
|
const insertAfterColumn = insertAfter ? startColumn + focusCell.__colSpan - 1 : startColumn - 1;
|
|
641
709
|
const gridFirstChild = grid.getFirstChild();
|
|
642
710
|
if (!$isTableRowNode(gridFirstChild)) {
|
|
643
|
-
|
|
711
|
+
formatDevErrorMessage(`Expected firstTable child to be a row`);
|
|
644
712
|
}
|
|
645
713
|
let firstInsertedCell = null;
|
|
646
714
|
function $createTableCellNodeForInsertTableColumn(headerState = TableCellHeaderStates.NO_STATUS) {
|
|
@@ -655,7 +723,7 @@ function $insertTableColumn__EXPERIMENTAL(insertAfter = true) {
|
|
|
655
723
|
if (i !== 0) {
|
|
656
724
|
const currentRow = loopRow.getNextSibling();
|
|
657
725
|
if (!$isTableRowNode(currentRow)) {
|
|
658
|
-
|
|
726
|
+
formatDevErrorMessage(`Expected row nextSibling to be a row`);
|
|
659
727
|
}
|
|
660
728
|
loopRow = currentRow;
|
|
661
729
|
}
|
|
@@ -697,6 +765,15 @@ function $insertTableColumn__EXPERIMENTAL(insertAfter = true) {
|
|
|
697
765
|
if (firstInsertedCell !== null) {
|
|
698
766
|
$moveSelectionToCell(firstInsertedCell);
|
|
699
767
|
}
|
|
768
|
+
const colWidths = grid.getColWidths();
|
|
769
|
+
if (colWidths) {
|
|
770
|
+
const newColWidths = [...colWidths];
|
|
771
|
+
const columnIndex = insertAfterColumn < 0 ? 0 : insertAfterColumn;
|
|
772
|
+
const newWidth = newColWidths[columnIndex];
|
|
773
|
+
newColWidths.splice(columnIndex, 0, newWidth);
|
|
774
|
+
grid.setColWidths(newColWidths);
|
|
775
|
+
}
|
|
776
|
+
return firstInsertedCell;
|
|
700
777
|
}
|
|
701
778
|
function $deleteTableColumn(tableNode, targetIndex) {
|
|
702
779
|
const tableRows = tableNode.getChildren();
|
|
@@ -715,10 +792,9 @@ function $deleteTableColumn(tableNode, targetIndex) {
|
|
|
715
792
|
function $deleteTableRow__EXPERIMENTAL() {
|
|
716
793
|
const selection = lexical.$getSelection();
|
|
717
794
|
if (!(lexical.$isRangeSelection(selection) || $isTableSelection(selection))) {
|
|
718
|
-
|
|
795
|
+
formatDevErrorMessage(`Expected a RangeSelection or TableSelection`);
|
|
719
796
|
}
|
|
720
|
-
const anchor = selection.anchor.getNode();
|
|
721
|
-
const focus = selection.focus.getNode();
|
|
797
|
+
const [anchor, focus] = selection.isBackward() ? [selection.focus.getNode(), selection.anchor.getNode()] : [selection.anchor.getNode(), selection.focus.getNode()];
|
|
722
798
|
const [anchorCell,, grid] = $getNodeTriplet(anchor);
|
|
723
799
|
const [focusCell] = $getNodeTriplet(focus);
|
|
724
800
|
const [gridMap, anchorCellMap, focusCellMap] = $computeTableMap(grid, anchorCell, focusCell);
|
|
@@ -735,6 +811,7 @@ function $deleteTableRow__EXPERIMENTAL() {
|
|
|
735
811
|
return;
|
|
736
812
|
}
|
|
737
813
|
const columnCount = gridMap[0].length;
|
|
814
|
+
const selectedRowCount = anchorCell.__rowSpan;
|
|
738
815
|
const nextRow = gridMap[focusEndRow + 1];
|
|
739
816
|
const nextRowNode = grid.getChildAtIndex(focusEndRow + 1);
|
|
740
817
|
for (let row = focusEndRow; row >= anchorStartRow; row--) {
|
|
@@ -750,27 +827,37 @@ function $deleteTableRow__EXPERIMENTAL() {
|
|
|
750
827
|
}
|
|
751
828
|
// Rows overflowing top have to be trimmed
|
|
752
829
|
if (row === anchorStartRow && cellStartRow < anchorStartRow) {
|
|
753
|
-
|
|
830
|
+
const overflowTop = anchorStartRow - cellStartRow;
|
|
831
|
+
cell.setRowSpan(cell.__rowSpan - Math.min(selectedRowCount, cell.__rowSpan - overflowTop));
|
|
754
832
|
}
|
|
755
833
|
// Rows overflowing bottom have to be trimmed and moved to the next row
|
|
756
834
|
if (cellStartRow >= anchorStartRow && cellStartRow + cell.__rowSpan - 1 > focusEndRow) {
|
|
757
835
|
cell.setRowSpan(cell.__rowSpan - (focusEndRow - cellStartRow + 1));
|
|
758
836
|
if (!(nextRowNode !== null)) {
|
|
759
|
-
|
|
837
|
+
formatDevErrorMessage(`Expected nextRowNode not to be null`);
|
|
838
|
+
}
|
|
839
|
+
let insertAfterCell = null;
|
|
840
|
+
for (let columnIndex = 0; columnIndex < column; columnIndex++) {
|
|
841
|
+
const currentCellMap = nextRow[columnIndex];
|
|
842
|
+
const currentCell = currentCellMap.cell;
|
|
843
|
+
// Checking the cell having startRow as same as nextRow
|
|
844
|
+
if (currentCellMap.startRow === row + 1) {
|
|
845
|
+
insertAfterCell = currentCell;
|
|
846
|
+
}
|
|
847
|
+
if (currentCell.__colSpan > 1) {
|
|
848
|
+
columnIndex += currentCell.__colSpan - 1;
|
|
849
|
+
}
|
|
760
850
|
}
|
|
761
|
-
if (
|
|
851
|
+
if (insertAfterCell === null) {
|
|
762
852
|
$insertFirst(nextRowNode, cell);
|
|
763
853
|
} else {
|
|
764
|
-
|
|
765
|
-
cell: previousCell
|
|
766
|
-
} = nextRow[column - 1];
|
|
767
|
-
previousCell.insertAfter(cell);
|
|
854
|
+
insertAfterCell.insertAfter(cell);
|
|
768
855
|
}
|
|
769
856
|
}
|
|
770
857
|
}
|
|
771
858
|
const rowNode = grid.getChildAtIndex(row);
|
|
772
859
|
if (!$isTableRowNode(rowNode)) {
|
|
773
|
-
|
|
860
|
+
formatDevErrorMessage(`Expected TableNode childAtIndex(${String(row)}) to be RowNode`);
|
|
774
861
|
}
|
|
775
862
|
rowNode.remove();
|
|
776
863
|
}
|
|
@@ -790,7 +877,7 @@ function $deleteTableRow__EXPERIMENTAL() {
|
|
|
790
877
|
function $deleteTableColumn__EXPERIMENTAL() {
|
|
791
878
|
const selection = lexical.$getSelection();
|
|
792
879
|
if (!(lexical.$isRangeSelection(selection) || $isTableSelection(selection))) {
|
|
793
|
-
|
|
880
|
+
formatDevErrorMessage(`Expected a RangeSelection or TableSelection`);
|
|
794
881
|
}
|
|
795
882
|
const anchor = selection.anchor.getNode();
|
|
796
883
|
const focus = selection.focus.getNode();
|
|
@@ -854,6 +941,12 @@ function $deleteTableColumn__EXPERIMENTAL() {
|
|
|
854
941
|
} = previousRow;
|
|
855
942
|
$moveSelectionToCell(cell);
|
|
856
943
|
}
|
|
944
|
+
const colWidths = grid.getColWidths();
|
|
945
|
+
if (colWidths) {
|
|
946
|
+
const newColWidths = [...colWidths];
|
|
947
|
+
newColWidths.splice(startColumn, selectedColumnCount);
|
|
948
|
+
grid.setColWidths(newColWidths);
|
|
949
|
+
}
|
|
857
950
|
}
|
|
858
951
|
function $moveSelectionToCell(cell) {
|
|
859
952
|
const firstDescendant = cell.getFirstDescendant();
|
|
@@ -874,31 +967,57 @@ function $insertFirst(parent, node) {
|
|
|
874
967
|
function $unmergeCell() {
|
|
875
968
|
const selection = lexical.$getSelection();
|
|
876
969
|
if (!(lexical.$isRangeSelection(selection) || $isTableSelection(selection))) {
|
|
877
|
-
|
|
970
|
+
formatDevErrorMessage(`Expected a RangeSelection or TableSelection`);
|
|
878
971
|
}
|
|
879
972
|
const anchor = selection.anchor.getNode();
|
|
880
973
|
const [cell, row, grid] = $getNodeTriplet(anchor);
|
|
881
974
|
const colSpan = cell.__colSpan;
|
|
882
975
|
const rowSpan = cell.__rowSpan;
|
|
976
|
+
if (colSpan === 1 && rowSpan === 1) {
|
|
977
|
+
return;
|
|
978
|
+
}
|
|
979
|
+
const [map, cellMap] = $computeTableMap(grid, cell, cell);
|
|
980
|
+
const {
|
|
981
|
+
startColumn,
|
|
982
|
+
startRow
|
|
983
|
+
} = cellMap;
|
|
984
|
+
// Create a heuristic for what the style of the unmerged cells should be
|
|
985
|
+
// based on whether every row or column already had that state before the
|
|
986
|
+
// unmerge.
|
|
987
|
+
const baseColStyle = cell.__headerState & TableCellHeaderStates.COLUMN;
|
|
988
|
+
const colStyles = Array.from({
|
|
989
|
+
length: colSpan
|
|
990
|
+
}, (_v, i) => {
|
|
991
|
+
let colStyle = baseColStyle;
|
|
992
|
+
for (let rowIdx = 0; colStyle !== 0 && rowIdx < map.length; rowIdx++) {
|
|
993
|
+
colStyle &= map[rowIdx][i + startColumn].cell.__headerState;
|
|
994
|
+
}
|
|
995
|
+
return colStyle;
|
|
996
|
+
});
|
|
997
|
+
const baseRowStyle = cell.__headerState & TableCellHeaderStates.ROW;
|
|
998
|
+
const rowStyles = Array.from({
|
|
999
|
+
length: rowSpan
|
|
1000
|
+
}, (_v, i) => {
|
|
1001
|
+
let rowStyle = baseRowStyle;
|
|
1002
|
+
for (let colIdx = 0; rowStyle !== 0 && colIdx < map[0].length; colIdx++) {
|
|
1003
|
+
rowStyle &= map[i + startRow][colIdx].cell.__headerState;
|
|
1004
|
+
}
|
|
1005
|
+
return rowStyle;
|
|
1006
|
+
});
|
|
883
1007
|
if (colSpan > 1) {
|
|
884
1008
|
for (let i = 1; i < colSpan; i++) {
|
|
885
|
-
cell.insertAfter($createTableCellNode(
|
|
1009
|
+
cell.insertAfter($createTableCellNode(colStyles[i] | rowStyles[0]).append(lexical.$createParagraphNode()));
|
|
886
1010
|
}
|
|
887
1011
|
cell.setColSpan(1);
|
|
888
1012
|
}
|
|
889
1013
|
if (rowSpan > 1) {
|
|
890
|
-
const [map, cellMap] = $computeTableMap(grid, cell, cell);
|
|
891
|
-
const {
|
|
892
|
-
startColumn,
|
|
893
|
-
startRow
|
|
894
|
-
} = cellMap;
|
|
895
1014
|
let currentRowNode;
|
|
896
1015
|
for (let i = 1; i < rowSpan; i++) {
|
|
897
1016
|
const currentRow = startRow + i;
|
|
898
1017
|
const currentRowMap = map[currentRow];
|
|
899
1018
|
currentRowNode = (currentRowNode || row).getNextSibling();
|
|
900
1019
|
if (!$isTableRowNode(currentRowNode)) {
|
|
901
|
-
|
|
1020
|
+
formatDevErrorMessage(`Expected row next sibling to be a row`);
|
|
902
1021
|
}
|
|
903
1022
|
let insertAfterCell = null;
|
|
904
1023
|
for (let column = 0; column < startColumn; column++) {
|
|
@@ -912,75 +1031,84 @@ function $unmergeCell() {
|
|
|
912
1031
|
}
|
|
913
1032
|
}
|
|
914
1033
|
if (insertAfterCell === null) {
|
|
915
|
-
for (let j =
|
|
916
|
-
$insertFirst(currentRowNode, $createTableCellNode(
|
|
1034
|
+
for (let j = colSpan - 1; j >= 0; j--) {
|
|
1035
|
+
$insertFirst(currentRowNode, $createTableCellNode(colStyles[j] | rowStyles[i]).append(lexical.$createParagraphNode()));
|
|
917
1036
|
}
|
|
918
1037
|
} else {
|
|
919
|
-
for (let j =
|
|
920
|
-
insertAfterCell.insertAfter($createTableCellNode(
|
|
1038
|
+
for (let j = colSpan - 1; j >= 0; j--) {
|
|
1039
|
+
insertAfterCell.insertAfter($createTableCellNode(colStyles[j] | rowStyles[i]).append(lexical.$createParagraphNode()));
|
|
921
1040
|
}
|
|
922
1041
|
}
|
|
923
1042
|
}
|
|
924
1043
|
cell.setRowSpan(1);
|
|
925
1044
|
}
|
|
926
1045
|
}
|
|
927
|
-
function $computeTableMap(
|
|
928
|
-
const [tableMap, cellAValue, cellBValue] = $computeTableMapSkipCellCheck(
|
|
1046
|
+
function $computeTableMap(tableNode, cellA, cellB) {
|
|
1047
|
+
const [tableMap, cellAValue, cellBValue] = $computeTableMapSkipCellCheck(tableNode, cellA, cellB);
|
|
929
1048
|
if (!(cellAValue !== null)) {
|
|
930
|
-
|
|
1049
|
+
formatDevErrorMessage(`Anchor not found in Table`);
|
|
931
1050
|
}
|
|
932
1051
|
if (!(cellBValue !== null)) {
|
|
933
|
-
|
|
1052
|
+
formatDevErrorMessage(`Focus not found in Table`);
|
|
934
1053
|
}
|
|
935
1054
|
return [tableMap, cellAValue, cellBValue];
|
|
936
1055
|
}
|
|
937
|
-
function $computeTableMapSkipCellCheck(
|
|
1056
|
+
function $computeTableMapSkipCellCheck(tableNode, cellA, cellB) {
|
|
938
1057
|
const tableMap = [];
|
|
939
1058
|
let cellAValue = null;
|
|
940
1059
|
let cellBValue = null;
|
|
941
|
-
function
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
startRow
|
|
946
|
-
};
|
|
947
|
-
const rowSpan = cell.__rowSpan;
|
|
948
|
-
const colSpan = cell.__colSpan;
|
|
949
|
-
for (let i = 0; i < rowSpan; i++) {
|
|
950
|
-
if (tableMap[startRow + i] === undefined) {
|
|
951
|
-
tableMap[startRow + i] = [];
|
|
952
|
-
}
|
|
953
|
-
for (let j = 0; j < colSpan; j++) {
|
|
954
|
-
tableMap[startRow + i][startColumn + j] = value;
|
|
955
|
-
}
|
|
956
|
-
}
|
|
957
|
-
if (cellA !== null && cellA.is(cell)) {
|
|
958
|
-
cellAValue = value;
|
|
1060
|
+
function getMapRow(i) {
|
|
1061
|
+
let row = tableMap[i];
|
|
1062
|
+
if (row === undefined) {
|
|
1063
|
+
tableMap[i] = row = [];
|
|
959
1064
|
}
|
|
960
|
-
|
|
961
|
-
cellBValue = value;
|
|
962
|
-
}
|
|
963
|
-
}
|
|
964
|
-
function isEmpty(row, column) {
|
|
965
|
-
return tableMap[row] === undefined || tableMap[row][column] === undefined;
|
|
1065
|
+
return row;
|
|
966
1066
|
}
|
|
967
|
-
const gridChildren =
|
|
968
|
-
for (let
|
|
969
|
-
const row = gridChildren[
|
|
1067
|
+
const gridChildren = tableNode.getChildren();
|
|
1068
|
+
for (let rowIdx = 0; rowIdx < gridChildren.length; rowIdx++) {
|
|
1069
|
+
const row = gridChildren[rowIdx];
|
|
970
1070
|
if (!$isTableRowNode(row)) {
|
|
971
|
-
|
|
1071
|
+
formatDevErrorMessage(`Expected TableNode children to be TableRowNode`);
|
|
972
1072
|
}
|
|
973
|
-
const
|
|
974
|
-
let
|
|
975
|
-
for (const cell of rowChildren) {
|
|
1073
|
+
const startMapRow = getMapRow(rowIdx);
|
|
1074
|
+
for (let cell = row.getFirstChild(), colIdx = 0; cell != null; cell = cell.getNextSibling()) {
|
|
976
1075
|
if (!$isTableCellNode(cell)) {
|
|
977
|
-
|
|
1076
|
+
formatDevErrorMessage(`Expected TableRowNode children to be TableCellNode`);
|
|
1077
|
+
} // Skip past any columns that were merged from a higher row
|
|
1078
|
+
while (startMapRow[colIdx] !== undefined) {
|
|
1079
|
+
colIdx++;
|
|
1080
|
+
}
|
|
1081
|
+
const value = {
|
|
1082
|
+
cell,
|
|
1083
|
+
startColumn: colIdx,
|
|
1084
|
+
startRow: rowIdx
|
|
1085
|
+
};
|
|
1086
|
+
const {
|
|
1087
|
+
__rowSpan: rowSpan,
|
|
1088
|
+
__colSpan: colSpan
|
|
1089
|
+
} = cell;
|
|
1090
|
+
for (let j = 0; j < rowSpan; j++) {
|
|
1091
|
+
if (rowIdx + j >= gridChildren.length) {
|
|
1092
|
+
// The table is non-rectangular with a rowSpan
|
|
1093
|
+
// below the last <tr> in the table.
|
|
1094
|
+
// We should probably handle this with a node transform
|
|
1095
|
+
// to ensure that tables are always rectangular but this
|
|
1096
|
+
// will avoid crashes such as #6584
|
|
1097
|
+
// Note that there are probably still latent bugs
|
|
1098
|
+
// regarding colSpan or general cell count mismatches.
|
|
1099
|
+
break;
|
|
1100
|
+
}
|
|
1101
|
+
const mapRow = getMapRow(rowIdx + j);
|
|
1102
|
+
for (let i = 0; i < colSpan; i++) {
|
|
1103
|
+
mapRow[colIdx + i] = value;
|
|
1104
|
+
}
|
|
978
1105
|
}
|
|
979
|
-
|
|
980
|
-
|
|
1106
|
+
if (cellA !== null && cellAValue === null && cellA.is(cell)) {
|
|
1107
|
+
cellAValue = value;
|
|
1108
|
+
}
|
|
1109
|
+
if (cellB !== null && cellBValue === null && cellB.is(cell)) {
|
|
1110
|
+
cellBValue = value;
|
|
981
1111
|
}
|
|
982
|
-
write(i, j, cell);
|
|
983
|
-
j += cell.__colSpan;
|
|
984
1112
|
}
|
|
985
1113
|
}
|
|
986
1114
|
return [tableMap, cellAValue, cellBValue];
|
|
@@ -992,26 +1120,107 @@ function $getNodeTriplet(source) {
|
|
|
992
1120
|
} else if ('__type' in source) {
|
|
993
1121
|
const cell_ = LexicalUtils.$findMatchingParent(source, $isTableCellNode);
|
|
994
1122
|
if (!$isTableCellNode(cell_)) {
|
|
995
|
-
|
|
1123
|
+
formatDevErrorMessage(`Expected to find a parent TableCellNode`);
|
|
996
1124
|
}
|
|
997
1125
|
cell = cell_;
|
|
998
1126
|
} else {
|
|
999
1127
|
const cell_ = LexicalUtils.$findMatchingParent(source.getNode(), $isTableCellNode);
|
|
1000
1128
|
if (!$isTableCellNode(cell_)) {
|
|
1001
|
-
|
|
1129
|
+
formatDevErrorMessage(`Expected to find a parent TableCellNode`);
|
|
1002
1130
|
}
|
|
1003
1131
|
cell = cell_;
|
|
1004
1132
|
}
|
|
1005
1133
|
const row = cell.getParent();
|
|
1006
1134
|
if (!$isTableRowNode(row)) {
|
|
1007
|
-
|
|
1135
|
+
formatDevErrorMessage(`Expected TableCellNode to have a parent TableRowNode`);
|
|
1008
1136
|
}
|
|
1009
1137
|
const grid = row.getParent();
|
|
1010
1138
|
if (!$isTableNode(grid)) {
|
|
1011
|
-
|
|
1139
|
+
formatDevErrorMessage(`Expected TableRowNode to have a parent TableNode`);
|
|
1012
1140
|
}
|
|
1013
1141
|
return [cell, row, grid];
|
|
1014
1142
|
}
|
|
1143
|
+
function $computeTableCellRectSpans(map, boundary) {
|
|
1144
|
+
const {
|
|
1145
|
+
minColumn,
|
|
1146
|
+
maxColumn,
|
|
1147
|
+
minRow,
|
|
1148
|
+
maxRow
|
|
1149
|
+
} = boundary;
|
|
1150
|
+
let topSpan = 1;
|
|
1151
|
+
let leftSpan = 1;
|
|
1152
|
+
let rightSpan = 1;
|
|
1153
|
+
let bottomSpan = 1;
|
|
1154
|
+
const topRow = map[minRow];
|
|
1155
|
+
const bottomRow = map[maxRow];
|
|
1156
|
+
for (let col = minColumn; col <= maxColumn; col++) {
|
|
1157
|
+
topSpan = Math.max(topSpan, topRow[col].cell.__rowSpan);
|
|
1158
|
+
bottomSpan = Math.max(bottomSpan, bottomRow[col].cell.__rowSpan);
|
|
1159
|
+
}
|
|
1160
|
+
for (let row = minRow; row <= maxRow; row++) {
|
|
1161
|
+
leftSpan = Math.max(leftSpan, map[row][minColumn].cell.__colSpan);
|
|
1162
|
+
rightSpan = Math.max(rightSpan, map[row][maxColumn].cell.__colSpan);
|
|
1163
|
+
}
|
|
1164
|
+
return {
|
|
1165
|
+
bottomSpan,
|
|
1166
|
+
leftSpan,
|
|
1167
|
+
rightSpan,
|
|
1168
|
+
topSpan
|
|
1169
|
+
};
|
|
1170
|
+
}
|
|
1171
|
+
function $computeTableCellRectBoundary(map, cellAMap, cellBMap) {
|
|
1172
|
+
// Initial boundaries based on the anchor and focus cells
|
|
1173
|
+
let minColumn = Math.min(cellAMap.startColumn, cellBMap.startColumn);
|
|
1174
|
+
let minRow = Math.min(cellAMap.startRow, cellBMap.startRow);
|
|
1175
|
+
let maxColumn = Math.max(cellAMap.startColumn + cellAMap.cell.__colSpan - 1, cellBMap.startColumn + cellBMap.cell.__colSpan - 1);
|
|
1176
|
+
let maxRow = Math.max(cellAMap.startRow + cellAMap.cell.__rowSpan - 1, cellBMap.startRow + cellBMap.cell.__rowSpan - 1);
|
|
1177
|
+
|
|
1178
|
+
// Keep expanding until we have a complete rectangle
|
|
1179
|
+
let hasChanges;
|
|
1180
|
+
do {
|
|
1181
|
+
hasChanges = false;
|
|
1182
|
+
|
|
1183
|
+
// Check all cells in the table
|
|
1184
|
+
for (let row = 0; row < map.length; row++) {
|
|
1185
|
+
for (let col = 0; col < map[0].length; col++) {
|
|
1186
|
+
const cell = map[row][col];
|
|
1187
|
+
if (!cell) {
|
|
1188
|
+
continue;
|
|
1189
|
+
}
|
|
1190
|
+
const cellEndCol = cell.startColumn + cell.cell.__colSpan - 1;
|
|
1191
|
+
const cellEndRow = cell.startRow + cell.cell.__rowSpan - 1;
|
|
1192
|
+
|
|
1193
|
+
// Check if this cell intersects with our current selection rectangle
|
|
1194
|
+
const intersectsHorizontally = cell.startColumn <= maxColumn && cellEndCol >= minColumn;
|
|
1195
|
+
const intersectsVertically = cell.startRow <= maxRow && cellEndRow >= minRow;
|
|
1196
|
+
|
|
1197
|
+
// If the cell intersects either horizontally or vertically
|
|
1198
|
+
if (intersectsHorizontally && intersectsVertically) {
|
|
1199
|
+
// Expand boundaries to include this cell completely
|
|
1200
|
+
const newMinColumn = Math.min(minColumn, cell.startColumn);
|
|
1201
|
+
const newMaxColumn = Math.max(maxColumn, cellEndCol);
|
|
1202
|
+
const newMinRow = Math.min(minRow, cell.startRow);
|
|
1203
|
+
const newMaxRow = Math.max(maxRow, cellEndRow);
|
|
1204
|
+
|
|
1205
|
+
// Check if boundaries changed
|
|
1206
|
+
if (newMinColumn !== minColumn || newMaxColumn !== maxColumn || newMinRow !== minRow || newMaxRow !== maxRow) {
|
|
1207
|
+
minColumn = newMinColumn;
|
|
1208
|
+
maxColumn = newMaxColumn;
|
|
1209
|
+
minRow = newMinRow;
|
|
1210
|
+
maxRow = newMaxRow;
|
|
1211
|
+
hasChanges = true;
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
} while (hasChanges);
|
|
1217
|
+
return {
|
|
1218
|
+
maxColumn,
|
|
1219
|
+
maxRow,
|
|
1220
|
+
minColumn,
|
|
1221
|
+
minRow
|
|
1222
|
+
};
|
|
1223
|
+
}
|
|
1015
1224
|
function $getTableCellNodeRect(tableCellNode) {
|
|
1016
1225
|
const [cellNode,, gridNode] = $getNodeTriplet(tableCellNode);
|
|
1017
1226
|
const rows = gridNode.getChildren();
|
|
@@ -1058,14 +1267,38 @@ function $getTableCellNodeRect(tableCellNode) {
|
|
|
1058
1267
|
return null;
|
|
1059
1268
|
}
|
|
1060
1269
|
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1270
|
+
function $getCellNodes(tableSelection) {
|
|
1271
|
+
const [[anchorNode, anchorCell, anchorRow, anchorTable], [focusNode, focusCell, focusRow, focusTable]] = ['anchor', 'focus'].map(k => {
|
|
1272
|
+
const node = tableSelection[k].getNode();
|
|
1273
|
+
const cellNode = LexicalUtils.$findMatchingParent(node, $isTableCellNode);
|
|
1274
|
+
if (!$isTableCellNode(cellNode)) {
|
|
1275
|
+
formatDevErrorMessage(`Expected TableSelection ${k} to be (or a child of) TableCellNode, got key ${node.getKey()} of type ${node.getType()}`);
|
|
1276
|
+
}
|
|
1277
|
+
const rowNode = cellNode.getParent();
|
|
1278
|
+
if (!$isTableRowNode(rowNode)) {
|
|
1279
|
+
formatDevErrorMessage(`Expected TableSelection ${k} cell parent to be a TableRowNode`);
|
|
1280
|
+
}
|
|
1281
|
+
const tableNode = rowNode.getParent();
|
|
1282
|
+
if (!$isTableNode(tableNode)) {
|
|
1283
|
+
formatDevErrorMessage(`Expected TableSelection ${k} row parent to be a TableNode`);
|
|
1284
|
+
}
|
|
1285
|
+
return [node, cellNode, rowNode, tableNode];
|
|
1286
|
+
});
|
|
1287
|
+
// TODO: nested tables may violate this
|
|
1288
|
+
if (!anchorTable.is(focusTable)) {
|
|
1289
|
+
formatDevErrorMessage(`Expected TableSelection anchor and focus to be in the same table`);
|
|
1290
|
+
}
|
|
1291
|
+
return {
|
|
1292
|
+
anchorCell,
|
|
1293
|
+
anchorNode,
|
|
1294
|
+
anchorRow,
|
|
1295
|
+
anchorTable,
|
|
1296
|
+
focusCell,
|
|
1297
|
+
focusNode,
|
|
1298
|
+
focusRow,
|
|
1299
|
+
focusTable
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1069
1302
|
class TableSelection {
|
|
1070
1303
|
constructor(tableKey, anchor, focus) {
|
|
1071
1304
|
this.anchor = anchor;
|
|
@@ -1080,6 +1313,17 @@ class TableSelection {
|
|
|
1080
1313
|
return [this.anchor, this.focus];
|
|
1081
1314
|
}
|
|
1082
1315
|
|
|
1316
|
+
/**
|
|
1317
|
+
* {@link $createTableSelection} unfortunately makes it very easy to create
|
|
1318
|
+
* nonsense selections, so we have a method to see if the selection probably
|
|
1319
|
+
* makes sense.
|
|
1320
|
+
*
|
|
1321
|
+
* @returns true if the TableSelection is (probably) valid
|
|
1322
|
+
*/
|
|
1323
|
+
isValid() {
|
|
1324
|
+
return this.tableKey !== 'root' && this.anchor.key !== 'root' && this.anchor.type === 'element' && this.focus.key !== 'root' && this.focus.type === 'element';
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1083
1327
|
/**
|
|
1084
1328
|
* Returns whether the Selection is "backwards", meaning the focus
|
|
1085
1329
|
* logically precedes the anchor in the EditorState.
|
|
@@ -1095,20 +1339,18 @@ class TableSelection {
|
|
|
1095
1339
|
this._cachedNodes = nodes;
|
|
1096
1340
|
}
|
|
1097
1341
|
is(selection) {
|
|
1098
|
-
|
|
1099
|
-
return false;
|
|
1100
|
-
}
|
|
1101
|
-
return this.tableKey === selection.tableKey && this.anchor.is(selection.anchor) && this.focus.is(selection.focus);
|
|
1342
|
+
return $isTableSelection(selection) && this.tableKey === selection.tableKey && this.anchor.is(selection.anchor) && this.focus.is(selection.focus);
|
|
1102
1343
|
}
|
|
1103
1344
|
set(tableKey, anchorCellKey, focusCellKey) {
|
|
1104
|
-
|
|
1345
|
+
// note: closure compiler's acorn does not support ||=
|
|
1346
|
+
this.dirty = this.dirty || tableKey !== this.tableKey || anchorCellKey !== this.anchor.key || focusCellKey !== this.focus.key;
|
|
1105
1347
|
this.tableKey = tableKey;
|
|
1106
1348
|
this.anchor.key = anchorCellKey;
|
|
1107
1349
|
this.focus.key = focusCellKey;
|
|
1108
1350
|
this._cachedNodes = null;
|
|
1109
1351
|
}
|
|
1110
1352
|
clone() {
|
|
1111
|
-
return new TableSelection(this.tableKey, this.anchor, this.focus);
|
|
1353
|
+
return new TableSelection(this.tableKey, lexical.$createPoint(this.anchor.key, this.anchor.offset, this.anchor.type), lexical.$createPoint(this.focus.key, this.focus.offset, this.focus.type));
|
|
1112
1354
|
}
|
|
1113
1355
|
isCollapsed() {
|
|
1114
1356
|
return false;
|
|
@@ -1122,10 +1364,30 @@ class TableSelection {
|
|
|
1122
1364
|
insertText() {
|
|
1123
1365
|
// Do nothing?
|
|
1124
1366
|
}
|
|
1367
|
+
|
|
1368
|
+
/**
|
|
1369
|
+
* Returns whether the provided TextFormatType is present on the Selection.
|
|
1370
|
+
* This will be true if any paragraph in table cells has the specified format.
|
|
1371
|
+
*
|
|
1372
|
+
* @param type the TextFormatType to check for.
|
|
1373
|
+
* @returns true if the provided format is currently toggled on on the Selection, false otherwise.
|
|
1374
|
+
*/
|
|
1375
|
+
hasFormat(type) {
|
|
1376
|
+
let format = 0;
|
|
1377
|
+
const cellNodes = this.getNodes().filter($isTableCellNode);
|
|
1378
|
+
cellNodes.forEach(cellNode => {
|
|
1379
|
+
const paragraph = cellNode.getFirstChild();
|
|
1380
|
+
if (lexical.$isParagraphNode(paragraph)) {
|
|
1381
|
+
format |= paragraph.getTextFormat();
|
|
1382
|
+
}
|
|
1383
|
+
});
|
|
1384
|
+
const formatFlag = lexical.TEXT_TYPE_TO_FORMAT[type];
|
|
1385
|
+
return (format & formatFlag) !== 0;
|
|
1386
|
+
}
|
|
1125
1387
|
insertNodes(nodes) {
|
|
1126
1388
|
const focusNode = this.focus.getNode();
|
|
1127
1389
|
if (!lexical.$isElementNode(focusNode)) {
|
|
1128
|
-
|
|
1390
|
+
formatDevErrorMessage(`Expected TableSelection focus to be an ElementNode`);
|
|
1129
1391
|
}
|
|
1130
1392
|
const selection = lexical.$normalizeSelection__EXPERIMENTAL(focusNode.select(0, focusNode.getChildrenSize()));
|
|
1131
1393
|
selection.insertNodes(nodes);
|
|
@@ -1133,26 +1395,22 @@ class TableSelection {
|
|
|
1133
1395
|
|
|
1134
1396
|
// TODO Deprecate this method. It's confusing when used with colspan|rowspan
|
|
1135
1397
|
getShape() {
|
|
1136
|
-
const
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
}
|
|
1140
|
-
const anchorCellNodeRect = $getTableCellNodeRect(
|
|
1398
|
+
const {
|
|
1399
|
+
anchorCell,
|
|
1400
|
+
focusCell
|
|
1401
|
+
} = $getCellNodes(this);
|
|
1402
|
+
const anchorCellNodeRect = $getTableCellNodeRect(anchorCell);
|
|
1141
1403
|
if (!(anchorCellNodeRect !== null)) {
|
|
1142
|
-
|
|
1404
|
+
formatDevErrorMessage(`getCellRect: expected to find AnchorNode`);
|
|
1143
1405
|
}
|
|
1144
|
-
const
|
|
1145
|
-
if (!$isTableCellNode(focusCellNode)) {
|
|
1146
|
-
throw Error(`Expected TableSelection focus to be (or a child of) TableCellNode`);
|
|
1147
|
-
}
|
|
1148
|
-
const focusCellNodeRect = $getTableCellNodeRect(focusCellNode);
|
|
1406
|
+
const focusCellNodeRect = $getTableCellNodeRect(focusCell);
|
|
1149
1407
|
if (!(focusCellNodeRect !== null)) {
|
|
1150
|
-
|
|
1408
|
+
formatDevErrorMessage(`getCellRect: expected to find focusCellNode`);
|
|
1151
1409
|
}
|
|
1152
1410
|
const startX = Math.min(anchorCellNodeRect.columnIndex, focusCellNodeRect.columnIndex);
|
|
1153
|
-
const stopX = Math.max(anchorCellNodeRect.columnIndex, focusCellNodeRect.columnIndex);
|
|
1411
|
+
const stopX = Math.max(anchorCellNodeRect.columnIndex + anchorCellNodeRect.colSpan - 1, focusCellNodeRect.columnIndex + focusCellNodeRect.colSpan - 1);
|
|
1154
1412
|
const startY = Math.min(anchorCellNodeRect.rowIndex, focusCellNodeRect.rowIndex);
|
|
1155
|
-
const stopY = Math.max(anchorCellNodeRect.rowIndex, focusCellNodeRect.rowIndex);
|
|
1413
|
+
const stopY = Math.max(anchorCellNodeRect.rowIndex + anchorCellNodeRect.rowSpan - 1, focusCellNodeRect.rowIndex + focusCellNodeRect.rowSpan - 1);
|
|
1156
1414
|
return {
|
|
1157
1415
|
fromX: Math.min(startX, stopX),
|
|
1158
1416
|
fromY: Math.min(startY, stopY),
|
|
@@ -1161,43 +1419,32 @@ class TableSelection {
|
|
|
1161
1419
|
};
|
|
1162
1420
|
}
|
|
1163
1421
|
getNodes() {
|
|
1422
|
+
if (!this.isValid()) {
|
|
1423
|
+
return [];
|
|
1424
|
+
}
|
|
1164
1425
|
const cachedNodes = this._cachedNodes;
|
|
1165
1426
|
if (cachedNodes !== null) {
|
|
1166
1427
|
return cachedNodes;
|
|
1167
1428
|
}
|
|
1168
|
-
const
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
if (!$isTableCellNode(anchorCell)) {
|
|
1174
|
-
throw Error(`Expected TableSelection anchor to be (or a child of) TableCellNode`);
|
|
1175
|
-
}
|
|
1176
|
-
if (!$isTableCellNode(focusCell)) {
|
|
1177
|
-
throw Error(`Expected TableSelection focus to be (or a child of) TableCellNode`);
|
|
1178
|
-
}
|
|
1179
|
-
const anchorRow = anchorCell.getParent();
|
|
1180
|
-
if (!$isTableRowNode(anchorRow)) {
|
|
1181
|
-
throw Error(`Expected anchorCell to have a parent TableRowNode`);
|
|
1182
|
-
}
|
|
1183
|
-
const tableNode = anchorRow.getParent();
|
|
1184
|
-
if (!$isTableNode(tableNode)) {
|
|
1185
|
-
throw Error(`Expected tableNode to have a parent TableNode`);
|
|
1186
|
-
}
|
|
1429
|
+
const {
|
|
1430
|
+
anchorTable: tableNode,
|
|
1431
|
+
anchorCell,
|
|
1432
|
+
focusCell
|
|
1433
|
+
} = $getCellNodes(this);
|
|
1187
1434
|
const focusCellGrid = focusCell.getParents()[1];
|
|
1188
1435
|
if (focusCellGrid !== tableNode) {
|
|
1189
1436
|
if (!tableNode.isParentOf(focusCell)) {
|
|
1190
1437
|
// focus is on higher Grid level than anchor
|
|
1191
1438
|
const gridParent = tableNode.getParent();
|
|
1192
1439
|
if (!(gridParent != null)) {
|
|
1193
|
-
|
|
1440
|
+
formatDevErrorMessage(`Expected gridParent to have a parent`);
|
|
1194
1441
|
}
|
|
1195
1442
|
this.set(this.tableKey, gridParent.getKey(), focusCell.getKey());
|
|
1196
1443
|
} else {
|
|
1197
1444
|
// anchor is on higher Grid level than focus
|
|
1198
1445
|
const focusCellParent = focusCellGrid.getParent();
|
|
1199
1446
|
if (!(focusCellParent != null)) {
|
|
1200
|
-
|
|
1447
|
+
formatDevErrorMessage(`Expected focusCellParent to have a parent`);
|
|
1201
1448
|
}
|
|
1202
1449
|
this.set(this.tableKey, focusCell.getKey(), focusCellParent.getKey());
|
|
1203
1450
|
}
|
|
@@ -1209,64 +1456,16 @@ class TableSelection {
|
|
|
1209
1456
|
// ability to store a state. Killing TableSelection and moving the logic to the plugin would make
|
|
1210
1457
|
// this possible.
|
|
1211
1458
|
const [map, cellAMap, cellBMap] = $computeTableMap(tableNode, anchorCell, focusCell);
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
cell,
|
|
1223
|
-
startColumn: cellStartColumn,
|
|
1224
|
-
startRow: cellStartRow
|
|
1225
|
-
} = mapValue;
|
|
1226
|
-
minColumn = Math.min(minColumn, cellStartColumn);
|
|
1227
|
-
minRow = Math.min(minRow, cellStartRow);
|
|
1228
|
-
maxColumn = Math.max(maxColumn, cellStartColumn + cell.__colSpan - 1);
|
|
1229
|
-
maxRow = Math.max(maxRow, cellStartRow + cell.__rowSpan - 1);
|
|
1230
|
-
}
|
|
1231
|
-
while (minColumn < exploredMinColumn || minRow < exploredMinRow || maxColumn > exploredMaxColumn || maxRow > exploredMaxRow) {
|
|
1232
|
-
if (minColumn < exploredMinColumn) {
|
|
1233
|
-
// Expand on the left
|
|
1234
|
-
const rowDiff = exploredMaxRow - exploredMinRow;
|
|
1235
|
-
const previousColumn = exploredMinColumn - 1;
|
|
1236
|
-
for (let i = 0; i <= rowDiff; i++) {
|
|
1237
|
-
expandBoundary(map[exploredMinRow + i][previousColumn]);
|
|
1238
|
-
}
|
|
1239
|
-
exploredMinColumn = previousColumn;
|
|
1240
|
-
}
|
|
1241
|
-
if (minRow < exploredMinRow) {
|
|
1242
|
-
// Expand on top
|
|
1243
|
-
const columnDiff = exploredMaxColumn - exploredMinColumn;
|
|
1244
|
-
const previousRow = exploredMinRow - 1;
|
|
1245
|
-
for (let i = 0; i <= columnDiff; i++) {
|
|
1246
|
-
expandBoundary(map[previousRow][exploredMinColumn + i]);
|
|
1247
|
-
}
|
|
1248
|
-
exploredMinRow = previousRow;
|
|
1249
|
-
}
|
|
1250
|
-
if (maxColumn > exploredMaxColumn) {
|
|
1251
|
-
// Expand on the right
|
|
1252
|
-
const rowDiff = exploredMaxRow - exploredMinRow;
|
|
1253
|
-
const nextColumn = exploredMaxColumn + 1;
|
|
1254
|
-
for (let i = 0; i <= rowDiff; i++) {
|
|
1255
|
-
expandBoundary(map[exploredMinRow + i][nextColumn]);
|
|
1256
|
-
}
|
|
1257
|
-
exploredMaxColumn = nextColumn;
|
|
1258
|
-
}
|
|
1259
|
-
if (maxRow > exploredMaxRow) {
|
|
1260
|
-
// Expand on the bottom
|
|
1261
|
-
const columnDiff = exploredMaxColumn - exploredMinColumn;
|
|
1262
|
-
const nextRow = exploredMaxRow + 1;
|
|
1263
|
-
for (let i = 0; i <= columnDiff; i++) {
|
|
1264
|
-
expandBoundary(map[nextRow][exploredMinColumn + i]);
|
|
1265
|
-
}
|
|
1266
|
-
exploredMaxRow = nextRow;
|
|
1267
|
-
}
|
|
1268
|
-
}
|
|
1269
|
-
const nodes = [tableNode];
|
|
1459
|
+
const {
|
|
1460
|
+
minColumn,
|
|
1461
|
+
maxColumn,
|
|
1462
|
+
minRow,
|
|
1463
|
+
maxRow
|
|
1464
|
+
} = $computeTableCellRectBoundary(map, cellAMap, cellBMap);
|
|
1465
|
+
|
|
1466
|
+
// We use a Map here because merged cells in the grid would otherwise
|
|
1467
|
+
// show up multiple times in the nodes array
|
|
1468
|
+
const nodeMap = new Map([[tableNode.getKey(), tableNode]]);
|
|
1270
1469
|
let lastRow = null;
|
|
1271
1470
|
for (let i = minRow; i <= maxRow; i++) {
|
|
1272
1471
|
for (let j = minColumn; j <= maxColumn; j++) {
|
|
@@ -1275,15 +1474,20 @@ class TableSelection {
|
|
|
1275
1474
|
} = map[i][j];
|
|
1276
1475
|
const currentRow = cell.getParent();
|
|
1277
1476
|
if (!$isTableRowNode(currentRow)) {
|
|
1278
|
-
|
|
1477
|
+
formatDevErrorMessage(`Expected TableCellNode parent to be a TableRowNode`);
|
|
1279
1478
|
}
|
|
1280
1479
|
if (currentRow !== lastRow) {
|
|
1281
|
-
|
|
1480
|
+
nodeMap.set(currentRow.getKey(), currentRow);
|
|
1481
|
+
lastRow = currentRow;
|
|
1482
|
+
}
|
|
1483
|
+
if (!nodeMap.has(cell.getKey())) {
|
|
1484
|
+
$visitRecursively(cell, childNode => {
|
|
1485
|
+
nodeMap.set(childNode.getKey(), childNode);
|
|
1486
|
+
});
|
|
1282
1487
|
}
|
|
1283
|
-
nodes.push(cell, ...$getChildrenRecursively(cell));
|
|
1284
|
-
lastRow = currentRow;
|
|
1285
1488
|
}
|
|
1286
1489
|
}
|
|
1490
|
+
const nodes = Array.from(nodeMap.values());
|
|
1287
1491
|
if (!lexical.isCurrentlyReadOnlyMode()) {
|
|
1288
1492
|
this._cachedNodes = nodes;
|
|
1289
1493
|
}
|
|
@@ -1305,36 +1509,67 @@ function $isTableSelection(x) {
|
|
|
1305
1509
|
return x instanceof TableSelection;
|
|
1306
1510
|
}
|
|
1307
1511
|
function $createTableSelection() {
|
|
1512
|
+
// TODO this is a suboptimal design, it doesn't make sense to have
|
|
1513
|
+
// a table selection that isn't associated with a table. This
|
|
1514
|
+
// constructor should have required argumnets and in true we
|
|
1515
|
+
// should check that they point to a table and are element points to
|
|
1516
|
+
// cell nodes of that table.
|
|
1308
1517
|
const anchor = lexical.$createPoint('root', 0, 'element');
|
|
1309
1518
|
const focus = lexical.$createPoint('root', 0, 'element');
|
|
1310
1519
|
return new TableSelection('root', anchor, focus);
|
|
1311
1520
|
}
|
|
1312
|
-
function $
|
|
1313
|
-
const
|
|
1314
|
-
const
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
if (!(
|
|
1318
|
-
|
|
1319
|
-
}
|
|
1320
|
-
if (lexical.$isElementNode(currentNode)) {
|
|
1321
|
-
stack.unshift(...currentNode.getChildren());
|
|
1521
|
+
function $createTableSelectionFrom(tableNode, anchorCell, focusCell) {
|
|
1522
|
+
const tableNodeKey = tableNode.getKey();
|
|
1523
|
+
const anchorCellKey = anchorCell.getKey();
|
|
1524
|
+
const focusCellKey = focusCell.getKey();
|
|
1525
|
+
{
|
|
1526
|
+
if (!tableNode.isAttached()) {
|
|
1527
|
+
formatDevErrorMessage(`$createTableSelectionFrom: tableNode ${tableNodeKey} is not attached`);
|
|
1322
1528
|
}
|
|
1323
|
-
if (
|
|
1324
|
-
|
|
1529
|
+
if (!tableNode.is($findTableNode(anchorCell))) {
|
|
1530
|
+
formatDevErrorMessage(`$createTableSelectionFrom: anchorCell ${anchorCellKey} is not in table ${tableNodeKey}`);
|
|
1325
1531
|
}
|
|
1532
|
+
if (!tableNode.is($findTableNode(focusCell))) {
|
|
1533
|
+
formatDevErrorMessage(`$createTableSelectionFrom: focusCell ${focusCellKey} is not in table ${tableNodeKey}`);
|
|
1534
|
+
} // TODO: Check for rectangular grid
|
|
1326
1535
|
}
|
|
1327
|
-
|
|
1536
|
+
const prevSelection = lexical.$getSelection();
|
|
1537
|
+
const nextSelection = $isTableSelection(prevSelection) ? prevSelection.clone() : $createTableSelection();
|
|
1538
|
+
nextSelection.set(tableNode.getKey(), anchorCell.getKey(), focusCell.getKey());
|
|
1539
|
+
return nextSelection;
|
|
1328
1540
|
}
|
|
1329
1541
|
|
|
1330
1542
|
/**
|
|
1331
|
-
*
|
|
1332
|
-
*
|
|
1333
|
-
*
|
|
1334
|
-
* LICENSE file in the root directory of this source tree.
|
|
1335
|
-
*
|
|
1543
|
+
* Depth first visitor
|
|
1544
|
+
* @param node The starting node
|
|
1545
|
+
* @param $visit The function to call for each node. If the function returns false, then children of this node will not be explored
|
|
1336
1546
|
*/
|
|
1547
|
+
function $visitRecursively(node, $visit) {
|
|
1548
|
+
const stack = [[node]];
|
|
1549
|
+
for (let currentArray = stack.at(-1); currentArray !== undefined && stack.length > 0; currentArray = stack.at(-1)) {
|
|
1550
|
+
const currentNode = currentArray.pop();
|
|
1551
|
+
if (currentNode === undefined) {
|
|
1552
|
+
stack.pop();
|
|
1553
|
+
} else if ($visit(currentNode) !== false && lexical.$isElementNode(currentNode)) {
|
|
1554
|
+
stack.push(currentNode.getChildren());
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
}
|
|
1337
1558
|
|
|
1559
|
+
function $getTableAndElementByKey(tableNodeKey, editor = lexical.$getEditor()) {
|
|
1560
|
+
const tableNode = lexical.$getNodeByKey(tableNodeKey);
|
|
1561
|
+
if (!$isTableNode(tableNode)) {
|
|
1562
|
+
formatDevErrorMessage(`TableObserver: Expected tableNodeKey ${tableNodeKey} to be a TableNode`);
|
|
1563
|
+
}
|
|
1564
|
+
const tableElement = getTableElement(tableNode, editor.getElementByKey(tableNodeKey));
|
|
1565
|
+
if (!(tableElement !== null)) {
|
|
1566
|
+
formatDevErrorMessage(`TableObserver: Expected to find TableElement in DOM for key ${tableNodeKey}`);
|
|
1567
|
+
}
|
|
1568
|
+
return {
|
|
1569
|
+
tableElement,
|
|
1570
|
+
tableNode
|
|
1571
|
+
};
|
|
1572
|
+
}
|
|
1338
1573
|
class TableObserver {
|
|
1339
1574
|
constructor(editor, tableNodeKey) {
|
|
1340
1575
|
this.isHighlightingCells = false;
|
|
@@ -1356,18 +1591,30 @@ class TableObserver {
|
|
|
1356
1591
|
this.anchorCell = null;
|
|
1357
1592
|
this.focusCell = null;
|
|
1358
1593
|
this.hasHijackedSelectionStyles = false;
|
|
1359
|
-
this.trackTable();
|
|
1360
1594
|
this.isSelecting = false;
|
|
1595
|
+
this.pointerType = null;
|
|
1596
|
+
this.shouldCheckSelection = false;
|
|
1597
|
+
this.abortController = new AbortController();
|
|
1598
|
+
this.listenerOptions = {
|
|
1599
|
+
signal: this.abortController.signal
|
|
1600
|
+
};
|
|
1601
|
+
this.nextFocus = null;
|
|
1602
|
+
this.trackTable();
|
|
1361
1603
|
}
|
|
1362
1604
|
getTable() {
|
|
1363
1605
|
return this.table;
|
|
1364
1606
|
}
|
|
1365
1607
|
removeListeners() {
|
|
1608
|
+
this.abortController.abort('removeListeners');
|
|
1366
1609
|
Array.from(this.listenersToRemove).forEach(removeListener => removeListener());
|
|
1610
|
+
this.listenersToRemove.clear();
|
|
1611
|
+
}
|
|
1612
|
+
$lookup() {
|
|
1613
|
+
return $getTableAndElementByKey(this.tableNodeKey, this.editor);
|
|
1367
1614
|
}
|
|
1368
1615
|
trackTable() {
|
|
1369
1616
|
const observer = new MutationObserver(records => {
|
|
1370
|
-
this.editor.
|
|
1617
|
+
this.editor.getEditorState().read(() => {
|
|
1371
1618
|
let gridNeedsRedraw = false;
|
|
1372
1619
|
for (let i = 0; i < records.length; i++) {
|
|
1373
1620
|
const record = records[i];
|
|
@@ -1381,27 +1628,31 @@ class TableObserver {
|
|
|
1381
1628
|
if (!gridNeedsRedraw) {
|
|
1382
1629
|
return;
|
|
1383
1630
|
}
|
|
1384
|
-
const
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
}
|
|
1388
|
-
this.table = getTable(tableElement);
|
|
1631
|
+
const {
|
|
1632
|
+
tableNode,
|
|
1633
|
+
tableElement
|
|
1634
|
+
} = this.$lookup();
|
|
1635
|
+
this.table = getTable(tableNode, tableElement);
|
|
1636
|
+
}, {
|
|
1637
|
+
editor: this.editor
|
|
1389
1638
|
});
|
|
1390
1639
|
});
|
|
1391
|
-
this.editor.
|
|
1392
|
-
const
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
}
|
|
1396
|
-
this.table = getTable(tableElement);
|
|
1640
|
+
this.editor.getEditorState().read(() => {
|
|
1641
|
+
const {
|
|
1642
|
+
tableNode,
|
|
1643
|
+
tableElement
|
|
1644
|
+
} = this.$lookup();
|
|
1645
|
+
this.table = getTable(tableNode, tableElement);
|
|
1397
1646
|
observer.observe(tableElement, {
|
|
1398
1647
|
attributes: true,
|
|
1399
1648
|
childList: true,
|
|
1400
1649
|
subtree: true
|
|
1401
1650
|
});
|
|
1651
|
+
}, {
|
|
1652
|
+
editor: this.editor
|
|
1402
1653
|
});
|
|
1403
1654
|
}
|
|
1404
|
-
clearHighlight() {
|
|
1655
|
+
$clearHighlight() {
|
|
1405
1656
|
const editor = this.editor;
|
|
1406
1657
|
this.isHighlightingCells = false;
|
|
1407
1658
|
this.anchorX = -1;
|
|
@@ -1414,275 +1665,395 @@ class TableObserver {
|
|
|
1414
1665
|
this.anchorCell = null;
|
|
1415
1666
|
this.focusCell = null;
|
|
1416
1667
|
this.hasHijackedSelectionStyles = false;
|
|
1417
|
-
this
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
throw new Error('Expected to find TableElement in DOM');
|
|
1426
|
-
}
|
|
1427
|
-
const grid = getTable(tableElement);
|
|
1428
|
-
$updateDOMForSelection(editor, grid, null);
|
|
1668
|
+
this.$enableHighlightStyle();
|
|
1669
|
+
const {
|
|
1670
|
+
tableNode,
|
|
1671
|
+
tableElement
|
|
1672
|
+
} = this.$lookup();
|
|
1673
|
+
const grid = getTable(tableNode, tableElement);
|
|
1674
|
+
$updateDOMForSelection(editor, grid, null);
|
|
1675
|
+
if (lexical.$getSelection() !== null) {
|
|
1429
1676
|
lexical.$setSelection(null);
|
|
1430
1677
|
editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND, undefined);
|
|
1431
|
-
}
|
|
1678
|
+
}
|
|
1432
1679
|
}
|
|
1433
|
-
enableHighlightStyle() {
|
|
1680
|
+
$enableHighlightStyle() {
|
|
1434
1681
|
const editor = this.editor;
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
tableElement.classList.remove('disable-selection');
|
|
1442
|
-
this.hasHijackedSelectionStyles = false;
|
|
1443
|
-
});
|
|
1682
|
+
const {
|
|
1683
|
+
tableElement
|
|
1684
|
+
} = this.$lookup();
|
|
1685
|
+
LexicalUtils.removeClassNamesFromElement(tableElement, editor._config.theme.tableSelection);
|
|
1686
|
+
tableElement.classList.remove('disable-selection');
|
|
1687
|
+
this.hasHijackedSelectionStyles = false;
|
|
1444
1688
|
}
|
|
1445
|
-
disableHighlightStyle() {
|
|
1446
|
-
const
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
}
|
|
1452
|
-
LexicalUtils.addClassNamesToElement(tableElement, editor._config.theme.tableSelection);
|
|
1453
|
-
this.hasHijackedSelectionStyles = true;
|
|
1454
|
-
});
|
|
1689
|
+
$disableHighlightStyle() {
|
|
1690
|
+
const {
|
|
1691
|
+
tableElement
|
|
1692
|
+
} = this.$lookup();
|
|
1693
|
+
LexicalUtils.addClassNamesToElement(tableElement, this.editor._config.theme.tableSelection);
|
|
1694
|
+
this.hasHijackedSelectionStyles = true;
|
|
1455
1695
|
}
|
|
1456
|
-
updateTableTableSelection(selection) {
|
|
1457
|
-
if (selection !== null
|
|
1696
|
+
$updateTableTableSelection(selection) {
|
|
1697
|
+
if (selection !== null) {
|
|
1698
|
+
if (!(selection.tableKey === this.tableNodeKey)) {
|
|
1699
|
+
formatDevErrorMessage(`TableObserver.$updateTableTableSelection: selection.tableKey !== this.tableNodeKey ('${selection.tableKey}' !== '${this.tableNodeKey}')`);
|
|
1700
|
+
}
|
|
1458
1701
|
const editor = this.editor;
|
|
1459
1702
|
this.tableSelection = selection;
|
|
1460
1703
|
this.isHighlightingCells = true;
|
|
1461
|
-
this
|
|
1704
|
+
this.$disableHighlightStyle();
|
|
1705
|
+
this.updateDOMSelection();
|
|
1462
1706
|
$updateDOMForSelection(editor, this.table, this.tableSelection);
|
|
1463
|
-
} else if (selection == null) {
|
|
1464
|
-
this.clearHighlight();
|
|
1465
1707
|
} else {
|
|
1466
|
-
this
|
|
1467
|
-
this.updateTableTableSelection(selection);
|
|
1708
|
+
this.$clearHighlight();
|
|
1468
1709
|
}
|
|
1469
1710
|
}
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1711
|
+
|
|
1712
|
+
/**
|
|
1713
|
+
* @internal
|
|
1714
|
+
* Firefox has a strange behavior where pressing the down arrow key from
|
|
1715
|
+
* above the table will move the caret after the table and then lexical
|
|
1716
|
+
* will select the last cell instead of the first.
|
|
1717
|
+
* We do still want to let the browser handle caret movement but we will
|
|
1718
|
+
* use this property to "tag" the update so that we can recheck the
|
|
1719
|
+
* selection after the event is processed.
|
|
1720
|
+
*/
|
|
1721
|
+
setShouldCheckSelection() {
|
|
1722
|
+
this.shouldCheckSelection = true;
|
|
1723
|
+
}
|
|
1724
|
+
/**
|
|
1725
|
+
* @internal
|
|
1726
|
+
*/
|
|
1727
|
+
getAndClearShouldCheckSelection() {
|
|
1728
|
+
if (this.shouldCheckSelection) {
|
|
1729
|
+
this.shouldCheckSelection = false;
|
|
1730
|
+
return true;
|
|
1731
|
+
}
|
|
1732
|
+
return false;
|
|
1733
|
+
}
|
|
1734
|
+
|
|
1735
|
+
/**
|
|
1736
|
+
* @internal
|
|
1737
|
+
* When handling mousemove events we track what the focus cell should be, but
|
|
1738
|
+
* the DOM selection may end up somewhere else entirely. We don't have an elegant
|
|
1739
|
+
* way to handle this after the DOM selection has been resolved in a
|
|
1740
|
+
* SELECTION_CHANGE_COMMAND callback.
|
|
1741
|
+
*/
|
|
1742
|
+
setNextFocus(nextFocus) {
|
|
1743
|
+
this.nextFocus = nextFocus;
|
|
1744
|
+
}
|
|
1745
|
+
|
|
1746
|
+
/** @internal */
|
|
1747
|
+
getAndClearNextFocus() {
|
|
1748
|
+
const {
|
|
1749
|
+
nextFocus
|
|
1750
|
+
} = this;
|
|
1751
|
+
if (nextFocus !== null) {
|
|
1752
|
+
this.nextFocus = null;
|
|
1753
|
+
}
|
|
1754
|
+
return nextFocus;
|
|
1755
|
+
}
|
|
1756
|
+
|
|
1757
|
+
/** @internal */
|
|
1758
|
+
updateDOMSelection() {
|
|
1759
|
+
if (this.anchorCell !== null && this.focusCell !== null) {
|
|
1760
|
+
const domSelection = lexical.getDOMSelection(this.editor._window);
|
|
1761
|
+
// We are not using a native selection for tables, and if we
|
|
1762
|
+
// set one then the reconciler will undo it.
|
|
1763
|
+
// TODO - it would make sense to have one so that native
|
|
1764
|
+
// copy/paste worked. Right now we have to emulate with
|
|
1765
|
+
// keyboard events but it won't fire if trigged from the menu
|
|
1766
|
+
if (domSelection && domSelection.rangeCount > 0) {
|
|
1767
|
+
domSelection.removeAllRanges();
|
|
1496
1768
|
}
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1769
|
+
}
|
|
1770
|
+
}
|
|
1771
|
+
$setFocusCellForSelection(cell, ignoreStart = false) {
|
|
1772
|
+
const editor = this.editor;
|
|
1773
|
+
const {
|
|
1774
|
+
tableNode
|
|
1775
|
+
} = this.$lookup();
|
|
1776
|
+
const cellX = cell.x;
|
|
1777
|
+
const cellY = cell.y;
|
|
1778
|
+
this.focusCell = cell;
|
|
1779
|
+
if (!this.isHighlightingCells && (this.anchorX !== cellX || this.anchorY !== cellY || ignoreStart)) {
|
|
1780
|
+
this.isHighlightingCells = true;
|
|
1781
|
+
this.$disableHighlightStyle();
|
|
1782
|
+
} else if (cellX === this.focusX && cellY === this.focusY) {
|
|
1783
|
+
return false;
|
|
1784
|
+
}
|
|
1785
|
+
this.focusX = cellX;
|
|
1786
|
+
this.focusY = cellY;
|
|
1787
|
+
if (this.isHighlightingCells) {
|
|
1788
|
+
const focusTableCellNode = $getNearestTableCellInTableFromDOMNode(tableNode, cell.elem);
|
|
1789
|
+
if (this.tableSelection != null && this.anchorCellNodeKey != null && focusTableCellNode !== null) {
|
|
1790
|
+
this.focusCellNodeKey = focusTableCellNode.getKey();
|
|
1791
|
+
this.tableSelection = $createTableSelectionFrom(tableNode, this.$getAnchorTableCellOrThrow(), focusTableCellNode);
|
|
1792
|
+
lexical.$setSelection(this.tableSelection);
|
|
1793
|
+
editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND, undefined);
|
|
1794
|
+
$updateDOMForSelection(editor, this.table, this.tableSelection);
|
|
1795
|
+
return true;
|
|
1510
1796
|
}
|
|
1511
|
-
}
|
|
1797
|
+
}
|
|
1798
|
+
return false;
|
|
1799
|
+
}
|
|
1800
|
+
$getAnchorTableCell() {
|
|
1801
|
+
return this.anchorCellNodeKey ? lexical.$getNodeByKey(this.anchorCellNodeKey) : null;
|
|
1802
|
+
}
|
|
1803
|
+
$getAnchorTableCellOrThrow() {
|
|
1804
|
+
const anchorTableCell = this.$getAnchorTableCell();
|
|
1805
|
+
if (!(anchorTableCell !== null)) {
|
|
1806
|
+
formatDevErrorMessage(`TableObserver anchorTableCell is null`);
|
|
1807
|
+
}
|
|
1808
|
+
return anchorTableCell;
|
|
1809
|
+
}
|
|
1810
|
+
$getFocusTableCell() {
|
|
1811
|
+
return this.focusCellNodeKey ? lexical.$getNodeByKey(this.focusCellNodeKey) : null;
|
|
1812
|
+
}
|
|
1813
|
+
$getFocusTableCellOrThrow() {
|
|
1814
|
+
const focusTableCell = this.$getFocusTableCell();
|
|
1815
|
+
if (!(focusTableCell !== null)) {
|
|
1816
|
+
formatDevErrorMessage(`TableObserver focusTableCell is null`);
|
|
1817
|
+
}
|
|
1818
|
+
return focusTableCell;
|
|
1512
1819
|
}
|
|
1513
|
-
setAnchorCellForSelection(cell) {
|
|
1820
|
+
$setAnchorCellForSelection(cell) {
|
|
1514
1821
|
this.isHighlightingCells = false;
|
|
1515
1822
|
this.anchorCell = cell;
|
|
1516
1823
|
this.anchorX = cell.x;
|
|
1517
1824
|
this.anchorY = cell.y;
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1825
|
+
const {
|
|
1826
|
+
tableNode
|
|
1827
|
+
} = this.$lookup();
|
|
1828
|
+
const anchorTableCellNode = $getNearestTableCellInTableFromDOMNode(tableNode, cell.elem);
|
|
1829
|
+
if (anchorTableCellNode !== null) {
|
|
1830
|
+
const anchorNodeKey = anchorTableCellNode.getKey();
|
|
1831
|
+
this.tableSelection = this.tableSelection != null ? this.tableSelection.clone() : $createTableSelection();
|
|
1832
|
+
this.anchorCellNodeKey = anchorNodeKey;
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
$formatCells(type) {
|
|
1836
|
+
const selection = lexical.$getSelection();
|
|
1837
|
+
if (!$isTableSelection(selection)) {
|
|
1838
|
+
formatDevErrorMessage(`Expected Table selection`);
|
|
1839
|
+
}
|
|
1840
|
+
const formatSelection = lexical.$createRangeSelection();
|
|
1841
|
+
const anchor = formatSelection.anchor;
|
|
1842
|
+
const focus = formatSelection.focus;
|
|
1843
|
+
const cellNodes = selection.getNodes().filter($isTableCellNode);
|
|
1844
|
+
if (!(cellNodes.length > 0)) {
|
|
1845
|
+
formatDevErrorMessage(`No table cells present`);
|
|
1846
|
+
}
|
|
1847
|
+
const paragraph = cellNodes[0].getFirstChild();
|
|
1848
|
+
const alignFormatWith = lexical.$isParagraphNode(paragraph) ? paragraph.getFormatFlags(type, null) : null;
|
|
1849
|
+
cellNodes.forEach(cellNode => {
|
|
1850
|
+
anchor.set(cellNode.getKey(), 0, 'element');
|
|
1851
|
+
focus.set(cellNode.getKey(), cellNode.getChildrenSize(), 'element');
|
|
1852
|
+
formatSelection.formatText(type, alignFormatWith);
|
|
1525
1853
|
});
|
|
1854
|
+
lexical.$setSelection(selection);
|
|
1855
|
+
this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND, undefined);
|
|
1526
1856
|
}
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1857
|
+
$clearText() {
|
|
1858
|
+
const {
|
|
1859
|
+
editor
|
|
1860
|
+
} = this;
|
|
1861
|
+
const tableNode = lexical.$getNodeByKey(this.tableNodeKey);
|
|
1862
|
+
if (!$isTableNode(tableNode)) {
|
|
1863
|
+
throw new Error('Expected TableNode.');
|
|
1864
|
+
}
|
|
1865
|
+
const selection = lexical.$getSelection();
|
|
1866
|
+
if (!$isTableSelection(selection)) {
|
|
1867
|
+
formatDevErrorMessage(`Expected TableSelection`);
|
|
1868
|
+
}
|
|
1869
|
+
const selectedNodes = selection.getNodes().filter($isTableCellNode);
|
|
1870
|
+
if (selectedNodes.length === this.table.columns * this.table.rows) {
|
|
1871
|
+
tableNode.selectPrevious();
|
|
1872
|
+
const parent = tableNode.getParent();
|
|
1873
|
+
// Delete entire table
|
|
1874
|
+
tableNode.remove();
|
|
1875
|
+
// Handle case when table was the only node
|
|
1876
|
+
if (lexical.$isRootNode(parent) && parent.isEmpty()) {
|
|
1877
|
+
editor.dispatchCommand(lexical.INSERT_PARAGRAPH_COMMAND, undefined);
|
|
1878
|
+
}
|
|
1879
|
+
return;
|
|
1880
|
+
}
|
|
1881
|
+
selectedNodes.forEach(cellNode => {
|
|
1882
|
+
if (lexical.$isElementNode(cellNode)) {
|
|
1883
|
+
const paragraphNode = lexical.$createParagraphNode();
|
|
1884
|
+
const textNode = lexical.$createTextNode();
|
|
1885
|
+
paragraphNode.append(textNode);
|
|
1886
|
+
cellNode.append(paragraphNode);
|
|
1887
|
+
cellNode.getChildren().forEach(child => {
|
|
1888
|
+
if (child !== paragraphNode) {
|
|
1889
|
+
child.remove();
|
|
1890
|
+
}
|
|
1891
|
+
});
|
|
1534
1892
|
}
|
|
1535
|
-
const formatSelection = lexical.$createRangeSelection();
|
|
1536
|
-
const anchor = formatSelection.anchor;
|
|
1537
|
-
const focus = formatSelection.focus;
|
|
1538
|
-
selection.getNodes().forEach(cellNode => {
|
|
1539
|
-
if ($isTableCellNode(cellNode) && cellNode.getTextContentSize() !== 0) {
|
|
1540
|
-
anchor.set(cellNode.getKey(), 0, 'element');
|
|
1541
|
-
focus.set(cellNode.getKey(), cellNode.getChildrenSize(), 'element');
|
|
1542
|
-
formatSelection.formatText(type);
|
|
1543
|
-
}
|
|
1544
|
-
});
|
|
1545
|
-
lexical.$setSelection(selection);
|
|
1546
|
-
this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND, undefined);
|
|
1547
|
-
});
|
|
1548
|
-
}
|
|
1549
|
-
clearText() {
|
|
1550
|
-
const editor = this.editor;
|
|
1551
|
-
editor.update(() => {
|
|
1552
|
-
const tableNode = lexical.$getNodeByKey(this.tableNodeKey);
|
|
1553
|
-
if (!$isTableNode(tableNode)) {
|
|
1554
|
-
throw new Error('Expected TableNode.');
|
|
1555
|
-
}
|
|
1556
|
-
const selection = lexical.$getSelection();
|
|
1557
|
-
if (!$isTableSelection(selection)) {
|
|
1558
|
-
{
|
|
1559
|
-
throw Error(`Expected grid selection`);
|
|
1560
|
-
}
|
|
1561
|
-
}
|
|
1562
|
-
const selectedNodes = selection.getNodes().filter($isTableCellNode);
|
|
1563
|
-
if (selectedNodes.length === this.table.columns * this.table.rows) {
|
|
1564
|
-
tableNode.selectPrevious();
|
|
1565
|
-
// Delete entire table
|
|
1566
|
-
tableNode.remove();
|
|
1567
|
-
const rootNode = lexical.$getRoot();
|
|
1568
|
-
rootNode.selectStart();
|
|
1569
|
-
return;
|
|
1570
|
-
}
|
|
1571
|
-
selectedNodes.forEach(cellNode => {
|
|
1572
|
-
if (lexical.$isElementNode(cellNode)) {
|
|
1573
|
-
const paragraphNode = lexical.$createParagraphNode();
|
|
1574
|
-
const textNode = lexical.$createTextNode();
|
|
1575
|
-
paragraphNode.append(textNode);
|
|
1576
|
-
cellNode.append(paragraphNode);
|
|
1577
|
-
cellNode.getChildren().forEach(child => {
|
|
1578
|
-
if (child !== paragraphNode) {
|
|
1579
|
-
child.remove();
|
|
1580
|
-
}
|
|
1581
|
-
});
|
|
1582
|
-
}
|
|
1583
|
-
});
|
|
1584
|
-
$updateDOMForSelection(editor, this.table, null);
|
|
1585
|
-
lexical.$setSelection(null);
|
|
1586
|
-
editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND, undefined);
|
|
1587
1893
|
});
|
|
1894
|
+
$updateDOMForSelection(editor, this.table, null);
|
|
1895
|
+
lexical.$setSelection(null);
|
|
1896
|
+
editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND, undefined);
|
|
1588
1897
|
}
|
|
1589
1898
|
}
|
|
1590
1899
|
|
|
1591
|
-
/**
|
|
1592
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1593
|
-
*
|
|
1594
|
-
* This source code is licensed under the MIT license found in the
|
|
1595
|
-
* LICENSE file in the root directory of this source tree.
|
|
1596
|
-
*
|
|
1597
|
-
*/
|
|
1598
|
-
|
|
1599
1900
|
const LEXICAL_ELEMENT_KEY = '__lexicalTableSelection';
|
|
1600
|
-
const
|
|
1601
|
-
const isMouseDownOnEvent = event => {
|
|
1901
|
+
const isPointerDownOnEvent = event => {
|
|
1602
1902
|
return (event.buttons & 1) === 1;
|
|
1603
1903
|
};
|
|
1604
|
-
function
|
|
1904
|
+
function getTableElement(tableNode, dom) {
|
|
1905
|
+
if (!dom) {
|
|
1906
|
+
return dom;
|
|
1907
|
+
}
|
|
1908
|
+
const element = dom.nodeName === 'TABLE' ? dom : tableNode.getDOMSlot(dom).element;
|
|
1909
|
+
if (!(element.nodeName === 'TABLE')) {
|
|
1910
|
+
formatDevErrorMessage(`getTableElement: Expecting table in as DOM node for TableNode, not ${dom.nodeName}`);
|
|
1911
|
+
}
|
|
1912
|
+
return element;
|
|
1913
|
+
}
|
|
1914
|
+
function getEditorWindow(editor) {
|
|
1915
|
+
return editor._window;
|
|
1916
|
+
}
|
|
1917
|
+
function $findParentTableCellNodeInTable(tableNode, node) {
|
|
1918
|
+
for (let currentNode = node, lastTableCellNode = null; currentNode !== null; currentNode = currentNode.getParent()) {
|
|
1919
|
+
if (tableNode.is(currentNode)) {
|
|
1920
|
+
return lastTableCellNode;
|
|
1921
|
+
} else if ($isTableCellNode(currentNode)) {
|
|
1922
|
+
lastTableCellNode = currentNode;
|
|
1923
|
+
}
|
|
1924
|
+
}
|
|
1925
|
+
return null;
|
|
1926
|
+
}
|
|
1927
|
+
const ARROW_KEY_COMMANDS_WITH_DIRECTION = [[lexical.KEY_ARROW_DOWN_COMMAND, 'down'], [lexical.KEY_ARROW_UP_COMMAND, 'up'], [lexical.KEY_ARROW_LEFT_COMMAND, 'backward'], [lexical.KEY_ARROW_RIGHT_COMMAND, 'forward']];
|
|
1928
|
+
const DELETE_TEXT_COMMANDS = [lexical.DELETE_WORD_COMMAND, lexical.DELETE_LINE_COMMAND, lexical.DELETE_CHARACTER_COMMAND];
|
|
1929
|
+
const DELETE_KEY_COMMANDS = [lexical.KEY_BACKSPACE_COMMAND, lexical.KEY_DELETE_COMMAND];
|
|
1930
|
+
function applyTableHandlers(tableNode, element, editor, hasTabHandler) {
|
|
1605
1931
|
const rootElement = editor.getRootElement();
|
|
1606
|
-
|
|
1607
|
-
|
|
1932
|
+
const editorWindow = getEditorWindow(editor);
|
|
1933
|
+
if (!(rootElement !== null && editorWindow !== null)) {
|
|
1934
|
+
formatDevErrorMessage(`applyTableHandlers: editor has no root element set`);
|
|
1608
1935
|
}
|
|
1609
1936
|
const tableObserver = new TableObserver(editor, tableNode.getKey());
|
|
1610
|
-
const
|
|
1937
|
+
const tableElement = getTableElement(tableNode, element);
|
|
1611
1938
|
attachTableObserverToTableElement(tableElement, tableObserver);
|
|
1612
|
-
|
|
1613
|
-
|
|
1939
|
+
tableObserver.listenersToRemove.add(() => detatchTableObserverFromTableElement(tableElement, tableObserver));
|
|
1940
|
+
const createPointerHandlers = () => {
|
|
1941
|
+
if (tableObserver.isSelecting) {
|
|
1942
|
+
return;
|
|
1943
|
+
}
|
|
1944
|
+
const onPointerUp = () => {
|
|
1614
1945
|
tableObserver.isSelecting = false;
|
|
1615
|
-
editorWindow.removeEventListener('
|
|
1616
|
-
editorWindow.removeEventListener('
|
|
1617
|
-
};
|
|
1618
|
-
const onMouseMove = moveEvent => {
|
|
1619
|
-
// delaying mousemove handler to allow selectionchange handler from LexicalEvents.ts to be executed first
|
|
1620
|
-
setTimeout(() => {
|
|
1621
|
-
if (!isMouseDownOnEvent(moveEvent) && tableObserver.isSelecting) {
|
|
1622
|
-
tableObserver.isSelecting = false;
|
|
1623
|
-
editorWindow.removeEventListener('mouseup', onMouseUp);
|
|
1624
|
-
editorWindow.removeEventListener('mousemove', onMouseMove);
|
|
1625
|
-
return;
|
|
1626
|
-
}
|
|
1627
|
-
const focusCell = getDOMCellFromTarget(moveEvent.target);
|
|
1628
|
-
if (focusCell !== null && (tableObserver.anchorX !== focusCell.x || tableObserver.anchorY !== focusCell.y)) {
|
|
1629
|
-
moveEvent.preventDefault();
|
|
1630
|
-
tableObserver.setFocusCellForSelection(focusCell);
|
|
1631
|
-
}
|
|
1632
|
-
}, 0);
|
|
1633
|
-
};
|
|
1634
|
-
return {
|
|
1635
|
-
onMouseMove: onMouseMove,
|
|
1636
|
-
onMouseUp: onMouseUp
|
|
1946
|
+
editorWindow.removeEventListener('pointerup', onPointerUp);
|
|
1947
|
+
editorWindow.removeEventListener('pointermove', onPointerMove);
|
|
1637
1948
|
};
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1949
|
+
const onPointerMove = moveEvent => {
|
|
1950
|
+
if (!isPointerDownOnEvent(moveEvent) && tableObserver.isSelecting) {
|
|
1951
|
+
tableObserver.isSelecting = false;
|
|
1952
|
+
editorWindow.removeEventListener('pointerup', onPointerUp);
|
|
1953
|
+
editorWindow.removeEventListener('pointermove', onPointerMove);
|
|
1642
1954
|
return;
|
|
1643
1955
|
}
|
|
1644
|
-
if (!
|
|
1956
|
+
if (!lexical.isDOMNode(moveEvent.target)) {
|
|
1645
1957
|
return;
|
|
1646
1958
|
}
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1959
|
+
let focusCell = null;
|
|
1960
|
+
// In firefox the moveEvent.target may be captured so we must always
|
|
1961
|
+
// consult the coordinates #7245
|
|
1962
|
+
const override = !(IS_FIREFOX || tableElement.contains(moveEvent.target));
|
|
1963
|
+
if (override) {
|
|
1964
|
+
focusCell = getDOMCellInTableFromTarget(tableElement, moveEvent.target);
|
|
1965
|
+
} else {
|
|
1966
|
+
for (const el of document.elementsFromPoint(moveEvent.clientX, moveEvent.clientY)) {
|
|
1967
|
+
focusCell = getDOMCellInTableFromTarget(tableElement, el);
|
|
1968
|
+
if (focusCell) {
|
|
1969
|
+
break;
|
|
1970
|
+
}
|
|
1971
|
+
}
|
|
1651
1972
|
}
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
}
|
|
1973
|
+
if (focusCell && (tableObserver.focusCell === null || focusCell.elem !== tableObserver.focusCell.elem)) {
|
|
1974
|
+
tableObserver.setNextFocus({
|
|
1975
|
+
focusCell,
|
|
1976
|
+
override
|
|
1977
|
+
});
|
|
1978
|
+
editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND, undefined);
|
|
1979
|
+
}
|
|
1980
|
+
};
|
|
1981
|
+
tableObserver.isSelecting = true;
|
|
1982
|
+
editorWindow.addEventListener('pointerup', onPointerUp, tableObserver.listenerOptions);
|
|
1983
|
+
editorWindow.addEventListener('pointermove', onPointerMove, tableObserver.listenerOptions);
|
|
1984
|
+
};
|
|
1985
|
+
const onPointerDown = event => {
|
|
1986
|
+
tableObserver.pointerType = event.pointerType;
|
|
1987
|
+
if (event.button !== 0 || !lexical.isDOMNode(event.target) || !editorWindow) {
|
|
1988
|
+
return;
|
|
1989
|
+
}
|
|
1990
|
+
const targetCell = getDOMCellFromTarget(event.target);
|
|
1991
|
+
if (targetCell !== null) {
|
|
1992
|
+
editor.update(() => {
|
|
1993
|
+
const prevSelection = lexical.$getPreviousSelection();
|
|
1994
|
+
// We can't trust Firefox to do the right thing with the selection and
|
|
1995
|
+
// we don't have a proper state machine to do this "correctly" but
|
|
1996
|
+
// if we go ahead and make the table selection now it will work
|
|
1997
|
+
if (IS_FIREFOX && event.shiftKey && $isSelectionInTable(prevSelection, tableNode) && (lexical.$isRangeSelection(prevSelection) || $isTableSelection(prevSelection))) {
|
|
1998
|
+
const prevAnchorNode = prevSelection.anchor.getNode();
|
|
1999
|
+
const prevAnchorCell = $findParentTableCellNodeInTable(tableNode, prevSelection.anchor.getNode());
|
|
2000
|
+
if (prevAnchorCell) {
|
|
2001
|
+
tableObserver.$setAnchorCellForSelection($getObserverCellFromCellNodeOrThrow(tableObserver, prevAnchorCell));
|
|
2002
|
+
tableObserver.$setFocusCellForSelection(targetCell);
|
|
2003
|
+
stopEvent(event);
|
|
2004
|
+
} else {
|
|
2005
|
+
const newSelection = tableNode.isBefore(prevAnchorNode) ? tableNode.selectStart() : tableNode.selectEnd();
|
|
2006
|
+
newSelection.anchor.set(prevSelection.anchor.key, prevSelection.anchor.offset, prevSelection.anchor.type);
|
|
2007
|
+
}
|
|
2008
|
+
} else {
|
|
2009
|
+
tableObserver.$setAnchorCellForSelection(targetCell);
|
|
2010
|
+
}
|
|
2011
|
+
});
|
|
2012
|
+
}
|
|
2013
|
+
createPointerHandlers();
|
|
2014
|
+
};
|
|
2015
|
+
tableElement.addEventListener('pointerdown', onPointerDown, tableObserver.listenerOptions);
|
|
2016
|
+
tableObserver.listenersToRemove.add(() => {
|
|
2017
|
+
tableElement.removeEventListener('pointerdown', onPointerDown);
|
|
2018
|
+
});
|
|
2019
|
+
const onTripleClick = event => {
|
|
2020
|
+
if (event.detail >= 3 && lexical.isDOMNode(event.target)) {
|
|
2021
|
+
const targetCell = getDOMCellFromTarget(event.target);
|
|
2022
|
+
if (targetCell !== null) {
|
|
2023
|
+
event.preventDefault();
|
|
2024
|
+
}
|
|
2025
|
+
}
|
|
2026
|
+
};
|
|
2027
|
+
tableElement.addEventListener('mousedown', onTripleClick, tableObserver.listenerOptions);
|
|
2028
|
+
tableObserver.listenersToRemove.add(() => {
|
|
2029
|
+
tableElement.removeEventListener('mousedown', onTripleClick);
|
|
1660
2030
|
});
|
|
1661
2031
|
|
|
1662
2032
|
// Clear selection when clicking outside of dom.
|
|
1663
|
-
const
|
|
1664
|
-
|
|
2033
|
+
const pointerDownCallback = event => {
|
|
2034
|
+
const target = event.target;
|
|
2035
|
+
if (event.button !== 0 || !lexical.isDOMNode(target)) {
|
|
1665
2036
|
return;
|
|
1666
2037
|
}
|
|
1667
2038
|
editor.update(() => {
|
|
1668
2039
|
const selection = lexical.$getSelection();
|
|
1669
|
-
const target = event.target;
|
|
1670
2040
|
if ($isTableSelection(selection) && selection.tableKey === tableObserver.tableNodeKey && rootElement.contains(target)) {
|
|
1671
|
-
tableObserver
|
|
2041
|
+
tableObserver.$clearHighlight();
|
|
1672
2042
|
}
|
|
1673
2043
|
});
|
|
1674
2044
|
};
|
|
1675
|
-
editorWindow.addEventListener('
|
|
1676
|
-
tableObserver.listenersToRemove.add(() =>
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
2045
|
+
editorWindow.addEventListener('pointerdown', pointerDownCallback, tableObserver.listenerOptions);
|
|
2046
|
+
tableObserver.listenersToRemove.add(() => {
|
|
2047
|
+
editorWindow.removeEventListener('pointerdown', pointerDownCallback);
|
|
2048
|
+
});
|
|
2049
|
+
for (const [command, direction] of ARROW_KEY_COMMANDS_WITH_DIRECTION) {
|
|
2050
|
+
tableObserver.listenersToRemove.add(editor.registerCommand(command, event => $handleArrowKey(editor, event, direction, tableNode, tableObserver), lexical.COMMAND_PRIORITY_HIGH));
|
|
2051
|
+
}
|
|
1681
2052
|
tableObserver.listenersToRemove.add(editor.registerCommand(lexical.KEY_ESCAPE_COMMAND, event => {
|
|
1682
2053
|
const selection = lexical.$getSelection();
|
|
1683
2054
|
if ($isTableSelection(selection)) {
|
|
1684
|
-
const focusCellNode =
|
|
1685
|
-
if (
|
|
2055
|
+
const focusCellNode = $findParentTableCellNodeInTable(tableNode, selection.focus.getNode());
|
|
2056
|
+
if (focusCellNode !== null) {
|
|
1686
2057
|
stopEvent(event);
|
|
1687
2058
|
focusCellNode.selectEnd();
|
|
1688
2059
|
return true;
|
|
@@ -1696,10 +2067,10 @@ function applyTableHandlers(tableNode, tableElement, editor, hasTabHandler) {
|
|
|
1696
2067
|
return false;
|
|
1697
2068
|
}
|
|
1698
2069
|
if ($isTableSelection(selection)) {
|
|
1699
|
-
tableObserver
|
|
2070
|
+
tableObserver.$clearText();
|
|
1700
2071
|
return true;
|
|
1701
2072
|
} else if (lexical.$isRangeSelection(selection)) {
|
|
1702
|
-
const tableCellNode =
|
|
2073
|
+
const tableCellNode = $findParentTableCellNodeInTable(tableNode, selection.anchor.getNode());
|
|
1703
2074
|
if (!$isTableCellNode(tableCellNode)) {
|
|
1704
2075
|
return false;
|
|
1705
2076
|
}
|
|
@@ -1709,7 +2080,7 @@ function applyTableHandlers(tableNode, tableElement, editor, hasTabHandler) {
|
|
|
1709
2080
|
const isFocusInside = tableNode.isParentOf(focusNode);
|
|
1710
2081
|
const selectionContainsPartialTable = isAnchorInside && !isFocusInside || isFocusInside && !isAnchorInside;
|
|
1711
2082
|
if (selectionContainsPartialTable) {
|
|
1712
|
-
tableObserver
|
|
2083
|
+
tableObserver.$clearText();
|
|
1713
2084
|
return true;
|
|
1714
2085
|
}
|
|
1715
2086
|
const nearestElementNode = LexicalUtils.$findMatchingParent(selection.anchor.getNode(), n => lexical.$isElementNode(n));
|
|
@@ -1724,23 +2095,37 @@ function applyTableHandlers(tableNode, tableElement, editor, hasTabHandler) {
|
|
|
1724
2095
|
}
|
|
1725
2096
|
return false;
|
|
1726
2097
|
};
|
|
1727
|
-
|
|
2098
|
+
for (const command of DELETE_TEXT_COMMANDS) {
|
|
1728
2099
|
tableObserver.listenersToRemove.add(editor.registerCommand(command, deleteTextHandler(command), lexical.COMMAND_PRIORITY_CRITICAL));
|
|
1729
|
-
}
|
|
2100
|
+
}
|
|
1730
2101
|
const $deleteCellHandler = event => {
|
|
1731
2102
|
const selection = lexical.$getSelection();
|
|
2103
|
+
if (!($isTableSelection(selection) || lexical.$isRangeSelection(selection))) {
|
|
2104
|
+
return false;
|
|
2105
|
+
}
|
|
2106
|
+
|
|
2107
|
+
// If the selection is inside the table but should remove the whole table
|
|
2108
|
+
// we expand the selection so that both the anchor and focus are outside
|
|
2109
|
+
// the table and the editor's command listener will handle the delete
|
|
2110
|
+
const isAnchorInside = tableNode.isParentOf(selection.anchor.getNode());
|
|
2111
|
+
const isFocusInside = tableNode.isParentOf(selection.focus.getNode());
|
|
2112
|
+
if (isAnchorInside !== isFocusInside) {
|
|
2113
|
+
const tablePoint = isAnchorInside ? 'anchor' : 'focus';
|
|
2114
|
+
const outerPoint = isAnchorInside ? 'focus' : 'anchor';
|
|
2115
|
+
// Preserve the outer point
|
|
2116
|
+
const {
|
|
2117
|
+
key,
|
|
2118
|
+
offset,
|
|
2119
|
+
type
|
|
2120
|
+
} = selection[outerPoint];
|
|
2121
|
+
// Expand the selection around the table
|
|
2122
|
+
const newSelection = tableNode[selection[tablePoint].isBefore(selection[outerPoint]) ? 'selectPrevious' : 'selectNext']();
|
|
2123
|
+
// Restore the outer point of the selection
|
|
2124
|
+
newSelection[outerPoint].set(key, offset, type);
|
|
2125
|
+
// Let the base implementation handle the rest
|
|
2126
|
+
return false;
|
|
2127
|
+
}
|
|
1732
2128
|
if (!$isSelectionInTable(selection, tableNode)) {
|
|
1733
|
-
const nodes = selection ? selection.getNodes() : null;
|
|
1734
|
-
if (nodes) {
|
|
1735
|
-
const table = nodes.find(node => $isTableNode(node) && node.getKey() === tableObserver.tableNodeKey);
|
|
1736
|
-
if ($isTableNode(table)) {
|
|
1737
|
-
const parentNode = table.getParent();
|
|
1738
|
-
if (!parentNode) {
|
|
1739
|
-
return false;
|
|
1740
|
-
}
|
|
1741
|
-
table.remove();
|
|
1742
|
-
}
|
|
1743
|
-
}
|
|
1744
2129
|
return false;
|
|
1745
2130
|
}
|
|
1746
2131
|
if ($isTableSelection(selection)) {
|
|
@@ -1748,18 +2133,14 @@ function applyTableHandlers(tableNode, tableElement, editor, hasTabHandler) {
|
|
|
1748
2133
|
event.preventDefault();
|
|
1749
2134
|
event.stopPropagation();
|
|
1750
2135
|
}
|
|
1751
|
-
tableObserver
|
|
2136
|
+
tableObserver.$clearText();
|
|
1752
2137
|
return true;
|
|
1753
|
-
} else if (lexical.$isRangeSelection(selection)) {
|
|
1754
|
-
const tableCellNode = LexicalUtils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
|
|
1755
|
-
if (!$isTableCellNode(tableCellNode)) {
|
|
1756
|
-
return false;
|
|
1757
|
-
}
|
|
1758
2138
|
}
|
|
1759
2139
|
return false;
|
|
1760
2140
|
};
|
|
1761
|
-
|
|
1762
|
-
|
|
2141
|
+
for (const command of DELETE_KEY_COMMANDS) {
|
|
2142
|
+
tableObserver.listenersToRemove.add(editor.registerCommand(command, $deleteCellHandler, lexical.COMMAND_PRIORITY_CRITICAL));
|
|
2143
|
+
}
|
|
1763
2144
|
tableObserver.listenersToRemove.add(editor.registerCommand(lexical.CUT_COMMAND, event => {
|
|
1764
2145
|
const selection = lexical.$getSelection();
|
|
1765
2146
|
if (selection) {
|
|
@@ -1772,6 +2153,7 @@ function applyTableHandlers(tableNode, tableElement, editor, hasTabHandler) {
|
|
|
1772
2153
|
const intercepted = $deleteCellHandler(event);
|
|
1773
2154
|
if (lexical.$isRangeSelection(selection)) {
|
|
1774
2155
|
selection.removeText();
|
|
2156
|
+
return true;
|
|
1775
2157
|
}
|
|
1776
2158
|
return intercepted;
|
|
1777
2159
|
}
|
|
@@ -1783,7 +2165,7 @@ function applyTableHandlers(tableNode, tableElement, editor, hasTabHandler) {
|
|
|
1783
2165
|
return false;
|
|
1784
2166
|
}
|
|
1785
2167
|
if ($isTableSelection(selection)) {
|
|
1786
|
-
tableObserver
|
|
2168
|
+
tableObserver.$formatCells(payload);
|
|
1787
2169
|
return true;
|
|
1788
2170
|
} else if (lexical.$isRangeSelection(selection)) {
|
|
1789
2171
|
const tableCellNode = LexicalUtils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
|
|
@@ -1803,14 +2185,25 @@ function applyTableHandlers(tableNode, tableElement, editor, hasTabHandler) {
|
|
|
1803
2185
|
if (!$isTableCellNode(anchorNode) || !$isTableCellNode(focusNode)) {
|
|
1804
2186
|
return false;
|
|
1805
2187
|
}
|
|
2188
|
+
|
|
2189
|
+
// Align the table if the entire table is selected
|
|
2190
|
+
if ($isFullTableSelection(selection, tableNode)) {
|
|
2191
|
+
tableNode.setFormat(formatType);
|
|
2192
|
+
return true;
|
|
2193
|
+
}
|
|
1806
2194
|
const [tableMap, anchorCell, focusCell] = $computeTableMap(tableNode, anchorNode, focusNode);
|
|
1807
|
-
const maxRow = Math.max(anchorCell.startRow, focusCell.startRow);
|
|
1808
|
-
const maxColumn = Math.max(anchorCell.startColumn, focusCell.startColumn);
|
|
2195
|
+
const maxRow = Math.max(anchorCell.startRow + anchorCell.cell.__rowSpan - 1, focusCell.startRow + focusCell.cell.__rowSpan - 1);
|
|
2196
|
+
const maxColumn = Math.max(anchorCell.startColumn + anchorCell.cell.__colSpan - 1, focusCell.startColumn + focusCell.cell.__colSpan - 1);
|
|
1809
2197
|
const minRow = Math.min(anchorCell.startRow, focusCell.startRow);
|
|
1810
2198
|
const minColumn = Math.min(anchorCell.startColumn, focusCell.startColumn);
|
|
2199
|
+
const visited = new Set();
|
|
1811
2200
|
for (let i = minRow; i <= maxRow; i++) {
|
|
1812
2201
|
for (let j = minColumn; j <= maxColumn; j++) {
|
|
1813
2202
|
const cell = tableMap[i][j].cell;
|
|
2203
|
+
if (visited.has(cell)) {
|
|
2204
|
+
continue;
|
|
2205
|
+
}
|
|
2206
|
+
visited.add(cell);
|
|
1814
2207
|
cell.setFormat(formatType);
|
|
1815
2208
|
const cellChildren = cell.getChildren();
|
|
1816
2209
|
for (let k = 0; k < cellChildren.length; k++) {
|
|
@@ -1829,7 +2222,7 @@ function applyTableHandlers(tableNode, tableElement, editor, hasTabHandler) {
|
|
|
1829
2222
|
return false;
|
|
1830
2223
|
}
|
|
1831
2224
|
if ($isTableSelection(selection)) {
|
|
1832
|
-
tableObserver
|
|
2225
|
+
tableObserver.$clearHighlight();
|
|
1833
2226
|
return false;
|
|
1834
2227
|
} else if (lexical.$isRangeSelection(selection)) {
|
|
1835
2228
|
const tableCellNode = LexicalUtils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
|
|
@@ -1853,22 +2246,17 @@ function applyTableHandlers(tableNode, tableElement, editor, hasTabHandler) {
|
|
|
1853
2246
|
return false;
|
|
1854
2247
|
}
|
|
1855
2248
|
const tableCellNode = $findCellNode(selection.anchor.getNode());
|
|
1856
|
-
if (tableCellNode === null) {
|
|
2249
|
+
if (tableCellNode === null || !tableNode.is($findTableNode(tableCellNode))) {
|
|
1857
2250
|
return false;
|
|
1858
2251
|
}
|
|
1859
2252
|
stopEvent(event);
|
|
1860
|
-
|
|
1861
|
-
selectTableNodeInDirection(tableObserver, tableNode, currentCords.x, currentCords.y, !event.shiftKey ? 'forward' : 'backward');
|
|
2253
|
+
$selectAdjacentCell(tableCellNode, event.shiftKey ? 'previous' : 'next');
|
|
1862
2254
|
return true;
|
|
1863
2255
|
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
|
1864
2256
|
}
|
|
1865
2257
|
tableObserver.listenersToRemove.add(editor.registerCommand(lexical.FOCUS_COMMAND, payload => {
|
|
1866
2258
|
return tableNode.isSelected();
|
|
1867
2259
|
}, lexical.COMMAND_PRIORITY_HIGH));
|
|
1868
|
-
function getObserverCellFromCellNode(tableCellNode) {
|
|
1869
|
-
const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableObserver.table);
|
|
1870
|
-
return tableNode.getDOMCellFromCordsOrThrow(currentCords.x, currentCords.y, tableObserver.table);
|
|
1871
|
-
}
|
|
1872
2260
|
tableObserver.listenersToRemove.add(editor.registerCommand(lexical.SELECTION_INSERT_CLIPBOARD_NODES_COMMAND, selectionPayload => {
|
|
1873
2261
|
const {
|
|
1874
2262
|
nodes,
|
|
@@ -1943,6 +2331,42 @@ function applyTableHandlers(tableNode, tableElement, editor, hasTabHandler) {
|
|
|
1943
2331
|
tableObserver.listenersToRemove.add(editor.registerCommand(lexical.SELECTION_CHANGE_COMMAND, () => {
|
|
1944
2332
|
const selection = lexical.$getSelection();
|
|
1945
2333
|
const prevSelection = lexical.$getPreviousSelection();
|
|
2334
|
+
const nextFocus = tableObserver.getAndClearNextFocus();
|
|
2335
|
+
if (nextFocus !== null) {
|
|
2336
|
+
const {
|
|
2337
|
+
focusCell
|
|
2338
|
+
} = nextFocus;
|
|
2339
|
+
if ($isTableSelection(selection) && selection.tableKey === tableObserver.tableNodeKey) {
|
|
2340
|
+
if (focusCell.x === tableObserver.focusX && focusCell.y === tableObserver.focusY) {
|
|
2341
|
+
// The selection is already the correct table selection
|
|
2342
|
+
return false;
|
|
2343
|
+
} else {
|
|
2344
|
+
tableObserver.$setFocusCellForSelection(focusCell);
|
|
2345
|
+
return true;
|
|
2346
|
+
}
|
|
2347
|
+
} else if (focusCell !== tableObserver.anchorCell && $isSelectionInTable(selection, tableNode)) {
|
|
2348
|
+
// The selection has crossed cells
|
|
2349
|
+
tableObserver.$setFocusCellForSelection(focusCell);
|
|
2350
|
+
return true;
|
|
2351
|
+
}
|
|
2352
|
+
}
|
|
2353
|
+
const shouldCheckSelection = tableObserver.getAndClearShouldCheckSelection();
|
|
2354
|
+
// If they pressed the down arrow with the selection outside of the
|
|
2355
|
+
// table, and then the selection ends up in the table but not in the
|
|
2356
|
+
// first cell, then move the selection to the first cell.
|
|
2357
|
+
if (shouldCheckSelection && lexical.$isRangeSelection(prevSelection) && lexical.$isRangeSelection(selection) && selection.isCollapsed()) {
|
|
2358
|
+
const anchor = selection.anchor.getNode();
|
|
2359
|
+
const firstRow = tableNode.getFirstChild();
|
|
2360
|
+
const anchorCell = $findCellNode(anchor);
|
|
2361
|
+
if (anchorCell !== null && $isTableRowNode(firstRow)) {
|
|
2362
|
+
const firstCell = firstRow.getFirstChild();
|
|
2363
|
+
if ($isTableCellNode(firstCell) && tableNode.is(LexicalUtils.$findMatchingParent(anchorCell, node => node.is(tableNode) || node.is(firstCell)))) {
|
|
2364
|
+
// The selection moved to the table, but not in the first cell
|
|
2365
|
+
firstCell.selectStart();
|
|
2366
|
+
return true;
|
|
2367
|
+
}
|
|
2368
|
+
}
|
|
2369
|
+
}
|
|
1946
2370
|
if (lexical.$isRangeSelection(selection)) {
|
|
1947
2371
|
const {
|
|
1948
2372
|
anchor,
|
|
@@ -1956,46 +2380,58 @@ function applyTableHandlers(tableNode, tableElement, editor, hasTabHandler) {
|
|
|
1956
2380
|
const focusCellNode = $findCellNode(focusNode);
|
|
1957
2381
|
const isAnchorInside = !!(anchorCellNode && tableNode.is($findTableNode(anchorCellNode)));
|
|
1958
2382
|
const isFocusInside = !!(focusCellNode && tableNode.is($findTableNode(focusCellNode)));
|
|
1959
|
-
const
|
|
2383
|
+
const isPartiallyWithinTable = isAnchorInside !== isFocusInside;
|
|
1960
2384
|
const isWithinTable = isAnchorInside && isFocusInside;
|
|
1961
2385
|
const isBackward = selection.isBackward();
|
|
1962
|
-
if (
|
|
2386
|
+
if (isPartiallyWithinTable) {
|
|
1963
2387
|
const newSelection = selection.clone();
|
|
1964
2388
|
if (isFocusInside) {
|
|
1965
2389
|
const [tableMap] = $computeTableMap(tableNode, focusCellNode, focusCellNode);
|
|
1966
2390
|
const firstCell = tableMap[0][0].cell;
|
|
1967
2391
|
const lastCell = tableMap[tableMap.length - 1].at(-1).cell;
|
|
1968
2392
|
newSelection.focus.set(isBackward ? firstCell.getKey() : lastCell.getKey(), isBackward ? firstCell.getChildrenSize() : lastCell.getChildrenSize(), 'element');
|
|
2393
|
+
} else if (isAnchorInside) {
|
|
2394
|
+
const [tableMap] = $computeTableMap(tableNode, anchorCellNode, anchorCellNode);
|
|
2395
|
+
const firstCell = tableMap[0][0].cell;
|
|
2396
|
+
const lastCell = tableMap[tableMap.length - 1].at(-1).cell;
|
|
2397
|
+
/**
|
|
2398
|
+
* If isBackward, set the anchor to be at the end of the table so that when the cursor moves outside of
|
|
2399
|
+
* the table in the backward direction, the entire table will be selected from its end.
|
|
2400
|
+
* Otherwise, if forward, set the anchor to be at the start of the table so that when the focus is dragged
|
|
2401
|
+
* outside th end of the table, it will start from the beginning of the table.
|
|
2402
|
+
*/
|
|
2403
|
+
newSelection.anchor.set(isBackward ? lastCell.getKey() : firstCell.getKey(), isBackward ? lastCell.getChildrenSize() : 0, 'element');
|
|
1969
2404
|
}
|
|
1970
2405
|
lexical.$setSelection(newSelection);
|
|
1971
2406
|
$addHighlightStyleToTable(editor, tableObserver);
|
|
1972
2407
|
} else if (isWithinTable) {
|
|
1973
2408
|
// Handle case when selection spans across multiple cells but still
|
|
1974
|
-
// has range selection, then we convert it into
|
|
2409
|
+
// has range selection, then we convert it into table selection
|
|
1975
2410
|
if (!anchorCellNode.is(focusCellNode)) {
|
|
1976
|
-
tableObserver
|
|
1977
|
-
tableObserver
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
2411
|
+
tableObserver.$setAnchorCellForSelection($getObserverCellFromCellNodeOrThrow(tableObserver, anchorCellNode));
|
|
2412
|
+
tableObserver.$setFocusCellForSelection($getObserverCellFromCellNodeOrThrow(tableObserver, focusCellNode), true);
|
|
2413
|
+
}
|
|
2414
|
+
|
|
2415
|
+
// Handle case when the pointer type is touch and the current and
|
|
2416
|
+
// previous selection are collapsed, and the previous anchor and current
|
|
2417
|
+
// focus cell nodes are different, then we convert it into table selection
|
|
2418
|
+
if (tableObserver.pointerType === 'touch' && selection.isCollapsed() && lexical.$isRangeSelection(prevSelection) && prevSelection.isCollapsed()) {
|
|
2419
|
+
const prevAnchorCellNode = $findCellNode(prevSelection.anchor.getNode());
|
|
2420
|
+
if (prevAnchorCellNode && !prevAnchorCellNode.is(focusCellNode)) {
|
|
2421
|
+
tableObserver.$setAnchorCellForSelection($getObserverCellFromCellNodeOrThrow(tableObserver, prevAnchorCellNode));
|
|
2422
|
+
tableObserver.$setFocusCellForSelection($getObserverCellFromCellNodeOrThrow(tableObserver, focusCellNode), true);
|
|
2423
|
+
tableObserver.pointerType = null;
|
|
1988
2424
|
}
|
|
1989
2425
|
}
|
|
1990
2426
|
}
|
|
1991
2427
|
} else if (selection && $isTableSelection(selection) && selection.is(prevSelection) && selection.tableKey === tableNode.getKey()) {
|
|
1992
2428
|
// if selection goes outside of the table we need to change it to Range selection
|
|
1993
|
-
const domSelection = getDOMSelection(
|
|
2429
|
+
const domSelection = lexical.getDOMSelection(editorWindow);
|
|
1994
2430
|
if (domSelection && domSelection.anchorNode && domSelection.focusNode) {
|
|
1995
2431
|
const focusNode = lexical.$getNearestNodeFromDOMNode(domSelection.focusNode);
|
|
1996
|
-
const isFocusOutside = focusNode && !tableNode.
|
|
2432
|
+
const isFocusOutside = focusNode && !tableNode.isParentOf(focusNode);
|
|
1997
2433
|
const anchorNode = lexical.$getNearestNodeFromDOMNode(domSelection.anchorNode);
|
|
1998
|
-
const isAnchorInside = anchorNode && tableNode.
|
|
2434
|
+
const isAnchorInside = anchorNode && tableNode.isParentOf(anchorNode);
|
|
1999
2435
|
if (isFocusOutside && isAnchorInside && domSelection.rangeCount > 0) {
|
|
2000
2436
|
const newSelection = lexical.$createRangeSelectionFromDom(domSelection, editor);
|
|
2001
2437
|
if (newSelection) {
|
|
@@ -2008,9 +2444,9 @@ function applyTableHandlers(tableNode, tableElement, editor, hasTabHandler) {
|
|
|
2008
2444
|
}
|
|
2009
2445
|
if (selection && !selection.is(prevSelection) && ($isTableSelection(selection) || $isTableSelection(prevSelection)) && tableObserver.tableSelection && !tableObserver.tableSelection.is(prevSelection)) {
|
|
2010
2446
|
if ($isTableSelection(selection) && selection.tableKey === tableObserver.tableNodeKey) {
|
|
2011
|
-
tableObserver
|
|
2447
|
+
tableObserver.$updateTableTableSelection(selection);
|
|
2012
2448
|
} else if (!$isTableSelection(selection) && $isTableSelection(prevSelection) && prevSelection.tableKey === tableObserver.tableNodeKey) {
|
|
2013
|
-
tableObserver
|
|
2449
|
+
tableObserver.$updateTableTableSelection(null);
|
|
2014
2450
|
}
|
|
2015
2451
|
return false;
|
|
2016
2452
|
}
|
|
@@ -2035,11 +2471,19 @@ function applyTableHandlers(tableNode, tableElement, editor, hasTabHandler) {
|
|
|
2035
2471
|
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
|
2036
2472
|
return tableObserver;
|
|
2037
2473
|
}
|
|
2474
|
+
function detatchTableObserverFromTableElement(tableElement, tableObserver) {
|
|
2475
|
+
if (getTableObserverFromTableElement(tableElement) === tableObserver) {
|
|
2476
|
+
delete tableElement[LEXICAL_ELEMENT_KEY];
|
|
2477
|
+
}
|
|
2478
|
+
}
|
|
2038
2479
|
function attachTableObserverToTableElement(tableElement, tableObserver) {
|
|
2480
|
+
if (!(getTableObserverFromTableElement(tableElement) === null)) {
|
|
2481
|
+
formatDevErrorMessage(`tableElement already has an attached TableObserver`);
|
|
2482
|
+
}
|
|
2039
2483
|
tableElement[LEXICAL_ELEMENT_KEY] = tableObserver;
|
|
2040
2484
|
}
|
|
2041
2485
|
function getTableObserverFromTableElement(tableElement) {
|
|
2042
|
-
return tableElement[LEXICAL_ELEMENT_KEY];
|
|
2486
|
+
return tableElement[LEXICAL_ELEMENT_KEY] || null;
|
|
2043
2487
|
}
|
|
2044
2488
|
function getDOMCellFromTarget(node) {
|
|
2045
2489
|
let currentNode = node;
|
|
@@ -2057,14 +2501,32 @@ function getDOMCellFromTarget(node) {
|
|
|
2057
2501
|
}
|
|
2058
2502
|
return null;
|
|
2059
2503
|
}
|
|
2060
|
-
function
|
|
2504
|
+
function getDOMCellInTableFromTarget(table, node) {
|
|
2505
|
+
if (!table.contains(node)) {
|
|
2506
|
+
return null;
|
|
2507
|
+
}
|
|
2508
|
+
let cell = null;
|
|
2509
|
+
for (let currentNode = node; currentNode != null; currentNode = currentNode.parentNode) {
|
|
2510
|
+
if (currentNode === table) {
|
|
2511
|
+
return cell;
|
|
2512
|
+
}
|
|
2513
|
+
const nodeName = currentNode.nodeName;
|
|
2514
|
+
if (nodeName === 'TD' || nodeName === 'TH') {
|
|
2515
|
+
// @ts-expect-error: internal field
|
|
2516
|
+
cell = currentNode._cell || null;
|
|
2517
|
+
}
|
|
2518
|
+
}
|
|
2519
|
+
return null;
|
|
2520
|
+
}
|
|
2521
|
+
function getTable(tableNode, dom) {
|
|
2522
|
+
const tableElement = getTableElement(tableNode, dom);
|
|
2061
2523
|
const domRows = [];
|
|
2062
2524
|
const grid = {
|
|
2063
2525
|
columns: 0,
|
|
2064
2526
|
domRows,
|
|
2065
2527
|
rows: 0
|
|
2066
2528
|
};
|
|
2067
|
-
let currentNode = tableElement.
|
|
2529
|
+
let currentNode = tableElement.querySelector('tr');
|
|
2068
2530
|
let x = 0;
|
|
2069
2531
|
let y = 0;
|
|
2070
2532
|
domRows.length = 0;
|
|
@@ -2156,14 +2618,14 @@ function $forEachTableCell(grid, cb) {
|
|
|
2156
2618
|
}
|
|
2157
2619
|
}
|
|
2158
2620
|
function $addHighlightStyleToTable(editor, tableSelection) {
|
|
2159
|
-
tableSelection
|
|
2621
|
+
tableSelection.$disableHighlightStyle();
|
|
2160
2622
|
$forEachTableCell(tableSelection.table, cell => {
|
|
2161
2623
|
cell.highlighted = true;
|
|
2162
2624
|
$addHighlightToDOM(editor, cell);
|
|
2163
2625
|
});
|
|
2164
2626
|
}
|
|
2165
2627
|
function $removeHighlightStyleToTable(editor, tableObserver) {
|
|
2166
|
-
tableObserver
|
|
2628
|
+
tableObserver.$enableHighlightStyle();
|
|
2167
2629
|
$forEachTableCell(tableObserver.table, cell => {
|
|
2168
2630
|
const elem = cell.elem;
|
|
2169
2631
|
cell.highlighted = false;
|
|
@@ -2173,6 +2635,29 @@ function $removeHighlightStyleToTable(editor, tableObserver) {
|
|
|
2173
2635
|
}
|
|
2174
2636
|
});
|
|
2175
2637
|
}
|
|
2638
|
+
function $selectAdjacentCell(tableCellNode, direction) {
|
|
2639
|
+
const siblingMethod = direction === 'next' ? 'getNextSibling' : 'getPreviousSibling';
|
|
2640
|
+
const childMethod = direction === 'next' ? 'getFirstChild' : 'getLastChild';
|
|
2641
|
+
const sibling = tableCellNode[siblingMethod]();
|
|
2642
|
+
if (lexical.$isElementNode(sibling)) {
|
|
2643
|
+
return sibling.selectEnd();
|
|
2644
|
+
}
|
|
2645
|
+
const parentRow = LexicalUtils.$findMatchingParent(tableCellNode, $isTableRowNode);
|
|
2646
|
+
if (!(parentRow !== null)) {
|
|
2647
|
+
formatDevErrorMessage(`selectAdjacentCell: Cell not in table row`);
|
|
2648
|
+
}
|
|
2649
|
+
for (let nextRow = parentRow[siblingMethod](); $isTableRowNode(nextRow); nextRow = nextRow[siblingMethod]()) {
|
|
2650
|
+
const child = nextRow[childMethod]();
|
|
2651
|
+
if (lexical.$isElementNode(child)) {
|
|
2652
|
+
return child.selectEnd();
|
|
2653
|
+
}
|
|
2654
|
+
}
|
|
2655
|
+
const parentTable = LexicalUtils.$findMatchingParent(parentRow, $isTableNode);
|
|
2656
|
+
if (!(parentTable !== null)) {
|
|
2657
|
+
formatDevErrorMessage(`selectAdjacentCell: Row not in table`);
|
|
2658
|
+
}
|
|
2659
|
+
return direction === 'next' ? parentTable.selectNext() : parentTable.selectPrevious();
|
|
2660
|
+
}
|
|
2176
2661
|
const selectTableNodeInDirection = (tableObserver, tableNode, x, y, direction) => {
|
|
2177
2662
|
const isForward = direction === 'forward';
|
|
2178
2663
|
switch (direction) {
|
|
@@ -2208,41 +2693,127 @@ const selectTableNodeInDirection = (tableObserver, tableNode, x, y, direction) =
|
|
|
2208
2693
|
return false;
|
|
2209
2694
|
}
|
|
2210
2695
|
};
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
case 'up':
|
|
2221
|
-
if (y !== 0) {
|
|
2222
|
-
tableObserver.setFocusCellForSelection(tableNode.getDOMCellFromCordsOrThrow(x, y - 1, tableObserver.table));
|
|
2223
|
-
return true;
|
|
2224
|
-
} else {
|
|
2225
|
-
return false;
|
|
2226
|
-
}
|
|
2227
|
-
case 'down':
|
|
2228
|
-
if (y !== tableObserver.table.rows - 1) {
|
|
2229
|
-
tableObserver.setFocusCellForSelection(tableNode.getDOMCellFromCordsOrThrow(x, y + 1, tableObserver.table));
|
|
2230
|
-
return true;
|
|
2231
|
-
} else {
|
|
2232
|
-
return false;
|
|
2233
|
-
}
|
|
2234
|
-
default:
|
|
2235
|
-
return false;
|
|
2696
|
+
function getCorner(rect, cellValue) {
|
|
2697
|
+
let colName;
|
|
2698
|
+
let rowName;
|
|
2699
|
+
if (cellValue.startColumn === rect.minColumn) {
|
|
2700
|
+
colName = 'minColumn';
|
|
2701
|
+
} else if (cellValue.startColumn + cellValue.cell.__colSpan - 1 === rect.maxColumn) {
|
|
2702
|
+
colName = 'maxColumn';
|
|
2703
|
+
} else {
|
|
2704
|
+
return null;
|
|
2236
2705
|
}
|
|
2237
|
-
|
|
2706
|
+
if (cellValue.startRow === rect.minRow) {
|
|
2707
|
+
rowName = 'minRow';
|
|
2708
|
+
} else if (cellValue.startRow + cellValue.cell.__rowSpan - 1 === rect.maxRow) {
|
|
2709
|
+
rowName = 'maxRow';
|
|
2710
|
+
} else {
|
|
2711
|
+
return null;
|
|
2712
|
+
}
|
|
2713
|
+
return [colName, rowName];
|
|
2714
|
+
}
|
|
2715
|
+
function getCornerOrThrow(rect, cellValue) {
|
|
2716
|
+
const corner = getCorner(rect, cellValue);
|
|
2717
|
+
if (!(corner !== null)) {
|
|
2718
|
+
formatDevErrorMessage(`getCornerOrThrow: cell ${cellValue.cell.getKey()} is not at a corner of rect`);
|
|
2719
|
+
}
|
|
2720
|
+
return corner;
|
|
2721
|
+
}
|
|
2722
|
+
function oppositeCorner([colName, rowName]) {
|
|
2723
|
+
return [colName === 'minColumn' ? 'maxColumn' : 'minColumn', rowName === 'minRow' ? 'maxRow' : 'minRow'];
|
|
2724
|
+
}
|
|
2725
|
+
function cellAtCornerOrThrow(tableMap, rect, [colName, rowName]) {
|
|
2726
|
+
const rowNum = rect[rowName];
|
|
2727
|
+
const rowMap = tableMap[rowNum];
|
|
2728
|
+
if (!(rowMap !== undefined)) {
|
|
2729
|
+
formatDevErrorMessage(`cellAtCornerOrThrow: ${rowName} = ${String(rowNum)} missing in tableMap`);
|
|
2730
|
+
}
|
|
2731
|
+
const colNum = rect[colName];
|
|
2732
|
+
const cell = rowMap[colNum];
|
|
2733
|
+
if (!(cell !== undefined)) {
|
|
2734
|
+
formatDevErrorMessage(`cellAtCornerOrThrow: ${colName} = ${String(colNum)} missing in tableMap`);
|
|
2735
|
+
}
|
|
2736
|
+
return cell;
|
|
2737
|
+
}
|
|
2738
|
+
function $extractRectCorners(tableMap, anchorCellValue, newFocusCellValue) {
|
|
2739
|
+
// We are sure that the focus now either contracts or expands the rect
|
|
2740
|
+
// but both the anchor and focus might be moved to ensure a rectangle
|
|
2741
|
+
// given a potentially ragged merge shape
|
|
2742
|
+
const rect = $computeTableCellRectBoundary(tableMap, anchorCellValue, newFocusCellValue);
|
|
2743
|
+
const anchorCorner = getCorner(rect, anchorCellValue);
|
|
2744
|
+
if (anchorCorner) {
|
|
2745
|
+
return [cellAtCornerOrThrow(tableMap, rect, anchorCorner), cellAtCornerOrThrow(tableMap, rect, oppositeCorner(anchorCorner))];
|
|
2746
|
+
}
|
|
2747
|
+
const newFocusCorner = getCorner(rect, newFocusCellValue);
|
|
2748
|
+
if (newFocusCorner) {
|
|
2749
|
+
return [cellAtCornerOrThrow(tableMap, rect, oppositeCorner(newFocusCorner)), cellAtCornerOrThrow(tableMap, rect, newFocusCorner)];
|
|
2750
|
+
}
|
|
2751
|
+
// TODO this doesn't have to be arbitrary, use the closest corner instead
|
|
2752
|
+
const newAnchorCorner = ['minColumn', 'minRow'];
|
|
2753
|
+
return [cellAtCornerOrThrow(tableMap, rect, newAnchorCorner), cellAtCornerOrThrow(tableMap, rect, oppositeCorner(newAnchorCorner))];
|
|
2754
|
+
}
|
|
2755
|
+
function $adjustFocusInDirection(tableObserver, tableMap, anchorCellValue, focusCellValue, direction) {
|
|
2756
|
+
const rect = $computeTableCellRectBoundary(tableMap, anchorCellValue, focusCellValue);
|
|
2757
|
+
const spans = $computeTableCellRectSpans(tableMap, rect);
|
|
2758
|
+
const {
|
|
2759
|
+
topSpan,
|
|
2760
|
+
leftSpan,
|
|
2761
|
+
bottomSpan,
|
|
2762
|
+
rightSpan
|
|
2763
|
+
} = spans;
|
|
2764
|
+
const anchorCorner = getCornerOrThrow(rect, anchorCellValue);
|
|
2765
|
+
const [focusColumn, focusRow] = oppositeCorner(anchorCorner);
|
|
2766
|
+
let fCol = rect[focusColumn];
|
|
2767
|
+
let fRow = rect[focusRow];
|
|
2768
|
+
if (direction === 'forward') {
|
|
2769
|
+
fCol += focusColumn === 'maxColumn' ? 1 : leftSpan;
|
|
2770
|
+
} else if (direction === 'backward') {
|
|
2771
|
+
fCol -= focusColumn === 'minColumn' ? 1 : rightSpan;
|
|
2772
|
+
} else if (direction === 'down') {
|
|
2773
|
+
fRow += focusRow === 'maxRow' ? 1 : topSpan;
|
|
2774
|
+
} else if (direction === 'up') {
|
|
2775
|
+
fRow -= focusRow === 'minRow' ? 1 : bottomSpan;
|
|
2776
|
+
}
|
|
2777
|
+
const targetRowMap = tableMap[fRow];
|
|
2778
|
+
if (targetRowMap === undefined) {
|
|
2779
|
+
return false;
|
|
2780
|
+
}
|
|
2781
|
+
const newFocusCellValue = targetRowMap[fCol];
|
|
2782
|
+
if (newFocusCellValue === undefined) {
|
|
2783
|
+
return false;
|
|
2784
|
+
}
|
|
2785
|
+
// We can be certain that anchorCellValue and newFocusCellValue are
|
|
2786
|
+
// contained within the desired selection, but we are not certain if
|
|
2787
|
+
// they need to be expanded or not to maintain a rectangular shape
|
|
2788
|
+
const [finalAnchorCell, finalFocusCell] = $extractRectCorners(tableMap, anchorCellValue, newFocusCellValue);
|
|
2789
|
+
const anchorDOM = $getObserverCellFromCellNodeOrThrow(tableObserver, finalAnchorCell.cell);
|
|
2790
|
+
const focusDOM = $getObserverCellFromCellNodeOrThrow(tableObserver, finalFocusCell.cell);
|
|
2791
|
+
tableObserver.$setAnchorCellForSelection(anchorDOM);
|
|
2792
|
+
tableObserver.$setFocusCellForSelection(focusDOM, true);
|
|
2793
|
+
return true;
|
|
2794
|
+
}
|
|
2238
2795
|
function $isSelectionInTable(selection, tableNode) {
|
|
2239
2796
|
if (lexical.$isRangeSelection(selection) || $isTableSelection(selection)) {
|
|
2797
|
+
// TODO this should probably return false if there's an unrelated
|
|
2798
|
+
// shadow root between the node and the table (e.g. another table,
|
|
2799
|
+
// collapsible, etc.)
|
|
2240
2800
|
const isAnchorInside = tableNode.isParentOf(selection.anchor.getNode());
|
|
2241
2801
|
const isFocusInside = tableNode.isParentOf(selection.focus.getNode());
|
|
2242
2802
|
return isAnchorInside && isFocusInside;
|
|
2243
2803
|
}
|
|
2244
2804
|
return false;
|
|
2245
2805
|
}
|
|
2806
|
+
function $isFullTableSelection(selection, tableNode) {
|
|
2807
|
+
if ($isTableSelection(selection)) {
|
|
2808
|
+
const anchorNode = selection.anchor.getNode();
|
|
2809
|
+
const focusNode = selection.focus.getNode();
|
|
2810
|
+
if (tableNode && anchorNode && focusNode) {
|
|
2811
|
+
const [map] = $computeTableMap(tableNode, anchorNode, focusNode);
|
|
2812
|
+
return anchorNode.getKey() === map[0][0].cell.getKey() && focusNode.getKey() === map[map.length - 1].at(-1).cell.getKey();
|
|
2813
|
+
}
|
|
2814
|
+
}
|
|
2815
|
+
return false;
|
|
2816
|
+
}
|
|
2246
2817
|
function selectTableCellNode(tableCell, fromStart) {
|
|
2247
2818
|
if (fromStart) {
|
|
2248
2819
|
tableCell.selectStart();
|
|
@@ -2250,33 +2821,23 @@ function selectTableCellNode(tableCell, fromStart) {
|
|
|
2250
2821
|
tableCell.selectEnd();
|
|
2251
2822
|
}
|
|
2252
2823
|
}
|
|
2253
|
-
const BROWSER_BLUE_RGB = '172,206,247';
|
|
2254
2824
|
function $addHighlightToDOM(editor, cell) {
|
|
2255
2825
|
const element = cell.elem;
|
|
2826
|
+
const editorThemeClasses = editor._config.theme;
|
|
2256
2827
|
const node = lexical.$getNearestNodeFromDOMNode(element);
|
|
2257
2828
|
if (!$isTableCellNode(node)) {
|
|
2258
|
-
|
|
2829
|
+
formatDevErrorMessage(`Expected to find LexicalNode from Table Cell DOMNode`);
|
|
2259
2830
|
}
|
|
2260
|
-
|
|
2261
|
-
if (backgroundColor === null) {
|
|
2262
|
-
element.style.setProperty('background-color', `rgb(${BROWSER_BLUE_RGB})`);
|
|
2263
|
-
} else {
|
|
2264
|
-
element.style.setProperty('background-image', `linear-gradient(to right, rgba(${BROWSER_BLUE_RGB},0.85), rgba(${BROWSER_BLUE_RGB},0.85))`);
|
|
2265
|
-
}
|
|
2266
|
-
element.style.setProperty('caret-color', 'transparent');
|
|
2831
|
+
LexicalUtils.addClassNamesToElement(element, editorThemeClasses.tableCellSelected);
|
|
2267
2832
|
}
|
|
2268
2833
|
function $removeHighlightFromDOM(editor, cell) {
|
|
2269
2834
|
const element = cell.elem;
|
|
2270
2835
|
const node = lexical.$getNearestNodeFromDOMNode(element);
|
|
2271
2836
|
if (!$isTableCellNode(node)) {
|
|
2272
|
-
|
|
2273
|
-
}
|
|
2274
|
-
const backgroundColor = node.getBackgroundColor();
|
|
2275
|
-
if (backgroundColor === null) {
|
|
2276
|
-
element.style.removeProperty('background-color');
|
|
2837
|
+
formatDevErrorMessage(`Expected to find LexicalNode from Table Cell DOMNode`);
|
|
2277
2838
|
}
|
|
2278
|
-
|
|
2279
|
-
element.
|
|
2839
|
+
const editorThemeClasses = editor._config.theme;
|
|
2840
|
+
LexicalUtils.removeClassNamesFromElement(element, editorThemeClasses.tableCellSelected);
|
|
2280
2841
|
}
|
|
2281
2842
|
function $findCellNode(node) {
|
|
2282
2843
|
const cellNode = LexicalUtils.$findMatchingParent(node, $isTableCellNode);
|
|
@@ -2286,6 +2847,98 @@ function $findTableNode(node) {
|
|
|
2286
2847
|
const tableNode = LexicalUtils.$findMatchingParent(node, $isTableNode);
|
|
2287
2848
|
return $isTableNode(tableNode) ? tableNode : null;
|
|
2288
2849
|
}
|
|
2850
|
+
function $getBlockParentIfFirstNode(node) {
|
|
2851
|
+
for (let prevNode = node, currentNode = node; currentNode !== null; prevNode = currentNode, currentNode = currentNode.getParent()) {
|
|
2852
|
+
if (lexical.$isElementNode(currentNode)) {
|
|
2853
|
+
if (currentNode !== prevNode && currentNode.getFirstChild() !== prevNode) {
|
|
2854
|
+
// Not the first child or the initial node
|
|
2855
|
+
return null;
|
|
2856
|
+
} else if (!currentNode.isInline()) {
|
|
2857
|
+
return currentNode;
|
|
2858
|
+
}
|
|
2859
|
+
}
|
|
2860
|
+
}
|
|
2861
|
+
return null;
|
|
2862
|
+
}
|
|
2863
|
+
function $handleHorizontalArrowKeyRangeSelection(editor, event, selection, alter, isBackward, tableNode, tableObserver) {
|
|
2864
|
+
const initialFocus = lexical.$caretFromPoint(selection.focus, isBackward ? 'previous' : 'next');
|
|
2865
|
+
if (lexical.$isExtendableTextPointCaret(initialFocus)) {
|
|
2866
|
+
return false;
|
|
2867
|
+
}
|
|
2868
|
+
let lastCaret = initialFocus;
|
|
2869
|
+
// TableCellNode is the only shadow root we are interested in piercing so
|
|
2870
|
+
// we find the last internal caret and then check its parent
|
|
2871
|
+
for (const nextCaret of lexical.$extendCaretToRange(initialFocus).iterNodeCarets('shadowRoot')) {
|
|
2872
|
+
if (!(lexical.$isSiblingCaret(nextCaret) && lexical.$isElementNode(nextCaret.origin))) {
|
|
2873
|
+
return false;
|
|
2874
|
+
}
|
|
2875
|
+
lastCaret = nextCaret;
|
|
2876
|
+
}
|
|
2877
|
+
const lastCaretParent = lastCaret.getParentAtCaret();
|
|
2878
|
+
if (!$isTableCellNode(lastCaretParent)) {
|
|
2879
|
+
return false;
|
|
2880
|
+
}
|
|
2881
|
+
const anchorCell = lastCaretParent;
|
|
2882
|
+
const focusCaret = $findNextTableCell(lexical.$getSiblingCaret(anchorCell, lastCaret.direction));
|
|
2883
|
+
const anchorCellTable = LexicalUtils.$findMatchingParent(anchorCell, $isTableNode);
|
|
2884
|
+
if (!(anchorCellTable && anchorCellTable.is(tableNode))) {
|
|
2885
|
+
return false;
|
|
2886
|
+
}
|
|
2887
|
+
const anchorCellDOM = editor.getElementByKey(anchorCell.getKey());
|
|
2888
|
+
const anchorDOMCell = getDOMCellFromTarget(anchorCellDOM);
|
|
2889
|
+
if (!anchorCellDOM || !anchorDOMCell) {
|
|
2890
|
+
return false;
|
|
2891
|
+
}
|
|
2892
|
+
const anchorCellTableElement = $getElementForTableNode(editor, anchorCellTable);
|
|
2893
|
+
tableObserver.table = anchorCellTableElement;
|
|
2894
|
+
if (!focusCaret) {
|
|
2895
|
+
if (alter === 'extend') {
|
|
2896
|
+
// extend the selection from a range inside the cell to a table selection of the cell
|
|
2897
|
+
tableObserver.$setAnchorCellForSelection(anchorDOMCell);
|
|
2898
|
+
tableObserver.$setFocusCellForSelection(anchorDOMCell, true);
|
|
2899
|
+
} else {
|
|
2900
|
+
// exit the table
|
|
2901
|
+
const outerFocusCaret = $getTableExitCaret(lexical.$getSiblingCaret(anchorCellTable, initialFocus.direction));
|
|
2902
|
+
lexical.$setPointFromCaret(selection.anchor, outerFocusCaret);
|
|
2903
|
+
lexical.$setPointFromCaret(selection.focus, outerFocusCaret);
|
|
2904
|
+
}
|
|
2905
|
+
} else if (alter === 'extend') {
|
|
2906
|
+
const focusDOMCell = getDOMCellFromTarget(editor.getElementByKey(focusCaret.origin.getKey()));
|
|
2907
|
+
if (!focusDOMCell) {
|
|
2908
|
+
return false;
|
|
2909
|
+
}
|
|
2910
|
+
tableObserver.$setAnchorCellForSelection(anchorDOMCell);
|
|
2911
|
+
tableObserver.$setFocusCellForSelection(focusDOMCell, true);
|
|
2912
|
+
} else {
|
|
2913
|
+
// alter === 'move'
|
|
2914
|
+
const innerFocusCaret = lexical.$normalizeCaret(focusCaret);
|
|
2915
|
+
lexical.$setPointFromCaret(selection.anchor, innerFocusCaret);
|
|
2916
|
+
lexical.$setPointFromCaret(selection.focus, innerFocusCaret);
|
|
2917
|
+
}
|
|
2918
|
+
stopEvent(event);
|
|
2919
|
+
return true;
|
|
2920
|
+
}
|
|
2921
|
+
function $getTableExitCaret(initialCaret) {
|
|
2922
|
+
const adjacent = lexical.$getAdjacentChildCaret(initialCaret);
|
|
2923
|
+
return lexical.$isChildCaret(adjacent) ? lexical.$normalizeCaret(adjacent) : initialCaret;
|
|
2924
|
+
}
|
|
2925
|
+
function $findNextTableCell(initialCaret) {
|
|
2926
|
+
for (const nextCaret of lexical.$extendCaretToRange(initialCaret).iterNodeCarets('root')) {
|
|
2927
|
+
const {
|
|
2928
|
+
origin
|
|
2929
|
+
} = nextCaret;
|
|
2930
|
+
if ($isTableCellNode(origin)) {
|
|
2931
|
+
// not sure why ts isn't narrowing here (even if the guard is on nextCaret.origin)
|
|
2932
|
+
// but returning a new caret is fine
|
|
2933
|
+
if (lexical.$isChildCaret(nextCaret)) {
|
|
2934
|
+
return lexical.$getChildCaret(origin, initialCaret.direction);
|
|
2935
|
+
}
|
|
2936
|
+
} else if (!$isTableRowNode(origin)) {
|
|
2937
|
+
break;
|
|
2938
|
+
}
|
|
2939
|
+
}
|
|
2940
|
+
return null;
|
|
2941
|
+
}
|
|
2289
2942
|
function $handleArrowKey(editor, event, direction, tableNode, tableObserver) {
|
|
2290
2943
|
if ((direction === 'up' || direction === 'down') && isTypeaheadMenuInView(editor)) {
|
|
2291
2944
|
return false;
|
|
@@ -2293,34 +2946,67 @@ function $handleArrowKey(editor, event, direction, tableNode, tableObserver) {
|
|
|
2293
2946
|
const selection = lexical.$getSelection();
|
|
2294
2947
|
if (!$isSelectionInTable(selection, tableNode)) {
|
|
2295
2948
|
if (lexical.$isRangeSelection(selection)) {
|
|
2296
|
-
if (
|
|
2297
|
-
|
|
2298
|
-
const anchorOffset = selection.anchor.offset;
|
|
2299
|
-
if (anchorType !== 'element' && !(anchorType === 'text' && anchorOffset === 0)) {
|
|
2300
|
-
return false;
|
|
2301
|
-
}
|
|
2302
|
-
const anchorNode = selection.anchor.getNode();
|
|
2303
|
-
if (!anchorNode) {
|
|
2949
|
+
if (direction === 'backward') {
|
|
2950
|
+
if (selection.focus.offset > 0) {
|
|
2304
2951
|
return false;
|
|
2305
2952
|
}
|
|
2306
|
-
const parentNode =
|
|
2953
|
+
const parentNode = $getBlockParentIfFirstNode(selection.focus.getNode());
|
|
2307
2954
|
if (!parentNode) {
|
|
2308
2955
|
return false;
|
|
2309
2956
|
}
|
|
2310
2957
|
const siblingNode = parentNode.getPreviousSibling();
|
|
2311
|
-
if (
|
|
2958
|
+
if (!$isTableNode(siblingNode)) {
|
|
2312
2959
|
return false;
|
|
2313
2960
|
}
|
|
2314
2961
|
stopEvent(event);
|
|
2315
|
-
|
|
2962
|
+
if (event.shiftKey) {
|
|
2963
|
+
selection.focus.set(siblingNode.getParentOrThrow().getKey(), siblingNode.getIndexWithinParent(), 'element');
|
|
2964
|
+
} else {
|
|
2965
|
+
siblingNode.selectEnd();
|
|
2966
|
+
}
|
|
2316
2967
|
return true;
|
|
2317
2968
|
} else if (event.shiftKey && (direction === 'up' || direction === 'down')) {
|
|
2318
2969
|
const focusNode = selection.focus.getNode();
|
|
2319
|
-
|
|
2320
|
-
|
|
2970
|
+
const isTableUnselect = !selection.isCollapsed() && (direction === 'up' && !selection.isBackward() || direction === 'down' && selection.isBackward());
|
|
2971
|
+
if (isTableUnselect) {
|
|
2972
|
+
let focusParentNode = LexicalUtils.$findMatchingParent(focusNode, n => $isTableNode(n));
|
|
2973
|
+
if ($isTableCellNode(focusParentNode)) {
|
|
2974
|
+
focusParentNode = LexicalUtils.$findMatchingParent(focusParentNode, $isTableNode);
|
|
2975
|
+
}
|
|
2976
|
+
if (focusParentNode !== tableNode) {
|
|
2977
|
+
return false;
|
|
2978
|
+
}
|
|
2979
|
+
if (!focusParentNode) {
|
|
2980
|
+
return false;
|
|
2981
|
+
}
|
|
2982
|
+
const sibling = direction === 'down' ? focusParentNode.getNextSibling() : focusParentNode.getPreviousSibling();
|
|
2983
|
+
if (!sibling) {
|
|
2984
|
+
return false;
|
|
2985
|
+
}
|
|
2986
|
+
let newOffset = 0;
|
|
2987
|
+
if (direction === 'up') {
|
|
2988
|
+
if (lexical.$isElementNode(sibling)) {
|
|
2989
|
+
newOffset = sibling.getChildrenSize();
|
|
2990
|
+
}
|
|
2991
|
+
}
|
|
2992
|
+
let newFocusNode = sibling;
|
|
2993
|
+
if (direction === 'up') {
|
|
2994
|
+
if (lexical.$isElementNode(sibling)) {
|
|
2995
|
+
const lastCell = sibling.getLastChild();
|
|
2996
|
+
newFocusNode = lastCell ? lastCell : sibling;
|
|
2997
|
+
newOffset = lexical.$isTextNode(newFocusNode) ? newFocusNode.getTextContentSize() : 0;
|
|
2998
|
+
}
|
|
2999
|
+
}
|
|
3000
|
+
const newSelection = selection.clone();
|
|
3001
|
+
newSelection.focus.set(newFocusNode.getKey(), newOffset, lexical.$isTextNode(newFocusNode) ? 'text' : 'element');
|
|
3002
|
+
lexical.$setSelection(newSelection);
|
|
3003
|
+
stopEvent(event);
|
|
3004
|
+
return true;
|
|
3005
|
+
} else if (lexical.$isRootOrShadowRoot(focusNode)) {
|
|
3006
|
+
const selectedNode = direction === 'up' ? selection.getNodes()[selection.getNodes().length - 1] : selection.getNodes()[0];
|
|
2321
3007
|
if (selectedNode) {
|
|
2322
|
-
const tableCellNode =
|
|
2323
|
-
if (tableCellNode
|
|
3008
|
+
const tableCellNode = $findParentTableCellNodeInTable(tableNode, selectedNode);
|
|
3009
|
+
if (tableCellNode !== null) {
|
|
2324
3010
|
const firstDescendant = tableNode.getFirstDescendant();
|
|
2325
3011
|
const lastDescendant = tableNode.getLastDescendant();
|
|
2326
3012
|
if (!firstDescendant || !lastDescendant) {
|
|
@@ -2332,14 +3018,17 @@ function $handleArrowKey(editor, event, direction, tableNode, tableObserver) {
|
|
|
2332
3018
|
const lastCellCoords = tableNode.getCordsFromCellNode(lastCellNode, tableObserver.table);
|
|
2333
3019
|
const firstCellDOM = tableNode.getDOMCellFromCordsOrThrow(firstCellCoords.x, firstCellCoords.y, tableObserver.table);
|
|
2334
3020
|
const lastCellDOM = tableNode.getDOMCellFromCordsOrThrow(lastCellCoords.x, lastCellCoords.y, tableObserver.table);
|
|
2335
|
-
tableObserver
|
|
2336
|
-
tableObserver
|
|
3021
|
+
tableObserver.$setAnchorCellForSelection(firstCellDOM);
|
|
3022
|
+
tableObserver.$setFocusCellForSelection(lastCellDOM, true);
|
|
2337
3023
|
return true;
|
|
2338
3024
|
}
|
|
2339
3025
|
}
|
|
2340
3026
|
return false;
|
|
2341
3027
|
} else {
|
|
2342
|
-
|
|
3028
|
+
let focusParentNode = LexicalUtils.$findMatchingParent(focusNode, n => lexical.$isElementNode(n) && !n.isInline());
|
|
3029
|
+
if ($isTableCellNode(focusParentNode)) {
|
|
3030
|
+
focusParentNode = LexicalUtils.$findMatchingParent(focusParentNode, $isTableNode);
|
|
3031
|
+
}
|
|
2343
3032
|
if (!focusParentNode) {
|
|
2344
3033
|
return false;
|
|
2345
3034
|
}
|
|
@@ -2354,85 +3043,80 @@ function $handleArrowKey(editor, event, direction, tableNode, tableObserver) {
|
|
|
2354
3043
|
const [lastCellNode] = $getNodeTriplet(lastDescendant);
|
|
2355
3044
|
const newSelection = selection.clone();
|
|
2356
3045
|
newSelection.focus.set((direction === 'up' ? firstCellNode : lastCellNode).getKey(), direction === 'up' ? 0 : lastCellNode.getChildrenSize(), 'element');
|
|
3046
|
+
stopEvent(event);
|
|
2357
3047
|
lexical.$setSelection(newSelection);
|
|
2358
3048
|
return true;
|
|
2359
3049
|
}
|
|
2360
3050
|
}
|
|
2361
3051
|
}
|
|
2362
3052
|
}
|
|
3053
|
+
if (direction === 'down' && $isScrollableTablesActive(editor)) {
|
|
3054
|
+
// Enable Firefox workaround
|
|
3055
|
+
tableObserver.setShouldCheckSelection();
|
|
3056
|
+
}
|
|
2363
3057
|
return false;
|
|
2364
3058
|
}
|
|
2365
|
-
if (lexical.$isRangeSelection(selection)
|
|
2366
|
-
const {
|
|
2367
|
-
anchor,
|
|
2368
|
-
focus
|
|
2369
|
-
} = selection;
|
|
2370
|
-
const anchorCellNode = LexicalUtils.$findMatchingParent(anchor.getNode(), $isTableCellNode);
|
|
2371
|
-
const focusCellNode = LexicalUtils.$findMatchingParent(focus.getNode(), $isTableCellNode);
|
|
2372
|
-
if (!$isTableCellNode(anchorCellNode) || !anchorCellNode.is(focusCellNode)) {
|
|
2373
|
-
return false;
|
|
2374
|
-
}
|
|
2375
|
-
const anchorCellTable = $findTableNode(anchorCellNode);
|
|
2376
|
-
if (anchorCellTable !== tableNode && anchorCellTable != null) {
|
|
2377
|
-
const anchorCellTableElement = editor.getElementByKey(anchorCellTable.getKey());
|
|
2378
|
-
if (anchorCellTableElement != null) {
|
|
2379
|
-
tableObserver.table = getTable(anchorCellTableElement);
|
|
2380
|
-
return $handleArrowKey(editor, event, direction, anchorCellTable, tableObserver);
|
|
2381
|
-
}
|
|
2382
|
-
}
|
|
3059
|
+
if (lexical.$isRangeSelection(selection)) {
|
|
2383
3060
|
if (direction === 'backward' || direction === 'forward') {
|
|
2384
|
-
const
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
3061
|
+
const alter = event.shiftKey ? 'extend' : 'move';
|
|
3062
|
+
return $handleHorizontalArrowKeyRangeSelection(editor, event, selection, alter, direction === 'backward', tableNode, tableObserver);
|
|
3063
|
+
}
|
|
3064
|
+
if (selection.isCollapsed()) {
|
|
3065
|
+
const {
|
|
3066
|
+
anchor,
|
|
3067
|
+
focus
|
|
3068
|
+
} = selection;
|
|
3069
|
+
const anchorCellNode = LexicalUtils.$findMatchingParent(anchor.getNode(), $isTableCellNode);
|
|
3070
|
+
const focusCellNode = LexicalUtils.$findMatchingParent(focus.getNode(), $isTableCellNode);
|
|
3071
|
+
if (!$isTableCellNode(anchorCellNode) || !anchorCellNode.is(focusCellNode)) {
|
|
2388
3072
|
return false;
|
|
2389
3073
|
}
|
|
2390
|
-
const
|
|
2391
|
-
if (
|
|
3074
|
+
const anchorCellTable = $findTableNode(anchorCellNode);
|
|
3075
|
+
if (anchorCellTable !== tableNode && anchorCellTable != null) {
|
|
3076
|
+
const anchorCellTableElement = getTableElement(anchorCellTable, editor.getElementByKey(anchorCellTable.getKey()));
|
|
3077
|
+
if (anchorCellTableElement != null) {
|
|
3078
|
+
tableObserver.table = getTable(anchorCellTable, anchorCellTableElement);
|
|
3079
|
+
return $handleArrowKey(editor, event, direction, anchorCellTable, tableObserver);
|
|
3080
|
+
}
|
|
3081
|
+
}
|
|
3082
|
+
const anchorCellDom = editor.getElementByKey(anchorCellNode.__key);
|
|
3083
|
+
const anchorDOM = editor.getElementByKey(anchor.key);
|
|
3084
|
+
if (anchorDOM == null || anchorCellDom == null) {
|
|
2392
3085
|
return false;
|
|
2393
3086
|
}
|
|
2394
|
-
|
|
2395
|
-
|
|
3087
|
+
let edgeSelectionRect;
|
|
3088
|
+
if (anchor.type === 'element') {
|
|
3089
|
+
edgeSelectionRect = anchorDOM.getBoundingClientRect();
|
|
3090
|
+
} else {
|
|
3091
|
+
const domSelection = lexical.getDOMSelection(getEditorWindow(editor));
|
|
3092
|
+
if (domSelection === null || domSelection.rangeCount === 0) {
|
|
3093
|
+
return false;
|
|
3094
|
+
}
|
|
3095
|
+
const range = domSelection.getRangeAt(0);
|
|
3096
|
+
edgeSelectionRect = range.getBoundingClientRect();
|
|
2396
3097
|
}
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
const anchorCellDom = editor.getElementByKey(anchorCellNode.__key);
|
|
2400
|
-
const anchorDOM = editor.getElementByKey(anchor.key);
|
|
2401
|
-
if (anchorDOM == null || anchorCellDom == null) {
|
|
2402
|
-
return false;
|
|
2403
|
-
}
|
|
2404
|
-
let edgeSelectionRect;
|
|
2405
|
-
if (anchor.type === 'element') {
|
|
2406
|
-
edgeSelectionRect = anchorDOM.getBoundingClientRect();
|
|
2407
|
-
} else {
|
|
2408
|
-
const domSelection = window.getSelection();
|
|
2409
|
-
if (domSelection === null || domSelection.rangeCount === 0) {
|
|
3098
|
+
const edgeChild = direction === 'up' ? anchorCellNode.getFirstChild() : anchorCellNode.getLastChild();
|
|
3099
|
+
if (edgeChild == null) {
|
|
2410
3100
|
return false;
|
|
2411
3101
|
}
|
|
2412
|
-
const
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
const cell = tableNode.getDOMCellFromCordsOrThrow(cords.x, cords.y, tableObserver.table);
|
|
2430
|
-
tableObserver.setAnchorCellForSelection(cell);
|
|
2431
|
-
tableObserver.setFocusCellForSelection(cell, true);
|
|
2432
|
-
} else {
|
|
2433
|
-
return selectTableNodeInDirection(tableObserver, tableNode, cords.x, cords.y, direction);
|
|
3102
|
+
const edgeChildDOM = editor.getElementByKey(edgeChild.__key);
|
|
3103
|
+
if (edgeChildDOM == null) {
|
|
3104
|
+
return false;
|
|
3105
|
+
}
|
|
3106
|
+
const edgeRect = edgeChildDOM.getBoundingClientRect();
|
|
3107
|
+
const isExiting = direction === 'up' ? edgeRect.top > edgeSelectionRect.top - edgeSelectionRect.height : edgeSelectionRect.bottom + edgeSelectionRect.height > edgeRect.bottom;
|
|
3108
|
+
if (isExiting) {
|
|
3109
|
+
stopEvent(event);
|
|
3110
|
+
const cords = tableNode.getCordsFromCellNode(anchorCellNode, tableObserver.table);
|
|
3111
|
+
if (event.shiftKey) {
|
|
3112
|
+
const cell = tableNode.getDOMCellFromCordsOrThrow(cords.x, cords.y, tableObserver.table);
|
|
3113
|
+
tableObserver.$setAnchorCellForSelection(cell);
|
|
3114
|
+
tableObserver.$setFocusCellForSelection(cell, true);
|
|
3115
|
+
} else {
|
|
3116
|
+
return selectTableNodeInDirection(tableObserver, tableNode, cords.x, cords.y, direction);
|
|
3117
|
+
}
|
|
3118
|
+
return true;
|
|
2434
3119
|
}
|
|
2435
|
-
return true;
|
|
2436
3120
|
}
|
|
2437
3121
|
} else if ($isTableSelection(selection)) {
|
|
2438
3122
|
const {
|
|
@@ -2442,19 +3126,22 @@ function $handleArrowKey(editor, event, direction, tableNode, tableObserver) {
|
|
|
2442
3126
|
const anchorCellNode = LexicalUtils.$findMatchingParent(anchor.getNode(), $isTableCellNode);
|
|
2443
3127
|
const focusCellNode = LexicalUtils.$findMatchingParent(focus.getNode(), $isTableCellNode);
|
|
2444
3128
|
const [tableNodeFromSelection] = selection.getNodes();
|
|
2445
|
-
|
|
3129
|
+
if (!$isTableNode(tableNodeFromSelection)) {
|
|
3130
|
+
formatDevErrorMessage(`$handleArrowKey: TableSelection.getNodes()[0] expected to be TableNode`);
|
|
3131
|
+
}
|
|
3132
|
+
const tableElement = getTableElement(tableNodeFromSelection, editor.getElementByKey(tableNodeFromSelection.getKey()));
|
|
2446
3133
|
if (!$isTableCellNode(anchorCellNode) || !$isTableCellNode(focusCellNode) || !$isTableNode(tableNodeFromSelection) || tableElement == null) {
|
|
2447
3134
|
return false;
|
|
2448
3135
|
}
|
|
2449
|
-
tableObserver
|
|
2450
|
-
const grid = getTable(tableElement);
|
|
3136
|
+
tableObserver.$updateTableTableSelection(selection);
|
|
3137
|
+
const grid = getTable(tableNodeFromSelection, tableElement);
|
|
2451
3138
|
const cordsAnchor = tableNode.getCordsFromCellNode(anchorCellNode, grid);
|
|
2452
3139
|
const anchorCell = tableNode.getDOMCellFromCordsOrThrow(cordsAnchor.x, cordsAnchor.y, grid);
|
|
2453
|
-
tableObserver
|
|
3140
|
+
tableObserver.$setAnchorCellForSelection(anchorCell);
|
|
2454
3141
|
stopEvent(event);
|
|
2455
3142
|
if (event.shiftKey) {
|
|
2456
|
-
const
|
|
2457
|
-
return
|
|
3143
|
+
const [tableMap, anchorValue, focusValue] = $computeTableMap(tableNode, anchorCellNode, focusCellNode);
|
|
3144
|
+
return $adjustFocusInDirection(tableObserver, tableMap, anchorValue, focusValue, direction);
|
|
2458
3145
|
} else {
|
|
2459
3146
|
focusCellNode.selectEnd();
|
|
2460
3147
|
}
|
|
@@ -2476,58 +3163,6 @@ function isTypeaheadMenuInView(editor) {
|
|
|
2476
3163
|
}
|
|
2477
3164
|
return root.hasAttribute('aria-controls') && root.getAttribute('aria-controls') === 'typeahead-menu';
|
|
2478
3165
|
}
|
|
2479
|
-
function isExitingTableAnchor(type, offset, anchorNode, direction) {
|
|
2480
|
-
return isExitingTableElementAnchor(type, anchorNode, direction) || $isExitingTableTextAnchor(type, offset, anchorNode, direction);
|
|
2481
|
-
}
|
|
2482
|
-
function isExitingTableElementAnchor(type, anchorNode, direction) {
|
|
2483
|
-
return type === 'element' && (direction === 'backward' ? anchorNode.getPreviousSibling() === null : anchorNode.getNextSibling() === null);
|
|
2484
|
-
}
|
|
2485
|
-
function $isExitingTableTextAnchor(type, offset, anchorNode, direction) {
|
|
2486
|
-
const parentNode = LexicalUtils.$findMatchingParent(anchorNode, n => lexical.$isElementNode(n) && !n.isInline());
|
|
2487
|
-
if (!parentNode) {
|
|
2488
|
-
return false;
|
|
2489
|
-
}
|
|
2490
|
-
const hasValidOffset = direction === 'backward' ? offset === 0 : offset === anchorNode.getTextContentSize();
|
|
2491
|
-
return type === 'text' && hasValidOffset && (direction === 'backward' ? parentNode.getPreviousSibling() === null : parentNode.getNextSibling() === null);
|
|
2492
|
-
}
|
|
2493
|
-
function $handleTableExit(event, anchorNode, tableNode, direction) {
|
|
2494
|
-
const anchorCellNode = LexicalUtils.$findMatchingParent(anchorNode, $isTableCellNode);
|
|
2495
|
-
if (!$isTableCellNode(anchorCellNode)) {
|
|
2496
|
-
return false;
|
|
2497
|
-
}
|
|
2498
|
-
const [tableMap, cellValue] = $computeTableMap(tableNode, anchorCellNode, anchorCellNode);
|
|
2499
|
-
if (!isExitingCell(tableMap, cellValue, direction)) {
|
|
2500
|
-
return false;
|
|
2501
|
-
}
|
|
2502
|
-
const toNode = $getExitingToNode(anchorNode, direction, tableNode);
|
|
2503
|
-
if (!toNode || $isTableNode(toNode)) {
|
|
2504
|
-
return false;
|
|
2505
|
-
}
|
|
2506
|
-
stopEvent(event);
|
|
2507
|
-
if (direction === 'backward') {
|
|
2508
|
-
toNode.selectEnd();
|
|
2509
|
-
} else {
|
|
2510
|
-
toNode.selectStart();
|
|
2511
|
-
}
|
|
2512
|
-
return true;
|
|
2513
|
-
}
|
|
2514
|
-
function isExitingCell(tableMap, cellValue, direction) {
|
|
2515
|
-
const firstCell = tableMap[0][0];
|
|
2516
|
-
const lastCell = tableMap[tableMap.length - 1][tableMap[0].length - 1];
|
|
2517
|
-
const {
|
|
2518
|
-
startColumn,
|
|
2519
|
-
startRow
|
|
2520
|
-
} = cellValue;
|
|
2521
|
-
return direction === 'backward' ? startColumn === firstCell.startColumn && startRow === firstCell.startRow : startColumn === lastCell.startColumn && startRow === lastCell.startRow;
|
|
2522
|
-
}
|
|
2523
|
-
function $getExitingToNode(anchorNode, direction, tableNode) {
|
|
2524
|
-
const parentNode = LexicalUtils.$findMatchingParent(anchorNode, n => lexical.$isElementNode(n) && !n.isInline());
|
|
2525
|
-
if (!parentNode) {
|
|
2526
|
-
return undefined;
|
|
2527
|
-
}
|
|
2528
|
-
const anchorSibling = direction === 'backward' ? parentNode.getPreviousSibling() : parentNode.getNextSibling();
|
|
2529
|
-
return anchorSibling && $isTableNode(anchorSibling) ? anchorSibling : direction === 'backward' ? tableNode.getPreviousSibling() : tableNode.getNextSibling();
|
|
2530
|
-
}
|
|
2531
3166
|
function $insertParagraphAtTableEdge(edgePosition, tableNode, children) {
|
|
2532
3167
|
const paragraphNode = lexical.$createParagraphNode();
|
|
2533
3168
|
if (edgePosition === 'first') {
|
|
@@ -2543,14 +3178,21 @@ function $getTableEdgeCursorPosition(editor, selection, tableNode) {
|
|
|
2543
3178
|
if (!tableNodeParent) {
|
|
2544
3179
|
return undefined;
|
|
2545
3180
|
}
|
|
2546
|
-
const tableNodeParentDOM = editor.getElementByKey(tableNodeParent.getKey());
|
|
2547
|
-
if (!tableNodeParentDOM) {
|
|
2548
|
-
return undefined;
|
|
2549
|
-
}
|
|
2550
3181
|
|
|
2551
3182
|
// TODO: Add support for nested tables
|
|
2552
|
-
const domSelection =
|
|
2553
|
-
if (!domSelection
|
|
3183
|
+
const domSelection = lexical.getDOMSelection(getEditorWindow(editor));
|
|
3184
|
+
if (!domSelection) {
|
|
3185
|
+
return undefined;
|
|
3186
|
+
}
|
|
3187
|
+
const domAnchorNode = domSelection.anchorNode;
|
|
3188
|
+
const tableNodeParentDOM = editor.getElementByKey(tableNodeParent.getKey());
|
|
3189
|
+
const tableElement = getTableElement(tableNode, editor.getElementByKey(tableNode.getKey()));
|
|
3190
|
+
// We are only interested in the scenario where the
|
|
3191
|
+
// native selection anchor is:
|
|
3192
|
+
// - at or inside the table's parent DOM
|
|
3193
|
+
// - and NOT at or inside the table DOM
|
|
3194
|
+
// It may be adjacent to the table DOM (e.g. in a wrapper)
|
|
3195
|
+
if (!domAnchorNode || !tableNodeParentDOM || !tableElement || !tableNodeParentDOM.contains(domAnchorNode) || tableElement.contains(domAnchorNode)) {
|
|
2554
3196
|
return undefined;
|
|
2555
3197
|
}
|
|
2556
3198
|
const anchorCellNode = LexicalUtils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
|
|
@@ -2578,23 +3220,118 @@ function $getTableEdgeCursorPosition(editor, selection, tableNode) {
|
|
|
2578
3220
|
return undefined;
|
|
2579
3221
|
}
|
|
2580
3222
|
}
|
|
3223
|
+
function $getObserverCellFromCellNodeOrThrow(tableObserver, tableCellNode) {
|
|
3224
|
+
const {
|
|
3225
|
+
tableNode
|
|
3226
|
+
} = tableObserver.$lookup();
|
|
3227
|
+
const currentCords = tableNode.getCordsFromCellNode(tableCellNode, tableObserver.table);
|
|
3228
|
+
return tableNode.getDOMCellFromCordsOrThrow(currentCords.x, currentCords.y, tableObserver.table);
|
|
3229
|
+
}
|
|
3230
|
+
function $getNearestTableCellInTableFromDOMNode(tableNode, startingDOM, editorState) {
|
|
3231
|
+
return $findParentTableCellNodeInTable(tableNode, lexical.$getNearestNodeFromDOMNode(startingDOM, editorState));
|
|
3232
|
+
}
|
|
2581
3233
|
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
3234
|
+
function updateColgroup(dom, config, colCount, colWidths) {
|
|
3235
|
+
const colGroup = dom.querySelector('colgroup');
|
|
3236
|
+
if (!colGroup) {
|
|
3237
|
+
return;
|
|
3238
|
+
}
|
|
3239
|
+
const cols = [];
|
|
3240
|
+
for (let i = 0; i < colCount; i++) {
|
|
3241
|
+
const col = document.createElement('col');
|
|
3242
|
+
const width = colWidths && colWidths[i];
|
|
3243
|
+
if (width) {
|
|
3244
|
+
col.style.width = `${width}px`;
|
|
3245
|
+
}
|
|
3246
|
+
cols.push(col);
|
|
3247
|
+
}
|
|
3248
|
+
colGroup.replaceChildren(...cols);
|
|
3249
|
+
}
|
|
3250
|
+
function setRowStriping(dom, config, rowStriping) {
|
|
3251
|
+
if (rowStriping) {
|
|
3252
|
+
LexicalUtils.addClassNamesToElement(dom, config.theme.tableRowStriping);
|
|
3253
|
+
dom.setAttribute('data-lexical-row-striping', 'true');
|
|
3254
|
+
} else {
|
|
3255
|
+
LexicalUtils.removeClassNamesFromElement(dom, config.theme.tableRowStriping);
|
|
3256
|
+
dom.removeAttribute('data-lexical-row-striping');
|
|
3257
|
+
}
|
|
3258
|
+
}
|
|
3259
|
+
function setFrozenColumns(dom, config, frozenColumnCount) {
|
|
3260
|
+
if (frozenColumnCount > 0) {
|
|
3261
|
+
LexicalUtils.addClassNamesToElement(dom, config.theme.tableFrozenColumn);
|
|
3262
|
+
dom.setAttribute('data-lexical-frozen-column', 'true');
|
|
3263
|
+
} else {
|
|
3264
|
+
LexicalUtils.removeClassNamesFromElement(dom, config.theme.tableFrozenColumn);
|
|
3265
|
+
dom.removeAttribute('data-lexical-frozen-column');
|
|
3266
|
+
}
|
|
3267
|
+
}
|
|
3268
|
+
function setFrozenRows(dom, config, frozenRowCount) {
|
|
3269
|
+
if (frozenRowCount > 0) {
|
|
3270
|
+
LexicalUtils.addClassNamesToElement(dom, config.theme.tableFrozenRow);
|
|
3271
|
+
dom.setAttribute('data-lexical-frozen-row', 'true');
|
|
3272
|
+
} else {
|
|
3273
|
+
LexicalUtils.removeClassNamesFromElement(dom, config.theme.tableFrozenRow);
|
|
3274
|
+
dom.removeAttribute('data-lexical-frozen-row');
|
|
3275
|
+
}
|
|
3276
|
+
}
|
|
3277
|
+
function alignTableElement(dom, config, formatType) {
|
|
3278
|
+
if (!config.theme.tableAlignment) {
|
|
3279
|
+
return;
|
|
3280
|
+
}
|
|
3281
|
+
const removeClasses = [];
|
|
3282
|
+
const addClasses = [];
|
|
3283
|
+
for (const format of ['center', 'right']) {
|
|
3284
|
+
const classes = config.theme.tableAlignment[format];
|
|
3285
|
+
if (!classes) {
|
|
3286
|
+
continue;
|
|
3287
|
+
}
|
|
3288
|
+
(format === formatType ? addClasses : removeClasses).push(classes);
|
|
3289
|
+
}
|
|
3290
|
+
LexicalUtils.removeClassNamesFromElement(dom, ...removeClasses);
|
|
3291
|
+
LexicalUtils.addClassNamesToElement(dom, ...addClasses);
|
|
3292
|
+
}
|
|
3293
|
+
const scrollableEditors = new WeakSet();
|
|
3294
|
+
function $isScrollableTablesActive(editor = lexical.$getEditor()) {
|
|
3295
|
+
return scrollableEditors.has(editor);
|
|
3296
|
+
}
|
|
3297
|
+
function setScrollableTablesActive(editor, active) {
|
|
3298
|
+
if (active) {
|
|
3299
|
+
if (!editor._config.theme.tableScrollableWrapper) {
|
|
3300
|
+
console.warn('TableNode: hasHorizontalScroll is active but theme.tableScrollableWrapper is not defined.');
|
|
3301
|
+
}
|
|
3302
|
+
scrollableEditors.add(editor);
|
|
3303
|
+
} else {
|
|
3304
|
+
scrollableEditors.delete(editor);
|
|
3305
|
+
}
|
|
3306
|
+
}
|
|
2589
3307
|
|
|
2590
3308
|
/** @noInheritDoc */
|
|
2591
3309
|
class TableNode extends lexical.ElementNode {
|
|
3310
|
+
/** @internal */
|
|
3311
|
+
|
|
2592
3312
|
static getType() {
|
|
2593
3313
|
return 'table';
|
|
2594
3314
|
}
|
|
3315
|
+
getColWidths() {
|
|
3316
|
+
const self = this.getLatest();
|
|
3317
|
+
return self.__colWidths;
|
|
3318
|
+
}
|
|
3319
|
+
setColWidths(colWidths) {
|
|
3320
|
+
const self = this.getWritable();
|
|
3321
|
+
// NOTE: Node properties should be immutable. Freeze to prevent accidental mutation.
|
|
3322
|
+
self.__colWidths = colWidths !== undefined && true ? Object.freeze(colWidths) : colWidths;
|
|
3323
|
+
return self;
|
|
3324
|
+
}
|
|
2595
3325
|
static clone(node) {
|
|
2596
3326
|
return new TableNode(node.__key);
|
|
2597
3327
|
}
|
|
3328
|
+
afterCloneFrom(prevNode) {
|
|
3329
|
+
super.afterCloneFrom(prevNode);
|
|
3330
|
+
this.__colWidths = prevNode.__colWidths;
|
|
3331
|
+
this.__rowStriping = prevNode.__rowStriping;
|
|
3332
|
+
this.__frozenColumnCount = prevNode.__frozenColumnCount;
|
|
3333
|
+
this.__frozenRowCount = prevNode.__frozenRowCount;
|
|
3334
|
+
}
|
|
2598
3335
|
static importDOM() {
|
|
2599
3336
|
return {
|
|
2600
3337
|
table: _node => ({
|
|
@@ -2603,51 +3340,159 @@ class TableNode extends lexical.ElementNode {
|
|
|
2603
3340
|
})
|
|
2604
3341
|
};
|
|
2605
3342
|
}
|
|
2606
|
-
static importJSON(
|
|
2607
|
-
return $createTableNode();
|
|
3343
|
+
static importJSON(serializedNode) {
|
|
3344
|
+
return $createTableNode().updateFromJSON(serializedNode);
|
|
3345
|
+
}
|
|
3346
|
+
updateFromJSON(serializedNode) {
|
|
3347
|
+
return super.updateFromJSON(serializedNode).setRowStriping(serializedNode.rowStriping || false).setFrozenColumns(serializedNode.frozenColumnCount || 0).setFrozenRows(serializedNode.frozenRowCount || 0).setColWidths(serializedNode.colWidths);
|
|
2608
3348
|
}
|
|
2609
3349
|
constructor(key) {
|
|
2610
3350
|
super(key);
|
|
3351
|
+
this.__rowStriping = false;
|
|
3352
|
+
this.__frozenColumnCount = 0;
|
|
3353
|
+
this.__frozenRowCount = 0;
|
|
2611
3354
|
}
|
|
2612
3355
|
exportJSON() {
|
|
2613
3356
|
return {
|
|
2614
3357
|
...super.exportJSON(),
|
|
2615
|
-
|
|
2616
|
-
|
|
3358
|
+
colWidths: this.getColWidths(),
|
|
3359
|
+
frozenColumnCount: this.__frozenColumnCount ? this.__frozenColumnCount : undefined,
|
|
3360
|
+
frozenRowCount: this.__frozenRowCount ? this.__frozenRowCount : undefined,
|
|
3361
|
+
rowStriping: this.__rowStriping ? this.__rowStriping : undefined
|
|
2617
3362
|
};
|
|
2618
3363
|
}
|
|
3364
|
+
extractWithChild(child, selection, destination) {
|
|
3365
|
+
return destination === 'html';
|
|
3366
|
+
}
|
|
3367
|
+
getDOMSlot(element) {
|
|
3368
|
+
const tableElement = element.nodeName !== 'TABLE' && element.querySelector('table') || element;
|
|
3369
|
+
if (!(tableElement.nodeName === 'TABLE')) {
|
|
3370
|
+
formatDevErrorMessage(`TableNode.getDOMSlot: createDOM() did not return a table`);
|
|
3371
|
+
}
|
|
3372
|
+
return super.getDOMSlot(tableElement).withAfter(tableElement.querySelector('colgroup'));
|
|
3373
|
+
}
|
|
2619
3374
|
createDOM(config, editor) {
|
|
2620
3375
|
const tableElement = document.createElement('table');
|
|
3376
|
+
if (this.__style) {
|
|
3377
|
+
tableElement.style.cssText = this.__style;
|
|
3378
|
+
}
|
|
3379
|
+
const colGroup = document.createElement('colgroup');
|
|
3380
|
+
tableElement.appendChild(colGroup);
|
|
3381
|
+
updateColgroup(tableElement, config, this.getColumnCount(), this.getColWidths());
|
|
3382
|
+
lexical.setDOMUnmanaged(colGroup);
|
|
2621
3383
|
LexicalUtils.addClassNamesToElement(tableElement, config.theme.table);
|
|
3384
|
+
alignTableElement(tableElement, config, this.getFormatType());
|
|
3385
|
+
if (this.__frozenColumnCount) {
|
|
3386
|
+
setFrozenColumns(tableElement, config, this.__frozenColumnCount);
|
|
3387
|
+
}
|
|
3388
|
+
if (this.__frozenRowCount) {
|
|
3389
|
+
setFrozenRows(tableElement, config, this.__frozenRowCount);
|
|
3390
|
+
}
|
|
3391
|
+
if (this.__rowStriping) {
|
|
3392
|
+
setRowStriping(tableElement, config, true);
|
|
3393
|
+
}
|
|
3394
|
+
if ($isScrollableTablesActive(editor)) {
|
|
3395
|
+
const wrapperElement = document.createElement('div');
|
|
3396
|
+
const classes = config.theme.tableScrollableWrapper;
|
|
3397
|
+
if (classes) {
|
|
3398
|
+
LexicalUtils.addClassNamesToElement(wrapperElement, classes);
|
|
3399
|
+
} else {
|
|
3400
|
+
wrapperElement.style.cssText = 'overflow-x: auto;';
|
|
3401
|
+
}
|
|
3402
|
+
wrapperElement.appendChild(tableElement);
|
|
3403
|
+
return wrapperElement;
|
|
3404
|
+
}
|
|
2622
3405
|
return tableElement;
|
|
2623
3406
|
}
|
|
2624
|
-
updateDOM() {
|
|
3407
|
+
updateDOM(prevNode, dom, config) {
|
|
3408
|
+
if (prevNode.__rowStriping !== this.__rowStriping) {
|
|
3409
|
+
setRowStriping(dom, config, this.__rowStriping);
|
|
3410
|
+
}
|
|
3411
|
+
if (prevNode.__frozenColumnCount !== this.__frozenColumnCount) {
|
|
3412
|
+
setFrozenColumns(dom, config, this.__frozenColumnCount);
|
|
3413
|
+
}
|
|
3414
|
+
if (prevNode.__frozenRowCount !== this.__frozenRowCount) {
|
|
3415
|
+
setFrozenRows(dom, config, this.__frozenRowCount);
|
|
3416
|
+
}
|
|
3417
|
+
updateColgroup(dom, config, this.getColumnCount(), this.getColWidths());
|
|
3418
|
+
const tableElement = this.getDOMSlot(dom).element;
|
|
3419
|
+
if (prevNode.__style !== this.__style) {
|
|
3420
|
+
tableElement.style.cssText = this.__style;
|
|
3421
|
+
}
|
|
3422
|
+
alignTableElement(tableElement, config, this.getFormatType());
|
|
2625
3423
|
return false;
|
|
2626
3424
|
}
|
|
2627
3425
|
exportDOM(editor) {
|
|
3426
|
+
const superExport = super.exportDOM(editor);
|
|
3427
|
+
const {
|
|
3428
|
+
element
|
|
3429
|
+
} = superExport;
|
|
2628
3430
|
return {
|
|
2629
|
-
...super.exportDOM(editor),
|
|
2630
3431
|
after: tableElement => {
|
|
2631
|
-
if (
|
|
2632
|
-
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
if (LexicalUtils.isHTMLElement(tableElement)) {
|
|
2636
|
-
tBody.append(...tableElement.children);
|
|
3432
|
+
if (superExport.after) {
|
|
3433
|
+
tableElement = superExport.after(tableElement);
|
|
3434
|
+
if (this.__format) {
|
|
3435
|
+
alignTableElement(tableElement, editor._config, this.getFormatType());
|
|
2637
3436
|
}
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
|
|
3437
|
+
}
|
|
3438
|
+
if (LexicalUtils.isHTMLElement(tableElement) && tableElement.nodeName !== 'TABLE') {
|
|
3439
|
+
tableElement = tableElement.querySelector('table');
|
|
3440
|
+
}
|
|
3441
|
+
if (!LexicalUtils.isHTMLElement(tableElement)) {
|
|
3442
|
+
return null;
|
|
3443
|
+
}
|
|
3444
|
+
|
|
3445
|
+
// Scan the table map to build a map of table cell key to the columns it needs
|
|
3446
|
+
const [tableMap] = $computeTableMapSkipCellCheck(this, null, null);
|
|
3447
|
+
const cellValues = new Map();
|
|
3448
|
+
for (const mapRow of tableMap) {
|
|
3449
|
+
for (const mapValue of mapRow) {
|
|
3450
|
+
const key = mapValue.cell.getKey();
|
|
3451
|
+
if (!cellValues.has(key)) {
|
|
3452
|
+
cellValues.set(key, {
|
|
3453
|
+
colSpan: mapValue.cell.getColSpan(),
|
|
3454
|
+
startColumn: mapValue.startColumn
|
|
3455
|
+
});
|
|
3456
|
+
}
|
|
2641
3457
|
}
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
3458
|
+
}
|
|
3459
|
+
|
|
3460
|
+
// scan the DOM to find the table cell keys that were used and mark those columns
|
|
3461
|
+
const knownColumns = new Set();
|
|
3462
|
+
for (const cellDOM of tableElement.querySelectorAll(':scope > tr > [data-temporary-table-cell-lexical-key]')) {
|
|
3463
|
+
const key = cellDOM.getAttribute('data-temporary-table-cell-lexical-key');
|
|
3464
|
+
if (key) {
|
|
3465
|
+
const cellSpan = cellValues.get(key);
|
|
3466
|
+
cellDOM.removeAttribute('data-temporary-table-cell-lexical-key');
|
|
3467
|
+
if (cellSpan) {
|
|
3468
|
+
cellValues.delete(key);
|
|
3469
|
+
for (let i = 0; i < cellSpan.colSpan; i++) {
|
|
3470
|
+
knownColumns.add(i + cellSpan.startColumn);
|
|
3471
|
+
}
|
|
3472
|
+
}
|
|
2646
3473
|
}
|
|
2647
|
-
newElement.replaceChildren(colGroup, tBody);
|
|
2648
|
-
return newElement;
|
|
2649
3474
|
}
|
|
2650
|
-
|
|
3475
|
+
|
|
3476
|
+
// Compute the colgroup and columns in the export
|
|
3477
|
+
const colGroup = tableElement.querySelector(':scope > colgroup');
|
|
3478
|
+
if (colGroup) {
|
|
3479
|
+
// Only include the <col /> for rows that are in the output
|
|
3480
|
+
const cols = Array.from(tableElement.querySelectorAll(':scope > colgroup > col')).filter((dom, i) => knownColumns.has(i));
|
|
3481
|
+
colGroup.replaceChildren(...cols);
|
|
3482
|
+
}
|
|
3483
|
+
|
|
3484
|
+
// Wrap direct descendant rows in a tbody for export
|
|
3485
|
+
const rows = tableElement.querySelectorAll(':scope > tr');
|
|
3486
|
+
if (rows.length > 0) {
|
|
3487
|
+
const tBody = document.createElement('tbody');
|
|
3488
|
+
for (const row of rows) {
|
|
3489
|
+
tBody.appendChild(row);
|
|
3490
|
+
}
|
|
3491
|
+
tableElement.append(tBody);
|
|
3492
|
+
}
|
|
3493
|
+
return tableElement;
|
|
3494
|
+
},
|
|
3495
|
+
element: LexicalUtils.isHTMLElement(element) && element.nodeName !== 'TABLE' ? element.querySelector('table') : element
|
|
2651
3496
|
};
|
|
2652
3497
|
}
|
|
2653
3498
|
canBeEmpty() {
|
|
@@ -2666,21 +3511,21 @@ class TableNode extends lexical.ElementNode {
|
|
|
2666
3511
|
if (row == null) {
|
|
2667
3512
|
continue;
|
|
2668
3513
|
}
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
3514
|
+
for (let x = 0; x < row.length; x++) {
|
|
3515
|
+
const cell = row[x];
|
|
3516
|
+
if (cell == null) {
|
|
3517
|
+
continue;
|
|
2672
3518
|
}
|
|
2673
3519
|
const {
|
|
2674
3520
|
elem
|
|
2675
3521
|
} = cell;
|
|
2676
|
-
const cellNode =
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
};
|
|
3522
|
+
const cellNode = $getNearestTableCellInTableFromDOMNode(this, elem);
|
|
3523
|
+
if (cellNode !== null && tableCellNode.is(cellNode)) {
|
|
3524
|
+
return {
|
|
3525
|
+
x,
|
|
3526
|
+
y
|
|
3527
|
+
};
|
|
3528
|
+
}
|
|
2684
3529
|
}
|
|
2685
3530
|
}
|
|
2686
3531
|
throw new Error('Cell not found in table.');
|
|
@@ -2725,23 +3570,84 @@ class TableNode extends lexical.ElementNode {
|
|
|
2725
3570
|
}
|
|
2726
3571
|
return node;
|
|
2727
3572
|
}
|
|
3573
|
+
getRowStriping() {
|
|
3574
|
+
return Boolean(this.getLatest().__rowStriping);
|
|
3575
|
+
}
|
|
3576
|
+
setRowStriping(newRowStriping) {
|
|
3577
|
+
const self = this.getWritable();
|
|
3578
|
+
self.__rowStriping = newRowStriping;
|
|
3579
|
+
return self;
|
|
3580
|
+
}
|
|
3581
|
+
setFrozenColumns(columnCount) {
|
|
3582
|
+
const self = this.getWritable();
|
|
3583
|
+
self.__frozenColumnCount = columnCount;
|
|
3584
|
+
return self;
|
|
3585
|
+
}
|
|
3586
|
+
getFrozenColumns() {
|
|
3587
|
+
return this.getLatest().__frozenColumnCount;
|
|
3588
|
+
}
|
|
3589
|
+
setFrozenRows(rowCount) {
|
|
3590
|
+
const self = this.getWritable();
|
|
3591
|
+
self.__frozenRowCount = rowCount;
|
|
3592
|
+
return self;
|
|
3593
|
+
}
|
|
3594
|
+
getFrozenRows() {
|
|
3595
|
+
return this.getLatest().__frozenRowCount;
|
|
3596
|
+
}
|
|
2728
3597
|
canSelectBefore() {
|
|
2729
3598
|
return true;
|
|
2730
3599
|
}
|
|
2731
3600
|
canIndent() {
|
|
2732
3601
|
return false;
|
|
2733
3602
|
}
|
|
3603
|
+
getColumnCount() {
|
|
3604
|
+
const firstRow = this.getFirstChild();
|
|
3605
|
+
if (!firstRow) {
|
|
3606
|
+
return 0;
|
|
3607
|
+
}
|
|
3608
|
+
let columnCount = 0;
|
|
3609
|
+
firstRow.getChildren().forEach(cell => {
|
|
3610
|
+
if ($isTableCellNode(cell)) {
|
|
3611
|
+
columnCount += cell.getColSpan();
|
|
3612
|
+
}
|
|
3613
|
+
});
|
|
3614
|
+
return columnCount;
|
|
3615
|
+
}
|
|
2734
3616
|
}
|
|
2735
3617
|
function $getElementForTableNode(editor, tableNode) {
|
|
2736
3618
|
const tableElement = editor.getElementByKey(tableNode.getKey());
|
|
2737
|
-
if (tableElement
|
|
2738
|
-
|
|
3619
|
+
if (!(tableElement !== null)) {
|
|
3620
|
+
formatDevErrorMessage(`$getElementForTableNode: Table Element Not Found`);
|
|
2739
3621
|
}
|
|
2740
|
-
return getTable(tableElement);
|
|
3622
|
+
return getTable(tableNode, tableElement);
|
|
2741
3623
|
}
|
|
2742
|
-
function $convertTableElement(
|
|
3624
|
+
function $convertTableElement(domNode) {
|
|
3625
|
+
const tableNode = $createTableNode();
|
|
3626
|
+
if (domNode.hasAttribute('data-lexical-row-striping')) {
|
|
3627
|
+
tableNode.setRowStriping(true);
|
|
3628
|
+
}
|
|
3629
|
+
const colGroup = domNode.querySelector(':scope > colgroup');
|
|
3630
|
+
if (colGroup) {
|
|
3631
|
+
let columns = [];
|
|
3632
|
+
for (const col of colGroup.querySelectorAll(':scope > col')) {
|
|
3633
|
+
let width = col.style.width || '';
|
|
3634
|
+
if (!PIXEL_VALUE_REG_EXP.test(width)) {
|
|
3635
|
+
// Also support deprecated width attribute for google docs
|
|
3636
|
+
width = col.getAttribute('width') || '';
|
|
3637
|
+
if (!/^\d+$/.test(width)) {
|
|
3638
|
+
columns = undefined;
|
|
3639
|
+
break;
|
|
3640
|
+
}
|
|
3641
|
+
}
|
|
3642
|
+
columns.push(parseFloat(width));
|
|
3643
|
+
}
|
|
3644
|
+
if (columns) {
|
|
3645
|
+
tableNode.setColWidths(columns);
|
|
3646
|
+
}
|
|
3647
|
+
}
|
|
2743
3648
|
return {
|
|
2744
|
-
|
|
3649
|
+
after: children => LexicalUtils.$descendantsMatching(children, $isTableRowNode),
|
|
3650
|
+
node: tableNode
|
|
2745
3651
|
};
|
|
2746
3652
|
}
|
|
2747
3653
|
function $createTableNode() {
|
|
@@ -2751,6 +3657,224 @@ function $isTableNode(node) {
|
|
|
2751
3657
|
return node instanceof TableNode;
|
|
2752
3658
|
}
|
|
2753
3659
|
|
|
3660
|
+
function $insertTableCommandListener({
|
|
3661
|
+
rows,
|
|
3662
|
+
columns,
|
|
3663
|
+
includeHeaders
|
|
3664
|
+
}) {
|
|
3665
|
+
const selection = lexical.$getSelection();
|
|
3666
|
+
if (!selection || !lexical.$isRangeSelection(selection)) {
|
|
3667
|
+
return false;
|
|
3668
|
+
}
|
|
3669
|
+
|
|
3670
|
+
// Prevent nested tables by checking if we're already inside a table
|
|
3671
|
+
if ($findTableNode(selection.anchor.getNode())) {
|
|
3672
|
+
return false;
|
|
3673
|
+
}
|
|
3674
|
+
const tableNode = $createTableNodeWithDimensions(Number(rows), Number(columns), includeHeaders);
|
|
3675
|
+
LexicalUtils.$insertNodeToNearestRoot(tableNode);
|
|
3676
|
+
const firstDescendant = tableNode.getFirstDescendant();
|
|
3677
|
+
if (lexical.$isTextNode(firstDescendant)) {
|
|
3678
|
+
firstDescendant.select();
|
|
3679
|
+
}
|
|
3680
|
+
return true;
|
|
3681
|
+
}
|
|
3682
|
+
function $tableCellTransform(node) {
|
|
3683
|
+
if (!$isTableRowNode(node.getParent())) {
|
|
3684
|
+
// TableCellNode must be a child of TableRowNode.
|
|
3685
|
+
node.remove();
|
|
3686
|
+
} else if (node.isEmpty()) {
|
|
3687
|
+
// TableCellNode should never be empty
|
|
3688
|
+
node.append(lexical.$createParagraphNode());
|
|
3689
|
+
}
|
|
3690
|
+
}
|
|
3691
|
+
function $tableRowTransform(node) {
|
|
3692
|
+
if (!$isTableNode(node.getParent())) {
|
|
3693
|
+
// TableRowNode must be a child of TableNode.
|
|
3694
|
+
// TODO: Future support of tbody/thead/tfoot may change this
|
|
3695
|
+
node.remove();
|
|
3696
|
+
} else {
|
|
3697
|
+
LexicalUtils.$unwrapAndFilterDescendants(node, $isTableCellNode);
|
|
3698
|
+
}
|
|
3699
|
+
}
|
|
3700
|
+
function $tableTransform(node) {
|
|
3701
|
+
// TableRowNode is the only valid child for TableNode
|
|
3702
|
+
// TODO: Future support of tbody/thead/tfoot/caption may change this
|
|
3703
|
+
LexicalUtils.$unwrapAndFilterDescendants(node, $isTableRowNode);
|
|
3704
|
+
const [gridMap] = $computeTableMapSkipCellCheck(node, null, null);
|
|
3705
|
+
const maxRowLength = gridMap.reduce((curLength, row) => {
|
|
3706
|
+
return Math.max(curLength, row.length);
|
|
3707
|
+
}, 0);
|
|
3708
|
+
const rowNodes = node.getChildren();
|
|
3709
|
+
for (let i = 0; i < gridMap.length; ++i) {
|
|
3710
|
+
const rowNode = rowNodes[i];
|
|
3711
|
+
if (!rowNode) {
|
|
3712
|
+
continue;
|
|
3713
|
+
}
|
|
3714
|
+
if (!$isTableRowNode(rowNode)) {
|
|
3715
|
+
formatDevErrorMessage(`TablePlugin: Expecting all children of TableNode to be TableRowNode, found ${rowNode.constructor.name} (type ${rowNode.getType()})`);
|
|
3716
|
+
}
|
|
3717
|
+
const rowLength = gridMap[i].reduce((acc, cell) => cell ? 1 + acc : acc, 0);
|
|
3718
|
+
if (rowLength === maxRowLength) {
|
|
3719
|
+
continue;
|
|
3720
|
+
}
|
|
3721
|
+
for (let j = rowLength; j < maxRowLength; ++j) {
|
|
3722
|
+
// TODO: inherit header state from another header or body
|
|
3723
|
+
const newCell = $createTableCellNode();
|
|
3724
|
+
newCell.append(lexical.$createParagraphNode());
|
|
3725
|
+
rowNode.append(newCell);
|
|
3726
|
+
}
|
|
3727
|
+
}
|
|
3728
|
+
}
|
|
3729
|
+
function $tableClickCommand(event) {
|
|
3730
|
+
if (event.detail < 3 || !lexical.isDOMNode(event.target)) {
|
|
3731
|
+
return false;
|
|
3732
|
+
}
|
|
3733
|
+
const startNode = lexical.$getNearestNodeFromDOMNode(event.target);
|
|
3734
|
+
if (startNode === null) {
|
|
3735
|
+
return false;
|
|
3736
|
+
}
|
|
3737
|
+
const blockNode = LexicalUtils.$findMatchingParent(startNode, node => lexical.$isElementNode(node) && !node.isInline());
|
|
3738
|
+
if (blockNode === null) {
|
|
3739
|
+
return false;
|
|
3740
|
+
}
|
|
3741
|
+
const rootNode = blockNode.getParent();
|
|
3742
|
+
if (!$isTableCellNode(rootNode)) {
|
|
3743
|
+
return false;
|
|
3744
|
+
}
|
|
3745
|
+
blockNode.select(0);
|
|
3746
|
+
return true;
|
|
3747
|
+
}
|
|
3748
|
+
|
|
3749
|
+
/**
|
|
3750
|
+
* Register a transform to ensure that all TableCellNode have a colSpan and rowSpan of 1.
|
|
3751
|
+
* This should only be registered when you do not want to support merged cells.
|
|
3752
|
+
*
|
|
3753
|
+
* @param editor The editor
|
|
3754
|
+
* @returns An unregister callback
|
|
3755
|
+
*/
|
|
3756
|
+
function registerTableCellUnmergeTransform(editor) {
|
|
3757
|
+
return editor.registerNodeTransform(TableCellNode, node => {
|
|
3758
|
+
if (node.getColSpan() > 1 || node.getRowSpan() > 1) {
|
|
3759
|
+
// When we have rowSpan we have to map the entire Table to understand where the new Cells
|
|
3760
|
+
// fit best; let's analyze all Cells at once to save us from further transform iterations
|
|
3761
|
+
const [,, gridNode] = $getNodeTriplet(node);
|
|
3762
|
+
const [gridMap] = $computeTableMap(gridNode, node, node);
|
|
3763
|
+
// TODO this function expects Tables to be normalized. Look into this once it exists
|
|
3764
|
+
const rowsCount = gridMap.length;
|
|
3765
|
+
const columnsCount = gridMap[0].length;
|
|
3766
|
+
let row = gridNode.getFirstChild();
|
|
3767
|
+
if (!$isTableRowNode(row)) {
|
|
3768
|
+
formatDevErrorMessage(`Expected TableNode first child to be a RowNode`);
|
|
3769
|
+
}
|
|
3770
|
+
const unmerged = [];
|
|
3771
|
+
for (let i = 0; i < rowsCount; i++) {
|
|
3772
|
+
if (i !== 0) {
|
|
3773
|
+
row = row.getNextSibling();
|
|
3774
|
+
if (!$isTableRowNode(row)) {
|
|
3775
|
+
formatDevErrorMessage(`Expected TableNode first child to be a RowNode`);
|
|
3776
|
+
}
|
|
3777
|
+
}
|
|
3778
|
+
let lastRowCell = null;
|
|
3779
|
+
for (let j = 0; j < columnsCount; j++) {
|
|
3780
|
+
const cellMap = gridMap[i][j];
|
|
3781
|
+
const cell = cellMap.cell;
|
|
3782
|
+
if (cellMap.startRow === i && cellMap.startColumn === j) {
|
|
3783
|
+
lastRowCell = cell;
|
|
3784
|
+
unmerged.push(cell);
|
|
3785
|
+
} else if (cell.getColSpan() > 1 || cell.getRowSpan() > 1) {
|
|
3786
|
+
if (!$isTableCellNode(cell)) {
|
|
3787
|
+
formatDevErrorMessage(`Expected TableNode cell to be a TableCellNode`);
|
|
3788
|
+
}
|
|
3789
|
+
const newCell = $createTableCellNode(cell.__headerState);
|
|
3790
|
+
if (lastRowCell !== null) {
|
|
3791
|
+
lastRowCell.insertAfter(newCell);
|
|
3792
|
+
} else {
|
|
3793
|
+
LexicalUtils.$insertFirst(row, newCell);
|
|
3794
|
+
}
|
|
3795
|
+
}
|
|
3796
|
+
}
|
|
3797
|
+
}
|
|
3798
|
+
for (const cell of unmerged) {
|
|
3799
|
+
cell.setColSpan(1);
|
|
3800
|
+
cell.setRowSpan(1);
|
|
3801
|
+
}
|
|
3802
|
+
}
|
|
3803
|
+
});
|
|
3804
|
+
}
|
|
3805
|
+
function registerTableSelectionObserver(editor, hasTabHandler = true) {
|
|
3806
|
+
const tableSelections = new Map();
|
|
3807
|
+
const initializeTableNode = (tableNode, nodeKey, dom) => {
|
|
3808
|
+
const tableElement = getTableElement(tableNode, dom);
|
|
3809
|
+
const tableSelection = applyTableHandlers(tableNode, tableElement, editor, hasTabHandler);
|
|
3810
|
+
tableSelections.set(nodeKey, [tableSelection, tableElement]);
|
|
3811
|
+
};
|
|
3812
|
+
const unregisterMutationListener = editor.registerMutationListener(TableNode, nodeMutations => {
|
|
3813
|
+
editor.getEditorState().read(() => {
|
|
3814
|
+
for (const [nodeKey, mutation] of nodeMutations) {
|
|
3815
|
+
const tableSelection = tableSelections.get(nodeKey);
|
|
3816
|
+
if (mutation === 'created' || mutation === 'updated') {
|
|
3817
|
+
const {
|
|
3818
|
+
tableNode,
|
|
3819
|
+
tableElement
|
|
3820
|
+
} = $getTableAndElementByKey(nodeKey);
|
|
3821
|
+
if (tableSelection === undefined) {
|
|
3822
|
+
initializeTableNode(tableNode, nodeKey, tableElement);
|
|
3823
|
+
} else if (tableElement !== tableSelection[1]) {
|
|
3824
|
+
// The update created a new DOM node, destroy the existing TableObserver
|
|
3825
|
+
tableSelection[0].removeListeners();
|
|
3826
|
+
tableSelections.delete(nodeKey);
|
|
3827
|
+
initializeTableNode(tableNode, nodeKey, tableElement);
|
|
3828
|
+
}
|
|
3829
|
+
} else if (mutation === 'destroyed') {
|
|
3830
|
+
if (tableSelection !== undefined) {
|
|
3831
|
+
tableSelection[0].removeListeners();
|
|
3832
|
+
tableSelections.delete(nodeKey);
|
|
3833
|
+
}
|
|
3834
|
+
}
|
|
3835
|
+
}
|
|
3836
|
+
}, {
|
|
3837
|
+
editor
|
|
3838
|
+
});
|
|
3839
|
+
}, {
|
|
3840
|
+
skipInitialization: false
|
|
3841
|
+
});
|
|
3842
|
+
return () => {
|
|
3843
|
+
unregisterMutationListener();
|
|
3844
|
+
// Hook might be called multiple times so cleaning up tables listeners as well,
|
|
3845
|
+
// as it'll be reinitialized during recurring call
|
|
3846
|
+
for (const [, [tableSelection]] of tableSelections) {
|
|
3847
|
+
tableSelection.removeListeners();
|
|
3848
|
+
}
|
|
3849
|
+
};
|
|
3850
|
+
}
|
|
3851
|
+
|
|
3852
|
+
/**
|
|
3853
|
+
* Register the INSERT_TABLE_COMMAND listener and the table integrity transforms. The
|
|
3854
|
+
* table selection observer should be registered separately after this with
|
|
3855
|
+
* {@link registerTableSelectionObserver}.
|
|
3856
|
+
*
|
|
3857
|
+
* @param editor The editor
|
|
3858
|
+
* @returns An unregister callback
|
|
3859
|
+
*/
|
|
3860
|
+
function registerTablePlugin(editor) {
|
|
3861
|
+
if (!editor.hasNodes([TableNode])) {
|
|
3862
|
+
{
|
|
3863
|
+
formatDevErrorMessage(`TablePlugin: TableNode is not registered on editor`);
|
|
3864
|
+
}
|
|
3865
|
+
}
|
|
3866
|
+
return LexicalUtils.mergeRegister(editor.registerCommand(INSERT_TABLE_COMMAND, $insertTableCommandListener, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.SELECTION_INSERT_CLIPBOARD_NODES_COMMAND, ({
|
|
3867
|
+
nodes,
|
|
3868
|
+
selection
|
|
3869
|
+
}) => {
|
|
3870
|
+
if (!lexical.$isRangeSelection(selection)) {
|
|
3871
|
+
return false;
|
|
3872
|
+
}
|
|
3873
|
+
const isInsideTableCell = $findTableNode(selection.anchor.getNode()) !== null;
|
|
3874
|
+
return isInsideTableCell && nodes.some($isTableNode);
|
|
3875
|
+
}, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.CLICK_COMMAND, $tableClickCommand, lexical.COMMAND_PRIORITY_EDITOR), editor.registerNodeTransform(TableNode, $tableTransform), editor.registerNodeTransform(TableRowNode, $tableRowTransform), editor.registerNodeTransform(TableCellNode, $tableCellTransform));
|
|
3876
|
+
}
|
|
3877
|
+
|
|
2754
3878
|
exports.$computeTableMap = $computeTableMap;
|
|
2755
3879
|
exports.$computeTableMapSkipCellCheck = $computeTableMapSkipCellCheck;
|
|
2756
3880
|
exports.$createTableCellNode = $createTableCellNode;
|
|
@@ -2758,6 +3882,7 @@ exports.$createTableNode = $createTableNode;
|
|
|
2758
3882
|
exports.$createTableNodeWithDimensions = $createTableNodeWithDimensions;
|
|
2759
3883
|
exports.$createTableRowNode = $createTableRowNode;
|
|
2760
3884
|
exports.$createTableSelection = $createTableSelection;
|
|
3885
|
+
exports.$createTableSelectionFrom = $createTableSelectionFrom;
|
|
2761
3886
|
exports.$deleteTableColumn = $deleteTableColumn;
|
|
2762
3887
|
exports.$deleteTableColumn__EXPERIMENTAL = $deleteTableColumn__EXPERIMENTAL;
|
|
2763
3888
|
exports.$deleteTableRow__EXPERIMENTAL = $deleteTableRow__EXPERIMENTAL;
|
|
@@ -2765,6 +3890,7 @@ exports.$findCellNode = $findCellNode;
|
|
|
2765
3890
|
exports.$findTableNode = $findTableNode;
|
|
2766
3891
|
exports.$getElementForTableNode = $getElementForTableNode;
|
|
2767
3892
|
exports.$getNodeTriplet = $getNodeTriplet;
|
|
3893
|
+
exports.$getTableAndElementByKey = $getTableAndElementByKey;
|
|
2768
3894
|
exports.$getTableCellNodeFromLexicalNode = $getTableCellNodeFromLexicalNode;
|
|
2769
3895
|
exports.$getTableCellNodeRect = $getTableCellNodeRect;
|
|
2770
3896
|
exports.$getTableColumnIndexFromTableCellNode = $getTableColumnIndexFromTableCellNode;
|
|
@@ -2775,6 +3901,7 @@ exports.$insertTableColumn = $insertTableColumn;
|
|
|
2775
3901
|
exports.$insertTableColumn__EXPERIMENTAL = $insertTableColumn__EXPERIMENTAL;
|
|
2776
3902
|
exports.$insertTableRow = $insertTableRow;
|
|
2777
3903
|
exports.$insertTableRow__EXPERIMENTAL = $insertTableRow__EXPERIMENTAL;
|
|
3904
|
+
exports.$isScrollableTablesActive = $isScrollableTablesActive;
|
|
2778
3905
|
exports.$isTableCellNode = $isTableCellNode;
|
|
2779
3906
|
exports.$isTableNode = $isTableNode;
|
|
2780
3907
|
exports.$isTableRowNode = $isTableRowNode;
|
|
@@ -2789,4 +3916,9 @@ exports.TableObserver = TableObserver;
|
|
|
2789
3916
|
exports.TableRowNode = TableRowNode;
|
|
2790
3917
|
exports.applyTableHandlers = applyTableHandlers;
|
|
2791
3918
|
exports.getDOMCellFromTarget = getDOMCellFromTarget;
|
|
3919
|
+
exports.getTableElement = getTableElement;
|
|
2792
3920
|
exports.getTableObserverFromTableElement = getTableObserverFromTableElement;
|
|
3921
|
+
exports.registerTableCellUnmergeTransform = registerTableCellUnmergeTransform;
|
|
3922
|
+
exports.registerTablePlugin = registerTablePlugin;
|
|
3923
|
+
exports.registerTableSelectionObserver = registerTableSelectionObserver;
|
|
3924
|
+
exports.setScrollableTablesActive = setScrollableTablesActive;
|