Blame view

src/layouts/default/tabs/useMultipleTabs.ts 2.19 KB
1
import { toRaw, ref, nextTick } from 'vue';
vben authored
2
import { RouteLocationNormalized } from 'vue-router';
vben authored
3
import { useDesign } from '/@/hooks/web/useDesign';
4
import { useSortable } from '/@/hooks/web/useSortable';
5
import router from '/@/router';
vben authored
6
7
import { tabStore } from '/@/store/modules/tab';
import { isNullAndUnDef } from '/@/utils/is';
8
import projectSetting from '/@/settings/projectSetting';
9
vben authored
10
11
export function initAffixTabs(): string[] {
  const affixList = ref<RouteLocationNormalized[]>([]);
12
13
14
  /**
   * @description: Filter all fixed routes
   */
vben authored
15
16
  function filterAffixTabs(routes: RouteLocationNormalized[]) {
    const tabs: RouteLocationNormalized[] = [];
17
18
19
    routes &&
      routes.forEach((route) => {
        if (route.meta && route.meta.affix) {
vben authored
20
          tabs.push(toRaw(route));
21
22
23
24
25
26
27
28
29
        }
      });
    return tabs;
  }

  /**
   * @description: Set fixed tabs
   */
  function addAffixTabs(): void {
vben authored
30
    const affixTabs = filterAffixTabs((router.getRoutes() as unknown) as RouteLocationNormalized[]);
31
    affixList.value = affixTabs;
32
    for (const tab of affixTabs) {
vben authored
33
34
35
36
37
      tabStore.addTabAction(({
        meta: tab.meta,
        name: tab.name,
        path: tab.path,
      } as unknown) as RouteLocationNormalized);
38
39
    }
  }
40
41
42
43
44
45
  let isAddAffix = false;
  if (!isAddAffix) {
    addAffixTabs();
    isAddAffix = true;
  }
Vben authored
46
  return affixList.value.map((item) => item.meta?.title).filter(Boolean) as string[];
47
}
vben authored
48
49

export function useTabsDrag(affixTextList: string[]) {
50
  const { multiTabsSetting } = projectSetting;
vben authored
51
vben authored
52
  const { prefixCls } = useDesign('multiple-tabs');
53
  nextTick(() => {
vben authored
54
    if (!multiTabsSetting.canDrag) return;
55
56
57
58
59
60
61
62
63
    const el = document.querySelectorAll(`.${prefixCls} .ant-tabs-nav > div`)?.[0] as HTMLElement;
    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
64
65
66
67
        if (isNullAndUnDef(oldIndex) || isNullAndUnDef(newIndex) || oldIndex === newIndex) {
          return;
        }
vben authored
68
69
70
        tabStore.commitSortTabs({ oldIndex, newIndex });
      },
vben authored
71
    });
72
    initSortable();
vben authored
73
74
  });
}