UNPKG

@lexical/selection

Version:

This package contains utilities and helpers for handling Lexical selection.

11 lines (9 loc) • 10.8 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{getRootOwnerDocument as e,$isTextNode as t,$getEditor as n,$isRootNode as o,$getSelection as l,$isRangeSelection as r,$caretRangeFromSelection as i,$isTokenOrSegmented as s,$isElementNode as c,$getCharacterOffsets as f,$cloneWithPropertiesEphemeral as u,$getNodeByKey as g,$getPreviousSelection as a,$createTextNode as d,getStyleObjectFromCSS as p,$caretFromPoint as h,$isExtendableTextPointCaret as y,$findMatchingParent as m,INTERNAL_$isBlock as S,flipDirection as x,$isTextPointCaret as C,$isTabNode as N,$extendCaretToRange as T,$isChildCaret as w,$isDecoratorNode as v,$isRootOrShadowRoot as P,$hasAncestor as K,$isLeafNode as E,$setSelection as I}from"lexical";export{$cloneWithProperties,$selectAll}from"lexical";function B(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.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.`)} /*@__INLINE__*/function k(e){let t=e;for(;null!=t;){if(t.nodeType===Node.TEXT_NODE)return t;t=t.firstChild}return null}function F(e){const t=e.parentNode;if(null==t)throw new Error("Should never happen");return[t,Array.from(t.childNodes).indexOf(e)]}function b(n,o,l,r,i){const s=o.getKey(),c=r.getKey(),f=e(n.getRootElement()).createRange();let u=n.getElementByKey(s),g=n.getElementByKey(c),a=l,d=i;if(t(o)&&(u=k(u)),t(r)&&(g=k(g)),void 0===o||void 0===r||null===u||null===g)return null;"BR"===u.nodeName&&([u,a]=F(u)),"BR"===g.nodeName&&([g,d]=F(g));const p=u.firstChild;u===g&&null!=p&&"BR"===p.nodeName&&0===a&&0===d&&(d=1);try{f.setStart(u,a),f.setEnd(g,d)}catch(e){return null}return!f.collapsed||a===d&&s===c||(f.setStart(g,d),f.setEnd(u,a)),f}function z(e,t){const n=e.getRootElement();if(null===n)return[];const o=n.getBoundingClientRect(),l=getComputedStyle(n),r=parseFloat(l.paddingLeft)+parseFloat(l.paddingRight),i=Array.from(t.getClientRects());let s,c=i.length;i.sort((e,t)=>{const n=e.top-t.top;return Math.abs(n)<=3?e.left-t.left:n});for(let e=0;e<c;e++){const t=i[e],n=s&&s.top<=t.top&&s.top+s.height>t.top&&s.left+s.width>t.left,l=t.width+r===o.width;n||l?(i.splice(e--,1),c--):s=t}return i}function A(e){let t="";for(const n in e)n&&(t+=`${n}: ${e[n]};`);return t}function R(e){const t=n().getElementByKey(e.getKey());if(null===t)return null;const o=t.ownerDocument.defaultView;return null===o?null:o.getComputedStyle(t)}function O(e){return R(o(e)?e:e.getParentOrThrow())}function _(e){const t=O(e);return null!==t&&"rtl"===t.direction}function L(e,t,n="self"){const o=e.getStartEndPoints();if(t.isSelected(e)&&!s(t)&&null!==o){const[l,r]=o,i=e.isBackward(),s=l.getNode(),c=r.getNode(),g=t.is(s),a=t.is(c);if(g||a){const[o,l]=f(e),r=s.is(c),g=t.is(i?c:s),a=t.is(i?s:c);let d,p=0;if(r)p=o>l?l:o,d=o>l?o:l;else if(g){p=i?l:o,d=void 0}else if(a){p=0,d=i?o:l}const h=t.__text.slice(p,d);h!==t.__text&&("clone"===n&&(t=u(t)),t.__text=h)}}return t}function M(e){if("text"===e.type)return e.offset===e.getNode().getTextContentSize();const t=e.getNode();return c(t)||B(177),e.offset===t.getChildrenSize()}function $(e,n,l){let i=n.getNode(),s=l;if(c(i)){const e=i.getDescendantByIndex(n.offset);null!==e&&(i=e)}for(;s>0&&null!==i;){if(c(i)){const e=i.getLastDescendant();null!==e&&(i=e)}let l=i.getPreviousSibling(),f=0;if(null===l){let e=i.getParentOrThrow(),t=e.getPreviousSibling();for(;null===t;){if(e=e.getParent(),null===e){l=null;break}t=e.getPreviousSibling()}null!==e&&(f=e.isInline()?0:2,l=t)}let u=i.getTextContent();""===u&&c(i)&&!i.isInline()&&(u="\n\n");const p=u.length;if(!t(i)||s>=p){const e=i.getParent();i.remove(),null==e||0!==e.getChildrenSize()||o(e)||e.remove(),s-=p+f,i=l}else{const o=i.getKey(),l=e.read("latest",()=>{const e=g(o);return t(e)&&e.isSimpleText()?e.getTextContent():null}),c=p-s,f=u.slice(0,c);if(null!==l&&l!==u){const e=a();let t=i;if(i.isSimpleText())i.setTextContent(l);else{const e=d(l);i.replace(e),t=e}if(r(e)&&e.isCollapsed()){const n=e.anchor.offset;t.select(n,n)}}else if(i.isSimpleText()){const e=n.key===o;let t=n.offset;t<s&&(t=p);const l=e?t-s:0,r=e?t:c;if(e&&0===l){const[e]=i.splitText(l,r);e.remove()}else{const[,e]=i.splitText(l,r);e.remove()}}else{const e=d(f);i.replace(e)}s=0}}}const D=()=>{};function U(e,n){(r(e)?e.isCollapsed():t(e)||c(e))||B(280);const o=p(r(e)?e.style:t(e)?e.getStyle():e.getTextStyle()),l=A(Object.entries(n).reduce((t,[n,l])=>("function"==typeof l?t[n]=l(o[n],e):null===l?delete t[n]:t[n]=l,t),{...o}));r(e)||t(e)?e.setStyle(l):e.setTextStyle(l)}function j(e,t){if(r(e)&&e.isCollapsed()){U(e,t);const n=e.anchor.getNode();c(n)&&n.isEmpty()&&U(n,t)}H(e=>{U(e,t)});const n=e.getNodes();if(n.length>0){const e=new Set;for(const o of n){if(!c(o)||!o.canBeEmpty()||0!==o.getChildrenSize())continue;const n=o.getKey();e.has(n)||(e.add(n),U(o,t))}}}function H(e){const n=l();if(!n)return;const o=new Map,c=e=>o.get(e.getKey())||[0,e.getTextContentSize()];if(r(n))for(const e of i(n).getTextSlices())e&&o.set(e.caret.origin.getKey(),e.getSliceIndices());const f=n.getNodes();for(const n of f){if(!t(n)||!n.canHaveFormat())continue;const[o,l]=c(n);if(l!==o)if(s(n)||0===o&&l===n.getTextContentSize())e(n);else{e(n.splitText(o,l)[0===o?0:1])}}r(n)&&"text"===n.anchor.type&&"text"===n.focus.type&&n.anchor.key===n.focus.key&&V(n)}function V(e){if(e.isBackward()){const{anchor:t,focus:n}=e,{key:o,offset:l,type:r}=t;t.set(n.key,n.offset,n.type),n.set(o,l,r)}}function W(e,t){const n=e.getFormatType(),o=e.getIndent();n!==t.getFormatType()&&t.setFormat(n),o!==t.getIndent()&&t.setIndent(o)}function X(e,t,n){let o=h(e,n);if(y(o))return!1;for(;o;o=o.getParentCaret()){const e=o.getParentAtCaret();if(!e||o.getNodeAtCaret())return!1;if(t.is(e))return!0}return!1}function q(e,t,n=W){if(!e)return;const o=e.getStartEndPoints();let l=!1,r=null;const i=new Map;if(o){const[t,n]=o,s=m(t.getNode(),S);r=m(n.getNode(),S);const f=e.isBackward()?"previous":"next";l=c(r)&&!r.is(s)&&function(e,t,n){const o=e.getNode();return(!c(o)||!o.isEmpty())&&X(e,t,n)}(n,r,x(f)),c(s)&&i.set(s.getKey(),s),c(r)&&!l&&i.set(r.getKey(),r)}for(const t of e.getNodes())if(c(t)&&S(t)){if(l&&t.is(r))continue;i.set(t.getKey(),t)}else if(!o){const e=m(t,S);c(e)&&i.set(e.getKey(),e)}for(const e of i.values()){const o=t();n(e,o),e.replace(o,!0)}}function G(e){return e.getNode().isAttached()}function J(e){let t=e;for(;null!==t&&!P(t);){const e=t.getLatest(),n=t.getParent();0===e.getChildrenSize()&&t.remove(!0),t=n}}function Q(e,t,n=null){const o=e.getStartEndPoints(),l=o?o[0]:null,r=e.getNodes(),i=r.length;if(null!==l&&(0===i||1===i&&"element"===l.type&&0===l.getNode().getChildrenSize())){const e="text"===l.type?l.getNode().getParentOrThrow():l.getNode(),o=e.getChildren();let r=t();return r.setFormat(e.getFormatType()),r.setIndent(e.getIndent()),o.forEach(e=>r.append(e)),n&&(r=n.append(r)),void e.replace(r)}let s=null,c=[];for(let o=0;o<i;o++){const l=r[o];P(l)?(Y(e,c,c.length,t,n),c=[],s=l):null===s||null!==s&&K(l,s)?c.push(l):(Y(e,c,c.length,t,n),c=[l])}Y(e,c,c.length,t,n)}function Y(e,t,n,o,l=null){if(0===t.length)return;const i=t[0],s=new Map,f=[],u=c(i)?i:i.getParentOrThrow();let g=u.isInline()?u.getParentOrThrow():u,d=!1;for(;null!==g;){const e=g.getPreviousSibling();if(null!==e){g=e,d=!0;break}if(g=g.getParentOrThrow(),P(g))break}const p=new Set;for(let e=0;e<n;e++){const n=t[e];c(n)&&0===n.getChildrenSize()&&p.add(n.getKey())}const h=new Set;for(let e=0;e<n;e++){const n=t[e];let l=n.getParent();if(null!==l&&l.isInline()&&(l=l.getParent()),null!==l&&E(n)&&!h.has(n.getKey())){const e=l.getKey();if(void 0===s.get(e)){const t=o();t.setFormat(l.getFormatType()),t.setIndent(l.getIndent()),f.push(t),s.set(e,t);const n=l.getChildren();t.splice(t.getChildrenSize(),0,n);for(const e of n)if(h.add(e.getKey()),c(e))for(const t of e.getChildrenKeys())h.add(t);J(l)}}else if(p.has(n.getKey())){c(n)||B(179);const e=o();e.setFormat(n.getFormatType()),e.setIndent(n.getIndent()),f.push(e),n.remove(!0)}}if(null!==l)for(let e=0;e<f.length;e++){const t=f[e];l.append(t)}let y=null;if(P(g))if(d)if(null!==l)g.insertAfter(l);else for(let e=f.length-1;e>=0;e--){const t=f[e];g.insertAfter(t)}else{const e=g,t=e.getFirstChild();if(c(t)&&(g=t),null===t)if(l)e.append(l);else for(let t=0;t<f.length;t++){const n=f[t];e.append(n),y=n}else if(null!==l)t.insertBefore(l);else for(let e=0;e<f.length;e++){const n=f[e];t.insertBefore(n),y=n}}else if(l)g.insertAfter(l);else for(let e=f.length-1;e>=0;e--){const t=f[e];g.insertAfter(t),y=t}const m=a();r(m)&&G(m.anchor)&&G(m.focus)?I(m.clone()):null!==y?y.selectEnd():e.dirty=!0}function Z(e){const t=ee(e);return null!==t&&"vertical-rl"===t.writingMode}function ee(e){const t=e.anchor.getNode();return c(t)?R(t):O(t)}function te(e,n){let o=Z(e)?!n:n;oe(e)&&(o=!o);const l=h(e.focus,o?"previous":"next");if(y(l))return!1;if(C(l)&&!N(l.origin)&&l.origin.isUnmergeable()){const e=l.getNodeAtCaret();if(t(e)&&!N(e))return!0}for(const e of T(l)){if(w(e))return!e.origin.isInline();if(!c(e.origin)){if(v(e.origin))return!0;break}}return!1}function ne(e,t,n,o){e.modify(t?"extend":"move",n,o)}function oe(e){const t=ee(e);return null!==t&&"rtl"===t.direction}function le(e,t,n){const o=oe(e);let l;l=Z(e)||o?!n:n,ne(e,t,l,"character")}function re(e,t,n){const o=e.getStyle(),l=p(o);return null!==l&&l[t]||n}function ie(e,n,o=""){let l=null;const i=e.getNodes();let s,c;if(r(e)){if(e.isCollapsed()&&""!==e.style){const t=p(e.style);if(null!==t&&n in t)return t[n]}const{anchor:o,focus:l}=e,r=e.isBackward(),i=r?l.getNode():o.getNode(),f=r?o.getNode():l.getNode(),u=r?l.offset:o.offset,g=r?o.offset:l.offset;t(i)&&u===i.getTextContentSize()&&(s=i),0===g&&(c=f)}for(let e=0;e<i.length;e++){const r=i[e];if(t(r)&&!r.is(0===e?s:c)){const e=re(r,n,o);if(null===l)l=e;else if(l!==e){l="";break}}}return null===l?o:l}const se=p,ce=$;export{D as $addNodeStyle,W as $copyBlockFormatIndent,V as $ensureForwardRangeSelection,H as $forEachSelectedTextNode,R as $getComputedStyleForElement,O as $getComputedStyleForParent,ie as $getSelectionStyleValueForProperty,X as $isAtEdgeOfElement,M as $isAtNodeEnd,oe as $isParentElementRTL,_ as $isParentRTL,ne as $moveCaretSelection,le as $moveCharacter,j as $patchStyleText,q as $setBlocksType,te as $shouldOverrideDefaultCharacterSelection,L as $sliceSelectedTextNodeContent,$ as $trimTextContentFromAnchor,Q as $wrapNodes,b as createDOMRange,z as createRectsFromDOMRange,A as getCSSFromStyleObject,se as getStyleObjectFromCSS,ce as trimTextContentFromAnchor};