fix: respect platform link edit shortcut

This commit is contained in:
Neil
2026-05-31 09:31:03 -07:00
committed by GitHub
parent 2246e26757
commit 4c5bfe5c32
2 changed files with 23 additions and 1 deletions
@@ -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)
})
})
@@ -28,6 +28,16 @@ export function getLinkBubblePosition(
}
}
export function isLinkEditCancelShortcut(
event: Pick<KeyboardEvent, 'key' | 'metaKey' | 'ctrlKey'>,
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()
}