@tarojs/router 4.0.0-alpha.2 → 4.0.0-alpha.21
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/LICENSE +8 -8
- package/dist/api.d.ts +6 -7
- package/dist/api.js +10 -17
- package/dist/events/resize.d.ts +2 -3
- package/dist/events/scroll.d.ts +2 -3
- package/dist/history.d.ts +10 -8
- package/dist/history.js +2 -1
- package/dist/index.cjs.js +363 -226
- package/dist/index.d.ts +9 -10
- package/dist/index.esm.js +362 -226
- package/dist/index.js +3 -3
- package/dist/navigationBar.d.ts +2 -2
- package/dist/navigationBar.js +24 -7
- package/dist/router/index.d.ts +3 -4
- package/dist/router/index.js +3 -2
- package/dist/router/mpa.d.ts +4 -5
- package/dist/router/mpa.js +12 -7
- package/dist/router/multi-page.d.ts +7 -8
- package/dist/router/navigation-bar.d.ts +8 -4
- package/dist/router/navigation-bar.js +62 -19
- package/dist/router/page.d.ts +11 -10
- package/dist/router/page.js +26 -14
- package/dist/router/spa.d.ts +4 -5
- package/dist/router/spa.js +51 -27
- package/dist/router/stack.d.ts +2 -2
- package/dist/style.d.ts +4 -5
- package/dist/style.js +52 -13
- package/dist/tabbar.d.ts +2 -3
- package/dist/utils/index.d.ts +2 -3
- package/dist/utils/navigate.d.ts +6 -6
- package/dist/utils/navigate.js +5 -2
- package/package.json +18 -27
- package/types/api.d.ts +5 -0
- package/dist/index.cjs.d.ts +0 -65
- package/dist/index.esm.d.ts +0 -65
package/dist/index.esm.js
CHANGED
|
@@ -1,12 +1,183 @@
|
|
|
1
1
|
import { defineCustomElementTaroTabbar } from '@tarojs/components/dist/components';
|
|
2
2
|
import { initTabBarApis } from '@tarojs/taro';
|
|
3
3
|
import { __awaiter } from 'tslib';
|
|
4
|
-
import {
|
|
4
|
+
import { addLeadingSlash, eventCenter, Current, stripBasename, incrementId, createPageConfig, hooks, stringify, getHomePage, getCurrentPage, stripTrailing, requestAnimationFrame as requestAnimationFrame$1, safeExecute } from '@tarojs/runtime';
|
|
5
|
+
import { EventChannel } from '@tarojs/shared';
|
|
5
6
|
import { createBrowserHistory, createHashHistory, Action, parsePath } from 'history';
|
|
6
7
|
export { createBrowserHistory, createHashHistory } from 'history';
|
|
7
8
|
import queryString from 'query-string';
|
|
8
9
|
import UniversalRouter from 'universal-router';
|
|
9
10
|
|
|
11
|
+
/**
|
|
12
|
+
* 插入页面动画需要的样式
|
|
13
|
+
*/
|
|
14
|
+
function loadAnimateStyle(ms = 300) {
|
|
15
|
+
const css = `
|
|
16
|
+
body {
|
|
17
|
+
/* 防止 iOS 页面滚动 */
|
|
18
|
+
overflow: hidden;
|
|
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:has(+.taro_page_stationed),
|
|
76
|
+
.taro_page_shade.taro_tabbar_page,
|
|
77
|
+
.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) {
|
|
78
|
+
display: none;
|
|
79
|
+
}
|
|
80
|
+
`;
|
|
81
|
+
addStyle(css);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* 插入导航栏相关的样式
|
|
85
|
+
*/
|
|
86
|
+
function loadNavigationBarStyle() {
|
|
87
|
+
const css = `
|
|
88
|
+
.taro-navigation-bar-show {
|
|
89
|
+
display: flex;
|
|
90
|
+
background: white;
|
|
91
|
+
position: sticky;
|
|
92
|
+
z-index: 500;
|
|
93
|
+
top: 0;
|
|
94
|
+
padding-bottom: 8px;
|
|
95
|
+
padding-top: calc(env(safe-area-inset-top) + 8px);
|
|
96
|
+
justify-content: center;
|
|
97
|
+
align-items: center;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
.taro-navigation-bar-hide {
|
|
101
|
+
display: none;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
.taro-navigation-bar-title-wrap {
|
|
105
|
+
display: flex;
|
|
106
|
+
height: 24px;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
.taro-navigation-bar-title-wrap > .taro-navigation-bar-loading {
|
|
110
|
+
display: none;
|
|
111
|
+
animation: loading 2s linear infinite;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
.taro-navigation-bar-title-wrap .taro-navigation-bar-loading.taro-navigation-bar-loading-show {
|
|
115
|
+
display: flex;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
.taro-navigation-bar-title-wrap > .taro-navigation-bar-title {
|
|
119
|
+
font-size: 24px;
|
|
120
|
+
height: 24px;
|
|
121
|
+
line-height: 24px;
|
|
122
|
+
max-width: 100px;
|
|
123
|
+
white-space: nowrap;
|
|
124
|
+
overflow: hidden;
|
|
125
|
+
line-height: 24px;
|
|
126
|
+
text-overflow: ellipsis;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
@keyframes loading {
|
|
130
|
+
from {
|
|
131
|
+
transform: rotate(0deg);
|
|
132
|
+
}
|
|
133
|
+
to {
|
|
134
|
+
transform: rotate(360deg);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
@keyframes loading {
|
|
139
|
+
from {
|
|
140
|
+
transform: rotate(0deg);
|
|
141
|
+
}
|
|
142
|
+
to {
|
|
143
|
+
transform: rotate(360deg);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
.taro-navigation-bar-no-icon > .taro-navigation-bar-home {
|
|
148
|
+
display: none;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
.taro-navigation-bar-no-icon > .taro-navigation-bar-back {
|
|
152
|
+
display: none;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
.taro-navigation-bar-home-icon > .taro-navigation-bar-home {
|
|
156
|
+
display: flex;
|
|
157
|
+
left: 8px;
|
|
158
|
+
position: absolute;
|
|
159
|
+
width: 24px;
|
|
160
|
+
height: 24px;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
.taro-navigation-bar-back-icon > .taro-navigation-bar-back {
|
|
164
|
+
display: flex;
|
|
165
|
+
left: 8px;
|
|
166
|
+
position: absolute;
|
|
167
|
+
width: 24px;
|
|
168
|
+
height: 24px;
|
|
169
|
+
}
|
|
170
|
+
`;
|
|
171
|
+
addStyle(css);
|
|
172
|
+
}
|
|
173
|
+
function addStyle(css) {
|
|
174
|
+
if (!css)
|
|
175
|
+
return;
|
|
176
|
+
const style = document.createElement('style');
|
|
177
|
+
style.innerHTML = css;
|
|
178
|
+
document.getElementsByTagName('head')[0].appendChild(style);
|
|
179
|
+
}
|
|
180
|
+
|
|
10
181
|
const home_svg_str = `
|
|
11
182
|
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
12
183
|
<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"/>
|
|
@@ -16,21 +187,36 @@ const back_svg_str = `
|
|
|
16
187
|
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
17
188
|
<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"/>
|
|
18
189
|
</svg>
|
|
19
|
-
|
|
20
190
|
`;
|
|
21
|
-
|
|
22
|
-
|
|
191
|
+
const loading_svg_str = `
|
|
192
|
+
<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>
|
|
193
|
+
`;
|
|
194
|
+
function initNavigationBar(config, container) {
|
|
195
|
+
if (config.router.mode === 'multi')
|
|
196
|
+
return;
|
|
197
|
+
const navigationBar = document.createElement('div');
|
|
23
198
|
navigationBar.classList.add('taro-navigation-bar-no-icon');
|
|
24
|
-
const navigationBarBackBtn = document.createElement('
|
|
25
|
-
|
|
199
|
+
const navigationBarBackBtn = document.createElement('div');
|
|
200
|
+
navigationBarBackBtn.classList.add('taro-navigation-bar-back');
|
|
201
|
+
const navigationBarHomeBtn = document.createElement('div');
|
|
202
|
+
navigationBarHomeBtn.classList.add('taro-navigation-bar-home');
|
|
26
203
|
navigationBarBackBtn.innerHTML = back_svg_str;
|
|
27
204
|
navigationBarHomeBtn.innerHTML = home_svg_str;
|
|
28
|
-
const
|
|
205
|
+
const navigationBarTitleWrap = document.createElement('div');
|
|
206
|
+
navigationBarTitleWrap.classList.add('taro-navigation-bar-title-wrap');
|
|
207
|
+
const navigationBarLoading = document.createElement('div');
|
|
208
|
+
navigationBarLoading.classList.add('taro-navigation-bar-loading');
|
|
209
|
+
navigationBarLoading.innerHTML = loading_svg_str;
|
|
210
|
+
const navigationBarTitle = document.createElement('div');
|
|
211
|
+
navigationBarTitle.classList.add('taro-navigation-bar-title');
|
|
212
|
+
navigationBarTitleWrap.appendChild(navigationBarLoading);
|
|
213
|
+
navigationBarTitleWrap.appendChild(navigationBarTitle);
|
|
29
214
|
navigationBar.appendChild(navigationBarHomeBtn);
|
|
30
215
|
navigationBar.appendChild(navigationBarBackBtn);
|
|
31
|
-
navigationBar.appendChild(
|
|
216
|
+
navigationBar.appendChild(navigationBarTitleWrap);
|
|
32
217
|
navigationBar.id = 'taro-navigation-bar';
|
|
33
218
|
container.prepend(navigationBar);
|
|
219
|
+
loadNavigationBarStyle();
|
|
34
220
|
}
|
|
35
221
|
|
|
36
222
|
function initTabbar(config, history) {
|
|
@@ -78,8 +264,9 @@ class RouterConfig {
|
|
|
78
264
|
return this.router.mode || 'hash';
|
|
79
265
|
}
|
|
80
266
|
static get customRoutes() { return this.router.customRoutes || {}; }
|
|
267
|
+
// 这个方法不考虑 basename 和 customRoutes,只判断原始的 url 是否在 pages 中
|
|
81
268
|
static isPage(url = '') {
|
|
82
|
-
return this.pages.findIndex(e =>
|
|
269
|
+
return this.pages.findIndex(e => addLeadingSlash(e) === url) !== -1;
|
|
83
270
|
}
|
|
84
271
|
}
|
|
85
272
|
|
|
@@ -106,7 +293,7 @@ class MpaHistory {
|
|
|
106
293
|
}
|
|
107
294
|
parseUrl(to) {
|
|
108
295
|
let url = to.pathname || '';
|
|
109
|
-
if (RouterConfig.isPage(url)) {
|
|
296
|
+
if (RouterConfig.isPage(addLeadingSlash(url))) {
|
|
110
297
|
url += '.html';
|
|
111
298
|
}
|
|
112
299
|
if (to.search) {
|
|
@@ -289,11 +476,14 @@ function setMpaTitle(title) {
|
|
|
289
476
|
}
|
|
290
477
|
}
|
|
291
478
|
function setTitle(title) {
|
|
292
|
-
eventCenter.trigger('
|
|
479
|
+
eventCenter.trigger('__taroH5SetNavigationBarTitle', title);
|
|
293
480
|
}
|
|
294
481
|
function setNavigationBarStyle(option) {
|
|
295
482
|
eventCenter.trigger('__taroH5setNavigationBarColor', option);
|
|
296
483
|
}
|
|
484
|
+
function setNavigationBarLoading(loading) {
|
|
485
|
+
eventCenter.trigger('__taroH5setNavigationBarLoading', loading);
|
|
486
|
+
}
|
|
297
487
|
|
|
298
488
|
class RoutesAlias {
|
|
299
489
|
constructor() {
|
|
@@ -336,6 +526,7 @@ class RoutesAlias {
|
|
|
336
526
|
}
|
|
337
527
|
const routesAlias = new RoutesAlias();
|
|
338
528
|
|
|
529
|
+
const routeEvtChannel = EventChannel.routeChannel;
|
|
339
530
|
function processNavigateUrl(option) {
|
|
340
531
|
var _a;
|
|
341
532
|
const pathPieces = parsePath(option.url);
|
|
@@ -344,13 +535,14 @@ function processNavigateUrl(option) {
|
|
|
344
535
|
const parts = routesAlias.getOrigin(history.location.pathname).split('/');
|
|
345
536
|
parts.pop();
|
|
346
537
|
pathPieces.pathname.split('/').forEach((item) => {
|
|
347
|
-
if (item === '.')
|
|
538
|
+
if (item === '.')
|
|
348
539
|
return;
|
|
349
|
-
}
|
|
350
540
|
item === '..' ? parts.pop() : parts.push(item);
|
|
351
541
|
});
|
|
352
542
|
pathPieces.pathname = parts.join('/');
|
|
353
543
|
}
|
|
544
|
+
// 确保是 / 开头的路径
|
|
545
|
+
pathPieces.pathname = addLeadingSlash(pathPieces.pathname);
|
|
354
546
|
// 处理自定义路由
|
|
355
547
|
pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname));
|
|
356
548
|
// 处理 basename
|
|
@@ -367,6 +559,10 @@ function navigate(option, method) {
|
|
|
367
559
|
const { success, complete, fail } = option;
|
|
368
560
|
const unListen = history.listen(() => {
|
|
369
561
|
const res = { errMsg: `${method}:ok` };
|
|
562
|
+
if (method === 'navigateTo') {
|
|
563
|
+
res.eventChannel = routeEvtChannel;
|
|
564
|
+
routeEvtChannel.addEvents(option.events);
|
|
565
|
+
}
|
|
370
566
|
success === null || success === void 0 ? void 0 : success(res);
|
|
371
567
|
complete === null || complete === void 0 ? void 0 : complete(res);
|
|
372
568
|
resolve(res);
|
|
@@ -376,22 +572,8 @@ function navigate(option, method) {
|
|
|
376
572
|
if ('url' in option) {
|
|
377
573
|
const pathPieces = processNavigateUrl(option);
|
|
378
574
|
const state = { timestamp: Date.now() };
|
|
379
|
-
if (pathPieces.pathname) {
|
|
380
|
-
const originPath = routesAlias.getOrigin(pathPieces.pathname);
|
|
381
|
-
const pagePath = originPath.startsWith('/') ? originPath.substring(1) : originPath;
|
|
382
|
-
if (!RouterConfig.pages.includes(pagePath)) {
|
|
383
|
-
const res = { errMsg: `${method}:fail page ${pagePath} is not found` };
|
|
384
|
-
fail === null || fail === void 0 ? void 0 : fail(res);
|
|
385
|
-
complete === null || complete === void 0 ? void 0 : complete(res);
|
|
386
|
-
if (fail || complete) {
|
|
387
|
-
return resolve(res);
|
|
388
|
-
}
|
|
389
|
-
else {
|
|
390
|
-
return reject(res);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
575
|
if (method === 'navigateTo') {
|
|
576
|
+
// Note: 由于 spa 会针对弱网情况下,短时间内多次跳转同一个页面跳转加了锁,后续如果有用户反馈返回无效,那可能是这个问题
|
|
395
577
|
history.push(pathPieces, state);
|
|
396
578
|
}
|
|
397
579
|
else if (method === 'redirectTo' || method === 'switchTab') {
|
|
@@ -506,137 +688,6 @@ function getOffset() {
|
|
|
506
688
|
}
|
|
507
689
|
}
|
|
508
690
|
|
|
509
|
-
/**
|
|
510
|
-
* 插入页面动画需要的样式
|
|
511
|
-
*/
|
|
512
|
-
function loadAnimateStyle(ms = 300) {
|
|
513
|
-
const css = `
|
|
514
|
-
body {
|
|
515
|
-
overflow: hidden; // 防止 iOS 页面滚动
|
|
516
|
-
}
|
|
517
|
-
.taro_router > .taro_page {
|
|
518
|
-
position: absolute;
|
|
519
|
-
left: 0;
|
|
520
|
-
top: 0;
|
|
521
|
-
width: 100%;
|
|
522
|
-
height: 100%;
|
|
523
|
-
background-color: #fff;
|
|
524
|
-
transform: translate(100%, 0);
|
|
525
|
-
transition: transform ${ms}ms;
|
|
526
|
-
z-index: 0;
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
.taro_router > .taro_page.taro_tabbar_page,
|
|
530
|
-
.taro_router > .taro_page.taro_page_show.taro_page_stationed {
|
|
531
|
-
transform: none;
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
.taro_router > .taro_page.taro_page_show {
|
|
535
|
-
transform: translate(0, 0);
|
|
536
|
-
}
|
|
537
|
-
`;
|
|
538
|
-
addStyle(css);
|
|
539
|
-
}
|
|
540
|
-
/**
|
|
541
|
-
* 插入路由相关样式
|
|
542
|
-
*/
|
|
543
|
-
function loadRouterStyle(enableTabBar, enableWindowScroll) {
|
|
544
|
-
const css = `
|
|
545
|
-
.taro_router {
|
|
546
|
-
position: relative;
|
|
547
|
-
width: 100%;
|
|
548
|
-
height: 100%;
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
.taro_page {
|
|
552
|
-
width: 100%;
|
|
553
|
-
height: 100%;
|
|
554
|
-
${enableWindowScroll ? '' : `
|
|
555
|
-
overflow-x: hidden;
|
|
556
|
-
overflow-y: scroll;
|
|
557
|
-
max-height: 100vh;
|
|
558
|
-
`}
|
|
559
|
-
}
|
|
560
|
-
${enableTabBar ? `
|
|
561
|
-
.taro-tabbar__container > .taro-tabbar__panel {
|
|
562
|
-
overflow: hidden;
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
.taro-tabbar__container > .taro-tabbar__panel > .taro_page.taro_tabbar_page {
|
|
566
|
-
max-height: calc(100vh - var(--taro-tabbar-height) - constant(safe-area-inset-bottom));
|
|
567
|
-
max-height: calc(100vh - var(--taro-tabbar-height) - env(safe-area-inset-bottom));
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
` : ''}
|
|
571
|
-
.taro_page_shade,
|
|
572
|
-
.taro_router > .taro_page.taro_page_show.taro_page_stationed:not(.taro_page_shade):not(.taro_tabbar_page):not(:last-child) {
|
|
573
|
-
display: none;
|
|
574
|
-
}
|
|
575
|
-
`;
|
|
576
|
-
addStyle(css);
|
|
577
|
-
}
|
|
578
|
-
/**
|
|
579
|
-
* 插入导航栏相关的样式
|
|
580
|
-
*/
|
|
581
|
-
function loadNavigationBarStyle() {
|
|
582
|
-
const css = `
|
|
583
|
-
.taro-navigation-bar-show {
|
|
584
|
-
background: white;
|
|
585
|
-
position: sticky;
|
|
586
|
-
z-index: 500;
|
|
587
|
-
top: 0;
|
|
588
|
-
padding-bottom: 8px;
|
|
589
|
-
padding-top: calc(env(safe-area-inset-top) + 8px);
|
|
590
|
-
display: flex;
|
|
591
|
-
justify-content: center;
|
|
592
|
-
align-items: center;
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
.taro-navigation-bar-hide {
|
|
596
|
-
display: none;
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
taro-navigation-bar-title {
|
|
600
|
-
font-size: 24px;
|
|
601
|
-
height: 24px;
|
|
602
|
-
line-height: 24px;
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
.taro-navigation-bar-no-icon > taro-navigation-bar-home {
|
|
606
|
-
display: none;
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
.taro-navigation-bar-no-icon > taro-navigation-bar-back {
|
|
610
|
-
display: none;
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
.taro-navigation-bar-home > taro-navigation-bar-home {
|
|
614
|
-
display: block;
|
|
615
|
-
left: 8px;
|
|
616
|
-
position: absolute;
|
|
617
|
-
width: 24px;
|
|
618
|
-
height: 24px;
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
.taro-navigation-bar-back > taro-navigation-bar-back {
|
|
622
|
-
display: block;
|
|
623
|
-
left: 8px;
|
|
624
|
-
position: absolute;
|
|
625
|
-
width: 24px;
|
|
626
|
-
height: 24px;
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
`;
|
|
630
|
-
addStyle(css);
|
|
631
|
-
}
|
|
632
|
-
function addStyle(css) {
|
|
633
|
-
if (!css)
|
|
634
|
-
return;
|
|
635
|
-
const style = document.createElement('style');
|
|
636
|
-
style.innerHTML = css;
|
|
637
|
-
document.getElementsByTagName('head')[0].appendChild(style);
|
|
638
|
-
}
|
|
639
|
-
|
|
640
691
|
/* eslint-disable dot-notation */
|
|
641
692
|
class MultiPageHandler {
|
|
642
693
|
constructor(config, history) {
|
|
@@ -792,16 +843,16 @@ const launchStampId$1 = createStampId$1();
|
|
|
792
843
|
* - 不支持路由动画
|
|
793
844
|
*/
|
|
794
845
|
function createMultiRouter(history, app, config, framework) {
|
|
795
|
-
var _a, _b, _c, _d, _e, _f;
|
|
796
846
|
return __awaiter(this, void 0, void 0, function* () {
|
|
847
|
+
var _a, _b, _c, _d, _e, _f;
|
|
797
848
|
if (typeof app.onUnhandledRejection === 'function') {
|
|
798
849
|
window.addEventListener('unhandledrejection', app.onUnhandledRejection);
|
|
799
850
|
}
|
|
800
|
-
eventCenter.on('
|
|
851
|
+
eventCenter.on('__taroH5SetNavigationBarTitle', setMpaTitle);
|
|
801
852
|
RouterConfig.config = config;
|
|
802
853
|
const handler = new MultiPageHandler(config, history);
|
|
803
854
|
const launchParam = {
|
|
804
|
-
path: config.pageName,
|
|
855
|
+
path: config.pageName, // 多页面模式没新开一个页面相当于重启,所以直接使用当前页面路径
|
|
805
856
|
query: handler.getQuery(launchStampId$1),
|
|
806
857
|
scene: 0,
|
|
807
858
|
shareTicket: '',
|
|
@@ -844,12 +895,17 @@ function createMultiRouter(history, app, config, framework) {
|
|
|
844
895
|
handler.load(page, pageConfig);
|
|
845
896
|
(_f = app.onShow) === null || _f === void 0 ? void 0 : _f.call(app, launchParam);
|
|
846
897
|
window.addEventListener('visibilitychange', () => {
|
|
847
|
-
var _a, _b;
|
|
898
|
+
var _a, _b, _c;
|
|
899
|
+
const currentPath = ((_a = Current.page) === null || _a === void 0 ? void 0 : _a.path) || '';
|
|
900
|
+
const path = currentPath.substring(0, currentPath.indexOf('?'));
|
|
901
|
+
const param = {};
|
|
902
|
+
// app的 onShow/onHide 生命周期的路径信息为当前页面的路径
|
|
903
|
+
Object.assign(param, launchParam, { path });
|
|
848
904
|
if (document.visibilityState === 'visible') {
|
|
849
|
-
(
|
|
905
|
+
(_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, param);
|
|
850
906
|
}
|
|
851
907
|
else {
|
|
852
|
-
(
|
|
908
|
+
(_c = app.onHide) === null || _c === void 0 ? void 0 : _c.call(app, param);
|
|
853
909
|
}
|
|
854
910
|
});
|
|
855
911
|
});
|
|
@@ -861,10 +917,12 @@ class NavigationBarHandler {
|
|
|
861
917
|
this.cache = {};
|
|
862
918
|
this.pageContext = pageContext;
|
|
863
919
|
this.init();
|
|
864
|
-
|
|
865
|
-
eventCenter.on('__taroH5SetNavigationTitle', (title) => {
|
|
920
|
+
eventCenter.on('__taroH5SetNavigationBarTitle', (title) => {
|
|
866
921
|
this.setTitle(title);
|
|
867
922
|
});
|
|
923
|
+
eventCenter.on('__taroH5setNavigationBarLoading', (loading) => {
|
|
924
|
+
this.setNavigationLoading(loading);
|
|
925
|
+
});
|
|
868
926
|
eventCenter.on('__taroH5setNavigationBarColor', ({ backgroundColor, frontColor }) => {
|
|
869
927
|
if (typeof backgroundColor === 'string')
|
|
870
928
|
this.setNavigationBarBackground(backgroundColor);
|
|
@@ -873,7 +931,7 @@ class NavigationBarHandler {
|
|
|
873
931
|
});
|
|
874
932
|
}
|
|
875
933
|
toHomeFn() {
|
|
876
|
-
reLaunch({ url: this.pageContext.
|
|
934
|
+
reLaunch({ url: this.pageContext.originHomePage });
|
|
877
935
|
}
|
|
878
936
|
backFn() {
|
|
879
937
|
navigateBack();
|
|
@@ -882,19 +940,24 @@ class NavigationBarHandler {
|
|
|
882
940
|
var _a;
|
|
883
941
|
if (!this.navigationBarElement)
|
|
884
942
|
return null;
|
|
885
|
-
return (_a = this.navigationBarElement.
|
|
943
|
+
return (_a = this.navigationBarElement.getElementsByClassName('taro-navigation-bar-home')) === null || _a === void 0 ? void 0 : _a[0];
|
|
886
944
|
}
|
|
887
945
|
get backBtnElement() {
|
|
888
946
|
var _a;
|
|
889
947
|
if (!this.navigationBarElement)
|
|
890
948
|
return null;
|
|
891
|
-
return (_a = this.navigationBarElement.
|
|
949
|
+
return (_a = this.navigationBarElement.getElementsByClassName('taro-navigation-bar-back')) === null || _a === void 0 ? void 0 : _a[0];
|
|
892
950
|
}
|
|
893
951
|
get titleElement() {
|
|
894
952
|
var _a;
|
|
895
953
|
if (!this.navigationBarElement)
|
|
896
954
|
return null;
|
|
897
|
-
return (_a = this.navigationBarElement.
|
|
955
|
+
return (_a = this.navigationBarElement.getElementsByClassName('taro-navigation-bar-title')) === null || _a === void 0 ? void 0 : _a[0];
|
|
956
|
+
}
|
|
957
|
+
get loadingElement() {
|
|
958
|
+
if (!this.navigationBarElement)
|
|
959
|
+
return null;
|
|
960
|
+
return this.navigationBarElement.getElementsByClassName('taro-navigation-bar-loading')[0];
|
|
898
961
|
}
|
|
899
962
|
init() {
|
|
900
963
|
var _a, _b;
|
|
@@ -905,8 +968,7 @@ class NavigationBarHandler {
|
|
|
905
968
|
(_b = this.backBtnElement) === null || _b === void 0 ? void 0 : _b.addEventListener('click', this.backFn.bind(this));
|
|
906
969
|
}
|
|
907
970
|
setNavigationBarElement() {
|
|
908
|
-
|
|
909
|
-
this.navigationBarElement = (_a = document.getElementsByTagName('taro-navigation-bar-wrap')) === null || _a === void 0 ? void 0 : _a[0];
|
|
971
|
+
this.navigationBarElement = document.getElementById('taro-navigation-bar');
|
|
910
972
|
}
|
|
911
973
|
load() {
|
|
912
974
|
this.setCacheValue();
|
|
@@ -915,9 +977,10 @@ class NavigationBarHandler {
|
|
|
915
977
|
this.setFnBtnState();
|
|
916
978
|
this.setNavigationBarBackground();
|
|
917
979
|
this.setNavigationBarTextStyle();
|
|
980
|
+
this.setNavigationLoading();
|
|
918
981
|
}
|
|
919
982
|
setCacheValue() {
|
|
920
|
-
const currentPage = this.pageContext.
|
|
983
|
+
const currentPage = this.pageContext.originPathname;
|
|
921
984
|
if (typeof this.cache[currentPage] !== 'object') {
|
|
922
985
|
this.cache[currentPage] = {};
|
|
923
986
|
}
|
|
@@ -934,11 +997,46 @@ class NavigationBarHandler {
|
|
|
934
997
|
this.fnBtnToggleToHome();
|
|
935
998
|
}
|
|
936
999
|
}
|
|
1000
|
+
shiftLoadingState(show) {
|
|
1001
|
+
if (!this.loadingElement)
|
|
1002
|
+
return;
|
|
1003
|
+
if (show) {
|
|
1004
|
+
this.loadingElement.classList.add('taro-navigation-bar-loading-show');
|
|
1005
|
+
}
|
|
1006
|
+
else {
|
|
1007
|
+
this.loadingElement.classList.remove('taro-navigation-bar-loading-show');
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
setNavigationLoading(show) {
|
|
1011
|
+
var _a;
|
|
1012
|
+
if (!this.navigationBarElement)
|
|
1013
|
+
return;
|
|
1014
|
+
const currentPage = this.pageContext.originPathname;
|
|
1015
|
+
let isShow;
|
|
1016
|
+
if (typeof show === 'boolean') {
|
|
1017
|
+
isShow = show;
|
|
1018
|
+
this.cache[currentPage] &&
|
|
1019
|
+
(this.cache[currentPage].loading = isShow);
|
|
1020
|
+
}
|
|
1021
|
+
else {
|
|
1022
|
+
const cacheValue = (_a = this.cache[currentPage]) === null || _a === void 0 ? void 0 : _a.loading;
|
|
1023
|
+
if (typeof cacheValue === 'boolean') {
|
|
1024
|
+
isShow = cacheValue;
|
|
1025
|
+
}
|
|
1026
|
+
else {
|
|
1027
|
+
// 默认值为 false
|
|
1028
|
+
isShow = false;
|
|
1029
|
+
this.cache[currentPage] &&
|
|
1030
|
+
(this.cache[currentPage].loading = isShow);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
this.shiftLoadingState(isShow);
|
|
1034
|
+
}
|
|
937
1035
|
setNavigationBarBackground(backgroundColor) {
|
|
938
1036
|
var _a, _b, _c;
|
|
939
1037
|
if (!this.navigationBarElement)
|
|
940
1038
|
return;
|
|
941
|
-
const currentPage = this.pageContext.
|
|
1039
|
+
const currentPage = this.pageContext.originPathname;
|
|
942
1040
|
let color;
|
|
943
1041
|
if (typeof backgroundColor === 'string') {
|
|
944
1042
|
color = backgroundColor;
|
|
@@ -962,7 +1060,7 @@ class NavigationBarHandler {
|
|
|
962
1060
|
var _a, _b, _c;
|
|
963
1061
|
if (!this.navigationBarElement)
|
|
964
1062
|
return;
|
|
965
|
-
const currentPage = this.pageContext.
|
|
1063
|
+
const currentPage = this.pageContext.originPathname;
|
|
966
1064
|
let color;
|
|
967
1065
|
if (typeof fontColor === 'string') {
|
|
968
1066
|
color = fontColor;
|
|
@@ -984,7 +1082,7 @@ class NavigationBarHandler {
|
|
|
984
1082
|
}
|
|
985
1083
|
setTitle(title) {
|
|
986
1084
|
var _a, _b, _c;
|
|
987
|
-
const currentPage = this.pageContext.
|
|
1085
|
+
const currentPage = this.pageContext.originPathname;
|
|
988
1086
|
let proceedTitle;
|
|
989
1087
|
if (typeof title === 'string') {
|
|
990
1088
|
proceedTitle = title;
|
|
@@ -1018,23 +1116,25 @@ class NavigationBarHandler {
|
|
|
1018
1116
|
fnBtnToggleToHome() {
|
|
1019
1117
|
if (!this.navigationBarElement)
|
|
1020
1118
|
return;
|
|
1021
|
-
this.navigationBarElement.classList.add('taro-navigation-bar-home');
|
|
1022
|
-
this.navigationBarElement.classList.remove('taro-navigation-bar-back');
|
|
1119
|
+
this.navigationBarElement.classList.add('taro-navigation-bar-home-icon');
|
|
1120
|
+
this.navigationBarElement.classList.remove('taro-navigation-bar-back-icon');
|
|
1023
1121
|
}
|
|
1024
1122
|
fnBtnToggleToBack() {
|
|
1025
1123
|
if (!this.navigationBarElement)
|
|
1026
1124
|
return;
|
|
1027
|
-
this.navigationBarElement.classList.remove('taro-navigation-bar-home');
|
|
1028
|
-
this.navigationBarElement.classList.add('taro-navigation-bar-back');
|
|
1125
|
+
this.navigationBarElement.classList.remove('taro-navigation-bar-home-icon');
|
|
1126
|
+
this.navigationBarElement.classList.add('taro-navigation-bar-back-icon');
|
|
1029
1127
|
}
|
|
1030
1128
|
fnBtnToggleToNone() {
|
|
1031
1129
|
if (!this.navigationBarElement)
|
|
1032
1130
|
return;
|
|
1033
|
-
this.navigationBarElement.classList.remove('taro-navigation-bar-home');
|
|
1034
|
-
this.navigationBarElement.classList.remove('taro-navigation-bar-back');
|
|
1131
|
+
this.navigationBarElement.classList.remove('taro-navigation-bar-home-icon');
|
|
1132
|
+
this.navigationBarElement.classList.remove('taro-navigation-bar-back-icon');
|
|
1035
1133
|
}
|
|
1036
1134
|
setNavigationBarVisible(show) {
|
|
1037
1135
|
var _a, _b;
|
|
1136
|
+
if (!this.navigationBarElement)
|
|
1137
|
+
return;
|
|
1038
1138
|
let shouldShow;
|
|
1039
1139
|
if (typeof show === 'boolean') {
|
|
1040
1140
|
shouldShow = show;
|
|
@@ -1063,6 +1163,7 @@ class PageHandler {
|
|
|
1063
1163
|
this.defaultAnimation = { duration: 300, delay: 50 };
|
|
1064
1164
|
this.config = config;
|
|
1065
1165
|
this.homePage = getHomePage(this.routes[0].path, this.basename, this.customRoutes, this.config.entryPagePath);
|
|
1166
|
+
this.originHomePage = this.config.entryPagePath || this.routes[0].path || this.basename;
|
|
1066
1167
|
this.mount();
|
|
1067
1168
|
this.navigationBarHandler = new NavigationBarHandler(this);
|
|
1068
1169
|
}
|
|
@@ -1096,14 +1197,14 @@ class PageHandler {
|
|
|
1096
1197
|
}
|
|
1097
1198
|
set pathname(p) { this.router.pathname = p; }
|
|
1098
1199
|
get pathname() { return this.router.pathname; }
|
|
1200
|
+
// Note: 把 pathname 转换为原始路径,主要是处理 customRoutes 和 basename
|
|
1201
|
+
get originPathname() { return routesAlias.getOrigin(addLeadingSlash(stripBasename(this.pathname, this.basename))); }
|
|
1099
1202
|
get basename() { return this.router.basename || ''; }
|
|
1100
1203
|
get pageConfig() {
|
|
1101
|
-
const routePath = addLeadingSlash(stripBasename(this.pathname, this.basename));
|
|
1102
1204
|
const homePage = addLeadingSlash(this.homePage);
|
|
1103
1205
|
return this.routes.find(r => {
|
|
1104
|
-
var _a;
|
|
1105
1206
|
const pagePath = addLeadingSlash(r.path);
|
|
1106
|
-
return [pagePath, homePage].includes(
|
|
1207
|
+
return [pagePath, homePage].includes(this.originPathname);
|
|
1107
1208
|
});
|
|
1108
1209
|
}
|
|
1109
1210
|
isTabBar(pathname) {
|
|
@@ -1293,24 +1394,35 @@ class PageHandler {
|
|
|
1293
1394
|
});
|
|
1294
1395
|
}
|
|
1295
1396
|
}
|
|
1296
|
-
hide(page) {
|
|
1297
|
-
var _a;
|
|
1397
|
+
hide(page, animation = false) {
|
|
1398
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
1298
1399
|
if (!page)
|
|
1299
1400
|
return;
|
|
1300
1401
|
// NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
|
|
1301
1402
|
const pageEl = this.getPageContainer(page);
|
|
1302
1403
|
if (pageEl) {
|
|
1303
|
-
if (
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1404
|
+
if (animation) {
|
|
1405
|
+
if (this.hideTimer) {
|
|
1406
|
+
clearTimeout(this.hideTimer);
|
|
1407
|
+
this.hideTimer = null;
|
|
1408
|
+
(_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');
|
|
1409
|
+
}
|
|
1410
|
+
this.lastHidePage = pageEl;
|
|
1411
|
+
this.hideTimer = setTimeout(() => {
|
|
1412
|
+
this.hideTimer = null;
|
|
1413
|
+
pageEl.classList.add('taro_page_shade');
|
|
1414
|
+
}, this.animationDuration + this.animationDelay);
|
|
1415
|
+
(_d = page.onHide) === null || _d === void 0 ? void 0 : _d.call(page);
|
|
1307
1416
|
}
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1417
|
+
else {
|
|
1418
|
+
if (this.hideTimer) {
|
|
1419
|
+
clearTimeout(this.hideTimer);
|
|
1420
|
+
this.hideTimer = null;
|
|
1421
|
+
(_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');
|
|
1422
|
+
}
|
|
1311
1423
|
pageEl.classList.add('taro_page_shade');
|
|
1312
|
-
|
|
1313
|
-
|
|
1424
|
+
this.lastHidePage = pageEl;
|
|
1425
|
+
}
|
|
1314
1426
|
}
|
|
1315
1427
|
else {
|
|
1316
1428
|
setTimeout(() => this.hide(page), 0);
|
|
@@ -1381,6 +1493,10 @@ function createRouter(history, app, config, framework) {
|
|
|
1381
1493
|
}
|
|
1382
1494
|
RouterConfig.config = config;
|
|
1383
1495
|
const handler = new PageHandler(config, history);
|
|
1496
|
+
// Note: 弱网情况下,快速切换 tab,会造成同个页面实例被多次挂在到页面上,原因是资源请求是异步的,短时间内发起多个请求,
|
|
1497
|
+
// 会在资源加载完成后,同时走到挂载的逻辑,造成 pageStampId 更新不及时,两个 page 的Id 相同,后面很多操作是通过 getElementById 来进行的
|
|
1498
|
+
// 所以需要加一个锁来应对这个情况
|
|
1499
|
+
const pageLock = {};
|
|
1384
1500
|
routesAlias.set(handler.router.customRoutes);
|
|
1385
1501
|
const basename = handler.router.basename;
|
|
1386
1502
|
const routes = handler.routes.map(route => {
|
|
@@ -1402,29 +1518,35 @@ function createRouter(history, app, config, framework) {
|
|
|
1402
1518
|
eventCenter.trigger('__taroRouterLaunch', launchParam);
|
|
1403
1519
|
(_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
|
|
1404
1520
|
app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
|
|
1405
|
-
const render = (
|
|
1406
|
-
var
|
|
1407
|
-
|
|
1408
|
-
|
|
1521
|
+
const render = (_c) => __awaiter(this, [_c], void 0, function* ({ location, action }) {
|
|
1522
|
+
var _d, _e, _f, _g, _h, _j, _k, _l;
|
|
1523
|
+
// Note: 由于下面有异步加载操作 先不要在这里去设置 handler.pathname
|
|
1524
|
+
const currentPathname = decodeURI(location.pathname);
|
|
1525
|
+
if ((_d = window.__taroAppConfig) === null || _d === void 0 ? void 0 : _d.usingWindowScroll)
|
|
1409
1526
|
window.scrollTo(0, 0);
|
|
1410
1527
|
eventCenter.trigger('__taroRouterChange', {
|
|
1411
1528
|
toLocation: {
|
|
1412
|
-
path:
|
|
1529
|
+
path: currentPathname
|
|
1413
1530
|
}
|
|
1414
1531
|
});
|
|
1415
|
-
let element, params;
|
|
1532
|
+
let element, context, params;
|
|
1533
|
+
const routerPath = handler.router.forcePath || currentPathname;
|
|
1534
|
+
pageLock[routerPath] = typeof pageLock[routerPath] === 'number' ? pageLock[routerPath] + 1 : 1;
|
|
1535
|
+
const currentLock = pageLock[routerPath];
|
|
1536
|
+
let postLock;
|
|
1416
1537
|
try {
|
|
1417
|
-
const result = yield router.resolve(
|
|
1418
|
-
[element, , params] = yield Promise.all(result);
|
|
1538
|
+
const result = yield router.resolve(routerPath);
|
|
1539
|
+
[element, context, params] = yield Promise.all(result);
|
|
1540
|
+
postLock = pageLock[routerPath];
|
|
1419
1541
|
}
|
|
1420
1542
|
catch (error) {
|
|
1421
1543
|
if (error.status === 404) {
|
|
1422
1544
|
const notFoundEvent = {
|
|
1423
1545
|
isEntryPage: stacks.length === 0,
|
|
1424
|
-
path:
|
|
1546
|
+
path: currentPathname,
|
|
1425
1547
|
query: handler.getQuery(createStampId()),
|
|
1426
1548
|
};
|
|
1427
|
-
(
|
|
1549
|
+
(_e = app.onPageNotFound) === null || _e === void 0 ? void 0 : _e.call(app, notFoundEvent);
|
|
1428
1550
|
eventCenter.trigger('__taroRouterNotFound', notFoundEvent);
|
|
1429
1551
|
}
|
|
1430
1552
|
else if (/Loading hot update .* failed./.test(error.message)) {
|
|
@@ -1435,13 +1557,16 @@ function createRouter(history, app, config, framework) {
|
|
|
1435
1557
|
throw error;
|
|
1436
1558
|
}
|
|
1437
1559
|
}
|
|
1438
|
-
if (!element)
|
|
1560
|
+
if (!element || currentLock !== postLock)
|
|
1439
1561
|
return;
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
let
|
|
1562
|
+
// Note: 异步结束后,在设置 handler.pathname
|
|
1563
|
+
// context.pathname 在 universal-router 被处理过了,是发起资源请求的时候传入的 pathname,即 await router.resolve(routerPath) 这个 routerPath
|
|
1564
|
+
handler.pathname = context.pathname;
|
|
1565
|
+
const { pathname, pageConfig } = handler;
|
|
1566
|
+
let enablePullDownRefresh = ((_f = config === null || config === void 0 ? void 0 : config.window) === null || _f === void 0 ? void 0 : _f.enablePullDownRefresh) || false;
|
|
1567
|
+
let navigationStyle = ((_g = config === null || config === void 0 ? void 0 : config.window) === null || _g === void 0 ? void 0 : _g.navigationStyle) || 'default';
|
|
1568
|
+
let navigationBarTextStyle = ((_h = config === null || config === void 0 ? void 0 : config.window) === null || _h === void 0 ? void 0 : _h.navigationBarTextStyle) || 'white';
|
|
1569
|
+
let navigationBarBackgroundColor = ((_j = config === null || config === void 0 ? void 0 : config.window) === null || _j === void 0 ? void 0 : _j.navigationBarBackgroundColor) || '#000000';
|
|
1445
1570
|
if (pageConfig) {
|
|
1446
1571
|
if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
|
|
1447
1572
|
enablePullDownRefresh = pageConfig.enablePullDownRefresh;
|
|
@@ -1458,8 +1583,7 @@ function createRouter(history, app, config, framework) {
|
|
|
1458
1583
|
}
|
|
1459
1584
|
eventCenter.trigger('__taroSetNavigationStyle', navigationStyle, navigationBarTextStyle, navigationBarBackgroundColor);
|
|
1460
1585
|
const currentPage = Current.page;
|
|
1461
|
-
const
|
|
1462
|
-
const methodName = (_j = stacks.method) !== null && _j !== void 0 ? _j : '';
|
|
1586
|
+
const methodName = (_k = stacks.method) !== null && _k !== void 0 ? _k : '';
|
|
1463
1587
|
const cacheTabs = stacks.getTabs();
|
|
1464
1588
|
let shouldLoad = false;
|
|
1465
1589
|
stacks.method = '';
|
|
@@ -1474,10 +1598,11 @@ function createRouter(history, app, config, framework) {
|
|
|
1474
1598
|
}
|
|
1475
1599
|
shouldLoad = true;
|
|
1476
1600
|
}
|
|
1477
|
-
else if (currentPage && handler.isTabBar(
|
|
1601
|
+
else if (currentPage && handler.isTabBar(pathname)) {
|
|
1478
1602
|
if (handler.isSamePage(currentPage))
|
|
1479
1603
|
return;
|
|
1480
1604
|
if (handler.isTabBar(currentPage.path)) {
|
|
1605
|
+
// NOTE: 从 tabBar 页面切换到 tabBar 页面
|
|
1481
1606
|
handler.hide(currentPage);
|
|
1482
1607
|
stacks.pushTab(currentPage.path.split('?')[0]);
|
|
1483
1608
|
}
|
|
@@ -1491,8 +1616,8 @@ function createRouter(history, app, config, framework) {
|
|
|
1491
1616
|
handler.unload(currentPage, stacks.length, true);
|
|
1492
1617
|
}
|
|
1493
1618
|
}
|
|
1494
|
-
if (cacheTabs[
|
|
1495
|
-
stacks.popTab(
|
|
1619
|
+
if (cacheTabs[pathname]) {
|
|
1620
|
+
stacks.popTab(pathname);
|
|
1496
1621
|
return handler.show(stacks.getItem(0), pageConfig, 0);
|
|
1497
1622
|
}
|
|
1498
1623
|
shouldLoad = true;
|
|
@@ -1521,11 +1646,11 @@ function createRouter(history, app, config, framework) {
|
|
|
1521
1646
|
shouldLoad = true;
|
|
1522
1647
|
}
|
|
1523
1648
|
else if (action === 'PUSH') {
|
|
1524
|
-
handler.hide(currentPage);
|
|
1649
|
+
handler.hide(currentPage, true);
|
|
1525
1650
|
shouldLoad = true;
|
|
1526
1651
|
}
|
|
1527
1652
|
if (shouldLoad || stacks.length < 1) {
|
|
1528
|
-
const el = (
|
|
1653
|
+
const el = (_l = element.default) !== null && _l !== void 0 ? _l : element;
|
|
1529
1654
|
const loadConfig = Object.assign({}, pageConfig);
|
|
1530
1655
|
const stacksIndex = stacks.length;
|
|
1531
1656
|
delete loadConfig['path'];
|
|
@@ -1551,12 +1676,23 @@ function createRouter(history, app, config, framework) {
|
|
|
1551
1676
|
render({ location: history.location, action: Action.Push });
|
|
1552
1677
|
(_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, launchParam);
|
|
1553
1678
|
window.addEventListener('visibilitychange', () => {
|
|
1554
|
-
var _a, _b;
|
|
1679
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
1680
|
+
const currentPath = ((_a = Current.page) === null || _a === void 0 ? void 0 : _a.path) || '';
|
|
1681
|
+
const path = currentPath.substring(0, currentPath.indexOf('?'));
|
|
1682
|
+
const param = {};
|
|
1683
|
+
// app的 onShow/onHide 生命周期的路径信息为当前页面的路径
|
|
1684
|
+
Object.assign(param, launchParam, { path });
|
|
1555
1685
|
if (document.visibilityState === 'visible') {
|
|
1556
|
-
(
|
|
1686
|
+
(_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, param);
|
|
1687
|
+
// 单页面app显示后一刻会触发当前 page.onShow 生命周期函数
|
|
1688
|
+
(_d = (_c = Current.page) === null || _c === void 0 ? void 0 : _c.onShow) === null || _d === void 0 ? void 0 : _d.call(_c);
|
|
1557
1689
|
}
|
|
1558
1690
|
else {
|
|
1559
|
-
|
|
1691
|
+
// 单页面app隐藏前一刻会触发当前 page.onHide 生命周期函数
|
|
1692
|
+
if ((_e = Current.page) === null || _e === void 0 ? void 0 : _e.path) {
|
|
1693
|
+
safeExecute((_f = Current.page) === null || _f === void 0 ? void 0 : _f.path, 'onHide');
|
|
1694
|
+
}
|
|
1695
|
+
(_g = app.onHide) === null || _g === void 0 ? void 0 : _g.call(app, param);
|
|
1560
1696
|
}
|
|
1561
1697
|
});
|
|
1562
1698
|
return history.listen(render);
|
|
@@ -1574,7 +1710,7 @@ function handleAppMount(config, _, appId = config.appId || 'app') {
|
|
|
1574
1710
|
app.classList.add('taro_router');
|
|
1575
1711
|
if (!isPosition)
|
|
1576
1712
|
appWrapper.appendChild(app);
|
|
1577
|
-
initNavigationBar(appWrapper);
|
|
1713
|
+
initNavigationBar(config, appWrapper);
|
|
1578
1714
|
}
|
|
1579
1715
|
function handleAppMountWithTabbar(config, history, appId = config.appId || 'app') {
|
|
1580
1716
|
let app = document.getElementById(appId);
|
|
@@ -1600,7 +1736,7 @@ function handleAppMountWithTabbar(config, history, appId = config.appId || 'app'
|
|
|
1600
1736
|
appWrapper.replaceChild(container, app);
|
|
1601
1737
|
}
|
|
1602
1738
|
initTabbar(config, history);
|
|
1603
|
-
initNavigationBar(container);
|
|
1739
|
+
initNavigationBar(config, container);
|
|
1604
1740
|
}
|
|
1605
1741
|
|
|
1606
|
-
export { createMpaHistory, createMultiRouter, createRouter, getCurrentPages, handleAppMount, handleAppMountWithTabbar, history, isDingTalk, isWeixin, navigateBack, navigateTo, prependBasename, reLaunch, redirectTo, routesAlias, setHistory, setHistoryMode, setMpaTitle, setNavigationBarStyle, setTitle, switchTab };
|
|
1742
|
+
export { createMpaHistory, createMultiRouter, createRouter, getCurrentPages, handleAppMount, handleAppMountWithTabbar, history, isDingTalk, isWeixin, navigateBack, navigateTo, prependBasename, reLaunch, redirectTo, routesAlias, setHistory, setHistoryMode, setMpaTitle, setNavigationBarLoading, setNavigationBarStyle, setTitle, switchTab };
|