From 1e34d3e9e4c79a62a16c6209639f5394dea61148 Mon Sep 17 00:00:00 2001
From: xachary <179740385@qq.com>
Date: 星期三, 27 十二月 2023 10:19:34 +0800
Subject: [PATCH] feat(layout->tabs): support insert new tab after current tab (#3471)

---
 src/hooks/web/usePage.ts |   49 ++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/src/hooks/web/usePage.ts b/src/hooks/web/usePage.ts
index 9e78820..0d60d45 100644
--- a/src/hooks/web/usePage.ts
+++ b/src/hooks/web/usePage.ts
@@ -8,6 +8,8 @@
 import { isHttpUrl } from '@/utils/is';
 import { openWindow } from '@/utils';
 
+import { useMultipleTabStore } from '@/store/modules/multipleTab';
+
 export type PathAsPageEnum<T> = T extends { path: string } ? T & { path: PageEnum } : T;
 export type RouteLocationRawEx = PathAsPageEnum<RouteLocationRaw>;
 
@@ -15,12 +17,24 @@
   console.error(e);
 }
 
+export enum GoType {
+  'replace',
+  'after',
+}
+
 /**
  * page switch
  */
 export function useGo(_router?: Router) {
-  const { push, replace } = _router || useRouter();
-  function go(opt: RouteLocationRawEx = PageEnum.BASE_HOME, isReplace = false) {
+  const { push, replace, currentRoute } = _router || useRouter();
+
+  function go(opt?: RouteLocationRawEx): void;
+  function go(opt: RouteLocationRawEx, isReplace: boolean): void;
+  function go(opt: RouteLocationRawEx, goType: GoType): void;
+  function go(
+    opt: RouteLocationRawEx = PageEnum.BASE_HOME,
+    goTypeOrIsReplace: boolean | GoType = false,
+  ) {
     if (!opt) {
       return;
     }
@@ -31,7 +45,36 @@
     if (isHttpUrl(path)) {
       return openWindow(path);
     }
-    isReplace ? replace(opt).catch(handleError) : push(opt).catch(handleError);
+
+    const isReplace = goTypeOrIsReplace === true || goTypeOrIsReplace === GoType.replace;
+    const isAfter = goTypeOrIsReplace === GoType.after;
+
+    if (isReplace) {
+      replace(opt).catch(handleError);
+    } else if (isAfter) {
+      const tabStore = useMultipleTabStore();
+      const currentName = unref(currentRoute).name;
+      // 褰撳墠 tab
+      const currentIndex = tabStore.getTabList.findIndex((item) => item.name === currentName);
+      // 褰撳墠 tab 鏁伴噺
+      const currentCount = tabStore.getTabList.length;
+      push(opt)
+        .then(() => {
+          if (tabStore.getTabList.length > currentCount) {
+            // 浜х敓鏂� tab
+            // 鏂� tab锛堜篃鏄渶鍚庝竴涓級
+            const targetIndex = tabStore.getTabList.length - 1;
+            // 鏂� tab 鍦� 褰撳墠 tab 鐨勫悗闈�
+            if (currentIndex > -1 && targetIndex > currentIndex) {
+              // 绉诲姩 tab
+              tabStore.sortTabs(targetIndex, currentIndex + 1);
+            }
+          }
+        })
+        .catch(handleError);
+    } else {
+      push(opt).catch(handleError);
+    }
   }
   return go;
 }

--
Gitblit v1.8.0