UNPKG

@lexical/selection

Version:

This package contains utilities and helpers for handling Lexical selection.

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