Skip to content

Commit

Permalink
Give details on mistake in alert message
Browse files Browse the repository at this point in the history
Closes #25
  • Loading branch information
qsantos committed Sep 19, 2024
1 parent 3f17855 commit 61102f1
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ const translations = {
'acknowledgements.cc-by-license': 'CC-BY License',
'acknowledgements.jscwlib': 'JavaScript library for Morse Code',
'acknowledgements.cat-icon': 'Cat icon',
'info.incorrect': 'Incorrect!',
'info.incorrect': 'You typed ${typed} but the next character played was ${played}!',
'info.tooFast': 'You typed ${typed} before the next character was played!',
'info.tooSlow': 'Too slow!',
'info.lostFocus': 'Focus lost!',
},
Expand Down Expand Up @@ -173,7 +174,8 @@ const translations = {
'acknowledgements.cc-by-license': 'Licence CC-BY',
'acknowledgements.jscwlib': 'Bibliothèque JavaScript pour le code Morse',
'acknowledgements.cat-icon': 'Icône de chat',
'info.incorrect': 'Incorrect !',
'info.incorrect': 'Vous avez tapé ${typed}, mais le caractère suivant était ${played} !',
'info.tooFast': 'Vous avez tapé ${typed} avant que le caractère suivant ne soit joué !',
'info.tooSlow': 'Trop lent !',
'info.lostFocus': 'Focus perdu !',
},
Expand Down Expand Up @@ -228,7 +230,8 @@ const translations = {
'acknowledgements.cc-by-license': 'CC-BY ライセンス',
'acknowledgements.jscwlib': 'モールス信号用JavaScriptライブラリ',
'acknowledgements.cat-icon': '猫アイコン',
'info.incorrect': '不正確!',
'info.incorrect': 'あなたが入力したのは ${typed} ですが、次に再生された文字は ${played} です!',
'info.tooFast': '次の文字が再生される前に ${typed} を入力しました!',
'info.tooSlow': '遅すぎます!',
'info.lostFocus': 'フォーカスが外れました!',
},
Expand Down Expand Up @@ -283,7 +286,8 @@ const translations = {
'acknowledgements.cc-by-license': 'Licencia CC-BY',
'acknowledgements.jscwlib': 'Biblioteca JavaScript para código Morse',
'acknowledgements.cat-icon': 'Ícono de gato',
'info.incorrect': '¡Incorrecto!',
'info.incorrect': 'Has escrito ${typed}, pero el siguiente carácter jugado fue ${played}.',
'info.tooFast': 'Escribiste ${typed} antes de que se jugara el siguiente carácter.',
'info.tooSlow': '¡Demasiado lento!',
'info.lostFocus': '¡Se perdió el foco!',
},
Expand Down Expand Up @@ -338,7 +342,8 @@ const translations = {
'acknowledgements.cc-by-license': 'Llicència CC-BY',
'acknowledgements.jscwlib': 'Biblioteca JavaScript per al codi Morse',
'acknowledgements.cat-icon': 'Icona de gat',
'info.incorrect': 'Incorrecte!',
'info.incorrect': 'Has escrit ${typed}, però el següent caràcter jugat ha estat ${played}!',
'info.tooFast': 'Has escrit ${typed} abans que es jugués el següent caràcter!',
'info.tooSlow': 'Massa lent!',
'info.lostFocus': "S'ha perdut el focus!",
},
Expand Down Expand Up @@ -734,7 +739,7 @@ function setLanguage(lang) {
function setInfoMessage(message) {
infoMessage = message;
const infoElement = getElement('info', HTMLElement);
infoElement.innerText = message;
infoElement.innerHTML = message;
if (message) {
infoElement.parentElement?.classList?.remove('d-none');
} else {
Expand Down Expand Up @@ -763,7 +768,7 @@ function render() {
getElement('current-session', HTMLTextAreaElement).value = copiedText;
if (infoMessage) {
const infoElement = getElement('info', HTMLElement);
infoElement.innerText = infoMessage;
infoElement.innerHTML = infoMessage;
infoElement.parentElement?.classList.remove('d-none');
}

Expand Down Expand Up @@ -990,6 +995,20 @@ function fail(sent, userInput) {
replayAfterMistake(sent?.character);
}

/**
* @param {string} character
* @return {string}
*/
function characterNameWithMorse(character) {
if (character === ' ') {
return '<code>Space</code>';
} else {
const name = character.toUpperCase();
const morse = cwPlayer.alphabet[character].replaceAll('.', '·').replaceAll('-', '−');
return `<code>${name}</code> (<code>${morse}</code>)`;
}
}

/**
* @param {KeyboardEvent} event
*/
Expand Down Expand Up @@ -1029,7 +1048,19 @@ function onKeyDown(event) {
// incorrect
// play sound, replay character, and end session
fail(sent, userInput);
setInfoMessage(t('info.incorrect'));
// display info message
if (sent) {
const template = t('info.incorrect');
setInfoMessage(evaluateTemplate(template, {
played: characterNameWithMorse(sent.character),
typed: characterNameWithMorse(userInput),
}));
} else {
const template = t('info.tooFast');
setInfoMessage(evaluateTemplate(template, {
typed: characterNameWithMorse(userInput),
}));
}
}
}

Expand Down

0 comments on commit 61102f1

Please sign in to comment.