multipleTab.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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, QIANKUN_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. import { useGlobSetting } from '/@/hooks/setting';
  19. function handleGotoPage(router: Router) {
  20. const go = useGo(router);
  21. go(unref(router.currentRoute).path, true);
  22. }
  23. const getToTarget = (tabItem: RouteLocationNormalized) => {
  24. const { params, path, query } = tabItem;
  25. return {
  26. params: params || {},
  27. path,
  28. query: query || {},
  29. };
  30. };
  31. const cacheTab = projectSetting.multiTabsSetting.cache;
  32. const glob = useGlobSetting();
  33. export const useMultipleTabStore = defineStore({
  34. id: 'app-multiple-tab',
  35. state: (): MultipleTabState => ({
  36. // Tabs that need to be cached
  37. cacheTabList: new Set(),
  38. // multiple tab list
  39. tabList: cacheTab ? Persistent.getLocal(MULTIPLE_TABS_KEY) || [] : [],
  40. // Index of the last moved tab
  41. lastDragEndIndex: 0,
  42. }),
  43. getters: {
  44. getTabList(): RouteLocationNormalized[] {
  45. return this.tabList;
  46. },
  47. getCachedTabList(): string[] {
  48. return Array.from(this.cacheTabList);
  49. },
  50. getLastDragEndIndex(): number {
  51. return this.lastDragEndIndex;
  52. },
  53. },
  54. actions: {
  55. /**
  56. * Update the cache according to the currently opened tabs
  57. */
  58. async updateCacheTab() {
  59. const cacheMap: Set<string> = new Set();
  60. for (const tab of this.tabList) {
  61. const item = getRawRoute(tab);
  62. // Ignore the cache
  63. const needCache = !item.meta?.ignoreKeepAlive;
  64. if (!needCache) {
  65. continue;
  66. }
  67. const name = item.name as string;
  68. cacheMap.add(name);
  69. }
  70. this.cacheTabList = cacheMap;
  71. },
  72. /**
  73. * Refresh tabs
  74. */
  75. async refreshPage(router: Router) {
  76. const { currentRoute } = router;
  77. const route = unref(currentRoute);
  78. const name = route.name;
  79. const findTab = this.getCachedTabList.find((item) => item === name);
  80. if (findTab) {
  81. this.cacheTabList.delete(findTab);
  82. }
  83. const redo = useRedo(router);
  84. await redo();
  85. },
  86. clearCacheTabs(): void {
  87. this.cacheTabList = new Set();
  88. },
  89. resetState(): void {
  90. this.tabList = [];
  91. this.clearCacheTabs();
  92. },
  93. goToPage(router: Router) {
  94. const go = useGo(router);
  95. const len = this.tabList.length;
  96. const { path } = unref(router.currentRoute);
  97. let toPath: PageEnum | string = glob.homePath || PageEnum.BASE_HOME;
  98. if (len > 0) {
  99. const page = this.tabList[len - 1];
  100. const p = page.fullPath || page.path;
  101. if (p) {
  102. toPath = p;
  103. }
  104. }
  105. // Jump to the current page and report an error
  106. path !== toPath && go(toPath as PageEnum, true);
  107. },
  108. async addTab(route: RouteLocationNormalized) {
  109. const { path, name, fullPath, params, query, meta } = getRawRoute(route);
  110. // 404 The page does not need to add a tab
  111. if (
  112. path === PageEnum.ERROR_PAGE ||
  113. path === PageEnum.BASE_LOGIN ||
  114. !name ||
  115. [REDIRECT_ROUTE.name, PAGE_NOT_FOUND_ROUTE.name].includes(name as string)
  116. ) {
  117. return;
  118. }
  119. let updateIndex = -1;
  120. // Existing pages, do not add tabs repeatedly
  121. const tabHasExits = this.tabList.some((tab, index) => {
  122. updateIndex = index;
  123. return (tab.fullPath || tab.path) === (fullPath || path);
  124. });
  125. // If the tab already exists, perform the update operation
  126. if (tabHasExits) {
  127. const curTab = toRaw(this.tabList)[updateIndex];
  128. if (!curTab) {
  129. return;
  130. }
  131. curTab.params = params || curTab.params;
  132. curTab.query = query || curTab.query;
  133. curTab.fullPath = fullPath || curTab.fullPath;
  134. this.tabList.splice(updateIndex, 1, curTab);
  135. } else {
  136. // Add tab
  137. // 获取动态路由打开数,超过 0 即代表需要控制打开数
  138. const dynamicLevel = meta?.dynamicLevel ?? -1;
  139. if (dynamicLevel > 0) {
  140. // 如果动态路由层级大于 0 了,那么就要限制该路由的打开数限制了
  141. // 首先获取到真实的路由,使用配置方式减少计算开销.
  142. // const realName: string = path.match(/(\S*)\//)![1];
  143. const realPath = meta?.realPath ?? '';
  144. // 获取到已经打开的动态路由数, 判断是否大于某一个值
  145. if (this.tabList.filter((e) => e.meta?.realPath ?? '' === realPath).length >= dynamicLevel) {
  146. // 关闭第一个
  147. const index = this.tabList.findIndex((item) => item.meta.realPath === realPath);
  148. index !== -1 && this.tabList.splice(index, 1);
  149. }
  150. }
  151. this.tabList.push(route);
  152. }
  153. this.updateCacheTab();
  154. cacheTab && Persistent.setLocal(MULTIPLE_TABS_KEY, this.tabList);
  155. },
  156. async closeTab(tab: RouteLocationNormalized, router: Router) {
  157. const close = (route: RouteLocationNormalized) => {
  158. const { fullPath, meta: { affix } = {} } = route;
  159. if (affix) {
  160. return;
  161. }
  162. const index = this.tabList.findIndex((item) => item.fullPath === fullPath);
  163. index !== -1 && this.tabList.splice(index, 1);
  164. };
  165. const { currentRoute, replace } = router;
  166. const { path } = unref(currentRoute);
  167. if (path !== tab.path) {
  168. // Closed is not the activation tab
  169. close(tab);
  170. return;
  171. }
  172. // Closed is activated atb
  173. let toTarget: RouteLocationRaw = {};
  174. const index = this.tabList.findIndex((item) => item.path === path);
  175. // If the current is the leftmost tab
  176. if (index === 0) {
  177. // There is only one tab, then jump to the homepage, otherwise jump to the right tab
  178. if (this.tabList.length === 1) {
  179. const userStore = useUserStore();
  180. toTarget = userStore.getUserInfo.homePath || glob.homePath || PageEnum.BASE_HOME;
  181. } else {
  182. // Jump to the right tab
  183. const page = this.tabList[index + 1];
  184. toTarget = getToTarget(page);
  185. }
  186. } else {
  187. // Close the current tab
  188. const page = this.tabList[index - 1];
  189. toTarget = getToTarget(page);
  190. }
  191. close(currentRoute.value);
  192. await replace(toTarget);
  193. },
  194. // Close according to key
  195. async closeTabByKey(key: string, router: Router) {
  196. const index = this.tabList.findIndex((item) => (item.fullPath || item.path) === key);
  197. if (index !== -1) {
  198. await this.closeTab(this.tabList[index], router);
  199. const { currentRoute, replace } = router;
  200. // 检查当前路由是否存在于tabList中
  201. const isActivated = this.tabList.findIndex((item) => {
  202. return item.fullPath === currentRoute.value.fullPath;
  203. });
  204. // 如果当前路由不存在于TabList中,尝试切换到其它路由
  205. if (isActivated === -1) {
  206. let pageIndex;
  207. if (index > 0) {
  208. pageIndex = index - 1;
  209. } else if (index < this.tabList.length - 1) {
  210. pageIndex = index + 1;
  211. } else {
  212. pageIndex = -1;
  213. }
  214. if (pageIndex >= 0) {
  215. const page = this.tabList[index - 1];
  216. const toTarget = getToTarget(page);
  217. await replace(toTarget);
  218. }
  219. }
  220. }
  221. },
  222. // Sort the tabs
  223. async sortTabs(oldIndex: number, newIndex: number) {
  224. const currentTab = this.tabList[oldIndex];
  225. this.tabList.splice(oldIndex, 1);
  226. this.tabList.splice(newIndex, 0, currentTab);
  227. this.lastDragEndIndex = this.lastDragEndIndex + 1;
  228. },
  229. // Close the tab on the right and jump
  230. async closeLeftTabs(route: RouteLocationNormalized, router: Router) {
  231. const index = this.tabList.findIndex((item) => item.path === route.path);
  232. if (index > 0) {
  233. const leftTabs = this.tabList.slice(0, index);
  234. const pathList: string[] = [];
  235. for (const item of leftTabs) {
  236. const affix = item?.meta?.affix ?? false;
  237. if (!affix) {
  238. pathList.push(item.fullPath);
  239. }
  240. }
  241. this.bulkCloseTabs(pathList);
  242. }
  243. this.updateCacheTab();
  244. handleGotoPage(router);
  245. },
  246. // Close the tab on the left and jump
  247. async closeRightTabs(route: RouteLocationNormalized, router: Router) {
  248. const index = this.tabList.findIndex((item) => item.fullPath === route.fullPath);
  249. if (index >= 0 && index < this.tabList.length - 1) {
  250. const rightTabs = this.tabList.slice(index + 1, this.tabList.length);
  251. const pathList: string[] = [];
  252. for (const item of rightTabs) {
  253. const affix = item?.meta?.affix ?? false;
  254. if (!affix) {
  255. pathList.push(item.fullPath);
  256. }
  257. }
  258. this.bulkCloseTabs(pathList);
  259. }
  260. this.updateCacheTab();
  261. handleGotoPage(router);
  262. },
  263. async closeAllTab(router: Router) {
  264. this.tabList = this.tabList.filter((item) => item?.meta?.affix ?? false);
  265. this.clearCacheTabs();
  266. this.goToPage(router);
  267. },
  268. /**
  269. * Close other tabs
  270. */
  271. async closeOtherTabs(route: RouteLocationNormalized, router: Router) {
  272. const closePathList = this.tabList.map((item) => item.fullPath);
  273. const pathList: string[] = [];
  274. for (const path of closePathList) {
  275. if (path !== route.fullPath) {
  276. const closeItem = this.tabList.find((item) => item.path === path);
  277. if (!closeItem) {
  278. continue;
  279. }
  280. const affix = closeItem?.meta?.affix ?? false;
  281. if (!affix) {
  282. pathList.push(closeItem.fullPath);
  283. }
  284. }
  285. }
  286. this.bulkCloseTabs(pathList);
  287. this.updateCacheTab();
  288. handleGotoPage(router);
  289. },
  290. /**
  291. * Close tabs in bulk
  292. */
  293. async bulkCloseTabs(pathList: string[]) {
  294. this.tabList = this.tabList.filter((item) => !pathList.includes(item.fullPath));
  295. },
  296. /**
  297. * Set tab's title
  298. */
  299. async setTabTitle(title: string, route: RouteLocationNormalized) {
  300. const findTab = this.getTabList.find((item) => item === route);
  301. if (findTab) {
  302. findTab.meta.title = title;
  303. await this.updateCacheTab();
  304. }
  305. },
  306. /**
  307. * replace tab's path
  308. * **/
  309. async updateTabPath(fullPath: string, route: RouteLocationNormalized) {
  310. const findTab = this.getTabList.find((item) => item === route);
  311. if (findTab) {
  312. findTab.fullPath = fullPath;
  313. findTab.path = fullPath;
  314. await this.updateCacheTab();
  315. }
  316. },
  317. },
  318. });
  319. // Need to be used outside the setup
  320. export function useMultipleTabWithOutStore() {
  321. return useMultipleTabStore(store);
  322. }