From f06bdd9a3999c4665a9952553da47e872cad8ac3 Mon Sep 17 00:00:00 2001 From: JimmFly <447268514@qq.com> Date: Tue, 21 Nov 2023 12:51:22 +0000 Subject: [PATCH] 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 --- .../core/src/components/pure/cmdk/data.tsx | 21 ++++++++++--------- .../core/src/components/pure/cmdk/main.tsx | 7 ++++++- tests/affine-local/e2e/quick-search.spec.ts | 12 +++++++---- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/packages/frontend/core/src/components/pure/cmdk/data.tsx b/packages/frontend/core/src/components/pure/cmdk/data.tsx index 143e74cc52..0be2d3c3df 100644 --- a/packages/frontend/core/src/components/pure/cmdk/data.tsx +++ b/packages/frontend/core/src/components/pure/cmdk/data.tsx @@ -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, diff --git a/packages/frontend/core/src/components/pure/cmdk/main.tsx b/packages/frontend/core/src/components/pure/cmdk/main.tsx index 1ec2a91773..4252265658 100644 --- a/packages/frontend/core/src/components/pure/cmdk/main.tsx +++ b/packages/frontend/core/src/components/pure/cmdk/main.tsx @@ -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 ( onCommendSelect(command)} - value={command.value} + value={escapeValue} data-is-danger={ command.id === 'editor:page-move-to-trash' || command.id === 'editor:edgeless-move-to-trash' diff --git a/tests/affine-local/e2e/quick-search.spec.ts b/tests/affine-local/e2e/quick-search.spec.ts index 236cb93d53..cb5ceb836b 100644 --- a/tests/affine-local/e2e/quick-search.spec.ts +++ b/tests/affine-local/e2e/quick-search.spec.ts @@ -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 }) => {