|  | 
|  | 1 | +import React, { | 
|  | 2 | +  KeyboardEventHandler, | 
|  | 3 | +  useCallback, | 
|  | 4 | +  useMemo, | 
|  | 5 | +  useRef, | 
|  | 6 | +  useState, | 
|  | 7 | +} from 'react' | 
|  | 8 | +import { | 
|  | 9 | +  getGenericShortcutString, | 
|  | 10 | +  KeymapItemEditableProps, | 
|  | 11 | +} from '../../../lib/keymap' | 
|  | 12 | +import Button from '../../../design/components/atoms/Button' | 
|  | 13 | +import styled from '../../../design/lib/styled' | 
|  | 14 | +import { inputStyle } from '../../../design/lib/styled/styleFunctions' | 
|  | 15 | +import cc from 'classcat' | 
|  | 16 | +import { useToast } from '../../../design/lib/stores/toast' | 
|  | 17 | + | 
|  | 18 | +const invalidShortcutInputs = [' '] | 
|  | 19 | +const rejectedShortcutInputs = [' ', 'control', 'alt', 'shift', 'meta'] | 
|  | 20 | + | 
|  | 21 | +interface KeymapItemSectionProps { | 
|  | 22 | +  keymapKey: string | 
|  | 23 | +  currentKeymapItem?: KeymapItemEditableProps | 
|  | 24 | +  updateKeymap: ( | 
|  | 25 | +    key: string, | 
|  | 26 | +    shortcutFirst: KeymapItemEditableProps, | 
|  | 27 | +    shortcutSecond?: KeymapItemEditableProps | 
|  | 28 | +  ) => Promise<void> | 
|  | 29 | +  removeKeymap: (key: string) => void | 
|  | 30 | +  description: string | 
|  | 31 | +} | 
|  | 32 | + | 
|  | 33 | +const KeymapItemSection = ({ | 
|  | 34 | +  keymapKey, | 
|  | 35 | +  currentKeymapItem, | 
|  | 36 | +  updateKeymap, | 
|  | 37 | +  removeKeymap, | 
|  | 38 | +  description, | 
|  | 39 | +}: KeymapItemSectionProps) => { | 
|  | 40 | +  const [inputError, setInputError] = useState<boolean>(false) | 
|  | 41 | +  const [shortcutInputValue, setShortcutInputValue] = useState<string>('') | 
|  | 42 | +  const [changingShortcut, setChangingShortcut] = useState<boolean>(false) | 
|  | 43 | +  const [ | 
|  | 44 | +    currentShortcut, | 
|  | 45 | +    setCurrentShortcut, | 
|  | 46 | +  ] = useState<KeymapItemEditableProps | null>( | 
|  | 47 | +    currentKeymapItem != null ? currentKeymapItem : null | 
|  | 48 | +  ) | 
|  | 49 | +  const [ | 
|  | 50 | +    previousShortcut, | 
|  | 51 | +    setPreviousShortcut, | 
|  | 52 | +  ] = useState<KeymapItemEditableProps | null>(null) | 
|  | 53 | +  const shortcutInputRef = useRef<HTMLInputElement>(null) | 
|  | 54 | + | 
|  | 55 | +  const { pushMessage } = useToast() | 
|  | 56 | + | 
|  | 57 | +  const fetchInputShortcuts: KeyboardEventHandler<HTMLInputElement> = ( | 
|  | 58 | +    event | 
|  | 59 | +  ) => { | 
|  | 60 | +    event.stopPropagation() | 
|  | 61 | +    event.preventDefault() | 
|  | 62 | +    if (invalidShortcutInputs.includes(event.key.toLowerCase())) { | 
|  | 63 | +      setInputError(true) | 
|  | 64 | +      return | 
|  | 65 | +    } | 
|  | 66 | + | 
|  | 67 | +    setInputError(false) | 
|  | 68 | + | 
|  | 69 | +    const shortcut: KeymapItemEditableProps = { | 
|  | 70 | +      key: event.key.toUpperCase(), | 
|  | 71 | +      keycode: event.keyCode, | 
|  | 72 | +      modifiers: { | 
|  | 73 | +        ctrl: event.ctrlKey, | 
|  | 74 | +        alt: event.altKey, | 
|  | 75 | +        shift: event.shiftKey, | 
|  | 76 | +        meta: event.metaKey, | 
|  | 77 | +      }, | 
|  | 78 | +    } | 
|  | 79 | +    setCurrentShortcut(shortcut) | 
|  | 80 | +    setShortcutInputValue(getGenericShortcutString(shortcut)) | 
|  | 81 | +  } | 
|  | 82 | + | 
|  | 83 | +  const applyKeymap = useCallback(() => { | 
|  | 84 | +    if (currentShortcut == null) { | 
|  | 85 | +      return | 
|  | 86 | +    } | 
|  | 87 | +    if (rejectedShortcutInputs.includes(currentShortcut.key.toLowerCase())) { | 
|  | 88 | +      setInputError(true) | 
|  | 89 | +      if (shortcutInputRef.current != null) { | 
|  | 90 | +        shortcutInputRef.current.focus() | 
|  | 91 | +      } | 
|  | 92 | +      return | 
|  | 93 | +    } | 
|  | 94 | + | 
|  | 95 | +    updateKeymap(keymapKey, currentShortcut, undefined) | 
|  | 96 | +      .then(() => { | 
|  | 97 | +        setChangingShortcut(false) | 
|  | 98 | +        setInputError(false) | 
|  | 99 | +      }) | 
|  | 100 | +      .catch(() => { | 
|  | 101 | +        pushMessage({ | 
|  | 102 | +          title: 'Keymap assignment failed', | 
|  | 103 | +          description: 'Cannot assign to already assigned shortcut', | 
|  | 104 | +        }) | 
|  | 105 | +        setInputError(true) | 
|  | 106 | +      }) | 
|  | 107 | +  }, [currentShortcut, keymapKey, updateKeymap, pushMessage]) | 
|  | 108 | + | 
|  | 109 | +  const toggleChangingShortcut = useCallback(() => { | 
|  | 110 | +    if (changingShortcut) { | 
|  | 111 | +      applyKeymap() | 
|  | 112 | +    } else { | 
|  | 113 | +      setChangingShortcut(true) | 
|  | 114 | +      setPreviousShortcut(currentShortcut) | 
|  | 115 | +      if (currentShortcut != null) { | 
|  | 116 | +        setShortcutInputValue(getGenericShortcutString(currentShortcut)) | 
|  | 117 | +      } | 
|  | 118 | +    } | 
|  | 119 | +  }, [applyKeymap, currentShortcut, changingShortcut]) | 
|  | 120 | + | 
|  | 121 | +  const handleCancelKeymapChange = useCallback(() => { | 
|  | 122 | +    setCurrentShortcut(previousShortcut) | 
|  | 123 | +    setChangingShortcut(false) | 
|  | 124 | +    setShortcutInputValue('') | 
|  | 125 | +    setInputError(false) | 
|  | 126 | +  }, [previousShortcut]) | 
|  | 127 | + | 
|  | 128 | +  const handleRemoveKeymap = useCallback(() => { | 
|  | 129 | +    setCurrentShortcut(null) | 
|  | 130 | +    setPreviousShortcut(null) | 
|  | 131 | +    setShortcutInputValue('') | 
|  | 132 | +    removeKeymap(keymapKey) | 
|  | 133 | +  }, [keymapKey, removeKeymap]) | 
|  | 134 | + | 
|  | 135 | +  const shortcutString = useMemo(() => { | 
|  | 136 | +    return currentShortcut != null && currentKeymapItem != null | 
|  | 137 | +      ? getGenericShortcutString(currentKeymapItem) | 
|  | 138 | +      : '' | 
|  | 139 | +  }, [currentKeymapItem, currentShortcut]) | 
|  | 140 | +  return ( | 
|  | 141 | +    <KeymapItemSectionContainer> | 
|  | 142 | +      <div>{description}</div> | 
|  | 143 | +      <KeymapItemInputSection> | 
|  | 144 | +        {currentShortcut != null && currentKeymapItem != null && ( | 
|  | 145 | +          <ShortcutItemStyle>{shortcutString}</ShortcutItemStyle> | 
|  | 146 | +        )} | 
|  | 147 | +        {changingShortcut && ( | 
|  | 148 | +          <StyledInput | 
|  | 149 | +            className={cc([inputError && 'error'])} | 
|  | 150 | +            placeholder={'Press key'} | 
|  | 151 | +            autoFocus={true} | 
|  | 152 | +            ref={shortcutInputRef} | 
|  | 153 | +            value={shortcutInputValue} | 
|  | 154 | +            onChange={() => undefined} | 
|  | 155 | +            onKeyDown={fetchInputShortcuts} | 
|  | 156 | +          /> | 
|  | 157 | +        )} | 
|  | 158 | +        <Button variant={'primary'} onClick={toggleChangingShortcut}> | 
|  | 159 | +          {currentShortcut == null | 
|  | 160 | +            ? 'Assign' | 
|  | 161 | +            : changingShortcut | 
|  | 162 | +            ? 'Apply' | 
|  | 163 | +            : 'Change'} | 
|  | 164 | +        </Button> | 
|  | 165 | +        {changingShortcut && ( | 
|  | 166 | +          <Button onClick={handleCancelKeymapChange}>Cancel</Button> | 
|  | 167 | +        )} | 
|  | 168 | + | 
|  | 169 | +        {currentShortcut != null && !changingShortcut && ( | 
|  | 170 | +          <Button onClick={handleRemoveKeymap}>Un-assign</Button> | 
|  | 171 | +        )} | 
|  | 172 | +      </KeymapItemInputSection> | 
|  | 173 | +    </KeymapItemSectionContainer> | 
|  | 174 | +  ) | 
|  | 175 | +} | 
|  | 176 | + | 
|  | 177 | +const ShortcutItemStyle = styled.div` | 
|  | 178 | +  min-width: 88px; | 
|  | 179 | +  max-width: 120px; | 
|  | 180 | +  height: 32px; | 
|  | 181 | +  font-size: 15px; | 
|  | 182 | +  display: flex; | 
|  | 183 | +  align-items: center; | 
|  | 184 | +  justify-content: center; | 
|  | 185 | +
 | 
|  | 186 | +  background-color: ${({ theme }) => theme.colors.background.tertiary}; | 
|  | 187 | +  color: ${({ theme }) => theme.colors.text.primary}; | 
|  | 188 | +
 | 
|  | 189 | +  border: 1px solid ${({ theme }) => theme.colors.border.main}; | 
|  | 190 | +  border-radius: 4px; | 
|  | 191 | +` | 
|  | 192 | + | 
|  | 193 | +const StyledInput = styled.input` | 
|  | 194 | +  ${inputStyle}; | 
|  | 195 | +  max-width: 120px; | 
|  | 196 | +  min-width: 110px; | 
|  | 197 | +  height: 1.3em; | 
|  | 198 | +
 | 
|  | 199 | +  &.error { | 
|  | 200 | +    border: 1px solid red; | 
|  | 201 | +  } | 
|  | 202 | +` | 
|  | 203 | + | 
|  | 204 | +const KeymapItemSectionContainer = styled.div` | 
|  | 205 | +  display: grid; | 
|  | 206 | +  grid-template-columns: 45% minmax(55%, 400px); | 
|  | 207 | +` | 
|  | 208 | + | 
|  | 209 | +const KeymapItemInputSection = styled.div` | 
|  | 210 | +  display: grid; | 
|  | 211 | +  grid-auto-flow: column; | 
|  | 212 | +  align-items: center; | 
|  | 213 | +
 | 
|  | 214 | +  max-width: 380px; | 
|  | 215 | +  justify-items: left; | 
|  | 216 | +
 | 
|  | 217 | +  margin-right: auto; | 
|  | 218 | +
 | 
|  | 219 | +  column-gap: 1em; | 
|  | 220 | +` | 
|  | 221 | + | 
|  | 222 | +export default KeymapItemSection | 
0 commit comments