@lexical/offset
Version:
This package contains selection offset helpers for Lexical.
10 lines (8 loc) • 4.05 kB
JavaScript
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
;var e=require("lexical");class t{_offsetMap;_firstNode;_blockOffsetSize;constructor(e,t,n=1){this._offsetMap=e,this._firstNode=t,this._blockOffsetSize=n}createSelectionFromOffsets(t,o,r){const l=this._firstNode;if(null===l)return null;let i=t,f=o,c=s(l,i,this._blockOffsetSize),a=s(l,f,this._blockOffsetSize);if(void 0!==r&&(i=n(i,c,r,this,this._blockOffsetSize),c=s(l,i,this._blockOffsetSize),f=n(f,a,r,this,this._blockOffsetSize),a=s(l,f,this._blockOffsetSize)),null===c||null===a)return null;let u=c.key,d=a.key;const p=e.$getNodeByKey(u),g=e.$getNodeByKey(d);if(null===p||null===g)return null;let h=0,y=0,k="element",v="element";if("text"===c.type){h=i-c.start,k="text";const t=p.getNextSibling();i!==f&&h===p.getTextContentSize()&&e.$isTextNode(t)&&(h=0,u=t.__key)}else"inline"===c.type&&(u=p.getParentOrThrow().getKey(),h=f>c.start?c.end:c.start);"text"===a.type?(y=f-a.start,v="text"):"inline"===a.type&&(d=g.getParentOrThrow().getKey(),y=f>a.start?a.end:a.start);const x=e.$createRangeSelection();return null===x?null:(x.anchor.set(u,h,k),x.focus.set(d,y,v),x)}getOffsetsFromSelection(e){const t=e.anchor,n=e.focus,s=this._offsetMap,o=t.offset,r=n.offset;let l=-1,i=-1;if("text"===t.type){const e=s.get(t.key);void 0!==e&&(l=e.start+o)}else{const e=t.getNode().getDescendantByIndex(o);if(null!==e){const t=s.get(e.getKey());if(void 0!==t){l=e.getIndexWithinParent()!==o?t.end:t.start}}}if("text"===n.type){const e=s.get(n.key);void 0!==e&&(i=e.start+n.offset)}else{const e=n.getNode().getDescendantByIndex(r);if(null!==e){const t=s.get(e.getKey());if(void 0!==t){i=e.getIndexWithinParent()!==r?t.end:t.start}}}return[l,i]}}function n(e,t,n,o,r){const l=n._offsetMap,i=o._offsetMap,f=new Set;let c=e,a=t;for(;null!==a;){const e=a.key,t=l.get(e),n=a.end-a.start;if(f.add(e),void 0===t)c+=n;else{const e=t.end-t.start;e!==n&&(c+=n-e)}const s=a.prev;if(null!==s){a=s;continue}let o=a.parent;for(;null!==o;){let e=o.prev;if(null!==e){const t=e.key,n=l.get(t),s=e.end-e.start;if(f.add(t),void 0===n)c+=s;else{const e=n.end-n.start;e!==s&&(c+=s-e)}e=e.prev}o=o.parent}break}const u=n._firstNode;if(null!==u){a=s(u,e,r);let t=!1;for(;null!==a;){if(!f.has(a.key)){t=!0;break}a=a.parent}if(!t)for(;null!==a;){const e=a.key;if(!f.has(e)){const t=i.get(e),n=a.end-a.start;if(void 0===t)c-=n;else{const e=t.end-t.start;n!==e&&(c+=e-n)}}a=a.prev}}return c}function s(e,t,n){let s=e;for(;null!==s;){if(t<s.end+("element"!==s.type||0===n?1:0)){const e=s.child;if(null!==e){s=e;continue}return s}const e=s.next;if(null===e)break;s=e}return null}function o(e,t,n,s,o,r){return{child:e,end:s,key:o,next:null,parent:r,prev:null,start:n,type:t}}function r(t,n,s,r,f,c){const a=r.get(n);void 0===a&&function(e,...t){const n=new URL("https://lexical.dev/docs/error"),s=new URLSearchParams;s.append("code",e);for(const e of t)s.append("v",e);throw n.search=s.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}(3);const u=t.offset;if(e.$isElementNode(a)){const e=i(a,r),d=0===e.length,p=d?null:l(t,e,null,r,f,c);t.prevIsBlock&&!d||(t.prevIsBlock=!0,t.offset+=c);const g=o(p,"element",u,u,n,s);null!==p&&(p.parent=g);const h=t.offset;return g.end=h,f.set(n,g),g}t.prevIsBlock=!1;const d=e.$isTextNode(a),p=d?a.__text.length:1,g=o(null,d?"text":"inline",u,t.offset+=p,n,s);return f.set(n,g),g}function l(e,t,n,s,o,l){let i=null,f=null;const c=t.length;for(let a=0;a<c;a++){const c=r(e,t[a],n,s,o,l);null===f?i=c:(c.prev=f,f.next=c),f=c}return i}const i=e.$createChildrenArray;exports.$createChildrenArray=i,exports.$createOffsetView=function(e,n=1,s){const o=(s||e._pendingEditorState||e._editorState)._nodeMap,r=o.get("root"),f=new Map,c=l({offset:0,prevIsBlock:!1},i(r,o),null,o,f,n);return new t(f,c,n)},exports.OffsetView=t;