fix: 优化助手输入区发送/展开与浅色对比,并精简侧栏展开态图标

This commit is contained in:
2026-09-26 00:19:36 +08:00
parent 67d01c678a
commit 883a1d0cfe
2 changed files with 368 additions and 52 deletions
@@ -1,7 +1,8 @@
<script lang="ts" setup>
import { computed, nextTick, onBeforeUnmount, ref, watch } from 'vue';
import { message } from 'ant-design-vue';
import { createIconifyIcon } from '@vben/icons';
import { Tooltip, message } from 'ant-design-vue';
import { ChatSender } from '@tdesign-vue-next/chat';
import { getProductPageApi } from '#/api/ec/product';
@@ -19,11 +20,15 @@ import {
type CatalogCiteKind,
} from './catalog-cite';
const ArrowUpIcon = createIconifyIcon('lucide:arrow-up');
const ExpandIcon = createIconifyIcon('lucide:maximize-2');
const CollapseIcon = createIconifyIcon('lucide:minimize-2');
/** 包一层 ChatSender:@ 先选分类,选中后在输入行内嵌标签。 */
const draft = defineModel<string>({ required: true });
const cites = defineModel<CatalogCite[]>('cites', { required: true });
defineProps<{
const props = defineProps<{
loading?: boolean;
}>();
@@ -39,6 +44,10 @@ const dismissed = ref(false);
const loadingList = ref(false);
const activeKey = ref('');
const items = ref<CatalogCite[]>([]);
/** 输入框是否已手动展开 */
const expanded = ref(false);
/** 内容是否超过 1 行(用于显示右上角展开按钮) */
const multiLine = ref(false);
let timer: ReturnType<typeof setTimeout> | undefined;
let seq = 0;
@@ -47,6 +56,19 @@ const menuOpen = computed(
() => mentionQuery.value !== null && !dismissed.value,
);
/** 未展开:1~14 行自动增高;展开后再抬高上限 */
const textareaAutosize = computed(() =>
expanded.value
? { minRows: 8, maxRows: 28 }
: { minRows: 1, maxRows: 14 },
);
const showExpandToggle = computed(() => expanded.value || multiLine.value);
const canSend = computed(
() => !props.loading && !!(draft.value || '').trim(),
);
watch(mentionQuery, (query, prev) => {
if (query !== prev) {
dismissed.value = false;
@@ -67,6 +89,14 @@ watch(kind, () => {
scheduleSearch(mentionQuery.value);
});
watch(draft, () => {
checkMultiLine();
});
watch(expanded, () => {
void nextTick(() => checkMultiLine());
});
function scheduleSearch(query: string) {
if (timer) clearTimeout(timer);
loadingList.value = true;
@@ -131,7 +161,93 @@ async function safeList(task: Promise<CatalogCite[]>) {
}
}
/** 按可视行数判断:超过 1 行就稳定显示展开按钮(不依赖 scroll overflow,避免 autosize 时闪烁消失) */
function checkMultiLine() {
const run = () => {
const el = rootRef.value?.querySelector('textarea');
if (!el) {
multiLine.value = false;
return;
}
if (expanded.value) {
multiLine.value = true;
return;
}
const text = draft.value || '';
if (!text) {
multiLine.value = false;
return;
}
// 显式换行
if (text.includes('\n')) {
multiLine.value = true;
return;
}
const style = window.getComputedStyle(el);
const lineHeight = parseFloat(style.lineHeight);
const fontSize = parseFloat(style.fontSize) || 14;
const lh =
Number.isFinite(lineHeight) && lineHeight > 0 ? lineHeight : fontSize * 1.5;
const padY =
(parseFloat(style.paddingTop) || 0) +
(parseFloat(style.paddingBottom) || 0);
// scrollHeight 在 autosize 下仍反映内容总高度
const rows = Math.max(1, Math.round((el.scrollHeight - padY) / lh));
multiLine.value = rows > 1;
};
// 等 autosize 改完高度再量,避免中间态误判
void nextTick(() => {
requestAnimationFrame(run);
});
}
function toggleExpand() {
expanded.value = !expanded.value;
focusInput();
}
function onSendClick() {
const text = draft.value;
if (!text?.trim() || props.loading) return;
emit('send', text);
resetExpandAfterSend();
}
function onChatSend(value: string) {
emit('send', value);
resetExpandAfterSend();
}
function resetExpandAfterSend() {
expanded.value = false;
multiLine.value = false;
}
function onStopClick() {
emit('stop');
}
function onKeydown(event: KeyboardEvent) {
// 光标在输入框开头(或内容为空)时,Backspace 删掉最后一个引用 chip
if (
event.key === 'Backspace' &&
!event.isComposing &&
event.keyCode !== 229 &&
cites.value.length > 0
) {
const el = rootRef.value?.querySelector('textarea');
if (el) {
const atStart = el.selectionStart === 0 && el.selectionEnd === 0;
if (atStart || !draft.value) {
event.preventDefault();
event.stopPropagation();
const last = cites.value[cites.value.length - 1];
if (last) removeCite(last.key);
return;
}
}
}
if (!menuOpen.value || event.isComposing || event.keyCode === 229) return;
if (event.key === 'ArrowDown') {
event.preventDefault();
@@ -228,17 +344,40 @@ onBeforeUnmount(() => {
</script>
<template>
<div ref="rootRef" class="catalog-cite-sender relative" @keydown.capture="onKeydown">
<div
ref="rootRef"
class="catalog-cite-sender relative"
:class="{ 'catalog-cite-sender--expandable': showExpandToggle }"
@keydown.capture="onKeydown"
>
<!-- 展开/收起固定在输入框右上角 -->
<Tooltip
v-if="showExpandToggle"
:title="expanded ? '收起输入框' : '展开输入框'"
placement="top"
>
<button
type="button"
class="sender-expand-corner text-muted-foreground hover:text-foreground hover:bg-muted"
:aria-label="expanded ? '收起输入框' : '展开输入框'"
@mousedown.prevent
@click="toggleExpand"
>
<CollapseIcon v-if="expanded" class="size-4" />
<ExpandIcon v-else class="size-4" />
</button>
</Tooltip>
<div
v-if="menuOpen"
class="border-border bg-card absolute bottom-full left-0 z-20 mb-2 w-80 overflow-hidden rounded-lg border shadow-lg"
class="border-border bg-popover absolute bottom-full left-0 z-20 mb-2 w-80 overflow-hidden rounded-lg border shadow-lg"
>
<div class="flex gap-1 p-1.5">
<div class="flex shrink-0 gap-1 p-1.5">
<button
v-for="item in kinds"
:key="item"
type="button"
class="flex-1 rounded-md px-2 py-1.5 text-xs"
class="flex-1 cursor-pointer rounded-md px-2 py-1.5 text-xs"
:class="
item === kind
? 'bg-primary/10 text-primary font-medium'
@@ -249,16 +388,17 @@ onBeforeUnmount(() => {
{{ catalogCiteLabel(item) }}
</button>
</div>
<div class="max-h-72 overflow-y-auto py-1">
<!-- 固定高度,避免加载前后弹层跳动 -->
<div class="cite-menu-list h-72 overflow-y-auto py-1">
<div
v-if="loadingList && items.length === 0"
class="text-muted-foreground px-3 py-6 text-center text-xs"
class="text-muted-foreground flex h-full items-center justify-center px-3 text-center text-xs"
>
搜索中…
</div>
<div
v-else-if="items.length === 0"
class="text-muted-foreground px-3 py-6 text-center text-xs"
class="text-muted-foreground flex h-full items-center justify-center px-3 text-center text-xs"
>
这个分类下没有匹配
</div>
@@ -268,7 +408,7 @@ onBeforeUnmount(() => {
:key="item.key"
type="button"
:data-cite-key="item.key"
class="flex w-full flex-col px-3 py-1.5 text-left"
class="flex w-full cursor-pointer flex-col px-3 py-1.5 text-left"
:class="item.key === activeKey ? 'bg-primary/10' : 'hover:bg-muted'"
@mouseenter="activeKey = item.key"
@mousedown.prevent="pick(item)"
@@ -284,7 +424,7 @@ onBeforeUnmount(() => {
</template>
</div>
<div class="text-muted-foreground border-border border-t px-3 py-1.5 text-[11px]">
↑↓ 选择 · Enter 引用 · Esc 关闭
↑↓ 选择 · Enter 引用 · Esc 关闭 · ⌫ 删引用
</div>
</div>
@@ -293,8 +433,8 @@ onBeforeUnmount(() => {
class="agent-td-sender"
:loading="loading"
placeholder="问点什么,或用 @ 引用店铺 / 商品 / SKU"
:textarea-props="{ autosize: { minRows: 2, maxRows: 6 } }"
@send="emit('send', $event)"
:textarea-props="{ autosize: textareaAutosize }"
@send="onChatSend"
@stop="emit('stop')"
>
<template #input-prefix>
@@ -308,7 +448,7 @@ onBeforeUnmount(() => {
<span class="truncate">{{ cite.title }}</span>
<button
type="button"
class="opacity-60 hover:opacity-100"
class="cursor-pointer opacity-60 hover:opacity-100"
aria-label="移除引用"
@mousedown.prevent
@click="removeCite(cite.key)"
@@ -319,13 +459,44 @@ onBeforeUnmount(() => {
</div>
</template>
<template #footer-prefix>
<button
type="button"
class="text-muted-foreground hover:text-primary rounded px-1 py-1 text-xs"
@click="insertMention"
>
@ 引用
</button>
<div class="catalog-cite-footer flex min-w-0 flex-1 items-center gap-2">
<button
type="button"
class="text-muted-foreground hover:text-primary shrink-0 cursor-pointer rounded px-1 py-1 text-xs"
@click="insertMention"
>
@ 引用
</button>
<slot name="footer-extra"></slot>
</div>
</template>
<template #suffix>
<div class="sender-suffix flex items-center gap-1">
<Tooltip v-if="loading" title="停止生成">
<button
type="button"
class="sender-send-btn sender-send-btn--stop"
aria-label="停止生成"
@mousedown.prevent
@click="onStopClick"
>
<span class="sender-stop-square" />
</button>
</Tooltip>
<Tooltip v-else title="发送">
<button
type="button"
class="sender-send-btn"
:class="{ 'sender-send-btn--disabled': !canSend }"
:disabled="!canSend"
aria-label="发送"
@mousedown.prevent
@click="onSendClick"
>
<ArrowUpIcon class="size-4" />
</button>
</Tooltip>
</div>
</template>
</ChatSender>
</div>
@@ -335,14 +506,32 @@ onBeforeUnmount(() => {
.agent-td-sender {
width: 100%;
}
.catalog-cite-sender :deep(.t-chat-sender__textarea__wrapper) {
align-items: flex-start;
padding: 0;
margin-bottom: 0 !important;
}
.catalog-cite-sender :deep(.t-textarea) {
flex: 1;
width: auto;
min-width: 0;
}
.catalog-cite-sender :deep(.t-textarea__inner) {
padding: 0 !important;
}
.catalog-cite-sender--expandable :deep(.t-textarea__inner) {
/* 给右上角展开按钮留出点击空间,避免文字压住图标 */
padding-right: 28px !important;
}
.catalog-cite-sender :deep(.t-chat-sender__footer) {
align-items: center;
}
.cite-inline {
display: flex;
flex-shrink: 0;
@@ -354,4 +543,62 @@ onBeforeUnmount(() => {
margin-right: 8px;
overflow-x: auto;
}
.sender-expand-corner {
position: absolute;
top: 8px;
right: 8px;
z-index: 5;
display: inline-flex;
align-items: center;
justify-content: center;
width: 28px;
height: 28px;
cursor: pointer;
border: none;
border-radius: 999px;
background: transparent;
transition:
background-color 0.15s,
color 0.15s;
}
.sender-send-btn {
display: inline-flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
cursor: pointer;
border: none;
border-radius: 999px;
color: hsl(var(--primary-foreground, 0 0% 100%));
background: hsl(var(--primary));
transition:
opacity 0.15s,
transform 0.15s;
}
.sender-send-btn:hover:not(:disabled) {
opacity: 0.9;
}
.sender-send-btn--disabled,
.sender-send-btn:disabled {
cursor: not-allowed;
opacity: 0.4;
}
.sender-send-btn--stop {
color: hsl(var(--foreground));
background: hsl(var(--muted));
}
.sender-stop-square {
display: block;
width: 10px;
height: 10px;
border-radius: 2px;
background: currentColor;
}
</style>
@@ -603,9 +603,8 @@ void loadEnabledModels().then(() => refreshSessions());
v-if="!siderCollapsed"
class="flex h-12 shrink-0 items-center gap-1 px-3"
>
<div class="flex min-w-0 flex-1 items-center gap-2 truncate text-sm font-semibold">
<AgentBrandIcon class="size-5 shrink-0" />
<span class="truncate">智能助手</span>
<div class="min-w-0 flex-1 truncate text-sm font-semibold">
智能助手
</div>
<Tooltip title="搜索会话">
<button
@@ -686,11 +685,11 @@ void loadEnabledModels().then(() => refreshSessions());
v-for="s in recentSessions"
:key="s.threadId"
type="button"
class="mb-0.5 w-full rounded-lg px-2.5 py-2 text-left text-sm transition-colors"
class="agent-session-item mb-0.5 w-full cursor-pointer rounded-lg px-2.5 py-2 text-left text-sm transition-colors"
:class="
s.threadId === activeThreadId
? 'bg-muted font-medium'
: 'hover:bg-muted/70'
? 'agent-session-item--active'
: ''
"
@click="pickSessionFromOverlay(s.threadId)"
>
@@ -762,11 +761,11 @@ void loadEnabledModels().then(() => refreshSessions());
v-for="s in group.items"
:key="s.threadId"
type="button"
class="mb-0.5 w-full rounded-lg px-3 py-2 text-left text-sm transition-colors"
class="agent-session-item mb-0.5 w-full cursor-pointer rounded-lg px-3 py-2 text-left text-sm transition-colors"
:class="
s.threadId === activeThreadId
? 'bg-muted font-medium'
: 'hover:bg-muted/70 text-foreground'
? 'agent-session-item--active'
: ''
"
@click="onSessionClick(s.threadId)"
>
@@ -782,22 +781,6 @@ void loadEnabledModels().then(() => refreshSessions());
<!-- 主区:空态居中 / 对话时消息 + 底栏输入 -->
<section class="agent-main relative flex min-h-0 min-w-0 flex-1 flex-col">
<header
class="absolute right-4 top-3 z-20 flex items-center gap-2"
>
<Select
:value="selectedModelKey"
:options="modelOptions"
:loading="patchingModel"
:disabled="sending || enabledModels.length === 0"
placeholder="模型"
size="small"
class="agent-model-select w-48"
:allow-clear="false"
@change="(v) => onModelChange(String(v ?? ''))"
/>
</header>
<div
v-if="showMessageLoading"
class="text-muted-foreground absolute inset-0 z-10 flex items-center justify-center bg-background/60 text-sm"
@@ -820,7 +803,21 @@ void loadEnabledModels().then(() => refreshSessions());
:loading="sending"
@send="onSenderSend"
@stop="onSenderStop"
/>
>
<template #footer-extra>
<Select
:value="selectedModelKey"
:options="modelOptions"
:loading="patchingModel"
:disabled="sending || enabledModels.length === 0"
placeholder="模型"
size="small"
class="agent-model-select min-w-0 max-w-[200px] flex-1"
:allow-clear="false"
@change="(v) => onModelChange(String(v ?? ''))"
/>
</template>
</CatalogCiteSender>
<div class="mt-5 flex flex-wrap justify-center gap-2">
<Tooltip
v-for="item in starterPrompts"
@@ -829,7 +826,7 @@ void loadEnabledModels().then(() => refreshSessions());
>
<button
type="button"
class="hover:bg-muted border-border text-foreground/80 hover:text-foreground inline-flex items-center gap-2 rounded-full border bg-card px-3.5 py-1.5 text-sm transition-colors"
class="hover:bg-muted border-border text-foreground/80 hover:text-foreground inline-flex cursor-pointer items-center gap-2 rounded-full border bg-card px-3.5 py-1.5 text-sm transition-colors"
@click="applyStarterPrompt(item.prompt)"
>
<component :is="item.icon" class="text-muted-foreground" />
@@ -844,7 +841,7 @@ void loadEnabledModels().then(() => refreshSessions());
<template v-else>
<div
ref="messagePaneRef"
class="agent-td-chat t-chat t-chat--normal min-h-0 flex-1 overflow-y-auto px-4 pb-4 pt-14"
class="agent-td-chat t-chat t-chat--normal min-h-0 flex-1 overflow-y-auto px-4 pb-4 pt-6"
>
<Empty
v-if="!showMessageLoading && messages.length === 0"
@@ -882,7 +879,21 @@ void loadEnabledModels().then(() => refreshSessions());
:loading="sending"
@send="onSenderSend"
@stop="onSenderStop"
/>
>
<template #footer-extra>
<Select
:value="selectedModelKey"
:options="modelOptions"
:loading="patchingModel"
:disabled="sending || enabledModels.length === 0"
placeholder="模型"
size="small"
class="agent-model-select min-w-0 max-w-[200px] flex-1"
:allow-clear="false"
@change="(v) => onModelChange(String(v ?? ''))"
/>
</template>
</CatalogCiteSender>
</div>
</div>
</template>
@@ -898,10 +909,37 @@ void loadEnabledModels().then(() => refreshSessions());
</template>
<style scoped>
/* 浅色:侧栏略深于主区;深色沿用 background-deep */
.agent-sider {
background: hsl(var(--muted) / 0.45);
}
html.dark .agent-sider,
.dark .agent-sider {
background: hsl(var(--background-deep, var(--background)));
}
.agent-session-item:hover {
background: hsl(var(--accent));
}
.agent-session-item,
.agent-session-item :deep(*) {
cursor: pointer;
}
.agent-session-item--active {
background: hsl(var(--background));
font-weight: 500;
box-shadow: 0 0 0 1px hsl(var(--border));
}
html.dark .agent-session-item--active,
.dark .agent-session-item--active {
background: hsl(var(--muted));
box-shadow: none;
}
.agent-composer :deep(.t-chat-sender) {
border-radius: 24px;
box-shadow: 0 0 0 1px hsl(var(--border) / 0.8);
@@ -909,8 +947,8 @@ void loadEnabledModels().then(() => refreshSessions());
.agent-composer :deep(.t-chat-sender__textarea__wrapper) {
border-radius: 24px;
padding-block: 10px;
padding-inline: 14px;
padding: 0;
margin-bottom: 0 !important;
}
.agent-model-select :deep(.ant-select-selector) {
@@ -940,4 +978,35 @@ void loadEnabledModels().then(() => refreshSessions());
opacity: 0.55;
font-size: 12px;
}
/* 浅色:给气泡补边框与底色,避免与主背景糊在一起 */
.agent-td-item :deep(.t-chat__text--variant--base .t-chat__detail),
.agent-td-item :deep(.t-chat__detail) {
border: 1px solid hsl(var(--border));
border-radius: 12px;
background: hsl(var(--muted) / 0.65);
padding-block: 10px;
}
.agent-td-item :deep(.t-chat__inner.user .t-chat__text--variant--base .t-chat__detail),
.agent-td-item :deep(.t-chat__inner.user .t-chat__detail) {
background: hsl(var(--primary) / 0.1);
border-color: hsl(var(--primary) / 0.22);
}
html.dark .agent-td-item :deep(.t-chat__text--variant--base .t-chat__detail),
html.dark .agent-td-item :deep(.t-chat__detail),
.dark .agent-td-item :deep(.t-chat__text--variant--base .t-chat__detail),
.dark .agent-td-item :deep(.t-chat__detail) {
border-color: transparent;
background: hsl(var(--muted) / 0.4);
}
html.dark .agent-td-item :deep(.t-chat__inner.user .t-chat__text--variant--base .t-chat__detail),
html.dark .agent-td-item :deep(.t-chat__inner.user .t-chat__detail),
.dark .agent-td-item :deep(.t-chat__inner.user .t-chat__text--variant--base .t-chat__detail),
.dark .agent-td-item :deep(.t-chat__inner.user .t-chat__detail) {
background: hsl(var(--primary) / 0.22);
border-color: transparent;
}
</style>