@tarojs/router 3.5.0-alpha.1 → 3.5.0-alpha.2

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.
@@ -1,110 +0,0 @@
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
- }
@@ -1,57 +0,0 @@
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 DELETED
@@ -1,29 +0,0 @@
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 DELETED
@@ -1,39 +0,0 @@
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();