fundon
2024-09-12 08:25:28 +00:00
parent 2cba8a4ccd
commit 8a9d9b42a3
5 changed files with 191 additions and 68 deletions
@@ -10,7 +10,6 @@ import { truncate } from 'lodash-es';
import { EMPTY, map, mergeMap, of, switchMap } from 'rxjs';
import type { DocsSearchService } from '../../docs-search';
import { resolveLinkToDoc } from '../../navigation';
import type { QuickSearchSession } from '../providers/quick-search-provider';
import type { DocDisplayMetaService } from '../services/doc-display-meta';
import type { QuickSearchItem } from '../types/item';
@@ -55,29 +54,7 @@ export class DocsQuickSearchSession
if (!query) {
out = of([] as QuickSearchItem<'docs', DocsPayload>[]);
} else {
const resolvedDoc = resolveLinkToDoc(query);
const resolvedDocId = resolvedDoc?.docId;
const resolvedBlockId = resolvedDoc?.blockIds?.[0];
out = this.docsSearchService.search$(query).pipe(
map(docs => {
if (
resolvedDocId &&
!docs.some(doc => doc.docId === resolvedDocId)
) {
return [
{
docId: resolvedDocId,
score: 100,
blockId: resolvedBlockId,
blockContent: '',
},
...docs,
];
}
return docs;
}),
map(docs =>
docs
.map(doc => {
@@ -0,0 +1,107 @@
import type { ReferenceParams } from '@blocksuite/blocks';
import { BlockLinkIcon, LinkIcon } from '@blocksuite/icons/rc';
import type { DocsService } from '@toeverything/infra';
import { Entity, LiveData } from '@toeverything/infra';
import { isEmpty, pick, truncate } from 'lodash-es';
import { resolveLinkToDoc } from '../../navigation';
import type { QuickSearchSession } from '../providers/quick-search-provider';
import type { DocDisplayMetaService } from '../services/doc-display-meta';
import type { QuickSearchItem } from '../types/item';
type LinkPayload = {
internal?: {
docId: string;
title?: string;
blockId?: string;
blockContent?: string;
params?: ReferenceParams;
};
external?: {
url: string;
};
};
export class LinksQuickSearchSession
extends Entity
implements QuickSearchSession<'link', LinkPayload>
{
constructor(
private readonly docsService: DocsService,
private readonly docDisplayMetaService: DocDisplayMetaService
) {
super();
}
query$ = new LiveData('');
items$ = LiveData.computed(get => {
const query = get(this.query$);
if (!query) return [];
const isLink = query.startsWith('http://') || query.startsWith('https://');
if (!isLink) return [];
const resolvedDoc = resolveLinkToDoc(query);
if (!resolvedDoc) {
return [
{
id: 'link',
source: 'link',
icon: LinkIcon,
label: {
key: 'com.affine.cmdk.affine.insert-link',
},
payload: { external: { url: query } },
} as QuickSearchItem<'link', LinkPayload>,
];
}
const docId = resolvedDoc.docId;
const doc = this.docsService.list.doc$(docId).value;
if (!doc || get(doc.trash$)) return [];
const params = pick(resolvedDoc, ['mode', 'blockIds', 'elementIds']);
const { title, icon, updatedDate } =
this.docDisplayMetaService.getDocDisplayMeta(doc);
const blockId = params?.blockIds?.[0];
const linkToNode = Boolean(blockId);
const score = 100;
const internal = {
docId,
score,
blockId,
blockContent: '',
};
if (linkToNode && !isEmpty(params)) {
Object.assign(internal, { params });
}
return [
{
id: ['doc', doc.id, linkToNode ? blockId : ''].join(':'),
source: 'link',
group: {
id: 'docs',
label: {
key: 'com.affine.quicksearch.group.searchfor',
options: { query: truncate(query) },
},
score: 5,
},
label: {
title: title,
},
score,
icon: linkToNode ? BlockLinkIcon : icon,
timestamp: updatedDate,
payload: { internal },
} as QuickSearchItem<'link', LinkPayload>,
];
});
query(query: string) {
this.query$.next(query);
}
}
@@ -16,6 +16,7 @@ import { CollectionsQuickSearchSession } from './impls/collections';
import { CommandsQuickSearchSession } from './impls/commands';
import { CreationQuickSearchSession } from './impls/creation';
import { DocsQuickSearchSession } from './impls/docs';
import { LinksQuickSearchSession } from './impls/links';
import { RecentDocsQuickSearchSession } from './impls/recent-docs';
import { TagsQuickSearchSession } from './impls/tags';
import { CMDKQuickSearchService } from './services/cmdk';
@@ -29,6 +30,7 @@ export { CollectionsQuickSearchSession } from './impls/collections';
export { CommandsQuickSearchSession } from './impls/commands';
export { CreationQuickSearchSession } from './impls/creation';
export { DocsQuickSearchSession } from './impls/docs';
export { LinksQuickSearchSession } from './impls/links';
export { RecentDocsQuickSearchSession } from './impls/recent-docs';
export { TagsQuickSearchSession } from './impls/tags';
export type { QuickSearchItem } from './types/item';
@@ -53,6 +55,7 @@ export function configureQuickSearchModule(framework: Framework) {
DocsService,
DocDisplayMetaService,
])
.entity(LinksQuickSearchSession, [DocsService, DocDisplayMetaService])
.entity(CreationQuickSearchSession)
.entity(CollectionsQuickSearchSession, [CollectionService])
.entity(TagsQuickSearchSession, [TagService])
@@ -7,6 +7,7 @@ import { CollectionsQuickSearchSession } from '../impls/collections';
import { CommandsQuickSearchSession } from '../impls/commands';
import { CreationQuickSearchSession } from '../impls/creation';
import { DocsQuickSearchSession } from '../impls/docs';
import { LinksQuickSearchSession } from '../impls/links';
import { RecentDocsQuickSearchSession } from '../impls/recent-docs';
import { TagsQuickSearchSession } from '../impls/tags';
import type { QuickSearchService } from './quick-search';
@@ -31,20 +32,33 @@ export class CMDKQuickSearchService extends Service {
this.framework.createEntity(CommandsQuickSearchSession),
this.framework.createEntity(CreationQuickSearchSession),
this.framework.createEntity(DocsQuickSearchSession),
this.framework.createEntity(LinksQuickSearchSession),
this.framework.createEntity(TagsQuickSearchSession),
],
result => {
if (!result) {
return;
}
if (result.source === 'commands') {
result.payload.run()?.catch(err => {
console.error(err);
});
} else if (
result.source === 'recent-doc' ||
result.source === 'docs'
) {
return;
}
if (result.source === 'link') {
if (result.payload.internal) {
const { docId, params } = result.payload.internal;
this.workbenchService.workbench.openDoc({
docId,
...params,
});
}
return;
}
if (result.source === 'recent-doc' || result.source === 'docs') {
const doc: {
docId: string;
blockId?: string;
@@ -62,13 +76,22 @@ export class CMDKQuickSearchService extends Service {
}
this.workbenchService.workbench.openDoc(options);
} else if (result.source === 'collections') {
return;
}
if (result.source === 'collections') {
this.workbenchService.workbench.openCollection(
result.payload.collectionId
);
} else if (result.source === 'tags') {
return;
}
if (result.source === 'tags') {
this.workbenchService.workbench.openTag(result.payload.tagId);
} else if (result.source === 'creation') {
return;
}
if (result.source === 'creation') {
if (result.id === 'creation:create-page') {
const newDoc = this.docsService.createDoc({
primaryMode: 'page',
@@ -82,6 +105,7 @@ export class CMDKQuickSearchService extends Service {
});
this.workbenchService.workbench.openDoc(newDoc.id);
}
return;
}
},
{