Skip to content

Commit

Permalink
fix(store): workspace search (toeverything#3201)
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed Jun 24, 2023
1 parent 10e3e0b commit 751f717
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
20 changes: 20 additions & 0 deletions packages/store/src/__tests__/workspace.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
`);
});
});
24 changes: 13 additions & 11 deletions packages/store/src/workspace/indexer/search.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -112,7 +111,10 @@ export class SearchIndexer {
if (!page) {
return;
}
const yBlocks = page.get('blocks') as YMap<YBlock>;
const yBlocks = page.get('blocks');
if (!(yBlocks instanceof YMap<YBlock>)) {
return;
}
yBlocks.forEach((_, key) => {
this._refreshIndex(pageId, key, 'add', yBlocks.get(key));
});
Expand Down

0 comments on commit 751f717

Please sign in to comment.