Blame view

src/layouts/default/tabs/useMultipleTabs.ts 2.33 KB
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';
4
import { useSortable } from '/@/hooks/web/useSortable';
Vben authored
5
import { useMultipleTabStore } from '/@/store/modules/multipleTab';
vben authored
6
import { isNullAndUnDef } from '/@/utils/is';
7
import projectSetting from '/@/settings/projectSetting';
Vben authored
8
import { useRouter } from 'vue-router';
9
vben authored
10
11
export function initAffixTabs(): string[] {
  const affixList = ref<RouteLocationNormalized[]>([]);
Vben authored
12
13
14

  const tabStore = useMultipleTabStore();
  const router = useRouter();
15
16
17
  /**
   * @description: Filter all fixed routes
   */
vben authored
18
19
  function filterAffixTabs(routes: RouteLocationNormalized[]) {
    const tabs: RouteLocationNormalized[] = [];
20
21
22
    routes &&
      routes.forEach((route) => {
        if (route.meta && route.meta.affix) {
vben authored
23
          tabs.push(toRaw(route));
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[]);
34
    affixList.value = affixTabs;
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);
41
42
    }
  }
43
44
  let isAddAffix = false;
Vben authored
45
46
47
48
49
  if (!isAddAffix) {
    addAffixTabs();
    isAddAffix = true;
  }
Vben authored
50
  return affixList.value.map((item) => item.meta?.title).filter(Boolean) as string[];
51
}
vben authored
52
53

export function useTabsDrag(affixTextList: string[]) {
Vben authored
54
  const tabStore = useMultipleTabStore();
55
  const { multiTabsSetting } = projectSetting;
vben authored
56
  const { prefixCls } = useDesign('multiple-tabs');
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;
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
71
72
73
        if (isNullAndUnDef(oldIndex) || isNullAndUnDef(newIndex) || oldIndex === newIndex) {
          return;
        }
vben authored
74
Vben authored
75
        tabStore.sortTabs(oldIndex, newIndex);
76
      },
vben authored
77
    });
78
    initSortable();
vben authored
79
80
  });
}