feat(core): watching references from multiple document ids (#11047)

Close [BS-2464](https://linear.app/affine-design/issue/BS-2464).
This commit is contained in:
akumatus
2025-03-21 05:09:19 +00:00
parent 281951ecd1
commit 321dfaa881
@@ -2,7 +2,7 @@ import { toURLSearchParams } from '@affine/core/modules/navigation';
import type { ReferenceParams } from '@blocksuite/affine/model'; import type { ReferenceParams } from '@blocksuite/affine/model';
import { fromPromise, OnEvent, Service } from '@toeverything/infra'; import { fromPromise, OnEvent, Service } from '@toeverything/infra';
import { isEmpty, omit } from 'lodash-es'; import { isEmpty, omit } from 'lodash-es';
import { map, type Observable, switchMap } from 'rxjs'; import { map, type Observable, of, switchMap } from 'rxjs';
import { z } from 'zod'; import { z } from 'zod';
import type { WorkspaceService } from '../../workspace'; import type { WorkspaceService } from '../../workspace';
@@ -134,7 +134,12 @@ export class DocsSearchService extends Service {
); );
} }
watchRefsFrom(docId: string) { watchRefsFrom(ids: string | string[]) {
const docIds = Array.isArray(ids) ? ids : [ids];
if (docIds.length === 0) {
return of([]);
}
return this.indexer.blockIndex return this.indexer.blockIndex
.search$( .search$(
{ {
@@ -142,9 +147,13 @@ export class DocsSearchService extends Service {
occur: 'must', occur: 'must',
queries: [ queries: [
{ {
type: 'match', type: 'boolean',
field: 'docId', occur: 'should',
match: docId, queries: docIds.map(id => ({
type: 'match',
field: 'docId',
match: id,
})),
}, },
{ {
type: 'exists', type: 'exists',
@@ -171,7 +180,7 @@ export class DocsSearchService extends Service {
? [JSON.parse(ref)] ? [JSON.parse(ref)]
: ref.map(item => JSON.parse(item)); : ref.map(item => JSON.parse(item));
}) })
.filter(ref => ref.docId !== docId) .filter(ref => !docIds.includes(ref.docId))
.map(ref => [ref.docId, ref]) .map(ref => [ref.docId, ref])
).values() ).values()
); );