mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-27 13:48:51 +01:00
Create SpeechSynthesis.tsx
This commit is contained in:
parent
252325dcda
commit
f9ed2ad8db
1 changed files with 41 additions and 0 deletions
41
client/src/components/Messages/SpeechSynthesis.tsx
Normal file
41
client/src/components/Messages/SpeechSynthesis.tsx
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import { useState, useEffect } from 'react';
|
||||
|
||||
function useSpeechSynthesis() {
|
||||
const [isSpeechEnabled, setIsSpeechEnabled] = useState(false);
|
||||
const [textToSpeak, setTextToSpeak] = useState('');
|
||||
|
||||
const synthesizeSpeech = (text) => {
|
||||
setTextToSpeak(text);
|
||||
};
|
||||
|
||||
const toggleSpeechSynthesis = () => {
|
||||
setIsSpeechEnabled(!isSpeechEnabled);
|
||||
console.log('Toggle Text-To-Speech', !isSpeechEnabled);
|
||||
};
|
||||
|
||||
const handleKeyDown = (event) => {
|
||||
if (event.shiftKey && event.altKey && event.key === 'P') {
|
||||
toggleSpeechSynthesis();
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
window.addEventListener('keydown', handleKeyDown);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('keydown', handleKeyDown);
|
||||
};
|
||||
}, [isSpeechEnabled]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isSpeechEnabled || !textToSpeak) return;
|
||||
|
||||
const synth = window.speechSynthesis;
|
||||
const utterance = new SpeechSynthesisUtterance(textToSpeak);
|
||||
synth.speak(utterance);
|
||||
}, [textToSpeak, isSpeechEnabled]);
|
||||
|
||||
return { synthesizeSpeech, toggleSpeechSynthesis, isSpeechEnabled };
|
||||
}
|
||||
|
||||
export default useSpeechSynthesis;
|
||||
Loading…
Add table
Add a link
Reference in a new issue