Overview
Document represents the hierarchical structure of an AppFlowy Editor document. It contains a root node and provides methods for querying and traversing the document tree.
Do not directly mutate document properties. Use Transaction operations to modify the document for proper undo/redo support and collaborative editing.
Constructor
Document({required Node root})
Named Constructors
// Create from JSON
Document.fromJson(Map<String, dynamic> json)
// Create a blank document
Document.blank({bool withInitialText = false})
Parameters
The root node of the document tree.
Properties
The root node of the document.
The first child node of the root, or null if the document is empty.
The last descendant node in the document tree.
Whether the document is empty (contains no text content).
Methods
nodeAtPath
Node? nodeAtPath(Path path)
Returns the node at the specified path, or null if not found.
The path to the node (e.g., [0], [1, 2]).
Returns: The node at the path, or null.
Example:
final node = document.nodeAtPath([0]); // Get first node
final nestedNode = document.nodeAtPath([0, 1]); // Get second child of first node
insert
bool insert(Path path, Iterable<Node> nodes)
Inserts nodes at the specified path. This is a low-level method; prefer using Transaction instead.
The path where nodes should be inserted.
Returns: true if successful, false otherwise.
delete
bool delete(Path path, [int length = 1])
Deletes nodes starting at the specified path. This is a low-level method; prefer using Transaction instead.
The starting path for deletion.
Number of consecutive nodes to delete.
Returns: true if successful, false otherwise.
update
bool update(Path path, Attributes attributes)
Updates node attributes at the specified path. This is a low-level method; prefer using Transaction instead.
The attributes to merge into the node.
Returns: true if successful, false otherwise.
updateText
bool updateText(Path path, Delta delta)
Updates node text using a Delta operation. This is a low-level method; prefer using Transaction instead.
The path to the text node.
The Delta operation to apply.
Returns: true if successful, false otherwise.
toJson
Map<String, Object> toJson()
Serializes the document to JSON format.
Returns: A JSON representation of the document.
Example:
final json = document.toJson();
// {
// 'document': {
// 'type': 'page',
// 'children': [...]
// }
// }
dispose
Cleans up resources. Must be called when the document is no longer needed.
Documents can be serialized to and deserialized from JSON:
{
"document": {
"type": "page",
"children": [
{
"type": "paragraph",
"data": {
"delta": [
{ "insert": "Hello " },
{ "insert": "World", "attributes": { "bold": true } }
]
}
}
]
}
}
Example
// Create a blank document
final document = Document.blank(withInitialText: true);
// Access the first node
final firstNode = document.first;
print(firstNode?.type); // 'paragraph'
// Query a node by path
final node = document.nodeAtPath([0]);
// Check if empty
if (document.isEmpty) {
print('Document is empty');
}
// Serialize to JSON
final json = document.toJson();
// Deserialize from JSON
final loadedDocument = Document.fromJson(json);
// Clean up
document.dispose();
See Also