UNPKG

@lexical/utils

Version:

This package contains misc utilities for Lexical.

10 lines (8 loc) • 13.4 kB
/** * 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. * */ import{createRectsFromDOMRange as t,$isAtEdgeOfElement as e}from"@lexical/selection";import{getRootOwnerDocument as n,isHTMLElement as o,mergeRegister as r,$getSelection as i,$isRangeSelection as l,$isElementNode as s,$isTextNode as u,$getDOMTextNode as c,getDOMTextNode as f,$getDOMSlot as d,registerEventListener as a,getDOMSelectionPoints as g,getParentElement as p,$isSlotHost as h,$getSlotNames as m,$getSlot as y,$getChildCaret as v,$getSlotHost as E,$findMatchingParent as w,$getChildCaretOrSelf as C,$getSiblingCaret as x,$getAdjacentSiblingOrParentSiblingCaret as S,$caretRangeFromSelection as N,$getCaretRangeInDirection as A,$removeTextFromCaretRange as b,$isTextPointCaret as R,$splitAtPointCaretNext as _,$setSelectionFromCaretRange as I,$getCaretRange as L,$getPreviousSelection as P,$caretFromPoint as D,$getRoot as B,$createParagraphNode as O,$insertNodeToNearestRootAtCaret as T,$getAdjacentChildCaret as M,$isChildCaret as F,$normalizeCaret as K,$getCollapsedCaretRange as $,$getSlotFrame as H,$getCaretInDirection as k,$comparePointCaretNext as U,$cloneWithProperties as j,$fullReconcile as z,$setSelection as V,$rewindSiblingCaret as W,makeStepwiseIterator as X,$isSiblingCaret as G,$getState as Y,$setState as q,IS_FIREFOX as J,CAN_USE_DOM as Q}from"lexical";export{$findMatchingParent,$getAdjacentSiblingOrParentSiblingCaret,$insertNodeToNearestRootAtCaret,$splitNode,CAN_USE_BEFORE_INPUT,CAN_USE_DOM,IS_ANDROID,IS_ANDROID_CHROME,IS_APPLE,IS_APPLE_WEBKIT,IS_CHROME,IS_FIREFOX,IS_IOS,IS_SAFARI,addClassNamesToElement,isBlockDomNode,isHTMLAnchorElement,isHTMLElement,isInlineDomNode,mergeRegister,removeClassNamesFromElement}from"lexical";function Z(t,...e){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",t);for(const t of e)o.append("v",t);throw n.search=o.toString(),Error(`Minified Lexical error #${t}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}function tt(t){const e=(t,e)=>e.left>=t.left-1&&e.top>=t.top-1&&e.right<=t.right+1&&e.bottom<=t.bottom+1,n=[];for(const o of Array.from(t))if(!(o.width<.5||o.height<.5||n.some(t=>e(o,t)))){for(let t=n.length-1;t>=0;t--)e(n[t],o)&&n.splice(t,1);n.push(o)}return n}function et(t){return`${t}px`}const nt={attributes:!0,characterData:!0,childList:!0,subtree:!0};function ot(e,r,i){let l=null,s=null,u=null,c=[];const f=n(e.getRootElement()).createElement("div");function d(){null===l&&Z(182),null===s&&Z(183);const{left:o,top:u}=s.getBoundingClientRect(),d=tt(t(e,r));var a,g;f.isConnected||(g=f,(a=s).insertBefore(g,a.firstChild));let p=!1;for(let t=0;t<d.length;t++){const e=d[t],r=c[t]||n(l).createElement("div"),i=r.style;"absolute"!==i.position&&(i.position="absolute",p=!0);const s=et(e.left-o);i.left!==s&&(i.left=s,p=!0);const a=et(e.top-u);i.top!==a&&(r.style.top=a,p=!0);const g=et(e.width);i.width!==g&&(r.style.width=g,p=!0);const h=et(e.height);i.height!==h&&(r.style.height=h,p=!0),r.parentNode!==f&&(f.append(r),p=!0),c[t]=r}for(;c.length>d.length;){const t=c.pop();null!=t&&t.remove()}p&&i(c)}function a(){s=null,l=null,null!==u&&u.disconnect(),u=null,f.remove();for(const t of c)t.remove();c=[]}f.style.position="relative";const g=e.registerRootListener(function t(){const n=e.getRootElement();if(null===n)return a();const r=n.parentElement;if(!o(r))return a();a(),l=n,s=r,u=new MutationObserver(n=>{const o=e.getRootElement(),r=o&&o.parentElement;if(o!==l||r!==s)return t();for(const t of n)if(!f.contains(t.target))return d()}),u.observe(r,nt),d()});return()=>{g(),a()}}function rt(t,e,n,o){if("text"!==e.type&&s(n)){const r=d(n,o,t);return[r.element,r.getFirstChildOffset()+e.offset]}return[(u(n)?c(n,o,t):f(o))||o,e.offset]}function it(t){for(const e of t){const t=e.style;"Highlight"!==t.background&&(t.background="Highlight"),"HighlightText"!==t.color&&(t.color="HighlightText"),t.marginTop!==et(-1.5)&&(t.marginTop=et(-1.5)),t.paddingTop!==et(4)&&(t.paddingTop=et(4)),t.paddingBottom!==et(0)&&(t.paddingBottom=et(0))}}function lt(t,e=it){let n=null,o=null,s=null,u=null,c=null,f=null,d=()=>{};function a(r){r.read(()=>{const r=i();if(!l(r))return n=null,s=null,u=null,f=null,d(),void(d=()=>{});const[a,g]=function(t){const e=t.getStartEndPoints();return t.isBackward()?[e[1],e[0]]:e}(r),p=a.getNode(),h=p.getKey(),m=a.offset,y=g.getNode(),v=y.getKey(),E=g.offset,w=t.getElementByKey(h),C=t.getElementByKey(v),x=null===n||w!==o||m!==s||h!==n.getKey(),S=null===u||C!==c||E!==f||v!==u.getKey();if((x||S)&&null!==w&&null!==C){const n=function(t,e,n,o,r,i,l){const s=(t._window?t._window.document:document).createRange();return s.setStart(...rt(t,e,n,o)),s.setEnd(...rt(t,r,i,l)),s}(t,a,p,w,g,y,C);d(),d=ot(t,n,e)}n=p,o=w,s=m,u=y,c=C,f=E},{editor:t})}return a(t.getEditorState()),r(t.registerUpdateListener(({editorState:t})=>a(t)),()=>{d()})}function st(t,e){let n=null;const o=()=>{const o=t.getRootElement(),r=null!==o?o.ownerDocument.defaultView:null,i=null!==r?r.getSelection():null,l=null!==i?g(i,o).anchorNode:null;null!==l&&null!==o&&o.contains(l)?null!==n&&(n(),n=null):null===n&&(n=lt(t,e))};return t.registerRootListener(t=>{if(t){const e=t.ownerDocument,i=r(a(e,"selectionchange",o),()=>{null!==n&&n()});return o(),i}})}function ut(t,e){const n=t.ownerDocument,o=n.defaultView||window;let r=o.getComputedStyle(t);const i="absolute"===r.position,l=e?/(auto|scroll|hidden)/:/(auto|scroll)/;if("fixed"===r.position)return n.body;for(let e=t;e=p(e);)if(r=o.getComputedStyle(e),(!i||"static"!==r.position)&&l.test(r.overflow+r.overflowY+r.overflowX))return e;return n.body}function ct(t,e){for(const n of e)if(t.type.startsWith(n))return!0;return!1}function ft(t,e){const n=t[Symbol.iterator]();return new Promise((t,o)=>{const r=[],i=()=>{const{done:l,value:s}=n.next();if(l)return t(r);const u=new FileReader;u.addEventListener("error",o),u.addEventListener("load",()=>{const t=u.result;"string"==typeof t&&r.push({file:s,result:t}),i()}),ct(s,e)?u.readAsDataURL(s):i()};i()})}function dt(t,e){return Array.from(pt(t,e))}function at(t){return t?t.getAdjacentCaret():null}function gt(t,e){return Array.from(St(t,e))}function pt(t,e){return Et("next",t,e)}function ht(t,e){return Array.from(mt(t,e))}function*mt(t,e){for(const n of Et("next",t,e)){yield n;const{node:t,depth:o}=n;if(h(t)&&!t.is(e))for(const e of m(t)){const n=y(t,e);null!==n&&(yield*yt(n,o+1))}}}function*yt(t,e){yield{depth:e,node:t};const n=e+1;if(h(t))for(const e of m(t)){const o=y(t,e);null!==o&&(yield*yt(o,n))}if(s(t))for(const e of t.getChildren())yield*yt(e,n)}function vt(t,e){const n=S(x(t,e));return n&&n[0]}function Et(t,e,n){const o=B(),r=e||o,i=s(r)?v(r,t):x(r,t),l=Ct(r),u=n?M(C(x(n,t)))||vt(n,t):vt(r,t);let c=l;return X({hasNext:t=>null!==t,initial:i,map:t=>({depth:c,node:t.origin}),step:t=>{if(t.isSameNodeCaret(u))return null;F(t)&&c++;const e=S(t);return!e||e[0].isSameNodeCaret(u)?null:(c+=e[1],e[0])}})}function wt(t){const e=S(x(t,"next"));return e&&[e[0].origin,e[1]]}function Ct(t){let e=-1;for(let n=t;null!==n;n=n.getParent()??E(n))e++;return e}function xt(t){const e=C(x(t,"previous")),n=S(e,"root");return n&&n[0].origin}function St(t,e){return Et("previous",t,e)}function Nt(t,e){return Array.from(At(t,e))}function*At(t,e){const n=[];for(const o of Et("previous",t,e)){for(;n.length>0&&o.depth<=n[n.length-1].depth;){const t=n.pop();yield*bt(t.node,t.depth+1)}yield o;const{node:t,depth:r}=o;h(t)&&m(t).length>0&&!t.is(e)&&n.push({depth:r,node:t})}for(;n.length>0;){const t=n.pop();yield*bt(t.node,t.depth+1)}}function*bt(t,e){const n=m(t);for(let o=n.length-1;o>=0;o--){const r=y(t,n[o]);null!==r&&(yield*Rt(r,e))}}function*Rt(t,e){yield{depth:e,node:t};const n=e+1;if(s(t)){const e=t.getChildren();for(let t=e.length-1;t>=0;t--)yield*Rt(e[t],n)}h(t)&&(yield*bt(t,n))}function _t(t,e){let n=t;for(;null!=n;){if(n instanceof e)return n;n=n.getParent()}return null}function It(t){const e=w(t,t=>s(t)&&!t.isInline());return s(e)||Z(4,t.__key),e}function Lt(t,e){const n=A(l(e)?N(e):e,"next"),o=H(n.anchor.origin),r=H(t.getLatest());if(null===o?null!==r:!o.is(r))return!1;const i=K(v(t,"next")),s=k(K(v(t,"previous")),"next");return U(n.anchor,i)<=0&&U(n.focus,s)>=0}function Pt(t,e,n,o){const r=t=>t instanceof e;return t.registerNodeTransform(e,t=>{const e=(t=>{const e=t.getChildren();for(let t=0;t<e.length;t++){const n=e[t];if(r(n))return null}let n=t,o=t;for(;null!==n;)if(o=n,n=n.getParent(),r(n))return{child:o,parent:n};return null})(t);if(null!==e){const{child:r,parent:i}=e;if(r.is(t)){o(i,t);const e=r.getNextSiblings(),l=e.length;if(i.insertAfter(r),0!==l){const t=n(i);r.insertAfter(t);for(let n=0;n<l;n++)t.append(e[n])}i.canBeEmpty()||0!==i.getChildrenSize()||i.remove()}}})}function Dt(t,e){const n=new Map,o=t._pendingEditorState;for(const[t,o]of e._nodeMap)n.set(t,j(o));o&&(o._nodeMap=n),z();const r=e._selection;V(null===r?null:r.clone())}function Bt(t){const e=i()||P();let n;if(l(e))n=D(e.focus,"next");else{if(null!=e){const t=e.getNodes(),o=t[t.length-1];o&&(n=x(o,"next"))}n=n||v(B(),"previous").getFlipped().insert(O())}const o=T(t,n),r=M(o),s=F(r)?K(r):o;return I($(s)),t.getLatest()}function Ot(t){const e=i();if(!l(e))return void(e&&e.insertNodes([t]));const n=N(e);let o=A(b(n),"next").anchor;if(R(o)){const t=_(o);if(!t)return;o=t}const r=o.getFlipped();r.insert(t),I(L(r,r))}function Tt(t,e){const n=e();return t.replace(n),n.append(t),n}function Mt(t,e){return null!==t&&Object.getPrototypeOf(t).constructor.name===e.name}function Ft(t){let e=null;if(Mt(t,DragEvent)?e=t.dataTransfer:Mt(t,ClipboardEvent)&&(e=t.clipboardData),null===e)return[!1,[],!1];const n=e.types,o=n.includes("Files"),r=n.includes("text/html")||n.includes("text/plain");return[o,Array.from(e.files),r]}function Kt(t,e){const n=[];for(let o=0;o<t.length;o++){const r=e(t[o]);null!==r&&n.push(r)}return n}function $t(t){const e=i();if(!l(e))return!1;const n=new Set,o=e.getNodes();for(let e=0;e<o.length;e++){const r=o[e],i=r.getKey();if(n.has(i))continue;const l=w(r,t=>s(t)&&!t.isInline());if(null===l)continue;const u=l.getKey();l.canIndent()&&!n.has(u)&&(n.add(u),t(l))}return n.size>0}function Ht(t,e){v(t,"next").insert(e)}let kt=!(J||!Q)&&void 0;function Ut(t,e=!1){let n=1;if(function(){if(void 0===kt){const t=document.createElement("div");t.style.position="absolute",t.style.opacity="0",t.style.width="100px",t.style.left="-1000px",document.body.appendChild(t);const e=t.getBoundingClientRect();t.style.setProperty("zoom","2"),kt=t.getBoundingClientRect().width===e.width,document.body.removeChild(t)}return kt}()||e){const e=t&&t.ownerDocument.defaultView||window;for(;t;)n*=Number(e.getComputedStyle(t).getPropertyValue("zoom")),t=p(t)}return n}function jt(t){return null!==t._parentEditor}function zt(t,e){return Vt(t,e,null)}function Vt(t,e,n){let o=!1;for(const r of Gt(t))e(r)?null!==n&&n(r):(o=!0,s(r)&&Vt(r,e,n||(t=>r.insertAfter(t))),r.remove());return o}function Wt(t,e){const n=[],o=Array.from(t).reverse();for(let t=o.pop();void 0!==t;t=o.pop())if(e(t))n.push(t);else if(s(t))for(const e of Gt(t))o.push(e);return n}function Xt(t){return Yt(v(t,"next"))}function Gt(t){return Yt(v(t,"previous"))}function Yt(t){return X({hasNext:G,initial:t.getAdjacentCaret(),map:t=>t.origin.getLatest(),step:t=>t.getAdjacentCaret()})}function qt(t){W(x(t,"next")).splice(1,t.getChildren())}function Jt(t){const e=e=>Y(e,t),n=(e,n)=>q(e,t,n);return{$get:e,$set:n,accessors:[e,n],makeGetterMethod:()=>function(){return e(this)},makeSetterMethod:()=>function(t){return n(this,t)},stateConfig:t}}function Qt(t,e){const n=i();if(l(n)&&n.isCollapsed()){const o=w(n.anchor.getNode(),t);if(o){const t=o.getParent();if(null!==t&&t.getFirstChild()===o&&te(n.anchor,o))return o.insertBefore(O()).selectEnd(),e&&e.preventDefault(),!0}}return!1}function Zt(t,e){const n=i();if(l(n)&&n.isCollapsed()){const o=w(n.anchor.getNode(),t);if(o){const t=o.getParent();if(null!==t&&t.getLastChild()===o&&ee(n.anchor,o))return o.insertAfter(O()).selectEnd(),e&&e.preventDefault(),!0}}return!1}function te(t,n){return e(t,n,"previous")}function ee(t,n){return e(t,n,"next")}export{Wt as $descendantsMatching,dt as $dfs,pt as $dfsIterator,ht as $dfsWithSlots,mt as $dfsWithSlotsIterator,Kt as $filter,Xt as $firstToLastIterator,at as $getAdjacentCaret,Ct as $getDepth,It as $getNearestBlockElementAncestorOrThrow,_t as $getNearestNodeOfType,xt as $getNextRightPreorderNode,wt as $getNextSiblingOrParentSibling,$t as $handleIndentAndOutdent,Ht as $insertFirst,Ot as $insertNodeIntoLeaf,Bt as $insertNodeToNearestRoot,ee as $isAtEndOfNode,te as $isAtStartOfNode,Lt as $isBlockFullySelected,jt as $isEditorIsNestedEditor,Gt as $lastToFirstIterator,Zt as $onEscapeDown,Qt as $onEscapeUp,Dt as $restoreEditorState,gt as $reverseDfs,St as $reverseDfsIterator,Nt as $reverseDfsWithSlots,At as $reverseDfsWithSlotsIterator,zt as $unwrapAndFilterDescendants,qt as $unwrapNode,Tt as $wrapNodeInElement,Ut as calculateZoomLevel,tt as dedupeSelectionRects,Ft as eventFiles,ut as getScrollParent,ct as isMimeType,Jt as makeStateWrapper,lt as markSelection,ft as mediaFileReader,Mt as objectKlassEquals,ot as positionNodeOnRange,Pt as registerNestedElementResolver,st as selectionAlwaysOnDisplay};