@tarojs/router 3.5.0-canary.1 → 3.5.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.
Files changed (44) hide show
  1. package/dist/animation.d.ts +4 -0
  2. package/dist/animation.js +29 -0
  3. package/dist/api.d.ts +7 -0
  4. package/dist/api.js +103 -0
  5. package/dist/events/resize.d.ts +2 -0
  6. package/dist/events/resize.js +13 -0
  7. package/dist/events/scroll.d.ts +2 -0
  8. package/dist/events/scroll.js +31 -0
  9. package/dist/history.d.ts +5 -0
  10. package/dist/history.js +100 -0
  11. package/dist/index.cjs.d.ts +23 -0
  12. package/dist/index.cjs.js +1043 -0
  13. package/dist/index.cjs.js.map +1 -0
  14. package/dist/index.d.ts +4 -0
  15. package/dist/index.esm.d.ts +23 -0
  16. package/dist/index.esm.js +1026 -0
  17. package/dist/index.esm.js.map +1 -0
  18. package/dist/index.js +4 -2674
  19. package/dist/router/index.d.ts +11 -0
  20. package/dist/router/index.js +22 -0
  21. package/dist/router/mpa.d.ts +10 -0
  22. package/dist/router/mpa.js +62 -0
  23. package/dist/router/multi-page.d.ts +27 -0
  24. package/dist/router/multi-page.js +112 -0
  25. package/dist/router/page.d.ts +42 -0
  26. package/dist/router/page.js +281 -0
  27. package/dist/router/spa.d.ts +3 -0
  28. package/dist/router/spa.js +130 -0
  29. package/dist/router/stack.d.ts +18 -0
  30. package/dist/router/stack.js +59 -0
  31. package/dist/tabbar.d.ts +2 -0
  32. package/dist/tabbar.js +29 -0
  33. package/dist/utils/index.d.ts +15 -0
  34. package/dist/utils/index.js +47 -0
  35. package/dist/utils/navigate.d.ts +5 -0
  36. package/dist/utils/navigate.js +44 -0
  37. package/package.json +18 -17
  38. package/types/api.d.ts +13 -0
  39. package/types/history.d.ts +6 -0
  40. package/types/router.d.ts +30 -0
  41. package/dist/index.js.map +0 -1
  42. package/dist/router.esm.js +0 -2650
  43. package/dist/router.esm.js.map +0 -1
  44. package/types/index.d.ts +0 -287
@@ -0,0 +1,4 @@
1
+ /**
2
+ * 插入页面动画需要的样式
3
+ */
4
+ export declare function loadAnimateStyle(ms?: number): void;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * 插入页面动画需要的样式
3
+ */
4
+ export function loadAnimateStyle(ms = 300) {
5
+ const css = `
6
+ .taro_router .taro_page {
7
+ position: absolute;
8
+ left: 0;
9
+ top: 0;
10
+ width: 100%;
11
+ height: 100%;
12
+ background-color: #fff;
13
+ transform: translate(100%, 0);
14
+ transition: transform ${ms}ms;
15
+ z-index: 0;
16
+ }
17
+
18
+ .taro_router .taro_page.taro_tabbar_page,
19
+ .taro_router .taro_page.taro_page_show.taro_page_stationed {
20
+ transform: none;
21
+ }
22
+
23
+ .taro_router .taro_page.taro_page_show {
24
+ transform: translate(0, 0);
25
+ }`;
26
+ const style = document.createElement('style');
27
+ style.innerHTML = css;
28
+ document.getElementsByTagName('head')[0].appendChild(style);
29
+ }
package/dist/api.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import Taro from '@tarojs/taro';
2
+ export declare function navigateTo(option: Taro.navigateTo.Option): ReturnType<typeof Taro.navigateTo>;
3
+ export declare function redirectTo(option: Taro.redirectTo.Option): ReturnType<typeof Taro.redirectTo>;
4
+ export declare function navigateBack(option?: Taro.navigateBack.Option): ReturnType<typeof Taro.navigateBack>;
5
+ export declare function switchTab(option: Taro.switchTab.Option): ReturnType<typeof Taro.switchTab>;
6
+ export declare function reLaunch(option: Taro.reLaunch.Option): ReturnType<typeof Taro.reLaunch>;
7
+ export declare function getCurrentPages(): Taro.Page[];
package/dist/api.js ADDED
@@ -0,0 +1,103 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { parsePath } from 'history';
11
+ import { history, prependBasename } from './history';
12
+ import { RouterConfig } from './router';
13
+ import stacks from './router/stack';
14
+ import { addLeadingSlash, routesAlias } from './utils';
15
+ function processNavigateUrl(option) {
16
+ var _a;
17
+ const pathPieces = parsePath(option.url);
18
+ // 处理相对路径
19
+ if ((_a = pathPieces.pathname) === null || _a === void 0 ? void 0 : _a.includes('./')) {
20
+ const parts = routesAlias.getOrigin(history.location.pathname).split('/');
21
+ parts.pop();
22
+ pathPieces.pathname.split('/').forEach((item) => {
23
+ if (item === '.') {
24
+ return;
25
+ }
26
+ item === '..' ? parts.pop() : parts.push(item);
27
+ });
28
+ pathPieces.pathname = parts.join('/');
29
+ }
30
+ // 处理自定义路由
31
+ pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname));
32
+ // 处理 basename
33
+ pathPieces.pathname = prependBasename(pathPieces.pathname);
34
+ // hack fix history v5 bug: https://github.com/remix-run/history/issues/814
35
+ if (!pathPieces.search)
36
+ pathPieces.search = '';
37
+ return pathPieces;
38
+ }
39
+ function navigate(option, method) {
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ return new Promise((resolve, reject) => {
42
+ const { success, complete, fail } = option;
43
+ const unListen = history.listen(() => {
44
+ const res = { errMsg: `${method}:ok` };
45
+ success === null || success === void 0 ? void 0 : success(res);
46
+ complete === null || complete === void 0 ? void 0 : complete(res);
47
+ resolve(res);
48
+ unListen();
49
+ });
50
+ try {
51
+ if ('url' in option) {
52
+ const pathPieces = processNavigateUrl(option);
53
+ const state = { timestamp: Date.now() };
54
+ if (method === 'navigateTo') {
55
+ history.push(pathPieces, state);
56
+ }
57
+ else if (method === 'redirectTo' || method === 'switchTab') {
58
+ history.replace(pathPieces, state);
59
+ }
60
+ else if (method === 'reLaunch') {
61
+ stacks.delta = stacks.length;
62
+ history.replace(pathPieces, state);
63
+ }
64
+ }
65
+ else if (method === 'navigateBack') {
66
+ stacks.delta = option.delta;
67
+ history.go(-option.delta);
68
+ }
69
+ }
70
+ catch (error) {
71
+ const res = { errMsg: `${method}:fail ${error.message || error}` };
72
+ fail === null || fail === void 0 ? void 0 : fail(res);
73
+ complete === null || complete === void 0 ? void 0 : complete(res);
74
+ reject(res);
75
+ }
76
+ });
77
+ });
78
+ }
79
+ export function navigateTo(option) {
80
+ return navigate(option, 'navigateTo');
81
+ }
82
+ export function redirectTo(option) {
83
+ return navigate(option, 'redirectTo');
84
+ }
85
+ export function navigateBack(option = { delta: 1 }) {
86
+ if (!option.delta || option.delta < 1) {
87
+ option.delta = 1;
88
+ }
89
+ return navigate(option, 'navigateBack');
90
+ }
91
+ export function switchTab(option) {
92
+ return navigate(option, 'switchTab');
93
+ }
94
+ export function reLaunch(option) {
95
+ return navigate(option, 'reLaunch');
96
+ }
97
+ export function getCurrentPages() {
98
+ if (process.env.NODE_ENV === 'development' && RouterConfig.mode === 'multi') {
99
+ console.warn('多页面路由模式不支持使用 getCurrentPages 方法!');
100
+ }
101
+ const pages = stacks.get();
102
+ return pages.map(e => (Object.assign(Object.assign({}, e), { route: e.path || '' })));
103
+ }
@@ -0,0 +1,2 @@
1
+ import { PageInstance } from '@tarojs/runtime';
2
+ export declare function bindPageResize(page: PageInstance): void;
@@ -0,0 +1,13 @@
1
+ let pageResizeFn;
2
+ export function bindPageResize(page) {
3
+ pageResizeFn && window.removeEventListener('resize', pageResizeFn);
4
+ pageResizeFn = function () {
5
+ page.onResize && page.onResize({
6
+ size: {
7
+ windowHeight: window.innerHeight,
8
+ windowWidth: window.innerWidth
9
+ }
10
+ });
11
+ };
12
+ window.addEventListener('resize', pageResizeFn, false);
13
+ }
@@ -0,0 +1,2 @@
1
+ import { PageInstance } from '@tarojs/runtime';
2
+ export declare function bindPageScroll(page: PageInstance, pageEl: HTMLElement, distance?: number): void;
@@ -0,0 +1,31 @@
1
+ let pageScrollFn;
2
+ let pageDOM = window;
3
+ export function bindPageScroll(page, pageEl, distance = 50) {
4
+ pageScrollFn && pageEl.removeEventListener('scroll', pageScrollFn);
5
+ pageDOM = pageEl;
6
+ let isReachBottom = false;
7
+ pageScrollFn = function () {
8
+ var _a;
9
+ (_a = page.onPageScroll) === null || _a === void 0 ? void 0 : _a.call(page, {
10
+ scrollTop: pageDOM instanceof Window ? window.scrollY : pageDOM.scrollTop
11
+ });
12
+ if (isReachBottom && getOffset() > distance) {
13
+ isReachBottom = false;
14
+ }
15
+ if (page.onReachBottom &&
16
+ !isReachBottom &&
17
+ getOffset() < distance) {
18
+ isReachBottom = true;
19
+ page.onReachBottom();
20
+ }
21
+ };
22
+ pageDOM.addEventListener('scroll', pageScrollFn, false);
23
+ }
24
+ function getOffset() {
25
+ if (pageDOM instanceof Window) {
26
+ return document.documentElement.scrollHeight - window.scrollY - window.innerHeight;
27
+ }
28
+ else {
29
+ return pageDOM.scrollHeight - pageDOM.scrollTop - pageDOM.clientHeight;
30
+ }
31
+ }
@@ -0,0 +1,5 @@
1
+ import { IH5RouterConfig } from '@tarojs/taro/types/compile';
2
+ import { History } from 'history';
3
+ export declare let history: History;
4
+ export declare function setHistoryMode(mode?: IH5RouterConfig['mode'], base?: string): void;
5
+ export declare function prependBasename(url?: string): string;
@@ -0,0 +1,100 @@
1
+ import { Action, createBrowserHistory, createHashHistory } from 'history';
2
+ import { RouterConfig } from './router';
3
+ export let history;
4
+ let basename = '/';
5
+ class MpaHistory {
6
+ constructor() {
7
+ this.back = window.history.back;
8
+ this.forward = window.history.forward;
9
+ this.pushState = this.eventState('pushState');
10
+ this.replaceState = this.eventState('replaceState');
11
+ }
12
+ get location() {
13
+ return {
14
+ pathname: window.location.pathname,
15
+ search: window.location.search,
16
+ hash: window.location.hash,
17
+ key: `${window.history.length}`,
18
+ state: window.history.state
19
+ };
20
+ }
21
+ createHref(_to) {
22
+ throw new Error('Method not implemented.');
23
+ }
24
+ parseUrl(to) {
25
+ let url = to.pathname || '';
26
+ if (RouterConfig.isPage(url)) {
27
+ url += '.html';
28
+ }
29
+ if (to.search) {
30
+ url += `?${to.search}`;
31
+ }
32
+ if (to.hash) {
33
+ url += `#${to.hash}`;
34
+ }
35
+ return url;
36
+ }
37
+ push(to, _state = {}) {
38
+ window.location.pathname = this.parseUrl(to);
39
+ // this.pushState(_state, '', this.parseUrl(to))
40
+ }
41
+ replace(to, _state = {}) {
42
+ window.location.replace(this.parseUrl(to));
43
+ // this.replaceState(_state, '', this.parseUrl(to))
44
+ }
45
+ go(delta) {
46
+ window.history.go(delta);
47
+ }
48
+ listen(listener) {
49
+ function callback(e) {
50
+ if (e.action === 'pushState') {
51
+ listener({ action: Action.Push, location: this.location });
52
+ }
53
+ else if (e.action === 'replaceState') {
54
+ listener({ action: Action.Replace, location: this.location });
55
+ }
56
+ else {
57
+ // NOTE: 这里包括 back、forward、go 三种可能,并非是 POP 事件
58
+ listener({ action: Action.Pop, location: this.location });
59
+ }
60
+ }
61
+ window.addEventListener('popstate', callback);
62
+ return () => {
63
+ window.removeEventListener('popstate', callback);
64
+ };
65
+ }
66
+ block(_blocker) {
67
+ throw new Error('Method not implemented.');
68
+ }
69
+ eventState(action) {
70
+ return (data, unused, url) => {
71
+ const wrapper = window.history[action](data, unused, url);
72
+ const evt = new Event(action);
73
+ evt.action = action;
74
+ evt.state = data;
75
+ evt.unused = unused;
76
+ evt.url = url;
77
+ window.dispatchEvent(evt);
78
+ return wrapper;
79
+ };
80
+ }
81
+ }
82
+ export function setHistoryMode(mode, base = '/') {
83
+ const options = {
84
+ window
85
+ };
86
+ basename = base;
87
+ if (mode === 'browser') {
88
+ history = createBrowserHistory(options);
89
+ }
90
+ else if (mode === 'multi') {
91
+ history = new MpaHistory();
92
+ }
93
+ else {
94
+ // default is hash
95
+ history = createHashHistory(options);
96
+ }
97
+ }
98
+ export function prependBasename(url = '') {
99
+ return basename.replace(/\/$/, '') + '/' + url.replace(/^\//, '');
100
+ }
@@ -0,0 +1,23 @@
1
+ import Taro from "@tarojs/taro";
2
+ import { History } from "history";
3
+ /* eslint-disable dot-notation */
4
+ import { AppInstance } from "@tarojs/runtime";
5
+ import { MpaRouterConfig, SpaRouterConfig } from "../../types/router";
6
+ declare function navigateTo(option: Taro.navigateTo.Option): ReturnType<typeof Taro.navigateTo>;
7
+ declare function redirectTo(option: Taro.redirectTo.Option): ReturnType<typeof Taro.redirectTo>;
8
+ declare function navigateBack(option?: Taro.navigateBack.Option): ReturnType<typeof Taro.navigateBack>;
9
+ declare function switchTab(option: Taro.switchTab.Option): ReturnType<typeof Taro.switchTab>;
10
+ declare function reLaunch(option: Taro.reLaunch.Option): ReturnType<typeof Taro.reLaunch>;
11
+ declare function getCurrentPages(): Taro.Page[];
12
+ declare let history: History;
13
+ // TODO 支持多路由 (APP 生命周期仅触发一次)
14
+ /** Note: 关于多页面应用
15
+ * - 需要配置路由映射(根目录跳转、404 页面……)
16
+ * - app.onPageNotFound 事件不支持
17
+ * - 应用生命周期可能多次触发
18
+ * - TabBar 会多次加载
19
+ * - 不支持路由动画
20
+ */
21
+ declare function createMultiRouter(app: AppInstance, config: MpaRouterConfig, framework?: string): Promise<void>;
22
+ declare function createRouter(app: AppInstance, config: SpaRouterConfig, framework?: string): () => void;
23
+ export { navigateTo, redirectTo, navigateBack, switchTab, reLaunch, getCurrentPages, history, createMultiRouter, createRouter };