Principle:AFK surf Open agent Block Tree Construction
| Knowledge Sources | |
|---|---|
| Domains | Document_Model, BlockSuite |
| Last Updated | 2026-10-25 00:00 GMT |
Overview
Pattern of building a structured document by appending typed blocks into a parent-child tree rooted at a page block.
Description
A BlockSuite document is a tree of blocks. The canonical hierarchy is affine:page (root, holds the title) → affine:surface (whiteboard layer) and affine:note (content container) → content blocks (affine:paragraph, affine:list, affine:code, affine:database, etc.). Database rows are themselves child blocks of the database. Each block is created with a flavour string, an optional props bag (e.g. title: Text, xywh: geometry string, type: paragraph variant), and an optional parent id; the store returns the new block id. Mutations go through the store so the yjs CRDT stays the source of truth and the undo stack is consistent.
Usage
Apply this principle whenever you need to construct or extend a BlockSuite document programmatically — scaffolding a page, adding content, seeding a database, or building a preset.
Theoretical Basis
The document is a tree in a CRDT. addBlock(flavour, props, parentId) appends a node under parentId (or the root) and returns its id. The flavour determines the block model class; props are the initial state. The tree invariant (e.g. note must live under page) is enforced by the schema.
Pseudo-code:
const rootId = store.addBlock('affine:page', { title: new Text(title) });
store.addBlock('affine:surface', {}, rootId);
const noteId = store.addBlock('affine:note', { xywh: '[0,100,800,640]' }, rootId);
const rowId = store.addBlock('affine:paragraph', { type: 'h1', text: new Text(t) }, noteId);