mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-29 00:06:09 +08:00
feat(editor): add editor store (#9584)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { SignalWatcher, WithDisposable } from '@blocksuite/global/utils';
|
||||
import type { Blocks, ExtensionType } from '@blocksuite/store';
|
||||
import { type Blocks, type ExtensionType, Store } from '@blocksuite/store';
|
||||
import { html } from 'lit';
|
||||
import { customElement, property } from 'lit/decorators.js';
|
||||
|
||||
@@ -18,8 +18,9 @@ export class TestEditorContainer extends SignalWatcher(
|
||||
|
||||
override connectedCallback() {
|
||||
super.connectedCallback();
|
||||
const store = new Store({ blocks: this.doc });
|
||||
this._std = new BlockStdScope({
|
||||
doc: this.doc,
|
||||
store,
|
||||
extensions: this.specs,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import type { ServiceProvider } from '@blocksuite/global/di';
|
||||
import { Container } from '@blocksuite/global/di';
|
||||
import { Container, type ServiceProvider } from '@blocksuite/global/di';
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import {
|
||||
type Blocks,
|
||||
type ExtensionType,
|
||||
Job,
|
||||
type JobMiddleware,
|
||||
type Store,
|
||||
} from '@blocksuite/store';
|
||||
|
||||
import { Clipboard } from '../clipboard/index.js';
|
||||
@@ -36,7 +35,7 @@ import { EditorHost } from '../view/element/index.js';
|
||||
import { ViewStore } from '../view/view-store.js';
|
||||
|
||||
export interface BlockStdOptions {
|
||||
doc: Blocks;
|
||||
store: Store;
|
||||
extensions: ExtensionType[];
|
||||
}
|
||||
|
||||
@@ -65,7 +64,7 @@ export class BlockStdScope {
|
||||
|
||||
readonly container: Container;
|
||||
|
||||
readonly doc: Blocks;
|
||||
readonly store: Store;
|
||||
|
||||
readonly provider: ServiceProvider;
|
||||
|
||||
@@ -75,6 +74,10 @@ export class BlockStdScope {
|
||||
return this.provider.getAll(LifeCycleWatcherIdentifier);
|
||||
}
|
||||
|
||||
get doc() {
|
||||
return this.store.blocks;
|
||||
}
|
||||
|
||||
get clipboard() {
|
||||
return this.get(Clipboard);
|
||||
}
|
||||
@@ -122,7 +125,7 @@ export class BlockStdScope {
|
||||
'Host is not ready to use, the `render` method should be called first'
|
||||
);
|
||||
};
|
||||
this.doc = options.doc;
|
||||
this.store = options.store;
|
||||
this.userExtensions = options.extensions;
|
||||
this.container = new Container();
|
||||
this.container.addImpl(StdIdentifier, () => this);
|
||||
@@ -137,7 +140,7 @@ export class BlockStdScope {
|
||||
ext.setup(container);
|
||||
});
|
||||
|
||||
this.provider = this.container.provider();
|
||||
this.provider = this.container.provider(undefined, this.store.provider);
|
||||
|
||||
this._lifeCycleWatchers.forEach(watcher => {
|
||||
watcher.created.call(watcher);
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import type { Container } from '@blocksuite/global/di';
|
||||
|
||||
/**
|
||||
* Generic extension.
|
||||
* Extensions are used to set up the dependency injection container.
|
||||
* In most cases, you won't need to use this class directly.
|
||||
* We provide helper classes like `CommandExtension` and `BlockViewExtension` to make it easier to create extensions.
|
||||
*/
|
||||
export abstract class Extension {
|
||||
static setup(_di: Container): void {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
export interface ExtensionType {
|
||||
setup(di: Container): void;
|
||||
}
|
||||
@@ -1,17 +1,2 @@
|
||||
import type { Container } from '@blocksuite/global/di';
|
||||
|
||||
/**
|
||||
* Generic extension.
|
||||
* Extensions are used to set up the dependency injection container.
|
||||
* In most cases, you won't need to use this class directly.
|
||||
* We provide helper classes like `CommandExtension` and `BlockViewExtension` to make it easier to create extensions.
|
||||
*/
|
||||
export abstract class Extension {
|
||||
static setup(_di: Container): void {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
export interface ExtensionType {
|
||||
setup(di: Container): void;
|
||||
}
|
||||
export * from './extension';
|
||||
export * from './store-extension';
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { type Container, createIdentifier } from '@blocksuite/global/di';
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
|
||||
import { StoreIdentifier } from '../store/identifier';
|
||||
import type { Store } from '../store/store';
|
||||
import { Extension } from './extension';
|
||||
|
||||
export const StoreExtensionIdentifier =
|
||||
createIdentifier<StoreExtension>('StoreExtension');
|
||||
|
||||
export const storeExtensionSymbol = Symbol('StoreExtension');
|
||||
|
||||
export class StoreExtension extends Extension {
|
||||
constructor(readonly store: Store) {
|
||||
super();
|
||||
}
|
||||
|
||||
static readonly [storeExtensionSymbol] = true;
|
||||
|
||||
static override setup(di: Container) {
|
||||
if (!this.key) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ValueNotExists,
|
||||
'Key is not defined in the StoreExtension'
|
||||
);
|
||||
}
|
||||
|
||||
di.add(this, [StoreIdentifier]);
|
||||
di.addImpl(StoreExtensionIdentifier(this.key), provider =>
|
||||
provider.get(this)
|
||||
);
|
||||
}
|
||||
|
||||
static readonly key: string;
|
||||
}
|
||||
|
||||
export function isStoreExtensionConstructor(
|
||||
extension: object
|
||||
): extension is typeof StoreExtension {
|
||||
return storeExtensionSymbol in extension;
|
||||
}
|
||||
@@ -6,6 +6,7 @@ export * from './extension';
|
||||
export * from './model';
|
||||
export * from './reactive';
|
||||
export * from './schema';
|
||||
export * from './store';
|
||||
export * from './transformer';
|
||||
export { type IdGenerator, nanoid, uuidv4 } from './utils/id-generator';
|
||||
export * from './yjs';
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import { createIdentifier } from '@blocksuite/global/di';
|
||||
|
||||
import type { Store } from './store';
|
||||
|
||||
export const StoreIdentifier = createIdentifier<Store>('Store');
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './identifier';
|
||||
export * from './store';
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Container, type ServiceProvider } from '@blocksuite/global/di';
|
||||
|
||||
import type { Extension, StoreExtension } from '../extension';
|
||||
import type { Blocks } from '../model';
|
||||
import { StoreIdentifier } from './identifier';
|
||||
|
||||
export interface StoreOptions {
|
||||
blocks: Blocks;
|
||||
provider?: ServiceProvider;
|
||||
extensions?: (typeof Extension | typeof StoreExtension)[];
|
||||
}
|
||||
|
||||
export class Store {
|
||||
private readonly _blocks: Blocks;
|
||||
private readonly _provider: ServiceProvider;
|
||||
|
||||
get blocks() {
|
||||
return this._blocks;
|
||||
}
|
||||
|
||||
get provider() {
|
||||
return this._provider;
|
||||
}
|
||||
|
||||
get awareness() {
|
||||
return this._blocks.awarenessStore;
|
||||
}
|
||||
|
||||
constructor(options: StoreOptions) {
|
||||
this._blocks = options.blocks;
|
||||
const container = new Container();
|
||||
container.addImpl(StoreIdentifier, () => this);
|
||||
|
||||
const userExtensions = options.extensions ?? [];
|
||||
userExtensions.forEach(extension => {
|
||||
extension.setup(container);
|
||||
});
|
||||
|
||||
this._provider = container.provider(undefined, options.provider);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user