@liujitcn/kratos-taro-app-core 0.0.23 → 0.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/navigation.ts +48 -2
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "type": "git",
8
8
  "url": "git@github.com:liujitcn/kratos-admin.git"
9
9
  },
10
- "version": "0.0.23",
10
+ "version": "0.0.24",
11
11
  "type": "module",
12
12
  "files": [
13
13
  "dist",
package/src/navigation.ts CHANGED
@@ -1,4 +1,4 @@
1
- import Taro from '@tarojs/taro'
1
+ import Taro, { getCurrentPages } from '@tarojs/taro'
2
2
  import { create } from 'zustand'
3
3
  import { defBaseMenuService } from './api/base/menu'
4
4
  import { resolveStaticView } from './module'
@@ -153,6 +153,9 @@ const defaultMenuTitleKeys: Record<string, string> = {
153
153
  AppAi: 'system.ai.chat_title',
154
154
  }
155
155
 
156
+ let tabNavigationTarget: string | undefined
157
+ const nativeTabViewKeys = ['HOME', 'PROFILE_HOME']
158
+
156
159
  function localizedDefaultAppMenus(): AppMenu[] {
157
160
  return defaultAppMenus.map((menu) => ({
158
161
  ...menu,
@@ -242,13 +245,56 @@ export function navigateAppRoute(
242
245
  .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
243
246
  .join('&')
244
247
  const url = `/${resolved.physicalRoute}${query ? `?${query}` : ''}`
245
- if (options.replace || resolved.menu.inTabBar) {
248
+ if (options.replace) {
246
249
  void Taro.reLaunch({ url })
247
250
  return
248
251
  }
252
+ if (resolved.menu.inTabBar) {
253
+ navigateTabRoute(url)
254
+ return
255
+ }
249
256
  void Taro.navigateTo({ url, fail: () => void Taro.reLaunch({ url }) })
250
257
  }
251
258
 
259
+ /** 切换 tab 页面,优先复用已有页面,避免重建页面栈产生空白帧。 */
260
+ function navigateTabRoute(url: string): void {
261
+ const targetRoute = url.slice(1).split('?', 1)[0].replace(/^\/+/, '')
262
+ const pages = getCurrentPages()
263
+ const currentIndex = pages.length - 1
264
+ const currentRoute = pages[currentIndex]?.route?.replace(/^\/+/, '')
265
+ if (currentRoute === targetRoute || tabNavigationTarget) return
266
+
267
+ tabNavigationTarget = targetRoute
268
+ const release = () => {
269
+ if (tabNavigationTarget === targetRoute) tabNavigationTarget = undefined
270
+ }
271
+ if (nativeTabViewKeys.some((viewKey) => resolveStaticView(viewKey) === targetRoute)) {
272
+ void Taro.switchTab({ url }).then(release).catch(() => {
273
+ navigateTabRouteInStack(url, targetRoute, pages, currentIndex, release)
274
+ })
275
+ return
276
+ }
277
+ navigateTabRouteInStack(url, targetRoute, pages, currentIndex, release)
278
+ }
279
+
280
+ /** 在没有原生 tab 路由的动态菜单上复用页面栈。 */
281
+ function navigateTabRouteInStack(
282
+ url: string,
283
+ targetRoute: string,
284
+ pages: ReturnType<typeof getCurrentPages>,
285
+ currentIndex: number,
286
+ release: () => void,
287
+ ): void {
288
+ const targetIndex = pages.findIndex((page) => page.route?.replace(/^\/+/, '') === targetRoute)
289
+ const navigation =
290
+ targetIndex >= 0 && targetIndex < currentIndex
291
+ ? Taro.navigateBack({ delta: currentIndex - targetIndex })
292
+ : Taro.navigateTo({ url })
293
+ void navigation.then(release).catch(() => {
294
+ void Taro.reLaunch({ url }).then(release, release)
295
+ })
296
+ }
297
+
252
298
  /** 打开可替换的状态视图。 */
253
299
  export function launchAppStatus(
254
300
  state: import('./module').BootstrapViewKey,