refactor(settings): key code & controlled component

This commit is contained in:
EnixCoda 2018-06-13 23:22:52 +08:00
parent 126c1226b7
commit 46436b0575
2 changed files with 3 additions and 6 deletions

View file

@ -21,7 +21,7 @@ function detectOS() {
}
function friendlyFormatShortcut(shortcut) {
if (!shortcut) return
if (typeof shortcut !== 'string') return ''
const OS = detectOS()
if (OS === OperatingSystems.Windows) {
return shortcut.replace(/meta/, 'win')
@ -158,9 +158,9 @@ export default class SettingsBar extends React.Component {
<input
className={'toggle-shortcut-input form-control'}
placeholder={'focus here and press the shortcut keys'}
default={''}
value={friendlyFormatShortcut(toggleShowSideBarShortcut)}
onKeyDown={this.onShortCutInputKeyDown}
readOnly
/>
<button className={'btn'} onClick={this.saveShortcut}>
Save

View file

@ -35,15 +35,12 @@ function parse(keysString) {
function parseKeyCode(code) {
return code
.toLowerCase()
.replace(/(left|right)$/, '')
.replace(/^control$/, 'ctrl')
.replace(/^digit/, '')
.replace(/^key/, '')
}
function parseEvent(e) {
const { altKey: alt, shiftKey: shift, metaKey: meta, ctrlKey: ctrl } = e
const code = parseKeyCode(e.code)
const code = parseKeyCode(e.key)
const keys = { meta, ctrl, shift, alt, [code]: true }
const combination = parse(
Object.entries(keys)