For each block, BlockSuite creates a block model to simplify its manipulation.
The Block
interface serves as the data abstraction for a block. It includes three key properties:
**flavour**
: Indicates the type of the block, such as paragraph
, code
, or list
.
**id**
: A unique identifier for the block. This ID can be used to query, update, or delete the block.
**model**
: Represents the block's model.
A block model provides an interface for reading and writing data within a block. Each piece of data in a block is referred to as a prop
, which can be accessed through model.props
.
For example:
Developers do not need in-depth knowledge of Yjs or its API. The block model serves as a bridge between Yjs and native JavaScript data, simplifying data manipulation within blocks.
For more information on how the block model works with Yjs, please refer to Block Reactive.
You may have noticed in the previous example that we can perform CRUD operations on blocks using the block model:
This approach allows you to easily create, read, update, and delete blocks within the store.
A block schema defines the structure and properties of a block. Below, I'll guide you through the process of defining a block schema step by step.
Each block has a set of props that determine how its data is displayed. For example, a typical paragraph block might have the following props:
Every block requires a model. You can create a block model by extending the BlockModel
class:
Next, use defineBlockSchema
to define the schema for your block:
In the metadata
property, we specify the valid parent blocks for this block. You can also define valid child blocks by declaring the children
property.
Finally, convert the schema into an extension:
To use this extension, simply add it to the list of extensions when initializing the Store
:
By following these steps, you can successfully define and use a custom block schema in your application.
In a CRDT-based editor, data migration is considered impossible because there is no central server to serve as a "single source of truth." Therefore, if you want to add new properties to an existing block, it is best to declare them as optional.
Fortunately, defining an optional property in BlockSuite is very straightforward. You can simply set its default value to undefined
: