feat(ai): refine voice input layout and cancellation
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
<p
|
||||
class="voice-placeholder"
|
||||
:class="{ hidden: voiceState !== 'idle' }"
|
||||
v-text="'长按下方输入框开始语音输入'"
|
||||
v-text="'点击语音按钮,按住下方按钮说话;移出按钮后松开取消'"
|
||||
/>
|
||||
<ai-chat-composer
|
||||
v-model="draft"
|
||||
@@ -14,7 +14,6 @@
|
||||
@send="sendMessage"
|
||||
@voice-start="onVoiceStart"
|
||||
@voice-stop="onVoiceStop"
|
||||
@voice-keep="onVoiceKeep"
|
||||
@voice-cancel="onVoiceCancel"
|
||||
/>
|
||||
|
||||
@@ -87,16 +86,15 @@ let recognition: SpeechRecognitionLike | undefined;
|
||||
let finalText = "";
|
||||
let mockStepTimer: number | undefined;
|
||||
let mockFinishTimer: number | undefined;
|
||||
let sendOnFinish = true;
|
||||
|
||||
const hint = computed(() => {
|
||||
if (mockEnabled.value) {
|
||||
return "模拟模式:按住输入框说话,松手发送;上滑到按钮可选择取消或保留。";
|
||||
return "模拟模式:按住按钮说话,松手发送;移出按钮后松开取消。";
|
||||
}
|
||||
if (!supported.value) {
|
||||
return "当前浏览器不支持语音识别,已切换为模拟。";
|
||||
}
|
||||
return "长按输入框说话,松手发送;上滑到「取消发送」或「保留识别」按钮释放。";
|
||||
return "按住按钮说话,识别文本显示在整个控件上方;松手发送,移出按钮后松开取消。";
|
||||
});
|
||||
|
||||
/** 获取浏览器实现;Chrome 与 Edge 需要 webkit 前缀。 */
|
||||
@@ -134,7 +132,6 @@ function sendMessage(message: string): void {
|
||||
function onVoiceStart(): void {
|
||||
voiceText.value = "";
|
||||
finalText = "";
|
||||
sendOnFinish = true;
|
||||
if (mockEnabled.value || !supported.value) {
|
||||
startMock();
|
||||
return;
|
||||
@@ -162,12 +159,6 @@ function onVoiceStart(): void {
|
||||
}
|
||||
|
||||
function onVoiceStop(): void {
|
||||
sendOnFinish = true;
|
||||
stopVoice();
|
||||
}
|
||||
|
||||
function onVoiceKeep(): void {
|
||||
sendOnFinish = false;
|
||||
stopVoice();
|
||||
}
|
||||
|
||||
@@ -197,7 +188,7 @@ function onVoiceCancel(): void {
|
||||
resetVoice();
|
||||
}
|
||||
|
||||
/** 实时拼接最终结果与临时结果,组件会同步到输入框。 */
|
||||
/** 实时拼接最终结果与临时结果,组件在按住说话区上方展示。 */
|
||||
function handleResult(event: SpeechResultEvent): void {
|
||||
let interim = "";
|
||||
for (let index = event.resultIndex; index < event.results.length; index += 1) {
|
||||
@@ -236,15 +227,7 @@ function handleEnd(): void {
|
||||
|
||||
function finishVoice(): void {
|
||||
const message = voiceText.value.trim();
|
||||
const shouldSend = sendOnFinish;
|
||||
resetVoice();
|
||||
if (!shouldSend) {
|
||||
if (message) {
|
||||
draft.value = message;
|
||||
}
|
||||
push("已保留识别结果,可继续编辑");
|
||||
return;
|
||||
}
|
||||
sendMessage(message);
|
||||
}
|
||||
|
||||
@@ -295,7 +278,6 @@ function resetVoice(): void {
|
||||
voiceState.value = "idle";
|
||||
voiceText.value = "";
|
||||
finalText = "";
|
||||
sendOnFinish = true;
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
|
||||
Reference in New Issue
Block a user