refactor(editor): rename store api (#9518)

This commit is contained in:
Saul-Mirone
2025-01-04 12:51:56 +00:00
parent 650e4fb6b2
commit c773982ced
83 changed files with 293 additions and 290 deletions

View File

@@ -23,7 +23,7 @@ export class Blocks {
runQuery(this._query, block);
};
protected readonly _blockCollection: Doc;
protected readonly _doc: Doc;
protected readonly _blocks = signal<Record<string, Block>>({});
@@ -136,19 +136,19 @@ export class Blocks {
};
private get _yBlocks() {
return this._blockCollection.yBlocks;
return this._doc.yBlocks;
}
get awarenessStore() {
return this._blockCollection.awarenessStore;
return this._doc.awarenessStore;
}
get blobSync() {
return this.collection.blobSync;
return this.workspace.blobSync;
}
get blockCollection() {
return this._blockCollection;
get doc() {
return this._doc;
}
get blocks() {
@@ -160,31 +160,31 @@ export class Blocks {
}
get canRedo() {
return this._blockCollection.canRedo;
return this._doc.canRedo;
}
get canUndo() {
return this._blockCollection.canUndo;
return this._doc.canUndo;
}
get captureSync() {
return this._blockCollection.captureSync.bind(this._blockCollection);
return this._doc.captureSync.bind(this._doc);
}
get clear() {
return this._blockCollection.clear.bind(this._blockCollection);
return this._doc.clear.bind(this._doc);
}
get collection() {
return this._blockCollection.collection;
get workspace() {
return this._doc.workspace;
}
get history() {
return this._blockCollection.history;
return this._doc.history;
}
get id() {
return this._blockCollection.id;
return this._doc.id;
}
get isEmpty() {
@@ -192,40 +192,37 @@ export class Blocks {
}
get loaded() {
return this._blockCollection.loaded;
return this._doc.loaded;
}
get meta() {
return this._blockCollection.meta;
return this._doc.meta;
}
get readonly() {
if (this._blockCollection.readonly) {
if (this._doc.readonly) {
return true;
}
return this._readonly === true;
}
set readonly(value: boolean) {
this._blockCollection.awarenessStore.setReadonly(
this._blockCollection,
value
);
this._doc.awarenessStore.setReadonly(this._doc, value);
if (this._readonly !== undefined && this._readonly !== value) {
this._readonly = value;
}
}
get ready() {
return this._blockCollection.ready;
return this._doc.ready;
}
get redo() {
return this._blockCollection.redo.bind(this._blockCollection);
return this._doc.redo.bind(this._doc);
}
get resetHistory() {
return this._blockCollection.resetHistory.bind(this._blockCollection);
return this._doc.resetHistory.bind(this._doc);
}
get root() {
@@ -235,7 +232,7 @@ export class Blocks {
}
get rootDoc() {
return this._blockCollection.rootDoc;
return this._doc.rootDoc;
}
get schema() {
@@ -243,31 +240,31 @@ export class Blocks {
}
get spaceDoc() {
return this._blockCollection.spaceDoc;
return this._doc.spaceDoc;
}
get transact() {
return this._blockCollection.transact.bind(this._blockCollection);
return this._doc.transact.bind(this._doc);
}
get undo() {
return this._blockCollection.undo.bind(this._blockCollection);
return this._doc.undo.bind(this._doc);
}
get withoutTransact() {
return this._blockCollection.withoutTransact.bind(this._blockCollection);
return this._doc.withoutTransact.bind(this._doc);
}
constructor({ schema, blockCollection, readonly, query }: DocOptions) {
this._blockCollection = blockCollection;
this._doc = blockCollection;
this.slots = {
ready: new Slot(),
rootAdded: new Slot(),
rootDeleted: new Slot(),
blockUpdated: new Slot(),
historyUpdated: this._blockCollection.slots.historyUpdated,
yBlockUpdated: this._blockCollection.slots.yBlockUpdated,
historyUpdated: this._doc.slots.historyUpdated,
yBlockUpdated: this._doc.slots.yBlockUpdated,
};
this._crud = new DocCRUD(this._yBlocks, blockCollection.schema);
@@ -284,7 +281,7 @@ export class Blocks {
this._onBlockAdded(id, true);
});
this._disposeBlockUpdated = this._blockCollection.slots.yBlockUpdated.on(
this._disposeBlockUpdated = this._doc.slots.yBlockUpdated.on(
({ type, id }) => {
switch (type) {
case 'add': {
@@ -424,7 +421,7 @@ export class Blocks {
);
}
const id = blockProps.id ?? this._blockCollection.collection.idGenerator();
const id = blockProps.id ?? this._doc.workspace.idGenerator();
this.transact(() => {
this._crud.addBlock(
@@ -635,7 +632,7 @@ export class Blocks {
}
load(initFn?: () => void) {
this._blockCollection.load(initFn);
this._doc.load(initFn);
this.slots.ready.emit();
return this;
}

View File

@@ -30,17 +30,18 @@ export interface DocMeta {
favorite?: boolean;
}
export type GetDocOptions = {
export type GetBlocksOptions = {
query?: Query;
readonly?: boolean;
};
export type CreateDocOptions = GetDocOptions & {
export type CreateBlocksOptions = GetBlocksOptions & {
id?: string;
};
export interface WorkspaceMeta {
get docMetas(): DocMeta[];
addDocMeta(props: DocMeta, index?: number): void;
getDocMeta(id: string): DocMeta | undefined;
setDocMeta(id: string, props: Partial<DocMeta>): void;
removeDocMeta(id: string): void;
@@ -60,6 +61,10 @@ export interface WorkspaceMeta {
writeVersion(workspace: Workspace): void;
get docs(): unknown[] | undefined;
initialize(): void;
docMetaAdded: Slot<string>;
docMetaRemoved: Slot<string>;
docMetaUpdated: Slot;
}
export interface Workspace {
@@ -80,8 +85,8 @@ export interface Workspace {
docRemoved: Slot<string>;
};
createDoc(options?: CreateDocOptions): Blocks;
getDoc(docId: string, options?: GetDocOptions): Blocks | null;
createDoc(options?: CreateBlocksOptions): Blocks;
getDoc(docId: string, options?: GetBlocksOptions): Blocks | null;
removeDoc(docId: string): void;
dispose(): void;
@@ -111,6 +116,7 @@ export interface Doc {
>;
};
get history(): Y.UndoManager;
get canRedo(): boolean;
get canUndo(): boolean;
undo(): void;
@@ -121,15 +127,14 @@ export interface Doc {
captureSync(): void;
clear(): void;
getDoc(options?: GetDocOptions): Blocks;
getBlocks(options?: GetBlocksOptions): Blocks;
clearQuery(query: Query, readonly?: boolean): void;
get history(): Y.UndoManager;
get loaded(): boolean;
get readonly(): boolean;
get awarenessStore(): AwarenessStore;
get collection(): Workspace;
get workspace(): Workspace;
get rootDoc(): BlockSuiteDoc;
get spaceDoc(): Y.Doc;

View File

@@ -5,7 +5,7 @@ import * as Y from 'yjs';
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, GetDocOptions, Workspace } from '../store/workspace.js';
import type { Doc, GetBlocksOptions, Workspace } from '../store/workspace.js';
import type { AwarenessStore, BlockSuiteDoc } from '../yjs/index.js';
type DocOptions = {
@@ -124,7 +124,7 @@ export class TestDoc implements Doc {
};
get blobSync() {
return this.collection.blobSync;
return this.workspace.blobSync;
}
get canRedo() {
@@ -143,12 +143,12 @@ export class TestDoc implements Doc {
return this._canUndo$;
}
get collection() {
get workspace() {
return this._collection;
}
get docSync() {
return this.collection.docSync;
return this.workspace.docSync;
}
get history() {
@@ -164,7 +164,7 @@ export class TestDoc implements Doc {
}
get meta() {
return this.collection.meta.getDocMeta(this.id);
return this.workspace.meta.getDocMeta(this.id);
}
get readonly(): boolean {
@@ -176,7 +176,7 @@ export class TestDoc implements Doc {
}
get schema() {
return this.collection.schema;
return this.workspace.schema;
}
get spaceDoc() {
@@ -204,8 +204,8 @@ export class TestDoc implements Doc {
private _handleVersion() {
// Initialization from empty yDoc, indicating that the document is new.
if (!this.collection.meta.hasVersion) {
this.collection.meta.writeVersion(this.collection);
if (!this.workspace.meta.hasVersion) {
this.workspace.meta.writeVersion(this.workspace);
}
}
@@ -283,7 +283,7 @@ export class TestDoc implements Doc {
}
}
getDoc({ readonly, query }: GetDocOptions = {}) {
getBlocks({ readonly, query }: GetBlocksOptions = {}) {
const readonlyKey = this._getReadonlyKey(readonly);
const key = JSON.stringify(query);
@@ -294,7 +294,7 @@ export class TestDoc implements Doc {
const doc = new Blocks({
blockCollection: this,
schema: this.collection.schema,
schema: this.workspace.schema,
readonly,
query,
});
@@ -311,7 +311,7 @@ export class TestDoc implements Doc {
this._ySpaceDoc.load();
if ((this.collection.meta.docs?.length ?? 0) <= 1) {
if ((this.workspace.meta.docs?.length ?? 0) <= 1) {
this._handleVersion();
}

View File

@@ -18,10 +18,11 @@ import { Awareness } from 'y-protocols/awareness.js';
import type { Schema } from '../schema/index.js';
import {
type Blocks,
type CreateDocOptions,
type CreateBlocksOptions,
DocCollectionMeta,
type GetDocOptions,
type GetBlocksOptions,
type Workspace,
type WorkspaceMeta,
} from '../store/index.js';
import { type IdGenerator, nanoid } from '../utils/id-generator.js';
import {
@@ -90,7 +91,7 @@ export class TestWorkspace implements Workspace {
readonly idGenerator: IdGenerator;
meta: DocCollectionMeta;
meta: WorkspaceMeta;
slots = {
docListUpdated: new Slot(),
@@ -191,7 +192,7 @@ export class TestWorkspace implements Workspace {
* If the `init` parameter is passed, a `surface`, `note`, and `paragraph` block
* will be created in the doc simultaneously.
*/
createDoc(options: CreateDocOptions = {}) {
createDoc(options: CreateBlocksOptions = {}) {
const { id: docId = this.idGenerator(), query, readonly } = options;
if (this._hasDoc(docId)) {
throw new BlockSuiteError(
@@ -229,9 +230,9 @@ export class TestWorkspace implements Workspace {
return space ?? null;
}
getDoc(docId: string, options?: GetDocOptions): Blocks | null {
getDoc(docId: string, options?: GetBlocksOptions): Blocks | null {
const collection = this.getBlockCollection(docId);
return collection?.getDoc(options) ?? null;
return collection?.getBlocks(options) ?? null;
}
removeDoc(docId: string) {

View File

@@ -25,7 +25,7 @@ export class Slice {
static fromModels(doc: Blocks, models: DraftModel[]) {
return new Slice({
content: models,
workspaceId: doc.collection.id,
workspaceId: doc.workspace.id,
pageId: doc.id,
});
}