feat(core): adjust filter logic (#4831)

This commit is contained in:
JimmFly
2023-11-06 14:41:18 +08:00
committed by GitHub
parent 9664d142ad
commit 2b34e1a9cd

View File

@@ -198,6 +198,8 @@ export const pageToCommand = (
}; };
const contentMatchedMagicString = '__$$content_matched$$__'; const contentMatchedMagicString = '__$$content_matched$$__';
const contentMatchedWithoutSubtitle =
'__$$content_matched_without_subtitle$$__';
export const usePageCommands = () => { export const usePageCommands = () => {
const recentPages = useRecentPages(); const recentPages = useRecentPages();
@@ -241,11 +243,11 @@ export const usePageCommands = () => {
const category = const category =
pageMode === 'edgeless' ? 'affine:edgeless' : 'affine:pages'; pageMode === 'edgeless' ? 'affine:edgeless' : 'affine:pages';
const subTitle = resultValues.find(result => result.space === page.id)
?.content;
const label = { const label = {
title: page.title || t['Untitled'](), // Used to ensure that a title exists title: page.title || t['Untitled'](), // Used to ensure that a title exists
subTitle: subTitle: subTitle || '',
resultValues.find(result => result.space === page.id)?.content ||
'',
}; };
const blockId = reverseMapping.get(page.id); const blockId = reverseMapping.get(page.id);
@@ -264,6 +266,10 @@ export const usePageCommands = () => {
// hack to make the page always showing in the search result // hack to make the page always showing in the search result
command.value += contentMatchedMagicString; command.value += contentMatchedMagicString;
} }
if (!subTitle) {
// hack to make the page title result always before the content result
command.value += contentMatchedWithoutSubtitle;
}
return command; return command;
}); });
@@ -392,8 +398,8 @@ export const useCMDKCommandGroups = () => {
return useMemo(() => { return useMemo(() => {
const commands = [ const commands = [
...pageCommands,
...collectionCommands, ...collectionCommands,
...pageCommands,
...affineCommands, ...affineCommands,
]; ];
const groups = groupBy(commands, command => command.category); const groups = groupBy(commands, command => command.category);
@@ -412,14 +418,25 @@ export const customCommandFilter = (value: string, search: string) => {
if (pageContentMatched) { if (pageContentMatched) {
label = label.replace(contentMatchedMagicString, ''); label = label.replace(contentMatchedMagicString, '');
} }
const pageTitleMatched = label.includes(contentMatchedWithoutSubtitle);
if (pageTitleMatched) {
label = label.replace(contentMatchedWithoutSubtitle, '');
}
const originalScore = commandScore(label, search); const originalScore = commandScore(label, search);
// hack to make the page title result always before the content result
// if the command has matched the title but not the subtitle,
// we should give it a higher score
if (originalScore > 0 && pageTitleMatched) {
return 0.999;
}
// if the command has matched the content but not the label, // if the command has matched the content but not the label,
// we should give it a higher score, but not too high // we should give it a higher score, but not too high
if (originalScore < 0.01 && pageContentMatched) { if (originalScore < 0.01 && pageContentMatched) {
return 0.3; return 0.3;
} }
return originalScore; return originalScore;
}; };