Skip to content

Commit

Permalink
fix: update lazy audio
Browse files Browse the repository at this point in the history
  • Loading branch information
ymzuiku committed Oct 14, 2023
1 parent 5f5ca73 commit ae79dca
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
49 changes: 27 additions & 22 deletions src/lib/components/speech.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@
const handleSpeech = async (e: { currentTarget: HTMLElement }, connectNext?: boolean) => {
const button = e.currentTarget;
const nowAudio = button.querySelector('audio');
if (nowAudio && !connectNext) {
if (speechCache.lastAudio === nowAudio) {
nowAudio.currentTime = 0.05;
nowAudio.pause();
speechCache.lastAudio = null;
return;
}
speechCache.lastAudio = nowAudio;
}
if (text) {
audioLoaded = {
...audioLoaded,
Expand All @@ -32,7 +22,19 @@
}).toString()}`,
};
await tick();
nowAudio?.load();
}
const nowAudio = button.querySelector('audio');
nowAudio?.load();
if (nowAudio && !connectNext) {
if (speechCache.lastAudio === nowAudio) {
nowAudio.currentTime = 0.05;
nowAudio.pause();
speechCache.lastAudio = null;
return;
}
speechCache.lastAudio = nowAudio;
}
document.querySelectorAll('audio').forEach((audio) => {
Expand All @@ -51,25 +53,28 @@
}
};
let audio: HTMLAudioElement;
let span: HTMLSpanElement;
onMount(() => {
if (connect >= 0 && audio) {
if (connect >= 0 && span) {
/* eslint-disable @typescript-eslint/no-explicit-any */
(audio as any).nowConnect = connect;
(span as any).nowConnect = connect;
// (audio as any).handleSpeech = handleSpeech;
}
});
</script>

<!-- svelte-ignore a11y-no-static-element-interactions -->
<!-- svelte-ignore a11y-click-events-have-key-events -->
<span on:click={(e) => handleSpeech(e)} class={className}>
<audio
data-text={text}
data-connect-audio={connect}
bind:this={audio}
class="pointer-events-none"
src={audioLoaded[text || '']}
/>
<span
bind:this={span}
data-text={text}
data-connect-audio={connect}
on:click={(e) => handleSpeech(e)}
class={className}
>
{#if audioLoaded[text]}
<audio class="pointer-events-none" src={audioLoaded[text || '']} />
{/if}

<slot />
</span>
24 changes: 12 additions & 12 deletions src/lib/helpers/loop-play-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,38 @@ export function loopPlayHooks() {

onMount(() => {
if (browser) {
let nextAudio: HTMLAudioElement | null;
let nextAudioSpan: HTMLAudioElement | null;
loopUpdateTimer = setInterval(() => {
if (speechCache.lastAudio?.paused) {
scrollToElement(nextAudio?.parentElement?.parentElement);
scrollToElement(nextAudioSpan?.parentElement);

if (get(speechConnect)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((speechCache.lastAudio as any).nowConnect === void 0) {
if ((speechCache.lastAudio?.parentElement as any)?.nowConnect === void 0) {
return;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const connectId = (speechCache.lastAudio as any).nowConnect + 1;
if (nextAudio?.parentElement) {
nextAudio.parentElement.style.border = 'none';
const connectId = (speechCache.lastAudio.parentElement as any).nowConnect + 1;
if (nextAudioSpan?.parentElement) {
nextAudioSpan.style.border = 'none';
}
nextAudio = document.querySelector(
nextAudioSpan = document.querySelector(
`[data-connect-audio="${connectId}"]`,
) as HTMLAudioElement;
if (nextAudio) {
if (nextAudio?.parentElement) {
nextAudio.parentElement.style.border = '1px solid #33f';
if (nextAudioSpan) {
if (nextAudioSpan) {
nextAudioSpan.style.border = '1px solid #33f';
}
const src = `/brain/audio?${new URLSearchParams({
text: nextAudio.getAttribute('data-text') || '',
text: nextAudioSpan.getAttribute('data-text') || '',
people: get(speechPeople),
learn: get(user).learn,
}).toString()}`;
if (speechCache.lastAudio.src === src) {
return;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(speechCache.lastAudio as any).nowConnect += 1;
(speechCache.lastAudio.parentElement as any).nowConnect += 1;
speechCache.lastAudio.src = src;
speechCache.lastAudio.load();
speechCache.lastAudio.playbackRate = get(speedAudio);
Expand Down
5 changes: 2 additions & 3 deletions src/routes/article/learn/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script lang="ts">
import { css } from '$lib/components/atom-css';
import Goback from '$lib/components/goback.svelte';
import LazyShow from '$lib/components/lazy-show.svelte';
import SpeechController from '$lib/components/speech-controller.svelte';
import Speech from '$lib/components/speech.svelte';
import TranslateWord from '$lib/components/translate-word.svelte';
Expand Down Expand Up @@ -62,7 +61,7 @@

<main id="setting" aria-label="setting page" class="flex flex-col p-4 min-h-full h-full gap-4">
{#each list as item, index}
<LazyShow class="flex flex-row gap-2 text-lg min-h-[60px]">
<div class="flex flex-row gap-2 text-lg min-h-[60px]">
<button class={css.miniCard} on:click={() => translateText(item.text)}>
<iconify-icon icon="heroicons-outline:translate" />
</button>
Expand All @@ -82,7 +81,7 @@
<div class="text-sm mt-2 text-gray-500">{$translateSentence[item.text]}</div>
{/if}
</div>
</LazyShow>
</div>
{/each}

<div class="h-20" />
Expand Down

0 comments on commit ae79dca

Please sign in to comment.