diff --git a/frontend/src/store/modules/tagsView.js b/frontend/src/store/modules/tagsView.js index d5d0deac..93c50827 100644 --- a/frontend/src/store/modules/tagsView.js +++ b/frontend/src/store/modules/tagsView.js @@ -1,6 +1,32 @@ import cache from '@/plugins/cache' const PERSIST_KEY = 'tags-view-visited' +const MAX_VISITED_VIEWS = 8 + +function removeViewCache(state, view) { + const cachedViewIndex = state.cachedViews.indexOf(view.name) + if (cachedViewIndex > -1) { + state.cachedViews.splice(cachedViewIndex, 1) + } + state.iframeViews = state.iframeViews.filter(item => item.path !== view.path) +} + +function trimVisitedViews(state, currentView) { + while (state.visitedViews.length > MAX_VISITED_VIEWS) { + const removableViewIndex = state.visitedViews.findIndex(view => { + const isAffix = view.meta && view.meta.affix + + return !isAffix && view.path !== currentView.path + }) + + if (removableViewIndex === -1) { + return + } + + const [removedView] = state.visitedViews.splice(removableViewIndex, 1) + removeViewCache(state, removedView) + } +} // 布局已固定,不启用标签页持久化 function isPersistEnabled() { @@ -43,6 +69,7 @@ const mutations = { title: view.meta.title || 'no-name' }) ) + trimVisitedViews(state, view) saveVisitedViews(state.visitedViews) }, ADD_VISITED_VIEW_FIRST: (state, view) => { @@ -52,6 +79,7 @@ const mutations = { title: view.meta.title || 'no-name' }) ) + trimVisitedViews(state, view) }, ADD_CACHED_VIEW: (state, view) => { if (state.cachedViews.includes(view.name)) return