feat(editor): merge store and blocks (#9591)

This commit is contained in:
Saul-Mirone
2025-01-08 13:01:19 +00:00
parent a0cba55a5b
commit 5842d45ab1
124 changed files with 362 additions and 414 deletions
@@ -2,7 +2,7 @@ import { type Disposable, Slot } from '@blocksuite/global/utils';
import { computed, type Signal, signal } from '@preact/signals-core';
import type { Text } from '../../reactive/index.js';
import type { Blocks } from '../blocks/blocks.js';
import type { Store } from '../store/store.js';
import type { YBlock } from './types.js';
import type { RoleType } from './zod.js';
@@ -38,7 +38,7 @@ export class BlockModel<
/**
* @deprecated use doc instead
*/
page!: Blocks;
page!: Store;
private readonly _childModels = computed(() => {
const value: BlockModel[] = [];
@@ -102,7 +102,7 @@ export class BlockModel<
return this.page;
}
set doc(doc: Blocks) {
set doc(doc: Store) {
this.page = doc;
}
@@ -1,5 +1,5 @@
import type { Schema } from '../../schema/index.js';
import type { Blocks } from '../blocks/blocks.js';
import type { Store } from '../store/store.js';
import { SyncController } from './sync-controller.js';
import type { BlockOptions, YBlock } from './types.js';
@@ -37,7 +37,7 @@ export class Block {
constructor(
readonly schema: Schema,
readonly yBlock: YBlock,
readonly doc?: Blocks,
readonly doc?: Store,
readonly options: BlockOptions = {}
) {
const onChange = !options.onChange
@@ -11,7 +11,7 @@ import {
y2Native,
} from '../../reactive/index.js';
import type { Schema } from '../../schema/schema.js';
import type { Blocks } from '../blocks/blocks.js';
import type { Store } from '../store/store.js';
import { BlockModel } from './block-model.js';
import type { YBlock } from './types.js';
import { internalPrimitives } from './zod.js';
@@ -104,7 +104,7 @@ export class SyncController {
constructor(
readonly schema: Schema,
readonly yBlock: YBlock,
readonly doc?: Blocks,
readonly doc?: Store,
readonly onChange?: (key: string, value: unknown) => void
) {
const { id, flavour, version, yChildren, props } = this._parseYBlock();
@@ -1,2 +0,0 @@
export * from './blocks.js';
export * from './query.js';
+4 -7
View File
@@ -4,15 +4,12 @@ import type * as Y from 'yjs';
import type { Schema } from '../schema/schema.js';
import type { AwarenessStore } from '../yjs/awareness.js';
import type { YBlock } from './block/types.js';
import type { Blocks } from './blocks/blocks.js';
import type { Query } from './blocks/query.js';
import type { Query } from './store/query.js';
import type { Store, StoreOptions } from './store/store.js';
import type { Workspace } from './workspace.js';
import type { DocMeta } from './workspace-meta.js';
export type GetBlocksOptions = {
query?: Query;
readonly?: boolean;
};
export type GetBlocksOptions = Omit<StoreOptions, 'schema' | 'doc'>;
export type CreateBlocksOptions = GetBlocksOptions & {
id?: string;
};
@@ -53,7 +50,7 @@ export interface Doc {
captureSync(): void;
clear(): void;
getBlocks(options?: GetBlocksOptions): Blocks;
getStore(options?: GetBlocksOptions): Store;
clearQuery(query: Query, readonly?: boolean): void;
get loaded(): boolean;
@@ -1,8 +1,8 @@
import type { BlockModel } from './block/block-model.js';
export * from './block/index.js';
export * from './blocks/index.js';
export * from './doc.js';
export * from './store/index.js';
export * from './workspace.js';
export * from './workspace-meta.js';
@@ -0,0 +1,5 @@
import { createIdentifier } from '@blocksuite/global/di';
import type { Store } from './store';
export const StoreIdentifier = createIdentifier<Store>('Store');
@@ -0,0 +1,3 @@
export * from './identifier.js';
export * from './query.js';
export * from './store.js';
@@ -1,7 +1,9 @@
import { Container, type ServiceProvider } from '@blocksuite/global/di';
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { type Disposable, Slot } from '@blocksuite/global/utils';
import { signal } from '@preact/signals-core';
import type { ExtensionType } from '../../extension/extension.js';
import type { Schema } from '../../schema/index.js';
import {
Block,
@@ -12,17 +14,22 @@ import {
} from '../block/index.js';
import type { Doc } from '../doc.js';
import { DocCRUD } from './crud.js';
import { StoreIdentifier } from './identifier.js';
import { type Query, runQuery } from './query.js';
import { syncBlockProps } from './utils.js';
type DocOptions = {
export type StoreOptions = {
schema: Schema;
blockCollection: Doc;
doc: Doc;
readonly?: boolean;
query?: Query;
provider?: ServiceProvider;
extensions?: ExtensionType[];
};
export class Blocks {
export class Store {
private readonly _provider: ServiceProvider;
private readonly _runQuery = (block: Block) => {
runQuery(this._query, block);
};
@@ -147,6 +154,10 @@ export class Blocks {
return this._doc.awarenessStore;
}
get provider() {
return this._provider;
}
get blobSync() {
return this.workspace.blobSync;
}
@@ -259,8 +270,24 @@ export class Blocks {
return this._doc.withoutTransact.bind(this._doc);
}
constructor({ schema, blockCollection, readonly, query }: DocOptions) {
this._doc = blockCollection;
constructor({
schema,
doc,
readonly,
query,
provider,
extensions,
}: StoreOptions) {
const container = new Container();
container.addImpl(StoreIdentifier, () => this);
const userExtensions = extensions ?? [];
userExtensions.forEach(extension => {
extension.setup(container);
});
this._provider = container.provider(undefined, provider);
this._doc = doc;
this.slots = {
ready: new Slot(),
@@ -271,7 +298,7 @@ export class Blocks {
yBlockUpdated: this._doc.slots.yBlockUpdated,
};
this._crud = new DocCRUD(this._yBlocks, blockCollection.schema);
this._crud = new DocCRUD(this._yBlocks, doc.schema);
this._schema = schema;
this._readonly = readonly;
if (query) {
@@ -565,7 +592,7 @@ export class Blocks {
return (this.getBlock(id)?.model ?? null) as Model | null;
}
getBlocks() {
getStore() {
return Object.values(this._blocks.peek()).map(block => block.model);
}
@@ -5,8 +5,8 @@ import type * as Y from 'yjs';
import type { Schema } from '../schema/schema.js';
import type { IdGenerator } from '../utils/id-generator.js';
import type { AwarenessStore } from '../yjs/awareness.js';
import type { Blocks } from './blocks/blocks.js';
import type { CreateBlocksOptions, Doc, GetBlocksOptions } from './doc.js';
import type { Store } from './store/store.js';
import type { WorkspaceMeta } from './workspace-meta.js';
export interface Workspace {
@@ -26,8 +26,8 @@ export interface Workspace {
docRemoved: Slot<string>;
};
createDoc(options?: CreateBlocksOptions): Blocks;
getDoc(docId: string, options?: GetBlocksOptions): Blocks | null;
createDoc(options?: CreateBlocksOptions): Store;
getDoc(docId: string, options?: GetBlocksOptions): Store | null;
removeDoc(docId: string): void;
dispose(): void;