refactor(core): new quick search service (#7214)

This commit is contained in:
EYHN
2024-07-02 09:17:53 +00:00
parent 15e99c7819
commit 40e381e272
63 changed files with 1809 additions and 2007 deletions
@@ -4,7 +4,6 @@ import {
useLitPortalFactory,
} from '@affine/component';
import { useJournalInfoHelper } from '@affine/core/hooks/use-journal';
import { QuickSearchService } from '@affine/core/modules/cmdk';
import { PeekViewService } from '@affine/core/modules/peek-view';
import { WorkbenchService } from '@affine/core/modules/workbench';
import {
@@ -19,6 +18,7 @@ import {
type DocMode,
DocService,
DocsService,
useFramework,
useLiveData,
useService,
} from '@toeverything/infra';
@@ -79,9 +79,9 @@ interface BlocksuiteEditorProps {
const usePatchSpecs = (page: Doc, shared: boolean, mode: DocMode) => {
const [reactToLit, portals] = useLitPortalFactory();
const peekViewService = useService(PeekViewService);
const quickSearchService = useService(QuickSearchService);
const docService = useService(DocService);
const docsService = useService(DocsService);
const framework = useFramework();
const referenceRenderer: ReferenceReactRenderer = useMemo(() => {
return function customReference(reference) {
const pageId = reference.delta.attributes?.reference?.pageId;
@@ -105,7 +105,7 @@ const usePatchSpecs = (page: Doc, shared: boolean, mode: DocMode) => {
patched = patchPeekViewService(patched, peekViewService);
}
if (!page.readonly) {
patched = patchQuickSearchService(patched, quickSearchService);
patched = patchQuickSearchService(patched, framework);
}
if (shared) {
patched = patchForSharedPage(patched);
@@ -116,9 +116,9 @@ const usePatchSpecs = (page: Doc, shared: boolean, mode: DocMode) => {
confirmModal,
docService,
docsService,
framework,
page.readonly,
peekViewService,
quickSearchService,
reactToLit,
referenceRenderer,
shared,
@@ -7,12 +7,17 @@ import {
toReactNode,
type useConfirmModal,
} from '@affine/component';
import type {
QuickSearchService,
SearchCallbackResult,
} from '@affine/core/modules/cmdk';
import { DocsSearchService } from '@affine/core/modules/docs-search';
import { resolveLinkToDoc } from '@affine/core/modules/navigation';
import type { PeekViewService } from '@affine/core/modules/peek-view';
import type { ActivePeekView } from '@affine/core/modules/peek-view/entities/peek-view';
import {
CreationQuickSearchSession,
DocsQuickSearchSession,
type QuickSearchItem,
QuickSearchService,
RecentDocsQuickSearchSession,
} from '@affine/core/modules/quicksearch';
import { mixpanel } from '@affine/core/utils';
import { DebugLogger } from '@affine/debug';
import type { BlockSpec, WidgetElement } from '@blocksuite/block-std';
@@ -23,7 +28,13 @@ import {
type ParagraphBlockService,
type RootService,
} from '@blocksuite/blocks';
import type { DocMode, DocService, DocsService } from '@toeverything/infra';
import { LinkIcon } from '@blocksuite/icons/rc';
import type {
DocMode,
DocService,
DocsService,
FrameworkProvider,
} from '@toeverything/infra';
import { type TemplateResult } from 'lit';
import { customElement } from 'lit/decorators.js';
import { literal } from 'lit/static-html.js';
@@ -302,7 +313,7 @@ export function patchDocModeService(
export function patchQuickSearchService(
specs: BlockSpec[],
service: QuickSearchService
framework: FrameworkProvider
) {
const rootSpec = specs.find(
spec => spec.schema.model.flavour === 'affine:page'
@@ -317,39 +328,93 @@ export function patchQuickSearchService(
pageService => {
pageService.quickSearchService = {
async searchDoc(options) {
let searchResult: SearchCallbackResult | null = null;
let searchResult:
| { docId: string; isNewDoc?: boolean }
| { userInput: string }
| null = null;
if (options.skipSelection) {
const query = options.userInput;
if (!query) {
logger.error('No user input provided');
} else {
const searchedDoc = service.quickSearch
.getSearchedDocs(query)
.at(0);
const searchedDoc = (
await framework.get(DocsSearchService).search(query)
).at(0);
if (searchedDoc) {
searchResult = {
docId: searchedDoc.doc.id,
blockId: searchedDoc.blockId,
action: 'insert',
query,
docId: searchedDoc.docId,
};
}
}
} else {
searchResult = await service.quickSearch.search(options.userInput);
searchResult = await new Promise(resolve =>
framework.get(QuickSearchService).quickSearch.show(
[
framework.get(RecentDocsQuickSearchSession),
framework.get(DocsQuickSearchSession),
framework.get(CreationQuickSearchSession),
(query: string) => {
if (
(query.startsWith('http://') ||
query.startsWith('https://')) &&
resolveLinkToDoc(query) === null
) {
return [
{
id: 'link',
source: 'link',
icon: LinkIcon,
label: {
key: 'com.affine.cmdk.affine.insert-link',
},
payload: { url: query },
} as QuickSearchItem<'link', { url: string }>,
];
}
return [];
},
],
result => {
if (result === null) {
resolve(null);
return;
}
if (
result.source === 'docs' ||
result.source === 'recent-doc'
) {
resolve({
docId: result.payload.docId,
});
} else if (result.source === 'link') {
resolve({
userInput: result.payload.url,
});
} else if (
result.source === 'creation' &&
result.id === 'creation:create-page'
) {
throw new Error('Not implemented');
// resolve({
// docId: 'new-doc',
// isNewDoc: true,
// });
}
},
{
defaultQuery: options.userInput,
label: {
key: 'com.affine.cmdk.insert-links',
},
placeholder: {
key: 'com.affine.cmdk.docs.placeholder',
},
}
)
);
}
if (searchResult) {
if ('docId' in searchResult) {
return searchResult;
} else {
return {
userInput: searchResult.query,
action: 'insert',
};
}
}
return null;
return searchResult;
},
};
},
@@ -81,7 +81,7 @@ export const PageOperationCell = ({
}, [page.id, page.title, setTrashModal]);
const onOpenInSplitView = useCallback(() => {
workbench.openPage(page.id, { at: 'tail' });
workbench.openDoc(page.id, { at: 'tail' });
}, [page.id, workbench]);
const onToggleFavoritePage = useCallback(() => {
@@ -69,7 +69,7 @@ export const OperationMenuButton = ({ ...props }: OperationMenuButtonProps) => {
}, [pageId, removeFromAllowList]);
const handleOpenInSplitView = useCallback(() => {
workbench.openPage(pageId, { at: 'tail' });
workbench.openDoc(pageId, { at: 'tail' });
}, [pageId, workbench]);
return (