From f2906bc6d079d98e7b3ab300c73acf39865f9e67 Mon Sep 17 00:00:00 2001 From: EYHN Date: Thu, 2 Jan 2025 10:35:46 +0000 Subject: [PATCH] refactor(core): add all docs properties api (#9484) --- .../frontend/core/src/modules/doc/index.ts | 2 +- .../core/src/modules/doc/services/docs.ts | 30 +++++++++++++- .../src/modules/doc/stores/doc-properties.ts | 39 +++++++++++++++++++ .../core/src/modules/doc/stores/docs.ts | 18 +++++++++ 4 files changed, 86 insertions(+), 3 deletions(-) diff --git a/packages/frontend/core/src/modules/doc/index.ts b/packages/frontend/core/src/modules/doc/index.ts index 40c418b6cb..27405acca0 100644 --- a/packages/frontend/core/src/modules/doc/index.ts +++ b/packages/frontend/core/src/modules/doc/index.ts @@ -23,7 +23,7 @@ import { DocsStore } from './stores/docs'; export function configureDocModule(framework: Framework) { framework .scope(WorkspaceScope) - .service(DocsService, [DocsStore]) + .service(DocsService, [DocsStore, DocPropertiesStore]) .store(DocPropertiesStore, [WorkspaceService, WorkspaceDBService]) .store(DocsStore, [WorkspaceService, DocPropertiesStore]) .entity(DocRecord, [DocsStore, DocPropertiesStore]) diff --git a/packages/frontend/core/src/modules/doc/services/docs.ts b/packages/frontend/core/src/modules/doc/services/docs.ts index ed10a53ac4..2e4087c365 100644 --- a/packages/frontend/core/src/modules/doc/services/docs.ts +++ b/packages/frontend/core/src/modules/doc/services/docs.ts @@ -3,17 +3,21 @@ import { Unreachable } from '@affine/env/constant'; import type { DocMode } from '@blocksuite/affine/blocks'; import type { DeltaInsert } from '@blocksuite/affine/inline'; import type { AffineTextAttributes } from '@blocksuite/affine-shared/types'; -import { ObjectPool, Service } from '@toeverything/infra'; +import { LiveData, ObjectPool, Service } from '@toeverything/infra'; +import { omitBy } from 'lodash-es'; +import { combineLatest, map } from 'rxjs'; import { type DocProps, initDocFromProps, } from '../../../blocksuite/initialization'; +import type { DocProperties } from '../../db'; import type { Doc } from '../entities/doc'; import { DocPropertyList } from '../entities/property-list'; import { DocRecordList } from '../entities/record-list'; import { DocCreated } from '../events'; import { DocScope } from '../scopes/doc'; +import type { DocPropertiesStore } from '../stores/doc-properties'; import type { DocsStore } from '../stores/docs'; import { DocService } from './doc'; @@ -30,7 +34,29 @@ export class DocsService extends Service { propertyList = this.framework.createEntity(DocPropertyList); - constructor(private readonly store: DocsStore) { + /** + * used for search doc by properties, for convenience of search, all non-exist doc or trash doc have been filtered + */ + allDocProperties$: LiveData> = LiveData.from( + combineLatest([ + this.docPropertiesStore.watchAllDocProperties(), + this.store.watchNonTrashDocIds(), + ]).pipe( + map(([properties, docIds]) => { + const allIds = new Set(docIds); + return omitBy( + properties as Record, + (_, id) => !allIds.has(id) + ); + }) + ), + {} + ); + + constructor( + private readonly store: DocsStore, + private readonly docPropertiesStore: DocPropertiesStore + ) { super(); } diff --git a/packages/frontend/core/src/modules/doc/stores/doc-properties.ts b/packages/frontend/core/src/modules/doc/stores/doc-properties.ts index b94e66a992..9237f36e50 100644 --- a/packages/frontend/core/src/modules/doc/stores/doc-properties.ts +++ b/packages/frontend/core/src/modules/doc/stores/doc-properties.ts @@ -137,6 +137,28 @@ export class DocPropertiesStore extends Store { }; } + watchAllDocProperties() { + const allDocProperties$ = this.dbService.db.docProperties.find$(); + const allLegacyDocProperties$ = this.watchAllLegacyDocProperties(); + + return combineLatest([allDocProperties$, allLegacyDocProperties$]).pipe( + map(([db, legacy]) => { + const map = new Map(db.map(i => [i.id, i])); + const allIds = new Set([...map.keys(), ...Object.keys(legacy ?? {})]); + + const result = {} as Record>; + + for (const id of allIds) { + result[id] = { + ...this.upgradeLegacyDocProperties(legacy?.[id]), + ...omitBy(map.get(id), isNil), + }; + } + return result; + }) + ); + } + watchDocProperties(id: string) { return combineLatest([ this.watchLegacyDocProperties(id).pipe( @@ -203,6 +225,23 @@ export class DocPropertiesStore extends Store { ?.toJSON() as LegacyDocProperties | undefined; } + private watchAllLegacyDocProperties() { + return yjsObserveByPath( + this.workspaceService.workspace.rootYDoc.getMap( + 'affine:workspace-properties' + ), + `pageProperties` + ).pipe( + switchMap(yjsObserveDeep), + map( + p => + (p instanceof YAbstractType ? p.toJSON() : p) as + | { [docId: string]: LegacyDocProperties } + | undefined + ) + ); + } + private watchLegacyDocProperties(id: string) { return yjsObserveByPath( this.workspaceService.workspace.rootYDoc.getMap( diff --git a/packages/frontend/core/src/modules/doc/stores/docs.ts b/packages/frontend/core/src/modules/doc/stores/docs.ts index 0fd6c1f781..8e0cbe9690 100644 --- a/packages/frontend/core/src/modules/doc/stores/docs.ts +++ b/packages/frontend/core/src/modules/doc/stores/docs.ts @@ -44,6 +44,24 @@ export class DocsStore extends Store { ); } + watchNonTrashDocIds() { + return yjsObserveByPath( + this.workspaceService.workspace.rootYDoc.getMap('meta'), + 'pages' + ).pipe( + switchMap(yjsObserveDeep), + map(meta => { + if (meta instanceof YArray) { + return meta + .map(v => (v.get('trash') ? null : v.get('id'))) + .filter(Boolean) as string[]; + } else { + return []; + } + }) + ); + } + watchTrashDocIds() { return yjsObserveByPath( this.workspaceService.workspace.rootYDoc.getMap('meta'),