diff --git a/blocksuite/blocks/src/__tests__/database/database.unit.spec.ts b/blocksuite/blocks/src/__tests__/database/database.unit.spec.ts index 79952b9006c5d..ec3375bf30557 100644 --- a/blocksuite/blocks/src/__tests__/database/database.unit.spec.ts +++ b/blocksuite/blocks/src/__tests__/database/database.unit.spec.ts @@ -18,7 +18,11 @@ import { } from '@blocksuite/affine-model'; import { propertyModelPresets } from '@blocksuite/data-view/property-pure-presets'; import type { BlockModel, Doc } from '@blocksuite/store'; -import { DocCollection, IdGeneratorType, Schema } from '@blocksuite/store'; +import { Schema } from '@blocksuite/store'; +import { + createAutoIncrementIdGenerator, + TestWorkspace, +} from '@blocksuite/store/test'; import { beforeEach, describe, expect, test } from 'vitest'; const AffineSchemas = [ @@ -29,7 +33,7 @@ const AffineSchemas = [ ]; function createTestOptions() { - const idGenerator = IdGeneratorType.AutoIncrement; + const idGenerator = createAutoIncrementIdGenerator(); const schema = new Schema(); schema.register(AffineSchemas); return { id: 'test-collection', idGenerator, schema }; @@ -37,7 +41,7 @@ function createTestOptions() { function createTestDoc(docId = 'doc0') { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); const doc = collection.createDoc({ id: docId }); doc.load(); diff --git a/blocksuite/blocks/src/__tests__/utils/create-job.ts b/blocksuite/blocks/src/__tests__/utils/create-job.ts index 032dc178d3fcf..669180b5c62b1 100644 --- a/blocksuite/blocks/src/__tests__/utils/create-job.ts +++ b/blocksuite/blocks/src/__tests__/utils/create-job.ts @@ -1,9 +1,5 @@ -import { - DocCollection, - Job, - type JobMiddleware, - Schema, -} from '@blocksuite/store'; +import { Job, type JobMiddleware, Schema } from '@blocksuite/store'; +import { TestWorkspace } from '@blocksuite/store/test'; import { defaultImageProxyMiddleware } from '../../_common/transformers/middlewares.js'; import { AffineSchemas } from '../../schemas.js'; @@ -25,7 +21,7 @@ export function createJob(middlewares?: JobMiddleware[]) { const testMiddlewares = middlewares ?? []; testMiddlewares.push(defaultImageProxyMiddleware); const schema = new Schema().register(AffineSchemas); - const docCollection = new DocCollection({ schema }); + const docCollection = new TestWorkspace({ schema }); docCollection.meta.initialize(); return new Job({ schema, diff --git a/blocksuite/framework/block-std/src/__tests__/editor-host.unit.spec.ts b/blocksuite/framework/block-std/src/__tests__/editor-host.unit.spec.ts index 6ec2f6c57b5c8..1af2ab846fcc4 100644 --- a/blocksuite/framework/block-std/src/__tests__/editor-host.unit.spec.ts +++ b/blocksuite/framework/block-std/src/__tests__/editor-host.unit.spec.ts @@ -1,4 +1,8 @@ -import { DocCollection, IdGeneratorType, Schema } from '@blocksuite/store'; +import { Schema } from '@blocksuite/store'; +import { + createAutoIncrementIdGenerator, + TestWorkspace, +} from '@blocksuite/store/test'; import { describe, expect, test } from 'vitest'; import { effects } from '../effects.js'; @@ -14,7 +18,7 @@ import { testSpecs } from './test-spec.js'; effects(); function createTestOptions() { - const idGenerator = IdGeneratorType.AutoIncrement; + const idGenerator = createAutoIncrementIdGenerator(); const schema = new Schema(); schema.register([RootBlockSchema, NoteBlockSchema, HeadingBlockSchema]); return { id: 'test-collection', idGenerator, schema }; @@ -26,7 +30,7 @@ function wait(time: number) { describe('editor host', () => { test('editor host should rerender model when view changes', async () => { - const collection = new DocCollection(createTestOptions()); + const collection = new TestWorkspace(createTestOptions()); collection.meta.initialize(); const doc = collection.createDoc({ id: 'home' }); diff --git a/blocksuite/framework/store/package.json b/blocksuite/framework/store/package.json index bf432b5b95217..fcdf2d6cde227 100644 --- a/blocksuite/framework/store/package.json +++ b/blocksuite/framework/store/package.json @@ -35,7 +35,8 @@ "@types/lodash.merge": "^4.6.9" }, "exports": { - ".": "./src/index.ts" + ".": "./src/index.ts", + "./test": "./src/test/index.ts" }, "files": [ "src", diff --git a/blocksuite/framework/store/src/__tests__/block.unit.spec.ts b/blocksuite/framework/store/src/__tests__/block.unit.spec.ts index 751880beb45bd..3fc0f8af2e59f 100644 --- a/blocksuite/framework/store/src/__tests__/block.unit.spec.ts +++ b/blocksuite/framework/store/src/__tests__/block.unit.spec.ts @@ -9,7 +9,8 @@ import { type SchemaToModel, } from '../schema/index.js'; import { Block, type YBlock } from '../store/doc/block/index.js'; -import { DocCollection, IdGeneratorType } from '../store/index.js'; +import { TestWorkspace } from '../test/test-workspace.js'; +import { createAutoIncrementIdGenerator } from '../utils/id-generator.js'; const pageSchema = defineBlockSchema({ flavour: 'page', @@ -28,7 +29,7 @@ const pageSchema = defineBlockSchema({ type RootModel = SchemaToModel; function createTestOptions() { - const idGenerator = IdGeneratorType.AutoIncrement; + const idGenerator = createAutoIncrementIdGenerator(); const schema = new Schema(); schema.register([pageSchema]); return { id: 'test-collection', idGenerator, schema }; @@ -37,7 +38,7 @@ function createTestOptions() { const defaultDocId = 'doc:home'; function createTestDoc(docId = defaultDocId) { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); const doc = collection.createDoc({ id: docId }); doc.load(); diff --git a/blocksuite/framework/store/src/__tests__/collection.unit.spec.ts b/blocksuite/framework/store/src/__tests__/collection.unit.spec.ts index 64342ed769651..bf708872389df 100644 --- a/blocksuite/framework/store/src/__tests__/collection.unit.spec.ts +++ b/blocksuite/framework/store/src/__tests__/collection.unit.spec.ts @@ -6,8 +6,10 @@ import { applyUpdate, encodeStateAsUpdate } from 'yjs'; import { COLLECTION_VERSION, PAGE_VERSION } from '../consts.js'; import type { BlockModel, BlockSchemaType, Doc } from '../index.js'; -import { DocCollection, IdGeneratorType, Schema } from '../index.js'; +import { Schema } from '../index.js'; import type { DocMeta } from '../store/workspace.js'; +import { TestWorkspace } from '../test/test-workspace.js'; +import { createAutoIncrementIdGenerator } from '../utils/id-generator.js'; import type { BlockSuiteDoc } from '../yjs/index.js'; import { NoteBlockSchema, @@ -23,7 +25,7 @@ export const BlockSchemas = [ ] as BlockSchemaType[]; function createTestOptions() { - const idGenerator = IdGeneratorType.AutoIncrement; + const idGenerator = createAutoIncrementIdGenerator(); const schema = new Schema(); schema.register(BlockSchemas); return { id: 'test-collection', idGenerator, schema }; @@ -60,7 +62,7 @@ function createRoot(doc: Doc) { function createTestDoc(docId = defaultDocId) { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); const doc = collection.createDoc({ id: docId }); doc.load(); @@ -92,7 +94,7 @@ beforeEach(() => { describe('basic', () => { it('can init collection', () => { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); const doc = collection.createDoc({ id: 'doc:home' }); @@ -132,7 +134,7 @@ describe('basic', () => { it('init collection with custom id generator', () => { const options = createTestOptions(); let id = 100; - const collection = new DocCollection({ + const collection = new TestWorkspace({ ...options, idGenerator: () => { return String(id++); @@ -151,7 +153,7 @@ describe('basic', () => { it('doc ready lifecycle', () => { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); const doc = collection.createDoc({ id: 'space:0', @@ -176,9 +178,9 @@ describe('basic', () => { it('collection docs with yjs applyUpdate', () => { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); - const collection2 = new DocCollection(options); + const collection2 = new TestWorkspace(options); const doc = collection.createDoc({ id: 'space:0', }); @@ -378,7 +380,7 @@ describe('addBlock', () => { it('can add and remove multi docs', async () => { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); const doc0 = collection.createDoc({ id: 'doc:home' }); @@ -403,7 +405,7 @@ describe('addBlock', () => { it('can remove doc that has not been loaded', () => { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); const doc0 = collection.createDoc({ id: 'doc:home' }); @@ -414,7 +416,7 @@ describe('addBlock', () => { it('can set doc state', () => { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); collection.createDoc({ id: 'doc:home' }); @@ -456,7 +458,7 @@ describe('addBlock', () => { it('can set collection common meta fields', async () => { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); queueMicrotask(() => collection.meta.setName('hello')); await waitOnce(collection.meta.commonFieldsUpdated); @@ -863,7 +865,7 @@ describe('getBlock', () => { describe('flags', () => { it('update flags', () => { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); const awareness = collection.awarenessStore; diff --git a/blocksuite/framework/store/src/__tests__/doc.unit.spec.ts b/blocksuite/framework/store/src/__tests__/doc.unit.spec.ts index 804ddd0de5300..d2b624d0f598c 100644 --- a/blocksuite/framework/store/src/__tests__/doc.unit.spec.ts +++ b/blocksuite/framework/store/src/__tests__/doc.unit.spec.ts @@ -2,11 +2,9 @@ import { expect, test, vi } from 'vitest'; import * as Y from 'yjs'; import { Schema } from '../schema/index.js'; -import { - BlockViewType, - DocCollection, - IdGeneratorType, -} from '../store/index.js'; +import { BlockViewType } from '../store/index.js'; +import { TestWorkspace } from '../test/test-workspace.js'; +import { createAutoIncrementIdGenerator } from '../utils/id-generator.js'; import { DividerBlockSchema, ListBlockSchema, @@ -25,7 +23,7 @@ const BlockSchemas = [ ]; function createTestOptions() { - const idGenerator = IdGeneratorType.AutoIncrement; + const idGenerator = createAutoIncrementIdGenerator(); const schema = new Schema(); schema.register(BlockSchemas); return { id: 'test-collection', idGenerator, schema }; @@ -33,7 +31,7 @@ function createTestOptions() { test('trigger props updated', () => { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); const doc = collection.createDoc({ id: 'home' }); @@ -93,7 +91,7 @@ test('trigger props updated', () => { test('stash and pop', () => { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); const doc = collection.createDoc({ id: 'home' }); @@ -163,7 +161,7 @@ test('stash and pop', () => { test('always get latest value in onChange', () => { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); const doc = collection.createDoc({ id: 'home' }); @@ -210,7 +208,7 @@ test('always get latest value in onChange', () => { test('query', () => { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); const doc1 = collection.createDoc({ id: 'home' }); doc1.load(); @@ -247,7 +245,7 @@ test('query', () => { test('local readonly', () => { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); const doc1 = collection.createDoc({ id: 'home' }); doc1.load(); diff --git a/blocksuite/framework/store/src/__tests__/schema.unit.spec.ts b/blocksuite/framework/store/src/__tests__/schema.unit.spec.ts index a4455e227270b..ae27a2f5e6a40 100644 --- a/blocksuite/framework/store/src/__tests__/schema.unit.spec.ts +++ b/blocksuite/framework/store/src/__tests__/schema.unit.spec.ts @@ -5,7 +5,8 @@ import { describe, expect, it, vi } from 'vitest'; import { type BlockModel, defineBlockSchema } from '../schema/base.js'; import { SchemaValidateError } from '../schema/error.js'; import { Schema } from '../schema/index.js'; -import { DocCollection, IdGeneratorType } from '../store/index.js'; +import { TestWorkspace } from '../test/test-workspace.js'; +import { createAutoIncrementIdGenerator } from '../utils/id-generator.js'; import { DividerBlockSchema, ListBlockSchema, @@ -15,7 +16,7 @@ import { } from './test-schema.js'; function createTestOptions() { - const idGenerator = IdGeneratorType.AutoIncrement; + const idGenerator = createAutoIncrementIdGenerator(); const schema = new Schema(); schema.register(BlockSchemas); return { id: 'test-collection', idGenerator, schema }; @@ -60,7 +61,7 @@ const BlockSchemas = [ const defaultDocId = 'doc0'; function createTestDoc(docId = defaultDocId) { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); const doc = collection.createDoc({ id: docId }); doc.load(); diff --git a/blocksuite/framework/store/src/__tests__/test-utils-dom.ts b/blocksuite/framework/store/src/__tests__/test-utils-dom.ts index e677cecbc74a0..034ed38717fd4 100644 --- a/blocksuite/framework/store/src/__tests__/test-utils-dom.ts +++ b/blocksuite/framework/store/src/__tests__/test-utils-dom.ts @@ -1,11 +1,11 @@ -import type { DocCollection } from '../store/index.js'; +import type { TestWorkspace } from '../test'; declare global { interface WindowEventMap { 'test-result': CustomEvent; } interface Window { - collection: DocCollection; + collection: TestWorkspace; } } diff --git a/blocksuite/framework/store/src/__tests__/transformer.unit.spec.ts b/blocksuite/framework/store/src/__tests__/transformer.unit.spec.ts index 84dda265e25c4..51067ac912cbb 100644 --- a/blocksuite/framework/store/src/__tests__/transformer.unit.spec.ts +++ b/blocksuite/framework/store/src/__tests__/transformer.unit.spec.ts @@ -9,8 +9,9 @@ import { Schema, type SchemaToModel, } from '../schema/index.js'; -import { DocCollection, IdGeneratorType } from '../store/index.js'; +import { TestWorkspace } from '../test/test-workspace.js'; import { AssetsManager, BaseBlockTransformer } from '../transformer/index.js'; +import { createAutoIncrementIdGenerator } from '../utils/id-generator.js'; const docSchema = defineBlockSchema({ flavour: 'page', @@ -44,7 +45,7 @@ const docSchema = defineBlockSchema({ type RootBlockModel = SchemaToModel; function createTestOptions() { - const idGenerator = IdGeneratorType.AutoIncrement; + const idGenerator = createAutoIncrementIdGenerator(); const schema = new Schema(); schema.register([docSchema]); return { id: 'test-collection', idGenerator, schema }; @@ -56,7 +57,7 @@ const assets = new AssetsManager({ blob: blobCRUD }); test('model to snapshot', () => { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); const doc = collection.createDoc({ id: 'home' }); doc.load(); @@ -73,7 +74,7 @@ test('model to snapshot', () => { test('snapshot to model', async () => { const options = createTestOptions(); - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.meta.initialize(); const doc = collection.createDoc({ id: 'home' }); doc.load(); diff --git a/blocksuite/framework/store/src/store/id.ts b/blocksuite/framework/store/src/store/id.ts index 1000e4e458499..1c1b89c093fe3 100644 --- a/blocksuite/framework/store/src/store/id.ts +++ b/blocksuite/framework/store/src/store/id.ts @@ -1,11 +1,3 @@ -import { - createAutoIncrementIdGenerator, - createAutoIncrementIdGeneratorByClientId, - type IdGenerator, - nanoid, - uuidv4, -} from '../utils/id-generator.js'; - export enum IdGeneratorType { /** * **Warning**: This generator mode will crash the collaborative feature @@ -14,41 +6,9 @@ export enum IdGeneratorType { */ AutoIncrement = 'autoIncrement', - /** - * This generator is trying to fix the real-time collaboration on debug mode. - * This will make generator predictable and won't make conflict - * @link https://docs.yjs.dev/api/faq#i-get-a-new-clientid-for-every-session-is-there-a-way-to-make-it-static-for-a-peer-accessing-the-doc - */ - AutoIncrementByClientId = 'autoIncrementByClientId', - /** * Default mode, generator for the unpredictable id */ NanoID = 'nanoID', UUIDv4 = 'uuidV4', } - -export function pickIdGenerator( - idGenerator: IdGeneratorType | IdGenerator | undefined, - clientId: number -) { - if (typeof idGenerator === 'function') { - return idGenerator; - } - - switch (idGenerator) { - case IdGeneratorType.AutoIncrement: { - return createAutoIncrementIdGenerator(); - } - case IdGeneratorType.AutoIncrementByClientId: { - return createAutoIncrementIdGeneratorByClientId(clientId); - } - case IdGeneratorType.UUIDv4: { - return uuidv4; - } - case IdGeneratorType.NanoID: - default: { - return nanoid; - } - } -} diff --git a/blocksuite/framework/store/src/store/index.ts b/blocksuite/framework/store/src/store/index.ts index b7ab3da9b484c..59ba15f16f726 100644 --- a/blocksuite/framework/store/src/store/index.ts +++ b/blocksuite/framework/store/src/store/index.ts @@ -1,5 +1,3 @@ -export type * from './collection.js'; -export { DocCollection } from './collection.js'; export type * from './doc/block-collection.js'; export * from './doc/index.js'; export * from './id.js'; diff --git a/blocksuite/framework/store/src/store/meta.ts b/blocksuite/framework/store/src/store/meta.ts index 33f8e02e7686c..8eac6dd2d51e7 100644 --- a/blocksuite/framework/store/src/store/meta.ts +++ b/blocksuite/framework/store/src/store/meta.ts @@ -3,10 +3,10 @@ import type * as Y from 'yjs'; import { COLLECTION_VERSION, PAGE_VERSION } from '../consts.js'; import type { BlockSuiteDoc } from '../yjs/index.js'; -import type { DocCollection } from './collection.js'; import type { DocMeta, DocsPropertiesMeta, + Workspace, WorkspaceMeta, } from './workspace.js'; @@ -231,7 +231,7 @@ export class DocCollectionMeta implements WorkspaceMeta { /** * @internal Only for doc initialization */ - writeVersion(collection: DocCollection) { + writeVersion(collection: Workspace) { const { blockVersions, pageVersion, workspaceVersion } = this._proxy; if (!workspaceVersion) { diff --git a/blocksuite/framework/store/src/store/workspace.ts b/blocksuite/framework/store/src/store/workspace.ts index f5b64bfeeb369..745be082e1044 100644 --- a/blocksuite/framework/store/src/store/workspace.ts +++ b/blocksuite/framework/store/src/store/workspace.ts @@ -86,5 +86,5 @@ export interface Workspace { } export interface StackItem { - meta: Map<'cursor-location' | 'selection-state', unknown>; + meta: Map<'selection-state', unknown>; } diff --git a/blocksuite/framework/store/src/test/index.ts b/blocksuite/framework/store/src/test/index.ts new file mode 100644 index 0000000000000..5d61ea21bf170 --- /dev/null +++ b/blocksuite/framework/store/src/test/index.ts @@ -0,0 +1,2 @@ +export { createAutoIncrementIdGenerator } from '../utils/id-generator.js'; +export * from './test-workspace.js'; diff --git a/blocksuite/framework/store/src/store/collection.ts b/blocksuite/framework/store/src/test/test-workspace.ts similarity index 90% rename from blocksuite/framework/store/src/store/collection.ts rename to blocksuite/framework/store/src/test/test-workspace.ts index 2b89845e0afa3..bd866544385f9 100644 --- a/blocksuite/framework/store/src/store/collection.ts +++ b/blocksuite/framework/store/src/test/test-workspace.ts @@ -1,6 +1,6 @@ import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions'; import type { BlockSuiteFlags } from '@blocksuite/global/types'; -import { type Logger, NoopLogger, Slot } from '@blocksuite/global/utils'; +import { NoopLogger, Slot } from '@blocksuite/global/utils'; import { AwarenessEngine, type AwarenessSource, @@ -16,29 +16,26 @@ import merge from 'lodash.merge'; import { Awareness } from 'y-protocols/awareness.js'; import type { Schema } from '../schema/index.js'; -import type { IdGenerator } from '../utils/id-generator.js'; +import { + BlockCollection, + type CreateDocOptions, + type Doc, + DocCollectionMeta, + type GetDocOptions, + type Workspace, +} from '../store/index.js'; +import { type IdGenerator, nanoid } from '../utils/id-generator.js'; import { AwarenessStore, BlockSuiteDoc, type RawAwarenessState, } from '../yjs/index.js'; -import { BlockCollection } from './doc/block-collection.js'; -import type { Doc } from './doc/index.js'; -import type { IdGeneratorType } from './id.js'; -import { pickIdGenerator } from './id.js'; -import { DocCollectionMeta } from './meta.js'; -import type { - CreateDocOptions, - GetDocOptions, - Workspace, -} from './workspace.js'; export type DocCollectionOptions = { schema: Schema; id?: string; - idGenerator?: IdGeneratorType | IdGenerator; + idGenerator?: IdGenerator; defaultFlags?: Partial; - logger?: Logger; docSources?: { main: DocSource; shadows?: DocSource[]; @@ -70,7 +67,11 @@ const FLAGS_PRESET = { readonly: {}, } satisfies BlockSuiteFlags; -export class DocCollection implements Workspace { +/** + * Test only + * Do not use this in production + */ +export class TestWorkspace implements Workspace { protected readonly _schema: Schema; readonly awarenessStore: AwarenessStore; @@ -117,7 +118,6 @@ export class DocCollection implements Workspace { blobSources = { main: new MemoryBlobSource(), }, - logger = new NoopLogger(), }: DocCollectionOptions) { this._schema = schema; @@ -128,6 +128,8 @@ export class DocCollection implements Workspace { merge(clonedeep(FLAGS_PRESET), defaultFlags) ); + const logger = new NoopLogger(); + this.awarenessSync = new AwarenessEngine( this.awarenessStore.awareness, awarenessSources @@ -144,7 +146,7 @@ export class DocCollection implements Workspace { logger ); - this.idGenerator = pickIdGenerator(idGenerator, this.doc.clientID); + this.idGenerator = idGenerator ?? nanoid; this.meta = new DocCollectionMeta(this.doc); this._bindDocMetaEvents(); diff --git a/blocksuite/framework/store/src/utils/id-generator.ts b/blocksuite/framework/store/src/utils/id-generator.ts index 54a513f7fb4c5..a97ec922c93e4 100644 --- a/blocksuite/framework/store/src/utils/id-generator.ts +++ b/blocksuite/framework/store/src/utils/id-generator.ts @@ -8,13 +8,6 @@ export function createAutoIncrementIdGenerator(): IdGenerator { return () => (i++).toString(); } -export function createAutoIncrementIdGeneratorByClientId( - clientId: number -): IdGenerator { - let i = 0; - return () => `${clientId}:${i++}`; -} - export const uuidv4: IdGenerator = () => { return uuidv4IdGenerator(); }; diff --git a/blocksuite/playground/apps/_common/helper.ts b/blocksuite/playground/apps/_common/helper.ts index 28210561f972d..cffe9a4466be5 100644 --- a/blocksuite/playground/apps/_common/helper.ts +++ b/blocksuite/playground/apps/_common/helper.ts @@ -1,9 +1,10 @@ import { AffineSchemas } from '@blocksuite/blocks'; -import { DocCollection, Schema } from '@blocksuite/store'; +import { Schema } from '@blocksuite/store'; +import { TestWorkspace } from '@blocksuite/store/test'; export function createEmptyDoc() { const schema = new Schema().register(AffineSchemas); - const collection = new DocCollection({ schema }); + const collection = new TestWorkspace({ schema }); collection.meta.initialize(); const doc = collection.createDoc(); diff --git a/blocksuite/playground/apps/default/utils/collection.ts b/blocksuite/playground/apps/default/utils/collection.ts index a8dbbae8479bd..70da6a672410e 100644 --- a/blocksuite/playground/apps/default/utils/collection.ts +++ b/blocksuite/playground/apps/default/utils/collection.ts @@ -1,13 +1,10 @@ import { AffineSchemas } from '@blocksuite/blocks'; import type { BlockSuiteFlags } from '@blocksuite/global/types'; +import { Job, nanoid, Schema, Text } from '@blocksuite/store'; import { - DocCollection, type DocCollectionOptions, - IdGeneratorType, - Job, - Schema, - Text, -} from '@blocksuite/store'; + TestWorkspace, +} from '@blocksuite/store/test'; import { BroadcastChannelAwarenessSource, BroadcastChannelDocSource, @@ -22,7 +19,7 @@ import { WebSocketDocSource } from '../../_common/sync/websocket/doc'; const BASE_WEBSOCKET_URL = new URL(import.meta.env.PLAYGROUND_WS); export async function createDefaultDocCollection() { - const idGenerator: IdGeneratorType = IdGeneratorType.NanoID; + const idGenerator = nanoid; const schema = new Schema(); schema.register(AffineSchemas); @@ -79,7 +76,7 @@ export async function createDefaultDocCollection() { ...flags, }, }; - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.start(); // debug info @@ -99,7 +96,7 @@ export async function createDefaultDocCollection() { return collection; } -export async function initDefaultDocCollection(collection: DocCollection) { +export async function initDefaultDocCollection(collection: TestWorkspace) { const params = new URLSearchParams(location.search); await collection.waitForSynced(); diff --git a/blocksuite/playground/apps/starter/utils/collection.ts b/blocksuite/playground/apps/starter/utils/collection.ts index e1dd74bc5c3e4..d55a40f791c3a 100644 --- a/blocksuite/playground/apps/starter/utils/collection.ts +++ b/blocksuite/playground/apps/starter/utils/collection.ts @@ -1,14 +1,12 @@ import { AffineSchemas, TestUtils } from '@blocksuite/blocks'; import type { BlockSuiteFlags } from '@blocksuite/global/types'; import { assertExists } from '@blocksuite/global/utils'; +import { type BlockCollection, Job, nanoid, Schema } from '@blocksuite/store'; import { - type BlockCollection, - DocCollection, + createAutoIncrementIdGenerator, type DocCollectionOptions, - IdGeneratorType, - Job, - Schema, -} from '@blocksuite/store'; + TestWorkspace, +} from '@blocksuite/store/test'; import { type BlobSource, BroadcastChannelAwarenessSource, @@ -30,9 +28,7 @@ export function createStarterDocCollection() { const collectionId = room ?? 'starter'; const schema = new Schema(); schema.register(AffineSchemas); - const idGenerator = isE2E - ? IdGeneratorType.AutoIncrement - : IdGeneratorType.NanoID; + const idGenerator = isE2E ? createAutoIncrementIdGenerator() : nanoid; let docSources: DocCollectionOptions['docSources']; if (room) { @@ -78,7 +74,7 @@ export function createStarterDocCollection() { docSources, blobSources, }; - const collection = new DocCollection(options); + const collection = new TestWorkspace(options); collection.start(); // debug info @@ -99,7 +95,7 @@ export function createStarterDocCollection() { return collection; } -export async function initStarterDocCollection(collection: DocCollection) { +export async function initStarterDocCollection(collection: TestWorkspace) { // init from other clients if (room && !params.has('init')) { const firstCollection = collection.docs.values().next().value as @@ -129,7 +125,7 @@ export async function initStarterDocCollection(collection: DocCollection) { // use built-in init function const functionMap = new Map< string, - (collection: DocCollection, id: string) => Promise | void + (collection: TestWorkspace, id: string) => Promise | void >(); Object.values( (await import('../data/index.js')) as Record diff --git a/blocksuite/presets/src/__tests__/utils/setup.ts b/blocksuite/presets/src/__tests__/utils/setup.ts index b6f4a5d916cc1..f8db7deec31c2 100644 --- a/blocksuite/presets/src/__tests__/utils/setup.ts +++ b/blocksuite/presets/src/__tests__/utils/setup.ts @@ -13,12 +13,11 @@ import { } from '@blocksuite/blocks'; import { AffineSchemas } from '@blocksuite/blocks/schemas'; import { assertExists } from '@blocksuite/global/utils'; +import { Schema, Text } from '@blocksuite/store'; import { - DocCollection, - IdGeneratorType, - Schema, - Text, -} from '@blocksuite/store'; + createAutoIncrementIdGenerator, + TestWorkspace, +} from '@blocksuite/store/test'; import { AffineEditorContainer } from '../../index.js'; @@ -28,7 +27,7 @@ function createCollectionOptions() { schema.register(AffineSchemas); - const idGenerator: IdGeneratorType = IdGeneratorType.AutoIncrement; // works only in single user mode + const idGenerator = createAutoIncrementIdGenerator(); return { id: room, @@ -44,7 +43,7 @@ function createCollectionOptions() { }; } -function initCollection(collection: DocCollection) { +function initCollection(collection: TestWorkspace) { const doc = collection.createDoc({ id: 'doc:home' }); doc.load(() => { @@ -56,7 +55,7 @@ function initCollection(collection: DocCollection) { doc.resetHistory(); } -async function createEditor(collection: DocCollection, mode: DocMode = 'page') { +async function createEditor(collection: TestWorkspace, mode: DocMode = 'page') { const app = document.createElement('div'); const blockCollection = collection.docs.values().next().value as | BlockCollection @@ -86,7 +85,7 @@ async function createEditor(collection: DocCollection, mode: DocMode = 'page') { } export async function setupEditor(mode: DocMode = 'page') { - const collection = new DocCollection(createCollectionOptions()); + const collection = new TestWorkspace(createCollectionOptions()); collection.meta.initialize(); window.collection = collection; @@ -113,12 +112,12 @@ export function cleanup() { declare global { const editor: AffineEditorContainer; const doc: Doc; - const collection: DocCollection; + const collection: TestWorkspace; const job: Job; interface Window { editor: AffineEditorContainer; doc: Doc; job: Job; - collection: DocCollection; + collection: TestWorkspace; } } diff --git a/packages/frontend/core/src/blocksuite/presets/_common/utils/markdown-utils.ts b/packages/frontend/core/src/blocksuite/presets/_common/utils/markdown-utils.ts index f9dfb803c93a8..2785575a0f461 100644 --- a/packages/frontend/core/src/blocksuite/presets/_common/utils/markdown-utils.ts +++ b/packages/frontend/core/src/blocksuite/presets/_common/utils/markdown-utils.ts @@ -14,17 +14,18 @@ import { titleMiddleware, } from '@blocksuite/affine/blocks'; import type { ServiceProvider } from '@blocksuite/affine/global/di'; -import type { JobMiddleware, Schema } from '@blocksuite/affine/store'; -import { Job } from '@blocksuite/affine/store'; -import { assertExists } from '@blocksuite/global/utils'; +import { assertExists } from '@blocksuite/affine/global/utils'; import type { BlockModel, BlockSnapshot, Doc, DraftModel, + JobMiddleware, + Schema, Slice, SliceSnapshot, -} from '@blocksuite/store'; +} from '@blocksuite/affine/store'; +import { Job } from '@blocksuite/affine/store'; const updateSnapshotText = ( point: TextRangePoint, diff --git a/packages/frontend/core/src/blocksuite/presets/ai/mini-mindmap/__tests__/mindmap-preview.unit.spec.ts b/packages/frontend/core/src/blocksuite/presets/ai/mini-mindmap/__tests__/mindmap-preview.unit.spec.ts index 54af06e2e0224..be68835b12fbe 100644 --- a/packages/frontend/core/src/blocksuite/presets/ai/mini-mindmap/__tests__/mindmap-preview.unit.spec.ts +++ b/packages/frontend/core/src/blocksuite/presets/ai/mini-mindmap/__tests__/mindmap-preview.unit.spec.ts @@ -4,7 +4,8 @@ import { markdownInlineToDeltaMatchers, } from '@blocksuite/affine/blocks'; import { Container } from '@blocksuite/affine/global/di'; -import { DocCollection, Schema } from '@blocksuite/affine/store'; +import { Schema } from '@blocksuite/store'; +import { TestWorkspace } from '@blocksuite/store/test'; import { describe, expect, test } from 'vitest'; import { markdownToMindmap } from '../mindmap-preview.js'; @@ -28,7 +29,7 @@ describe('markdownToMindmap: convert markdown list to a mind map tree', () => { - Text D - Text E `; - const collection = new DocCollection({ schema: new Schema() }); + const collection = new TestWorkspace({ schema: new Schema() }); collection.meta.initialize(); const doc = collection.createDoc(); const nodes = markdownToMindmap(markdown, doc, provider); @@ -66,7 +67,7 @@ describe('markdownToMindmap: convert markdown list to a mind map tree', () => { - Text D - Text E `; - const collection = new DocCollection({ schema: new Schema() }); + const collection = new TestWorkspace({ schema: new Schema() }); collection.meta.initialize(); const doc = collection.createDoc(); const nodes = markdownToMindmap(markdown, doc, provider); @@ -98,7 +99,7 @@ describe('markdownToMindmap: convert markdown list to a mind map tree', () => { test('empty case', () => { const markdown = ''; - const collection = new DocCollection({ schema: new Schema() }); + const collection = new TestWorkspace({ schema: new Schema() }); collection.meta.initialize(); const doc = collection.createDoc(); const nodes = markdownToMindmap(markdown, doc, provider); diff --git a/packages/frontend/core/src/components/page-list/__tests__/use-block-suite-page-preview.spec.ts b/packages/frontend/core/src/components/page-list/__tests__/use-block-suite-page-preview.spec.ts index d8fba4c4896a8..0c1ee0f192ab0 100644 --- a/packages/frontend/core/src/components/page-list/__tests__/use-block-suite-page-preview.spec.ts +++ b/packages/frontend/core/src/components/page-list/__tests__/use-block-suite-page-preview.spec.ts @@ -6,20 +6,21 @@ import 'fake-indexeddb/auto'; import { AffineSchemas } from '@blocksuite/affine/blocks/schemas'; import { assertExists } from '@blocksuite/affine/global/utils'; import type { Doc } from '@blocksuite/affine/store'; -import { DocCollection, Schema } from '@blocksuite/affine/store'; +import { Schema } from '@blocksuite/store'; +import { TestWorkspace } from '@blocksuite/store/test'; import { renderHook } from '@testing-library/react'; import { useAtomValue } from 'jotai'; import { beforeEach, describe, expect, test, vi } from 'vitest'; import { useBlockSuitePagePreview } from '../use-block-suite-page-preview'; -let docCollection: DocCollection; +let docCollection: TestWorkspace; const schema = new Schema(); schema.register(AffineSchemas); beforeEach(async () => { vi.useFakeTimers({ toFake: ['requestIdleCallback'] }); - docCollection = new DocCollection({ id: 'test', schema }); + docCollection = new TestWorkspace({ id: 'test', schema }); docCollection.meta.initialize(); const initPage = async (page: Doc) => { page.load(); diff --git a/packages/frontend/core/src/modules/at-menu-config/services/index.ts b/packages/frontend/core/src/modules/at-menu-config/services/index.ts index 8b600a58e8fb0..cdeaaa8ccc9bc 100644 --- a/packages/frontend/core/src/modules/at-menu-config/services/index.ts +++ b/packages/frontend/core/src/modules/at-menu-config/services/index.ts @@ -9,6 +9,7 @@ import { type LinkedWidgetConfig, LinkedWidgetUtils, } from '@blocksuite/affine/blocks'; +import type { DocMeta } from '@blocksuite/affine/store'; import { Text } from '@blocksuite/affine/store'; import { createSignalFromObservable } from '@blocksuite/affine-shared/utils'; import type { EditorHost } from '@blocksuite/block-std'; @@ -17,7 +18,6 @@ import { NewXxxEdgelessIcon, NewXxxPageIcon, } from '@blocksuite/icons/lit'; -import type { DocMeta } from '@blocksuite/store'; import { computed } from '@preact/signals-core'; import { Service } from '@toeverything/infra'; import { cssVarV2 } from '@toeverything/theme/v2';