Compare commits
12
Commits
86da3d44d8
..
v2.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ecdb1a4948 | ||
|
|
d8487807cd | ||
|
|
6885015825 | ||
|
|
38038b9461 | ||
|
|
9e06f1e9b8 | ||
|
|
0f369dc42d | ||
|
|
5f4c35cdd3 | ||
|
|
35885b6e9d | ||
|
|
a0a978f58b | ||
|
|
986f8c75db | ||
|
|
13db8077f4 | ||
|
|
7c8d2566d7 |
+1
-1
@@ -9,7 +9,7 @@
|
|||||||
"**/*.css"
|
"**/*.css"
|
||||||
],
|
],
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "2.1.0",
|
"version": "2.0.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "pnpm run -C docs dev",
|
"dev": "pnpm run -C docs dev",
|
||||||
|
|||||||
@@ -13,35 +13,14 @@
|
|||||||
</slot>
|
</slot>
|
||||||
</div>
|
</div>
|
||||||
<t-form class="form glass-white" @submit="submitMessage">
|
<t-form class="form glass-white" @submit="submitMessage">
|
||||||
<div class="input-wrap">
|
|
||||||
<t-textarea
|
<t-textarea
|
||||||
ref="textareaRef"
|
|
||||||
v-model="draft"
|
v-model="draft"
|
||||||
class="input"
|
class="input"
|
||||||
:autosize="{ minRows: 3, maxRows: 6 }"
|
:autosize="{ minRows: 3, maxRows: 6 }"
|
||||||
:disabled="props.isGenerating || props.disabled"
|
:disabled="props.isGenerating || props.disabled"
|
||||||
:maxlength="props.maxLength"
|
:maxlength="props.maxLength"
|
||||||
:placeholder="props.placeholder"
|
: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="actions">
|
||||||
<div class="tools">
|
<div class="tools">
|
||||||
<t-button
|
<t-button
|
||||||
@@ -110,8 +89,6 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed, onBeforeUnmount, ref, useTemplateRef } from "vue";
|
|
||||||
|
|
||||||
defineOptions({
|
defineOptions({
|
||||||
name: "AiChatComposer"
|
name: "AiChatComposer"
|
||||||
});
|
});
|
||||||
@@ -126,20 +103,6 @@ export interface Props {
|
|||||||
stopText?: string;
|
stopText?: string;
|
||||||
retryText?: string;
|
retryText?: string;
|
||||||
maxLength?: number;
|
maxLength?: number;
|
||||||
/** 是否启用长按输入框语音输入 */
|
|
||||||
voiceEnabled?: boolean;
|
|
||||||
/** 语音识别状态,由业务侧驱动 */
|
|
||||||
voiceState?: "idle" | "listening" | "processing";
|
|
||||||
/** 语音中间结果 */
|
|
||||||
voiceText?: string;
|
|
||||||
/** 松开发送提示 */
|
|
||||||
voiceSendHint?: string;
|
|
||||||
/** 上滑取消提示 */
|
|
||||||
voiceCancelHint?: string;
|
|
||||||
/** 正在聆听提示 */
|
|
||||||
voiceListeningText?: string;
|
|
||||||
/** 正在识别提示 */
|
|
||||||
voiceProcessingText?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const props = withDefaults(defineProps<Props>(), {
|
const props = withDefaults(defineProps<Props>(), {
|
||||||
@@ -150,14 +113,7 @@ const props = withDefaults(defineProps<Props>(), {
|
|||||||
sendText: "发送",
|
sendText: "发送",
|
||||||
stopText: "停止",
|
stopText: "停止",
|
||||||
retryText: "重试",
|
retryText: "重试",
|
||||||
maxLength: 2000,
|
maxLength: 2000
|
||||||
voiceEnabled: false,
|
|
||||||
voiceState: "idle",
|
|
||||||
voiceText: "",
|
|
||||||
voiceSendHint: "松开发送",
|
|
||||||
voiceCancelHint: "松开取消",
|
|
||||||
voiceListeningText: "正在听你说…",
|
|
||||||
voiceProcessingText: "正在识别…"
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const draft = defineModel<string>({ required: true });
|
const draft = defineModel<string>({ required: true });
|
||||||
@@ -167,44 +123,8 @@ const emit = defineEmits<{
|
|||||||
retry: [];
|
retry: [];
|
||||||
"new-conversation": [];
|
"new-conversation": [];
|
||||||
"add-attachment": [];
|
"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 {
|
function startNewConversation(): void {
|
||||||
if (props.disabled || props.isGenerating) {
|
if (props.disabled || props.isGenerating) {
|
||||||
return;
|
return;
|
||||||
@@ -231,90 +151,6 @@ function submitMessage(): void {
|
|||||||
draft.value = "";
|
draft.value = "";
|
||||||
emit("send", message);
|
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>
|
</script>
|
||||||
|
|
||||||
<style scoped lang="less">
|
<style scoped lang="less">
|
||||||
@@ -357,12 +193,6 @@ onBeforeUnmount(() => {
|
|||||||
border-radius: 1.25rem;
|
border-radius: 1.25rem;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
|
|
||||||
.input-wrap {
|
|
||||||
position: relative;
|
|
||||||
flex: 1;
|
|
||||||
min-width: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.input {
|
.input {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
@@ -370,74 +200,6 @@ onBeforeUnmount(() => {
|
|||||||
min-width: 0;
|
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 {
|
.actions {
|
||||||
--td-button-border-radius: .75rem;
|
--td-button-border-radius: .75rem;
|
||||||
|
|
||||||
@@ -460,19 +222,6 @@ onBeforeUnmount(() => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@keyframes voice-wave {
|
|
||||||
0%,
|
|
||||||
100% {
|
|
||||||
height: 25%;
|
|
||||||
opacity: .5;
|
|
||||||
}
|
|
||||||
|
|
||||||
50% {
|
|
||||||
height: 100%;
|
|
||||||
opacity: 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
:global(html.theme-dark) {
|
:global(html.theme-dark) {
|
||||||
.error-bar {
|
.error-bar {
|
||||||
color: var(--ai-chat-error-color-dark, #fda29b);
|
color: var(--ai-chat-error-color-dark, #fda29b);
|
||||||
|
|||||||
Reference in New Issue
Block a user