import { defineStore } from 'pinia' import { ref, computed } from 'vue' import type { Connection } from '../api/connections' export interface TerminalTab { id: string connectionId: number title: string active: boolean } export const useTerminalTabsStore = defineStore('terminalTabs', () => { const tabs = ref([]) const activeTabId = ref(null) const activeTab = computed(() => tabs.value.find(t => t.id === activeTabId.value) || null) function generateTabId() { return `tab-${Date.now()}-${Math.random().toString(16).slice(2)}` } function openOrFocus(connection: Connection) { // 检查是否已存在该连接的标签页 const existing = tabs.value.find(t => t.connectionId === connection.id) if (existing) { activate(existing.id) return existing.id } // 创建新标签页 const newTab: TerminalTab = { id: generateTabId(), connectionId: connection.id, title: connection.name, active: true, } tabs.value.push(newTab) activate(newTab.id) return newTab.id } function activate(tabId: string) { tabs.value.forEach(t => { t.active = t.id === tabId }) activeTabId.value = tabId } function close(tabId: string) { const index = tabs.value.findIndex(t => t.id === tabId) if (index === -1) return const wasActive = tabs.value[index]!.active tabs.value.splice(index, 1) // 如果关闭的是活动标签,激活相邻标签 if (wasActive && tabs.value.length > 0) { const newIndex = Math.min(index, tabs.value.length - 1) activate(tabs.value[newIndex]!.id) } else if (tabs.value.length === 0) { activeTabId.value = null } } return { tabs, activeTabId, activeTab, openOrFocus, activate, close, } })