import { useNamespace, useSortable } from '@eciol/hooks'; import { isNil } from '@eciol/shared'; import { ChangeEvent } from 'ant-design-vue/lib/_util/EventInterface'; import { nextTick, ref, toRaw } from 'vue'; import type { RouteLocationNormalized } from 'vue-router'; import { useRouter } from 'vue-router'; import { context } from '../../context'; export function initAffixTabs(): string[] { const affixList = ref([]); const tabStore = context.useMultipleTabStore(); const router = useRouter(); /** * @description: Filter all fixed routes */ function filterAffixTabs(routes: RouteLocationNormalized[]) { const tabs: RouteLocationNormalized[] = []; routes && routes.forEach((route) => { if (route.meta && route.meta.affix) { tabs.push(toRaw(route)); } }); return tabs; } /** * @description: Set fixed tabs */ function addAffixTabs(): void { const affixTabs = filterAffixTabs(router.getRoutes() as unknown as RouteLocationNormalized[]); affixList.value = affixTabs; for (const tab of affixTabs) { tabStore.addTab({ meta: tab.meta, name: tab.name, path: tab.path, } as unknown as RouteLocationNormalized); } } let isAddAffix = false; if (!isAddAffix) { addAffixTabs(); isAddAffix = true; } return affixList.value.map((item) => item.meta?.title).filter(Boolean) as string[]; } export function useTabsDrag(affixTextList: string[]) { const tabStore = context.useMultipleTabStore(); const { multiTabsSetting } = context.projectSetting; const { b } = useNamespace('multiple-tabs', false); nextTick(() => { if (!multiTabsSetting.canDrag) return; const el = document.querySelectorAll(`.${b()} .ant-tabs-nav-wrap > div`)?.[0] as HTMLElement; const { initSortable } = useSortable(el, { filter: (e: ChangeEvent) => { const text = e?.target?.innerText; if (!text) return false; return affixTextList.includes(text); }, draggable: '.ant-tabs-tab-with-remove', onEnd: (evt) => { const { oldIndex, newIndex } = evt; if (isNil(oldIndex) || isNil(newIndex) || oldIndex === newIndex) { return; } tabStore.sortTabs(oldIndex, newIndex); }, }); initSortable(); }); }