blocksuite/docs/api/@blocksuite/store/classes/Store.md
BlockSuite API Documentation / @blocksuite/store / Store
Core store class that manages blocks and their lifecycle in BlockSuite
The Store class is responsible for managing the lifecycle of blocks, handling transactions, and maintaining the block tree structure. A store is a piece of data created from one or a part of a Y.Doc.
get blockSize():
number
Get the number of blocks in the store
number
get isEmpty():
boolean
Check if there are no blocks in the store.
boolean
get isEmpty$():
ReadonlySignal<boolean>
Get the signal for the empty state of the store.
ReadonlySignal<boolean>
get readonly():
boolean
Check if the store is readonly.
boolean
set readonly(
value):void
Set the readonly state of the store.
boolean
void
get readonly$():
Signal<boolean>
Get the signal for the readonly state of the store.
Signal<boolean>
get root():
BlockModel<object> |null
Get the root block of the store.
BlockModel<object> | null
addBlock<
T>(flavour,blockProps,parent?,parentIndex?):string
Creates and adds a new block to the store
T extends BlockModel<object> = BlockModel<object>
string
The block's flavour (type)
Partial<BlockProps | PropsOfModel<T> & BlockSysProps> = {}
Optional properties for the new block
Optional parent block or parent block ID
string | BlockModel<object> | null
number
Optional index position in parent's children
string
The ID of the newly created block
When store is in readonly mode
addBlocks(
blocks,parent?,parentIndex?):string[]
Add multiple blocks to the store
object[]
Array of blocks to add
Optional parent block or parent block ID
string | BlockModel<object> | null
number
Optional index position in parent's children
string[]
Array of IDs of the newly created blocks
addSiblingBlocks(
targetModel,props,placement):string[]
Add sibling blocks to the store
BlockModel
The target block model
Partial<BlockProps>[]
Array of block properties
Optional position to place the new blocks ('after' or 'before')
"after" | "before"
string[]
Array of IDs of the newly created blocks
deleteBlock(
model,options):void
Delete a block from the store
The block model or block ID to delete
string | BlockModel<object>
Optional options for the deletion
BlockModel<object>
Optional block model to bring children to
boolean
Optional flag to delete children
void
getAllModels():
BlockModel<object>[]
Get all models in the store
BlockModel<object>[]
Array of all models
getBlock(
id):Block|undefined
Gets a block by its ID
string
The block's ID
Block | undefined
The block instance if found, undefined otherwise
getBlock$(
id):Block|undefined
Gets a block by its ID
string
The block's ID
Block | undefined
The block instance in signal if found, undefined otherwise
getBlocksByFlavour(
blockFlavour):Block[]
Gets all blocks of specified flavour(s)
Single flavour or array of flavours to filter by
string | string[]
Block[]
Array of matching blocks
getModelById<
Model>(id):Model|null
Get a model by its ID
Model extends BlockModel<object> = BlockModel<object>
string
The model's ID
Model | null
The model instance if found, null otherwise
getModelsByFlavour(
blockFlavour):BlockModel<object>[]
Get all models of specified flavour(s)
Single flavour or array of flavours to filter by
string | string[]
BlockModel<object>[]
Array of matching models
getNext(
block):BlockModel<object> |null
Get the next sibling block of a given block
Block model or block ID to find next sibling for
string | BlockModel<object>
BlockModel<object> | null
The next sibling block model if found, null otherwise
getNexts(
block):BlockModel<object>[]
Get all next sibling blocks of a given block
Block model or block ID to find next siblings for
string | BlockModel<object>
BlockModel<object>[]
Array of next sibling blocks if found, empty array otherwise
getParent(
target):BlockModel<object> |null
Gets the parent block of a given block
Block model or block ID to find parent for
string | BlockModel<object>
BlockModel<object> | null
The parent block model if found, null otherwise
getPrev(
block):BlockModel<object> |null
Get the previous sibling block of a given block
Block model or block ID to find previous sibling for
string | BlockModel<object>
BlockModel<object> | null
The previous sibling block model if found, null otherwise
getPrevs(
block):BlockModel<object>[]
Get all previous sibling blocks of a given block
Block model or block ID to find previous siblings for
string | BlockModel<object>
BlockModel<object>[]
Array of previous sibling blocks if found, empty array otherwise
hasBlock(
id):boolean
Check if a block exists by its ID
string
The block's ID
boolean
True if the block exists, false otherwise
moveBlocks(
blocksToMove,newParent,targetSibling,shouldInsertBeforeSibling):void
Move blocks to a new parent block
BlockModel<object>[]
Array of block models to move
BlockModel
The new parent block model
Optional target sibling block model
BlockModel<object> | null
boolean = true
Optional flag to insert before sibling
void
updateBlock<
T>(modelOrId,callBackOrProps):void
Updates a block's properties or executes a callback in a transaction
T extends BlockModel<object> = BlockModel<object>
The block model or block ID to update
string | T
Either a callback function to execute or properties to update
() => void | Partial<BlockProps | PropsOfModel<T> & BlockSysProps>
void
When the block is not found or schema validation fails
get get(): <
T>(identifier,options?) =>T
Get an extension instance from the store
const extension = store.get(SomeExtension);
The extension instance
<
T>(identifier,options?):T
T
GeneralServiceIdentifier<T>
ResolveOptions
T
get getOptional(): <
T>(identifier,options?) =>T|null
Optional get an extension instance from the store.
The major difference between get and getOptional is that getOptional will not throw an error if the extension is not found.
const extension = store.getOptional(SomeExtension);
The extension instance
<
T>(identifier,options?):T|null
T
GeneralServiceIdentifier<T>
ResolveOptions
T | null
get provider():
ServiceProvider
Get the di provider for current store.
ServiceProvider
get canRedo():
boolean
Check if the store can redo
boolean
get canUndo():
boolean
Check if the store can undo
boolean
get history():
HistoryExtension
Get the Y.UndoManager instance for current store.
HistoryExtension
captureSync():
void
Force the following history to be captured into a new stack.
void
op1();
op2();
store.captureSync();
op3();
store.undo(); // undo op3
store.undo(); // undo op1, op2
redo():
void
Redo the last undone transaction.
void
resetHistory():
void
Reset the history of the store.
void
transact(
fn,shouldTransact):void
Execute a transaction.
() => void
boolean = ...
void
store.transact(() => {
op1();
op2();
});
undo():
void
Undo the last transaction.
void
withoutTransact(
fn):void
Execute a transaction without capturing the history.
() => void
void
store.withoutTransact(() => {
op1();
op2();
});
disposableGroup:
DisposableGroup
Group of disposable resources managed by the store
readonlyslots:StoreSlots
Slots for receiving events from the store. All events are rxjs Subjects, you can subscribe to them like this:
store.slots.ready.subscribe(() => {
console.log('store is ready');
});
You can also use rxjs operators to handle the events.
get id():
string
Get the id of the store.
string
get loaded():
boolean
Check if the store is loaded.
boolean
get ready():
boolean
Check if the store is ready. Which means the Y.Doc is loaded and the root block is added.
boolean
dispose():
void
Disposes the store and releases all resources
void
load(
initFn?):Store
Initializes and loads the store
() => void
Optional initialization function
Store
The store instance
getTransformer(
middlewares):Transformer
Creates a new transformer instance for the store
TransformerMiddleware[] = []
Optional array of transformer middlewares
Transformer
A new Transformer instance
get awarenessStore():
AwarenessStore
Get the AwarenessStore instance for current store
AwarenessStore
get blobSync():
BlobEngine
Get the BlobEngine instance for current store.
BlobEngine
get doc():
Doc
Get the Doc instance for current store.
Doc
get schema():
Schema
Get the Schema instance of the store.
get workspace():
Workspace
Get the Workspace instance for current store.
Workspace