Loheide-eu-mitarbeiterplatt.../resources/js/components/ReadAloud.vue
Friederich Loheide 856a5c30af
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
Vorlesen und Leichte Sprache mit KI-Vorschlag (0.34.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 13:24:56 +00:00

111 lines
3.4 KiB
Vue
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<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>