API Overview
A quick overview of the DocNode API
A document (class Doc) is composed of a set of nodes (class DocNode) forming a tree structure.
Nodes must follow an interface called NodeDefinition, and Documents must define all the NodeDefinitions that the document accepts.
The entire DocNode API is included in the following snippet.
import { Doc, string, defineNode } from "docnode";
/*===============================================
CONSTRUCTORS AND BUILDERS
===============================================*/
const MyNodeDef = defineNode({
type: "myNode",
state: {
foo: string(""), // "" is the default value
},
});
const doc = new Doc({ extensions: [{ nodes: [MyNodeDef] }] }); // New doc that accepts MyNodeDef
const docnode = doc.createNode(MyNodeDef); // Creates a node of type DocNode<typeof MyNodeDef>
doc.root.append(docnode); // An example of how you can insert the node into the doc
/*===============================================
READ ONLY
===============================================*/
docnode.id; // Get the id of the node
docnode.type; // Get the type of the node
docnode.parent; // Get the parent of the node
docnode.next; // Get the next sibling of the node
docnode.prev; // Get the previous sibling of the node
docnode.first; // Get the first child of the node
docnode.last; // Get the last child of the node
docnode.doc; // Get the doc of the node
doc.root; // Get the root node of the doc
doc.getNodeById(id); // Get the node with the given id
docnode.state.foo.get(); // Get the current value of the state property foo
docnode.state.foo.getPrev(); // Get [fooChanged, fooPrev]. I.e., If foo changed and its previous value
docnode.is(MyNodeDef); // Check if the node satisfies the given node definition
// Iterators methods. E.g. `docnode.descendants().forEach((node) => { ... })`
docnode.descendants(); // Accesses `find` and `forEach` methods over the descendants
docnode.ancestors(); // Accesses `find` and `forEach` methods over the ancestors
docnode.prevSiblings(); // Accesses `find` and `forEach` methods over the previous siblings
docnode.nextSiblings(); // Accesses `find` and `forEach` methods over the next siblings
docnode.children(); // Accesses `find` and `forEach` methods over the children
docnode.to(node2); // Accesses `find` and `forEach` methods over the nodes in the range of siblings
/*===============================================
EVENTS
===============================================*/
doc.onChange(callback); // At the end of a transaction. Doesn't allow mutating the doc
doc.onNormalize(callback); // Before the change event. Allows mutating the doc.
/*===============================================
MUTATORS
===============================================*/
// These methods cannot be used in a change event.
docnode.state.foo.set(value); // Set the value of the state property foo
docnode.append(...nodes); // Insert nodes as children at the end
docnode.prepend(...nodes); // Insert nodes as children at the beginning
docnode.insertAfter(...nodes); // Insert nodes after the node
docnode.insertBefore(...nodes); // Insert nodes before the node
docnode.delete(); // Delete the node
docnode.deleteChildren(); // Delete the children of the node
docnode.replace(...nodes); // Replace the node with the given nodes
docnode.replaceChildren(...nodes); // Replace the children of the node with the given nodes
docnode.move(target, position); // Move the node to a position relative to the target node
docnode.copy(target, position); // Copy the node to a position relative to the target node
docnode.to(node2).delete(); // delete a range of sibling nodes
docnode.to(node2).replace(...nodes); // replace a range of sibling nodes
docnode.to(node2).move(target, position); // move a range of sibling nodes
docnode.to(node2).copy(target, position); // copy a range of sibling nodes
doc.applyOperations(operations); // Apply the operations received in a change event
doc.forceCommit(); // Terminates the transaction early and synchronously, triggering events.
doc.abort(); // Aborts the current transaction.
/*===============================================
SERIALIZATION
===============================================*/
const jsonDoc = doc.toJSON(); // Get the JSON representation of the doc
// Create a new doc from the given JSON:
const doc2 = Doc.fromJSON({ extensions: [{ nodes: [MyNodeDef] }] }, jsonDoc);
const jsonDocNode = docnode.toJSON(); // Get the JSON of the node. Useful for debugging