|
@@ -2,14 +2,9 @@ import router from '@/router'
|
|
|
import type { RouteLocationNormalizedLoaded } from 'vue-router'
|
|
|
import { getRawRoute } from '@/utils/routerHelper'
|
|
|
import { defineStore } from 'pinia'
|
|
|
-import { store } from '@/store'
|
|
|
+import { ref, computed } from 'vue'
|
|
|
import { findIndex } from '@/utils'
|
|
|
|
|
|
-export interface TagsViewState {
|
|
|
- visitedViews: RouteLocationNormalizedLoaded[]
|
|
|
- cachedViews: Set<string>
|
|
|
-}
|
|
|
-
|
|
|
/**
|
|
|
* 判断是否为相同路由tag
|
|
|
* 判断path同时根据query确定是否相同(兼容详情页多开)
|
|
@@ -23,136 +18,153 @@ export const isSameRouteTag = (
|
|
|
return currentTag.fullPath === TargetTag.fullPath
|
|
|
}
|
|
|
|
|
|
-export const useTagsViewStore = defineStore('tagsView', {
|
|
|
- state: (): TagsViewState => ({
|
|
|
- visitedViews: [],
|
|
|
- cachedViews: new Set()
|
|
|
- }),
|
|
|
- getters: {
|
|
|
- getVisitedViews(): RouteLocationNormalizedLoaded[] {
|
|
|
- return this.visitedViews
|
|
|
- },
|
|
|
- getCachedViews(): string[] {
|
|
|
- return Array.from(this.cachedViews)
|
|
|
- }
|
|
|
- },
|
|
|
- actions: {
|
|
|
- // 新增缓存和tag
|
|
|
- addView(view: RouteLocationNormalizedLoaded): void {
|
|
|
- console.log('view: ', view)
|
|
|
- this.addVisitedView(view)
|
|
|
- this.addCachedView()
|
|
|
- },
|
|
|
- allResetView(views: RouteLocationNormalizedLoaded[]): void {
|
|
|
- this.visitedViews = views
|
|
|
- this.addCachedView() // 缓存保存顺序
|
|
|
- },
|
|
|
- // 新增tag
|
|
|
- addVisitedView(view: RouteLocationNormalizedLoaded) {
|
|
|
- if (this.visitedViews.some((v) => isSameRouteTag(v, view))) return
|
|
|
- if (view.meta?.noTagsView) return
|
|
|
- this.visitedViews.push(
|
|
|
- Object.assign({}, view, {
|
|
|
- title: view.meta?.title || 'no-name'
|
|
|
- })
|
|
|
- )
|
|
|
- },
|
|
|
- // 新增缓存
|
|
|
- addCachedView() {
|
|
|
- const cacheMap: Set<string> = new Set()
|
|
|
- for (const v of this.visitedViews) {
|
|
|
- const item = getRawRoute(v)
|
|
|
- const needCache = !item.meta?.noCache
|
|
|
- if (!needCache) {
|
|
|
- continue
|
|
|
- }
|
|
|
- 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
|
|
|
- },
|
|
|
- // 删除某个
|
|
|
- delView(view: RouteLocationNormalizedLoaded) {
|
|
|
- this.delVisitedView(view)
|
|
|
- this.delCachedView()
|
|
|
- },
|
|
|
- // 删除tag
|
|
|
- delVisitedView(view: RouteLocationNormalizedLoaded) {
|
|
|
- for (const [i, v] of this.visitedViews.entries()) {
|
|
|
- if (v.path === view.path) {
|
|
|
- this.visitedViews.splice(i, 1)
|
|
|
- break
|
|
|
- }
|
|
|
- }
|
|
|
- },
|
|
|
- // 删除缓存
|
|
|
- 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])
|
|
|
- }
|
|
|
- },
|
|
|
- // 删除所有缓存和tag
|
|
|
- delAllViews() {
|
|
|
- this.delAllVisitedViews()
|
|
|
- this.delCachedView()
|
|
|
- },
|
|
|
- // 删除所有tag
|
|
|
- delAllVisitedViews() {
|
|
|
- // const affixTags = this.visitedViews.filter((tag) => tag.meta.affix)
|
|
|
- this.visitedViews = []
|
|
|
- },
|
|
|
- // 删除其他
|
|
|
- delOthersViews(view: RouteLocationNormalizedLoaded) {
|
|
|
- this.delOthersVisitedViews(view)
|
|
|
- this.addCachedView()
|
|
|
- },
|
|
|
- // 删除其他tag
|
|
|
- delOthersVisitedViews(view: RouteLocationNormalizedLoaded) {
|
|
|
- this.visitedViews = this.visitedViews.filter((v) => {
|
|
|
- return v?.meta?.affix || v.path === view.path
|
|
|
+/**
|
|
|
+ * 头部tab导航栏store
|
|
|
+ */
|
|
|
+export const useTagsViewStore = defineStore('tagsView', () => {
|
|
|
+ const visitedViews = ref<RouteLocationNormalizedLoaded[]>([])
|
|
|
+ const cachedViews = ref<Set<string>>(new Set())
|
|
|
+ const getCachedViews = computed(() => {
|
|
|
+ return Array.from(cachedViews.value)
|
|
|
+ })
|
|
|
+
|
|
|
+ // 新增缓存和tag
|
|
|
+ const addView = (view: RouteLocationNormalizedLoaded): void => {
|
|
|
+ addVisitedView(view)
|
|
|
+ addCachedView()
|
|
|
+ }
|
|
|
+
|
|
|
+ // 重置缓存和tag(用于tag排序)
|
|
|
+ const allResetView = (views: RouteLocationNormalizedLoaded[]): void => {
|
|
|
+ visitedViews.value = views
|
|
|
+ addCachedView()
|
|
|
+ }
|
|
|
+
|
|
|
+ // 新增tag
|
|
|
+ const addVisitedView = (view: RouteLocationNormalizedLoaded): void => {
|
|
|
+ if (unref(visitedViews).some((v) => isSameRouteTag(v, view))) return
|
|
|
+ if (view.meta?.noTagsView) return
|
|
|
+ visitedViews.value.push(
|
|
|
+ Object.assign({}, view, {
|
|
|
+ title: view.meta?.title || 'no-name'
|
|
|
})
|
|
|
- },
|
|
|
- // 删除左侧
|
|
|
- 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()
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ // 新增缓存
|
|
|
+ const addCachedView = (): void => {
|
|
|
+ const cacheMap: Set<string> = new Set()
|
|
|
+ for (const v of unref(visitedViews)) {
|
|
|
+ const item = getRawRoute(v)
|
|
|
+ const needCache = !item.meta?.noCache
|
|
|
+ if (!needCache) {
|
|
|
+ continue
|
|
|
}
|
|
|
- },
|
|
|
- // 删除右侧
|
|
|
- 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
|
|
|
- })
|
|
|
- this.addCachedView()
|
|
|
+ const name = item.name as string
|
|
|
+ cacheMap.add(name)
|
|
|
+ }
|
|
|
+ if (Array.from(visitedViews.value).sort().toString() === Array.from(cacheMap).sort().toString())
|
|
|
+ return
|
|
|
+ cachedViews.value = cacheMap
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新某tag
|
|
|
+ const updateVisitedView = (view: RouteLocationNormalizedLoaded): void => {
|
|
|
+ for (let v of unref(visitedViews)) {
|
|
|
+ if (v.path === view.path) {
|
|
|
+ v = Object.assign(v, view)
|
|
|
+ break
|
|
|
}
|
|
|
- },
|
|
|
- updateVisitedView(view: RouteLocationNormalizedLoaded) {
|
|
|
- for (let v of this.visitedViews) {
|
|
|
- if (v.path === view.path) {
|
|
|
- v = Object.assign(v, view)
|
|
|
- break
|
|
|
- }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除某个
|
|
|
+ const delView = (view: RouteLocationNormalizedLoaded): void => {
|
|
|
+ delVisitedView(view)
|
|
|
+ delCachedView()
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除tag
|
|
|
+ const delVisitedView = (view: RouteLocationNormalizedLoaded): void => {
|
|
|
+ for (const [i, v] of unref(visitedViews).entries()) {
|
|
|
+ if (v.path === view.path) {
|
|
|
+ visitedViews.value.splice(i, 1)
|
|
|
+ break
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-})
|
|
|
|
|
|
-export const useTagsViewStoreWithOut = () => {
|
|
|
- return useTagsViewStore(store)
|
|
|
-}
|
|
|
+ // 删除缓存
|
|
|
+ const delCachedView = (): void => {
|
|
|
+ const route = router.currentRoute.value
|
|
|
+ const index = findIndex<string>(unref(getCachedViews), (v) => v === route.name)
|
|
|
+ if (index > -1) {
|
|
|
+ cachedViews.value.delete(unref(getCachedViews)[index])
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除所有缓存和tag
|
|
|
+ const delAllViews = (): void => {
|
|
|
+ delAllVisitedViews()
|
|
|
+ delCachedView()
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除所有tag
|
|
|
+ const delAllVisitedViews = (): void => {
|
|
|
+ // const affixTags = this.visitedViews.filter((tag) => tag.meta.affix)
|
|
|
+ visitedViews.value = []
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除多个标签方法
|
|
|
+ const delVisitedViews = (
|
|
|
+ view: RouteLocationNormalizedLoaded,
|
|
|
+ type: 'other' | 'left' | 'right'
|
|
|
+ ): void => {
|
|
|
+ const index = findIndex<RouteLocationNormalizedLoaded>(
|
|
|
+ unref(visitedViews),
|
|
|
+ (v) => v.path === view.path
|
|
|
+ )
|
|
|
+ visitedViews.value = visitedViews.value.filter((v, i) => {
|
|
|
+ return (
|
|
|
+ v?.meta?.affix ||
|
|
|
+ isSameRouteTag(unref(v), view) ||
|
|
|
+ (type === 'other' ? false : type === 'left' ? i > index : i < index)
|
|
|
+ )
|
|
|
+ })
|
|
|
+ addCachedView()
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除其他
|
|
|
+ const delOthersViews = (view: RouteLocationNormalizedLoaded): void => {
|
|
|
+ delVisitedViews(view, 'other')
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除左侧
|
|
|
+ const delLeftViews = (view: RouteLocationNormalizedLoaded): void => {
|
|
|
+ delVisitedViews(view, 'left')
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除右侧
|
|
|
+ const delRightViews = (view: RouteLocationNormalizedLoaded): void => {
|
|
|
+ delVisitedViews(view, 'right')
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ visitedViews,
|
|
|
+ cachedViews,
|
|
|
+ getCachedViews,
|
|
|
+ addView,
|
|
|
+ allResetView,
|
|
|
+ addVisitedView,
|
|
|
+ addCachedView,
|
|
|
+ updateVisitedView,
|
|
|
+ delView,
|
|
|
+ delVisitedView,
|
|
|
+ delCachedView,
|
|
|
+ delAllViews,
|
|
|
+ delAllVisitedViews,
|
|
|
+ delOthersViews,
|
|
|
+ delLeftViews,
|
|
|
+ delRightViews
|
|
|
+ }
|
|
|
+})
|