@lexical/offset 0.2.8 → 0.3.1

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.
@@ -4,14 +4,15 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
- * @flow strict
8
7
  */
8
+
9
9
  import type {
10
10
  EditorState,
11
11
  LexicalEditor,
12
12
  NodeKey,
13
13
  RangeSelection,
14
14
  } from 'lexical';
15
+
15
16
  type OffsetElementNode = {
16
17
  child: null | OffsetNode;
17
18
  end: number;
@@ -14,7 +14,7 @@ var lexical = require('lexical');
14
14
  * This source code is licensed under the MIT license found in the
15
15
  * LICENSE file in the root directory of this source tree.
16
16
  *
17
- *
17
+ *
18
18
  */
19
19
  class OffsetView {
20
20
  constructor(offsetMap, firstNode, blockOffsetSize = 1) {
@@ -113,11 +113,14 @@ class OffsetView {
113
113
  }
114
114
  } else {
115
115
  const node = anchor.getNode().getDescendantByIndex(anchorOffset);
116
- const offsetNode = offsetMap.get(node.getKey());
117
116
 
118
- if (offsetNode !== undefined) {
119
- const isAtEnd = node.getIndexWithinParent() !== anchorOffset;
120
- start = isAtEnd ? offsetNode.end : offsetNode.start;
117
+ if (node !== null) {
118
+ const offsetNode = offsetMap.get(node.getKey());
119
+
120
+ if (offsetNode !== undefined) {
121
+ const isAtEnd = node.getIndexWithinParent() !== anchorOffset;
122
+ start = isAtEnd ? offsetNode.end : offsetNode.start;
123
+ }
121
124
  }
122
125
  }
123
126
 
@@ -129,11 +132,14 @@ class OffsetView {
129
132
  }
130
133
  } else {
131
134
  const node = focus.getNode().getDescendantByIndex(focusOffset);
132
- const offsetNode = offsetMap.get(node.getKey());
133
135
 
134
- if (offsetNode !== undefined) {
135
- const isAtEnd = node.getIndexWithinParent() !== focusOffset;
136
- end = isAtEnd ? offsetNode.end : offsetNode.start;
136
+ if (node !== null) {
137
+ const offsetNode = offsetMap.get(node.getKey());
138
+
139
+ if (offsetNode !== undefined) {
140
+ const isAtEnd = node.getIndexWithinParent() !== focusOffset;
141
+ end = isAtEnd ? offsetNode.end : offsetNode.start;
142
+ }
137
143
  }
138
144
  }
139
145
 
@@ -277,7 +283,6 @@ function $searchForNodeWithOffset(firstNode, offset, blockOffsetSize) {
277
283
  }
278
284
 
279
285
  function $createInternalOffsetNode(child, type, start, end, key, parent) {
280
- // $FlowFixMe: not sure why Flow doesn't like this?
281
286
  return {
282
287
  child,
283
288
  end,
@@ -325,8 +330,7 @@ function $createOffsetNode(state, key, parent, nodeMap, offsetMap, blockOffsetSi
325
330
  }
326
331
 
327
332
  state.prevIsBlock = false;
328
- const isText = lexical.$isTextNode(node); // $FlowFixMe: isText means __text is available
329
-
333
+ const isText = lexical.$isTextNode(node);
330
334
  const length = isText ? node.__text.length : 1;
331
335
  const end = state.offset += length;
332
336
  const offsetNode = $createInternalOffsetNode(null, isText ? 'text' : 'inline', start, end, key, parent);
@@ -358,8 +362,7 @@ function $createOffsetChild(state, children, parent, nodeMap, offsetMap, blockOf
358
362
 
359
363
  function $createOffsetView(editor, blockOffsetSize = 1, editorState) {
360
364
  const targetEditorState = editorState || editor._pendingEditorState || editor._editorState;
361
- const nodeMap = targetEditorState._nodeMap; // $FlowFixMe: root is always in the Map
362
-
365
+ const nodeMap = targetEditorState._nodeMap;
363
366
  const root = nodeMap.get('root');
364
367
  const offsetMap = new Map();
365
368
  const state = {
@@ -4,12 +4,12 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
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 #2; 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;
7
+ 'use strict';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;let m=q.$getNodeByKey(c),k=q.$getNodeByKey(e);if(null===m||null===
9
+ 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;let 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 if(a=a.getNode().getDescendantByIndex(f),null!==a){let 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),null!==c&&(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){let h=c._offsetMap;e=e._offsetMap;let m=new Set,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!==a;)b=a.key,
12
+ 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)){let 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,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.");let x=d.offset;if(q.$isElementNode(p)){let 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,prev:null,
14
+ 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}exports.$createOffsetView=function(b,a=1,c){c=(c||b._pendingEditorState||b._editorState)._nodeMap;let 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)};
15
+ exports.OffsetView=t
package/package.json CHANGED
@@ -8,10 +8,10 @@
8
8
  "offset"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.2.8",
11
+ "version": "0.3.1",
12
12
  "main": "LexicalOffset.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.2.8"
14
+ "lexical": "0.3.1"
15
15
  },
16
16
  "repository": {
17
17
  "type": "git",