@tarojs/router 3.6.7 → 3.6.8

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
@@ -1,5 +1,5 @@
1
1
  import { createBrowserHistory, createHashHistory, Action, parsePath } from 'history';
2
- import { Current, incrementId, eventCenter, createPageConfig, hooks, stringify, requestAnimationFrame } from '@tarojs/runtime';
2
+ import { Current, incrementId, eventCenter, createPageConfig, hooks, stringify, requestAnimationFrame as requestAnimationFrame$1 } from '@tarojs/runtime';
3
3
  import MobileDetect from 'mobile-detect';
4
4
  import queryString from 'query-string';
5
5
  import { defineCustomElementTaroTabbar } from '@tarojs/components/dist/components';
@@ -430,11 +430,11 @@ function bindPageResize(page) {
430
430
 
431
431
  const pageScrollFn = {};
432
432
  let pageDOM = window;
433
- function bindPageScroll(page, pageEl, distance = 50) {
433
+ function bindPageScroll(page, scrollEl, distance = 50) {
434
434
  var _a;
435
435
  const pagePath = (page ? page === null || page === void 0 ? void 0 : page.path : (_a = Current.router) === null || _a === void 0 ? void 0 : _a.path);
436
- pageScrollFn[pagePath] && pageEl.removeEventListener('scroll', pageScrollFn[pagePath]);
437
- pageDOM = pageEl;
436
+ pageScrollFn[pagePath] && scrollEl.removeEventListener('scroll', pageScrollFn[pagePath]);
437
+ pageDOM = scrollEl;
438
438
  let isReachBottom = false;
439
439
  pageScrollFn[pagePath] = function () {
440
440
  var _a;
@@ -462,6 +462,78 @@ function getOffset() {
462
462
  }
463
463
  }
464
464
 
465
+ /**
466
+ * 插入页面动画需要的样式
467
+ */
468
+ function loadAnimateStyle(ms = 300) {
469
+ const css = `
470
+ .taro_router .taro_page {
471
+ position: absolute;
472
+ left: 0;
473
+ top: 0;
474
+ width: 100%;
475
+ height: 100%;
476
+ background-color: #fff;
477
+ transform: translate(100%, 0);
478
+ transition: transform ${ms}ms;
479
+ z-index: 0;
480
+ }
481
+
482
+ .taro_router .taro_page.taro_tabbar_page,
483
+ .taro_router .taro_page.taro_page_show.taro_page_stationed {
484
+ transform: none;
485
+ }
486
+
487
+ .taro_router .taro_page.taro_page_show {
488
+ transform: translate(0, 0);
489
+ }`;
490
+ addStyle(css);
491
+ }
492
+ /**
493
+ * 插入路由相关样式
494
+ */
495
+ function loadRouterStyle(usingWindowScroll) {
496
+ const css = `
497
+ .taro_router {
498
+ overflow: hidden;
499
+ position: relative;
500
+ width: 100%;
501
+ height: 100%;
502
+ min-height: 100vh;
503
+ }
504
+
505
+ .taro-tabbar__container .taro_router {
506
+ min-height: calc(100vh - 50px);
507
+ }
508
+
509
+ .taro_page {
510
+ width: 100%;
511
+ height: 100%;
512
+ ${usingWindowScroll ? '' : `
513
+ overflow-x: hidden;
514
+ overflow-y: scroll;
515
+ max-height: 100vh;
516
+ `}
517
+ }
518
+
519
+ .taro-tabbar__container .taro-tabbar__panel {
520
+ overflow: hidden;
521
+ }
522
+
523
+ .taro-tabbar__container .taro_page.taro_tabbar_page {
524
+ max-height: calc(100vh - 50px);
525
+ }
526
+ `;
527
+ addStyle(css);
528
+ }
529
+ function addStyle(css) {
530
+ if (!css)
531
+ return;
532
+ const style = document.createElement('style');
533
+ style.innerHTML = css;
534
+ document.getElementsByTagName('head')[0].appendChild(style);
535
+ }
536
+
465
537
  // @ts-nocheck
466
538
  function initTabbar(config) {
467
539
  if (config.tabBar == null) {
@@ -522,6 +594,17 @@ class MultiPageHandler {
522
594
  return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
523
595
  }
524
596
  get search() { return location.search.substr(1); }
597
+ get usingWindowScroll() {
598
+ var _a;
599
+ let usingWindowScroll = true;
600
+ if (typeof ((_a = this.pageConfig) === null || _a === void 0 ? void 0 : _a.usingWindowScroll) === 'boolean') {
601
+ usingWindowScroll = this.pageConfig.usingWindowScroll;
602
+ }
603
+ const win = window;
604
+ win.__taroAppConfig || (win.__taroAppConfig = {});
605
+ win.__taroAppConfig.usingWindowScroll = usingWindowScroll;
606
+ return usingWindowScroll;
607
+ }
525
608
  getQuery(search = '', options = {}) {
526
609
  search = search ? `${search}&${this.search}` : this.search;
527
610
  const query = search
@@ -531,6 +614,7 @@ class MultiPageHandler {
531
614
  }
532
615
  mount() {
533
616
  setHistoryMode(this.routerMode, this.router.basename);
617
+ loadRouterStyle(this.usingWindowScroll);
534
618
  const appId = this.appId;
535
619
  let app = document.getElementById(appId);
536
620
  let isPosition = true;
@@ -567,7 +651,20 @@ class MultiPageHandler {
567
651
  const pageEl = this.getPageContainer(page);
568
652
  if (pageEl && !(pageEl === null || pageEl === void 0 ? void 0 : pageEl['__isReady'])) {
569
653
  const el = pageEl.firstElementChild;
570
- (_a = el === null || el === void 0 ? void 0 : el['componentOnReady']) === null || _a === void 0 ? void 0 : _a.call(el);
654
+ const componentOnReady = el === null || el === void 0 ? void 0 : el['componentOnReady'];
655
+ if (componentOnReady) {
656
+ componentOnReady === null || componentOnReady === void 0 ? void 0 : componentOnReady().then(() => {
657
+ requestAnimationFrame(() => {
658
+ var _a;
659
+ (_a = page.onReady) === null || _a === void 0 ? void 0 : _a.call(page);
660
+ pageEl['__isReady'] = true;
661
+ });
662
+ });
663
+ }
664
+ else {
665
+ (_a = page.onReady) === null || _a === void 0 ? void 0 : _a.call(page);
666
+ pageEl['__isReady'] = true;
667
+ }
571
668
  onLoad && (pageEl['__page'] = page);
572
669
  }
573
670
  }
@@ -577,11 +674,13 @@ class MultiPageHandler {
577
674
  return;
578
675
  (_a = page.onLoad) === null || _a === void 0 ? void 0 : _a.call(page, this.getQuery('', page.options), () => {
579
676
  var _a;
580
- const pageEl = this.getPageContainer(page);
581
- this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
677
+ if (this.isTabBar) {
678
+ const pageEl = this.getPageContainer(page);
679
+ pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page');
680
+ }
582
681
  this.onReady(page, true);
583
682
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
584
- this.bindPageEvents(page, pageEl, pageConfig);
683
+ this.bindPageEvents(page, pageConfig);
585
684
  });
586
685
  }
587
686
  getPageContainer(page) {
@@ -595,15 +694,18 @@ class MultiPageHandler {
595
694
  ? document.querySelector(`.taro_page#${id}`)
596
695
  : document.querySelector('.taro_page') ||
597
696
  document.querySelector('.taro_router'));
598
- return el || window;
697
+ return el;
599
698
  }
600
- bindPageEvents(page, pageEl, config = {}) {
699
+ getScrollingElement(page) {
700
+ if (this.usingWindowScroll)
701
+ return window;
702
+ return this.getPageContainer(page) || window;
703
+ }
704
+ bindPageEvents(page, config = {}) {
601
705
  var _a;
602
- if (!pageEl) {
603
- pageEl = this.getPageContainer();
604
- }
706
+ const scrollEl = this.getScrollingElement(page);
605
707
  const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
606
- bindPageScroll(page, pageEl, distance);
708
+ bindPageScroll(page, scrollEl, distance);
607
709
  bindPageResize(page);
608
710
  }
609
711
  }
@@ -672,36 +774,6 @@ function createMultiRouter(app, config, framework) {
672
774
  });
673
775
  }
674
776
 
675
- /**
676
- * 插入页面动画需要的样式
677
- */
678
- function loadAnimateStyle(ms = 300) {
679
- const css = `
680
- .taro_router .taro_page {
681
- position: absolute;
682
- left: 0;
683
- top: 0;
684
- width: 100%;
685
- height: 100%;
686
- background-color: #fff;
687
- transform: translate(100%, 0);
688
- transition: transform ${ms}ms;
689
- z-index: 0;
690
- }
691
-
692
- .taro_router .taro_page.taro_tabbar_page,
693
- .taro_router .taro_page.taro_page_show.taro_page_stationed {
694
- transform: none;
695
- }
696
-
697
- .taro_router .taro_page.taro_page_show {
698
- transform: translate(0, 0);
699
- }`;
700
- const style = document.createElement('style');
701
- style.innerHTML = css;
702
- document.getElementsByTagName('head')[0].appendChild(style);
703
- }
704
-
705
777
  /* eslint-disable dot-notation */
706
778
  function setDisplay(el, type = '') {
707
779
  if (el) {
@@ -787,6 +859,17 @@ class PageHandler {
787
859
  }
788
860
  return search.substring(1);
789
861
  }
862
+ get usingWindowScroll() {
863
+ var _a;
864
+ let usingWindowScroll = false;
865
+ if (typeof ((_a = this.pageConfig) === null || _a === void 0 ? void 0 : _a.usingWindowScroll) === 'boolean') {
866
+ usingWindowScroll = this.pageConfig.usingWindowScroll;
867
+ }
868
+ const win = window;
869
+ win.__taroAppConfig || (win.__taroAppConfig = {});
870
+ win.__taroAppConfig.usingWindowScroll = usingWindowScroll;
871
+ return usingWindowScroll;
872
+ }
790
873
  getQuery(stamp = '', search = '', options = {}) {
791
874
  search = search ? `${search}&${this.search}` : this.search;
792
875
  const query = search
@@ -797,7 +880,9 @@ class PageHandler {
797
880
  }
798
881
  mount() {
799
882
  setHistoryMode(this.routerMode, this.router.basename);
883
+ this.pathname = history.location.pathname;
800
884
  this.animation && loadAnimateStyle(this.animationDuration);
885
+ loadRouterStyle(this.usingWindowScroll);
801
886
  const appId = this.appId;
802
887
  let app = document.getElementById(appId);
803
888
  let isPosition = true;
@@ -837,7 +922,7 @@ class PageHandler {
837
922
  const componentOnReady = el === null || el === void 0 ? void 0 : el['componentOnReady'];
838
923
  if (componentOnReady) {
839
924
  componentOnReady === null || componentOnReady === void 0 ? void 0 : componentOnReady().then(() => {
840
- requestAnimationFrame(() => {
925
+ requestAnimationFrame$1(() => {
841
926
  var _a;
842
927
  (_a = page.onReady) === null || _a === void 0 ? void 0 : _a.call(page);
843
928
  pageEl['__isReady'] = true;
@@ -864,7 +949,8 @@ class PageHandler {
864
949
  this.isTabBar(this.pathname) && pageEl.classList.add('taro_tabbar_page');
865
950
  this.addAnimation(pageEl, pageNo === 0);
866
951
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
867
- this.bindPageEvents(page, pageEl, pageConfig);
952
+ this.bindPageEvents(page, pageConfig);
953
+ this.triggerRouterChange();
868
954
  }
869
955
  else {
870
956
  (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
@@ -874,7 +960,8 @@ class PageHandler {
874
960
  this.addAnimation(pageEl, pageNo === 0);
875
961
  this.onReady(page, true);
876
962
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
877
- this.bindPageEvents(page, pageEl, pageConfig);
963
+ this.bindPageEvents(page, pageConfig);
964
+ this.triggerRouterChange();
878
965
  });
879
966
  }
880
967
  }
@@ -922,7 +1009,8 @@ class PageHandler {
922
1009
  setDisplay(pageEl);
923
1010
  this.addAnimation(pageEl, pageNo === 0);
924
1011
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
925
- this.bindPageEvents(page, pageEl, pageConfig);
1012
+ this.bindPageEvents(page, pageConfig);
1013
+ this.triggerRouterChange();
926
1014
  }
927
1015
  else {
928
1016
  (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
@@ -931,7 +1019,8 @@ class PageHandler {
931
1019
  this.addAnimation(pageEl, pageNo === 0);
932
1020
  this.onReady(page, false);
933
1021
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
934
- this.bindPageEvents(page, pageEl, pageConfig);
1022
+ this.bindPageEvents(page, pageConfig);
1023
+ this.triggerRouterChange();
935
1024
  });
936
1025
  }
937
1026
  }
@@ -985,17 +1074,33 @@ class PageHandler {
985
1074
  ? document.querySelector(`.taro_page#${id}`)
986
1075
  : document.querySelector('.taro_page') ||
987
1076
  document.querySelector('.taro_router'));
988
- return el || window;
1077
+ return el;
989
1078
  }
990
- bindPageEvents(page, pageEl, config = {}) {
1079
+ getScrollingElement(page) {
1080
+ if (this.usingWindowScroll)
1081
+ return window;
1082
+ return this.getPageContainer(page) || window;
1083
+ }
1084
+ bindPageEvents(page, config = {}) {
991
1085
  var _a;
992
- if (!pageEl) {
993
- pageEl = this.getPageContainer();
994
- }
1086
+ const scrollEl = this.getScrollingElement(page);
995
1087
  const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
996
- bindPageScroll(page, pageEl, distance);
1088
+ bindPageScroll(page, scrollEl, distance);
997
1089
  bindPageResize(page);
998
1090
  }
1091
+ triggerRouterChange() {
1092
+ /**
1093
+ * @tarojs/runtime 中生命周期跑在 promise 中,所以这里需要 setTimeout 延迟事件调用
1094
+ * TODO 考虑将生命周期返回 Promise,用于处理相关事件调用顺序
1095
+ */
1096
+ setTimeout(() => {
1097
+ eventCenter.trigger('__afterTaroRouterChange', {
1098
+ toLocation: {
1099
+ path: this.pathname
1100
+ }
1101
+ });
1102
+ }, 0);
1103
+ }
999
1104
  }
1000
1105
 
1001
1106
  const createStampId = incrementId();
@@ -1029,8 +1134,10 @@ function createRouter(app, config, framework) {
1029
1134
  (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
1030
1135
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
1031
1136
  const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
1032
- var _c, _d, _e, _f, _g;
1137
+ var _c, _d, _e, _f, _g, _h;
1033
1138
  handler.pathname = decodeURI(location.pathname);
1139
+ if ((_c = window.__taroAppConfig) === null || _c === void 0 ? void 0 : _c.usingWindowScroll)
1140
+ window.scrollTo(0, 0);
1034
1141
  eventCenter.trigger('__taroRouterChange', {
1035
1142
  toLocation: {
1036
1143
  path: handler.pathname
@@ -1048,7 +1155,7 @@ function createRouter(app, config, framework) {
1048
1155
  path: handler.pathname,
1049
1156
  query: handler.getQuery(createStampId()),
1050
1157
  };
1051
- (_c = app.onPageNotFound) === null || _c === void 0 ? void 0 : _c.call(app, notFoundEvent);
1158
+ (_d = app.onPageNotFound) === null || _d === void 0 ? void 0 : _d.call(app, notFoundEvent);
1052
1159
  eventCenter.trigger('__taroRouterNotFound', notFoundEvent);
1053
1160
  }
1054
1161
  else if (/Loading hot update .* failed./.test(error.message)) {
@@ -1062,16 +1169,16 @@ function createRouter(app, config, framework) {
1062
1169
  if (!element)
1063
1170
  return;
1064
1171
  const pageConfig = handler.pageConfig;
1065
- let enablePullDownRefresh = ((_d = config === null || config === void 0 ? void 0 : config.window) === null || _d === void 0 ? void 0 : _d.enablePullDownRefresh) || false;
1172
+ let enablePullDownRefresh = ((_e = config === null || config === void 0 ? void 0 : config.window) === null || _e === void 0 ? void 0 : _e.enablePullDownRefresh) || false;
1066
1173
  if (pageConfig) {
1067
- setTitle((_e = pageConfig.navigationBarTitleText) !== null && _e !== void 0 ? _e : document.title);
1174
+ setTitle((_f = pageConfig.navigationBarTitleText) !== null && _f !== void 0 ? _f : document.title);
1068
1175
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
1069
1176
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
1070
1177
  }
1071
1178
  }
1072
1179
  const currentPage = Current.page;
1073
1180
  const pathname = handler.pathname;
1074
- const methodName = (_f = stacks.method) !== null && _f !== void 0 ? _f : '';
1181
+ const methodName = (_g = stacks.method) !== null && _g !== void 0 ? _g : '';
1075
1182
  const cacheTabs = stacks.getTabs();
1076
1183
  let shouldLoad = false;
1077
1184
  stacks.method = '';
@@ -1135,7 +1242,7 @@ function createRouter(app, config, framework) {
1135
1242
  shouldLoad = true;
1136
1243
  }
1137
1244
  if (shouldLoad || stacks.length < 1) {
1138
- const el = (_g = element.default) !== null && _g !== void 0 ? _g : element;
1245
+ const el = (_h = element.default) !== null && _h !== void 0 ? _h : element;
1139
1246
  const loadConfig = Object.assign({}, pageConfig);
1140
1247
  const stacksIndex = stacks.length;
1141
1248
  delete loadConfig['path'];
@@ -1153,11 +1260,6 @@ function createRouter(app, config, framework) {
1153
1260
  page.options = params;
1154
1261
  handler.load(page, pageConfig, pageStampId, stacksIndex);
1155
1262
  }
1156
- eventCenter.trigger('__afterTaroRouterChange', {
1157
- toLocation: {
1158
- path: handler.pathname
1159
- }
1160
- });
1161
1263
  });
1162
1264
  const routePath = addLeadingSlash(stripBasename(history.location.pathname, handler.basename));
1163
1265
  if (routePath === '/') {