@tarojs/router 3.8.0-canary.0 → 4.0.0-beta.0

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/api.js CHANGED
@@ -65,7 +65,12 @@ function navigate(option, method) {
65
65
  }
66
66
  else if (method === 'navigateBack') {
67
67
  stacks.delta = option.delta;
68
- history.go(-option.delta);
68
+ if (stacks.length > option.delta) {
69
+ history.go(-option.delta);
70
+ }
71
+ else {
72
+ history.go(1 - stacks.length);
73
+ }
69
74
  }
70
75
  }
71
76
  catch (error) {
package/dist/index.cjs.js CHANGED
@@ -41,65 +41,6 @@ function __awaiter(thisArg, _arguments, P, generator) {
41
41
  });
42
42
  }
43
43
 
44
- // export const removeLeadingSlash = (str = '') => str.replace(/^\.?\//, '')
45
- // export const removeTrailingSearch = (str = '') => str.replace(/\?[\s\S]*$/, '')
46
- const addLeadingSlash = (url = '') => (url.charAt(0) === '/' ? url : '/' + url);
47
- const hasBasename = (path = '', prefix = '') => new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path) || path === prefix;
48
- const stripBasename = (path = '', prefix = '') => hasBasename(path, prefix) ? path.substring(prefix.length) : path;
49
- const stripTrailing = (str = '') => str.replace(/[?#][\s\S]*$/, '');
50
- const getHomePage = (path = '', basename = '', customRoutes = {}, entryPagePath = '') => {
51
- var _a;
52
- const routePath = addLeadingSlash(stripBasename(path, basename));
53
- const alias = ((_a = Object.entries(customRoutes).find(([key]) => key === routePath)) === null || _a === void 0 ? void 0 : _a[1]) || routePath;
54
- return entryPagePath || (typeof alias === 'string' ? alias : alias[0]) || basename;
55
- };
56
- const getCurrentPage = (routerMode = 'hash', basename = '/') => {
57
- const pagePath = routerMode === 'hash'
58
- ? location.hash.slice(1).split('?')[0]
59
- : location.pathname;
60
- return addLeadingSlash(stripBasename(pagePath, basename));
61
- };
62
- class RoutesAlias {
63
- constructor() {
64
- this.conf = [];
65
- this.getConfig = (url = '') => {
66
- const customRoute = this.conf.filter((arr) => {
67
- return arr.includes(url);
68
- });
69
- return customRoute[0];
70
- };
71
- this.getOrigin = (url = '') => {
72
- var _a;
73
- return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[0]) || url;
74
- };
75
- this.getAlias = (url = '') => {
76
- var _a;
77
- return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[1]) || url;
78
- };
79
- this.getAll = (url = '') => {
80
- return this.conf
81
- .filter((arr) => arr.includes(url))
82
- .reduceRight((p, a) => {
83
- p.unshift(a[1]);
84
- return p;
85
- }, []);
86
- };
87
- }
88
- set(customRoutes = {}) {
89
- for (let key in customRoutes) {
90
- const path = customRoutes[key];
91
- key = addLeadingSlash(key);
92
- if (typeof path === 'string') {
93
- this.conf.push([key, addLeadingSlash(path)]);
94
- }
95
- else if ((path === null || path === void 0 ? void 0 : path.length) > 0) {
96
- this.conf.push(...path.map(p => [key, addLeadingSlash(p)]));
97
- }
98
- }
99
- }
100
- }
101
- const routesAlias = new RoutesAlias();
102
-
103
44
  class RouterConfig {
104
45
  static set config(e) {
105
46
  this.__config = e;
@@ -118,7 +59,7 @@ class RouterConfig {
118
59
  }
119
60
  static get customRoutes() { return this.router.customRoutes || {}; }
120
61
  static isPage(url = '') {
121
- return this.pages.findIndex(e => addLeadingSlash(e) === url) !== -1;
62
+ return this.pages.findIndex(e => prependBasename(e) === url) !== -1;
122
63
  }
123
64
  }
124
65
 
@@ -302,6 +243,65 @@ class Stacks {
302
243
  }
303
244
  const stacks = new Stacks();
304
245
 
246
+ // export const removeLeadingSlash = (str = '') => str.replace(/^\.?\//, '')
247
+ // export const removeTrailingSearch = (str = '') => str.replace(/\?[\s\S]*$/, '')
248
+ const addLeadingSlash = (url = '') => (url.charAt(0) === '/' ? url : '/' + url);
249
+ const hasBasename = (path = '', prefix = '') => new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path) || path === prefix;
250
+ const stripBasename = (path = '', prefix = '') => hasBasename(path, prefix) ? path.substring(prefix.length) : path;
251
+ const stripTrailing = (str = '') => str.replace(/[?#][\s\S]*$/, '');
252
+ const getHomePage = (path = '', basename = '', customRoutes = {}, entryPagePath = '') => {
253
+ var _a;
254
+ const routePath = addLeadingSlash(stripBasename(path, basename));
255
+ const alias = ((_a = Object.entries(customRoutes).find(([key]) => key === routePath)) === null || _a === void 0 ? void 0 : _a[1]) || routePath;
256
+ return entryPagePath || (typeof alias === 'string' ? alias : alias[0]) || basename;
257
+ };
258
+ const getCurrentPage = (routerMode = 'hash', basename = '/') => {
259
+ const pagePath = routerMode === 'hash'
260
+ ? location.hash.slice(1).split('?')[0]
261
+ : location.pathname;
262
+ return addLeadingSlash(stripBasename(pagePath, basename));
263
+ };
264
+ class RoutesAlias {
265
+ constructor() {
266
+ this.conf = [];
267
+ this.getConfig = (url = '') => {
268
+ const customRoute = this.conf.filter((arr) => {
269
+ return arr.includes(url);
270
+ });
271
+ return customRoute[0];
272
+ };
273
+ this.getOrigin = (url = '') => {
274
+ var _a;
275
+ return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[0]) || url;
276
+ };
277
+ this.getAlias = (url = '') => {
278
+ var _a;
279
+ return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[1]) || url;
280
+ };
281
+ this.getAll = (url = '') => {
282
+ return this.conf
283
+ .filter((arr) => arr.includes(url))
284
+ .reduceRight((p, a) => {
285
+ p.unshift(a[1]);
286
+ return p;
287
+ }, []);
288
+ };
289
+ }
290
+ set(customRoutes = {}) {
291
+ for (let key in customRoutes) {
292
+ const path = customRoutes[key];
293
+ key = addLeadingSlash(key);
294
+ if (typeof path === 'string') {
295
+ this.conf.push([key, addLeadingSlash(path)]);
296
+ }
297
+ else if ((path === null || path === void 0 ? void 0 : path.length) > 0) {
298
+ this.conf.push(...path.map(p => [key, addLeadingSlash(p)]));
299
+ }
300
+ }
301
+ }
302
+ }
303
+ const routesAlias = new RoutesAlias();
304
+
305
305
  function processNavigateUrl(option) {
306
306
  var _a;
307
307
  const pathPieces = history.parsePath(option.url);
@@ -355,7 +355,12 @@ function navigate(option, method) {
355
355
  }
356
356
  else if (method === 'navigateBack') {
357
357
  stacks.delta = option.delta;
358
- exports.history.go(-option.delta);
358
+ if (stacks.length > option.delta) {
359
+ exports.history.go(-option.delta);
360
+ }
361
+ else {
362
+ exports.history.go(1 - stacks.length);
363
+ }
359
364
  }
360
365
  }
361
366
  catch (error) {
@@ -477,7 +482,7 @@ function getOffset() {
477
482
  */
478
483
  function loadAnimateStyle(ms = 300) {
479
484
  const css = `
480
- .taro_router .taro_page {
485
+ .taro_router > .taro_page {
481
486
  position: absolute;
482
487
  left: 0;
483
488
  top: 0;
@@ -489,14 +494,15 @@ function loadAnimateStyle(ms = 300) {
489
494
  z-index: 0;
490
495
  }
491
496
 
492
- .taro_router .taro_page.taro_tabbar_page,
493
- .taro_router .taro_page.taro_page_show.taro_page_stationed {
497
+ .taro_router > .taro_page.taro_tabbar_page,
498
+ .taro_router > .taro_page.taro_page_show.taro_page_stationed {
494
499
  transform: none;
495
500
  }
496
501
 
497
- .taro_router .taro_page.taro_page_show {
502
+ .taro_router > .taro_page.taro_page_show {
498
503
  transform: translate(0, 0);
499
- }`;
504
+ }
505
+ `;
500
506
  addStyle(css);
501
507
  }
502
508
  /**
@@ -508,11 +514,6 @@ function loadRouterStyle(usingWindowScroll) {
508
514
  position: relative;
509
515
  width: 100%;
510
516
  height: 100%;
511
- min-height: 100vh;
512
- }
513
-
514
- .taro-tabbar__container .taro_router {
515
- min-height: calc(100vh - 50px);
516
517
  }
517
518
 
518
519
  .taro_page {
@@ -525,12 +526,18 @@ function loadRouterStyle(usingWindowScroll) {
525
526
  `}
526
527
  }
527
528
 
528
- .taro-tabbar__container .taro-tabbar__panel {
529
+ .taro-tabbar__container > .taro-tabbar__panel {
529
530
  overflow: hidden;
530
531
  }
531
532
 
532
- .taro-tabbar__container .taro_page.taro_tabbar_page {
533
- max-height: calc(100vh - 50px);
533
+ .taro-tabbar__container > .taro-tabbar__panel > .taro_page.taro_tabbar_page {
534
+ max-height: calc(100vh - var(--taro-tabbar-height) - constant(safe-area-inset-bottom));
535
+ max-height: calc(100vh - var(--taro-tabbar-height) - env(safe-area-inset-bottom));
536
+ }
537
+
538
+ .taro_page_shade,
539
+ .taro_router > .taro_page.taro_page_show.taro_page_stationed:not(.taro_page_shade):not(.taro_tabbar_page):not(:last-child) {
540
+ display: none;
534
541
  }
535
542
  `;
536
543
  addStyle(css);
@@ -545,7 +552,7 @@ function addStyle(css) {
545
552
 
546
553
  // @ts-nocheck
547
554
  function initTabbar(config) {
548
- if (config.tabBar == null) {
555
+ if (config.tabBar == null || config.tabBar.custom) {
549
556
  return;
550
557
  }
551
558
  // TODO: custom-tab-bar
@@ -690,6 +697,7 @@ class MultiPageHandler {
690
697
  this.onReady(page, true);
691
698
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
692
699
  this.bindPageEvents(page, pageConfig);
700
+ this.triggerRouterChange();
693
701
  });
694
702
  }
695
703
  getPageContainer(page) {
@@ -717,6 +725,19 @@ class MultiPageHandler {
717
725
  bindPageScroll(page, scrollEl, distance);
718
726
  bindPageResize(page);
719
727
  }
728
+ triggerRouterChange() {
729
+ /**
730
+ * @tarojs/runtime 中生命周期跑在 promise 中,所以这里需要 setTimeout 延迟事件调用
731
+ * TODO 考虑将生命周期返回 Promise,用于处理相关事件调用顺序
732
+ */
733
+ setTimeout(() => {
734
+ runtime.eventCenter.trigger('__afterTaroRouterChange', {
735
+ toLocation: {
736
+ path: this.pathname
737
+ }
738
+ });
739
+ }, 0);
740
+ }
720
741
  }
721
742
 
722
743
  const createStampId$1 = runtime.incrementId();
@@ -784,11 +805,6 @@ function createMultiRouter(app, config, framework) {
784
805
  }
785
806
 
786
807
  /* eslint-disable dot-notation */
787
- function setDisplay(el, type = '') {
788
- if (el) {
789
- el.style.display = type;
790
- }
791
- }
792
808
  class PageHandler {
793
809
  constructor(config) {
794
810
  this.defaultAnimation = { duration: 300, delay: 50 };
@@ -954,7 +970,7 @@ class PageHandler {
954
970
  const param = this.getQuery(stampId, '', page.options);
955
971
  let pageEl = this.getPageContainer(page);
956
972
  if (pageEl) {
957
- setDisplay(pageEl);
973
+ pageEl.classList.remove('taro_page_shade');
958
974
  this.isTabBar(this.pathname) && pageEl.classList.add('taro_tabbar_page');
959
975
  this.addAnimation(pageEl, pageNo === 0);
960
976
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
@@ -1019,7 +1035,7 @@ class PageHandler {
1019
1035
  const param = this.getQuery(page['$taroParams']['stamp'], '', page.options);
1020
1036
  let pageEl = this.getPageContainer(page);
1021
1037
  if (pageEl) {
1022
- setDisplay(pageEl);
1038
+ pageEl.classList.remove('taro_page_shade');
1023
1039
  this.addAnimation(pageEl, pageNo === 0);
1024
1040
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
1025
1041
  this.bindPageEvents(page, pageConfig);
@@ -1047,12 +1063,12 @@ class PageHandler {
1047
1063
  if (this.hideTimer) {
1048
1064
  clearTimeout(this.hideTimer);
1049
1065
  this.hideTimer = null;
1050
- setDisplay(this.lastHidePage, 'none');
1066
+ pageEl.classList.add('taro_page_shade');
1051
1067
  }
1052
1068
  this.lastHidePage = pageEl;
1053
1069
  this.hideTimer = setTimeout(() => {
1054
1070
  this.hideTimer = null;
1055
- setDisplay(this.lastHidePage, 'none');
1071
+ pageEl.classList.add('taro_page_shade');
1056
1072
  }, this.animationDuration + this.animationDelay);
1057
1073
  (_a = page.onHide) === null || _a === void 0 ? void 0 : _a.call(page);
1058
1074
  }