import { ref } from 'vue' import { defineStore } from 'pinia' export interface TabItem { path: string title: string closable: boolean } export const HOME_PATH = '/' /** 多页签状态(持久化,刷新保留)。 */ export const useTabsStore = defineStore( 'tabsStore', () => { const tabs = ref([{ path: HOME_PATH, title: '首页', closable: false }]) function addTab(tab: TabItem): void { if (!tabs.value.some((t) => t.path === tab.path)) { tabs.value.push(tab) } } function removeTab(path: string): void { const tab = tabs.value.find((t) => t.path === path) if (!tab || !tab.closable) return tabs.value = tabs.value.filter((t) => t.path !== path) } /** 关闭其他(保留首页与指定 tab)。 */ function removeOthers(path: string): void { tabs.value = tabs.value.filter((t) => !t.closable || t.path === path) } /** 关闭左侧(保留首页、指定 tab 及其右侧)。 */ function removeLeft(path: string): void { const index = tabs.value.findIndex((t) => t.path === path) if (index < 0) return tabs.value = tabs.value.filter((t, i) => !t.closable || i >= index) } /** 关闭右侧(保留首页、指定 tab 及其左侧)。 */ function removeRight(path: string): void { const index = tabs.value.findIndex((t) => t.path === path) if (index < 0) return tabs.value = tabs.value.filter((t, i) => !t.closable || i <= index) } /** 关闭全部(仅保留首页)。 */ function removeAll(): void { tabs.value = tabs.value.filter((t) => !t.closable) } return { tabs, addTab, removeTab, removeOthers, removeLeft, removeRight, removeAll } }, { persist: true }, )