onchain-lexical-instance 0.0.13 → 0.0.14
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/OnchainLexicalInstance.js +113 -41
- package/dist/OnchainLexicalInstance.mjs +115 -44
- package/package.json +2 -2
- package/src/instancePlugin.ts +465 -427
- package/src/internalLink/index.tsx +114 -94
- package/src/parameters/index.tsx +149 -110
- package/src/parameters/parametersComponent.tsx +82 -55
- package/src/parameters/types.d.ts +3 -0
- package/src/utils.ts +607 -568
package/src/instancePlugin.ts
CHANGED
|
@@ -1,427 +1,465 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
*/
|
|
8
|
-
import {$isListItemNode} from '@lexical/list';
|
|
9
|
-
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
|
|
10
|
-
import {
|
|
11
|
-
$createTableCellNode,
|
|
12
|
-
$createTableRowNode,
|
|
13
|
-
$isTableCellNode,
|
|
14
|
-
$isTableSelection,
|
|
15
|
-
TableCellNode,
|
|
16
|
-
TableRowNode,
|
|
17
|
-
} from '@lexical/table';
|
|
18
|
-
import {$findMatchingParent, mergeRegister} from '@lexical/utils';
|
|
19
|
-
import {
|
|
20
|
-
$createRangeSelection,
|
|
21
|
-
$createTextNode,
|
|
22
|
-
$getNodeByKey,
|
|
23
|
-
$getSelection,
|
|
24
|
-
$
|
|
25
|
-
$
|
|
26
|
-
$
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
import {
|
|
41
|
-
import {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
} from './list
|
|
53
|
-
import {
|
|
54
|
-
$
|
|
55
|
-
$
|
|
56
|
-
} from './
|
|
57
|
-
import {
|
|
58
|
-
$
|
|
59
|
-
$
|
|
60
|
-
} from './
|
|
61
|
-
import {
|
|
62
|
-
|
|
63
|
-
$
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
} from './
|
|
67
|
-
import {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
$
|
|
81
|
-
|
|
82
|
-
editor
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
);
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
}
|
|
250
|
-
elementNodes.
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
const
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
import {$isListItemNode} from '@lexical/list';
|
|
9
|
+
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
|
|
10
|
+
import {
|
|
11
|
+
$createTableCellNode,
|
|
12
|
+
$createTableRowNode,
|
|
13
|
+
$isTableCellNode,
|
|
14
|
+
$isTableSelection,
|
|
15
|
+
TableCellNode,
|
|
16
|
+
TableRowNode,
|
|
17
|
+
} from '@lexical/table';
|
|
18
|
+
import {$findMatchingParent, mergeRegister} from '@lexical/utils';
|
|
19
|
+
import {
|
|
20
|
+
$createRangeSelection,
|
|
21
|
+
$createTextNode,
|
|
22
|
+
$getNodeByKey,
|
|
23
|
+
$getSelection,
|
|
24
|
+
$isDecoratorNode,
|
|
25
|
+
$isElementNode,
|
|
26
|
+
$isNodeSelection,
|
|
27
|
+
$isRangeSelection,
|
|
28
|
+
$setSelection,
|
|
29
|
+
CLICK_COMMAND,
|
|
30
|
+
COMMAND_PRIORITY_CRITICAL,
|
|
31
|
+
COMMAND_PRIORITY_LOW,
|
|
32
|
+
DELETE_CHARACTER_COMMAND,
|
|
33
|
+
ElementNode,
|
|
34
|
+
exportNodeToJSON,
|
|
35
|
+
INSERT_PARAGRAPH_COMMAND,
|
|
36
|
+
LexicalNode,
|
|
37
|
+
SELECTION_CHANGE_COMMAND,
|
|
38
|
+
SerializedLexicalNode,
|
|
39
|
+
} from 'lexical';
|
|
40
|
+
import {getStorageSerializedString} from 'onchain-lexical-markdown';
|
|
41
|
+
import {dfs} from 'onchain-utility/traversal';
|
|
42
|
+
import React, {useEffect} from 'react';
|
|
43
|
+
|
|
44
|
+
import {$isInstanceNode} from './base';
|
|
45
|
+
import {
|
|
46
|
+
ADD_NEW_INSTANCE_NODE,
|
|
47
|
+
DisableSelector,
|
|
48
|
+
PluginProps,
|
|
49
|
+
SPLIT_INSTANCE_NODE,
|
|
50
|
+
} from './const';
|
|
51
|
+
import {HorizontalRulePlugin} from './horizontal/horizontalPlugin';
|
|
52
|
+
import {$createInstanceListNode, $isInstanceListNode} from './list';
|
|
53
|
+
import {
|
|
54
|
+
$isInstanceListItemNode,
|
|
55
|
+
$registerInstanceListItemInsertParagraph,
|
|
56
|
+
} from './list/item';
|
|
57
|
+
import {
|
|
58
|
+
$registerNumberDecoratorDomUpdate,
|
|
59
|
+
$registerNumberDecoratorNodeUpdate,
|
|
60
|
+
} from './number';
|
|
61
|
+
import {
|
|
62
|
+
$createInstanceParagraphNode,
|
|
63
|
+
$registerInstanceParagraphNodeTransform,
|
|
64
|
+
} from './paragraph';
|
|
65
|
+
import {$registerInstanceHeadingNodeTransform} from './paragraph/title';
|
|
66
|
+
import {ParametersNode} from './parameters';
|
|
67
|
+
import {
|
|
68
|
+
$createInstanceTableNode,
|
|
69
|
+
$isInstanceTableNode,
|
|
70
|
+
$registerTableCommand,
|
|
71
|
+
} from './table';
|
|
72
|
+
import {$addInstancesNode, $selectDecoratorNode, clearCache, setTemporaryContentText} from './utils';
|
|
73
|
+
|
|
74
|
+
export const InstancePlugin: React.FC<PluginProps> = (props) => {
|
|
75
|
+
const {placeholder} = props;
|
|
76
|
+
const [editor] = useLexicalComposerContext();
|
|
77
|
+
// const {setSelectedInstance} = useInstanceConfig();
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
return mergeRegister(
|
|
80
|
+
$registerInstanceParagraphNodeTransform(editor, {placeholder}),
|
|
81
|
+
$registerInstanceHeadingNodeTransform(editor),
|
|
82
|
+
$registerInstanceListItemInsertParagraph(editor),
|
|
83
|
+
$registerNumberDecoratorNodeUpdate(editor),
|
|
84
|
+
$registerNumberDecoratorDomUpdate(editor),
|
|
85
|
+
$registerTableCommand(editor),
|
|
86
|
+
// $selectionChange(editor, setSelectedInstance),
|
|
87
|
+
editor.registerCommand(
|
|
88
|
+
SELECTION_CHANGE_COMMAND,
|
|
89
|
+
() => {
|
|
90
|
+
const selection = $getSelection();
|
|
91
|
+
if ($isNodeSelection(selection)) {
|
|
92
|
+
const [node] = selection.getNodes();
|
|
93
|
+
if ($isDecoratorNode(node)) {
|
|
94
|
+
$selectDecoratorNode(node);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return false;
|
|
98
|
+
},
|
|
99
|
+
COMMAND_PRIORITY_CRITICAL,
|
|
100
|
+
),
|
|
101
|
+
editor.registerCommand(
|
|
102
|
+
CLICK_COMMAND,
|
|
103
|
+
(event: MouseEvent) => {
|
|
104
|
+
if (event.target instanceof HTMLElement) {
|
|
105
|
+
const decoratorRootEle = event.target.closest(
|
|
106
|
+
'[data-lexical-decorator="true"]',
|
|
107
|
+
);
|
|
108
|
+
if (decoratorRootEle) {
|
|
109
|
+
const key = decoratorRootEle.getAttribute('key');
|
|
110
|
+
if (key) {
|
|
111
|
+
const node = $getNodeByKey<ParametersNode>(key);
|
|
112
|
+
$selectDecoratorNode(node);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return false;
|
|
117
|
+
},
|
|
118
|
+
COMMAND_PRIORITY_LOW,
|
|
119
|
+
),
|
|
120
|
+
editor.registerCommand(
|
|
121
|
+
DELETE_CHARACTER_COMMAND,
|
|
122
|
+
(event) => {
|
|
123
|
+
const selection = $getSelection();
|
|
124
|
+
if (selection) {
|
|
125
|
+
return selection.getNodes().some((node) => {
|
|
126
|
+
return editor
|
|
127
|
+
.getElementByKey(node.getKey())
|
|
128
|
+
?.closest(DisableSelector);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return false;
|
|
132
|
+
},
|
|
133
|
+
COMMAND_PRIORITY_CRITICAL,
|
|
134
|
+
),
|
|
135
|
+
editor.registerCommand(
|
|
136
|
+
INSERT_PARAGRAPH_COMMAND,
|
|
137
|
+
(event) => {
|
|
138
|
+
const selection = $getSelection();
|
|
139
|
+
const [start, end] = selection?.getStartEndPoints() || [];
|
|
140
|
+
if (start && end && start.key === end.key) {
|
|
141
|
+
return $isInstanceNode($getNodeByKey(start.key));
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
144
|
+
},
|
|
145
|
+
COMMAND_PRIORITY_CRITICAL,
|
|
146
|
+
),
|
|
147
|
+
editor.registerCommand(
|
|
148
|
+
INSERT_PARAGRAPH_COMMAND,
|
|
149
|
+
(event) => {
|
|
150
|
+
const selection = $getSelection();
|
|
151
|
+
const [start, end] = selection?.getStartEndPoints() || [];
|
|
152
|
+
if (start && end && start.key === end.key) {
|
|
153
|
+
return $isInstanceNode($getNodeByKey(start.key));
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
},
|
|
157
|
+
COMMAND_PRIORITY_CRITICAL,
|
|
158
|
+
),
|
|
159
|
+
editor.registerCommand(
|
|
160
|
+
ADD_NEW_INSTANCE_NODE,
|
|
161
|
+
({insNodeKey, instances, isAddChildLevel}) => {
|
|
162
|
+
$addInstancesNode({insNodeKey, instances, isAddChildLevel});
|
|
163
|
+
return true;
|
|
164
|
+
},
|
|
165
|
+
COMMAND_PRIORITY_LOW,
|
|
166
|
+
),
|
|
167
|
+
/** 需求拆分 */
|
|
168
|
+
editor.registerCommand(
|
|
169
|
+
SPLIT_INSTANCE_NODE,
|
|
170
|
+
({selection, insNodeKey, instance, isAddChildLevel}) => {
|
|
171
|
+
$setSelection(null);
|
|
172
|
+
if ($isRangeSelection(selection) || $isTableSelection(selection)) {
|
|
173
|
+
const forwardSelection = $createRangeSelection();
|
|
174
|
+
const [startPoint, endPoint] = selection.isBackward()
|
|
175
|
+
? [selection.focus, selection.anchor]
|
|
176
|
+
: [selection.anchor, selection.focus];
|
|
177
|
+
forwardSelection.anchor.set(
|
|
178
|
+
startPoint.key,
|
|
179
|
+
startPoint.offset,
|
|
180
|
+
startPoint.type,
|
|
181
|
+
);
|
|
182
|
+
forwardSelection.focus.set(
|
|
183
|
+
endPoint.key,
|
|
184
|
+
endPoint.offset,
|
|
185
|
+
endPoint.type,
|
|
186
|
+
);
|
|
187
|
+
const nodes = forwardSelection.extract();
|
|
188
|
+
|
|
189
|
+
const hasElementNode = nodes.some((node) => $isElementNode(node));
|
|
190
|
+
const elementNodes: LexicalNode[] = [];
|
|
191
|
+
if (!hasElementNode) {
|
|
192
|
+
const paragraph = $createInstanceParagraphNode();
|
|
193
|
+
nodes.forEach((node) => node.remove());
|
|
194
|
+
paragraph.append(...nodes);
|
|
195
|
+
elementNodes.push(paragraph);
|
|
196
|
+
} else {
|
|
197
|
+
if ($isTableSelection(selection)) {
|
|
198
|
+
const size = {
|
|
199
|
+
cell: 0,
|
|
200
|
+
row: 0,
|
|
201
|
+
rowIndex: Infinity,
|
|
202
|
+
};
|
|
203
|
+
const newRowMap = new Map<number, TableRowNode>();
|
|
204
|
+
const configs = selection
|
|
205
|
+
.getNodes()
|
|
206
|
+
.filter((node) => $isTableCellNode(node))
|
|
207
|
+
.map((node) => {
|
|
208
|
+
const row = node.getParent()!;
|
|
209
|
+
const rowIndex = row.getIndexWithinParent();
|
|
210
|
+
if (size.rowIndex !== rowIndex) {
|
|
211
|
+
size.rowIndex = rowIndex;
|
|
212
|
+
size.cell = 1;
|
|
213
|
+
size.row++;
|
|
214
|
+
} else {
|
|
215
|
+
size.cell++;
|
|
216
|
+
}
|
|
217
|
+
if (!newRowMap.has(rowIndex)) {
|
|
218
|
+
newRowMap.set(rowIndex, $createTableRowNode());
|
|
219
|
+
}
|
|
220
|
+
return {
|
|
221
|
+
col: node.getIndexWithinParent(),
|
|
222
|
+
node,
|
|
223
|
+
rowIndex: rowIndex,
|
|
224
|
+
rowNode: row,
|
|
225
|
+
};
|
|
226
|
+
});
|
|
227
|
+
const tableNode = $createInstanceTableNode();
|
|
228
|
+
const maxCellSize = configs[0].rowNode.getChildrenSize();
|
|
229
|
+
const maxRowSize = configs[0].rowNode
|
|
230
|
+
.getParent()
|
|
231
|
+
?.getChildrenSize();
|
|
232
|
+
if (size.cell === maxCellSize) {
|
|
233
|
+
tableNode.append(...configs.map((config) => config.rowNode));
|
|
234
|
+
} else {
|
|
235
|
+
configs.forEach((config) => {
|
|
236
|
+
const rowNode =
|
|
237
|
+
newRowMap.get(config.rowIndex) ||
|
|
238
|
+
newRowMap
|
|
239
|
+
.set(config.rowIndex, $createTableRowNode())
|
|
240
|
+
.get(config.rowIndex)!;
|
|
241
|
+
if (size.row !== maxRowSize) {
|
|
242
|
+
config.node.replace($createTableCellNode());
|
|
243
|
+
}
|
|
244
|
+
rowNode.append(config.node);
|
|
245
|
+
});
|
|
246
|
+
newRowMap.values().forEach((row) => {
|
|
247
|
+
tableNode.append(row);
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
elementNodes.push(tableNode);
|
|
251
|
+
} else {
|
|
252
|
+
const [startNode, endNode] = [
|
|
253
|
+
startPoint.getNode(),
|
|
254
|
+
endPoint.getNode(),
|
|
255
|
+
];
|
|
256
|
+
|
|
257
|
+
const startAncestors = getAncestors(startNode);
|
|
258
|
+
const endAncestors = getAncestors(endNode);
|
|
259
|
+
|
|
260
|
+
const sameLevel = new Map<string, LexicalNode>();
|
|
261
|
+
startAncestors.some((sa, idx) => {
|
|
262
|
+
const eaIdx = endAncestors.findIndex(
|
|
263
|
+
(ea, idx) => ea.getKey() === sa.getKey(),
|
|
264
|
+
);
|
|
265
|
+
const isSame = eaIdx > -1;
|
|
266
|
+
if (isSame) {
|
|
267
|
+
sameLevel.set('start', startAncestors[idx - 1]);
|
|
268
|
+
sameLevel.set('end', endAncestors[eaIdx - 1]);
|
|
269
|
+
}
|
|
270
|
+
return isSame;
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
const next = $getFollowUpNode({
|
|
274
|
+
node: startNode,
|
|
275
|
+
terminate: sameLevel.get('start'),
|
|
276
|
+
type: 'getNextSiblings',
|
|
277
|
+
});
|
|
278
|
+
const previous = $getFollowUpNode({
|
|
279
|
+
node: endNode.getNextSibling() || endNode,
|
|
280
|
+
terminate: sameLevel.get('end'),
|
|
281
|
+
type: 'getPreviousSiblings',
|
|
282
|
+
});
|
|
283
|
+
let node = next.original.getNextSibling();
|
|
284
|
+
while (node && node.getKey() !== previous.original.getKey()) {
|
|
285
|
+
elementNodes.push(node);
|
|
286
|
+
node = node.getNextSibling();
|
|
287
|
+
}
|
|
288
|
+
elementNodes.unshift(...next.nodes);
|
|
289
|
+
elementNodes.push(...previous.nodes);
|
|
290
|
+
// 添加嵌套Item父List
|
|
291
|
+
if (elementNodes.every((node) => $isListItemNode(node))) {
|
|
292
|
+
const originalListNode = $findMatchingParent(
|
|
293
|
+
next.original,
|
|
294
|
+
(node) => $isInstanceListNode(node),
|
|
295
|
+
);
|
|
296
|
+
if (originalListNode) {
|
|
297
|
+
const listNode = $createInstanceListNode(
|
|
298
|
+
originalListNode.getListType(),
|
|
299
|
+
);
|
|
300
|
+
listNode.append(...elementNodes);
|
|
301
|
+
elementNodes.length = 0;
|
|
302
|
+
(elementNodes as LexicalNode[]).push(listNode);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const paragraph = $createInstanceParagraphNode();
|
|
309
|
+
paragraph.append(...elementNodes);
|
|
310
|
+
const json = exportNodeToJSON<
|
|
311
|
+
SerializedLexicalNode & {children: SerializedLexicalNode[]}
|
|
312
|
+
>(paragraph);
|
|
313
|
+
setTemporaryContentText(
|
|
314
|
+
instance,
|
|
315
|
+
getStorageSerializedString(json.children),
|
|
316
|
+
);
|
|
317
|
+
|
|
318
|
+
$addInstancesNode({
|
|
319
|
+
insNodeKey,
|
|
320
|
+
instances: [instance],
|
|
321
|
+
isAddChildLevel,
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
return true;
|
|
325
|
+
},
|
|
326
|
+
COMMAND_PRIORITY_LOW,
|
|
327
|
+
),
|
|
328
|
+
);
|
|
329
|
+
}, [editor, placeholder]);
|
|
330
|
+
useEffect(() => {
|
|
331
|
+
return () => {
|
|
332
|
+
clearCache();
|
|
333
|
+
};
|
|
334
|
+
}, []);
|
|
335
|
+
return React.createElement(
|
|
336
|
+
React.Fragment,
|
|
337
|
+
{},
|
|
338
|
+
React.createElement(HorizontalRulePlugin),
|
|
339
|
+
);
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
function getDescendants(node: ElementNode) {
|
|
343
|
+
if ($isElementNode(node)) {
|
|
344
|
+
return dfs(node.getChildren(), (node) => {
|
|
345
|
+
if ($isElementNode(node)) {
|
|
346
|
+
return node.getChildren();
|
|
347
|
+
} else {
|
|
348
|
+
return [];
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
} else {
|
|
352
|
+
return [];
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function getAncestors(node: LexicalNode) {
|
|
357
|
+
const parent = node.getParent();
|
|
358
|
+
if (parent) {
|
|
359
|
+
return dfs([parent], (node) => {
|
|
360
|
+
const parent = node.getParent();
|
|
361
|
+
if (parent) {
|
|
362
|
+
return [parent];
|
|
363
|
+
} else {
|
|
364
|
+
return [];
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
} else {
|
|
368
|
+
return [];
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function $getFollowUpNode({
|
|
373
|
+
node,
|
|
374
|
+
terminate,
|
|
375
|
+
type,
|
|
376
|
+
}: {
|
|
377
|
+
node: LexicalNode;
|
|
378
|
+
terminate?: LexicalNode;
|
|
379
|
+
type: 'getNextSiblings' | 'getPreviousSiblings';
|
|
380
|
+
}) {
|
|
381
|
+
const isTableCellNode = $isTableCellNode(node);
|
|
382
|
+
if (isTableCellNode) {
|
|
383
|
+
const descendants = getDescendants(node as TableCellNode);
|
|
384
|
+
const text = $createTextNode('');
|
|
385
|
+
node = text;
|
|
386
|
+
if (type === 'getNextSiblings') {
|
|
387
|
+
descendants[0].insertBefore(text);
|
|
388
|
+
} else {
|
|
389
|
+
descendants[descendants.length - 1].insertAfter(text);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const ancestors = terminate ? [terminate] : getAncestors(node);
|
|
394
|
+
const index = ancestors.findIndex((node) => $isInstanceNode(node));
|
|
395
|
+
const term = ancestors[index - 1] || ancestors[ancestors.length - 1];
|
|
396
|
+
const original = term;
|
|
397
|
+
|
|
398
|
+
// console.log(
|
|
399
|
+
// {
|
|
400
|
+
// node,
|
|
401
|
+
// original,
|
|
402
|
+
// terminate,
|
|
403
|
+
// type,
|
|
404
|
+
// },
|
|
405
|
+
// 'getFollowUpNode',
|
|
406
|
+
// );
|
|
407
|
+
|
|
408
|
+
const set = new Set<LexicalNode>();
|
|
409
|
+
const termKey = term.getKey();
|
|
410
|
+
dfs([node], (node) => {
|
|
411
|
+
const parent = node.getParent();
|
|
412
|
+
const parentKey = parent?.getKey();
|
|
413
|
+
const isStop = parentKey === termKey;
|
|
414
|
+
const siblings = node[type]();
|
|
415
|
+
siblings.forEach((sibling) => {
|
|
416
|
+
if ($isInstanceListItemNode(sibling)) {
|
|
417
|
+
sibling.defaultRemove();
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
sibling.remove();
|
|
421
|
+
});
|
|
422
|
+
if (parent) {
|
|
423
|
+
const constructor = parent.constructor;
|
|
424
|
+
const newParent = constructor.importJSON(
|
|
425
|
+
parent.exportJSON(),
|
|
426
|
+
) as ElementNode;
|
|
427
|
+
newParent.append(...siblings);
|
|
428
|
+
if (set.size) {
|
|
429
|
+
const children = Array.from(set)[0];
|
|
430
|
+
if (type === 'getNextSiblings') {
|
|
431
|
+
if (siblings.length) {
|
|
432
|
+
siblings[0].insertBefore(children);
|
|
433
|
+
} else {
|
|
434
|
+
newParent.append(children);
|
|
435
|
+
}
|
|
436
|
+
} else {
|
|
437
|
+
if (siblings.length) {
|
|
438
|
+
siblings[siblings.length - 1].insertAfter(children);
|
|
439
|
+
} else {
|
|
440
|
+
newParent.append(children);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
set.clear();
|
|
444
|
+
}
|
|
445
|
+
set.add(newParent);
|
|
446
|
+
}
|
|
447
|
+
if (isStop || !parent) {
|
|
448
|
+
return [];
|
|
449
|
+
} else {
|
|
450
|
+
return [parent].filter(Boolean);
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
if (isTableCellNode) {
|
|
454
|
+
const tableNode = $findMatchingParent(node, (node) =>
|
|
455
|
+
$isInstanceTableNode(node),
|
|
456
|
+
);
|
|
457
|
+
if (tableNode) {
|
|
458
|
+
tableNode.remove();
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
return {
|
|
462
|
+
nodes: Array.from(set.values()),
|
|
463
|
+
original,
|
|
464
|
+
};
|
|
465
|
+
}
|