UNPKG

@lexical/selection

Version:

This package contains utilities and helpers for handling Lexical selection.

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