@lexical/utils
Advanced tools
Comparing version 0.2.5 to 0.2.6
@@ -8,4 +8,4 @@ /** | ||
*/ | ||
import type {LexicalNode, ElementNode} from 'lexical'; | ||
export type DFSNode = $ReadOnly<{ | ||
import type {LexicalNode, ElementNode, LexicalEditor} from 'lexical'; | ||
export type DFSNode = Readonly<{ | ||
depth: number; | ||
@@ -20,3 +20,3 @@ node: LexicalNode; | ||
element: HTMLElement, | ||
...classNames: Array<string> | ||
...classNames: Array<typeof undefined | boolean | null | string> | ||
): void; | ||
@@ -30,11 +30,12 @@ declare function $dfs( | ||
node: LexicalNode, | ||
klass: Class<T>, | ||
klass: T, | ||
): T | null; | ||
export type DOMNodeToLexicalConversion = (element: Node) => LexicalNode; | ||
export type DOMNodeToLexicalConversionMap = { | ||
[string]: DOMNodeToLexicalConversion; | ||
}; | ||
export type DOMNodeToLexicalConversionMap = Record< | ||
string, | ||
DOMNodeToLexicalConversion | ||
>; | ||
declare function $findMatchingParent( | ||
startingNode: LexicalNode, | ||
findFn: (LexicalNode) => boolean, | ||
findFn: (node: LexicalNode) => boolean, | ||
): LexicalNode | null; | ||
@@ -46,1 +47,8 @@ type Func = () => void; | ||
): ElementNode; | ||
declare function registerNestedElementResolver<N extends ElementNode>( | ||
editor: LexicalEditor, | ||
targetNode: N, | ||
cloneNode: (from: N) => N, | ||
handleOverlap: (from: N, to: N) => void, | ||
): () => void; |
@@ -21,3 +21,3 @@ /** | ||
classNames.forEach(className => { | ||
if (className != null && typeof className === 'string') { | ||
if (typeof className === 'string') { | ||
element.classList.add(...className.split(' ')); | ||
@@ -29,3 +29,5 @@ } | ||
classNames.forEach(className => { | ||
element.classList.remove(...className.split(' ')); | ||
if (typeof className === 'string') { | ||
element.classList.remove(...className.split(' ')); | ||
} | ||
}); | ||
@@ -129,3 +131,72 @@ } | ||
} | ||
function registerNestedElementResolver(editor, targetNode, cloneNode, handleOverlap) { | ||
const $isTargetNode = node => { | ||
return node instanceof targetNode; | ||
}; | ||
const $findMatch = node => { | ||
// First validate we don't have any children that are of the target, | ||
// as we need to handle them first. | ||
const children = node.getChildren(); | ||
for (let i = 0; i < children.length; i++) { | ||
const child = children[i]; | ||
if ($isTargetNode(child)) { | ||
return null; | ||
} | ||
} | ||
let parentNode = node; | ||
let childNode = node; | ||
while (parentNode !== null) { | ||
childNode = parentNode; | ||
parentNode = parentNode.getParent(); | ||
if ($isTargetNode(parentNode)) { | ||
return { | ||
child: childNode, | ||
parent: parentNode | ||
}; | ||
} | ||
} | ||
return null; | ||
}; | ||
const elementNodeTransform = node => { | ||
const match = $findMatch(node); | ||
if (match !== null) { | ||
const { | ||
child, | ||
parent | ||
} = match; // Simple path, we can move child out and siblings into a new parent. | ||
if (child.is(node)) { | ||
handleOverlap(parent, node); | ||
const nextSiblings = child.getNextSiblings(); | ||
const nextSiblingsLength = nextSiblings.length; | ||
parent.insertAfter(child); | ||
if (nextSiblingsLength !== 0) { | ||
const newParent = cloneNode(parent); | ||
child.insertAfter(newParent); | ||
for (let i = 0; i < nextSiblingsLength; i++) { | ||
newParent.append(nextSiblings[i]); | ||
} | ||
} | ||
if (!parent.canBeEmpty() && parent.getChildrenSize() === 0) { | ||
parent.remove(); | ||
} | ||
} | ||
} | ||
}; | ||
return editor.registerNodeTransform(targetNode, elementNodeTransform); | ||
} | ||
exports.$dfs = $dfs; | ||
@@ -137,2 +208,3 @@ exports.$findMatchingParent = $findMatchingParent; | ||
exports.mergeRegister = mergeRegister; | ||
exports.registerNestedElementResolver = registerNestedElementResolver; | ||
exports.removeClassNamesFromElement = removeClassNamesFromElement; |
@@ -7,5 +7,7 @@ /** | ||
*/ | ||
var f=require("lexical");function g(a,b){for(;a!==f.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null} | ||
exports.$dfs=function(a,b){const c=[];a=(a||f.$getRoot()).getLatest();b=b||(f.$isElementNode(a)?a.getLastDescendant():a);for(var d=a,e=0;null!==(d=d.getParent());)e++;for(d=e;null!==a&&!a.is(b);)if(c.push({depth:d,node:a}),f.$isElementNode(a)&&0<a.getChildrenSize())a=a.getFirstChild(),d++;else for(e=null;null===e&&null!==a;)e=a.getNextSibling(),null===e?(a=a.getParent(),d--):a=e;null!==a&&a.is(b)&&c.push({depth:d,node:a});return c};exports.$findMatchingParent=g; | ||
exports.$getNearestBlockElementAncestorOrThrow=function(a){const b=g(a,c=>f.$isElementNode(c)&&!c.isInline());if(!f.$isElementNode(b))throw Error(`Expected node ${a.__key} to have closest block element node.`);return b};exports.$getNearestNodeOfType=function(a,b){for(;null!=a&&!(a instanceof b);)a=a.getParent();return a};exports.addClassNamesToElement=function(a,...b){b.forEach(c=>{null!=c&&"string"===typeof c&&a.classList.add(...c.split(" "))})}; | ||
exports.mergeRegister=function(...a){return()=>{a.forEach(b=>b())}};exports.removeClassNamesFromElement=function(a,...b){b.forEach(c=>{a.classList.remove(...c.split(" "))})}; | ||
var h=require("lexical");function n(a,b){for(;a!==h.$getRoot()&&null!=a;){if(b(a))return a;a=a.getParent()}return null} | ||
exports.$dfs=function(a,b){const e=[];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(e.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)&&e.push({depth:f,node:a});return e};exports.$findMatchingParent=n; | ||
exports.$getNearestBlockElementAncestorOrThrow=function(a){a=n(a,b=>h.$isElementNode(b)&&!b.isInline());if(!h.$isElementNode(a))throw Error("Minified Lexical error #3; see codes.json 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&&!(a instanceof b);)a=a.getParent();return a};exports.addClassNamesToElement=function(a,...b){b.forEach(e=>{"string"===typeof e&&a.classList.add(...e.split(" "))})}; | ||
exports.mergeRegister=function(...a){return()=>{a.forEach(b=>b())}}; | ||
exports.registerNestedElementResolver=function(a,b,e,f){return a.registerNodeTransform(b,d=>{a:{var c=d.getChildren();for(var g=0;g<c.length;g++)if(c[g]instanceof b){c=null;break a}for(c=d;null!==c;)if(g=c,c=c.getParent(),c instanceof b){c={child:g,parent:c};break a}c=null}if(null!==c){const {child:l,parent:k}=c;if(l.is(d)){f(k,d);d=l.getNextSiblings();c=d.length;k.insertAfter(l);if(0!==c){g=e(k);l.insertAfter(g);for(let m=0;m<c;m++)g.append(d[m])}k.canBeEmpty()||0!==k.getChildrenSize()||k.remove()}}})}; | ||
exports.removeClassNamesFromElement=function(a,...b){b.forEach(e=>{"string"===typeof e&&a.classList.remove(...e.split(" "))})}; |
@@ -11,10 +11,10 @@ { | ||
"license": "MIT", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"main": "LexicalUtils.js", | ||
"peerDependencies": { | ||
"lexical": "0.2.5" | ||
"lexical": "0.2.6" | ||
}, | ||
"dependencies": { | ||
"@lexical/list": "0.2.5", | ||
"@lexical/table": "0.2.5" | ||
"@lexical/list": "0.2.6", | ||
"@lexical/table": "0.2.6" | ||
}, | ||
@@ -21,0 +21,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
12300
243
+ Added@lexical/list@0.2.6(transitive)
+ Added@lexical/table@0.2.6(transitive)
+ Addedlexical@0.2.6(transitive)
- Removed@lexical/list@0.2.5(transitive)
- Removed@lexical/table@0.2.5(transitive)
- Removedlexical@0.2.5(transitive)
Updated@lexical/list@0.2.6
Updated@lexical/table@0.2.6