feat(ai): refine voice input layout and cancellation
This commit is contained in:
@@ -209,6 +209,13 @@ onUnmounted(() => {
|
||||
// 文档预览里的过渡组件就是用来验证转场的,因此在预览范围内恢复原定时长。
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.tui-demo {
|
||||
// 语音输入的呼吸边框是交互反馈的一部分,预览中恢复动画
|
||||
:deep(.composer.voice-active .form::after) {
|
||||
animation-delay: 0s !important;
|
||||
animation-duration: 1.6s !important;
|
||||
animation-iteration-count: infinite !important;
|
||||
}
|
||||
|
||||
:deep(.page-transition .push-left-enter-active) {
|
||||
transition-duration: 520ms, 200ms !important;
|
||||
}
|
||||
|
||||
@@ -12,9 +12,9 @@ demo-preview=../demos/ai-chat-composer/basic.vue
|
||||
|
||||
## 语音输入
|
||||
|
||||
输入框为空且未聚焦时,长按 300ms 进入语音输入:识别文本直接写进输入框,卡片显示主题色呼吸边框,发送按钮变为「松开发送」。松手发送;按住上滑到「取消发送」或「保留识别」按钮后释放,执行对应操作。组件只负责手势与状态展示,识别能力由业务侧接入。
|
||||
开启 `voice-enabled` 后,输入框为空时右侧发送按钮会变成语音按钮(图标 `voice-wave`,文案「语音」)。点击后输入区切换为「按住说话」,右侧按钮变为键盘按钮(图标 `keyboard-1`,文案「输入」);点击键盘按钮回到文本输入。组件只负责手势与状态展示,识别能力由业务侧接入。
|
||||
|
||||
:::preview 长按语音输入 || 长按输入框说话,识别文本实时进入输入框;松手发送,上滑到按钮可取消或只保留文本;示例接入浏览器 Web Speech API,不支持时可勾选「模拟识别」
|
||||
:::preview 语音输入 || 点击语音按钮进入按住说话,识别文本实时显示在整个控件上方;松手发送,移出按住按钮后松开取消;示例接入浏览器 Web Speech API,不支持时可勾选「模拟识别」
|
||||
|
||||
demo-preview=../demos/ai-chat-composer/voice.vue
|
||||
|
||||
@@ -22,29 +22,30 @@ demo-preview=../demos/ai-chat-composer/voice.vue
|
||||
|
||||
### 交互约定
|
||||
|
||||
- 短按(小于 300ms)仍然聚焦文本域;输入框有内容或已聚焦时不接管手势,避免影响编辑与选择
|
||||
- 触发后整个输入卡片显示主题色呼吸边框,上方浮出「取消发送」和「保留识别」两个投放区
|
||||
- `voiceText` 的实时结果直接同步到输入框,不再单独占用语音面板
|
||||
- 按住时发送按钮文案切换为 `voiceSendHint`,默认「松开发送」
|
||||
- 松手时指针在「取消发送」上:触发 `voice-cancel`,组件清空本次识别文本
|
||||
- 松手时指针在「保留识别」上:触发 `voice-keep`,停止识别并把文本留在输入框,不发送
|
||||
- 松手时指针不在按钮上:触发 `voice-stop`,由业务侧停止识别、取最终结果并决定发送
|
||||
- 输入框有内容时右侧仍是发送按钮;只有内容为空且 `voiceState` 为 `idle` 时才显示语音入口
|
||||
- 点击语音按钮进入语音模式,输入区替换为「按住说话」按钮;按住立即触发 `voice-start`,没有长按等待
|
||||
- 按住期间整个输入卡片显示主题色呼吸边框,识别文本实时浮在整个输入卡片上方,最多两行
|
||||
- 按住时按钮文案使用 `voiceSendHint`,默认「松开发送」;指针移出按住按钮后改用 `voiceCancelHint`,默认「松开取消」
|
||||
- 松手时指针仍在按住按钮内:触发 `voice-stop`,由业务侧停止识别、取最终结果并发送
|
||||
- 松手时指针已移出按住按钮:触发 `voice-cancel`,业务侧应中断识别并丢弃本次结果
|
||||
- 指针被系统取消时同样触发 `voice-cancel`
|
||||
- 语音模式右侧固定为键盘按钮(图标 `keyboard-1`,文案「输入」),识别中和按住时禁用;点击回到文本输入
|
||||
- 文本输入与按住说话共用同一输入区高度,切换模式时组件不跳动;文本域按内容换行,最少 3 行、最多 6 行,超出后在文本域内滚动
|
||||
- `voiceEnabled` 默认关闭,需要显式开启
|
||||
|
||||
`voiceState` 由业务侧驱动:
|
||||
|
||||
| 状态 | 含义 |
|
||||
| --- | --- |
|
||||
| `idle` | 未在识别,语音操作区关闭 |
|
||||
| `listening` | 正在聆听,实时回写 `voiceText`,组件同步到输入框 |
|
||||
| `processing` | 已松手或选择保留,等待最终识别结果 |
|
||||
| `idle` | 未在识别,可以切换模式或按住说话 |
|
||||
| `listening` | 正在聆听,实时回写 `voiceText`,组件显示在整个控件上方 |
|
||||
| `processing` | 已松手,等待最终识别结果 |
|
||||
|
||||
识别完成或失败后回到 `idle`,语音操作区随之关闭。组件不会自动发送;正常松手由业务侧在 `voice-stop` 后自行处理。想换文案时用 `voiceSendHint`、`voiceCancelHint`、`voiceKeepHint`、`voiceCancelText`、`voiceKeepText`、`voiceListeningText`、`voiceProcessingText`。
|
||||
识别完成或失败后回到 `idle`。组件不会自动发送;正常松手由业务侧在 `voice-stop` 后自行处理。想换文案时用 `voiceHoldText`、`voiceSendHint`、`voiceCancelHint`、`voiceListeningText`、`voiceProcessingText`。
|
||||
|
||||
### 组件边界与兼容性
|
||||
|
||||
`AiChatComposer` 不包含 ASR,也不依赖 Web Speech API。`voice-start`、`voice-stop`、`voice-keep`、`voice-cancel` 只表达手势,录音、识别和失败降级全部由调用方决定。上面的 demo 使用 Web Speech API 只是演示适配器,可以整体换成服务端流式识别或原生桥接。
|
||||
`AiChatComposer` 不包含 ASR,也不依赖 Web Speech API。`voice-start`、`voice-stop`、`voice-cancel` 只表达手势,录音、识别和失败降级全部由调用方决定。上面的 demo 使用 Web Speech API 只是演示适配器,可以整体换成服务端流式识别或原生桥接。
|
||||
|
||||
Web Speech API 的实际兼容性(2026-11):
|
||||
|
||||
@@ -58,7 +59,7 @@ Web Speech API 的实际兼容性(2026-11):
|
||||
| Firefox 桌面 160+ | 有实现,但生态较新,建议实测;Firefox Android 不支持 |
|
||||
| SafariViewController / iOS App 内嵌页 | Web Speech API 不可用或不可靠,改用原生桥接 |
|
||||
|
||||
Web Speech API 还需要 HTTPS(localhost 除外)和麦克风授权;音频会交给浏览器厂商的服务,隐私敏感或企业内网场景不建议使用。iOS 对用户手势要求更严格,如果 `SpeechRecognition.start()` 在 300ms 长按回调里被拒绝,需要在 `pointerdown` 阶段提前启动,或改成点击说话。
|
||||
Web Speech API 还需要 HTTPS(localhost 除外)和麦克风授权;音频会交给浏览器厂商的服务,隐私敏感或企业内网场景不建议使用。组件在 `pointerdown` 时立即触发 `voice-start`,iOS 上应在这个事件里同步调用 `SpeechRecognition.start()`;等异步连接建立后再启动容易被用户手势策略拒绝。
|
||||
|
||||
生产环境建议把识别放到服务端流式 ASR(WebSocket)或原生语音插件。业务侧只需要按组件契约驱动状态:
|
||||
|
||||
@@ -76,11 +77,6 @@ function onVoiceStop() {
|
||||
// 停止录音,等待最终结果;拿到结果后发送
|
||||
}
|
||||
|
||||
function onVoiceKeep() {
|
||||
voiceState.value = "processing";
|
||||
// 停止录音但不发送,最终结果留在输入框
|
||||
}
|
||||
|
||||
function onVoiceCancel() {
|
||||
voiceState.value = "idle";
|
||||
voiceText.value = "";
|
||||
@@ -88,7 +84,7 @@ function onVoiceCancel() {
|
||||
}
|
||||
```
|
||||
|
||||
`voiceText` 会实时同步到输入框;识别不可用时退回文字输入,不要阻塞发送。
|
||||
`voiceText` 只在按住说话区上方展示,不回写 `v-model`;语音结果由业务侧在 `voice-stop` 后取出并发送。识别不可用时退回文字输入,不要阻塞发送。
|
||||
|
||||
## 何时使用
|
||||
|
||||
@@ -96,12 +92,15 @@ function onVoiceCancel() {
|
||||
|
||||
## 输入区与操作按钮
|
||||
|
||||
文本域占满整行并随内容自动增高。下一行左侧是新开会话(`chat-add`)和添加附件(`file-add`),右侧根据状态显示纯文本发送或停止按钮。按住语音时发送按钮文案切换为 `voiceSendHint`。
|
||||
文本域占满整行并随内容自动增高。下一行左侧是新开会话(`chat-add`)和添加附件(`file-add`),右侧根据状态显示语音、键盘、发送或停止按钮。
|
||||
|
||||
| 状态 | 按钮 |
|
||||
| --- | --- |
|
||||
| 空闲且未禁用 | 新开会话、添加附件、发送(`sendText`) |
|
||||
| `isGenerating` 为真 | 停止(`stopText`,危险色) |
|
||||
| 空闲、输入框为空且启用 `voiceEnabled` | 语音(`voice-wave`),点击进入语音模式 |
|
||||
| 语音模式且未在识别 | 键盘(`keyboard-1`),点击回到文本输入 |
|
||||
| 语音模式正在识别或处理 | 键盘按钮禁用 |
|
||||
| 文本模式且有内容 | 发送(`sendText`) |
|
||||
| `isGenerating` 为真 | 停止(`stopText`,危险色),优先于语音模式 |
|
||||
| 有 `lastError` 且 `canRetry` | 错误条 + 重试(`retryText`) |
|
||||
|
||||
生成中或 `disabled` 为真时输入框不可编辑,新开会话、添加附件和 `send` 事件不会触发。
|
||||
@@ -113,16 +112,15 @@ function onVoiceCancel() {
|
||||
- `retry`:点错误条上的重试
|
||||
- `new-conversation`:清空输入框并请求业务侧开始一个新会话
|
||||
- `add-attachment`:请求业务侧添加附件
|
||||
- `voice-start`:长按达到阈值,可以开始录音或启动识别
|
||||
- `voice-stop`:松手且不在操作按钮上,可以停止识别并取最终结果
|
||||
- `voice-keep`:松手在「保留识别」上,停止识别,文本留在输入框但不发送
|
||||
- `voice-cancel`:松手在「取消发送」上,或指针被系统取消;组件会清空输入框,业务侧应丢弃本次语音
|
||||
- `voice-start`:按住「按住说话」按钮时立即触发,可以开始录音或启动识别
|
||||
- `voice-stop`:松手且不在取消区,可以停止识别并取最终结果
|
||||
- `voice-cancel`:松手时指针已移出按住按钮,或指针被系统取消;业务侧应中断识别并丢弃本次结果
|
||||
|
||||
输入框内容用 `v-model` 绑定,`maxLength` 默认 2000。
|
||||
输入框内容用 `v-model` 绑定,`maxLength` 默认 2000。语音识别结果只通过 `voiceText` 展示,不写入 `v-model`。
|
||||
|
||||
## 自定义插槽
|
||||
|
||||
`send`、`stop`、`error` 三个插槽可以替换对应区域的渲染,都带作用域参数(`disabled`、`onSend`、`onStop`、`error`、`canRetry`)。
|
||||
`send`、`stop`、`error` 三个插槽可以替换对应区域的渲染,都带作用域参数(`disabled`、`onSend`、`onStop`、`error`、`canRetry`)。`send` 插槽只用于文本发送;语音入口和键盘切换按钮由组件内置。
|
||||
|
||||
## API
|
||||
|
||||
|
||||
@@ -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(() => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<template>
|
||||
<div class="composer" :class="{ 'voice-active': voiceActive }">
|
||||
<div class="composer" :class="{ 'voice-active': voicePressing }">
|
||||
<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,45 +13,38 @@
|
||||
</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 v-if="voiceMode && (voiceTranscript || voiceTranscriptHint)" class="voice-transcript" 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>
|
||||
<p v-if="voiceTranscript" class="voice-text" v-text="voiceTranscript" />
|
||||
<p v-else class="voice-placeholder" v-text="voiceTranscriptHint" />
|
||||
</div>
|
||||
<div class="input-wrap">
|
||||
<t-textarea
|
||||
ref="textareaRef"
|
||||
v-model="draft"
|
||||
class="input"
|
||||
:autosize="{ minRows: 3, maxRows: 6 }"
|
||||
:disabled="props.isGenerating || props.disabled"
|
||||
:class="{ 'voice-input-hidden': voiceMode }"
|
||||
:autosize="{ minRows: 2, maxRows: 6 }"
|
||||
:disabled="voiceMode || props.isGenerating || props.disabled"
|
||||
:maxlength="props.maxLength"
|
||||
:placeholder="props.placeholder"
|
||||
@focus="inputFocused = true"
|
||||
@blur="inputFocused = false"
|
||||
:aria-hidden="voiceMode"
|
||||
/>
|
||||
<div
|
||||
v-if="voiceHitVisible"
|
||||
class="voice-hit"
|
||||
<button
|
||||
v-if="voiceMode"
|
||||
ref="holdRef"
|
||||
class="voice-hold"
|
||||
:class="{ active: voicePressing, cancel: willCancel }"
|
||||
type="button"
|
||||
:disabled="props.disabled || props.isGenerating"
|
||||
@pointerdown="handleVoicePointerDown"
|
||||
@pointermove="handleVoicePointerMove"
|
||||
@pointerup="handleVoicePointerUp"
|
||||
@pointercancel="handleVoicePointerCancel"
|
||||
@contextmenu.prevent
|
||||
/>
|
||||
>
|
||||
<t-icon name="voice-wave" size="small" />
|
||||
<span v-text="voiceHoldLabel" />
|
||||
</button>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<div class="tools">
|
||||
@@ -93,13 +86,49 @@
|
||||
<t-button
|
||||
class="send"
|
||||
type="button"
|
||||
size="extra-small"
|
||||
size="small"
|
||||
theme="danger"
|
||||
:disabled="props.disabled"
|
||||
:content="props.stopText"
|
||||
@click="emit('stop')"
|
||||
/>
|
||||
>
|
||||
<template #icon>
|
||||
<t-icon name="stop" />
|
||||
</template>
|
||||
</t-button>
|
||||
</slot>
|
||||
<template v-else-if="props.voiceEnabled && voiceMode">
|
||||
<t-button
|
||||
class="send voice-toggle"
|
||||
type="button"
|
||||
size="small"
|
||||
theme="primary"
|
||||
aria-label="切换到键盘输入"
|
||||
:disabled="props.disabled || voicePressing || props.voiceState !== 'idle'"
|
||||
content="输入"
|
||||
@click="exitVoiceMode"
|
||||
>
|
||||
<template #icon>
|
||||
<t-icon name="keyboard-1" />
|
||||
</template>
|
||||
</t-button>
|
||||
</template>
|
||||
<template v-else-if="props.voiceEnabled && !draft.trim()">
|
||||
<t-button
|
||||
class="send voice-toggle"
|
||||
type="button"
|
||||
size="small"
|
||||
theme="primary"
|
||||
aria-label="语音输入"
|
||||
:disabled="props.disabled || props.voiceState !== 'idle'"
|
||||
content="语音"
|
||||
@click="enterVoiceMode"
|
||||
>
|
||||
<template #icon>
|
||||
<t-icon name="voice-wave" />
|
||||
</template>
|
||||
</t-button>
|
||||
</template>
|
||||
<slot
|
||||
v-else
|
||||
name="send"
|
||||
@@ -109,11 +138,15 @@
|
||||
<t-button
|
||||
class="send"
|
||||
type="submit"
|
||||
size="extra-small"
|
||||
size="small"
|
||||
theme="primary"
|
||||
:disabled="props.disabled"
|
||||
:content="sendButtonText"
|
||||
/>
|
||||
:content="props.sendText"
|
||||
>
|
||||
<template #icon>
|
||||
<t-icon name="send-1" />
|
||||
</template>
|
||||
</t-button>
|
||||
</slot>
|
||||
</div>
|
||||
</t-form>
|
||||
@@ -121,7 +154,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, ref, useTemplateRef, watch } from "vue";
|
||||
import { computed, nextTick, onBeforeUnmount, ref, useTemplateRef, watch } from "vue";
|
||||
|
||||
defineOptions({
|
||||
name: "AiChatComposer"
|
||||
@@ -137,22 +170,18 @@ export interface Props {
|
||||
stopText?: string;
|
||||
retryText?: string;
|
||||
maxLength?: number;
|
||||
/** 是否启用长按输入框语音输入 */
|
||||
/** 是否启用语音输入 */
|
||||
voiceEnabled?: boolean;
|
||||
/** 语音识别状态,由业务侧驱动 */
|
||||
voiceState?: "idle" | "listening" | "processing";
|
||||
/** 语音中间结果 */
|
||||
voiceText?: string;
|
||||
/** 松开发送提示,同时作为按住时的发送按钮文案 */
|
||||
/** 按住说话提示 */
|
||||
voiceHoldText?: string;
|
||||
/** 按住时的提示 */
|
||||
voiceSendHint?: string;
|
||||
/** 上滑到取消按钮时的提示 */
|
||||
/** 移出按住按钮时的提示 */
|
||||
voiceCancelHint?: string;
|
||||
/** 上滑到保留按钮时的提示 */
|
||||
voiceKeepHint?: string;
|
||||
/** 取消发送按钮文案 */
|
||||
voiceCancelText?: string;
|
||||
/** 保留识别按钮文案 */
|
||||
voiceKeepText?: string;
|
||||
/** 正在聆听提示 */
|
||||
voiceListeningText?: string;
|
||||
/** 正在识别提示 */
|
||||
@@ -171,11 +200,9 @@ const props = withDefaults(defineProps<Props>(), {
|
||||
voiceEnabled: false,
|
||||
voiceState: "idle",
|
||||
voiceText: "",
|
||||
voiceSendHint: "松开发送",
|
||||
voiceCancelHint: "松开取消",
|
||||
voiceKeepHint: "松开保留",
|
||||
voiceCancelText: "取消发送",
|
||||
voiceKeepText: "保留识别",
|
||||
voiceHoldText: "按住 说话",
|
||||
voiceSendHint: "松开 发送",
|
||||
voiceCancelHint: "松开 取消",
|
||||
voiceListeningText: "正在听你说…",
|
||||
voiceProcessingText: "正在识别…"
|
||||
});
|
||||
@@ -189,71 +216,55 @@ const emit = defineEmits<{
|
||||
"add-attachment": [];
|
||||
"voice-start": [];
|
||||
"voice-stop": [];
|
||||
"voice-keep": [];
|
||||
"voice-cancel": [];
|
||||
}>();
|
||||
|
||||
/** 长按触发语音的阈值,短按仍然聚焦输入 */
|
||||
const VOICE_HOLD_DELAY = 300;
|
||||
/** 未触发语音时,手指移动超过该距离视为滑动 */
|
||||
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 holdRef = useTemplateRef<HTMLElement>("holdRef");
|
||||
const voiceMode = 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
|
||||
&& (voicePressing.value || (!draft.value.trim() && !inputFocused.value && props.voiceState === "idle")));
|
||||
const voiceTranscript = computed(() => props.voiceText.trim());
|
||||
|
||||
const voiceActive = computed(() => voicePressing.value && voiceTriggered.value);
|
||||
|
||||
const voiceActionsVisible = computed(() => voiceActive.value);
|
||||
|
||||
const voiceStatusText = computed(() => {
|
||||
const voiceHoldLabel = computed(() => {
|
||||
if (willCancel.value) {
|
||||
return props.voiceCancelHint;
|
||||
}
|
||||
if (willKeep.value) {
|
||||
return props.voiceKeepHint;
|
||||
if (props.voiceState === "processing") {
|
||||
return props.voiceProcessingText;
|
||||
}
|
||||
return props.voiceState === "processing" ? props.voiceProcessingText : props.voiceListeningText;
|
||||
if (voicePressing.value) {
|
||||
return props.voiceSendHint;
|
||||
}
|
||||
if (props.voiceState === "listening") {
|
||||
return props.voiceListeningText;
|
||||
}
|
||||
return props.voiceHoldText;
|
||||
});
|
||||
|
||||
const sendButtonText = computed(() => voiceActive.value ? props.voiceSendHint : props.sendText);
|
||||
|
||||
/** ASR 结果直接写进输入框;取消后停止同步,保留后忽略调用方清空 voiceText 的动作 */
|
||||
watch(() => props.voiceText, (text) => {
|
||||
if (!voiceSessionActive.value) {
|
||||
return;
|
||||
const voiceTranscriptHint = computed(() => {
|
||||
if (voiceTranscript.value) {
|
||||
return "";
|
||||
}
|
||||
if (voiceKeepPending.value && !text) {
|
||||
return;
|
||||
if (props.voiceState === "processing") {
|
||||
return props.voiceProcessingText;
|
||||
}
|
||||
draft.value = text;
|
||||
if (voicePressing.value || props.voiceState === "listening") {
|
||||
return props.voiceListeningText;
|
||||
}
|
||||
return "";
|
||||
});
|
||||
|
||||
watch(() => props.voiceState, (state) => {
|
||||
if (state === "idle" && voiceSessionActive.value) {
|
||||
voiceSessionActive.value = false;
|
||||
voiceKeepPending.value = false;
|
||||
watch(() => props.voiceEnabled, (enabled) => {
|
||||
if (enabled) {
|
||||
return;
|
||||
}
|
||||
if (voicePressing.value) {
|
||||
cancelVoice();
|
||||
}
|
||||
voiceMode.value = false;
|
||||
});
|
||||
|
||||
function startNewConversation(): void {
|
||||
@@ -283,130 +294,94 @@ function submitMessage(): void {
|
||||
emit("send", message);
|
||||
}
|
||||
|
||||
function handleVoicePointerDown(event: PointerEvent): void {
|
||||
function enterVoiceMode(): void {
|
||||
if (!props.voiceEnabled || props.disabled || props.isGenerating || props.voiceState !== "idle" || draft.value.trim()) {
|
||||
return;
|
||||
}
|
||||
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();
|
||||
try {
|
||||
(event.currentTarget as HTMLElement).setPointerCapture(event.pointerId);
|
||||
} catch {
|
||||
// 指针捕获失败时仍可依赖冒泡事件,忽略
|
||||
}
|
||||
voiceTimer = window.setTimeout(() => {
|
||||
if (!voicePressing.value) {
|
||||
voiceMode.value = true;
|
||||
}
|
||||
|
||||
function exitVoiceMode(): void {
|
||||
if (voicePressing.value) {
|
||||
return;
|
||||
}
|
||||
voiceTriggered.value = true;
|
||||
voiceSessionActive.value = true;
|
||||
voiceKeepPending.value = false;
|
||||
draft.value = "";
|
||||
voiceMode.value = false;
|
||||
nextTick(() => textareaRef.value?.focus());
|
||||
}
|
||||
|
||||
function handleVoicePointerDown(event: PointerEvent): void {
|
||||
if (!props.voiceEnabled || !voiceMode.value || props.disabled || props.isGenerating || props.voiceState !== "idle" || voicePressing.value) {
|
||||
return;
|
||||
}
|
||||
voicePressing.value = true;
|
||||
willCancel.value = false;
|
||||
voicePointerId = event.pointerId;
|
||||
event.preventDefault();
|
||||
// 监听 window,手指移出按钮后仍能收到移动、释放和取消
|
||||
window.addEventListener("pointermove", handleVoicePointerMove);
|
||||
window.addEventListener("pointerup", handleVoicePointerUp);
|
||||
window.addEventListener("pointercancel", handleVoicePointerCancel);
|
||||
navigator.vibrate?.(15);
|
||||
emit("voice-start");
|
||||
}, VOICE_HOLD_DELAY);
|
||||
}
|
||||
|
||||
function handleVoicePointerMove(event: PointerEvent): void {
|
||||
if (!voicePressing.value || event.pointerId !== voicePointerId) {
|
||||
return;
|
||||
}
|
||||
if (!voiceTriggered.value) {
|
||||
if (Math.abs(event.clientY - voiceStartY) > VOICE_MOVE_TOLERANCE) {
|
||||
resetVoicePress();
|
||||
}
|
||||
return;
|
||||
}
|
||||
const action = resolveVoiceAction(event);
|
||||
willCancel.value = action === "cancel";
|
||||
willKeep.value = action === "keep";
|
||||
willCancel.value = !isPointInsideHold(event);
|
||||
}
|
||||
|
||||
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 {
|
||||
function isPointInsideHold(event: PointerEvent): boolean {
|
||||
const element = holdRef.value;
|
||||
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;
|
||||
return event.clientX >= rect.left
|
||||
&& event.clientX <= rect.right
|
||||
&& event.clientY >= rect.top
|
||||
&& event.clientY <= rect.bottom;
|
||||
}
|
||||
|
||||
function handleVoicePointerUp(event: PointerEvent): void {
|
||||
if (!voicePressing.value || event.pointerId !== voicePointerId) {
|
||||
return;
|
||||
}
|
||||
const triggered = voiceTriggered.value;
|
||||
const action = resolveVoiceAction(event);
|
||||
resetVoicePress();
|
||||
if (!triggered) {
|
||||
textareaRef.value?.focus();
|
||||
return;
|
||||
}
|
||||
if (action === "cancel") {
|
||||
if (!isPointInsideHold(event)) {
|
||||
cancelVoice();
|
||||
return;
|
||||
}
|
||||
if (action === "keep") {
|
||||
voiceKeepPending.value = true;
|
||||
emit("voice-keep");
|
||||
return;
|
||||
}
|
||||
resetVoicePress();
|
||||
emit("voice-stop");
|
||||
}
|
||||
|
||||
function handleVoicePointerCancel(): void {
|
||||
if (!voicePressing.value) {
|
||||
function handleVoicePointerCancel(event: PointerEvent): void {
|
||||
if (!voicePressing.value || event.pointerId !== voicePointerId) {
|
||||
return;
|
||||
}
|
||||
const triggered = voiceTriggered.value;
|
||||
resetVoicePress();
|
||||
if (triggered) {
|
||||
cancelVoice();
|
||||
}
|
||||
}
|
||||
|
||||
function cancelVoice(): void {
|
||||
voiceSessionActive.value = false;
|
||||
voiceKeepPending.value = false;
|
||||
draft.value = "";
|
||||
resetVoicePress();
|
||||
emit("voice-cancel");
|
||||
}
|
||||
|
||||
function resetVoicePress(): void {
|
||||
if (voiceTimer !== undefined) {
|
||||
window.clearTimeout(voiceTimer);
|
||||
voiceTimer = undefined;
|
||||
}
|
||||
window.removeEventListener("pointermove", handleVoicePointerMove);
|
||||
window.removeEventListener("pointerup", handleVoicePointerUp);
|
||||
window.removeEventListener("pointercancel", handleVoicePointerCancel);
|
||||
voicePressing.value = false;
|
||||
voiceTriggered.value = false;
|
||||
willCancel.value = false;
|
||||
willKeep.value = false;
|
||||
voicePointerId = undefined;
|
||||
}
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
if (voiceTimer !== undefined) {
|
||||
window.clearTimeout(voiceTimer);
|
||||
if (voicePressing.value) {
|
||||
emit("voice-cancel");
|
||||
}
|
||||
resetVoicePress();
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -453,9 +428,12 @@ onBeforeUnmount(() => {
|
||||
flex-direction: column;
|
||||
|
||||
.input-wrap {
|
||||
gap: .375rem;
|
||||
display: flex;
|
||||
position: relative;
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.input {
|
||||
@@ -463,115 +441,118 @@ onBeforeUnmount(() => {
|
||||
width: 100%;
|
||||
padding: 0 .25rem;
|
||||
min-width: 0;
|
||||
|
||||
&.voice-input-hidden {
|
||||
visibility: hidden;
|
||||
}
|
||||
}
|
||||
|
||||
.voice-hit {
|
||||
inset: 0;
|
||||
z-index: 1;
|
||||
position: absolute;
|
||||
touch-action: none;
|
||||
-webkit-tap-highlight-color: transparent;
|
||||
}
|
||||
|
||||
.voice-actions {
|
||||
gap: .375rem;
|
||||
.voice-transcript {
|
||||
left: 50%;
|
||||
gap: .5rem;
|
||||
width: 100%;
|
||||
z-index: 3;
|
||||
display: flex;
|
||||
padding: .5rem;
|
||||
padding: .5rem .75rem;
|
||||
position: absolute;
|
||||
box-sizing: border-box;
|
||||
bottom: calc(100% + .5rem);
|
||||
user-select: none;
|
||||
align-items: center;
|
||||
pointer-events: none;
|
||||
border: 1px solid var(--td-brand-color);
|
||||
background: var(--td-bg-color-container);
|
||||
box-shadow: 0 .5rem 1.5rem rgba(15, 23, 42, .16);
|
||||
animation: voice-actions-in .16s ease-out;
|
||||
animation: voice-transcript-in .16s ease-out;
|
||||
border-radius: 1rem;
|
||||
flex-direction: column;
|
||||
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: .875rem;
|
||||
height: 1rem;
|
||||
display: flex;
|
||||
flex: none;
|
||||
align-items: center;
|
||||
|
||||
span {
|
||||
width: .125rem;
|
||||
height: 30%;
|
||||
height: 100%;
|
||||
border-radius: .125rem;
|
||||
background: var(--td-brand-color);
|
||||
animation: voice-wave .9s ease-in-out infinite;
|
||||
animation-delay: calc(var(--i) * .1s);
|
||||
transform: scaleY(.3);
|
||||
transform-origin: center;
|
||||
}
|
||||
}
|
||||
|
||||
.voice-status-text {
|
||||
.voice-text,
|
||||
.voice-placeholder {
|
||||
margin: 0;
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
.voice-text {
|
||||
display: -webkit-box;
|
||||
overflow: hidden;
|
||||
color: var(--td-text-color-primary);
|
||||
font-size: .875rem;
|
||||
line-height: 1.4;
|
||||
-webkit-box-orient: vertical;
|
||||
-webkit-line-clamp: 2;
|
||||
}
|
||||
|
||||
.voice-placeholder {
|
||||
overflow: hidden;
|
||||
color: var(--td-text-color-placeholder);
|
||||
font-size: .75rem;
|
||||
white-space: nowrap;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.voice-buttons {
|
||||
gap: .5rem;
|
||||
.voice-hold {
|
||||
inset: 0;
|
||||
gap: .375rem;
|
||||
z-index: 1;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.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;
|
||||
padding: 0 .75rem;
|
||||
position: absolute;
|
||||
color: var(--td-brand-color);
|
||||
font-size: .875rem;
|
||||
font-weight: 500;
|
||||
transition: color .15s, border-color .15s;
|
||||
border: 0;
|
||||
border-bottom: 1px solid var(--td-component-border);
|
||||
background: transparent;
|
||||
border-radius: 0;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
touch-action: none;
|
||||
user-select: none;
|
||||
-webkit-tap-highlight-color: transparent;
|
||||
|
||||
&.active {
|
||||
color: var(--td-brand-color);
|
||||
border-bottom-color: var(--td-brand-color);
|
||||
}
|
||||
|
||||
&.cancel {
|
||||
color: var(--td-error-color);
|
||||
border-bottom-color: var(--td-error-color);
|
||||
}
|
||||
|
||||
&.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);
|
||||
}
|
||||
&:disabled {
|
||||
opacity: .55;
|
||||
}
|
||||
}
|
||||
|
||||
.actions {
|
||||
--td-button-border-radius: .75rem;
|
||||
--td-button-border-radius: 1rem;
|
||||
|
||||
gap: .5rem;
|
||||
min-height: 2rem;
|
||||
margin-top: .375rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
@@ -582,44 +563,53 @@ onBeforeUnmount(() => {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.send {
|
||||
padding: 0 1rem;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
&.voice-active .form {
|
||||
border-color: var(--td-brand-color);
|
||||
animation: voice-breathe 1.8s ease-in-out infinite;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes voice-wave {
|
||||
0%,
|
||||
100% {
|
||||
height: 25%;
|
||||
opacity: .5;
|
||||
&::after {
|
||||
content: "";
|
||||
inset: -.125rem;
|
||||
border: 1px solid var(--td-brand-color);
|
||||
position: absolute;
|
||||
animation: voice-breathe 1s ease-in-out 0s infinite;
|
||||
will-change: transform, opacity;
|
||||
border-radius: inherit;
|
||||
pointer-events: none;
|
||||
transform-origin: center;
|
||||
}
|
||||
|
||||
50% {
|
||||
height: 100%;
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
@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);
|
||||
opacity: .6;
|
||||
transform: scaleX(1) scaleY(1);
|
||||
}
|
||||
|
||||
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);
|
||||
opacity: .2;
|
||||
transform: scaleX(1.02) scaleY(1.08);
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes voice-actions-in {
|
||||
@keyframes voice-wave {
|
||||
0%,
|
||||
100% {
|
||||
opacity: .5;
|
||||
transform: scaleY(.3);
|
||||
}
|
||||
|
||||
50% {
|
||||
opacity: 1;
|
||||
transform: scaleY(1);
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes voice-transcript-in {
|
||||
from {
|
||||
opacity: 0;
|
||||
}
|
||||
@@ -629,14 +619,6 @@ onBeforeUnmount(() => {
|
||||
}
|
||||
}
|
||||
|
||||
@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