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');
|
const searchVoiceBtn = document.getElementById('searchVoiceBtn');
|
||||||
|
|
||||||
function startSearchVoice() {
|
function startSearchVoice() {
|
||||||
|
// If already recognizing, stop it instead (toggle behavior)
|
||||||
|
if (isSearchRecognizing) {
|
||||||
|
stopSearchVoice();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!searchRecognition) {
|
if (!searchRecognition) {
|
||||||
const SR = window.SpeechRecognition || window.webkitSpeechRecognition;
|
const SR = window.SpeechRecognition || window.webkitSpeechRecognition;
|
||||||
if (!SR) {
|
if (!SR) {
|
||||||
@@ -3077,16 +3083,32 @@
|
|||||||
|
|
||||||
searchRecognition.onresult = (event) => {
|
searchRecognition.onresult = (event) => {
|
||||||
let finalTranscript = '';
|
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) {
|
if (event.results[i].isFinal) {
|
||||||
finalTranscript += event.results[i][0].transcript + ' ';
|
finalTranscript += transcript + ' ';
|
||||||
|
} else {
|
||||||
|
interimTranscript += transcript;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (finalTranscript) {
|
|
||||||
notesSearchInput.value = finalTranscript.trim();
|
// Update with interim results for real-time feedback
|
||||||
searchQuery = notesSearchInput.value;
|
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();
|
applySearch();
|
||||||
}
|
}
|
||||||
|
|
||||||
// auto-stop after 2s of silence
|
// auto-stop after 2s of silence
|
||||||
clearTimeout(searchSilenceTimer);
|
clearTimeout(searchSilenceTimer);
|
||||||
searchSilenceTimer = setTimeout(() => {
|
searchSilenceTimer = setTimeout(() => {
|
||||||
@@ -3100,7 +3122,8 @@
|
|||||||
searchVoiceBtn.classList.add('text-purple-600');
|
searchVoiceBtn.classList.add('text-purple-600');
|
||||||
};
|
};
|
||||||
|
|
||||||
searchRecognition.onerror = () => {
|
searchRecognition.onerror = (event) => {
|
||||||
|
console.error('Speech recognition error:', event.error);
|
||||||
stopSearchVoice();
|
stopSearchVoice();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user