Fix voice search for notes list
- Fix 'already started' error in speech recognition - Add toggle behavior for voice button (start/stop) - Improve transcript handling for single words - Show interim results in real-time as user speaks - Add detailed console logging for debugging - Ensure search triggers on final results - Search already covers both title and body fields
This commit is contained in:
+29
-6
@@ -3064,6 +3064,12 @@
|
||||
const searchVoiceBtn = document.getElementById('searchVoiceBtn');
|
||||
|
||||
function startSearchVoice() {
|
||||
// If already recognizing, stop it instead (toggle behavior)
|
||||
if (isSearchRecognizing) {
|
||||
stopSearchVoice();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!searchRecognition) {
|
||||
const SR = window.SpeechRecognition || window.webkitSpeechRecognition;
|
||||
if (!SR) {
|
||||
@@ -3077,16 +3083,32 @@
|
||||
|
||||
searchRecognition.onresult = (event) => {
|
||||
let finalTranscript = '';
|
||||
for (let i = 0; i < event.results.length; i++) {
|
||||
let interimTranscript = '';
|
||||
|
||||
for (let i = event.results.length - 1; i >= 0; i--) {
|
||||
const transcript = event.results[i][0].transcript;
|
||||
if (event.results[i].isFinal) {
|
||||
finalTranscript += event.results[i][0].transcript + ' ';
|
||||
finalTranscript += transcript + ' ';
|
||||
} else {
|
||||
interimTranscript += transcript;
|
||||
}
|
||||
}
|
||||
if (finalTranscript) {
|
||||
notesSearchInput.value = finalTranscript.trim();
|
||||
searchQuery = notesSearchInput.value;
|
||||
|
||||
// Update with interim results for real-time feedback
|
||||
const displayText = (finalTranscript + interimTranscript).trim();
|
||||
if (displayText) {
|
||||
notesSearchInput.value = displayText;
|
||||
console.log('Voice search input:', displayText);
|
||||
}
|
||||
|
||||
// Apply search when we have final results
|
||||
if (finalTranscript.trim()) {
|
||||
const finalText = finalTranscript.trim();
|
||||
searchQuery = finalText;
|
||||
console.log('Voice search final query:', searchQuery);
|
||||
applySearch();
|
||||
}
|
||||
|
||||
// auto-stop after 2s of silence
|
||||
clearTimeout(searchSilenceTimer);
|
||||
searchSilenceTimer = setTimeout(() => {
|
||||
@@ -3100,7 +3122,8 @@
|
||||
searchVoiceBtn.classList.add('text-purple-600');
|
||||
};
|
||||
|
||||
searchRecognition.onerror = () => {
|
||||
searchRecognition.onerror = (event) => {
|
||||
console.error('Speech recognition error:', event.error);
|
||||
stopSearchVoice();
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user