@tarojs/router 3.6.22 → 3.6.23
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 +14 -0
- package/dist/api.d.ts +7 -6
- package/dist/api.js +15 -19
- package/dist/events/resize.d.ts +2 -1
- package/dist/events/resize.js +3 -1
- package/dist/events/scroll.d.ts +2 -1
- package/dist/events/scroll.js +4 -1
- package/dist/history.d.ts +24 -5
- package/dist/history.js +17 -6
- package/dist/index.cjs.d.ts +44 -6
- package/dist/index.cjs.js +201 -239
- package/dist/index.d.ts +10 -4
- package/dist/index.esm.d.ts +44 -6
- package/dist/index.esm.js +162 -210
- package/dist/index.js +49 -4
- package/dist/router/index.d.ts +4 -3
- package/dist/router/index.js +5 -2
- package/dist/router/mpa.d.ts +5 -3
- package/dist/router/mpa.js +10 -16
- package/dist/router/multi-page.d.ts +9 -6
- package/dist/router/multi-page.js +15 -42
- package/dist/router/page.d.ts +9 -6
- package/dist/router/page.js +17 -43
- package/dist/router/spa.d.ts +5 -3
- package/dist/router/spa.js +15 -21
- package/dist/router/stack.d.ts +1 -1
- package/dist/router/stack.js +2 -1
- package/dist/style.d.ts +4 -3
- package/dist/style.js +12 -6
- package/dist/tabbar.d.ts +3 -1
- package/dist/tabbar.js +4 -3
- package/dist/utils/index.d.ts +3 -9
- package/dist/utils/index.js +6 -20
- package/dist/utils/navigate.d.ts +4 -3
- package/dist/utils/navigate.js +7 -12
- package/package.json +24 -14
- package/types/component.d.ts +5 -0
- package/types/taro.d.ts +8 -0
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.esm.js.map +0 -1
package/dist/index.cjs.js
CHANGED
|
@@ -1,102 +1,40 @@
|
|
|
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 queryString = require('query-string');
|
|
8
3
|
var components = require('@tarojs/components/dist/components');
|
|
9
4
|
var taro = require('@tarojs/taro');
|
|
5
|
+
var tslib = require('tslib');
|
|
6
|
+
var runtime = require('@tarojs/runtime');
|
|
7
|
+
var history = require('history');
|
|
8
|
+
var queryString = require('query-string');
|
|
10
9
|
var UniversalRouter = require('universal-router');
|
|
11
10
|
|
|
12
|
-
function
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
var UniversalRouter__default = /*#__PURE__*/_interopDefaultLegacy(UniversalRouter);
|
|
16
|
-
|
|
17
|
-
/******************************************************************************
|
|
18
|
-
Copyright (c) Microsoft Corporation.
|
|
19
|
-
|
|
20
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
21
|
-
purpose with or without fee is hereby granted.
|
|
22
|
-
|
|
23
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
24
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
25
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
26
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
27
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
28
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
29
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
30
|
-
***************************************************************************** */
|
|
31
|
-
|
|
32
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
33
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
34
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
35
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
36
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
37
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
38
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// export const removeLeadingSlash = (str = '') => str.replace(/^\.?\//, '')
|
|
43
|
-
// export const removeTrailingSearch = (str = '') => str.replace(/\?[\s\S]*$/, '')
|
|
44
|
-
const addLeadingSlash = (url = '') => (url.charAt(0) === '/' ? url : '/' + url);
|
|
45
|
-
const hasBasename = (path = '', prefix = '') => new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path) || path === prefix;
|
|
46
|
-
const stripBasename = (path = '', prefix = '') => hasBasename(path, prefix) ? path.substring(prefix.length) : path;
|
|
47
|
-
const stripTrailing = (str = '') => str.replace(/[?#][\s\S]*$/, '');
|
|
48
|
-
const getHomePage = (path = '', basename = '', customRoutes = {}, entryPagePath = '') => {
|
|
49
|
-
var _a;
|
|
50
|
-
const routePath = addLeadingSlash(stripBasename(path, basename));
|
|
51
|
-
const alias = ((_a = Object.entries(customRoutes).find(([key]) => key === routePath)) === null || _a === void 0 ? void 0 : _a[1]) || routePath;
|
|
52
|
-
return entryPagePath || (typeof alias === 'string' ? alias : alias[0]) || basename;
|
|
53
|
-
};
|
|
54
|
-
const getCurrentPage = (routerMode = 'hash', basename = '/') => {
|
|
55
|
-
const pagePath = routerMode === 'hash'
|
|
56
|
-
? location.hash.slice(1).split('?')[0]
|
|
57
|
-
: location.pathname;
|
|
58
|
-
return addLeadingSlash(stripBasename(pagePath, basename));
|
|
59
|
-
};
|
|
60
|
-
class RoutesAlias {
|
|
61
|
-
constructor() {
|
|
62
|
-
this.conf = [];
|
|
63
|
-
this.getConfig = (url = '') => {
|
|
64
|
-
const customRoute = this.conf.filter((arr) => {
|
|
65
|
-
return arr.includes(url);
|
|
66
|
-
});
|
|
67
|
-
return customRoute[0];
|
|
68
|
-
};
|
|
69
|
-
this.getOrigin = (url = '') => {
|
|
70
|
-
var _a;
|
|
71
|
-
return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[0]) || url;
|
|
72
|
-
};
|
|
73
|
-
this.getAlias = (url = '') => {
|
|
74
|
-
var _a;
|
|
75
|
-
return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[1]) || url;
|
|
76
|
-
};
|
|
77
|
-
this.getAll = (url = '') => {
|
|
78
|
-
return this.conf
|
|
79
|
-
.filter((arr) => arr.includes(url))
|
|
80
|
-
.reduceRight((p, a) => {
|
|
81
|
-
p.unshift(a[1]);
|
|
82
|
-
return p;
|
|
83
|
-
}, []);
|
|
84
|
-
};
|
|
11
|
+
function initTabbar(config, history) {
|
|
12
|
+
if (config.tabBar == null) {
|
|
13
|
+
return;
|
|
85
14
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
15
|
+
// TODO: custom-tab-bar
|
|
16
|
+
components.defineCustomElementTaroTabbar();
|
|
17
|
+
const tabbar = document.createElement('taro-tabbar');
|
|
18
|
+
const homePage = config.entryPagePath || (config.pages ? config.pages[0] : '');
|
|
19
|
+
tabbar.conf = config.tabBar;
|
|
20
|
+
tabbar.conf.homePage = history.location.pathname === '/' ? homePage : history.location.pathname;
|
|
21
|
+
const routerConfig = config.router;
|
|
22
|
+
tabbar.conf.mode = routerConfig && routerConfig.mode ? routerConfig.mode : 'hash';
|
|
23
|
+
if (routerConfig.customRoutes) {
|
|
24
|
+
tabbar.conf.custom = true;
|
|
25
|
+
tabbar.conf.customRoutes = routerConfig.customRoutes;
|
|
97
26
|
}
|
|
27
|
+
else {
|
|
28
|
+
tabbar.conf.custom = false;
|
|
29
|
+
tabbar.conf.customRoutes = {};
|
|
30
|
+
}
|
|
31
|
+
if (typeof routerConfig.basename !== 'undefined') {
|
|
32
|
+
tabbar.conf.basename = routerConfig.basename;
|
|
33
|
+
}
|
|
34
|
+
const container = document.getElementById('container');
|
|
35
|
+
container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
|
|
36
|
+
taro.initTabBarApis(config);
|
|
98
37
|
}
|
|
99
|
-
const routesAlias = new RoutesAlias();
|
|
100
38
|
|
|
101
39
|
class RouterConfig {
|
|
102
40
|
static set config(e) {
|
|
@@ -116,7 +54,7 @@ class RouterConfig {
|
|
|
116
54
|
}
|
|
117
55
|
static get customRoutes() { return this.router.customRoutes || {}; }
|
|
118
56
|
static isPage(url = '') {
|
|
119
|
-
return this.pages.findIndex(e => addLeadingSlash(e) === url) !== -1;
|
|
57
|
+
return this.pages.findIndex(e => runtime.addLeadingSlash(e) === url) !== -1;
|
|
120
58
|
}
|
|
121
59
|
}
|
|
122
60
|
|
|
@@ -199,6 +137,13 @@ class MpaHistory {
|
|
|
199
137
|
};
|
|
200
138
|
}
|
|
201
139
|
}
|
|
140
|
+
function setHistory(h, base = '/') {
|
|
141
|
+
exports.history = h;
|
|
142
|
+
basename = base;
|
|
143
|
+
}
|
|
144
|
+
function createMpaHistory(_) {
|
|
145
|
+
return new MpaHistory();
|
|
146
|
+
}
|
|
202
147
|
function setHistoryMode(mode, base = '/') {
|
|
203
148
|
const options = {
|
|
204
149
|
window
|
|
@@ -208,7 +153,7 @@ function setHistoryMode(mode, base = '/') {
|
|
|
208
153
|
exports.history = history.createBrowserHistory(options);
|
|
209
154
|
}
|
|
210
155
|
else if (mode === 'multi') {
|
|
211
|
-
exports.history =
|
|
156
|
+
exports.history = createMpaHistory();
|
|
212
157
|
}
|
|
213
158
|
else {
|
|
214
159
|
// default is hash
|
|
@@ -300,6 +245,69 @@ class Stacks {
|
|
|
300
245
|
}
|
|
301
246
|
const stacks = new Stacks();
|
|
302
247
|
|
|
248
|
+
let preTitle = document.title;
|
|
249
|
+
let isLoadDdEntry = false;
|
|
250
|
+
const isWeixin = () => !!navigator.userAgent.match(/\bMicroMessenger\b/ig);
|
|
251
|
+
const isDingTalk = () => !!navigator.userAgent.match(/\bDingTalk\b/ig);
|
|
252
|
+
function setTitle(title) {
|
|
253
|
+
return tslib.__awaiter(this, void 0, void 0, function* () {
|
|
254
|
+
if (preTitle === title)
|
|
255
|
+
return title;
|
|
256
|
+
document.title = title;
|
|
257
|
+
preTitle = title;
|
|
258
|
+
if (process.env.SUPPORT_DINGTALK_NAVIGATE !== 'disabled' && isDingTalk()) {
|
|
259
|
+
if (!isLoadDdEntry) {
|
|
260
|
+
isLoadDdEntry = true;
|
|
261
|
+
require('dingtalk-jsapi/platform');
|
|
262
|
+
}
|
|
263
|
+
const setDingTitle = require('dingtalk-jsapi/api/biz/navigation/setTitle').default;
|
|
264
|
+
setDingTitle({ title });
|
|
265
|
+
}
|
|
266
|
+
return title;
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
class RoutesAlias {
|
|
271
|
+
constructor() {
|
|
272
|
+
this.conf = [];
|
|
273
|
+
this.getConfig = (url = '') => {
|
|
274
|
+
const customRoute = this.conf.filter((arr) => {
|
|
275
|
+
return arr.includes(url);
|
|
276
|
+
});
|
|
277
|
+
return customRoute[0];
|
|
278
|
+
};
|
|
279
|
+
this.getOrigin = (url = '') => {
|
|
280
|
+
var _a;
|
|
281
|
+
return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[0]) || url;
|
|
282
|
+
};
|
|
283
|
+
this.getAlias = (url = '') => {
|
|
284
|
+
var _a;
|
|
285
|
+
return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[1]) || url;
|
|
286
|
+
};
|
|
287
|
+
this.getAll = (url = '') => {
|
|
288
|
+
return this.conf
|
|
289
|
+
.filter((arr) => arr.includes(url))
|
|
290
|
+
.reduceRight((p, a) => {
|
|
291
|
+
p.unshift(a[1]);
|
|
292
|
+
return p;
|
|
293
|
+
}, []);
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
set(customRoutes = {}) {
|
|
297
|
+
for (let key in customRoutes) {
|
|
298
|
+
const path = customRoutes[key];
|
|
299
|
+
key = runtime.addLeadingSlash(key);
|
|
300
|
+
if (typeof path === 'string') {
|
|
301
|
+
this.conf.push([key, runtime.addLeadingSlash(path)]);
|
|
302
|
+
}
|
|
303
|
+
else if ((path === null || path === void 0 ? void 0 : path.length) > 0) {
|
|
304
|
+
this.conf.push(...path.map(p => [key, runtime.addLeadingSlash(p)]));
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
const routesAlias = new RoutesAlias();
|
|
310
|
+
|
|
303
311
|
function processNavigateUrl(option) {
|
|
304
312
|
var _a;
|
|
305
313
|
const pathPieces = history.parsePath(option.url);
|
|
@@ -316,7 +324,7 @@ function processNavigateUrl(option) {
|
|
|
316
324
|
pathPieces.pathname = parts.join('/');
|
|
317
325
|
}
|
|
318
326
|
// 处理自定义路由
|
|
319
|
-
pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname));
|
|
327
|
+
pathPieces.pathname = routesAlias.getAlias(runtime.addLeadingSlash(pathPieces.pathname));
|
|
320
328
|
// 处理 basename
|
|
321
329
|
pathPieces.pathname = prependBasename(pathPieces.pathname);
|
|
322
330
|
// hack fix history v5 bug: https://github.com/remix-run/history/issues/814
|
|
@@ -325,7 +333,7 @@ function processNavigateUrl(option) {
|
|
|
325
333
|
return pathPieces;
|
|
326
334
|
}
|
|
327
335
|
function navigate(option, method) {
|
|
328
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
336
|
+
return tslib.__awaiter(this, void 0, void 0, function* () {
|
|
329
337
|
return new Promise((resolve, reject) => {
|
|
330
338
|
stacks.method = method;
|
|
331
339
|
const { success, complete, fail } = option;
|
|
@@ -396,27 +404,6 @@ function getCurrentPages() {
|
|
|
396
404
|
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, '')) || '' })); });
|
|
397
405
|
}
|
|
398
406
|
|
|
399
|
-
let preTitle = document.title;
|
|
400
|
-
let isLoadDdEntry = false;
|
|
401
|
-
const isDingTalk = () => !!navigator.userAgent.match(/\bDingTalk\b/ig);
|
|
402
|
-
function setTitle(title) {
|
|
403
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
404
|
-
if (preTitle === title)
|
|
405
|
-
return title;
|
|
406
|
-
document.title = title;
|
|
407
|
-
preTitle = title;
|
|
408
|
-
if (process.env.SUPPORT_DINGTALK_NAVIGATE !== 'disabled' && isDingTalk()) {
|
|
409
|
-
if (!isLoadDdEntry) {
|
|
410
|
-
isLoadDdEntry = true;
|
|
411
|
-
require('dingtalk-jsapi/platform');
|
|
412
|
-
}
|
|
413
|
-
const setDingTitle = require('dingtalk-jsapi/api/biz/navigation/setTitle').default;
|
|
414
|
-
setDingTitle({ title });
|
|
415
|
-
}
|
|
416
|
-
return title;
|
|
417
|
-
});
|
|
418
|
-
}
|
|
419
|
-
|
|
420
407
|
let pageResizeFn;
|
|
421
408
|
function bindPageResize(page) {
|
|
422
409
|
pageResizeFn && window.removeEventListener('resize', pageResizeFn);
|
|
@@ -470,6 +457,9 @@ function getOffset() {
|
|
|
470
457
|
*/
|
|
471
458
|
function loadAnimateStyle(ms = 300) {
|
|
472
459
|
const css = `
|
|
460
|
+
body {
|
|
461
|
+
overflow: hidden; // 防止 iOS 页面滚动
|
|
462
|
+
}
|
|
473
463
|
.taro_router > .taro_page {
|
|
474
464
|
position: absolute;
|
|
475
465
|
left: 0;
|
|
@@ -496,7 +486,7 @@ function loadAnimateStyle(ms = 300) {
|
|
|
496
486
|
/**
|
|
497
487
|
* 插入路由相关样式
|
|
498
488
|
*/
|
|
499
|
-
function loadRouterStyle(
|
|
489
|
+
function loadRouterStyle(enableTabBar, enableWindowScroll) {
|
|
500
490
|
const css = `
|
|
501
491
|
.taro_router {
|
|
502
492
|
position: relative;
|
|
@@ -507,13 +497,13 @@ function loadRouterStyle(usingWindowScroll) {
|
|
|
507
497
|
.taro_page {
|
|
508
498
|
width: 100%;
|
|
509
499
|
height: 100%;
|
|
510
|
-
|
|
500
|
+
${enableWindowScroll ? '' : `
|
|
511
501
|
overflow-x: hidden;
|
|
512
502
|
overflow-y: scroll;
|
|
513
503
|
max-height: 100vh;
|
|
514
|
-
|
|
504
|
+
`}
|
|
515
505
|
}
|
|
516
|
-
|
|
506
|
+
${enableTabBar ? `
|
|
517
507
|
.taro-tabbar__container > .taro-tabbar__panel {
|
|
518
508
|
overflow: hidden;
|
|
519
509
|
}
|
|
@@ -523,6 +513,7 @@ function loadRouterStyle(usingWindowScroll) {
|
|
|
523
513
|
max-height: calc(100vh - var(--taro-tabbar-height) - env(safe-area-inset-bottom));
|
|
524
514
|
}
|
|
525
515
|
|
|
516
|
+
` : ''}
|
|
526
517
|
.taro_page_shade,
|
|
527
518
|
.taro_router > .taro_page.taro_page_show.taro_page_stationed:not(.taro_page_shade):not(.taro_tabbar_page):not(:last-child) {
|
|
528
519
|
display: none;
|
|
@@ -538,38 +529,10 @@ function addStyle(css) {
|
|
|
538
529
|
document.getElementsByTagName('head')[0].appendChild(style);
|
|
539
530
|
}
|
|
540
531
|
|
|
541
|
-
// @ts-nocheck
|
|
542
|
-
function initTabbar(config) {
|
|
543
|
-
if (config.tabBar == null) {
|
|
544
|
-
return;
|
|
545
|
-
}
|
|
546
|
-
// TODO: custom-tab-bar
|
|
547
|
-
components.defineCustomElementTaroTabbar();
|
|
548
|
-
const tabbar = document.createElement('taro-tabbar');
|
|
549
|
-
const homePage = config.entryPagePath || (config.pages ? config.pages[0] : '');
|
|
550
|
-
tabbar.conf = config.tabBar;
|
|
551
|
-
tabbar.conf.homePage = exports.history.location.pathname === '/' ? homePage : exports.history.location.pathname;
|
|
552
|
-
const routerConfig = config.router;
|
|
553
|
-
tabbar.conf.mode = routerConfig && routerConfig.mode ? routerConfig.mode : 'hash';
|
|
554
|
-
if (routerConfig.customRoutes) {
|
|
555
|
-
tabbar.conf.custom = true;
|
|
556
|
-
tabbar.conf.customRoutes = routerConfig.customRoutes;
|
|
557
|
-
}
|
|
558
|
-
else {
|
|
559
|
-
tabbar.conf.custom = false;
|
|
560
|
-
tabbar.conf.customRoutes = {};
|
|
561
|
-
}
|
|
562
|
-
if (typeof routerConfig.basename !== 'undefined') {
|
|
563
|
-
tabbar.conf.basename = routerConfig.basename;
|
|
564
|
-
}
|
|
565
|
-
const container = document.getElementById('container');
|
|
566
|
-
container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
|
|
567
|
-
taro.initTabBarApis(config);
|
|
568
|
-
}
|
|
569
|
-
|
|
570
532
|
/* eslint-disable dot-notation */
|
|
571
533
|
class MultiPageHandler {
|
|
572
|
-
constructor(config) {
|
|
534
|
+
constructor(config, history) {
|
|
535
|
+
this.history = history;
|
|
573
536
|
this.config = config;
|
|
574
537
|
this.mount();
|
|
575
538
|
}
|
|
@@ -585,7 +548,7 @@ class MultiPageHandler {
|
|
|
585
548
|
get pageConfig() { return this.config.route; }
|
|
586
549
|
get isTabBar() {
|
|
587
550
|
var _a;
|
|
588
|
-
const routePath = addLeadingSlash(stripBasename(this.pathname, this.basename));
|
|
551
|
+
const routePath = runtime.addLeadingSlash(runtime.stripBasename(this.pathname, this.basename));
|
|
589
552
|
const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
|
|
590
553
|
if (typeof target === 'string') {
|
|
591
554
|
return target === routePath;
|
|
@@ -612,7 +575,7 @@ class MultiPageHandler {
|
|
|
612
575
|
getQuery(search = '', options = {}) {
|
|
613
576
|
search = search ? `${search}&${this.search}` : this.search;
|
|
614
577
|
const query = search
|
|
615
|
-
?
|
|
578
|
+
? queryString.parse(search)
|
|
616
579
|
: {};
|
|
617
580
|
return Object.assign(Object.assign({}, query), options);
|
|
618
581
|
}
|
|
@@ -625,38 +588,9 @@ class MultiPageHandler {
|
|
|
625
588
|
return style !== 'custom';
|
|
626
589
|
}
|
|
627
590
|
mount() {
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
let app = document.getElementById(appId);
|
|
632
|
-
let isPosition = true;
|
|
633
|
-
if (!app) {
|
|
634
|
-
app = document.createElement('div');
|
|
635
|
-
app.id = appId;
|
|
636
|
-
isPosition = false;
|
|
637
|
-
}
|
|
638
|
-
const appWrapper = (app === null || app === void 0 ? void 0 : app.parentNode) || (app === null || app === void 0 ? void 0 : app.parentElement) || document.body;
|
|
639
|
-
app.classList.add('taro_router');
|
|
640
|
-
if (this.tabBarList.length > 1) {
|
|
641
|
-
const container = document.createElement('div');
|
|
642
|
-
container.classList.add('taro-tabbar__container');
|
|
643
|
-
container.id = 'container';
|
|
644
|
-
const panel = document.createElement('div');
|
|
645
|
-
panel.classList.add('taro-tabbar__panel');
|
|
646
|
-
panel.appendChild(app.cloneNode(true));
|
|
647
|
-
container.appendChild(panel);
|
|
648
|
-
if (!isPosition) {
|
|
649
|
-
appWrapper.appendChild(container);
|
|
650
|
-
}
|
|
651
|
-
else {
|
|
652
|
-
appWrapper.replaceChild(container, app);
|
|
653
|
-
}
|
|
654
|
-
initTabbar(this.config);
|
|
655
|
-
}
|
|
656
|
-
else {
|
|
657
|
-
if (!isPosition)
|
|
658
|
-
appWrapper.appendChild(app);
|
|
659
|
-
}
|
|
591
|
+
setHistory(this.history, this.basename);
|
|
592
|
+
// Note: 注入页面样式
|
|
593
|
+
loadRouterStyle(this.tabBarList.length > 1, this.usingWindowScroll);
|
|
660
594
|
}
|
|
661
595
|
onReady(page, onLoad = true) {
|
|
662
596
|
var _a;
|
|
@@ -735,14 +669,14 @@ const launchStampId$1 = createStampId$1();
|
|
|
735
669
|
* - TabBar 会多次加载
|
|
736
670
|
* - 不支持路由动画
|
|
737
671
|
*/
|
|
738
|
-
function createMultiRouter(app, config, framework) {
|
|
672
|
+
function createMultiRouter(history, app, config, framework) {
|
|
739
673
|
var _a, _b, _c, _d, _e, _f;
|
|
740
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
674
|
+
return tslib.__awaiter(this, void 0, void 0, function* () {
|
|
741
675
|
if (typeof app.onUnhandledRejection === 'function') {
|
|
742
676
|
window.addEventListener('unhandledrejection', app.onUnhandledRejection);
|
|
743
677
|
}
|
|
744
678
|
RouterConfig.config = config;
|
|
745
|
-
const handler = new MultiPageHandler(config);
|
|
679
|
+
const handler = new MultiPageHandler(config, history);
|
|
746
680
|
const launchParam = {
|
|
747
681
|
path: config.pageName,
|
|
748
682
|
query: handler.getQuery(launchStampId$1),
|
|
@@ -791,14 +725,15 @@ function createMultiRouter(app, config, framework) {
|
|
|
791
725
|
|
|
792
726
|
/* eslint-disable dot-notation */
|
|
793
727
|
class PageHandler {
|
|
794
|
-
constructor(config) {
|
|
728
|
+
constructor(config, history) {
|
|
729
|
+
this.history = history;
|
|
795
730
|
this.defaultAnimation = { duration: 300, delay: 50 };
|
|
796
731
|
this.config = config;
|
|
797
|
-
this.homePage = getHomePage(this.routes[0].path, this.basename, this.customRoutes, this.config.entryPagePath);
|
|
732
|
+
this.homePage = runtime.getHomePage(this.routes[0].path, this.basename, this.customRoutes, this.config.entryPagePath);
|
|
798
733
|
this.mount();
|
|
799
734
|
}
|
|
800
735
|
get currentPage() {
|
|
801
|
-
const routePath = getCurrentPage(this.routerMode, this.basename);
|
|
736
|
+
const routePath = runtime.getCurrentPage(this.routerMode, this.basename);
|
|
802
737
|
return routePath === '/' ? this.homePage : routePath;
|
|
803
738
|
}
|
|
804
739
|
get appId() { return this.config.appId || 'app'; }
|
|
@@ -829,17 +764,17 @@ class PageHandler {
|
|
|
829
764
|
get pathname() { return this.router.pathname; }
|
|
830
765
|
get basename() { return this.router.basename || ''; }
|
|
831
766
|
get pageConfig() {
|
|
832
|
-
const routePath = addLeadingSlash(stripBasename(this.pathname, this.basename));
|
|
833
|
-
const homePage = addLeadingSlash(this.homePage);
|
|
767
|
+
const routePath = runtime.addLeadingSlash(runtime.stripBasename(this.pathname, this.basename));
|
|
768
|
+
const homePage = runtime.addLeadingSlash(this.homePage);
|
|
834
769
|
return this.routes.find(r => {
|
|
835
770
|
var _a;
|
|
836
|
-
const pagePath = addLeadingSlash(r.path);
|
|
771
|
+
const pagePath = runtime.addLeadingSlash(r.path);
|
|
837
772
|
return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
|
|
838
773
|
});
|
|
839
774
|
}
|
|
840
775
|
isTabBar(pathname) {
|
|
841
776
|
var _a;
|
|
842
|
-
const routePath = addLeadingSlash(stripBasename(pathname, this.basename)).split('?')[0];
|
|
777
|
+
const routePath = runtime.addLeadingSlash(runtime.stripBasename(pathname, this.basename)).split('?')[0];
|
|
843
778
|
const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
|
|
844
779
|
if (typeof target === 'string') {
|
|
845
780
|
return target === routePath;
|
|
@@ -849,7 +784,7 @@ class PageHandler {
|
|
|
849
784
|
}
|
|
850
785
|
return false;
|
|
851
786
|
})) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
|
|
852
|
-
return !!pagePath && this.tabBarList.some(t => stripTrailing(t.pagePath) === pagePath);
|
|
787
|
+
return !!pagePath && this.tabBarList.some(t => runtime.stripTrailing(t.pagePath) === pagePath);
|
|
853
788
|
}
|
|
854
789
|
isDefaultNavigationStyle() {
|
|
855
790
|
var _a, _b;
|
|
@@ -860,8 +795,8 @@ class PageHandler {
|
|
|
860
795
|
return style !== 'custom';
|
|
861
796
|
}
|
|
862
797
|
isSamePage(page) {
|
|
863
|
-
const routePath = stripBasename(this.pathname, this.basename);
|
|
864
|
-
const pagePath = stripBasename(page === null || page === void 0 ? void 0 : page.path, this.basename);
|
|
798
|
+
const routePath = runtime.stripBasename(this.pathname, this.basename);
|
|
799
|
+
const pagePath = runtime.stripBasename(page === null || page === void 0 ? void 0 : page.path, this.basename);
|
|
865
800
|
return pagePath.startsWith(routePath + '?');
|
|
866
801
|
}
|
|
867
802
|
get search() {
|
|
@@ -891,46 +826,17 @@ class PageHandler {
|
|
|
891
826
|
getQuery(stamp = '', search = '', options = {}) {
|
|
892
827
|
search = search ? `${search}&${this.search}` : this.search;
|
|
893
828
|
const query = search
|
|
894
|
-
?
|
|
829
|
+
? queryString.parse(search, { decode: false })
|
|
895
830
|
: {};
|
|
896
831
|
query.stamp = stamp;
|
|
897
832
|
return Object.assign(Object.assign({}, query), options);
|
|
898
833
|
}
|
|
899
834
|
mount() {
|
|
900
|
-
|
|
835
|
+
setHistory(this.history, this.basename);
|
|
901
836
|
this.pathname = exports.history.location.pathname;
|
|
837
|
+
// Note: 注入页面样式
|
|
902
838
|
this.animation && loadAnimateStyle(this.animationDuration);
|
|
903
|
-
loadRouterStyle(this.usingWindowScroll);
|
|
904
|
-
const appId = this.appId;
|
|
905
|
-
let app = document.getElementById(appId);
|
|
906
|
-
let isPosition = true;
|
|
907
|
-
if (!app) {
|
|
908
|
-
app = document.createElement('div');
|
|
909
|
-
app.id = appId;
|
|
910
|
-
isPosition = false;
|
|
911
|
-
}
|
|
912
|
-
const appWrapper = (app === null || app === void 0 ? void 0 : app.parentNode) || (app === null || app === void 0 ? void 0 : app.parentElement) || document.body;
|
|
913
|
-
app.classList.add('taro_router');
|
|
914
|
-
if (this.tabBarList.length > 1) {
|
|
915
|
-
const container = document.createElement('div');
|
|
916
|
-
container.classList.add('taro-tabbar__container');
|
|
917
|
-
container.id = 'container';
|
|
918
|
-
const panel = document.createElement('div');
|
|
919
|
-
panel.classList.add('taro-tabbar__panel');
|
|
920
|
-
panel.appendChild(app.cloneNode(true));
|
|
921
|
-
container.appendChild(panel);
|
|
922
|
-
if (!isPosition) {
|
|
923
|
-
appWrapper.appendChild(container);
|
|
924
|
-
}
|
|
925
|
-
else {
|
|
926
|
-
appWrapper.replaceChild(container, app);
|
|
927
|
-
}
|
|
928
|
-
initTabbar(this.config);
|
|
929
|
-
}
|
|
930
|
-
else {
|
|
931
|
-
if (!isPosition)
|
|
932
|
-
appWrapper.appendChild(app);
|
|
933
|
-
}
|
|
839
|
+
loadRouterStyle(this.tabBarList.length > 1, this.usingWindowScroll);
|
|
934
840
|
}
|
|
935
841
|
onReady(page, onLoad = true) {
|
|
936
842
|
var _a;
|
|
@@ -1130,24 +1036,24 @@ class PageHandler {
|
|
|
1130
1036
|
|
|
1131
1037
|
const createStampId = runtime.incrementId();
|
|
1132
1038
|
let launchStampId = createStampId();
|
|
1133
|
-
function createRouter(app, config, framework) {
|
|
1039
|
+
function createRouter(history$1, app, config, framework) {
|
|
1134
1040
|
var _a, _b;
|
|
1135
1041
|
if (typeof app.onUnhandledRejection === 'function') {
|
|
1136
1042
|
window.addEventListener('unhandledrejection', app.onUnhandledRejection);
|
|
1137
1043
|
}
|
|
1138
1044
|
RouterConfig.config = config;
|
|
1139
|
-
const handler = new PageHandler(config);
|
|
1045
|
+
const handler = new PageHandler(config, history$1);
|
|
1140
1046
|
routesAlias.set(handler.router.customRoutes);
|
|
1141
1047
|
const basename = handler.router.basename;
|
|
1142
1048
|
const routes = handler.routes.map(route => {
|
|
1143
|
-
const routePath = addLeadingSlash(route.path);
|
|
1049
|
+
const routePath = runtime.addLeadingSlash(route.path);
|
|
1144
1050
|
const paths = routesAlias.getAll(routePath);
|
|
1145
1051
|
return {
|
|
1146
1052
|
path: paths.length < 1 ? routePath : paths,
|
|
1147
1053
|
action: route.load
|
|
1148
1054
|
};
|
|
1149
1055
|
});
|
|
1150
|
-
const router = new
|
|
1056
|
+
const router = new UniversalRouter(routes, { baseUrl: basename || '' });
|
|
1151
1057
|
const launchParam = {
|
|
1152
1058
|
path: handler.currentPage,
|
|
1153
1059
|
query: handler.getQuery(launchStampId),
|
|
@@ -1158,7 +1064,7 @@ function createRouter(app, config, framework) {
|
|
|
1158
1064
|
runtime.eventCenter.trigger('__taroRouterLaunch', launchParam);
|
|
1159
1065
|
(_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
|
|
1160
1066
|
app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
|
|
1161
|
-
const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
|
|
1067
|
+
const render = ({ location, action }) => tslib.__awaiter(this, void 0, void 0, function* () {
|
|
1162
1068
|
var _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
1163
1069
|
handler.pathname = decodeURI(location.pathname);
|
|
1164
1070
|
if ((_c = window.__taroAppConfig) === null || _c === void 0 ? void 0 : _c.usingWindowScroll)
|
|
@@ -1301,21 +1207,77 @@ function createRouter(app, config, framework) {
|
|
|
1301
1207
|
handler.load(page, pageConfig, pageStampId, stacksIndex);
|
|
1302
1208
|
}
|
|
1303
1209
|
});
|
|
1304
|
-
const routePath = addLeadingSlash(stripBasename(
|
|
1210
|
+
const routePath = runtime.addLeadingSlash(runtime.stripBasename(history$1.location.pathname, handler.basename));
|
|
1305
1211
|
if (routePath === '/') {
|
|
1306
|
-
|
|
1212
|
+
history$1.replace(prependBasename(handler.homePage + history$1.location.search));
|
|
1307
1213
|
}
|
|
1308
|
-
render({ location:
|
|
1214
|
+
render({ location: history$1.location, action: history.Action.Push });
|
|
1309
1215
|
(_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, launchParam);
|
|
1310
|
-
return
|
|
1216
|
+
return history$1.listen(render);
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
function handleAppMount(config, _, appId = config.appId || 'app') {
|
|
1220
|
+
let app = document.getElementById(appId);
|
|
1221
|
+
let isPosition = true;
|
|
1222
|
+
if (!app) {
|
|
1223
|
+
app = document.createElement('div');
|
|
1224
|
+
app.id = appId;
|
|
1225
|
+
isPosition = false;
|
|
1226
|
+
}
|
|
1227
|
+
const appWrapper = (app === null || app === void 0 ? void 0 : app.parentNode) || (app === null || app === void 0 ? void 0 : app.parentElement) || document.body;
|
|
1228
|
+
app.classList.add('taro_router');
|
|
1229
|
+
if (!isPosition)
|
|
1230
|
+
appWrapper.appendChild(app);
|
|
1231
|
+
}
|
|
1232
|
+
function handleAppMountWithTabbar(config, history, appId = config.appId || 'app') {
|
|
1233
|
+
let app = document.getElementById(appId);
|
|
1234
|
+
let isPosition = true;
|
|
1235
|
+
if (!app) {
|
|
1236
|
+
app = document.createElement('div');
|
|
1237
|
+
app.id = appId;
|
|
1238
|
+
isPosition = false;
|
|
1239
|
+
}
|
|
1240
|
+
const appWrapper = (app === null || app === void 0 ? void 0 : app.parentNode) || (app === null || app === void 0 ? void 0 : app.parentElement) || document.body;
|
|
1241
|
+
app.classList.add('taro_router');
|
|
1242
|
+
const container = document.createElement('div');
|
|
1243
|
+
container.classList.add('taro-tabbar__container');
|
|
1244
|
+
container.id = 'container';
|
|
1245
|
+
const panel = document.createElement('div');
|
|
1246
|
+
panel.classList.add('taro-tabbar__panel');
|
|
1247
|
+
panel.appendChild(app.cloneNode(true));
|
|
1248
|
+
container.appendChild(panel);
|
|
1249
|
+
if (!isPosition) {
|
|
1250
|
+
appWrapper.appendChild(container);
|
|
1251
|
+
}
|
|
1252
|
+
else {
|
|
1253
|
+
appWrapper.replaceChild(container, app);
|
|
1254
|
+
}
|
|
1255
|
+
initTabbar(config, history);
|
|
1311
1256
|
}
|
|
1312
1257
|
|
|
1258
|
+
Object.defineProperty(exports, 'createBrowserHistory', {
|
|
1259
|
+
enumerable: true,
|
|
1260
|
+
get: function () { return history.createBrowserHistory; }
|
|
1261
|
+
});
|
|
1262
|
+
Object.defineProperty(exports, 'createHashHistory', {
|
|
1263
|
+
enumerable: true,
|
|
1264
|
+
get: function () { return history.createHashHistory; }
|
|
1265
|
+
});
|
|
1266
|
+
exports.createMpaHistory = createMpaHistory;
|
|
1313
1267
|
exports.createMultiRouter = createMultiRouter;
|
|
1314
1268
|
exports.createRouter = createRouter;
|
|
1315
1269
|
exports.getCurrentPages = getCurrentPages;
|
|
1270
|
+
exports.handleAppMount = handleAppMount;
|
|
1271
|
+
exports.handleAppMountWithTabbar = handleAppMountWithTabbar;
|
|
1272
|
+
exports.isDingTalk = isDingTalk;
|
|
1273
|
+
exports.isWeixin = isWeixin;
|
|
1316
1274
|
exports.navigateBack = navigateBack;
|
|
1317
1275
|
exports.navigateTo = navigateTo;
|
|
1276
|
+
exports.prependBasename = prependBasename;
|
|
1318
1277
|
exports.reLaunch = reLaunch;
|
|
1319
1278
|
exports.redirectTo = redirectTo;
|
|
1279
|
+
exports.routesAlias = routesAlias;
|
|
1280
|
+
exports.setHistory = setHistory;
|
|
1281
|
+
exports.setHistoryMode = setHistoryMode;
|
|
1282
|
+
exports.setTitle = setTitle;
|
|
1320
1283
|
exports.switchTab = switchTab;
|
|
1321
|
-
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { History } from "./history.js";
|
|
2
|
+
import { MpaRouterConfig, SpaRouterConfig } from '../types/router';
|
|
3
|
+
declare function handleAppMount(config: SpaRouterConfig | MpaRouterConfig, _: History, appId?: string): void;
|
|
4
|
+
declare function handleAppMountWithTabbar(config: SpaRouterConfig | MpaRouterConfig, history: History, appId?: string): void;
|
|
5
|
+
export * from "./api.js";
|
|
6
|
+
export * from "./history.js";
|
|
7
|
+
export { createMultiRouter } from "./router/mpa.js";
|
|
8
|
+
export { createRouter } from "./router/spa.js";
|
|
9
|
+
export * from "./utils/index.js";
|
|
10
|
+
export { handleAppMount, handleAppMountWithTabbar };
|