Skip to content

Commit

Permalink
refactor: use event.key instead of event.keyCode
Browse files Browse the repository at this point in the history
  • Loading branch information
ambar committed Jan 24, 2022
1 parent 1fe1fb7 commit e957cb9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 56 deletions.
58 changes: 29 additions & 29 deletions packages/griffith/src/components/Controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {css} from 'aphrodite/no-important'
import debounce from 'lodash/debounce'
import clamp from 'lodash/clamp'
import {ProgressDot} from '../types'
import KeyCode from '../constants/KeyCode'
import PlayButtonItem from './items/PlayButtonItem'
import TimelineItem from './items/TimelineItem'
import CombinedTimeItem from './items/CombinedTimeItem'
Expand Down Expand Up @@ -192,66 +191,67 @@ class Controller extends Component<ControllerProps, State> {
isPageFullScreen,
} = this.props
let handled = true
switch (event.keyCode) {
case KeyCode.SPACE:
case KeyCode.K:

switch (event.key) {
case ' ':
case 'k':
if (this.firstKey) {
this.handleToggle('keyCode')
}
break

case KeyCode.ENTER:
case KeyCode.F:
case 'Enter':
case 'f':
if (this.firstKey) {
onToggleFullScreen?.()
}
break
case KeyCode.ESC:
case 'Escape':
if (this.firstKey && isPageFullScreen) {
onTogglePageFullScreen?.()
}
break
case KeyCode.LEFT:
case 'ArrowLeft':
this.handleSeek(currentTime - 5)
break

case KeyCode.RIGHT:
case 'ArrowRight':
this.handleSeek(currentTime + 5)
break

case KeyCode.J:
case 'j':
if (this.firstKey) {
this.handleSeek(currentTime - 10)
}
break

case KeyCode.L:
case 'l':
if (this.firstKey) {
this.handleSeek(currentTime + 10)
}
break
case KeyCode.ZERO:
case KeyCode.ONE:
case KeyCode.TWO:
case KeyCode.THREE:
case KeyCode.FOUR:
case KeyCode.FIVE:
case KeyCode.SIX:
case KeyCode.SEVEN:
case KeyCode.EIGHT:
case KeyCode.NINE:
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (show && this.firstKey) {
const nextTime = (duration / 10) * (event.keyCode - KeyCode.ZERO)
const nextTime = (duration / 10) * Number(event.key)
this.handleSeek(nextTime)
}
break

case KeyCode.M:
case 'm':
if (this.firstKey) {
this.handleToggleMuted()
}
break
case KeyCode.UP:
case 'ArrowUp':
if (this.firstKey && volume) {
this.prevVolume = volume
}
Expand All @@ -261,7 +261,7 @@ class Controller extends Component<ControllerProps, State> {
this.handleVolumeChange(volume + 0.05)
break

case KeyCode.DOWN:
case 'ArrowDown':
if (this.firstKey && volume) {
this.prevVolume = volume
}
Expand All @@ -278,12 +278,12 @@ class Controller extends Component<ControllerProps, State> {
this.firstKey = false
}

handleKeyUp = (event: any) => {
handleKeyUp = (event: KeyboardEvent) => {
this.firstKey = true

switch (event.keyCode) {
case KeyCode.UP:
case KeyCode.DOWN:
switch (event.key) {
case 'ArrowUp':
case 'ArrowDown':
this.handleVolumeKeyboard()
break
}
Expand Down
5 changes: 2 additions & 3 deletions packages/griffith/src/components/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import clamp from 'lodash/clamp'
import {ProgressDot as ProgressDotType} from '../types'
import ProgressDot, {ProgressDotsProps} from './ProgressDot'
import formatPercent from '../utils/formatPercent'
import KeyCode from '../constants/KeyCode'

import styles, {
horizontal as horizontalStyles,
Expand Down Expand Up @@ -145,10 +144,10 @@ class Slider extends Component<SliderProps, State> {
const {reverse, value, total, step} = this.props

let direction = 0
if (event.keyCode === KeyCode.LEFT || event.keyCode === KeyCode.DOWN) {
if (event.key === 'ArrowLeft' || event.key === 'ArrowDown') {
direction = -1
}
if (event.keyCode === KeyCode.RIGHT || event.keyCode === KeyCode.UP) {
if (event.key === 'ArrowRight' || event.key === 'ArrowUp') {
direction = 1
}
if (reverse) {
Expand Down
24 changes: 0 additions & 24 deletions packages/griffith/src/constants/KeyCode.ts

This file was deleted.

0 comments on commit e957cb9

Please sign in to comment.