@tarojs/router 3.4.6 → 3.5.0-alpha.1

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.
@@ -0,0 +1,23 @@
1
+ import { addLeadingSlash } from '../utils';
2
+ export class RouterConfig {
3
+ static __config;
4
+ static set config(e) {
5
+ this.__config = e;
6
+ }
7
+ static get config() {
8
+ return this.__config;
9
+ }
10
+ static get pages() {
11
+ return this.config.pages || [];
12
+ }
13
+ static get router() {
14
+ return this.config.router || {};
15
+ }
16
+ static get mode() {
17
+ return this.router.mode || 'hash';
18
+ }
19
+ static get customRoutes() { return this.router.customRoutes || {}; }
20
+ static isPage(url = '') {
21
+ return this.pages.findIndex(e => addLeadingSlash(e) === url) !== -1;
22
+ }
23
+ }
@@ -0,0 +1,49 @@
1
+ /* eslint-disable dot-notation */
2
+ import { container, createPageConfig, eventCenter, SERVICE_IDENTIFIER, stringify } from '@tarojs/runtime';
3
+ import MultiPageHandler from './multi-page';
4
+ import { RouterConfig } from '.';
5
+ // TODO 支持多路由 (APP 生命周期仅触发一次)
6
+ /** Note: 关于多页面应用
7
+ * - 需要配置路由映射(根目录跳转、404 页面……)
8
+ * - app.onPageNotFound 事件不支持
9
+ * - 应用生命周期可能多次触发
10
+ * - TabBar 会多次加载
11
+ * - 不支持路由动画
12
+ */
13
+ export async function createMultiRouter(app, config, framework) {
14
+ RouterConfig.config = config;
15
+ const handler = new MultiPageHandler(config);
16
+ const runtimeHooks = container.get(SERVICE_IDENTIFIER.Hooks);
17
+ const launchParam = handler.getQuery();
18
+ app.onLaunch?.(launchParam);
19
+ const pathName = config.pageName;
20
+ const pageConfig = handler.pageConfig;
21
+ let element;
22
+ try {
23
+ element = await pageConfig.load?.();
24
+ }
25
+ catch (error) {
26
+ throw new Error(error);
27
+ }
28
+ if (!element)
29
+ return;
30
+ let enablePullDownRefresh = config?.window?.enablePullDownRefresh || false;
31
+ eventCenter.trigger('__taroRouterChange', {
32
+ toLocation: {
33
+ path: pathName
34
+ }
35
+ });
36
+ if (pageConfig) {
37
+ document.title = pageConfig.navigationBarTitleText ?? document.title;
38
+ if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
39
+ enablePullDownRefresh = pageConfig.enablePullDownRefresh;
40
+ }
41
+ }
42
+ const el = element.default ?? element;
43
+ const loadConfig = { ...pageConfig };
44
+ delete loadConfig['path'];
45
+ delete loadConfig['load'];
46
+ const page = createPageConfig(enablePullDownRefresh ? runtimeHooks.createPullDownComponent?.(el, location.pathname, framework, config.PullDownRefresh) : el, pathName + stringify(launchParam), {}, loadConfig);
47
+ handler.load(page, pageConfig);
48
+ app.onShow?.(launchParam);
49
+ }
@@ -0,0 +1,105 @@
1
+ import { Current } from '@tarojs/runtime';
2
+ import queryString from 'query-string';
3
+ import { bindPageResize } from '../events/resize';
4
+ import { bindPageScroll } from '../events/scroll';
5
+ import { setHistoryMode } from '../history';
6
+ import { initTabbar } from '../tabbar';
7
+ import { stripBasename } from '../utils';
8
+ export default class MultiPageHandler {
9
+ config;
10
+ constructor(config) {
11
+ this.config = config;
12
+ this.mount();
13
+ }
14
+ get appId() { return 'app'; }
15
+ get router() { return this.config.router; }
16
+ get routerMode() { return this.router.mode || 'hash'; }
17
+ get customRoutes() { return this.router.customRoutes || {}; }
18
+ get tabBarList() { return this.config.tabBar?.list || []; }
19
+ get PullDownRefresh() { return this.config.PullDownRefresh; }
20
+ set pathname(p) { this.router.pathname = p; }
21
+ get pathname() { return this.router.pathname; }
22
+ get basename() { return this.router.basename || ''; }
23
+ get pageConfig() { return this.config.route; }
24
+ get isTabBar() {
25
+ const routePath = stripBasename(this.pathname, this.basename);
26
+ const pagePath = Object.entries(this.customRoutes).find(([, target]) => {
27
+ if (typeof target === 'string') {
28
+ return target === routePath;
29
+ }
30
+ else if (target?.length > 0) {
31
+ return target.includes(routePath);
32
+ }
33
+ return false;
34
+ })?.[0] || routePath;
35
+ return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
36
+ }
37
+ get search() { return location.search.substr(1); }
38
+ getQuery(search = '', options = {}) {
39
+ search = search ? `${search}&${this.search}` : this.search;
40
+ const query = search
41
+ ? queryString.parse(search)
42
+ : {};
43
+ return { ...query, ...options };
44
+ }
45
+ mount() {
46
+ setHistoryMode(this.routerMode, this.router.basename);
47
+ document.getElementById('app')?.remove();
48
+ const app = document.createElement('div');
49
+ app.id = this.appId;
50
+ app.classList.add('taro_router');
51
+ if (this.tabBarList.length > 1) {
52
+ const container = document.createElement('div');
53
+ container.classList.add('taro-tabbar__container');
54
+ container.id = 'container';
55
+ const panel = document.createElement('div');
56
+ panel.classList.add('taro-tabbar__panel');
57
+ panel.appendChild(app);
58
+ container.appendChild(panel);
59
+ document.body.appendChild(container);
60
+ initTabbar(this.config);
61
+ }
62
+ else {
63
+ document.body.appendChild(app);
64
+ }
65
+ }
66
+ onReady(page, onLoad = true) {
67
+ const pageEl = this.getPageContainer(page);
68
+ if (pageEl && !pageEl?.['__isReady']) {
69
+ const el = pageEl.firstElementChild;
70
+ el?.['componentOnReady']?.();
71
+ onLoad && (pageEl['__page'] = page);
72
+ }
73
+ }
74
+ load(page, pageConfig = {}) {
75
+ if (!page)
76
+ return;
77
+ page.onLoad?.(this.getQuery('', page.options), () => {
78
+ const pageEl = this.getPageContainer(page);
79
+ this.isTabBar && pageEl?.classList.add('taro_tabbar_page');
80
+ this.onReady(page, true);
81
+ page.onShow?.();
82
+ this.bindPageEvents(page, pageEl, pageConfig);
83
+ });
84
+ }
85
+ getPageContainer(page) {
86
+ const path = page ? page?.path : Current.page?.path;
87
+ const id = path?.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
88
+ if (page) {
89
+ return document.querySelector(`.taro_page#${id}`);
90
+ }
91
+ const el = (id
92
+ ? document.querySelector(`.taro_page#${id}`)
93
+ : document.querySelector('.taro_page') ||
94
+ document.querySelector('.taro_router'));
95
+ return el || window;
96
+ }
97
+ bindPageEvents(page, pageEl, config = {}) {
98
+ if (!pageEl) {
99
+ pageEl = this.getPageContainer();
100
+ }
101
+ const distance = config.onReachBottomDistance || this.config.window?.onReachBottomDistance || 50;
102
+ bindPageScroll(page, pageEl, distance);
103
+ bindPageResize(page);
104
+ }
105
+ }
@@ -0,0 +1,265 @@
1
+ import { Current, requestAnimationFrame } from '@tarojs/runtime';
2
+ import queryString from 'query-string';
3
+ import { loadAnimateStyle } from '../animation';
4
+ import { bindPageResize } from '../events/resize';
5
+ import { bindPageScroll } from '../events/scroll';
6
+ import { setHistoryMode } from '../history';
7
+ import { initTabbar } from '../tabbar';
8
+ import { addLeadingSlash, routesAlias, stripBasename } from '../utils';
9
+ import stacks from './stack';
10
+ function setDisplay(el, type = '') {
11
+ if (el) {
12
+ el.style.display = type;
13
+ }
14
+ }
15
+ export default class PageHandler {
16
+ config;
17
+ defaultAnimation = { duration: 300, delay: 50 };
18
+ unloadTimer;
19
+ hideTimer;
20
+ lastHidePage;
21
+ lastUnloadPage;
22
+ constructor(config) {
23
+ this.config = config;
24
+ this.mount();
25
+ }
26
+ get appId() { return 'app'; }
27
+ get router() { return this.config.router; }
28
+ get routerMode() { return this.router.mode || 'hash'; }
29
+ get customRoutes() { return this.router.customRoutes || {}; }
30
+ get routes() { return this.config.routes; }
31
+ get tabBarList() { return this.config.tabBar?.list || []; }
32
+ get PullDownRefresh() { return this.config.PullDownRefresh; }
33
+ get animation() { return this.config?.animation ?? this.defaultAnimation; }
34
+ get animationDelay() {
35
+ return (typeof this.animation === 'object'
36
+ ? this.animation.delay
37
+ : this.animation
38
+ ? this.defaultAnimation?.delay
39
+ : 0) || 0;
40
+ }
41
+ get animationDuration() {
42
+ return (typeof this.animation === 'object'
43
+ ? this.animation.duration
44
+ : this.animation
45
+ ? this.defaultAnimation?.duration
46
+ : 0) || 0;
47
+ }
48
+ set pathname(p) { this.router.pathname = p; }
49
+ get pathname() { return this.router.pathname; }
50
+ get basename() { return this.router.basename || ''; }
51
+ get pageConfig() {
52
+ return this.routes.find(r => {
53
+ const routePath = stripBasename(this.pathname, this.basename);
54
+ const pagePath = addLeadingSlash(r.path);
55
+ return pagePath === routePath || routesAlias.getConfig(pagePath)?.includes(routePath);
56
+ });
57
+ }
58
+ get isTabBar() {
59
+ const routePath = stripBasename(this.pathname, this.basename);
60
+ const pagePath = Object.entries(this.customRoutes).find(([, target]) => {
61
+ if (typeof target === 'string') {
62
+ return target === routePath;
63
+ }
64
+ else if (target?.length > 0) {
65
+ return target.includes(routePath);
66
+ }
67
+ return false;
68
+ })?.[0] || routePath;
69
+ return !!pagePath && this.tabBarList.some(t => t.pagePath === pagePath);
70
+ }
71
+ isSamePage(page) {
72
+ const routePath = stripBasename(this.pathname, this.basename);
73
+ const pagePath = stripBasename(page?.path, this.basename);
74
+ return pagePath.startsWith(routePath + '?');
75
+ }
76
+ get search() {
77
+ let search = '?';
78
+ if (this.routerMode === 'hash') {
79
+ const idx = location.hash.indexOf('?');
80
+ if (idx > -1) {
81
+ search = location.hash.slice(idx);
82
+ }
83
+ }
84
+ else {
85
+ search = location.search;
86
+ }
87
+ return search.substr(1);
88
+ }
89
+ getQuery(stamp = 0, search = '', options = {}) {
90
+ search = search ? `${search}&${this.search}` : this.search;
91
+ const query = search
92
+ ? queryString.parse(search, { decode: false })
93
+ : {};
94
+ query.stamp = stamp.toString();
95
+ return { ...query, ...options };
96
+ }
97
+ mount() {
98
+ setHistoryMode(this.routerMode, this.router.basename);
99
+ document.getElementById('app')?.remove();
100
+ this.animation && loadAnimateStyle(this.animationDuration);
101
+ const app = document.createElement('div');
102
+ app.id = this.appId;
103
+ app.classList.add('taro_router');
104
+ if (this.tabBarList.length > 1) {
105
+ const container = document.createElement('div');
106
+ container.classList.add('taro-tabbar__container');
107
+ container.id = 'container';
108
+ const panel = document.createElement('div');
109
+ panel.classList.add('taro-tabbar__panel');
110
+ panel.appendChild(app);
111
+ container.appendChild(panel);
112
+ document.body.appendChild(container);
113
+ initTabbar(this.config);
114
+ }
115
+ else {
116
+ document.body.appendChild(app);
117
+ }
118
+ }
119
+ onReady(page, onLoad = true) {
120
+ const pageEl = this.getPageContainer(page);
121
+ if (pageEl && !pageEl?.['__isReady']) {
122
+ const el = pageEl.firstElementChild;
123
+ el?.['componentOnReady']?.()?.then(() => {
124
+ requestAnimationFrame(() => {
125
+ page.onReady?.();
126
+ pageEl['__isReady'] = true;
127
+ });
128
+ });
129
+ onLoad && (pageEl['__page'] = page);
130
+ }
131
+ }
132
+ load(page, pageConfig = {}, stacksIndex = 0) {
133
+ if (!page)
134
+ return;
135
+ // NOTE: 页面栈推入太晚可能导致 getCurrentPages 无法获取到当前页面实例
136
+ stacks.push(page);
137
+ const param = this.getQuery(stacks.length, '', page.options);
138
+ let pageEl = this.getPageContainer(page);
139
+ if (pageEl) {
140
+ setDisplay(pageEl);
141
+ this.isTabBar && pageEl.classList.add('taro_tabbar_page');
142
+ this.addAnimation(pageEl, stacksIndex === 0);
143
+ page.onShow?.();
144
+ this.bindPageEvents(page, pageEl, pageConfig);
145
+ }
146
+ else {
147
+ page.onLoad?.(param, () => {
148
+ pageEl = this.getPageContainer(page);
149
+ this.isTabBar && pageEl?.classList.add('taro_tabbar_page');
150
+ this.addAnimation(pageEl, stacksIndex === 0);
151
+ this.onReady(page, true);
152
+ page.onShow?.();
153
+ this.bindPageEvents(page, pageEl, pageConfig);
154
+ });
155
+ }
156
+ }
157
+ unload(page, delta = 1, top = false) {
158
+ if (!page)
159
+ return;
160
+ stacks.delta = --delta;
161
+ stacks.pop();
162
+ if (this.animation && top) {
163
+ if (this.unloadTimer) {
164
+ clearTimeout(this.unloadTimer);
165
+ this.lastUnloadPage?.onUnload?.();
166
+ this.unloadTimer = null;
167
+ }
168
+ this.lastUnloadPage = page;
169
+ const pageEl = this.getPageContainer(page);
170
+ pageEl?.classList.remove('taro_page_stationed');
171
+ pageEl?.classList.remove('taro_page_show');
172
+ this.unloadTimer = setTimeout(() => {
173
+ this.unloadTimer = null;
174
+ this.lastUnloadPage?.onUnload?.();
175
+ }, this.animationDuration);
176
+ }
177
+ else {
178
+ const pageEl = this.getPageContainer(page);
179
+ pageEl?.classList.remove('taro_page_stationed');
180
+ pageEl?.classList.remove('taro_page_show');
181
+ page?.onUnload?.();
182
+ }
183
+ if (delta >= 1)
184
+ this.unload(stacks.last, delta);
185
+ }
186
+ show(page, pageConfig = {}, stacksIndex = 0) {
187
+ if (!page)
188
+ return;
189
+ const param = this.getQuery(stacks.length, '', page.options);
190
+ let pageEl = this.getPageContainer(page);
191
+ if (pageEl) {
192
+ setDisplay(pageEl);
193
+ this.addAnimation(pageEl, stacksIndex === 0);
194
+ page.onShow?.();
195
+ this.bindPageEvents(page, pageEl, pageConfig);
196
+ }
197
+ else {
198
+ page.onLoad?.(param, () => {
199
+ pageEl = this.getPageContainer(page);
200
+ this.addAnimation(pageEl, stacksIndex === 0);
201
+ this.onReady(page, false);
202
+ page.onShow?.();
203
+ this.bindPageEvents(page, pageEl, pageConfig);
204
+ });
205
+ }
206
+ }
207
+ hide(page) {
208
+ if (!page)
209
+ return;
210
+ // NOTE: 修复多页并发问题,此处可能因为路由跳转过快,执行时页面可能还没有创建成功
211
+ const pageEl = this.getPageContainer(page);
212
+ if (pageEl) {
213
+ if (this.hideTimer) {
214
+ clearTimeout(this.hideTimer);
215
+ this.hideTimer = null;
216
+ setDisplay(this.lastHidePage, 'none');
217
+ }
218
+ this.lastHidePage = pageEl;
219
+ this.hideTimer = setTimeout(() => {
220
+ this.hideTimer = null;
221
+ setDisplay(this.lastHidePage, 'none');
222
+ }, this.animationDuration + this.animationDelay);
223
+ page.onHide?.();
224
+ }
225
+ else {
226
+ setTimeout(() => this.hide(page), 0);
227
+ }
228
+ }
229
+ addAnimation(pageEl, first = false) {
230
+ if (!pageEl)
231
+ return;
232
+ if (this.animation && !first) {
233
+ setTimeout(() => {
234
+ pageEl.classList.add('taro_page_show');
235
+ setTimeout(() => {
236
+ pageEl.classList.add('taro_page_stationed');
237
+ }, this.animationDuration);
238
+ }, this.animationDelay);
239
+ }
240
+ else {
241
+ pageEl.classList.add('taro_page_show');
242
+ pageEl.classList.add('taro_page_stationed');
243
+ }
244
+ }
245
+ getPageContainer(page) {
246
+ const path = page ? page?.path : Current.page?.path;
247
+ const id = path?.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
248
+ if (page) {
249
+ return document.querySelector(`.taro_page#${id}`);
250
+ }
251
+ const el = (id
252
+ ? document.querySelector(`.taro_page#${id}`)
253
+ : document.querySelector('.taro_page') ||
254
+ document.querySelector('.taro_router'));
255
+ return el || window;
256
+ }
257
+ bindPageEvents(page, pageEl, config = {}) {
258
+ if (!pageEl) {
259
+ pageEl = this.getPageContainer();
260
+ }
261
+ const distance = config.onReachBottomDistance || this.config.window?.onReachBottomDistance || 50;
262
+ bindPageScroll(page, pageEl, distance);
263
+ bindPageResize(page);
264
+ }
265
+ }
@@ -0,0 +1,110 @@
1
+ /* eslint-disable dot-notation */
2
+ import { container, createPageConfig, Current, eventCenter, SERVICE_IDENTIFIER, stringify } from '@tarojs/runtime';
3
+ import { Action as LocationAction } from 'history';
4
+ import UniversalRouter from 'universal-router';
5
+ import { history, prependBasename } from '../history';
6
+ import PageHandler from './page';
7
+ import stacks from './stack';
8
+ import { addLeadingSlash, routesAlias, stripBasename } from '../utils';
9
+ import { RouterConfig } from '.';
10
+ export function createRouter(app, config, framework) {
11
+ RouterConfig.config = config;
12
+ const handler = new PageHandler(config);
13
+ const runtimeHooks = container.get(SERVICE_IDENTIFIER.Hooks);
14
+ routesAlias.set(handler.router.customRoutes);
15
+ const basename = handler.router.basename;
16
+ const routes = handler.routes.map(route => ({
17
+ path: routesAlias.getAll(addLeadingSlash(route.path)),
18
+ action: route.load
19
+ }));
20
+ const entryPagePath = config.entryPagePath || routes[0].path?.[0];
21
+ const router = new UniversalRouter(routes, { baseUrl: basename || '' });
22
+ const launchParam = handler.getQuery(stacks.length);
23
+ app.onLaunch?.(launchParam);
24
+ const render = async ({ location, action }) => {
25
+ handler.pathname = decodeURI(location.pathname);
26
+ let element;
27
+ try {
28
+ element = await router.resolve(handler.router.forcePath || handler.pathname);
29
+ }
30
+ catch (error) {
31
+ if (error.status === 404) {
32
+ app.onPageNotFound?.({
33
+ path: handler.pathname
34
+ });
35
+ }
36
+ else {
37
+ throw new Error(error);
38
+ }
39
+ }
40
+ if (!element)
41
+ return;
42
+ const pageConfig = handler.pageConfig;
43
+ let enablePullDownRefresh = config?.window?.enablePullDownRefresh || false;
44
+ eventCenter.trigger('__taroRouterChange', {
45
+ toLocation: {
46
+ path: handler.pathname
47
+ }
48
+ });
49
+ if (pageConfig) {
50
+ document.title = pageConfig.navigationBarTitleText ?? document.title;
51
+ if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
52
+ enablePullDownRefresh = pageConfig.enablePullDownRefresh;
53
+ }
54
+ }
55
+ const currentPage = Current.page;
56
+ const pathname = handler.pathname;
57
+ let shouldLoad = false;
58
+ if (action === 'POP') {
59
+ // NOTE: 浏览器事件退后多次时,该事件只会被触发一次
60
+ const prevIndex = stacks.getPrevIndex(pathname);
61
+ const delta = stacks.getDelta(pathname);
62
+ handler.unload(currentPage, delta, prevIndex > -1);
63
+ if (prevIndex > -1) {
64
+ handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
65
+ }
66
+ else {
67
+ shouldLoad = true;
68
+ }
69
+ }
70
+ else {
71
+ if (handler.isTabBar) {
72
+ if (handler.isSamePage(currentPage))
73
+ return;
74
+ const prevIndex = stacks.getPrevIndex(pathname, 0);
75
+ handler.hide(currentPage);
76
+ if (prevIndex > -1) {
77
+ // NOTE: tabbar 页且之前出现过,直接复用
78
+ return handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
79
+ }
80
+ }
81
+ else if (action === 'REPLACE') {
82
+ const delta = stacks.getDelta(pathname);
83
+ // NOTE: 页面路由记录并不会清空,只是移除掉缓存的 stack 以及页面
84
+ handler.unload(currentPage, delta);
85
+ }
86
+ else if (action === 'PUSH') {
87
+ handler.hide(currentPage);
88
+ }
89
+ shouldLoad = true;
90
+ }
91
+ if (shouldLoad || stacks.length < 1) {
92
+ const el = element.default ?? element;
93
+ const loadConfig = { ...pageConfig };
94
+ const stacksIndex = stacks.length;
95
+ delete loadConfig['path'];
96
+ delete loadConfig['load'];
97
+ const page = createPageConfig(enablePullDownRefresh ? runtimeHooks.createPullDownComponent?.(el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
98
+ return handler.load(page, pageConfig, stacksIndex);
99
+ }
100
+ };
101
+ if (history.location.pathname === '/') {
102
+ const stripped = stripBasename(history.location.pathname, handler.basename);
103
+ if (stripped === '/' || stripped === '') {
104
+ history.replace(prependBasename(entryPagePath + history.location.search));
105
+ }
106
+ }
107
+ render({ location: history.location, action: LocationAction.Push });
108
+ app.onShow?.(launchParam);
109
+ return history.listen(render);
110
+ }
@@ -0,0 +1,57 @@
1
+ class Stacks {
2
+ stacks = [];
3
+ backDelta = 0;
4
+ set delta(delta) {
5
+ if (delta > 0) {
6
+ this.backDelta = delta;
7
+ }
8
+ else if (this.backDelta > 0) {
9
+ --this.backDelta;
10
+ }
11
+ else {
12
+ this.backDelta = 0;
13
+ }
14
+ }
15
+ get delta() {
16
+ return this.backDelta;
17
+ }
18
+ get length() {
19
+ return this.stacks.length;
20
+ }
21
+ get last() {
22
+ return this.stacks[this.length - 1];
23
+ }
24
+ get() {
25
+ return this.stacks;
26
+ }
27
+ getItem(index) {
28
+ return this.stacks[index];
29
+ }
30
+ getLastIndex(pathname, stateWith = 1) {
31
+ const list = [...this.stacks].reverse();
32
+ return list.findIndex((page, i) => i >= stateWith && page.path?.replace(/\?.*/g, '') === pathname);
33
+ }
34
+ getDelta(pathname) {
35
+ if (this.backDelta >= 1) {
36
+ return this.backDelta;
37
+ }
38
+ const index = this.getLastIndex(pathname);
39
+ // NOTE: 此处为了修复浏览器后退多级页面,在大量重复路由状况下可能出现判断错误的情况 (增强判断能力只能考虑在 query 中新增参数来判断,暂时搁置)
40
+ return index > 0 ? index : 1;
41
+ }
42
+ getPrevIndex(pathname, stateWith = 1) {
43
+ const lastIndex = this.getLastIndex(pathname, stateWith);
44
+ if (lastIndex < 0) {
45
+ return -1;
46
+ }
47
+ return this.length - 1 - lastIndex;
48
+ }
49
+ pop() {
50
+ return this.stacks.pop();
51
+ }
52
+ push(page) {
53
+ return this.stacks.push(page);
54
+ }
55
+ }
56
+ const stacks = new Stacks();
57
+ export default stacks;
package/dist/tabbar.js ADDED
@@ -0,0 +1,29 @@
1
+ // @ts-nocheck
2
+ import { initTabBarApis } from '@tarojs/taro';
3
+ import { history } from './history';
4
+ export function initTabbar(config) {
5
+ if (config.tabBar == null) {
6
+ return;
7
+ }
8
+ // TODO: custom-tab-bar
9
+ const tabbar = document.createElement('taro-tabbar');
10
+ const homePage = config.entryPagePath || (config.pages ? config.pages[0] : '');
11
+ tabbar.conf = config.tabBar;
12
+ tabbar.conf.homePage = history.location.pathname === '/' ? homePage : history.location.pathname;
13
+ const routerConfig = config.router;
14
+ tabbar.conf.mode = routerConfig && routerConfig.mode ? routerConfig.mode : 'hash';
15
+ if (routerConfig.customRoutes) {
16
+ tabbar.conf.custom = true;
17
+ tabbar.conf.customRoutes = routerConfig.customRoutes;
18
+ }
19
+ else {
20
+ tabbar.conf.custom = false;
21
+ tabbar.conf.customRoutes = {};
22
+ }
23
+ if (typeof routerConfig.basename !== 'undefined') {
24
+ tabbar.conf.basename = routerConfig.basename;
25
+ }
26
+ const container = document.getElementById('container');
27
+ container?.appendChild(tabbar);
28
+ initTabBarApis(config);
29
+ }
package/dist/utils.js ADDED
@@ -0,0 +1,39 @@
1
+ export const addLeadingSlash = (url = '') => (url.charAt(0) === '/' ? url : '/' + url);
2
+ export const hasBasename = (path = '', prefix = '') => new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path) || path === prefix;
3
+ export const stripBasename = (path = '', prefix = '') => hasBasename(path, prefix) ? path.substr(prefix.length) : path;
4
+ class RoutesAlias {
5
+ conf = [];
6
+ set(customRoutes = {}) {
7
+ for (let key in customRoutes) {
8
+ const path = customRoutes[key];
9
+ key = addLeadingSlash(key);
10
+ if (typeof path === 'string') {
11
+ this.conf.push([key, addLeadingSlash(path)]);
12
+ }
13
+ else if (path?.length > 0) {
14
+ this.conf.push(...path.map(p => [key, addLeadingSlash(p)]));
15
+ }
16
+ }
17
+ }
18
+ getConfig = (url = '') => {
19
+ const customRoute = this.conf.filter((arr) => {
20
+ return arr.includes(url);
21
+ });
22
+ return customRoute[0];
23
+ };
24
+ getOrigin = (url = '') => {
25
+ return this.getConfig(url)?.[0] || url;
26
+ };
27
+ getAlias = (url = '') => {
28
+ return this.getConfig(url)?.[1] || url;
29
+ };
30
+ getAll = (url = '') => {
31
+ return this.conf
32
+ .filter((arr) => arr.includes(url))
33
+ .reduceRight((p, a) => {
34
+ p.unshift(a[1]);
35
+ return p;
36
+ }, []);
37
+ };
38
+ }
39
+ export const routesAlias = new RoutesAlias();