feat(TagsView): Add TagsView component

feat(ContextMenu): Add ContextMenu component

feat(store): Add tagsView store
This commit is contained in:
kailong321200875
2022-01-16 17:55:20 +08:00
parent 4612e5544b
commit 349ac9d398
20 changed files with 900 additions and 164 deletions

View File

@@ -1,8 +1,9 @@
// import router from '@/router'
import router from '@/router'
import type { RouteLocationNormalizedLoaded } from 'vue-router'
import { getRawRoute } from '@/utils/routerHelper'
import { defineStore } from 'pinia'
import { store } from '../index'
import { findIndex } from '@/utils'
export interface TagsViewState {
visitedViews: RouteLocationNormalizedLoaded[]
@@ -24,18 +25,24 @@ export const useTagsViewStore = defineStore({
}
},
actions: {
ADD_VISITED_VIEW(view: RouteLocationNormalizedLoaded): void {
if (this.visitedViews.some((v: RouteLocationNormalizedLoaded) => v.path === view.path)) return
// 新增缓存和tag
addView(view: RouteLocationNormalizedLoaded): void {
this.addVisitedView(view)
this.addCachedView()
},
// 新增tag
addVisitedView(view: RouteLocationNormalizedLoaded) {
if (this.visitedViews.some((v) => v.path === view.path)) return
if (view.meta?.noTagsView) return
this.visitedViews.push(
Object.assign({}, view, {
title: view.meta.title || 'no-name'
title: view.meta?.title || 'no-name'
})
)
},
SET_CACHED_VIEW(): void {
// 新增缓存
addCachedView() {
const cacheMap: Set<string> = new Set()
for (const v of this.visitedViews) {
const item = getRawRoute(v)
const needCache = !item.meta?.noCache
@@ -45,9 +52,17 @@ export const useTagsViewStore = defineStore({
const name = item.name as string
cacheMap.add(name)
}
if (Array.from(this.cachedViews).sort().toString() === Array.from(cacheMap).sort().toString())
return
this.cachedViews = cacheMap
},
DEL_VISITED_VIEW(view: RouteLocationNormalizedLoaded): void {
// 删除某个
delView(view: RouteLocationNormalizedLoaded) {
this.delVisitedView(view)
this.addCachedView()
},
// 删除tag
delVisitedView(view: RouteLocationNormalizedLoaded) {
for (const [i, v] of this.visitedViews.entries()) {
if (v.path === view.path) {
this.visitedViews.splice(i, 1)
@@ -55,117 +70,60 @@ export const useTagsViewStore = defineStore({
}
}
},
DEL_CACHED_VIEW(): void {
// const route = router.currentRoute.value
// for (const [key, value] of this.cachedViews) {
// const index = value.findIndex((item: string) => item === (route.name as string))
// if (index === -1) {
// continue
// }
// if (value.length === 1) {
// this.cachedViews.delete(key)
// continue
// }
// value.splice(index, 1)
// this.cachedViews.set(key, value)
// }
// 删除缓存
delCachedView() {
const route = router.currentRoute.value
const index = findIndex<string>(this.getCachedViews, (v) => v === route.name)
if (index > -1) {
this.cachedViews.delete(this.getCachedViews[index])
}
},
DEL_OTHERS_VISITED_VIEWS(view: RouteLocationNormalizedLoaded): void {
this.visitedViews = this.visitedViews.filter((v) => {
return v.meta.affix || v.path === view.path
})
// 删除所有缓存和tag
delAllViews() {
this.delAllVisitedViews()
this.addCachedView()
},
DEL_ALL_VISITED_VIEWS(): void {
// keep affix tags
// 删除所有tag
delAllVisitedViews() {
const affixTags = this.visitedViews.filter((tag) => tag.meta.affix)
this.visitedViews = affixTags
},
UPDATE_VISITED_VIEW(view: RouteLocationNormalizedLoaded): void {
for (let v of this.visitedViews) {
if (v.path === view.path) {
v = Object.assign(v, view)
break
}
}
},
addView(view: RouteLocationNormalizedLoaded): void {
this.addVisitedView(view)
// 删除其他
delOthersViews(view: RouteLocationNormalizedLoaded) {
this.delOthersVisitedViews(view)
this.addCachedView()
},
addVisitedView(view: RouteLocationNormalizedLoaded): void {
this.ADD_VISITED_VIEW(view)
// 删除其他tag
delOthersVisitedViews(view: RouteLocationNormalizedLoaded) {
this.visitedViews = this.visitedViews.filter((v) => {
return v?.meta?.affix || v.path === view.path
})
},
addCachedView(): void {
this.SET_CACHED_VIEW()
},
delView(view: RouteLocationNormalizedLoaded): Promise<unknown> {
return new Promise((resolve) => {
this.delVisitedView(view)
this.SET_CACHED_VIEW()
resolve({
visitedViews: [...this.visitedViews],
cachedViews: [...this.cachedViews]
// 删除左侧
delLeftViews(view: RouteLocationNormalizedLoaded) {
const index = findIndex<RouteLocationNormalizedLoaded>(
this.visitedViews,
(v) => v.path === view.path
)
if (index > -1) {
this.visitedViews = this.visitedViews.filter((v, i) => {
return v?.meta?.affix || v.path === view.path || i > index
})
})
this.addCachedView()
}
},
delVisitedView(view: RouteLocationNormalizedLoaded): Promise<unknown> {
return new Promise((resolve) => {
this.DEL_VISITED_VIEW(view)
resolve([...this.visitedViews])
})
},
delCachedView(): Promise<unknown> {
return new Promise((resolve) => {
this.DEL_CACHED_VIEW()
resolve([...this.cachedViews])
})
},
delOthersViews(view: RouteLocationNormalizedLoaded): Promise<unknown> {
return new Promise((resolve) => {
this.delOthersVisitedViews(view)
this.SET_CACHED_VIEW()
resolve({
visitedViews: [...this.visitedViews],
cachedViews: [...this.cachedViews]
// 删除右侧
delRightViews(view: RouteLocationNormalizedLoaded) {
const index = findIndex<RouteLocationNormalizedLoaded>(
this.visitedViews,
(v) => v.path === view.path
)
if (index > -1) {
this.visitedViews = this.visitedViews.filter((v, i) => {
return v?.meta?.affix || v.path === view.path || i < index
})
})
},
delOthersVisitedViews(view: RouteLocationNormalizedLoaded): Promise<unknown> {
return new Promise((resolve) => {
this.DEL_OTHERS_VISITED_VIEWS(view)
resolve([...this.visitedViews])
})
},
delOthersCachedViews(): Promise<unknown> {
return new Promise((resolve) => {
this.SET_CACHED_VIEW()
resolve([...this.cachedViews])
})
},
delAllViews(): Promise<unknown> {
return new Promise((resolve) => {
this.delAllVisitedViews()
this.SET_CACHED_VIEW()
resolve({
visitedViews: [...this.visitedViews],
cachedViews: [...this.cachedViews]
})
})
},
delAllVisitedViews(): Promise<unknown> {
return new Promise((resolve) => {
this.DEL_ALL_VISITED_VIEWS()
resolve([...this.visitedViews])
})
},
delAllCachedViews(): Promise<unknown> {
return new Promise((resolve) => {
this.SET_CACHED_VIEW()
resolve([...this.cachedViews])
})
},
updateVisitedView(view: RouteLocationNormalizedLoaded): void {
this.UPDATE_VISITED_VIEW(view)
this.addCachedView()
}
}
}
})