front/src/components/tab-bar/index.vue
zzh abe9389f66 feat: 顶栏标签折叠管控与业务页体验优化
标签栏最多展示15个并支持更多折叠/一键关闭;同步官网顾问、工作台关怀、下单与操作扫描等页面改动。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 18:39:55 +08:00

676 lines
17 KiB
Vue

<template>
<div
class="tab-bar"
:class="{
'is-overflow-left': overflowLeft,
'is-overflow-right': overflowRight,
'has-fold': foldedTags.length > 0,
}"
>
<button
v-show="overflowLeft"
type="button"
class="tab-bar-nav tab-bar-nav--left"
title="向左滚动"
@click="scrollByDir(-1)"
>
<icon-left />
</button>
<div
ref="scrollRef"
class="tab-bar-scroll"
@wheel="onWheel"
@scroll="updateOverflow"
>
<div ref="tagsWrapRef" class="tags-wrap">
<tab-item
v-for="entry in visibleTags"
:key="entry.tag.name || entry.tag.fullPath"
:index="entry.index"
:item-data="entry.tag"
/>
</div>
</div>
<button
v-show="overflowRight"
type="button"
class="tab-bar-nav tab-bar-nav--right"
title="向右滚动"
@click="scrollByDir(1)"
>
<icon-right />
</button>
<div class="tab-bar-actions">
<a-dropdown
v-if="foldedTags.length"
trigger="click"
position="br"
:popup-max-height="320"
>
<button type="button" class="tab-bar-action" :title="`已折叠 ${foldedTags.length} 个标签`">
<icon-down />
<span class="tab-bar-action__label">更多</span>
<span class="tab-bar-action__badge">{{ foldedTags.length }}</span>
</button>
<template #content>
<div class="fold-menu">
<button
v-for="entry in foldedTags"
:key="entry.tag.name || entry.tag.fullPath"
type="button"
class="fold-menu-item"
:class="{ 'is-active': isTagActive(entry.tag) }"
@click="gotoTag(entry.tag)"
>
<span class="fold-menu-item__title" :title="entry.tag.title">
{{ entry.tag.title }}
</span>
<span
class="fold-menu-item__close"
title="关闭"
@click.stop="closeTag(entry.tag, entry.index)"
>
<icon-close />
</span>
</button>
<div class="fold-menu-divider" />
<button
type="button"
class="fold-menu-item fold-menu-item--action"
:disabled="!canCloseOthers"
@click="closeOthers"
>
关闭其他
</button>
<button
type="button"
class="fold-menu-item fold-menu-item--action"
:disabled="tagList.length <= 1"
@click="closeAll"
>
关闭全部
</button>
</div>
</template>
</a-dropdown>
<a-dropdown trigger="hover" position="br" :disabled="tagList.length <= 1">
<button
type="button"
class="tab-bar-action tab-bar-action--icon"
:disabled="!canCloseOthers"
title="关闭其他"
>
<icon-close-circle :size="18" />
</button>
<template #content>
<a-doption :disabled="!canCloseOthers" @click="closeOthers">
关闭其他
</a-doption>
<a-doption :disabled="tagList.length <= 1" @click="closeAll">
关闭全部
</a-doption>
</template>
</a-dropdown>
<button
type="button"
class="tab-bar-action tab-bar-action--icon"
:disabled="tagList.length <= 1"
title="关闭全部标签"
@click="closeAll"
>
<icon-folder-delete :size="18" />
</button>
</div>
</div>
</template>
<script lang="ts" setup>
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue';
import type { RouteLocationNormalized } from 'vue-router';
import { useRoute, useRouter } from 'vue-router';
import Sortable from 'sortablejs';
import {
IconLeft,
IconRight,
IconDown,
IconClose,
IconCloseCircle,
IconFolderDelete,
} from '@arco-design/web-vue/es/icon';
import {
listenerRouteChange,
removeRouteListener,
} from '@/utils/route-listener';
import { useTabBarStore } from '@/store';
import type { TagProps } from '@/store/modules/tab-bar/types';
import { DEFAULT_ROUTE_NAME } from '@/router/constants';
import tabItem from './tab-item.vue';
/** 顶栏最多直接展示的标签数,超出收入「更多」 */
const MAX_VISIBLE_TABS = 15;
const route = useRoute();
const router = useRouter();
const tabBarStore = useTabBarStore();
const scrollRef = ref<HTMLElement | null>(null);
const tagsWrapRef = ref<HTMLElement | null>(null);
const overflowLeft = ref(false);
const overflowRight = ref(false);
let sortableInstance: Sortable | null = null;
const tagList = computed(() => tabBarStore.getTabList);
type TabEntry = { tag: TagProps; index: number };
function stripEphemeralQuery(fullPath: string) {
return fullPath
.replace(/([?&])_r=\d+/g, '$1')
.replace(/([?&])_refresh=[^&]*/g, '$1')
.replace(/\?&/, '?')
.replace(/[?&]$/, '');
}
function isTagActive(tag: TagProps) {
const previewPath = tabBarStore.previewingPath;
if (previewPath || tabBarStore.previewingName) {
if (
previewPath &&
(tag.fullPath === previewPath ||
stripEphemeralQuery(tag.fullPath) ===
stripEphemeralQuery(previewPath))
) {
return true;
}
return (
!!tabBarStore.previewingName && tag.name === tabBarStore.previewingName
);
}
if (route.name && tag.name === String(route.name)) return true;
if (route.fullPath === tag.fullPath) return true;
return (
stripEphemeralQuery(route.fullPath) === stripEphemeralQuery(tag.fullPath)
);
}
function findActiveIndex(list: TagProps[]) {
const idx = list.findIndex((tag) => isTagActive(tag));
return idx >= 0 ? idx : 0;
}
/** 首页固定 + 当前页始终可见,其余按顺序填满 15 个 */
const splitTags = computed(() => {
const list = tagList.value;
if (list.length <= MAX_VISIBLE_TABS) {
return {
visible: list.map((tag, index) => ({ tag, index })),
folded: [] as TabEntry[],
};
}
const activeIdx = findActiveIndex(list);
const reserved = new Set<number>([0]);
if (activeIdx > 0) reserved.add(activeIdx);
const indices: number[] = [...reserved];
for (let i = 1; i < list.length && indices.length < MAX_VISIBLE_TABS; i += 1) {
if (!reserved.has(i)) indices.push(i);
}
indices.sort((a, b) => a - b);
const visibleSet = new Set(indices);
return {
visible: indices.map((index) => ({ tag: list[index], index })),
folded: list
.map((tag, index) => ({ tag, index }))
.filter((entry) => !visibleSet.has(entry.index)),
};
});
const visibleTags = computed(() => splitTags.value.visible);
const foldedTags = computed(() => splitTags.value.folded);
const canCloseOthers = computed(() => {
if (tagList.value.length <= 1) return false;
const activeIdx = findActiveIndex(tagList.value);
return tagList.value.some((_, i) => i !== 0 && i !== activeIdx);
});
function updateOverflow() {
const el = scrollRef.value;
if (!el) {
overflowLeft.value = false;
overflowRight.value = false;
return;
}
const max = el.scrollWidth - el.clientWidth;
overflowLeft.value = el.scrollLeft > 2;
overflowRight.value = max > 2 && el.scrollLeft < max - 2;
}
function scrollByDir(dir: -1 | 1) {
const el = scrollRef.value;
if (!el) return;
el.scrollBy({
left: dir * Math.max(160, el.clientWidth * 0.55),
behavior: 'smooth',
});
}
function onWheel(e: WheelEvent) {
const el = scrollRef.value;
if (!el) return;
if (Math.abs(e.deltaY) <= Math.abs(e.deltaX)) return;
const max = el.scrollWidth - el.clientWidth;
if (max <= 0) return;
e.preventDefault();
el.scrollLeft += e.deltaY;
updateOverflow();
}
function scrollActiveTagIntoView() {
const wrap = scrollRef.value;
if (!wrap) return;
const active = wrap.querySelector('.link-activated') as HTMLElement | null;
if (!active) return;
const wrapRect = wrap.getBoundingClientRect();
const tagRect = active.getBoundingClientRect();
if (tagRect.left < wrapRect.left + 8) {
wrap.scrollBy({
left: tagRect.left - wrapRect.left - 24,
behavior: 'smooth',
});
} else if (tagRect.right > wrapRect.right - 8) {
wrap.scrollBy({
left: tagRect.right - wrapRect.right + 24,
behavior: 'smooth',
});
}
updateOverflow();
}
function gotoTag(tag: TagProps) {
if (isTagActive(tag)) return;
tabBarStore.previewNavigate(tag);
if (tag.name) {
router.push({ name: tag.name, query: tag.query });
return;
}
router.push(tag.fullPath);
}
function closeTag(tag: TagProps, idx: number) {
const wasActive = isTagActive(tag);
tabBarStore.deleteTag(idx, tag);
if (wasActive) {
const latest = tagList.value[Math.max(0, idx - 1)];
if (latest?.name) {
router.push({ name: latest.name, query: latest.query });
}
}
}
function closeAll() {
if (tagList.value.length <= 1) return;
tabBarStore.resetTabList();
router.push({ name: DEFAULT_ROUTE_NAME });
}
function closeOthers() {
if (!canCloseOthers.value) return;
const list = tagList.value;
const activeIdx = findActiveIndex(list);
const keep = list.filter((_, i) => i === 0 || i === activeIdx);
tabBarStore.freshTabList(keep);
const current = keep[keep.length - 1];
if (current?.name && !isTagActive(current)) {
router.push({ name: current.name, query: current.query });
}
}
watch(
() => route.fullPath,
async () => {
tabBarStore.clearPreview();
await nextTick();
scrollActiveTagIntoView();
},
);
watch(
() => [tagList.value.length, visibleTags.value.length] as const,
async () => {
await nextTick();
updateOverflow();
},
);
listenerRouteChange((nextRoute: RouteLocationNormalized) => {
if (nextRoute.query?._refresh || nextRoute.query?._r) {
return;
}
if (!nextRoute.meta.noAffix) {
tabBarStore.updateTabList(nextRoute);
}
nextTick(() => scrollActiveTagIntoView());
}, true);
function onWindowResize() {
updateOverflow();
}
function initSortable() {
sortableInstance?.destroy();
sortableInstance = null;
if (!tagsWrapRef.value) return;
sortableInstance = new Sortable(tagsWrapRef.value, {
animation: 160,
filter: '.arco-tag-close-btn, .arco-tag-refresh-btn',
preventOnFilter: true,
onEnd(evt) {
const { oldIndex, newIndex } = evt;
if (oldIndex == null || newIndex == null || oldIndex === newIndex) {
return;
}
const fromReal = visibleTags.value[oldIndex]?.index;
const toReal = visibleTags.value[newIndex]?.index;
if (fromReal == null || toReal == null || fromReal === toReal) return;
tabBarStore.reorderTabs(fromReal, toReal);
},
});
}
onMounted(() => {
initSortable();
window.addEventListener('resize', onWindowResize, { passive: true });
nextTick(() => {
scrollActiveTagIntoView();
updateOverflow();
});
});
watch(
() => visibleTags.value.map((e) => e.tag.name || e.tag.fullPath).join('|'),
async () => {
await nextTick();
initSortable();
},
);
onUnmounted(() => {
sortableInstance?.destroy();
sortableInstance = null;
window.removeEventListener('resize', onWindowResize);
removeRouteListener();
});
</script>
<style lang="less" scoped>
.tab-bar {
position: relative;
height: 100%;
display: flex;
align-items: center;
gap: 2px;
padding: 0 6px 0 8px;
background: transparent;
&.is-overflow-left::before,
&.is-overflow-right::after {
content: '';
position: absolute;
top: 0;
bottom: 0;
width: 20px;
pointer-events: none;
z-index: 1;
}
&.is-overflow-left::before {
left: 26px;
background: linear-gradient(
90deg,
var(--color-fill-1, #f0f3f4) 10%,
transparent
);
}
&.is-overflow-right::after {
right: 72px;
background: linear-gradient(
270deg,
var(--color-fill-1, #f0f3f4) 10%,
transparent
);
}
&.has-fold.is-overflow-right::after {
right: 140px;
}
.tab-bar-nav {
flex-shrink: 0;
width: 22px;
height: 22px;
padding: 0;
border: none;
border-radius: 4px;
background: transparent;
color: var(--color-text-4);
display: inline-flex;
align-items: center;
justify-content: center;
cursor: pointer;
z-index: 2;
transition: background-color 0.15s ease, color 0.15s ease;
&:hover {
color: var(--color-text-2);
background: var(--color-fill-2);
}
}
.tab-bar-scroll {
flex: 1;
min-width: 0;
height: 100%;
overflow-x: auto;
overflow-y: hidden;
display: flex;
align-items: center;
scroll-behavior: auto;
&::-webkit-scrollbar {
height: 0;
}
.tags-wrap {
height: 100%;
display: flex;
align-items: center;
padding: 0 2px;
white-space: nowrap;
:deep(.arco-tag) {
display: inline-flex;
align-items: center;
margin-right: 4px;
cursor: pointer;
border-radius: 4px;
height: 26px;
line-height: 26px;
padding: 0 8px;
font-size: 12px;
background: transparent;
border: 1px solid transparent;
color: var(--color-text-2);
transition:
background-color 0.15s ease,
color 0.15s ease,
border-color 0.15s ease;
.tag-link {
cursor: grab;
}
&.sortable-ghost {
opacity: 0.4;
}
&.sortable-drag {
cursor: grabbing;
}
&:first-child {
.arco-tag-close-btn {
display: none;
}
}
&:not(.link-activated):hover {
background-color: var(--color-fill-2);
border-color: transparent;
}
}
}
}
.tab-bar-actions {
flex-shrink: 0;
display: inline-flex;
align-items: center;
gap: 4px;
margin-left: 4px;
padding-left: 6px;
border-left: 1px solid var(--color-border-1);
}
.tab-bar-action {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 4px;
height: 28px;
min-width: 28px;
padding: 0 8px;
border: none;
border-radius: 4px;
background: transparent;
color: var(--color-text-2);
font-size: 13px;
cursor: pointer;
transition: background-color 0.15s ease, color 0.15s ease;
&:hover:not(:disabled) {
color: var(--color-text-1);
background: var(--color-fill-2);
}
&:disabled {
opacity: 0.35;
cursor: not-allowed;
}
&--icon {
padding: 0;
width: 28px;
:deep(svg) {
width: 18px;
height: 18px;
}
}
&__label {
line-height: 1;
}
&__badge {
min-width: 16px;
height: 16px;
padding: 0 4px;
border-radius: 8px;
background: var(--color-fill-3);
color: var(--color-text-2);
font-size: 11px;
line-height: 16px;
text-align: center;
}
}
}
</style>
<style lang="less">
.fold-menu {
min-width: 160px;
max-width: 240px;
padding: 4px 0;
}
.fold-menu-item {
display: flex;
align-items: center;
width: 100%;
gap: 8px;
padding: 6px 10px;
border: none;
background: transparent;
color: var(--color-text-2);
font-size: 12px;
text-align: left;
cursor: pointer;
&:hover:not(:disabled) {
background: var(--color-fill-2);
}
&:disabled {
opacity: 0.4;
cursor: not-allowed;
}
&.is-active {
color: var(--app-primary-color);
background: var(--app-primary-color-light);
font-weight: 600;
}
&--action {
color: var(--color-text-3);
}
&__title {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
&__close {
flex-shrink: 0;
width: 18px;
height: 18px;
border-radius: 4px;
display: inline-flex;
align-items: center;
justify-content: center;
color: var(--color-text-3);
&:hover {
color: var(--color-text-1);
background: var(--color-fill-3);
}
}
}
.fold-menu-divider {
height: 1px;
margin: 4px 8px;
background: var(--color-border-2);
}
</style>