diff --git a/components/Key.js b/components/Key.js index 0525a48..24d6f73 100644 --- a/components/Key.js +++ b/components/Key.js @@ -1,26 +1,4 @@ -const getKeyLabels = keyContent => { - /* - Code line below is called `destructuring`, - because we destructure an object `this.keyContent` - into 3 separate constants. - */ - const { main = '', shifted = '', label, code } = keyContent - const isUpperCaseLang = main.toUpperCase() === shifted - const mainOutput = isUpperCaseLang ? shifted : main - const shiftedOutput = isUpperCaseLang ? '' : shifted - - return { - /* - || is a logical `or` operator. - The line below is `or chain`. - If `label` exists, it will be returned. - If `label` doesn't exist, but `main` exists -- will be returned `main`. - If `label` and main don't exist, will be returned `code`. - */ - main: label || mainOutput || code, - shifted: shiftedOutput - } -} +import { getKeyLabels } from '../utils.js' const Key = { template: ` diff --git a/components/Keyboard.js b/components/Keyboard.js index f5ed3ec..e85ab06 100644 --- a/components/Keyboard.js +++ b/components/Keyboard.js @@ -1,20 +1,5 @@ import Key from './Key.js' - -const getAudioFileName = (keyContent, shiftKey) => { - const { main, mainName, shifted, shiftedName, code } = keyContent - - let fileName - - if (shiftKey) { - // will be returned 1 of 3 values (if it exist). priority to the first one - fileName = shiftedName || shifted || code - } else { - fileName = mainName || main || code - } - - // to have a guarantee, that everything is written in the same (lower) case - return fileName.toLowerCase() -} +import { getAudioFileName, loadKeyboardData, playKeyAudio } from '../utils.js' const Keyboard = { template: ` @@ -52,11 +37,11 @@ const Keyboard = { }, watch: { currentLang: function (currentLang) { - this.getKeyboardData(currentLang) + this.setKeyboardData(currentLang) } }, mounted() { - this.getKeyboardData(this.currentLang) + this.setKeyboardData(this.currentLang) window.addEventListener('keydown', event => { event.preventDefault() @@ -82,11 +67,8 @@ const Keyboard = { }, methods: { - async getKeyboardData(lang) { - const { default: keyboardData } = await import( - `../keyboardData/${lang}.js` - ) - this.keyboardData[lang] = keyboardData + async setKeyboardData(lang) { + this.keyboardData[lang] = await loadKeyboardData(lang) }, getKeyContent(lang, code) { return this.keyboardData[lang].flat().find(elem => elem.code === code) @@ -100,17 +82,11 @@ const Keyboard = { const { code } = keyContent const { shiftKey, currentLang } = this - const playKeyAudio = (lang, code, shiftKey) => { - const keyContent = this.getKeyContent(lang, code) - const fileName = getAudioFileName(keyContent, shiftKey) - const audio = new Audio(`../keyboardData/${lang}/${fileName}.mp3`) - return audio.play() - } - - playKeyAudio(currentLang, code, shiftKey).catch(() => { + playKeyAudio(currentLang, keyContent, shiftKey).catch(() => { // fallback if (this.currentLang !== 'en') { - playKeyAudio('en', code, shiftKey) + const keyContent = this.getKeyContent('en', code) + playKeyAudio('en', keyContent, shiftKey) } }) }, diff --git a/utils.js b/utils.js new file mode 100644 index 0000000..5411e50 --- /dev/null +++ b/utils.js @@ -0,0 +1,50 @@ +export const getAudioFileName = (keyContent, shiftKey) => { + const { main, mainName, shifted, shiftedName, code } = keyContent + + let fileName + + if (shiftKey) { + // will be returned 1 of 3 values (if it exist). priority to the first one + fileName = shiftedName || shifted || code + } else { + fileName = mainName || main || code + } + + // to have a guarantee, that everything is written in the same (lower) case + return fileName.toLowerCase() +} + +export const getKeyLabels = keyContent => { + /* + Code line below is called `destructuring`, + because we destructure an object `this.keyContent` + into 3 separate constants. + */ + const { main = '', shifted = '', label, code } = keyContent + const isUpperCaseLang = main.toUpperCase() === shifted + const mainOutput = isUpperCaseLang ? shifted : main + const shiftedOutput = isUpperCaseLang ? '' : shifted + + return { + /* + || is a logical `or` operator. + The line below is `or chain`. + If `label` exists, it will be returned. + If `label` doesn't exist, but `main` exists -- will be returned `main`. + If `label` and main don't exist, will be returned `code`. + */ + main: label || mainOutput || code, + shifted: shiftedOutput + } +} + +export const loadKeyboardData = async lang => { + const { default: keyboardData } = await import(`../keyboardData/${lang}.js`) + return keyboardData +} + +export const playKeyAudio = (lang, keyContent, shiftKey) => { + const fileName = getAudioFileName(keyContent, shiftKey) + const audio = new Audio(`../keyboardData/${lang}/${fileName}.mp3`) + return audio.play() +}