From 751f7170c7ed910303ba4febaeee1cd64aab2613 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Sun, 25 Jun 2023 00:32:41 +0800 Subject: [PATCH] fix(store): workspace search (#3201) --- .../src/__tests__/workspace.unit.spec.ts | 20 ++++++++++++++++ .../store/src/workspace/indexer/search.ts | 24 ++++++++++--------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/packages/store/src/__tests__/workspace.unit.spec.ts b/packages/store/src/__tests__/workspace.unit.spec.ts index 61001e1aae74..9e2a5841152a 100644 --- a/packages/store/src/__tests__/workspace.unit.spec.ts +++ b/packages/store/src/__tests__/workspace.unit.spec.ts @@ -583,3 +583,23 @@ describe('workspace.exportJSX works', () => { `); }); }); + +describe('workspace search', () => { + it('search page meta title', async () => { + const options = createTestOptions(); + const workspace = new Workspace(options).register(BlockSchemas); + const page = workspace.createPage({ id: 'page0' }); + await page.waitForLoaded(); + const pageId = page.addBlock('affine:page', { + title: new page.Text('test123'), + }); + const noteId = page.addBlock('affine:note', {}, pageId); + page.addBlock('affine:paragraph', {}, noteId); + const result = workspace.search('test'); + expect(result).toMatchInlineSnapshot(` + Map { + "0" => "space:page0", + } + `); + }); +}); diff --git a/packages/store/src/workspace/indexer/search.ts b/packages/store/src/workspace/indexer/search.ts index 0720d1894b11..0fd47dad831e 100644 --- a/packages/store/src/workspace/indexer/search.ts +++ b/packages/store/src/workspace/indexer/search.ts @@ -1,7 +1,7 @@ import type { DocumentSearchOptions } from 'flexsearch'; import FlexSearch from 'flexsearch'; -import type { Doc, Map as YMap } from 'yjs'; -import { Text as YText } from 'yjs'; +import type { Doc } from 'yjs'; +import { Map as YMap, Text as YText } from 'yjs'; import type { BlockSuiteDoc } from '../../yjs/index.js'; import type { YBlock } from '../page.js'; @@ -77,14 +77,13 @@ export class SearchIndexer { context: true, }); - Array.from(doc.spaces.keys()) - .map(k => [k, this._getPage(k)] as const) - .forEach(([pageId, page]) => this._handlePageIndexing(pageId, page)); - } - - // TODO: remove this method, observe page meta instead - onPageCreated(pageId: string) { - this._handlePageIndexing(pageId, this._getPage(pageId)); + // fixme(Mirone): use better way to listen to page changes + doc.spaces.observe(event => { + event.keysChanged.forEach(pageId => { + const page = this._getPage(pageId); + this._handlePageIndexing(pageId, page); + }); + }); } search(query: QueryContent) { @@ -112,7 +111,10 @@ export class SearchIndexer { if (!page) { return; } - const yBlocks = page.get('blocks') as YMap; + const yBlocks = page.get('blocks'); + if (!(yBlocks instanceof YMap)) { + return; + } yBlocks.forEach((_, key) => { this._refreshIndex(pageId, key, 'add', yBlocks.get(key)); });