Skip to content

Commit

Permalink
refactor(editor): move workspace implementation in affine
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Jan 3, 2025
1 parent 897c7d4 commit 13e1893
Show file tree
Hide file tree
Showing 18 changed files with 284 additions and 48 deletions.
9 changes: 7 additions & 2 deletions blocksuite/affine/all/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"@blocksuite/global": "workspace:*",
"@blocksuite/inline": "workspace:*",
"@blocksuite/presets": "workspace:*",
"@blocksuite/store": "workspace:*"
"@blocksuite/store": "workspace:*",
"@blocksuite/sync": "workspace:*"
},
"exports": {
".": "./src/index.ts",
Expand All @@ -37,7 +38,8 @@
"./inline/types": "./src/inline/types.ts",
"./presets": "./src/presets/index.ts",
"./blocks": "./src/blocks/index.ts",
"./blocks/schemas": "./src/blocks/schemas.ts"
"./blocks/schemas": "./src/blocks/schemas.ts",
"./sync": "./src/sync/index.ts"
},
"typesVersions": {
"*": {
Expand Down Expand Up @@ -88,6 +90,9 @@
],
"blocks/schemas": [
"dist/blocks/schemas.d.ts"
],
"sync": [
"dist/sync/index.d.ts"
]
}
},
Expand Down
1 change: 1 addition & 0 deletions blocksuite/affine/all/src/sync/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@blocksuite/sync';
4 changes: 0 additions & 4 deletions blocksuite/framework/store/src/store/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ const FLAGS_PRESET = {
readonly: {},
} satisfies BlockSuiteFlags;

export interface StackItem {
meta: Map<'cursor-location' | 'selection-state', unknown>;
}

export class DocCollection implements Workspace {
protected readonly _schema: Schema;

Expand Down
2 changes: 1 addition & 1 deletion blocksuite/framework/store/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export { DocCollection } from './collection.js';
export type * from './doc/block-collection.js';
export * from './doc/index.js';
export * from './id.js';
export type * from './meta.js';
export * from './meta.js';
export * from './workspace.js';
4 changes: 4 additions & 0 deletions blocksuite/framework/store/src/store/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ export interface Workspace {

dispose(): void;
}

export interface StackItem {
meta: Map<'cursor-location' | 'selection-state', unknown>;
}
3 changes: 3 additions & 0 deletions blocksuite/framework/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
},
{
"path": "./store"
},
{
"path": "./sync"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { WorkspaceImpl } from '@affine/core/modules/workspace/impl/workspace';
import type {
EditorHost,
TextRangePoint,
Expand All @@ -14,7 +15,7 @@ import {
} from '@blocksuite/affine/blocks';
import type { ServiceProvider } from '@blocksuite/affine/global/di';
import type { JobMiddleware, Schema } from '@blocksuite/affine/store';
import { DocCollection, Job } from '@blocksuite/affine/store';
import { Job } from '@blocksuite/affine/store';
import { assertExists } from '@blocksuite/global/utils';
import type {
BlockModel,
Expand Down Expand Up @@ -207,7 +208,7 @@ export async function markDownToDoc(
additionalMiddlewares?: JobMiddleware[]
) {
// Should not create a new doc in the original collection
const collection = new DocCollection({
const collection = new WorkspaceImpl({
schema,
});
collection.meta.initialize();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { WorkspaceImpl } from '@affine/core/modules/workspace/impl/workspace';
import { BlockStdScope, type EditorHost } from '@blocksuite/affine/block-std';
import {
type AffineAIPanelWidgetConfig,
Expand All @@ -6,7 +7,7 @@ import {
import { AffineSchemas } from '@blocksuite/affine/blocks/schemas';
import { WithDisposable } from '@blocksuite/affine/global/utils';
import type { Doc } from '@blocksuite/affine/store';
import { DocCollection, Schema } from '@blocksuite/affine/store';
import { Schema } from '@blocksuite/affine/store';
import { css, html, LitElement, nothing } from 'lit';
import { property, query } from 'lit/decorators.js';
import { createRef, type Ref, ref } from 'lit/directives/ref.js';
Expand Down Expand Up @@ -54,7 +55,7 @@ export class AISlidesRenderer extends WithDisposable(LitElement) {

private _doc!: Doc;

private _docCollection: DocCollection | null = null;
private _docCollection: WorkspaceImpl | null = null;

@query('editor-host')
private accessor _editorHost!: EditorHost;
Expand Down Expand Up @@ -220,7 +221,7 @@ export class AISlidesRenderer extends WithDisposable(LitElement) {
super.connectedCallback();

const schema = new Schema().register(AffineSchemas);
const collection = new DocCollection({
const collection = new WorkspaceImpl({
schema,
id: 'SLIDES_PREVIEW',
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { WorkspaceImpl } from '@affine/core/modules/workspace/impl/workspace.js';
import { BlockStdScope, type EditorHost } from '@blocksuite/affine/block-std';
import {
MarkdownAdapter,
Expand All @@ -13,7 +14,6 @@ import type { ServiceProvider } from '@blocksuite/affine/global/di';
import { WithDisposable } from '@blocksuite/affine/global/utils';
import {
type Doc,
DocCollection,
type DocCollectionOptions,
IdGeneratorType,
Job,
Expand Down Expand Up @@ -109,7 +109,7 @@ export class MiniMindmapPreview extends WithDisposable(LitElement) {
awarenessSources: [],
};

const collection = new DocCollection(options);
const collection = new WorkspaceImpl(options);
collection.meta.initialize();
collection.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { useDocMetaHelper } from '@affine/core/components/hooks/use-block-suite-
import { useDocCollectionPage } from '@affine/core/components/hooks/use-block-suite-workspace-page';
import { FetchService, GraphQLService } from '@affine/core/modules/cloud';
import { getAFFiNEWorkspaceSchema } from '@affine/core/modules/workspace';
import { WorkspaceImpl } from '@affine/core/modules/workspace/impl/workspace';
import { DebugLogger } from '@affine/debug';
import type { ListHistoryQuery } from '@affine/graphql';
import { listHistoryQuery, recoverDocMutation } from '@affine/graphql';
import { i18nTime } from '@affine/i18n';
import { assertEquals } from '@blocksuite/affine/global/utils';
import { DocCollection, type Workspace } from '@blocksuite/affine/store';
import type { Workspace } from '@blocksuite/affine/store';
import { useService } from '@toeverything/infra';
import { useEffect, useMemo } from 'react';
import useSWRImmutable from 'swr/immutable';
Expand Down Expand Up @@ -114,11 +115,9 @@ const getOrCreateShellWorkspace = (
fetchService,
graphQLService
);
docCollection = new DocCollection({
docCollection = new WorkspaceImpl({
id: workspaceId,
blobSources: {
main: blobStorage,
},
blobSource: blobStorage,
schema: getAFFiNEWorkspaceSchema(),
});
docCollectionMap.set(workspaceId, docCollection);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { WorkspaceImpl } from '@affine/core/modules/workspace/impl/workspace';
import { AffineSchemas } from '@blocksuite/affine/blocks';
import type { Doc, DocSnapshot } from '@blocksuite/affine/store';
import { DocCollection, Job, Schema } from '@blocksuite/affine/store';
import { Job, Schema } from '@blocksuite/affine/store';

const getCollection = (() => {
let collection: DocCollection | null = null;
let collection: WorkspaceImpl | null = null;
return async function () {
if (collection) {
return collection;
}
const schema = new Schema();
schema.register(AffineSchemas);
collection = new DocCollection({ schema });
collection = new WorkspaceImpl({ schema });
collection.meta.initialize();
return collection;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
import { Container } from '@blocksuite/affine/global/di';
import {
createYProxy,
DocCollection,
type DraftModel,
Job,
type JobMiddleware,
Expand All @@ -35,6 +34,7 @@ import {
} from 'yjs';

import { getAFFiNEWorkspaceSchema } from '../../workspace/global-schema';
import { WorkspaceImpl } from '../../workspace/impl/workspace';
import type { BlockIndexSchema, DocIndexSchema } from '../schema';
import type {
WorkerIngoingMessage,
Expand Down Expand Up @@ -118,7 +118,7 @@ const bookmarkFlavours = new Set([
'affine:embed-loom',
]);

const markdownPreviewDocCollection = new DocCollection({
const markdownPreviewDocCollection = new WorkspaceImpl({
id: 'indexer',
schema: blocksuiteSchema,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
getWorkspaceInfoQuery,
getWorkspacesQuery,
} from '@affine/graphql';
import { DocCollection } from '@blocksuite/affine/store';
import {
type BlobStorage,
catchErrorInto,
Expand All @@ -20,7 +19,6 @@ import {
Service,
} from '@toeverything/infra';
import { isEqual } from 'lodash-es';
import { nanoid } from 'nanoid';
import { EMPTY, map, mergeMap, Observable, switchMap } from 'rxjs';
import { encodeStateAsUpdate } from 'yjs';

Expand All @@ -43,6 +41,7 @@ import {
type WorkspaceMetadata,
type WorkspaceProfileInfo,
} from '../../workspace';
import { WorkspaceImpl } from '../../workspace/impl/workspace';
import type { WorkspaceEngineStorageProvider } from '../providers/engine';
import { BroadcastChannelAwarenessConnection } from './engine/awareness-broadcast-channel';
import { CloudAwarenessConnection } from './engine/awareness-cloud';
Expand Down Expand Up @@ -101,7 +100,7 @@ class CloudWorkspaceFlavourProvider implements WorkspaceFlavourProvider {

async createWorkspace(
initial: (
docCollection: DocCollection,
docCollection: WorkspaceImpl,
blobStorage: BlobStorage,
docStorage: DocStorage
) => Promise<void>
Expand All @@ -117,13 +116,10 @@ class CloudWorkspaceFlavourProvider implements WorkspaceFlavourProvider {
const blobStorage = this.storageProvider.getBlobStorage(workspaceId);
const docStorage = this.storageProvider.getDocStorage(workspaceId);

const docCollection = new DocCollection({
const docCollection = new WorkspaceImpl({
id: workspaceId,
idGenerator: () => nanoid(),
schema: getAFFiNEWorkspaceSchema(),
blobSources: {
main: blobStorage,
},
blobSource: blobStorage,
});

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DebugLogger } from '@affine/debug';
import { DocCollection } from '@blocksuite/affine/store';
import type {
BlobStorage,
DocStorage,
Expand All @@ -20,6 +19,7 @@ import {
type WorkspaceMetadata,
type WorkspaceProfileInfo,
} from '../../workspace';
import { WorkspaceImpl } from '../../workspace/impl/workspace';
import type { WorkspaceEngineStorageProvider } from '../providers/engine';
import { BroadcastChannelAwarenessConnection } from './engine/awareness-broadcast-channel';
import { StaticBlobStorage } from './engine/blob-static';
Expand Down Expand Up @@ -79,7 +79,7 @@ class LocalWorkspaceFlavourProvider implements WorkspaceFlavourProvider {
}
async createWorkspace(
initial: (
docCollection: DocCollection,
docCollection: WorkspaceImpl,
blobStorage: BlobStorage,
docStorage: DocStorage
) => Promise<void>
Expand All @@ -90,11 +90,10 @@ class LocalWorkspaceFlavourProvider implements WorkspaceFlavourProvider {
const blobStorage = this.storageProvider.getBlobStorage(id);
const docStorage = this.storageProvider.getDocStorage(id);

const docCollection = new DocCollection({
const docCollection = new WorkspaceImpl({
id: id,
idGenerator: () => nanoid(),
schema: getAFFiNEWorkspaceSchema(),
blobSources: { main: blobStorage },
blobSource: blobStorage,
});

try {
Expand Down
16 changes: 5 additions & 11 deletions packages/frontend/core/src/modules/workspace/entities/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import {
DocCollection,
type Workspace as BSWorkspace,
} from '@blocksuite/affine/store';
import type { Workspace as WorkspaceInterface } from '@blocksuite/affine/store';
import { Entity, LiveData } from '@toeverything/infra';
import { nanoid } from 'nanoid';
import { Observable } from 'rxjs';
import type { Awareness } from 'y-protocols/awareness.js';

import { WorkspaceDBService } from '../../db';
import { getAFFiNEWorkspaceSchema } from '../global-schema';
import { WorkspaceImpl } from '../impl/workspace';
import type { WorkspaceScope } from '../scopes/workspace';
import { WorkspaceEngineService } from '../services/engine';

Expand All @@ -25,16 +22,13 @@ export class Workspace extends Entity {

readonly flavour = this.meta.flavour;

_docCollection: BSWorkspace | null = null;
_docCollection: WorkspaceInterface | null = null;

get docCollection() {
if (!this._docCollection) {
this._docCollection = new DocCollection({
this._docCollection = new WorkspaceImpl({
id: this.openOptions.metadata.id,
blobSources: {
main: this.engine.blob,
},
idGenerator: () => nanoid(),
blobSource: this.engine.blob,
schema: getAFFiNEWorkspaceSchema(),
});
this._docCollection.slots.docCreated.on(id => {
Expand Down
Loading

0 comments on commit 13e1893

Please sign in to comment.