@tarojs/router 4.0.0-beta.75 → 4.0.0-beta.76

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.
package/dist/index.esm.js CHANGED
@@ -14,7 +14,8 @@ import UniversalRouter from 'universal-router';
14
14
  function loadAnimateStyle(ms = 300) {
15
15
  const css = `
16
16
  body {
17
- overflow: hidden; // 防止 iOS 页面滚动
17
+ /* 防止 iOS 页面滚动 */
18
+ overflow: hidden;
18
19
  }
19
20
  .taro_router > .taro_page {
20
21
  position: absolute;
@@ -71,8 +72,9 @@ ${enableTabBar ? `
71
72
  }
72
73
 
73
74
  ` : ''}
74
- .taro_page_shade,
75
- .taro_router > .taro_page.taro_page_show.taro_page_stationed:not(.taro_page_shade):not(.taro_tabbar_page):not(:last-child) {
75
+ .taro_page_shade:has(+.taro_page_stationed),
76
+ .taro_page_shade.taro_tabbar_page,
77
+ .taro_router > .taro_page.taro_page_show.taro_page_stationed:not(.taro_page_shade):not(.taro_tabbar_page):not(:last-child):has(+.taro_page_stationed) {
76
78
  display: none;
77
79
  }
78
80
  `;
@@ -140,7 +142,7 @@ function loadNavigationBarStyle() {
140
142
  to {
141
143
  transform: rotate(360deg);
142
144
  }
143
- }
145
+ }
144
146
 
145
147
  .taro-navigation-bar-no-icon > .taro-navigation-bar-home {
146
148
  display: none;
@@ -262,8 +264,9 @@ class RouterConfig {
262
264
  return this.router.mode || 'hash';
263
265
  }
264
266
  static get customRoutes() { return this.router.customRoutes || {}; }
267
+ // 这个方法不考虑 basename 和 customRoutes,只判断原始的 url 是否在 pages 中
265
268
  static isPage(url = '') {
266
- return this.pages.findIndex(e => prependBasename(e) === url) !== -1;
269
+ return this.pages.findIndex(e => addLeadingSlash(e) === url) !== -1;
267
270
  }
268
271
  }
269
272
 
@@ -532,13 +535,14 @@ function processNavigateUrl(option) {
532
535
  const parts = routesAlias.getOrigin(history.location.pathname).split('/');
533
536
  parts.pop();
534
537
  pathPieces.pathname.split('/').forEach((item) => {
535
- if (item === '.') {
538
+ if (item === '.')
536
539
  return;
537
- }
538
540
  item === '..' ? parts.pop() : parts.push(item);
539
541
  });
540
542
  pathPieces.pathname = parts.join('/');
541
543
  }
544
+ // 确保是 / 开头的路径
545
+ pathPieces.pathname = addLeadingSlash(pathPieces.pathname);
542
546
  // 处理自定义路由
543
547
  pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname));
544
548
  // 处理 basename
@@ -568,20 +572,6 @@ function navigate(option, method) {
568
572
  if ('url' in option) {
569
573
  const pathPieces = processNavigateUrl(option);
570
574
  const state = { timestamp: Date.now() };
571
- if (pathPieces.pathname) {
572
- const originPath = routesAlias.getOrigin(pathPieces.pathname);
573
- if (!RouterConfig.isPage(addLeadingSlash(originPath)) && !RouterConfig.isPage(addLeadingSlash(pathPieces.pathname))) {
574
- const res = { errMsg: `${method}:fail page ${originPath} is not found` };
575
- fail === null || fail === void 0 ? void 0 : fail(res);
576
- complete === null || complete === void 0 ? void 0 : complete(res);
577
- if (fail || complete) {
578
- return resolve(res);
579
- }
580
- else {
581
- return reject(res);
582
- }
583
- }
584
- }
585
575
  if (method === 'navigateTo') {
586
576
  history.push(pathPieces, state);
587
577
  }
@@ -852,8 +842,8 @@ const launchStampId$1 = createStampId$1();
852
842
  * - 不支持路由动画
853
843
  */
854
844
  function createMultiRouter(history, app, config, framework) {
855
- var _a, _b, _c, _d, _e, _f;
856
845
  return __awaiter(this, void 0, void 0, function* () {
846
+ var _a, _b, _c, _d, _e, _f;
857
847
  if (typeof app.onUnhandledRejection === 'function') {
858
848
  window.addEventListener('unhandledrejection', app.onUnhandledRejection);
859
849
  }
@@ -861,7 +851,7 @@ function createMultiRouter(history, app, config, framework) {
861
851
  RouterConfig.config = config;
862
852
  const handler = new MultiPageHandler(config, history);
863
853
  const launchParam = {
864
- path: config.pageName,
854
+ path: config.pageName, // 多页面模式没新开一个页面相当于重启,所以直接使用当前页面路径
865
855
  query: handler.getQuery(launchStampId$1),
866
856
  scene: 0,
867
857
  shareTicket: '',
@@ -940,7 +930,7 @@ class NavigationBarHandler {
940
930
  });
941
931
  }
942
932
  toHomeFn() {
943
- reLaunch({ url: this.pageContext.homePage });
933
+ reLaunch({ url: this.pageContext.originHomePage });
944
934
  }
945
935
  backFn() {
946
936
  navigateBack();
@@ -989,7 +979,7 @@ class NavigationBarHandler {
989
979
  this.setNavigationLoading();
990
980
  }
991
981
  setCacheValue() {
992
- const currentPage = this.pageContext.currentPage;
982
+ const currentPage = this.pageContext.originPathname;
993
983
  if (typeof this.cache[currentPage] !== 'object') {
994
984
  this.cache[currentPage] = {};
995
985
  }
@@ -1020,7 +1010,7 @@ class NavigationBarHandler {
1020
1010
  var _a;
1021
1011
  if (!this.navigationBarElement)
1022
1012
  return;
1023
- const currentPage = this.pageContext.currentPage;
1013
+ const currentPage = this.pageContext.originPathname;
1024
1014
  let isShow;
1025
1015
  if (typeof show === 'boolean') {
1026
1016
  isShow = show;
@@ -1045,7 +1035,7 @@ class NavigationBarHandler {
1045
1035
  var _a, _b, _c;
1046
1036
  if (!this.navigationBarElement)
1047
1037
  return;
1048
- const currentPage = this.pageContext.currentPage;
1038
+ const currentPage = this.pageContext.originPathname;
1049
1039
  let color;
1050
1040
  if (typeof backgroundColor === 'string') {
1051
1041
  color = backgroundColor;
@@ -1069,7 +1059,7 @@ class NavigationBarHandler {
1069
1059
  var _a, _b, _c;
1070
1060
  if (!this.navigationBarElement)
1071
1061
  return;
1072
- const currentPage = this.pageContext.currentPage;
1062
+ const currentPage = this.pageContext.originPathname;
1073
1063
  let color;
1074
1064
  if (typeof fontColor === 'string') {
1075
1065
  color = fontColor;
@@ -1091,7 +1081,7 @@ class NavigationBarHandler {
1091
1081
  }
1092
1082
  setTitle(title) {
1093
1083
  var _a, _b, _c;
1094
- const currentPage = this.pageContext.currentPage;
1084
+ const currentPage = this.pageContext.originPathname;
1095
1085
  let proceedTitle;
1096
1086
  if (typeof title === 'string') {
1097
1087
  proceedTitle = title;
@@ -1172,6 +1162,7 @@ class PageHandler {
1172
1162
  this.defaultAnimation = { duration: 300, delay: 50 };
1173
1163
  this.config = config;
1174
1164
  this.homePage = getHomePage(this.routes[0].path, this.basename, this.customRoutes, this.config.entryPagePath);
1165
+ this.originHomePage = this.config.entryPagePath || this.routes[0].path || this.basename;
1175
1166
  this.mount();
1176
1167
  this.navigationBarHandler = new NavigationBarHandler(this);
1177
1168
  }
@@ -1205,14 +1196,14 @@ class PageHandler {
1205
1196
  }
1206
1197
  set pathname(p) { this.router.pathname = p; }
1207
1198
  get pathname() { return this.router.pathname; }
1199
+ // Note: 把 pathname 转换为原始路径,主要是处理 customRoutes 和 basename
1200
+ get originPathname() { return routesAlias.getOrigin(addLeadingSlash(stripBasename(this.pathname, this.basename))); }
1208
1201
  get basename() { return this.router.basename || ''; }
1209
1202
  get pageConfig() {
1210
- const routePath = addLeadingSlash(stripBasename(this.pathname, this.basename));
1211
1203
  const homePage = addLeadingSlash(this.homePage);
1212
1204
  return this.routes.find(r => {
1213
- var _a;
1214
1205
  const pagePath = addLeadingSlash(r.path);
1215
- return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
1206
+ return [pagePath, homePage].includes(this.originPathname);
1216
1207
  });
1217
1208
  }
1218
1209
  isTabBar(pathname) {
@@ -1402,24 +1393,35 @@ class PageHandler {
1402
1393
  });
1403
1394
  }
1404
1395
  }
1405
- hide(page) {
1406
- var _a;
1396
+ hide(page, animation = false) {
1397
+ var _a, _b, _c, _d, _e, _f, _g;
1407
1398
  if (!page)
1408
1399
  return;
1409
1400
  // NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
1410
1401
  const pageEl = this.getPageContainer(page);
1411
1402
  if (pageEl) {
1412
- if (this.hideTimer) {
1413
- clearTimeout(this.hideTimer);
1414
- this.hideTimer = null;
1415
- pageEl.classList.add('taro_page_shade');
1403
+ if (animation) {
1404
+ if (this.hideTimer) {
1405
+ clearTimeout(this.hideTimer);
1406
+ this.hideTimer = null;
1407
+ (_c = (_b = (_a = this.lastHidePage) === null || _a === void 0 ? void 0 : _a.classList) === null || _b === void 0 ? void 0 : _b.add) === null || _c === void 0 ? void 0 : _c.call(_b, 'taro_page_shade');
1408
+ }
1409
+ this.lastHidePage = pageEl;
1410
+ this.hideTimer = setTimeout(() => {
1411
+ this.hideTimer = null;
1412
+ pageEl.classList.add('taro_page_shade');
1413
+ }, this.animationDuration + this.animationDelay);
1414
+ (_d = page.onHide) === null || _d === void 0 ? void 0 : _d.call(page);
1416
1415
  }
1417
- this.lastHidePage = pageEl;
1418
- this.hideTimer = setTimeout(() => {
1419
- this.hideTimer = null;
1416
+ else {
1417
+ if (this.hideTimer) {
1418
+ clearTimeout(this.hideTimer);
1419
+ this.hideTimer = null;
1420
+ (_g = (_f = (_e = this.lastHidePage) === null || _e === void 0 ? void 0 : _e.classList) === null || _f === void 0 ? void 0 : _f.add) === null || _g === void 0 ? void 0 : _g.call(_f, 'taro_page_shade');
1421
+ }
1420
1422
  pageEl.classList.add('taro_page_shade');
1421
- }, this.animationDuration + this.animationDelay);
1422
- (_a = page.onHide) === null || _a === void 0 ? void 0 : _a.call(page);
1423
+ this.lastHidePage = pageEl;
1424
+ }
1423
1425
  }
1424
1426
  else {
1425
1427
  setTimeout(() => this.hide(page), 0);
@@ -1511,10 +1513,10 @@ function createRouter(history, app, config, framework) {
1511
1513
  eventCenter.trigger('__taroRouterLaunch', launchParam);
1512
1514
  (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
1513
1515
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
1514
- const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
1515
- var _c, _d, _e, _f, _g, _h, _j, _k;
1516
+ const render = (_c) => __awaiter(this, [_c], void 0, function* ({ location, action }) {
1517
+ var _d, _e, _f, _g, _h, _j, _k, _l;
1516
1518
  handler.pathname = decodeURI(location.pathname);
1517
- if ((_c = window.__taroAppConfig) === null || _c === void 0 ? void 0 : _c.usingWindowScroll)
1519
+ if ((_d = window.__taroAppConfig) === null || _d === void 0 ? void 0 : _d.usingWindowScroll)
1518
1520
  window.scrollTo(0, 0);
1519
1521
  eventCenter.trigger('__taroRouterChange', {
1520
1522
  toLocation: {
@@ -1533,7 +1535,7 @@ function createRouter(history, app, config, framework) {
1533
1535
  path: handler.pathname,
1534
1536
  query: handler.getQuery(createStampId()),
1535
1537
  };
1536
- (_d = app.onPageNotFound) === null || _d === void 0 ? void 0 : _d.call(app, notFoundEvent);
1538
+ (_e = app.onPageNotFound) === null || _e === void 0 ? void 0 : _e.call(app, notFoundEvent);
1537
1539
  eventCenter.trigger('__taroRouterNotFound', notFoundEvent);
1538
1540
  }
1539
1541
  else if (/Loading hot update .* failed./.test(error.message)) {
@@ -1547,10 +1549,10 @@ function createRouter(history, app, config, framework) {
1547
1549
  if (!element)
1548
1550
  return;
1549
1551
  const pageConfig = handler.pageConfig;
1550
- let enablePullDownRefresh = ((_e = config === null || config === void 0 ? void 0 : config.window) === null || _e === void 0 ? void 0 : _e.enablePullDownRefresh) || false;
1551
- let navigationStyle = ((_f = config === null || config === void 0 ? void 0 : config.window) === null || _f === void 0 ? void 0 : _f.navigationStyle) || 'default';
1552
- let navigationBarTextStyle = ((_g = config === null || config === void 0 ? void 0 : config.window) === null || _g === void 0 ? void 0 : _g.navigationBarTextStyle) || 'white';
1553
- let navigationBarBackgroundColor = ((_h = config === null || config === void 0 ? void 0 : config.window) === null || _h === void 0 ? void 0 : _h.navigationBarBackgroundColor) || '#000000';
1552
+ let enablePullDownRefresh = ((_f = config === null || config === void 0 ? void 0 : config.window) === null || _f === void 0 ? void 0 : _f.enablePullDownRefresh) || false;
1553
+ let navigationStyle = ((_g = config === null || config === void 0 ? void 0 : config.window) === null || _g === void 0 ? void 0 : _g.navigationStyle) || 'default';
1554
+ let navigationBarTextStyle = ((_h = config === null || config === void 0 ? void 0 : config.window) === null || _h === void 0 ? void 0 : _h.navigationBarTextStyle) || 'white';
1555
+ let navigationBarBackgroundColor = ((_j = config === null || config === void 0 ? void 0 : config.window) === null || _j === void 0 ? void 0 : _j.navigationBarBackgroundColor) || '#000000';
1554
1556
  if (pageConfig) {
1555
1557
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
1556
1558
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
@@ -1568,7 +1570,7 @@ function createRouter(history, app, config, framework) {
1568
1570
  eventCenter.trigger('__taroSetNavigationStyle', navigationStyle, navigationBarTextStyle, navigationBarBackgroundColor);
1569
1571
  const currentPage = Current.page;
1570
1572
  const pathname = handler.pathname;
1571
- const methodName = (_j = stacks.method) !== null && _j !== void 0 ? _j : '';
1573
+ const methodName = (_k = stacks.method) !== null && _k !== void 0 ? _k : '';
1572
1574
  const cacheTabs = stacks.getTabs();
1573
1575
  let shouldLoad = false;
1574
1576
  stacks.method = '';
@@ -1587,6 +1589,7 @@ function createRouter(history, app, config, framework) {
1587
1589
  if (handler.isSamePage(currentPage))
1588
1590
  return;
1589
1591
  if (handler.isTabBar(currentPage.path)) {
1592
+ // NOTE: 从 tabBar 页面切换到 tabBar 页面
1590
1593
  handler.hide(currentPage);
1591
1594
  stacks.pushTab(currentPage.path.split('?')[0]);
1592
1595
  }
@@ -1630,11 +1633,11 @@ function createRouter(history, app, config, framework) {
1630
1633
  shouldLoad = true;
1631
1634
  }
1632
1635
  else if (action === 'PUSH') {
1633
- handler.hide(currentPage);
1636
+ handler.hide(currentPage, true);
1634
1637
  shouldLoad = true;
1635
1638
  }
1636
1639
  if (shouldLoad || stacks.length < 1) {
1637
- const el = (_k = element.default) !== null && _k !== void 0 ? _k : element;
1640
+ const el = (_l = element.default) !== null && _l !== void 0 ? _l : element;
1638
1641
  const loadConfig = Object.assign({}, pageConfig);
1639
1642
  const stacksIndex = stacks.length;
1640
1643
  delete loadConfig['path'];
@@ -1,3 +1,2 @@
1
- import { MpaRouterConfig, SpaRouterConfig } from '../types/router';
2
- declare function initNavigationBar(config: SpaRouterConfig | MpaRouterConfig, container: HTMLElement): void;
3
- export { initNavigationBar };
1
+ import type { MpaRouterConfig, SpaRouterConfig } from '../types/router';
2
+ export declare function initNavigationBar(config: SpaRouterConfig | MpaRouterConfig, container: HTMLElement): void;
@@ -1,12 +1,11 @@
1
- import { MpaRouterConfig, SpaRouterConfig } from '../../types/router';
2
- declare class RouterConfig {
1
+ import type { MpaRouterConfig, SpaRouterConfig } from '../../types/router';
2
+ export declare class RouterConfig {
3
3
  private static __config;
4
4
  static set config(e: SpaRouterConfig | MpaRouterConfig);
5
5
  static get config(): SpaRouterConfig | MpaRouterConfig;
6
6
  static get pages(): string[];
7
- static get router(): import("../types/router").Router;
7
+ static get router(): import("../../types/router").Router;
8
8
  static get mode(): "hash" | "browser" | "multi";
9
9
  static get customRoutes(): Record<string, string | string[]>;
10
10
  static isPage(url?: string): boolean;
11
11
  }
12
- export { RouterConfig };
@@ -1,4 +1,4 @@
1
- import { prependBasename } from '../history.js';
1
+ import { addLeadingSlash } from '@tarojs/runtime';
2
2
 
3
3
  class RouterConfig {
4
4
  static set config(e) {
@@ -17,8 +17,9 @@ class RouterConfig {
17
17
  return this.router.mode || 'hash';
18
18
  }
19
19
  static get customRoutes() { return this.router.customRoutes || {}; }
20
+ // 这个方法不考虑 basename 和 customRoutes,只判断原始的 url 是否在 pages 中
20
21
  static isPage(url = '') {
21
- return this.pages.findIndex(e => prependBasename(e) === url) !== -1;
22
+ return this.pages.findIndex(e => addLeadingSlash(e) === url) !== -1;
22
23
  }
23
24
  }
24
25
 
@@ -1,6 +1,6 @@
1
- import { AppInstance } from '@tarojs/runtime';
2
- import { History } from "../history.js";
3
- import { MpaRouterConfig } from '../../types/router';
1
+ import type { AppInstance } from '@tarojs/runtime';
2
+ import type { History } from 'history';
3
+ import type { MpaRouterConfig } from '../../types/router';
4
4
  /** Note: 关于多页面应用
5
5
  * - 需要配置路由映射(根目录跳转、404 页面……)
6
6
  * - app.onPageNotFound 事件不支持
@@ -8,5 +8,4 @@ import { MpaRouterConfig } from '../../types/router';
8
8
  * - TabBar 会多次加载
9
9
  * - 不支持路由动画
10
10
  */
11
- declare function createMultiRouter(history: History, app: AppInstance, config: MpaRouterConfig, framework?: string): Promise<void>;
12
- export { createMultiRouter };
11
+ export declare function createMultiRouter(history: History, app: AppInstance, config: MpaRouterConfig, framework?: string): Promise<void>;
@@ -16,8 +16,8 @@ const launchStampId = createStampId();
16
16
  * - 不支持路由动画
17
17
  */
18
18
  function createMultiRouter(history, app, config, framework) {
19
- var _a, _b, _c, _d, _e, _f;
20
19
  return __awaiter(this, void 0, void 0, function* () {
20
+ var _a, _b, _c, _d, _e, _f;
21
21
  if (typeof app.onUnhandledRejection === 'function') {
22
22
  window.addEventListener('unhandledrejection', app.onUnhandledRejection);
23
23
  }
@@ -25,7 +25,7 @@ function createMultiRouter(history, app, config, framework) {
25
25
  RouterConfig.config = config;
26
26
  const handler = new MultiPageHandler(config, history);
27
27
  const launchParam = {
28
- path: config.pageName,
28
+ path: config.pageName, // 多页面模式没新开一个页面相当于重启,所以直接使用当前页面路径
29
29
  query: handler.getQuery(launchStampId),
30
30
  scene: 0,
31
31
  shareTicket: '',
@@ -1,13 +1,13 @@
1
- import { PageInstance } from '@tarojs/runtime';
2
- import { PageConfig } from '@tarojs/taro';
3
- import { History } from "../history.js";
4
- import { MpaRouterConfig, Route } from '../../types/router';
5
- declare class MultiPageHandler {
1
+ import type { PageInstance } from '@tarojs/runtime';
2
+ import type { PageConfig } from '@tarojs/taro';
3
+ import type { History } from 'history';
4
+ import type { MpaRouterConfig, Route } from '../../types/router';
5
+ export default class MultiPageHandler {
6
6
  history: History;
7
7
  protected config: MpaRouterConfig;
8
8
  constructor(config: MpaRouterConfig, history: History);
9
9
  get appId(): string;
10
- get router(): import("../types/router").Router;
10
+ get router(): import("../../types/router").Router;
11
11
  get routerMode(): "hash" | "browser" | "multi";
12
12
  get customRoutes(): Record<string, string | string[]>;
13
13
  get tabBarList(): import("@tarojs/taro").TabBarItem[];
@@ -27,8 +27,7 @@ declare class MultiPageHandler {
27
27
  onReady(page: PageInstance, onLoad?: boolean): void;
28
28
  load(page: PageInstance, pageConfig?: Route): void;
29
29
  getPageContainer(page?: PageInstance | null): HTMLElement | null;
30
- getScrollingElement(page?: PageInstance | null): HTMLElement | (Window & typeof globalThis);
30
+ getScrollingElement(page?: PageInstance | null): (Window & typeof globalThis) | HTMLElement;
31
31
  bindPageEvents(page: PageInstance, config?: Partial<PageConfig>): void;
32
32
  triggerRouterChange(): void;
33
33
  }
34
- export { MultiPageHandler as default };
@@ -1,4 +1,4 @@
1
- import PageHandler from "./page.js";
1
+ import type PageHandler from './page';
2
2
  interface NavigationBarCache {
3
3
  backgroundColor?: string;
4
4
  fontColor?: string;
@@ -6,7 +6,7 @@ interface NavigationBarCache {
6
6
  show?: boolean;
7
7
  loading?: boolean;
8
8
  }
9
- declare class NavigationBarHandler {
9
+ export default class NavigationBarHandler {
10
10
  pageContext: PageHandler;
11
11
  navigationBarElement: HTMLElement;
12
12
  cache: Record<string, NavigationBarCache>;
@@ -33,4 +33,4 @@ declare class NavigationBarHandler {
33
33
  fnBtnToggleToNone(): void;
34
34
  setNavigationBarVisible(show?: any): void;
35
35
  }
36
- export { NavigationBarHandler as default };
36
+ export {};
@@ -24,7 +24,7 @@ class NavigationBarHandler {
24
24
  });
25
25
  }
26
26
  toHomeFn() {
27
- reLaunch({ url: this.pageContext.homePage });
27
+ reLaunch({ url: this.pageContext.originHomePage });
28
28
  }
29
29
  backFn() {
30
30
  navigateBack();
@@ -73,7 +73,7 @@ class NavigationBarHandler {
73
73
  this.setNavigationLoading();
74
74
  }
75
75
  setCacheValue() {
76
- const currentPage = this.pageContext.currentPage;
76
+ const currentPage = this.pageContext.originPathname;
77
77
  if (typeof this.cache[currentPage] !== 'object') {
78
78
  this.cache[currentPage] = {};
79
79
  }
@@ -104,7 +104,7 @@ class NavigationBarHandler {
104
104
  var _a;
105
105
  if (!this.navigationBarElement)
106
106
  return;
107
- const currentPage = this.pageContext.currentPage;
107
+ const currentPage = this.pageContext.originPathname;
108
108
  let isShow;
109
109
  if (typeof show === 'boolean') {
110
110
  isShow = show;
@@ -129,7 +129,7 @@ class NavigationBarHandler {
129
129
  var _a, _b, _c;
130
130
  if (!this.navigationBarElement)
131
131
  return;
132
- const currentPage = this.pageContext.currentPage;
132
+ const currentPage = this.pageContext.originPathname;
133
133
  let color;
134
134
  if (typeof backgroundColor === 'string') {
135
135
  color = backgroundColor;
@@ -153,7 +153,7 @@ class NavigationBarHandler {
153
153
  var _a, _b, _c;
154
154
  if (!this.navigationBarElement)
155
155
  return;
156
- const currentPage = this.pageContext.currentPage;
156
+ const currentPage = this.pageContext.originPathname;
157
157
  let color;
158
158
  if (typeof fontColor === 'string') {
159
159
  color = fontColor;
@@ -175,7 +175,7 @@ class NavigationBarHandler {
175
175
  }
176
176
  setTitle(title) {
177
177
  var _a, _b, _c;
178
- const currentPage = this.pageContext.currentPage;
178
+ const currentPage = this.pageContext.originPathname;
179
179
  let proceedTitle;
180
180
  if (typeof title === 'string') {
181
181
  proceedTitle = title;
@@ -1,9 +1,9 @@
1
- import NavigationBarHandler from "./navigation-bar.js";
2
- import { PageInstance } from '@tarojs/runtime';
3
- import { PageConfig, RouterAnimate } from '@tarojs/taro';
4
- import { History } from "../history.js";
5
- import { Route, SpaRouterConfig } from '../../types/router';
6
- declare class PageHandler {
1
+ import NavigationBarHandler from './navigation-bar';
2
+ import type { PageInstance } from '@tarojs/runtime';
3
+ import type { PageConfig, RouterAnimate } from '@tarojs/taro';
4
+ import type { History } from 'history';
5
+ import type { Route, SpaRouterConfig } from '../../types/router';
6
+ export default class PageHandler {
7
7
  history: History;
8
8
  config: SpaRouterConfig;
9
9
  protected readonly defaultAnimation: RouterAnimate;
@@ -13,10 +13,11 @@ declare class PageHandler {
13
13
  protected lastUnloadPage: PageInstance | null;
14
14
  protected navigationBarHandler: NavigationBarHandler;
15
15
  homePage: string;
16
+ originHomePage: string;
16
17
  constructor(config: SpaRouterConfig, history: History);
17
18
  get currentPage(): string;
18
19
  get appId(): string;
19
- get router(): import("../types/router").Router;
20
+ get router(): import("../../types/router").Router;
20
21
  get routerMode(): "hash" | "browser" | "multi";
21
22
  get customRoutes(): Record<string, string | string[]>;
22
23
  get routes(): Route[];
@@ -27,6 +28,7 @@ declare class PageHandler {
27
28
  get animationDuration(): number;
28
29
  set pathname(p: string);
29
30
  get pathname(): string;
31
+ get originPathname(): string;
30
32
  get basename(): string;
31
33
  get pageConfig(): Route | undefined;
32
34
  isTabBar(pathname: string): boolean;
@@ -42,11 +44,10 @@ declare class PageHandler {
42
44
  load(page: PageInstance, pageConfig: Route | undefined, stampId: string, pageNo?: number): void;
43
45
  unload(page?: PageInstance | null, delta?: number, top?: boolean): void;
44
46
  show(page?: PageInstance | null, pageConfig?: Route, pageNo?: number): void;
45
- hide(page?: PageInstance | null): void;
47
+ hide(page?: PageInstance | null, animation?: boolean): void;
46
48
  addAnimation(pageEl?: HTMLElement | null, first?: boolean): void;
47
49
  getPageContainer(page?: PageInstance | null): HTMLElement | null;
48
- getScrollingElement(page?: PageInstance | null): HTMLElement | (Window & typeof globalThis);
50
+ getScrollingElement(page?: PageInstance | null): (Window & typeof globalThis) | HTMLElement;
49
51
  bindPageEvents(page: PageInstance, config?: Partial<PageConfig>): void;
50
52
  triggerRouterChange(): void;
51
53
  }
52
- export { PageHandler as default };
@@ -15,6 +15,7 @@ class PageHandler {
15
15
  this.defaultAnimation = { duration: 300, delay: 50 };
16
16
  this.config = config;
17
17
  this.homePage = getHomePage(this.routes[0].path, this.basename, this.customRoutes, this.config.entryPagePath);
18
+ this.originHomePage = this.config.entryPagePath || this.routes[0].path || this.basename;
18
19
  this.mount();
19
20
  this.navigationBarHandler = new NavigationBarHandler(this);
20
21
  }
@@ -48,14 +49,14 @@ class PageHandler {
48
49
  }
49
50
  set pathname(p) { this.router.pathname = p; }
50
51
  get pathname() { return this.router.pathname; }
52
+ // Note: 把 pathname 转换为原始路径,主要是处理 customRoutes 和 basename
53
+ get originPathname() { return routesAlias.getOrigin(addLeadingSlash(stripBasename(this.pathname, this.basename))); }
51
54
  get basename() { return this.router.basename || ''; }
52
55
  get pageConfig() {
53
- const routePath = addLeadingSlash(stripBasename(this.pathname, this.basename));
54
56
  const homePage = addLeadingSlash(this.homePage);
55
57
  return this.routes.find(r => {
56
- var _a;
57
58
  const pagePath = addLeadingSlash(r.path);
58
- return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
59
+ return [pagePath, homePage].includes(this.originPathname);
59
60
  });
60
61
  }
61
62
  isTabBar(pathname) {
@@ -245,24 +246,35 @@ class PageHandler {
245
246
  });
246
247
  }
247
248
  }
248
- hide(page) {
249
- var _a;
249
+ hide(page, animation = false) {
250
+ var _a, _b, _c, _d, _e, _f, _g;
250
251
  if (!page)
251
252
  return;
252
253
  // NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
253
254
  const pageEl = this.getPageContainer(page);
254
255
  if (pageEl) {
255
- if (this.hideTimer) {
256
- clearTimeout(this.hideTimer);
257
- this.hideTimer = null;
258
- pageEl.classList.add('taro_page_shade');
256
+ if (animation) {
257
+ if (this.hideTimer) {
258
+ clearTimeout(this.hideTimer);
259
+ this.hideTimer = null;
260
+ (_c = (_b = (_a = this.lastHidePage) === null || _a === void 0 ? void 0 : _a.classList) === null || _b === void 0 ? void 0 : _b.add) === null || _c === void 0 ? void 0 : _c.call(_b, 'taro_page_shade');
261
+ }
262
+ this.lastHidePage = pageEl;
263
+ this.hideTimer = setTimeout(() => {
264
+ this.hideTimer = null;
265
+ pageEl.classList.add('taro_page_shade');
266
+ }, this.animationDuration + this.animationDelay);
267
+ (_d = page.onHide) === null || _d === void 0 ? void 0 : _d.call(page);
259
268
  }
260
- this.lastHidePage = pageEl;
261
- this.hideTimer = setTimeout(() => {
262
- this.hideTimer = null;
269
+ else {
270
+ if (this.hideTimer) {
271
+ clearTimeout(this.hideTimer);
272
+ this.hideTimer = null;
273
+ (_g = (_f = (_e = this.lastHidePage) === null || _e === void 0 ? void 0 : _e.classList) === null || _f === void 0 ? void 0 : _f.add) === null || _g === void 0 ? void 0 : _g.call(_f, 'taro_page_shade');
274
+ }
263
275
  pageEl.classList.add('taro_page_shade');
264
- }, this.animationDuration + this.animationDelay);
265
- (_a = page.onHide) === null || _a === void 0 ? void 0 : _a.call(page);
276
+ this.lastHidePage = pageEl;
277
+ }
266
278
  }
267
279
  else {
268
280
  setTimeout(() => this.hide(page), 0);
@@ -1,5 +1,4 @@
1
- import { AppInstance } from '@tarojs/runtime';
2
- import { History } from "../history.js";
3
- import { SpaRouterConfig } from '../../types/router';
4
- declare function createRouter(history: History, app: AppInstance, config: SpaRouterConfig, framework?: string): () => void;
5
- export { createRouter };
1
+ import type { AppInstance } from '@tarojs/runtime';
2
+ import type { History } from 'history';
3
+ import type { SpaRouterConfig } from '../../types/router';
4
+ export declare function createRouter(history: History, app: AppInstance, config: SpaRouterConfig, framework?: string): () => void;