Files
EasyFlow/easyflow-ui-admin/packages/effects/layouts/src/basic/tabbar/use-tabbar.ts
陈子默 99f792f6de fix: 统一AI详情页面包屑与Tab导航表现
- 为工作流、插件、知识库、聊天助手详情路由恢复面包屑与Tab参与并保持父模块高亮

- 列表卡片跳转统一追加 navTitle 与 pageKey,详情页Tab标题与面包屑优先显示卡片名称

- 工作流设计页、知识库详情页、聊天助手设置页在深链缺少 navTitle 时自动 replace 补写
2026-03-11 20:07:00 +08:00

249 lines
6.1 KiB
TypeScript

import type { RouteLocationNormalizedGeneric } from 'vue-router';
import type { TabDefinition } from '@easyflow/types';
import type { IContextMenuItem } from '@easyflow-core/tabs-ui';
import { computed, ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useContentMaximize, useTabs } from '@easyflow/hooks';
import {
ArrowLeftToLine,
ArrowRightLeft,
ArrowRightToLine,
ExternalLink,
FoldHorizontal,
Fullscreen,
Minimize2,
Pin,
PinOff,
RotateCw,
X,
} from '@easyflow/icons';
import { $t, useI18n } from '@easyflow/locales';
import { getTabKey, useAccessStore, useTabbarStore } from '@easyflow/stores';
import { filterTree } from '@easyflow/utils';
export function useTabbar() {
const router = useRouter();
const route = useRoute();
const accessStore = useAccessStore();
const tabbarStore = useTabbarStore();
const { contentIsMaximize, toggleMaximize } = useContentMaximize();
const {
closeAllTabs,
closeCurrentTab,
closeLeftTabs,
closeOtherTabs,
closeRightTabs,
closeTabByKey,
getTabDisableState,
openTabInNewWindow,
refreshTab,
toggleTabPin,
} = useTabs();
/**
* 当前路径对应的tab的key
*/
const currentActive = computed(() => {
return getTabKey(route);
});
const { locale } = useI18n();
const currentTabs = ref<RouteLocationNormalizedGeneric[]>();
watch(
[
() => tabbarStore.getTabs,
() => tabbarStore.updateTime,
() => locale.value,
],
([tabs]) => {
currentTabs.value = tabs.map((item) => wrapperTabLocale(item));
},
);
/**
* 初始化固定标签页
*/
const initAffixTabs = () => {
const affixTabs = filterTree(router.getRoutes(), (route) => {
return !!route.meta?.affixTab;
});
tabbarStore.setAffixTabs(affixTabs);
};
// 点击tab,跳转路由
const handleClick = (key: string) => {
const { fullPath, path } = tabbarStore.getTabByKey(key);
router.push(fullPath || path);
};
// 关闭tab
const handleClose = async (key: string) => {
await closeTabByKey(key);
};
function resolveNavTitle(navTitleQuery: unknown): string {
const rawValue = Array.isArray(navTitleQuery)
? navTitleQuery[0]
: navTitleQuery;
if (typeof rawValue !== 'string' || !rawValue.trim()) {
return '';
}
try {
return decodeURIComponent(rawValue);
} catch {
return rawValue;
}
}
function wrapperTabLocale(tab: RouteLocationNormalizedGeneric) {
const navTitle = tab?.meta?.navTitle as string | undefined;
return {
...tab,
meta: {
...tab?.meta,
title: navTitle || $t(tab?.meta?.title as string),
},
};
}
watch(
() => accessStore.accessMenus,
() => {
initAffixTabs();
},
{ immediate: true },
);
watch(
() => route.fullPath,
() => {
const routeMeta = route.matched?.[route.matched.length - 1]?.meta;
const navTitle = resolveNavTitle(route.query.navTitle);
const meta = {
...(routeMeta || route.meta),
navTitle,
title: navTitle || (routeMeta?.title ?? route.meta?.title),
};
tabbarStore.addTab({
...route,
meta,
});
},
{ immediate: true },
);
const createContextMenus = (tab: TabDefinition) => {
const {
disabledCloseAll,
disabledCloseCurrent,
disabledCloseLeft,
disabledCloseOther,
disabledCloseRight,
disabledRefresh,
} = getTabDisableState(tab);
const affixTab = tab?.meta?.affixTab ?? false;
const menus: IContextMenuItem[] = [
{
disabled: disabledCloseCurrent,
handler: async () => {
await closeCurrentTab(tab);
},
icon: X,
key: 'close',
text: $t('preferences.tabbar.contextMenu.close'),
},
{
handler: async () => {
await toggleTabPin(tab);
},
icon: affixTab ? PinOff : Pin,
key: 'affix',
text: affixTab
? $t('preferences.tabbar.contextMenu.unpin')
: $t('preferences.tabbar.contextMenu.pin'),
},
{
handler: async () => {
if (!contentIsMaximize.value) {
await router.push(tab.fullPath);
}
toggleMaximize();
},
icon: contentIsMaximize.value ? Minimize2 : Fullscreen,
key: contentIsMaximize.value ? 'restore-maximize' : 'maximize',
text: contentIsMaximize.value
? $t('preferences.tabbar.contextMenu.restoreMaximize')
: $t('preferences.tabbar.contextMenu.maximize'),
},
{
disabled: disabledRefresh,
handler: () => refreshTab(),
icon: RotateCw,
key: 'reload',
text: $t('preferences.tabbar.contextMenu.reload'),
},
{
handler: async () => {
await openTabInNewWindow(tab);
},
icon: ExternalLink,
key: 'open-in-new-window',
separator: true,
text: $t('preferences.tabbar.contextMenu.openInNewWindow'),
},
{
disabled: disabledCloseLeft,
handler: async () => {
await closeLeftTabs(tab);
},
icon: ArrowLeftToLine,
key: 'close-left',
text: $t('preferences.tabbar.contextMenu.closeLeft'),
},
{
disabled: disabledCloseRight,
handler: async () => {
await closeRightTabs(tab);
},
icon: ArrowRightToLine,
key: 'close-right',
separator: true,
text: $t('preferences.tabbar.contextMenu.closeRight'),
},
{
disabled: disabledCloseOther,
handler: async () => {
await closeOtherTabs(tab);
},
icon: FoldHorizontal,
key: 'close-other',
text: $t('preferences.tabbar.contextMenu.closeOther'),
},
{
disabled: disabledCloseAll,
handler: closeAllTabs,
icon: ArrowRightLeft,
key: 'close-all',
text: $t('preferences.tabbar.contextMenu.closeAll'),
},
];
return menus.filter((item) => tabbarStore.getMenuList.includes(item.key));
};
return {
createContextMenus,
currentActive,
currentTabs,
handleClick,
handleClose,
};
}