@tarojs/router 4.0.0-canary.9 → 4.0.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/index.cjs.js CHANGED
@@ -4,10 +4,226 @@ var components = require('@tarojs/components/dist/components');
4
4
  var taro = require('@tarojs/taro');
5
5
  var tslib = require('tslib');
6
6
  var runtime = require('@tarojs/runtime');
7
+ var shared = require('@tarojs/shared');
7
8
  var history = require('history');
8
9
  var queryString = require('query-string');
9
10
  var UniversalRouter = require('universal-router');
10
11
 
12
+ /**
13
+ * 插入页面动画需要的样式
14
+ */
15
+ function loadAnimateStyle(ms = 300) {
16
+ const css = `
17
+ body {
18
+ /* 防止 iOS 页面滚动 */
19
+ overflow: hidden;
20
+ }
21
+ .taro_router > .taro_page {
22
+ position: absolute;
23
+ left: 0;
24
+ top: 0;
25
+ width: 100%;
26
+ height: 100%;
27
+ background-color: #fff;
28
+ transform: translate(100%, 0);
29
+ transition: transform ${ms}ms;
30
+ z-index: 0;
31
+ }
32
+
33
+ .taro_router > .taro_page.taro_tabbar_page,
34
+ .taro_router > .taro_page.taro_page_show.taro_page_stationed {
35
+ transform: none;
36
+ transition: none;
37
+ }
38
+
39
+ .taro_router > .taro_page.taro_page_show {
40
+ transform: translate(0, 0);
41
+ }
42
+ `;
43
+ addStyle(css);
44
+ }
45
+ /**
46
+ * 插入路由相关样式
47
+ */
48
+ function loadRouterStyle(enableTabBar, enableWindowScroll, enhanceAnimation) {
49
+ const css = `
50
+ .taro_router {
51
+ position: relative;
52
+ width: 100%;
53
+ height: 100%;
54
+ }
55
+
56
+ .taro_page {
57
+ width: 100%;
58
+ height: 100%;
59
+ ${enableWindowScroll ? '' : `
60
+ overflow-x: hidden;
61
+ overflow-y: scroll;
62
+ max-height: 100vh;
63
+ `}
64
+ }
65
+ ${enableTabBar ? `
66
+ .taro-tabbar__container > .taro-tabbar__panel {
67
+ overflow: hidden;
68
+ }
69
+
70
+ .taro-tabbar__container > .taro-tabbar__panel > .taro_page.taro_tabbar_page {
71
+ max-height: calc(100vh - var(--taro-tabbar-height) - constant(safe-area-inset-bottom));
72
+ max-height: calc(100vh - var(--taro-tabbar-height) - env(safe-area-inset-bottom));
73
+ }
74
+
75
+ ` : ''}
76
+ ${enhanceAnimation
77
+ ? `.taro_page_shade:has(+.taro_page_stationed),
78
+ .taro_page_shade.taro_tabbar_page,
79
+ .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) {
80
+ display: none;
81
+ }` : ` .taro_page_shade,
82
+ .taro_router > .taro_page.taro_page_show.taro_page_stationed:not(.taro_page_shade):not(.taro_tabbar_page):not(:last-child) {
83
+ display: none;
84
+ }`}
85
+ `;
86
+ addStyle(css);
87
+ }
88
+ /**
89
+ * 插入导航栏相关的样式
90
+ */
91
+ function loadNavigationBarStyle() {
92
+ const css = `
93
+ .taro-navigation-bar-show {
94
+ display: flex;
95
+ background: white;
96
+ position: sticky;
97
+ z-index: 500;
98
+ top: 0;
99
+ padding-bottom: 8px;
100
+ padding-top: calc(env(safe-area-inset-top) + 8px);
101
+ justify-content: center;
102
+ align-items: center;
103
+ }
104
+
105
+ .taro-navigation-bar-hide {
106
+ display: none;
107
+ }
108
+
109
+ .taro-navigation-bar-title-wrap {
110
+ display: flex;
111
+ height: 24px;
112
+ }
113
+
114
+ .taro-navigation-bar-title-wrap > .taro-navigation-bar-loading {
115
+ display: none;
116
+ animation: loading 2s linear infinite;
117
+ }
118
+
119
+ .taro-navigation-bar-title-wrap .taro-navigation-bar-loading.taro-navigation-bar-loading-show {
120
+ display: flex;
121
+ }
122
+
123
+ .taro-navigation-bar-title-wrap > .taro-navigation-bar-title {
124
+ font-size: 24px;
125
+ height: 24px;
126
+ line-height: 24px;
127
+ max-width: 100px;
128
+ white-space: nowrap;
129
+ overflow: hidden;
130
+ line-height: 24px;
131
+ text-overflow: ellipsis;
132
+ }
133
+
134
+ @keyframes loading {
135
+ from {
136
+ transform: rotate(0deg);
137
+ }
138
+ to {
139
+ transform: rotate(360deg);
140
+ }
141
+ }
142
+
143
+ @keyframes loading {
144
+ from {
145
+ transform: rotate(0deg);
146
+ }
147
+ to {
148
+ transform: rotate(360deg);
149
+ }
150
+ }
151
+
152
+ .taro-navigation-bar-no-icon > .taro-navigation-bar-home {
153
+ display: none;
154
+ }
155
+
156
+ .taro-navigation-bar-no-icon > .taro-navigation-bar-back {
157
+ display: none;
158
+ }
159
+
160
+ .taro-navigation-bar-home-icon > .taro-navigation-bar-home {
161
+ display: flex;
162
+ left: 8px;
163
+ position: absolute;
164
+ width: 24px;
165
+ height: 24px;
166
+ }
167
+
168
+ .taro-navigation-bar-back-icon > .taro-navigation-bar-back {
169
+ display: flex;
170
+ left: 8px;
171
+ position: absolute;
172
+ width: 24px;
173
+ height: 24px;
174
+ }
175
+ `;
176
+ addStyle(css);
177
+ }
178
+ function addStyle(css) {
179
+ if (!css)
180
+ return;
181
+ const style = document.createElement('style');
182
+ style.innerHTML = css;
183
+ document.getElementsByTagName('head')[0].appendChild(style);
184
+ }
185
+
186
+ const home_svg_str = `
187
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
188
+ <path d="M23.8899 12.2737C23.8232 12.3584 23.7237 12.3997 23.6198 12.3974H20.7994V23.5996C20.7994 23.8194 20.6213 24 20.4001 24H14.7994C14.5791 24 14.4002 23.8194 14.4002 23.5996V15.6H9.59963V23.5996C9.59963 23.8194 9.42075 24 9.20033 24H3.59968C3.48981 24 3.38964 23.954 3.31764 23.8811C3.24495 23.8091 3.2004 23.7087 3.2004 23.5996V12.3975H0.398546V12.3967C0.296893 12.396 0.194446 12.3544 0.11579 12.2738C-0.0371146 12.114 -0.0400714 11.864 0.11579 11.7076L11.7201 0.117284C11.8767 -0.0390948 12.1298 -0.0390948 12.2863 0.117284L23.8899 11.7076C24.0465 11.864 24.0265 12.0995 23.8899 12.2737ZM12.0029 0.964625L1.37086 11.5854L3.59968 11.5839V11.5999C3.65537 11.5999 3.70804 11.611 3.75557 11.6307C3.89952 11.692 4.00046 11.8339 4.00046 11.9996V23.1991H8.79955V15.2003C8.79955 14.9789 8.97917 14.8002 9.20033 14.8002H14.7995C15.0207 14.8002 15.2003 14.9789 15.2003 15.2003V23.1991H20.0001V11.9996C20.0001 11.8339 20.1003 11.692 20.2443 11.6307C20.2918 11.611 20.3453 11.5999 20.4001 11.5999V11.5713L22.6193 11.5691L12.0029 0.964625Z" fill="currentColor"/>
189
+ </svg>
190
+ `;
191
+ const back_svg_str = `
192
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
193
+ <path d="M17.8206 22.9016L7.45515 11.8756L17.8206 1.09845C18.0598 0.849741 18.0598 0.435233 17.8206 0.186528C17.5814 -0.0621762 17.1827 -0.0621762 16.9435 0.186528L6.1794 11.4611C5.9402 11.7098 5.9402 12.1244 6.1794 12.3731L16.9435 23.8135C17.1827 24.0622 17.5814 24.0622 17.8206 23.8135C18.0598 23.5648 18.0598 23.1503 17.8206 22.9016Z" fill="currentColor"/>
194
+ </svg>
195
+ `;
196
+ const loading_svg_str = `
197
+ <svg t="1709608074670" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4741" width="24" height="24"><path d="M256 529.066667H85.333333a17.066667 17.066667 0 1 1 0-34.133334h170.666667a17.066667 17.066667 0 0 1 0 34.133334z" opacity=".278" p-id="4742"></path><path d="M99.84 640a17.066667 17.066667 0 0 1-4.437333-33.553067l164.693333-44.373333a17.066667 17.066667 0 1 1 8.891733 32.9728l-164.693333 44.373333a17.544533 17.544533 0 0 1-4.4544 0.580267z" opacity=".322" p-id="4743"></path><path d="M264.533333 462.523733a16.896 16.896 0 0 1-4.369066-0.580266l-164.693334-43.52a17.0496 17.0496 0 1 1 8.721067-32.989867l164.693333 43.52a17.066667 17.066667 0 1 1-4.352 33.570133z" opacity=".239" p-id="4744"></path><path d="M384.017067 307.2a17.032533 17.032533 0 0 1-14.7968-8.533333l-85.333334-147.626667a17.066667 17.066667 0 0 1 29.559467-17.083733l85.333333 147.626666A17.066667 17.066667 0 0 1 384.017067 307.2z" opacity=".122" p-id="4745"></path><path d="M639.982933 307.2a17.0496 17.0496 0 0 1-14.762666-25.6l85.333333-147.626667a17.066667 17.066667 0 1 1 29.559467 17.066667l-85.333334 147.626667a17.032533 17.032533 0 0 1-14.7968 8.533333z" opacity=".922" p-id="4746"></path><path d="M692.906667 347.306667a17.066667 17.066667 0 0 1-12.117334-29.098667l120.337067-121.173333a17.066667 17.066667 0 1 1 24.234667 24.046933l-120.337067 121.173333a17.1008 17.1008 0 0 1-12.117333 5.051734z" opacity=".878" p-id="4747"></path><path d="M733.883733 401.066667a17.066667 17.066667 0 0 1-8.5504-31.8464l147.626667-85.333334a17.0496 17.0496 0 1 1 17.066667 29.5424l-147.626667 85.333334a16.776533 16.776533 0 0 1-8.516267 2.304z" opacity=".839" p-id="4748"></path><path d="M512 273.066667a17.066667 17.066667 0 0 1-17.066667-17.066667V85.333333a17.066667 17.066667 0 0 1 34.133334 0v170.666667a17.066667 17.066667 0 0 1-17.066667 17.066667z" p-id="4749"></path><path d="M578.577067 281.6a17.066667 17.066667 0 0 1-16.520534-21.418667l43.52-164.693333a17.066667 17.066667 0 0 1 33.006934 8.721067l-43.52 164.693333a17.066667 17.066667 0 0 1-16.4864 12.6976z" opacity=".961" p-id="4750"></path><path d="M445.44 282.453333a17.066667 17.066667 0 0 1-16.469333-12.629333l-44.373334-164.693333a17.066667 17.066667 0 0 1 32.955734-8.891734l44.373333 164.693334a17.066667 17.066667 0 0 1-16.4864 21.521066z" opacity=".078" p-id="4751"></path><path d="M924.177067 640c-1.4848 0-2.9696-0.187733-4.4544-0.580267l-164.693334-44.373333a17.066667 17.066667 0 0 1 8.874667-32.9728l164.693333 44.373333a17.066667 17.066667 0 0 1-4.420266 33.553067z" opacity=".722" p-id="4752"></path><path d="M881.476267 742.4a17.015467 17.015467 0 0 1-8.482134-2.269867l-148.48-85.333333a17.0496 17.0496 0 1 1 16.9984-29.5936l148.48 85.333333a17.0496 17.0496 0 0 1-8.516266 31.863467z" opacity=".678" p-id="4753"></path><path d="M813.226667 830.293333a17.015467 17.015467 0 0 1-12.066134-5.000533l-120.337066-120.337067a17.0496 17.0496 0 1 1 24.132266-24.132266l120.337067 120.337066a17.0496 17.0496 0 0 1-12.066133 29.1328z" opacity=".639" p-id="4754"></path><path d="M938.666667 529.066667H768a17.066667 17.066667 0 1 1 0-34.133334h170.666667a17.066667 17.066667 0 1 1 0 34.133334z" opacity=".761" p-id="4755"></path><path d="M401.066667 941.226667a17.066667 17.066667 0 0 1-16.4864-21.504l44.373333-164.693334a17.066667 17.066667 0 1 1 32.955733 8.874667l-44.373333 164.693333a17.066667 17.066667 0 0 1-16.469333 12.629334z" opacity=".478" p-id="4756"></path><path d="M298.6496 898.56a17.066667 17.066667 0 0 1-14.779733-25.565867l85.333333-148.48a17.083733 17.083733 0 0 1 29.5936 16.9984l-85.333333 148.48a17.032533 17.032533 0 0 1-14.813867 8.567467z" opacity=".439" p-id="4757"></path><path d="M512 955.733333a17.066667 17.066667 0 0 1-17.066667-17.066666V768a17.066667 17.066667 0 1 1 34.133334 0v170.666667a17.066667 17.066667 0 0 1-17.066667 17.066666z" opacity=".522" p-id="4758"></path><path d="M725.3504 898.56a17.032533 17.032533 0 0 1-14.7968-8.533333l-85.333333-147.626667a17.066667 17.066667 0 0 1 29.559466-17.066667l85.333334 147.626667a17.066667 17.066667 0 0 1-14.762667 25.6z" opacity=".6" p-id="4759"></path><path d="M622.062933 942.08c-7.509333 0-14.421333-5.0176-16.469333-12.629333l-44.3904-164.693334a17.066667 17.066667 0 1 1 32.9728-8.874666l44.3904 164.693333a17.066667 17.066667 0 0 1-16.503467 21.504z" opacity=".561" p-id="4760"></path><path d="M759.4496 463.36a17.083733 17.083733 0 0 1-4.420267-33.553067l164.693334-44.373333a17.066667 17.066667 0 0 1 8.874666 32.955733l-164.693333 44.373334a16.657067 16.657067 0 0 1-4.4544 0.597333z" opacity=".702" p-id="4761"></path><path d="M330.24 347.306667a17.015467 17.015467 0 0 1-12.066133-5.000534l-120.32-120.32a17.0496 17.0496 0 1 1 24.132266-24.132266l120.32 120.32a17.0496 17.0496 0 0 1-12.066133 29.1328z" opacity=".161" p-id="4762"></path><path d="M290.116267 401.066667a17.032533 17.032533 0 0 1-8.533334-2.286934l-147.626666-85.333333a17.066667 17.066667 0 1 1 17.083733-29.5424l147.626667 85.333333a17.066667 17.066667 0 0 1-8.5504 31.829334z" opacity=".2" p-id="4763"></path><path d="M142.523733 742.4a17.066667 17.066667 0 0 1-8.567466-31.8464l147.626666-85.333333a17.066667 17.066667 0 1 1 17.083734 29.559466l-147.626667 85.333334a16.930133 16.930133 0 0 1-8.516267 2.286933z" opacity=".361" p-id="4764"></path><path d="M209.92 830.293333a17.066667 17.066667 0 0 1-12.117333-29.098666l120.32-121.173334a17.066667 17.066667 0 0 1 24.2176 24.029867l-120.32 121.1904a16.896 16.896 0 0 1-12.100267 5.051733z" opacity=".4" p-id="4765"></path></svg>
198
+ `;
199
+ function initNavigationBar(config, container) {
200
+ if (config.router.mode === 'multi')
201
+ return;
202
+ const navigationBar = document.createElement('div');
203
+ navigationBar.classList.add('taro-navigation-bar-no-icon');
204
+ const navigationBarBackBtn = document.createElement('div');
205
+ navigationBarBackBtn.classList.add('taro-navigation-bar-back');
206
+ const navigationBarHomeBtn = document.createElement('div');
207
+ navigationBarHomeBtn.classList.add('taro-navigation-bar-home');
208
+ navigationBarBackBtn.innerHTML = back_svg_str;
209
+ navigationBarHomeBtn.innerHTML = home_svg_str;
210
+ const navigationBarTitleWrap = document.createElement('div');
211
+ navigationBarTitleWrap.classList.add('taro-navigation-bar-title-wrap');
212
+ const navigationBarLoading = document.createElement('div');
213
+ navigationBarLoading.classList.add('taro-navigation-bar-loading');
214
+ navigationBarLoading.innerHTML = loading_svg_str;
215
+ const navigationBarTitle = document.createElement('div');
216
+ navigationBarTitle.classList.add('taro-navigation-bar-title');
217
+ navigationBarTitleWrap.appendChild(navigationBarLoading);
218
+ navigationBarTitleWrap.appendChild(navigationBarTitle);
219
+ navigationBar.appendChild(navigationBarHomeBtn);
220
+ navigationBar.appendChild(navigationBarBackBtn);
221
+ navigationBar.appendChild(navigationBarTitleWrap);
222
+ navigationBar.id = 'taro-navigation-bar';
223
+ container.prepend(navigationBar);
224
+ loadNavigationBarStyle();
225
+ }
226
+
11
227
  function initTabbar(config, history) {
12
228
  if (config.tabBar == null || config.tabBar.custom) {
13
229
  return;
@@ -53,8 +269,9 @@ class RouterConfig {
53
269
  return this.router.mode || 'hash';
54
270
  }
55
271
  static get customRoutes() { return this.router.customRoutes || {}; }
272
+ // 这个方法不考虑 basename 和 customRoutes,只判断原始的 url 是否在 pages 中
56
273
  static isPage(url = '') {
57
- return this.pages.findIndex(e => prependBasename(e) === url) !== -1;
274
+ return this.pages.findIndex(e => runtime.addLeadingSlash(e) === url) !== -1;
58
275
  }
59
276
  }
60
277
 
@@ -81,14 +298,14 @@ class MpaHistory {
81
298
  }
82
299
  parseUrl(to) {
83
300
  let url = to.pathname || '';
84
- if (RouterConfig.isPage(url)) {
301
+ if (RouterConfig.isPage(runtime.addLeadingSlash(url))) {
85
302
  url += '.html';
86
303
  }
87
304
  if (to.search) {
88
305
  url += `?${to.search}`;
89
306
  }
90
307
  if (to.hash) {
91
- url += `#${to.hash}`;
308
+ url += to.hash.startsWith('#') ? to.hash : `#${to.hash}`;
92
309
  }
93
310
  return url;
94
311
  }
@@ -245,26 +462,32 @@ class Stacks {
245
462
  }
246
463
  const stacks = new Stacks();
247
464
 
248
- let preTitle = document.title;
249
- let isLoadDdEntry = false;
250
465
  const isWeixin = () => !!navigator.userAgent.match(/\bMicroMessenger\b/ig);
251
466
  const isDingTalk = () => !!navigator.userAgent.match(/\bDingTalk\b/ig);
252
- function setTitle(title) {
253
- return tslib.__awaiter(this, void 0, void 0, function* () {
254
- if (preTitle === title)
255
- return title;
256
- document.title = title;
257
- preTitle = title;
258
- if (process.env.SUPPORT_DINGTALK_NAVIGATE !== 'disabled' && isDingTalk()) {
259
- if (!isLoadDdEntry) {
260
- isLoadDdEntry = true;
261
- require('dingtalk-jsapi/platform');
262
- }
263
- const setDingTitle = require('dingtalk-jsapi/api/biz/navigation/setTitle').default;
264
- setDingTitle({ title });
467
+ let preTitle = document.title;
468
+ let isLoadDdEntry = false;
469
+ function setMpaTitle(title) {
470
+ if (preTitle === title)
471
+ return;
472
+ document.title = title;
473
+ preTitle = title;
474
+ if (process.env.SUPPORT_DINGTALK_NAVIGATE !== 'disabled' && isDingTalk()) {
475
+ if (!isLoadDdEntry) {
476
+ isLoadDdEntry = true;
477
+ require('dingtalk-jsapi/platform');
265
478
  }
266
- return title;
267
- });
479
+ const setDingTitle = require('dingtalk-jsapi/api/biz/navigation/setTitle').default;
480
+ setDingTitle({ title });
481
+ }
482
+ }
483
+ function setTitle(title) {
484
+ runtime.eventCenter.trigger('__taroH5SetNavigationBarTitle', title);
485
+ }
486
+ function setNavigationBarStyle(option) {
487
+ runtime.eventCenter.trigger('__taroH5setNavigationBarColor', option);
488
+ }
489
+ function setNavigationBarLoading(loading) {
490
+ runtime.eventCenter.trigger('__taroH5setNavigationBarLoading', loading);
268
491
  }
269
492
 
270
493
  class RoutesAlias {
@@ -308,6 +531,7 @@ class RoutesAlias {
308
531
  }
309
532
  const routesAlias = new RoutesAlias();
310
533
 
534
+ const routeEvtChannel = shared.EventChannel.routeChannel;
311
535
  function processNavigateUrl(option) {
312
536
  var _a;
313
537
  const pathPieces = history.parsePath(option.url);
@@ -316,13 +540,14 @@ function processNavigateUrl(option) {
316
540
  const parts = routesAlias.getOrigin(exports.history.location.pathname).split('/');
317
541
  parts.pop();
318
542
  pathPieces.pathname.split('/').forEach((item) => {
319
- if (item === '.') {
543
+ if (item === '.')
320
544
  return;
321
- }
322
545
  item === '..' ? parts.pop() : parts.push(item);
323
546
  });
324
547
  pathPieces.pathname = parts.join('/');
325
548
  }
549
+ // 确保是 / 开头的路径
550
+ pathPieces.pathname = runtime.addLeadingSlash(pathPieces.pathname);
326
551
  // 处理自定义路由
327
552
  pathPieces.pathname = routesAlias.getAlias(runtime.addLeadingSlash(pathPieces.pathname));
328
553
  // 处理 basename
@@ -339,6 +564,10 @@ function navigate(option, method) {
339
564
  const { success, complete, fail } = option;
340
565
  const unListen = exports.history.listen(() => {
341
566
  const res = { errMsg: `${method}:ok` };
567
+ if (method === 'navigateTo') {
568
+ res.eventChannel = routeEvtChannel;
569
+ routeEvtChannel.addEvents(option.events);
570
+ }
342
571
  success === null || success === void 0 ? void 0 : success(res);
343
572
  complete === null || complete === void 0 ? void 0 : complete(res);
344
573
  resolve(res);
@@ -349,6 +578,7 @@ function navigate(option, method) {
349
578
  const pathPieces = processNavigateUrl(option);
350
579
  const state = { timestamp: Date.now() };
351
580
  if (method === 'navigateTo') {
581
+ // Note: 由于 spa 会针对弱网情况下,短时间内多次跳转同一个页面跳转加了锁,后续如果有用户反馈返回无效,那可能是这个问题
352
582
  exports.history.push(pathPieces, state);
353
583
  }
354
584
  else if (method === 'redirectTo' || method === 'switchTab') {
@@ -373,7 +603,12 @@ function navigate(option, method) {
373
603
  const res = { errMsg: `${method}:fail ${error.message || error}` };
374
604
  fail === null || fail === void 0 ? void 0 : fail(res);
375
605
  complete === null || complete === void 0 ? void 0 : complete(res);
376
- reject(res);
606
+ if (fail || complete) {
607
+ return resolve(res);
608
+ }
609
+ else {
610
+ return reject(res);
611
+ }
377
612
  }
378
613
  });
379
614
  });
@@ -408,12 +643,18 @@ let pageResizeFn;
408
643
  function bindPageResize(page) {
409
644
  pageResizeFn && window.removeEventListener('resize', pageResizeFn);
410
645
  pageResizeFn = function () {
411
- page.onResize && page.onResize({
412
- size: {
413
- windowHeight: window.innerHeight,
414
- windowWidth: window.innerWidth
415
- }
416
- });
646
+ if (page.onResize) {
647
+ const mediaQuery = window.matchMedia('(orientation: portrait)');
648
+ page.onResize({
649
+ deviceOrientation: mediaQuery.matches ? 'portrait' : 'landscape',
650
+ size: {
651
+ windowHeight: window.innerHeight,
652
+ windowWidth: window.innerWidth,
653
+ screenHeight: window.screen.height,
654
+ screenWidth: window.screen.width,
655
+ }
656
+ });
657
+ }
417
658
  };
418
659
  window.addEventListener('resize', pageResizeFn, false);
419
660
  }
@@ -452,80 +693,6 @@ function getOffset() {
452
693
  }
453
694
  }
454
695
 
455
- /**
456
- * 插入页面动画需要的样式
457
- */
458
- function loadAnimateStyle(ms = 300) {
459
- const css = `
460
- .taro_router > .taro_page {
461
- position: absolute;
462
- left: 0;
463
- top: 0;
464
- width: 100%;
465
- height: 100%;
466
- background-color: #fff;
467
- transform: translate(100%, 0);
468
- transition: transform ${ms}ms;
469
- z-index: 0;
470
- }
471
-
472
- .taro_router > .taro_page.taro_tabbar_page,
473
- .taro_router > .taro_page.taro_page_show.taro_page_stationed {
474
- transform: none;
475
- }
476
-
477
- .taro_router > .taro_page.taro_page_show {
478
- transform: translate(0, 0);
479
- }
480
- `;
481
- addStyle(css);
482
- }
483
- /**
484
- * 插入路由相关样式
485
- */
486
- function loadRouterStyle(enableTabBar, enableWindowScroll) {
487
- const css = `
488
- .taro_router {
489
- position: relative;
490
- width: 100%;
491
- height: 100%;
492
- }
493
-
494
- .taro_page {
495
- width: 100%;
496
- height: 100%;
497
- ${enableWindowScroll ? '' : `
498
- overflow-x: hidden;
499
- overflow-y: scroll;
500
- max-height: 100vh;
501
- `}
502
- }
503
- ${enableTabBar ? `
504
- .taro-tabbar__container > .taro-tabbar__panel {
505
- overflow: hidden;
506
- }
507
-
508
- .taro-tabbar__container > .taro-tabbar__panel > .taro_page.taro_tabbar_page {
509
- max-height: calc(100vh - var(--taro-tabbar-height) - constant(safe-area-inset-bottom));
510
- max-height: calc(100vh - var(--taro-tabbar-height) - env(safe-area-inset-bottom));
511
- }
512
-
513
- ` : ''}
514
- .taro_page_shade,
515
- .taro_router > .taro_page.taro_page_show.taro_page_stationed:not(.taro_page_shade):not(.taro_tabbar_page):not(:last-child) {
516
- display: none;
517
- }
518
- `;
519
- addStyle(css);
520
- }
521
- function addStyle(css) {
522
- if (!css)
523
- return;
524
- const style = document.createElement('style');
525
- style.innerHTML = css;
526
- document.getElementsByTagName('head')[0].appendChild(style);
527
- }
528
-
529
696
  /* eslint-disable dot-notation */
530
697
  class MultiPageHandler {
531
698
  constructor(config, history) {
@@ -681,15 +848,16 @@ const launchStampId$1 = createStampId$1();
681
848
  * - 不支持路由动画
682
849
  */
683
850
  function createMultiRouter(history, app, config, framework) {
684
- var _a, _b, _c, _d, _e, _f;
685
851
  return tslib.__awaiter(this, void 0, void 0, function* () {
852
+ var _a, _b, _c, _d, _e, _f;
686
853
  if (typeof app.onUnhandledRejection === 'function') {
687
854
  window.addEventListener('unhandledrejection', app.onUnhandledRejection);
688
855
  }
856
+ runtime.eventCenter.on('__taroH5SetNavigationBarTitle', setMpaTitle);
689
857
  RouterConfig.config = config;
690
858
  const handler = new MultiPageHandler(config, history);
691
859
  const launchParam = {
692
- path: config.pageName,
860
+ path: config.pageName, // 多页面模式没新开一个页面相当于重启,所以直接使用当前页面路径
693
861
  query: handler.getQuery(launchStampId$1),
694
862
  scene: 0,
695
863
  shareTicket: '',
@@ -719,7 +887,7 @@ function createMultiRouter(history, app, config, framework) {
719
887
  return;
720
888
  let enablePullDownRefresh = ((_c = config === null || config === void 0 ? void 0 : config.window) === null || _c === void 0 ? void 0 : _c.enablePullDownRefresh) || false;
721
889
  if (pageConfig) {
722
- setTitle((_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title);
890
+ setMpaTitle((_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title);
723
891
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
724
892
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
725
893
  }
@@ -731,9 +899,268 @@ function createMultiRouter(history, app, config, framework) {
731
899
  const page = runtime.createPageConfig(enablePullDownRefresh ? runtime.hooks.call('createPullDownComponent', el, pathName, framework, handler.PullDownRefresh) : el, pathName + runtime.stringify(launchParam), {}, loadConfig);
732
900
  handler.load(page, pageConfig);
733
901
  (_f = app.onShow) === null || _f === void 0 ? void 0 : _f.call(app, launchParam);
902
+ window.addEventListener('visibilitychange', () => {
903
+ var _a, _b, _c;
904
+ const currentPath = ((_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path) || '';
905
+ const path = currentPath.substring(0, currentPath.indexOf('?'));
906
+ const param = {};
907
+ // app的 onShow/onHide 生命周期的路径信息为当前页面的路径
908
+ Object.assign(param, launchParam, { path });
909
+ if (document.visibilityState === 'visible') {
910
+ (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, param);
911
+ }
912
+ else {
913
+ (_c = app.onHide) === null || _c === void 0 ? void 0 : _c.call(app, param);
914
+ }
915
+ });
734
916
  });
735
917
  }
736
918
 
919
+ class NavigationBarHandler {
920
+ constructor(pageContext) {
921
+ this.isLoadDdEntry = false;
922
+ this.cache = {};
923
+ this.pageContext = pageContext;
924
+ this.init();
925
+ runtime.eventCenter.on('__taroH5SetNavigationBarTitle', (title) => {
926
+ this.setTitle(title);
927
+ });
928
+ runtime.eventCenter.on('__taroH5setNavigationBarLoading', (loading) => {
929
+ this.setNavigationLoading(loading);
930
+ });
931
+ runtime.eventCenter.on('__taroH5setNavigationBarColor', ({ backgroundColor, frontColor }) => {
932
+ if (typeof backgroundColor === 'string')
933
+ this.setNavigationBarBackground(backgroundColor);
934
+ if (typeof frontColor === 'string')
935
+ this.setNavigationBarTextStyle(frontColor);
936
+ });
937
+ }
938
+ toHomeFn() {
939
+ reLaunch({ url: this.pageContext.originHomePage });
940
+ }
941
+ backFn() {
942
+ navigateBack();
943
+ }
944
+ get homeBtnElement() {
945
+ var _a;
946
+ if (!this.navigationBarElement)
947
+ return null;
948
+ return (_a = this.navigationBarElement.getElementsByClassName('taro-navigation-bar-home')) === null || _a === void 0 ? void 0 : _a[0];
949
+ }
950
+ get backBtnElement() {
951
+ var _a;
952
+ if (!this.navigationBarElement)
953
+ return null;
954
+ return (_a = this.navigationBarElement.getElementsByClassName('taro-navigation-bar-back')) === null || _a === void 0 ? void 0 : _a[0];
955
+ }
956
+ get titleElement() {
957
+ var _a;
958
+ if (!this.navigationBarElement)
959
+ return null;
960
+ return (_a = this.navigationBarElement.getElementsByClassName('taro-navigation-bar-title')) === null || _a === void 0 ? void 0 : _a[0];
961
+ }
962
+ get loadingElement() {
963
+ if (!this.navigationBarElement)
964
+ return null;
965
+ return this.navigationBarElement.getElementsByClassName('taro-navigation-bar-loading')[0];
966
+ }
967
+ init() {
968
+ var _a, _b;
969
+ this.setNavigationBarElement();
970
+ if (!this.navigationBarElement)
971
+ return;
972
+ (_a = this.homeBtnElement) === null || _a === void 0 ? void 0 : _a.addEventListener('click', this.toHomeFn.bind(this));
973
+ (_b = this.backBtnElement) === null || _b === void 0 ? void 0 : _b.addEventListener('click', this.backFn.bind(this));
974
+ }
975
+ setNavigationBarElement() {
976
+ this.navigationBarElement = document.getElementById('taro-navigation-bar');
977
+ }
978
+ load() {
979
+ this.setCacheValue();
980
+ this.setTitle();
981
+ this.setNavigationBarVisible();
982
+ this.setFnBtnState();
983
+ this.setNavigationBarBackground();
984
+ this.setNavigationBarTextStyle();
985
+ this.setNavigationLoading();
986
+ }
987
+ setCacheValue() {
988
+ const currentPage = this.pageContext.originPathname;
989
+ if (typeof this.cache[currentPage] !== 'object') {
990
+ this.cache[currentPage] = {};
991
+ }
992
+ }
993
+ setFnBtnState() {
994
+ const currentRouter = this.pageContext.currentPage;
995
+ if (this.pageContext.isTabBar(currentRouter) || this.pageContext.homePage === currentRouter) {
996
+ this.fnBtnToggleToNone();
997
+ }
998
+ else if (stacks.length > 1) {
999
+ this.fnBtnToggleToBack();
1000
+ }
1001
+ else {
1002
+ this.fnBtnToggleToHome();
1003
+ }
1004
+ }
1005
+ shiftLoadingState(show) {
1006
+ if (!this.loadingElement)
1007
+ return;
1008
+ if (show) {
1009
+ this.loadingElement.classList.add('taro-navigation-bar-loading-show');
1010
+ }
1011
+ else {
1012
+ this.loadingElement.classList.remove('taro-navigation-bar-loading-show');
1013
+ }
1014
+ }
1015
+ setNavigationLoading(show) {
1016
+ var _a;
1017
+ if (!this.navigationBarElement)
1018
+ return;
1019
+ const currentPage = this.pageContext.originPathname;
1020
+ let isShow;
1021
+ if (typeof show === 'boolean') {
1022
+ isShow = show;
1023
+ this.cache[currentPage] &&
1024
+ (this.cache[currentPage].loading = isShow);
1025
+ }
1026
+ else {
1027
+ const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.loading;
1028
+ if (typeof cacheValue === 'boolean') {
1029
+ isShow = cacheValue;
1030
+ }
1031
+ else {
1032
+ // 默认值为 false
1033
+ isShow = false;
1034
+ this.cache[currentPage] &&
1035
+ (this.cache[currentPage].loading = isShow);
1036
+ }
1037
+ }
1038
+ this.shiftLoadingState(isShow);
1039
+ }
1040
+ setNavigationBarBackground(backgroundColor) {
1041
+ var _a, _b, _c;
1042
+ if (!this.navigationBarElement)
1043
+ return;
1044
+ const currentPage = this.pageContext.originPathname;
1045
+ let color;
1046
+ if (typeof backgroundColor === 'string') {
1047
+ color = backgroundColor;
1048
+ this.cache[currentPage] &&
1049
+ (this.cache[currentPage].backgroundColor = color);
1050
+ }
1051
+ else {
1052
+ const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.backgroundColor;
1053
+ if (typeof cacheValue === 'string') {
1054
+ color = cacheValue;
1055
+ }
1056
+ else {
1057
+ color = ((_c = (_b = this.pageContext.config) === null || _b === void 0 ? void 0 : _b.window) === null || _c === void 0 ? void 0 : _c.navigationBarBackgroundColor) || '#000000';
1058
+ this.cache[currentPage] &&
1059
+ (this.cache[currentPage].backgroundColor = color);
1060
+ }
1061
+ }
1062
+ this.navigationBarElement.style.background = color;
1063
+ }
1064
+ setNavigationBarTextStyle(fontColor) {
1065
+ var _a, _b, _c;
1066
+ if (!this.navigationBarElement)
1067
+ return;
1068
+ const currentPage = this.pageContext.originPathname;
1069
+ let color;
1070
+ if (typeof fontColor === 'string') {
1071
+ color = fontColor;
1072
+ this.cache[currentPage] &&
1073
+ (this.cache[currentPage].fontColor = color);
1074
+ }
1075
+ else {
1076
+ const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.fontColor;
1077
+ if (typeof cacheValue === 'string') {
1078
+ color = cacheValue;
1079
+ }
1080
+ else {
1081
+ color = ((_c = (_b = this.pageContext.config) === null || _b === void 0 ? void 0 : _b.window) === null || _c === void 0 ? void 0 : _c.navigationBarTextStyle) || 'white';
1082
+ this.cache[currentPage] &&
1083
+ (this.cache[currentPage].fontColor = color);
1084
+ }
1085
+ }
1086
+ this.navigationBarElement.style.color = color;
1087
+ }
1088
+ setTitle(title) {
1089
+ var _a, _b, _c;
1090
+ const currentPage = this.pageContext.originPathname;
1091
+ let proceedTitle;
1092
+ if (typeof title === 'string') {
1093
+ proceedTitle = title;
1094
+ this.cache[currentPage] &&
1095
+ (this.cache[currentPage].title = proceedTitle);
1096
+ }
1097
+ else {
1098
+ const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.title;
1099
+ if (typeof cacheValue === 'string') {
1100
+ proceedTitle = cacheValue;
1101
+ }
1102
+ else {
1103
+ proceedTitle = (_c = (_b = this.pageContext.pageConfig) === null || _b === void 0 ? void 0 : _b.navigationBarTitleText) !== null && _c !== void 0 ? _c : document.title;
1104
+ this.cache[currentPage] &&
1105
+ (this.cache[currentPage].title = proceedTitle);
1106
+ }
1107
+ }
1108
+ if (process.env.SUPPORT_DINGTALK_NAVIGATE !== 'disabled' && isDingTalk()) {
1109
+ if (!this.isLoadDdEntry) {
1110
+ this.isLoadDdEntry = true;
1111
+ require('dingtalk-jsapi/platform');
1112
+ }
1113
+ const setDingTitle = require('dingtalk-jsapi/api/biz/navigation/setTitle').default;
1114
+ setDingTitle({ proceedTitle });
1115
+ }
1116
+ document.title = proceedTitle;
1117
+ if (!this.titleElement)
1118
+ return;
1119
+ this.titleElement.innerHTML = proceedTitle;
1120
+ }
1121
+ fnBtnToggleToHome() {
1122
+ if (!this.navigationBarElement)
1123
+ return;
1124
+ this.navigationBarElement.classList.add('taro-navigation-bar-home-icon');
1125
+ this.navigationBarElement.classList.remove('taro-navigation-bar-back-icon');
1126
+ }
1127
+ fnBtnToggleToBack() {
1128
+ if (!this.navigationBarElement)
1129
+ return;
1130
+ this.navigationBarElement.classList.remove('taro-navigation-bar-home-icon');
1131
+ this.navigationBarElement.classList.add('taro-navigation-bar-back-icon');
1132
+ }
1133
+ fnBtnToggleToNone() {
1134
+ if (!this.navigationBarElement)
1135
+ return;
1136
+ this.navigationBarElement.classList.remove('taro-navigation-bar-home-icon');
1137
+ this.navigationBarElement.classList.remove('taro-navigation-bar-back-icon');
1138
+ }
1139
+ setNavigationBarVisible(show) {
1140
+ var _a, _b;
1141
+ if (!this.navigationBarElement)
1142
+ return;
1143
+ let shouldShow;
1144
+ if (typeof show === 'boolean') {
1145
+ shouldShow = show;
1146
+ }
1147
+ else {
1148
+ shouldShow = (_a = this.pageContext.config.window) === null || _a === void 0 ? void 0 : _a.navigationStyle;
1149
+ if (typeof ((_b = this.pageContext.pageConfig) === null || _b === void 0 ? void 0 : _b.navigationStyle) === 'string') {
1150
+ shouldShow = this.pageContext.pageConfig.navigationStyle;
1151
+ }
1152
+ }
1153
+ if (shouldShow === 'default') {
1154
+ this.navigationBarElement.classList.add('taro-navigation-bar-show');
1155
+ this.navigationBarElement.classList.remove('taro-navigation-bar-hide');
1156
+ }
1157
+ else {
1158
+ this.navigationBarElement.classList.add('taro-navigation-bar-hide');
1159
+ this.navigationBarElement.classList.remove('taro-navigation-bar-show');
1160
+ }
1161
+ }
1162
+ }
1163
+
737
1164
  /* eslint-disable dot-notation */
738
1165
  class PageHandler {
739
1166
  constructor(config, history) {
@@ -741,7 +1168,9 @@ class PageHandler {
741
1168
  this.defaultAnimation = { duration: 300, delay: 50 };
742
1169
  this.config = config;
743
1170
  this.homePage = runtime.getHomePage(this.routes[0].path, this.basename, this.customRoutes, this.config.entryPagePath);
1171
+ this.originHomePage = this.config.entryPagePath || this.routes[0].path || this.basename;
744
1172
  this.mount();
1173
+ this.navigationBarHandler = new NavigationBarHandler(this);
745
1174
  }
746
1175
  get currentPage() {
747
1176
  const routePath = runtime.getCurrentPage(this.routerMode, this.basename);
@@ -773,14 +1202,14 @@ class PageHandler {
773
1202
  }
774
1203
  set pathname(p) { this.router.pathname = p; }
775
1204
  get pathname() { return this.router.pathname; }
1205
+ // Note: 把 pathname 转换为原始路径,主要是处理 customRoutes 和 basename
1206
+ get originPathname() { return routesAlias.getOrigin(runtime.addLeadingSlash(runtime.stripBasename(this.pathname, this.basename))); }
776
1207
  get basename() { return this.router.basename || ''; }
777
1208
  get pageConfig() {
778
- const routePath = runtime.addLeadingSlash(runtime.stripBasename(this.pathname, this.basename));
779
1209
  const homePage = runtime.addLeadingSlash(this.homePage);
780
1210
  return this.routes.find(r => {
781
- var _a;
782
1211
  const pagePath = runtime.addLeadingSlash(r.path);
783
- return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
1212
+ return [pagePath, homePage].includes(this.originPathname);
784
1213
  });
785
1214
  }
786
1215
  isTabBar(pathname) {
@@ -847,7 +1276,7 @@ class PageHandler {
847
1276
  this.pathname = exports.history.location.pathname;
848
1277
  // Note: 注入页面样式
849
1278
  this.animation && loadAnimateStyle(this.animationDuration);
850
- loadRouterStyle(this.tabBarList.length > 1, this.usingWindowScroll);
1279
+ loadRouterStyle(this.tabBarList.length > 1, this.usingWindowScroll, this.router.enhanceAnimation);
851
1280
  }
852
1281
  onReady(page, onLoad = true) {
853
1282
  var _a;
@@ -885,6 +1314,7 @@ class PageHandler {
885
1314
  this.isDefaultNavigationStyle() && pageEl.classList.add('taro_navigation_page');
886
1315
  this.addAnimation(pageEl, pageNo === 0);
887
1316
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
1317
+ this.navigationBarHandler.load();
888
1318
  this.bindPageEvents(page, pageConfig);
889
1319
  this.triggerRouterChange();
890
1320
  }
@@ -897,6 +1327,7 @@ class PageHandler {
897
1327
  this.isDefaultNavigationStyle() && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_navigation_page'));
898
1328
  this.addAnimation(pageEl, pageNo === 0);
899
1329
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
1330
+ this.navigationBarHandler.load();
900
1331
  this.onReady(page, true);
901
1332
  this.bindPageEvents(page, pageConfig);
902
1333
  this.triggerRouterChange();
@@ -951,6 +1382,7 @@ class PageHandler {
951
1382
  pageEl.classList.remove('taro_page_shade');
952
1383
  this.addAnimation(pageEl, pageNo === 0);
953
1384
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
1385
+ this.navigationBarHandler.load();
954
1386
  this.bindPageEvents(page, pageConfig);
955
1387
  this.triggerRouterChange();
956
1388
  }
@@ -960,30 +1392,43 @@ class PageHandler {
960
1392
  pageEl = this.getPageContainer(page);
961
1393
  this.addAnimation(pageEl, pageNo === 0);
962
1394
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
1395
+ this.navigationBarHandler.load();
963
1396
  this.onReady(page, false);
964
1397
  this.bindPageEvents(page, pageConfig);
965
1398
  this.triggerRouterChange();
966
1399
  });
967
1400
  }
968
1401
  }
969
- hide(page) {
970
- var _a;
1402
+ hide(page, animation = false) {
1403
+ var _a, _b, _c, _d, _e, _f, _g, _h;
971
1404
  if (!page)
972
1405
  return;
973
1406
  // NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
974
1407
  const pageEl = this.getPageContainer(page);
975
1408
  if (pageEl) {
976
- if (this.hideTimer) {
977
- clearTimeout(this.hideTimer);
978
- this.hideTimer = null;
979
- pageEl.classList.add('taro_page_shade');
1409
+ if (animation) {
1410
+ if (this.hideTimer) {
1411
+ clearTimeout(this.hideTimer);
1412
+ this.hideTimer = null;
1413
+ (_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');
1414
+ }
1415
+ this.lastHidePage = pageEl;
1416
+ this.hideTimer = setTimeout(() => {
1417
+ this.hideTimer = null;
1418
+ pageEl.classList.add('taro_page_shade');
1419
+ }, this.animationDuration + this.animationDelay);
1420
+ (_d = page.onHide) === null || _d === void 0 ? void 0 : _d.call(page);
980
1421
  }
981
- this.lastHidePage = pageEl;
982
- this.hideTimer = setTimeout(() => {
983
- this.hideTimer = null;
1422
+ else {
1423
+ if (this.hideTimer) {
1424
+ clearTimeout(this.hideTimer);
1425
+ this.hideTimer = null;
1426
+ (_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');
1427
+ }
1428
+ (_h = page.onHide) === null || _h === void 0 ? void 0 : _h.call(page);
984
1429
  pageEl.classList.add('taro_page_shade');
985
- }, this.animationDuration + this.animationDelay);
986
- (_a = page.onHide) === null || _a === void 0 ? void 0 : _a.call(page);
1430
+ this.lastHidePage = pageEl;
1431
+ }
987
1432
  }
988
1433
  else {
989
1434
  setTimeout(() => this.hide(page), 0);
@@ -1054,6 +1499,10 @@ function createRouter(history$1, app, config, framework) {
1054
1499
  }
1055
1500
  RouterConfig.config = config;
1056
1501
  const handler = new PageHandler(config, history$1);
1502
+ // Note: 弱网情况下,快速切换 tab,会造成同个页面实例被多次挂在到页面上,原因是资源请求是异步的,短时间内发起多个请求,
1503
+ // 会在资源加载完成后,同时走到挂载的逻辑,造成 pageStampId 更新不及时,两个 page 的Id 相同,后面很多操作是通过 getElementById 来进行的
1504
+ // 所以需要加一个锁来应对这个情况
1505
+ const pageLock = {};
1057
1506
  routesAlias.set(handler.router.customRoutes);
1058
1507
  const basename = handler.router.basename;
1059
1508
  const routes = handler.routes.map(route => {
@@ -1075,29 +1524,35 @@ function createRouter(history$1, app, config, framework) {
1075
1524
  runtime.eventCenter.trigger('__taroRouterLaunch', launchParam);
1076
1525
  (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
1077
1526
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
1078
- const render = ({ location, action }) => tslib.__awaiter(this, void 0, void 0, function* () {
1079
- var _c, _d, _e, _f, _g, _h, _j, _k, _l;
1080
- handler.pathname = decodeURI(location.pathname);
1081
- if ((_c = window.__taroAppConfig) === null || _c === void 0 ? void 0 : _c.usingWindowScroll)
1527
+ const render = (_c) => tslib.__awaiter(this, [_c], void 0, function* ({ location, action }) {
1528
+ var _d, _e, _f, _g, _h, _j, _k, _l;
1529
+ // Note: 由于下面有异步加载操作 先不要在这里去设置 handler.pathname
1530
+ const currentPathname = decodeURI(location.pathname);
1531
+ if ((_d = window.__taroAppConfig) === null || _d === void 0 ? void 0 : _d.usingWindowScroll)
1082
1532
  window.scrollTo(0, 0);
1083
1533
  runtime.eventCenter.trigger('__taroRouterChange', {
1084
1534
  toLocation: {
1085
- path: handler.pathname
1535
+ path: currentPathname
1086
1536
  }
1087
1537
  });
1088
- let element, params;
1538
+ let element, context, params;
1539
+ const routerPath = handler.router.forcePath || currentPathname;
1540
+ pageLock[routerPath] = typeof pageLock[routerPath] === 'number' ? pageLock[routerPath] + 1 : 1;
1541
+ const currentLock = pageLock[routerPath];
1542
+ let postLock;
1089
1543
  try {
1090
- const result = yield router.resolve(handler.router.forcePath || handler.pathname);
1091
- [element, , params] = yield Promise.all(result);
1544
+ const result = yield router.resolve(routerPath);
1545
+ [element, context, params] = yield Promise.all(result);
1546
+ postLock = pageLock[routerPath];
1092
1547
  }
1093
1548
  catch (error) {
1094
1549
  if (error.status === 404) {
1095
1550
  const notFoundEvent = {
1096
1551
  isEntryPage: stacks.length === 0,
1097
- path: handler.pathname,
1552
+ path: currentPathname,
1098
1553
  query: handler.getQuery(createStampId()),
1099
1554
  };
1100
- (_d = app.onPageNotFound) === null || _d === void 0 ? void 0 : _d.call(app, notFoundEvent);
1555
+ (_e = app.onPageNotFound) === null || _e === void 0 ? void 0 : _e.call(app, notFoundEvent);
1101
1556
  runtime.eventCenter.trigger('__taroRouterNotFound', notFoundEvent);
1102
1557
  }
1103
1558
  else if (/Loading hot update .* failed./.test(error.message)) {
@@ -1108,15 +1563,17 @@ function createRouter(history$1, app, config, framework) {
1108
1563
  throw error;
1109
1564
  }
1110
1565
  }
1111
- if (!element)
1566
+ if (!element || currentLock !== postLock)
1112
1567
  return;
1113
- const pageConfig = handler.pageConfig;
1114
- let enablePullDownRefresh = ((_e = config === null || config === void 0 ? void 0 : config.window) === null || _e === void 0 ? void 0 : _e.enablePullDownRefresh) || false;
1115
- let navigationStyle = ((_f = config === null || config === void 0 ? void 0 : config.window) === null || _f === void 0 ? void 0 : _f.navigationStyle) || 'default';
1116
- let navigationBarTextStyle = ((_g = config === null || config === void 0 ? void 0 : config.window) === null || _g === void 0 ? void 0 : _g.navigationBarTextStyle) || 'white';
1117
- let navigationBarBackgroundColor = ((_h = config === null || config === void 0 ? void 0 : config.window) === null || _h === void 0 ? void 0 : _h.navigationBarBackgroundColor) || '#000000';
1568
+ // Note: 异步结束后,在设置 handler.pathname
1569
+ // context.pathname universal-router 被处理过了,是发起资源请求的时候传入的 pathname,即 await router.resolve(routerPath) 这个 routerPath
1570
+ handler.pathname = context.pathname;
1571
+ const { pathname, pageConfig } = handler;
1572
+ let enablePullDownRefresh = ((_f = config === null || config === void 0 ? void 0 : config.window) === null || _f === void 0 ? void 0 : _f.enablePullDownRefresh) || false;
1573
+ let navigationStyle = ((_g = config === null || config === void 0 ? void 0 : config.window) === null || _g === void 0 ? void 0 : _g.navigationStyle) || 'default';
1574
+ let navigationBarTextStyle = ((_h = config === null || config === void 0 ? void 0 : config.window) === null || _h === void 0 ? void 0 : _h.navigationBarTextStyle) || 'white';
1575
+ let navigationBarBackgroundColor = ((_j = config === null || config === void 0 ? void 0 : config.window) === null || _j === void 0 ? void 0 : _j.navigationBarBackgroundColor) || '#000000';
1118
1576
  if (pageConfig) {
1119
- setTitle((_j = pageConfig.navigationBarTitleText) !== null && _j !== void 0 ? _j : document.title);
1120
1577
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
1121
1578
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
1122
1579
  }
@@ -1132,7 +1589,6 @@ function createRouter(history$1, app, config, framework) {
1132
1589
  }
1133
1590
  runtime.eventCenter.trigger('__taroSetNavigationStyle', navigationStyle, navigationBarTextStyle, navigationBarBackgroundColor);
1134
1591
  const currentPage = runtime.Current.page;
1135
- const pathname = handler.pathname;
1136
1592
  const methodName = (_k = stacks.method) !== null && _k !== void 0 ? _k : '';
1137
1593
  const cacheTabs = stacks.getTabs();
1138
1594
  let shouldLoad = false;
@@ -1148,10 +1604,11 @@ function createRouter(history$1, app, config, framework) {
1148
1604
  }
1149
1605
  shouldLoad = true;
1150
1606
  }
1151
- else if (currentPage && handler.isTabBar(handler.pathname)) {
1607
+ else if (currentPage && handler.isTabBar(pathname)) {
1152
1608
  if (handler.isSamePage(currentPage))
1153
1609
  return;
1154
1610
  if (handler.isTabBar(currentPage.path)) {
1611
+ // NOTE: 从 tabBar 页面切换到 tabBar 页面
1155
1612
  handler.hide(currentPage);
1156
1613
  stacks.pushTab(currentPage.path.split('?')[0]);
1157
1614
  }
@@ -1165,8 +1622,8 @@ function createRouter(history$1, app, config, framework) {
1165
1622
  handler.unload(currentPage, stacks.length, true);
1166
1623
  }
1167
1624
  }
1168
- if (cacheTabs[handler.pathname]) {
1169
- stacks.popTab(handler.pathname);
1625
+ if (cacheTabs[pathname]) {
1626
+ stacks.popTab(pathname);
1170
1627
  return handler.show(stacks.getItem(0), pageConfig, 0);
1171
1628
  }
1172
1629
  shouldLoad = true;
@@ -1195,7 +1652,7 @@ function createRouter(history$1, app, config, framework) {
1195
1652
  shouldLoad = true;
1196
1653
  }
1197
1654
  else if (action === 'PUSH') {
1198
- handler.hide(currentPage);
1655
+ handler.hide(currentPage, true);
1199
1656
  shouldLoad = true;
1200
1657
  }
1201
1658
  if (shouldLoad || stacks.length < 1) {
@@ -1224,6 +1681,26 @@ function createRouter(history$1, app, config, framework) {
1224
1681
  }
1225
1682
  render({ location: history$1.location, action: history.Action.Push });
1226
1683
  (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, launchParam);
1684
+ window.addEventListener('visibilitychange', () => {
1685
+ var _a, _b, _c, _d, _e, _f, _g;
1686
+ const currentPath = ((_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path) || '';
1687
+ const path = currentPath.substring(0, currentPath.indexOf('?'));
1688
+ const param = {};
1689
+ // app的 onShow/onHide 生命周期的路径信息为当前页面的路径
1690
+ Object.assign(param, launchParam, { path });
1691
+ if (document.visibilityState === 'visible') {
1692
+ (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, param);
1693
+ // 单页面app显示后一刻会触发当前 page.onShow 生命周期函数
1694
+ (_d = (_c = runtime.Current.page) === null || _c === void 0 ? void 0 : _c.onShow) === null || _d === void 0 ? void 0 : _d.call(_c);
1695
+ }
1696
+ else {
1697
+ // 单页面app隐藏前一刻会触发当前 page.onHide 生命周期函数
1698
+ if ((_e = runtime.Current.page) === null || _e === void 0 ? void 0 : _e.path) {
1699
+ runtime.safeExecute((_f = runtime.Current.page) === null || _f === void 0 ? void 0 : _f.path, 'onHide');
1700
+ }
1701
+ (_g = app.onHide) === null || _g === void 0 ? void 0 : _g.call(app, param);
1702
+ }
1703
+ });
1227
1704
  return history$1.listen(render);
1228
1705
  }
1229
1706
 
@@ -1235,10 +1712,11 @@ function handleAppMount(config, _, appId = config.appId || 'app') {
1235
1712
  app.id = appId;
1236
1713
  isPosition = false;
1237
1714
  }
1238
- const appWrapper = (app === null || app === void 0 ? void 0 : app.parentNode) || (app === null || app === void 0 ? void 0 : app.parentElement) || document.body;
1715
+ const appWrapper = ((app === null || app === void 0 ? void 0 : app.parentNode) || (app === null || app === void 0 ? void 0 : app.parentElement) || document.body);
1239
1716
  app.classList.add('taro_router');
1240
1717
  if (!isPosition)
1241
1718
  appWrapper.appendChild(app);
1719
+ initNavigationBar(config, appWrapper);
1242
1720
  }
1243
1721
  function handleAppMountWithTabbar(config, history, appId = config.appId || 'app') {
1244
1722
  let app = document.getElementById(appId);
@@ -1248,7 +1726,7 @@ function handleAppMountWithTabbar(config, history, appId = config.appId || 'app'
1248
1726
  app.id = appId;
1249
1727
  isPosition = false;
1250
1728
  }
1251
- const appWrapper = (app === null || app === void 0 ? void 0 : app.parentNode) || (app === null || app === void 0 ? void 0 : app.parentElement) || document.body;
1729
+ const appWrapper = ((app === null || app === void 0 ? void 0 : app.parentNode) || (app === null || app === void 0 ? void 0 : app.parentElement) || document.body);
1252
1730
  app.classList.add('taro_router');
1253
1731
  const container = document.createElement('div');
1254
1732
  container.classList.add('taro-tabbar__container');
@@ -1264,13 +1742,14 @@ function handleAppMountWithTabbar(config, history, appId = config.appId || 'app'
1264
1742
  appWrapper.replaceChild(container, app);
1265
1743
  }
1266
1744
  initTabbar(config, history);
1745
+ initNavigationBar(config, container);
1267
1746
  }
1268
1747
 
1269
- Object.defineProperty(exports, 'createBrowserHistory', {
1748
+ Object.defineProperty(exports, "createBrowserHistory", {
1270
1749
  enumerable: true,
1271
1750
  get: function () { return history.createBrowserHistory; }
1272
1751
  });
1273
- Object.defineProperty(exports, 'createHashHistory', {
1752
+ Object.defineProperty(exports, "createHashHistory", {
1274
1753
  enumerable: true,
1275
1754
  get: function () { return history.createHashHistory; }
1276
1755
  });
@@ -1290,5 +1769,8 @@ exports.redirectTo = redirectTo;
1290
1769
  exports.routesAlias = routesAlias;
1291
1770
  exports.setHistory = setHistory;
1292
1771
  exports.setHistoryMode = setHistoryMode;
1772
+ exports.setMpaTitle = setMpaTitle;
1773
+ exports.setNavigationBarLoading = setNavigationBarLoading;
1774
+ exports.setNavigationBarStyle = setNavigationBarStyle;
1293
1775
  exports.setTitle = setTitle;
1294
1776
  exports.switchTab = switchTab;