增加tab模式以及tab缓存

This commit is contained in:
Gary Fu
2023-12-25 19:21:24 +08:00
parent 62aad55987
commit 7e6d0d2d1e
12 changed files with 244 additions and 12 deletions

View File

@@ -0,0 +1,85 @@
import { ref } from 'vue'
import { defineStore } from 'pinia'
export const useTabsViewStore = defineStore('tabsView', () => {
const isTabMode = ref(true)
const isCachedTabMode = ref(true)
const historyTabs = ref([])
const cachedTabs = ref([])
const clearHistoryTabs = () => {
if (historyTabs.value.length) {
const tab = historyTabs.value[0]
historyTabs.value = [tab]
cachedTabs.value = []
if (isCachedTabMode.value && tab.name) {
cachedTabs.value = [tab.name]
}
}
}
const findHistoryTab = (path) => {
const idx = historyTabs.value.findIndex(v => v.path === path)
if (idx > -1) {
return historyTabs.value[idx]
}
}
const addHistoryTab = tab => {
// 添加tab
if (isTabMode.value) {
const idx = historyTabs.value.findIndex(v => v.path === tab.path)
if (idx < 0) {
historyTabs.value.push(Object.assign({}, tab)) // 可能是Proxy需要解析出来
if (isCachedTabMode.value && tab.name) {
cachedTabs.value.push(tab.name)
}
}
}
}
const removeHistoryTab = path => {
if (historyTabs.value.length > 1) {
const idx = historyTabs.value.findIndex(v => v.path === path)
if (idx > -1) {
removeCachedTab(historyTabs.value[idx])
// 删除tab
historyTabs.value.splice(idx, 1)
}
return historyTabs.value[historyTabs.value.length - 1]
}
}
const removeCachedTab = tab => {
if (tab) {
const idx = cachedTabs.value.findIndex(v => v === tab.name)
if (idx > -1) {
cachedTabs.value.splice(idx, 1)
}
}
}
return {
isTabMode,
isCachedTabMode,
historyTabs,
cachedTabs,
changeTabMode () {
isTabMode.value = !isTabMode.value
if (!isTabMode.value) {
clearHistoryTabs()
}
},
changeCachedTabMode () {
isCachedTabMode.value = !isCachedTabMode.value
if (!isCachedTabMode.value) {
cachedTabs.value = []
}
},
removeHistoryTab,
clearHistoryTabs,
findHistoryTab,
addHistoryTab
}
}, {
persist: true
})