multipleTab.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import type { RouteLocationNormalized, RouteLocationRaw, Router } from 'vue-router';
  2. import { toRaw, unref } from 'vue';
  3. import { defineStore } from 'pinia';
  4. import { store } from '/@/store';
  5. import { useGo, useRedo } from '/@/hooks/web/usePage';
  6. import { Persistent } from '/@/utils/cache/persistent';
  7. import { PageEnum } from '/@/enums/pageEnum';
  8. import { PAGE_NOT_FOUND_ROUTE, REDIRECT_ROUTE } from '/@/router/routes/basic';
  9. import { getRawRoute } from '/@/utils';
  10. import { MULTIPLE_TABS_KEY } from '/@/enums/cacheEnum';
  11. import projectSetting from '/@/settings/projectSetting';
  12. import { useUserStore } from '/@/store/modules/user';
  13. export interface MultipleTabState {
  14. cacheTabList: Set<string>;
  15. tabList: RouteLocationNormalized[];
  16. lastDragEndIndex: number;
  17. }
  18. function handleGotoPage(router: Router) {
  19. const go = useGo(router);
  20. go(unref(router.currentRoute).path, true);
  21. }
  22. const cacheTab = projectSetting.multiTabsSetting.cache;
  23. export const useMultipleTabStore = defineStore({
  24. id: 'app-multiple-tab',
  25. state: (): MultipleTabState => ({
  26. // Tabs that need to be cached
  27. cacheTabList: new Set(),
  28. // multiple tab list
  29. tabList: cacheTab ? Persistent.getLocal(MULTIPLE_TABS_KEY) || [] : [],
  30. // Index of the last moved tab
  31. lastDragEndIndex: 0,
  32. }),
  33. getters: {
  34. getTabList(): RouteLocationNormalized[] {
  35. return this.tabList;
  36. },
  37. getCachedTabList(): string[] {
  38. return Array.from(this.cacheTabList);
  39. },
  40. getLastDragEndIndex(): number {
  41. return this.lastDragEndIndex;
  42. },
  43. },
  44. actions: {
  45. /**
  46. * Update the cache according to the currently opened tabs
  47. */
  48. async updateCacheTab() {
  49. const cacheMap: Set<string> = new Set();
  50. for (const tab of this.tabList) {
  51. const item = getRawRoute(tab);
  52. // Ignore the cache
  53. const needCache = !item.meta?.ignoreKeepAlive;
  54. if (!needCache) {
  55. continue;
  56. }
  57. const name = item.name as string;
  58. cacheMap.add(name);
  59. }
  60. this.cacheTabList = cacheMap;
  61. },
  62. /**
  63. * Refresh tabs
  64. */
  65. async refreshPage(router: Router) {
  66. const { currentRoute } = router;
  67. const route = unref(currentRoute);
  68. const name = route.name;
  69. const findTab = this.getCachedTabList.find((item) => item === name);
  70. if (findTab) {
  71. this.cacheTabList.delete(findTab);
  72. }
  73. const redo = useRedo(router);
  74. await redo();
  75. },
  76. clearCacheTabs(): void {
  77. this.cacheTabList = new Set();
  78. },
  79. resetState(): void {
  80. this.tabList = [];
  81. this.clearCacheTabs();
  82. },
  83. goToPage(router: Router) {
  84. const go = useGo(router);
  85. const len = this.tabList.length;
  86. const { path } = unref(router.currentRoute);
  87. let toPath: PageEnum | string = PageEnum.BASE_HOME;
  88. if (len > 0) {
  89. const page = this.tabList[len - 1];
  90. const p = page.fullPath || page.path;
  91. if (p) {
  92. toPath = p;
  93. }
  94. }
  95. // Jump to the current page and report an error
  96. path !== toPath && go(toPath as PageEnum, true);
  97. },
  98. async addTab(route: RouteLocationNormalized) {
  99. const { path, name, fullPath, params, query } = getRawRoute(route);
  100. // 404 The page does not need to add a tab
  101. if (
  102. path === PageEnum.ERROR_PAGE ||
  103. path === PageEnum.BASE_LOGIN ||
  104. !name ||
  105. [REDIRECT_ROUTE.name, PAGE_NOT_FOUND_ROUTE.name].includes(name as string)
  106. ) {
  107. return;
  108. }
  109. let updateIndex = -1;
  110. // Existing pages, do not add tabs repeatedly
  111. const tabHasExits = this.tabList.some((tab, index) => {
  112. updateIndex = index;
  113. return (tab.fullPath || tab.path) === (fullPath || path);
  114. });
  115. // If the tab already exists, perform the update operation
  116. if (tabHasExits) {
  117. const curTab = toRaw(this.tabList)[updateIndex];
  118. if (!curTab) {
  119. return;
  120. }
  121. curTab.params = params || curTab.params;
  122. curTab.query = query || curTab.query;
  123. curTab.fullPath = fullPath || curTab.fullPath;
  124. this.tabList.splice(updateIndex, 1, curTab);
  125. } else {
  126. // Add tab
  127. this.tabList.push(route);
  128. }
  129. this.updateCacheTab();
  130. cacheTab && Persistent.setLocal(MULTIPLE_TABS_KEY, this.tabList);
  131. },
  132. async closeTab(tab: RouteLocationNormalized, router: Router) {
  133. const getToTarget = (tabItem: RouteLocationNormalized) => {
  134. const { params, path, query } = tabItem;
  135. return {
  136. params: params || {},
  137. path,
  138. query: query || {},
  139. };
  140. };
  141. const close = (route: RouteLocationNormalized) => {
  142. const { fullPath, meta: { affix } = {} } = route;
  143. if (affix) {
  144. return;
  145. }
  146. const index = this.tabList.findIndex((item) => item.fullPath === fullPath);
  147. index !== -1 && this.tabList.splice(index, 1);
  148. };
  149. const { currentRoute, replace } = router;
  150. const { path } = unref(currentRoute);
  151. if (path !== tab.path) {
  152. // Closed is not the activation tab
  153. close(tab);
  154. return;
  155. }
  156. // Closed is activated atb
  157. let toTarget: RouteLocationRaw = {};
  158. const index = this.tabList.findIndex((item) => item.path === path);
  159. // If the current is the leftmost tab
  160. if (index === 0) {
  161. // There is only one tab, then jump to the homepage, otherwise jump to the right tab
  162. if (this.tabList.length === 1) {
  163. const userStore = useUserStore();
  164. toTarget = userStore.getUserInfo.homePath || PageEnum.BASE_HOME;
  165. } else {
  166. // Jump to the right tab
  167. const page = this.tabList[index + 1];
  168. toTarget = getToTarget(page);
  169. }
  170. } else {
  171. // Close the current tab
  172. const page = this.tabList[index - 1];
  173. toTarget = getToTarget(page);
  174. }
  175. close(currentRoute.value);
  176. replace(toTarget);
  177. },
  178. // Close according to key
  179. async closeTabByKey(key: string, router: Router) {
  180. const index = this.tabList.findIndex((item) => (item.fullPath || item.path) === key);
  181. index !== -1 && this.closeTab(this.tabList[index], router);
  182. },
  183. // Sort the tabs
  184. async sortTabs(oldIndex: number, newIndex: number) {
  185. const currentTab = this.tabList[oldIndex];
  186. this.tabList.splice(oldIndex, 1);
  187. this.tabList.splice(newIndex, 0, currentTab);
  188. this.lastDragEndIndex = this.lastDragEndIndex + 1;
  189. },
  190. // Close the tab on the right and jump
  191. async closeLeftTabs(route: RouteLocationNormalized, router: Router) {
  192. const index = this.tabList.findIndex((item) => item.path === route.path);
  193. if (index > 0) {
  194. const leftTabs = this.tabList.slice(0, index);
  195. const pathList: string[] = [];
  196. for (const item of leftTabs) {
  197. const affix = item?.meta?.affix ?? false;
  198. if (!affix) {
  199. pathList.push(item.fullPath);
  200. }
  201. }
  202. this.bulkCloseTabs(pathList);
  203. }
  204. this.updateCacheTab();
  205. handleGotoPage(router);
  206. },
  207. // Close the tab on the left and jump
  208. async closeRightTabs(route: RouteLocationNormalized, router: Router) {
  209. const index = this.tabList.findIndex((item) => item.fullPath === route.fullPath);
  210. if (index >= 0 && index < this.tabList.length - 1) {
  211. const rightTabs = this.tabList.slice(index + 1, this.tabList.length);
  212. const pathList: string[] = [];
  213. for (const item of rightTabs) {
  214. const affix = item?.meta?.affix ?? false;
  215. if (!affix) {
  216. pathList.push(item.fullPath);
  217. }
  218. }
  219. this.bulkCloseTabs(pathList);
  220. }
  221. this.updateCacheTab();
  222. handleGotoPage(router);
  223. },
  224. async closeAllTab(router: Router) {
  225. this.tabList = this.tabList.filter((item) => item?.meta?.affix ?? false);
  226. this.clearCacheTabs();
  227. this.goToPage(router);
  228. },
  229. /**
  230. * Close other tabs
  231. */
  232. async closeOtherTabs(route: RouteLocationNormalized, router: Router) {
  233. const closePathList = this.tabList.map((item) => item.fullPath);
  234. const pathList: string[] = [];
  235. for (const path of closePathList) {
  236. if (path !== route.fullPath) {
  237. const closeItem = this.tabList.find((item) => item.path === path);
  238. if (!closeItem) {
  239. continue;
  240. }
  241. const affix = closeItem?.meta?.affix ?? false;
  242. if (!affix) {
  243. pathList.push(closeItem.fullPath);
  244. }
  245. }
  246. }
  247. this.bulkCloseTabs(pathList);
  248. this.updateCacheTab();
  249. handleGotoPage(router);
  250. },
  251. /**
  252. * Close tabs in bulk
  253. */
  254. async bulkCloseTabs(pathList: string[]) {
  255. this.tabList = this.tabList.filter((item) => !pathList.includes(item.fullPath));
  256. },
  257. /**
  258. * Set tab's title
  259. */
  260. async setTabTitle(title: string, route: RouteLocationNormalized) {
  261. const findTab = this.getTabList.find((item) => item === route);
  262. if (findTab) {
  263. findTab.meta.title = title;
  264. await this.updateCacheTab();
  265. }
  266. },
  267. },
  268. });
  269. // Need to be used outside the setup
  270. export function useMultipleTabWithOutStore() {
  271. return useMultipleTabStore(store);
  272. }