diff --git a/src/renderer/src/components/editor/RichMarkdownLinkBubble.test.ts b/src/renderer/src/components/editor/RichMarkdownLinkBubble.test.ts new file mode 100644 index 00000000000..8e67e0a7d40 --- /dev/null +++ b/src/renderer/src/components/editor/RichMarkdownLinkBubble.test.ts @@ -0,0 +1,11 @@ +import { describe, expect, it } from 'vitest' +import { isLinkEditCancelShortcut } from './RichMarkdownLinkBubble' + +describe('isLinkEditCancelShortcut', () => { + it('uses only the platform primary modifier for link-edit cancellation', () => { + expect(isLinkEditCancelShortcut({ key: 'k', metaKey: true, ctrlKey: false }, true)).toBe(true) + expect(isLinkEditCancelShortcut({ key: 'k', metaKey: false, ctrlKey: true }, true)).toBe(false) + expect(isLinkEditCancelShortcut({ key: 'k', metaKey: false, ctrlKey: true }, false)).toBe(true) + expect(isLinkEditCancelShortcut({ key: 'k', metaKey: true, ctrlKey: false }, false)).toBe(false) + }) +}) diff --git a/src/renderer/src/components/editor/RichMarkdownLinkBubble.tsx b/src/renderer/src/components/editor/RichMarkdownLinkBubble.tsx index 5a033cdc530..0d2a510c801 100644 --- a/src/renderer/src/components/editor/RichMarkdownLinkBubble.tsx +++ b/src/renderer/src/components/editor/RichMarkdownLinkBubble.tsx @@ -28,6 +28,16 @@ export function getLinkBubblePosition( } } +export function isLinkEditCancelShortcut( + event: Pick, + isMac: boolean +): boolean { + if (event.key.toLowerCase() !== 'k') { + return false + } + return isMac ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey +} + function LinkEditInput({ initialHref, onSave, @@ -38,6 +48,7 @@ function LinkEditInput({ onCancel: () => void }): React.JSX.Element { const [value, setValue] = useState(initialHref) + const isMac = navigator.userAgent.includes('Mac') const setInputElement = useCallback((input: HTMLInputElement | null) => { if (!input) { @@ -64,7 +75,7 @@ function LinkEditInput({ onCancel() } // Cmd/Ctrl+K while editing cancels the edit. - if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k') { + if (isLinkEditCancelShortcut(e, isMac)) { e.preventDefault() onCancel() }