UNPKG

@lexical/selection

Version:

This package contains utilities and helpers for handling Lexical selection.

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