@lexical/offset 0.6.5 → 0.7.0

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.
@@ -300,7 +300,7 @@ function $createOffsetNode(state, key, parent, nodeMap, offsetMap, blockOffsetSi
300
300
  const start = state.offset;
301
301
 
302
302
  if (lexical.$isElementNode(node)) {
303
- const childKeys = node.__children;
303
+ const childKeys = node.getChildrenKeys();
304
304
  const blockIsEmpty = childKeys.length === 0;
305
305
  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
306
306
  // account for the user being able to selection the block (due to the \n).
@@ -353,6 +353,25 @@ function $createOffsetChild(state, children, parent, nodeMap, offsetMap, blockOf
353
353
  return firstNode;
354
354
  }
355
355
 
356
+ function createChildrenArray(element, nodeMap) {
357
+ const children = [];
358
+ let nodeKey = element.__first;
359
+
360
+ while (nodeKey !== null) {
361
+ const node = nodeMap === null ? lexical.$getNodeByKey(nodeKey) : nodeMap.get(nodeKey);
362
+
363
+ if (node === null || node === undefined) {
364
+ {
365
+ throw Error(`createChildrenArray: node does not exist in nodeMap`);
366
+ }
367
+ }
368
+
369
+ children.push(nodeKey);
370
+ nodeKey = node.__next;
371
+ }
372
+
373
+ return children;
374
+ }
356
375
  function $createOffsetView(editor, blockOffsetSize = 1, editorState) {
357
376
  const targetEditorState = editorState || editor._pendingEditorState || editor._editorState;
358
377
  const nodeMap = targetEditorState._nodeMap;
@@ -362,9 +381,10 @@ function $createOffsetView(editor, blockOffsetSize = 1, editorState) {
362
381
  offset: 0,
363
382
  prevIsBlock: false
364
383
  };
365
- const node = $createOffsetChild(state, root.__children, null, nodeMap, offsetMap, blockOffsetSize);
384
+ const node = $createOffsetChild(state, createChildrenArray(root, nodeMap), null, nodeMap, offsetMap, blockOffsetSize);
366
385
  return new OffsetView(offsetMap, node, blockOffsetSize);
367
386
  }
368
387
 
369
388
  exports.$createOffsetView = $createOffsetView;
370
389
  exports.OffsetView = OffsetView;
390
+ exports.createChildrenArray = createChildrenArray;
@@ -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
- '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; visit https://lexical.dev/docs/error?code=3 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,
14
- key:g,next:null,parent:n,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;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)};exports.OffsetView=t
7
+ 'use strict';var p=require("lexical");
8
+ class t{constructor(a,b,c=1){this._offsetMap=a;this._firstNode=b;this._blockOffsetSize=c}createSelectionFromOffsets(a,b,c){var d=this._firstNode;if(null===d)return null;var f=y(d,a,this._blockOffsetSize);let h=y(d,b,this._blockOffsetSize);void 0!==c&&(a=z(a,f,c,this,this._blockOffsetSize),f=y(d,a,this._blockOffsetSize),b=z(b,h,c,this,this._blockOffsetSize),h=y(d,b,this._blockOffsetSize));if(null===f||null===h)return null;c=f.key;d=h.key;let m=p.$getNodeByKey(c),k=p.$getNodeByKey(d);if(null===m||null===
9
+ k)return null;let l=0,e=0,g="element",n="element";"text"===f.type?(l=a-f.start,g="text",f=m.getNextSibling(),a!==b&&l===m.getTextContentSize()&&p.$isTextNode(f)&&(l=0,c=f.__key)):"inline"===f.type&&(c=m.getParentOrThrow().getKey(),l=b>f.start?f.end:f.start);"text"===h.type?(e=b-h.start,n="text"):"inline"===h.type&&(d=k.getParentOrThrow().getKey(),e=b>h.start?h.end:h.start);a=p.$createRangeSelection();if(null===a)return null;a.anchor.set(c,l,g);a.focus.set(d,e,n);return a}getOffsetsFromSelection(a){var b=
10
+ a.anchor,c=a.focus,d=this._offsetMap;let f=b.offset;var h=c.offset;let m=a=-1;if("text"===b.type)b=d.get(b.key),void 0!==b&&(a=b.start+f);else if(b=b.getNode().getDescendantByIndex(f),null!==b){let k=d.get(b.getKey());void 0!==k&&(a=b.getIndexWithinParent()!==f?k.end:k.start)}"text"===c.type?(h=d.get(c.key),void 0!==h&&(m=h.start+c.offset)):(c=c.getNode().getDescendantByIndex(h),null!==c&&(d=d.get(c.getKey()),void 0!==d&&(m=c.getIndexWithinParent()!==h?d.end:d.start)));return[a,m]}}
11
+ function z(a,b,c,d,f){let h=c._offsetMap;d=d._offsetMap;let m=new Set,k=a;for(;null!==b;){var l=b.key,e=h.get(l),g=b.end-b.start;m.add(l);void 0===e?k+=g:(l=e.end-e.start,l!==g&&(k+=g-l));g=b.prev;if(null!==g)b=g;else{for(b=b.parent;null!==b;)e=b.prev,null!==e&&(g=e.key,l=h.get(g),e=e.end-e.start,m.add(g),void 0===l?k+=e:(g=l.end-l.start,g!==e&&(k+=e-g))),b=b.parent;break}}c=c._firstNode;if(null!==c){b=y(c,a,f);for(a=!1;null!==b;){if(!m.has(b.key)){a=!0;break}b=b.parent}if(!a)for(;null!==b;)a=b.key,
12
+ m.has(a)||(f=d.get(a),a=b.end-b.start,void 0===f?k-=a:(f=f.end-f.start,a!==f&&(k+=f-a))),b=b.prev}return k}function y(a,b,c){for(;null!==a;){if(b<a.end+("element"!==a.type||0===c?1:0)){let d=a.child;if(null!==d){a=d;continue}return a}a=a.next;if(null===a)break}return null}
13
+ function A(a,b,c,d,f,h){let m=null,k=null,l=b.length;for(let v=0;v<l;v++){{var e=a;var g=b[v];var n=c,r=d,w=f,u=h,q=r.get(g);if(void 0===q)throw Error("Minified Lexical error #3; visit https://lexical.dev/docs/error?code=3 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");let x=e.offset;if(p.$isElementNode(q)){let B=q.getChildrenKeys();r=(q=0===B.length)?null:A(e,B,null,r,w,u);if(!e.prevIsBlock||q)e.prevIsBlock=!0,e.offset+=u;n={child:r,
14
+ end:x,key:g,next:null,parent:n,prev:null,start:x,type:"element"};null!==r&&(r.parent=n);n.end=e.offset;w.set(g,n);g=n}else e.prevIsBlock=!1,u=p.$isTextNode(q),e={child:null,end:e.offset+=u?q.__text.length:1,key:g,next:null,parent:n,prev:null,start:x,type:u?"text":"inline"},w.set(g,e),g=e}null===k?m=g:(g.prev=k,k.next=g);k=g}return m}
15
+ function C(a,b){let c=[];for(a=a.__first;null!==a;){let d=null===b?p.$getNodeByKey(a):b.get(a);if(null===d||void 0===d)throw Error("createChildrenArray: node does not exist in nodeMap");c.push(a);a=d.__next}return c}exports.$createOffsetView=function(a,b=1,c){c=(c||a._pendingEditorState||a._editorState)._nodeMap;let d=c.get("root");a=new Map;c=A({offset:0,prevIsBlock:!1},C(d,c),null,c,a,b);return new t(a,c,b)};exports.OffsetView=t;exports.createChildrenArray=C
package/index.d.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  *
8
8
  */
9
- import type { EditorState, LexicalEditor, NodeKey, RangeSelection } from 'lexical';
9
+ import type { EditorState, ElementNode, LexicalEditor, NodeKey, NodeMap, RangeSelection } from 'lexical';
10
10
  declare type OffsetElementNode = {
11
11
  child: null | OffsetNode;
12
12
  end: number;
@@ -47,5 +47,6 @@ export declare class OffsetView {
47
47
  createSelectionFromOffsets(originalStart: number, originalEnd: number, diffOffsetView?: OffsetView): null | RangeSelection;
48
48
  getOffsetsFromSelection(selection: RangeSelection): [number, number];
49
49
  }
50
+ export declare function createChildrenArray(element: ElementNode, nodeMap: null | NodeMap): Array<NodeKey>;
50
51
  export declare function $createOffsetView(editor: LexicalEditor, blockOffsetSize?: number, editorState?: EditorState | null): OffsetView;
51
52
  export {};
package/package.json CHANGED
@@ -8,10 +8,10 @@
8
8
  "offset"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.6.5",
11
+ "version": "0.7.0",
12
12
  "main": "LexicalOffset.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.6.5"
14
+ "lexical": "0.7.0"
15
15
  },
16
16
  "repository": {
17
17
  "type": "git",