Module: @lexical/utils
References
$splitNode
Re-exports $splitNode
isHTMLAnchorElement
Re-exports isHTMLAnchorElement
isHTMLElement
Re-exports isHTMLElement
Type Aliases
DFSNode
Ƭ DFSNode: Readonly
<{ depth
: number
; node
: LexicalNode
}>
Defined in
DOMNodeToLexicalConversion
Ƭ DOMNodeToLexicalConversion: (element
: Node
) => LexicalNode
Type declaration
▸ (element
): LexicalNode
Parameters
Name | Type |
---|---|
element | Node |
Returns
Defined in
lexical-utils/src/index.ts:249
DOMNodeToLexicalConversionMap
Ƭ DOMNodeToLexicalConversionMap: Record
<string
, DOMNodeToLexicalConversion
>
Defined in
lexical-utils/src/index.ts:251
Functions
$dfs
▸ $dfs(startingNode?
, endingNode?
): DFSNode
[]
"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.
Parameters
Name | Type | Description |
---|---|---|
startingNode? | LexicalNode | The node to start the search, if ommitted, it will start at the root node. |
endingNode? | LexicalNode | The node to end the search, if ommitted, it will find all descendants of the startingNode. |
Returns
DFSNode
[]
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
Defined in
lexical-utils/src/index.ts:151
$filter
▸ $filter<T
>(nodes
, filterFn
): T
[]
Filter the nodes
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
nodes | LexicalNode [] | Array of nodes that needs to be filtered |
filterFn | (node : LexicalNode ) => null | T | A filter function that returns node if the current node satisfies the condition otherwise null |
Returns
T
[]
Array of filtered nodes
Defined in
lexical-utils/src/index.ts:503
$findMatchingParent
▸ $findMatchingParent<T
>(startingNode
, findFn
): null
| T
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
Type parameters
Name | Type |
---|---|
T | extends LexicalNode |
Parameters
Name | Type | Description |
---|---|---|
startingNode | LexicalNode | The node where the search starts. |
findFn | (node : LexicalNode ) => node is T | A testing function that returns true if the current node satisfies the testing parameters. |
Returns
null
| T
A parent node that matches the findFn parameters, or null if one wasn't found.
Defined in
lexical-utils/src/index.ts:265
▸ $findMatchingParent(startingNode
, findFn
): null
| LexicalNode
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
Parameters
Name | Type | Description |
---|---|---|
startingNode | LexicalNode | The node where the search starts. |
findFn | (node : LexicalNode ) => boolean | A testing function that returns true if the current node satisfies the testing parameters. |
Returns
null
| LexicalNode
A parent node that matches the findFn parameters, or null if one wasn't found.
Defined in
lexical-utils/src/index.ts:269
$getNearestBlockElementAncestorOrThrow
▸ $getNearestBlockElementAncestorOrThrow(startNode
): ElementNode
Returns the element node of the nearest ancestor, otherwise throws an error.
Parameters
Name | Type | Description |
---|---|---|
startNode | LexicalNode | The starting node of the search |
Returns
The ancestor node found
Defined in
lexical-utils/src/index.ts:232
$getNearestNodeOfType
▸ $getNearestNodeOfType<T
>(node
, klass
): T
| null
Takes a node and traverses up its ancestors (toward the root node) in order to find a specific type of node.
Type parameters
Name | Type |
---|---|
T | extends ElementNode |
Parameters
Name | Type | Description |
---|---|---|
node | LexicalNode | the node to begin searching. |
klass | Klass <T > | an instance of the type of node to look for. |
Returns
T
| null
the node of type klass that was passed, or null if none exist.
Defined in
lexical-utils/src/index.ts:210
$insertFirst
▸ $insertFirst(parent
, node
): void
Appends the node before the first child of the parent node
Parameters
Name | Type | Description |
---|---|---|
parent | ElementNode | A parent node |
node | LexicalNode | Node that needs to be appended |
Returns
void
Defined in
lexical-utils/src/index.ts:521
$insertNodeToNearestRoot
▸ $insertNodeToNearestRoot<T
>(node
): T
If the selected insertion area is the root/shadow root node (see $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.
Type parameters
Name | Type |
---|---|
T | extends LexicalNode |
Parameters
Name | Type | Description |
---|---|---|
node | T | The node to be inserted |
Returns
T
The node after its insertion
Defined in
lexical-utils/src/index.ts:414
$restoreEditorState
▸ $restoreEditorState(editor
, editorState
): 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.
Parameters
Name | Type | Description |
---|---|---|
editor | LexicalEditor | The lexical editor |
editorState | EditorState | The editor's state |
Returns
void
Defined in
lexical-utils/src/index.ts:380
$wrapNodeInElement
▸ $wrapNodeInElement(node
, createElementNode
): ElementNode
Wraps the node into another node created from a createElementNode function, eg. $createParagraphNode
Parameters
Name | Type | Description |
---|---|---|
node | LexicalNode | Node to be wrapped. |
createElementNode | () => ElementNode | Creates a new lexical element to wrap the to-be-wrapped node and returns it. |
Returns
A new lexical element with the previous node appended within (as a child, including its children).
Defined in
lexical-utils/src/index.ts:469
addClassNamesToElement
▸ addClassNamesToElement(element
, ...classNames
): void
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.
Parameters
Name | Type | Description |
---|---|---|
element | HTMLElement | The element in which the classes are added |
...classNames | (undefined | null | string | boolean )[] | An array defining the class names to add to the element |
Returns
void
Defined in
isMimeType
▸ isMimeType(file
, acceptableMimeTypes
): boolean
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.
Parameters
Name | Type | Description |
---|---|---|
file | File | The file you want to type check. |
acceptableMimeTypes | string [] | An array of strings of types which the file is checked against. |
Returns
boolean
true if the file is an acceptable mime type, false otherwise.
Defined in
markSelection
▸ markSelection(editor
, onReposition?
): () => void
Parameters
Name | Type |
---|---|
editor | LexicalEditor |
onReposition? | (node : HTMLElement []) => void |
Returns
fn
▸ (): void
Returns
void
Defined in
lexical-utils/src/markSelection.ts:23
mediaFileReader
▸ mediaFileReader(files
, acceptableMimeTypes
): Promise
<{ file
: File
; result
: string
}[]>
Lexical File Reader with:
- MIME type support
- batched results (HistoryPlugin compatibility)
- Order aware (respects the order when multiple Files are passed)
const filesResult = await mediaFileReader(files, ['image/']); filesResult.forEach(file => editor.dispatchCommand('INSERT_IMAGE', { src: file.result, }));
Parameters
Name | Type |
---|---|
files | File [] |
acceptableMimeTypes | string [] |
Returns
Promise
<{ file
: File
; result
: string
}[]>
Defined in
lexical-utils/src/index.ts:110
mergeRegister
▸ mergeRegister(...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
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 component 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.
Parameters
Name | Type | Description |
---|---|---|
...func | Func [] | An array of functions meant to be executed by the returned function. |
Returns
fn
the function which executes all the passed register command functions.
▸ (): void
Returns
void
Defined in
lexical-utils/src/mergeRegister.ts:33
objectKlassEquals
▸ objectKlassEquals<T
>(object
, objectClass
): boolean
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
object | unknown | = The instance of the type |
objectClass | ObjectKlass <T > | = The class of the type |
Returns
boolean
Whether the object is has the same Klass of the objectClass, ignoring the difference across window (e.g. different iframs)
Defined in
lexical-utils/src/index.ts:487
positionNodeOnRange
▸ positionNodeOnRange(editor
, range
, onReposition
): () => void
Parameters
Name | Type |
---|---|
editor | LexicalEditor |
range | Range |
onReposition | (node : HTMLElement []) => void |
Returns
fn
▸ (): void
Returns
void
Defined in
lexical-utils/src/positionNodeOnRange.ts:23
registerNestedElementResolver
▸ registerNestedElementResolver<N
>(editor
, targetNode
, cloneNode
, handleOverlap
): () => 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
Type parameters
Name | Type |
---|---|
N | extends ElementNode |
Parameters
Name | Type | Description |
---|---|---|
editor | LexicalEditor | The lexical editor |
targetNode | Klass <N > | The target for the nested element to be extracted from. |
cloneNode | (from : N ) => N | See $createMarkNode |
handleOverlap | (from : N , to : N ) => void | Handles any overlap between the node to extract and the targetNode |
Returns
fn
The lexical editor
▸ (): void
Returns
void
Defined in
lexical-utils/src/index.ts:299
removeClassNamesFromElement
▸ removeClassNamesFromElement(element
, ...classNames
): 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.
Parameters
Name | Type | Description |
---|---|---|
element | HTMLElement | The element in which the classes are removed |
...classNames | (undefined | null | string | boolean )[] | An array defining the class names to remove from the element |
Returns
void