|
| 1 | +import { useEffect, useRef, useState } from 'react'; |
| 2 | +import { Input, InputRef, Modal, Space } from 'antd'; |
| 3 | +import { useSetAtom } from 'jotai'; |
| 4 | +import { KeyboardIcon, SquarePenIcon } from 'lucide-react'; |
| 5 | +import { useTranslation } from 'react-i18next'; |
| 6 | +import { isKeyboardEnableAtom } from '@/jotai/keyboard.ts'; |
| 7 | +import { modifierKeys, Modifiers, ShortcutProps } from '@/libs/device/keyboard.ts'; |
| 8 | + |
| 9 | +interface KeyboardShortcutCustomProps { |
| 10 | + addShortcut: (shortcut: ShortcutProps) => void; |
| 11 | +} |
| 12 | + |
| 13 | +export const KeyboardShortcutCustom = ({ addShortcut }: KeyboardShortcutCustomProps) => { |
| 14 | + const { t } = useTranslation(); |
| 15 | + const setIsKeyboardEnable = useSetAtom(isKeyboardEnableAtom); |
| 16 | + const [isModalOpen, setIsModalOpen] = useState<boolean>(false); |
| 17 | + const [isCapturingShortcut, setIsCapturingShortcut] = useState<boolean>(false); |
| 18 | + const [shortcut, setShortcut] = useState<ShortcutProps | null>(null); |
| 19 | + const [shortcutLabel, setShortcutLabel] = useState<string>(''); |
| 20 | + const [shortcutFixedLabel, setShortcutFixedLabel] = useState<string>(''); |
| 21 | + const pressedModifiersRef = useRef<Set<string>>(new Set()); |
| 22 | + const inputRef = useRef<InputRef>(null); |
| 23 | + |
| 24 | + function getPressedShortcutString(event: KeyboardEvent) { |
| 25 | + let pressedKey: string = ''; |
| 26 | + pressedKey += event.ctrlKey ? 'Ctrl + ' : ''; |
| 27 | + pressedKey += event.shiftKey ? 'Shift + ' : ''; |
| 28 | + pressedKey += event.altKey ? 'Alt + ' : ''; |
| 29 | + pressedKey += event.metaKey ? 'Meta + ' : ''; |
| 30 | + if (!modifierKeys.has(event.key)) { |
| 31 | + if (event.code.startsWith('Key') || event.code.startsWith('Digit')) { |
| 32 | + pressedKey += event.code.slice(-1); |
| 33 | + } else { |
| 34 | + pressedKey += event.key; |
| 35 | + } |
| 36 | + } |
| 37 | + return pressedKey; |
| 38 | + } |
| 39 | + |
| 40 | + function handleModelDone() { |
| 41 | + if (shortcut == null) { |
| 42 | + cleanShortcut(); |
| 43 | + return; |
| 44 | + } |
| 45 | + addShortcut({ |
| 46 | + modifiers: shortcut.modifiers, |
| 47 | + label: shortcutLabel == '' ? shortcutFixedLabel : shortcutLabel, |
| 48 | + keyCode: shortcut.keyCode |
| 49 | + }); |
| 50 | + cleanShortcut(); |
| 51 | + } |
| 52 | + |
| 53 | + function cleanShortcut() { |
| 54 | + setIsCapturingShortcut(false); |
| 55 | + setIsModalOpen(false); |
| 56 | + setShortcut(null); |
| 57 | + setShortcutLabel(''); |
| 58 | + setShortcutFixedLabel(''); |
| 59 | + } |
| 60 | + |
| 61 | + async function handleKeyDown(event: KeyboardEvent) { |
| 62 | + event.preventDefault(); |
| 63 | + event.stopPropagation(); |
| 64 | + |
| 65 | + const label = getPressedShortcutString(event); |
| 66 | + setShortcutFixedLabel(label); |
| 67 | + |
| 68 | + if (modifierKeys.has(event.key)) { |
| 69 | + if (event.type == 'keydown') { |
| 70 | + pressedModifiersRef.current.add(event.code); |
| 71 | + } else { |
| 72 | + pressedModifiersRef.current.delete(event.code); |
| 73 | + } |
| 74 | + } else { |
| 75 | + setIsCapturingShortcut(false); |
| 76 | + window.removeEventListener('keydown', handleKeyDown); |
| 77 | + window.removeEventListener('keyup', handleKeyDown); |
| 78 | + const modifiers = Modifiers.getModifiers(event, pressedModifiersRef.current); |
| 79 | + const ret: ShortcutProps = { |
| 80 | + modifiers: modifiers, |
| 81 | + label: label, |
| 82 | + keyCode: event.code |
| 83 | + }; |
| 84 | + setShortcut(ret); |
| 85 | + |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + function handleFullscreen() { |
| 90 | + if (!document.fullscreenElement) { |
| 91 | + const element = document.documentElement; |
| 92 | + element.requestFullscreen().then(); |
| 93 | + // @ts-expect-error - https://developer.mozilla.org/en-US/docs/Web/API/Keyboard/lock |
| 94 | + navigator.keyboard?.lock(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + useEffect(() => { |
| 99 | + setIsKeyboardEnable(!isModalOpen); |
| 100 | + }, [isModalOpen]); |
| 101 | + |
| 102 | + useEffect(() => { |
| 103 | + if (inputRef.current && !isCapturingShortcut) { |
| 104 | + inputRef.current.blur(); |
| 105 | + } |
| 106 | + }, [isCapturingShortcut]); |
| 107 | + |
| 108 | + return ( |
| 109 | + <> |
| 110 | + <div |
| 111 | + className="flex h-[30px] cursor-pointer items-center space-x-1 rounded px-3 text-neutral-300 hover:bg-neutral-700/60" |
| 112 | + onClick={() => { |
| 113 | + setIsModalOpen(true); |
| 114 | + }} |
| 115 | + > |
| 116 | + <span>{t('keyboard.shortcut.custom')}</span> |
| 117 | + </div> |
| 118 | + <Modal |
| 119 | + width={400} |
| 120 | + title={t('keyboard.shortcut.custom')} |
| 121 | + open={isModalOpen} |
| 122 | + onOk={handleModelDone} |
| 123 | + onCancel={cleanShortcut} |
| 124 | + okText={t('keyboard.shortcut.save')} |
| 125 | + cancelText={t('keyboard.shortcut.cancel')}> |
| 126 | + <Space direction="vertical" style={{ width: '100%' }} size="middle"> |
| 127 | + <div className="flex"> |
| 128 | + {t('keyboard.shortcut.captureTips')} |
| 129 | + <a onClick={handleFullscreen}>{t('keyboard.shortcut.enterFullScreen')}</a> |
| 130 | + </div> |
| 131 | + <Input |
| 132 | + ref={inputRef} |
| 133 | + placeholder={t('keyboard.shortcut.capture')} |
| 134 | + value={shortcutFixedLabel} |
| 135 | + prefix={<KeyboardIcon size={16} />} |
| 136 | + onFocus={() => { |
| 137 | + setIsCapturingShortcut(true); |
| 138 | + window.addEventListener('keydown', handleKeyDown); |
| 139 | + window.addEventListener('keyup', handleKeyDown); |
| 140 | + }} |
| 141 | + onBlur={() => { |
| 142 | + setIsCapturingShortcut(false); |
| 143 | + window.removeEventListener('keydown', handleKeyDown); |
| 144 | + window.removeEventListener('keyup', handleKeyDown); |
| 145 | + }} |
| 146 | + /> |
| 147 | + |
| 148 | + <Input |
| 149 | + placeholder={shortcutFixedLabel || t('keyboard.shortcut.label')} |
| 150 | + value={shortcutLabel} |
| 151 | + prefix={<SquarePenIcon size={16} />} |
| 152 | + onChange={(e) => setShortcutLabel(e.target.value)} |
| 153 | + /> |
| 154 | + |
| 155 | + </Space> |
| 156 | + </Modal> |
| 157 | + </> |
| 158 | + ); |
| 159 | +}; |
0 commit comments