Skip to content

Commit

Permalink
Added: Add showPronunciation toggle buttons to FlashCards component (#6)
Browse files Browse the repository at this point in the history
* feat: Add showPronunciation option to FlashCards component

* feat: Enable showPronunciation option in FlashCards component by default

* feat: Add showPronunciation toggle buttons to FlashCards component

* feat: Add loading skeleton for show pronunciation toggle buttons
  • Loading branch information
drikusroor authored Apr 24, 2024
1 parent dba7551 commit 9e61064
Showing 1 changed file with 63 additions and 5 deletions.
68 changes: 63 additions & 5 deletions src/components/FlashCards.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
let t: (key: TranslationKeys, defaultValue?: string) => string;
let currentLanguage: LanguageCode;
let learningMode: 'from' | 'to';
let showPronunciation: boolean = true;
// QA flow related
let questionBoxClasses = '';
Expand Down Expand Up @@ -82,11 +83,26 @@
}
}
function getQuestionText(currentSentence: Sentence, { learningMode = 'from', showPronunciation = true }) {
let questionText = currentSentence.q;
if (learningMode === 'to') {
questionText = currentSentence[currentLanguage].join(' / ');
}
if (!showPronunciation) {
// Remove the pronunciation between the parentheses from the question text
const re = /\(.*?\)/;
return questionText.replace(re, '').trim();
}
return questionText;
}
function loadNextSentence() {
currentTimeout ? clearTimeout(currentTimeout) : null;
currentSentence = getNextSentence();
questionText =
learningMode === 'from' ? currentSentence.q : currentSentence[currentLanguage].join(' / ');
questionText = getQuestionText(currentSentence, { learningMode, showPronunciation });
answerInputValue = '';
feedbackText = '';
isFeedbackDisplayed = false;
Expand Down Expand Up @@ -298,9 +314,20 @@
loadNextSentence();
}
function changeShowPronunciation(event: Event) {
event.preventDefault();
const target = event.currentTarget as HTMLLinkElement;
const url = new URL(target.href);
goto(url.href, { replaceState: true });
const newShowPronunciation = url.searchParams.get('showPronunciation') as 'true' | 'false';
showPronunciation = newShowPronunciation === 'true';
loadNextSentence();
}
onMount(() => {
currentLanguage = getCurrentLanguage();
learningMode = getMode();
showPronunciation = getShowPronunciation();
sentences = shuffle(data.questions);
filteredSentences = [...sentences];
Expand All @@ -324,6 +351,10 @@
return new URLSearchParams(window.location.search).get('mode') === 'to' ? 'to' : 'from';
}
function getShowPronunciation() {
return new URLSearchParams(window.location.search).get('showPronunciation') === 'true';
}
if (localStorage.getItem('mistakes')) {
mistakes = JSON.parse(localStorage.getItem('mistakes') || '{}') as Mistakes;
}
Expand All @@ -340,7 +371,7 @@
<div class="rounded-xl bg-slate-900 p-4 mb-4 flex flex-row gap-2">
{#each languages as language, index}
<a
href="{base}?lang={language.code}&mode={learningMode}"
href="{base}?lang={language.code}&mode={learningMode}&showPronunciation={showPronunciation}"
class="text-white hover:text-pink-400 {language.code === currentLanguage
? 'underline'
: ''}"
Expand All @@ -357,7 +388,7 @@
<div class="rounded-xl bg-slate-900 p-4 mb-4 flex flex-row gap-2">
<a
id="fromLink"
href="{base}?lang={currentLanguage}&mode=from"
href="{base}?lang={currentLanguage}&mode=from&showPronunciation={showPronunciation}"
class="text-white hover:text-pink-400 {learningMode === 'from' ? 'underline' : ''}"
on:click={changeMode}
>
Expand All @@ -366,13 +397,34 @@
<span class="text-white">|</span>
<a
id="toLink"
href="{base}?lang={currentLanguage}&mode=to"
href="{base}?lang={currentLanguage}&mode=to&showPronunciation={showPronunciation}"
class="text-white hover:text-pink-400 {learningMode === 'to' ? 'underline' : ''}"
on:click={changeMode}
>
{t('toLinkText', 'To')}
</a>
</div>

<div class="rounded-xl bg-slate-900 p-4 mb-4 flex flex-row gap-2">
<a
id="showPronunciation"
href="{base}?lang={currentLanguage}&mode={learningMode}&showPronunciation=true"
class="text-white hover:text-pink-400 {showPronunciation ? 'underline' : ''}"
on:click={changeShowPronunciation}
>
{t('showPronunciationText', 'は (ha)')}
</a>
<span class="text-white">|</span>
<a
id="hidePronunciation"
href="{base}?lang={currentLanguage}&mode={learningMode}&showPronunciation=false"
class="text-white hover:text-pink-400 {!showPronunciation ? 'underline' : ''}"
on:click={changeShowPronunciation}
>
{t('hidePronunciationText', '')}
</a>
</div>

{/if}

{#if loading}
Expand All @@ -387,6 +439,12 @@
<span class="text-white">|</span>
<span class="text-white w-12 text-center">...</span>
</div>

<div class="rounded-xl bg-purple-900 animate-pulse p-4 mb-4 flex flex-row gap-2">
<span class="text-white w-12 text-center">...</span>
<span class="text-white">|</span>
<span class="text-white w-12 text-center">...</span>
</div>
{/if}
</div>

Expand Down

0 comments on commit 9e61064

Please sign in to comment.