mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 12:28:42 +00:00
refactor(editor): remove legacy blocksuite doc (#9521)
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
import type { Slot } from '@blocksuite/global/utils';
|
||||
import { assert, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { applyUpdate, encodeStateAsUpdate } from 'yjs';
|
||||
import { applyUpdate, type Doc, encodeStateAsUpdate } from 'yjs';
|
||||
|
||||
import { COLLECTION_VERSION, PAGE_VERSION } from '../consts.js';
|
||||
import type { BlockModel, Blocks, BlockSchemaType } from '../index.js';
|
||||
@@ -11,7 +11,6 @@ import { Text } from '../reactive/text.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,
|
||||
ParagraphBlockSchema,
|
||||
@@ -36,9 +35,9 @@ const defaultDocId = 'doc:home';
|
||||
const spaceId = defaultDocId;
|
||||
const spaceMetaId = 'meta';
|
||||
|
||||
function serializCollection(doc: BlockSuiteDoc): Record<string, any> {
|
||||
function serializCollection(doc: Doc): Record<string, any> {
|
||||
const spaces = {};
|
||||
doc.spaces.forEach((subDoc, key) => {
|
||||
doc.getMap('spaces').forEach((subDoc, key) => {
|
||||
// @ts-expect-error ignore
|
||||
spaces[key] = subDoc.toJSON();
|
||||
});
|
||||
@@ -200,7 +199,7 @@ describe('basic', () => {
|
||||
expect(collection2.docs.size).toBe(0);
|
||||
const update = encodeStateAsUpdate(collection.doc);
|
||||
applyUpdate(collection2.doc, update);
|
||||
expect(collection2.doc.toJSON()['spaces']).toEqual({
|
||||
expect(serializCollection(collection2.doc)['spaces']).toEqual({
|
||||
'space:0': {
|
||||
blocks: {},
|
||||
},
|
||||
@@ -215,7 +214,7 @@ describe('basic', () => {
|
||||
const doc2 = collection2.getDoc('space:0');
|
||||
assertExists(doc2);
|
||||
applyUpdate(doc2.spaceDoc, update);
|
||||
expect(collection2.doc.toJSON()['spaces']).toEqual({
|
||||
expect(serializCollection(collection2.doc)['spaces']).toEqual({
|
||||
'space:0': {
|
||||
blocks: {
|
||||
'0': {
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Slot } from '@blocksuite/global/utils';
|
||||
import type * as Y from 'yjs';
|
||||
|
||||
import { COLLECTION_VERSION, PAGE_VERSION } from '../consts.js';
|
||||
import type { BlockSuiteDoc } from '../yjs/index.js';
|
||||
import { createYProxy } from '../reactive/proxy.js';
|
||||
import type {
|
||||
DocMeta,
|
||||
DocsPropertiesMeta,
|
||||
@@ -52,7 +52,7 @@ export class DocCollectionMeta implements WorkspaceMeta {
|
||||
|
||||
commonFieldsUpdated = new Slot();
|
||||
|
||||
readonly doc: BlockSuiteDoc;
|
||||
readonly doc: Y.Doc;
|
||||
|
||||
docMetaAdded = new Slot<string>();
|
||||
|
||||
@@ -116,10 +116,13 @@ export class DocCollectionMeta implements WorkspaceMeta {
|
||||
return this._yMap.get('pages') as unknown as Y.Array<unknown>;
|
||||
}
|
||||
|
||||
constructor(doc: BlockSuiteDoc) {
|
||||
constructor(doc: Y.Doc) {
|
||||
this.doc = doc;
|
||||
this._yMap = doc.getMap(this.id);
|
||||
this._proxy = doc.getMapProxy<string, DocCollectionMetaState>(this.id);
|
||||
const map = doc.getMap(this.id) as Y.Map<
|
||||
DocCollectionMetaState[keyof DocCollectionMetaState]
|
||||
>;
|
||||
this._yMap = map;
|
||||
this._proxy = createYProxy(map);
|
||||
this._yMap.observeDeep(this._handleDocCollectionMetaEvents);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ import type { BlockModel } from '../schema/base.js';
|
||||
import type { Schema } from '../schema/schema.js';
|
||||
import type { IdGenerator } from '../utils/id-generator.js';
|
||||
import type { AwarenessStore } from '../yjs/awareness.js';
|
||||
import type { BlockSuiteDoc } from '../yjs/doc.js';
|
||||
import type { YBlock } from './doc/block/types.js';
|
||||
import type { Blocks } from './doc/doc.js';
|
||||
import type { Query } from './doc/query.js';
|
||||
@@ -76,7 +75,7 @@ export interface Workspace {
|
||||
readonly awarenessStore: AwarenessStore;
|
||||
|
||||
get schema(): Schema;
|
||||
get doc(): BlockSuiteDoc;
|
||||
get doc(): Y.Doc;
|
||||
get docs(): Map<string, Doc>;
|
||||
|
||||
slots: {
|
||||
@@ -136,7 +135,7 @@ export interface Doc {
|
||||
|
||||
get workspace(): Workspace;
|
||||
|
||||
get rootDoc(): BlockSuiteDoc;
|
||||
get rootDoc(): Y.Doc;
|
||||
get spaceDoc(): Y.Doc;
|
||||
get yBlocks(): Y.Map<YBlock>;
|
||||
}
|
||||
|
||||
@@ -6,12 +6,12 @@ import { Blocks } from '../store/doc/doc.js';
|
||||
import type { YBlock } from '../store/doc/index.js';
|
||||
import type { Query } from '../store/doc/query.js';
|
||||
import type { Doc, GetBlocksOptions, Workspace } from '../store/workspace.js';
|
||||
import type { AwarenessStore, BlockSuiteDoc } from '../yjs/index.js';
|
||||
import type { AwarenessStore } from '../yjs/index.js';
|
||||
|
||||
type DocOptions = {
|
||||
id: string;
|
||||
collection: Workspace;
|
||||
doc: BlockSuiteDoc;
|
||||
doc: Y.Doc;
|
||||
awarenessStore: AwarenessStore;
|
||||
};
|
||||
|
||||
@@ -43,12 +43,12 @@ export class TestDoc implements Doc {
|
||||
};
|
||||
|
||||
private readonly _initSubDoc = () => {
|
||||
let subDoc = this.rootDoc.spaces.get(this.id);
|
||||
let subDoc = this.rootDoc.getMap('spaces').get(this.id);
|
||||
if (!subDoc) {
|
||||
subDoc = new Y.Doc({
|
||||
guid: this.id,
|
||||
});
|
||||
this.rootDoc.spaces.set(this.id, subDoc);
|
||||
this.rootDoc.getMap('spaces').set(this.id, subDoc);
|
||||
this._loaded = true;
|
||||
this._onLoadSlot.emit();
|
||||
} else {
|
||||
@@ -107,7 +107,7 @@ export class TestDoc implements Doc {
|
||||
|
||||
readonly id: string;
|
||||
|
||||
readonly rootDoc: BlockSuiteDoc;
|
||||
readonly rootDoc: Y.Doc;
|
||||
|
||||
readonly slots = {
|
||||
historyUpdated: new Slot(),
|
||||
@@ -192,7 +192,7 @@ export class TestDoc implements Doc {
|
||||
this.rootDoc = doc;
|
||||
this.awarenessStore = awarenessStore;
|
||||
|
||||
this._ySpaceDoc = this._initSubDoc();
|
||||
this._ySpaceDoc = this._initSubDoc() as Y.Doc;
|
||||
|
||||
this._yBlocks = this._ySpaceDoc.getMap('blocks');
|
||||
this._collection = collection;
|
||||
@@ -353,7 +353,7 @@ export class TestDoc implements Doc {
|
||||
|
||||
remove() {
|
||||
this._destroy();
|
||||
this.rootDoc.spaces.delete(this.id);
|
||||
this.rootDoc.getMap('spaces').delete(this.id);
|
||||
}
|
||||
|
||||
resetHistory() {
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
import clonedeep from 'lodash.clonedeep';
|
||||
import merge from 'lodash.merge';
|
||||
import { Awareness } from 'y-protocols/awareness.js';
|
||||
import * as Y from 'yjs';
|
||||
|
||||
import type { Schema } from '../schema/index.js';
|
||||
import {
|
||||
@@ -25,11 +26,7 @@ import {
|
||||
type WorkspaceMeta,
|
||||
} from '../store/index.js';
|
||||
import { type IdGenerator, nanoid } from '../utils/id-generator.js';
|
||||
import {
|
||||
AwarenessStore,
|
||||
BlockSuiteDoc,
|
||||
type RawAwarenessState,
|
||||
} from '../yjs/index.js';
|
||||
import { AwarenessStore, type RawAwarenessState } from '../yjs/index.js';
|
||||
import { TestDoc } from './test-doc.js';
|
||||
|
||||
export type DocCollectionOptions = {
|
||||
@@ -83,7 +80,7 @@ export class TestWorkspace implements Workspace {
|
||||
|
||||
readonly blockCollections = new Map<string, TestDoc>();
|
||||
|
||||
readonly doc: BlockSuiteDoc;
|
||||
readonly doc: Y.Doc;
|
||||
|
||||
readonly docSync: DocEngine;
|
||||
|
||||
@@ -123,7 +120,7 @@ export class TestWorkspace implements Workspace {
|
||||
this._schema = schema;
|
||||
|
||||
this.id = id || '';
|
||||
this.doc = new BlockSuiteDoc({ guid: id });
|
||||
this.doc = new Y.Doc({ guid: id });
|
||||
this.awarenessStore = new AwarenessStore(
|
||||
new Awareness<RawAwarenessState>(this.doc),
|
||||
merge(clonedeep(FLAGS_PRESET), defaultFlags)
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
import type { Transaction } from 'yjs';
|
||||
import * as Y from 'yjs';
|
||||
|
||||
import { createYProxy } from '../reactive/proxy.js';
|
||||
|
||||
export type BlockSuiteDocAllowedValue =
|
||||
| Record<string, unknown>
|
||||
| unknown[]
|
||||
| Y.Text;
|
||||
export type BlockSuiteDocData = Record<string, BlockSuiteDocAllowedValue>;
|
||||
|
||||
export class BlockSuiteDoc extends Y.Doc {
|
||||
private readonly _spaces: Y.Map<Y.Doc> = this.getMap('spaces');
|
||||
|
||||
get spaces() {
|
||||
return this._spaces;
|
||||
}
|
||||
|
||||
getArrayProxy<
|
||||
Key extends keyof BlockSuiteDocData & string,
|
||||
Value extends unknown[] = BlockSuiteDocData[Key] extends unknown[]
|
||||
? BlockSuiteDocData[Key]
|
||||
: never,
|
||||
>(key: Key): Value {
|
||||
const array = super.getArray(key);
|
||||
return createYProxy(array) as Value;
|
||||
}
|
||||
|
||||
getMapProxy<
|
||||
Key extends keyof BlockSuiteDocData & string,
|
||||
Value extends Record<
|
||||
string,
|
||||
unknown
|
||||
> = BlockSuiteDocData[Key] extends Record<string, unknown>
|
||||
? BlockSuiteDocData[Key]
|
||||
: never,
|
||||
>(key: Key): Value {
|
||||
const map = super.getMap(key);
|
||||
return createYProxy(map);
|
||||
}
|
||||
|
||||
override toJSON(): Record<string, any> {
|
||||
const json = super.toJSON();
|
||||
delete json.spaces;
|
||||
const spaces: Record<string, unknown> = {};
|
||||
this.spaces.forEach((doc, key) => {
|
||||
spaces[key] = doc.toJSON();
|
||||
});
|
||||
return {
|
||||
...json,
|
||||
spaces,
|
||||
};
|
||||
}
|
||||
|
||||
override transact<T>(f: (arg0: Transaction) => T, origin?: number | string) {
|
||||
return super.transact(f, origin);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,2 @@
|
||||
export * from './awareness.js';
|
||||
export * from './doc.js';
|
||||
export * from './utils.js';
|
||||
|
||||
Reference in New Issue
Block a user