@tarojs/router 3.6.7 → 3.6.9-alpha.1

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.
@@ -1,2 +1,2 @@
1
1
  import { PageInstance } from '@tarojs/runtime';
2
- export declare function bindPageScroll(page: PageInstance, pageEl: HTMLElement, distance?: number): void;
2
+ export declare function bindPageScroll(page: PageInstance, scrollEl: HTMLElement | Window, distance?: number): void;
@@ -1,11 +1,11 @@
1
1
  import { Current } from '@tarojs/runtime';
2
2
  const pageScrollFn = {};
3
3
  let pageDOM = window;
4
- export function bindPageScroll(page, pageEl, distance = 50) {
4
+ export function bindPageScroll(page, scrollEl, distance = 50) {
5
5
  var _a;
6
6
  const pagePath = (page ? page === null || page === void 0 ? void 0 : page.path : (_a = Current.router) === null || _a === void 0 ? void 0 : _a.path);
7
- pageScrollFn[pagePath] && pageEl.removeEventListener('scroll', pageScrollFn[pagePath]);
8
- pageDOM = pageEl;
7
+ pageScrollFn[pagePath] && scrollEl.removeEventListener('scroll', pageScrollFn[pagePath]);
8
+ pageDOM = scrollEl;
9
9
  let isReachBottom = false;
10
10
  pageScrollFn[pagePath] = function () {
11
11
  var _a;
package/dist/index.cjs.js CHANGED
@@ -440,11 +440,11 @@ function bindPageResize(page) {
440
440
 
441
441
  const pageScrollFn = {};
442
442
  let pageDOM = window;
443
- function bindPageScroll(page, pageEl, distance = 50) {
443
+ function bindPageScroll(page, scrollEl, distance = 50) {
444
444
  var _a;
445
445
  const pagePath = (page ? page === null || page === void 0 ? void 0 : page.path : (_a = runtime.Current.router) === null || _a === void 0 ? void 0 : _a.path);
446
- pageScrollFn[pagePath] && pageEl.removeEventListener('scroll', pageScrollFn[pagePath]);
447
- pageDOM = pageEl;
446
+ pageScrollFn[pagePath] && scrollEl.removeEventListener('scroll', pageScrollFn[pagePath]);
447
+ pageDOM = scrollEl;
448
448
  let isReachBottom = false;
449
449
  pageScrollFn[pagePath] = function () {
450
450
  var _a;
@@ -472,6 +472,78 @@ function getOffset() {
472
472
  }
473
473
  }
474
474
 
475
+ /**
476
+ * 插入页面动画需要的样式
477
+ */
478
+ function loadAnimateStyle(ms = 300) {
479
+ const css = `
480
+ .taro_router .taro_page {
481
+ position: absolute;
482
+ left: 0;
483
+ top: 0;
484
+ width: 100%;
485
+ height: 100%;
486
+ background-color: #fff;
487
+ transform: translate(100%, 0);
488
+ transition: transform ${ms}ms;
489
+ z-index: 0;
490
+ }
491
+
492
+ .taro_router .taro_page.taro_tabbar_page,
493
+ .taro_router .taro_page.taro_page_show.taro_page_stationed {
494
+ transform: none;
495
+ }
496
+
497
+ .taro_router .taro_page.taro_page_show {
498
+ transform: translate(0, 0);
499
+ }`;
500
+ addStyle(css);
501
+ }
502
+ /**
503
+ * 插入路由相关样式
504
+ */
505
+ function loadRouterStyle(usingWindowScroll) {
506
+ const css = `
507
+ .taro_router {
508
+ overflow: hidden;
509
+ position: relative;
510
+ width: 100%;
511
+ height: 100%;
512
+ min-height: 100vh;
513
+ }
514
+
515
+ .taro-tabbar__container .taro_router {
516
+ min-height: calc(100vh - 50px);
517
+ }
518
+
519
+ .taro_page {
520
+ width: 100%;
521
+ height: 100%;
522
+ ${usingWindowScroll ? '' : `
523
+ overflow-x: hidden;
524
+ overflow-y: scroll;
525
+ max-height: 100vh;
526
+ `}
527
+ }
528
+
529
+ .taro-tabbar__container .taro-tabbar__panel {
530
+ overflow: hidden;
531
+ }
532
+
533
+ .taro-tabbar__container .taro_page.taro_tabbar_page {
534
+ max-height: calc(100vh - 50px);
535
+ }
536
+ `;
537
+ addStyle(css);
538
+ }
539
+ function addStyle(css) {
540
+ if (!css)
541
+ return;
542
+ const style = document.createElement('style');
543
+ style.innerHTML = css;
544
+ document.getElementsByTagName('head')[0].appendChild(style);
545
+ }
546
+
475
547
  // @ts-nocheck
476
548
  function initTabbar(config) {
477
549
  if (config.tabBar == null) {
@@ -532,6 +604,17 @@ class MultiPageHandler {
532
604
  return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
533
605
  }
534
606
  get search() { return location.search.substr(1); }
607
+ get usingWindowScroll() {
608
+ var _a;
609
+ let usingWindowScroll = true;
610
+ if (typeof ((_a = this.pageConfig) === null || _a === void 0 ? void 0 : _a.usingWindowScroll) === 'boolean') {
611
+ usingWindowScroll = this.pageConfig.usingWindowScroll;
612
+ }
613
+ const win = window;
614
+ win.__taroAppConfig || (win.__taroAppConfig = {});
615
+ win.__taroAppConfig.usingWindowScroll = usingWindowScroll;
616
+ return usingWindowScroll;
617
+ }
535
618
  getQuery(search = '', options = {}) {
536
619
  search = search ? `${search}&${this.search}` : this.search;
537
620
  const query = search
@@ -541,6 +624,7 @@ class MultiPageHandler {
541
624
  }
542
625
  mount() {
543
626
  setHistoryMode(this.routerMode, this.router.basename);
627
+ loadRouterStyle(this.usingWindowScroll);
544
628
  const appId = this.appId;
545
629
  let app = document.getElementById(appId);
546
630
  let isPosition = true;
@@ -577,7 +661,20 @@ class MultiPageHandler {
577
661
  const pageEl = this.getPageContainer(page);
578
662
  if (pageEl && !(pageEl === null || pageEl === void 0 ? void 0 : pageEl['__isReady'])) {
579
663
  const el = pageEl.firstElementChild;
580
- (_a = el === null || el === void 0 ? void 0 : el['componentOnReady']) === null || _a === void 0 ? void 0 : _a.call(el);
664
+ const componentOnReady = el === null || el === void 0 ? void 0 : el['componentOnReady'];
665
+ if (componentOnReady) {
666
+ componentOnReady === null || componentOnReady === void 0 ? void 0 : componentOnReady().then(() => {
667
+ requestAnimationFrame(() => {
668
+ var _a;
669
+ (_a = page.onReady) === null || _a === void 0 ? void 0 : _a.call(page);
670
+ pageEl['__isReady'] = true;
671
+ });
672
+ });
673
+ }
674
+ else {
675
+ (_a = page.onReady) === null || _a === void 0 ? void 0 : _a.call(page);
676
+ pageEl['__isReady'] = true;
677
+ }
581
678
  onLoad && (pageEl['__page'] = page);
582
679
  }
583
680
  }
@@ -587,11 +684,13 @@ class MultiPageHandler {
587
684
  return;
588
685
  (_a = page.onLoad) === null || _a === void 0 ? void 0 : _a.call(page, this.getQuery('', page.options), () => {
589
686
  var _a;
590
- const pageEl = this.getPageContainer(page);
591
- this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
687
+ if (this.isTabBar) {
688
+ const pageEl = this.getPageContainer(page);
689
+ pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page');
690
+ }
592
691
  this.onReady(page, true);
593
692
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
594
- this.bindPageEvents(page, pageEl, pageConfig);
693
+ this.bindPageEvents(page, pageConfig);
595
694
  });
596
695
  }
597
696
  getPageContainer(page) {
@@ -605,15 +704,18 @@ class MultiPageHandler {
605
704
  ? document.querySelector(`.taro_page#${id}`)
606
705
  : document.querySelector('.taro_page') ||
607
706
  document.querySelector('.taro_router'));
608
- return el || window;
707
+ return el;
609
708
  }
610
- bindPageEvents(page, pageEl, config = {}) {
709
+ getScrollingElement(page) {
710
+ if (this.usingWindowScroll)
711
+ return window;
712
+ return this.getPageContainer(page) || window;
713
+ }
714
+ bindPageEvents(page, config = {}) {
611
715
  var _a;
612
- if (!pageEl) {
613
- pageEl = this.getPageContainer();
614
- }
716
+ const scrollEl = this.getScrollingElement(page);
615
717
  const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
616
- bindPageScroll(page, pageEl, distance);
718
+ bindPageScroll(page, scrollEl, distance);
617
719
  bindPageResize(page);
618
720
  }
619
721
  }
@@ -682,36 +784,6 @@ function createMultiRouter(app, config, framework) {
682
784
  });
683
785
  }
684
786
 
685
- /**
686
- * 插入页面动画需要的样式
687
- */
688
- function loadAnimateStyle(ms = 300) {
689
- const css = `
690
- .taro_router .taro_page {
691
- position: absolute;
692
- left: 0;
693
- top: 0;
694
- width: 100%;
695
- height: 100%;
696
- background-color: #fff;
697
- transform: translate(100%, 0);
698
- transition: transform ${ms}ms;
699
- z-index: 0;
700
- }
701
-
702
- .taro_router .taro_page.taro_tabbar_page,
703
- .taro_router .taro_page.taro_page_show.taro_page_stationed {
704
- transform: none;
705
- }
706
-
707
- .taro_router .taro_page.taro_page_show {
708
- transform: translate(0, 0);
709
- }`;
710
- const style = document.createElement('style');
711
- style.innerHTML = css;
712
- document.getElementsByTagName('head')[0].appendChild(style);
713
- }
714
-
715
787
  /* eslint-disable dot-notation */
716
788
  function setDisplay(el, type = '') {
717
789
  if (el) {
@@ -797,6 +869,17 @@ class PageHandler {
797
869
  }
798
870
  return search.substring(1);
799
871
  }
872
+ get usingWindowScroll() {
873
+ var _a;
874
+ let usingWindowScroll = false;
875
+ if (typeof ((_a = this.pageConfig) === null || _a === void 0 ? void 0 : _a.usingWindowScroll) === 'boolean') {
876
+ usingWindowScroll = this.pageConfig.usingWindowScroll;
877
+ }
878
+ const win = window;
879
+ win.__taroAppConfig || (win.__taroAppConfig = {});
880
+ win.__taroAppConfig.usingWindowScroll = usingWindowScroll;
881
+ return usingWindowScroll;
882
+ }
800
883
  getQuery(stamp = '', search = '', options = {}) {
801
884
  search = search ? `${search}&${this.search}` : this.search;
802
885
  const query = search
@@ -807,7 +890,9 @@ class PageHandler {
807
890
  }
808
891
  mount() {
809
892
  setHistoryMode(this.routerMode, this.router.basename);
893
+ this.pathname = exports.history.location.pathname;
810
894
  this.animation && loadAnimateStyle(this.animationDuration);
895
+ loadRouterStyle(this.usingWindowScroll);
811
896
  const appId = this.appId;
812
897
  let app = document.getElementById(appId);
813
898
  let isPosition = true;
@@ -874,7 +959,8 @@ class PageHandler {
874
959
  this.isTabBar(this.pathname) && pageEl.classList.add('taro_tabbar_page');
875
960
  this.addAnimation(pageEl, pageNo === 0);
876
961
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
877
- this.bindPageEvents(page, pageEl, pageConfig);
962
+ this.bindPageEvents(page, pageConfig);
963
+ this.triggerRouterChange();
878
964
  }
879
965
  else {
880
966
  (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
@@ -884,7 +970,8 @@ class PageHandler {
884
970
  this.addAnimation(pageEl, pageNo === 0);
885
971
  this.onReady(page, true);
886
972
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
887
- this.bindPageEvents(page, pageEl, pageConfig);
973
+ this.bindPageEvents(page, pageConfig);
974
+ this.triggerRouterChange();
888
975
  });
889
976
  }
890
977
  }
@@ -932,7 +1019,8 @@ class PageHandler {
932
1019
  setDisplay(pageEl);
933
1020
  this.addAnimation(pageEl, pageNo === 0);
934
1021
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
935
- this.bindPageEvents(page, pageEl, pageConfig);
1022
+ this.bindPageEvents(page, pageConfig);
1023
+ this.triggerRouterChange();
936
1024
  }
937
1025
  else {
938
1026
  (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
@@ -941,7 +1029,8 @@ class PageHandler {
941
1029
  this.addAnimation(pageEl, pageNo === 0);
942
1030
  this.onReady(page, false);
943
1031
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
944
- this.bindPageEvents(page, pageEl, pageConfig);
1032
+ this.bindPageEvents(page, pageConfig);
1033
+ this.triggerRouterChange();
945
1034
  });
946
1035
  }
947
1036
  }
@@ -995,17 +1084,33 @@ class PageHandler {
995
1084
  ? document.querySelector(`.taro_page#${id}`)
996
1085
  : document.querySelector('.taro_page') ||
997
1086
  document.querySelector('.taro_router'));
998
- return el || window;
1087
+ return el;
999
1088
  }
1000
- bindPageEvents(page, pageEl, config = {}) {
1089
+ getScrollingElement(page) {
1090
+ if (this.usingWindowScroll)
1091
+ return window;
1092
+ return this.getPageContainer(page) || window;
1093
+ }
1094
+ bindPageEvents(page, config = {}) {
1001
1095
  var _a;
1002
- if (!pageEl) {
1003
- pageEl = this.getPageContainer();
1004
- }
1096
+ const scrollEl = this.getScrollingElement(page);
1005
1097
  const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
1006
- bindPageScroll(page, pageEl, distance);
1098
+ bindPageScroll(page, scrollEl, distance);
1007
1099
  bindPageResize(page);
1008
1100
  }
1101
+ triggerRouterChange() {
1102
+ /**
1103
+ * @tarojs/runtime 中生命周期跑在 promise 中,所以这里需要 setTimeout 延迟事件调用
1104
+ * TODO 考虑将生命周期返回 Promise,用于处理相关事件调用顺序
1105
+ */
1106
+ setTimeout(() => {
1107
+ runtime.eventCenter.trigger('__afterTaroRouterChange', {
1108
+ toLocation: {
1109
+ path: this.pathname
1110
+ }
1111
+ });
1112
+ }, 0);
1113
+ }
1009
1114
  }
1010
1115
 
1011
1116
  const createStampId = runtime.incrementId();
@@ -1039,8 +1144,10 @@ function createRouter(app, config, framework) {
1039
1144
  (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
1040
1145
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
1041
1146
  const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
1042
- var _c, _d, _e, _f, _g;
1147
+ var _c, _d, _e, _f, _g, _h;
1043
1148
  handler.pathname = decodeURI(location.pathname);
1149
+ if ((_c = window.__taroAppConfig) === null || _c === void 0 ? void 0 : _c.usingWindowScroll)
1150
+ window.scrollTo(0, 0);
1044
1151
  runtime.eventCenter.trigger('__taroRouterChange', {
1045
1152
  toLocation: {
1046
1153
  path: handler.pathname
@@ -1058,7 +1165,7 @@ function createRouter(app, config, framework) {
1058
1165
  path: handler.pathname,
1059
1166
  query: handler.getQuery(createStampId()),
1060
1167
  };
1061
- (_c = app.onPageNotFound) === null || _c === void 0 ? void 0 : _c.call(app, notFoundEvent);
1168
+ (_d = app.onPageNotFound) === null || _d === void 0 ? void 0 : _d.call(app, notFoundEvent);
1062
1169
  runtime.eventCenter.trigger('__taroRouterNotFound', notFoundEvent);
1063
1170
  }
1064
1171
  else if (/Loading hot update .* failed./.test(error.message)) {
@@ -1072,16 +1179,16 @@ function createRouter(app, config, framework) {
1072
1179
  if (!element)
1073
1180
  return;
1074
1181
  const pageConfig = handler.pageConfig;
1075
- let enablePullDownRefresh = ((_d = config === null || config === void 0 ? void 0 : config.window) === null || _d === void 0 ? void 0 : _d.enablePullDownRefresh) || false;
1182
+ let enablePullDownRefresh = ((_e = config === null || config === void 0 ? void 0 : config.window) === null || _e === void 0 ? void 0 : _e.enablePullDownRefresh) || false;
1076
1183
  if (pageConfig) {
1077
- setTitle((_e = pageConfig.navigationBarTitleText) !== null && _e !== void 0 ? _e : document.title);
1184
+ setTitle((_f = pageConfig.navigationBarTitleText) !== null && _f !== void 0 ? _f : document.title);
1078
1185
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
1079
1186
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
1080
1187
  }
1081
1188
  }
1082
1189
  const currentPage = runtime.Current.page;
1083
1190
  const pathname = handler.pathname;
1084
- const methodName = (_f = stacks.method) !== null && _f !== void 0 ? _f : '';
1191
+ const methodName = (_g = stacks.method) !== null && _g !== void 0 ? _g : '';
1085
1192
  const cacheTabs = stacks.getTabs();
1086
1193
  let shouldLoad = false;
1087
1194
  stacks.method = '';
@@ -1145,7 +1252,7 @@ function createRouter(app, config, framework) {
1145
1252
  shouldLoad = true;
1146
1253
  }
1147
1254
  if (shouldLoad || stacks.length < 1) {
1148
- const el = (_g = element.default) !== null && _g !== void 0 ? _g : element;
1255
+ const el = (_h = element.default) !== null && _h !== void 0 ? _h : element;
1149
1256
  const loadConfig = Object.assign({}, pageConfig);
1150
1257
  const stacksIndex = stacks.length;
1151
1258
  delete loadConfig['path'];
@@ -1163,11 +1270,6 @@ function createRouter(app, config, framework) {
1163
1270
  page.options = params;
1164
1271
  handler.load(page, pageConfig, pageStampId, stacksIndex);
1165
1272
  }
1166
- runtime.eventCenter.trigger('__afterTaroRouterChange', {
1167
- toLocation: {
1168
- path: handler.pathname
1169
- }
1170
- });
1171
1273
  });
1172
1274
  const routePath = addLeadingSlash(stripBasename(exports.history.location.pathname, handler.basename));
1173
1275
  if (routePath === '/') {