Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@lexical/selection

Package Overview
Dependencies
Maintainers
5
Versions
192
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lexical/selection - npm Package Compare versions

Comparing version 0.2.9 to 0.3.0

24

LexicalSelection.d.ts

@@ -7,4 +7,4 @@ /**

*
* @flow strict
*/
import type {

@@ -19,11 +19,15 @@ ElementNode,

RangeSelection,
TextNode,
} from 'lexical';
export function $cloneContents(
export function $cloneContents<T extends LexicalNode>(
selection: RangeSelection | NodeSelection | GridSelection,
): {
nodeMap: Array<[NodeKey, LexicalNode]>;
nodeMap: Array<[NodeKey, T]>;
range: Array<NodeKey>;
};
export function $cloneWithProperties<LexicalNode>(
node: LexicalNode,
export function $cloneWithProperties<T extends LexicalNode>(node: T): T;
export function $sliceSelectedTextNodeContent(
selection: RangeSelection | GridSelection | NodeSelection,
node: TextNode,
): LexicalNode;

@@ -68,3 +72,3 @@ export function getStyleObjectFromCSS(css: string): {

declare function createDOMRange(
export declare function createDOMRange(
editor: LexicalEditor,

@@ -77,5 +81,11 @@ anchorNode: LexicalNode,

declare function createRectsFromDOMRange(
export declare function createRectsFromDOMRange(
editor: LexicalEditor,
range: Range,
): Array<ClientRect>;
export declare function trimTextContentFromAnchor(
editor: LexicalEditor,
anchor: Point,
delCount: number,
): void;

@@ -17,3 +17,2 @@ /**

*
*
*/

@@ -23,3 +22,4 @@ const cssToStyles = new Map();

const latest = node.getLatest();
const constructor = latest.constructor;
const constructor = latest.constructor; // @ts-expect-error
const clone = constructor.clone(latest);

@@ -38,5 +38,4 @@ clone.__parent = latest.__parent;

clone.__detail = latest.__detail;
} // $FlowFixMe
}
return clone;

@@ -115,3 +114,3 @@ }

function $cloneContents(selection) {
const clone = $cloneContentsImpl(selection);
const clone = $cloneContentsImpl(selection); // @ts-ignore

@@ -122,3 +121,3 @@ {

for (let i = 0; i < nodeMap.length; i++) {
const node = nodeMap[i][1]; // $FlowFixMe[method-unbinding]
const node = nodeMap[i][1];

@@ -322,3 +321,3 @@ if (node.getLatest === errGetLatestOnClone) {

// the entire first node isn't selected, so split it
[, firstNode] = firstNode.splitText(startOffset);
firstNode = firstNode.splitText(startOffset)[1];
startOffset = 0;

@@ -435,3 +434,2 @@ }

lastNode = lastNode.getParentOrThrow();
lastOffset = lastNode.getChildrenSize();
}

@@ -625,4 +623,3 @@

while (node != null) {
if (node.nodeType === 3) {
// $FlowFixMe: this is a Text
if (node.nodeType === Node.TEXT_NODE) {
return node;

@@ -733,2 +730,169 @@ }

function doesContainGrapheme(str) {
return /[\uD800-\uDBFF][\uDC00-\uDFFF]/g.test(str);
}
function trimTextContentFromAnchor(editor, anchor, delCount) {
// Work from the current selection anchor point
let currentNode = anchor.getNode();
let remaining = delCount;
if (lexical.$isElementNode(currentNode)) {
const descendantNode = currentNode.getDescendantByIndex(anchor.offset);
if (descendantNode !== null) {
currentNode = descendantNode;
}
}
while (remaining > 0 && currentNode !== null) {
let nextNode = currentNode.getPreviousSibling();
let additionalElementWhitespace = 0;
if (nextNode === null) {
let parent = currentNode.getParentOrThrow();
let parentSibling = parent.getPreviousSibling();
while (parentSibling === null) {
parent = parent.getParent();
if (parent === null) {
nextNode = null;
break;
}
parentSibling = parent.getPreviousSibling();
}
if (parent !== null) {
additionalElementWhitespace = parent.isInline() ? 0 : 2;
if (lexical.$isElementNode(parentSibling)) {
nextNode = parentSibling.getLastDescendant();
} else {
nextNode = parentSibling;
}
}
}
let text = currentNode.getTextContent(); // If the text is empty, we need to consider adding in two line breaks to match
// the content if we were to get it from its parent.
if (text === '' && lexical.$isElementNode(currentNode) && !currentNode.isInline()) {
// TODO: should this be handled in core?
text = '\n\n';
}
const textNodeSize = text.length;
const offset = textNodeSize - remaining;
const slicedText = text.slice(0, offset); // Sometimes the text we're putting in might be a partial grapheme.
// So we just remove the entire thing, rather than show a partial unicode grapheme.
const containsPartialGraphemeHeuristic = doesContainGrapheme(text) && !doesContainGrapheme(slicedText);
if (!lexical.$isTextNode(currentNode) || remaining >= textNodeSize || containsPartialGraphemeHeuristic) {
const parent = currentNode.getParent();
currentNode.remove();
if (parent.getChildrenSize() === 0) {
parent.remove();
}
remaining -= textNodeSize + additionalElementWhitespace;
currentNode = nextNode;
} else {
const key = currentNode.getKey(); // See if we can just revert it to what was in the last editor state
const prevTextContent = editor.getEditorState().read(() => {
const prevNode = lexical.$getNodeByKey(key);
if (lexical.$isTextNode(prevNode) && prevNode.isSimpleText()) {
return prevNode.getTextContent();
}
return null;
});
if (prevTextContent !== null && prevTextContent !== text) {
const prevSelection = lexical.$getPreviousSelection();
let target = currentNode;
if (!currentNode.isSimpleText()) {
const textNode = lexical.$createTextNode(prevTextContent);
currentNode.replace(textNode);
target = textNode;
} else {
currentNode.setTextContent(prevTextContent);
}
if (lexical.$isRangeSelection(prevSelection) && prevSelection.isCollapsed()) {
const prevOffset = prevSelection.anchor.offset;
target.select(prevOffset, prevOffset);
}
} else if (currentNode.isSimpleText()) {
// Split text
const isSelected = anchor.key === key;
let anchorOffset = anchor.offset; // Move offset to end if it's less than the remaniing number, otherwise
// we'll have a negative splitStart.
if (anchorOffset < remaining) {
anchorOffset = textNodeSize;
}
const splitStart = isSelected ? anchorOffset - remaining : 0;
const splitEnd = isSelected ? anchorOffset : offset;
if (isSelected && splitStart === 0) {
const [excessNode] = currentNode.splitText(splitStart, splitEnd);
excessNode.remove();
} else {
const [, excessNode] = currentNode.splitText(splitStart, splitEnd);
excessNode.remove();
}
} else {
const textNode = lexical.$createTextNode(slicedText);
currentNode.replace(textNode);
}
remaining = 0;
}
}
}
function $sliceSelectedTextNodeContent(selection, textNode) {
if (textNode.isSelected() && !textNode.isSegmented() && !textNode.isToken() && (lexical.$isRangeSelection(selection) || lexical.$isGridSelection(selection))) {
const anchorNode = selection.anchor.getNode();
const focusNode = selection.focus.getNode();
const isAnchor = textNode.is(anchorNode);
const isFocus = textNode.is(focusNode);
if (isAnchor || isFocus) {
const isBackward = selection.isBackward();
const [anchorOffset, focusOffset] = selection.getCharacterOffsets();
const isSame = anchorNode.is(focusNode);
const isFirst = textNode.is(isBackward ? focusNode : anchorNode);
const isLast = textNode.is(isBackward ? anchorNode : focusNode);
let startOffset = 0;
let endOffset = undefined;
if (isSame) {
startOffset = anchorOffset > focusOffset ? focusOffset : anchorOffset;
endOffset = anchorOffset > focusOffset ? anchorOffset : focusOffset;
} else if (isFirst) {
const offset = isBackward ? focusOffset : anchorOffset;
startOffset = offset;
endOffset = undefined;
} else if (isLast) {
const offset = isBackward ? anchorOffset : focusOffset;
startOffset = 0;
endOffset = offset;
}
textNode.__text = textNode.__text.slice(startOffset, endOffset);
return textNode;
}
}
return textNode;
}
exports.$cloneContents = $cloneContents;

@@ -744,2 +908,3 @@ exports.$cloneWithProperties = $cloneWithProperties;

exports.$shouldOverrideDefaultCharacterSelection = $shouldOverrideDefaultCharacterSelection;
exports.$sliceSelectedTextNodeContent = $sliceSelectedTextNodeContent;
exports.$wrapLeafNodesInElements = $wrapLeafNodesInElements;

@@ -749,1 +914,2 @@ exports.createDOMRange = createDOMRange;

exports.getStyleObjectFromCSS = getStyleObjectFromCSS;
exports.trimTextContentFromAnchor = trimTextContentFromAnchor;

@@ -7,19 +7,23 @@ /**

*/
var l=require("lexical");const t=new Map;function u(a){a=a.getLatest();const c=a.constructor.clone(a);c.__parent=a.__parent;l.$isElementNode(a)&&l.$isElementNode(c)?(c.__children=Array.from(a.__children),c.__format=a.__format,c.__indent=a.__indent,c.__dir=a.__dir):l.$isTextNode(a)&&l.$isTextNode(c)&&(c.__format=a.__format,c.__style=a.__style,c.__mode=a.__mode,c.__detail=a.__detail);return c}
function w(a,c,b,g,k,f){for(var d=c;null!==a;){for(c=a.getParent();null!==c&&c.excludeFromCopy("clone");)c=c.getParent();if(null===c)break;if(!l.$isElementNode(a)||!a.excludeFromCopy("clone")){const h=a.getKey();let e=f.get(h);const m=void 0===e;m&&(e=u(a),f.set(h,e));!l.$isTextNode(e)||e.isSegmented()||e.isToken()?l.$isElementNode(e)&&(e.__children=e.__children.slice(g?d:0,g?void 0:(d||0)+1)):e.__text=e.__text.slice(g?d:0,g?b:d);if(l.$isRootNode(c)){m&&k.push(h);break}}d=f.get(c.getKey());d=l.$isElementNode(d)?
'use strict';var l=require("lexical");let t=new Map;function u(a){a=a.getLatest();let c=a.constructor.clone(a);c.__parent=a.__parent;l.$isElementNode(a)&&l.$isElementNode(c)?(c.__children=Array.from(a.__children),c.__format=a.__format,c.__indent=a.__indent,c.__dir=a.__dir):l.$isTextNode(a)&&l.$isTextNode(c)&&(c.__format=a.__format,c.__style=a.__style,c.__mode=a.__mode,c.__detail=a.__detail);return c}
function w(a,c,b,e,f,g){for(var d=c;null!==a;){for(c=a.getParent();null!==c&&c.excludeFromCopy("clone");)c=c.getParent();if(null===c)break;if(!l.$isElementNode(a)||!a.excludeFromCopy("clone")){let k=a.getKey(),h=g.get(k),m=void 0===h;m&&(h=u(a),g.set(k,h));!l.$isTextNode(h)||h.isSegmented()||h.isToken()?l.$isElementNode(h)&&(h.__children=h.__children.slice(e?d:0,e?void 0:(d||0)+1)):h.__text=h.__text.slice(e?d:0,e?b:d);if(l.$isRootNode(c)){m&&f.push(k);break}}d=g.get(c.getKey());d=l.$isElementNode(d)?
d.__children.indexOf(a.getKey()):a.getIndexWithinParent();a=c}}
function x(a){if(l.$isRangeSelection(a)){var c=a.anchor,b=a.focus;const [e,m]=a.getCharacterOffsets();a=a.getNodes();if(0===a.length)return{nodeMap:[],range:[]};let n=a.length;var g=a[0],k=g.getParent();if(null!==k&&(!k.canBeEmpty()||l.$isRootNode(k))){var f=k.__children;if(f.length===n){var d=!0;for(var h=0;h<f.length;h++)if(f[h]!==a[h].__key){d=!1;break}d&&(n++,a.push(k))}}k=a[n-1];c=c.isBefore(b);b=new Map;f=[];d=l.$isTextNode(g)&&1===n;w(g,c?e:m,d?c?m:e:void 0,!0,f,b);for(g=0;g<n;g++){h=a[g];
const r=h.getKey();if(!(b.has(r)||l.$isElementNode(h)&&h.excludeFromCopy("clone"))){const p=u(h);l.$isRootNode(h.getParent())&&f.push(h.getKey());"root"!==r&&b.set(r,p)}}w(k,d?void 0:c?m:e,void 0,!1,f,b);return{nodeMap:Array.from(b.entries()),range:f}}if(l.$isGridSelection(a))return{nodeMap:a.getNodes().map(e=>{const m=e.getKey();e=u(e);return[m,e]}),range:[a.gridKey]};throw Error("Minified Lexical error #1; see codes.json for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");
}function y(a){return t.get(a)||null}function z(a,c){var b=y(a.getStyle());c=b?{...b,...c}:c;b="";for(g in c)g&&(b+=`${g}: ${c[g]};`);var g=b;a.setStyle(g);t.set(g,c)}function A(a,c,b,g){a.modify(c?"extend":"move",b,g)}function B(a){a=a.anchor.getNode();return"rtl"===(l.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}function C(a){for(;null!==a&&!l.$isRootNode(a);){const c=a.getLatest(),b=a.getParent();0===c.__children.length&&a.remove(!0);a=b}}
function D(a){for(;null!=a;){if(3===a.nodeType)return a;a=a.firstChild}return null}function E(a){const c=a.parentNode;if(null==c)throw Error("Should never happen");return[c,Array.from(c.childNodes).indexOf(a)]}exports.$cloneContents=function(a){return x(a)};exports.$cloneWithProperties=u;
exports.$getSelectionStyleValueForProperty=function(a,c,b=""){let g=null;const k=a.getNodes();var f=a.anchor,d=a.focus,h=a.isBackward();a=h?d.offset:f.offset;f=h?d.getNode():f.getNode();for(d=0;d<k.length;d++){var e=k[d];if((0===d||0!==a||!e.is(f))&&l.$isTextNode(e)){h=c;var m=b;e=e.getStyle();e=y(e);h=null!==e?e[h]||m:m;if(null===g)g=h;else if(g!==h){g="";break}}}return null===g?b:g};exports.$isAtNodeEnd=function(a){return"text"===a.type?a.offset===a.getNode().getTextContentSize():a.offset===a.getNode().getChildrenSize()};
exports.$isParentElementRTL=B;exports.$moveCaretSelection=A;exports.$moveCharacter=function(a,c,b){const g=B(a);A(a,c,b?!g:g,"character")};
exports.$patchStyleText=function(a,c){var b=a.getNodes();const g=b.length-1;let k=b[0],f=b[g];if(!a.isCollapsed()){var d=a.anchor,h=a.focus;a=k.getTextContent().length;var e=h.offset,m=d.offset;d=(h=d.isBefore(h))?m:e;h=h?e:m;if(d===k.getTextContentSize()){const n=k.getNextSibling();l.$isTextNode(n)&&(d=m=0,k=n)}if(k.is(f))l.$isTextNode(k)&&(d=m>e?e:m,h=m>e?m:e,d!==h&&(0===d&&h===a?(z(k,c),k.select(d,h)):(b=k.splitText(d,h),b=0===d?b[0]:b[1],z(b,c),b.select(0,h-d))));else for(l.$isTextNode(k)&&(0!==
d&&([,k]=k.splitText(d)),z(k,c)),l.$isTextNode(f)&&(a=f.getTextContent().length,h!==a&&([f]=f.splitText(h)),0!==h&&z(f,c)),a=1;a<g;a++)e=b[a],m=e.getKey(),l.$isTextNode(e)&&m!==k.getKey()&&m!==f.getKey()&&!e.isToken()&&z(e,c)}};
exports.$selectAll=function(a){const c=a.anchor;a=a.focus;var b=c.getNode().getTopLevelElementOrThrow().getParentOrThrow();let g=b.getFirstDescendant();b=b.getLastDescendant();let k="element",f="element",d=0;l.$isTextNode(g)?k="text":l.$isElementNode(g)||null===g||(g=g.getParentOrThrow());l.$isTextNode(b)?(f="text",d=b.getTextContentSize()):l.$isElementNode(b)||null===b||(b=b.getParentOrThrow(),d=b.getChildrenSize());g&&b&&(c.set(g.getKey(),0,k),a.set(b.getKey(),d,f))};
function x(a){if(l.$isRangeSelection(a)){var c=a.anchor,b=a.focus;let [h,m]=a.getCharacterOffsets();a=a.getNodes();if(0===a.length)return{nodeMap:[],range:[]};let n=a.length;var e=a[0],f=e.getParent();if(null!==f&&(!f.canBeEmpty()||l.$isRootNode(f))){var g=f.__children;if(g.length===n){var d=!0;for(var k=0;k<g.length;k++)if(g[k]!==a[k].__key){d=!1;break}d&&(n++,a.push(f))}}f=a[n-1];c=c.isBefore(b);b=new Map;g=[];d=l.$isTextNode(e)&&1===n;w(e,c?h:m,d?c?m:h:void 0,!0,g,b);for(e=0;e<n;e++){k=a[e];let q=
k.getKey();if(!(b.has(q)||l.$isElementNode(k)&&k.excludeFromCopy("clone"))){let p=u(k);l.$isRootNode(k.getParent())&&g.push(k.getKey());"root"!==q&&b.set(q,p)}}w(f,d?void 0:c?m:h,void 0,!1,g,b);return{nodeMap:Array.from(b.entries()),range:g}}if(l.$isGridSelection(a))return{nodeMap:a.getNodes().map(h=>{const m=h.getKey();h=u(h);return[m,h]}),range:[a.gridKey]};throw Error("Minified Lexical error #1; see codes.json for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");
}function y(a){return t.get(a)||null}function z(a,c){var b=y(a.getStyle());c=b?{...b,...c}:c;b="";for(e in c)e&&(b+=`${e}: ${c[e]};`);var e=b;a.setStyle(e);t.set(e,c)}function A(a,c,b,e){a.modify(c?"extend":"move",b,e)}function B(a){a=a.anchor.getNode();return"rtl"===(l.$isRootNode(a)?a:a.getParentOrThrow()).getDirection()}function C(a){for(;null!==a&&!l.$isRootNode(a);){let c=a.getLatest(),b=a.getParent();0===c.__children.length&&a.remove(!0);a=b}}
function D(a){for(;null!=a;){if(a.nodeType===Node.TEXT_NODE)return a;a=a.firstChild}return null}function E(a){let c=a.parentNode;if(null==c)throw Error("Should never happen");return[c,Array.from(c.childNodes).indexOf(a)]}function F(a){return/[\uD800-\uDBFF][\uDC00-\uDFFF]/g.test(a)}exports.$cloneContents=function(a){return x(a)};exports.$cloneWithProperties=u;
exports.$getSelectionStyleValueForProperty=function(a,c,b=""){let e=null,f=a.getNodes();var g=a.anchor,d=a.focus,k=a.isBackward();a=k?d.offset:g.offset;g=k?d.getNode():g.getNode();for(d=0;d<f.length;d++){var h=f[d];if((0===d||0!==a||!h.is(g))&&l.$isTextNode(h)){k=c;var m=b;h=h.getStyle();h=y(h);k=null!==h?h[k]||m:m;if(null===e)e=k;else if(e!==k){e="";break}}}return null===e?b:e};exports.$isAtNodeEnd=function(a){return"text"===a.type?a.offset===a.getNode().getTextContentSize():a.offset===a.getNode().getChildrenSize()};
exports.$isParentElementRTL=B;exports.$moveCaretSelection=A;exports.$moveCharacter=function(a,c,b){let e=B(a);A(a,c,b?!e:e,"character")};
exports.$patchStyleText=function(a,c){var b=a.getNodes();let e=b.length-1,f=b[0],g=b[e];if(!a.isCollapsed()){var d=a.anchor,k=a.focus;a=f.getTextContent().length;var h=k.offset,m=d.offset;d=(k=d.isBefore(k))?m:h;k=k?h:m;if(d===f.getTextContentSize()){let n=f.getNextSibling();l.$isTextNode(n)&&(d=m=0,f=n)}if(f.is(g))l.$isTextNode(f)&&(d=m>h?h:m,k=m>h?m:h,d!==k&&(0===d&&k===a?(z(f,c),f.select(d,k)):(b=f.splitText(d,k),b=0===d?b[0]:b[1],z(b,c),b.select(0,k-d))));else for(l.$isTextNode(f)&&(0!==d&&(f=
f.splitText(d)[1]),z(f,c)),l.$isTextNode(g)&&(a=g.getTextContent().length,k!==a&&([g]=g.splitText(k)),0!==k&&z(g,c)),a=1;a<e;a++)h=b[a],m=h.getKey(),l.$isTextNode(h)&&m!==f.getKey()&&m!==g.getKey()&&!h.isToken()&&z(h,c)}};
exports.$selectAll=function(a){let c=a.anchor;a=a.focus;var b=c.getNode().getTopLevelElementOrThrow().getParentOrThrow();let e=b.getFirstDescendant();b=b.getLastDescendant();let f="element",g="element",d=0;l.$isTextNode(e)?f="text":l.$isElementNode(e)||null===e||(e=e.getParentOrThrow());l.$isTextNode(b)?(g="text",d=b.getTextContentSize()):l.$isElementNode(b)||null===b||(b=b.getParentOrThrow());e&&b&&(c.set(e.getKey(),0,f),a.set(b.getKey(),d,g))};
exports.$shouldOverrideDefaultCharacterSelection=function(a,c){a=l.$getDecoratorNode(a.focus,c);return l.$isDecoratorNode(a)&&!a.isIsolated()};
exports.$wrapLeafNodesInElements=function(a,c,b){const g=a.getNodes(),k=g.length;var f=a.anchor;if(0===k||1===k&&"element"===f.type&&0===f.getNode().getChildrenSize()){a="text"===f.type?f.getNode().getParentOrThrow():f.getNode();f=a.getChildren();let q=c();f.forEach(v=>q.append(v));b&&(q=b.append(q));a.replace(q)}else{var d=g[0],h=new Map;f=[];d=l.$isElementNode(d)?d:d.getParentOrThrow();for(d.isInline()&&(d=d.getParentOrThrow());null!==d;){var e=d.getPreviousSibling();if(null!==e){d=e;break}d=d.getParentOrThrow();
if(l.$isRootNode(d))break}e=new Set;for(var m=0;m<k;m++){var n=g[m];l.$isElementNode(n)&&0===n.getChildrenSize()&&e.add(n.getKey())}var r=new Set;for(m=0;m<k;m++){var p=g[m];n=p.getParent();null!==n&&n.isInline()&&(n=n.getParent());if(null!==n&&l.$isLeafNode(p)&&!r.has(p.getKey())){if(p=n.getKey(),void 0===h.get(p)){const q=c();f.push(q);h.set(p,q);n.getChildren().forEach(v=>{q.append(v);r.add(v.getKey())});C(n)}}else e.has(p.getKey())&&(f.push(c()),p.remove())}if(b)for(c=0;c<f.length;c++)b.append(f[c]);
if(l.$isRootNode(d))if(c=d.getFirstChild(),l.$isElementNode(c)&&(d=c),null===c)if(b)d.append(b);else for(b=0;b<f.length;b++)d.append(f[b]);else if(b)c.insertBefore(b);else for(b=0;b<f.length;b++)c.insertBefore(f[b]);else if(b)d.insertAfter(b);else for(b=f.length-1;0<=b;b--)d.insertAfter(f[b]);b=l.$getPreviousSelection();l.$isRangeSelection(b)&&b.anchor.getNode().isAttached()&&b.focus.getNode().isAttached()?l.$setSelection(b.clone()):a.dirty=!0}};
exports.createDOMRange=function(a,c,b,g,k){const f=c.getKey(),d=g.getKey(),h=document.createRange();let e=a.getElementByKey(f);a=a.getElementByKey(d);l.$isTextNode(c)&&(e=D(e));l.$isTextNode(g)&&(a=D(a));if(void 0===c||void 0===g||null===e||null===a)return null;"BR"===e.nodeName&&([e,b]=E(e));"BR"===a.nodeName&&([a,k]=E(a));c=e.firstChild;e===a&&null!=c&&"BR"===c.nodeName&&0===b&&0===k&&(k=1);try{h.setStart(e,b),h.setEnd(a,k)}catch(m){return null}!h.collapsed||b===k&&f===d||(h.setStart(a,k),h.setEnd(e,
b));return h};exports.createRectsFromDOMRange=function(a,c){var b=a.getRootElement();if(null===b)return[];a=b.getBoundingClientRect();b=getComputedStyle(b);b=parseFloat(b.paddingLeft)+parseFloat(b.paddingRight);c=Array.from(c.getClientRects());let g=c.length,k;for(let f=0;f<g;f++){const d=c[f],h=d.width+b===a.width;k&&k.top===d.top&&k.left===d.left&&k.width===d.width&&k.height===d.height||h?(c.splice(f--,1),g--):k=d}return c};exports.getStyleObjectFromCSS=y;
exports.$sliceSelectedTextNodeContent=function(a,c){if(c.isSelected()&&!c.isSegmented()&&!c.isToken()&&(l.$isRangeSelection(a)||l.$isGridSelection(a))){var b=a.anchor.getNode(),e=a.focus.getNode(),f=c.is(b),g=c.is(e);if(f||g){f=a.isBackward();let [d,k]=a.getCharacterOffsets();a=b.is(e);g=c.is(f?e:b);e=c.is(f?b:e);b=0;let h=void 0;a?(b=d>k?k:d,h=d>k?d:k):g?(b=f?k:d,h=void 0):e&&(f=f?d:k,b=0,h=f);c.__text=c.__text.slice(b,h)}}return c};
exports.$wrapLeafNodesInElements=function(a,c,b){let e=a.getNodes(),f=e.length;var g=a.anchor;if(0===f||1===f&&"element"===g.type&&0===g.getNode().getChildrenSize()){a="text"===g.type?g.getNode().getParentOrThrow():g.getNode();g=a.getChildren();let r=c();g.forEach(v=>r.append(v));b&&(r=b.append(r));a.replace(r)}else{var d=e[0],k=new Map;g=[];d=l.$isElementNode(d)?d:d.getParentOrThrow();for(d.isInline()&&(d=d.getParentOrThrow());null!==d;){var h=d.getPreviousSibling();if(null!==h){d=h;break}d=d.getParentOrThrow();
if(l.$isRootNode(d))break}h=new Set;for(var m=0;m<f;m++){var n=e[m];l.$isElementNode(n)&&0===n.getChildrenSize()&&h.add(n.getKey())}var q=new Set;for(m=0;m<f;m++){var p=e[m];n=p.getParent();null!==n&&n.isInline()&&(n=n.getParent());if(null!==n&&l.$isLeafNode(p)&&!q.has(p.getKey())){if(p=n.getKey(),void 0===k.get(p)){let r=c();g.push(r);k.set(p,r);n.getChildren().forEach(v=>{r.append(v);q.add(v.getKey())});C(n)}}else h.has(p.getKey())&&(g.push(c()),p.remove())}if(b)for(c=0;c<g.length;c++)b.append(g[c]);
if(l.$isRootNode(d))if(c=d.getFirstChild(),l.$isElementNode(c)&&(d=c),null===c)if(b)d.append(b);else for(b=0;b<g.length;b++)d.append(g[b]);else if(b)c.insertBefore(b);else for(b=0;b<g.length;b++)c.insertBefore(g[b]);else if(b)d.insertAfter(b);else for(b=g.length-1;0<=b;b--)d.insertAfter(g[b]);b=l.$getPreviousSelection();l.$isRangeSelection(b)&&b.anchor.getNode().isAttached()&&b.focus.getNode().isAttached()?l.$setSelection(b.clone()):a.dirty=!0}};
exports.createDOMRange=function(a,c,b,e,f){let g=c.getKey(),d=e.getKey(),k=document.createRange(),h=a.getElementByKey(g);a=a.getElementByKey(d);l.$isTextNode(c)&&(h=D(h));l.$isTextNode(e)&&(a=D(a));if(void 0===c||void 0===e||null===h||null===a)return null;"BR"===h.nodeName&&([h,b]=E(h));"BR"===a.nodeName&&([a,f]=E(a));c=h.firstChild;h===a&&null!=c&&"BR"===c.nodeName&&0===b&&0===f&&(f=1);try{k.setStart(h,b),k.setEnd(a,f)}catch(m){return null}!k.collapsed||b===f&&g===d||(k.setStart(a,f),k.setEnd(h,
b));return k};exports.createRectsFromDOMRange=function(a,c){var b=a.getRootElement();if(null===b)return[];a=b.getBoundingClientRect();b=getComputedStyle(b);b=parseFloat(b.paddingLeft)+parseFloat(b.paddingRight);c=Array.from(c.getClientRects());let e=c.length,f;for(let g=0;g<e;g++){let d=c[g],k=d.width+b===a.width;f&&f.top===d.top&&f.left===d.left&&f.width===d.width&&f.height===d.height||k?(c.splice(g--,1),e--):f=d}return c};exports.getStyleObjectFromCSS=y;
exports.trimTextContentFromAnchor=function(a,c,b){let e=c.getNode();if(l.$isElementNode(e)){var f=e.getDescendantByIndex(c.offset);null!==f&&(e=f)}for(;0<b&&null!==e;){var g=e.getPreviousSibling(),d=0;if(null===g){f=e.getParentOrThrow();for(var k=f.getPreviousSibling();null===k;){f=f.getParent();if(null===f){g=null;break}k=f.getPreviousSibling()}null!==f&&(d=f.isInline()?0:2,g=l.$isElementNode(k)?k.getLastDescendant():k)}let h=e.getTextContent();""===h&&l.$isElementNode(e)&&!e.isInline()&&(h="\n\n");
f=h.length;k=f-b;let m=h.slice(0,k),n=F(h)&&!F(m);if(!l.$isTextNode(e)||b>=f||n)k=e.getParent(),e.remove(),0===k.getChildrenSize()&&k.remove(),b-=f+d,e=g;else{let q=e.getKey();g=a.getEditorState().read(()=>{const p=l.$getNodeByKey(q);return l.$isTextNode(p)&&p.isSimpleText()?p.getTextContent():null});null!==g&&g!==h?(b=l.$getPreviousSelection(),f=e,e.isSimpleText()?e.setTextContent(g):(f=l.$createTextNode(g),e.replace(f)),l.$isRangeSelection(b)&&b.isCollapsed()&&(b=b.anchor.offset,f.select(b,b))):
e.isSimpleText()?(g=c.key===q,d=c.offset,d<b&&(d=f),b=g?d-b:0,f=g?d:k,g&&0===b?([b]=e.splitText(b,f),b.remove()):([,b]=e.splitText(b,f),b.remove())):(b=l.$createTextNode(m),e.replace(b));b=0}}}

@@ -12,6 +12,6 @@ {

"license": "MIT",
"version": "0.2.9",
"version": "0.3.0",
"main": "LexicalSelection.js",
"peerDependencies": {
"lexical": "0.2.9"
"lexical": "0.3.0"
},

@@ -18,0 +18,0 @@ "repository": {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc