@tarojs/router 3.6.0-canary.4 → 3.6.0-canary.6

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
@@ -39,6 +39,7 @@ function processNavigateUrl(option) {
39
39
  function navigate(option, method) {
40
40
  return __awaiter(this, void 0, void 0, function* () {
41
41
  return new Promise((resolve, reject) => {
42
+ stacks.method = method;
42
43
  const { success, complete, fail } = option;
43
44
  const unListen = history.listen(() => {
44
45
  const res = { errMsg: `${method}:ok` };
package/dist/index.cjs.js CHANGED
@@ -212,6 +212,8 @@ class Stacks {
212
212
  constructor() {
213
213
  this.stacks = [];
214
214
  this.backDelta = 0;
215
+ this.tabs = {};
216
+ this.methodName = '';
215
217
  }
216
218
  set delta(delta) {
217
219
  if (delta > 0) {
@@ -227,6 +229,12 @@ class Stacks {
227
229
  get delta() {
228
230
  return this.backDelta;
229
231
  }
232
+ set method(methodName) {
233
+ this.methodName = methodName;
234
+ }
235
+ get method() {
236
+ return this.methodName;
237
+ }
230
238
  get length() {
231
239
  return this.stacks.length;
232
240
  }
@@ -264,6 +272,20 @@ class Stacks {
264
272
  push(page) {
265
273
  return this.stacks.push(page);
266
274
  }
275
+ getTabs() {
276
+ return this.tabs;
277
+ }
278
+ pushTab(path) {
279
+ this.tabs[path] = this.last;
280
+ this.pop();
281
+ }
282
+ popTab(path) {
283
+ this.push(this.tabs[path]);
284
+ delete this.tabs[path];
285
+ }
286
+ removeTab(path) {
287
+ delete this.tabs[path];
288
+ }
267
289
  }
268
290
  const stacks = new Stacks();
269
291
 
@@ -294,6 +316,7 @@ function processNavigateUrl(option) {
294
316
  function navigate(option, method) {
295
317
  return __awaiter(this, void 0, void 0, function* () {
296
318
  return new Promise((resolve, reject) => {
319
+ stacks.method = method;
297
320
  const { success, complete, fail } = option;
298
321
  const unListen = exports.history.listen(() => {
299
322
  const res = { errMsg: `${method}:ok` };
@@ -470,7 +493,7 @@ class MultiPageHandler {
470
493
  this.config = config;
471
494
  this.mount();
472
495
  }
473
- get appId() { return 'app'; }
496
+ get appId() { return this.config.appId || 'app'; }
474
497
  get router() { return this.config.router || {}; }
475
498
  get routerMode() { return this.router.mode || 'hash'; }
476
499
  get customRoutes() { return this.router.customRoutes || {}; }
@@ -503,11 +526,13 @@ class MultiPageHandler {
503
526
  return Object.assign(Object.assign({}, query), options);
504
527
  }
505
528
  mount() {
506
- var _a;
507
529
  setHistoryMode(this.routerMode, this.router.basename);
508
- (_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
509
- const app = document.createElement('div');
510
- app.id = this.appId;
530
+ const appId = this.appId;
531
+ let app = document.getElementById(appId);
532
+ if (!app) {
533
+ app = document.createElement('div');
534
+ app.id = appId;
535
+ }
511
536
  app.classList.add('taro_router');
512
537
  if (this.tabBarList.length > 1) {
513
538
  const container = document.createElement('div');
@@ -570,6 +595,8 @@ class MultiPageHandler {
570
595
  }
571
596
  }
572
597
 
598
+ const createStampId$1 = runtime.incrementId();
599
+ const launchStampId$1 = createStampId$1();
573
600
  // TODO 支持多路由 (APP 生命周期仅触发一次)
574
601
  /** Note: 关于多页面应用
575
602
  * - 需要配置路由映射(根目录跳转、404 页面……)
@@ -585,7 +612,7 @@ function createMultiRouter(app, config, framework) {
585
612
  const handler = new MultiPageHandler(config);
586
613
  const launchParam = {
587
614
  path: config.pageName,
588
- query: handler.getQuery(),
615
+ query: handler.getQuery(launchStampId$1),
589
616
  scene: 0,
590
617
  shareTicket: '',
591
618
  referrerInfo: {}
@@ -672,7 +699,7 @@ class PageHandler {
672
699
  this.homePage = this.getHomePage();
673
700
  this.mount();
674
701
  }
675
- get appId() { return 'app'; }
702
+ get appId() { return this.config.appId || 'app'; }
676
703
  get router() { return this.config.router || {}; }
677
704
  get routerMode() { return this.router.mode || 'hash'; }
678
705
  get customRoutes() { return this.router.customRoutes || {}; }
@@ -708,9 +735,9 @@ class PageHandler {
708
735
  return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
709
736
  });
710
737
  }
711
- get isTabBar() {
738
+ isTabBar(pathname) {
712
739
  var _a;
713
- const routePath = addLeadingSlash(stripBasename(this.pathname, this.basename));
740
+ const routePath = addLeadingSlash(stripBasename(pathname, this.basename)).split('?')[0];
714
741
  const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
715
742
  if (typeof target === 'string') {
716
743
  return target === routePath;
@@ -746,21 +773,23 @@ class PageHandler {
746
773
  }
747
774
  return search.substr(1);
748
775
  }
749
- getQuery(stamp = 0, search = '', options = {}) {
776
+ getQuery(stamp = '', search = '', options = {}) {
750
777
  search = search ? `${search}&${this.search}` : this.search;
751
778
  const query = search
752
779
  ? queryString__default["default"].parse(search, { decode: false })
753
780
  : {};
754
- query.stamp = stamp.toString();
781
+ query.stamp = stamp;
755
782
  return Object.assign(Object.assign({}, query), options);
756
783
  }
757
784
  mount() {
758
- var _a;
759
785
  setHistoryMode(this.routerMode, this.router.basename);
760
- (_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
761
786
  this.animation && loadAnimateStyle(this.animationDuration);
762
- const app = document.createElement('div');
763
- app.id = this.appId;
787
+ const appId = this.appId;
788
+ let app = document.getElementById(appId);
789
+ if (!app) {
790
+ app = document.createElement('div');
791
+ app.id = appId;
792
+ }
764
793
  app.classList.add('taro_router');
765
794
  if (this.tabBarList.length > 1) {
766
795
  const container = document.createElement('div');
@@ -792,18 +821,18 @@ class PageHandler {
792
821
  onLoad && (pageEl['__page'] = page);
793
822
  }
794
823
  }
795
- load(page, pageConfig = {}, stacksIndex = 0) {
824
+ load(page, pageConfig = {}, stampId, pageNo = 0) {
796
825
  var _a, _b;
797
826
  if (!page)
798
827
  return;
799
828
  // NOTE: 页面栈推入太晚可能导致 getCurrentPages 无法获取到当前页面实例
800
829
  stacks.push(page);
801
- const param = this.getQuery(stacks.length, '', page.options);
830
+ const param = this.getQuery(stampId, '', page.options);
802
831
  let pageEl = this.getPageContainer(page);
803
832
  if (pageEl) {
804
833
  setDisplay(pageEl);
805
- this.isTabBar && pageEl.classList.add('taro_tabbar_page');
806
- this.addAnimation(pageEl, stacksIndex === 0);
834
+ this.isTabBar(this.pathname) && pageEl.classList.add('taro_tabbar_page');
835
+ this.addAnimation(pageEl, pageNo === 0);
807
836
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
808
837
  this.bindPageEvents(page, pageEl, pageConfig);
809
838
  }
@@ -811,8 +840,8 @@ class PageHandler {
811
840
  (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
812
841
  var _a;
813
842
  pageEl = this.getPageContainer(page);
814
- this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
815
- this.addAnimation(pageEl, stacksIndex === 0);
843
+ this.isTabBar(this.pathname) && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
844
+ this.addAnimation(pageEl, pageNo === 0);
816
845
  this.onReady(page, true);
817
846
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
818
847
  this.bindPageEvents(page, pageEl, pageConfig);
@@ -850,15 +879,15 @@ class PageHandler {
850
879
  if (delta >= 1)
851
880
  this.unload(stacks.last, delta);
852
881
  }
853
- show(page, pageConfig = {}, stacksIndex = 0) {
882
+ show(page, pageConfig = {}, pageNo = 0) {
854
883
  var _a, _b;
855
884
  if (!page)
856
885
  return;
857
- const param = this.getQuery(stacks.length, '', page.options);
886
+ const param = this.getQuery(page['$taroParams']['stamp'], '', page.options);
858
887
  let pageEl = this.getPageContainer(page);
859
888
  if (pageEl) {
860
889
  setDisplay(pageEl);
861
- this.addAnimation(pageEl, stacksIndex === 0);
890
+ this.addAnimation(pageEl, pageNo === 0);
862
891
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
863
892
  this.bindPageEvents(page, pageEl, pageConfig);
864
893
  }
@@ -866,7 +895,7 @@ class PageHandler {
866
895
  (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
867
896
  var _a;
868
897
  pageEl = this.getPageContainer(page);
869
- this.addAnimation(pageEl, stacksIndex === 0);
898
+ this.addAnimation(pageEl, pageNo === 0);
870
899
  this.onReady(page, false);
871
900
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
872
901
  this.bindPageEvents(page, pageEl, pageConfig);
@@ -936,6 +965,8 @@ class PageHandler {
936
965
  }
937
966
  }
938
967
 
968
+ const createStampId = runtime.incrementId();
969
+ let launchStampId = createStampId();
939
970
  function createRouter(app, config, framework) {
940
971
  var _a, _b;
941
972
  RouterConfig.config = config;
@@ -953,7 +984,7 @@ function createRouter(app, config, framework) {
953
984
  const router = new UniversalRouter__default["default"](routes, { baseUrl: basename || '' });
954
985
  const launchParam = {
955
986
  path: handler.homePage,
956
- query: handler.getQuery(stacks.length),
987
+ query: handler.getQuery(launchStampId),
957
988
  scene: 0,
958
989
  shareTicket: '',
959
990
  referrerInfo: {}
@@ -962,7 +993,7 @@ function createRouter(app, config, framework) {
962
993
  (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
963
994
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
964
995
  const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
965
- var _c, _d, _e, _f, _g;
996
+ var _c, _d, _e, _f, _g, _h;
966
997
  handler.pathname = decodeURI(location.pathname);
967
998
  runtime.eventCenter.trigger('__taroRouterChange', {
968
999
  toLocation: {
@@ -1001,8 +1032,45 @@ function createRouter(app, config, framework) {
1001
1032
  }
1002
1033
  const currentPage = runtime.Current.page;
1003
1034
  const pathname = handler.pathname;
1035
+ const methodName = (_g = stacks.method) !== null && _g !== void 0 ? _g : '';
1036
+ const cacheTabs = stacks.getTabs();
1004
1037
  let shouldLoad = false;
1005
- if (action === 'POP') {
1038
+ stacks.method = '';
1039
+ if (methodName === 'reLaunch') {
1040
+ handler.unload(currentPage, stacks.length);
1041
+ // NOTE: 同时卸载缓存在tabs里面的页面实例
1042
+ for (const key in cacheTabs) {
1043
+ if (cacheTabs[key]) {
1044
+ handler.unload(cacheTabs[key]);
1045
+ stacks.removeTab(key);
1046
+ }
1047
+ }
1048
+ shouldLoad = true;
1049
+ }
1050
+ else if (handler.isTabBar(handler.pathname)) {
1051
+ if (handler.isSamePage(currentPage))
1052
+ return;
1053
+ if (handler.isTabBar(currentPage.path)) {
1054
+ handler.hide(currentPage);
1055
+ stacks.pushTab(currentPage.path.split('?')[0]);
1056
+ }
1057
+ else if (stacks.length > 0) {
1058
+ const firstIns = stacks.getItem(0);
1059
+ if (handler.isTabBar(firstIns.path)) {
1060
+ handler.unload(currentPage, stacks.length - 1);
1061
+ stacks.pushTab(firstIns.path.split('?')[0]);
1062
+ }
1063
+ else {
1064
+ handler.unload(currentPage, stacks.length);
1065
+ }
1066
+ }
1067
+ if (cacheTabs[handler.pathname]) {
1068
+ stacks.popTab(handler.pathname);
1069
+ return handler.show(stacks.getItem(0), pageConfig, 0);
1070
+ }
1071
+ shouldLoad = true;
1072
+ }
1073
+ else if (action === 'POP') {
1006
1074
  // NOTE: 浏览器事件退后多次时,该事件只会被触发一次
1007
1075
  const prevIndex = stacks.getPrevIndex(pathname);
1008
1076
  const delta = stacks.getDelta(pathname);
@@ -1017,37 +1085,34 @@ function createRouter(app, config, framework) {
1017
1085
  }
1018
1086
  }
1019
1087
  }
1020
- else {
1021
- if (handler.isTabBar) {
1022
- if (handler.isSamePage(currentPage))
1023
- return;
1024
- const prevIndex = stacks.getPrevIndex(pathname, 0);
1025
- handler.hide(currentPage);
1026
- if (prevIndex > -1) {
1027
- // NOTE: tabbar 页且之前出现过,直接复用
1028
- return handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
1029
- }
1030
- }
1031
- else if (action === 'REPLACE') {
1032
- const delta = stacks.getDelta(pathname);
1033
- // NOTE: 页面路由记录并不会清空,只是移除掉缓存的 stack 以及页面
1034
- handler.unload(currentPage, delta);
1035
- }
1036
- else if (action === 'PUSH') {
1037
- handler.hide(currentPage);
1038
- }
1088
+ else if (action === 'REPLACE') {
1089
+ const delta = stacks.getDelta(pathname);
1090
+ // NOTE: 页面路由记录并不会清空,只是移除掉缓存的 stack 以及页面
1091
+ handler.unload(currentPage, delta);
1092
+ shouldLoad = true;
1093
+ }
1094
+ else if (action === 'PUSH') {
1095
+ handler.hide(currentPage);
1039
1096
  shouldLoad = true;
1040
1097
  }
1041
1098
  if (shouldLoad || stacks.length < 1) {
1042
- const el = (_g = element.default) !== null && _g !== void 0 ? _g : element;
1099
+ const el = (_h = element.default) !== null && _h !== void 0 ? _h : element;
1043
1100
  const loadConfig = Object.assign({}, pageConfig);
1044
1101
  const stacksIndex = stacks.length;
1045
1102
  delete loadConfig['path'];
1046
1103
  delete loadConfig['load'];
1047
- const page = runtime.createPageConfig(enablePullDownRefresh ? runtime.hooks.call('createPullDownComponent', el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + runtime.stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
1104
+ let pageStampId = '';
1105
+ if (launchStampId) {
1106
+ pageStampId = launchStampId;
1107
+ launchStampId = '';
1108
+ }
1109
+ else {
1110
+ pageStampId = createStampId();
1111
+ }
1112
+ const page = runtime.createPageConfig(enablePullDownRefresh ? runtime.hooks.call('createPullDownComponent', el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + runtime.stringify(handler.getQuery(pageStampId)), {}, loadConfig);
1048
1113
  if (params)
1049
1114
  page.options = params;
1050
- return handler.load(page, pageConfig, stacksIndex);
1115
+ return handler.load(page, pageConfig, pageStampId, stacksIndex);
1051
1116
  }
1052
1117
  });
1053
1118
  const routePath = addLeadingSlash(stripBasename(exports.history.location.pathname, handler.basename));