@tarojs/router 3.4.5 → 3.5.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/animation.js +29 -0
- package/dist/api.js +91 -0
- package/dist/events/resize.js +13 -0
- package/dist/events/scroll.js +30 -0
- package/dist/history.js +99 -0
- package/dist/{router.esm.js → index.cjs.js} +375 -110
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.js +4 -688
- package/dist/router/index.js +23 -0
- package/dist/router/mpa.js +49 -0
- package/dist/router/multi-page.js +105 -0
- package/dist/router/page.js +265 -0
- package/dist/router/spa.js +110 -0
- package/dist/router/stack.js +57 -0
- package/dist/tabbar.js +29 -0
- package/dist/utils.js +39 -0
- package/package.json +9 -9
- package/types/index.d.ts +2 -1
- package/dist/index.js.map +0 -1
- package/dist/router.esm.js.map +0 -1
|
@@ -1,29 +1,178 @@
|
|
|
1
|
-
|
|
2
|
-
import { requestAnimationFrame, Current, container, SERVICE_IDENTIFIER, eventCenter, createPageConfig, stringify } from '@tarojs/runtime';
|
|
3
|
-
import UniversalRouter from 'universal-router';
|
|
4
|
-
import queryString from 'query-string';
|
|
5
|
-
import { initTabBarApis } from '@tarojs/taro';
|
|
1
|
+
'use strict';
|
|
6
2
|
|
|
7
|
-
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var history = require('history');
|
|
6
|
+
var runtime = require('@tarojs/runtime');
|
|
7
|
+
var UniversalRouter = require('universal-router');
|
|
8
|
+
var queryString = require('query-string');
|
|
9
|
+
var taro = require('@tarojs/taro');
|
|
10
|
+
|
|
11
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
12
|
+
|
|
13
|
+
var UniversalRouter__default = /*#__PURE__*/_interopDefaultLegacy(UniversalRouter);
|
|
14
|
+
var queryString__default = /*#__PURE__*/_interopDefaultLegacy(queryString);
|
|
15
|
+
|
|
16
|
+
const addLeadingSlash = (url = '') => (url.charAt(0) === '/' ? url : '/' + url);
|
|
17
|
+
const hasBasename = (path = '', prefix = '') => new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path) || path === prefix;
|
|
18
|
+
const stripBasename = (path = '', prefix = '') => hasBasename(path, prefix) ? path.substr(prefix.length) : path;
|
|
19
|
+
class RoutesAlias {
|
|
20
|
+
conf = [];
|
|
21
|
+
set(customRoutes = {}) {
|
|
22
|
+
for (let key in customRoutes) {
|
|
23
|
+
const path = customRoutes[key];
|
|
24
|
+
key = addLeadingSlash(key);
|
|
25
|
+
if (typeof path === 'string') {
|
|
26
|
+
this.conf.push([key, addLeadingSlash(path)]);
|
|
27
|
+
}
|
|
28
|
+
else if (path?.length > 0) {
|
|
29
|
+
this.conf.push(...path.map(p => [key, addLeadingSlash(p)]));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
getConfig = (url = '') => {
|
|
34
|
+
const customRoute = this.conf.filter((arr) => {
|
|
35
|
+
return arr.includes(url);
|
|
36
|
+
});
|
|
37
|
+
return customRoute[0];
|
|
38
|
+
};
|
|
39
|
+
getOrigin = (url = '') => {
|
|
40
|
+
return this.getConfig(url)?.[0] || url;
|
|
41
|
+
};
|
|
42
|
+
getAlias = (url = '') => {
|
|
43
|
+
return this.getConfig(url)?.[1] || url;
|
|
44
|
+
};
|
|
45
|
+
getAll = (url = '') => {
|
|
46
|
+
return this.conf
|
|
47
|
+
.filter((arr) => arr.includes(url))
|
|
48
|
+
.reduceRight((p, a) => {
|
|
49
|
+
p.unshift(a[1]);
|
|
50
|
+
return p;
|
|
51
|
+
}, []);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const routesAlias = new RoutesAlias();
|
|
55
|
+
|
|
56
|
+
class RouterConfig {
|
|
57
|
+
static __config;
|
|
58
|
+
static set config(e) {
|
|
59
|
+
this.__config = e;
|
|
60
|
+
}
|
|
61
|
+
static get config() {
|
|
62
|
+
return this.__config;
|
|
63
|
+
}
|
|
64
|
+
static get pages() {
|
|
65
|
+
return this.config.pages || [];
|
|
66
|
+
}
|
|
67
|
+
static get router() {
|
|
68
|
+
return this.config.router || {};
|
|
69
|
+
}
|
|
70
|
+
static get mode() {
|
|
71
|
+
return this.router.mode || 'hash';
|
|
72
|
+
}
|
|
73
|
+
static get customRoutes() { return this.router.customRoutes || {}; }
|
|
74
|
+
static isPage(url = '') {
|
|
75
|
+
return this.pages.findIndex(e => addLeadingSlash(e) === url) !== -1;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
exports.history = void 0;
|
|
8
80
|
let basename = '/';
|
|
81
|
+
class MpaHistory {
|
|
82
|
+
action;
|
|
83
|
+
get location() {
|
|
84
|
+
return {
|
|
85
|
+
pathname: window.location.pathname,
|
|
86
|
+
search: window.location.search,
|
|
87
|
+
hash: window.location.hash,
|
|
88
|
+
key: `${window.history.length}`,
|
|
89
|
+
state: window.history.state
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
createHref(_to) {
|
|
93
|
+
throw new Error('Method not implemented.');
|
|
94
|
+
}
|
|
95
|
+
parseUrl(to) {
|
|
96
|
+
let url = to.pathname || '';
|
|
97
|
+
if (RouterConfig.isPage(url)) {
|
|
98
|
+
url += '.html';
|
|
99
|
+
}
|
|
100
|
+
if (to.search) {
|
|
101
|
+
url += `?${to.search}`;
|
|
102
|
+
}
|
|
103
|
+
if (to.hash) {
|
|
104
|
+
url += `#${to.hash}`;
|
|
105
|
+
}
|
|
106
|
+
return url;
|
|
107
|
+
}
|
|
108
|
+
push(to, _state = {}) {
|
|
109
|
+
window.location.pathname = this.parseUrl(to);
|
|
110
|
+
// this.pushState(_state, '', this.parseUrl(to))
|
|
111
|
+
}
|
|
112
|
+
replace(to, _state = {}) {
|
|
113
|
+
window.location.replace(this.parseUrl(to));
|
|
114
|
+
// this.replaceState(_state, '', this.parseUrl(to))
|
|
115
|
+
}
|
|
116
|
+
go(delta) {
|
|
117
|
+
window.history.go(delta);
|
|
118
|
+
}
|
|
119
|
+
back = window.history.back;
|
|
120
|
+
forward = window.history.forward;
|
|
121
|
+
listen(listener) {
|
|
122
|
+
function callback(e) {
|
|
123
|
+
if (e.action === 'pushState') {
|
|
124
|
+
listener({ action: history.Action.Push, location: this.location });
|
|
125
|
+
}
|
|
126
|
+
else if (e.action === 'replaceState') {
|
|
127
|
+
listener({ action: history.Action.Replace, location: this.location });
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
// NOTE: 这里包括 back、forward、go 三种可能,并非是 POP 事件
|
|
131
|
+
listener({ action: history.Action.Pop, location: this.location });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
window.addEventListener('popstate', callback);
|
|
135
|
+
return () => {
|
|
136
|
+
window.removeEventListener('popstate', callback);
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
block(_blocker) {
|
|
140
|
+
throw new Error('Method not implemented.');
|
|
141
|
+
}
|
|
142
|
+
pushState = this.eventState('pushState');
|
|
143
|
+
replaceState = this.eventState('replaceState');
|
|
144
|
+
eventState(action) {
|
|
145
|
+
return (data, unused, url) => {
|
|
146
|
+
const wrapper = window.history[action](data, unused, url);
|
|
147
|
+
const evt = new Event(action);
|
|
148
|
+
evt.action = action;
|
|
149
|
+
evt.state = data;
|
|
150
|
+
evt.unused = unused;
|
|
151
|
+
evt.url = url;
|
|
152
|
+
window.dispatchEvent(evt);
|
|
153
|
+
return wrapper;
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
}
|
|
9
157
|
function setHistoryMode(mode, base = '/') {
|
|
10
158
|
const options = {
|
|
11
159
|
window
|
|
12
160
|
};
|
|
13
161
|
basename = base;
|
|
14
162
|
if (mode === 'browser') {
|
|
15
|
-
history = createBrowserHistory(options);
|
|
163
|
+
exports.history = history.createBrowserHistory(options);
|
|
164
|
+
}
|
|
165
|
+
else if (mode === 'multi') {
|
|
166
|
+
exports.history = new MpaHistory();
|
|
16
167
|
}
|
|
17
168
|
else {
|
|
18
169
|
// default is hash
|
|
19
|
-
history = createHashHistory(options);
|
|
170
|
+
exports.history = history.createHashHistory(options);
|
|
20
171
|
}
|
|
21
172
|
}
|
|
22
173
|
function prependBasename(url = '') {
|
|
23
174
|
return basename.replace(/\/$/, '') + '/' + url.replace(/^\//, '');
|
|
24
175
|
}
|
|
25
|
-
const hasBasename = (path = '', prefix = '') => new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path);
|
|
26
|
-
const stripBasename = (path = '', prefix = '') => hasBasename(path, prefix) ? path.substr(prefix.length) : path;
|
|
27
176
|
|
|
28
177
|
class Stacks {
|
|
29
178
|
stacks = [];
|
|
@@ -82,54 +231,11 @@ class Stacks {
|
|
|
82
231
|
}
|
|
83
232
|
const stacks = new Stacks();
|
|
84
233
|
|
|
85
|
-
function addLeadingSlash(path) {
|
|
86
|
-
if (path == null) {
|
|
87
|
-
return '';
|
|
88
|
-
}
|
|
89
|
-
return path.charAt(0) === '/' ? path : '/' + path;
|
|
90
|
-
}
|
|
91
|
-
class RoutesAlias {
|
|
92
|
-
conf = [];
|
|
93
|
-
set(customRoutes = {}) {
|
|
94
|
-
for (let key in customRoutes) {
|
|
95
|
-
const path = customRoutes[key];
|
|
96
|
-
key = addLeadingSlash(key);
|
|
97
|
-
if (typeof path === 'string') {
|
|
98
|
-
this.conf.push([key, addLeadingSlash(path)]);
|
|
99
|
-
}
|
|
100
|
-
else if (path?.length > 0) {
|
|
101
|
-
this.conf.push(...path.map(p => [key, addLeadingSlash(p)]));
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
getConfig = (url = '') => {
|
|
106
|
-
const customRoute = this.conf.filter((arr) => {
|
|
107
|
-
return arr.includes(url);
|
|
108
|
-
});
|
|
109
|
-
return customRoute[0];
|
|
110
|
-
};
|
|
111
|
-
getOrigin = (url = '') => {
|
|
112
|
-
return this.getConfig(url)?.[0] || url;
|
|
113
|
-
};
|
|
114
|
-
getAlias = (url = '') => {
|
|
115
|
-
return this.getConfig(url)?.[1] || url;
|
|
116
|
-
};
|
|
117
|
-
getAll = (url = '') => {
|
|
118
|
-
return this.conf.filter((arr) => {
|
|
119
|
-
return arr.includes(url);
|
|
120
|
-
}).reduceRight((p, a) => {
|
|
121
|
-
p.unshift(a[1]);
|
|
122
|
-
return p;
|
|
123
|
-
}, [url]);
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
const routesAlias = new RoutesAlias();
|
|
127
|
-
|
|
128
234
|
function processNavigateUrl(option) {
|
|
129
|
-
const pathPieces = parsePath(option.url);
|
|
235
|
+
const pathPieces = history.parsePath(option.url);
|
|
130
236
|
// 处理相对路径
|
|
131
237
|
if (pathPieces.pathname?.includes('./')) {
|
|
132
|
-
const parts = routesAlias.getOrigin(history.location.pathname).split('/');
|
|
238
|
+
const parts = routesAlias.getOrigin(exports.history.location.pathname).split('/');
|
|
133
239
|
parts.pop();
|
|
134
240
|
pathPieces.pathname.split('/').forEach((item) => {
|
|
135
241
|
if (item === '.') {
|
|
@@ -151,7 +257,7 @@ function processNavigateUrl(option) {
|
|
|
151
257
|
async function navigate(option, method) {
|
|
152
258
|
return new Promise((resolve, reject) => {
|
|
153
259
|
const { success, complete, fail } = option;
|
|
154
|
-
const unListen = history.listen(() => {
|
|
260
|
+
const unListen = exports.history.listen(() => {
|
|
155
261
|
const res = { errMsg: `${method}:ok` };
|
|
156
262
|
success?.(res);
|
|
157
263
|
complete?.(res);
|
|
@@ -163,19 +269,19 @@ async function navigate(option, method) {
|
|
|
163
269
|
const pathPieces = processNavigateUrl(option);
|
|
164
270
|
const state = { timestamp: Date.now() };
|
|
165
271
|
if (method === 'navigateTo') {
|
|
166
|
-
history.push(pathPieces, state);
|
|
272
|
+
exports.history.push(pathPieces, state);
|
|
167
273
|
}
|
|
168
274
|
else if (method === 'redirectTo' || method === 'switchTab') {
|
|
169
|
-
history.replace(pathPieces, state);
|
|
275
|
+
exports.history.replace(pathPieces, state);
|
|
170
276
|
}
|
|
171
277
|
else if (method === 'reLaunch') {
|
|
172
278
|
stacks.delta = stacks.length;
|
|
173
|
-
history.replace(pathPieces, state);
|
|
279
|
+
exports.history.replace(pathPieces, state);
|
|
174
280
|
}
|
|
175
281
|
}
|
|
176
282
|
else if (method === 'navigateBack') {
|
|
177
283
|
stacks.delta = option.delta;
|
|
178
|
-
history.go(-option.delta);
|
|
284
|
+
exports.history.go(-option.delta);
|
|
179
285
|
}
|
|
180
286
|
}
|
|
181
287
|
catch (error) {
|
|
@@ -192,11 +298,11 @@ function navigateTo(option) {
|
|
|
192
298
|
function redirectTo(option) {
|
|
193
299
|
return navigate(option, 'redirectTo');
|
|
194
300
|
}
|
|
195
|
-
function navigateBack(
|
|
196
|
-
if (!
|
|
197
|
-
|
|
301
|
+
function navigateBack(option = { delta: 1 }) {
|
|
302
|
+
if (!option.delta || option.delta < 1) {
|
|
303
|
+
option.delta = 1;
|
|
198
304
|
}
|
|
199
|
-
return navigate(
|
|
305
|
+
return navigate(option, 'navigateBack');
|
|
200
306
|
}
|
|
201
307
|
function switchTab(option) {
|
|
202
308
|
return navigate(option, 'switchTab');
|
|
@@ -205,6 +311,9 @@ function reLaunch(option) {
|
|
|
205
311
|
return navigate(option, 'reLaunch');
|
|
206
312
|
}
|
|
207
313
|
function getCurrentPages() {
|
|
314
|
+
if (process.env.NODE_ENV === 'development' && RouterConfig.mode === 'multi') {
|
|
315
|
+
console.warn('多页面路由模式不支持使用 getCurrentPages 方法!');
|
|
316
|
+
}
|
|
208
317
|
const pages = stacks.get();
|
|
209
318
|
return pages.map(e => ({ ...e, route: e.path || '' }));
|
|
210
319
|
}
|
|
@@ -239,37 +348,9 @@ function loadAnimateStyle(ms = 300) {
|
|
|
239
348
|
document.getElementsByTagName('head')[0].appendChild(style);
|
|
240
349
|
}
|
|
241
350
|
|
|
242
|
-
// @ts-nocheck
|
|
243
|
-
function initTabbar(config) {
|
|
244
|
-
if (config.tabBar == null) {
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
// TODO: custom-tab-bar
|
|
248
|
-
const tabbar = document.createElement('taro-tabbar');
|
|
249
|
-
const homePage = config.entryPagePath || (config.pages ? config.pages[0] : '');
|
|
250
|
-
tabbar.conf = config.tabBar;
|
|
251
|
-
tabbar.conf.homePage = history.location.pathname === '/' ? homePage : history.location.pathname;
|
|
252
|
-
const routerConfig = config.router;
|
|
253
|
-
tabbar.conf.mode = routerConfig && routerConfig.mode ? routerConfig.mode : 'hash';
|
|
254
|
-
if (routerConfig.customRoutes) {
|
|
255
|
-
tabbar.conf.custom = true;
|
|
256
|
-
tabbar.conf.customRoutes = routerConfig.customRoutes;
|
|
257
|
-
}
|
|
258
|
-
else {
|
|
259
|
-
tabbar.conf.custom = false;
|
|
260
|
-
tabbar.conf.customRoutes = {};
|
|
261
|
-
}
|
|
262
|
-
if (typeof routerConfig.basename !== 'undefined') {
|
|
263
|
-
tabbar.conf.basename = routerConfig.basename;
|
|
264
|
-
}
|
|
265
|
-
const container = document.getElementById('container');
|
|
266
|
-
container?.appendChild(tabbar);
|
|
267
|
-
initTabBarApis(config);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
351
|
let pageResizeFn;
|
|
271
352
|
function bindPageResize(page) {
|
|
272
|
-
window.removeEventListener('resize', pageResizeFn);
|
|
353
|
+
pageResizeFn && window.removeEventListener('resize', pageResizeFn);
|
|
273
354
|
pageResizeFn = function () {
|
|
274
355
|
page.onResize && page.onResize({
|
|
275
356
|
size: {
|
|
@@ -284,7 +365,7 @@ function bindPageResize(page) {
|
|
|
284
365
|
let pageScrollFn;
|
|
285
366
|
let pageDOM = window;
|
|
286
367
|
function bindPageScroll(page, pageEl, distance = 50) {
|
|
287
|
-
pageEl.removeEventListener('scroll', pageScrollFn);
|
|
368
|
+
pageScrollFn && pageEl.removeEventListener('scroll', pageScrollFn);
|
|
288
369
|
pageDOM = pageEl;
|
|
289
370
|
let isReachBottom = false;
|
|
290
371
|
pageScrollFn = function () {
|
|
@@ -312,6 +393,34 @@ function getOffset() {
|
|
|
312
393
|
}
|
|
313
394
|
}
|
|
314
395
|
|
|
396
|
+
// @ts-nocheck
|
|
397
|
+
function initTabbar(config) {
|
|
398
|
+
if (config.tabBar == null) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
// TODO: custom-tab-bar
|
|
402
|
+
const tabbar = document.createElement('taro-tabbar');
|
|
403
|
+
const homePage = config.entryPagePath || (config.pages ? config.pages[0] : '');
|
|
404
|
+
tabbar.conf = config.tabBar;
|
|
405
|
+
tabbar.conf.homePage = exports.history.location.pathname === '/' ? homePage : exports.history.location.pathname;
|
|
406
|
+
const routerConfig = config.router;
|
|
407
|
+
tabbar.conf.mode = routerConfig && routerConfig.mode ? routerConfig.mode : 'hash';
|
|
408
|
+
if (routerConfig.customRoutes) {
|
|
409
|
+
tabbar.conf.custom = true;
|
|
410
|
+
tabbar.conf.customRoutes = routerConfig.customRoutes;
|
|
411
|
+
}
|
|
412
|
+
else {
|
|
413
|
+
tabbar.conf.custom = false;
|
|
414
|
+
tabbar.conf.customRoutes = {};
|
|
415
|
+
}
|
|
416
|
+
if (typeof routerConfig.basename !== 'undefined') {
|
|
417
|
+
tabbar.conf.basename = routerConfig.basename;
|
|
418
|
+
}
|
|
419
|
+
const container = document.getElementById('container');
|
|
420
|
+
container?.appendChild(tabbar);
|
|
421
|
+
taro.initTabBarApis(config);
|
|
422
|
+
}
|
|
423
|
+
|
|
315
424
|
function setDisplay(el, type = '') {
|
|
316
425
|
if (el) {
|
|
317
426
|
el.style.display = type;
|
|
@@ -394,7 +503,7 @@ class PageHandler {
|
|
|
394
503
|
getQuery(stamp = 0, search = '', options = {}) {
|
|
395
504
|
search = search ? `${search}&${this.search}` : this.search;
|
|
396
505
|
const query = search
|
|
397
|
-
?
|
|
506
|
+
? queryString__default["default"].parse(search, { decode: false })
|
|
398
507
|
: {};
|
|
399
508
|
query.stamp = stamp.toString();
|
|
400
509
|
return { ...query, ...options };
|
|
@@ -426,7 +535,7 @@ class PageHandler {
|
|
|
426
535
|
if (pageEl && !pageEl?.['__isReady']) {
|
|
427
536
|
const el = pageEl.firstElementChild;
|
|
428
537
|
el?.['componentOnReady']?.()?.then(() => {
|
|
429
|
-
requestAnimationFrame(() => {
|
|
538
|
+
runtime.requestAnimationFrame(() => {
|
|
430
539
|
page.onReady?.();
|
|
431
540
|
pageEl['__isReady'] = true;
|
|
432
541
|
});
|
|
@@ -548,7 +657,7 @@ class PageHandler {
|
|
|
548
657
|
}
|
|
549
658
|
}
|
|
550
659
|
getPageContainer(page) {
|
|
551
|
-
const path = page ? page?.path : Current.page?.path;
|
|
660
|
+
const path = page ? page?.path : runtime.Current.page?.path;
|
|
552
661
|
const id = path?.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
|
|
553
662
|
if (page) {
|
|
554
663
|
return document.querySelector(`.taro_page#${id}`);
|
|
@@ -571,8 +680,9 @@ class PageHandler {
|
|
|
571
680
|
|
|
572
681
|
/* eslint-disable dot-notation */
|
|
573
682
|
function createRouter(app, config, framework) {
|
|
683
|
+
RouterConfig.config = config;
|
|
574
684
|
const handler = new PageHandler(config);
|
|
575
|
-
const runtimeHooks = container.get(SERVICE_IDENTIFIER.Hooks);
|
|
685
|
+
const runtimeHooks = runtime.container.get(runtime.SERVICE_IDENTIFIER.Hooks);
|
|
576
686
|
routesAlias.set(handler.router.customRoutes);
|
|
577
687
|
const basename = handler.router.basename;
|
|
578
688
|
const routes = handler.routes.map(route => ({
|
|
@@ -580,11 +690,11 @@ function createRouter(app, config, framework) {
|
|
|
580
690
|
action: route.load
|
|
581
691
|
}));
|
|
582
692
|
const entryPagePath = config.entryPagePath || routes[0].path?.[0];
|
|
583
|
-
const router = new
|
|
693
|
+
const router = new UniversalRouter__default["default"](routes, { baseUrl: basename || '' });
|
|
584
694
|
const launchParam = handler.getQuery(stacks.length);
|
|
585
695
|
app.onLaunch?.(launchParam);
|
|
586
696
|
const render = async ({ location, action }) => {
|
|
587
|
-
handler.pathname = location.pathname;
|
|
697
|
+
handler.pathname = decodeURI(location.pathname);
|
|
588
698
|
let element;
|
|
589
699
|
try {
|
|
590
700
|
element = await router.resolve(handler.router.forcePath || handler.pathname);
|
|
@@ -603,7 +713,7 @@ function createRouter(app, config, framework) {
|
|
|
603
713
|
return;
|
|
604
714
|
const pageConfig = handler.pageConfig;
|
|
605
715
|
let enablePullDownRefresh = config?.window?.enablePullDownRefresh || false;
|
|
606
|
-
eventCenter.trigger('__taroRouterChange', {
|
|
716
|
+
runtime.eventCenter.trigger('__taroRouterChange', {
|
|
607
717
|
toLocation: {
|
|
608
718
|
path: handler.pathname
|
|
609
719
|
}
|
|
@@ -614,7 +724,7 @@ function createRouter(app, config, framework) {
|
|
|
614
724
|
enablePullDownRefresh = pageConfig.enablePullDownRefresh;
|
|
615
725
|
}
|
|
616
726
|
}
|
|
617
|
-
const currentPage = Current.page;
|
|
727
|
+
const currentPage = runtime.Current.page;
|
|
618
728
|
const pathname = handler.pathname;
|
|
619
729
|
let shouldLoad = false;
|
|
620
730
|
if (action === 'POP') {
|
|
@@ -656,18 +766,173 @@ function createRouter(app, config, framework) {
|
|
|
656
766
|
const stacksIndex = stacks.length;
|
|
657
767
|
delete loadConfig['path'];
|
|
658
768
|
delete loadConfig['load'];
|
|
659
|
-
const page = createPageConfig(enablePullDownRefresh ? runtimeHooks.createPullDownComponent?.(el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
|
|
769
|
+
const page = runtime.createPageConfig(enablePullDownRefresh ? runtimeHooks.createPullDownComponent?.(el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + runtime.stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
|
|
660
770
|
return handler.load(page, pageConfig, stacksIndex);
|
|
661
771
|
}
|
|
662
772
|
};
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
773
|
+
if (exports.history.location.pathname === '/') {
|
|
774
|
+
const stripped = stripBasename(exports.history.location.pathname, handler.basename);
|
|
775
|
+
if (stripped === '/' || stripped === '') {
|
|
776
|
+
exports.history.replace(prependBasename(entryPagePath + exports.history.location.search));
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
render({ location: exports.history.location, action: history.Action.Push });
|
|
780
|
+
app.onShow?.(launchParam);
|
|
781
|
+
return exports.history.listen(render);
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
class MultiPageHandler {
|
|
785
|
+
config;
|
|
786
|
+
constructor(config) {
|
|
787
|
+
this.config = config;
|
|
788
|
+
this.mount();
|
|
789
|
+
}
|
|
790
|
+
get appId() { return 'app'; }
|
|
791
|
+
get router() { return this.config.router; }
|
|
792
|
+
get routerMode() { return this.router.mode || 'hash'; }
|
|
793
|
+
get customRoutes() { return this.router.customRoutes || {}; }
|
|
794
|
+
get tabBarList() { return this.config.tabBar?.list || []; }
|
|
795
|
+
get PullDownRefresh() { return this.config.PullDownRefresh; }
|
|
796
|
+
set pathname(p) { this.router.pathname = p; }
|
|
797
|
+
get pathname() { return this.router.pathname; }
|
|
798
|
+
get basename() { return this.router.basename || ''; }
|
|
799
|
+
get pageConfig() { return this.config.route; }
|
|
800
|
+
get isTabBar() {
|
|
801
|
+
const routePath = stripBasename(this.pathname, this.basename);
|
|
802
|
+
const pagePath = Object.entries(this.customRoutes).find(([, target]) => {
|
|
803
|
+
if (typeof target === 'string') {
|
|
804
|
+
return target === routePath;
|
|
805
|
+
}
|
|
806
|
+
else if (target?.length > 0) {
|
|
807
|
+
return target.includes(routePath);
|
|
808
|
+
}
|
|
809
|
+
return false;
|
|
810
|
+
})?.[0] || routePath;
|
|
811
|
+
return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
|
|
812
|
+
}
|
|
813
|
+
get search() { return location.search.substr(1); }
|
|
814
|
+
getQuery(search = '', options = {}) {
|
|
815
|
+
search = search ? `${search}&${this.search}` : this.search;
|
|
816
|
+
const query = search
|
|
817
|
+
? queryString__default["default"].parse(search)
|
|
818
|
+
: {};
|
|
819
|
+
return { ...query, ...options };
|
|
820
|
+
}
|
|
821
|
+
mount() {
|
|
822
|
+
setHistoryMode(this.routerMode, this.router.basename);
|
|
823
|
+
document.getElementById('app')?.remove();
|
|
824
|
+
const app = document.createElement('div');
|
|
825
|
+
app.id = this.appId;
|
|
826
|
+
app.classList.add('taro_router');
|
|
827
|
+
if (this.tabBarList.length > 1) {
|
|
828
|
+
const container = document.createElement('div');
|
|
829
|
+
container.classList.add('taro-tabbar__container');
|
|
830
|
+
container.id = 'container';
|
|
831
|
+
const panel = document.createElement('div');
|
|
832
|
+
panel.classList.add('taro-tabbar__panel');
|
|
833
|
+
panel.appendChild(app);
|
|
834
|
+
container.appendChild(panel);
|
|
835
|
+
document.body.appendChild(container);
|
|
836
|
+
initTabbar(this.config);
|
|
837
|
+
}
|
|
838
|
+
else {
|
|
839
|
+
document.body.appendChild(app);
|
|
840
|
+
}
|
|
666
841
|
}
|
|
667
|
-
|
|
842
|
+
onReady(page, onLoad = true) {
|
|
843
|
+
const pageEl = this.getPageContainer(page);
|
|
844
|
+
if (pageEl && !pageEl?.['__isReady']) {
|
|
845
|
+
const el = pageEl.firstElementChild;
|
|
846
|
+
el?.['componentOnReady']?.();
|
|
847
|
+
onLoad && (pageEl['__page'] = page);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
load(page, pageConfig = {}) {
|
|
851
|
+
if (!page)
|
|
852
|
+
return;
|
|
853
|
+
page.onLoad?.(this.getQuery('', page.options), () => {
|
|
854
|
+
const pageEl = this.getPageContainer(page);
|
|
855
|
+
this.isTabBar && pageEl?.classList.add('taro_tabbar_page');
|
|
856
|
+
this.onReady(page, true);
|
|
857
|
+
page.onShow?.();
|
|
858
|
+
this.bindPageEvents(page, pageEl, pageConfig);
|
|
859
|
+
});
|
|
860
|
+
}
|
|
861
|
+
getPageContainer(page) {
|
|
862
|
+
const path = page ? page?.path : runtime.Current.page?.path;
|
|
863
|
+
const id = path?.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
|
|
864
|
+
if (page) {
|
|
865
|
+
return document.querySelector(`.taro_page#${id}`);
|
|
866
|
+
}
|
|
867
|
+
const el = (id
|
|
868
|
+
? document.querySelector(`.taro_page#${id}`)
|
|
869
|
+
: document.querySelector('.taro_page') ||
|
|
870
|
+
document.querySelector('.taro_router'));
|
|
871
|
+
return el || window;
|
|
872
|
+
}
|
|
873
|
+
bindPageEvents(page, pageEl, config = {}) {
|
|
874
|
+
if (!pageEl) {
|
|
875
|
+
pageEl = this.getPageContainer();
|
|
876
|
+
}
|
|
877
|
+
const distance = config.onReachBottomDistance || this.config.window?.onReachBottomDistance || 50;
|
|
878
|
+
bindPageScroll(page, pageEl, distance);
|
|
879
|
+
bindPageResize(page);
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
/* eslint-disable dot-notation */
|
|
884
|
+
// TODO 支持多路由 (APP 生命周期仅触发一次)
|
|
885
|
+
/** Note: 关于多页面应用
|
|
886
|
+
* - 需要配置路由映射(根目录跳转、404 页面……)
|
|
887
|
+
* - app.onPageNotFound 事件不支持
|
|
888
|
+
* - 应用生命周期可能多次触发
|
|
889
|
+
* - TabBar 会多次加载
|
|
890
|
+
* - 不支持路由动画
|
|
891
|
+
*/
|
|
892
|
+
async function createMultiRouter(app, config, framework) {
|
|
893
|
+
RouterConfig.config = config;
|
|
894
|
+
const handler = new MultiPageHandler(config);
|
|
895
|
+
const runtimeHooks = runtime.container.get(runtime.SERVICE_IDENTIFIER.Hooks);
|
|
896
|
+
const launchParam = handler.getQuery();
|
|
897
|
+
app.onLaunch?.(launchParam);
|
|
898
|
+
const pathName = config.pageName;
|
|
899
|
+
const pageConfig = handler.pageConfig;
|
|
900
|
+
let element;
|
|
901
|
+
try {
|
|
902
|
+
element = await pageConfig.load?.();
|
|
903
|
+
}
|
|
904
|
+
catch (error) {
|
|
905
|
+
throw new Error(error);
|
|
906
|
+
}
|
|
907
|
+
if (!element)
|
|
908
|
+
return;
|
|
909
|
+
let enablePullDownRefresh = config?.window?.enablePullDownRefresh || false;
|
|
910
|
+
runtime.eventCenter.trigger('__taroRouterChange', {
|
|
911
|
+
toLocation: {
|
|
912
|
+
path: pathName
|
|
913
|
+
}
|
|
914
|
+
});
|
|
915
|
+
if (pageConfig) {
|
|
916
|
+
document.title = pageConfig.navigationBarTitleText ?? document.title;
|
|
917
|
+
if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
|
|
918
|
+
enablePullDownRefresh = pageConfig.enablePullDownRefresh;
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
const el = element.default ?? element;
|
|
922
|
+
const loadConfig = { ...pageConfig };
|
|
923
|
+
delete loadConfig['path'];
|
|
924
|
+
delete loadConfig['load'];
|
|
925
|
+
const page = runtime.createPageConfig(enablePullDownRefresh ? runtimeHooks.createPullDownComponent?.(el, location.pathname, framework, config.PullDownRefresh) : el, pathName + runtime.stringify(launchParam), {}, loadConfig);
|
|
926
|
+
handler.load(page, pageConfig);
|
|
668
927
|
app.onShow?.(launchParam);
|
|
669
|
-
return history.listen(render);
|
|
670
928
|
}
|
|
671
929
|
|
|
672
|
-
|
|
673
|
-
|
|
930
|
+
exports.createMultiRouter = createMultiRouter;
|
|
931
|
+
exports.createRouter = createRouter;
|
|
932
|
+
exports.getCurrentPages = getCurrentPages;
|
|
933
|
+
exports.navigateBack = navigateBack;
|
|
934
|
+
exports.navigateTo = navigateTo;
|
|
935
|
+
exports.reLaunch = reLaunch;
|
|
936
|
+
exports.redirectTo = redirectTo;
|
|
937
|
+
exports.switchTab = switchTab;
|
|
938
|
+
//# sourceMappingURL=index.cjs.js.map
|