@tarojs/router 4.0.0-beta.8 → 4.0.0-beta.81

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,220 @@ 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
+ overflow: hidden; // 防止 iOS 页面滚动
19
+ }
20
+ .taro_router > .taro_page {
21
+ position: absolute;
22
+ left: 0;
23
+ top: 0;
24
+ width: 100%;
25
+ height: 100%;
26
+ background-color: #fff;
27
+ transform: translate(100%, 0);
28
+ transition: transform ${ms}ms;
29
+ z-index: 0;
30
+ }
31
+
32
+ .taro_router > .taro_page.taro_tabbar_page,
33
+ .taro_router > .taro_page.taro_page_show.taro_page_stationed {
34
+ transform: none;
35
+ transition: none;
36
+ }
37
+
38
+ .taro_router > .taro_page.taro_page_show {
39
+ transform: translate(0, 0);
40
+ }
41
+ `;
42
+ addStyle(css);
43
+ }
44
+ /**
45
+ * 插入路由相关样式
46
+ */
47
+ function loadRouterStyle(enableTabBar, enableWindowScroll) {
48
+ const css = `
49
+ .taro_router {
50
+ position: relative;
51
+ width: 100%;
52
+ height: 100%;
53
+ }
54
+
55
+ .taro_page {
56
+ width: 100%;
57
+ height: 100%;
58
+ ${enableWindowScroll ? '' : `
59
+ overflow-x: hidden;
60
+ overflow-y: scroll;
61
+ max-height: 100vh;
62
+ `}
63
+ }
64
+ ${enableTabBar ? `
65
+ .taro-tabbar__container > .taro-tabbar__panel {
66
+ overflow: hidden;
67
+ }
68
+
69
+ .taro-tabbar__container > .taro-tabbar__panel > .taro_page.taro_tabbar_page {
70
+ max-height: calc(100vh - var(--taro-tabbar-height) - constant(safe-area-inset-bottom));
71
+ max-height: calc(100vh - var(--taro-tabbar-height) - env(safe-area-inset-bottom));
72
+ }
73
+
74
+ ` : ''}
75
+ .taro_page_shade,
76
+ .taro_router > .taro_page.taro_page_show.taro_page_stationed:not(.taro_page_shade):not(.taro_tabbar_page):not(:last-child) {
77
+ display: none;
78
+ }
79
+ `;
80
+ addStyle(css);
81
+ }
82
+ /**
83
+ * 插入导航栏相关的样式
84
+ */
85
+ function loadNavigationBarStyle() {
86
+ const css = `
87
+ .taro-navigation-bar-show {
88
+ display: flex;
89
+ background: white;
90
+ position: sticky;
91
+ z-index: 500;
92
+ top: 0;
93
+ padding-bottom: 8px;
94
+ padding-top: calc(env(safe-area-inset-top) + 8px);
95
+ justify-content: center;
96
+ align-items: center;
97
+ }
98
+
99
+ .taro-navigation-bar-hide {
100
+ display: none;
101
+ }
102
+
103
+ .taro-navigation-bar-title-wrap {
104
+ display: flex;
105
+ height: 24px;
106
+ }
107
+
108
+ .taro-navigation-bar-title-wrap > .taro-navigation-bar-loading {
109
+ display: none;
110
+ animation: loading 2s linear infinite;
111
+ }
112
+
113
+ .taro-navigation-bar-title-wrap .taro-navigation-bar-loading.taro-navigation-bar-loading-show {
114
+ display: flex;
115
+ }
116
+
117
+ .taro-navigation-bar-title-wrap > .taro-navigation-bar-title {
118
+ font-size: 24px;
119
+ height: 24px;
120
+ line-height: 24px;
121
+ max-width: 100px;
122
+ white-space: nowrap;
123
+ overflow: hidden;
124
+ line-height: 24px;
125
+ text-overflow: ellipsis;
126
+ }
127
+
128
+ @keyframes loading {
129
+ from {
130
+ transform: rotate(0deg);
131
+ }
132
+ to {
133
+ transform: rotate(360deg);
134
+ }
135
+ }
136
+
137
+ @keyframes loading {
138
+ from {
139
+ transform: rotate(0deg);
140
+ }
141
+ to {
142
+ transform: rotate(360deg);
143
+ }
144
+ }
145
+
146
+ .taro-navigation-bar-no-icon > .taro-navigation-bar-home {
147
+ display: none;
148
+ }
149
+
150
+ .taro-navigation-bar-no-icon > .taro-navigation-bar-back {
151
+ display: none;
152
+ }
153
+
154
+ .taro-navigation-bar-home-icon > .taro-navigation-bar-home {
155
+ display: flex;
156
+ left: 8px;
157
+ position: absolute;
158
+ width: 24px;
159
+ height: 24px;
160
+ }
161
+
162
+ .taro-navigation-bar-back-icon > .taro-navigation-bar-back {
163
+ display: flex;
164
+ left: 8px;
165
+ position: absolute;
166
+ width: 24px;
167
+ height: 24px;
168
+ }
169
+ `;
170
+ addStyle(css);
171
+ }
172
+ function addStyle(css) {
173
+ if (!css)
174
+ return;
175
+ const style = document.createElement('style');
176
+ style.innerHTML = css;
177
+ document.getElementsByTagName('head')[0].appendChild(style);
178
+ }
179
+
180
+ const home_svg_str = `
181
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
182
+ <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"/>
183
+ </svg>
184
+ `;
185
+ const back_svg_str = `
186
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
187
+ <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"/>
188
+ </svg>
189
+ `;
190
+ const loading_svg_str = `
191
+ <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>
192
+ `;
193
+ function initNavigationBar(config, container) {
194
+ if (config.router.mode === 'multi')
195
+ return;
196
+ const navigationBar = document.createElement('div');
197
+ navigationBar.classList.add('taro-navigation-bar-no-icon');
198
+ const navigationBarBackBtn = document.createElement('div');
199
+ navigationBarBackBtn.classList.add('taro-navigation-bar-back');
200
+ const navigationBarHomeBtn = document.createElement('div');
201
+ navigationBarHomeBtn.classList.add('taro-navigation-bar-home');
202
+ navigationBarBackBtn.innerHTML = back_svg_str;
203
+ navigationBarHomeBtn.innerHTML = home_svg_str;
204
+ const navigationBarTitleWrap = document.createElement('div');
205
+ navigationBarTitleWrap.classList.add('taro-navigation-bar-title-wrap');
206
+ const navigationBarLoading = document.createElement('div');
207
+ navigationBarLoading.classList.add('taro-navigation-bar-loading');
208
+ navigationBarLoading.innerHTML = loading_svg_str;
209
+ const navigationBarTitle = document.createElement('div');
210
+ navigationBarTitle.classList.add('taro-navigation-bar-title');
211
+ navigationBarTitleWrap.appendChild(navigationBarLoading);
212
+ navigationBarTitleWrap.appendChild(navigationBarTitle);
213
+ navigationBar.appendChild(navigationBarHomeBtn);
214
+ navigationBar.appendChild(navigationBarBackBtn);
215
+ navigationBar.appendChild(navigationBarTitleWrap);
216
+ navigationBar.id = 'taro-navigation-bar';
217
+ container.prepend(navigationBar);
218
+ loadNavigationBarStyle();
219
+ }
220
+
11
221
  function initTabbar(config, history) {
12
222
  if (config.tabBar == null || config.tabBar.custom) {
13
223
  return;
@@ -81,14 +291,14 @@ class MpaHistory {
81
291
  }
82
292
  parseUrl(to) {
83
293
  let url = to.pathname || '';
84
- if (RouterConfig.isPage(url)) {
294
+ if (RouterConfig.isPage(runtime.addLeadingSlash(url))) {
85
295
  url += '.html';
86
296
  }
87
297
  if (to.search) {
88
298
  url += `?${to.search}`;
89
299
  }
90
300
  if (to.hash) {
91
- url += `#${to.hash}`;
301
+ url += to.hash.startsWith('#') ? to.hash : `#${to.hash}`;
92
302
  }
93
303
  return url;
94
304
  }
@@ -245,26 +455,32 @@ class Stacks {
245
455
  }
246
456
  const stacks = new Stacks();
247
457
 
248
- let preTitle = document.title;
249
- let isLoadDdEntry = false;
250
458
  const isWeixin = () => !!navigator.userAgent.match(/\bMicroMessenger\b/ig);
251
459
  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 });
460
+ let preTitle = document.title;
461
+ let isLoadDdEntry = false;
462
+ function setMpaTitle(title) {
463
+ if (preTitle === title)
464
+ return;
465
+ document.title = title;
466
+ preTitle = title;
467
+ if (process.env.SUPPORT_DINGTALK_NAVIGATE !== 'disabled' && isDingTalk()) {
468
+ if (!isLoadDdEntry) {
469
+ isLoadDdEntry = true;
470
+ require('dingtalk-jsapi/platform');
265
471
  }
266
- return title;
267
- });
472
+ const setDingTitle = require('dingtalk-jsapi/api/biz/navigation/setTitle').default;
473
+ setDingTitle({ title });
474
+ }
475
+ }
476
+ function setTitle(title) {
477
+ runtime.eventCenter.trigger('__taroH5SetNavigationBarTitle', title);
478
+ }
479
+ function setNavigationBarStyle(option) {
480
+ runtime.eventCenter.trigger('__taroH5setNavigationBarColor', option);
481
+ }
482
+ function setNavigationBarLoading(loading) {
483
+ runtime.eventCenter.trigger('__taroH5setNavigationBarLoading', loading);
268
484
  }
269
485
 
270
486
  class RoutesAlias {
@@ -308,6 +524,7 @@ class RoutesAlias {
308
524
  }
309
525
  const routesAlias = new RoutesAlias();
310
526
 
527
+ const routeEvtChannel = shared.EventChannel.routeChannel;
311
528
  function processNavigateUrl(option) {
312
529
  var _a;
313
530
  const pathPieces = history.parsePath(option.url);
@@ -339,6 +556,10 @@ function navigate(option, method) {
339
556
  const { success, complete, fail } = option;
340
557
  const unListen = exports.history.listen(() => {
341
558
  const res = { errMsg: `${method}:ok` };
559
+ if (method === 'navigateTo') {
560
+ res.eventChannel = routeEvtChannel;
561
+ routeEvtChannel.addEvents(option.events);
562
+ }
342
563
  success === null || success === void 0 ? void 0 : success(res);
343
564
  complete === null || complete === void 0 ? void 0 : complete(res);
344
565
  resolve(res);
@@ -348,6 +569,20 @@ function navigate(option, method) {
348
569
  if ('url' in option) {
349
570
  const pathPieces = processNavigateUrl(option);
350
571
  const state = { timestamp: Date.now() };
572
+ if (pathPieces.pathname) {
573
+ const originPath = routesAlias.getOrigin(pathPieces.pathname);
574
+ if (!RouterConfig.isPage(runtime.addLeadingSlash(originPath)) && !RouterConfig.isPage(runtime.addLeadingSlash(pathPieces.pathname))) {
575
+ const res = { errMsg: `${method}:fail page ${originPath} is not found` };
576
+ fail === null || fail === void 0 ? void 0 : fail(res);
577
+ complete === null || complete === void 0 ? void 0 : complete(res);
578
+ if (fail || complete) {
579
+ return resolve(res);
580
+ }
581
+ else {
582
+ return reject(res);
583
+ }
584
+ }
585
+ }
351
586
  if (method === 'navigateTo') {
352
587
  exports.history.push(pathPieces, state);
353
588
  }
@@ -373,7 +608,12 @@ function navigate(option, method) {
373
608
  const res = { errMsg: `${method}:fail ${error.message || error}` };
374
609
  fail === null || fail === void 0 ? void 0 : fail(res);
375
610
  complete === null || complete === void 0 ? void 0 : complete(res);
376
- reject(res);
611
+ if (fail || complete) {
612
+ return resolve(res);
613
+ }
614
+ else {
615
+ return reject(res);
616
+ }
377
617
  }
378
618
  });
379
619
  });
@@ -408,12 +648,18 @@ let pageResizeFn;
408
648
  function bindPageResize(page) {
409
649
  pageResizeFn && window.removeEventListener('resize', pageResizeFn);
410
650
  pageResizeFn = function () {
411
- page.onResize && page.onResize({
412
- size: {
413
- windowHeight: window.innerHeight,
414
- windowWidth: window.innerWidth
415
- }
416
- });
651
+ if (page.onResize) {
652
+ const mediaQuery = window.matchMedia('(orientation: portrait)');
653
+ page.onResize({
654
+ deviceOrientation: mediaQuery.matches ? 'portrait' : 'landscape',
655
+ size: {
656
+ windowHeight: window.innerHeight,
657
+ windowWidth: window.innerWidth,
658
+ screenHeight: window.screen.height,
659
+ screenWidth: window.screen.width,
660
+ }
661
+ });
662
+ }
417
663
  };
418
664
  window.addEventListener('resize', pageResizeFn, false);
419
665
  }
@@ -452,80 +698,6 @@ function getOffset() {
452
698
  }
453
699
  }
454
700
 
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
701
  /* eslint-disable dot-notation */
530
702
  class MultiPageHandler {
531
703
  constructor(config, history) {
@@ -686,6 +858,7 @@ function createMultiRouter(history, app, config, framework) {
686
858
  if (typeof app.onUnhandledRejection === 'function') {
687
859
  window.addEventListener('unhandledrejection', app.onUnhandledRejection);
688
860
  }
861
+ runtime.eventCenter.on('__taroH5SetNavigationBarTitle', setMpaTitle);
689
862
  RouterConfig.config = config;
690
863
  const handler = new MultiPageHandler(config, history);
691
864
  const launchParam = {
@@ -719,7 +892,7 @@ function createMultiRouter(history, app, config, framework) {
719
892
  return;
720
893
  let enablePullDownRefresh = ((_c = config === null || config === void 0 ? void 0 : config.window) === null || _c === void 0 ? void 0 : _c.enablePullDownRefresh) || false;
721
894
  if (pageConfig) {
722
- setTitle((_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title);
895
+ setMpaTitle((_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title);
723
896
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
724
897
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
725
898
  }
@@ -731,9 +904,268 @@ function createMultiRouter(history, app, config, framework) {
731
904
  const page = runtime.createPageConfig(enablePullDownRefresh ? runtime.hooks.call('createPullDownComponent', el, pathName, framework, handler.PullDownRefresh) : el, pathName + runtime.stringify(launchParam), {}, loadConfig);
732
905
  handler.load(page, pageConfig);
733
906
  (_f = app.onShow) === null || _f === void 0 ? void 0 : _f.call(app, launchParam);
907
+ window.addEventListener('visibilitychange', () => {
908
+ var _a, _b, _c;
909
+ const currentPath = ((_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path) || '';
910
+ const path = currentPath.substring(0, currentPath.indexOf('?'));
911
+ const param = {};
912
+ // app的 onShow/onHide 生命周期的路径信息为当前页面的路径
913
+ Object.assign(param, launchParam, { path });
914
+ if (document.visibilityState === 'visible') {
915
+ (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, param);
916
+ }
917
+ else {
918
+ (_c = app.onHide) === null || _c === void 0 ? void 0 : _c.call(app, param);
919
+ }
920
+ });
734
921
  });
735
922
  }
736
923
 
924
+ class NavigationBarHandler {
925
+ constructor(pageContext) {
926
+ this.isLoadDdEntry = false;
927
+ this.cache = {};
928
+ this.pageContext = pageContext;
929
+ this.init();
930
+ runtime.eventCenter.on('__taroH5SetNavigationBarTitle', (title) => {
931
+ this.setTitle(title);
932
+ });
933
+ runtime.eventCenter.on('__taroH5setNavigationBarLoading', (loading) => {
934
+ this.setNavigationLoading(loading);
935
+ });
936
+ runtime.eventCenter.on('__taroH5setNavigationBarColor', ({ backgroundColor, frontColor }) => {
937
+ if (typeof backgroundColor === 'string')
938
+ this.setNavigationBarBackground(backgroundColor);
939
+ if (typeof frontColor === 'string')
940
+ this.setNavigationBarTextStyle(frontColor);
941
+ });
942
+ }
943
+ toHomeFn() {
944
+ reLaunch({ url: this.pageContext.homePage });
945
+ }
946
+ backFn() {
947
+ navigateBack();
948
+ }
949
+ get homeBtnElement() {
950
+ var _a;
951
+ if (!this.navigationBarElement)
952
+ return null;
953
+ return (_a = this.navigationBarElement.getElementsByClassName('taro-navigation-bar-home')) === null || _a === void 0 ? void 0 : _a[0];
954
+ }
955
+ get backBtnElement() {
956
+ var _a;
957
+ if (!this.navigationBarElement)
958
+ return null;
959
+ return (_a = this.navigationBarElement.getElementsByClassName('taro-navigation-bar-back')) === null || _a === void 0 ? void 0 : _a[0];
960
+ }
961
+ get titleElement() {
962
+ var _a;
963
+ if (!this.navigationBarElement)
964
+ return null;
965
+ return (_a = this.navigationBarElement.getElementsByClassName('taro-navigation-bar-title')) === null || _a === void 0 ? void 0 : _a[0];
966
+ }
967
+ get loadingElement() {
968
+ if (!this.navigationBarElement)
969
+ return null;
970
+ return this.navigationBarElement.getElementsByClassName('taro-navigation-bar-loading')[0];
971
+ }
972
+ init() {
973
+ var _a, _b;
974
+ this.setNavigationBarElement();
975
+ if (!this.navigationBarElement)
976
+ return;
977
+ (_a = this.homeBtnElement) === null || _a === void 0 ? void 0 : _a.addEventListener('click', this.toHomeFn.bind(this));
978
+ (_b = this.backBtnElement) === null || _b === void 0 ? void 0 : _b.addEventListener('click', this.backFn.bind(this));
979
+ }
980
+ setNavigationBarElement() {
981
+ this.navigationBarElement = document.getElementById('taro-navigation-bar');
982
+ }
983
+ load() {
984
+ this.setCacheValue();
985
+ this.setTitle();
986
+ this.setNavigationBarVisible();
987
+ this.setFnBtnState();
988
+ this.setNavigationBarBackground();
989
+ this.setNavigationBarTextStyle();
990
+ this.setNavigationLoading();
991
+ }
992
+ setCacheValue() {
993
+ const currentPage = this.pageContext.currentPage;
994
+ if (typeof this.cache[currentPage] !== 'object') {
995
+ this.cache[currentPage] = {};
996
+ }
997
+ }
998
+ setFnBtnState() {
999
+ const currentRouter = this.pageContext.currentPage;
1000
+ if (this.pageContext.isTabBar(currentRouter) || this.pageContext.homePage === currentRouter) {
1001
+ this.fnBtnToggleToNone();
1002
+ }
1003
+ else if (stacks.length > 1) {
1004
+ this.fnBtnToggleToBack();
1005
+ }
1006
+ else {
1007
+ this.fnBtnToggleToHome();
1008
+ }
1009
+ }
1010
+ shiftLoadingState(show) {
1011
+ if (!this.loadingElement)
1012
+ return;
1013
+ if (show) {
1014
+ this.loadingElement.classList.add('taro-navigation-bar-loading-show');
1015
+ }
1016
+ else {
1017
+ this.loadingElement.classList.remove('taro-navigation-bar-loading-show');
1018
+ }
1019
+ }
1020
+ setNavigationLoading(show) {
1021
+ var _a;
1022
+ if (!this.navigationBarElement)
1023
+ return;
1024
+ const currentPage = this.pageContext.currentPage;
1025
+ let isShow;
1026
+ if (typeof show === 'boolean') {
1027
+ isShow = show;
1028
+ this.cache[currentPage] &&
1029
+ (this.cache[currentPage].loading = isShow);
1030
+ }
1031
+ else {
1032
+ const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.loading;
1033
+ if (typeof cacheValue === 'boolean') {
1034
+ isShow = cacheValue;
1035
+ }
1036
+ else {
1037
+ // 默认值为 false
1038
+ isShow = false;
1039
+ this.cache[currentPage] &&
1040
+ (this.cache[currentPage].loading = isShow);
1041
+ }
1042
+ }
1043
+ this.shiftLoadingState(isShow);
1044
+ }
1045
+ setNavigationBarBackground(backgroundColor) {
1046
+ var _a, _b, _c;
1047
+ if (!this.navigationBarElement)
1048
+ return;
1049
+ const currentPage = this.pageContext.currentPage;
1050
+ let color;
1051
+ if (typeof backgroundColor === 'string') {
1052
+ color = backgroundColor;
1053
+ this.cache[currentPage] &&
1054
+ (this.cache[currentPage].backgroundColor = color);
1055
+ }
1056
+ else {
1057
+ const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.backgroundColor;
1058
+ if (typeof cacheValue === 'string') {
1059
+ color = cacheValue;
1060
+ }
1061
+ else {
1062
+ color = ((_c = (_b = this.pageContext.config) === null || _b === void 0 ? void 0 : _b.window) === null || _c === void 0 ? void 0 : _c.navigationBarBackgroundColor) || '#000000';
1063
+ this.cache[currentPage] &&
1064
+ (this.cache[currentPage].backgroundColor = color);
1065
+ }
1066
+ }
1067
+ this.navigationBarElement.style.background = color;
1068
+ }
1069
+ setNavigationBarTextStyle(fontColor) {
1070
+ var _a, _b, _c;
1071
+ if (!this.navigationBarElement)
1072
+ return;
1073
+ const currentPage = this.pageContext.currentPage;
1074
+ let color;
1075
+ if (typeof fontColor === 'string') {
1076
+ color = fontColor;
1077
+ this.cache[currentPage] &&
1078
+ (this.cache[currentPage].fontColor = color);
1079
+ }
1080
+ else {
1081
+ const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.fontColor;
1082
+ if (typeof cacheValue === 'string') {
1083
+ color = cacheValue;
1084
+ }
1085
+ else {
1086
+ color = ((_c = (_b = this.pageContext.config) === null || _b === void 0 ? void 0 : _b.window) === null || _c === void 0 ? void 0 : _c.navigationBarTextStyle) || 'white';
1087
+ this.cache[currentPage] &&
1088
+ (this.cache[currentPage].fontColor = color);
1089
+ }
1090
+ }
1091
+ this.navigationBarElement.style.color = color;
1092
+ }
1093
+ setTitle(title) {
1094
+ var _a, _b, _c;
1095
+ const currentPage = this.pageContext.currentPage;
1096
+ let proceedTitle;
1097
+ if (typeof title === 'string') {
1098
+ proceedTitle = title;
1099
+ this.cache[currentPage] &&
1100
+ (this.cache[currentPage].title = proceedTitle);
1101
+ }
1102
+ else {
1103
+ const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.title;
1104
+ if (typeof cacheValue === 'string') {
1105
+ proceedTitle = cacheValue;
1106
+ }
1107
+ else {
1108
+ proceedTitle = (_c = (_b = this.pageContext.pageConfig) === null || _b === void 0 ? void 0 : _b.navigationBarTitleText) !== null && _c !== void 0 ? _c : document.title;
1109
+ this.cache[currentPage] &&
1110
+ (this.cache[currentPage].title = proceedTitle);
1111
+ }
1112
+ }
1113
+ if (process.env.SUPPORT_DINGTALK_NAVIGATE !== 'disabled' && isDingTalk()) {
1114
+ if (!this.isLoadDdEntry) {
1115
+ this.isLoadDdEntry = true;
1116
+ require('dingtalk-jsapi/platform');
1117
+ }
1118
+ const setDingTitle = require('dingtalk-jsapi/api/biz/navigation/setTitle').default;
1119
+ setDingTitle({ proceedTitle });
1120
+ }
1121
+ document.title = proceedTitle;
1122
+ if (!this.titleElement)
1123
+ return;
1124
+ this.titleElement.innerHTML = proceedTitle;
1125
+ }
1126
+ fnBtnToggleToHome() {
1127
+ if (!this.navigationBarElement)
1128
+ return;
1129
+ this.navigationBarElement.classList.add('taro-navigation-bar-home-icon');
1130
+ this.navigationBarElement.classList.remove('taro-navigation-bar-back-icon');
1131
+ }
1132
+ fnBtnToggleToBack() {
1133
+ if (!this.navigationBarElement)
1134
+ return;
1135
+ this.navigationBarElement.classList.remove('taro-navigation-bar-home-icon');
1136
+ this.navigationBarElement.classList.add('taro-navigation-bar-back-icon');
1137
+ }
1138
+ fnBtnToggleToNone() {
1139
+ if (!this.navigationBarElement)
1140
+ return;
1141
+ this.navigationBarElement.classList.remove('taro-navigation-bar-home-icon');
1142
+ this.navigationBarElement.classList.remove('taro-navigation-bar-back-icon');
1143
+ }
1144
+ setNavigationBarVisible(show) {
1145
+ var _a, _b;
1146
+ if (!this.navigationBarElement)
1147
+ return;
1148
+ let shouldShow;
1149
+ if (typeof show === 'boolean') {
1150
+ shouldShow = show;
1151
+ }
1152
+ else {
1153
+ shouldShow = (_a = this.pageContext.config.window) === null || _a === void 0 ? void 0 : _a.navigationStyle;
1154
+ if (typeof ((_b = this.pageContext.pageConfig) === null || _b === void 0 ? void 0 : _b.navigationStyle) === 'string') {
1155
+ shouldShow = this.pageContext.pageConfig.navigationStyle;
1156
+ }
1157
+ }
1158
+ if (shouldShow === 'default') {
1159
+ this.navigationBarElement.classList.add('taro-navigation-bar-show');
1160
+ this.navigationBarElement.classList.remove('taro-navigation-bar-hide');
1161
+ }
1162
+ else {
1163
+ this.navigationBarElement.classList.add('taro-navigation-bar-hide');
1164
+ this.navigationBarElement.classList.remove('taro-navigation-bar-show');
1165
+ }
1166
+ }
1167
+ }
1168
+
737
1169
  /* eslint-disable dot-notation */
738
1170
  class PageHandler {
739
1171
  constructor(config, history) {
@@ -742,6 +1174,7 @@ class PageHandler {
742
1174
  this.config = config;
743
1175
  this.homePage = runtime.getHomePage(this.routes[0].path, this.basename, this.customRoutes, this.config.entryPagePath);
744
1176
  this.mount();
1177
+ this.navigationBarHandler = new NavigationBarHandler(this);
745
1178
  }
746
1179
  get currentPage() {
747
1180
  const routePath = runtime.getCurrentPage(this.routerMode, this.basename);
@@ -885,6 +1318,7 @@ class PageHandler {
885
1318
  this.isDefaultNavigationStyle() && pageEl.classList.add('taro_navigation_page');
886
1319
  this.addAnimation(pageEl, pageNo === 0);
887
1320
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
1321
+ this.navigationBarHandler.load();
888
1322
  this.bindPageEvents(page, pageConfig);
889
1323
  this.triggerRouterChange();
890
1324
  }
@@ -897,6 +1331,7 @@ class PageHandler {
897
1331
  this.isDefaultNavigationStyle() && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_navigation_page'));
898
1332
  this.addAnimation(pageEl, pageNo === 0);
899
1333
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
1334
+ this.navigationBarHandler.load();
900
1335
  this.onReady(page, true);
901
1336
  this.bindPageEvents(page, pageConfig);
902
1337
  this.triggerRouterChange();
@@ -951,6 +1386,7 @@ class PageHandler {
951
1386
  pageEl.classList.remove('taro_page_shade');
952
1387
  this.addAnimation(pageEl, pageNo === 0);
953
1388
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
1389
+ this.navigationBarHandler.load();
954
1390
  this.bindPageEvents(page, pageConfig);
955
1391
  this.triggerRouterChange();
956
1392
  }
@@ -960,6 +1396,7 @@ class PageHandler {
960
1396
  pageEl = this.getPageContainer(page);
961
1397
  this.addAnimation(pageEl, pageNo === 0);
962
1398
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
1399
+ this.navigationBarHandler.load();
963
1400
  this.onReady(page, false);
964
1401
  this.bindPageEvents(page, pageConfig);
965
1402
  this.triggerRouterChange();
@@ -1076,7 +1513,7 @@ function createRouter(history$1, app, config, framework) {
1076
1513
  (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
1077
1514
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
1078
1515
  const render = ({ location, action }) => tslib.__awaiter(this, void 0, void 0, function* () {
1079
- var _c, _d, _e, _f, _g, _h, _j, _k, _l;
1516
+ var _c, _d, _e, _f, _g, _h, _j, _k;
1080
1517
  handler.pathname = decodeURI(location.pathname);
1081
1518
  if ((_c = window.__taroAppConfig) === null || _c === void 0 ? void 0 : _c.usingWindowScroll)
1082
1519
  window.scrollTo(0, 0);
@@ -1116,7 +1553,6 @@ function createRouter(history$1, app, config, framework) {
1116
1553
  let navigationBarTextStyle = ((_g = config === null || config === void 0 ? void 0 : config.window) === null || _g === void 0 ? void 0 : _g.navigationBarTextStyle) || 'white';
1117
1554
  let navigationBarBackgroundColor = ((_h = config === null || config === void 0 ? void 0 : config.window) === null || _h === void 0 ? void 0 : _h.navigationBarBackgroundColor) || '#000000';
1118
1555
  if (pageConfig) {
1119
- setTitle((_j = pageConfig.navigationBarTitleText) !== null && _j !== void 0 ? _j : document.title);
1120
1556
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
1121
1557
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
1122
1558
  }
@@ -1133,7 +1569,7 @@ function createRouter(history$1, app, config, framework) {
1133
1569
  runtime.eventCenter.trigger('__taroSetNavigationStyle', navigationStyle, navigationBarTextStyle, navigationBarBackgroundColor);
1134
1570
  const currentPage = runtime.Current.page;
1135
1571
  const pathname = handler.pathname;
1136
- const methodName = (_k = stacks.method) !== null && _k !== void 0 ? _k : '';
1572
+ const methodName = (_j = stacks.method) !== null && _j !== void 0 ? _j : '';
1137
1573
  const cacheTabs = stacks.getTabs();
1138
1574
  let shouldLoad = false;
1139
1575
  stacks.method = '';
@@ -1199,7 +1635,7 @@ function createRouter(history$1, app, config, framework) {
1199
1635
  shouldLoad = true;
1200
1636
  }
1201
1637
  if (shouldLoad || stacks.length < 1) {
1202
- const el = (_l = element.default) !== null && _l !== void 0 ? _l : element;
1638
+ const el = (_k = element.default) !== null && _k !== void 0 ? _k : element;
1203
1639
  const loadConfig = Object.assign({}, pageConfig);
1204
1640
  const stacksIndex = stacks.length;
1205
1641
  delete loadConfig['path'];
@@ -1224,6 +1660,26 @@ function createRouter(history$1, app, config, framework) {
1224
1660
  }
1225
1661
  render({ location: history$1.location, action: history.Action.Push });
1226
1662
  (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, launchParam);
1663
+ window.addEventListener('visibilitychange', () => {
1664
+ var _a, _b, _c, _d, _e, _f, _g;
1665
+ const currentPath = ((_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path) || '';
1666
+ const path = currentPath.substring(0, currentPath.indexOf('?'));
1667
+ const param = {};
1668
+ // app的 onShow/onHide 生命周期的路径信息为当前页面的路径
1669
+ Object.assign(param, launchParam, { path });
1670
+ if (document.visibilityState === 'visible') {
1671
+ (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, param);
1672
+ // 单页面app显示后一刻会触发当前 page.onShow 生命周期函数
1673
+ (_d = (_c = runtime.Current.page) === null || _c === void 0 ? void 0 : _c.onShow) === null || _d === void 0 ? void 0 : _d.call(_c);
1674
+ }
1675
+ else {
1676
+ // 单页面app隐藏前一刻会触发当前 page.onHide 生命周期函数
1677
+ if ((_e = runtime.Current.page) === null || _e === void 0 ? void 0 : _e.path) {
1678
+ runtime.safeExecute((_f = runtime.Current.page) === null || _f === void 0 ? void 0 : _f.path, 'onHide');
1679
+ }
1680
+ (_g = app.onHide) === null || _g === void 0 ? void 0 : _g.call(app, param);
1681
+ }
1682
+ });
1227
1683
  return history$1.listen(render);
1228
1684
  }
1229
1685
 
@@ -1239,6 +1695,7 @@ function handleAppMount(config, _, appId = config.appId || 'app') {
1239
1695
  app.classList.add('taro_router');
1240
1696
  if (!isPosition)
1241
1697
  appWrapper.appendChild(app);
1698
+ initNavigationBar(config, appWrapper);
1242
1699
  }
1243
1700
  function handleAppMountWithTabbar(config, history, appId = config.appId || 'app') {
1244
1701
  let app = document.getElementById(appId);
@@ -1264,6 +1721,7 @@ function handleAppMountWithTabbar(config, history, appId = config.appId || 'app'
1264
1721
  appWrapper.replaceChild(container, app);
1265
1722
  }
1266
1723
  initTabbar(config, history);
1724
+ initNavigationBar(config, container);
1267
1725
  }
1268
1726
 
1269
1727
  Object.defineProperty(exports, 'createBrowserHistory', {
@@ -1290,5 +1748,8 @@ exports.redirectTo = redirectTo;
1290
1748
  exports.routesAlias = routesAlias;
1291
1749
  exports.setHistory = setHistory;
1292
1750
  exports.setHistoryMode = setHistoryMode;
1751
+ exports.setMpaTitle = setMpaTitle;
1752
+ exports.setNavigationBarLoading = setNavigationBarLoading;
1753
+ exports.setNavigationBarStyle = setNavigationBarStyle;
1293
1754
  exports.setTitle = setTitle;
1294
1755
  exports.switchTab = switchTab;