@lexical/offset 0.12.2 → 0.12.4
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/LexicalOffset.dev.js +8 -83
- package/package.json +2 -2
package/LexicalOffset.dev.js
CHANGED
|
@@ -15,52 +15,42 @@ class OffsetView {
|
|
|
15
15
|
this._firstNode = firstNode;
|
|
16
16
|
this._blockOffsetSize = blockOffsetSize;
|
|
17
17
|
}
|
|
18
|
-
|
|
19
18
|
createSelectionFromOffsets(originalStart, originalEnd, diffOffsetView) {
|
|
20
19
|
const firstNode = this._firstNode;
|
|
21
|
-
|
|
22
20
|
if (firstNode === null) {
|
|
23
21
|
return null;
|
|
24
22
|
}
|
|
25
|
-
|
|
26
23
|
let start = originalStart;
|
|
27
24
|
let end = originalEnd;
|
|
28
25
|
let startOffsetNode = $searchForNodeWithOffset(firstNode, start, this._blockOffsetSize);
|
|
29
26
|
let endOffsetNode = $searchForNodeWithOffset(firstNode, end, this._blockOffsetSize);
|
|
30
|
-
|
|
31
27
|
if (diffOffsetView !== undefined) {
|
|
32
28
|
start = $getAdjustedOffsetFromDiff(start, startOffsetNode, diffOffsetView, this, this._blockOffsetSize);
|
|
33
29
|
startOffsetNode = $searchForNodeWithOffset(firstNode, start, this._blockOffsetSize);
|
|
34
30
|
end = $getAdjustedOffsetFromDiff(end, endOffsetNode, diffOffsetView, this, this._blockOffsetSize);
|
|
35
31
|
endOffsetNode = $searchForNodeWithOffset(firstNode, end, this._blockOffsetSize);
|
|
36
32
|
}
|
|
37
|
-
|
|
38
33
|
if (startOffsetNode === null || endOffsetNode === null) {
|
|
39
34
|
return null;
|
|
40
35
|
}
|
|
41
|
-
|
|
42
36
|
let startKey = startOffsetNode.key;
|
|
43
37
|
let endKey = endOffsetNode.key;
|
|
44
38
|
const startNode = lexical.$getNodeByKey(startKey);
|
|
45
39
|
const endNode = lexical.$getNodeByKey(endKey);
|
|
46
|
-
|
|
47
40
|
if (startNode === null || endNode === null) {
|
|
48
41
|
return null;
|
|
49
42
|
}
|
|
50
|
-
|
|
51
43
|
let startOffset = 0;
|
|
52
44
|
let endOffset = 0;
|
|
53
45
|
let startType = 'element';
|
|
54
46
|
let endType = 'element';
|
|
55
|
-
|
|
56
47
|
if (startOffsetNode.type === 'text') {
|
|
57
48
|
startOffset = start - startOffsetNode.start;
|
|
58
|
-
startType = 'text';
|
|
49
|
+
startType = 'text';
|
|
50
|
+
// If we are at the edge of a text node and we
|
|
59
51
|
// don't have a collapsed selection, then let's
|
|
60
52
|
// try and correct the offset node.
|
|
61
|
-
|
|
62
53
|
const sibling = startNode.getNextSibling();
|
|
63
|
-
|
|
64
54
|
if (start !== end && startOffset === startNode.getTextContentSize() && lexical.$isTextNode(sibling)) {
|
|
65
55
|
startOffset = 0;
|
|
66
56
|
startKey = sibling.__key;
|
|
@@ -69,7 +59,6 @@ class OffsetView {
|
|
|
69
59
|
startKey = startNode.getParentOrThrow().getKey();
|
|
70
60
|
startOffset = end > startOffsetNode.start ? startOffsetNode.end : startOffsetNode.start;
|
|
71
61
|
}
|
|
72
|
-
|
|
73
62
|
if (endOffsetNode.type === 'text') {
|
|
74
63
|
endOffset = end - endOffsetNode.start;
|
|
75
64
|
endType = 'text';
|
|
@@ -77,18 +66,14 @@ class OffsetView {
|
|
|
77
66
|
endKey = endNode.getParentOrThrow().getKey();
|
|
78
67
|
endOffset = end > endOffsetNode.start ? endOffsetNode.end : endOffsetNode.start;
|
|
79
68
|
}
|
|
80
|
-
|
|
81
69
|
const selection = lexical.$createRangeSelection();
|
|
82
|
-
|
|
83
70
|
if (selection === null) {
|
|
84
71
|
return null;
|
|
85
72
|
}
|
|
86
|
-
|
|
87
73
|
selection.anchor.set(startKey, startOffset, startType);
|
|
88
74
|
selection.focus.set(endKey, endOffset, endType);
|
|
89
75
|
return selection;
|
|
90
76
|
}
|
|
91
|
-
|
|
92
77
|
getOffsetsFromSelection(selection) {
|
|
93
78
|
const anchor = selection.anchor;
|
|
94
79
|
const focus = selection.focus;
|
|
@@ -97,184 +82,141 @@ class OffsetView {
|
|
|
97
82
|
const focusOffset = focus.offset;
|
|
98
83
|
let start = -1;
|
|
99
84
|
let end = -1;
|
|
100
|
-
|
|
101
85
|
if (anchor.type === 'text') {
|
|
102
86
|
const offsetNode = offsetMap.get(anchor.key);
|
|
103
|
-
|
|
104
87
|
if (offsetNode !== undefined) {
|
|
105
88
|
start = offsetNode.start + anchorOffset;
|
|
106
89
|
}
|
|
107
90
|
} else {
|
|
108
91
|
const node = anchor.getNode().getDescendantByIndex(anchorOffset);
|
|
109
|
-
|
|
110
92
|
if (node !== null) {
|
|
111
93
|
const offsetNode = offsetMap.get(node.getKey());
|
|
112
|
-
|
|
113
94
|
if (offsetNode !== undefined) {
|
|
114
95
|
const isAtEnd = node.getIndexWithinParent() !== anchorOffset;
|
|
115
96
|
start = isAtEnd ? offsetNode.end : offsetNode.start;
|
|
116
97
|
}
|
|
117
98
|
}
|
|
118
99
|
}
|
|
119
|
-
|
|
120
100
|
if (focus.type === 'text') {
|
|
121
101
|
const offsetNode = offsetMap.get(focus.key);
|
|
122
|
-
|
|
123
102
|
if (offsetNode !== undefined) {
|
|
124
103
|
end = offsetNode.start + focus.offset;
|
|
125
104
|
}
|
|
126
105
|
} else {
|
|
127
106
|
const node = focus.getNode().getDescendantByIndex(focusOffset);
|
|
128
|
-
|
|
129
107
|
if (node !== null) {
|
|
130
108
|
const offsetNode = offsetMap.get(node.getKey());
|
|
131
|
-
|
|
132
109
|
if (offsetNode !== undefined) {
|
|
133
110
|
const isAtEnd = node.getIndexWithinParent() !== focusOffset;
|
|
134
111
|
end = isAtEnd ? offsetNode.end : offsetNode.start;
|
|
135
112
|
}
|
|
136
113
|
}
|
|
137
114
|
}
|
|
138
|
-
|
|
139
115
|
return [start, end];
|
|
140
116
|
}
|
|
141
|
-
|
|
142
117
|
}
|
|
143
|
-
|
|
144
118
|
function $getAdjustedOffsetFromDiff(offset, offsetNode, prevOffsetView, offsetView, blockOffsetSize) {
|
|
145
119
|
const prevOffsetMap = prevOffsetView._offsetMap;
|
|
146
120
|
const offsetMap = offsetView._offsetMap;
|
|
147
121
|
const visited = new Set();
|
|
148
122
|
let adjustedOffset = offset;
|
|
149
123
|
let currentNode = offsetNode;
|
|
150
|
-
|
|
151
124
|
while (currentNode !== null) {
|
|
152
125
|
const key = currentNode.key;
|
|
153
126
|
const prevNode = prevOffsetMap.get(key);
|
|
154
127
|
const diff = currentNode.end - currentNode.start;
|
|
155
128
|
visited.add(key);
|
|
156
|
-
|
|
157
129
|
if (prevNode === undefined) {
|
|
158
130
|
adjustedOffset += diff;
|
|
159
131
|
} else {
|
|
160
132
|
const prevDiff = prevNode.end - prevNode.start;
|
|
161
|
-
|
|
162
133
|
if (prevDiff !== diff) {
|
|
163
134
|
adjustedOffset += diff - prevDiff;
|
|
164
135
|
}
|
|
165
136
|
}
|
|
166
|
-
|
|
167
137
|
const sibling = currentNode.prev;
|
|
168
|
-
|
|
169
138
|
if (sibling !== null) {
|
|
170
139
|
currentNode = sibling;
|
|
171
140
|
continue;
|
|
172
141
|
}
|
|
173
|
-
|
|
174
142
|
let parent = currentNode.parent;
|
|
175
|
-
|
|
176
143
|
while (parent !== null) {
|
|
177
144
|
let parentSibling = parent.prev;
|
|
178
|
-
|
|
179
145
|
if (parentSibling !== null) {
|
|
180
146
|
const parentSiblingKey = parentSibling.key;
|
|
181
147
|
const prevParentSibling = prevOffsetMap.get(parentSiblingKey);
|
|
182
148
|
const parentDiff = parentSibling.end - parentSibling.start;
|
|
183
149
|
visited.add(parentSiblingKey);
|
|
184
|
-
|
|
185
150
|
if (prevParentSibling === undefined) {
|
|
186
151
|
adjustedOffset += parentDiff;
|
|
187
152
|
} else {
|
|
188
153
|
const prevParentDiff = prevParentSibling.end - prevParentSibling.start;
|
|
189
|
-
|
|
190
154
|
if (prevParentDiff !== parentDiff) {
|
|
191
155
|
adjustedOffset += parentDiff - prevParentDiff;
|
|
192
156
|
}
|
|
193
157
|
}
|
|
194
|
-
|
|
195
158
|
parentSibling = parentSibling.prev;
|
|
196
159
|
}
|
|
197
|
-
|
|
198
160
|
parent = parent.parent;
|
|
199
161
|
}
|
|
200
|
-
|
|
201
162
|
break;
|
|
202
|
-
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Now traverse through the old offsets nodes and find any nodes we missed
|
|
203
166
|
// above, because they were not in the latest offset node view (they have been
|
|
204
167
|
// deleted).
|
|
205
|
-
|
|
206
|
-
|
|
207
168
|
const prevFirstNode = prevOffsetView._firstNode;
|
|
208
|
-
|
|
209
169
|
if (prevFirstNode !== null) {
|
|
210
170
|
currentNode = $searchForNodeWithOffset(prevFirstNode, offset, blockOffsetSize);
|
|
211
171
|
let alreadyVisitedParentOfCurrentNode = false;
|
|
212
|
-
|
|
213
172
|
while (currentNode !== null) {
|
|
214
173
|
if (!visited.has(currentNode.key)) {
|
|
215
174
|
alreadyVisitedParentOfCurrentNode = true;
|
|
216
175
|
break;
|
|
217
176
|
}
|
|
218
|
-
|
|
219
177
|
currentNode = currentNode.parent;
|
|
220
178
|
}
|
|
221
|
-
|
|
222
179
|
if (!alreadyVisitedParentOfCurrentNode) {
|
|
223
180
|
while (currentNode !== null) {
|
|
224
181
|
const key = currentNode.key;
|
|
225
|
-
|
|
226
182
|
if (!visited.has(key)) {
|
|
227
183
|
const node = offsetMap.get(key);
|
|
228
184
|
const prevDiff = currentNode.end - currentNode.start;
|
|
229
|
-
|
|
230
185
|
if (node === undefined) {
|
|
231
186
|
adjustedOffset -= prevDiff;
|
|
232
187
|
} else {
|
|
233
188
|
const diff = node.end - node.start;
|
|
234
|
-
|
|
235
189
|
if (prevDiff !== diff) {
|
|
236
190
|
adjustedOffset += diff - prevDiff;
|
|
237
191
|
}
|
|
238
192
|
}
|
|
239
193
|
}
|
|
240
|
-
|
|
241
194
|
currentNode = currentNode.prev;
|
|
242
195
|
}
|
|
243
196
|
}
|
|
244
197
|
}
|
|
245
|
-
|
|
246
198
|
return adjustedOffset;
|
|
247
199
|
}
|
|
248
|
-
|
|
249
200
|
function $searchForNodeWithOffset(firstNode, offset, blockOffsetSize) {
|
|
250
201
|
let currentNode = firstNode;
|
|
251
|
-
|
|
252
202
|
while (currentNode !== null) {
|
|
253
203
|
const end = currentNode.end + (currentNode.type !== 'element' || blockOffsetSize === 0 ? 1 : 0);
|
|
254
|
-
|
|
255
204
|
if (offset < end) {
|
|
256
205
|
const child = currentNode.child;
|
|
257
|
-
|
|
258
206
|
if (child !== null) {
|
|
259
207
|
currentNode = child;
|
|
260
208
|
continue;
|
|
261
209
|
}
|
|
262
|
-
|
|
263
210
|
return currentNode;
|
|
264
211
|
}
|
|
265
|
-
|
|
266
212
|
const sibling = currentNode.next;
|
|
267
|
-
|
|
268
213
|
if (sibling === null) {
|
|
269
214
|
break;
|
|
270
215
|
}
|
|
271
|
-
|
|
272
216
|
currentNode = sibling;
|
|
273
217
|
}
|
|
274
|
-
|
|
275
218
|
return null;
|
|
276
219
|
}
|
|
277
|
-
|
|
278
220
|
function $createInternalOffsetNode(child, type, start, end, key, parent) {
|
|
279
221
|
return {
|
|
280
222
|
child,
|
|
@@ -287,41 +229,34 @@ function $createInternalOffsetNode(child, type, start, end, key, parent) {
|
|
|
287
229
|
type
|
|
288
230
|
};
|
|
289
231
|
}
|
|
290
|
-
|
|
291
232
|
function $createOffsetNode(state, key, parent, nodeMap, offsetMap, blockOffsetSize) {
|
|
292
233
|
const node = nodeMap.get(key);
|
|
293
|
-
|
|
294
234
|
if (node === undefined) {
|
|
295
235
|
{
|
|
296
236
|
throw Error(`createOffsetModel: could not find node by key`);
|
|
297
237
|
}
|
|
298
238
|
}
|
|
299
|
-
|
|
300
239
|
const start = state.offset;
|
|
301
|
-
|
|
302
240
|
if (lexical.$isElementNode(node)) {
|
|
303
241
|
const childKeys = createChildrenArray(node, nodeMap);
|
|
304
242
|
const blockIsEmpty = childKeys.length === 0;
|
|
305
|
-
const child = blockIsEmpty ? null : $createOffsetChild(state, childKeys, null, nodeMap, offsetMap, blockOffsetSize);
|
|
306
|
-
// account for the user being able to selection the block (due to the \n).
|
|
243
|
+
const child = blockIsEmpty ? null : $createOffsetChild(state, childKeys, null, nodeMap, offsetMap, blockOffsetSize);
|
|
307
244
|
|
|
245
|
+
// If the prev node was not a block or the block is empty, we should
|
|
246
|
+
// account for the user being able to selection the block (due to the \n).
|
|
308
247
|
if (!state.prevIsBlock || blockIsEmpty) {
|
|
309
248
|
state.prevIsBlock = true;
|
|
310
249
|
state.offset += blockOffsetSize;
|
|
311
250
|
}
|
|
312
|
-
|
|
313
251
|
const offsetNode = $createInternalOffsetNode(child, 'element', start, start, key, parent);
|
|
314
|
-
|
|
315
252
|
if (child !== null) {
|
|
316
253
|
child.parent = offsetNode;
|
|
317
254
|
}
|
|
318
|
-
|
|
319
255
|
const end = state.offset;
|
|
320
256
|
offsetNode.end = end;
|
|
321
257
|
offsetMap.set(key, offsetNode);
|
|
322
258
|
return offsetNode;
|
|
323
259
|
}
|
|
324
|
-
|
|
325
260
|
state.prevIsBlock = false;
|
|
326
261
|
const isText = lexical.$isTextNode(node);
|
|
327
262
|
const length = isText ? node.__text.length : 1;
|
|
@@ -330,46 +265,36 @@ function $createOffsetNode(state, key, parent, nodeMap, offsetMap, blockOffsetSi
|
|
|
330
265
|
offsetMap.set(key, offsetNode);
|
|
331
266
|
return offsetNode;
|
|
332
267
|
}
|
|
333
|
-
|
|
334
268
|
function $createOffsetChild(state, children, parent, nodeMap, offsetMap, blockOffsetSize) {
|
|
335
269
|
let firstNode = null;
|
|
336
270
|
let currentNode = null;
|
|
337
271
|
const childrenLength = children.length;
|
|
338
|
-
|
|
339
272
|
for (let i = 0; i < childrenLength; i++) {
|
|
340
273
|
const childKey = children[i];
|
|
341
274
|
const offsetNode = $createOffsetNode(state, childKey, parent, nodeMap, offsetMap, blockOffsetSize);
|
|
342
|
-
|
|
343
275
|
if (currentNode === null) {
|
|
344
276
|
firstNode = offsetNode;
|
|
345
277
|
} else {
|
|
346
278
|
offsetNode.prev = currentNode;
|
|
347
279
|
currentNode.next = offsetNode;
|
|
348
280
|
}
|
|
349
|
-
|
|
350
281
|
currentNode = offsetNode;
|
|
351
282
|
}
|
|
352
|
-
|
|
353
283
|
return firstNode;
|
|
354
284
|
}
|
|
355
|
-
|
|
356
285
|
function createChildrenArray(element, nodeMap) {
|
|
357
286
|
const children = [];
|
|
358
287
|
let nodeKey = element.__first;
|
|
359
|
-
|
|
360
288
|
while (nodeKey !== null) {
|
|
361
289
|
const node = nodeMap === null ? lexical.$getNodeByKey(nodeKey) : nodeMap.get(nodeKey);
|
|
362
|
-
|
|
363
290
|
if (node === null || node === undefined) {
|
|
364
291
|
{
|
|
365
292
|
throw Error(`createChildrenArray: node does not exist in nodeMap`);
|
|
366
293
|
}
|
|
367
294
|
}
|
|
368
|
-
|
|
369
295
|
children.push(nodeKey);
|
|
370
296
|
nodeKey = node.__next;
|
|
371
297
|
}
|
|
372
|
-
|
|
373
298
|
return children;
|
|
374
299
|
}
|
|
375
300
|
function $createOffsetView(editor, blockOffsetSize = 1, editorState) {
|
package/package.json
CHANGED