front/src/components/tab-bar/index.vue

176 lines
4.4 KiB
Vue

<template>
<div class="tab-bar">
<div class="tab-bar-scroll">
<div ref="tagsWrapRef" class="tags-wrap">
<tab-item
v-for="(tag, index) in tagList"
:key="tag.fullPath"
:index="index"
:item-data="tag"
/>
</div>
</div>
</div>
</template>
<script lang="ts" setup>
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue';
import type { RouteLocationNormalized } from 'vue-router';
import { useRoute } from 'vue-router';
import Sortable from 'sortablejs';
import {
listenerRouteChange,
removeRouteListener,
} from '@/utils/route-listener';
import { useAppStore, useTabBarStore } from '@/store';
import tabItem from './tab-item.vue';
const route = useRoute();
const appStore = useAppStore();
const tabBarStore = useTabBarStore();
const tagsWrapRef = ref<HTMLElement | null>(null);
let sortableInstance: Sortable | null = null;
const tagList = computed(() => tabBarStore.getTabList);
function stripEphemeralQuery(fullPath: string) {
return fullPath
.replace(/([?&])_r=\d+/g, '$1')
.replace(/([?&])_refresh=[^&]*/g, '$1')
.replace(/\?&/, '?')
.replace(/[?&]$/, '');
}
function scrollActiveTagIntoView() {
const wrap = tagsWrapRef.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: 'auto' });
} else if (tagRect.right > wrapRect.right - 8) {
wrap.scrollBy({ left: tagRect.right - wrapRect.right + 24, behavior: 'auto' });
}
}
watch(
() => route.fullPath,
async () => {
await nextTick();
scrollActiveTagIntoView();
},
);
listenerRouteChange((nextRoute: RouteLocationNormalized) => {
if (nextRoute.query?._refresh || nextRoute.query?._r) {
return;
}
const exists = tagList.value.some(
(tag) =>
tag.fullPath === nextRoute.fullPath ||
stripEphemeralQuery(tag.fullPath) ===
stripEphemeralQuery(nextRoute.fullPath),
);
if (!nextRoute.meta.noAffix && !exists) {
tabBarStore.updateTabList(nextRoute);
}
nextTick(() => scrollActiveTagIntoView());
}, true);
onMounted(() => {
if (tagsWrapRef.value) {
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) {
tabBarStore.reorderTabs(oldIndex, newIndex);
}
},
});
}
nextTick(() => scrollActiveTagIntoView());
});
onUnmounted(() => {
sortableInstance?.destroy();
sortableInstance = null;
removeRouteListener();
});
</script>
<style lang="less" scoped>
.tab-bar {
position: relative;
height: 100%;
display: flex;
align-items: center;
padding: 0 12px 0 16px;
background-color: var(--color-bg-2);
.tab-bar-scroll {
flex: 1;
height: 100%;
overflow: hidden;
display: flex;
align-items: center;
.tags-wrap {
height: 100%;
display: flex;
align-items: center;
padding: 0 4px;
overflow-x: auto;
white-space: nowrap;
scroll-behavior: smooth;
&::-webkit-scrollbar {
height: 0;
}
:deep(.arco-tag) {
display: inline-flex;
align-items: center;
margin-right: 6px;
cursor: pointer;
border-radius: 16px;
transition:
background-color 0.18s ease,
color 0.18s ease,
box-shadow 0.18s ease,
transform 0.18s 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);
}
}
}
}
}
</style>