zhou zhou
18 小时以前 fec285d150b377d004e47f0973d298b92fe4c711
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { router } from '@/router'
import { useCommon } from '@/hooks/core/useCommon'
const useWorktabStore = defineStore(
  'worktabStore',
  () => {
    const current = ref({})
    const opened = ref([])
    const keepAliveExclude = ref([])
    const hasOpenedTabs = computed(() => opened.value.length > 0)
    const hasMultipleTabs = computed(() => opened.value.length > 1)
    const currentTabIndex = computed(() =>
      current.value.path ? opened.value.findIndex((tab) => tab.path === current.value.path) : -1
    )
    const findTabIndex = (path) => {
      return opened.value.findIndex((tab) => tab.path === path)
    }
    const getTab = (path) => {
      return opened.value.find((tab) => tab.path === path)
    }
    const isTabClosable = (tab) => {
      return !tab.fixedTab
    }
    const safeRouterPush = (tab) => {
      if (!tab.path) {
        console.warn('尝试跳转到无效路径的标签页')
        return
      }
      try {
        router.push({
          path: tab.path,
          query: tab.query
        })
      } catch (error) {
        console.error('路由跳转失败:', error)
      }
    }
    const openTab = (tab) => {
      if (!tab.path) {
        console.warn('尝试打开无效的标签页')
        return
      }
      if (tab.name) {
        removeKeepAliveExclude(tab.name)
      }
      let existingIndex = -1
      if (tab.name) {
        existingIndex = opened.value.findIndex((t) => t.name === tab.name)
      }
      if (existingIndex === -1) {
        existingIndex = findTabIndex(tab.path)
      }
      if (existingIndex === -1) {
        const insertIndex = tab.fixedTab ? findFixedTabInsertIndex() : opened.value.length
        const newTab = { ...tab }
        if (tab.fixedTab) {
          opened.value.splice(insertIndex, 0, newTab)
        } else {
          opened.value.push(newTab)
        }
        current.value = newTab
      } else {
        const existingTab = opened.value[existingIndex]
        opened.value[existingIndex] = {
          ...existingTab,
          path: tab.path,
          params: tab.params,
          query: tab.query,
          title: tab.title || existingTab.title,
          fixedTab: tab.fixedTab ?? existingTab.fixedTab,
          keepAlive: tab.keepAlive ?? existingTab.keepAlive,
          name: tab.name || existingTab.name,
          icon: tab.icon || existingTab.icon
        }
        current.value = opened.value[existingIndex]
      }
    }
    const findFixedTabInsertIndex = () => {
      let insertIndex = 0
      for (let i = 0; i < opened.value.length; i++) {
        if (opened.value[i].fixedTab) {
          insertIndex = i + 1
        } else {
          break
        }
      }
      return insertIndex
    }
    const removeTab = (path) => {
      const targetTab = getTab(path)
      const targetIndex = findTabIndex(path)
      if (targetIndex === -1) {
        console.warn(`尝试关闭不存在的标签页: ${path}`)
        return
      }
      if (targetTab && !isTabClosable(targetTab)) {
        console.warn(`尝试关闭固定标签页: ${path}`)
        return
      }
      opened.value.splice(targetIndex, 1)
      if (targetTab?.name) {
        addKeepAliveExclude(targetTab)
      }
      const { homePath } = useCommon()
      if (!hasOpenedTabs.value) {
        if (path !== homePath.value) {
          current.value = {}
          safeRouterPush({ path: homePath.value })
        }
        return
      }
      if (current.value.path === path) {
        const newIndex = targetIndex >= opened.value.length ? opened.value.length - 1 : targetIndex
        current.value = opened.value[newIndex]
        safeRouterPush(current.value)
      }
    }
    const removeLeft = (path) => {
      const targetIndex = findTabIndex(path)
      if (targetIndex === -1) {
        console.warn(`尝试关闭左侧标签页,但目标标签页不存在: ${path}`)
        return
      }
      const leftTabs = opened.value.slice(0, targetIndex)
      const closableLeftTabs = leftTabs.filter(isTabClosable)
      if (closableLeftTabs.length === 0) {
        console.warn('左侧没有可关闭的标签页')
        return
      }
      markTabsToRemove(closableLeftTabs)
      opened.value = opened.value.filter(
        (tab, index) => index >= targetIndex || !isTabClosable(tab)
      )
      const targetTab = getTab(path)
      if (targetTab) {
        current.value = targetTab
      }
    }
    const removeRight = (path) => {
      const targetIndex = findTabIndex(path)
      if (targetIndex === -1) {
        console.warn(`尝试关闭右侧标签页,但目标标签页不存在: ${path}`)
        return
      }
      const rightTabs = opened.value.slice(targetIndex + 1)
      const closableRightTabs = rightTabs.filter(isTabClosable)
      if (closableRightTabs.length === 0) {
        console.warn('右侧没有可关闭的标签页')
        return
      }
      markTabsToRemove(closableRightTabs)
      opened.value = opened.value.filter(
        (tab, index) => index <= targetIndex || !isTabClosable(tab)
      )
      const targetTab = getTab(path)
      if (targetTab) {
        current.value = targetTab
      }
    }
    const removeOthers = (path) => {
      const targetTab = getTab(path)
      if (!targetTab) {
        console.warn(`尝试关闭其他标签页,但目标标签页不存在: ${path}`)
        return
      }
      const otherTabs = opened.value.filter((tab) => tab.path !== path)
      const closableTabs = otherTabs.filter(isTabClosable)
      if (closableTabs.length === 0) {
        console.warn('没有其他可关闭的标签页')
        return
      }
      markTabsToRemove(closableTabs)
      opened.value = opened.value.filter((tab) => tab.path === path || !isTabClosable(tab))
      current.value = targetTab
    }
    const removeAll = () => {
      const { homePath } = useCommon()
      const hasFixedTabs = opened.value.some((tab) => tab.fixedTab)
      const closableTabs = opened.value.filter((tab) => {
        if (!isTabClosable(tab)) return false
        return hasFixedTabs || tab.path !== homePath.value
      })
      if (closableTabs.length === 0) {
        console.warn('没有可关闭的标签页')
        return
      }
      markTabsToRemove(closableTabs)
      opened.value = opened.value.filter((tab) => {
        return !isTabClosable(tab) || (!hasFixedTabs && tab.path === homePath.value)
      })
      if (!hasOpenedTabs.value) {
        current.value = {}
        safeRouterPush({ path: homePath.value })
        return
      }
      const homeTab = opened.value.find((tab) => tab.path === homePath.value)
      const targetTab = homeTab || opened.value[0]
      current.value = targetTab
      safeRouterPush(targetTab)
    }
    const addKeepAliveExclude = (tab) => {
      if (!tab.keepAlive || !tab.name) return
      if (!keepAliveExclude.value.includes(tab.name)) {
        keepAliveExclude.value.push(tab.name)
      }
    }
    const removeKeepAliveExclude = (name) => {
      if (!name) return
      keepAliveExclude.value = keepAliveExclude.value.filter((item) => item !== name)
    }
    const markTabsToRemove = (tabs) => {
      tabs.forEach((tab) => {
        if (tab.name) {
          addKeepAliveExclude(tab)
        }
      })
    }
    const toggleFixedTab = (path) => {
      const targetIndex = findTabIndex(path)
      if (targetIndex === -1) {
        console.warn(`尝试切换不存在标签页的固定状态: ${path}`)
        return
      }
      const tab = { ...opened.value[targetIndex] }
      tab.fixedTab = !tab.fixedTab
      opened.value.splice(targetIndex, 1)
      if (tab.fixedTab) {
        const firstNonFixedIndex = opened.value.findIndex((t) => !t.fixedTab)
        const insertIndex = firstNonFixedIndex === -1 ? opened.value.length : firstNonFixedIndex
        opened.value.splice(insertIndex, 0, tab)
      } else {
        const fixedCount = opened.value.filter((t) => t.fixedTab).length
        opened.value.splice(fixedCount, 0, tab)
      }
      if (current.value.path === path) {
        current.value = tab
      }
    }
    const validateWorktabs = (routerInstance) => {
      try {
        const isTabRouteValid = (tab) => {
          try {
            if (tab.name) {
              const routes = routerInstance.getRoutes()
              if (routes.some((r) => r.name === tab.name)) return true
            }
            if (tab.path) {
              const resolved = routerInstance.resolve({
                path: tab.path,
                query: tab.query || void 0
              })
              return resolved.matched.length > 0
            }
            return false
          } catch {
            return false
          }
        }
        const validTabs = opened.value.filter((tab) => isTabRouteValid(tab))
        if (validTabs.length !== opened.value.length) {
          console.warn('发现无效的标签页路由,已自动清理')
          opened.value = validTabs
        }
        const isCurrentValid = current.value && isTabRouteValid(current.value)
        if (!isCurrentValid && validTabs.length > 0) {
          console.warn('当前激活标签无效,已自动切换')
          current.value = validTabs[0]
        } else if (!isCurrentValid) {
          current.value = {}
        }
      } catch (error) {
        console.error('验证工作台标签页失败:', error)
      }
    }
    const clearAll = () => {
      current.value = {}
      opened.value = []
      keepAliveExclude.value = []
    }
    const getStateSnapshot = () => {
      return {
        current: { ...current.value },
        opened: [...opened.value],
        keepAliveExclude: [...keepAliveExclude.value]
      }
    }
    const getTabTitle = (path) => {
      const tab = getTab(path)
      return tab
    }
    const updateTabTitle = (path, title) => {
      const tab = getTab(path)
      if (tab) {
        tab.customTitle = title
      }
    }
    const resetTabTitle = (path) => {
      const tab = getTab(path)
      if (tab) {
        tab.customTitle = ''
      }
    }
    return {
      // 状态
      current,
      opened,
      keepAliveExclude,
      // 计算属性
      hasOpenedTabs,
      hasMultipleTabs,
      currentTabIndex,
      // 方法
      openTab,
      removeTab,
      removeLeft,
      removeRight,
      removeOthers,
      removeAll,
      toggleFixedTab,
      validateWorktabs,
      clearAll,
      getStateSnapshot,
      // 工具方法
      findTabIndex,
      getTab,
      isTabClosable,
      addKeepAliveExclude,
      removeKeepAliveExclude,
      markTabsToRemove,
      getTabTitle,
      updateTabTitle,
      resetTabTitle
    }
  },
  {
    persist: {
      key: 'worktab',
      storage: localStorage
    }
  }
)
export { useWorktabStore }