mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00:00
21
blocksuite/docs/api/@blocksuite/std/index/README.md
Normal file
21
blocksuite/docs/api/@blocksuite/std/index/README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
[**BlockSuite API Documentation**](../../../README.md)
|
||||
|
||||
***
|
||||
|
||||
[BlockSuite API Documentation](../../../README.md) / [@blocksuite/std](../README.md) / index
|
||||
|
||||
# index
|
||||
|
||||
## Classes
|
||||
|
||||
- [~~BlockService~~](classes/BlockService.md)
|
||||
- [CommandManager](classes/CommandManager.md)
|
||||
- [LifeCycleWatcher](classes/LifeCycleWatcher.md)
|
||||
|
||||
## Functions
|
||||
|
||||
- [BlockViewExtension](functions/BlockViewExtension.md)
|
||||
- [ConfigExtensionFactory](functions/ConfigExtensionFactory.md)
|
||||
- [FlavourExtension](functions/FlavourExtension.md)
|
||||
- [KeymapExtension](functions/KeymapExtension.md)
|
||||
- [WidgetViewExtension](functions/WidgetViewExtension.md)
|
||||
@@ -0,0 +1,19 @@
|
||||
[**BlockSuite API Documentation**](../../../../README.md)
|
||||
|
||||
***
|
||||
|
||||
[BlockSuite API Documentation](../../../../README.md) / [@blocksuite/std](../../README.md) / [index](../README.md) / BlockService
|
||||
|
||||
# Class: ~~`abstract` BlockService~~
|
||||
|
||||
## Deprecated
|
||||
|
||||
BlockService is deprecated. You should reconsider where to put your feature.
|
||||
|
||||
BlockService is a legacy extension that is used to provide services to the block.
|
||||
In the previous version of BlockSuite, block service provides a way to extend the block.
|
||||
However, in the new version, we recommend using the new extension system.
|
||||
|
||||
## Extends
|
||||
|
||||
- [`Extension`](../../../store/classes/Extension.md)
|
||||
@@ -0,0 +1,186 @@
|
||||
[**BlockSuite API Documentation**](../../../../README.md)
|
||||
|
||||
***
|
||||
|
||||
[BlockSuite API Documentation](../../../../README.md) / [@blocksuite/std](../../README.md) / [index](../README.md) / CommandManager
|
||||
|
||||
# Class: CommandManager
|
||||
|
||||
Command manager to manage all commands
|
||||
|
||||
Commands are functions that take a context and a next function as arguments
|
||||
|
||||
```ts
|
||||
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
|
||||
|
||||
```ts
|
||||
// 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:
|
||||
|
||||
1. Using `exec` method
|
||||
`exec` is used to run a single command
|
||||
```ts
|
||||
const [result, data] = commandManager.exec(myCommand, payload);
|
||||
```
|
||||
|
||||
2. Using `chain` method
|
||||
`chain` is used to run a series of commands
|
||||
```ts
|
||||
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
|
||||
|
||||
```ts
|
||||
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
|
||||
```ts
|
||||
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
|
||||
```ts
|
||||
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
|
||||
|
||||
- [`LifeCycleWatcher`](LifeCycleWatcher.md)
|
||||
|
||||
## Methods
|
||||
|
||||
### chain()
|
||||
|
||||
> **chain**(): `Chain`\<`InitCommandCtx`\>
|
||||
|
||||
Create a chain to run a series of commands
|
||||
```ts
|
||||
const chain = commandManager.chain();
|
||||
const [result, data] = chain
|
||||
.myCommand1()
|
||||
.myCommand2(payload)
|
||||
.run();
|
||||
```
|
||||
|
||||
#### Returns
|
||||
|
||||
`Chain`\<`InitCommandCtx`\>
|
||||
|
||||
[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`
|
||||
|
||||
Called when std is created.
|
||||
|
||||
#### Returns
|
||||
|
||||
`void`
|
||||
|
||||
#### Inherited from
|
||||
|
||||
[`LifeCycleWatcher`](LifeCycleWatcher.md).[`created`](LifeCycleWatcher.md#created)
|
||||
|
||||
***
|
||||
|
||||
### mounted()
|
||||
|
||||
> **mounted**(): `void`
|
||||
|
||||
Called when editor host is mounted.
|
||||
Which means the editor host emit the `connectedCallback` lifecycle event.
|
||||
|
||||
#### Returns
|
||||
|
||||
`void`
|
||||
|
||||
#### Inherited from
|
||||
|
||||
[`LifeCycleWatcher`](LifeCycleWatcher.md).[`mounted`](LifeCycleWatcher.md#mounted)
|
||||
|
||||
***
|
||||
|
||||
### rendered()
|
||||
|
||||
> **rendered**(): `void`
|
||||
|
||||
Called when `std.render` is called.
|
||||
|
||||
#### Returns
|
||||
|
||||
`void`
|
||||
|
||||
#### Inherited from
|
||||
|
||||
[`LifeCycleWatcher`](LifeCycleWatcher.md).[`rendered`](LifeCycleWatcher.md#rendered)
|
||||
|
||||
***
|
||||
|
||||
### unmounted()
|
||||
|
||||
> **unmounted**(): `void`
|
||||
|
||||
Called when editor host is unmounted.
|
||||
Which means the editor host emit the `disconnectedCallback` lifecycle event.
|
||||
|
||||
#### Returns
|
||||
|
||||
`void`
|
||||
|
||||
#### Inherited from
|
||||
|
||||
[`LifeCycleWatcher`](LifeCycleWatcher.md).[`unmounted`](LifeCycleWatcher.md#unmounted)
|
||||
@@ -0,0 +1,81 @@
|
||||
[**BlockSuite API Documentation**](../../../../README.md)
|
||||
|
||||
***
|
||||
|
||||
[BlockSuite API Documentation](../../../../README.md) / [@blocksuite/std](../../README.md) / [index](../README.md) / LifeCycleWatcher
|
||||
|
||||
# Class: `abstract` LifeCycleWatcher
|
||||
|
||||
A life cycle watcher is an extension that watches the life cycle of the editor.
|
||||
It is used to perform actions when the editor is created, mounted, rendered, or unmounted.
|
||||
|
||||
When creating a life cycle watcher, you must define a key that is unique to the watcher.
|
||||
The key is used to identify the watcher in the dependency injection container.
|
||||
```ts
|
||||
class MyLifeCycleWatcher extends LifeCycleWatcher {
|
||||
static override readonly key = 'my-life-cycle-watcher';
|
||||
```
|
||||
|
||||
In the life cycle watcher, the methods will be called in the following order:
|
||||
1. `created`: Called when the std is created.
|
||||
2. `rendered`: Called when `std.render` is called.
|
||||
3. `mounted`: Called when the editor host is mounted.
|
||||
4. `unmounted`: Called when the editor host is unmounted.
|
||||
|
||||
## Extends
|
||||
|
||||
- [`Extension`](../../../store/classes/Extension.md)
|
||||
|
||||
## Extended by
|
||||
|
||||
- [`CommandManager`](CommandManager.md)
|
||||
|
||||
## Methods
|
||||
|
||||
### created()
|
||||
|
||||
> **created**(): `void`
|
||||
|
||||
Called when std is created.
|
||||
|
||||
#### Returns
|
||||
|
||||
`void`
|
||||
|
||||
***
|
||||
|
||||
### mounted()
|
||||
|
||||
> **mounted**(): `void`
|
||||
|
||||
Called when editor host is mounted.
|
||||
Which means the editor host emit the `connectedCallback` lifecycle event.
|
||||
|
||||
#### Returns
|
||||
|
||||
`void`
|
||||
|
||||
***
|
||||
|
||||
### rendered()
|
||||
|
||||
> **rendered**(): `void`
|
||||
|
||||
Called when `std.render` is called.
|
||||
|
||||
#### Returns
|
||||
|
||||
`void`
|
||||
|
||||
***
|
||||
|
||||
### unmounted()
|
||||
|
||||
> **unmounted**(): `void`
|
||||
|
||||
Called when editor host is unmounted.
|
||||
Which means the editor host emit the `disconnectedCallback` lifecycle event.
|
||||
|
||||
#### Returns
|
||||
|
||||
`void`
|
||||
@@ -0,0 +1,42 @@
|
||||
[**BlockSuite API Documentation**](../../../../README.md)
|
||||
|
||||
***
|
||||
|
||||
[BlockSuite API Documentation](../../../../README.md) / [@blocksuite/std](../../README.md) / [index](../README.md) / BlockViewExtension
|
||||
|
||||
# Function: BlockViewExtension()
|
||||
|
||||
> **BlockViewExtension**(`flavour`, `view`): `ExtensionType`
|
||||
|
||||
Create a block view extension.
|
||||
|
||||
## Parameters
|
||||
|
||||
### flavour
|
||||
|
||||
`string`
|
||||
|
||||
The flavour of the block that the view is for.
|
||||
|
||||
### view
|
||||
|
||||
`BlockViewType`
|
||||
|
||||
Lit literal template for the view. Example: `my-list-block`
|
||||
|
||||
The view is a lit template that is used to render the block.
|
||||
|
||||
## Returns
|
||||
|
||||
`ExtensionType`
|
||||
|
||||
## Example
|
||||
|
||||
```ts
|
||||
import { BlockViewExtension } from '@blocksuite/std';
|
||||
|
||||
const MyListBlockViewExtension = BlockViewExtension(
|
||||
'affine:list',
|
||||
literal`my-list-block`
|
||||
);
|
||||
```
|
||||
@@ -0,0 +1,45 @@
|
||||
[**BlockSuite API Documentation**](../../../../README.md)
|
||||
|
||||
***
|
||||
|
||||
[BlockSuite API Documentation](../../../../README.md) / [@blocksuite/std](../../README.md) / [index](../README.md) / ConfigExtensionFactory
|
||||
|
||||
# Function: ConfigExtensionFactory()
|
||||
|
||||
> **ConfigExtensionFactory**\<`Config`\>(`configId`): `ConfigFactory`\<`Config`\>
|
||||
|
||||
Create a config extension.
|
||||
A config extension provides a configuration object for a block flavour.
|
||||
The configuration object can be used like:
|
||||
```ts
|
||||
const config = std.provider.getOptional(ConfigIdentifier('my-flavour'));
|
||||
```
|
||||
|
||||
## Type Parameters
|
||||
|
||||
### Config
|
||||
|
||||
`Config` *extends* `Record`\<`string`, `any`\>
|
||||
|
||||
## Parameters
|
||||
|
||||
### configId
|
||||
|
||||
`string`
|
||||
|
||||
The id of the config. Should be unique for each config.
|
||||
|
||||
## Returns
|
||||
|
||||
`ConfigFactory`\<`Config`\>
|
||||
|
||||
## Example
|
||||
|
||||
```ts
|
||||
import { ConfigExtensionFactory } from '@blocksuite/std';
|
||||
const MyConfigExtensionFactory = ConfigExtensionFactory<ConfigType>('my-flavour');
|
||||
const MyConfigExtension = MyConfigExtensionFactory({
|
||||
option1: 'value1',
|
||||
option2: 'value2',
|
||||
});
|
||||
```
|
||||
@@ -0,0 +1,31 @@
|
||||
[**BlockSuite API Documentation**](../../../../README.md)
|
||||
|
||||
***
|
||||
|
||||
[BlockSuite API Documentation](../../../../README.md) / [@blocksuite/std](../../README.md) / [index](../README.md) / FlavourExtension
|
||||
|
||||
# Function: FlavourExtension()
|
||||
|
||||
> **FlavourExtension**(`flavour`): `ExtensionType`
|
||||
|
||||
Create a flavour extension.
|
||||
|
||||
## Parameters
|
||||
|
||||
### flavour
|
||||
|
||||
`string`
|
||||
|
||||
The flavour of the block that the extension is for.
|
||||
|
||||
## Returns
|
||||
|
||||
`ExtensionType`
|
||||
|
||||
## Example
|
||||
|
||||
```ts
|
||||
import { FlavourExtension } from '@blocksuite/std';
|
||||
|
||||
const MyFlavourExtension = FlavourExtension('my-flavour');
|
||||
```
|
||||
@@ -0,0 +1,49 @@
|
||||
[**BlockSuite API Documentation**](../../../../README.md)
|
||||
|
||||
***
|
||||
|
||||
[BlockSuite API Documentation](../../../../README.md) / [@blocksuite/std](../../README.md) / [index](../README.md) / KeymapExtension
|
||||
|
||||
# Function: KeymapExtension()
|
||||
|
||||
> **KeymapExtension**(`keymapFactory`, `options`?): `ExtensionType`
|
||||
|
||||
Create a keymap extension.
|
||||
|
||||
## Parameters
|
||||
|
||||
### keymapFactory
|
||||
|
||||
(`std`) => `Record`\<`string`, `UIEventHandler`\>
|
||||
|
||||
Create keymap of the extension.
|
||||
It should return an object with `keymap` and `options`.
|
||||
|
||||
`keymap` is a record of keymap.
|
||||
|
||||
### options?
|
||||
|
||||
`EventOptions`
|
||||
|
||||
`options` is an optional object that restricts the event to be handled.
|
||||
|
||||
## Returns
|
||||
|
||||
`ExtensionType`
|
||||
|
||||
## Example
|
||||
|
||||
```ts
|
||||
import { KeymapExtension } from '@blocksuite/std';
|
||||
|
||||
const MyKeymapExtension = KeymapExtension(std => {
|
||||
return {
|
||||
keymap: {
|
||||
'mod-a': SelectAll
|
||||
}
|
||||
options: {
|
||||
flavour: 'affine:paragraph'
|
||||
}
|
||||
}
|
||||
});
|
||||
```
|
||||
@@ -0,0 +1,45 @@
|
||||
[**BlockSuite API Documentation**](../../../../README.md)
|
||||
|
||||
***
|
||||
|
||||
[BlockSuite API Documentation](../../../../README.md) / [@blocksuite/std](../../README.md) / [index](../README.md) / WidgetViewExtension
|
||||
|
||||
# Function: WidgetViewExtension()
|
||||
|
||||
> **WidgetViewExtension**(`flavour`, `id`, `view`): `ExtensionType`
|
||||
|
||||
Create a widget view extension.
|
||||
|
||||
## Parameters
|
||||
|
||||
### flavour
|
||||
|
||||
`string`
|
||||
|
||||
The flavour of the block that the widget view is for.
|
||||
|
||||
### id
|
||||
|
||||
`string`
|
||||
|
||||
The id of the widget view.
|
||||
|
||||
### view
|
||||
|
||||
`StaticValue`
|
||||
|
||||
The widget view lit literal.
|
||||
|
||||
A widget view is to provide a widget view for a block.
|
||||
For every target block, it's view will be rendered with the widget view.
|
||||
|
||||
## Returns
|
||||
|
||||
`ExtensionType`
|
||||
|
||||
## Example
|
||||
|
||||
```ts
|
||||
import { WidgetViewExtension } from '@blocksuite/std';
|
||||
|
||||
const MyWidgetViewExtension = WidgetViewExtension('my-flavour', 'my-widget', literal`my-widget-view`);
|
||||
Reference in New Issue
Block a user