@lexical/utils
Advanced tools
Comparing version 0.7.8 to 0.7.9
@@ -9,3 +9,4 @@ /** @module @lexical/utils */ | ||
*/ | ||
import { EditorState, ElementNode, Klass, LexicalEditor, LexicalNode } from 'lexical'; | ||
import { $splitNode, EditorState, ElementNode, Klass, LexicalEditor, LexicalNode } from 'lexical'; | ||
export { $splitNode }; | ||
export declare type DFSNode = Readonly<{ | ||
@@ -45,3 +46,1 @@ depth: number; | ||
export declare function $wrapNodeInElement(node: LexicalNode, createElementNode: () => ElementNode): ElementNode; | ||
export declare function $splitNode(node: ElementNode, offset: number): [ElementNode | null, ElementNode]; | ||
export {}; |
@@ -9,2 +9,3 @@ /** | ||
var selection = require('@lexical/selection'); | ||
var lexical = require('lexical'); | ||
@@ -34,5 +35,5 @@ | ||
} | ||
return false; | ||
} | ||
/** | ||
@@ -49,3 +50,2 @@ * Lexical File Reader with: | ||
*/ | ||
function mediaFileReader(files, acceptableMimeTypes) { | ||
@@ -55,3 +55,2 @@ const filesIterator = files[Symbol.iterator](); | ||
const processed = []; | ||
const handleNextFile = () => { | ||
@@ -62,7 +61,5 @@ const { | ||
} = filesIterator.next(); | ||
if (done) { | ||
return resolve(processed); | ||
} | ||
const fileReader = new FileReader(); | ||
@@ -72,3 +69,2 @@ fileReader.addEventListener('error', reject); | ||
const result = fileReader.result; | ||
if (typeof result === 'string') { | ||
@@ -80,6 +76,4 @@ processed.push({ | ||
} | ||
handleNextFile(); | ||
}); | ||
if (isMimeType(file, acceptableMimeTypes)) { | ||
@@ -91,3 +85,2 @@ fileReader.readAsDataURL(file); | ||
}; | ||
handleNextFile(); | ||
@@ -102,3 +95,2 @@ }); | ||
let depth = $getDepth(node); | ||
while (node !== null && !node.is(end)) { | ||
@@ -109,3 +101,2 @@ nodes.push({ | ||
}); | ||
if (lexical.$isElementNode(node) && node.getChildrenSize() > 0) { | ||
@@ -117,6 +108,4 @@ node = node.getFirstChild(); | ||
let sibling = null; | ||
while (sibling === null && node !== null) { | ||
sibling = node.getNextSibling(); | ||
if (sibling === null) { | ||
@@ -131,3 +120,2 @@ node = node.getParent(); | ||
} | ||
if (node !== null && node.is(end)) { | ||
@@ -139,20 +127,14 @@ nodes.push({ | ||
} | ||
return nodes; | ||
} | ||
function $getDepth(node) { | ||
let innerNode = node; | ||
let depth = 0; | ||
while ((innerNode = innerNode.getParent()) !== null) { | ||
depth++; | ||
} | ||
return depth; | ||
} | ||
function $getNearestNodeOfType(node, klass) { | ||
let parent = node; | ||
while (parent != null) { | ||
@@ -162,6 +144,4 @@ if (parent instanceof klass) { | ||
} | ||
parent = parent.getParent(); | ||
} | ||
return null; | ||
@@ -171,3 +151,2 @@ } | ||
const blockNode = $findMatchingParent(startNode, node => lexical.$isElementNode(node) && !node.isInline()); | ||
if (!lexical.$isElementNode(blockNode)) { | ||
@@ -178,3 +157,2 @@ { | ||
} | ||
return blockNode; | ||
@@ -184,3 +162,2 @@ } | ||
let curr = startingNode; | ||
while (curr !== lexical.$getRoot() && curr != null) { | ||
@@ -190,6 +167,4 @@ if (findFn(curr)) { | ||
} | ||
curr = curr.getParent(); | ||
} | ||
return null; | ||
@@ -206,3 +181,2 @@ } | ||
}; | ||
const $findMatch = node => { | ||
@@ -212,6 +186,4 @@ // First validate we don't have any children that are of the target, | ||
const children = node.getChildren(); | ||
for (let i = 0; i < children.length; i++) { | ||
const child = children[i]; | ||
if ($isTargetNode(child)) { | ||
@@ -221,10 +193,7 @@ return null; | ||
} | ||
let parentNode = node; | ||
let childNode = node; | ||
while (parentNode !== null) { | ||
childNode = parentNode; | ||
parentNode = parentNode.getParent(); | ||
if ($isTargetNode(parentNode)) { | ||
@@ -237,9 +206,6 @@ return { | ||
} | ||
return null; | ||
}; | ||
const elementNodeTransform = node => { | ||
const match = $findMatch(node); | ||
if (match !== null) { | ||
@@ -249,4 +215,6 @@ const { | ||
parent | ||
} = match; // Simple path, we can move child out and siblings into a new parent. | ||
} = match; | ||
// Simple path, we can move child out and siblings into a new parent. | ||
if (child.is(node)) { | ||
@@ -257,7 +225,5 @@ handleOverlap(parent, node); | ||
parent.insertAfter(child); | ||
if (nextSiblingsLength !== 0) { | ||
const newParent = cloneNode(parent); | ||
child.insertAfter(newParent); | ||
for (let i = 0; i < nextSiblingsLength; i++) { | ||
@@ -267,3 +233,2 @@ newParent.append(nextSiblings[i]); | ||
} | ||
if (!parent.canBeEmpty() && parent.getChildrenSize() === 0) { | ||
@@ -275,3 +240,2 @@ parent.remove(); | ||
}; | ||
return editor.registerNodeTransform(targetNode, elementNodeTransform); | ||
@@ -283,19 +247,18 @@ } | ||
const activeEditorState = editor._pendingEditorState; | ||
for (const [key, node] of editorState._nodeMap) { | ||
// @ts-ignore | ||
nodeMap.set(key, node.constructor.clone(node)); | ||
const clone = selection.$cloneWithProperties(node); | ||
if (lexical.$isTextNode(clone)) { | ||
clone.__text = node.__text; | ||
} | ||
nodeMap.set(key, clone); | ||
} | ||
if (activeEditorState) { | ||
activeEditorState._nodeMap = nodeMap; | ||
} | ||
editor._dirtyType = FULL_RECONCILE; | ||
const selection = editorState._selection; | ||
lexical.$setSelection(selection === null ? null : selection.clone()); | ||
const selection$1 = editorState._selection; | ||
lexical.$setSelection(selection$1 === null ? null : selection$1.clone()); | ||
} | ||
function $insertNodeToNearestRoot(node) { | ||
const selection = lexical.$getSelection(); | ||
if (lexical.$isRangeSelection(selection)) { | ||
@@ -307,6 +270,4 @@ const { | ||
const focusOffset = focus.offset; | ||
if (lexical.$isRootOrShadowRoot(focusNode)) { | ||
const focusChild = focusNode.getChildAtIndex(focusOffset); | ||
if (focusChild == null) { | ||
@@ -317,3 +278,2 @@ focusNode.append(node); | ||
} | ||
node.selectNext(); | ||
@@ -323,7 +283,5 @@ } else { | ||
let splitOffset; | ||
if (lexical.$isTextNode(focusNode)) { | ||
splitNode = focusNode.getParentOrThrow(); | ||
splitOffset = focusNode.getIndexWithinParent(); | ||
if (focusOffset > 0) { | ||
@@ -337,4 +295,3 @@ splitOffset += 1; | ||
} | ||
const [, rightTree] = $splitNode(splitNode, splitOffset); | ||
const [, rightTree] = lexical.$splitNode(splitNode, splitOffset); | ||
rightTree.insertBefore(node); | ||
@@ -351,3 +308,2 @@ rightTree.selectStart(); | ||
} | ||
const paragraphNode = lexical.$createParagraphNode(); | ||
@@ -357,3 +313,2 @@ node.insertAfter(paragraphNode); | ||
} | ||
return node.getLatest(); | ||
@@ -367,35 +322,4 @@ } | ||
} | ||
function $splitNode(node, offset) { | ||
let startNode = node.getChildAtIndex(offset); | ||
if (startNode == null) { | ||
startNode = node; | ||
} | ||
if (!!lexical.$isRootOrShadowRoot(node)) { | ||
throw Error(`Can not call $splitNode() on root element`); | ||
} | ||
const recurse = currentNode => { | ||
const parent = currentNode.getParentOrThrow(); | ||
const isParentRoot = lexical.$isRootOrShadowRoot(parent); // The node we start split from (leaf) is moved, but its recursive | ||
// parents are copied to create separate tree | ||
const nodeToMove = currentNode === startNode && !isParentRoot ? currentNode : lexical.$copyNode(currentNode); | ||
if (isParentRoot) { | ||
currentNode.insertAfter(nodeToMove); | ||
return [currentNode, nodeToMove, nodeToMove]; | ||
} else { | ||
const [leftTree, rightTree, newParent] = recurse(parent); | ||
const nextSiblings = currentNode.getNextSiblings(); | ||
newParent.append(nodeToMove, ...nextSiblings); | ||
return [leftTree, rightTree, nodeToMove]; | ||
} | ||
}; | ||
const [leftTree, rightTree] = recurse(startNode); | ||
return [leftTree, rightTree]; | ||
} | ||
exports.$splitNode = lexical.$splitNode; | ||
exports.$dfs = $dfs; | ||
@@ -407,3 +331,2 @@ exports.$findMatchingParent = $findMatchingParent; | ||
exports.$restoreEditorState = $restoreEditorState; | ||
exports.$splitNode = $splitNode; | ||
exports.$wrapNodeInElement = $wrapNodeInElement; | ||
@@ -410,0 +333,0 @@ exports.addClassNamesToElement = addClassNamesToElement; |
@@ -7,11 +7,10 @@ /** | ||
*/ | ||
'use strict';var h=require("lexical");function p(a){throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?code=${a} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}function q(a,b){for(let c of b)if(a.type.startsWith(c))return!0;return!1}function r(a,b){for(;a!==h.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null} | ||
function t(a,b){let c=a.getChildAtIndex(b);null==c&&(c=a);h.$isRootOrShadowRoot(a)&&p(102);let f=g=>{const m=g.getParentOrThrow(),l=h.$isRootOrShadowRoot(m),k=g!==c||l?h.$copyNode(g):g;if(l)return g.insertAfter(k),[g,k,k];const [n,u,v]=f(m);g=g.getNextSiblings();v.append(k,...g);return[n,u,k]},[d,e]=f(c);return[d,e]} | ||
exports.$dfs=function(a,b){let c=[];a=(a||h.$getRoot()).getLatest();b=b||(h.$isElementNode(a)?a.getLastDescendant():a);for(var f=a,d=0;null!==(f=f.getParent());)d++;for(f=d;null!==a&&!a.is(b);)if(c.push({depth:f,node:a}),h.$isElementNode(a)&&0<a.getChildrenSize())a=a.getFirstChild(),f++;else for(d=null;null===d&&null!==a;)d=a.getNextSibling(),null===d?(a=a.getParent(),f--):a=d;null!==a&&a.is(b)&&c.push({depth:f,node:a});return c};exports.$findMatchingParent=r; | ||
exports.$getNearestBlockElementAncestorOrThrow=function(a){a=r(a,b=>h.$isElementNode(b)&&!b.isInline());h.$isElementNode(a)||p(4);return a};exports.$getNearestNodeOfType=function(a,b){for(;null!=a;){if(a instanceof b)return a;a=a.getParent()}return null}; | ||
exports.$insertNodeToNearestRoot=function(a){var b=h.$getSelection();if(h.$isRangeSelection(b)){var {focus:c}=b;b=c.getNode();c=c.offset;if(h.$isRootOrShadowRoot(b))c=b.getChildAtIndex(c),null==c?b.append(a):c.insertBefore(a),a.selectNext();else{let f,d;h.$isTextNode(b)?(f=b.getParentOrThrow(),d=b.getIndexWithinParent(),0<c&&(d+=1,b.splitText(c))):(f=b,d=c);[,b]=t(f,d);b.insertBefore(a);b.selectStart()}}else h.$isNodeSelection(b)||h.DEPRECATED_$isGridSelection(b)?(b=b.getNodes(),b[b.length-1].getTopLevelElementOrThrow().insertAfter(a)): | ||
h.$getRoot().append(a),b=h.$createParagraphNode(),a.insertAfter(b),b.select();return a.getLatest()};exports.$restoreEditorState=function(a,b){let c=new Map,f=a._pendingEditorState;for(let [d,e]of b._nodeMap)c.set(d,e.constructor.clone(e));f&&(f._nodeMap=c);a._dirtyType=2;a=b._selection;h.$setSelection(null===a?null:a.clone())};exports.$splitNode=t;exports.$wrapNodeInElement=function(a,b){b=b();a.replace(b);b.append(a);return b}; | ||
exports.addClassNamesToElement=function(a,...b){b.forEach(c=>{"string"===typeof c&&(c=c.split(" ").filter(f=>""!==f),a.classList.add(...c))})};exports.isMimeType=q; | ||
exports.mediaFileReader=function(a,b){let c=a[Symbol.iterator]();return new Promise((f,d)=>{let e=[],g=()=>{const {done:m,value:l}=c.next();if(m)return f(e);const k=new FileReader;k.addEventListener("error",d);k.addEventListener("load",()=>{const n=k.result;"string"===typeof n&&e.push({file:l,result:n});g()});q(l,b)?k.readAsDataURL(l):g()};g()})};exports.mergeRegister=function(...a){return()=>{a.forEach(b=>b())}}; | ||
exports.registerNestedElementResolver=function(a,b,c,f){return a.registerNodeTransform(b,d=>{a:{var e=d.getChildren();for(var g=0;g<e.length;g++)if(e[g]instanceof b){e=null;break a}for(e=d;null!==e;)if(g=e,e=e.getParent(),e instanceof b){e={child:g,parent:e};break a}e=null}if(null!==e){const {child:m,parent:l}=e;if(m.is(d)){f(l,d);d=m.getNextSiblings();e=d.length;l.insertAfter(m);if(0!==e){g=c(l);m.insertAfter(g);for(let k=0;k<e;k++)g.append(d[k])}l.canBeEmpty()||0!==l.getChildrenSize()||l.remove()}}})}; | ||
'use strict';var g=require("@lexical/selection"),n=require("lexical");function p(a,b){for(let c of b)if(a.type.startsWith(c))return!0;return!1}function r(a,b){for(;a!==n.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null}exports.$splitNode=n.$splitNode; | ||
exports.$dfs=function(a,b){let c=[];a=(a||n.$getRoot()).getLatest();b=b||(n.$isElementNode(a)?a.getLastDescendant():a);for(var f=a,d=0;null!==(f=f.getParent());)d++;for(f=d;null!==a&&!a.is(b);)if(c.push({depth:f,node:a}),n.$isElementNode(a)&&0<a.getChildrenSize())a=a.getFirstChild(),f++;else for(d=null;null===d&&null!==a;)d=a.getNextSibling(),null===d?(a=a.getParent(),f--):a=d;null!==a&&a.is(b)&&c.push({depth:f,node:a});return c};exports.$findMatchingParent=r; | ||
exports.$getNearestBlockElementAncestorOrThrow=function(a){a=r(a,b=>n.$isElementNode(b)&&!b.isInline());if(!n.$isElementNode(a))throw Error("Minified Lexical error #4; visit https://lexical.dev/docs/error?code=4 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.");return a};exports.$getNearestNodeOfType=function(a,b){for(;null!=a;){if(a instanceof b)return a;a=a.getParent()}return null}; | ||
exports.$insertNodeToNearestRoot=function(a){var b=n.$getSelection();if(n.$isRangeSelection(b)){var {focus:c}=b;b=c.getNode();c=c.offset;if(n.$isRootOrShadowRoot(b))c=b.getChildAtIndex(c),null==c?b.append(a):c.insertBefore(a),a.selectNext();else{let f,d;n.$isTextNode(b)?(f=b.getParentOrThrow(),d=b.getIndexWithinParent(),0<c&&(d+=1,b.splitText(c))):(f=b,d=c);[,b]=n.$splitNode(f,d);b.insertBefore(a);b.selectStart()}}else n.$isNodeSelection(b)||n.DEPRECATED_$isGridSelection(b)?(b=b.getNodes(),b[b.length- | ||
1].getTopLevelElementOrThrow().insertAfter(a)):n.$getRoot().append(a),b=n.$createParagraphNode(),a.insertAfter(b),b.select();return a.getLatest()};exports.$restoreEditorState=function(a,b){let c=new Map,f=a._pendingEditorState;for(let [d,e]of b._nodeMap){let h=g.$cloneWithProperties(e);n.$isTextNode(h)&&(h.__text=e.__text);c.set(d,h)}f&&(f._nodeMap=c);a._dirtyType=2;a=b._selection;n.$setSelection(null===a?null:a.clone())};exports.$wrapNodeInElement=function(a,b){b=b();a.replace(b);b.append(a);return b}; | ||
exports.addClassNamesToElement=function(a,...b){b.forEach(c=>{"string"===typeof c&&(c=c.split(" ").filter(f=>""!==f),a.classList.add(...c))})};exports.isMimeType=p; | ||
exports.mediaFileReader=function(a,b){let c=a[Symbol.iterator]();return new Promise((f,d)=>{let e=[],h=()=>{const {done:m,value:k}=c.next();if(m)return f(e);const l=new FileReader;l.addEventListener("error",d);l.addEventListener("load",()=>{const q=l.result;"string"===typeof q&&e.push({file:k,result:q});h()});p(k,b)?l.readAsDataURL(k):h()};h()})};exports.mergeRegister=function(...a){return()=>{a.forEach(b=>b())}}; | ||
exports.registerNestedElementResolver=function(a,b,c,f){return a.registerNodeTransform(b,d=>{a:{var e=d.getChildren();for(var h=0;h<e.length;h++)if(e[h]instanceof b){e=null;break a}for(e=d;null!==e;)if(h=e,e=e.getParent(),e instanceof b){e={child:h,parent:e};break a}e=null}if(null!==e){const {child:m,parent:k}=e;if(m.is(d)){f(k,d);d=m.getNextSiblings();e=d.length;k.insertAfter(m);if(0!==e){h=c(k);m.insertAfter(h);for(let l=0;l<e;l++)h.append(d[l])}k.canBeEmpty()||0!==k.getChildrenSize()||k.remove()}}})}; | ||
exports.removeClassNamesFromElement=function(a,...b){b.forEach(c=>{"string"===typeof c&&a.classList.remove(...c.split(" "))})} |
@@ -11,10 +11,11 @@ { | ||
"license": "MIT", | ||
"version": "0.7.8", | ||
"version": "0.7.9", | ||
"main": "LexicalUtils.js", | ||
"peerDependencies": { | ||
"lexical": "0.7.8" | ||
"lexical": "0.7.9" | ||
}, | ||
"dependencies": { | ||
"@lexical/list": "0.7.8", | ||
"@lexical/table": "0.7.8" | ||
"@lexical/list": "0.7.9", | ||
"@lexical/table": "0.7.9", | ||
"@lexical/selection": "0.7.9" | ||
}, | ||
@@ -21,0 +22,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19746
4
371
+ Added@lexical/selection@0.7.9
+ Added@lexical/list@0.7.9(transitive)
+ Added@lexical/selection@0.7.9(transitive)
+ Added@lexical/table@0.7.9(transitive)
+ Addedlexical@0.7.9(transitive)
- Removed@lexical/list@0.7.8(transitive)
- Removed@lexical/table@0.7.8(transitive)
- Removedlexical@0.7.8(transitive)
Updated@lexical/list@0.7.9
Updated@lexical/table@0.7.9