Files
gongxue-base/apps/admin/src/components/RouteDock/dockTabs.ts

32 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { DockTab } from '../../store/app/appTypes';
/** 页签数量上限超出后淘汰最旧的非当前页签LRU 式),避免无限堆积。 */
export const MAX_DOCK_TABS = 20;
function clampToLimit(list: readonly DockTab[], activeKey: string): DockTab[] {
// 未超限时保留原引用,避免触发无谓的 tab 列表重渲染
if (list.length <= MAX_DOCK_TABS) return list as DockTab[];
// 恢复/迁移或新增后超出上限:保留当前页签 + 最新的其余页签LRU 式淘汰)
const active = list.find((tab) => tab.key === activeKey);
const rest = list.filter((tab) => tab.key !== activeKey);
const keptRest = rest.slice(rest.length - (MAX_DOCK_TABS - 1));
return active ? [active, ...keptRest] : keptRest.slice(-MAX_DOCK_TABS);
}
/**
* 路由页签合并:按 pathname 建 tab重复时更新标题并始终把列表压回上限。
*/
export function upsertDockTab(
tabs: readonly DockTab[],
activeKey: string,
label: string,
): DockTab[] {
const existing = tabs.find((tab) => tab.key === activeKey);
if (existing) {
const updated =
existing.label === label ? tabs : tabs.map((tab) => (tab.key === activeKey ? { ...tab, label } : tab));
return clampToLimit(updated, activeKey);
}
return clampToLimit([...tabs, { key: activeKey, label }], activeKey);
}