mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-11-20 21:23:37 +03:00
fix(editor): resolve doc title with references (#15865)
fix #15850 #### PR Dependency Tree * **PR #15865** 👈 This tree was auto-generated by [Charcoal](https://github.com/danerwilliams/charcoal) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * Search results now include database rows that reference matching documents, showing the referenced document’s current title. * Linked-document titles appear consistently in database title and rich-text values, including after a title changes. * Search includes ordinary text matches alongside linked-document references and supports retrieving additional results. * **Bug Fixes** * Rows referencing unavailable documents are not presented as linked-title matches. * If linked-row lookup fails, ordinary document search results remain available. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
1 parent
0d594345a3
commit
dc2b221244
16 files changed
+530
-124
No files matched your search
@@ -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<AffineTextAttributes>) => {
|
||||
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 }) => {
|
||||
|
||||
@@ -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<Text>(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);
|
||||
|
||||
@@ -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('');
|
||||
};
|
||||
@@ -27,7 +27,11 @@ export type PropertyConfig<Data, RawValue = unknown, JsonValue = unknown> = {
|
||||
rawValue: {
|
||||
schema: ZodType<RawValue>;
|
||||
default: () => RawValue;
|
||||
toString: (config: { value: RawValue; data: Data }) => string;
|
||||
toString: (config: {
|
||||
value: RawValue;
|
||||
data: Data;
|
||||
dataSource?: DataSource;
|
||||
}) => string;
|
||||
fromString: (
|
||||
config: WithCommonPropertyConfig<{
|
||||
value: string;
|
||||
|
||||
@@ -84,6 +84,7 @@ export class CellBase<
|
||||
return toString({
|
||||
value: this.value$.value,
|
||||
data: this.property.data$.value,
|
||||
dataSource: this.dataSource,
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -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<TemplateResult>;
|
||||
title: (
|
||||
docId: string,
|
||||
referenceInfo?: DocDisplayMetaParams
|
||||
) => ReadonlySignal<string>;
|
||||
}
|
||||
|
||||
export const DocDisplayMetaProvider = createIdentifier<DocDisplayMetaExtension>(
|
||||
'DocDisplayMetaService'
|
||||
);
|
||||
@@ -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<TemplateResult>;
|
||||
title: (
|
||||
docId: string,
|
||||
referenceInfo?: DocDisplayMetaParams
|
||||
) => ReadonlySignal<string>;
|
||||
}
|
||||
|
||||
export const DocDisplayMetaProvider = createIdentifier<DocDisplayMetaExtension>(
|
||||
'DocDisplayMetaService'
|
||||
);
|
||||
|
||||
export class DocDisplayMetaService
|
||||
extends LifeCycleWatcher
|
||||
implements DocDisplayMetaExtension
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -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<AffineTextAttributes>([
|
||||
{
|
||||
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
|
||||
|
||||
@@ -67,7 +67,8 @@ function initCollection(collection: TestWorkspace) {
|
||||
async function createEditor(
|
||||
collection: TestWorkspace,
|
||||
mode: DocMode = 'page',
|
||||
extensions: ExtensionType[] = []
|
||||
extensions: ExtensionType[] = [],
|
||||
editorStyle?: Partial<CSSStyleDeclaration>
|
||||
) {
|
||||
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<CSSStyleDeclaration>;
|
||||
};
|
||||
|
||||
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();
|
||||
|
||||
@@ -83,7 +83,7 @@ type HighlightAbleField<T extends keyof IndexerSchema> = {
|
||||
}[keyof IndexerSchema[T]];
|
||||
|
||||
export type SearchOptions<T extends keyof IndexerSchema> = {
|
||||
pagination?: PaginationOption;
|
||||
pagination?: PaginationOption & { cursor?: string };
|
||||
highlights?: { field: HighlightAbleField<T>; before: string; end: string }[];
|
||||
fields?: (keyof IndexerSchema[T])[];
|
||||
};
|
||||
@@ -92,7 +92,7 @@ export type SearchResult<
|
||||
T extends keyof IndexerSchema,
|
||||
O extends SearchOptions<T>,
|
||||
> = {
|
||||
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) &
|
||||
|
||||
@@ -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<IndexerStorage['search$']>
|
||||
) =>
|
||||
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<IndexerStorage['aggregate$']>
|
||||
) =>
|
||||
failure === 'ordinary'
|
||||
? throwError(() => lookupError)
|
||||
: indexer.aggregate$(...args),
|
||||
aggregate: (
|
||||
...args: Parameters<IndexerStorage['aggregate']>
|
||||
) =>
|
||||
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();
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
@@ -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<DocSearchResult[]> {
|
||||
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 ?? ''
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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))
|
||||
);
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user