@tarojs/router 3.7.0-canary.6 → 3.8.0-canary.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.
Files changed (46) hide show
  1. package/LICENSE +14 -0
  2. package/dist/api.d.ts +7 -6
  3. package/dist/api.js +31 -22
  4. package/dist/events/resize.d.ts +2 -1
  5. package/dist/events/resize.js +15 -7
  6. package/dist/events/scroll.d.ts +2 -1
  7. package/dist/events/scroll.js +4 -1
  8. package/dist/history.d.ts +24 -5
  9. package/dist/history.js +20 -8
  10. package/dist/index.cjs.d.ts +50 -6
  11. package/dist/index.cjs.js +792 -342
  12. package/dist/index.d.ts +10 -4
  13. package/dist/index.esm.d.ts +50 -6
  14. package/dist/index.esm.js +751 -313
  15. package/dist/index.js +52 -4
  16. package/dist/navigationBar.d.ts +3 -0
  17. package/dist/navigationBar.js +44 -0
  18. package/dist/router/index.d.ts +5 -3
  19. package/dist/router/index.js +7 -3
  20. package/dist/router/mpa.d.ts +5 -3
  21. package/dist/router/mpa.js +27 -17
  22. package/dist/router/multi-page.d.ts +10 -7
  23. package/dist/router/multi-page.js +27 -57
  24. package/dist/router/navigation-bar.d.ts +36 -0
  25. package/dist/router/navigation-bar.js +252 -0
  26. package/dist/router/page.d.ts +16 -8
  27. package/dist/router/page.js +63 -59
  28. package/dist/router/spa.d.ts +5 -3
  29. package/dist/router/spa.js +77 -39
  30. package/dist/router/stack.d.ts +1 -1
  31. package/dist/router/stack.js +2 -1
  32. package/dist/style.d.ts +8 -2
  33. package/dist/style.js +109 -6
  34. package/dist/tabbar.d.ts +3 -1
  35. package/dist/tabbar.js +5 -4
  36. package/dist/utils/index.d.ts +3 -9
  37. package/dist/utils/index.js +5 -20
  38. package/dist/utils/navigate.d.ts +10 -5
  39. package/dist/utils/navigate.js +24 -37
  40. package/package.json +26 -14
  41. package/types/api.d.ts +5 -0
  42. package/types/component.d.ts +5 -0
  43. package/types/router.d.ts +2 -0
  44. package/types/taro.d.ts +8 -0
  45. package/dist/index.cjs.js.map +0 -1
  46. package/dist/index.esm.js.map +0 -1
package/LICENSE CHANGED
@@ -158,3 +158,17 @@ MIT (stencil-vue2-output-target):
158
158
  The following files embed [stencil-vue2-output-target](https://github.com/diondree/stencil-vue2-output-target) MIT:
159
159
  `/packages/taro-components-library-vue2/src/vue-component-lib/utils.ts`
160
160
  See `/LICENSE` for details of the license.
161
+
162
+ ==================
163
+
164
+ MIT (weui):
165
+ The following files embed [stencil-vue2-output-target](https://github.com/Tencent/weui) MIT:
166
+ `/packages/taro-components/src/components/*.scss`
167
+ See `/LICENSE.txt` for details of the license.
168
+
169
+ ==================
170
+
171
+ Apache-2.0 (intersection-observer):
172
+ The following files embed [intersection-observer](https://github.com/GoogleChromeLabs/intersection-observer) Apache-2.0:
173
+ `/packages/taro-api/src/polyfill/intersection-observer.ts`
174
+ See `/LICENSE.txt` for details of the license.
package/dist/api.d.ts CHANGED
@@ -1,7 +1,8 @@
1
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[];
2
+ declare function navigateTo(option: Taro.navigateTo.Option): ReturnType<typeof Taro.navigateTo>;
3
+ declare function redirectTo(option: Taro.redirectTo.Option): ReturnType<typeof Taro.redirectTo>;
4
+ declare function navigateBack(option?: Taro.navigateBack.Option): ReturnType<typeof Taro.navigateBack>;
5
+ declare function switchTab(option: Taro.switchTab.Option): ReturnType<typeof Taro.switchTab>;
6
+ declare function reLaunch(option: Taro.reLaunch.Option): ReturnType<typeof Taro.reLaunch>;
7
+ declare function getCurrentPages(): Taro.Page[];
8
+ export { navigateTo, redirectTo, navigateBack, switchTab, reLaunch, getCurrentPages };
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,3 @@
1
1
  import { PageInstance } from '@tarojs/runtime';
2
- export declare function bindPageResize(page: PageInstance): void;
2
+ declare function bindPageResize(page: PageInstance): void;
3
+ export { bindPageResize };
@@ -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,3 @@
1
1
  import { PageInstance } from '@tarojs/runtime';
2
- export declare function bindPageScroll(page: PageInstance, scrollEl: HTMLElement | Window, distance?: number): void;
2
+ declare function bindPageScroll(page: PageInstance, scrollEl: HTMLElement | Window, distance?: number): void;
3
+ export { bindPageScroll };
@@ -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,24 @@
1
- import type { IH5RouterConfig } from '@tarojs/taro/types/compile';
2
- import type { 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;
1
+ import { IH5RouterConfig } from '@tarojs/taro/types/compile';
2
+ import { StateEvent } from '../types/history';
3
+ declare let history: History;
4
+ declare class MpaHistory implements History {
5
+ action: Action;
6
+ get location(): Location;
7
+ createHref(_to: To): string;
8
+ parseUrl(to: Partial<Path>): string;
9
+ push(to: Partial<Path>, _state?: Record<string, unknown>): void;
10
+ replace(to: Partial<Path>, _state?: Record<string, unknown>): void;
11
+ go(delta: number): void;
12
+ back: () => void;
13
+ forward: () => void;
14
+ listen(listener: Listener): () => void;
15
+ block(_blocker: Blocker): () => void;
16
+ pushState: globalThis.History['pushState'];
17
+ replaceState: globalThis.History['replaceState'];
18
+ eventState(action: Required<StateEvent>['action']): (data: any, unused: string, url?: string | URL | null) => any;
19
+ }
20
+ declare function setHistory(h: History, base?: string): void;
21
+ declare function createMpaHistory(_?: HashHistoryOptions | BrowserHistoryOptions): MpaHistory;
22
+ declare function setHistoryMode(mode?: IH5RouterConfig['mode'], base?: string): void;
23
+ declare function prependBasename(url?: string): string;
24
+ export { history, setHistory, createMpaHistory, createBrowserHistory, createHashHistory, setHistoryMode, prependBasename };
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 };
@@ -1,14 +1,38 @@
1
+ import { Action, createBrowserHistory, createHashHistory, Blocker, BrowserHistoryOptions, HashHistoryOptions, History, Listener, Location, Path, To } from "history";
2
+ import { IH5RouterConfig } from "@tarojs/taro/types/compile";
3
+ import { StateEvent } from "../types/history";
4
+ import { MpaRouterConfig, SpaRouterConfig } from '../types/router';
1
5
  import Taro from "@tarojs/taro";
2
- import { History } from "history";
3
6
  import { AppInstance } from "@tarojs/runtime";
4
- import { MpaRouterConfig, SpaRouterConfig } from "../../types/router";
7
+ import { MpaRouterConfig as MpaRouterConfig$0 } from "../../types/router";
8
+ import { SpaRouterConfig as SpaRouterConfig$0 } from "../../types/router";
9
+ declare let history: History;
10
+ declare class MpaHistory implements History {
11
+ action: Action;
12
+ get location(): Location;
13
+ createHref(_to: To): string;
14
+ parseUrl(to: Partial<Path>): string;
15
+ push(to: Partial<Path>, _state?: Record<string, unknown>): void;
16
+ replace(to: Partial<Path>, _state?: Record<string, unknown>): void;
17
+ go(delta: number): void;
18
+ back: () => void;
19
+ forward: () => void;
20
+ listen(listener: Listener): () => void;
21
+ block(_blocker: Blocker): () => void;
22
+ pushState: globalThis.History["pushState"];
23
+ replaceState: globalThis.History["replaceState"];
24
+ eventState(action: Required<StateEvent>["action"]): (data: any, unused: string, url?: string | URL | null) => any;
25
+ }
26
+ declare function setHistory(h: History, base?: string): void;
27
+ declare function createMpaHistory(_?: HashHistoryOptions | BrowserHistoryOptions): MpaHistory;
28
+ declare function setHistoryMode(mode?: IH5RouterConfig["mode"], base?: string): void;
29
+ declare function prependBasename(url?: string): string;
5
30
  declare function navigateTo(option: Taro.navigateTo.Option): ReturnType<typeof Taro.navigateTo>;
6
31
  declare function redirectTo(option: Taro.redirectTo.Option): ReturnType<typeof Taro.redirectTo>;
7
32
  declare function navigateBack(option?: Taro.navigateBack.Option): ReturnType<typeof Taro.navigateBack>;
8
33
  declare function switchTab(option: Taro.switchTab.Option): ReturnType<typeof Taro.switchTab>;
9
34
  declare function reLaunch(option: Taro.reLaunch.Option): ReturnType<typeof Taro.reLaunch>;
10
35
  declare function getCurrentPages(): Taro.Page[];
11
- declare let history: History;
12
36
  // TODO 支持多路由 (APP 生命周期仅触发一次)
13
37
  /** Note: 关于多页面应用
14
38
  * - 需要配置路由映射(根目录跳转、404 页面……)
@@ -17,6 +41,26 @@ declare let history: History;
17
41
  * - TabBar 会多次加载
18
42
  * - 不支持路由动画
19
43
  */
20
- declare function createMultiRouter(app: AppInstance, config: MpaRouterConfig, framework?: string): Promise<void>;
21
- declare function createRouter(app: AppInstance, config: SpaRouterConfig, framework?: string): () => void;
22
- export { navigateTo, redirectTo, navigateBack, switchTab, reLaunch, getCurrentPages, history, createMultiRouter, createRouter };
44
+ declare function createMultiRouter(history: History, app: AppInstance, config: MpaRouterConfig$0, framework?: string): Promise<void>;
45
+ declare function createRouter(history: History, app: AppInstance, config: SpaRouterConfig$0, framework?: string): () => void;
46
+ declare class RoutesAlias {
47
+ conf: Array<string[]>;
48
+ set(customRoutes?: Record<string, string | string[]>): void;
49
+ getConfig: (url?: string) => string[];
50
+ getOrigin: (url?: string) => string;
51
+ getAlias: (url?: string) => string;
52
+ getAll: (url?: string) => string[];
53
+ }
54
+ declare const routesAlias: RoutesAlias;
55
+ declare const isWeixin: () => boolean;
56
+ declare const isDingTalk: () => boolean;
57
+ declare function setMpaTitle(title: string): void;
58
+ declare function setTitle(title: string): void;
59
+ declare function setNavigationBarStyle(option: {
60
+ backgroundColor: string;
61
+ frontColor: string;
62
+ }): void;
63
+ declare function setNavigationBarLoading(loading: boolean): void;
64
+ declare function handleAppMount(config: SpaRouterConfig | MpaRouterConfig, _: History, appId?: string): void;
65
+ declare function handleAppMountWithTabbar(config: SpaRouterConfig | MpaRouterConfig, history: History, appId?: string): void;
66
+ export { navigateTo, redirectTo, navigateBack, switchTab, reLaunch, getCurrentPages, history, setHistory, createMpaHistory, createBrowserHistory, createHashHistory, setHistoryMode, prependBasename, createMultiRouter, createRouter, routesAlias, isWeixin, isDingTalk, setMpaTitle, setTitle, setNavigationBarStyle, setNavigationBarLoading, handleAppMount, handleAppMountWithTabbar };