@tarojs/router 3.7.0-alpha.1 → 3.7.0-alpha.3

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.
@@ -28,6 +28,7 @@ export default class PageHandler {
28
28
  isTabBar(pathname: string): boolean;
29
29
  isSamePage(page?: PageInstance | null): boolean;
30
30
  get search(): string;
31
+ get usingWindowScroll(): boolean;
31
32
  getQuery(stamp?: string, search?: string, options?: Record<string, unknown>): {
32
33
  [x: string]: unknown;
33
34
  };
@@ -39,5 +40,7 @@ export default class PageHandler {
39
40
  hide(page?: PageInstance | null): void;
40
41
  addAnimation(pageEl?: HTMLElement | null, first?: boolean): void;
41
42
  getPageContainer(page?: PageInstance | null): HTMLElement | null;
42
- bindPageEvents(page: PageInstance, pageEl?: HTMLElement | null, config?: Partial<PageConfig>): void;
43
+ getScrollingElement(page?: PageInstance | null): (Window & typeof globalThis) | HTMLElement;
44
+ bindPageEvents(page: PageInstance, config?: Partial<PageConfig>): void;
45
+ triggerRouterChange(): void;
43
46
  }
@@ -1,10 +1,10 @@
1
1
  /* eslint-disable dot-notation */
2
- import { Current, requestAnimationFrame } from '@tarojs/runtime';
2
+ import { Current, eventCenter, requestAnimationFrame } from '@tarojs/runtime';
3
3
  import queryString from 'query-string';
4
- import { loadAnimateStyle } from '../animation';
5
4
  import { bindPageResize } from '../events/resize';
6
5
  import { bindPageScroll } from '../events/scroll';
7
- import { setHistoryMode } from '../history';
6
+ import { history, setHistoryMode } from '../history';
7
+ import { loadAnimateStyle, loadRouterStyle } from '../style';
8
8
  import { initTabbar } from '../tabbar';
9
9
  import { addLeadingSlash, getCurrentPage, getHomePage, routesAlias, stripBasename, stripTrailing } from '../utils';
10
10
  import stacks from './stack';
@@ -92,6 +92,17 @@ export default class PageHandler {
92
92
  }
93
93
  return search.substring(1);
94
94
  }
95
+ get usingWindowScroll() {
96
+ var _a;
97
+ let usingWindowScroll = false;
98
+ if (typeof ((_a = this.pageConfig) === null || _a === void 0 ? void 0 : _a.usingWindowScroll) === 'boolean') {
99
+ usingWindowScroll = this.pageConfig.usingWindowScroll;
100
+ }
101
+ const win = window;
102
+ win.__taroAppConfig || (win.__taroAppConfig = {});
103
+ win.__taroAppConfig.usingWindowScroll = usingWindowScroll;
104
+ return usingWindowScroll;
105
+ }
95
106
  getQuery(stamp = '', search = '', options = {}) {
96
107
  search = search ? `${search}&${this.search}` : this.search;
97
108
  const query = search
@@ -102,7 +113,9 @@ export default class PageHandler {
102
113
  }
103
114
  mount() {
104
115
  setHistoryMode(this.routerMode, this.router.basename);
116
+ this.pathname = history.location.pathname;
105
117
  this.animation && loadAnimateStyle(this.animationDuration);
118
+ loadRouterStyle(this.usingWindowScroll);
106
119
  const appId = this.appId;
107
120
  let app = document.getElementById(appId);
108
121
  let isPosition = true;
@@ -169,7 +182,8 @@ export default class PageHandler {
169
182
  this.isTabBar(this.pathname) && pageEl.classList.add('taro_tabbar_page');
170
183
  this.addAnimation(pageEl, pageNo === 0);
171
184
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
172
- this.bindPageEvents(page, pageEl, pageConfig);
185
+ this.bindPageEvents(page, pageConfig);
186
+ this.triggerRouterChange();
173
187
  }
174
188
  else {
175
189
  (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
@@ -179,7 +193,8 @@ export default class PageHandler {
179
193
  this.addAnimation(pageEl, pageNo === 0);
180
194
  this.onReady(page, true);
181
195
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
182
- this.bindPageEvents(page, pageEl, pageConfig);
196
+ this.bindPageEvents(page, pageConfig);
197
+ this.triggerRouterChange();
183
198
  });
184
199
  }
185
200
  }
@@ -199,6 +214,9 @@ export default class PageHandler {
199
214
  const pageEl = this.getPageContainer(page);
200
215
  pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_stationed');
201
216
  pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.remove('taro_page_show');
217
+ if (pageEl) {
218
+ pageEl.style.zIndex = '1';
219
+ }
202
220
  this.unloadTimer = setTimeout(() => {
203
221
  var _a, _b;
204
222
  this.unloadTimer = null;
@@ -224,7 +242,8 @@ export default class PageHandler {
224
242
  setDisplay(pageEl);
225
243
  this.addAnimation(pageEl, pageNo === 0);
226
244
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
227
- this.bindPageEvents(page, pageEl, pageConfig);
245
+ this.bindPageEvents(page, pageConfig);
246
+ this.triggerRouterChange();
228
247
  }
229
248
  else {
230
249
  (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
@@ -233,7 +252,8 @@ export default class PageHandler {
233
252
  this.addAnimation(pageEl, pageNo === 0);
234
253
  this.onReady(page, false);
235
254
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
236
- this.bindPageEvents(page, pageEl, pageConfig);
255
+ this.bindPageEvents(page, pageConfig);
256
+ this.triggerRouterChange();
237
257
  });
238
258
  }
239
259
  }
@@ -287,15 +307,31 @@ export default class PageHandler {
287
307
  ? document.querySelector(`.taro_page#${id}`)
288
308
  : document.querySelector('.taro_page') ||
289
309
  document.querySelector('.taro_router'));
290
- return el || window;
310
+ return el;
291
311
  }
292
- bindPageEvents(page, pageEl, config = {}) {
312
+ getScrollingElement(page) {
313
+ if (this.usingWindowScroll)
314
+ return window;
315
+ return this.getPageContainer(page) || window;
316
+ }
317
+ bindPageEvents(page, config = {}) {
293
318
  var _a;
294
- if (!pageEl) {
295
- pageEl = this.getPageContainer();
296
- }
319
+ const scrollEl = this.getScrollingElement(page);
297
320
  const distance = config.onReachBottomDistance || ((_a = this.config.window) === null || _a === void 0 ? void 0 : _a.onReachBottomDistance) || 50;
298
- bindPageScroll(page, pageEl, distance);
321
+ bindPageScroll(page, scrollEl, distance);
299
322
  bindPageResize(page);
300
323
  }
324
+ triggerRouterChange() {
325
+ /**
326
+ * @tarojs/runtime 中生命周期跑在 promise 中,所以这里需要 setTimeout 延迟事件调用
327
+ * TODO 考虑将生命周期返回 Promise,用于处理相关事件调用顺序
328
+ */
329
+ setTimeout(() => {
330
+ eventCenter.trigger('__afterTaroRouterChange', {
331
+ toLocation: {
332
+ path: this.pathname
333
+ }
334
+ });
335
+ }, 0);
336
+ }
301
337
  }
@@ -1,3 +1,3 @@
1
- import { AppInstance } from '@tarojs/runtime';
1
+ import type { AppInstance } from '@tarojs/runtime';
2
2
  import type { SpaRouterConfig } from '../../types/router';
3
3
  export declare function createRouter(app: AppInstance, config: SpaRouterConfig, framework?: string): () => void;
@@ -21,6 +21,9 @@ const createStampId = incrementId();
21
21
  let launchStampId = createStampId();
22
22
  export function createRouter(app, config, framework) {
23
23
  var _a, _b;
24
+ if (typeof app.onUnhandledRejection === 'function') {
25
+ window.addEventListener('unhandledrejection', app.onUnhandledRejection);
26
+ }
24
27
  RouterConfig.config = config;
25
28
  const handler = new PageHandler(config);
26
29
  routesAlias.set(handler.router.customRoutes);
@@ -45,8 +48,10 @@ export function createRouter(app, config, framework) {
45
48
  (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
46
49
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
47
50
  const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
48
- var _c, _d, _e, _f, _g;
51
+ var _c, _d, _e, _f, _g, _h;
49
52
  handler.pathname = decodeURI(location.pathname);
53
+ if ((_c = window.__taroAppConfig) === null || _c === void 0 ? void 0 : _c.usingWindowScroll)
54
+ window.scrollTo(0, 0);
50
55
  eventCenter.trigger('__taroRouterChange', {
51
56
  toLocation: {
52
57
  path: handler.pathname
@@ -59,9 +64,13 @@ export function createRouter(app, config, framework) {
59
64
  }
60
65
  catch (error) {
61
66
  if (error.status === 404) {
62
- (_c = app.onPageNotFound) === null || _c === void 0 ? void 0 : _c.call(app, {
63
- path: handler.pathname
64
- });
67
+ const notFoundEvent = {
68
+ isEntryPage: stacks.length === 0,
69
+ path: handler.pathname,
70
+ query: handler.getQuery(createStampId()),
71
+ };
72
+ (_d = app.onPageNotFound) === null || _d === void 0 ? void 0 : _d.call(app, notFoundEvent);
73
+ eventCenter.trigger('__taroRouterNotFound', notFoundEvent);
65
74
  }
66
75
  else if (/Loading hot update .* failed./.test(error.message)) {
67
76
  // NOTE: webpack5 与 prebundle 搭配使用时,开发环境下初次启动时偶发错误,由于 HMR 加载 chunk hash 错误,导致热更新失败
@@ -74,16 +83,16 @@ export function createRouter(app, config, framework) {
74
83
  if (!element)
75
84
  return;
76
85
  const pageConfig = handler.pageConfig;
77
- let enablePullDownRefresh = ((_d = config === null || config === void 0 ? void 0 : config.window) === null || _d === void 0 ? void 0 : _d.enablePullDownRefresh) || false;
86
+ let enablePullDownRefresh = ((_e = config === null || config === void 0 ? void 0 : config.window) === null || _e === void 0 ? void 0 : _e.enablePullDownRefresh) || false;
78
87
  if (pageConfig) {
79
- setTitle((_e = pageConfig.navigationBarTitleText) !== null && _e !== void 0 ? _e : document.title);
88
+ setTitle((_f = pageConfig.navigationBarTitleText) !== null && _f !== void 0 ? _f : document.title);
80
89
  if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
81
90
  enablePullDownRefresh = pageConfig.enablePullDownRefresh;
82
91
  }
83
92
  }
84
93
  const currentPage = Current.page;
85
94
  const pathname = handler.pathname;
86
- const methodName = (_f = stacks.method) !== null && _f !== void 0 ? _f : '';
95
+ const methodName = (_g = stacks.method) !== null && _g !== void 0 ? _g : '';
87
96
  const cacheTabs = stacks.getTabs();
88
97
  let shouldLoad = false;
89
98
  stacks.method = '';
@@ -108,11 +117,11 @@ export function createRouter(app, config, framework) {
108
117
  else if (stacks.length > 0) {
109
118
  const firstIns = stacks.getItem(0);
110
119
  if (handler.isTabBar(firstIns.path)) {
111
- handler.unload(currentPage, stacks.length - 1);
120
+ handler.unload(currentPage, stacks.length - 1, true);
112
121
  stacks.pushTab(firstIns.path.split('?')[0]);
113
122
  }
114
123
  else {
115
- handler.unload(currentPage, stacks.length);
124
+ handler.unload(currentPage, stacks.length, true);
116
125
  }
117
126
  }
118
127
  if (cacheTabs[handler.pathname]) {
@@ -147,7 +156,7 @@ export function createRouter(app, config, framework) {
147
156
  shouldLoad = true;
148
157
  }
149
158
  if (shouldLoad || stacks.length < 1) {
150
- const el = (_g = element.default) !== null && _g !== void 0 ? _g : element;
159
+ const el = (_h = element.default) !== null && _h !== void 0 ? _h : element;
151
160
  const loadConfig = Object.assign({}, pageConfig);
152
161
  const stacksIndex = stacks.length;
153
162
  delete loadConfig['path'];
@@ -160,16 +169,11 @@ export function createRouter(app, config, framework) {
160
169
  else {
161
170
  pageStampId = createStampId();
162
171
  }
163
- const page = createPageConfig(enablePullDownRefresh ? hooks.call('createPullDownComponent', el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + stringify(handler.getQuery(pageStampId)), {}, loadConfig);
172
+ const page = createPageConfig(enablePullDownRefresh ? hooks.call('createPullDownComponent', el, pathname, framework, handler.PullDownRefresh, pageStampId) : el, pathname + stringify(handler.getQuery(pageStampId)), {}, loadConfig);
164
173
  if (params)
165
174
  page.options = params;
166
175
  handler.load(page, pageConfig, pageStampId, stacksIndex);
167
176
  }
168
- eventCenter.trigger('__afterTaroRouterChange', {
169
- toLocation: {
170
- path: handler.pathname
171
- }
172
- });
173
177
  });
174
178
  const routePath = addLeadingSlash(stripBasename(history.location.pathname, handler.basename));
175
179
  if (routePath === '/') {
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 插入页面动画需要的样式
3
+ */
4
+ export declare function loadAnimateStyle(ms?: number): void;
5
+ /**
6
+ * 插入路由相关样式
7
+ */
8
+ export declare function loadRouterStyle(usingWindowScroll: any): void;
package/dist/style.js ADDED
@@ -0,0 +1,71 @@
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
+ addStyle(css);
27
+ }
28
+ /**
29
+ * 插入路由相关样式
30
+ */
31
+ export function loadRouterStyle(usingWindowScroll) {
32
+ const css = `
33
+ .taro_router {
34
+ overflow: hidden;
35
+ position: relative;
36
+ width: 100%;
37
+ height: 100%;
38
+ min-height: 100vh;
39
+ }
40
+
41
+ .taro-tabbar__container .taro_router {
42
+ min-height: calc(100vh - 50px);
43
+ }
44
+
45
+ .taro_page {
46
+ width: 100%;
47
+ height: 100%;
48
+ ${usingWindowScroll ? '' : `
49
+ overflow-x: hidden;
50
+ overflow-y: scroll;
51
+ max-height: 100vh;
52
+ `}
53
+ }
54
+
55
+ .taro-tabbar__container .taro-tabbar__panel {
56
+ overflow: hidden;
57
+ }
58
+
59
+ .taro-tabbar__container .taro_page.taro_tabbar_page {
60
+ max-height: calc(100vh - 50px);
61
+ }
62
+ `;
63
+ addStyle(css);
64
+ }
65
+ function addStyle(css) {
66
+ if (!css)
67
+ return;
68
+ const style = document.createElement('style');
69
+ style.innerHTML = css;
70
+ document.getElementsByTagName('head')[0].appendChild(style);
71
+ }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@tarojs/router",
3
- "version": "3.7.0-alpha.1",
3
+ "version": "3.7.0-alpha.3",
4
4
  "description": "Taro-router",
5
- "browser": "dist/index.esm.js",
6
- "main:h5": "dist/index.js",
5
+ "browser": "dist/index.js",
6
+ "main:h5": "dist/index.esm.js",
7
7
  "main": "dist/index.js",
8
- "module": "dist/index.cjs.js",
8
+ "module": "dist/index.esm.js",
9
9
  "typings": "dist/index.d.ts",
10
10
  "files": [
11
11
  "dist",
@@ -27,8 +27,8 @@
27
27
  "mobile-detect": "^1.4.2",
28
28
  "query-string": "^7.1.1",
29
29
  "universal-router": "^8.3.0",
30
- "@tarojs/runtime": "3.7.0-alpha.1",
31
- "@tarojs/taro": "3.7.0-alpha.1"
30
+ "@tarojs/runtime": "3.7.0-alpha.3",
31
+ "@tarojs/taro": "3.7.0-alpha.3"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@rollup/plugin-commonjs": "^20.0.0",
@@ -47,7 +47,8 @@
47
47
  "scripts": {
48
48
  "build": "rimraf ./dist && tsc && rollup -c",
49
49
  "dev": "tsc -w",
50
- "test": "jest",
51
- "test:dev": "jest --watch"
50
+ "test": "cross-env NODE_ENV=jest jest",
51
+ "test:ci": "cross-env NODE_ENV=jest jest --ci",
52
+ "test:dev": "cross-env NODE_ENV=jest jest --watch"
52
53
  }
53
54
  }
@@ -1,4 +0,0 @@
1
- /**
2
- * 插入页面动画需要的样式
3
- */
4
- export declare function loadAnimateStyle(ms?: number): void;
package/dist/animation.js DELETED
@@ -1,29 +0,0 @@
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
- }