@lexical/utils
Advanced tools
Comparing version 0.24.0 to 0.24.1-nightly.20250210.0
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
import { EditorState, ElementNode, Klass, LexicalEditor, LexicalNode } from 'lexical'; | ||
import { type CaretDirection, type EditorState, ElementNode, type Klass, type LexicalEditor, type LexicalNode, type NodeCaret, RootMode, type SiblingCaret } from 'lexical'; | ||
export { default as markSelection } from './markSelection'; | ||
@@ -67,6 +67,6 @@ export { default as mergeRegister } from './mergeRegister'; | ||
}>>; | ||
export type DFSNode = Readonly<{ | ||
depth: number; | ||
node: LexicalNode; | ||
}>; | ||
export interface DFSNode { | ||
readonly depth: number; | ||
readonly node: LexicalNode; | ||
} | ||
/** | ||
@@ -84,14 +84,15 @@ * "Depth-First Search" starts at the root/top node of a tree and goes as far as it can down a branch end | ||
/** | ||
* A function which will return exactly the reversed order of $dfs. That means that the tree is traversed | ||
* from right to left, starting at the leaf and working towards the root. | ||
* @param startNode - The node to start the search. If omitted, it will start at the last leaf node in the tree. | ||
* @param endNode - The node to end the search. If omitted, it will work backwards all the way to the root node | ||
* @returns An array of objects of all the nodes found by the search, including their depth into the tree. | ||
* \\{depth: number, node: LexicalNode\\} It will always return at least 1 node (the start node). | ||
* Get the adjacent caret in the same direction | ||
* | ||
* @param caret A caret or null | ||
* @returns `caret.getAdjacentCaret()` or `null` | ||
*/ | ||
export declare function $getAdjacentCaret<D extends CaretDirection>(caret: null | NodeCaret<D>): null | SiblingCaret<LexicalNode, D>; | ||
/** | ||
* $dfs iterator (right to left). Tree traversal is done on the fly as new values are requested with O(1) memory. | ||
* @param startNode - The node to start the search, if omitted, it will start at the root node. | ||
* @param endNode - The node to end the search, if omitted, it will find all descendants of the startingNode. | ||
* @returns An iterator, each yielded value is a DFSNode. It will always return at least 1 node (the start node). | ||
*/ | ||
export declare function $reverseDfs(startNode?: LexicalNode, endNode?: LexicalNode): Array<DFSNode>; | ||
type DFSIterator = { | ||
next: () => IteratorResult<DFSNode, void>; | ||
[Symbol.iterator]: () => DFSIterator; | ||
}; | ||
/** | ||
@@ -103,3 +104,3 @@ * $dfs iterator (left to right). Tree traversal is done on the fly as new values are requested with O(1) memory. | ||
*/ | ||
export declare function $dfsIterator(startNode?: LexicalNode, endNode?: LexicalNode): DFSIterator; | ||
export declare function $dfsIterator(startNode?: LexicalNode, endNode?: LexicalNode): IterableIterator<DFSNode>; | ||
/** | ||
@@ -114,6 +115,6 @@ * Returns the Node sibling when this exists, otherwise the closest parent sibling. For example | ||
export declare function $getNextSiblingOrParentSibling(node: LexicalNode): null | [LexicalNode, number]; | ||
export declare function $getDepth(node: LexicalNode): number; | ||
export declare function $getDepth(node: null | LexicalNode): number; | ||
/** | ||
* Performs a right-to-left preorder tree traversal. | ||
* From the starting node it goes to the rightmost child, than backtracks to paret and finds new rightmost path. | ||
* From the starting node it goes to the rightmost child, than backtracks to parent and finds new rightmost path. | ||
* It will return the next node in traversal sequence after the startingNode. | ||
@@ -131,3 +132,3 @@ * The traversal is similar to $dfs functions above, but the nodes are visited right-to-left, not left-to-right. | ||
*/ | ||
export declare function $reverseDfsIterator(startNode?: LexicalNode, endNode?: LexicalNode): DFSIterator; | ||
export declare function $reverseDfsIterator(startNode?: LexicalNode, endNode?: LexicalNode): IterableIterator<DFSNode>; | ||
/** | ||
@@ -194,3 +195,3 @@ * Takes a node and traverses up its ancestors (toward the root node) | ||
export declare function $wrapNodeInElement(node: LexicalNode, createElementNode: () => ElementNode): ElementNode; | ||
type ObjectKlass<T> = new (...args: any[]) => T; | ||
export type ObjectKlass<T> = new (...args: any[]) => T; | ||
/** | ||
@@ -275,3 +276,3 @@ * @param object = The instance of the type | ||
/** | ||
* Insert all children before this node, and then remove it. | ||
* Replace this node with its children | ||
* | ||
@@ -281,1 +282,10 @@ * @param node The ElementNode to unwrap and remove | ||
export declare function $unwrapNode(node: ElementNode): void; | ||
/** | ||
* Returns the Node sibling when this exists, otherwise the closest parent sibling. For example | ||
* R -> P -> T1, T2 | ||
* -> P2 | ||
* returns T2 for node T1, P2 for node T2, and null for node P2. | ||
* @param node LexicalNode. | ||
* @returns An array (tuple) containing the found Lexical node and the depth difference, or null, if this node doesn't exist. | ||
*/ | ||
export declare function $getAdjacentSiblingOrParentSiblingCaret<D extends CaretDirection>(startCaret: NodeCaret<D>, rootMode?: RootMode): null | [NodeCaret<D>, number]; |
@@ -544,20 +544,20 @@ /** | ||
/** | ||
* A function which will return exactly the reversed order of $dfs. That means that the tree is traversed | ||
* from right to left, starting at the leaf and working towards the root. | ||
* @param startNode - The node to start the search. If omitted, it will start at the last leaf node in the tree. | ||
* @param endNode - The node to end the search. If omitted, it will work backwards all the way to the root node | ||
* @returns An array of objects of all the nodes found by the search, including their depth into the tree. | ||
* \\{depth: number, node: LexicalNode\\} It will always return at least 1 node (the start node). | ||
* Get the adjacent caret in the same direction | ||
* | ||
* @param caret A caret or null | ||
* @returns `caret.getAdjacentCaret()` or `null` | ||
*/ | ||
function $getAdjacentCaret(caret) { | ||
return caret ? caret.getAdjacentCaret() : null; | ||
} | ||
/** | ||
* $dfs iterator (right to left). Tree traversal is done on the fly as new values are requested with O(1) memory. | ||
* @param startNode - The node to start the search, if omitted, it will start at the root node. | ||
* @param endNode - The node to end the search, if omitted, it will find all descendants of the startingNode. | ||
* @returns An iterator, each yielded value is a DFSNode. It will always return at least 1 node (the start node). | ||
*/ | ||
function $reverseDfs(startNode, endNode) { | ||
return Array.from($reverseDfsIterator(startNode, endNode)); | ||
} | ||
const iteratorDone = { | ||
done: true, | ||
value: undefined | ||
}; | ||
const iteratorNotDone = value => ({ | ||
done: false, | ||
value | ||
}); | ||
@@ -571,47 +571,34 @@ /** | ||
function $dfsIterator(startNode, endNode) { | ||
const start = (startNode || lexical.$getRoot()).getLatest(); | ||
const startDepth = $getDepth(start); | ||
const end = endNode; | ||
let node = start; | ||
return $dfsCaretIterator('next', startNode, endNode); | ||
} | ||
function $dfsCaretIterator(direction, startNode, endNode) { | ||
const rootMode = 'root'; | ||
const root = lexical.$getRoot(); | ||
const start = startNode || root; | ||
const startCaret = lexical.$isElementNode(start) ? lexical.$getChildCaret(start, direction) : lexical.$rewindSiblingCaret(lexical.$getSiblingCaret(start, direction)); | ||
const startDepth = $getDepth(startCaret.getParentAtCaret()); | ||
const endCaret = lexical.$getAdjacentChildCaret(endNode ? lexical.$getChildCaretOrSelf(lexical.$getSiblingCaret(endNode, direction)) : startCaret.getParentCaret(rootMode)); | ||
let depth = startDepth; | ||
let isFirstNext = true; | ||
const iterator = { | ||
next() { | ||
if (node === null) { | ||
return iteratorDone; | ||
return lexical.makeStepwiseIterator({ | ||
hasNext: state => state !== null, | ||
initial: startCaret, | ||
map: state => ({ | ||
depth, | ||
node: state.origin | ||
}), | ||
step: state => { | ||
if (state.isSameNodeCaret(endCaret)) { | ||
return null; | ||
} | ||
if (isFirstNext) { | ||
isFirstNext = false; | ||
return iteratorNotDone({ | ||
depth, | ||
node | ||
}); | ||
} | ||
if (node === end) { | ||
return iteratorDone; | ||
} | ||
if (lexical.$isElementNode(node) && node.getChildrenSize() > 0) { | ||
node = node.getFirstChild(); | ||
if (lexical.$isChildCaret(state)) { | ||
depth++; | ||
} else { | ||
let depthDiff; | ||
[node, depthDiff] = $getNextSiblingOrParentSibling(node) || [null, 0]; | ||
depth += depthDiff; | ||
if (end == null && depth <= startDepth) { | ||
node = null; | ||
} | ||
} | ||
if (node === null) { | ||
return iteratorDone; | ||
const rval = $getAdjacentSiblingOrParentSiblingCaret(state); | ||
if (!rval || rval[0].isSameNodeCaret(endCaret)) { | ||
return null; | ||
} | ||
return iteratorNotDone({ | ||
depth, | ||
node | ||
}); | ||
}, | ||
[Symbol.iterator]() { | ||
return iterator; | ||
depth += rval[1]; | ||
return rval[0]; | ||
} | ||
}; | ||
return iterator; | ||
}); | ||
} | ||
@@ -628,52 +615,8 @@ | ||
function $getNextSiblingOrParentSibling(node) { | ||
let node_ = node; | ||
// Find immediate sibling or nearest parent sibling | ||
let sibling = null; | ||
let depthDiff = 0; | ||
while (sibling === null && node_ !== null) { | ||
sibling = node_.getNextSibling(); | ||
if (sibling === null) { | ||
node_ = node_.getParent(); | ||
depthDiff--; | ||
} else { | ||
node_ = sibling; | ||
} | ||
} | ||
if (node_ === null) { | ||
return null; | ||
} | ||
return [node_, depthDiff]; | ||
const rval = $getAdjacentSiblingOrParentSiblingCaret(lexical.$getSiblingCaret(node, 'next')); | ||
return rval && [rval[0].origin, rval[1]]; | ||
} | ||
/** | ||
* Returns the Node's previous sibling when this exists, otherwise the closest parent previous sibling. For example | ||
* R -> P -> T1, T2 | ||
* -> P2 | ||
* returns T1 for node T2, P for node P2, and null for node P | ||
* @param node LexicalNode. | ||
* @returns An array (tuple) containing the found Lexical node and the depth difference, or null, if this node doesn't exist. | ||
*/ | ||
function $getPreviousSiblingOrParentSibling(node) { | ||
let node_ = node; | ||
// Find immediate sibling or nearest parent sibling | ||
let sibling = null; | ||
let depthDiff = 0; | ||
while (sibling === null && node_ !== null) { | ||
sibling = node_.getPreviousSibling(); | ||
if (sibling === null) { | ||
node_ = node_.getParent(); | ||
depthDiff--; | ||
} else { | ||
node_ = sibling; | ||
} | ||
} | ||
if (node_ === null) { | ||
return null; | ||
} | ||
return [node_, depthDiff]; | ||
} | ||
function $getDepth(node) { | ||
let innerNode = node; | ||
let depth = 0; | ||
while ((innerNode = innerNode.getParent()) !== null) { | ||
let depth = -1; | ||
for (let innerNode = node; innerNode !== null; innerNode = innerNode.getParent()) { | ||
depth++; | ||
@@ -686,3 +629,3 @@ } | ||
* Performs a right-to-left preorder tree traversal. | ||
* From the starting node it goes to the rightmost child, than backtracks to paret and finds new rightmost path. | ||
* From the starting node it goes to the rightmost child, than backtracks to parent and finds new rightmost path. | ||
* It will return the next node in traversal sequence after the startingNode. | ||
@@ -694,17 +637,5 @@ * The traversal is similar to $dfs functions above, but the nodes are visited right-to-left, not left-to-right. | ||
function $getNextRightPreorderNode(startingNode) { | ||
let node = startingNode; | ||
if (lexical.$isElementNode(node) && node.getChildrenSize() > 0) { | ||
node = node.getLastChild(); | ||
} else { | ||
let sibling = null; | ||
while (sibling === null && node !== null) { | ||
sibling = node.getPreviousSibling(); | ||
if (sibling === null) { | ||
node = node.getParent(); | ||
} else { | ||
node = sibling; | ||
} | ||
} | ||
} | ||
return node; | ||
const startCaret = lexical.$getChildCaretOrSelf(lexical.$getSiblingCaret(startingNode, 'previous')); | ||
const next = $getAdjacentSiblingOrParentSiblingCaret(startCaret, 'root'); | ||
return next && next[0].origin; | ||
} | ||
@@ -719,47 +650,3 @@ | ||
function $reverseDfsIterator(startNode, endNode) { | ||
const start = (startNode || lexical.$getRoot()).getLatest(); | ||
const startDepth = $getDepth(start); | ||
const end = endNode; | ||
let node = start; | ||
let depth = startDepth; | ||
let isFirstNext = true; | ||
const iterator = { | ||
next() { | ||
if (node === null) { | ||
return iteratorDone; | ||
} | ||
if (isFirstNext) { | ||
isFirstNext = false; | ||
return iteratorNotDone({ | ||
depth, | ||
node | ||
}); | ||
} | ||
if (node === end) { | ||
return iteratorDone; | ||
} | ||
if (lexical.$isElementNode(node) && node.getChildrenSize() > 0) { | ||
node = node.getLastChild(); | ||
depth++; | ||
} else { | ||
let depthDiff; | ||
[node, depthDiff] = $getPreviousSiblingOrParentSibling(node) || [null, 0]; | ||
depth += depthDiff; | ||
if (end == null && depth <= startDepth) { | ||
node = null; | ||
} | ||
} | ||
if (node === null) { | ||
return iteratorDone; | ||
} | ||
return iteratorNotDone({ | ||
depth, | ||
node | ||
}); | ||
}, | ||
[Symbol.iterator]() { | ||
return iterator; | ||
} | ||
}; | ||
return iterator; | ||
return $dfsCaretIterator('previous', startNode, endNode); | ||
} | ||
@@ -924,8 +811,3 @@ | ||
if (lexical.$isRootOrShadowRoot(focusNode)) { | ||
const focusChild = focusNode.getChildAtIndex(focusOffset); | ||
if (focusChild == null) { | ||
focusNode.append(node); | ||
} else { | ||
focusChild.insertBefore(node); | ||
} | ||
lexical.$getChildCaretAtIndex(focusNode, focusOffset, 'next').insert(node); | ||
node.selectNext(); | ||
@@ -955,4 +837,3 @@ } else { | ||
} else { | ||
const root = lexical.$getRoot(); | ||
root.append(node); | ||
lexical.$getRoot().append(node); | ||
} | ||
@@ -1013,8 +894,3 @@ const paragraphNode = lexical.$createParagraphNode(); | ||
function $insertFirst(parent, node) { | ||
const firstChild = parent.getFirstChild(); | ||
if (firstChild !== null) { | ||
firstChild.insertBefore(node); | ||
} else { | ||
parent.append(node); | ||
} | ||
lexical.$getChildCaret(parent, 'next').insert(node); | ||
} | ||
@@ -1088,3 +964,3 @@ let NEEDS_MANUAL_ZOOM = IS_FIREFOX || !CAN_USE_DOM ? false : undefined; | ||
if (lexical.$isElementNode(node)) { | ||
$unwrapAndFilterDescendantsImpl(node, $predicate, $onSuccess ? $onSuccess : child => node.insertAfter(child)); | ||
$unwrapAndFilterDescendantsImpl(node, $predicate, $onSuccess || (child => node.insertAfter(child))); | ||
} | ||
@@ -1116,3 +992,3 @@ node.remove(); | ||
const result = []; | ||
const stack = [...children].reverse(); | ||
const stack = Array.from(children).reverse(); | ||
for (let child = stack.pop(); child !== undefined; child = stack.pop()) { | ||
@@ -1139,5 +1015,3 @@ if ($predicate(child)) { | ||
function $firstToLastIterator(node) { | ||
return { | ||
[Symbol.iterator]: () => $childIterator(node.getFirstChild(), child => child.getNextSibling()) | ||
}; | ||
return $childIterator(lexical.$getChildCaret(node, 'next')); | ||
} | ||
@@ -1154,17 +1028,13 @@ | ||
function $lastToFirstIterator(node) { | ||
return { | ||
[Symbol.iterator]: () => $childIterator(node.getLastChild(), child => child.getPreviousSibling()) | ||
}; | ||
return $childIterator(lexical.$getChildCaret(node, 'previous')); | ||
} | ||
function $childIterator(initialNode, nextNode) { | ||
let state = initialNode; | ||
function $childIterator(startCaret) { | ||
const seen = new Set() ; | ||
return { | ||
next() { | ||
if (state === null) { | ||
return iteratorDone; | ||
} | ||
const rval = iteratorNotDone(state); | ||
return lexical.makeStepwiseIterator({ | ||
hasNext: lexical.$isSiblingCaret, | ||
initial: startCaret.getAdjacentCaret(), | ||
map: caret => { | ||
const origin = caret.origin.getLatest(); | ||
if (seen !== null) { | ||
const key = state.getKey(); | ||
const key = origin.getKey(); | ||
if (!!seen.has(key)) { | ||
@@ -1175,10 +1045,10 @@ throw Error(`$childIterator: Cycle detected, node with key ${String(key)} has already been traversed`); | ||
} | ||
state = nextNode(state); | ||
return rval; | ||
} | ||
}; | ||
return origin; | ||
}, | ||
step: caret => caret.getAdjacentCaret() | ||
}); | ||
} | ||
/** | ||
* Insert all children before this node, and then remove it. | ||
* Replace this node with its children | ||
* | ||
@@ -1188,6 +1058,27 @@ * @param node The ElementNode to unwrap and remove | ||
function $unwrapNode(node) { | ||
for (const child of $firstToLastIterator(node)) { | ||
node.insertBefore(child); | ||
lexical.$rewindSiblingCaret(lexical.$getSiblingCaret(node, 'next')).splice(1, node.getChildren()); | ||
} | ||
/** | ||
* Returns the Node sibling when this exists, otherwise the closest parent sibling. For example | ||
* R -> P -> T1, T2 | ||
* -> P2 | ||
* returns T2 for node T1, P2 for node T2, and null for node P2. | ||
* @param node LexicalNode. | ||
* @returns An array (tuple) containing the found Lexical node and the depth difference, or null, if this node doesn't exist. | ||
*/ | ||
function $getAdjacentSiblingOrParentSiblingCaret(startCaret, rootMode = 'root') { | ||
let depthDiff = 0; | ||
let caret = startCaret; | ||
let nextCaret = lexical.$getAdjacentChildCaret(caret); | ||
while (nextCaret === null) { | ||
depthDiff--; | ||
nextCaret = caret.getParentCaret(rootMode); | ||
if (!nextCaret) { | ||
return null; | ||
} | ||
caret = nextCaret; | ||
nextCaret = lexical.$getAdjacentChildCaret(caret); | ||
} | ||
node.remove(); | ||
return nextCaret && [nextCaret, depthDiff]; | ||
} | ||
@@ -1206,2 +1097,4 @@ | ||
exports.$firstToLastIterator = $firstToLastIterator; | ||
exports.$getAdjacentCaret = $getAdjacentCaret; | ||
exports.$getAdjacentSiblingOrParentSiblingCaret = $getAdjacentSiblingOrParentSiblingCaret; | ||
exports.$getDepth = $getDepth; | ||
@@ -1208,0 +1101,0 @@ exports.$getNearestBlockElementAncestorOrThrow = $getNearestBlockElementAncestorOrThrow; |
@@ -9,2 +9,2 @@ /** | ||
"use strict";var e=require("lexical"),t=require("@lexical/selection");function n(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var o=n((function(e){const t=new URLSearchParams;t.append("code",e);for(let e=1;e<arguments.length;e++)t.append("v",arguments[e]);throw Error(`Minified Lexical error #${e}; visit https://lexical.dev/docs/error?${t} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}));const r="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement,l=r&&"documentMode"in document?document.documentMode:null,i=r&&/Mac|iPod|iPhone|iPad/.test(navigator.platform),s=r&&/^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent),u=!(!r||!("InputEvent"in window)||l)&&"getTargetRanges"in new window.InputEvent("input"),c=r&&/Version\/[\d.]+.*Safari/.test(navigator.userAgent),d=r&&/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream,a=r&&/Android/.test(navigator.userAgent),f=r&&/^(?=.*Chrome).*/i.test(navigator.userAgent),p=r&&a&&f,g=r&&/AppleWebKit\/[\d.]+/.test(navigator.userAgent)&&!f;function m(...e){const t=[];for(const n of e)if(n&&"string"==typeof n)for(const[e]of n.matchAll(/\S+/g))t.push(e);return t}function h(...e){return()=>{for(let t=e.length-1;t>=0;t--)e[t]();e.length=0}}function x(e){return`${e}px`}const E={attributes:!0,characterData:!0,childList:!0,subtree:!0};function v(n,r,l){let i=null,s=null,u=null,c=[];const d=document.createElement("div");function a(){null===i&&o(182),null===s&&o(183);const{left:e,top:u}=s.getBoundingClientRect(),a=t.createRectsFromDOMRange(n,r);var f,p;d.isConnected||(p=d,(f=s).insertBefore(p,f.firstChild));let g=!1;for(let t=0;t<a.length;t++){const n=a[t],o=c[t]||document.createElement("div"),r=o.style;"absolute"!==r.position&&(r.position="absolute",g=!0);const l=x(n.left-e);r.left!==l&&(r.left=l,g=!0);const i=x(n.top-u);r.top!==i&&(o.style.top=i,g=!0);const s=x(n.width);r.width!==s&&(o.style.width=s,g=!0);const f=x(n.height);r.height!==f&&(o.style.height=f,g=!0),o.parentNode!==d&&(d.append(o),g=!0),c[t]=o}for(;c.length>a.length;)c.pop();g&&l(c)}function f(){s=null,i=null,null!==u&&u.disconnect(),u=null,d.remove();for(const e of c)e.remove();c=[]}d.style.position="relative";const p=n.registerRootListener((function t(){const o=n.getRootElement();if(null===o)return f();const r=o.parentElement;if(!e.isHTMLElement(r))return f();f(),i=o,s=r,u=new MutationObserver((e=>{const o=n.getRootElement(),r=o&&o.parentElement;if(o!==i||r!==s)return t();for(const t of e)if(!d.contains(t.target))return a()})),u.observe(r,E),a()}));return()=>{p(),f()}}function S(t,n,o){if("text"!==t.type&&e.$isElementNode(n)){const e=n.getDOMSlot(o);return[e.element,e.getFirstChildOffset()+t.offset]}return[e.getDOMTextNode(o)||o,t.offset]}function N(t,n){let o=null,r=null,l=null,i=null,s=null,u=null,c=()=>{};function d(d){d.read((()=>{const d=e.$getSelection();if(!e.$isRangeSelection(d))return o=null,l=null,i=null,u=null,c(),void(c=()=>{});const{anchor:a,focus:f}=d,p=a.getNode(),g=p.getKey(),m=a.offset,h=f.getNode(),E=h.getKey(),N=f.offset,$=t.getElementByKey(g),y=t.getElementByKey(E),w=null===o||$!==r||m!==l||g!==o.getKey(),P=null===i||y!==s||N!==u||E!==i.getKey();if((w||P)&&null!==$&&null!==y){const e=function(e,t,n,o,r,l,i){const s=(e._window?e._window.document:document).createRange();return l.isBefore(n)?(s.setStart(...S(r,l,i)),s.setEnd(...S(t,n,o))):(s.setStart(...S(t,n,o)),s.setEnd(...S(r,l,i))),s}(t,a,p,$,f,h,y);c(),c=v(t,e,(e=>{if(void 0===n)for(const t of e){const e=t.style;"Highlight"!==e.background&&(e.background="Highlight"),"HighlightText"!==e.color&&(e.color="HighlightText"),e.marginTop!==x(-1.5)&&(e.marginTop=x(-1.5)),e.paddingTop!==x(4)&&(e.paddingTop=x(4)),e.paddingBottom!==x(0)&&(e.paddingBottom=x(0))}else n(e)}))}o=p,r=$,l=m,i=h,s=y,u=N}))}return d(t.getEditorState()),h(t.registerUpdateListener((({editorState:e})=>d(e))),(()=>{c()}))}const $=u,y=r,w=a,P=p,R=i,A=g,T=f,b=s,C=d,I=c;function L(e,t){for(const n of t)if(e.type.startsWith(n))return!0;return!1}const _={done:!0,value:void 0},O=e=>({done:!1,value:e});function M(t,n){const o=(t||e.$getRoot()).getLatest(),r=D(o),l=n;let i=o,s=r,u=!0;const c={next(){if(null===i)return _;if(u)return u=!1,O({depth:s,node:i});if(i===l)return _;if(e.$isElementNode(i)&&i.getChildrenSize()>0)i=i.getFirstChild(),s++;else{let e;[i,e]=B(i)||[null,0],s+=e,null==l&&s<=r&&(i=null)}return null===i?_:O({depth:s,node:i})},[Symbol.iterator]:()=>c};return c}function B(e){let t=e,n=null,o=0;for(;null===n&&null!==t;)n=t.getNextSibling(),null===n?(t=t.getParent(),o--):t=n;return null===t?null:[t,o]}function D(e){let t=e,n=0;for(;null!==(t=t.getParent());)n++;return n}function F(t,n){const o=(t||e.$getRoot()).getLatest(),r=D(o),l=n;let i=o,s=r,u=!0;const c={next(){if(null===i)return _;if(u)return u=!1,O({depth:s,node:i});if(i===l)return _;if(e.$isElementNode(i)&&i.getChildrenSize()>0)i=i.getLastChild(),s++;else{let e;[i,e]=function(e){let t=e,n=null,o=0;for(;null===n&&null!==t;)n=t.getPreviousSibling(),null===n?(t=t.getParent(),o--):t=n;return null===t?null:[t,o]}(i)||[null,0],s+=e,null==l&&s<=r&&(i=null)}return null===i?_:O({depth:s,node:i})},[Symbol.iterator]:()=>c};return c}const H=(t,n)=>{let o=t;for(;o!==e.$getRoot()&&null!=o;){if(n(o))return o;o=o.getParent()}return null};let K=!(b||!y)&&void 0;function k(t,n,o){let r=!1;for(const l of U(t))n(l)?null!==o&&o(l):(r=!0,e.$isElementNode(l)&&k(l,n,o||(e=>l.insertAfter(e))),l.remove());return r}function z(e){return{[Symbol.iterator]:()=>W(e.getFirstChild(),(e=>e.getNextSibling()))}}function U(e){return{[Symbol.iterator]:()=>W(e.getLastChild(),(e=>e.getPreviousSibling()))}}function W(e,t){let n=e;return{next(){if(null===n)return _;const e=O(n);return n=t(n),e}}}exports.$splitNode=e.$splitNode,exports.isBlockDomNode=e.isBlockDomNode,exports.isHTMLAnchorElement=e.isHTMLAnchorElement,exports.isHTMLElement=e.isHTMLElement,exports.isInlineDomNode=e.isInlineDomNode,exports.$descendantsMatching=function(t,n){const o=[],r=[...t].reverse();for(let t=r.pop();void 0!==t;t=r.pop())if(n(t))o.push(t);else if(e.$isElementNode(t))for(const e of U(t))r.push(e);return o},exports.$dfs=function(e,t){return Array.from(M(e,t))},exports.$dfsIterator=M,exports.$filter=function(e,t){const n=[];for(let o=0;o<e.length;o++){const r=t(e[o]);null!==r&&n.push(r)}return n},exports.$findMatchingParent=H,exports.$firstToLastIterator=z,exports.$getDepth=D,exports.$getNearestBlockElementAncestorOrThrow=function(t){const n=H(t,(t=>e.$isElementNode(t)&&!t.isInline()));return e.$isElementNode(n)||o(4,t.__key),n},exports.$getNearestNodeOfType=function(e,t){let n=e;for(;null!=n;){if(n instanceof t)return n;n=n.getParent()}return null},exports.$getNextRightPreorderNode=function(t){let n=t;if(e.$isElementNode(n)&&n.getChildrenSize()>0)n=n.getLastChild();else{let e=null;for(;null===e&&null!==n;)e=n.getPreviousSibling(),n=null===e?n.getParent():e}return n},exports.$getNextSiblingOrParentSibling=B,exports.$insertFirst=function(e,t){const n=e.getFirstChild();null!==n?n.insertBefore(t):e.append(t)},exports.$insertNodeToNearestRoot=function(t){const n=e.$getSelection()||e.$getPreviousSelection();if(e.$isRangeSelection(n)){const{focus:o}=n,r=o.getNode(),l=o.offset;if(e.$isRootOrShadowRoot(r)){const e=r.getChildAtIndex(l);null==e?r.append(t):e.insertBefore(t),t.selectNext()}else{let n,o;e.$isTextNode(r)?(n=r.getParentOrThrow(),o=r.getIndexWithinParent(),l>0&&(o+=1,r.splitText(l))):(n=r,o=l);const[,i]=e.$splitNode(n,o);i.insertBefore(t),i.selectStart()}}else{if(null!=n){const e=n.getNodes();e[e.length-1].getTopLevelElementOrThrow().insertAfter(t)}else{e.$getRoot().append(t)}const o=e.$createParagraphNode();t.insertAfter(o),o.select()}return t.getLatest()},exports.$isEditorIsNestedEditor=function(e){return null!==e._parentEditor},exports.$lastToFirstIterator=U,exports.$restoreEditorState=function(t,n){const o=new Map,r=t._pendingEditorState;for(const[t,r]of n._nodeMap)o.set(t,e.$cloneWithProperties(r));r&&(r._nodeMap=o),t._dirtyType=2;const l=n._selection;e.$setSelection(null===l?null:l.clone())},exports.$reverseDfs=function(e,t){return Array.from(F(e,t))},exports.$reverseDfsIterator=F,exports.$unwrapAndFilterDescendants=function(e,t){return k(e,t,null)},exports.$unwrapNode=function(e){for(const t of z(e))e.insertBefore(t);e.remove()},exports.$wrapNodeInElement=function(e,t){const n=t();return e.replace(n),n.append(e),n},exports.CAN_USE_BEFORE_INPUT=$,exports.CAN_USE_DOM=y,exports.IS_ANDROID=w,exports.IS_ANDROID_CHROME=P,exports.IS_APPLE=R,exports.IS_APPLE_WEBKIT=A,exports.IS_CHROME=T,exports.IS_FIREFOX=b,exports.IS_IOS=C,exports.IS_SAFARI=I,exports.addClassNamesToElement=function(e,...t){const n=m(...t);n.length>0&&e.classList.add(...n)},exports.calculateZoomLevel=function(e){let t=1;if(function(){if(void 0===K){const e=document.createElement("div");e.style.cssText="position: absolute; opacity: 0; width: 100px; left: -1000px;",document.body.appendChild(e);const t=e.getBoundingClientRect();e.style.setProperty("zoom","2"),K=e.getBoundingClientRect().width===t.width,document.body.removeChild(e)}return K}())for(;e;)t*=Number(window.getComputedStyle(e).getPropertyValue("zoom")),e=e.parentElement;return t},exports.isMimeType=L,exports.markSelection=N,exports.mediaFileReader=function(e,t){const n=e[Symbol.iterator]();return new Promise(((e,o)=>{const r=[],l=()=>{const{done:i,value:s}=n.next();if(i)return e(r);const u=new FileReader;u.addEventListener("error",o),u.addEventListener("load",(()=>{const e=u.result;"string"==typeof e&&r.push({file:s,result:e}),l()})),L(s,t)?u.readAsDataURL(s):l()};l()}))},exports.mergeRegister=h,exports.objectKlassEquals=function(e,t){return null!==e&&Object.getPrototypeOf(e).constructor.name===t.name},exports.positionNodeOnRange=v,exports.registerNestedElementResolver=function(e,t,n,o){const r=e=>e instanceof t;return e.registerNodeTransform(t,(e=>{const t=(e=>{const t=e.getChildren();for(let e=0;e<t.length;e++){const n=t[e];if(r(n))return null}let n=e,o=e;for(;null!==n;)if(o=n,n=n.getParent(),r(n))return{child:o,parent:n};return null})(e);if(null!==t){const{child:r,parent:l}=t;if(r.is(e)){o(l,e);const t=r.getNextSiblings(),i=t.length;if(l.insertAfter(r),0!==i){const e=n(l);r.insertAfter(e);for(let n=0;n<i;n++)e.append(t[n])}l.canBeEmpty()||0!==l.getChildrenSize()||l.remove()}}}))},exports.removeClassNamesFromElement=function(e,...t){const n=m(...t);n.length>0&&e.classList.remove(...n)},exports.selectionAlwaysOnDisplay=function(e){let t=null;const n=()=>{const n=getSelection(),o=n&&n.anchorNode,r=e.getRootElement();null!==o&&null!==r&&r.contains(o)?null!==t&&(t(),t=null):null===t&&(t=N(e))};return document.addEventListener("selectionchange",n),()=>{null!==t&&t(),document.removeEventListener("selectionchange",n)}}; | ||
"use strict";var e=require("lexical"),t=require("@lexical/selection");function n(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var o=n((function(e){const t=new URLSearchParams;t.append("code",e);for(let e=1;e<arguments.length;e++)t.append("v",arguments[e]);throw Error(`Minified Lexical error #${e}; visit https://lexical.dev/docs/error?${t} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}));const r="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement,i=r&&"documentMode"in document?document.documentMode:null,s=r&&/Mac|iPod|iPhone|iPad/.test(navigator.platform),l=r&&/^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent),a=!(!r||!("InputEvent"in window)||i)&&"getTargetRanges"in new window.InputEvent("input"),u=r&&/Version\/[\d.]+.*Safari/.test(navigator.userAgent),c=r&&/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream,d=r&&/Android/.test(navigator.userAgent),f=r&&/^(?=.*Chrome).*/i.test(navigator.userAgent),g=r&&d&&f,p=r&&/AppleWebKit\/[\d.]+/.test(navigator.userAgent)&&!f;function m(...e){const t=[];for(const n of e)if(n&&"string"==typeof n)for(const[e]of n.matchAll(/\S+/g))t.push(e);return t}function h(...e){return()=>{for(let t=e.length-1;t>=0;t--)e[t]();e.length=0}}function x(e){return`${e}px`}const $={attributes:!0,characterData:!0,childList:!0,subtree:!0};function C(n,r,i){let s=null,l=null,a=null,u=[];const c=document.createElement("div");function d(){null===s&&o(182),null===l&&o(183);const{left:e,top:a}=l.getBoundingClientRect(),d=t.createRectsFromDOMRange(n,r);var f,g;c.isConnected||(g=c,(f=l).insertBefore(g,f.firstChild));let p=!1;for(let t=0;t<d.length;t++){const n=d[t],o=u[t]||document.createElement("div"),r=o.style;"absolute"!==r.position&&(r.position="absolute",p=!0);const i=x(n.left-e);r.left!==i&&(r.left=i,p=!0);const s=x(n.top-a);r.top!==s&&(o.style.top=s,p=!0);const l=x(n.width);r.width!==l&&(o.style.width=l,p=!0);const f=x(n.height);r.height!==f&&(o.style.height=f,p=!0),o.parentNode!==c&&(c.append(o),p=!0),u[t]=o}for(;u.length>d.length;)u.pop();p&&i(u)}function f(){l=null,s=null,null!==a&&a.disconnect(),a=null,c.remove();for(const e of u)e.remove();u=[]}c.style.position="relative";const g=n.registerRootListener((function t(){const o=n.getRootElement();if(null===o)return f();const r=o.parentElement;if(!e.isHTMLElement(r))return f();f(),s=o,l=r,a=new MutationObserver((e=>{const o=n.getRootElement(),r=o&&o.parentElement;if(o!==s||r!==l)return t();for(const t of e)if(!c.contains(t.target))return d()})),a.observe(r,$),d()}));return()=>{g(),f()}}function S(t,n,o){if("text"!==t.type&&e.$isElementNode(n)){const e=n.getDOMSlot(o);return[e.element,e.getFirstChildOffset()+t.offset]}return[e.getDOMTextNode(o)||o,t.offset]}function E(t,n){let o=null,r=null,i=null,s=null,l=null,a=null,u=()=>{};function c(c){c.read((()=>{const c=e.$getSelection();if(!e.$isRangeSelection(c))return o=null,i=null,s=null,a=null,u(),void(u=()=>{});const{anchor:d,focus:f}=c,g=d.getNode(),p=g.getKey(),m=d.offset,h=f.getNode(),$=h.getKey(),E=f.offset,v=t.getElementByKey(p),N=t.getElementByKey($),w=null===o||v!==r||m!==i||p!==o.getKey(),y=null===s||N!==l||E!==a||$!==s.getKey();if((w||y)&&null!==v&&null!==N){const e=function(e,t,n,o,r,i,s){const l=(e._window?e._window.document:document).createRange();return i.isBefore(n)?(l.setStart(...S(r,i,s)),l.setEnd(...S(t,n,o))):(l.setStart(...S(t,n,o)),l.setEnd(...S(r,i,s))),l}(t,d,g,v,f,h,N);u(),u=C(t,e,(e=>{if(void 0===n)for(const t of e){const e=t.style;"Highlight"!==e.background&&(e.background="Highlight"),"HighlightText"!==e.color&&(e.color="HighlightText"),e.marginTop!==x(-1.5)&&(e.marginTop=x(-1.5)),e.paddingTop!==x(4)&&(e.paddingTop=x(4)),e.paddingBottom!==x(0)&&(e.paddingBottom=x(0))}else n(e)}))}o=g,r=v,i=m,s=h,l=N,a=E}))}return c(t.getEditorState()),h(t.registerUpdateListener((({editorState:e})=>c(e))),(()=>{u()}))}const v=a,N=r,w=d,y=g,A=s,R=p,P=f,b=l,I=c,T=u;function O(e,t){for(const n of t)if(e.type.startsWith(n))return!0;return!1}function _(e,t){return L("next",e,t)}function L(t,n,o){const r=e.$getRoot(),i=n||r,s=e.$isElementNode(i)?e.$getChildCaret(i,t):e.$rewindSiblingCaret(e.$getSiblingCaret(i,t)),l=M(s.getParentAtCaret()),a=e.$getAdjacentChildCaret(o?e.$getChildCaretOrSelf(e.$getSiblingCaret(o,t)):s.getParentCaret("root"));let u=l;return e.makeStepwiseIterator({hasNext:e=>null!==e,initial:s,map:e=>({depth:u,node:e.origin}),step:t=>{if(t.isSameNodeCaret(a))return null;e.$isChildCaret(t)&&u++;const n=K(t);return!n||n[0].isSameNodeCaret(a)?null:(u+=n[1],n[0])}})}function M(e){let t=-1;for(let n=e;null!==n;n=n.getParent())t++;return t}function D(e,t){return L("previous",e,t)}const B=(t,n)=>{let o=t;for(;o!==e.$getRoot()&&null!=o;){if(n(o))return o;o=o.getParent()}return null};let F=!(b||!N)&&void 0;function j(t,n,o){let r=!1;for(const i of H(t))n(i)?null!==o&&o(i):(r=!0,e.$isElementNode(i)&&j(i,n,o||(e=>i.insertAfter(e))),i.remove());return r}function H(t){return k(e.$getChildCaret(t,"previous"))}function k(t){return e.makeStepwiseIterator({hasNext:e.$isSiblingCaret,initial:t.getAdjacentCaret(),map:e=>e.origin.getLatest(),step:e=>e.getAdjacentCaret()})}function K(t,n="root"){let o=0,r=t,i=e.$getAdjacentChildCaret(r);for(;null===i;){if(o--,i=r.getParentCaret(n),!i)return null;r=i,i=e.$getAdjacentChildCaret(r)}return i&&[i,o]}exports.$splitNode=e.$splitNode,exports.isBlockDomNode=e.isBlockDomNode,exports.isHTMLAnchorElement=e.isHTMLAnchorElement,exports.isHTMLElement=e.isHTMLElement,exports.isInlineDomNode=e.isInlineDomNode,exports.$descendantsMatching=function(t,n){const o=[],r=Array.from(t).reverse();for(let t=r.pop();void 0!==t;t=r.pop())if(n(t))o.push(t);else if(e.$isElementNode(t))for(const e of H(t))r.push(e);return o},exports.$dfs=function(e,t){return Array.from(_(e,t))},exports.$dfsIterator=_,exports.$filter=function(e,t){const n=[];for(let o=0;o<e.length;o++){const r=t(e[o]);null!==r&&n.push(r)}return n},exports.$findMatchingParent=B,exports.$firstToLastIterator=function(t){return k(e.$getChildCaret(t,"next"))},exports.$getAdjacentCaret=function(e){return e?e.getAdjacentCaret():null},exports.$getAdjacentSiblingOrParentSiblingCaret=K,exports.$getDepth=M,exports.$getNearestBlockElementAncestorOrThrow=function(t){const n=B(t,(t=>e.$isElementNode(t)&&!t.isInline()));return e.$isElementNode(n)||o(4,t.__key),n},exports.$getNearestNodeOfType=function(e,t){let n=e;for(;null!=n;){if(n instanceof t)return n;n=n.getParent()}return null},exports.$getNextRightPreorderNode=function(t){const n=K(e.$getChildCaretOrSelf(e.$getSiblingCaret(t,"previous")),"root");return n&&n[0].origin},exports.$getNextSiblingOrParentSibling=function(t){const n=K(e.$getSiblingCaret(t,"next"));return n&&[n[0].origin,n[1]]},exports.$insertFirst=function(t,n){e.$getChildCaret(t,"next").insert(n)},exports.$insertNodeToNearestRoot=function(t){const n=e.$getSelection()||e.$getPreviousSelection();if(e.$isRangeSelection(n)){const{focus:o}=n,r=o.getNode(),i=o.offset;if(e.$isRootOrShadowRoot(r))e.$getChildCaretAtIndex(r,i,"next").insert(t),t.selectNext();else{let n,o;e.$isTextNode(r)?(n=r.getParentOrThrow(),o=r.getIndexWithinParent(),i>0&&(o+=1,r.splitText(i))):(n=r,o=i);const[,s]=e.$splitNode(n,o);s.insertBefore(t),s.selectStart()}}else{if(null!=n){const e=n.getNodes();e[e.length-1].getTopLevelElementOrThrow().insertAfter(t)}else e.$getRoot().append(t);const o=e.$createParagraphNode();t.insertAfter(o),o.select()}return t.getLatest()},exports.$isEditorIsNestedEditor=function(e){return null!==e._parentEditor},exports.$lastToFirstIterator=H,exports.$restoreEditorState=function(t,n){const o=new Map,r=t._pendingEditorState;for(const[t,r]of n._nodeMap)o.set(t,e.$cloneWithProperties(r));r&&(r._nodeMap=o),t._dirtyType=2;const i=n._selection;e.$setSelection(null===i?null:i.clone())},exports.$reverseDfs=function(e,t){return Array.from(D(e,t))},exports.$reverseDfsIterator=D,exports.$unwrapAndFilterDescendants=function(e,t){return j(e,t,null)},exports.$unwrapNode=function(t){e.$rewindSiblingCaret(e.$getSiblingCaret(t,"next")).splice(1,t.getChildren())},exports.$wrapNodeInElement=function(e,t){const n=t();return e.replace(n),n.append(e),n},exports.CAN_USE_BEFORE_INPUT=v,exports.CAN_USE_DOM=N,exports.IS_ANDROID=w,exports.IS_ANDROID_CHROME=y,exports.IS_APPLE=A,exports.IS_APPLE_WEBKIT=R,exports.IS_CHROME=P,exports.IS_FIREFOX=b,exports.IS_IOS=I,exports.IS_SAFARI=T,exports.addClassNamesToElement=function(e,...t){const n=m(...t);n.length>0&&e.classList.add(...n)},exports.calculateZoomLevel=function(e){let t=1;if(function(){if(void 0===F){const e=document.createElement("div");e.style.cssText="position: absolute; opacity: 0; width: 100px; left: -1000px;",document.body.appendChild(e);const t=e.getBoundingClientRect();e.style.setProperty("zoom","2"),F=e.getBoundingClientRect().width===t.width,document.body.removeChild(e)}return F}())for(;e;)t*=Number(window.getComputedStyle(e).getPropertyValue("zoom")),e=e.parentElement;return t},exports.isMimeType=O,exports.markSelection=E,exports.mediaFileReader=function(e,t){const n=e[Symbol.iterator]();return new Promise(((e,o)=>{const r=[],i=()=>{const{done:s,value:l}=n.next();if(s)return e(r);const a=new FileReader;a.addEventListener("error",o),a.addEventListener("load",(()=>{const e=a.result;"string"==typeof e&&r.push({file:l,result:e}),i()})),O(l,t)?a.readAsDataURL(l):i()};i()}))},exports.mergeRegister=h,exports.objectKlassEquals=function(e,t){return null!==e&&Object.getPrototypeOf(e).constructor.name===t.name},exports.positionNodeOnRange=C,exports.registerNestedElementResolver=function(e,t,n,o){const r=e=>e instanceof t;return e.registerNodeTransform(t,(e=>{const t=(e=>{const t=e.getChildren();for(let e=0;e<t.length;e++){const n=t[e];if(r(n))return null}let n=e,o=e;for(;null!==n;)if(o=n,n=n.getParent(),r(n))return{child:o,parent:n};return null})(e);if(null!==t){const{child:r,parent:i}=t;if(r.is(e)){o(i,e);const t=r.getNextSiblings(),s=t.length;if(i.insertAfter(r),0!==s){const e=n(i);r.insertAfter(e);for(let n=0;n<s;n++)e.append(t[n])}i.canBeEmpty()||0!==i.getChildrenSize()||i.remove()}}}))},exports.removeClassNamesFromElement=function(e,...t){const n=m(...t);n.length>0&&e.classList.remove(...n)},exports.selectionAlwaysOnDisplay=function(e){let t=null;const n=()=>{const n=getSelection(),o=n&&n.anchorNode,r=e.getRootElement();null!==o&&null!==r&&r.contains(o)?null!==t&&(t(),t=null):null===t&&(t=E(e))};return document.addEventListener("selectionchange",n),()=>{null!==t&&t(),document.removeEventListener("selectionchange",n)}}; |
@@ -11,10 +11,10 @@ { | ||
"license": "MIT", | ||
"version": "0.24.0", | ||
"version": "0.24.1-nightly.20250210.0", | ||
"main": "LexicalUtils.js", | ||
"types": "index.d.ts", | ||
"dependencies": { | ||
"@lexical/list": "0.24.0", | ||
"@lexical/selection": "0.24.0", | ||
"@lexical/table": "0.24.0", | ||
"lexical": "0.24.0" | ||
"@lexical/list": "0.24.1-nightly.20250210.0", | ||
"@lexical/selection": "0.24.1-nightly.20250210.0", | ||
"@lexical/table": "0.24.1-nightly.20250210.0", | ||
"lexical": "0.24.1-nightly.20250210.0" | ||
}, | ||
@@ -21,0 +21,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
135559
2673
+ Added@lexical/clipboard@0.24.1-nightly.20250210.0(transitive)
+ Added@lexical/html@0.24.1-nightly.20250210.0(transitive)
+ Added@lexical/list@0.24.1-nightly.20250210.0(transitive)
+ Added@lexical/selection@0.24.1-nightly.20250210.0(transitive)
+ Added@lexical/table@0.24.1-nightly.20250210.0(transitive)
+ Addedlexical@0.24.1-nightly.20250210.0(transitive)
- Removed@lexical/clipboard@0.24.0(transitive)
- Removed@lexical/html@0.24.0(transitive)
- Removed@lexical/list@0.24.0(transitive)
- Removed@lexical/selection@0.24.0(transitive)
- Removed@lexical/table@0.24.0(transitive)
- Removedlexical@0.24.0(transitive)