refactor: remove 'affixTabs' from state (#25)

pull/48/MERGE
Li Kui 2024-07-06 14:55:51 +08:00 committed by GitHub
parent 478bbe17ee
commit 19b57fa4f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 33 additions and 54 deletions

View File

@ -28,6 +28,22 @@ function cloneTab(route: TabItem): TabItem {
}; };
} }
/**
* @zh_CN
* @param tab
*/
function isAffixTab(tab: TabItem) {
return tab.meta?.affixTab ?? false;
}
/**
* @zh_CN
* @param tab
*/
function isTabShow(tab: TabItem) {
return !tab.meta.hideInTab;
}
function routeToTab(route: RouteRecordNormalized) { function routeToTab(route: RouteRecordNormalized) {
return { return {
meta: route.meta, meta: route.meta,
@ -37,10 +53,6 @@ function routeToTab(route: RouteRecordNormalized) {
} }
interface TabsState { interface TabsState {
/**
* @zh_CN
*/
affixTabs: RouteRecordNormalized[];
/** /**
* @zh_CN * @zh_CN
*/ */
@ -80,7 +92,7 @@ const useCoreTabbarStore = defineStore('core-tabbar', {
*/ */
_close(tab: TabItem) { _close(tab: TabItem) {
const { fullPath } = tab; const { fullPath } = tab;
if (this._isAffixTab(tab)) { if (isAffixTab(tab)) {
return; return;
} }
const index = this.tabs.findIndex((item) => item.fullPath === fullPath); const index = this.tabs.findIndex((item) => item.fullPath === fullPath);
@ -110,21 +122,13 @@ const useCoreTabbarStore = defineStore('core-tabbar', {
}; };
await router.replace(toParams); await router.replace(toParams);
}, },
/**
* @zh_CN
* @param tab
*/
_isAffixTab(tab: TabItem) {
return tab?.meta?.affixTab ?? false;
},
/** /**
* @zh_CN * @zh_CN
* @param routeTab * @param routeTab
*/ */
addTab(routeTab: TabItem) { addTab(routeTab: TabItem) {
const tab = cloneTab(routeTab); const tab = cloneTab(routeTab);
const { fullPath, meta, params, query } = tab; if (!isTabShow(tab)) {
if (meta?.hideInTab) {
return; return;
} }
@ -137,14 +141,7 @@ const useCoreTabbarStore = defineStore('core-tabbar', {
} else { } else {
// 页面已经存在,不重复添加选项卡,只更新选项卡参数 // 页面已经存在,不重复添加选项卡,只更新选项卡参数
const currentTab = toRaw(this.tabs)[tabIndex]; const currentTab = toRaw(this.tabs)[tabIndex];
if (!currentTab) { this.tabs.splice(tabIndex, 1, { ...currentTab, ...tab });
return;
}
currentTab.params = params || currentTab.params;
currentTab.query = query || currentTab.query;
currentTab.fullPath = fullPath || currentTab.fullPath;
currentTab.meta = meta || currentTab.meta;
this.tabs.splice(tabIndex, 1, currentTab);
} }
this.updateCacheTab(); this.updateCacheTab();
}, },
@ -152,7 +149,7 @@ const useCoreTabbarStore = defineStore('core-tabbar', {
* @zh_CN * @zh_CN
*/ */
async closeAllTabs(router: Router) { async closeAllTabs(router: Router) {
this.tabs = this.tabs.filter((tab) => this._isAffixTab(tab)); this.tabs = this.tabs.filter((tab) => isAffixTab(tab));
await this._goToDefaultTab(router); await this._goToDefaultTab(router);
this.updateCacheTab(); this.updateCacheTab();
}, },
@ -169,7 +166,7 @@ const useCoreTabbarStore = defineStore('core-tabbar', {
const leftTabs = this.tabs.slice(0, index); const leftTabs = this.tabs.slice(0, index);
const paths: string[] = []; const paths: string[] = [];
for (const item of leftTabs) { for (const item of leftTabs) {
if (!this._isAffixTab(tab)) { if (!isAffixTab(tab)) {
paths.push(this.getTabPath(item)); paths.push(this.getTabPath(item));
} }
} }
@ -194,7 +191,7 @@ const useCoreTabbarStore = defineStore('core-tabbar', {
if (!closeTab) { if (!closeTab) {
continue; continue;
} }
if (!this._isAffixTab(tab)) { if (!isAffixTab(tab)) {
paths.push(this.getTabPath(closeTab)); paths.push(this.getTabPath(closeTab));
} }
} }
@ -216,7 +213,7 @@ const useCoreTabbarStore = defineStore('core-tabbar', {
const paths: string[] = []; const paths: string[] = [];
for (const item of rightTabs) { for (const item of rightTabs) {
if (!this._isAffixTab(tab)) { if (!isAffixTab(tab)) {
paths.push(this.getTabPath(item)); paths.push(this.getTabPath(item));
} }
} }
@ -283,15 +280,8 @@ const useCoreTabbarStore = defineStore('core-tabbar', {
(item) => this.getTabPath(item) === this.getTabPath(tab), (item) => this.getTabPath(item) === this.getTabPath(tab),
); );
if (index !== -1) { if (index !== -1) {
this.tabs[index].meta.affixTab = true;
}
// TODO: 这里应该把tab从tbs中移除
const affixIndex = this.affixTabs.findIndex(
(item) => this.getTabPath(item) === this.getTabPath(tab),
);
if (affixIndex === -1) {
tab.meta.affixTab = true; tab.meta.affixTab = true;
this.affixTabs.push(tab as unknown as RouteRecordNormalized); this.addTab(tab);
} }
}, },
/** /**
@ -317,23 +307,21 @@ const useCoreTabbarStore = defineStore('core-tabbar', {
*/ */
setAffixTabs(tabs: RouteRecordNormalized[]) { setAffixTabs(tabs: RouteRecordNormalized[]) {
for (const tab of tabs) { for (const tab of tabs) {
tab.meta.affixTab = true;
this.addTab(routeToTab(tab)); this.addTab(routeToTab(tab));
} }
this.affixTabs = tabs;
}, },
/** /**
* @zh_CN * @zh_CN
* @param tab * @param tab
*/ */
async unPushPinTab(tab: TabItem) { async unPushPinTab(tab: TabItem) {
const index = this.affixTabs.findIndex( const index = this.tabs.findIndex(
(item) => this.getTabPath(item) === this.getTabPath(tab), (item) => this.getTabPath(item) === this.getTabPath(tab),
); );
if (index !== -1) { if (index !== -1) {
tab.meta.affixTab = false; tab.meta.affixTab = false;
this.affixTabs[index].meta.affixTab = false;
this.affixTabs.splice(index, 1);
this.addTab(tab); this.addTab(tab);
} }
}, },
@ -363,28 +351,20 @@ const useCoreTabbarStore = defineStore('core-tabbar', {
}, },
}, },
getters: { getters: {
affixTabs(): TabItem[] {
return this.tabs.filter((tab) => isAffixTab(tab));
},
getCacheTabs(): string[] { getCacheTabs(): string[] {
return [...this.cacheTabs]; return [...this.cacheTabs];
}, },
getExcludeTabs(): string[] { getExcludeTabs(): string[] {
return [...this.excludeCacheTabs]; return [...this.excludeCacheTabs];
}, },
getTabs(): TabItem[] { getTabs(): TabItem[] {
const tabs: TabItem[] = []; const affixTabs = this.tabs.filter((tab) => isAffixTab(tab));
const affixTabPaths = new Set<string>(); const normalTabs = this.tabs.filter((tab) => !isAffixTab(tab));
for (const tab of this.affixTabs) {
if (!tab.meta.hideInTab) { return [...affixTabs, ...normalTabs];
tabs.push(routeToTab(tab));
affixTabPaths.add(tab.path);
}
}
for (const tab of this.tabs) {
if (!affixTabPaths.has(tab.path) && !tab.meta.hideInTab) {
tabs.push(tab);
}
}
return tabs;
}, },
}, },
persist: { persist: {
@ -392,7 +372,6 @@ const useCoreTabbarStore = defineStore('core-tabbar', {
paths: [], paths: [],
}, },
state: (): TabsState => ({ state: (): TabsState => ({
affixTabs: [],
cacheTabs: new Set(), cacheTabs: new Set(),
excludeCacheTabs: new Set(), excludeCacheTabs: new Set(),
renderRouteView: true, renderRouteView: true,