@lexical/utils
Advanced tools
Comparing version 0.9.0 to 0.9.1
113
index.d.ts
@@ -15,4 +15,28 @@ /** @module @lexical/utils */ | ||
}>; | ||
/** | ||
* Takes an HTML element and adds the classNames passed within an array, | ||
* ignoring any non-string types. A space can be used to add multiple classes | ||
* eg. addClassNamesToElement(element, ['element-inner active', true, null]) | ||
* will add both 'element-inner' and 'active' as classes to that element. | ||
* @param element - The element in which the classes are added | ||
* @param classNames - An array defining the class names to add to the element | ||
*/ | ||
export declare function addClassNamesToElement(element: HTMLElement, ...classNames: Array<typeof undefined | boolean | null | string>): void; | ||
/** | ||
* Takes an HTML element and removes the classNames passed within an array, | ||
* ignoring any non-string types. A space can be used to remove multiple classes | ||
* eg. removeClassNamesFromElement(element, ['active small', true, null]) | ||
* will remove both the 'active' and 'small' classes from that element. | ||
* @param element - The element in which the classes are removed | ||
* @param classNames - An array defining the class names to remove from the element | ||
*/ | ||
export declare function removeClassNamesFromElement(element: HTMLElement, ...classNames: Array<typeof undefined | boolean | null | string>): void; | ||
/** | ||
* Returns true if the file type matches the types passed within the acceptableMimeTypes array, false otherwise. | ||
* The types passed must be strings and are CASE-SENSITIVE. | ||
* eg. if file is of type 'text' and acceptableMimeTypes = ['TEXT', 'IMAGE'] the function will return false. | ||
* @param file - The file you want to type check. | ||
* @param acceptableMimeTypes - An array of strings of types which the file is checked against. | ||
* @returns true if the file is an acceptable mime type, false otherwise. | ||
*/ | ||
export declare function isMimeType(file: File, acceptableMimeTypes: Array<string>): boolean; | ||
@@ -34,15 +58,104 @@ /** | ||
}>>; | ||
/** | ||
* "Depth-First Search" starts at the root/top node of a tree and goes as far as it can down a branch end | ||
* before backtracking and finding a new path. Consider solving a maze by hugging either wall, moving down a | ||
* branch until you hit a dead-end (leaf) and backtracking to find the nearest branching path and repeat. | ||
* It will then return all the nodes found in the search in an array of objects. | ||
* @param startingNode - The node to start the search, if ommitted, it will start at the root node. | ||
* @param endingNode - The node to end the search, if ommitted, it will find all descendants of the startingNode. | ||
* @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 ending node) so long as it exists | ||
*/ | ||
export declare function $dfs(startingNode?: LexicalNode, endingNode?: LexicalNode): Array<DFSNode>; | ||
/** | ||
* Takes a node and traverses up its ancestors (toward the root node) | ||
* in order to find a specific type of node. | ||
* @param node - the node to begin searching. | ||
* @param klass - an instance of the type of node to look for. | ||
* @returns the node of type klass that was passed, or null if none exist. | ||
*/ | ||
export declare function $getNearestNodeOfType<T extends ElementNode>(node: LexicalNode, klass: Klass<T>): T | null; | ||
/** | ||
*Returns the element node of the nearest ancestor, otherwise throws an error. | ||
* @param startNode - The starting node of the search | ||
* @returns The ancestor node found | ||
*/ | ||
export declare function $getNearestBlockElementAncestorOrThrow(startNode: LexicalNode): ElementNode; | ||
export declare type DOMNodeToLexicalConversion = (element: Node) => LexicalNode; | ||
export declare type DOMNodeToLexicalConversionMap = Record<string, DOMNodeToLexicalConversion>; | ||
/** | ||
* Starts with a node and moves up the tree (toward the root node) to find a matching node based on | ||
* the search parameters of the findFn. (Consider JavaScripts' .find() function where a testing function must be | ||
* passed as an argument. eg. if( (node) => node.__type === 'div') ) return true; otherwise return false | ||
* @param startingNode - The node where the search starts. | ||
* @param findFn - A testing function that returns true if the current node satisfies the testing parameters. | ||
* @returns A parent node that matches the findFn parameters, or null if one wasn't found. | ||
*/ | ||
export declare function $findMatchingParent(startingNode: LexicalNode, findFn: (node: LexicalNode) => boolean): LexicalNode | null; | ||
declare type Func = () => void; | ||
/** | ||
* Returns a function that will execute all functions passed when called. It is generally used | ||
* to register multiple lexical listeners and then tear them down with a single function call, such | ||
* as React's useEffect hook. | ||
* @example | ||
* ```ts | ||
* useEffect(() => { | ||
* return mergeRegister( | ||
* editor.registerCommand(...registerCommand1 logic), | ||
* editor.registerCommand(...registerCommand2 logic), | ||
* editor.registerCommand(...registerCommand3 logic) | ||
* ) | ||
* }, [editor]) | ||
* ``` | ||
* In this case, useEffect is returning the function returned by mergeRegister as a cleanup | ||
* function to be executed after either the useEffect runs again (due to one of its dependencies | ||
* updating) or the compenent it resides in unmounts. | ||
* Note the functions don't neccesarily need to be in an array as all arguements | ||
* are considered to be the func argument and spread from there. | ||
* @param func - An array of functions meant to be executed by the returned function. | ||
* @returns the function which executes all the passed register command functions. | ||
*/ | ||
export declare function mergeRegister(...func: Array<Func>): () => void; | ||
/** | ||
* Attempts to resolve nested element nodes of the same type into a single node of that type. | ||
* It is generally used for marks/commenting | ||
* @param editor - The lexical editor | ||
* @param targetNode - The target for the nested element to be extracted from. | ||
* @param cloneNode - See {@link $createMarkNode} | ||
* @param handleOverlap - Handles any overlap between the node to extract and the targetNode | ||
* @returns The lexical editor | ||
*/ | ||
export declare function registerNestedElementResolver<N extends ElementNode>(editor: LexicalEditor, targetNode: Klass<N>, cloneNode: (from: N) => N, handleOverlap: (from: N, to: N) => void): () => void; | ||
/** | ||
* Clones the editor and marks it as dirty to be reconciled. If there was a selection, | ||
* it would be set back to its previous state, or null otherwise. | ||
* @param editor - The lexical editor | ||
* @param editorState - The editor's state | ||
*/ | ||
export declare function $restoreEditorState(editor: LexicalEditor, editorState: EditorState): void; | ||
/** | ||
* If the selected insertion area is the root/shadow root node (see {@link lexical!$isRootOrShadowRoot}), | ||
* the node will be appended there, otherwise, it will be inserted before the insertion area. | ||
* If there is no selection where the node is to be inserted, it will be appended after any current nodes | ||
* within the tree, as a child of the root node. A paragraph node will then be added after the inserted node and selected. | ||
* @param node - The node to be inserted | ||
* @returns The node after its insertion | ||
*/ | ||
export declare function $insertNodeToNearestRoot<T extends LexicalNode>(node: T): T; | ||
/** | ||
* Wraps the node into another node created from a createElementNode function, eg. $createParagraphNode | ||
* @param node - Node to be wrapped. | ||
* @param createElementNode - Creates a new lexcial element to wrap the to-be-wrapped node and returns it. | ||
* @returns A new lexcial element with the previous node appended within (as a child, including its children). | ||
*/ | ||
export declare function $wrapNodeInElement(node: LexicalNode, createElementNode: () => ElementNode): ElementNode; | ||
/** | ||
* @param x - The element being tested | ||
* @returns Returns true if x is an HTML anchor tag, false otherwise | ||
*/ | ||
export declare function isHTMLAnchorElement(x: Node): x is HTMLAnchorElement; | ||
/** | ||
* @param x - The element being testing | ||
* @returns Returns true if x is an HTML element, false otherwise. | ||
*/ | ||
export declare function isHTMLElement(x: Node | EventTarget): x is HTMLElement; |
@@ -13,2 +13,11 @@ /** | ||
/** @module @lexical/utils */ | ||
/** | ||
* Takes an HTML element and adds the classNames passed within an array, | ||
* ignoring any non-string types. A space can be used to add multiple classes | ||
* eg. addClassNamesToElement(element, ['element-inner active', true, null]) | ||
* will add both 'element-inner' and 'active' as classes to that element. | ||
* @param element - The element in which the classes are added | ||
* @param classNames - An array defining the class names to add to the element | ||
*/ | ||
function addClassNamesToElement(element, ...classNames) { | ||
@@ -22,2 +31,11 @@ classNames.forEach(className => { | ||
} | ||
/** | ||
* Takes an HTML element and removes the classNames passed within an array, | ||
* ignoring any non-string types. A space can be used to remove multiple classes | ||
* eg. removeClassNamesFromElement(element, ['active small', true, null]) | ||
* will remove both the 'active' and 'small' classes from that element. | ||
* @param element - The element in which the classes are removed | ||
* @param classNames - An array defining the class names to remove from the element | ||
*/ | ||
function removeClassNamesFromElement(element, ...classNames) { | ||
@@ -30,2 +48,11 @@ classNames.forEach(className => { | ||
} | ||
/** | ||
* Returns true if the file type matches the types passed within the acceptableMimeTypes array, false otherwise. | ||
* The types passed must be strings and are CASE-SENSITIVE. | ||
* eg. if file is of type 'text' and acceptableMimeTypes = ['TEXT', 'IMAGE'] the function will return false. | ||
* @param file - The file you want to type check. | ||
* @param acceptableMimeTypes - An array of strings of types which the file is checked against. | ||
* @returns true if the file is an acceptable mime type, false otherwise. | ||
*/ | ||
function isMimeType(file, acceptableMimeTypes) { | ||
@@ -92,2 +119,13 @@ for (const acceptableType of acceptableMimeTypes) { | ||
} | ||
/** | ||
* "Depth-First Search" starts at the root/top node of a tree and goes as far as it can down a branch end | ||
* before backtracking and finding a new path. Consider solving a maze by hugging either wall, moving down a | ||
* branch until you hit a dead-end (leaf) and backtracking to find the nearest branching path and repeat. | ||
* It will then return all the nodes found in the search in an array of objects. | ||
* @param startingNode - The node to start the search, if ommitted, it will start at the root node. | ||
* @param endingNode - The node to end the search, if ommitted, it will find all descendants of the startingNode. | ||
* @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 ending node) so long as it exists | ||
*/ | ||
function $dfs(startingNode, endingNode) { | ||
@@ -146,3 +184,11 @@ const nodes = []; | ||
} | ||
/** | ||
* Takes a node and traverses up its ancestors (toward the root node) | ||
* in order to find a specific type of node. | ||
* @param node - the node to begin searching. | ||
* @param klass - an instance of the type of node to look for. | ||
* @returns the node of type klass that was passed, or null if none exist. | ||
*/ | ||
function $getNearestNodeOfType(node, klass) { | ||
@@ -161,2 +207,8 @@ let parent = node; | ||
} | ||
/** | ||
*Returns the element node of the nearest ancestor, otherwise throws an error. | ||
* @param startNode - The starting node of the search | ||
* @returns The ancestor node found | ||
*/ | ||
function $getNearestBlockElementAncestorOrThrow(startNode) { | ||
@@ -173,2 +225,11 @@ const blockNode = $findMatchingParent(startNode, node => lexical.$isElementNode(node) && !node.isInline()); | ||
} | ||
/** | ||
* Starts with a node and moves up the tree (toward the root node) to find a matching node based on | ||
* the search parameters of the findFn. (Consider JavaScripts' .find() function where a testing function must be | ||
* passed as an argument. eg. if( (node) => node.__type === 'div') ) return true; otherwise return false | ||
* @param startingNode - The node where the search starts. | ||
* @param findFn - A testing function that returns true if the current node satisfies the testing parameters. | ||
* @returns A parent node that matches the findFn parameters, or null if one wasn't found. | ||
*/ | ||
function $findMatchingParent(startingNode, findFn) { | ||
@@ -187,2 +248,25 @@ let curr = startingNode; | ||
} | ||
/** | ||
* Returns a function that will execute all functions passed when called. It is generally used | ||
* to register multiple lexical listeners and then tear them down with a single function call, such | ||
* as React's useEffect hook. | ||
* @example | ||
* ```ts | ||
* useEffect(() => { | ||
* return mergeRegister( | ||
* editor.registerCommand(...registerCommand1 logic), | ||
* editor.registerCommand(...registerCommand2 logic), | ||
* editor.registerCommand(...registerCommand3 logic) | ||
* ) | ||
* }, [editor]) | ||
* ``` | ||
* In this case, useEffect is returning the function returned by mergeRegister as a cleanup | ||
* function to be executed after either the useEffect runs again (due to one of its dependencies | ||
* updating) or the compenent it resides in unmounts. | ||
* Note the functions don't neccesarily need to be in an array as all arguements | ||
* are considered to be the func argument and spread from there. | ||
* @param func - An array of functions meant to be executed by the returned function. | ||
* @returns the function which executes all the passed register command functions. | ||
*/ | ||
function mergeRegister(...func) { | ||
@@ -193,2 +277,12 @@ return () => { | ||
} | ||
/** | ||
* Attempts to resolve nested element nodes of the same type into a single node of that type. | ||
* It is generally used for marks/commenting | ||
* @param editor - The lexical editor | ||
* @param targetNode - The target for the nested element to be extracted from. | ||
* @param cloneNode - See {@link $createMarkNode} | ||
* @param handleOverlap - Handles any overlap between the node to extract and the targetNode | ||
* @returns The lexical editor | ||
*/ | ||
function registerNestedElementResolver(editor, targetNode, cloneNode, handleOverlap) { | ||
@@ -263,2 +357,9 @@ const $isTargetNode = node => { | ||
} | ||
/** | ||
* Clones the editor and marks it as dirty to be reconciled. If there was a selection, | ||
* it would be set back to its previous state, or null otherwise. | ||
* @param editor - The lexical editor | ||
* @param editorState - The editor's state | ||
*/ | ||
function $restoreEditorState(editor, editorState) { | ||
@@ -287,2 +388,11 @@ const FULL_RECONCILE = 2; | ||
} | ||
/** | ||
* If the selected insertion area is the root/shadow root node (see {@link lexical!$isRootOrShadowRoot}), | ||
* the node will be appended there, otherwise, it will be inserted before the insertion area. | ||
* If there is no selection where the node is to be inserted, it will be appended after any current nodes | ||
* within the tree, as a child of the root node. A paragraph node will then be added after the inserted node and selected. | ||
* @param node - The node to be inserted | ||
* @returns The node after its insertion | ||
*/ | ||
function $insertNodeToNearestRoot(node) { | ||
@@ -345,2 +455,9 @@ const selection = lexical.$getSelection(); | ||
} | ||
/** | ||
* Wraps the node into another node created from a createElementNode function, eg. $createParagraphNode | ||
* @param node - Node to be wrapped. | ||
* @param createElementNode - Creates a new lexcial element to wrap the to-be-wrapped node and returns it. | ||
* @returns A new lexcial element with the previous node appended within (as a child, including its children). | ||
*/ | ||
function $wrapNodeInElement(node, createElementNode) { | ||
@@ -352,5 +469,15 @@ const elementNode = createElementNode(); | ||
} | ||
/** | ||
* @param x - The element being tested | ||
* @returns Returns true if x is an HTML anchor tag, false otherwise | ||
*/ | ||
function isHTMLAnchorElement(x) { | ||
return isHTMLElement(x) && x.tagName === 'A'; | ||
} | ||
/** | ||
* @param x - The element being testing | ||
* @returns Returns true if x is an HTML element, false otherwise. | ||
*/ | ||
function isHTMLElement(x) { | ||
@@ -357,0 +484,0 @@ // @ts-ignore-next-line - strict check on nodeType here should filter out non-Element EventTarget implementors |
@@ -11,11 +11,11 @@ { | ||
"license": "MIT", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"main": "LexicalUtils.js", | ||
"peerDependencies": { | ||
"lexical": "0.9.0" | ||
"lexical": "0.9.1" | ||
}, | ||
"dependencies": { | ||
"@lexical/list": "0.9.0", | ||
"@lexical/table": "0.9.0", | ||
"@lexical/selection": "0.9.0" | ||
"@lexical/list": "0.9.1", | ||
"@lexical/table": "0.9.1", | ||
"@lexical/selection": "0.9.1" | ||
}, | ||
@@ -22,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
32834
607
+ Added@lexical/list@0.9.1(transitive)
+ Added@lexical/selection@0.9.1(transitive)
+ Added@lexical/table@0.9.1(transitive)
+ Addedlexical@0.9.1(transitive)
- Removed@lexical/list@0.9.0(transitive)
- Removed@lexical/selection@0.9.0(transitive)
- Removed@lexical/table@0.9.0(transitive)
- Removedlexical@0.9.0(transitive)
Updated@lexical/list@0.9.1
Updated@lexical/selection@0.9.1
Updated@lexical/table@0.9.1