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

@lexical/selection

Package Overview
Dependencies
Maintainers
6
Versions
193
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.4.1 to 0.5.0

17

index.d.ts

@@ -9,3 +9,3 @@ /** @module @lexical/selection */

*/
import type { ElementNode, GridSelection, LexicalEditor, LexicalNode, NodeKey, NodeSelection, Point, RangeSelection, TextNode } from 'lexical';
import { ElementNode, GridSelection, LexicalEditor, LexicalNode, NodeKey, NodeSelection, Point, RangeSelection, TextNode } from 'lexical';
export declare function $cloneWithProperties<T extends LexicalNode>(node: T): T;

@@ -16,3 +16,3 @@ export declare function $cloneContents(selection: RangeSelection | NodeSelection | GridSelection): {

};
export declare function getStyleObjectFromCSS(css: string): Record<string, string> | null;
export declare function getStyleObjectFromCSS(css: string): Record<string, string>;
export declare function $addNodeStyle(node: TextNode): void;

@@ -25,3 +25,14 @@ export declare function $patchStyleText(selection: RangeSelection | GridSelection, patch: Record<string, string>): void;

export declare function $selectAll(selection: RangeSelection): void;
export declare function $wrapLeafNodesInElements(selection: RangeSelection, createElement: () => ElementNode, wrappingElement?: ElementNode): void;
/**
* Attempts to wrap all nodes in the Selection in ElementNodes returned from createElement.
* If wrappingElement is provided, all of the wrapped leaves are appended to the wrappingElement.
* It attempts to append the resulting sub-tree to the nearest safe insertion target.
*
* @param selection
* @param createElement
* @param wrappingElement
* @returns
*/
export declare function $wrapNodes(selection: RangeSelection, createElement: () => ElementNode, wrappingElement?: null | ElementNode): void;
export declare function $wrapNodesImpl(selection: RangeSelection, nodes: LexicalNode[], nodesLength: number, createElement: () => ElementNode, wrappingElement?: null | ElementNode): void;
export declare function $isAtNodeEnd(point: Point): boolean;

@@ -28,0 +39,0 @@ export declare function $shouldOverrideDefaultCharacterSelection(selection: RangeSelection, isBackward: boolean): boolean;

115

LexicalSelection.dev.js

@@ -204,3 +204,3 @@ /**

};
} else if (lexical.$isGridSelection(selection)) {
} else if (lexical.DEPRECATED_$isGridSelection(selection)) {
const nodeMap = selection.getNodes().map(node => {

@@ -223,3 +223,10 @@ const nodeKey = node.getKey();

function getStyleObjectFromCSS(css) {
return cssToStyles.get(css) || null;
let value = cssToStyles.get(css);
if (value === undefined) {
value = getStyleObjectFromRawCSS(css);
cssToStyles.set(css, value);
}
return value;
}

@@ -456,3 +463,3 @@

while (node !== null && !lexical.$isRootNode(node)) {
while (node !== null && !lexical.$isRootOrShadowRoot(node)) {
const latest = node.getLatest();

@@ -468,4 +475,15 @@ const parentNode = node.getParent();

}
/**
* Attempts to wrap all nodes in the Selection in ElementNodes returned from createElement.
* If wrappingElement is provided, all of the wrapped leaves are appended to the wrappingElement.
* It attempts to append the resulting sub-tree to the nearest safe insertion target.
*
* @param selection
* @param createElement
* @param wrappingElement
* @returns
*/
function $wrapLeafNodesInElements(selection, createElement, wrappingElement) {
function $wrapNodes(selection, createElement, wrappingElement = null) {
const nodes = selection.getNodes();

@@ -491,2 +509,30 @@ const nodesLength = nodes.length;

let topLevelNode = null;
let descendants = [];
for (let i = 0; i < nodesLength; i++) {
const node = nodes[i]; // Determine whether wrapping has to be broken down into multiple chunks. This can happen if the
// user selected multiple Root-like nodes that have to be treated separately as if they are
// their own branch. I.e. you don't want to wrap a whole table, but rather the contents of each
// of each of the cell nodes.
if (lexical.$isRootOrShadowRoot(node)) {
$wrapNodesImpl(selection, descendants, descendants.length, createElement, wrappingElement);
descendants = [];
topLevelNode = node;
} else if (topLevelNode === null || topLevelNode !== null && lexical.$hasAncestor(node, topLevelNode)) {
descendants.push(node);
} else {
$wrapNodesImpl(selection, descendants, descendants.length, createElement, wrappingElement);
descendants = [node];
}
}
$wrapNodesImpl(selection, descendants, descendants.length, createElement, wrappingElement);
}
function $wrapNodesImpl(selection, nodes, nodesLength, createElement, wrappingElement = null) {
if (nodes.length === 0) {
return;
}
const firstNode = nodes[0];

@@ -505,2 +551,4 @@ const elementMapping = new Map();

let targetIsPrevSibling = false;
while (target !== null) {

@@ -511,2 +559,3 @@ const prevSibling = target.getPreviousSibling();

target = prevSibling;
targetIsPrevSibling = true;
break;

@@ -517,3 +566,3 @@ }

if (lexical.$isRootNode(target)) {
if (lexical.$isRootOrShadowRoot(target)) {
break;

@@ -567,7 +616,7 @@ }

elements.push(targetElement);
node.remove();
node.remove(true);
}
}
if (wrappingElement) {
if (wrappingElement !== null) {
for (let i = 0; i < elements.length; i++) {

@@ -577,29 +626,40 @@ const element = elements[i];

}
} // If our target is the root, let's see if we can re-adjust
} // If our target is Root-like, let's see if we can re-adjust
// so that the target is the first child instead.
if (lexical.$isRootNode(target)) {
const firstChild = target.getFirstChild();
if (lexical.$isElementNode(firstChild)) {
target = firstChild;
}
if (firstChild === null) {
if (wrappingElement) {
target.append(wrappingElement);
if (lexical.$isRootOrShadowRoot(target)) {
if (targetIsPrevSibling) {
if (wrappingElement !== null) {
target.insertAfter(wrappingElement);
} else {
for (let i = 0; i < elements.length; i++) {
for (let i = elements.length - 1; i >= 0; i--) {
const element = elements[i];
target.append(element);
target.insertAfter(element);
}
}
} else {
if (wrappingElement) {
firstChild.insertBefore(wrappingElement);
const firstChild = target.getFirstChild();
if (lexical.$isElementNode(firstChild)) {
target = firstChild;
}
if (firstChild === null) {
if (wrappingElement) {
target.append(wrappingElement);
} else {
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
target.append(element);
}
}
} else {
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
firstChild.insertBefore(element);
if (wrappingElement !== null) {
firstChild.insertBefore(wrappingElement);
} else {
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
firstChild.insertBefore(element);
}
}

@@ -877,3 +937,3 @@ }

function $sliceSelectedTextNodeContent(selection, textNode) {
if (textNode.isSelected() && !textNode.isSegmented() && !textNode.isToken() && (lexical.$isRangeSelection(selection) || lexical.$isGridSelection(selection))) {
if (textNode.isSelected() && !textNode.isSegmented() && !textNode.isToken() && (lexical.$isRangeSelection(selection) || lexical.DEPRECATED_$isGridSelection(selection))) {
const anchorNode = selection.anchor.getNode();

@@ -926,3 +986,4 @@ const focusNode = selection.focus.getNode();

exports.$sliceSelectedTextNodeContent = $sliceSelectedTextNodeContent;
exports.$wrapLeafNodesInElements = $wrapLeafNodesInElements;
exports.$wrapNodes = $wrapNodes;
exports.$wrapNodesImpl = $wrapNodesImpl;
exports.createDOMRange = createDOMRange;

@@ -929,0 +990,0 @@ exports.createRectsFromDOMRange = createRectsFromDOMRange;

@@ -7,24 +7,26 @@ /**

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

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

"license": "MIT",
"version": "0.4.1",
"version": "0.5.0",
"main": "LexicalSelection.js",
"peerDependencies": {
"lexical": "0.4.1"
"lexical": "0.5.0"
},

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

@@ -98,8 +98,8 @@ # `@lexical/selection`

#### `$wrapLeafNodesInElements`
#### `$wrapNodes`
Attempts to wrap all leaf nodes in the Selection in ElementNodes returned from createElement. If wrappingElement is provided, all of the wrapped leaves are appended to the wrappingElement. It attempts to append the resulting sub-tree to the nearest safe insertion target.
Attempts to wrap all nodes in the Selection in ElementNodes returned from createElement. If wrappingElement is provided, all of the wrapped leaves are appended to the wrappingElement. It attempts to append the resulting sub-tree to the nearest safe insertion target.
```ts
export function $wrapLeafNodesInElements(
export function $wrapNodes(
selection: RangeSelection,

@@ -106,0 +106,0 @@ createElement: () => ElementNode,

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