mirror of
https://gitee.com/dapppp/ruoyi-plus-vben5.git
synced 2026-03-17 10:02:02 +08:00
perf: optimize the closing jump logic of tabs
* 依据tab访问历史回退上一个tab,原逻辑是返回一下个 或 上一个
This commit is contained in:
@@ -11,7 +11,9 @@ import { toRaw } from 'vue';
|
||||
|
||||
import { preferences } from '@vben-core/preferences';
|
||||
import {
|
||||
createStack,
|
||||
openRouteInNewWindow,
|
||||
Stack,
|
||||
startProgress,
|
||||
stopProgress,
|
||||
} from '@vben-core/shared/utils';
|
||||
@@ -47,8 +49,17 @@ interface TabbarState {
|
||||
* @zh_CN 更新时间,用于一些更新场景,使用watch深度监听的话,会损耗性能
|
||||
*/
|
||||
updateTime?: number;
|
||||
/**
|
||||
* @zh_CN 上一个标签页打开的标签
|
||||
*/
|
||||
visitHistory: Stack<string>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh_CN 访问历史记录最大数量
|
||||
*/
|
||||
const MAX_VISIT_HISTORY = 50;
|
||||
|
||||
/**
|
||||
* @zh_CN 访问权限相关
|
||||
*/
|
||||
@@ -62,6 +73,7 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
||||
this.tabs = this.tabs.filter(
|
||||
(item) => !keySet.has(getTabKeyFromTab(item)),
|
||||
);
|
||||
this.visitHistory.remove(...keys);
|
||||
|
||||
await this.updateCacheTabs();
|
||||
},
|
||||
@@ -166,6 +178,8 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
||||
this.tabs.splice(tabIndex, 1, mergedTab);
|
||||
}
|
||||
this.updateCacheTabs();
|
||||
// 添加访问历史记录
|
||||
this.visitHistory.push(tab.key as string);
|
||||
return tab;
|
||||
},
|
||||
/**
|
||||
@@ -174,6 +188,8 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
||||
async closeAllTabs(router: Router) {
|
||||
const newTabs = this.tabs.filter((tab) => isAffixTab(tab));
|
||||
this.tabs = newTabs.length > 0 ? newTabs : [...this.tabs].splice(0, 1);
|
||||
// 设置访问历史
|
||||
this.visitHistory.retain(this.tabs.map((item) => getTabKeyFromTab(item)));
|
||||
await this._goToDefaultTab(router);
|
||||
this.updateCacheTabs();
|
||||
},
|
||||
@@ -249,29 +265,26 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
||||
*/
|
||||
async closeTab(tab: TabDefinition, router: Router) {
|
||||
const { currentRoute } = router;
|
||||
const currentTabKey = getTabKey(currentRoute.value);
|
||||
// 关闭不是激活选项卡
|
||||
if (getTabKey(currentRoute.value) !== getTabKeyFromTab(tab)) {
|
||||
if (currentTabKey !== getTabKeyFromTab(tab)) {
|
||||
this._close(tab);
|
||||
this.updateCacheTabs();
|
||||
// 移除访问历史
|
||||
this.visitHistory.remove(getTabKeyFromTab(tab));
|
||||
return;
|
||||
}
|
||||
const index = this.getTabs.findIndex(
|
||||
(item) => getTabKeyFromTab(item) === getTabKey(currentRoute.value),
|
||||
);
|
||||
|
||||
const before = this.getTabs[index - 1];
|
||||
const after = this.getTabs[index + 1];
|
||||
|
||||
// 下一个tab存在,跳转到下一个
|
||||
if (after) {
|
||||
this._close(tab);
|
||||
await this._goToTab(after, router);
|
||||
// 上一个tab存在,跳转到上一个
|
||||
} else if (before) {
|
||||
this._close(tab);
|
||||
await this._goToTab(before, router);
|
||||
} else {
|
||||
if (this.getTabs.length <= 1) {
|
||||
console.error('Failed to close the tab; only one tab remains open.');
|
||||
return;
|
||||
}
|
||||
// 从访问历史记录中移除当前关闭的tab
|
||||
this.visitHistory.remove(currentTabKey);
|
||||
this._close(tab);
|
||||
const previousTabKey = this.visitHistory.pop();
|
||||
if (previousTabKey) {
|
||||
// 跳转到上一个tab
|
||||
await this._goToTab(this.getTabByKey(previousTabKey), router);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -527,11 +540,12 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
||||
persist: [
|
||||
// tabs不需要保存在localStorage
|
||||
{
|
||||
pick: ['tabs'],
|
||||
pick: ['tabs', 'visitHistory'],
|
||||
storage: sessionStorage,
|
||||
},
|
||||
],
|
||||
state: (): TabbarState => ({
|
||||
visitHistory: createStack<string>(true, MAX_VISIT_HISTORY),
|
||||
cachedTabs: new Set(),
|
||||
dragEndIndex: 0,
|
||||
excludeCachedTabs: new Set(),
|
||||
|
||||
Reference in New Issue
Block a user