Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 9m24s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 9m15s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 9m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 9m21s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 10m33s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m5s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 8m10s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m8s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m23s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
111 lines
3.4 KiB
Vue
111 lines
3.4 KiB
Vue
<script setup>
|
||
import { onBeforeUnmount, onMounted, ref } from 'vue';
|
||
import { Pause, Play, Square, Volume2 } from '@lucide/vue';
|
||
import { Button } from '@/components/ui/button';
|
||
|
||
/**
|
||
* „Vorlesen“ mit der Sprachausgabe des Browsers bzw. Geräts – ohne Übertragung an Dritte.
|
||
* Liest den Text des übergebenen Elements (oder eine Funktion, die Text liefert).
|
||
*/
|
||
const props = defineProps({
|
||
/** Element oder () => string */
|
||
source: { type: [Object, Function], default: null },
|
||
label: { type: String, default: 'Vorlesen' },
|
||
});
|
||
|
||
const supported = ref(false);
|
||
const state = ref('idle'); // idle | playing | paused
|
||
let queue = [];
|
||
|
||
onMounted(() => {
|
||
supported.value = typeof window !== 'undefined' && 'speechSynthesis' in window && 'SpeechSynthesisUtterance' in window;
|
||
});
|
||
onBeforeUnmount(() => stop());
|
||
|
||
function text() {
|
||
const source = typeof props.source === 'function' ? props.source() : props.source;
|
||
|
||
if (typeof source === 'string') return source;
|
||
if (!source) return '';
|
||
|
||
// Nur sichtbarer Text, Absätze als Pausen
|
||
return (source.$el ?? source).innerText ?? '';
|
||
}
|
||
|
||
function voice() {
|
||
const voices = window.speechSynthesis.getVoices();
|
||
|
||
return voices.find((v) => v.lang === 'de-DE' && v.localService) ?? voices.find((v) => v.lang?.startsWith('de')) ?? null;
|
||
}
|
||
|
||
// Manche Browser brechen lange Texte ab – daher in Sätze bzw. kurze Abschnitte teilen
|
||
function chunks(value) {
|
||
return value
|
||
.split(/\n+/)
|
||
.flatMap((paragraph) => paragraph.match(/[^.!?…:]+[.!?…:]*\s*/g) ?? [paragraph])
|
||
.map((part) => part.trim())
|
||
.filter(Boolean)
|
||
.reduce((list, part) => {
|
||
const last = list[list.length - 1];
|
||
last && last.length + part.length < 220 ? (list[list.length - 1] = `${last} ${part}`) : list.push(part);
|
||
|
||
return list;
|
||
}, []);
|
||
}
|
||
|
||
function speakNext() {
|
||
const next = queue.shift();
|
||
|
||
if (!next) {
|
||
state.value = 'idle';
|
||
|
||
return;
|
||
}
|
||
|
||
const utterance = new SpeechSynthesisUtterance(next);
|
||
utterance.lang = 'de-DE';
|
||
utterance.voice = voice();
|
||
utterance.rate = 0.95;
|
||
utterance.onend = () => state.value === 'playing' && speakNext();
|
||
utterance.onerror = () => (state.value = 'idle');
|
||
window.speechSynthesis.speak(utterance);
|
||
}
|
||
|
||
function play() {
|
||
if (state.value === 'paused') {
|
||
window.speechSynthesis.resume();
|
||
state.value = 'playing';
|
||
|
||
return;
|
||
}
|
||
|
||
window.speechSynthesis.cancel();
|
||
queue = chunks(text());
|
||
state.value = 'playing';
|
||
speakNext();
|
||
}
|
||
|
||
function pause() {
|
||
window.speechSynthesis.pause();
|
||
state.value = 'paused';
|
||
}
|
||
|
||
function stop() {
|
||
queue = [];
|
||
state.value = 'idle';
|
||
|
||
if (supported.value) window.speechSynthesis.cancel();
|
||
}
|
||
|
||
defineExpose({ stop });
|
||
</script>
|
||
|
||
<template>
|
||
<div v-if="supported" class="inline-flex items-center gap-1" role="group" aria-label="Vorlesen">
|
||
<Button v-if="state !== 'playing'" type="button" variant="outline" size="sm" @click="play">
|
||
<component :is="state === 'paused' ? Play : Volume2" /> {{ state === 'paused' ? 'Weiter' : label }}
|
||
</Button>
|
||
<Button v-else type="button" variant="outline" size="sm" @click="pause"><Pause /> Pause</Button>
|
||
<Button v-if="state !== 'idle'" type="button" variant="ghost" size="icon" class="size-8" aria-label="Vorlesen beenden" @click="stop"><Square class="size-3.5" /></Button>
|
||
</div>
|
||
</template>
|