Read and edit content
Document holds your content. The controller edits it, moves the selection and keeps Undo and Redo.
Each element package adds its own commands as extension methods on the controller. Only installed extensions can run.
dart
import 'package:vyuh_document_editor/vyuh_document_editor.dart';
import 'package:vyuh_document/paragraph_extension.dart';
void editContent(DocumentEditorController controller) {
// Read the immutable document AST, independently of its widgets.
final Document content = controller.document;
final JsonObject stored = controller.toJson();
final String json = controller.toJsonString(pretty: true);
// Load canonical JSON as a new document session.
controller.replaceDocument(Document.fromJson(stored));
// Edit at the current selection through the canonical command pipeline.
controller.insertText('Hello, document.');
controller.toggleMark(TextMark.bold);
controller.undo();
controller.redo();
// Read these values as part of the consuming host.
content.toJson();
json.length;
}Separate replacement from editing
replaceDocumentopens another document or a saved snapshot as a new session.- Commands and transactions make ordinary edits, so history and selection stay coherent.
- Listen to the controller and save on your own schedule.
Construct semantic content
Text-first constructors produce the same typed content as the richer constructors:
dart
final title = HeadingBlock.text('Report', id: 'title', level: 1);
final summary = ParagraphBlock.text('A strong quarter.', id: 'summary');
final task = ChecklistBlock.text('Review evidence', id: 'review');
final code = CodeBlock.text(
'print("Hello");',
id: 'example',
language: 'dart',
presentation: const CodeBlockPresentation(fontSize: 12),
);A block can be inserted only when its extension is installed and the schema admits it. Give new blocks unique IDs; positions and transaction anchors target them by ID.
Keep resources with the content
Document JSON stores font and image references, not bytes. Keep the bytes available through your resource bindings.