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
3 changed files with 25 additions and 15 deletions

View File

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

View File

@@ -12,6 +12,7 @@ import {
cmdkQueryAtom, cmdkQueryAtom,
cmdkValueAtom, cmdkValueAtom,
customCommandFilter, customCommandFilter,
removeDoubleQuotes,
useCMDKCommandGroups, useCMDKCommandGroups,
} from './data'; } from './data';
import { HighlightLabel } from './highlight'; import { HighlightLabel } from './highlight';
@@ -78,11 +79,15 @@ const QuickSearchGroup = ({
title: command.label, title: command.label,
} }
: 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 ( return (
<Command.Item <Command.Item
key={command.id} key={command.id}
onSelect={() => onCommendSelect(command)} onSelect={() => onCommendSelect(command)}
value={command.value} value={escapeValue}
data-is-danger={ data-is-danger={
command.id === 'editor:page-move-to-trash' || command.id === 'editor:page-move-to-trash' ||
command.id === 'editor:edgeless-move-to-trash' command.id === 'editor:edgeless-move-to-trash'

View File

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