@tarojs/router 4.0.0-beta.1 → 4.0.0-beta.100

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.
Files changed (43) hide show
  1. package/LICENSE +17 -3
  2. package/dist/api.js +31 -22
  3. package/dist/events/resize.d.ts +1 -1
  4. package/dist/events/resize.js +15 -7
  5. package/dist/events/scroll.d.ts +1 -1
  6. package/dist/events/scroll.js +4 -1
  7. package/dist/history.d.ts +22 -1
  8. package/dist/history.js +20 -8
  9. package/dist/index.cjs.js +793 -339
  10. package/dist/index.d.ts +6 -1
  11. package/dist/index.esm.js +753 -311
  12. package/dist/index.js +52 -4
  13. package/dist/navigationBar.d.ts +2 -0
  14. package/dist/navigationBar.js +44 -0
  15. package/dist/router/index.js +7 -3
  16. package/dist/router/mpa.d.ts +2 -1
  17. package/dist/router/mpa.js +29 -19
  18. package/dist/router/multi-page.d.ts +5 -2
  19. package/dist/router/multi-page.js +27 -43
  20. package/dist/router/navigation-bar.d.ts +36 -0
  21. package/dist/router/navigation-bar.js +252 -0
  22. package/dist/router/page.d.ts +11 -4
  23. package/dist/router/page.js +62 -59
  24. package/dist/router/spa.d.ts +2 -1
  25. package/dist/router/spa.js +81 -43
  26. package/dist/router/stack.d.ts +1 -1
  27. package/dist/router/stack.js +2 -1
  28. package/dist/style.d.ts +6 -1
  29. package/dist/style.js +106 -7
  30. package/dist/tabbar.d.ts +2 -1
  31. package/dist/tabbar.js +4 -3
  32. package/dist/utils/index.d.ts +1 -8
  33. package/dist/utils/index.js +5 -20
  34. package/dist/utils/navigate.d.ts +9 -5
  35. package/dist/utils/navigate.js +24 -37
  36. package/package.json +28 -27
  37. package/types/api.d.ts +5 -0
  38. package/types/component.d.ts +5 -0
  39. package/types/taro.d.ts +8 -0
  40. package/dist/index.cjs.d.ts +0 -22
  41. package/dist/index.cjs.js.map +0 -1
  42. package/dist/index.esm.d.ts +0 -22
  43. package/dist/index.esm.js.map +0 -1
package/dist/index.cjs.js CHANGED
@@ -1,50 +1,252 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var history = require('history');
6
- var runtime = require('@tarojs/runtime');
7
- var MobileDetect = require('mobile-detect');
8
- var queryString = require('query-string');
9
3
  var components = require('@tarojs/components/dist/components');
10
4
  var taro = require('@tarojs/taro');
5
+ var tslib = require('tslib');
6
+ var runtime = require('@tarojs/runtime');
7
+ var shared = require('@tarojs/shared');
8
+ var history = require('history');
9
+ var queryString = require('query-string');
11
10
  var UniversalRouter = require('universal-router');
12
11
 
13
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
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) {
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
+ .taro_page_shade:has(+.taro_page_stationed),
77
+ .taro_page_shade.taro_tabbar_page,
78
+ .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) {
79
+ display: none;
80
+ }
81
+ `;
82
+ addStyle(css);
83
+ }
84
+ /**
85
+ * 插入导航栏相关的样式
86
+ */
87
+ function loadNavigationBarStyle() {
88
+ const css = `
89
+ .taro-navigation-bar-show {
90
+ display: flex;
91
+ background: white;
92
+ position: sticky;
93
+ z-index: 500;
94
+ top: 0;
95
+ padding-bottom: 8px;
96
+ padding-top: calc(env(safe-area-inset-top) + 8px);
97
+ justify-content: center;
98
+ align-items: center;
99
+ }
100
+
101
+ .taro-navigation-bar-hide {
102
+ display: none;
103
+ }
104
+
105
+ .taro-navigation-bar-title-wrap {
106
+ display: flex;
107
+ height: 24px;
108
+ }
109
+
110
+ .taro-navigation-bar-title-wrap > .taro-navigation-bar-loading {
111
+ display: none;
112
+ animation: loading 2s linear infinite;
113
+ }
114
+
115
+ .taro-navigation-bar-title-wrap .taro-navigation-bar-loading.taro-navigation-bar-loading-show {
116
+ display: flex;
117
+ }
118
+
119
+ .taro-navigation-bar-title-wrap > .taro-navigation-bar-title {
120
+ font-size: 24px;
121
+ height: 24px;
122
+ line-height: 24px;
123
+ max-width: 100px;
124
+ white-space: nowrap;
125
+ overflow: hidden;
126
+ line-height: 24px;
127
+ text-overflow: ellipsis;
128
+ }
129
+
130
+ @keyframes loading {
131
+ from {
132
+ transform: rotate(0deg);
133
+ }
134
+ to {
135
+ transform: rotate(360deg);
136
+ }
137
+ }
138
+
139
+ @keyframes loading {
140
+ from {
141
+ transform: rotate(0deg);
142
+ }
143
+ to {
144
+ transform: rotate(360deg);
145
+ }
146
+ }
147
+
148
+ .taro-navigation-bar-no-icon > .taro-navigation-bar-home {
149
+ display: none;
150
+ }
151
+
152
+ .taro-navigation-bar-no-icon > .taro-navigation-bar-back {
153
+ display: none;
154
+ }
155
+
156
+ .taro-navigation-bar-home-icon > .taro-navigation-bar-home {
157
+ display: flex;
158
+ left: 8px;
159
+ position: absolute;
160
+ width: 24px;
161
+ height: 24px;
162
+ }
163
+
164
+ .taro-navigation-bar-back-icon > .taro-navigation-bar-back {
165
+ display: flex;
166
+ left: 8px;
167
+ position: absolute;
168
+ width: 24px;
169
+ height: 24px;
170
+ }
171
+ `;
172
+ addStyle(css);
173
+ }
174
+ function addStyle(css) {
175
+ if (!css)
176
+ return;
177
+ const style = document.createElement('style');
178
+ style.innerHTML = css;
179
+ document.getElementsByTagName('head')[0].appendChild(style);
180
+ }
14
181
 
15
- var MobileDetect__default = /*#__PURE__*/_interopDefaultLegacy(MobileDetect);
16
- var queryString__default = /*#__PURE__*/_interopDefaultLegacy(queryString);
17
- var UniversalRouter__default = /*#__PURE__*/_interopDefaultLegacy(UniversalRouter);
182
+ const home_svg_str = `
183
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
184
+ <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"/>
185
+ </svg>
186
+ `;
187
+ const back_svg_str = `
188
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
189
+ <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"/>
190
+ </svg>
191
+ `;
192
+ const loading_svg_str = `
193
+ <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>
194
+ `;
195
+ function initNavigationBar(config, container) {
196
+ if (config.router.mode === 'multi')
197
+ return;
198
+ const navigationBar = document.createElement('div');
199
+ navigationBar.classList.add('taro-navigation-bar-no-icon');
200
+ const navigationBarBackBtn = document.createElement('div');
201
+ navigationBarBackBtn.classList.add('taro-navigation-bar-back');
202
+ const navigationBarHomeBtn = document.createElement('div');
203
+ navigationBarHomeBtn.classList.add('taro-navigation-bar-home');
204
+ navigationBarBackBtn.innerHTML = back_svg_str;
205
+ navigationBarHomeBtn.innerHTML = home_svg_str;
206
+ const navigationBarTitleWrap = document.createElement('div');
207
+ navigationBarTitleWrap.classList.add('taro-navigation-bar-title-wrap');
208
+ const navigationBarLoading = document.createElement('div');
209
+ navigationBarLoading.classList.add('taro-navigation-bar-loading');
210
+ navigationBarLoading.innerHTML = loading_svg_str;
211
+ const navigationBarTitle = document.createElement('div');
212
+ navigationBarTitle.classList.add('taro-navigation-bar-title');
213
+ navigationBarTitleWrap.appendChild(navigationBarLoading);
214
+ navigationBarTitleWrap.appendChild(navigationBarTitle);
215
+ navigationBar.appendChild(navigationBarHomeBtn);
216
+ navigationBar.appendChild(navigationBarBackBtn);
217
+ navigationBar.appendChild(navigationBarTitleWrap);
218
+ navigationBar.id = 'taro-navigation-bar';
219
+ container.prepend(navigationBar);
220
+ loadNavigationBarStyle();
221
+ }
18
222
 
19
- /******************************************************************************
20
- Copyright (c) Microsoft Corporation.
21
-
22
- Permission to use, copy, modify, and/or distribute this software for any
23
- purpose with or without fee is hereby granted.
24
-
25
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
26
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
27
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
28
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
29
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
30
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
31
- PERFORMANCE OF THIS SOFTWARE.
32
- ***************************************************************************** */
33
-
34
- function __awaiter(thisArg, _arguments, P, generator) {
35
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
36
- return new (P || (P = Promise))(function (resolve, reject) {
37
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
38
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
39
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
40
- step((generator = generator.apply(thisArg, _arguments || [])).next());
41
- });
42
- }
43
-
44
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
45
- var e = new Error(message);
46
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
47
- };
223
+ function initTabbar(config, history) {
224
+ if (config.tabBar == null || config.tabBar.custom) {
225
+ return;
226
+ }
227
+ // TODO: custom-tab-bar
228
+ components.defineCustomElementTaroTabbar();
229
+ const tabbar = document.createElement('taro-tabbar');
230
+ const homePage = config.entryPagePath || (config.pages ? config.pages[0] : '');
231
+ tabbar.conf = config.tabBar;
232
+ tabbar.conf.homePage = history.location.pathname === '/' ? homePage : history.location.pathname;
233
+ const routerConfig = config.router;
234
+ tabbar.conf.mode = routerConfig && routerConfig.mode ? routerConfig.mode : 'hash';
235
+ if (routerConfig.customRoutes) {
236
+ tabbar.conf.custom = true;
237
+ tabbar.conf.customRoutes = routerConfig.customRoutes;
238
+ }
239
+ else {
240
+ tabbar.conf.custom = false;
241
+ tabbar.conf.customRoutes = {};
242
+ }
243
+ if (typeof routerConfig.basename !== 'undefined') {
244
+ tabbar.conf.basename = routerConfig.basename;
245
+ }
246
+ const container = document.getElementById('container');
247
+ container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
248
+ taro.initTabBarApis(config);
249
+ }
48
250
 
49
251
  class RouterConfig {
50
252
  static set config(e) {
@@ -63,8 +265,9 @@ class RouterConfig {
63
265
  return this.router.mode || 'hash';
64
266
  }
65
267
  static get customRoutes() { return this.router.customRoutes || {}; }
268
+ // 这个方法不考虑 basename 和 customRoutes,只判断原始的 url 是否在 pages 中
66
269
  static isPage(url = '') {
67
- return this.pages.findIndex(e => prependBasename(e) === url) !== -1;
270
+ return this.pages.findIndex(e => runtime.addLeadingSlash(e) === url) !== -1;
68
271
  }
69
272
  }
70
273
 
@@ -91,14 +294,14 @@ class MpaHistory {
91
294
  }
92
295
  parseUrl(to) {
93
296
  let url = to.pathname || '';
94
- if (RouterConfig.isPage(url)) {
297
+ if (RouterConfig.isPage(runtime.addLeadingSlash(url))) {
95
298
  url += '.html';
96
299
  }
97
300
  if (to.search) {
98
301
  url += `?${to.search}`;
99
302
  }
100
303
  if (to.hash) {
101
- url += `#${to.hash}`;
304
+ url += to.hash.startsWith('#') ? to.hash : `#${to.hash}`;
102
305
  }
103
306
  return url;
104
307
  }
@@ -147,6 +350,13 @@ class MpaHistory {
147
350
  };
148
351
  }
149
352
  }
353
+ function setHistory(h, base = '/') {
354
+ exports.history = h;
355
+ basename = base;
356
+ }
357
+ function createMpaHistory(_) {
358
+ return new MpaHistory();
359
+ }
150
360
  function setHistoryMode(mode, base = '/') {
151
361
  const options = {
152
362
  window
@@ -156,7 +366,7 @@ function setHistoryMode(mode, base = '/') {
156
366
  exports.history = history.createBrowserHistory(options);
157
367
  }
158
368
  else if (mode === 'multi') {
159
- exports.history = new MpaHistory();
369
+ exports.history = createMpaHistory();
160
370
  }
161
371
  else {
162
372
  // default is hash
@@ -248,24 +458,34 @@ class Stacks {
248
458
  }
249
459
  const stacks = new Stacks();
250
460
 
251
- // export const removeLeadingSlash = (str = '') => str.replace(/^\.?\//, '')
252
- // export const removeTrailingSearch = (str = '') => str.replace(/\?[\s\S]*$/, '')
253
- const addLeadingSlash = (url = '') => (url.charAt(0) === '/' ? url : '/' + url);
254
- const hasBasename = (path = '', prefix = '') => new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path) || path === prefix;
255
- const stripBasename = (path = '', prefix = '') => hasBasename(path, prefix) ? path.substring(prefix.length) : path;
256
- const stripTrailing = (str = '') => str.replace(/[?#][\s\S]*$/, '');
257
- const getHomePage = (path = '', basename = '', customRoutes = {}, entryPagePath = '') => {
258
- var _a;
259
- const routePath = addLeadingSlash(stripBasename(path, basename));
260
- const alias = ((_a = Object.entries(customRoutes).find(([key]) => key === routePath)) === null || _a === void 0 ? void 0 : _a[1]) || routePath;
261
- return entryPagePath || (typeof alias === 'string' ? alias : alias[0]) || basename;
262
- };
263
- const getCurrentPage = (routerMode = 'hash', basename = '/') => {
264
- const pagePath = routerMode === 'hash'
265
- ? location.hash.slice(1).split('?')[0]
266
- : location.pathname;
267
- return addLeadingSlash(stripBasename(pagePath, basename));
268
- };
461
+ const isWeixin = () => !!navigator.userAgent.match(/\bMicroMessenger\b/ig);
462
+ const isDingTalk = () => !!navigator.userAgent.match(/\bDingTalk\b/ig);
463
+ let preTitle = document.title;
464
+ let isLoadDdEntry = false;
465
+ function setMpaTitle(title) {
466
+ if (preTitle === title)
467
+ return;
468
+ document.title = title;
469
+ preTitle = title;
470
+ if (process.env.SUPPORT_DINGTALK_NAVIGATE !== 'disabled' && isDingTalk()) {
471
+ if (!isLoadDdEntry) {
472
+ isLoadDdEntry = true;
473
+ require('dingtalk-jsapi/platform');
474
+ }
475
+ const setDingTitle = require('dingtalk-jsapi/api/biz/navigation/setTitle').default;
476
+ setDingTitle({ title });
477
+ }
478
+ }
479
+ function setTitle(title) {
480
+ runtime.eventCenter.trigger('__taroH5SetNavigationBarTitle', title);
481
+ }
482
+ function setNavigationBarStyle(option) {
483
+ runtime.eventCenter.trigger('__taroH5setNavigationBarColor', option);
484
+ }
485
+ function setNavigationBarLoading(loading) {
486
+ runtime.eventCenter.trigger('__taroH5setNavigationBarLoading', loading);
487
+ }
488
+
269
489
  class RoutesAlias {
270
490
  constructor() {
271
491
  this.conf = [];
@@ -295,18 +515,19 @@ class RoutesAlias {
295
515
  set(customRoutes = {}) {
296
516
  for (let key in customRoutes) {
297
517
  const path = customRoutes[key];
298
- key = addLeadingSlash(key);
518
+ key = runtime.addLeadingSlash(key);
299
519
  if (typeof path === 'string') {
300
- this.conf.push([key, addLeadingSlash(path)]);
520
+ this.conf.push([key, runtime.addLeadingSlash(path)]);
301
521
  }
302
522
  else if ((path === null || path === void 0 ? void 0 : path.length) > 0) {
303
- this.conf.push(...path.map(p => [key, addLeadingSlash(p)]));
523
+ this.conf.push(...path.map(p => [key, runtime.addLeadingSlash(p)]));
304
524
  }
305
525
  }
306
526
  }
307
527
  }
308
528
  const routesAlias = new RoutesAlias();
309
529
 
530
+ const routeEvtChannel = shared.EventChannel.routeChannel;
310
531
  function processNavigateUrl(option) {
311
532
  var _a;
312
533
  const pathPieces = history.parsePath(option.url);
@@ -315,15 +536,16 @@ function processNavigateUrl(option) {
315
536
  const parts = routesAlias.getOrigin(exports.history.location.pathname).split('/');
316
537
  parts.pop();
317
538
  pathPieces.pathname.split('/').forEach((item) => {
318
- if (item === '.') {
539
+ if (item === '.')
319
540
  return;
320
- }
321
541
  item === '..' ? parts.pop() : parts.push(item);
322
542
  });
323
543
  pathPieces.pathname = parts.join('/');
324
544
  }
545
+ // 确保是 / 开头的路径
546
+ pathPieces.pathname = runtime.addLeadingSlash(pathPieces.pathname);
325
547
  // 处理自定义路由
326
- pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname));
548
+ pathPieces.pathname = routesAlias.getAlias(runtime.addLeadingSlash(pathPieces.pathname));
327
549
  // 处理 basename
328
550
  pathPieces.pathname = prependBasename(pathPieces.pathname);
329
551
  // hack fix history v5 bug: https://github.com/remix-run/history/issues/814
@@ -332,12 +554,16 @@ function processNavigateUrl(option) {
332
554
  return pathPieces;
333
555
  }
334
556
  function navigate(option, method) {
335
- return __awaiter(this, void 0, void 0, function* () {
557
+ return tslib.__awaiter(this, void 0, void 0, function* () {
336
558
  return new Promise((resolve, reject) => {
337
559
  stacks.method = method;
338
560
  const { success, complete, fail } = option;
339
561
  const unListen = exports.history.listen(() => {
340
562
  const res = { errMsg: `${method}:ok` };
563
+ if (method === 'navigateTo') {
564
+ res.eventChannel = routeEvtChannel;
565
+ routeEvtChannel.addEvents(option.events);
566
+ }
341
567
  success === null || success === void 0 ? void 0 : success(res);
342
568
  complete === null || complete === void 0 ? void 0 : complete(res);
343
569
  resolve(res);
@@ -348,6 +574,7 @@ function navigate(option, method) {
348
574
  const pathPieces = processNavigateUrl(option);
349
575
  const state = { timestamp: Date.now() };
350
576
  if (method === 'navigateTo') {
577
+ // Note: 由于 spa 会针对弱网情况下,短时间内多次跳转同一个页面跳转加了锁,后续如果有用户反馈返回无效,那可能是这个问题
351
578
  exports.history.push(pathPieces, state);
352
579
  }
353
580
  else if (method === 'redirectTo' || method === 'switchTab') {
@@ -372,7 +599,12 @@ function navigate(option, method) {
372
599
  const res = { errMsg: `${method}:fail ${error.message || error}` };
373
600
  fail === null || fail === void 0 ? void 0 : fail(res);
374
601
  complete === null || complete === void 0 ? void 0 : complete(res);
375
- reject(res);
602
+ if (fail || complete) {
603
+ return resolve(res);
604
+ }
605
+ else {
606
+ return reject(res);
607
+ }
376
608
  }
377
609
  });
378
610
  });
@@ -403,47 +635,22 @@ function getCurrentPages() {
403
635
  return pages.map(e => { var _a; return (Object.assign(Object.assign({}, e), { route: ((_a = e.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) || '' })); });
404
636
  }
405
637
 
406
- let md;
407
- let preTitle = document.title;
408
- let isLoadDdEntry = false;
409
- function getMobileDetect() {
410
- if (!md) {
411
- md = new MobileDetect__default["default"](navigator.userAgent);
412
- }
413
- return md;
414
- }
415
- function setTitle(title) {
416
- return __awaiter(this, void 0, void 0, function* () {
417
- if (preTitle === title)
418
- return title;
419
- document.title = title;
420
- preTitle = title;
421
- if (process.env.SUPPORT_DINGTALK_NAVIGATE !== 'disabled' && isDingTalk()) {
422
- if (!isLoadDdEntry) {
423
- isLoadDdEntry = true;
424
- require('dingtalk-jsapi/platform');
425
- }
426
- const setDingTitle = require('dingtalk-jsapi/api/biz/navigation/setTitle').default;
427
- setDingTitle({ title });
428
- }
429
- return title;
430
- });
431
- }
432
- function isDingTalk() {
433
- const md = getMobileDetect();
434
- return md.match(/DingTalk/ig);
435
- }
436
-
437
638
  let pageResizeFn;
438
639
  function bindPageResize(page) {
439
640
  pageResizeFn && window.removeEventListener('resize', pageResizeFn);
440
641
  pageResizeFn = function () {
441
- page.onResize && page.onResize({
442
- size: {
443
- windowHeight: window.innerHeight,
444
- windowWidth: window.innerWidth
445
- }
446
- });
642
+ if (page.onResize) {
643
+ const mediaQuery = window.matchMedia('(orientation: portrait)');
644
+ page.onResize({
645
+ deviceOrientation: mediaQuery.matches ? 'portrait' : 'landscape',
646
+ size: {
647
+ windowHeight: window.innerHeight,
648
+ windowWidth: window.innerWidth,
649
+ screenHeight: window.screen.height,
650
+ screenWidth: window.screen.width,
651
+ }
652
+ });
653
+ }
447
654
  };
448
655
  window.addEventListener('resize', pageResizeFn, false);
449
656
  }
@@ -482,111 +689,10 @@ function getOffset() {
482
689
  }
483
690
  }
484
691
 
485
- /**
486
- * 插入页面动画需要的样式
487
- */
488
- function loadAnimateStyle(ms = 300) {
489
- const css = `
490
- .taro_router > .taro_page {
491
- position: absolute;
492
- left: 0;
493
- top: 0;
494
- width: 100%;
495
- height: 100%;
496
- background-color: #fff;
497
- transform: translate(100%, 0);
498
- transition: transform ${ms}ms;
499
- z-index: 0;
500
- }
501
-
502
- .taro_router > .taro_page.taro_tabbar_page,
503
- .taro_router > .taro_page.taro_page_show.taro_page_stationed {
504
- transform: none;
505
- }
506
-
507
- .taro_router > .taro_page.taro_page_show {
508
- transform: translate(0, 0);
509
- }
510
- `;
511
- addStyle(css);
512
- }
513
- /**
514
- * 插入路由相关样式
515
- */
516
- function loadRouterStyle(usingWindowScroll) {
517
- const css = `
518
- .taro_router {
519
- position: relative;
520
- width: 100%;
521
- height: 100%;
522
- }
523
-
524
- .taro_page {
525
- width: 100%;
526
- height: 100%;
527
- ${usingWindowScroll ? '' : `
528
- overflow-x: hidden;
529
- overflow-y: scroll;
530
- max-height: 100vh;
531
- `}
532
- }
533
-
534
- .taro-tabbar__container > .taro-tabbar__panel {
535
- overflow: hidden;
536
- }
537
-
538
- .taro-tabbar__container > .taro-tabbar__panel > .taro_page.taro_tabbar_page {
539
- max-height: calc(100vh - var(--taro-tabbar-height) - constant(safe-area-inset-bottom));
540
- max-height: calc(100vh - var(--taro-tabbar-height) - env(safe-area-inset-bottom));
541
- }
542
-
543
- .taro_page_shade,
544
- .taro_router > .taro_page.taro_page_show.taro_page_stationed:not(.taro_page_shade):not(.taro_tabbar_page):not(:last-child) {
545
- display: none;
546
- }
547
- `;
548
- addStyle(css);
549
- }
550
- function addStyle(css) {
551
- if (!css)
552
- return;
553
- const style = document.createElement('style');
554
- style.innerHTML = css;
555
- document.getElementsByTagName('head')[0].appendChild(style);
556
- }
557
-
558
- // @ts-nocheck
559
- function initTabbar(config) {
560
- if (config.tabBar == null || config.tabBar.custom) {
561
- return;
562
- }
563
- // TODO: custom-tab-bar
564
- components.defineCustomElementTaroTabbar();
565
- const tabbar = document.createElement('taro-tabbar');
566
- const homePage = config.entryPagePath || (config.pages ? config.pages[0] : '');
567
- tabbar.conf = config.tabBar;
568
- tabbar.conf.homePage = exports.history.location.pathname === '/' ? homePage : exports.history.location.pathname;
569
- const routerConfig = config.router;
570
- tabbar.conf.mode = routerConfig && routerConfig.mode ? routerConfig.mode : 'hash';
571
- if (routerConfig.customRoutes) {
572
- tabbar.conf.custom = true;
573
- tabbar.conf.customRoutes = routerConfig.customRoutes;
574
- }
575
- else {
576
- tabbar.conf.custom = false;
577
- tabbar.conf.customRoutes = {};
578
- }
579
- if (typeof routerConfig.basename !== 'undefined') {
580
- tabbar.conf.basename = routerConfig.basename;
581
- }
582
- const container = document.getElementById('container');
583
- container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
584
- taro.initTabBarApis(config);
585
- }
586
-
587
692
  /* eslint-disable dot-notation */
588
693
  class MultiPageHandler {
589
- constructor(config) {
694
+ constructor(config, history) {
695
+ this.history = history;
590
696
  this.config = config;
591
697
  this.mount();
592
698
  }
@@ -602,7 +708,7 @@ class MultiPageHandler {
602
708
  get pageConfig() { return this.config.route; }
603
709
  get isTabBar() {
604
710
  var _a;
605
- const routePath = addLeadingSlash(stripBasename(this.pathname, this.basename));
711
+ const routePath = runtime.addLeadingSlash(runtime.stripBasename(this.pathname, this.basename));
606
712
  const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
607
713
  if (typeof target === 'string') {
608
714
  return target === routePath;
@@ -629,43 +735,22 @@ class MultiPageHandler {
629
735
  getQuery(search = '', options = {}) {
630
736
  search = search ? `${search}&${this.search}` : this.search;
631
737
  const query = search
632
- ? queryString__default["default"].parse(search)
738
+ ? queryString.parse(search)
633
739
  : {};
634
740
  return Object.assign(Object.assign({}, query), options);
635
741
  }
636
- mount() {
637
- setHistoryMode(this.routerMode, this.router.basename);
638
- loadRouterStyle(this.usingWindowScroll);
639
- const appId = this.appId;
640
- let app = document.getElementById(appId);
641
- let isPosition = true;
642
- if (!app) {
643
- app = document.createElement('div');
644
- app.id = appId;
645
- isPosition = false;
646
- }
647
- const appWrapper = (app === null || app === void 0 ? void 0 : app.parentNode) || (app === null || app === void 0 ? void 0 : app.parentElement) || document.body;
648
- app.classList.add('taro_router');
649
- if (this.tabBarList.length > 1) {
650
- const container = document.createElement('div');
651
- container.classList.add('taro-tabbar__container');
652
- container.id = 'container';
653
- const panel = document.createElement('div');
654
- panel.classList.add('taro-tabbar__panel');
655
- panel.appendChild(app.cloneNode(true));
656
- container.appendChild(panel);
657
- if (!isPosition) {
658
- appWrapper.appendChild(container);
659
- }
660
- else {
661
- appWrapper.replaceChild(container, app);
662
- }
663
- initTabbar(this.config);
664
- }
665
- else {
666
- if (!isPosition)
667
- appWrapper.appendChild(app);
742
+ isDefaultNavigationStyle() {
743
+ var _a, _b;
744
+ let style = (_a = this.config.window) === null || _a === void 0 ? void 0 : _a.navigationStyle;
745
+ if (typeof ((_b = this.pageConfig) === null || _b === void 0 ? void 0 : _b.navigationStyle) === 'string') {
746
+ style = this.pageConfig.navigationStyle;
668
747
  }
748
+ return style !== 'custom';
749
+ }
750
+ mount() {
751
+ setHistory(this.history, this.basename);
752
+ // Note: 注入页面样式
753
+ loadRouterStyle(this.tabBarList.length > 1, this.usingWindowScroll);
669
754
  }
670
755
  onReady(page, onLoad = true) {
671
756
  var _a;
@@ -695,10 +780,13 @@ class MultiPageHandler {
695
780
  return;
696
781
  (_a = page.onLoad) === null || _a === void 0 ? void 0 : _a.call(page, this.getQuery('', page.options), () => {
697
782
  var _a;
783
+ const pageEl = this.getPageContainer(page);
698
784
  if (this.isTabBar) {
699
- const pageEl = this.getPageContainer(page);
700
785
  pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page');
701
786
  }
787
+ if (this.isDefaultNavigationStyle()) {
788
+ pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_navigation_page');
789
+ }
702
790
  this.onReady(page, true);
703
791
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
704
792
  this.bindPageEvents(page, pageConfig);
@@ -755,16 +843,17 @@ const launchStampId$1 = createStampId$1();
755
843
  * - TabBar 会多次加载
756
844
  * - 不支持路由动画
757
845
  */
758
- function createMultiRouter(app, config, framework) {
759
- var _a, _b, _c, _d, _e, _f;
760
- return __awaiter(this, void 0, void 0, function* () {
846
+ function createMultiRouter(history, app, config, framework) {
847
+ return tslib.__awaiter(this, void 0, void 0, function* () {
848
+ var _a, _b, _c, _d, _e, _f;
761
849
  if (typeof app.onUnhandledRejection === 'function') {
762
850
  window.addEventListener('unhandledrejection', app.onUnhandledRejection);
763
851
  }
852
+ runtime.eventCenter.on('__taroH5SetNavigationBarTitle', setMpaTitle);
764
853
  RouterConfig.config = config;
765
- const handler = new MultiPageHandler(config);
854
+ const handler = new MultiPageHandler(config, history);
766
855
  const launchParam = {
767
- path: config.pageName,
856
+ path: config.pageName, // 多页面模式没新开一个页面相当于重启,所以直接使用当前页面路径
768
857
  query: handler.getQuery(launchStampId$1),
769
858
  scene: 0,
770
859
  shareTicket: '',
@@ -794,7 +883,7 @@ function createMultiRouter(app, config, framework) {
794
883
  return;
795
884
  let enablePullDownRefresh = ((_c = config === null || config === void 0 ? void 0 : config.window) === null || _c === void 0 ? void 0 : _c.enablePullDownRefresh) || false;
796
885
  if (pageConfig) {
797
- setTitle((_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title);
886
+ setMpaTitle((_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title);
798
887
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
799
888
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
800
889
  }
@@ -806,19 +895,281 @@ function createMultiRouter(app, config, framework) {
806
895
  const page = runtime.createPageConfig(enablePullDownRefresh ? runtime.hooks.call('createPullDownComponent', el, pathName, framework, handler.PullDownRefresh) : el, pathName + runtime.stringify(launchParam), {}, loadConfig);
807
896
  handler.load(page, pageConfig);
808
897
  (_f = app.onShow) === null || _f === void 0 ? void 0 : _f.call(app, launchParam);
898
+ window.addEventListener('visibilitychange', () => {
899
+ var _a, _b, _c;
900
+ const currentPath = ((_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path) || '';
901
+ const path = currentPath.substring(0, currentPath.indexOf('?'));
902
+ const param = {};
903
+ // app的 onShow/onHide 生命周期的路径信息为当前页面的路径
904
+ Object.assign(param, launchParam, { path });
905
+ if (document.visibilityState === 'visible') {
906
+ (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, param);
907
+ }
908
+ else {
909
+ (_c = app.onHide) === null || _c === void 0 ? void 0 : _c.call(app, param);
910
+ }
911
+ });
809
912
  });
810
913
  }
811
914
 
915
+ class NavigationBarHandler {
916
+ constructor(pageContext) {
917
+ this.isLoadDdEntry = false;
918
+ this.cache = {};
919
+ this.pageContext = pageContext;
920
+ this.init();
921
+ runtime.eventCenter.on('__taroH5SetNavigationBarTitle', (title) => {
922
+ this.setTitle(title);
923
+ });
924
+ runtime.eventCenter.on('__taroH5setNavigationBarLoading', (loading) => {
925
+ this.setNavigationLoading(loading);
926
+ });
927
+ runtime.eventCenter.on('__taroH5setNavigationBarColor', ({ backgroundColor, frontColor }) => {
928
+ if (typeof backgroundColor === 'string')
929
+ this.setNavigationBarBackground(backgroundColor);
930
+ if (typeof frontColor === 'string')
931
+ this.setNavigationBarTextStyle(frontColor);
932
+ });
933
+ }
934
+ toHomeFn() {
935
+ reLaunch({ url: this.pageContext.originHomePage });
936
+ }
937
+ backFn() {
938
+ navigateBack();
939
+ }
940
+ get homeBtnElement() {
941
+ var _a;
942
+ if (!this.navigationBarElement)
943
+ return null;
944
+ return (_a = this.navigationBarElement.getElementsByClassName('taro-navigation-bar-home')) === null || _a === void 0 ? void 0 : _a[0];
945
+ }
946
+ get backBtnElement() {
947
+ var _a;
948
+ if (!this.navigationBarElement)
949
+ return null;
950
+ return (_a = this.navigationBarElement.getElementsByClassName('taro-navigation-bar-back')) === null || _a === void 0 ? void 0 : _a[0];
951
+ }
952
+ get titleElement() {
953
+ var _a;
954
+ if (!this.navigationBarElement)
955
+ return null;
956
+ return (_a = this.navigationBarElement.getElementsByClassName('taro-navigation-bar-title')) === null || _a === void 0 ? void 0 : _a[0];
957
+ }
958
+ get loadingElement() {
959
+ if (!this.navigationBarElement)
960
+ return null;
961
+ return this.navigationBarElement.getElementsByClassName('taro-navigation-bar-loading')[0];
962
+ }
963
+ init() {
964
+ var _a, _b;
965
+ this.setNavigationBarElement();
966
+ if (!this.navigationBarElement)
967
+ return;
968
+ (_a = this.homeBtnElement) === null || _a === void 0 ? void 0 : _a.addEventListener('click', this.toHomeFn.bind(this));
969
+ (_b = this.backBtnElement) === null || _b === void 0 ? void 0 : _b.addEventListener('click', this.backFn.bind(this));
970
+ }
971
+ setNavigationBarElement() {
972
+ this.navigationBarElement = document.getElementById('taro-navigation-bar');
973
+ }
974
+ load() {
975
+ this.setCacheValue();
976
+ this.setTitle();
977
+ this.setNavigationBarVisible();
978
+ this.setFnBtnState();
979
+ this.setNavigationBarBackground();
980
+ this.setNavigationBarTextStyle();
981
+ this.setNavigationLoading();
982
+ }
983
+ setCacheValue() {
984
+ const currentPage = this.pageContext.originPathname;
985
+ if (typeof this.cache[currentPage] !== 'object') {
986
+ this.cache[currentPage] = {};
987
+ }
988
+ }
989
+ setFnBtnState() {
990
+ const currentRouter = this.pageContext.currentPage;
991
+ if (this.pageContext.isTabBar(currentRouter) || this.pageContext.homePage === currentRouter) {
992
+ this.fnBtnToggleToNone();
993
+ }
994
+ else if (stacks.length > 1) {
995
+ this.fnBtnToggleToBack();
996
+ }
997
+ else {
998
+ this.fnBtnToggleToHome();
999
+ }
1000
+ }
1001
+ shiftLoadingState(show) {
1002
+ if (!this.loadingElement)
1003
+ return;
1004
+ if (show) {
1005
+ this.loadingElement.classList.add('taro-navigation-bar-loading-show');
1006
+ }
1007
+ else {
1008
+ this.loadingElement.classList.remove('taro-navigation-bar-loading-show');
1009
+ }
1010
+ }
1011
+ setNavigationLoading(show) {
1012
+ var _a;
1013
+ if (!this.navigationBarElement)
1014
+ return;
1015
+ const currentPage = this.pageContext.originPathname;
1016
+ let isShow;
1017
+ if (typeof show === 'boolean') {
1018
+ isShow = show;
1019
+ this.cache[currentPage] &&
1020
+ (this.cache[currentPage].loading = isShow);
1021
+ }
1022
+ else {
1023
+ const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.loading;
1024
+ if (typeof cacheValue === 'boolean') {
1025
+ isShow = cacheValue;
1026
+ }
1027
+ else {
1028
+ // 默认值为 false
1029
+ isShow = false;
1030
+ this.cache[currentPage] &&
1031
+ (this.cache[currentPage].loading = isShow);
1032
+ }
1033
+ }
1034
+ this.shiftLoadingState(isShow);
1035
+ }
1036
+ setNavigationBarBackground(backgroundColor) {
1037
+ var _a, _b, _c;
1038
+ if (!this.navigationBarElement)
1039
+ return;
1040
+ const currentPage = this.pageContext.originPathname;
1041
+ let color;
1042
+ if (typeof backgroundColor === 'string') {
1043
+ color = backgroundColor;
1044
+ this.cache[currentPage] &&
1045
+ (this.cache[currentPage].backgroundColor = color);
1046
+ }
1047
+ else {
1048
+ const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.backgroundColor;
1049
+ if (typeof cacheValue === 'string') {
1050
+ color = cacheValue;
1051
+ }
1052
+ else {
1053
+ color = ((_c = (_b = this.pageContext.config) === null || _b === void 0 ? void 0 : _b.window) === null || _c === void 0 ? void 0 : _c.navigationBarBackgroundColor) || '#000000';
1054
+ this.cache[currentPage] &&
1055
+ (this.cache[currentPage].backgroundColor = color);
1056
+ }
1057
+ }
1058
+ this.navigationBarElement.style.background = color;
1059
+ }
1060
+ setNavigationBarTextStyle(fontColor) {
1061
+ var _a, _b, _c;
1062
+ if (!this.navigationBarElement)
1063
+ return;
1064
+ const currentPage = this.pageContext.originPathname;
1065
+ let color;
1066
+ if (typeof fontColor === 'string') {
1067
+ color = fontColor;
1068
+ this.cache[currentPage] &&
1069
+ (this.cache[currentPage].fontColor = color);
1070
+ }
1071
+ else {
1072
+ const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.fontColor;
1073
+ if (typeof cacheValue === 'string') {
1074
+ color = cacheValue;
1075
+ }
1076
+ else {
1077
+ color = ((_c = (_b = this.pageContext.config) === null || _b === void 0 ? void 0 : _b.window) === null || _c === void 0 ? void 0 : _c.navigationBarTextStyle) || 'white';
1078
+ this.cache[currentPage] &&
1079
+ (this.cache[currentPage].fontColor = color);
1080
+ }
1081
+ }
1082
+ this.navigationBarElement.style.color = color;
1083
+ }
1084
+ setTitle(title) {
1085
+ var _a, _b, _c;
1086
+ const currentPage = this.pageContext.originPathname;
1087
+ let proceedTitle;
1088
+ if (typeof title === 'string') {
1089
+ proceedTitle = title;
1090
+ this.cache[currentPage] &&
1091
+ (this.cache[currentPage].title = proceedTitle);
1092
+ }
1093
+ else {
1094
+ const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.title;
1095
+ if (typeof cacheValue === 'string') {
1096
+ proceedTitle = cacheValue;
1097
+ }
1098
+ else {
1099
+ proceedTitle = (_c = (_b = this.pageContext.pageConfig) === null || _b === void 0 ? void 0 : _b.navigationBarTitleText) !== null && _c !== void 0 ? _c : document.title;
1100
+ this.cache[currentPage] &&
1101
+ (this.cache[currentPage].title = proceedTitle);
1102
+ }
1103
+ }
1104
+ if (process.env.SUPPORT_DINGTALK_NAVIGATE !== 'disabled' && isDingTalk()) {
1105
+ if (!this.isLoadDdEntry) {
1106
+ this.isLoadDdEntry = true;
1107
+ require('dingtalk-jsapi/platform');
1108
+ }
1109
+ const setDingTitle = require('dingtalk-jsapi/api/biz/navigation/setTitle').default;
1110
+ setDingTitle({ proceedTitle });
1111
+ }
1112
+ document.title = proceedTitle;
1113
+ if (!this.titleElement)
1114
+ return;
1115
+ this.titleElement.innerHTML = proceedTitle;
1116
+ }
1117
+ fnBtnToggleToHome() {
1118
+ if (!this.navigationBarElement)
1119
+ return;
1120
+ this.navigationBarElement.classList.add('taro-navigation-bar-home-icon');
1121
+ this.navigationBarElement.classList.remove('taro-navigation-bar-back-icon');
1122
+ }
1123
+ fnBtnToggleToBack() {
1124
+ if (!this.navigationBarElement)
1125
+ return;
1126
+ this.navigationBarElement.classList.remove('taro-navigation-bar-home-icon');
1127
+ this.navigationBarElement.classList.add('taro-navigation-bar-back-icon');
1128
+ }
1129
+ fnBtnToggleToNone() {
1130
+ if (!this.navigationBarElement)
1131
+ return;
1132
+ this.navigationBarElement.classList.remove('taro-navigation-bar-home-icon');
1133
+ this.navigationBarElement.classList.remove('taro-navigation-bar-back-icon');
1134
+ }
1135
+ setNavigationBarVisible(show) {
1136
+ var _a, _b;
1137
+ if (!this.navigationBarElement)
1138
+ return;
1139
+ let shouldShow;
1140
+ if (typeof show === 'boolean') {
1141
+ shouldShow = show;
1142
+ }
1143
+ else {
1144
+ shouldShow = (_a = this.pageContext.config.window) === null || _a === void 0 ? void 0 : _a.navigationStyle;
1145
+ if (typeof ((_b = this.pageContext.pageConfig) === null || _b === void 0 ? void 0 : _b.navigationStyle) === 'string') {
1146
+ shouldShow = this.pageContext.pageConfig.navigationStyle;
1147
+ }
1148
+ }
1149
+ if (shouldShow === 'default') {
1150
+ this.navigationBarElement.classList.add('taro-navigation-bar-show');
1151
+ this.navigationBarElement.classList.remove('taro-navigation-bar-hide');
1152
+ }
1153
+ else {
1154
+ this.navigationBarElement.classList.add('taro-navigation-bar-hide');
1155
+ this.navigationBarElement.classList.remove('taro-navigation-bar-show');
1156
+ }
1157
+ }
1158
+ }
1159
+
812
1160
  /* eslint-disable dot-notation */
813
1161
  class PageHandler {
814
- constructor(config) {
1162
+ constructor(config, history) {
1163
+ this.history = history;
815
1164
  this.defaultAnimation = { duration: 300, delay: 50 };
816
1165
  this.config = config;
817
- this.homePage = getHomePage(this.routes[0].path, this.basename, this.customRoutes, this.config.entryPagePath);
1166
+ this.homePage = runtime.getHomePage(this.routes[0].path, this.basename, this.customRoutes, this.config.entryPagePath);
1167
+ this.originHomePage = this.config.entryPagePath || this.routes[0].path || this.basename;
818
1168
  this.mount();
1169
+ this.navigationBarHandler = new NavigationBarHandler(this);
819
1170
  }
820
1171
  get currentPage() {
821
- const routePath = getCurrentPage(this.routerMode, this.basename);
1172
+ const routePath = runtime.getCurrentPage(this.routerMode, this.basename);
822
1173
  return routePath === '/' ? this.homePage : routePath;
823
1174
  }
824
1175
  get appId() { return this.config.appId || 'app'; }
@@ -847,19 +1198,19 @@ class PageHandler {
847
1198
  }
848
1199
  set pathname(p) { this.router.pathname = p; }
849
1200
  get pathname() { return this.router.pathname; }
1201
+ // Note: 把 pathname 转换为原始路径,主要是处理 customRoutes 和 basename
1202
+ get originPathname() { return routesAlias.getOrigin(runtime.addLeadingSlash(runtime.stripBasename(this.pathname, this.basename))); }
850
1203
  get basename() { return this.router.basename || ''; }
851
1204
  get pageConfig() {
852
- const routePath = addLeadingSlash(stripBasename(this.pathname, this.basename));
853
- const homePage = addLeadingSlash(this.homePage);
1205
+ const homePage = runtime.addLeadingSlash(this.homePage);
854
1206
  return this.routes.find(r => {
855
- var _a;
856
- const pagePath = addLeadingSlash(r.path);
857
- return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
1207
+ const pagePath = runtime.addLeadingSlash(r.path);
1208
+ return [pagePath, homePage].includes(this.originPathname);
858
1209
  });
859
1210
  }
860
1211
  isTabBar(pathname) {
861
1212
  var _a;
862
- const routePath = addLeadingSlash(stripBasename(pathname, this.basename)).split('?')[0];
1213
+ const routePath = runtime.addLeadingSlash(runtime.stripBasename(pathname, this.basename)).split('?')[0];
863
1214
  const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
864
1215
  if (typeof target === 'string') {
865
1216
  return target === routePath;
@@ -869,11 +1220,19 @@ class PageHandler {
869
1220
  }
870
1221
  return false;
871
1222
  })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
872
- return !!pagePath && this.tabBarList.some(t => stripTrailing(t.pagePath) === pagePath);
1223
+ return !!pagePath && this.tabBarList.some(t => runtime.stripTrailing(t.pagePath) === pagePath);
1224
+ }
1225
+ isDefaultNavigationStyle() {
1226
+ var _a, _b;
1227
+ let style = (_a = this.config.window) === null || _a === void 0 ? void 0 : _a.navigationStyle;
1228
+ if (typeof ((_b = this.pageConfig) === null || _b === void 0 ? void 0 : _b.navigationStyle) === 'string') {
1229
+ style = this.pageConfig.navigationStyle;
1230
+ }
1231
+ return style !== 'custom';
873
1232
  }
874
1233
  isSamePage(page) {
875
- const routePath = stripBasename(this.pathname, this.basename);
876
- const pagePath = stripBasename(page === null || page === void 0 ? void 0 : page.path, this.basename);
1234
+ const routePath = runtime.stripBasename(this.pathname, this.basename);
1235
+ const pagePath = runtime.stripBasename(page === null || page === void 0 ? void 0 : page.path, this.basename);
877
1236
  return pagePath.startsWith(routePath + '?');
878
1237
  }
879
1238
  get search() {
@@ -903,46 +1262,17 @@ class PageHandler {
903
1262
  getQuery(stamp = '', search = '', options = {}) {
904
1263
  search = search ? `${search}&${this.search}` : this.search;
905
1264
  const query = search
906
- ? queryString__default["default"].parse(search, { decode: false })
1265
+ ? queryString.parse(search, { decode: false })
907
1266
  : {};
908
1267
  query.stamp = stamp;
909
1268
  return Object.assign(Object.assign({}, query), options);
910
1269
  }
911
1270
  mount() {
912
- setHistoryMode(this.routerMode, this.router.basename);
1271
+ setHistory(this.history, this.basename);
913
1272
  this.pathname = exports.history.location.pathname;
1273
+ // Note: 注入页面样式
914
1274
  this.animation && loadAnimateStyle(this.animationDuration);
915
- loadRouterStyle(this.usingWindowScroll);
916
- const appId = this.appId;
917
- let app = document.getElementById(appId);
918
- let isPosition = true;
919
- if (!app) {
920
- app = document.createElement('div');
921
- app.id = appId;
922
- isPosition = false;
923
- }
924
- const appWrapper = (app === null || app === void 0 ? void 0 : app.parentNode) || (app === null || app === void 0 ? void 0 : app.parentElement) || document.body;
925
- app.classList.add('taro_router');
926
- if (this.tabBarList.length > 1) {
927
- const container = document.createElement('div');
928
- container.classList.add('taro-tabbar__container');
929
- container.id = 'container';
930
- const panel = document.createElement('div');
931
- panel.classList.add('taro-tabbar__panel');
932
- panel.appendChild(app.cloneNode(true));
933
- container.appendChild(panel);
934
- if (!isPosition) {
935
- appWrapper.appendChild(container);
936
- }
937
- else {
938
- appWrapper.replaceChild(container, app);
939
- }
940
- initTabbar(this.config);
941
- }
942
- else {
943
- if (!isPosition)
944
- appWrapper.appendChild(app);
945
- }
1275
+ loadRouterStyle(this.tabBarList.length > 1, this.usingWindowScroll);
946
1276
  }
947
1277
  onReady(page, onLoad = true) {
948
1278
  var _a;
@@ -977,19 +1307,24 @@ class PageHandler {
977
1307
  if (pageEl) {
978
1308
  pageEl.classList.remove('taro_page_shade');
979
1309
  this.isTabBar(this.pathname) && pageEl.classList.add('taro_tabbar_page');
1310
+ this.isDefaultNavigationStyle() && pageEl.classList.add('taro_navigation_page');
980
1311
  this.addAnimation(pageEl, pageNo === 0);
981
1312
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
1313
+ this.navigationBarHandler.load();
982
1314
  this.bindPageEvents(page, pageConfig);
983
1315
  this.triggerRouterChange();
984
1316
  }
985
1317
  else {
1318
+ // FIXME 在 iOS 端快速切换页面时,可能不会执行回调注入对应类名导致 TabBar 白屏
986
1319
  (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
987
1320
  var _a;
988
1321
  pageEl = this.getPageContainer(page);
989
1322
  this.isTabBar(this.pathname) && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
1323
+ this.isDefaultNavigationStyle() && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_navigation_page'));
990
1324
  this.addAnimation(pageEl, pageNo === 0);
991
- this.onReady(page, true);
992
1325
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
1326
+ this.navigationBarHandler.load();
1327
+ this.onReady(page, true);
993
1328
  this.bindPageEvents(page, pageConfig);
994
1329
  this.triggerRouterChange();
995
1330
  });
@@ -1043,6 +1378,7 @@ class PageHandler {
1043
1378
  pageEl.classList.remove('taro_page_shade');
1044
1379
  this.addAnimation(pageEl, pageNo === 0);
1045
1380
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
1381
+ this.navigationBarHandler.load();
1046
1382
  this.bindPageEvents(page, pageConfig);
1047
1383
  this.triggerRouterChange();
1048
1384
  }
@@ -1051,31 +1387,43 @@ class PageHandler {
1051
1387
  var _a;
1052
1388
  pageEl = this.getPageContainer(page);
1053
1389
  this.addAnimation(pageEl, pageNo === 0);
1054
- this.onReady(page, false);
1055
1390
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
1391
+ this.navigationBarHandler.load();
1392
+ this.onReady(page, false);
1056
1393
  this.bindPageEvents(page, pageConfig);
1057
1394
  this.triggerRouterChange();
1058
1395
  });
1059
1396
  }
1060
1397
  }
1061
- hide(page) {
1062
- var _a;
1398
+ hide(page, animation = false) {
1399
+ var _a, _b, _c, _d, _e, _f, _g;
1063
1400
  if (!page)
1064
1401
  return;
1065
1402
  // NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
1066
1403
  const pageEl = this.getPageContainer(page);
1067
1404
  if (pageEl) {
1068
- if (this.hideTimer) {
1069
- clearTimeout(this.hideTimer);
1070
- this.hideTimer = null;
1071
- pageEl.classList.add('taro_page_shade');
1405
+ if (animation) {
1406
+ if (this.hideTimer) {
1407
+ clearTimeout(this.hideTimer);
1408
+ this.hideTimer = null;
1409
+ (_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');
1410
+ }
1411
+ this.lastHidePage = pageEl;
1412
+ this.hideTimer = setTimeout(() => {
1413
+ this.hideTimer = null;
1414
+ pageEl.classList.add('taro_page_shade');
1415
+ }, this.animationDuration + this.animationDelay);
1416
+ (_d = page.onHide) === null || _d === void 0 ? void 0 : _d.call(page);
1072
1417
  }
1073
- this.lastHidePage = pageEl;
1074
- this.hideTimer = setTimeout(() => {
1075
- this.hideTimer = null;
1418
+ else {
1419
+ if (this.hideTimer) {
1420
+ clearTimeout(this.hideTimer);
1421
+ this.hideTimer = null;
1422
+ (_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');
1423
+ }
1076
1424
  pageEl.classList.add('taro_page_shade');
1077
- }, this.animationDuration + this.animationDelay);
1078
- (_a = page.onHide) === null || _a === void 0 ? void 0 : _a.call(page);
1425
+ this.lastHidePage = pageEl;
1426
+ }
1079
1427
  }
1080
1428
  else {
1081
1429
  setTimeout(() => this.hide(page), 0);
@@ -1139,24 +1487,28 @@ class PageHandler {
1139
1487
 
1140
1488
  const createStampId = runtime.incrementId();
1141
1489
  let launchStampId = createStampId();
1142
- function createRouter(app, config, framework) {
1490
+ function createRouter(history$1, app, config, framework) {
1143
1491
  var _a, _b;
1144
1492
  if (typeof app.onUnhandledRejection === 'function') {
1145
1493
  window.addEventListener('unhandledrejection', app.onUnhandledRejection);
1146
1494
  }
1147
1495
  RouterConfig.config = config;
1148
- const handler = new PageHandler(config);
1496
+ const handler = new PageHandler(config, history$1);
1497
+ // Note: 弱网情况下,快速切换 tab,会造成同个页面实例被多次挂在到页面上,原因是资源请求是异步的,短时间内发起多个请求,
1498
+ // 会在资源加载完成后,同时走到挂载的逻辑,造成 pageStampId 更新不及时,两个 page 的Id 相同,后面很多操作是通过 getElementById 来进行的
1499
+ // 所以需要加一个锁来应对这个情况
1500
+ const pageLock = {};
1149
1501
  routesAlias.set(handler.router.customRoutes);
1150
1502
  const basename = handler.router.basename;
1151
1503
  const routes = handler.routes.map(route => {
1152
- const routePath = addLeadingSlash(route.path);
1504
+ const routePath = runtime.addLeadingSlash(route.path);
1153
1505
  const paths = routesAlias.getAll(routePath);
1154
1506
  return {
1155
1507
  path: paths.length < 1 ? routePath : paths,
1156
1508
  action: route.load
1157
1509
  };
1158
1510
  });
1159
- const router = new UniversalRouter__default["default"](routes, { baseUrl: basename || '' });
1511
+ const router = new UniversalRouter(routes, { baseUrl: basename || '' });
1160
1512
  const launchParam = {
1161
1513
  path: handler.currentPage,
1162
1514
  query: handler.getQuery(launchStampId),
@@ -1167,29 +1519,35 @@ function createRouter(app, config, framework) {
1167
1519
  runtime.eventCenter.trigger('__taroRouterLaunch', launchParam);
1168
1520
  (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
1169
1521
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
1170
- const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
1171
- var _c, _d, _e, _f, _g, _h;
1172
- handler.pathname = decodeURI(location.pathname);
1173
- if ((_c = window.__taroAppConfig) === null || _c === void 0 ? void 0 : _c.usingWindowScroll)
1522
+ const render = (_c) => tslib.__awaiter(this, [_c], void 0, function* ({ location, action }) {
1523
+ var _d, _e, _f, _g, _h, _j, _k, _l;
1524
+ // Note: 由于下面有异步加载操作 先不要在这里去设置 handler.pathname
1525
+ const currentPathname = decodeURI(location.pathname);
1526
+ if ((_d = window.__taroAppConfig) === null || _d === void 0 ? void 0 : _d.usingWindowScroll)
1174
1527
  window.scrollTo(0, 0);
1175
1528
  runtime.eventCenter.trigger('__taroRouterChange', {
1176
1529
  toLocation: {
1177
- path: handler.pathname
1530
+ path: currentPathname
1178
1531
  }
1179
1532
  });
1180
- let element, params;
1533
+ let element, context, params;
1534
+ const routerPath = handler.router.forcePath || currentPathname;
1535
+ pageLock[routerPath] = typeof pageLock[routerPath] === 'number' ? pageLock[routerPath] + 1 : 1;
1536
+ const currentLock = pageLock[routerPath];
1537
+ let postLock;
1181
1538
  try {
1182
- const result = yield router.resolve(handler.router.forcePath || handler.pathname);
1183
- [element, , params] = yield Promise.all(result);
1539
+ const result = yield router.resolve(routerPath);
1540
+ [element, context, params] = yield Promise.all(result);
1541
+ postLock = pageLock[routerPath];
1184
1542
  }
1185
1543
  catch (error) {
1186
1544
  if (error.status === 404) {
1187
1545
  const notFoundEvent = {
1188
1546
  isEntryPage: stacks.length === 0,
1189
- path: handler.pathname,
1547
+ path: currentPathname,
1190
1548
  query: handler.getQuery(createStampId()),
1191
1549
  };
1192
- (_d = app.onPageNotFound) === null || _d === void 0 ? void 0 : _d.call(app, notFoundEvent);
1550
+ (_e = app.onPageNotFound) === null || _e === void 0 ? void 0 : _e.call(app, notFoundEvent);
1193
1551
  runtime.eventCenter.trigger('__taroRouterNotFound', notFoundEvent);
1194
1552
  }
1195
1553
  else if (/Loading hot update .* failed./.test(error.message)) {
@@ -1197,22 +1555,36 @@ function createRouter(app, config, framework) {
1197
1555
  window.location.reload();
1198
1556
  }
1199
1557
  else {
1200
- throw new Error(error);
1558
+ throw error;
1201
1559
  }
1202
1560
  }
1203
- if (!element)
1561
+ if (!element || currentLock !== postLock)
1204
1562
  return;
1205
- const pageConfig = handler.pageConfig;
1206
- let enablePullDownRefresh = ((_e = config === null || config === void 0 ? void 0 : config.window) === null || _e === void 0 ? void 0 : _e.enablePullDownRefresh) || false;
1563
+ // Note: 异步结束后,在设置 handler.pathname
1564
+ // context.pathname universal-router 被处理过了,是发起资源请求的时候传入的 pathname,即 await router.resolve(routerPath) 这个 routerPath
1565
+ handler.pathname = context.pathname;
1566
+ const { pathname, pageConfig } = handler;
1567
+ let enablePullDownRefresh = ((_f = config === null || config === void 0 ? void 0 : config.window) === null || _f === void 0 ? void 0 : _f.enablePullDownRefresh) || false;
1568
+ let navigationStyle = ((_g = config === null || config === void 0 ? void 0 : config.window) === null || _g === void 0 ? void 0 : _g.navigationStyle) || 'default';
1569
+ let navigationBarTextStyle = ((_h = config === null || config === void 0 ? void 0 : config.window) === null || _h === void 0 ? void 0 : _h.navigationBarTextStyle) || 'white';
1570
+ let navigationBarBackgroundColor = ((_j = config === null || config === void 0 ? void 0 : config.window) === null || _j === void 0 ? void 0 : _j.navigationBarBackgroundColor) || '#000000';
1207
1571
  if (pageConfig) {
1208
- setTitle((_f = pageConfig.navigationBarTitleText) !== null && _f !== void 0 ? _f : document.title);
1209
1572
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
1210
1573
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
1211
1574
  }
1575
+ if (typeof pageConfig.navigationStyle === 'string') {
1576
+ navigationStyle = pageConfig.navigationStyle;
1577
+ }
1578
+ if (typeof pageConfig.navigationBarTextStyle === 'string') {
1579
+ navigationBarTextStyle = pageConfig.navigationBarTextStyle;
1580
+ }
1581
+ if (typeof pageConfig.navigationBarBackgroundColor === 'string') {
1582
+ navigationBarBackgroundColor = pageConfig.navigationBarBackgroundColor;
1583
+ }
1212
1584
  }
1585
+ runtime.eventCenter.trigger('__taroSetNavigationStyle', navigationStyle, navigationBarTextStyle, navigationBarBackgroundColor);
1213
1586
  const currentPage = runtime.Current.page;
1214
- const pathname = handler.pathname;
1215
- const methodName = (_g = stacks.method) !== null && _g !== void 0 ? _g : '';
1587
+ const methodName = (_k = stacks.method) !== null && _k !== void 0 ? _k : '';
1216
1588
  const cacheTabs = stacks.getTabs();
1217
1589
  let shouldLoad = false;
1218
1590
  stacks.method = '';
@@ -1227,10 +1599,11 @@ function createRouter(app, config, framework) {
1227
1599
  }
1228
1600
  shouldLoad = true;
1229
1601
  }
1230
- else if (currentPage && handler.isTabBar(handler.pathname)) {
1602
+ else if (currentPage && handler.isTabBar(pathname)) {
1231
1603
  if (handler.isSamePage(currentPage))
1232
1604
  return;
1233
1605
  if (handler.isTabBar(currentPage.path)) {
1606
+ // NOTE: 从 tabBar 页面切换到 tabBar 页面
1234
1607
  handler.hide(currentPage);
1235
1608
  stacks.pushTab(currentPage.path.split('?')[0]);
1236
1609
  }
@@ -1244,8 +1617,8 @@ function createRouter(app, config, framework) {
1244
1617
  handler.unload(currentPage, stacks.length, true);
1245
1618
  }
1246
1619
  }
1247
- if (cacheTabs[handler.pathname]) {
1248
- stacks.popTab(handler.pathname);
1620
+ if (cacheTabs[pathname]) {
1621
+ stacks.popTab(pathname);
1249
1622
  return handler.show(stacks.getItem(0), pageConfig, 0);
1250
1623
  }
1251
1624
  shouldLoad = true;
@@ -1274,11 +1647,11 @@ function createRouter(app, config, framework) {
1274
1647
  shouldLoad = true;
1275
1648
  }
1276
1649
  else if (action === 'PUSH') {
1277
- handler.hide(currentPage);
1650
+ handler.hide(currentPage, true);
1278
1651
  shouldLoad = true;
1279
1652
  }
1280
1653
  if (shouldLoad || stacks.length < 1) {
1281
- const el = (_h = element.default) !== null && _h !== void 0 ? _h : element;
1654
+ const el = (_l = element.default) !== null && _l !== void 0 ? _l : element;
1282
1655
  const loadConfig = Object.assign({}, pageConfig);
1283
1656
  const stacksIndex = stacks.length;
1284
1657
  delete loadConfig['path'];
@@ -1297,21 +1670,102 @@ function createRouter(app, config, framework) {
1297
1670
  handler.load(page, pageConfig, pageStampId, stacksIndex);
1298
1671
  }
1299
1672
  });
1300
- const routePath = addLeadingSlash(stripBasename(exports.history.location.pathname, handler.basename));
1673
+ const routePath = runtime.addLeadingSlash(runtime.stripBasename(history$1.location.pathname, handler.basename));
1301
1674
  if (routePath === '/') {
1302
- exports.history.replace(prependBasename(handler.homePage + exports.history.location.search));
1675
+ history$1.replace(prependBasename(handler.homePage + history$1.location.search));
1303
1676
  }
1304
- render({ location: exports.history.location, action: history.Action.Push });
1677
+ render({ location: history$1.location, action: history.Action.Push });
1305
1678
  (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, launchParam);
1306
- return exports.history.listen(render);
1679
+ window.addEventListener('visibilitychange', () => {
1680
+ var _a, _b, _c, _d, _e, _f, _g;
1681
+ const currentPath = ((_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path) || '';
1682
+ const path = currentPath.substring(0, currentPath.indexOf('?'));
1683
+ const param = {};
1684
+ // app的 onShow/onHide 生命周期的路径信息为当前页面的路径
1685
+ Object.assign(param, launchParam, { path });
1686
+ if (document.visibilityState === 'visible') {
1687
+ (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, param);
1688
+ // 单页面app显示后一刻会触发当前 page.onShow 生命周期函数
1689
+ (_d = (_c = runtime.Current.page) === null || _c === void 0 ? void 0 : _c.onShow) === null || _d === void 0 ? void 0 : _d.call(_c);
1690
+ }
1691
+ else {
1692
+ // 单页面app隐藏前一刻会触发当前 page.onHide 生命周期函数
1693
+ if ((_e = runtime.Current.page) === null || _e === void 0 ? void 0 : _e.path) {
1694
+ runtime.safeExecute((_f = runtime.Current.page) === null || _f === void 0 ? void 0 : _f.path, 'onHide');
1695
+ }
1696
+ (_g = app.onHide) === null || _g === void 0 ? void 0 : _g.call(app, param);
1697
+ }
1698
+ });
1699
+ return history$1.listen(render);
1700
+ }
1701
+
1702
+ function handleAppMount(config, _, appId = config.appId || 'app') {
1703
+ let app = document.getElementById(appId);
1704
+ let isPosition = true;
1705
+ if (!app) {
1706
+ app = document.createElement('div');
1707
+ app.id = appId;
1708
+ isPosition = false;
1709
+ }
1710
+ const appWrapper = (app === null || app === void 0 ? void 0 : app.parentNode) || (app === null || app === void 0 ? void 0 : app.parentElement) || document.body;
1711
+ app.classList.add('taro_router');
1712
+ if (!isPosition)
1713
+ appWrapper.appendChild(app);
1714
+ initNavigationBar(config, appWrapper);
1715
+ }
1716
+ function handleAppMountWithTabbar(config, history, appId = config.appId || 'app') {
1717
+ let app = document.getElementById(appId);
1718
+ let isPosition = true;
1719
+ if (!app) {
1720
+ app = document.createElement('div');
1721
+ app.id = appId;
1722
+ isPosition = false;
1723
+ }
1724
+ const appWrapper = (app === null || app === void 0 ? void 0 : app.parentNode) || (app === null || app === void 0 ? void 0 : app.parentElement) || document.body;
1725
+ app.classList.add('taro_router');
1726
+ const container = document.createElement('div');
1727
+ container.classList.add('taro-tabbar__container');
1728
+ container.id = 'container';
1729
+ const panel = document.createElement('div');
1730
+ panel.classList.add('taro-tabbar__panel');
1731
+ panel.appendChild(app.cloneNode(true));
1732
+ container.appendChild(panel);
1733
+ if (!isPosition) {
1734
+ appWrapper.appendChild(container);
1735
+ }
1736
+ else {
1737
+ appWrapper.replaceChild(container, app);
1738
+ }
1739
+ initTabbar(config, history);
1740
+ initNavigationBar(config, container);
1307
1741
  }
1308
1742
 
1743
+ Object.defineProperty(exports, "createBrowserHistory", {
1744
+ enumerable: true,
1745
+ get: function () { return history.createBrowserHistory; }
1746
+ });
1747
+ Object.defineProperty(exports, "createHashHistory", {
1748
+ enumerable: true,
1749
+ get: function () { return history.createHashHistory; }
1750
+ });
1751
+ exports.createMpaHistory = createMpaHistory;
1309
1752
  exports.createMultiRouter = createMultiRouter;
1310
1753
  exports.createRouter = createRouter;
1311
1754
  exports.getCurrentPages = getCurrentPages;
1755
+ exports.handleAppMount = handleAppMount;
1756
+ exports.handleAppMountWithTabbar = handleAppMountWithTabbar;
1757
+ exports.isDingTalk = isDingTalk;
1758
+ exports.isWeixin = isWeixin;
1312
1759
  exports.navigateBack = navigateBack;
1313
1760
  exports.navigateTo = navigateTo;
1761
+ exports.prependBasename = prependBasename;
1314
1762
  exports.reLaunch = reLaunch;
1315
1763
  exports.redirectTo = redirectTo;
1764
+ exports.routesAlias = routesAlias;
1765
+ exports.setHistory = setHistory;
1766
+ exports.setHistoryMode = setHistoryMode;
1767
+ exports.setMpaTitle = setMpaTitle;
1768
+ exports.setNavigationBarLoading = setNavigationBarLoading;
1769
+ exports.setNavigationBarStyle = setNavigationBarStyle;
1770
+ exports.setTitle = setTitle;
1316
1771
  exports.switchTab = switchTab;
1317
- //# sourceMappingURL=index.cjs.js.map