@thepassle/app-tools 0.9.6 → 0.9.8
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.
- package/package.json +1 -1
- package/router/README.md +13 -2
- package/router/index.js +17 -10
- package/router/plugins/redirect.js +4 -4
package/package.json
CHANGED
package/router/README.md
CHANGED
|
@@ -213,14 +213,18 @@ App:
|
|
|
213
213
|
import { LitElement } from 'lit';
|
|
214
214
|
|
|
215
215
|
class MyEl extends LitElement {
|
|
216
|
+
static properties = {
|
|
217
|
+
route: {}
|
|
218
|
+
}
|
|
219
|
+
|
|
216
220
|
firstUpdated() {
|
|
217
221
|
router.addEventListener('route-changed', () => {
|
|
218
|
-
this.
|
|
222
|
+
this.route = router.render();
|
|
219
223
|
});
|
|
220
224
|
}
|
|
221
225
|
|
|
222
226
|
render() {
|
|
223
|
-
return
|
|
227
|
+
return this.route;
|
|
224
228
|
}
|
|
225
229
|
}
|
|
226
230
|
```
|
|
@@ -301,6 +305,13 @@ const router = new Router({
|
|
|
301
305
|
redirect('/404'),
|
|
302
306
|
],
|
|
303
307
|
},
|
|
308
|
+
{
|
|
309
|
+
path: '/legacy/detail/:product',
|
|
310
|
+
title: 'Foo',
|
|
311
|
+
plugins: [
|
|
312
|
+
redirect(context => '/detail/${context.params.product}'),
|
|
313
|
+
],
|
|
314
|
+
},
|
|
304
315
|
]
|
|
305
316
|
});
|
|
306
317
|
```
|
package/router/index.js
CHANGED
|
@@ -81,7 +81,7 @@ export class Router extends EventTarget {
|
|
|
81
81
|
* @template RenderResult
|
|
82
82
|
*/
|
|
83
83
|
render() {
|
|
84
|
-
log(`Rendering route ${this.context.url.pathname}${this.context.url.search}`, { context: this.context, route: this.route });
|
|
84
|
+
log(`Rendering route ${this.context.url.pathname}${this.context.url.search}${this.context.url.hash}`, { context: this.context, route: this.route });
|
|
85
85
|
return /** @type {RenderResult} */ (this.route?.render?.(this.context));
|
|
86
86
|
}
|
|
87
87
|
|
|
@@ -106,7 +106,7 @@ export class Router extends EventTarget {
|
|
|
106
106
|
return route;
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
|
-
log(`No route matched for ${url.pathname}${url.search}`, url);
|
|
109
|
+
log(`No route matched for ${url.pathname}${url.search}${url.hash}`, url);
|
|
110
110
|
return null;
|
|
111
111
|
}
|
|
112
112
|
|
|
@@ -154,6 +154,13 @@ export class Router extends EventTarget {
|
|
|
154
154
|
this.navigate(url);
|
|
155
155
|
}
|
|
156
156
|
|
|
157
|
+
_collectPlugins(route) {
|
|
158
|
+
return [
|
|
159
|
+
...(this.config?.plugins ?? []),
|
|
160
|
+
...(route?.plugins ?? []),
|
|
161
|
+
]
|
|
162
|
+
}
|
|
163
|
+
|
|
157
164
|
/**
|
|
158
165
|
* @param {string | URL} url The URL to navigate to.
|
|
159
166
|
* @param {{
|
|
@@ -165,14 +172,11 @@ export class Router extends EventTarget {
|
|
|
165
172
|
url = new URL(url, this.baseUrl);
|
|
166
173
|
}
|
|
167
174
|
|
|
168
|
-
|
|
169
|
-
log(`Navigating to ${url.pathname}${url.search}`, { context: this.context, route: this.route });
|
|
175
|
+
let route = this._matchRoute(url) || this._matchRoute(this.fallback);
|
|
176
|
+
log(`Navigating to ${url.pathname}${url.search}${url.hash}`, { context: this.context, route: this.route });
|
|
170
177
|
|
|
171
178
|
/** @type {Plugin[]} */
|
|
172
|
-
|
|
173
|
-
...(this.config?.plugins ?? []),
|
|
174
|
-
...(this.route?.plugins ?? []),
|
|
175
|
-
];
|
|
179
|
+
let plugins = this._collectPlugins(route);
|
|
176
180
|
|
|
177
181
|
for (const plugin of plugins) {
|
|
178
182
|
try {
|
|
@@ -181,7 +185,8 @@ export class Router extends EventTarget {
|
|
|
181
185
|
const condition = await result.condition();
|
|
182
186
|
if (!condition) {
|
|
183
187
|
url = new URL(result.redirect, this.baseUrl);
|
|
184
|
-
|
|
188
|
+
route = this._matchRoute(url) || this._matchRoute(this.fallback);
|
|
189
|
+
plugins = this._collectPlugins(route);
|
|
185
190
|
log('Redirecting', { context: this.context, route: this.route });
|
|
186
191
|
}
|
|
187
192
|
}
|
|
@@ -191,6 +196,8 @@ export class Router extends EventTarget {
|
|
|
191
196
|
}
|
|
192
197
|
}
|
|
193
198
|
|
|
199
|
+
this.route = route;
|
|
200
|
+
|
|
194
201
|
if (!this.route) {
|
|
195
202
|
throw new Error(`[ROUTER] No route or fallback matched for url ${url}`);
|
|
196
203
|
}
|
|
@@ -205,7 +212,7 @@ export class Router extends EventTarget {
|
|
|
205
212
|
}
|
|
206
213
|
|
|
207
214
|
if (!options.backNav) {
|
|
208
|
-
window.history.pushState(null, '', `${url.pathname}${url.search}`);
|
|
215
|
+
window.history.pushState(null, '', `${url.pathname}${url.search}${url.hash}`);
|
|
209
216
|
}
|
|
210
217
|
document.title = this.context.title;
|
|
211
218
|
this._notifyUrlChanged();
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @param {string} path
|
|
2
|
+
* @param {string|((context:(import('../index.js').Context) => string)} path
|
|
3
3
|
* @returns {import('../index.js').Plugin}
|
|
4
4
|
*/
|
|
5
5
|
export function redirect(path) {
|
|
6
6
|
return {
|
|
7
7
|
name: 'redirect',
|
|
8
|
-
shouldNavigate: () => ({
|
|
8
|
+
shouldNavigate: (context) => ({
|
|
9
9
|
condition: () => false,
|
|
10
|
-
redirect: path
|
|
10
|
+
redirect: typeof path === 'function' ? path(context) : path
|
|
11
11
|
})
|
|
12
12
|
}
|
|
13
|
-
}
|
|
13
|
+
}
|