This package provides essential utility functions for the Lexical rich text editor framework, offering a comprehensive set of DOM manipulation helpers, tree traversal algorithms, and editor state management tools.
npx @tessl/cli install tessl/npm-lexical--utils@0.34.0Lexical Utils is a comprehensive TypeScript utility library providing essential functions for the Lexical rich text editor framework. It offers DOM manipulation helpers, tree traversal algorithms, file handling utilities, and editor state management tools designed to support complex editor operations while maintaining high performance and cross-browser compatibility.
npm install @lexical/utilslexical (core package) for types and base functionalityimport {
addClassNamesToElement,
removeClassNamesFromElement,
$dfs,
$insertNodeToNearestRoot,
mergeRegister,
markSelection
} from "@lexical/utils";For CommonJS:
const {
addClassNamesToElement,
removeClassNamesFromElement,
$dfs,
$insertNodeToNearestRoot,
mergeRegister,
markSelection
} = require("@lexical/utils");import {
addClassNamesToElement,
$dfs,
$insertNodeToNearestRoot,
$createParagraphNode,
mergeRegister
} from "@lexical/utils";
import { $getRoot } from "lexical";
// DOM manipulation
const element = document.createElement('div');
addClassNamesToElement(element, 'editor-content', 'active');
// Tree traversal
const nodes = $dfs($getRoot());
nodes.forEach(({ node, depth }) => {
console.log(`Node at depth ${depth}:`, node);
});
// Node insertion
const paragraph = $createParagraphNode();
$insertNodeToNearestRoot(paragraph);
// Register cleanup functions
const cleanup = mergeRegister(
editor.registerCommand(...),
editor.registerUpdateListener(...),
editor.registerNodeTransform(...)
);Lexical Utils is organized around several key functional areas:
CSS class manipulation and HTML element utilities for managing editor presentation and styling.
function addClassNamesToElement(
element: HTMLElement,
...classNames: Array<undefined | boolean | null | string>
): void;
function removeClassNamesFromElement(
element: HTMLElement,
...classNames: Array<undefined | boolean | null | string>
): void;Comprehensive tree traversal algorithms and node navigation utilities for exploring the Lexical node tree structure.
function $dfs(
startNode?: LexicalNode,
endNode?: LexicalNode
): Array<DFSNode>;
function $dfsIterator(
startNode?: LexicalNode,
endNode?: LexicalNode
): IterableIterator<DFSNode>;
interface DFSNode {
readonly depth: number;
readonly node: LexicalNode;
}MIME type validation and asynchronous file reading utilities with support for batching and order preservation.
function isMimeType(
file: File,
acceptableMimeTypes: Array<string>
): boolean;
function mediaFileReader(
files: Array<File>,
acceptableMimeTypes: Array<string>
): Promise<Array<{file: File; result: string}>>;Advanced editor state manipulation, node insertion, and state restoration utilities for complex editor operations.
function $insertNodeToNearestRoot<T extends LexicalNode>(node: T): T;
function $restoreEditorState(
editor: LexicalEditor,
editorState: EditorState
): void;
function registerNestedElementResolver<N extends ElementNode>(
editor: LexicalEditor,
targetNode: Klass<N>,
cloneNode: (from: N) => N,
handleOverlap: (from: N, to: N) => void
): () => void;Focused utility modules providing selection marking, DOM node positioning, function merging, and CSS utilities.
function mergeRegister(...func: Array<() => void>): () => void;
function markSelection(
editor: LexicalEditor,
onReposition?: (node: Array<HTMLElement>) => void
): () => void;
function positionNodeOnRange(
editor: LexicalEditor,
range: Range,
onReposition: (node: Array<HTMLElement>) => void
): () => void;Cross-platform browser and environment detection constants for conditional functionality.
const CAN_USE_BEFORE_INPUT: boolean;
const CAN_USE_DOM: boolean;
const IS_ANDROID: boolean;
const IS_ANDROID_CHROME: boolean;
const IS_APPLE: boolean;
const IS_APPLE_WEBKIT: boolean;
const IS_CHROME: boolean;
const IS_FIREFOX: boolean;
const IS_IOS: boolean;
const IS_SAFARI: boolean;interface DFSNode {
readonly depth: number;
readonly node: LexicalNode;
}
interface StateConfigWrapper<K extends string, V> {
readonly stateConfig: StateConfig<K, V>;
readonly $get: <T extends LexicalNode>(node: T) => V;
readonly $set: <T extends LexicalNode>(
node: T,
valueOrUpdater: ValueOrUpdater<V>
) => T;
readonly accessors: readonly [$get: this['$get'], $set: this['$set']];
makeGetterMethod<T extends LexicalNode>(): (this: T) => V;
makeSetterMethod<T extends LexicalNode>(): (
this: T,
valueOrUpdater: ValueOrUpdater<V>
) => T;
}
type DOMNodeToLexicalConversion = (element: Node) => LexicalNode;
type DOMNodeToLexicalConversionMap = Record<string, DOMNodeToLexicalConversion>;
type ObjectKlass<T> = new (...args: any[]) => T;The following utility functions are re-exported from the core lexical package for convenience:
/**
* Splits a node at the current selection point
*/
function $splitNode(node: ElementNode): ElementNode;
/**
* Checks if a DOM node is a block-level element
*/
function isBlockDomNode(node: Node): boolean;
/**
* Type guard to check if element is an HTML anchor element
*/
function isHTMLAnchorElement(x: Node | EventTarget | null): x is HTMLAnchorElement;
/**
* Type guard to check if element is an HTML element
*/
function isHTMLElement(x: Node | EventTarget | null): x is HTMLElement;
/**
* Checks if a DOM node is an inline element
*/
function isInlineDomNode(node: Node): boolean;lexical CoreThe following types are imported from the core lexical package and used throughout the API:
'next' | 'previous' for tree traversal directionnew (...args: any[]) => TV | ((prev: V) => V) for state updates