diff --git a/blocksuite/affine/blocks/database/src/properties/rich-text/define.ts b/blocksuite/affine/blocks/database/src/properties/rich-text/define.ts index 7df11a61d6..7621ece504 100644 --- a/blocksuite/affine/blocks/database/src/properties/rich-text/define.ts +++ b/blocksuite/affine/blocks/database/src/properties/rich-text/define.ts @@ -1,12 +1,9 @@ -import type { AffineTextAttributes } from '@blocksuite/affine-shared/types'; import { propertyType, t } from '@blocksuite/data-view'; -import type { DeltaInsert } from '@blocksuite/store'; import { Text } from '@blocksuite/store'; import * as Y from 'yjs'; import zod from 'zod'; -import { EditorHostKey } from '../../context/host-context.js'; -import { isLinkedDoc } from '../../utils/title-doc.js'; +import { getTextWithResolvedReferences } from '../../utils/title-doc.js'; export const richTextColumnType = propertyType('rich-text'); export type RichTextCellType = Text | Text['yText']; @@ -24,7 +21,7 @@ export const richTextPropertyModelConfig = richTextColumnType.modelConfig({ default: () => ({}), }, jsonValue: { - schema: zod.string(), + schema: zod.string().nullable(), type: () => t.richText.instance(), isEmpty: ({ value }) => !value, }, @@ -41,26 +38,8 @@ export const richTextPropertyModelConfig = richTextColumnType.modelConfig({ value: new Text(value), }; }, - toJson: ({ value, dataSource }) => { - if (!value) return null; - const host = dataSource.serviceGet(EditorHostKey); - if (host) { - const collection = host.std.workspace; - const yText = toYText(value); - const deltas = yText?.toDelta(); - const text = deltas - .map((delta: DeltaInsert) => { - if (isLinkedDoc(delta)) { - const linkedDocId = delta.attributes?.reference?.pageId as string; - return collection.getDoc(linkedDocId)?.meta?.title; - } - return delta.insert; - }) - .join(''); - return text; - } - return value?.toString() ?? null; - }, + toJson: ({ value, dataSource }) => + value ? getTextWithResolvedReferences(value, dataSource) : null, fromJson: ({ value }) => typeof value !== 'string' ? undefined : new Text(value), onUpdate: ({ value, callback }) => { diff --git a/blocksuite/affine/blocks/database/src/properties/title/define.ts b/blocksuite/affine/blocks/database/src/properties/title/define.ts index 400b5d4a19..251da6ffdc 100644 --- a/blocksuite/affine/blocks/database/src/properties/title/define.ts +++ b/blocksuite/affine/blocks/database/src/properties/title/define.ts @@ -3,8 +3,7 @@ import { Text } from '@blocksuite/store'; import { Doc } from 'yjs'; import zod from 'zod'; -import { EditorHostKey } from '../../context/host-context.js'; -import { isLinkedDoc } from '../../utils/title-doc.js'; +import { getTextWithResolvedReferences } from '../../utils/title-doc.js'; export const titleColumnType = propertyType('title'); @@ -22,29 +21,13 @@ export const titlePropertyModelConfig = titleColumnType.modelConfig({ rawValue: { schema: zod.custom(data => data instanceof Text).optional(), default: () => undefined, - toString: ({ value }) => value?.toString() ?? '', + toString: ({ value, dataSource }) => + getTextWithResolvedReferences(value, dataSource), fromString: ({ value }) => { return { value: new Text(value) }; }, - toJson: ({ value, dataSource }) => { - if (!value) return ''; - const host = dataSource.serviceGet(EditorHostKey); - if (host) { - const collection = host.std.workspace; - const deltas = value.deltas$.value; - const text = deltas - .map(delta => { - if (isLinkedDoc(delta)) { - const linkedDocId = delta.attributes?.reference?.pageId as string; - return collection.getDoc(linkedDocId)?.meta?.title; - } - return delta.insert; - }) - .join(''); - return text; - } - return value?.toString() ?? ''; - }, + toJson: ({ value, dataSource }) => + getTextWithResolvedReferences(value, dataSource), fromJson: ({ value }) => new Text(value), onUpdate: ({ value, callback }) => { value?.yText.observe(callback); diff --git a/blocksuite/affine/blocks/database/src/utils/title-doc.ts b/blocksuite/affine/blocks/database/src/utils/title-doc.ts index 4fff4e83eb..52d0bdae3a 100644 --- a/blocksuite/affine/blocks/database/src/utils/title-doc.ts +++ b/blocksuite/affine/blocks/database/src/utils/title-doc.ts @@ -1,5 +1,9 @@ +import { DocDisplayMetaProvider } from '@blocksuite/affine-shared/services'; import type { AffineTextAttributes } from '@blocksuite/affine-shared/types'; -import type { DeltaOperation, Text } from '@blocksuite/store'; +import type { DataSource } from '@blocksuite/data-view'; +import { type DeltaOperation, Text } from '@blocksuite/store'; + +import { EditorHostKey } from '../context/host-context.js'; export const getSingleDocIdFromText = (text?: Text) => { const deltas = text?.deltas$.value; @@ -28,3 +32,28 @@ export const isPureText = (text?: Text): boolean => { if (!deltas) return true; return deltas.every(v => !isLinkedDoc(v)); }; + +export const getTextWithResolvedReferences = ( + value: Text | Text['yText'] | undefined, + dataSource?: DataSource +): string => { + if (!value) return ''; + const host = dataSource?.serviceGet(EditorHostKey); + if (!host) return value.toString(); + const displayMeta = host.std.get(DocDisplayMetaProvider); + const deltas: DeltaOperation[] = + value instanceof Text ? value.deltas$.value : value.toDelta(); + return deltas + .map(delta => { + const reference: AffineTextAttributes['reference'] = + delta.attributes?.reference; + return reference?.type === 'LinkedPage' + ? displayMeta.title(reference.pageId, { + params: reference.params, + title: reference.title, + referenced: true, + }).value + : delta.insert; + }) + .join(''); +}; diff --git a/blocksuite/affine/data-view/src/core/property/types.ts b/blocksuite/affine/data-view/src/core/property/types.ts index 0f22bc2052..7564d1ce0f 100644 --- a/blocksuite/affine/data-view/src/core/property/types.ts +++ b/blocksuite/affine/data-view/src/core/property/types.ts @@ -27,7 +27,11 @@ export type PropertyConfig = { rawValue: { schema: ZodType; default: () => RawValue; - toString: (config: { value: RawValue; data: Data }) => string; + toString: (config: { + value: RawValue; + data: Data; + dataSource?: DataSource; + }) => string; fromString: ( config: WithCommonPropertyConfig<{ value: string; diff --git a/blocksuite/affine/data-view/src/core/view-manager/cell.ts b/blocksuite/affine/data-view/src/core/view-manager/cell.ts index bf1685eca4..84d876198c 100644 --- a/blocksuite/affine/data-view/src/core/view-manager/cell.ts +++ b/blocksuite/affine/data-view/src/core/view-manager/cell.ts @@ -84,6 +84,7 @@ export class CellBase< return toString({ value: this.value$.value, data: this.property.data$.value, + dataSource: this.dataSource, }); }); diff --git a/blocksuite/affine/shared/src/services/doc-display-meta-provider.ts b/blocksuite/affine/shared/src/services/doc-display-meta-provider.ts new file mode 100644 index 0000000000..e2b902f386 --- /dev/null +++ b/blocksuite/affine/shared/src/services/doc-display-meta-provider.ts @@ -0,0 +1,38 @@ +import type { AliasInfo, ReferenceParams } from '@blocksuite/affine-model'; +import { createIdentifier } from '@blocksuite/global/di'; +import type { ReadonlySignal } from '@preact/signals-core'; +import type { TemplateResult } from 'lit'; + +export type DocDisplayMetaParams = { + referenced?: boolean; + params?: ReferenceParams; +} & AliasInfo; + +/** + * Customize document display title and icon. + * + * Supports the following blocks: + * + * * Inline View: + * `AffineReference` + * * Card View: + * `EmbedLinkedDocBlockComponent` + * `EmbedEdgelessLinkedDocBlockComponent` + * * Embed View: + * `EmbedSyncedDocBlockComponent` + * `EmbedEdgelessSyncedDocBlockComponent` + */ +export interface DocDisplayMetaExtension { + icon: ( + docId: string, + referenceInfo?: DocDisplayMetaParams + ) => ReadonlySignal; + title: ( + docId: string, + referenceInfo?: DocDisplayMetaParams + ) => ReadonlySignal; +} + +export const DocDisplayMetaProvider = createIdentifier( + 'DocDisplayMetaService' +); diff --git a/blocksuite/affine/shared/src/services/doc-display-meta-service.ts b/blocksuite/affine/shared/src/services/doc-display-meta-service.ts index 9efbf13abf..5c9e35a8a4 100644 --- a/blocksuite/affine/shared/src/services/doc-display-meta-service.ts +++ b/blocksuite/affine/shared/src/services/doc-display-meta-service.ts @@ -1,5 +1,4 @@ -import type { AliasInfo, ReferenceParams } from '@blocksuite/affine-model'; -import { type Container, createIdentifier } from '@blocksuite/global/di'; +import type { Container } from '@blocksuite/global/di'; import { type DisposableMember, disposeMember, @@ -24,42 +23,13 @@ import { import type { TemplateResult } from 'lit'; import { referenceToNode } from '../utils/reference.js'; +import { + type DocDisplayMetaExtension, + type DocDisplayMetaParams, + DocDisplayMetaProvider, +} from './doc-display-meta-provider.js'; import { DocModeProvider } from './doc-mode-service.js'; -export type DocDisplayMetaParams = { - referenced?: boolean; - params?: ReferenceParams; -} & AliasInfo; - -/** - * Customize document display title and icon. - * - * Supports the following blocks: - * - * * Inline View: - * `AffineReference` - * * Card View: - * `EmbedLinkedDocBlockComponent` - * `EmbedEdgelessLinkedDocBlockComponent` - * * Embed View: - * `EmbedSyncedDocBlockComponent` - * `EmbedEdgelessSyncedDocBlockComponent` - */ -export interface DocDisplayMetaExtension { - icon: ( - docId: string, - referenceInfo?: DocDisplayMetaParams - ) => ReadonlySignal; - title: ( - docId: string, - referenceInfo?: DocDisplayMetaParams - ) => ReadonlySignal; -} - -export const DocDisplayMetaProvider = createIdentifier( - 'DocDisplayMetaService' -); - export class DocDisplayMetaService extends LifeCycleWatcher implements DocDisplayMetaExtension diff --git a/blocksuite/affine/shared/src/services/index.ts b/blocksuite/affine/shared/src/services/index.ts index c8f7f6ab0c..f5b1cf991c 100644 --- a/blocksuite/affine/shared/src/services/index.ts +++ b/blocksuite/affine/shared/src/services/index.ts @@ -2,6 +2,7 @@ export * from './auto-clear-selection-service'; export * from './block-meta-service'; export * from './citation-service'; export * from './comment-service'; +export * from './doc-display-meta-provider'; export * from './doc-display-meta-service'; export * from './doc-mode-service'; export * from './drag-handle-config'; diff --git a/blocksuite/integration-test/src/__tests__/edgeless/mindmap.spec.ts b/blocksuite/integration-test/src/__tests__/edgeless/mindmap.spec.ts index 5938d328f3..5317f6c1a4 100644 --- a/blocksuite/integration-test/src/__tests__/edgeless/mindmap.spec.ts +++ b/blocksuite/integration-test/src/__tests__/edgeless/mindmap.spec.ts @@ -7,7 +7,7 @@ import { } from '@blocksuite/affine-model'; import { Bound, deserializeXYWH } from '@blocksuite/global/gfx'; import type { GfxController } from '@blocksuite/std/gfx'; -import { beforeEach, describe, expect, test, vi } from 'vitest'; +import { beforeEach, describe, expect, onTestFinished, test, vi } from 'vitest'; import { click, pointermove, wait } from '../utils/common.js'; import { getDocRootBlock } from '../utils/edgeless.js'; @@ -15,30 +15,44 @@ import { setupEditor } from '../utils/setup.js'; describe('mindmap', () => { let gfx: GfxController; - const moveAndClick = ({ x, y, w, h }: Bound) => { - const { left, top } = gfx.viewport; - x += left; - y += top; - - // trigger enter event - pointermove(editor.host!, { x: x + w / 2, y: y + h / 2 }); - // trigger move event - pointermove(editor.host!, { x: x + w / 2 + 1, y: y + h / 2 + 1 }); - click(editor.host!, { x: x + w / 2, y: y + h / 2 }); + const centerInHost = ({ x, y, w, h }: Bound) => { + const { left, top } = editor.host!.getBoundingClientRect(); + return { + x: x + w / 2 + gfx.viewport.left - left, + y: y + h / 2 + gfx.viewport.top - top, + }; }; - const move = ({ x, y, w, h }: Bound) => { - x += gfx.viewport.left; - y += gfx.viewport.top; + const moveAndClick = (bound: Bound) => { + const point = centerInHost(bound); + pointermove(editor.host!, point); + pointermove(editor.host!, { x: point.x + 1, y: point.y + 1 }); + click(editor.host!, point); + }; - pointermove(editor.host!, { x: x + w / 2, y: y + h / 2 }); + const move = (bound: Bound) => { + pointermove(editor.host!, centerInHost(bound)); }; beforeEach(async () => { - const cleanup = await setupEditor('edgeless'); + const cleanup = await setupEditor('edgeless', [], { + editorStyle: { + display: 'block', + marginLeft: '48px', + marginTop: '32px', + }, + }); + onTestFinished(cleanup); gfx = getDocRootBlock(window.doc, window.editor, 'edgeless').gfx; - - return cleanup; + const hostRect = editor.host!.getBoundingClientRect(); + const appRect = editor.parentElement!.getBoundingClientRect(); + expect([gfx.viewport.left, gfx.viewport.top]).toEqual([ + hostRect.left, + hostRect.top, + ]); + expect([hostRect.left - appRect.left, hostRect.top - appRect.top]).toEqual([ + 48, 32, + ]); }); test('should update mindmap node size during IME composition', async () => { diff --git a/blocksuite/integration-test/src/__tests__/main/editor-semantics.spec.ts b/blocksuite/integration-test/src/__tests__/main/editor-semantics.spec.ts index 6f7eda4425..47f28954c3 100644 --- a/blocksuite/integration-test/src/__tests__/main/editor-semantics.spec.ts +++ b/blocksuite/integration-test/src/__tests__/main/editor-semantics.spec.ts @@ -1,6 +1,7 @@ import { DatabaseBlockDataSource, databaseBlockProperties, + EditorHostKey, } from '@blocksuite/affine-block-database'; import { LinkExtension } from '@blocksuite/affine-inline-link'; import { textKeymap } from '@blocksuite/affine-inline-preset'; @@ -13,6 +14,7 @@ import type { } from '@blocksuite/affine-model'; import { insertContent } from '@blocksuite/affine-rich-text'; import { REFERENCE_NODE } from '@blocksuite/affine-shared/consts'; +import type { AffineTextAttributes } from '@blocksuite/affine-shared/types'; import { createDefaultDoc } from '@blocksuite/affine-shared/utils'; import type { SingleViewBase } from '@blocksuite/data-view'; import { SurfaceSelection, TextSelection } from '@blocksuite/std'; @@ -241,16 +243,27 @@ test.each( property => !['title', 'created-time'].includes(property.type) ) )( - 'database isolates malformed $type cells without changing persisted values', + 'database searches plain and linked titles despite malformed $type cells', async property => { const noteId = addNote(doc); const id = doc.addBlock('affine:database', {}, noteId); const model = doc.getModelById(id) as DatabaseBlockModel; - const source = new DatabaseBlockDataSource(model); + const source = new DatabaseBlockDataSource(model, source => { + source.serviceSet(EditorHostKey, editor.host!); + }); source.viewManager.viewAdd('table'); const propertyId = source.propertyAdd('end', { type: property.type })!; const malformedRow = source.rowAdd('end'); const normalRow = source.rowAdd('end'); + const linkedDoc = createDefaultDoc(collection, { title: 'Planning notes' }); + const linkedRow = source.rowAdd('end'); + const linkedText = new Text([ + { + insert: ' ', + attributes: { reference: { type: 'LinkedPage', pageId: linkedDoc.id } }, + }, + ]); + doc.updateBlock(doc.getModelById(linkedRow)!, { text: linkedText }); await wait(); const database = editor.host!.view.getBlock(id)!; await database.updateComplete; @@ -280,6 +293,44 @@ test.each( const view = source.viewManager.currentView$.value as SingleViewBase; view.setSearch('needle'); expect(view.rows$.value.map(row => row.rowId)).toEqual([normalRow]); + view.setSearch('Planning'); + expect(view.rows$.value.map(row => row.rowId)).toEqual([linkedRow]); + collection.meta.setDocMeta(linkedDoc.id, { title: 'Updated agenda' }); + expect(view.rows$.value.map(row => row.rowId)).toEqual([]); + view.setSearch('Updated'); + expect(view.rows$.value.map(row => row.rowId)).toEqual([linkedRow]); + expect(linkedText.toString()).toBe(' '); + const titleCell = view.cellGetOrCreate( + linkedRow, + view.mainProperties$.value.titleColumn! + ); + expect(titleCell.stringValue$.value).toBe('Updated agenda'); + expect(titleCell.jsonValue$.value).toBe('Updated agenda'); + linkedText.format(0, 1, { + reference: { + type: 'LinkedPage', + pageId: linkedDoc.id, + title: 'Roadmap alias', + }, + }); + view.setSearch('Roadmap'); + expect(view.rows$.value.map(row => row.rowId)).toEqual([linkedRow]); + collection.meta.setDocMeta(linkedDoc.id, { title: 'Renamed again' }); + expect(titleCell.stringValue$.value).toBe('Roadmap alias'); + linkedText.insert('Topic: ', 0); + view.setSearch('Topic: Roadmap'); + expect(view.rows$.value.map(row => row.rowId)).toEqual([linkedRow]); + linkedText.format(7, 1, { + reference: { type: 'LinkedPage', pageId: 'missing-document' }, + }); + view.setSearch('Topic: Deleted doc'); + expect(view.rows$.value.map(row => row.rowId)).toEqual([linkedRow]); + expect( + databaseBlockProperties.titleColumnConfig.config.rawValue.toString({ + value: linkedText, + data: {}, + }) + ).toBe('Topic: '); await wait(); expect(model.props.cells[malformedRow][propertyId].value).toEqual( invalidValue diff --git a/blocksuite/integration-test/src/__tests__/utils/setup.ts b/blocksuite/integration-test/src/__tests__/utils/setup.ts index 157b7ebc68..a1cebbc0b4 100644 --- a/blocksuite/integration-test/src/__tests__/utils/setup.ts +++ b/blocksuite/integration-test/src/__tests__/utils/setup.ts @@ -67,7 +67,8 @@ function initCollection(collection: TestWorkspace) { async function createEditor( collection: TestWorkspace, mode: DocMode = 'page', - extensions: ExtensionType[] = [] + extensions: ExtensionType[] = [], + editorStyle?: Partial ) { const app = document.createElement('div'); const blockCollection = collection.docs.values().next().value; @@ -88,6 +89,7 @@ async function createEditor( FontConfigExtension(CommunityCanvasTextFonts), ...extensions, ]; + Object.assign(editor.style, editorStyle); app.append(editor); window.editor = editor; @@ -116,6 +118,7 @@ export function createPainterWorker() { type SetupEditorOptions = { extensions?: ExtensionType[]; enableDomRenderer?: boolean; + editorStyle?: Partial; }; export async function setupEditor( @@ -143,7 +146,12 @@ export async function setupEditor( featureFlagService?.setFlag('enable_dom_renderer', true); } - const appElement = await createEditor(collection, mode, extensions); + const appElement = await createEditor( + collection, + mode, + extensions, + options.editorStyle + ); return async () => { await cleanup(); diff --git a/packages/common/nbstore/src/storage/indexer.ts b/packages/common/nbstore/src/storage/indexer.ts index 0ac80e3326..95e276bc7a 100644 --- a/packages/common/nbstore/src/storage/indexer.ts +++ b/packages/common/nbstore/src/storage/indexer.ts @@ -83,7 +83,7 @@ type HighlightAbleField = { }[keyof IndexerSchema[T]]; export type SearchOptions = { - pagination?: PaginationOption; + pagination?: PaginationOption & { cursor?: string }; highlights?: { field: HighlightAbleField; before: string; end: string }[]; fields?: (keyof IndexerSchema[T])[]; }; @@ -92,7 +92,7 @@ export type SearchResult< T extends keyof IndexerSchema, O extends SearchOptions, > = { - pagination: ResultPagination; + pagination: ResultPagination & { nextCursor?: string | null }; nodes: ({ id: string; score: number } & (O['fields'] extends any[] ? { fields: { [key in O['fields'][number]]: string | string[] } } : unknown) & diff --git a/packages/frontend/core/src/modules/docs-search/services/docs-search.spec.ts b/packages/frontend/core/src/modules/docs-search/services/docs-search.spec.ts index fbe615621b..4c809c4f52 100644 --- a/packages/frontend/core/src/modules/docs-search/services/docs-search.spec.ts +++ b/packages/frontend/core/src/modules/docs-search/services/docs-search.spec.ts @@ -1,12 +1,16 @@ /** @vitest-environment happy-dom */ +import 'fake-indexeddb/auto'; + +import { IndexerDocument, type IndexerStorage } from '@affine/nbstore'; +import { IndexedDBIndexerStorage } from '@affine/nbstore/idb'; import { Framework } from '@toeverything/infra'; -import { firstValueFrom, of } from 'rxjs'; +import { firstValueFrom, map, of, throwError } from 'rxjs'; import { describe, expect, it, vi } from 'vitest'; import { DocsSearchService } from './docs-search'; -describe('DocsSearchService refs', () => { +describe('DocsSearchService', () => { it('skips malformed reference items without poisoning the result', async () => { const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); const indexer = { @@ -127,4 +131,209 @@ describe('DocsSearchService refs', () => { pagination: { limit: Infinity }, }); }); + + it.each(['offset', 'cursor'] as const)( + 'finds linked database rows by current visible titles with %s pagination', + async pagination => { + const indexer: IndexerStorage = new IndexedDBIndexerStorage({ + flavour: 'test', + type: 'workspace', + id: `linked-row-search-${pagination}`, + }); + indexer.connection.connect(); + await indexer.connection.waitForConnected(); + const titles = new Map([ + ['database', 'Database'], + ['plain', 'Plain document'], + ['hidden-target', 'Planning private'], + ]); + let failure: 'titles' | 'page' | 'rows' | 'ordinary' | undefined; + const lookupError = new Error('index lookup failed'); + const framework = new Framework(); + framework.service( + DocsSearchService, + () => + new DocsSearchService( + { + workspace: { + engine: { + indexer: { + state$: of({ indexing: 0, errorMessage: null }), + search$: ( + ...args: Parameters + ) => + failure === 'titles' + ? throwError(() => lookupError) + : indexer.search$(...args).pipe( + map(page => ({ + ...page, + pagination: { + ...page.pagination, + nextCursor: + pagination === 'cursor' && + page.pagination.hasMore + ? `titles:${page.nodes.length}` + : undefined, + }, + })) + ), + search: async ( + ...[table, query, options]: Parameters< + IndexerStorage['search'] + > + ) => { + if (failure === 'page') throw lookupError; + const pageOptions = options?.pagination; + if (pagination === 'cursor') { + expect(pageOptions?.skip).toBeUndefined(); + expect(pageOptions?.cursor).toBe('titles:50'); + } + return await indexer.search(table, query, { + ...options, + pagination: { + ...pageOptions, + skip: + pagination === 'cursor' + ? Number( + pageOptions?.cursor?.slice('titles:'.length) + ) + : pageOptions?.skip, + }, + }); + }, + aggregate$: ( + ...args: Parameters + ) => + failure === 'ordinary' + ? throwError(() => lookupError) + : indexer.aggregate$(...args), + aggregate: ( + ...args: Parameters + ) => + failure === 'rows' + ? Promise.reject(lookupError) + : indexer.aggregate(...args), + }, + }, + }, + } as never, + { + list: { + doc$: (id: string) => ({ + value: titles.has(id) + ? { id, title$: { value: titles.get(id) } } + : null, + }), + }, + } as never + ) + ); + try { + for (let i = 0; i < 51; i++) { + await indexer.insert( + 'doc', + IndexerDocument.from<'doc'>( + `physical-title-${String(i).padStart(2, '0')}`, + { + docId: `target-${i}`, + title: `Planning notes ${i}`, + } + ) + ); + } + for (const [id, docId, content, refDocId, parentFlavour] of [ + ['row', 'database', ' ', 'target-50', 'affine:database'], + ['private-row', 'database', ' ', 'hidden-target', 'affine:database'], + ['paragraph', 'plain', 'Planning ordinary text', '', 'affine:note'], + ]) { + await indexer.insert( + 'block', + IndexerDocument.from<'block'>(id, { + docId, + blockId: id, + content, + refDocId, + flavour: 'affine:paragraph', + parentFlavour, + }) + ); + } + await indexer.refresh('doc'); + await indexer.refresh('block'); + const service = framework.provider().get(DocsSearchService); + const results = await firstValueFrom( + service.search$('Planning', 'local') + ); + expect(results.map(result => result.docId)).toEqual([ + 'plain', + 'database', + ]); + expect(results[1]).toMatchObject({ + blockId: 'row', + blockContent: 'Planning notes 50', + }); + for (failure of ['titles', 'page', 'rows'] as const) { + expect( + await firstValueFrom(service.search$('Planning', 'local')) + ).toEqual([results[0]]); + } + failure = 'ordinary'; + await expect( + firstValueFrom(service.search$('Planning', 'local')) + ).rejects.toBe(lookupError); + + failure = 'page'; + const liveResults: { docId: string }[][] = []; + const subscription = service + .search$('Planning', 'local') + .subscribe(result => liveResults.push(result)); + try { + await vi.waitFor(() => expect(liveResults).toEqual([[results[0]]])); + failure = undefined; + await indexer.update( + 'doc', + IndexerDocument.from<'doc'>('physical-title-0', { + docId: 'target-0', + title: 'Planning refreshed', + }) + ); + await indexer.refresh('doc'); + await vi.waitFor( + () => + expect(liveResults.at(-1)?.map(result => result.docId)).toEqual([ + 'plain', + 'database', + ]), + { timeout: 4000 } + ); + } finally { + subscription.unsubscribe(); + } + await indexer.update( + 'doc', + IndexerDocument.from<'doc'>('physical-title-50', { + docId: 'target-50', + title: 'Execution notes', + }) + ); + await indexer.refresh('doc'); + expect( + (await firstValueFrom(service.search$('Planning', 'local'))).map( + result => result.docId + ) + ).toEqual(['plain']); + expect( + await firstValueFrom(service.search$('Execution', 'local')) + ).toMatchObject([ + { + docId: 'database', + blockId: 'row', + blockContent: 'Execution notes', + }, + ]); + } finally { + indexer.connection.disconnect(); + } + } + ); }); diff --git a/packages/frontend/core/src/modules/docs-search/services/docs-search.ts b/packages/frontend/core/src/modules/docs-search/services/docs-search.ts index c4a62fb367..22a13d4f11 100644 --- a/packages/frontend/core/src/modules/docs-search/services/docs-search.ts +++ b/packages/frontend/core/src/modules/docs-search/services/docs-search.ts @@ -18,6 +18,11 @@ import { z } from 'zod'; import { normalizeSearchText } from '../../../utils/normalize-search-text'; import type { DocsService } from '../../doc/services/docs'; import type { WorkspaceService } from '../../workspace'; +import { + DOC_SEARCH_LIMIT, + type DocSearchResult, + searchLinkedRows$, +} from './linked-row-search'; const IndexedReferenceSchema = ReferenceParamsSchema.extend({ docId: z.string().min(1), @@ -117,15 +122,7 @@ export class DocsSearchService extends Service { search$( query: string, prefer: IndexerPreferOptions = 'remote' - ): Observable< - { - docId: string; - title: string; - score: number; - blockId?: string; - blockContent?: string; - }[] - > { + ): Observable { return this.indexer .aggregate$( 'block', @@ -163,7 +160,7 @@ export class DocsSearchService extends Service { 'docId', { pagination: { - limit: 50, + limit: DOC_SEARCH_LIMIT, skip: 0, }, hits: { @@ -224,7 +221,16 @@ export class DocsSearchService extends Service { } return result; - }) + }), + switchMap(results => + searchLinkedRows$( + this.indexer, + query, + prefer, + results, + docId => this.docsService.list.doc$(docId).value?.title$.value ?? '' + ) + ) ); } diff --git a/packages/frontend/core/src/modules/docs-search/services/linked-row-search.ts b/packages/frontend/core/src/modules/docs-search/services/linked-row-search.ts new file mode 100644 index 0000000000..d566bb1fd1 --- /dev/null +++ b/packages/frontend/core/src/modules/docs-search/services/linked-row-search.ts @@ -0,0 +1,111 @@ +import type { IndexerPreferOptions, SearchOptions } from '@affine/nbstore'; +import type { IndexerFrontend } from '@affine/nbstore/frontend'; +import { fromPromise } from '@toeverything/infra'; +import { escape } from 'lodash-es'; +import { catchError, of, switchMap } from 'rxjs'; + +export type DocSearchResult = { + docId: string; + title: string; + score: number; + blockId?: string; + blockContent?: string; +}; + +export const DOC_SEARCH_LIMIT = 50; + +const fieldString = (field: string | string[]) => + typeof field === 'string' ? field : field[0]; + +export function searchLinkedRows$( + indexer: IndexerFrontend, + query: string, + prefer: IndexerPreferOptions, + results: DocSearchResult[], + getTitle: (docId: string) => string +) { + if (results.length >= DOC_SEARCH_LIMIT) return of(results); + const titleQuery = { type: 'match', field: 'title', match: query } as const; + const titleOptions = { + fields: ['docId', 'title'], + pagination: { skip: 0, limit: DOC_SEARCH_LIMIT }, + prefer, + } satisfies SearchOptions<'doc'> & { prefer: IndexerPreferOptions }; + return indexer.search$('doc', titleQuery, titleOptions).pipe( + switchMap(firstPage => + fromPromise(async signal => { + const merged = new Map(results.map(result => [result.docId, result])); + let page = firstPage; + let skip = 0; + while (page.nodes.length) { + signal.throwIfAborted(); + const titles = new Map( + page.nodes.map(node => [ + fieldString(node.fields.docId), + fieldString(node.fields.title), + ]) + ); + const { buckets } = await indexer.aggregate( + 'block', + { + type: 'boolean', + occur: 'must', + queries: [ + { + type: 'match', + field: 'parentFlavour', + match: 'affine:database', + }, + { + type: 'boolean', + occur: 'should', + queries: [...titles.keys()].map(docId => ({ + type: 'match', + field: 'refDocId', + match: docId, + })), + }, + ], + }, + 'docId', + { + pagination: { skip: 0, limit: DOC_SEARCH_LIMIT }, + hits: { + pagination: { limit: 1 }, + fields: ['blockId', 'refDocId'], + }, + prefer, + } + ); + for (const bucket of buckets) { + if (merged.has(bucket.key)) continue; + const hit = bucket.hits.nodes[0]; + const refs = hit.fields.refDocId; + const target = (typeof refs === 'string' ? [refs] : refs).find( + docId => titles.has(docId) + ); + merged.set(bucket.key, { + docId: bucket.key, + title: getTitle(bucket.key), + score: bucket.score, + blockId: fieldString(hit.fields.blockId), + blockContent: escape(target ? titles.get(target) : ''), + }); + if (merged.size >= DOC_SEARCH_LIMIT) return [...merged.values()]; + } + if (!page.pagination.hasMore) break; + signal.throwIfAborted(); + skip += page.nodes.length; + page = await indexer.search('doc', titleQuery, { + ...titleOptions, + pagination: page.pagination.nextCursor + ? { cursor: page.pagination.nextCursor, limit: DOC_SEARCH_LIMIT } + : { skip, limit: DOC_SEARCH_LIMIT }, + }); + } + return [...merged.values()]; + }).pipe(catchError(() => of(results))) + ), + catchError(() => of(results)) + ); +} diff --git a/tests/blocksuite/e2e/paragraph.spec.ts b/tests/blocksuite/e2e/paragraph.spec.ts index 83eed46f05..da0f081119 100644 --- a/tests/blocksuite/e2e/paragraph.spec.ts +++ b/tests/blocksuite/e2e/paragraph.spec.ts @@ -1318,13 +1318,15 @@ test('select divider using delete keyboard from prev/next paragraph', async ({ await assertDivider(page, 3); await assertRichTexts(page, ['123', '123']); - await focusRichText(page, 0); + await focusRichTextEnd(page, 0); + await assertRichTextInlineRange(page, 0, 3); await pressForwardDelete(page); await assertBlockSelections(page, ['4']); await assertDivider(page, 3); - await focusRichText(page, 1); + await focusRichTextEnd(page, 1); await pressArrowLeft(page, 3); + await assertRichTextInlineRange(page, 1, 0); await pressBackspace(page); await assertBlockSelections(page, ['6']); await assertDivider(page, 3);