@tarojs/router 3.4.7 → 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.
@@ -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.js ADDED
@@ -0,0 +1,91 @@
1
+ import { parsePath } from 'history';
2
+ import stacks from './router/stack';
3
+ import { history, prependBasename } from './history';
4
+ import { routesAlias, addLeadingSlash } from './utils';
5
+ import { RouterConfig } from './router';
6
+ function processNavigateUrl(option) {
7
+ const pathPieces = parsePath(option.url);
8
+ // 处理相对路径
9
+ if (pathPieces.pathname?.includes('./')) {
10
+ const parts = routesAlias.getOrigin(history.location.pathname).split('/');
11
+ parts.pop();
12
+ pathPieces.pathname.split('/').forEach((item) => {
13
+ if (item === '.') {
14
+ return;
15
+ }
16
+ item === '..' ? parts.pop() : parts.push(item);
17
+ });
18
+ pathPieces.pathname = parts.join('/');
19
+ }
20
+ // 处理自定义路由
21
+ pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname));
22
+ // 处理 basename
23
+ pathPieces.pathname = prependBasename(pathPieces.pathname);
24
+ // hack fix history v5 bug: https://github.com/remix-run/history/issues/814
25
+ if (!pathPieces.search)
26
+ pathPieces.search = '';
27
+ return pathPieces;
28
+ }
29
+ async function navigate(option, method) {
30
+ return new Promise((resolve, reject) => {
31
+ const { success, complete, fail } = option;
32
+ const unListen = history.listen(() => {
33
+ const res = { errMsg: `${method}:ok` };
34
+ success?.(res);
35
+ complete?.(res);
36
+ resolve(res);
37
+ unListen();
38
+ });
39
+ try {
40
+ if ('url' in option) {
41
+ const pathPieces = processNavigateUrl(option);
42
+ const state = { timestamp: Date.now() };
43
+ if (method === 'navigateTo') {
44
+ history.push(pathPieces, state);
45
+ }
46
+ else if (method === 'redirectTo' || method === 'switchTab') {
47
+ history.replace(pathPieces, state);
48
+ }
49
+ else if (method === 'reLaunch') {
50
+ stacks.delta = stacks.length;
51
+ history.replace(pathPieces, state);
52
+ }
53
+ }
54
+ else if (method === 'navigateBack') {
55
+ stacks.delta = option.delta;
56
+ history.go(-option.delta);
57
+ }
58
+ }
59
+ catch (error) {
60
+ const res = { errMsg: `${method}:fail ${error.message || error}` };
61
+ fail?.(res);
62
+ complete?.(res);
63
+ reject(res);
64
+ }
65
+ });
66
+ }
67
+ export function navigateTo(option) {
68
+ return navigate(option, 'navigateTo');
69
+ }
70
+ export function redirectTo(option) {
71
+ return navigate(option, 'redirectTo');
72
+ }
73
+ export function navigateBack(option = { delta: 1 }) {
74
+ if (!option.delta || option.delta < 1) {
75
+ option.delta = 1;
76
+ }
77
+ return navigate(option, 'navigateBack');
78
+ }
79
+ export function switchTab(option) {
80
+ return navigate(option, 'switchTab');
81
+ }
82
+ export function reLaunch(option) {
83
+ return navigate(option, 'reLaunch');
84
+ }
85
+ export function getCurrentPages() {
86
+ if (process.env.NODE_ENV === 'development' && RouterConfig.mode === 'multi') {
87
+ console.warn('多页面路由模式不支持使用 getCurrentPages 方法!');
88
+ }
89
+ const pages = stacks.get();
90
+ return pages.map(e => ({ ...e, route: e.path || '' }));
91
+ }
@@ -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,30 @@
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
+ page.onPageScroll && page.onPageScroll({
9
+ scrollTop: pageDOM instanceof Window ? window.scrollY : pageDOM.scrollTop
10
+ });
11
+ if (isReachBottom && getOffset() > distance) {
12
+ isReachBottom = false;
13
+ }
14
+ if (page.onReachBottom &&
15
+ !isReachBottom &&
16
+ getOffset() < distance) {
17
+ isReachBottom = true;
18
+ page.onReachBottom();
19
+ }
20
+ };
21
+ pageDOM.addEventListener('scroll', pageScrollFn, false);
22
+ }
23
+ function getOffset() {
24
+ if (pageDOM instanceof Window) {
25
+ return document.documentElement.scrollHeight - window.scrollY - window.innerHeight;
26
+ }
27
+ else {
28
+ return pageDOM.scrollHeight - pageDOM.scrollTop - pageDOM.clientHeight;
29
+ }
30
+ }
@@ -0,0 +1,99 @@
1
+ import { Action, createBrowserHistory, createHashHistory } from 'history';
2
+ import { RouterConfig } from './router';
3
+ export let history;
4
+ let basename = '/';
5
+ class MpaHistory {
6
+ action;
7
+ get location() {
8
+ return {
9
+ pathname: window.location.pathname,
10
+ search: window.location.search,
11
+ hash: window.location.hash,
12
+ key: `${window.history.length}`,
13
+ state: window.history.state
14
+ };
15
+ }
16
+ createHref(_to) {
17
+ throw new Error('Method not implemented.');
18
+ }
19
+ parseUrl(to) {
20
+ let url = to.pathname || '';
21
+ if (RouterConfig.isPage(url)) {
22
+ url += '.html';
23
+ }
24
+ if (to.search) {
25
+ url += `?${to.search}`;
26
+ }
27
+ if (to.hash) {
28
+ url += `#${to.hash}`;
29
+ }
30
+ return url;
31
+ }
32
+ push(to, _state = {}) {
33
+ window.location.pathname = this.parseUrl(to);
34
+ // this.pushState(_state, '', this.parseUrl(to))
35
+ }
36
+ replace(to, _state = {}) {
37
+ window.location.replace(this.parseUrl(to));
38
+ // this.replaceState(_state, '', this.parseUrl(to))
39
+ }
40
+ go(delta) {
41
+ window.history.go(delta);
42
+ }
43
+ back = window.history.back;
44
+ forward = window.history.forward;
45
+ listen(listener) {
46
+ function callback(e) {
47
+ if (e.action === 'pushState') {
48
+ listener({ action: Action.Push, location: this.location });
49
+ }
50
+ else if (e.action === 'replaceState') {
51
+ listener({ action: Action.Replace, location: this.location });
52
+ }
53
+ else {
54
+ // NOTE: 这里包括 back、forward、go 三种可能,并非是 POP 事件
55
+ listener({ action: Action.Pop, location: this.location });
56
+ }
57
+ }
58
+ window.addEventListener('popstate', callback);
59
+ return () => {
60
+ window.removeEventListener('popstate', callback);
61
+ };
62
+ }
63
+ block(_blocker) {
64
+ throw new Error('Method not implemented.');
65
+ }
66
+ pushState = this.eventState('pushState');
67
+ replaceState = this.eventState('replaceState');
68
+ eventState(action) {
69
+ return (data, unused, url) => {
70
+ const wrapper = window.history[action](data, unused, url);
71
+ const evt = new Event(action);
72
+ evt.action = action;
73
+ evt.state = data;
74
+ evt.unused = unused;
75
+ evt.url = url;
76
+ window.dispatchEvent(evt);
77
+ return wrapper;
78
+ };
79
+ }
80
+ }
81
+ export function setHistoryMode(mode, base = '/') {
82
+ const options = {
83
+ window
84
+ };
85
+ basename = base;
86
+ if (mode === 'browser') {
87
+ history = createBrowserHistory(options);
88
+ }
89
+ else if (mode === 'multi') {
90
+ history = new MpaHistory();
91
+ }
92
+ else {
93
+ // default is hash
94
+ history = createHashHistory(options);
95
+ }
96
+ }
97
+ export function prependBasename(url = '') {
98
+ return basename.replace(/\/$/, '') + '/' + url.replace(/^\//, '');
99
+ }