@lexical/offset 0.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Dominic Gannaway
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,374 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ 'use strict';
8
+
9
+ var lexical = require('lexical');
10
+
11
+ /**
12
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
13
+ *
14
+ * This source code is licensed under the MIT license found in the
15
+ * LICENSE file in the root directory of this source tree.
16
+ *
17
+ *
18
+ */
19
+ class OffsetView {
20
+ constructor(offsetMap, firstNode, blockOffsetSize = 1) {
21
+ this._offsetMap = offsetMap;
22
+ this._firstNode = firstNode;
23
+ this._blockOffsetSize = blockOffsetSize;
24
+ }
25
+
26
+ createSelectionFromOffsets(originalStart, originalEnd, diffOffsetView) {
27
+ const firstNode = this._firstNode;
28
+
29
+ if (firstNode === null) {
30
+ return null;
31
+ }
32
+
33
+ let start = originalStart;
34
+ let end = originalEnd;
35
+ let startOffsetNode = $searchForNodeWithOffset(firstNode, start, this._blockOffsetSize);
36
+ let endOffsetNode = $searchForNodeWithOffset(firstNode, end, this._blockOffsetSize);
37
+
38
+ if (diffOffsetView !== undefined) {
39
+ start = $getAdjustedOffsetFromDiff(start, startOffsetNode, diffOffsetView, this, this._blockOffsetSize);
40
+ startOffsetNode = $searchForNodeWithOffset(firstNode, start, this._blockOffsetSize);
41
+ end = $getAdjustedOffsetFromDiff(end, endOffsetNode, diffOffsetView, this, this._blockOffsetSize);
42
+ endOffsetNode = $searchForNodeWithOffset(firstNode, end, this._blockOffsetSize);
43
+ }
44
+
45
+ if (startOffsetNode === null || endOffsetNode === null) {
46
+ return null;
47
+ }
48
+
49
+ let startKey = startOffsetNode.key;
50
+ let endKey = endOffsetNode.key;
51
+ const startNode = lexical.$getNodeByKey(startKey);
52
+ const endNode = lexical.$getNodeByKey(endKey);
53
+
54
+ if (startNode === null || endNode === null) {
55
+ return null;
56
+ }
57
+
58
+ let startOffset = 0;
59
+ let endOffset = 0;
60
+ let startType = 'element';
61
+ let endType = 'element';
62
+
63
+ if (startOffsetNode.type === 'text') {
64
+ startOffset = start - startOffsetNode.start;
65
+ startType = 'text'; // If we are at the edge of a text node and we
66
+ // don't have a collapsed selection, then let's
67
+ // try and correct the offset node.
68
+
69
+ const sibling = startNode.getNextSibling();
70
+
71
+ if (start !== end && startOffset === startNode.getTextContentSize() && lexical.$isTextNode(sibling)) {
72
+ startOffset = 0;
73
+ startKey = sibling.__key;
74
+ }
75
+ } else if (startOffsetNode.type === 'inline') {
76
+ startKey = startNode.getParentOrThrow().getKey();
77
+ startOffset = end > startOffsetNode.start ? startOffsetNode.end : startOffsetNode.start;
78
+ }
79
+
80
+ if (endOffsetNode.type === 'text') {
81
+ endOffset = end - endOffsetNode.start;
82
+ endType = 'text';
83
+ } else if (endOffsetNode.type === 'inline') {
84
+ endKey = endNode.getParentOrThrow().getKey();
85
+ endOffset = end > endOffsetNode.start ? endOffsetNode.end : endOffsetNode.start;
86
+ }
87
+
88
+ const selection = lexical.$createRangeSelection();
89
+
90
+ if (selection === null) {
91
+ return null;
92
+ }
93
+
94
+ selection.anchor.set(startKey, startOffset, startType);
95
+ selection.focus.set(endKey, endOffset, endType);
96
+ return selection;
97
+ }
98
+
99
+ getOffsetsFromSelection(selection) {
100
+ const anchor = selection.anchor;
101
+ const focus = selection.focus;
102
+ const offsetMap = this._offsetMap;
103
+ const anchorOffset = anchor.offset;
104
+ const focusOffset = focus.offset;
105
+ let start = -1;
106
+ let end = -1;
107
+
108
+ if (anchor.type === 'text') {
109
+ const offsetNode = offsetMap.get(anchor.key);
110
+
111
+ if (offsetNode !== undefined) {
112
+ start = offsetNode.start + anchorOffset;
113
+ }
114
+ } else {
115
+ const node = anchor.getNode().getDescendantByIndex(anchorOffset);
116
+ const offsetNode = offsetMap.get(node.getKey());
117
+
118
+ if (offsetNode !== undefined) {
119
+ const isAtEnd = node.getIndexWithinParent() !== anchorOffset;
120
+ start = isAtEnd ? offsetNode.end : offsetNode.start;
121
+ }
122
+ }
123
+
124
+ if (focus.type === 'text') {
125
+ const offsetNode = offsetMap.get(focus.key);
126
+
127
+ if (offsetNode !== undefined) {
128
+ end = offsetNode.start + focus.offset;
129
+ }
130
+ } else {
131
+ const node = focus.getNode().getDescendantByIndex(focusOffset);
132
+ const offsetNode = offsetMap.get(node.getKey());
133
+
134
+ if (offsetNode !== undefined) {
135
+ const isAtEnd = node.getIndexWithinParent() !== focusOffset;
136
+ end = isAtEnd ? offsetNode.end : offsetNode.start;
137
+ }
138
+ }
139
+
140
+ return [start, end];
141
+ }
142
+
143
+ }
144
+
145
+ function $getAdjustedOffsetFromDiff(offset, offsetNode, prevOffsetView, offsetView, blockOffsetSize) {
146
+ const prevOffsetMap = prevOffsetView._offsetMap;
147
+ const offsetMap = offsetView._offsetMap;
148
+ const visited = new Set();
149
+ let adjustedOffset = offset;
150
+ let currentNode = offsetNode;
151
+
152
+ while (currentNode !== null) {
153
+ const key = currentNode.key;
154
+ const prevNode = prevOffsetMap.get(key);
155
+ const diff = currentNode.end - currentNode.start;
156
+ visited.add(key);
157
+
158
+ if (prevNode === undefined) {
159
+ adjustedOffset += diff;
160
+ } else {
161
+ const prevDiff = prevNode.end - prevNode.start;
162
+
163
+ if (prevDiff !== diff) {
164
+ adjustedOffset += diff - prevDiff;
165
+ }
166
+ }
167
+
168
+ const sibling = currentNode.prev;
169
+
170
+ if (sibling !== null) {
171
+ currentNode = sibling;
172
+ continue;
173
+ }
174
+
175
+ let parent = currentNode.parent;
176
+
177
+ while (parent !== null) {
178
+ let parentSibling = parent.prev;
179
+
180
+ if (parentSibling !== null) {
181
+ const parentSiblingKey = parentSibling.key;
182
+ const prevParentSibling = prevOffsetMap.get(parentSiblingKey);
183
+ const parentDiff = parentSibling.end - parentSibling.start;
184
+ visited.add(parentSiblingKey);
185
+
186
+ if (prevParentSibling === undefined) {
187
+ adjustedOffset += parentDiff;
188
+ } else {
189
+ const prevParentDiff = prevParentSibling.end - prevParentSibling.start;
190
+
191
+ if (prevParentDiff !== parentDiff) {
192
+ adjustedOffset += parentDiff - prevParentDiff;
193
+ }
194
+ }
195
+
196
+ parentSibling = parentSibling.prev;
197
+ }
198
+
199
+ parent = parent.parent;
200
+ }
201
+
202
+ break;
203
+ } // Now traverse through the old offsets nodes and find any nodes we missed
204
+ // above, because they were not in the latest offset node view (they have been
205
+ // deleted).
206
+
207
+
208
+ const prevFirstNode = prevOffsetView._firstNode;
209
+
210
+ if (prevFirstNode !== null) {
211
+ currentNode = $searchForNodeWithOffset(prevFirstNode, offset, blockOffsetSize);
212
+ let alreadyVisistedParentOfCurrentNode = false;
213
+
214
+ while (currentNode !== null) {
215
+ if (!visited.has(currentNode.key)) {
216
+ alreadyVisistedParentOfCurrentNode = true;
217
+ break;
218
+ }
219
+
220
+ currentNode = currentNode.parent;
221
+ }
222
+
223
+ if (!alreadyVisistedParentOfCurrentNode) {
224
+ while (currentNode !== null) {
225
+ const key = currentNode.key;
226
+
227
+ if (!visited.has(key)) {
228
+ const node = offsetMap.get(key);
229
+ const prevDiff = currentNode.end - currentNode.start;
230
+
231
+ if (node === undefined) {
232
+ adjustedOffset -= prevDiff;
233
+ } else {
234
+ const diff = node.end - node.start;
235
+
236
+ if (prevDiff !== diff) {
237
+ adjustedOffset += diff - prevDiff;
238
+ }
239
+ }
240
+ }
241
+
242
+ currentNode = currentNode.prev;
243
+ }
244
+ }
245
+ }
246
+
247
+ return adjustedOffset;
248
+ }
249
+
250
+ function $searchForNodeWithOffset(firstNode, offset, blockOffsetSize) {
251
+ let currentNode = firstNode;
252
+
253
+ while (currentNode !== null) {
254
+ const end = currentNode.end + (currentNode.type !== 'element' || blockOffsetSize === 0 ? 1 : 0);
255
+
256
+ if (offset < end) {
257
+ const child = currentNode.child;
258
+
259
+ if (child !== null) {
260
+ currentNode = child;
261
+ continue;
262
+ }
263
+
264
+ return currentNode;
265
+ }
266
+
267
+ const sibling = currentNode.next;
268
+
269
+ if (sibling === null) {
270
+ break;
271
+ }
272
+
273
+ currentNode = sibling;
274
+ }
275
+
276
+ return null;
277
+ }
278
+
279
+ function $createInternalOffsetNode(child, type, start, end, key, parent) {
280
+ // $FlowFixMe: not sure why Flow doesn't like this?
281
+ return {
282
+ child,
283
+ end,
284
+ key,
285
+ next: null,
286
+ parent,
287
+ prev: null,
288
+ start,
289
+ type
290
+ };
291
+ }
292
+
293
+ function $createOffsetNode(state, key, parent, nodeMap, offsetMap, blockOffsetSize) {
294
+ const node = nodeMap.get(key);
295
+
296
+ if (node === undefined) {
297
+ {
298
+ throw Error(`createOffsetModel: could not find node by key`);
299
+ }
300
+ }
301
+
302
+ const start = state.offset;
303
+
304
+ if (lexical.$isElementNode(node)) {
305
+ const childKeys = node.__children;
306
+ const blockIsEmpty = childKeys.length === 0;
307
+ const child = blockIsEmpty ? null : $createOffsetChild(state, childKeys, null, nodeMap, offsetMap, blockOffsetSize); // If the prev node was not a block or the block is empty, we should
308
+ // account for the user being able to selection the block (due to the \n).
309
+
310
+ if (!state.prevIsBlock || blockIsEmpty) {
311
+ state.prevIsBlock = true;
312
+ state.offset += blockOffsetSize;
313
+ }
314
+
315
+ const offsetNode = $createInternalOffsetNode(child, 'element', start, start, key, parent);
316
+
317
+ if (child !== null) {
318
+ child.parent = offsetNode;
319
+ }
320
+
321
+ const end = state.offset;
322
+ offsetNode.end = end;
323
+ offsetMap.set(key, offsetNode);
324
+ return offsetNode;
325
+ }
326
+
327
+ state.prevIsBlock = false;
328
+ const isText = lexical.$isTextNode(node); // $FlowFixMe: isText means __text is available
329
+
330
+ const length = isText ? node.__text.length : 1;
331
+ const end = state.offset += length;
332
+ const offsetNode = $createInternalOffsetNode(null, isText ? 'text' : 'inline', start, end, key, parent);
333
+ offsetMap.set(key, offsetNode);
334
+ return offsetNode;
335
+ }
336
+
337
+ function $createOffsetChild(state, children, parent, nodeMap, offsetMap, blockOffsetSize) {
338
+ let firstNode = null;
339
+ let currentNode = null;
340
+ const childrenLength = children.length;
341
+
342
+ for (let i = 0; i < childrenLength; i++) {
343
+ const childKey = children[i];
344
+ const offsetNode = $createOffsetNode(state, childKey, parent, nodeMap, offsetMap, blockOffsetSize);
345
+
346
+ if (currentNode === null) {
347
+ firstNode = offsetNode;
348
+ } else {
349
+ offsetNode.prev = currentNode;
350
+ currentNode.next = offsetNode;
351
+ }
352
+
353
+ currentNode = offsetNode;
354
+ }
355
+
356
+ return firstNode;
357
+ }
358
+
359
+ function $createOffsetView(editor, blockOffsetSize = 1, editorState) {
360
+ const targetEditorState = editorState || editor._pendingEditorState || editor._editorState;
361
+ const nodeMap = targetEditorState._nodeMap; // $FlowFixMe: root is always in the Map
362
+
363
+ const root = nodeMap.get('root');
364
+ const offsetMap = new Map();
365
+ const state = {
366
+ offset: 0,
367
+ prevIsBlock: false
368
+ };
369
+ const node = $createOffsetChild(state, root.__children, null, nodeMap, offsetMap, blockOffsetSize);
370
+ return new OffsetView(offsetMap, node, blockOffsetSize);
371
+ }
372
+
373
+ exports.$createOffsetView = $createOffsetView;
374
+ exports.OffsetView = OffsetView;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ 'use strict'
8
+ const LexicalOffset = process.env.NODE_ENV === 'development' ? require('./LexicalOffset.dev.js') : require('./LexicalOffset.prod.js')
9
+ module.exports = LexicalOffset;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ var q=require("lexical");
8
+ class t{constructor(b,a,c=1){this._offsetMap=b;this._firstNode=a;this._blockOffsetSize=c}createSelectionFromOffsets(b,a,c){var e=this._firstNode;if(null===e)return null;var f=y(e,b,this._blockOffsetSize);let h=y(e,a,this._blockOffsetSize);void 0!==c&&(b=z(b,f,c,this,this._blockOffsetSize),f=y(e,b,this._blockOffsetSize),a=z(a,h,c,this,this._blockOffsetSize),h=y(e,a,this._blockOffsetSize));if(null===f||null===h)return null;c=f.key;e=h.key;const m=q.$getNodeByKey(c),k=q.$getNodeByKey(e);if(null===m||
9
+ null===k)return null;let l=0,d=0,g="element",n="element";"text"===f.type?(l=b-f.start,g="text",f=m.getNextSibling(),b!==a&&l===m.getTextContentSize()&&q.$isTextNode(f)&&(l=0,c=f.__key)):"inline"===f.type&&(c=m.getParentOrThrow().getKey(),l=a>f.start?f.end:f.start);"text"===h.type?(d=a-h.start,n="text"):"inline"===h.type&&(e=k.getParentOrThrow().getKey(),d=a>h.start?h.end:h.start);b=q.$createRangeSelection();if(null===b)return null;b.anchor.set(c,l,g);b.focus.set(e,d,n);return b}getOffsetsFromSelection(b){var a=
10
+ b.anchor,c=b.focus,e=this._offsetMap;const f=a.offset;var h=c.offset;let m=b=-1;if("text"===a.type)a=e.get(a.key),void 0!==a&&(b=a.start+f);else{a=a.getNode().getDescendantByIndex(f);const k=e.get(a.getKey());void 0!==k&&(b=a.getIndexWithinParent()!==f?k.end:k.start)}"text"===c.type?(h=e.get(c.key),void 0!==h&&(m=h.start+c.offset)):(c=c.getNode().getDescendantByIndex(h),e=e.get(c.getKey()),void 0!==e&&(m=c.getIndexWithinParent()!==h?e.end:e.start));return[b,m]}}
11
+ function z(b,a,c,e,f){const h=c._offsetMap;e=e._offsetMap;const m=new Set;let k=b;for(;null!==a;){var l=a.key,d=h.get(l),g=a.end-a.start;m.add(l);void 0===d?k+=g:(l=d.end-d.start,l!==g&&(k+=g-l));g=a.prev;if(null!==g)a=g;else{for(a=a.parent;null!==a;)d=a.prev,null!==d&&(g=d.key,l=h.get(g),d=d.end-d.start,m.add(g),void 0===l?k+=d:(g=l.end-l.start,g!==d&&(k+=d-g))),a=a.parent;break}}c=c._firstNode;if(null!==c){a=y(c,b,f);for(b=!1;null!==a;){if(!m.has(a.key)){b=!0;break}a=a.parent}if(!b)for(;null!==
12
+ a;)b=a.key,m.has(b)||(f=e.get(b),b=a.end-a.start,void 0===f?k-=b:(f=f.end-f.start,b!==f&&(k+=f-b))),a=a.prev}return k}function y(b,a,c){for(;null!==b;){if(a<b.end+("element"!==b.type||0===c?1:0)){const e=b.child;if(null!==e){b=e;continue}return b}b=b.next;if(null===b)break}return null}
13
+ function A(b,a,c,e,f,h){let m=null,k=null;const l=a.length;for(let v=0;v<l;v++){{var d=b;var g=a[v];var n=c,r=e,w=f,u=h,p=r.get(g);if(void 0===p)throw Error("Minified Lexical error #3; see codes.json for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");const x=d.offset;if(q.$isElementNode(p)){const B=p.__children;r=(p=0===B.length)?null:A(d,B,null,r,w,u);if(!d.prevIsBlock||p)d.prevIsBlock=!0,d.offset+=u;n={child:r,end:x,key:g,next:null,parent:n,
14
+ prev:null,start:x,type:"element"};null!==r&&(r.parent=n);n.end=d.offset;w.set(g,n);g=n}else d.prevIsBlock=!1,u=q.$isTextNode(p),d={child:null,end:d.offset+=u?p.__text.length:1,key:g,next:null,parent:n,prev:null,start:x,type:u?"text":"inline"},w.set(g,d),g=d}null===k?m=g:(g.prev=k,k.next=g);k=g}return m}
15
+ exports.$createOffsetView=function(b,a=1,c){c=(c||b._pendingEditorState||b._editorState)._nodeMap;const e=c.get("root");b=new Map;c=A({offset:0,prevIsBlock:!1},e.__children,null,c,b,a);return new t(b,c,a)};exports.OffsetView=t;
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # `@lexical/offset`
2
+
3
+ This package contains selection offset helpers for Lexical.
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@lexical/offset",
3
+ "author": {
4
+ "name": "Dominic Gannaway",
5
+ "email": "dg@domgan.com"
6
+ },
7
+ "description": "This package contains selection offset helpers for Lexical.",
8
+ "keywords": [
9
+ "lexical",
10
+ "editor",
11
+ "rich-text",
12
+ "offset"
13
+ ],
14
+ "license": "MIT",
15
+ "version": "0.1.15",
16
+ "main": "LexicalOffset.js",
17
+ "peerDependencies": {
18
+ "lexical": "0.1.15"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/facebook/lexical",
23
+ "directory": "packages/lexical-offset"
24
+ }
25
+ }