Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix: prevent note bold shortcut from toggling the sidebar (#42078)

Joelbce802c61f91
diff --git a/web/app/components/detail-sidebar/__tests__/index.spec.tsx b/web/app/components/detail-sidebar/__tests__/index.spec.tsx
index d333b2f5b61..34cc33f9bcc 100644
--- a/web/app/components/detail-sidebar/__tests__/index.spec.tsx
+++ b/web/app/components/detail-sidebar/__tests__/index.spec.tsx
@@ -1,31 +1,9 @@
-import { act, fireEvent, screen } from '@testing-library/react'
+import { HotkeysProvider } from '@tanstack/react-hotkeys'
+import { fireEvent, screen } from '@testing-library/react'
import { renderWithConsoleQuery } from '@/test/console/query-data'
import { DetailSidebarFrame } from '..'
import { DETAIL_SIDEBAR_STORAGE_KEY } from '../storage'
-const { hotkeyRegistrations } = vi.hoisted(() => ({
- hotkeyRegistrations: new Map<
- string,
- {
- handler: () => void
- options?: { ignoreInputs?: boolean; preventDefault?: boolean }
- }
- >(),
-}))
-vi.mock('@tanstack/react-hotkeys', async (importOriginal) => {
- const actual = await importOriginal<typeof import('@tanstack/react-hotkeys')>()
- return {
- ...actual,
- useHotkey: (
- hotkey: string,
- handler: () => void,
- options?: { ignoreInputs?: boolean; preventDefault?: boolean },
- ) => {
- hotkeyRegistrations.set(hotkey, { handler, options })
- },
- }
-})
-
vi.mock('@/app/components/main-nav/components/account-section', () => ({
default: ({ compact }: { compact?: boolean }) => (
<button type="button" aria-label="account">
@@ -46,22 +24,32 @@ vi.mock('@/app/components/header/env-nav', () => ({
default: () => <div>Environment tag</div>,
}))
-function renderDetailSidebarFrame(currentEnv: string | null = null) {
+function renderDetailSidebarFrame(
+ currentEnv: string | null = null,
+ platform: 'mac' | 'windows' = 'mac',
+) {
return renderWithConsoleQuery(
- <DetailSidebarFrame
- renderTop={({ expand, onToggle }) => (
- <div data-testid="detail-top" data-expand={expand}>
- <button type="button" data-testid="detail-toggle" onClick={onToggle}>
- Toggle
- </button>
- </div>
- )}
- renderSection={({ expand }) => (
- <div data-testid="detail-section" data-expand={expand}>
- Section
- </div>
- )}
- />,
+ <HotkeysProvider defaultOptions={{ hotkey: { platform } }}>
+ <DetailSidebarFrame
+ renderTop={({ expand, onToggle }) => (
+ <div data-testid="detail-top" data-expand={expand}>
+ <button type="button" data-testid="detail-toggle" onClick={onToggle}>
+ Toggle
+ </button>
+ </div>
+ )}
+ renderSection={({ expand }) => (
+ <div data-testid="detail-section" data-expand={expand}>
+ Section
+ </div>
+ )}
+ />
+ <div contentEditable suppressContentEditableWarning role="textbox" aria-label="Note">
+ <span>Note text</span>
+ </div>
+ <input aria-label="Name" />
+ <textarea aria-label="Description" />
+ </HotkeysProvider>,
{ accountProfileMeta: { currentEnv } },
)
}
@@ -69,26 +57,53 @@ function renderDetailSidebarFrame(currentEnv: string | null = null) {
describe('DetailSidebarFrame', () => {
beforeEach(() => {
localStorage.clear()
- hotkeyRegistrations.clear()
})
- it('renders expanded detail content by default and registers the shortcut for focused inputs', () => {
+ it('renders expanded detail content by default', () => {
renderDetailSidebarFrame()
expect(screen.getByTestId('detail-top')).toHaveAttribute('data-expand', 'true')
expect(screen.getByTestId('detail-section')).toHaveAttribute('data-expand', 'true')
- expect(hotkeyRegistrations.get('Mod+B')?.options).toEqual(
- expect.objectContaining({ ignoreInputs: false, preventDefault: true }),
- )
})
- it('toggles detail content from the registered shortcut', () => {
- renderDetailSidebarFrame()
+ describe.each(['mac', 'windows'] as const)('%s shortcut', (platform) => {
+ const modifiers = platform === 'mac' ? { metaKey: true } : { ctrlKey: true }
- act(() => hotkeyRegistrations.get('Mod+B')?.handler())
+ it('toggles detail content outside editing fields', () => {
+ renderDetailSidebarFrame(null, platform)
- expect(screen.getByTestId('detail-top')).toHaveAttribute('data-expand', 'false')
- expect(localStorage.getItem(DETAIL_SIDEBAR_STORAGE_KEY)).toBe('collapse')
+ fireEvent.keyDown(document.body, { key: 'b', ...modifiers })
+
+ expect(screen.getByTestId('detail-top')).toHaveAttribute('data-expand', 'false')
+ expect(localStorage.getItem(DETAIL_SIDEBAR_STORAGE_KEY)).toBe('collapse')
+
+ fireEvent.keyDown(document.body, { key: 'b', ...modifiers })
+
+ expect(screen.getByTestId('detail-top')).toHaveAttribute('data-expand', 'true')
+ expect(localStorage.getItem(DETAIL_SIDEBAR_STORAGE_KEY)).toBe('expand')
+ })
+
+ it.each(['Note', 'Note text', 'Name', 'Description'])(
+ 'leaves the shortcut to the %s editor',
+ (name) => {
+ renderDetailSidebarFrame(null, platform)
+ const editor =
+ name === 'Note text' ? screen.getByText(name) : screen.getByRole('textbox', { name })
+ editor.focus()
+ const event = new KeyboardEvent('keydown', {
+ key: 'b',
+ ...modifiers,
+ bubbles: true,
+ cancelable: true,
+ })
+
+ fireEvent(editor, event)
+
+ expect(event.defaultPrevented).toBe(false)
+ expect(screen.getByTestId('detail-top')).toHaveAttribute('data-expand', 'true')
+ expect(localStorage.getItem(DETAIL_SIDEBAR_STORAGE_KEY)).toBe('expand')
+ },
+ )
})
it('collapses detail content from the top toggle and hides environment metadata', () => {
diff --git a/web/app/components/detail-sidebar/index.tsx b/web/app/components/detail-sidebar/index.tsx
index 7b4bc886772..aed32173b14 100644
--- a/web/app/components/detail-sidebar/index.tsx
+++ b/web/app/components/detail-sidebar/index.tsx
@@ -110,7 +110,7 @@ export function DetailSidebarFrame({
}, [])
useHotkey(DETAIL_SIDEBAR_TOGGLE_HOTKEY, handleToggleDetailNavigation, {
- ignoreInputs: false,
+ ignoreInputs: true,
preventDefault: true,
})