@tarojs/router 4.0.0-canary.8 → 4.0.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 (45) hide show
  1. package/LICENSE +17 -3
  2. package/README.md +1 -1
  3. package/dist/api.js +31 -22
  4. package/dist/events/resize.d.ts +1 -1
  5. package/dist/events/resize.js +15 -7
  6. package/dist/events/scroll.d.ts +1 -1
  7. package/dist/events/scroll.js +4 -1
  8. package/dist/history.d.ts +22 -1
  9. package/dist/history.js +20 -8
  10. package/dist/index.cjs.js +798 -339
  11. package/dist/index.d.ts +6 -1
  12. package/dist/index.esm.js +758 -311
  13. package/dist/index.js +52 -4
  14. package/dist/navigationBar.d.ts +2 -0
  15. package/dist/navigationBar.js +44 -0
  16. package/dist/router/index.js +7 -3
  17. package/dist/router/mpa.d.ts +2 -1
  18. package/dist/router/mpa.js +29 -19
  19. package/dist/router/multi-page.d.ts +5 -2
  20. package/dist/router/multi-page.js +27 -43
  21. package/dist/router/navigation-bar.d.ts +36 -0
  22. package/dist/router/navigation-bar.js +252 -0
  23. package/dist/router/page.d.ts +11 -4
  24. package/dist/router/page.js +63 -59
  25. package/dist/router/spa.d.ts +2 -1
  26. package/dist/router/spa.js +81 -43
  27. package/dist/router/stack.d.ts +1 -1
  28. package/dist/router/stack.js +2 -1
  29. package/dist/style.d.ts +6 -1
  30. package/dist/style.js +109 -6
  31. package/dist/tabbar.d.ts +2 -1
  32. package/dist/tabbar.js +4 -3
  33. package/dist/utils/index.d.ts +1 -8
  34. package/dist/utils/index.js +5 -20
  35. package/dist/utils/navigate.d.ts +9 -5
  36. package/dist/utils/navigate.js +24 -37
  37. package/package.json +28 -27
  38. package/types/api.d.ts +5 -0
  39. package/types/component.d.ts +5 -0
  40. package/types/router.d.ts +2 -0
  41. package/types/taro.d.ts +8 -0
  42. package/dist/index.cjs.d.ts +0 -22
  43. package/dist/index.cjs.js.map +0 -1
  44. package/dist/index.esm.d.ts +0 -22
  45. package/dist/index.esm.js.map +0 -1
package/LICENSE CHANGED
@@ -154,7 +154,21 @@ See `/LICENSE` for details of the license.
154
154
 
155
155
  ==================
156
156
 
157
- MIT (stencil-vue2-output-target):
158
- The following files embed [stencil-vue2-output-target](https://github.com/diondree/stencil-vue2-output-target) MIT:
159
- `/packages/taro-components-library-vue2/src/vue-component-lib/utils.ts`
157
+ MIT (weui):
158
+ The following files embed [weui](https://github.com/Tencent/weui) MIT:
159
+ `/packages/taro-components/src/components/*.scss`
160
+ See `/LICENSE.txt` for details of the license.
161
+
162
+ ==================
163
+
164
+ Apache-2.0 (intersection-observer):
165
+ The following files embed [intersection-observer](https://github.com/GoogleChromeLabs/intersection-observer) Apache-2.0:
166
+ `/packages/taro-api/src/polyfill/intersection-observer.ts`
167
+ See `/LICENSE.txt` for details of the license.
168
+
169
+ ==================
170
+
171
+ MIT (babel-plugin-jsx-dom-expressions):
172
+ The following files embed [babel-plugin-jsx-dom-expressions](https://github.com/ryansolid/dom-expressions/blob/main/packages/babel-plugin-jsx-dom-expressions) MIT:
173
+ `/packages/babel-plugin-transform-solid-jsx/src/*`
160
174
  See `/LICENSE` for details of the license.
package/README.md CHANGED
@@ -20,7 +20,7 @@ H5 端路由系统。
20
20
 
21
21
  #### `type`
22
22
 
23
- 框架类型,`react` | `vue` | `nerv` 三选一。
23
+ 框架类型,`react` | `vue` | `solid` | `preact` 四选一。
24
24
 
25
25
  #### `framework`
26
26
 
package/dist/api.js CHANGED
@@ -1,17 +1,13 @@
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
- };
1
+ import { __awaiter } from 'tslib';
2
+ import { addLeadingSlash } from '@tarojs/runtime';
3
+ import { EventChannel } from '@tarojs/shared';
10
4
  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';
5
+ import { history, prependBasename } from './history.js';
6
+ import { RouterConfig } from './router/index.js';
7
+ import stacks from './router/stack.js';
8
+ import { routesAlias } from './utils/index.js';
9
+
10
+ const routeEvtChannel = EventChannel.routeChannel;
15
11
  function processNavigateUrl(option) {
16
12
  var _a;
17
13
  const pathPieces = parsePath(option.url);
@@ -20,13 +16,14 @@ function processNavigateUrl(option) {
20
16
  const parts = routesAlias.getOrigin(history.location.pathname).split('/');
21
17
  parts.pop();
22
18
  pathPieces.pathname.split('/').forEach((item) => {
23
- if (item === '.') {
19
+ if (item === '.')
24
20
  return;
25
- }
26
21
  item === '..' ? parts.pop() : parts.push(item);
27
22
  });
28
23
  pathPieces.pathname = parts.join('/');
29
24
  }
25
+ // 确保是 / 开头的路径
26
+ pathPieces.pathname = addLeadingSlash(pathPieces.pathname);
30
27
  // 处理自定义路由
31
28
  pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname));
32
29
  // 处理 basename
@@ -43,6 +40,10 @@ function navigate(option, method) {
43
40
  const { success, complete, fail } = option;
44
41
  const unListen = history.listen(() => {
45
42
  const res = { errMsg: `${method}:ok` };
43
+ if (method === 'navigateTo') {
44
+ res.eventChannel = routeEvtChannel;
45
+ routeEvtChannel.addEvents(option.events);
46
+ }
46
47
  success === null || success === void 0 ? void 0 : success(res);
47
48
  complete === null || complete === void 0 ? void 0 : complete(res);
48
49
  resolve(res);
@@ -53,6 +54,7 @@ function navigate(option, method) {
53
54
  const pathPieces = processNavigateUrl(option);
54
55
  const state = { timestamp: Date.now() };
55
56
  if (method === 'navigateTo') {
57
+ // Note: 由于 spa 会针对弱网情况下,短时间内多次跳转同一个页面跳转加了锁,后续如果有用户反馈返回无效,那可能是这个问题
56
58
  history.push(pathPieces, state);
57
59
  }
58
60
  else if (method === 'redirectTo' || method === 'switchTab') {
@@ -77,33 +79,40 @@ function navigate(option, method) {
77
79
  const res = { errMsg: `${method}:fail ${error.message || error}` };
78
80
  fail === null || fail === void 0 ? void 0 : fail(res);
79
81
  complete === null || complete === void 0 ? void 0 : complete(res);
80
- reject(res);
82
+ if (fail || complete) {
83
+ return resolve(res);
84
+ }
85
+ else {
86
+ return reject(res);
87
+ }
81
88
  }
82
89
  });
83
90
  });
84
91
  }
85
- export function navigateTo(option) {
92
+ function navigateTo(option) {
86
93
  return navigate(option, 'navigateTo');
87
94
  }
88
- export function redirectTo(option) {
95
+ function redirectTo(option) {
89
96
  return navigate(option, 'redirectTo');
90
97
  }
91
- export function navigateBack(option = { delta: 1 }) {
98
+ function navigateBack(option = { delta: 1 }) {
92
99
  if (!option.delta || option.delta < 1) {
93
100
  option.delta = 1;
94
101
  }
95
102
  return navigate(option, 'navigateBack');
96
103
  }
97
- export function switchTab(option) {
104
+ function switchTab(option) {
98
105
  return navigate(option, 'switchTab');
99
106
  }
100
- export function reLaunch(option) {
107
+ function reLaunch(option) {
101
108
  return navigate(option, 'reLaunch');
102
109
  }
103
- export function getCurrentPages() {
110
+ function getCurrentPages() {
104
111
  if (process.env.NODE_ENV !== 'production' && RouterConfig.mode === 'multi') {
105
112
  console.warn('多页面路由模式不支持使用 getCurrentPages 方法!');
106
113
  }
107
114
  const pages = stacks.get();
108
115
  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, '')) || '' })); });
109
116
  }
117
+
118
+ export { getCurrentPages, navigateBack, navigateTo, reLaunch, redirectTo, switchTab };
@@ -1,2 +1,2 @@
1
- import { PageInstance } from '@tarojs/runtime';
1
+ import type { PageInstance } from '@tarojs/runtime';
2
2
  export declare function bindPageResize(page: PageInstance): void;
@@ -1,13 +1,21 @@
1
1
  let pageResizeFn;
2
- export function bindPageResize(page) {
2
+ function bindPageResize(page) {
3
3
  pageResizeFn && window.removeEventListener('resize', pageResizeFn);
4
4
  pageResizeFn = function () {
5
- page.onResize && page.onResize({
6
- size: {
7
- windowHeight: window.innerHeight,
8
- windowWidth: window.innerWidth
9
- }
10
- });
5
+ if (page.onResize) {
6
+ const mediaQuery = window.matchMedia('(orientation: portrait)');
7
+ page.onResize({
8
+ deviceOrientation: mediaQuery.matches ? 'portrait' : 'landscape',
9
+ size: {
10
+ windowHeight: window.innerHeight,
11
+ windowWidth: window.innerWidth,
12
+ screenHeight: window.screen.height,
13
+ screenWidth: window.screen.width,
14
+ }
15
+ });
16
+ }
11
17
  };
12
18
  window.addEventListener('resize', pageResizeFn, false);
13
19
  }
20
+
21
+ export { bindPageResize };
@@ -1,2 +1,2 @@
1
- import { PageInstance } from '@tarojs/runtime';
1
+ import type { PageInstance } from '@tarojs/runtime';
2
2
  export declare function bindPageScroll(page: PageInstance, scrollEl: HTMLElement | Window, distance?: number): void;
@@ -1,7 +1,8 @@
1
1
  import { Current } from '@tarojs/runtime';
2
+
2
3
  const pageScrollFn = {};
3
4
  let pageDOM = window;
4
- export function bindPageScroll(page, scrollEl, distance = 50) {
5
+ function bindPageScroll(page, scrollEl, distance = 50) {
5
6
  var _a;
6
7
  const pagePath = (page ? page === null || page === void 0 ? void 0 : page.path : (_a = Current.router) === null || _a === void 0 ? void 0 : _a.path);
7
8
  pageScrollFn[pagePath] && scrollEl.removeEventListener('scroll', pageScrollFn[pagePath]);
@@ -32,3 +33,5 @@ function getOffset() {
32
33
  return pageDOM.scrollHeight - pageDOM.scrollTop - pageDOM.clientHeight;
33
34
  }
34
35
  }
36
+
37
+ export { bindPageScroll };
package/dist/history.d.ts CHANGED
@@ -1,5 +1,26 @@
1
+ import { Action, createBrowserHistory, createHashHistory } from 'history';
1
2
  import type { IH5RouterConfig } from '@tarojs/taro/types/compile';
2
- import type { History } from 'history';
3
+ import type { Blocker, BrowserHistoryOptions, HashHistoryOptions, History, Listener, Location, Path, To } from 'history';
4
+ import type { StateEvent } from '../types/history';
3
5
  export declare let history: History;
6
+ declare class MpaHistory implements History {
7
+ action: Action;
8
+ get location(): Location;
9
+ createHref(_to: To): string;
10
+ parseUrl(to: Partial<Path>): string;
11
+ push(to: Partial<Path>, _state?: Record<string, unknown>): void;
12
+ replace(to: Partial<Path>, _state?: Record<string, unknown>): void;
13
+ go(delta: number): void;
14
+ back: () => void;
15
+ forward: () => void;
16
+ listen(listener: Listener): () => void;
17
+ block(_blocker: Blocker): () => void;
18
+ pushState: globalThis.History['pushState'];
19
+ replaceState: globalThis.History['replaceState'];
20
+ eventState(action: Required<StateEvent>['action']): (data: any, unused: string, url?: string | URL | null) => any;
21
+ }
22
+ export declare function setHistory(h: History, base?: string): void;
23
+ export declare function createMpaHistory(_?: HashHistoryOptions | BrowserHistoryOptions): MpaHistory;
24
+ export { createBrowserHistory, createHashHistory };
4
25
  export declare function setHistoryMode(mode?: IH5RouterConfig['mode'], base?: string): void;
5
26
  export declare function prependBasename(url?: string): string;
package/dist/history.js CHANGED
@@ -1,6 +1,9 @@
1
- import { Action, createBrowserHistory, createHashHistory } from 'history';
2
- import { RouterConfig } from './router';
3
- export let history;
1
+ import { addLeadingSlash } from '@tarojs/runtime';
2
+ import { createBrowserHistory, createHashHistory, Action } from 'history';
3
+ export { createBrowserHistory, createHashHistory } from 'history';
4
+ import { RouterConfig } from './router/index.js';
5
+
6
+ let history;
4
7
  let basename = '/';
5
8
  class MpaHistory {
6
9
  constructor() {
@@ -23,14 +26,14 @@ class MpaHistory {
23
26
  }
24
27
  parseUrl(to) {
25
28
  let url = to.pathname || '';
26
- if (RouterConfig.isPage(url)) {
29
+ if (RouterConfig.isPage(addLeadingSlash(url))) {
27
30
  url += '.html';
28
31
  }
29
32
  if (to.search) {
30
33
  url += `?${to.search}`;
31
34
  }
32
35
  if (to.hash) {
33
- url += `#${to.hash}`;
36
+ url += to.hash.startsWith('#') ? to.hash : `#${to.hash}`;
34
37
  }
35
38
  return url;
36
39
  }
@@ -79,7 +82,14 @@ class MpaHistory {
79
82
  };
80
83
  }
81
84
  }
82
- export function setHistoryMode(mode, base = '/') {
85
+ function setHistory(h, base = '/') {
86
+ history = h;
87
+ basename = base;
88
+ }
89
+ function createMpaHistory(_) {
90
+ return new MpaHistory();
91
+ }
92
+ function setHistoryMode(mode, base = '/') {
83
93
  const options = {
84
94
  window
85
95
  };
@@ -88,13 +98,15 @@ export function setHistoryMode(mode, base = '/') {
88
98
  history = createBrowserHistory(options);
89
99
  }
90
100
  else if (mode === 'multi') {
91
- history = new MpaHistory();
101
+ history = createMpaHistory();
92
102
  }
93
103
  else {
94
104
  // default is hash
95
105
  history = createHashHistory(options);
96
106
  }
97
107
  }
98
- export function prependBasename(url = '') {
108
+ function prependBasename(url = '') {
99
109
  return basename.replace(/\/$/, '') + '/' + url.replace(/^\//, '');
100
110
  }
111
+
112
+ export { createMpaHistory, history, prependBasename, setHistory, setHistoryMode };