BlockSuite API Documentation / @blocksuite/block-std / CommandManager
Class: CommandManager
Defined in: packages/framework/block-std/src/command/manager.ts:95
Command manager to manage all commands
Commands are functions that take a context and a next function as arguments
const myCommand: Command<input, output> = (ctx, next) => {
const count = ctx.count || 0;
const success = someOperation();
if (success) {
return next({ count: count + 1 });
}
// if the command is not successful, you can return without calling next
return;Command input and output data can be defined in the Command type
// input: ctx.firstName, ctx.lastName
// output: ctx.fullName
const myCommand: Command<{ firstName: string; lastName: string }, { fullName: string }> = (ctx, next) => {
const { firstName, lastName } = ctx;
const fullName = `${firstName} ${lastName}`;
return next({ fullName });
}Commands can be run in two ways:
- Using
execmethodexecis used to run a single command
const [result, data] = commandManager.exec(myCommand, payload);- Using
chainmethodchainis used to run a series of commands
const chain = commandManager.chain();
const [result, data] = chain
.pipe(myCommand1)
.pipe(myCommand2, payload)
.run();Command chains will stop running if a command is not successful
const chain = commandManager.chain();
const [result, data] = chain
.chain(myCommand1) <-- if this fail
.chain(myCommand2, payload) <- this won't run
.run();
result <- result will be `false`You can use try to run a series of commands and if one of them is successful, it will continue to the next command
const chain = commandManager.chain();
const [result, data] = chain
.try(chain => [
chain.pipe(myCommand1), <- if this fail
chain.pipe(myCommand2, payload), <- this will run, if this success
chain.pipe(myCommand3), <- this won't run
])
.run();The tryAll method is similar to try, but it will run all commands even if one of them is successful
const chain = commandManager.chain();
const [result, data] = chain
.try(chain => [
chain.pipe(myCommand1), <- if this success
chain.pipe(myCommand2), <- this will also run
chain.pipe(myCommand3), <- so will this
])
.run();Extends
Constructors
new CommandManager()
new CommandManager(
std):CommandManager
Defined in: packages/framework/block-std/src/extension/lifecycle-watcher.ts:28
Parameters
std
Returns
CommandManager
Inherited from
Properties
std
readonlystd:BlockStdScope
Defined in: packages/framework/block-std/src/extension/lifecycle-watcher.ts:28
Inherited from
key
readonlystatickey:"commandManager"='commandManager'
Defined in: packages/framework/block-std/src/command/manager.ts:96
Overrides
Methods
chain()
chain():
Chain<InitCommandCtx>
Defined in: packages/framework/block-std/src/command/manager.ts:217
Create a chain to run a series of commands
const chain = commandManager.chain();
const [result, data] = chain
.myCommand1()
.myCommand2(payload)
.run();Returns
[success, data] - success is a boolean to indicate if the chain is successful, data is the final context after running the chain
created()
created():
void
Defined in: packages/framework/block-std/src/extension/lifecycle-watcher.ts:52
Called when std is created.
Returns
void
Inherited from
exec()
exec<
Output,Input>(command,input?): [false,Partial<InitCommandCtx&Input&Output> &InitCommandCtx] | [true,InitCommandCtx&Input&Output]
Defined in: packages/framework/block-std/src/command/manager.ts:221
Type Parameters
Output
Output extends object
Input
Input extends object
Parameters
command
Command<Input, Output>
input?
Input
Returns
[false, Partial<InitCommandCtx & Input & Output> & InitCommandCtx] | [true, InitCommandCtx & Input & Output]
mounted()
mounted():
void
Defined in: packages/framework/block-std/src/extension/lifecycle-watcher.ts:58
Called when editor host is mounted. Which means the editor host emit the connectedCallback lifecycle event.
Returns
void
Inherited from
rendered()
rendered():
void
Defined in: packages/framework/block-std/src/extension/lifecycle-watcher.ts:63
Called when std.render is called.
Returns
void
Inherited from
unmounted()
unmounted():
void
Defined in: packages/framework/block-std/src/extension/lifecycle-watcher.ts:69
Called when editor host is unmounted. Which means the editor host emit the disconnectedCallback lifecycle event.
Returns
void
Inherited from
setup()
staticsetup(di):void
Defined in: packages/framework/block-std/src/extension/lifecycle-watcher.ts:32
Parameters
di
Container
Returns
void