Skip to main content
Use these utility functions when building custom logic or manipulating the document tree programmatically.

Tree Operations

import { createNode, findNode } from "@open-email/editor";
createNode
(type, props?, children?, id?) => EmailNode
Creates a new node object. If id is omitted a unique ID is generated automatically. Pass a fixed id when you need deterministic output (e.g. SSR / hydration-safe initial documents).
cloneNode
(node) => EmailNode
Deeply clones a node and its children, generating new IDs for all.
findNode
(root, id) => EmailNode | null
Finds a node by ID within a tree.
findParent
(root, id) => EmailNode | null
Finds the parent of a given node ID.
flattenTree
(root) => EmailNode[]
Returns an array of all nodes in the tree.

Document Management

createEmptyDocument
(title?) => EmailDocument
Creates a fresh document with a default structure (Container > Section > Text).
validateDocument
(doc) => string[]
Checks a document for errors (missing required fields, duplicate IDs, etc.). Returns an array of error messages.
getNodePath
(node, root) => number[]
Returns the path of indices from the root to the node.
generateId
(prefix?) => string
Generates a unique ID for a node.