refactor(menu): improve template structure and readability

Consolidated template elements in menu.vue to enhance code clarity and maintainability. Adjusted formatting for better alignment and readability without altering existing functionality. This refactor aims to streamline the component's structure for future development.

Refs: #1303
This commit is contained in:
wizardchen
2026-05-16 11:56:44 +08:00
committed by lyingbug
parent 6c523144bf
commit a31d26153a
+126 -117
View File
@@ -6,13 +6,12 @@
<img class="logo" src="@/assets/img/weknora.png" alt="">
<sup v-if="isLiteEdition" class="lite-badge">Lite</sup>
</div>
<div class="sidebar-toggle"
@click="uiStore.toggleSidebar"
:title="t('menu.collapseSidebar')">
<div class="sidebar-toggle" @click="uiStore.toggleSidebar" :title="t('menu.collapseSidebar')">
<svg viewBox="0 0 20 20" width="20" height="20" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect x="1.5" y="1.5" width="17" height="17" rx="3" stroke="currentColor" stroke-width="1.2" />
<line x1="7.5" y1="1.5" x2="7.5" y2="18.5" stroke="currentColor" stroke-width="1.2" />
<line x1="4" y1="7.5" x2="4" y2="12.5" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" />
<line x1="4" y1="7.5" x2="4" y2="12.5" stroke="currentColor" stroke-width="1.2"
stroke-linecap="round" />
</svg>
</div>
</div>
@@ -21,11 +20,15 @@
<div class="menu_item sidebar-toggle-item" @click="uiStore.toggleSidebar">
<div class="menu_item-box">
<div class="menu_icon">
<svg class="icon" viewBox="0 0 20 20" width="20" height="20" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect x="1.5" y="1.5" width="17" height="17" rx="3" stroke="currentColor" stroke-width="1.2" />
<svg class="icon" viewBox="0 0 20 20" width="20" height="20" fill="none"
xmlns="http://www.w3.org/2000/svg">
<rect x="1.5" y="1.5" width="17" height="17" rx="3" stroke="currentColor"
stroke-width="1.2" />
<line x1="7.5" y1="1.5" x2="7.5" y2="18.5" stroke="currentColor" stroke-width="1.2" />
<line x1="5" y1="10" x2="3" y2="8" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" />
<line x1="5" y1="10" x2="3" y2="12" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" />
<line x1="5" y1="10" x2="3" y2="8" stroke="currentColor" stroke-width="1.2"
stroke-linecap="round" />
<line x1="5" y1="10" x2="3" y2="12" stroke="currentColor" stroke-width="1.2"
stroke-linecap="round" />
</svg>
</div>
</div>
@@ -36,9 +39,7 @@
<TenantSelector v-if="canAccessAllTenants && !uiStore.sidebarCollapsed" />
<!-- 折叠时右侧拖拽展开手柄 -->
<div v-if="uiStore.sidebarCollapsed"
class="sidebar-drag-handle"
@mousedown="onDragHandleMouseDown" />
<div v-if="uiStore.sidebarCollapsed" class="sidebar-drag-handle" @mousedown="onDragHandleMouseDown" />
<!-- 上半部分:知识库和对话 -->
<div class="menu_top">
@@ -61,30 +62,39 @@
</div>
</t-tooltip>
</div>
<div class="menu_box" :class="{ 'has-submenu': item.children }" v-for="(item, index) in topMenuItems" :key="index">
<div class="menu_box" :class="{ 'has-submenu': item.children }" v-for="(item, index) in topMenuItems"
:key="index">
<t-tooltip :content="item.title" placement="right" :disabled="!uiStore.sidebarCollapsed">
<div @click="handleMenuClick(item.path)"
@mouseenter="mouseenteMenu(item.path)" @mouseleave="mouseleaveMenu(item.path)"
:class="['menu_item', item.childrenPath && item.childrenPath == currentpath ? 'menu_item_c_active' : isMenuItemActive(item.path) ? 'menu_item_active' : '']">
<div class="menu_item-box">
<div class="menu_icon">
<img class="icon" :src="getImgSrc(item.icon == 'zhishiku' ? knowledgeIcon : item.icon == 'agent' ? agentIcon : item.icon == 'organization' ? organizationIcon : item.icon == 'logout' ? logoutIcon : item.icon == 'setting' ? settingIcon : prefixIcon)" alt="">
<div @click="handleMenuClick(item.path)" @mouseenter="mouseenteMenu(item.path)"
@mouseleave="mouseleaveMenu(item.path)"
:class="['menu_item', item.childrenPath && item.childrenPath == currentpath ? 'menu_item_c_active' : isMenuItemActive(item.path) ? 'menu_item_active' : '']">
<div class="menu_item-box">
<div class="menu_icon">
<img class="icon"
:src="getImgSrc(item.icon == 'zhishiku' ? knowledgeIcon : item.icon == 'agent' ? agentIcon : item.icon == 'organization' ? organizationIcon : item.icon == 'logout' ? logoutIcon : item.icon == 'setting' ? settingIcon : prefixIcon)"
alt="">
</div>
<template v-if="!uiStore.sidebarCollapsed">
<span class="menu_title" :title="item.title">{{ item.title }}</span>
<span v-if="item.path === 'organizations' && orgStore.totalPendingJoinRequestCount > 0"
class="menu-pending-badge"
:title="t('organization.settings.pendingJoinRequestsBadge')">{{
orgStore.totalPendingJoinRequestCount }}</span>
<span v-if="item.path === 'creatChat' && batchMode" class="batch-cancel-hint"
@click.stop="exitBatchMode">{{ t('batchManage.cancel') }}</span>
<t-icon v-else-if="item.path === 'creatChat'" name="add" class="menu-create-hint" />
</template>
</div>
<template v-if="!uiStore.sidebarCollapsed">
<span class="menu_title" :title="item.title">{{ item.title }}</span>
<span v-if="item.path === 'organizations' && orgStore.totalPendingJoinRequestCount > 0" class="menu-pending-badge" :title="t('organization.settings.pendingJoinRequestsBadge')">{{ orgStore.totalPendingJoinRequestCount }}</span>
<span v-if="item.path === 'creatChat' && batchMode" class="batch-cancel-hint" @click.stop="exitBatchMode">{{ t('batchManage.cancel') }}</span>
<t-icon v-else-if="item.path === 'creatChat'" name="add" class="menu-create-hint" />
</template>
</div>
</div>
</t-tooltip>
<div ref="submenuscrollContainer" @scroll="handleScroll" class="submenu" v-if="item.children && !uiStore.sidebarCollapsed">
<div ref="submenuscrollContainer" @scroll="handleScroll" class="submenu"
v-if="item.children && !uiStore.sidebarCollapsed">
<!-- 骨架屏占位 -->
<template v-if="loading && groupedSessions.length === 0">
<div v-for="n in 5" :key="'skel-'+n" class="submenu_item_p">
<div v-for="n in 5" :key="'skel-' + n" class="submenu_item_p">
<div class="submenu_item">
<t-skeleton animation="gradient" style="margin-left:18px;width:80%" :row-col="[{ width: '100%', height: '16px' }]" />
<t-skeleton animation="gradient" style="margin-left:14px;width:80%"
:row-col="[{ width: '100%', height: '14px' }]" />
</div>
</div>
</template>
@@ -94,27 +104,21 @@
<div :class="['submenu_item', !batchMode && currentSecondpath == subitem.path ? 'submenu_item_active' : '', batchMode && batchSelectedIds.includes(subitem.id) ? 'submenu_item_selected' : '', batchMode ? 'submenu_item_batch' : '']"
@mouseenter="mouseenteBotDownr(subitem.id)" @mouseleave="mouseleaveBotDown"
@click="batchMode ? toggleBatchSelect(subitem.id) : gotopage(subitem.path)">
<t-checkbox v-if="batchMode"
class="batch-checkbox"
:checked="batchSelectedIds.includes(subitem.id)"
@click.stop
@change="toggleBatchSelect(subitem.id)"
/>
<t-checkbox v-if="batchMode" class="batch-checkbox"
:checked="batchSelectedIds.includes(subitem.id)" @click.stop
@change="toggleBatchSelect(subitem.id)" />
<span class="submenu_title"
:style="batchMode ? 'margin-left:4px;max-width:170px;' : (currentSecondpath == subitem.path ? 'margin-left:18px;max-width:160px;' : 'margin-left:18px;max-width:185px;')">
<t-icon v-if="subitem.is_pinned" name="pin" class="submenu_pin_icon" :title="t('menu.pinned')" />
:style="batchMode ? 'margin-left:4px;max-width:165px;' : (currentSecondpath == subitem.path ? 'margin-left:14px;max-width:155px;' : 'margin-left:14px;max-width:178px;')">
<t-icon v-if="subitem.is_pinned" name="pin" class="submenu_pin_icon"
:title="t('menu.pinned')" />
<img v-if="subitem.im_platform && platformLogo(subitem.im_platform)"
:src="platformLogo(subitem.im_platform)"
:alt="subitem.im_platform"
:title="subitem.im_platform"
class="submenu_source_icon" />
:src="platformLogo(subitem.im_platform)" :alt="subitem.im_platform"
:title="subitem.im_platform" class="submenu_source_icon" />
{{ subitem.title }}
</span>
<t-dropdown v-if="!batchMode"
:options="buildSessionMenuOptions(subitem)"
<t-dropdown v-if="!batchMode" :options="buildSessionMenuOptions(subitem)"
@click="handleSessionMenuClick($event, subitem.originalIndex, subitem)"
placement="bottom-right"
trigger="click">
placement="bottom-right" trigger="click">
<div @click.stop class="menu-more-wrap">
<t-icon name="ellipsis" class="menu-more" />
</div>
@@ -123,24 +127,16 @@
</div>
</template>
</div>
<div v-if="batchMode && item.path === 'creatChat' && !uiStore.sidebarCollapsed" class="batch-inline-footer">
<div v-if="batchMode && item.path === 'creatChat' && !uiStore.sidebarCollapsed"
class="batch-inline-footer">
<div class="batch-footer-left">
<t-checkbox
:checked="isAllBatchSelected"
:indeterminate="isBatchIndeterminate"
@change="toggleBatchSelectAll"
>
<t-checkbox :checked="isAllBatchSelected" :indeterminate="isBatchIndeterminate"
@change="toggleBatchSelectAll">
{{ t('batchManage.selectAll') }}
</t-checkbox>
</div>
<t-button
size="small"
theme="danger"
variant="base"
:disabled="batchSelectedIds.length === 0"
:loading="batchDeleting"
@click="handleInlineBatchDelete"
>
<t-button size="small" theme="danger" variant="base" :disabled="batchSelectedIds.length === 0"
:loading="batchDeleting" @click="handleInlineBatchDelete">
{{ t('batchManage.delete') }}{{ batchSelectedIds.length > 0 ? `(${batchDisplayCount})` : '' }}
</t-button>
</div>
@@ -253,8 +249,8 @@ const canAccessAllTenants = computed(() => authStore.canAccessAllTenants);
// 是否处于知识库详情页(不包括全局聊天)
const isInKnowledgeBase = computed<boolean>(() => {
return route.name === 'knowledgeBaseDetail' ||
route.name === 'kbCreatChat' ||
route.name === 'knowledgeBaseSettings';
route.name === 'kbCreatChat' ||
route.name === 'knowledgeBaseSettings';
});
// 是否在知识库列表页面
@@ -283,8 +279,8 @@ const isMenuItemActive = (itemPath: string): boolean => {
switch (itemPath) {
case 'knowledge-bases':
return currentRoute === 'knowledgeBaseList' ||
currentRoute === 'knowledgeBaseDetail' ||
currentRoute === 'knowledgeBaseSettings';
currentRoute === 'knowledgeBaseDetail' ||
currentRoute === 'knowledgeBaseSettings';
case 'agents':
return currentRoute === 'agentList';
case 'organizations':
@@ -677,7 +673,7 @@ onMounted(async () => {
isLiteEdition.value = true
authStore.setLiteMode(true)
}
}).catch(() => {})
}).catch(() => { })
// 初始化知识库信息
const kbId = (route.params as any)?.kbId as string
@@ -688,7 +684,7 @@ onMounted(async () => {
currentKbName.value = kbRes.data.name || ''
currentKbInfo.value = kbRes.data
}
} catch {}
} catch { }
} else {
currentKbName.value = ''
currentKbInfo.value = null
@@ -717,7 +713,7 @@ watch([() => route.name, () => route.params], (newvalue, oldvalue) => {
// 2. 删除会话后已在 delCard 中处理,不需要在这里刷新
const oldRouteNameStr = typeof oldvalue?.[0] === 'string' ? (oldvalue[0] as string) : (oldvalue?.[0] ? String(oldvalue[0]) : '')
const isCreatingNewSession = (oldRouteNameStr === 'globalCreatChat' || oldRouteNameStr === 'kbCreatChat') &&
nameStr !== 'globalCreatChat' && nameStr !== 'kbCreatChat';
nameStr !== 'globalCreatChat' && nameStr !== 'kbCreatChat';
// 只在创建新会话时才刷新列表
if (isCreatingNewSession) {
@@ -752,31 +748,31 @@ let settingIcon = ref('setting.svg');
let agentIcon = ref('agent.svg');
let organizationIcon = ref('organization.svg');
let pathPrefix = ref(route.name)
const getIcon = (path: string) => {
// 根据当前路由状态更新所有图标
const kbActiveState = getIconActiveState('knowledge-bases');
const creatChatActiveState = getIconActiveState('creatChat');
const settingsActiveState = getIconActiveState('settings');
const agentsActiveState = route.name === 'agentList';
const organizationsActiveState = route.name === 'organizationList';
const getIcon = (path: string) => {
// 根据当前路由状态更新所有图标
const kbActiveState = getIconActiveState('knowledge-bases');
const creatChatActiveState = getIconActiveState('creatChat');
const settingsActiveState = getIconActiveState('settings');
const agentsActiveState = route.name === 'agentList';
const organizationsActiveState = route.name === 'organizationList';
// 知识库图标:只在知识库页面显示绿色
knowledgeIcon.value = kbActiveState.isKbActive ? 'zhishiku-green.svg' : 'zhishiku.svg';
// 知识库图标:只在知识库页面显示绿色
knowledgeIcon.value = kbActiveState.isKbActive ? 'zhishiku-green.svg' : 'zhishiku.svg';
// 智能体图标:只在智能体页面显示绿色
agentIcon.value = agentsActiveState ? 'agent-green.svg' : 'agent.svg';
// 智能体图标:只在智能体页面显示绿色
agentIcon.value = agentsActiveState ? 'agent-green.svg' : 'agent.svg';
// 组织图标:只在组织页面显示绿色
organizationIcon.value = organizationsActiveState ? 'organization-green.svg' : 'organization.svg';
// 组织图标:只在组织页面显示绿色
organizationIcon.value = organizationsActiveState ? 'organization-green.svg' : 'organization.svg';
// 对话图标:只在对话创建页面显示绿色,其他情况显示默认
prefixIcon.value = creatChatActiveState.isCreatChatActive ? 'prefixIcon-green.svg' : 'prefixIcon.svg';
// 对话图标:只在对话创建页面显示绿色,其他情况显示默认
prefixIcon.value = creatChatActiveState.isCreatChatActive ? 'prefixIcon-green.svg' : 'prefixIcon.svg';
// 设置图标:只在设置页面显示绿色
settingIcon.value = settingsActiveState.isSettingsActive ? 'setting-green.svg' : 'setting.svg';
// 设置图标:只在设置页面显示绿色
settingIcon.value = settingsActiveState.isSettingsActive ? 'setting-green.svg' : 'setting.svg';
// 退出图标:始终显示默认
logoutIcon.value = 'logout.svg';
// 退出图标:始终显示默认
logoutIcon.value = 'logout.svg';
}
getIcon(typeof route.name === 'string' ? route.name as string : (route.name ? String(route.name) : ''))
const handleMenuClick = async (path: string) => {
@@ -911,11 +907,13 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
.menu_item {
justify-content: center;
padding: 13px 0;
padding: 10px 0;
.menu_item-box {
justify-content: center;
width: auto;
}
.menu_icon {
margin-right: 0;
}
@@ -975,10 +973,11 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
min-width: 0;
overflow: hidden;
.logo{
.logo {
width: 134px;
height: auto;
}
.lite-badge {
margin-left: 2px;
align-self: flex-start;
@@ -1081,8 +1080,8 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
}
.menu_p {
height: 56px;
padding: 6px 0;
height: 50px;
padding: 4px 0;
box-sizing: border-box;
}
@@ -1091,10 +1090,10 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
display: flex;
align-items: center;
justify-content: space-between;
height: 48px;
padding: 13px 8px 13px 16px;
height: 42px;
padding: 10px 8px 10px 14px;
box-sizing: border-box;
margin-bottom: 4px;
margin-bottom: 3px;
border-radius: 4px;
transition: background-color 0.2s ease;
@@ -1116,12 +1115,12 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
.menu_icon {
display: flex;
margin-right: 10px;
margin-right: 8px;
color: var(--td-text-color-secondary);
.icon {
width: 20px;
height: 20px;
width: 18px;
height: 18px;
overflow: hidden;
}
}
@@ -1133,7 +1132,7 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
font-size: 14px;
font-style: normal;
font-weight: 600;
line-height: 22px;
line-height: 20px;
overflow: hidden;
white-space: nowrap;
max-width: 120px;
@@ -1179,29 +1178,36 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
}
@keyframes menuItemFadeIn {
from { opacity: 0; transform: translateX(-4px); }
to { opacity: 1; transform: translateX(0); }
from {
opacity: 0;
transform: translateX(-4px);
}
to {
opacity: 1;
transform: translateX(0);
}
}
.timeline_header {
font-family: var(--app-font-family);
font-size: 12px;
font-size: 11px;
font-weight: 600;
color: var(--td-text-color-disabled);
padding: 12px 18px 6px 18px;
margin-top: 8px;
line-height: 20px;
padding: 8px 14px 4px 14px;
margin-top: 6px;
line-height: 18px;
user-select: none;
animation: menuItemFadeIn 0.25s ease-out;
&:first-child {
margin-top: 4px;
margin-top: 2px;
}
}
.submenu_item_p {
height: 44px;
padding: 4px 0px 4px 0px;
height: 38px;
padding: 2px 0px 2px 0px;
box-sizing: border-box;
animation: menuItemFadeIn 0.25s ease-out;
}
@@ -1213,10 +1219,10 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
align-items: center;
color: var(--td-text-color-primary);
font-weight: 400;
line-height: 22px;
height: 36px;
line-height: 20px;
height: 32px;
padding-left: 0px;
padding-right: 14px;
padding-right: 10px;
position: relative;
.submenu_title {
@@ -1244,7 +1250,7 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
&:hover {
background: var(--td-bg-color-container-hover);
color: var(--td-text-color-primary);
border-radius: 8px;
border-radius: 6px;
.menu-more {
color: var(--td-text-color-primary);
@@ -1255,7 +1261,7 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
}
.submenu_title {
max-width: 160px !important;
max-width: 155px !important;
}
}
@@ -1264,7 +1270,7 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
.submenu_item_active {
background: var(--td-brand-color-light) !important;
color: var(--td-brand-color) !important;
border-radius: 8px;
border-radius: 6px;
.menu-more {
color: var(--td-brand-color) !important;
@@ -1275,19 +1281,19 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
}
.submenu_title {
max-width: 160px !important;
max-width: 155px !important;
}
}
.submenu_item_batch {
padding-left: 10px;
padding-left: 8px;
cursor: pointer;
user-select: none;
}
.submenu_item_selected {
background: rgba(7, 192, 95, 0.05) !important;
border-radius: 8px;
border-radius: 6px;
}
.batch-checkbox {
@@ -1315,7 +1321,7 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
display: flex;
align-items: center;
justify-content: space-between;
padding: 8px 14px;
padding: 6px 12px;
border-top: 1px solid var(--td-component-stroke);
background: var(--td-bg-color-container);
@@ -1411,8 +1417,8 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
.menu-create-hint {
margin-left: auto;
margin-right: 8px;
font-size: 16px;
margin-right: 6px;
font-size: 15px;
color: var(--td-brand-color);
opacity: 0.7;
transition: opacity 0.2s ease;
@@ -1425,7 +1431,7 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
.menu-cmdk-hint {
margin-left: auto;
margin-right: 8px;
margin-right: 6px;
display: inline-flex;
align-items: center;
gap: 2px;
@@ -1482,14 +1488,17 @@ html[theme-mode="dark"] .aside_box .menu_icon img.icon {
filter: invert(1);
opacity: 0.55;
}
// Hover state: brighter icon like text
html[theme-mode="dark"] .aside_box .menu_item:hover .menu_icon img.icon {
opacity: 0.9;
}
// menu_item_c_active: text is primary, so icon should match
html[theme-mode="dark"] .aside_box .menu_item_c_active .menu_icon img.icon {
opacity: 0.9;
}
// Active (green) icons should not be inverted
html[theme-mode="dark"] .aside_box .menu_item_active .menu_icon img.icon {
filter: none;