Skip to content

Commit

Permalink
13.3. Move getKeyContent to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ApayRus committed Aug 13, 2022
1 parent 21b05ce commit 0f653d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion components/Keyboard.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Key from './Key.js'
import { getAudioFileName, loadKeyboardData, playKeyAudio } from '../utils.js'
import { loadKeyboardData, playKeyAudio, getKeyContent } from '../utils.js'

const Keyboard = {
template: `
Expand Down Expand Up @@ -47,7 +47,11 @@ const Keyboard = {
event.preventDefault()
const { code, shiftKey } = event

/* replace:
const keyContent = this.getKeyContent(this.currentLang, code)
with next 2 lines */
const keyboardData = this.keyboardData[this.currentLang]
const keyContent = getKeyContent({ keyboardData, code })

this.setActiveKey(keyContent)
this.playKey(keyContent)
Expand Down Expand Up @@ -85,7 +89,11 @@ const Keyboard = {
playKeyAudio(currentLang, keyContent, shiftKey).catch(() => {
// fallback
if (this.currentLang !== 'en') {
/* replace:
const keyContent = this.getKeyContent('en', code)
with next 2 lines */
const keyboardData = this.keyboardData['en']
const keyContent = getKeyContent({ keyboardData, code })
playKeyAudio('en', keyContent, shiftKey)
}
})
Expand Down
7 changes: 7 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@ export const playKeyAudio = (lang, keyContent, shiftKey) => {
const audio = new Audio(`../keyboardData/sounds/${lang}/${fileName}.mp3`)
return audio.play()
}

export const getKeyContent = ({ keyboardData, code = '', value = '' }) => {
return keyboardData.flat().find(elem => {
const { main, shifted } = elem
return elem.code === code || value === main || value === shifted
})
}

0 comments on commit 0f653d9

Please sign in to comment.