Merge pull request 'v2.1.0' (#15) from dev into master

Reviewed-on: #15
This commit was merged in pull request #15.
This commit is contained in:
2026-11-25 09:15:31 +00:00
2 changed files with 261 additions and 10 deletions
+1 -1
View File
@@ -9,7 +9,7 @@
"**/*.css"
],
"private": false,
"version": "2.0.0",
"version": "2.1.0",
"license": "MIT",
"scripts": {
"dev": "pnpm run -C docs dev",
+260 -9
View File
@@ -13,14 +13,35 @@
</slot>
</div>
<t-form class="form glass-white" @submit="submitMessage">
<t-textarea
v-model="draft"
class="input"
:autosize="{ minRows: 3, maxRows: 6 }"
:disabled="props.isGenerating || props.disabled"
:maxlength="props.maxLength"
:placeholder="props.placeholder"
/>
<div class="input-wrap">
<t-textarea
ref="textareaRef"
v-model="draft"
class="input"
:autosize="{ minRows: 3, maxRows: 6 }"
:disabled="props.isGenerating || props.disabled"
:maxlength="props.maxLength"
:placeholder="props.placeholder"
@focus="inputFocused = true"
@blur="inputFocused = false"
/>
<div
v-if="voiceHitVisible"
class="voice-hit"
@pointerdown="handleVoicePointerDown"
@pointermove="handleVoicePointerMove"
@pointerup="handleVoicePointerUp"
@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">
<t-button
@@ -89,6 +110,8 @@
</template>
<script setup lang="ts">
import { computed, onBeforeUnmount, ref, useTemplateRef } from "vue";
defineOptions({
name: "AiChatComposer"
});
@@ -103,6 +126,20 @@ export interface Props {
stopText?: string;
retryText?: string;
maxLength?: number;
/** 是否启用长按输入框语音输入 */
voiceEnabled?: boolean;
/** 语音识别状态,由业务侧驱动 */
voiceState?: "idle" | "listening" | "processing";
/** 语音中间结果 */
voiceText?: string;
/** 松开发送提示 */
voiceSendHint?: string;
/** 上滑取消提示 */
voiceCancelHint?: string;
/** 正在聆听提示 */
voiceListeningText?: string;
/** 正在识别提示 */
voiceProcessingText?: string;
}
const props = withDefaults(defineProps<Props>(), {
@@ -113,7 +150,14 @@ const props = withDefaults(defineProps<Props>(), {
sendText: "发送",
stopText: "停止",
retryText: "重试",
maxLength: 2000
maxLength: 2000,
voiceEnabled: false,
voiceState: "idle",
voiceText: "",
voiceSendHint: "松开发送",
voiceCancelHint: "松开取消",
voiceListeningText: "正在听你说…",
voiceProcessingText: "正在识别…"
});
const draft = defineModel<string>({ required: true });
@@ -123,8 +167,44 @@ const emit = defineEmits<{
retry: [];
"new-conversation": [];
"add-attachment": [];
"voice-start": [];
"voice-stop": [];
"voice-cancel": [];
}>();
/** 长按触发语音的阈值,短按仍然聚焦输入 */
const VOICE_HOLD_DELAY = 300;
/** 上滑取消的位移阈值 */
const VOICE_CANCEL_DISTANCE = 48;
/** 未触发语音时,手指移动超过该距离视为滑动 */
const VOICE_MOVE_TOLERANCE = 12;
const textareaRef = useTemplateRef<{ focus: () => void }>("textareaRef");
const inputFocused = ref(false);
const voicePressing = ref(false);
const voiceTriggered = ref(false);
const willCancel = 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));
const voicePanelVisible = computed(() => props.voiceEnabled && (voiceTriggered.value || props.voiceState !== "idle"));
const voicePanelText = computed(() => {
if (props.voiceText) {
return props.voiceText;
}
return props.voiceState === "processing" ? props.voiceProcessingText : props.voiceListeningText;
});
function startNewConversation(): void {
if (props.disabled || props.isGenerating) {
return;
@@ -151,6 +231,90 @@ function submitMessage(): void {
draft.value = "";
emit("send", message);
}
function handleVoicePointerDown(event: PointerEvent): void {
if (!props.voiceEnabled || props.disabled || props.isGenerating || props.voiceState !== "idle" || draft.value.trim()) {
return;
}
voicePressing.value = true;
voiceTriggered.value = false;
willCancel.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) {
return;
}
voiceTriggered.value = true;
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;
}
willCancel.value = voiceStartY - event.clientY > VOICE_CANCEL_DISTANCE;
}
function handleVoicePointerUp(event: PointerEvent): void {
if (!voicePressing.value || event.pointerId !== voicePointerId) {
return;
}
const triggered = voiceTriggered.value;
const cancel = willCancel.value;
resetVoicePress();
if (!triggered) {
textareaRef.value?.focus();
return;
}
if (cancel) {
emit("voice-cancel");
return;
}
emit("voice-stop");
}
function handleVoicePointerCancel(): void {
if (!voicePressing.value) {
return;
}
const triggered = voiceTriggered.value;
resetVoicePress();
if (triggered) {
emit("voice-cancel");
}
}
function resetVoicePress(): void {
if (voiceTimer !== undefined) {
window.clearTimeout(voiceTimer);
voiceTimer = undefined;
}
voicePressing.value = false;
voiceTriggered.value = false;
willCancel.value = false;
voicePointerId = undefined;
}
onBeforeUnmount(() => {
if (voiceTimer !== undefined) {
window.clearTimeout(voiceTimer);
}
});
</script>
<style scoped lang="less">
@@ -193,6 +357,12 @@ function submitMessage(): void {
border-radius: 1.25rem;
flex-direction: column;
.input-wrap {
position: relative;
flex: 1;
min-width: 0;
}
.input {
flex: 1;
width: 100%;
@@ -200,6 +370,74 @@ function submitMessage(): void {
min-width: 0;
}
.voice-hit {
inset: 0;
z-index: 1;
position: absolute;
touch-action: none;
-webkit-tap-highlight-color: transparent;
}
.voice-panel {
inset: 0;
gap: .25rem;
display: flex;
z-index: 2;
padding: .5rem .75rem;
position: absolute;
user-select: none;
border-radius: .75rem;
pointer-events: none;
background: var(--td-bg-color-container);
align-items: center;
flex-direction: column;
justify-content: center;
.voice-wave {
gap: .1875rem;
height: 1.25rem;
display: flex;
align-items: center;
span {
width: .1875rem;
height: 30%;
border-radius: .125rem;
background: var(--td-brand-color);
animation: voice-wave .9s ease-in-out infinite;
animation-delay: calc(var(--i) * .1s);
}
}
.voice-text {
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;
}
.voice-hint {
margin: .25rem 0 0;
color: var(--td-text-color-secondary);
font-size: .6875rem;
}
&.cancel {
.voice-text,
.voice-hint {
color: var(--td-error-color);
}
.voice-wave span {
background: var(--td-error-color);
}
}
}
.actions {
--td-button-border-radius: .75rem;
@@ -222,6 +460,19 @@ function submitMessage(): void {
}
}
@keyframes voice-wave {
0%,
100% {
height: 25%;
opacity: .5;
}
50% {
height: 100%;
opacity: 1;
}
}
:global(html.theme-dark) {
.error-bar {
color: var(--ai-chat-error-color-dark, #fda29b);