feat(core): init organize (#7456)

This commit is contained in:
EYHN
2024-07-26 04:35:31 +00:00
parent b26b0c3a22
commit 54da85ec62
140 changed files with 6257 additions and 2804 deletions

View File

@@ -0,0 +1,10 @@
import type { Framework } from '../../framework';
import { WorkspaceScope, WorkspaceService } from '../workspace';
import { DBService } from './services/db';
export { AFFiNE_DB_SCHEMA } from './schema';
export { DBService } from './services/db';
export function configureDBModule(framework: Framework) {
framework.scope(WorkspaceScope).service(DBService, [WorkspaceService]);
}

View File

@@ -0,0 +1 @@
export { AFFiNE_DB_SCHEMA } from './schema';

View File

@@ -0,0 +1,14 @@
import { nanoid } from 'nanoid';
import { type DBSchemaBuilder, f } from '../../../orm';
export const AFFiNE_DB_SCHEMA = {
folders: {
id: f.string().primaryKey().optional().default(nanoid),
parentId: f.string().optional(),
data: f.string(),
type: f.string(),
index: f.string(),
},
} as const satisfies DBSchemaBuilder;
export type AFFiNE_DB_SCHEMA = typeof AFFiNE_DB_SCHEMA;

View File

@@ -0,0 +1,32 @@
import { Doc as YDoc } from 'yjs';
import { Service } from '../../../framework';
import { createORMClient, type TableMap, YjsDBAdapter } from '../../../orm';
import type { WorkspaceService } from '../../workspace';
import { AFFiNE_DB_SCHEMA } from '../schema';
export class DBService extends Service {
db: TableMap<AFFiNE_DB_SCHEMA>;
constructor(private readonly workspaceService: WorkspaceService) {
super();
const Client = createORMClient(AFFiNE_DB_SCHEMA);
this.db = new Client(
new YjsDBAdapter(AFFiNE_DB_SCHEMA, {
getDoc: guid => {
const ydoc = new YDoc({
// guid format: db${workspaceId}${guid}
guid: `db$${this.workspaceService.workspace.id}$${guid}`,
});
this.workspaceService.workspace.engine.doc.addDoc(ydoc, false);
this.workspaceService.workspace.engine.doc.setPriority(ydoc.guid, 50);
return ydoc;
},
})
);
}
static isDBDocId(docId: string) {
return docId.startsWith('db$');
}
}

View File

@@ -1,9 +1,13 @@
import { Entity } from '../../../framework';
import type { DocScope } from '../scopes/doc';
import type { DocsStore } from '../stores/docs';
import type { DocMode } from './record';
export class Doc extends Entity {
constructor(public readonly scope: DocScope) {
constructor(
public readonly scope: DocScope,
private readonly store: DocsStore
) {
super();
}
@@ -42,4 +46,12 @@ export class Doc extends Entity {
restoreFromTrash() {
return this.record.restoreFromTrash();
}
waitForSyncReady() {
return this.store.waitForDocLoadReady(this.id);
}
setPriorityLoad(priority: number) {
return this.store.setPriorityLoad(this.id, priority);
}
}

View File

@@ -28,6 +28,6 @@ export function configureDocModule(framework: Framework) {
.entity(DocRecord, [DocsStore])
.entity(DocRecordList, [DocsStore])
.scope(DocScope)
.entity(Doc, [DocScope])
.entity(Doc, [DocScope, DocsStore])
.service(DocService);
}

View File

@@ -1,4 +1,5 @@
import { Unreachable } from '@affine/env/constant';
import type { RootBlockModel } from '@blocksuite/blocks';
import { Service } from '../../../framework';
import { initEmptyPage } from '../../../initialization';
@@ -59,6 +60,7 @@ export class DocsService extends Service {
) {
const doc = this.store.createBlockSuiteDoc();
initEmptyPage(doc, options.title);
this.store.markDocSyncStateAsReady(doc.id);
const docRecord = this.list.doc$(doc.id).value;
if (!docRecord) {
throw new Unreachable();
@@ -68,4 +70,45 @@ export class DocsService extends Service {
}
return docRecord;
}
async addLinkedDoc(targetDocId: string, linkedDocId: string) {
const { doc, release } = this.open(targetDocId);
doc.setPriorityLoad(10);
await doc.waitForSyncReady();
const text = doc.blockSuiteDoc.Text.fromDelta([
{
insert: ' ',
attributes: {
reference: {
type: 'LinkedPage',
pageId: linkedDocId,
},
},
},
]);
const [frame] = doc.blockSuiteDoc.getBlocksByFlavour('affine:note');
frame &&
doc.blockSuiteDoc.addBlock(
'affine:paragraph' as never, // TODO(eyhn): fix type
{ text },
frame.id
);
release();
}
async changeDocTitle(docId: string, newTitle: string) {
const { doc, release } = this.open(docId);
doc.setPriorityLoad(10);
await doc.waitForSyncReady();
const pageBlock = doc.blockSuiteDoc.getBlocksByFlavour('affine:page').at(0)
?.model as RootBlockModel | undefined;
if (pageBlock) {
doc.blockSuiteDoc.transact(() => {
pageBlock.title.delete(0, pageBlock.title.length);
pageBlock.title.insert(newTitle, 0);
});
doc.record.setMeta({ title: newTitle });
}
release();
}
}

View File

@@ -112,4 +112,16 @@ export class DocsStore extends Store {
watchDocModeSetting(id: string) {
return this.localState.watch<DocMode>(`page:${id}:mode`);
}
waitForDocLoadReady(id: string) {
return this.workspaceService.workspace.engine.doc.waitForReady(id);
}
setPriorityLoad(id: string, priority: number) {
return this.workspaceService.workspace.engine.doc.setPriority(id, priority);
}
markDocSyncStateAsReady(id: string) {
this.workspaceService.workspace.engine.doc.markAsReady(id);
}
}

View File

@@ -8,10 +8,19 @@ export class GlobalContext extends Entity {
workspaceId = this.define<string>('workspaceId');
isDoc = this.define<boolean>('isDoc');
docId = this.define<string>('docId');
isCollection = this.define<boolean>('isCollection');
collectionId = this.define<string>('collectionId');
isTrash = this.define<boolean>('isTrash');
docMode = this.define<DocMode>('docMode');
isTag = this.define<boolean>('isTag');
tagId = this.define<string>('tagId');
define<T>(key: string) {
this.memento.set(key, null);
const livedata$ = LiveData.from(this.memento.watch<T>(key), null);