@tarojs/router 4.0.0-beta.87 → 4.0.0-beta.89

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,8 +14,7 @@ import UniversalRouter from 'universal-router';
14
14
  function loadAnimateStyle(ms = 300) {
15
15
  const css = `
16
16
  body {
17
- /* 防止 iOS 页面滚动 */
18
- overflow: hidden;
17
+ overflow: hidden; // 防止 iOS 页面滚动
19
18
  }
20
19
  .taro_router > .taro_page {
21
20
  position: absolute;
@@ -72,9 +71,8 @@ ${enableTabBar ? `
72
71
  }
73
72
 
74
73
  ` : ''}
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) {
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) {
78
76
  display: none;
79
77
  }
80
78
  `;
@@ -142,7 +140,7 @@ function loadNavigationBarStyle() {
142
140
  to {
143
141
  transform: rotate(360deg);
144
142
  }
145
- }
143
+ }
146
144
 
147
145
  .taro-navigation-bar-no-icon > .taro-navigation-bar-home {
148
146
  display: none;
@@ -264,9 +262,8 @@ class RouterConfig {
264
262
  return this.router.mode || 'hash';
265
263
  }
266
264
  static get customRoutes() { return this.router.customRoutes || {}; }
267
- // 这个方法不考虑 basename 和 customRoutes,只判断原始的 url 是否在 pages 中
268
265
  static isPage(url = '') {
269
- return this.pages.findIndex(e => addLeadingSlash(e) === url) !== -1;
266
+ return this.pages.findIndex(e => prependBasename(e) === url) !== -1;
270
267
  }
271
268
  }
272
269
 
@@ -535,14 +532,13 @@ function processNavigateUrl(option) {
535
532
  const parts = routesAlias.getOrigin(history.location.pathname).split('/');
536
533
  parts.pop();
537
534
  pathPieces.pathname.split('/').forEach((item) => {
538
- if (item === '.')
535
+ if (item === '.') {
539
536
  return;
537
+ }
540
538
  item === '..' ? parts.pop() : parts.push(item);
541
539
  });
542
540
  pathPieces.pathname = parts.join('/');
543
541
  }
544
- // 确保是 / 开头的路径
545
- pathPieces.pathname = addLeadingSlash(pathPieces.pathname);
546
542
  // 处理自定义路由
547
543
  pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname));
548
544
  // 处理 basename
@@ -572,8 +568,21 @@ function navigate(option, method) {
572
568
  if ('url' in option) {
573
569
  const pathPieces = processNavigateUrl(option);
574
570
  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
+ }
575
585
  if (method === 'navigateTo') {
576
- // Note: 由于 spa 会针对弱网情况下,短时间内多次跳转同一个页面跳转加了锁,后续如果有用户反馈返回无效,那可能是这个问题
577
586
  history.push(pathPieces, state);
578
587
  }
579
588
  else if (method === 'redirectTo' || method === 'switchTab') {
@@ -843,8 +852,8 @@ const launchStampId$1 = createStampId$1();
843
852
  * - 不支持路由动画
844
853
  */
845
854
  function createMultiRouter(history, app, config, framework) {
855
+ var _a, _b, _c, _d, _e, _f;
846
856
  return __awaiter(this, void 0, void 0, function* () {
847
- var _a, _b, _c, _d, _e, _f;
848
857
  if (typeof app.onUnhandledRejection === 'function') {
849
858
  window.addEventListener('unhandledrejection', app.onUnhandledRejection);
850
859
  }
@@ -852,7 +861,7 @@ function createMultiRouter(history, app, config, framework) {
852
861
  RouterConfig.config = config;
853
862
  const handler = new MultiPageHandler(config, history);
854
863
  const launchParam = {
855
- path: config.pageName, // 多页面模式没新开一个页面相当于重启,所以直接使用当前页面路径
864
+ path: config.pageName,
856
865
  query: handler.getQuery(launchStampId$1),
857
866
  scene: 0,
858
867
  shareTicket: '',
@@ -931,7 +940,7 @@ class NavigationBarHandler {
931
940
  });
932
941
  }
933
942
  toHomeFn() {
934
- reLaunch({ url: this.pageContext.originHomePage });
943
+ reLaunch({ url: this.pageContext.homePage });
935
944
  }
936
945
  backFn() {
937
946
  navigateBack();
@@ -980,7 +989,7 @@ class NavigationBarHandler {
980
989
  this.setNavigationLoading();
981
990
  }
982
991
  setCacheValue() {
983
- const currentPage = this.pageContext.originPathname;
992
+ const currentPage = this.pageContext.currentPage;
984
993
  if (typeof this.cache[currentPage] !== 'object') {
985
994
  this.cache[currentPage] = {};
986
995
  }
@@ -1011,7 +1020,7 @@ class NavigationBarHandler {
1011
1020
  var _a;
1012
1021
  if (!this.navigationBarElement)
1013
1022
  return;
1014
- const currentPage = this.pageContext.originPathname;
1023
+ const currentPage = this.pageContext.currentPage;
1015
1024
  let isShow;
1016
1025
  if (typeof show === 'boolean') {
1017
1026
  isShow = show;
@@ -1036,7 +1045,7 @@ class NavigationBarHandler {
1036
1045
  var _a, _b, _c;
1037
1046
  if (!this.navigationBarElement)
1038
1047
  return;
1039
- const currentPage = this.pageContext.originPathname;
1048
+ const currentPage = this.pageContext.currentPage;
1040
1049
  let color;
1041
1050
  if (typeof backgroundColor === 'string') {
1042
1051
  color = backgroundColor;
@@ -1060,7 +1069,7 @@ class NavigationBarHandler {
1060
1069
  var _a, _b, _c;
1061
1070
  if (!this.navigationBarElement)
1062
1071
  return;
1063
- const currentPage = this.pageContext.originPathname;
1072
+ const currentPage = this.pageContext.currentPage;
1064
1073
  let color;
1065
1074
  if (typeof fontColor === 'string') {
1066
1075
  color = fontColor;
@@ -1082,7 +1091,7 @@ class NavigationBarHandler {
1082
1091
  }
1083
1092
  setTitle(title) {
1084
1093
  var _a, _b, _c;
1085
- const currentPage = this.pageContext.originPathname;
1094
+ const currentPage = this.pageContext.currentPage;
1086
1095
  let proceedTitle;
1087
1096
  if (typeof title === 'string') {
1088
1097
  proceedTitle = title;
@@ -1163,7 +1172,6 @@ class PageHandler {
1163
1172
  this.defaultAnimation = { duration: 300, delay: 50 };
1164
1173
  this.config = config;
1165
1174
  this.homePage = getHomePage(this.routes[0].path, this.basename, this.customRoutes, this.config.entryPagePath);
1166
- this.originHomePage = this.config.entryPagePath || this.routes[0].path || this.basename;
1167
1175
  this.mount();
1168
1176
  this.navigationBarHandler = new NavigationBarHandler(this);
1169
1177
  }
@@ -1197,14 +1205,14 @@ class PageHandler {
1197
1205
  }
1198
1206
  set pathname(p) { this.router.pathname = p; }
1199
1207
  get pathname() { return this.router.pathname; }
1200
- // Note: 把 pathname 转换为原始路径,主要是处理 customRoutes 和 basename
1201
- get originPathname() { return routesAlias.getOrigin(addLeadingSlash(stripBasename(this.pathname, this.basename))); }
1202
1208
  get basename() { return this.router.basename || ''; }
1203
1209
  get pageConfig() {
1210
+ const routePath = addLeadingSlash(stripBasename(this.pathname, this.basename));
1204
1211
  const homePage = addLeadingSlash(this.homePage);
1205
1212
  return this.routes.find(r => {
1213
+ var _a;
1206
1214
  const pagePath = addLeadingSlash(r.path);
1207
- return [pagePath, homePage].includes(this.originPathname);
1215
+ return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
1208
1216
  });
1209
1217
  }
1210
1218
  isTabBar(pathname) {
@@ -1394,35 +1402,24 @@ class PageHandler {
1394
1402
  });
1395
1403
  }
1396
1404
  }
1397
- hide(page, animation = false) {
1398
- var _a, _b, _c, _d, _e, _f, _g;
1405
+ hide(page) {
1406
+ var _a;
1399
1407
  if (!page)
1400
1408
  return;
1401
1409
  // NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
1402
1410
  const pageEl = this.getPageContainer(page);
1403
1411
  if (pageEl) {
1404
- if (animation) {
1405
- if (this.hideTimer) {
1406
- clearTimeout(this.hideTimer);
1407
- this.hideTimer = null;
1408
- (_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');
1409
- }
1410
- this.lastHidePage = pageEl;
1411
- this.hideTimer = setTimeout(() => {
1412
- this.hideTimer = null;
1413
- pageEl.classList.add('taro_page_shade');
1414
- }, this.animationDuration + this.animationDelay);
1415
- (_d = page.onHide) === null || _d === void 0 ? void 0 : _d.call(page);
1416
- }
1417
- else {
1418
- if (this.hideTimer) {
1419
- clearTimeout(this.hideTimer);
1420
- this.hideTimer = null;
1421
- (_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');
1422
- }
1412
+ if (this.hideTimer) {
1413
+ clearTimeout(this.hideTimer);
1414
+ this.hideTimer = null;
1423
1415
  pageEl.classList.add('taro_page_shade');
1424
- this.lastHidePage = pageEl;
1425
1416
  }
1417
+ this.lastHidePage = pageEl;
1418
+ this.hideTimer = setTimeout(() => {
1419
+ this.hideTimer = null;
1420
+ pageEl.classList.add('taro_page_shade');
1421
+ }, this.animationDuration + this.animationDelay);
1422
+ (_a = page.onHide) === null || _a === void 0 ? void 0 : _a.call(page);
1426
1423
  }
1427
1424
  else {
1428
1425
  setTimeout(() => this.hide(page), 0);
@@ -1493,10 +1490,6 @@ function createRouter(history, app, config, framework) {
1493
1490
  }
1494
1491
  RouterConfig.config = config;
1495
1492
  const handler = new PageHandler(config, history);
1496
- // Note: 弱网情况下,快速切换 tab,会造成同个页面实例被多次挂在到页面上,原因是资源请求是异步的,短时间内发起多个请求,
1497
- // 会在资源加载完成后,同时走到挂载的逻辑,造成 pageStampId 更新不及时,两个 page 的Id 相同,后面很多操作是通过 getElementById 来进行的
1498
- // 所以需要加一个锁来应对这个情况
1499
- const pageLock = {};
1500
1493
  routesAlias.set(handler.router.customRoutes);
1501
1494
  const basename = handler.router.basename;
1502
1495
  const routes = handler.routes.map(route => {
@@ -1518,35 +1511,29 @@ function createRouter(history, app, config, framework) {
1518
1511
  eventCenter.trigger('__taroRouterLaunch', launchParam);
1519
1512
  (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
1520
1513
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
1521
- const render = (_c) => __awaiter(this, [_c], void 0, function* ({ location, action }) {
1522
- var _d, _e, _f, _g, _h, _j, _k, _l;
1523
- // Note: 由于下面有异步加载操作 先不要在这里去设置 handler.pathname
1524
- const currentPathname = decodeURI(location.pathname);
1525
- if ((_d = window.__taroAppConfig) === null || _d === void 0 ? void 0 : _d.usingWindowScroll)
1514
+ const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
1515
+ var _c, _d, _e, _f, _g, _h, _j, _k;
1516
+ handler.pathname = decodeURI(location.pathname);
1517
+ if ((_c = window.__taroAppConfig) === null || _c === void 0 ? void 0 : _c.usingWindowScroll)
1526
1518
  window.scrollTo(0, 0);
1527
1519
  eventCenter.trigger('__taroRouterChange', {
1528
1520
  toLocation: {
1529
- path: currentPathname
1521
+ path: handler.pathname
1530
1522
  }
1531
1523
  });
1532
- let element, context, params;
1533
- const routerPath = handler.router.forcePath || currentPathname;
1534
- pageLock[routerPath] = typeof pageLock[routerPath] === 'number' ? pageLock[routerPath] + 1 : 1;
1535
- const currentLock = pageLock[routerPath];
1536
- let postLock;
1524
+ let element, params;
1537
1525
  try {
1538
- const result = yield router.resolve(routerPath);
1539
- [element, context, params] = yield Promise.all(result);
1540
- postLock = pageLock[routerPath];
1526
+ const result = yield router.resolve(handler.router.forcePath || handler.pathname);
1527
+ [element, , params] = yield Promise.all(result);
1541
1528
  }
1542
1529
  catch (error) {
1543
1530
  if (error.status === 404) {
1544
1531
  const notFoundEvent = {
1545
1532
  isEntryPage: stacks.length === 0,
1546
- path: currentPathname,
1533
+ path: handler.pathname,
1547
1534
  query: handler.getQuery(createStampId()),
1548
1535
  };
1549
- (_e = app.onPageNotFound) === null || _e === void 0 ? void 0 : _e.call(app, notFoundEvent);
1536
+ (_d = app.onPageNotFound) === null || _d === void 0 ? void 0 : _d.call(app, notFoundEvent);
1550
1537
  eventCenter.trigger('__taroRouterNotFound', notFoundEvent);
1551
1538
  }
1552
1539
  else if (/Loading hot update .* failed./.test(error.message)) {
@@ -1557,16 +1544,13 @@ function createRouter(history, app, config, framework) {
1557
1544
  throw error;
1558
1545
  }
1559
1546
  }
1560
- if (!element || currentLock !== postLock)
1547
+ if (!element)
1561
1548
  return;
1562
- // Note: 异步结束后,在设置 handler.pathname
1563
- // context.pathname universal-router 被处理过了,是发起资源请求的时候传入的 pathname,即 await router.resolve(routerPath) 这个 routerPath
1564
- handler.pathname = context.pathname;
1565
- const { pathname, pageConfig } = handler;
1566
- let enablePullDownRefresh = ((_f = config === null || config === void 0 ? void 0 : config.window) === null || _f === void 0 ? void 0 : _f.enablePullDownRefresh) || false;
1567
- let navigationStyle = ((_g = config === null || config === void 0 ? void 0 : config.window) === null || _g === void 0 ? void 0 : _g.navigationStyle) || 'default';
1568
- let navigationBarTextStyle = ((_h = config === null || config === void 0 ? void 0 : config.window) === null || _h === void 0 ? void 0 : _h.navigationBarTextStyle) || 'white';
1569
- let navigationBarBackgroundColor = ((_j = config === null || config === void 0 ? void 0 : config.window) === null || _j === void 0 ? void 0 : _j.navigationBarBackgroundColor) || '#000000';
1549
+ 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';
1570
1554
  if (pageConfig) {
1571
1555
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
1572
1556
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
@@ -1583,7 +1567,8 @@ function createRouter(history, app, config, framework) {
1583
1567
  }
1584
1568
  eventCenter.trigger('__taroSetNavigationStyle', navigationStyle, navigationBarTextStyle, navigationBarBackgroundColor);
1585
1569
  const currentPage = Current.page;
1586
- const methodName = (_k = stacks.method) !== null && _k !== void 0 ? _k : '';
1570
+ const pathname = handler.pathname;
1571
+ const methodName = (_j = stacks.method) !== null && _j !== void 0 ? _j : '';
1587
1572
  const cacheTabs = stacks.getTabs();
1588
1573
  let shouldLoad = false;
1589
1574
  stacks.method = '';
@@ -1598,11 +1583,10 @@ function createRouter(history, app, config, framework) {
1598
1583
  }
1599
1584
  shouldLoad = true;
1600
1585
  }
1601
- else if (currentPage && handler.isTabBar(pathname)) {
1586
+ else if (currentPage && handler.isTabBar(handler.pathname)) {
1602
1587
  if (handler.isSamePage(currentPage))
1603
1588
  return;
1604
1589
  if (handler.isTabBar(currentPage.path)) {
1605
- // NOTE: 从 tabBar 页面切换到 tabBar 页面
1606
1590
  handler.hide(currentPage);
1607
1591
  stacks.pushTab(currentPage.path.split('?')[0]);
1608
1592
  }
@@ -1616,8 +1600,8 @@ function createRouter(history, app, config, framework) {
1616
1600
  handler.unload(currentPage, stacks.length, true);
1617
1601
  }
1618
1602
  }
1619
- if (cacheTabs[pathname]) {
1620
- stacks.popTab(pathname);
1603
+ if (cacheTabs[handler.pathname]) {
1604
+ stacks.popTab(handler.pathname);
1621
1605
  return handler.show(stacks.getItem(0), pageConfig, 0);
1622
1606
  }
1623
1607
  shouldLoad = true;
@@ -1646,11 +1630,11 @@ function createRouter(history, app, config, framework) {
1646
1630
  shouldLoad = true;
1647
1631
  }
1648
1632
  else if (action === 'PUSH') {
1649
- handler.hide(currentPage, true);
1633
+ handler.hide(currentPage);
1650
1634
  shouldLoad = true;
1651
1635
  }
1652
1636
  if (shouldLoad || stacks.length < 1) {
1653
- const el = (_l = element.default) !== null && _l !== void 0 ? _l : element;
1637
+ const el = (_k = element.default) !== null && _k !== void 0 ? _k : element;
1654
1638
  const loadConfig = Object.assign({}, pageConfig);
1655
1639
  const stacksIndex = stacks.length;
1656
1640
  delete loadConfig['path'];
@@ -1,2 +1,3 @@
1
- import type { MpaRouterConfig, SpaRouterConfig } from '../types/router';
2
- export declare function initNavigationBar(config: SpaRouterConfig | MpaRouterConfig, container: HTMLElement): void;
1
+ import { MpaRouterConfig, SpaRouterConfig } from '../types/router';
2
+ declare function initNavigationBar(config: SpaRouterConfig | MpaRouterConfig, container: HTMLElement): void;
3
+ export { initNavigationBar };
@@ -1,11 +1,12 @@
1
- import type { MpaRouterConfig, SpaRouterConfig } from '../../types/router';
2
- export declare class RouterConfig {
1
+ import { MpaRouterConfig, SpaRouterConfig } from '../../types/router';
2
+ 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 { addLeadingSlash } from '@tarojs/runtime';
1
+ import { prependBasename } from '../history.js';
2
2
 
3
3
  class RouterConfig {
4
4
  static set config(e) {
@@ -17,9 +17,8 @@ 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 中
21
20
  static isPage(url = '') {
22
- return this.pages.findIndex(e => addLeadingSlash(e) === url) !== -1;
21
+ return this.pages.findIndex(e => prependBasename(e) === url) !== -1;
23
22
  }
24
23
  }
25
24
 
@@ -1,6 +1,6 @@
1
- import type { AppInstance } from '@tarojs/runtime';
2
- import type { History } from 'history';
3
- import type { MpaRouterConfig } from '../../types/router';
1
+ import { AppInstance } from '@tarojs/runtime';
2
+ import { History } from "../history.js";
3
+ import { MpaRouterConfig } from '../../types/router';
4
4
  /** Note: 关于多页面应用
5
5
  * - 需要配置路由映射(根目录跳转、404 页面……)
6
6
  * - app.onPageNotFound 事件不支持
@@ -8,4 +8,5 @@ import type { MpaRouterConfig } from '../../types/router';
8
8
  * - TabBar 会多次加载
9
9
  * - 不支持路由动画
10
10
  */
11
- export declare function createMultiRouter(history: History, app: AppInstance, config: MpaRouterConfig, framework?: string): Promise<void>;
11
+ declare function createMultiRouter(history: History, app: AppInstance, config: MpaRouterConfig, framework?: string): Promise<void>;
12
+ export { createMultiRouter };
@@ -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;
19
20
  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 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 {
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 {
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,7 +27,8 @@ export default 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): (Window & typeof globalThis) | HTMLElement;
30
+ getScrollingElement(page?: PageInstance | null): HTMLElement | (Window & typeof globalThis);
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 type PageHandler from './page';
1
+ import PageHandler from "./page.js";
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
- export default class NavigationBarHandler {
9
+ declare class NavigationBarHandler {
10
10
  pageContext: PageHandler;
11
11
  navigationBarElement: HTMLElement;
12
12
  cache: Record<string, NavigationBarCache>;
@@ -33,4 +33,4 @@ export default class NavigationBarHandler {
33
33
  fnBtnToggleToNone(): void;
34
34
  setNavigationBarVisible(show?: any): void;
35
35
  }
36
- export {};
36
+ export { NavigationBarHandler as default };
@@ -24,7 +24,7 @@ class NavigationBarHandler {
24
24
  });
25
25
  }
26
26
  toHomeFn() {
27
- reLaunch({ url: this.pageContext.originHomePage });
27
+ reLaunch({ url: this.pageContext.homePage });
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.originPathname;
76
+ const currentPage = this.pageContext.currentPage;
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.originPathname;
107
+ const currentPage = this.pageContext.currentPage;
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.originPathname;
132
+ const currentPage = this.pageContext.currentPage;
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.originPathname;
156
+ const currentPage = this.pageContext.currentPage;
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.originPathname;
178
+ const currentPage = this.pageContext.currentPage;
179
179
  let proceedTitle;
180
180
  if (typeof title === 'string') {
181
181
  proceedTitle = title;
@@ -1,9 +1,9 @@
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 {
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 {
7
7
  history: History;
8
8
  config: SpaRouterConfig;
9
9
  protected readonly defaultAnimation: RouterAnimate;
@@ -13,11 +13,10 @@ export default class PageHandler {
13
13
  protected lastUnloadPage: PageInstance | null;
14
14
  protected navigationBarHandler: NavigationBarHandler;
15
15
  homePage: string;
16
- originHomePage: string;
17
16
  constructor(config: SpaRouterConfig, history: History);
18
17
  get currentPage(): string;
19
18
  get appId(): string;
20
- get router(): import("../../types/router").Router;
19
+ get router(): import("../types/router").Router;
21
20
  get routerMode(): "hash" | "browser" | "multi";
22
21
  get customRoutes(): Record<string, string | string[]>;
23
22
  get routes(): Route[];
@@ -28,7 +27,6 @@ export default class PageHandler {
28
27
  get animationDuration(): number;
29
28
  set pathname(p: string);
30
29
  get pathname(): string;
31
- get originPathname(): string;
32
30
  get basename(): string;
33
31
  get pageConfig(): Route | undefined;
34
32
  isTabBar(pathname: string): boolean;
@@ -44,10 +42,11 @@ export default class PageHandler {
44
42
  load(page: PageInstance, pageConfig: Route | undefined, stampId: string, pageNo?: number): void;
45
43
  unload(page?: PageInstance | null, delta?: number, top?: boolean): void;
46
44
  show(page?: PageInstance | null, pageConfig?: Route, pageNo?: number): void;
47
- hide(page?: PageInstance | null, animation?: boolean): void;
45
+ hide(page?: PageInstance | null): void;
48
46
  addAnimation(pageEl?: HTMLElement | null, first?: boolean): void;
49
47
  getPageContainer(page?: PageInstance | null): HTMLElement | null;
50
- getScrollingElement(page?: PageInstance | null): (Window & typeof globalThis) | HTMLElement;
48
+ getScrollingElement(page?: PageInstance | null): HTMLElement | (Window & typeof globalThis);
51
49
  bindPageEvents(page: PageInstance, config?: Partial<PageConfig>): void;
52
50
  triggerRouterChange(): void;
53
51
  }
52
+ export { PageHandler as default };
@@ -15,7 +15,6 @@ 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;
19
18
  this.mount();
20
19
  this.navigationBarHandler = new NavigationBarHandler(this);
21
20
  }
@@ -49,14 +48,14 @@ class PageHandler {
49
48
  }
50
49
  set pathname(p) { this.router.pathname = p; }
51
50
  get pathname() { return this.router.pathname; }
52
- // Note: 把 pathname 转换为原始路径,主要是处理 customRoutes 和 basename
53
- get originPathname() { return routesAlias.getOrigin(addLeadingSlash(stripBasename(this.pathname, this.basename))); }
54
51
  get basename() { return this.router.basename || ''; }
55
52
  get pageConfig() {
53
+ const routePath = addLeadingSlash(stripBasename(this.pathname, this.basename));
56
54
  const homePage = addLeadingSlash(this.homePage);
57
55
  return this.routes.find(r => {
56
+ var _a;
58
57
  const pagePath = addLeadingSlash(r.path);
59
- return [pagePath, homePage].includes(this.originPathname);
58
+ return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
60
59
  });
61
60
  }
62
61
  isTabBar(pathname) {
@@ -246,35 +245,24 @@ class PageHandler {
246
245
  });
247
246
  }
248
247
  }
249
- hide(page, animation = false) {
250
- var _a, _b, _c, _d, _e, _f, _g;
248
+ hide(page) {
249
+ var _a;
251
250
  if (!page)
252
251
  return;
253
252
  // NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
254
253
  const pageEl = this.getPageContainer(page);
255
254
  if (pageEl) {
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);
268
- }
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
- }
255
+ if (this.hideTimer) {
256
+ clearTimeout(this.hideTimer);
257
+ this.hideTimer = null;
275
258
  pageEl.classList.add('taro_page_shade');
276
- this.lastHidePage = pageEl;
277
259
  }
260
+ this.lastHidePage = pageEl;
261
+ this.hideTimer = setTimeout(() => {
262
+ this.hideTimer = null;
263
+ pageEl.classList.add('taro_page_shade');
264
+ }, this.animationDuration + this.animationDelay);
265
+ (_a = page.onHide) === null || _a === void 0 ? void 0 : _a.call(page);
278
266
  }
279
267
  else {
280
268
  setTimeout(() => this.hide(page), 0);