You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

82 lines
2.3 KiB

import { nextTick, ref, toRaw } from 'vue'
2 years ago
import type { RouteLocationNormalized } from 'vue-router'
import { useRouter } from 'vue-router'
import { useI18n } from '@/hooks/web/useI18n'
2 years ago
import { useDesign } from '@/hooks/web/useDesign'
import { useSortable } from '@/hooks/web/useSortable'
import { useMultipleTabStore } from '@/store/modules/multipleTab'
import { isNil } from '@/utils/is'
2 years ago
import projectSetting from '@/settings/projectSetting'
const { t } = useI18n()
2 years ago
export function initAffixTabs(): string[] {
const affixList = ref<RouteLocationNormalized[]>([])
const tabStore = 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)
2 years ago
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,
2 years ago
} as unknown as RouteLocationNormalized)
}
}
let isAddAffix = false
if (!isAddAffix) {
addAffixTabs()
isAddAffix = true
}
return affixList.value.map(item => item.meta?.title).filter(Boolean)
2 years ago
}
export function useTabsDrag(affixTextList: string[]) {
const tabStore = useMultipleTabStore()
const { multiTabsSetting } = projectSetting
const { prefixCls } = useDesign('multiple-tabs')
nextTick(() => {
if (!multiTabsSetting.canDrag)
return
2 years ago
const el = document.querySelectorAll(`.${prefixCls} .ant-tabs-nav-wrap > div`)?.[0] as HTMLElement
const { initSortable } = useSortable(el, {
filter: (_evt, target: HTMLElement) => {
2 years ago
const text = target.textContent
if (!text)
return false
return affixTextList.map(res => t(res)).includes(text)
2 years ago
},
onEnd: (evt) => {
const { oldIndex, newIndex } = evt
if (isNil(oldIndex) || isNil(newIndex) || oldIndex === newIndex)
2 years ago
return
tabStore.sortTabs(oldIndex, newIndex)
},
2 years ago
})
initSortable()
})
}