feat(ai): refine hold-to-talk voice actions
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<template>
|
||||
<div class="composer">
|
||||
<div class="composer" :class="{ 'voice-active': voiceActive }">
|
||||
<div v-if="props.lastError" class="error-bar">
|
||||
<slot name="error" :error="props.lastError" :can-retry="props.canRetry">
|
||||
<span class="error-text" v-text="props.lastError" />
|
||||
@@ -13,6 +13,24 @@
|
||||
</slot>
|
||||
</div>
|
||||
<t-form class="form glass-white" @submit="submitMessage">
|
||||
<div v-if="voiceActionsVisible" class="voice-actions" role="group" aria-label="语音操作">
|
||||
<div class="voice-status" aria-live="polite">
|
||||
<div class="voice-wave" aria-hidden="true">
|
||||
<span v-for="index in 5" :key="index" :style="`--i: ${index}`" />
|
||||
</div>
|
||||
<span class="voice-status-text" v-text="voiceStatusText" />
|
||||
</div>
|
||||
<div class="voice-buttons">
|
||||
<div ref="cancelActionRef" class="voice-action cancel" :class="{ active: willCancel }">
|
||||
<t-icon name="close" size="small" />
|
||||
<span v-text="props.voiceCancelText" />
|
||||
</div>
|
||||
<div ref="keepActionRef" class="voice-action keep" :class="{ active: willKeep }">
|
||||
<t-icon name="check" size="small" />
|
||||
<span v-text="props.voiceKeepText" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="input-wrap">
|
||||
<t-textarea
|
||||
ref="textareaRef"
|
||||
@@ -34,13 +52,6 @@
|
||||
@pointercancel="handleVoicePointerCancel"
|
||||
@contextmenu.prevent
|
||||
/>
|
||||
<div v-if="voicePanelVisible" class="voice-panel" :class="{ cancel: willCancel }">
|
||||
<div class="voice-wave" aria-hidden="true">
|
||||
<span v-for="index in 5" :key="index" :style="`--i: ${index}`" />
|
||||
</div>
|
||||
<p class="voice-text" v-text="voicePanelText" />
|
||||
<p class="voice-hint" v-text="willCancel ? props.voiceCancelHint : props.voiceSendHint" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<div class="tools">
|
||||
@@ -101,7 +112,7 @@
|
||||
size="extra-small"
|
||||
theme="primary"
|
||||
:disabled="props.disabled"
|
||||
:content="props.sendText"
|
||||
:content="sendButtonText"
|
||||
/>
|
||||
</slot>
|
||||
</div>
|
||||
@@ -110,7 +121,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, ref, useTemplateRef } from "vue";
|
||||
import { computed, onBeforeUnmount, ref, useTemplateRef, watch } from "vue";
|
||||
|
||||
defineOptions({
|
||||
name: "AiChatComposer"
|
||||
@@ -132,10 +143,16 @@ export interface Props {
|
||||
voiceState?: "idle" | "listening" | "processing";
|
||||
/** 语音中间结果 */
|
||||
voiceText?: string;
|
||||
/** 松开发送提示 */
|
||||
/** 松开发送提示,同时作为按住时的发送按钮文案 */
|
||||
voiceSendHint?: string;
|
||||
/** 上滑取消提示 */
|
||||
/** 上滑到取消按钮时的提示 */
|
||||
voiceCancelHint?: string;
|
||||
/** 上滑到保留按钮时的提示 */
|
||||
voiceKeepHint?: string;
|
||||
/** 取消发送按钮文案 */
|
||||
voiceCancelText?: string;
|
||||
/** 保留识别按钮文案 */
|
||||
voiceKeepText?: string;
|
||||
/** 正在聆听提示 */
|
||||
voiceListeningText?: string;
|
||||
/** 正在识别提示 */
|
||||
@@ -156,6 +173,9 @@ const props = withDefaults(defineProps<Props>(), {
|
||||
voiceText: "",
|
||||
voiceSendHint: "松开发送",
|
||||
voiceCancelHint: "松开取消",
|
||||
voiceKeepHint: "松开保留",
|
||||
voiceCancelText: "取消发送",
|
||||
voiceKeepText: "保留识别",
|
||||
voiceListeningText: "正在听你说…",
|
||||
voiceProcessingText: "正在识别…"
|
||||
});
|
||||
@@ -169,42 +189,73 @@ const emit = defineEmits<{
|
||||
"add-attachment": [];
|
||||
"voice-start": [];
|
||||
"voice-stop": [];
|
||||
"voice-keep": [];
|
||||
"voice-cancel": [];
|
||||
}>();
|
||||
|
||||
/** 长按触发语音的阈值,短按仍然聚焦输入 */
|
||||
const VOICE_HOLD_DELAY = 300;
|
||||
/** 上滑取消的位移阈值 */
|
||||
const VOICE_CANCEL_DISTANCE = 48;
|
||||
/** 未触发语音时,手指移动超过该距离视为滑动 */
|
||||
const VOICE_MOVE_TOLERANCE = 12;
|
||||
/** 按钮命中区域外扩,避免边缘难以对准;需小于按钮间距,防止两个按钮重叠 */
|
||||
const VOICE_ACTION_HIT_PADDING = 4;
|
||||
|
||||
type VoiceAction = "cancel" | "keep" | "send";
|
||||
|
||||
const textareaRef = useTemplateRef<{ focus: () => void }>("textareaRef");
|
||||
const cancelActionRef = useTemplateRef<HTMLElement>("cancelActionRef");
|
||||
const keepActionRef = useTemplateRef<HTMLElement>("keepActionRef");
|
||||
const inputFocused = ref(false);
|
||||
const voicePressing = ref(false);
|
||||
const voiceTriggered = ref(false);
|
||||
const willCancel = ref(false);
|
||||
const willKeep = ref(false);
|
||||
const voiceSessionActive = ref(false);
|
||||
const voiceKeepPending = ref(false);
|
||||
let voiceTimer: number | undefined;
|
||||
let voiceStartY = 0;
|
||||
let voicePointerId: number | undefined;
|
||||
|
||||
/** 输入框为空且未聚焦时才接管长按,避免影响选择和编辑 */
|
||||
/** 输入框为空且未聚焦时才接管长按;识别中保持覆盖层以持续接收指针事件 */
|
||||
const voiceHitVisible = computed(() => props.voiceEnabled
|
||||
&& !props.disabled
|
||||
&& !props.isGenerating
|
||||
&& !draft.value.trim()
|
||||
&& !inputFocused.value
|
||||
&& (props.voiceState === "idle" || voicePressing.value));
|
||||
&& (voicePressing.value || (!draft.value.trim() && !inputFocused.value && props.voiceState === "idle")));
|
||||
|
||||
const voicePanelVisible = computed(() => props.voiceEnabled && (voiceTriggered.value || props.voiceState !== "idle"));
|
||||
const voiceActive = computed(() => voicePressing.value && voiceTriggered.value);
|
||||
|
||||
const voicePanelText = computed(() => {
|
||||
if (props.voiceText) {
|
||||
return props.voiceText;
|
||||
const voiceActionsVisible = computed(() => voiceActive.value);
|
||||
|
||||
const voiceStatusText = computed(() => {
|
||||
if (willCancel.value) {
|
||||
return props.voiceCancelHint;
|
||||
}
|
||||
if (willKeep.value) {
|
||||
return props.voiceKeepHint;
|
||||
}
|
||||
return props.voiceState === "processing" ? props.voiceProcessingText : props.voiceListeningText;
|
||||
});
|
||||
|
||||
const sendButtonText = computed(() => voiceActive.value ? props.voiceSendHint : props.sendText);
|
||||
|
||||
/** ASR 结果直接写进输入框;取消后停止同步,保留后忽略调用方清空 voiceText 的动作 */
|
||||
watch(() => props.voiceText, (text) => {
|
||||
if (!voiceSessionActive.value) {
|
||||
return;
|
||||
}
|
||||
if (voiceKeepPending.value && !text) {
|
||||
return;
|
||||
}
|
||||
draft.value = text;
|
||||
});
|
||||
|
||||
watch(() => props.voiceState, (state) => {
|
||||
if (state === "idle" && voiceSessionActive.value) {
|
||||
voiceSessionActive.value = false;
|
||||
voiceKeepPending.value = false;
|
||||
}
|
||||
});
|
||||
|
||||
function startNewConversation(): void {
|
||||
if (props.disabled || props.isGenerating) {
|
||||
return;
|
||||
@@ -239,6 +290,9 @@ function handleVoicePointerDown(event: PointerEvent): void {
|
||||
voicePressing.value = true;
|
||||
voiceTriggered.value = false;
|
||||
willCancel.value = false;
|
||||
willKeep.value = false;
|
||||
voiceSessionActive.value = false;
|
||||
voiceKeepPending.value = false;
|
||||
voiceStartY = event.clientY;
|
||||
voicePointerId = event.pointerId;
|
||||
event.preventDefault();
|
||||
@@ -252,6 +306,9 @@ function handleVoicePointerDown(event: PointerEvent): void {
|
||||
return;
|
||||
}
|
||||
voiceTriggered.value = true;
|
||||
voiceSessionActive.value = true;
|
||||
voiceKeepPending.value = false;
|
||||
draft.value = "";
|
||||
navigator.vibrate?.(15);
|
||||
emit("voice-start");
|
||||
}, VOICE_HOLD_DELAY);
|
||||
@@ -267,7 +324,30 @@ function handleVoicePointerMove(event: PointerEvent): void {
|
||||
}
|
||||
return;
|
||||
}
|
||||
willCancel.value = voiceStartY - event.clientY > VOICE_CANCEL_DISTANCE;
|
||||
const action = resolveVoiceAction(event);
|
||||
willCancel.value = action === "cancel";
|
||||
willKeep.value = action === "keep";
|
||||
}
|
||||
|
||||
function resolveVoiceAction(event: PointerEvent): VoiceAction {
|
||||
if (isPointInsideAction(event, cancelActionRef.value)) {
|
||||
return "cancel";
|
||||
}
|
||||
if (isPointInsideAction(event, keepActionRef.value)) {
|
||||
return "keep";
|
||||
}
|
||||
return "send";
|
||||
}
|
||||
|
||||
function isPointInsideAction(event: PointerEvent, element: HTMLElement | null): boolean {
|
||||
if (!element) {
|
||||
return false;
|
||||
}
|
||||
const rect = element.getBoundingClientRect();
|
||||
return event.clientX >= rect.left - VOICE_ACTION_HIT_PADDING
|
||||
&& event.clientX <= rect.right + VOICE_ACTION_HIT_PADDING
|
||||
&& event.clientY >= rect.top - VOICE_ACTION_HIT_PADDING
|
||||
&& event.clientY <= rect.bottom + VOICE_ACTION_HIT_PADDING;
|
||||
}
|
||||
|
||||
function handleVoicePointerUp(event: PointerEvent): void {
|
||||
@@ -275,14 +355,19 @@ function handleVoicePointerUp(event: PointerEvent): void {
|
||||
return;
|
||||
}
|
||||
const triggered = voiceTriggered.value;
|
||||
const cancel = willCancel.value;
|
||||
const action = resolveVoiceAction(event);
|
||||
resetVoicePress();
|
||||
if (!triggered) {
|
||||
textareaRef.value?.focus();
|
||||
return;
|
||||
}
|
||||
if (cancel) {
|
||||
emit("voice-cancel");
|
||||
if (action === "cancel") {
|
||||
cancelVoice();
|
||||
return;
|
||||
}
|
||||
if (action === "keep") {
|
||||
voiceKeepPending.value = true;
|
||||
emit("voice-keep");
|
||||
return;
|
||||
}
|
||||
emit("voice-stop");
|
||||
@@ -295,10 +380,17 @@ function handleVoicePointerCancel(): void {
|
||||
const triggered = voiceTriggered.value;
|
||||
resetVoicePress();
|
||||
if (triggered) {
|
||||
emit("voice-cancel");
|
||||
cancelVoice();
|
||||
}
|
||||
}
|
||||
|
||||
function cancelVoice(): void {
|
||||
voiceSessionActive.value = false;
|
||||
voiceKeepPending.value = false;
|
||||
draft.value = "";
|
||||
emit("voice-cancel");
|
||||
}
|
||||
|
||||
function resetVoicePress(): void {
|
||||
if (voiceTimer !== undefined) {
|
||||
window.clearTimeout(voiceTimer);
|
||||
@@ -307,6 +399,7 @@ function resetVoicePress(): void {
|
||||
voicePressing.value = false;
|
||||
voiceTriggered.value = false;
|
||||
willCancel.value = false;
|
||||
willKeep.value = false;
|
||||
voicePointerId = undefined;
|
||||
}
|
||||
|
||||
@@ -352,7 +445,9 @@ onBeforeUnmount(() => {
|
||||
margin: .5rem 0 0;
|
||||
display: flex;
|
||||
padding: .5rem;
|
||||
position: relative;
|
||||
box-shadow: 0 .5rem 1.5rem rgba(15, 23, 42, .12);
|
||||
transition: border-color .2s, box-shadow .2s;
|
||||
align-items: stretch;
|
||||
border-radius: 1.25rem;
|
||||
flex-direction: column;
|
||||
@@ -378,29 +473,44 @@ onBeforeUnmount(() => {
|
||||
-webkit-tap-highlight-color: transparent;
|
||||
}
|
||||
|
||||
.voice-panel {
|
||||
inset: 0;
|
||||
gap: .25rem;
|
||||
.voice-actions {
|
||||
gap: .375rem;
|
||||
left: 50%;
|
||||
width: 100%;
|
||||
z-index: 3;
|
||||
display: flex;
|
||||
z-index: 2;
|
||||
padding: .5rem .75rem;
|
||||
padding: .5rem;
|
||||
position: absolute;
|
||||
box-sizing: border-box;
|
||||
bottom: calc(100% + .5rem);
|
||||
user-select: none;
|
||||
border-radius: .75rem;
|
||||
pointer-events: none;
|
||||
border: 1px solid var(--td-brand-color);
|
||||
background: var(--td-bg-color-container);
|
||||
align-items: center;
|
||||
box-shadow: 0 .5rem 1.5rem rgba(15, 23, 42, .16);
|
||||
animation: voice-actions-in .16s ease-out;
|
||||
border-radius: 1rem;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
transform: translateX(-50%);
|
||||
|
||||
.voice-status {
|
||||
gap: .375rem;
|
||||
display: flex;
|
||||
min-height: 1.25rem;
|
||||
color: var(--td-text-color-secondary);
|
||||
font-size: .75rem;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.voice-wave {
|
||||
gap: .1875rem;
|
||||
height: 1.25rem;
|
||||
height: .875rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
||||
span {
|
||||
width: .1875rem;
|
||||
width: .125rem;
|
||||
height: 30%;
|
||||
border-radius: .125rem;
|
||||
background: var(--td-brand-color);
|
||||
@@ -409,32 +519,52 @@ onBeforeUnmount(() => {
|
||||
}
|
||||
}
|
||||
|
||||
.voice-text {
|
||||
.voice-status-text {
|
||||
min-width: 0;
|
||||
overflow: hidden;
|
||||
margin: .375rem 0 0;
|
||||
font-size: .8125rem;
|
||||
text-align: center;
|
||||
line-height: 1.4;
|
||||
display: -webkit-box;
|
||||
-webkit-box-orient: vertical;
|
||||
-webkit-line-clamp: 2;
|
||||
white-space: nowrap;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.voice-hint {
|
||||
margin: .25rem 0 0;
|
||||
color: var(--td-text-color-secondary);
|
||||
font-size: .6875rem;
|
||||
.voice-buttons {
|
||||
gap: .5rem;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
&.cancel {
|
||||
.voice-text,
|
||||
.voice-hint {
|
||||
.voice-action {
|
||||
gap: .25rem;
|
||||
flex: 1;
|
||||
display: flex;
|
||||
padding: .4375rem .5rem;
|
||||
font-size: .75rem;
|
||||
transition: color .15s, background .15s, border-color .15s, transform .15s;
|
||||
border: 1px solid var(--td-component-border);
|
||||
border-radius: .625rem;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
|
||||
&.cancel {
|
||||
color: var(--td-error-color);
|
||||
}
|
||||
|
||||
.voice-wave span {
|
||||
&.keep {
|
||||
color: var(--td-brand-color);
|
||||
}
|
||||
|
||||
&.active {
|
||||
color: #fff;
|
||||
transform: translateY(-.0625rem);
|
||||
}
|
||||
|
||||
&.cancel.active {
|
||||
border-color: var(--td-error-color);
|
||||
background: var(--td-error-color);
|
||||
}
|
||||
|
||||
&.keep.active {
|
||||
border-color: var(--td-brand-color);
|
||||
background: var(--td-brand-color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -458,6 +588,11 @@ onBeforeUnmount(() => {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
&.voice-active .form {
|
||||
border-color: var(--td-brand-color);
|
||||
animation: voice-breathe 1.8s ease-in-out infinite;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes voice-wave {
|
||||
@@ -473,6 +608,35 @@ onBeforeUnmount(() => {
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes voice-breathe {
|
||||
0%,
|
||||
100% {
|
||||
box-shadow: 0 0 0 .125rem var(--td-brand-color-light, rgba(0, 82, 217, .16)), 0 .5rem 1.5rem rgba(15, 23, 42, .12);
|
||||
}
|
||||
|
||||
50% {
|
||||
box-shadow: 0 0 0 .375rem var(--td-brand-color-light, rgba(0, 82, 217, .16)), 0 .5rem 1.5rem rgba(15, 23, 42, .12);
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes voice-actions-in {
|
||||
from {
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
to {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.composer.voice-active .form,
|
||||
.voice-actions,
|
||||
.voice-actions .voice-wave span {
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
|
||||
:global(html.theme-dark) {
|
||||
.error-bar {
|
||||
color: var(--ai-chat-error-color-dark, #fda29b);
|
||||
|
||||
Reference in New Issue
Block a user