Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ No Connection Modal Dialog component.
import { t } from '$lib/data/stores';
import Modal from './Modal.svelte';

const modalId = 'noConnectionModal';
const modalId = 'audioAlertModal';
let modal: Modal | undefined = $state(undefined);

export function showModal() {
let messageKey = $state('');

export function showModal(_messageKey: string) {
messageKey = _messageKey;
modal?.showModal();
}
</script>
Expand All @@ -20,7 +23,7 @@ No Connection Modal Dialog component.
<div class="message-body" id="message-body">
<div class="message-header"></div>
<div class="message-text">
{$t['Audio_Download_Connect']}
{$t[messageKey]}
</div>
</div>

Expand Down
17 changes: 3 additions & 14 deletions src/lib/components/AudioDownloadModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Audio Download Modal Dialog component.
<script lang="ts">
import { updateAudioPlayer } from '$lib/data/audio';
import { addAudioClip } from '$lib/data/audioclipsDB';
import { refs, t, userSettings } from '$lib/data/stores';
import { modal as alert, ModalType, refs, t, userSettings } from '$lib/data/stores';
import { CheckboxIcon, CheckboxOutlineIcon } from '$lib/icons';
import { tick } from 'svelte';
import Modal from './Modal.svelte';
Expand Down Expand Up @@ -56,10 +56,8 @@ Audio Download Modal Dialog component.
async function finishModal() {
const addedAudioClip = await downloadAudio(audioUrl);
if (!addedAudioClip && !abortController?.signal.aborted) {
error = 'Audio clip could not be downloaded';
setTimeout(() => {
error = '';
}, 2000);
modal?.close();
alert.open(ModalType.AudioAlert, 'Audio_Download_Error');
return false;
}
}
Expand Down Expand Up @@ -117,15 +115,6 @@ Audio Download Modal Dialog component.
</div>
</div>
</Modal>
{#if error}
<div class="absolute inset-0 flex items-center justify-center z-50 pointer-events-none">
<div
class="dy-modal-box overflow-y-visible relative opacity-100 text-red-500 text-center pointer-events-auto"
>
{error}
</div>
</div>
{/if}
{#if downloadProgress > 0}
<div class="fixed inset-0 bg-black/50 backdrop-blur-xs flex items-center justify-center z-50">
<div class="message" id="container">
Expand Down
10 changes: 3 additions & 7 deletions src/lib/components/DownloadSelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A component for verse-on-image providing a dropdown where you can choose to down
-->

<script lang="ts">
import { convertStyle, s, t, themeColors } from '$lib/data/stores';
import { getPositioningCSS, t, themeColors } from '$lib/data/stores';
import { ImageIcon, VideoIcon } from '$lib/icons';
import Modal from './Modal.svelte';

Expand All @@ -15,18 +15,14 @@ A component for verse-on-image providing a dropdown where you can choose to down
export function showModal() {
modalThis.showModal();
}
const positioningCSS = $derived(
'position:absolute; top:' +
(Number(vertOffset.replace('rem', '')) + 1) +
'rem; inset-inline-end:1rem; width:auto;'
);
const positioningCSS = $derived(getPositioningCSS(vertOffset, 'top'));
</script>

<!-- svelte-ignore a11y_consider_explicit_label -->
<Modal
bind:this={modalThis}
id={modalId}
styling="background-color:{$themeColors['PopupBackgroundColor']}; {positioningCSS}"
styling="background-color:{$themeColors['PopupBackgroundColor']}; width:auto; {positioningCSS}"
>
<div class="grid gap-2 m-2">
<button
Expand Down
4 changes: 4 additions & 0 deletions src/lib/components/Modal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ See https://daisyui.com/components/modal/#modal-that-closes-when-clicked-outside
export function showModal() {
dialog?.showModal();
}

export function close() {
dialog?.close();
}
</script>

<dialog
Expand Down
9 changes: 1 addition & 8 deletions src/lib/components/PlanStopDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Plan Stop Modal Dialog component.
import { resolve } from '$lib/utils/paths';
import Modal from './Modal.svelte';

let { planId = $bindable(undefined), vertOffset = '1rem' } = $props();
let { planId = $bindable(undefined) } = $props();

const modalId = 'planStopDialog';
let modal: Modal | undefined = $state(undefined);
Expand All @@ -31,13 +31,6 @@ Plan Stop Modal Dialog component.
goto(resolve(`/plans`));
});
}

//The positioningCSS positions the modal 1rem below the navbar and 1rem from the right edge of the screen (on mobile it will be centered)
const positioningCSS = $derived(
'position:absolute; top:' +
(Number(vertOffset.replace('rem', '')) + 1) +
'rem; inset-inline-end:1rem;'
);
</script>

<Modal bind:this={modal} id={modalId}>
Expand Down
206 changes: 206 additions & 0 deletions src/lib/components/ShareSelector.svelte
Comment thread
TheNonPirate marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
<!--
@component
A component providing a dropdown where you can choose to download audio or video for selected text
-->

<script lang="ts">
import { scriptureConfig } from '$assets/config';
import { getBook, logShareContent } from '$lib/data/analytics';
import { getAudioSourceInfo } from '$lib/data/audio';
import { shareAudio, shareText } from '$lib/data/share';
import {
convertStyle,
getPositioningCSS,
modal,
ModalType,
refs,
s,
selectedVerses,
t
} from '$lib/data/stores';
import { AudioIcon } from '$lib/icons';
import FormatAlignLeftIcon from '$lib/icons/image/FormatAlignLeftIcon.svelte';
import {
AudioBufferSource,
BufferTarget,
canEncodeAudio,
Mp4OutputFormat,
Output,
WavOutputFormat,
WebMOutputFormat
} from 'mediabunny';
import type { AudioEncodingConfig } from 'mediabunny';
import Modal from './Modal.svelte';

let { vertOffset = '2rem' } = $props();
async function shareSelectedText() {
const book = $selectedVerses[0].book;
const reference = selectedVerses.getCompositeReference();
const text = await selectedVerses.getCompositeText();
const bookCol = $selectedVerses[0].collection;
const fullBook = getBook({ collection: bookCol, book: book });
const bookAbbrev = fullBook?.abbreviation ?? fullBook?.name;
const copyShareMessage = scriptureConfig.bookCollections?.find(
(x) => x.id === bookCol
)?.copyShareMessage;
shareText(
scriptureConfig.name ?? '',
text + '\n' + reference + (copyShareMessage ? '\n' + copyShareMessage : ''),
book + '.txt'
);
logShareContent('Text', bookCol, bookAbbrev ?? '', reference);
}
async function shareAudioFile() {
const reference = selectedVerses.getCompositeReference();
const audioCtx = new AudioContext();
try {
const audioConfig: AudioEncodingConfig = await pickSupportedAudioConfig();
const outputFormat =
audioConfig.codec === 'aac'
? new Mp4OutputFormat()
: audioConfig.codec === 'opus'
? new WebMOutputFormat()
: new WavOutputFormat();
const output = new Output({
format: outputFormat,
target: new BufferTarget()
});

const audioSourceInfo = await getAudioSourceInfo({
collection: $refs.collection,
book: $refs.book,
chapter: $refs.chapter
});
if (!audioSourceInfo?.source) {
throw new Error('No audio source available for this chapter');
}

const audioSource = new AudioBufferSource(audioConfig);
output.addAudioTrack(audioSource);
await output.start();

const audioBlob = await fetch(audioSourceInfo?.source).then((r) => r.blob());
const audioBuffer = await audioCtx.decodeAudioData(await audioBlob.arrayBuffer());

const sampleRate = audioBuffer.sampleRate;

for (let i = 0; i < $selectedVerses.length; i++) {
let startFrame = 0;
let endFrame = 0;
let foundVerseTiming = false;
for (var j = 0; j < (audioSourceInfo?.timing?.length || 0); j++) {
const timing = audioSourceInfo?.timing?.[j];
const verse = timing?.tag?.replace(/\D/g, '');
if (verse === $selectedVerses[i].verse) {
if (!foundVerseTiming) {
foundVerseTiming = true;
startFrame = Math.floor((timing?.starttime || 0) * sampleRate);
endFrame = Math.floor((timing?.endtime || 0) * sampleRate);
} else {
endFrame = Math.floor((timing?.endtime || 0) * sampleRate);
}
}
}
if (!foundVerseTiming || endFrame <= startFrame) {
console.warn(`No timing found for verse ${$selectedVerses[i].verse}, skipping`);
continue;
}
const trimmedBuffer = audioCtx.createBuffer(
audioBuffer.numberOfChannels,
endFrame - startFrame,
sampleRate
);

for (let ch = 0; ch < audioBuffer.numberOfChannels; ch++) {
const src = audioBuffer.getChannelData(ch);
const dst = trimmedBuffer.getChannelData(ch);
dst.set(src.slice(startFrame, endFrame));
}

await audioSource.add(trimmedBuffer);
}
Comment thread
TheNonPirate marked this conversation as resolved.
await output.finalize();

await shareAudio(
reference,
await selectedVerses.getCompositeText(),
reference.replace(/[\\/:*?"<>|]/g, '_') + outputFormat.fileExtension,
new Blob([output.target.buffer as BlobPart], {
type: outputFormat.mimeType
}),
outputFormat.mimeType
);
Comment thread
TheNonPirate marked this conversation as resolved.
} catch (error) {
modal.open(ModalType.AudioAlert, 'Audio_Download_Error');
console.error('Error generating audio export:', error);
} finally {
await audioCtx?.close();
}
Comment thread
FyreByrd marked this conversation as resolved.
}
async function pickSupportedAudioConfig() {
const candidates: AudioEncodingConfig[] = [
{ codec: 'aac', bitrate: 128000 },
{ codec: 'aac', bitrate: 96000 },
{ codec: 'aac', bitrate: 64000 },
{
codec: 'opus',
bitrate: 96000
},
{ codec: 'pcm-f32' },
{ codec: 'pcm-s24' },
{ codec: 'pcm-s16' }
];

for (const cfg of candidates) {
if (await canEncodeAudio(cfg.codec, cfg)) {
return cfg;
}
}

throw new Error('No supported audio configuration found.');
} //This is used to determine a supported audio configuration. It first tries AAC, but then falls back to opus if AAC isn't supported. This is a duplicate of the function with the same name in VerseOnImage.svelte, so maybe it should be moved to somewhere that exports it for any place that needs it to use it?
let modalId = 'shareSelector';
let modalThis: Modal;
export function showModal(shareTextOnly: boolean) {
if (shareTextOnly) {
shareSelectedText();
} else {
modalThis.showModal();
}
}
const positioningCSS = $derived(getPositioningCSS(vertOffset, 'bottom'));
const iconColor = $derived($s?.['ui.bar.audio.hint.text']?.['color'] || 'black');
</script>

<!-- svelte-ignore a11y_consider_explicit_label -->
<Modal
bind:this={modalThis}
id={modalId}
styling="box-shadow:none; padding:0; width:auto; {positioningCSS}"
>
Comment thread
TheNonPirate marked this conversation as resolved.
<div class="grid message" id="container">
<button
class="dy-btn flex items-center justify-center rounded-none border-transparent"
style={convertStyle($s?.['ui.bar.audio.hint.text'])}
onclick={() => shareSelectedText()}
>
<FormatAlignLeftIcon color={iconColor} />
{$t['Share_Text']}
</button>
<button
class="dy-btn flex items-center justify-center rounded-none border-transparent"
style={convertStyle($s?.['ui.bar.audio.hint.text'])}
onclick={() => shareAudioFile()}
>
<AudioIcon.Volume color={iconColor} />
{$t['Share_Audio']}
</button>
<!--<button

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this commented out?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's commented out because video download has not yet been implemented, but that is where the button would go if it was implemented.

class="dy-btn flex items-center justify-center rounded-none"
onclick={() => downloadVideo()}
>
<VideoIcon />
{$t['Share_Video']}
</button>-->
</div>
</Modal>
7 changes: 2 additions & 5 deletions src/lib/components/TextAppearanceSelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The navbar component. We have sliders that update reactively to both font size a
contentsFontSize,
currentFont,
fontChoices,
getPositioningCSS,
language,
languages,
modal,
Expand Down Expand Up @@ -68,11 +69,7 @@ The navbar component. We have sliders that update reactively to both font size a

const contentsMode = $derived(options?.contentsMode ?? false);
//The positioningCSS positions the modal 1rem below the navbar and 1rem from the right edge of the screen (on mobile it will be centered)
const positioningCSS = $derived(
'position:absolute; top:' +
(Number(vertOffset.replace('rem', '')) + 1) +
'rem; inset-inline-end:1rem;'
);
const positioningCSS = $derived(getPositioningCSS(vertOffset, 'top'));
const barColor = $derived($themeColors['SliderBarColor']);
const progressColor = $derived($themeColors['SliderProgressColor']);
const _showFontSize = showFontSize();
Expand Down
Loading