vben
authored
|
1
|
import { toRaw, ref, nextTick } from 'vue';
|
Vben
authored
|
2
|
import type { RouteLocationNormalized } from 'vue-router';
|
vben
authored
|
3
|
import { useDesign } from '/@/hooks/web/useDesign';
|
vben
authored
|
4
|
import { useSortable } from '/@/hooks/web/useSortable';
|
Vben
authored
|
5
|
import { useMultipleTabStore } from '/@/store/modules/multipleTab';
|
vben
authored
|
6
|
import { isNullAndUnDef } from '/@/utils/is';
|
Vben
authored
|
7
|
import projectSetting from '/@/settings/projectSetting';
|
Vben
authored
|
8
|
import { useRouter } from 'vue-router';
|
vben
authored
|
9
|
|
vben
authored
|
10
11
|
export function initAffixTabs(): string[] {
const affixList = ref<RouteLocationNormalized[]>([]);
|
Vben
authored
|
12
13
14
|
const tabStore = useMultipleTabStore();
const router = useRouter();
|
vben
authored
|
15
16
17
|
/**
* @description: Filter all fixed routes
*/
|
vben
authored
|
18
19
|
function filterAffixTabs(routes: RouteLocationNormalized[]) {
const tabs: RouteLocationNormalized[] = [];
|
vben
authored
|
20
21
22
|
routes &&
routes.forEach((route) => {
if (route.meta && route.meta.affix) {
|
vben
authored
|
23
|
tabs.push(toRaw(route));
|
vben
authored
|
24
25
26
27
28
29
30
31
32
|
}
});
return tabs;
}
/**
* @description: Set fixed tabs
*/
function addAffixTabs(): void {
|
Vben
authored
|
33
|
const affixTabs = filterAffixTabs(router.getRoutes() as unknown as RouteLocationNormalized[]);
|
vben
authored
|
34
|
affixList.value = affixTabs;
|
vben
authored
|
35
|
for (const tab of affixTabs) {
|
Vben
authored
|
36
|
tabStore.addTab({
|
vben
authored
|
37
38
39
|
meta: tab.meta,
name: tab.name,
path: tab.path,
|
Vben
authored
|
40
|
} as unknown as RouteLocationNormalized);
|
vben
authored
|
41
42
|
}
}
|
vben
authored
|
43
|
|
vben
authored
|
44
|
let isAddAffix = false;
|
Vben
authored
|
45
|
|
vben
authored
|
46
47
48
49
|
if (!isAddAffix) {
addAffixTabs();
isAddAffix = true;
}
|
Vben
authored
|
50
|
return affixList.value.map((item) => item.meta?.title).filter(Boolean) as string[];
|
vben
authored
|
51
|
}
|
vben
authored
|
52
53
|
export function useTabsDrag(affixTextList: string[]) {
|
Vben
authored
|
54
|
const tabStore = useMultipleTabStore();
|
Vben
authored
|
55
|
const { multiTabsSetting } = projectSetting;
|
vben
authored
|
56
|
const { prefixCls } = useDesign('multiple-tabs');
|
vben
authored
|
57
|
nextTick(() => {
|
vben
authored
|
58
|
if (!multiTabsSetting.canDrag) return;
|
|
59
60
61
|
const el = document.querySelectorAll(
`.${prefixCls} .ant-tabs-nav-wrap > div`,
)?.[0] as HTMLElement;
|
vben
authored
|
62
63
64
65
66
67
68
69
|
const { initSortable } = useSortable(el, {
filter: (e: ChangeEvent) => {
const text = e?.target?.innerText;
if (!text) return false;
return affixTextList.includes(text);
},
onEnd: (evt) => {
const { oldIndex, newIndex } = evt;
|
vben
authored
|
70
|
|
vben
authored
|
71
72
73
|
if (isNullAndUnDef(oldIndex) || isNullAndUnDef(newIndex) || oldIndex === newIndex) {
return;
}
|
vben
authored
|
74
|
|
Vben
authored
|
75
|
tabStore.sortTabs(oldIndex, newIndex);
|
vben
authored
|
76
|
},
|
vben
authored
|
77
|
});
|
vben
authored
|
78
|
initSortable();
|
vben
authored
|
79
80
|
});
}
|