fix(core): cmdk crash when entering double quotes (#5008)

Due to a bug in the upstream repository, a temporary fix was implemented until the issue in the upstream repository is resolved.
https://github.com/pacocoursey/cmdk/issues/189
This commit is contained in:
JimmFly 2023-11-21 12:51:22 +00:00
parent 00c11d40cf
commit f06bdd9a39
No known key found for this signature in database
GPG Key ID: 14A6F56854E1BED7
3 changed files with 25 additions and 15 deletions

View File

@ -24,7 +24,7 @@ import {
PreconditionStrategy,
} from '@toeverything/infra/command';
import { atom, useAtomValue } from 'jotai';
import { escape, groupBy } from 'lodash-es';
import { groupBy } from 'lodash-es';
import { useCallback, useMemo } from 'react';
import {
@ -44,6 +44,10 @@ interface SearchResultsValue {
content: string;
}
export function removeDoubleQuotes(str?: string): string | undefined {
return str?.replace(/"/g, '');
}
export const cmdkQueryAtom = atom('');
export const cmdkValueAtom = atom('');
@ -168,7 +172,6 @@ export const pageToCommand = (
const commandLabel = label || {
title: title,
};
const escapedTitle = escape(title);
return {
id: page.id,
@ -176,13 +179,8 @@ export const pageToCommand = (
// hack: when comparing, the part between >>> and <<< will be ignored
// adding this patch so that CMDK will not complain about duplicated commands
value:
escapedTitle +
valueWrapperStart +
page.id +
'.' +
category +
valueWrapperEnd,
originalValue: escapedTitle,
title + valueWrapperStart + page.id + '.' + category + valueWrapperEnd,
originalValue: title,
category: category,
run: () => {
if (!currentWorkspaceId) {
@ -429,7 +427,10 @@ export const customCommandFilter = (value: string, search: string) => {
label = label.replace(contentMatchedWithoutSubtitle, '');
}
const originalScore = commandScore(label, search);
// use to remove double quotes from a string until this issue is fixed
// https://github.com/pacocoursey/cmdk/issues/189
const escapedSearch = removeDoubleQuotes(search) || '';
const originalScore = commandScore(label, escapedSearch);
// hack to make the page title result always before the content result
// if the command has matched the title but not the subtitle,

View File

@ -12,6 +12,7 @@ import {
cmdkQueryAtom,
cmdkValueAtom,
customCommandFilter,
removeDoubleQuotes,
useCMDKCommandGroups,
} from './data';
import { HighlightLabel } from './highlight';
@ -78,11 +79,15 @@ const QuickSearchGroup = ({
title: command.label,
}
: command.label;
// use to remove double quotes from a string until this issue is fixed
// https://github.com/pacocoursey/cmdk/issues/189
const escapeValue = removeDoubleQuotes(command.value);
return (
<Command.Item
key={command.id}
onSelect={() => onCommendSelect(command)}
value={command.value}
value={escapeValue}
data-is-danger={
command.id === 'editor:page-move-to-trash' ||
command.id === 'editor:edgeless-move-to-trash'

View File

@ -82,7 +82,9 @@ async function assertResultList(page: Page, texts: string[]) {
.allInnerTexts();
const actualSplit = actual[0].split('\n');
expect(actualSplit[0]).toEqual(texts[0]);
expect(actualSplit[1]).toEqual(texts[1]);
if (actualSplit[1]) {
expect(actualSplit[1]).toEqual(texts[1]);
}
}
async function titleIsFocused(page: Page) {
@ -135,11 +137,13 @@ test('Create a new page with keyword', async ({ page }) => {
await waitForEditorLoad(page);
await clickNewPageButton(page);
await openQuickSearchByShortcut(page);
await page.keyboard.insertText('test123456');
const addNewPage = page.locator('[cmdk-item] >> text=New "test123456" Page');
await page.keyboard.insertText('"test123456"');
const addNewPage = page.locator(
'[cmdk-item] >> text=New ""test123456"" Page'
);
await addNewPage.click();
await page.waitForTimeout(300);
await assertTitle(page, 'test123456');
await assertTitle(page, '"test123456"');
});
test('Enter a keyword to search for', async ({ page }) => {