crelte 0.1.0 → 0.1.2
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/dist/Crelte.d.ts +89 -25
- package/dist/Crelte.d.ts.map +1 -1
- package/dist/Crelte.js +79 -38
- package/dist/CrelteRequest.d.ts +63 -0
- package/dist/CrelteRequest.d.ts.map +1 -0
- package/dist/CrelteRequest.js +94 -0
- package/dist/blocks/Blocks.d.ts +27 -3
- package/dist/blocks/Blocks.d.ts.map +1 -1
- package/dist/blocks/Blocks.js +8 -0
- package/dist/blocks/Blocks.svelte +1 -1
- package/dist/blocks/Blocks.svelte.d.ts +36 -4
- package/dist/blocks/Blocks.svelte.d.ts.map +1 -1
- package/dist/blocks/index.d.ts +6 -3
- package/dist/blocks/index.d.ts.map +1 -1
- package/dist/blocks/index.js +3 -2
- package/dist/cookies/ServerCookies.d.ts +1 -1
- package/dist/cookies/ServerCookies.d.ts.map +1 -1
- package/dist/cookies/index.d.ts +7 -0
- package/dist/cookies/index.d.ts.map +1 -1
- package/dist/graphql/GraphQl.d.ts +53 -14
- package/dist/graphql/GraphQl.d.ts.map +1 -1
- package/dist/graphql/GraphQl.js +44 -23
- package/dist/graphql/index.d.ts +1 -1
- package/dist/graphql/index.d.ts.map +1 -1
- package/dist/graphql/index.js +2 -2
- package/dist/index.d.ts +55 -22
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +50 -23
- package/dist/init/client.d.ts +43 -2
- package/dist/init/client.d.ts.map +1 -1
- package/dist/init/client.js +26 -6
- package/dist/init/server.d.ts +49 -2
- package/dist/init/server.d.ts.map +1 -1
- package/dist/init/server.js +56 -6
- package/dist/init/shared.d.ts +5 -4
- package/dist/init/shared.d.ts.map +1 -1
- package/dist/init/shared.js +3 -3
- package/dist/loadData/Globals.d.ts +41 -2
- package/dist/loadData/Globals.d.ts.map +1 -1
- package/dist/loadData/Globals.js +33 -5
- package/dist/loadData/index.d.ts +74 -11
- package/dist/loadData/index.d.ts.map +1 -1
- package/dist/loadData/index.js +5 -8
- package/dist/plugins/Events.d.ts +18 -5
- package/dist/plugins/Events.d.ts.map +1 -1
- package/dist/plugins/Events.js +3 -0
- package/dist/plugins/Plugins.d.ts +28 -2
- package/dist/plugins/Plugins.d.ts.map +1 -1
- package/dist/plugins/Plugins.js +8 -0
- package/dist/routing/History.d.ts +6 -3
- package/dist/routing/History.d.ts.map +1 -1
- package/dist/routing/History.js +7 -1
- package/dist/routing/InnerRouter.d.ts +10 -9
- package/dist/routing/InnerRouter.d.ts.map +1 -1
- package/dist/routing/InnerRouter.js +47 -35
- package/dist/routing/PageLoader.d.ts +5 -5
- package/dist/routing/PageLoader.d.ts.map +1 -1
- package/dist/routing/PageLoader.js +7 -7
- package/dist/routing/Request.d.ts +102 -0
- package/dist/routing/Request.d.ts.map +1 -0
- package/dist/routing/Request.js +128 -0
- package/dist/routing/Route.d.ts +73 -5
- package/dist/routing/Route.d.ts.map +1 -1
- package/dist/routing/Route.js +71 -6
- package/dist/routing/Router.d.ts +59 -52
- package/dist/routing/Router.d.ts.map +1 -1
- package/dist/routing/Router.js +95 -116
- package/dist/routing/Site.d.ts +1 -1
- package/dist/routing/Site.js +1 -1
- package/dist/routing/index.d.ts +4 -3
- package/dist/routing/index.d.ts.map +1 -1
- package/dist/routing/index.js +2 -1
- package/dist/ssr/SsrCache.d.ts +17 -0
- package/dist/ssr/SsrCache.d.ts.map +1 -1
- package/dist/ssr/SsrCache.js +17 -4
- package/dist/ssr/SsrComponents.d.ts +1 -0
- package/dist/ssr/SsrComponents.d.ts.map +1 -1
- package/dist/ssr/SsrComponents.js +1 -0
- package/package.json +6 -6
- package/src/Crelte.ts +124 -52
- package/src/CrelteRequest.ts +124 -0
- package/src/blocks/Blocks.svelte +35 -18
- package/src/blocks/Blocks.ts +38 -6
- package/src/blocks/index.ts +19 -10
- package/src/cookies/ServerCookies.ts +1 -1
- package/src/cookies/index.ts +7 -1
- package/src/graphql/GraphQl.ts +79 -27
- package/src/graphql/index.ts +7 -1
- package/src/index.ts +65 -29
- package/src/init/client.ts +55 -10
- package/src/init/server.ts +70 -10
- package/src/init/shared.ts +8 -7
- package/src/loadData/Globals.ts +43 -7
- package/src/loadData/index.ts +76 -13
- package/src/plugins/Events.ts +16 -5
- package/src/plugins/Plugins.ts +28 -2
- package/src/routing/History.ts +13 -4
- package/src/routing/InnerRouter.ts +55 -39
- package/src/routing/PageLoader.ts +10 -10
- package/src/routing/Request.ts +175 -0
- package/src/routing/Route.ts +73 -8
- package/src/routing/Router.ts +108 -162
- package/src/routing/Site.ts +1 -1
- package/src/routing/index.ts +12 -3
- package/src/ssr/SsrCache.ts +17 -4
- package/src/ssr/SsrComponents.ts +1 -0
- package/dist/CrelteBase.d.ts +0 -16
- package/dist/CrelteBase.d.ts.map +0 -1
- package/dist/CrelteBase.js +0 -1
- package/dist/CrelteRouted.d.ts +0 -50
- package/dist/CrelteRouted.d.ts.map +0 -1
- package/dist/CrelteRouted.js +0 -88
- package/src/CrelteBase.ts +0 -24
- package/src/CrelteRouted.ts +0 -128
|
@@ -1,11 +1,37 @@
|
|
|
1
|
-
import
|
|
1
|
+
import CrelteCore from '../Crelte.js';
|
|
2
|
+
/**
|
|
3
|
+
* A plugin
|
|
4
|
+
*/
|
|
2
5
|
export interface Plugin {
|
|
3
6
|
name: string;
|
|
4
7
|
}
|
|
5
|
-
|
|
8
|
+
/**
|
|
9
|
+
* A plugin create function
|
|
10
|
+
*
|
|
11
|
+
* Each plugin needs to export a createPluginname function which will be called
|
|
12
|
+
* by crelte to create the plugin. If youre plugin has options you can create
|
|
13
|
+
* a configurePluginname function which will be called by youre user and should
|
|
14
|
+
* return a createPlugin function.
|
|
15
|
+
*
|
|
16
|
+
* ## Example App.svelte in module="context"
|
|
17
|
+
* ```
|
|
18
|
+
* import { createPlugin, configurePlugin } from 'some-plugin';
|
|
19
|
+
*
|
|
20
|
+
* export plugins = [createPlugin, configurePlugin({ enableFeature: true })];
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export type PluginCreator = (crelte: CrelteCore) => Plugin;
|
|
24
|
+
/**
|
|
25
|
+
* A plugin manager
|
|
26
|
+
*/
|
|
6
27
|
export default class Plugins {
|
|
7
28
|
plugins: Map<string, Plugin>;
|
|
8
29
|
constructor();
|
|
30
|
+
/**
|
|
31
|
+
* @hidden
|
|
32
|
+
*
|
|
33
|
+
* Plugins should be added via App.svelte plugins export
|
|
34
|
+
*/
|
|
9
35
|
add(plugin: Plugin): void;
|
|
10
36
|
get(name: string): Plugin | null;
|
|
11
37
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Plugins.d.ts","sourceRoot":"","sources":["../../../../src/plugins/Plugins.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"Plugins.d.ts","sourceRoot":"","sources":["../../../../src/plugins/Plugins.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,cAAc,CAAC;AAEtC;;GAEG;AACH,MAAM,WAAW,MAAM;IACtB,IAAI,EAAE,MAAM,CAAC;CACb;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,UAAU,KAAK,MAAM,CAAC;AAE3D;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,OAAO;IAC3B,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;;IAM7B;;;;OAIG;IACH,GAAG,CAAC,MAAM,EAAE,MAAM;IAIlB,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;CAGhC"}
|
package/dist/plugins/Plugins.js
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A plugin manager
|
|
3
|
+
*/
|
|
1
4
|
export default class Plugins {
|
|
2
5
|
plugins;
|
|
3
6
|
constructor() {
|
|
4
7
|
this.plugins = new Map();
|
|
5
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* @hidden
|
|
11
|
+
*
|
|
12
|
+
* Plugins should be added via App.svelte plugins export
|
|
13
|
+
*/
|
|
6
14
|
add(plugin) {
|
|
7
15
|
this.plugins.set(plugin.name, plugin);
|
|
8
16
|
}
|
|
@@ -1,22 +1,25 @@
|
|
|
1
1
|
export default interface History {
|
|
2
|
-
scrollY(): number;
|
|
2
|
+
scrollY(): number | null;
|
|
3
3
|
replaceState(data: any, url?: string): void;
|
|
4
4
|
pushState(data: any, url: string): void;
|
|
5
5
|
open(url: string): void;
|
|
6
|
+
back(): void;
|
|
6
7
|
}
|
|
7
8
|
export declare class ClientHistory implements History {
|
|
8
|
-
scrollY(): number;
|
|
9
|
+
scrollY(): number | null;
|
|
9
10
|
replaceState(data: any, url?: string): void;
|
|
10
11
|
pushState(data: any, url: string): void;
|
|
11
12
|
open(url: string): void;
|
|
13
|
+
back(): void;
|
|
12
14
|
}
|
|
13
15
|
export declare class ServerHistory implements History {
|
|
14
16
|
state: any | null;
|
|
15
17
|
url: string | null;
|
|
16
18
|
constructor();
|
|
17
|
-
scrollY(): number;
|
|
19
|
+
scrollY(): number | null;
|
|
18
20
|
replaceState(data: any, url?: string): void;
|
|
19
21
|
pushState(data: any, url: string): void;
|
|
20
22
|
open(url: string): void;
|
|
23
|
+
back(): void;
|
|
21
24
|
}
|
|
22
25
|
//# sourceMappingURL=History.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"History.d.ts","sourceRoot":"","sources":["../../../../src/routing/History.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,WAAW,OAAO;IAC/B,OAAO,IAAI,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"History.d.ts","sourceRoot":"","sources":["../../../../src/routing/History.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,WAAW,OAAO;IAC/B,OAAO,IAAI,MAAM,GAAG,IAAI,CAAC;IACzB,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5C,SAAS,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACxC,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,IAAI,IAAI,IAAI,CAAC;CACb;AAED,qBAAa,aAAc,YAAW,OAAO;IAC5C,OAAO,IAAI,MAAM,GAAG,IAAI;IAIxB,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI;IAI3C,SAAS,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI;IAIvC,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAIvB,IAAI,IAAI,IAAI;CAGZ;AAED,qBAAa,aAAc,YAAW,OAAO;IAC5C,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC;IAClB,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;;IAOnB,OAAO,IAAI,MAAM,GAAG,IAAI;IAIxB,YAAY,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI;IAK3C,SAAS,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI;IAKvC,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAIvB,IAAI,IAAI,IAAI;CAGZ"}
|
package/dist/routing/History.js
CHANGED
|
@@ -11,6 +11,9 @@ export class ClientHistory {
|
|
|
11
11
|
open(url) {
|
|
12
12
|
window.location.href = url;
|
|
13
13
|
}
|
|
14
|
+
back() {
|
|
15
|
+
window.history.back();
|
|
16
|
+
}
|
|
14
17
|
}
|
|
15
18
|
export class ServerHistory {
|
|
16
19
|
state;
|
|
@@ -20,7 +23,7 @@ export class ServerHistory {
|
|
|
20
23
|
this.url = null;
|
|
21
24
|
}
|
|
22
25
|
scrollY() {
|
|
23
|
-
return
|
|
26
|
+
return null;
|
|
24
27
|
}
|
|
25
28
|
replaceState(data, url) {
|
|
26
29
|
this.state = data;
|
|
@@ -33,4 +36,7 @@ export class ServerHistory {
|
|
|
33
36
|
open(url) {
|
|
34
37
|
this.url = url;
|
|
35
38
|
}
|
|
39
|
+
back() {
|
|
40
|
+
throw new Error('Cannot go back on the server');
|
|
41
|
+
}
|
|
36
42
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import Route from './Route.js';
|
|
2
1
|
import Site, { SiteFromGraphQl } from './Site.js';
|
|
3
2
|
import History from './History.js';
|
|
3
|
+
import Request from './Request.js';
|
|
4
|
+
import Route from './Route.js';
|
|
4
5
|
export type InnerRouterOpts = {
|
|
5
6
|
preloadOnMouseOver: boolean;
|
|
6
7
|
};
|
|
@@ -17,8 +18,8 @@ export default class InnerRouter {
|
|
|
17
18
|
* @param changeHistory returns a function you need to call when you are ready to
|
|
18
19
|
update the window history (note do not call this after another onRoute call was made)
|
|
19
20
|
*/
|
|
20
|
-
onRoute: (route:
|
|
21
|
-
onPreload: (route:
|
|
21
|
+
onRoute: (route: Request, site: Site, changeHistory: () => void) => void;
|
|
22
|
+
onPreload: (route: Request, site: Site) => void;
|
|
22
23
|
private scrollDebounceTimeout;
|
|
23
24
|
/**
|
|
24
25
|
* Creates a new Router
|
|
@@ -56,7 +57,7 @@ export default class InnerRouter {
|
|
|
56
57
|
* @param target
|
|
57
58
|
* @return Returns null if the url does not match our host (the protocol get's ignored)
|
|
58
59
|
*/
|
|
59
|
-
|
|
60
|
+
targetToRequest(target: string | URL | Route | Request): Request;
|
|
60
61
|
/**
|
|
61
62
|
* Resolve a url and convert it to a Route
|
|
62
63
|
*
|
|
@@ -71,16 +72,16 @@ export default class InnerRouter {
|
|
|
71
72
|
* @param route a route object or an url or uri, never input the same route object again
|
|
72
73
|
* @param pushState if true pushed the state to the window.history
|
|
73
74
|
*/
|
|
74
|
-
open(target: string | URL | Route, pushState?: boolean): void;
|
|
75
|
+
open(target: string | URL | Route | Request, pushState?: boolean): void;
|
|
75
76
|
/**
|
|
76
77
|
* Sets a route
|
|
77
78
|
*
|
|
78
79
|
* Will trigger an onRoute event but will not store any scroll progress
|
|
79
80
|
* or modify the history
|
|
80
81
|
*
|
|
81
|
-
* @param
|
|
82
|
+
* @param req
|
|
82
83
|
*/
|
|
83
|
-
setRoute(
|
|
84
|
+
setRoute(req: Request): void;
|
|
84
85
|
/**
|
|
85
86
|
* This pushes the state of the route without triggering a currentRoute
|
|
86
87
|
* or currentSiteId change
|
|
@@ -105,7 +106,7 @@ export default class InnerRouter {
|
|
|
105
106
|
*
|
|
106
107
|
* @param url
|
|
107
108
|
*/
|
|
108
|
-
preload(target: string | URL | Route): void;
|
|
109
|
-
domReady(
|
|
109
|
+
preload(target: string | URL | Route | Request): void;
|
|
110
|
+
domReady(req: Request): void;
|
|
110
111
|
}
|
|
111
112
|
//# sourceMappingURL=InnerRouter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"InnerRouter.d.ts","sourceRoot":"","sources":["../../../../src/routing/InnerRouter.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"InnerRouter.d.ts","sourceRoot":"","sources":["../../../../src/routing/InnerRouter.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,EAAE,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAClD,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,OAAO,OAAsB,MAAM,cAAc,CAAC;AAClD,OAAO,KAAK,MAAM,YAAY,CAAC;AAE/B,MAAM,MAAM,eAAe,GAAG;IAC7B,kBAAkB,EAAE,OAAO,CAAC;CAC5B,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,WAAW;IAC/B,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,IAAI,EAAE,IAAI,CAAC;IACX,OAAO,EAAE,OAAO,CAAC;IACjB,kBAAkB,EAAE,OAAO,CAAC;IAC5B;;;OAGG;IACH,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,aAAa,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;IACzE,SAAS,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,KAAK,IAAI,CAAC;IAEhD,OAAO,CAAC,qBAAqB,CAAa;IAE1C;;;;;OAKG;gBACS,KAAK,EAAE,eAAe,EAAE,EAAE,IAAI,EAAE,eAAe;IAmB3D;;OAEG;IACH,UAAU;IAkBV;;OAEG;IACH,UAAU;IAEV;;;;;OAKG;IACH,gBAAgB,CAAC,UAAU,GAAE,MAAM,GAAG,IAAW,GAAG,IAAI;IAsCxD;;OAEG;IACH,WAAW,IAAI,IAAI;IAInB;;OAEG;IACH,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI;IAIjC;;;;;OAKG;IACH,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG,GAAG,KAAK,GAAG,OAAO,GAAG,OAAO;IAqBhE;;;;;OAKG;IACH,YAAY,CAAC,OAAO,EAAE,GAAG,GAAG,KAAK;IA8BjC,MAAM;IA0FN;;;;;OAKG;IACH,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG,GAAG,KAAK,GAAG,OAAO,EAAE,SAAS,GAAE,OAAc;IAyCtE;;;;;;;OAOG;IACH,QAAQ,CAAC,GAAG,EAAE,OAAO;IAOrB;;;;;;;;OAQG;IACH,SAAS,CAAC,KAAK,EAAE,KAAK;IAYtB;;;;;OAKG;IACH,YAAY,CAAC,KAAK,EAAE,KAAK;IAYzB;;;;;;OAMG;IACH,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG,GAAG,KAAK,GAAG,OAAO;IAoB9C,QAAQ,CAAC,GAAG,EAAE,OAAO;CAkErB"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import Route from './Route.js';
|
|
2
1
|
import Site from './Site.js';
|
|
3
2
|
import { ClientHistory, ServerHistory } from './History.js';
|
|
3
|
+
import Request, { isRequest } from './Request.js';
|
|
4
|
+
import Route from './Route.js';
|
|
4
5
|
/**
|
|
5
6
|
* Manages event listeners or functions.
|
|
6
7
|
*/
|
|
@@ -43,7 +44,7 @@ export default class InnerRouter {
|
|
|
43
44
|
initClient() {
|
|
44
45
|
this.listen();
|
|
45
46
|
// let's first try to load from the state
|
|
46
|
-
const route = this.
|
|
47
|
+
const route = this.targetToRequest(window.location.href);
|
|
47
48
|
route._fillFromState(window.history.state);
|
|
48
49
|
route.origin = 'init';
|
|
49
50
|
if (route.search.get('x-craft-live-preview')) {
|
|
@@ -115,7 +116,7 @@ export default class InnerRouter {
|
|
|
115
116
|
* @param target
|
|
116
117
|
* @return Returns null if the url does not match our host (the protocol get's ignored)
|
|
117
118
|
*/
|
|
118
|
-
|
|
119
|
+
targetToRequest(target) {
|
|
119
120
|
if (typeof target === 'string') {
|
|
120
121
|
if (target.startsWith('/')) {
|
|
121
122
|
const site = this.site;
|
|
@@ -126,7 +127,10 @@ export default class InnerRouter {
|
|
|
126
127
|
}
|
|
127
128
|
}
|
|
128
129
|
if (target instanceof URL) {
|
|
129
|
-
|
|
130
|
+
target = this.routeFromUrl(target);
|
|
131
|
+
}
|
|
132
|
+
if (!isRequest(target)) {
|
|
133
|
+
return Request.fromRoute(target);
|
|
130
134
|
}
|
|
131
135
|
return target;
|
|
132
136
|
}
|
|
@@ -228,7 +232,7 @@ export default class InnerRouter {
|
|
|
228
232
|
window.addEventListener('popstate', async (e) => {
|
|
229
233
|
if (!('route' in e.state))
|
|
230
234
|
return;
|
|
231
|
-
const route = this.
|
|
235
|
+
const route = this.targetToRequest(window.location.href);
|
|
232
236
|
route._fillFromState(e.state);
|
|
233
237
|
route.origin = 'pop';
|
|
234
238
|
// since the pop event replaced our state we can't replace the state
|
|
@@ -245,33 +249,38 @@ export default class InnerRouter {
|
|
|
245
249
|
* @param pushState if true pushed the state to the window.history
|
|
246
250
|
*/
|
|
247
251
|
open(target, pushState = true) {
|
|
248
|
-
const
|
|
252
|
+
const req = this.targetToRequest(target);
|
|
249
253
|
const current = this.route;
|
|
250
254
|
if (current) {
|
|
251
|
-
// if the scrollY
|
|
255
|
+
// if the scrollY would still be updated we clear the timeout
|
|
252
256
|
// since we should have the latest scrollY
|
|
253
257
|
if (this.scrollDebounceTimeout) {
|
|
254
258
|
clearTimeout(this.scrollDebounceTimeout);
|
|
255
259
|
this.scrollDebounceTimeout = null;
|
|
256
260
|
}
|
|
257
261
|
// store the scroll position
|
|
258
|
-
|
|
259
|
-
|
|
262
|
+
const scrollY = this.history.scrollY();
|
|
263
|
+
if (typeof scrollY === 'number') {
|
|
264
|
+
current.scrollY = scrollY;
|
|
265
|
+
this.history.replaceState(current._toState());
|
|
266
|
+
}
|
|
260
267
|
}
|
|
261
268
|
// if the domain of the current site is different than the domain of the
|
|
262
269
|
// new site we need to do a window.location.href call
|
|
263
|
-
if (current && current.url.origin !==
|
|
264
|
-
|
|
270
|
+
if ((current && current.url.origin !== req.url.origin) ||
|
|
271
|
+
// @ts-ignore
|
|
272
|
+
import.meta.env.SSR) {
|
|
273
|
+
this.history.open(req.url.href);
|
|
265
274
|
return;
|
|
266
275
|
}
|
|
267
276
|
if (pushState) {
|
|
268
|
-
|
|
269
|
-
this.onRoute(
|
|
270
|
-
this.pushState(
|
|
277
|
+
req.index = (current?.index ?? 0) + 1;
|
|
278
|
+
this.onRoute(req, req.site ?? this.site, () => {
|
|
279
|
+
this.pushState(req.toRoute());
|
|
271
280
|
});
|
|
272
281
|
}
|
|
273
282
|
else {
|
|
274
|
-
this.setRoute(
|
|
283
|
+
this.setRoute(req);
|
|
275
284
|
}
|
|
276
285
|
}
|
|
277
286
|
/**
|
|
@@ -280,13 +289,13 @@ export default class InnerRouter {
|
|
|
280
289
|
* Will trigger an onRoute event but will not store any scroll progress
|
|
281
290
|
* or modify the history
|
|
282
291
|
*
|
|
283
|
-
* @param
|
|
292
|
+
* @param req
|
|
284
293
|
*/
|
|
285
|
-
setRoute(
|
|
286
|
-
this.route =
|
|
287
|
-
if (
|
|
288
|
-
this.site =
|
|
289
|
-
this.onRoute(
|
|
294
|
+
setRoute(req) {
|
|
295
|
+
this.route = req.toRoute();
|
|
296
|
+
if (req.site)
|
|
297
|
+
this.site = req.site;
|
|
298
|
+
this.onRoute(req, this.site, () => { });
|
|
290
299
|
}
|
|
291
300
|
/**
|
|
292
301
|
* This pushes the state of the route without triggering a currentRoute
|
|
@@ -325,26 +334,29 @@ export default class InnerRouter {
|
|
|
325
334
|
* @param url
|
|
326
335
|
*/
|
|
327
336
|
preload(target) {
|
|
328
|
-
const
|
|
337
|
+
const req = this.targetToRequest(target);
|
|
338
|
+
// todo, don't think this makes any sense
|
|
329
339
|
// if the domain of the current site is different than the domain of the
|
|
330
340
|
// new site id does not make sense to preload
|
|
331
|
-
if (this.site.url.origin !==
|
|
341
|
+
if (this.site.url.origin !== req.url.origin) {
|
|
332
342
|
return;
|
|
333
343
|
}
|
|
334
344
|
const current = this.route;
|
|
335
|
-
const site =
|
|
345
|
+
const site = req.site ?? this.site;
|
|
336
346
|
// if the origin matches, the route will be able to be load
|
|
337
347
|
// so let's preload it
|
|
338
|
-
if (current && current.url.origin ===
|
|
339
|
-
this.onPreload(
|
|
348
|
+
if (current && current.url.origin === req.url.origin) {
|
|
349
|
+
this.onPreload(req, site);
|
|
340
350
|
}
|
|
341
351
|
}
|
|
342
|
-
domReady(
|
|
352
|
+
domReady(req) {
|
|
353
|
+
if (req.disableScroll)
|
|
354
|
+
return;
|
|
343
355
|
// scroll to target
|
|
344
356
|
let scrollTo = null;
|
|
345
357
|
// if the route is a live preview init and we have a scrollY stored
|
|
346
358
|
// scroll to that
|
|
347
|
-
if (
|
|
359
|
+
if (req.origin === 'live-preview-init') {
|
|
348
360
|
const scrollY = sessionStorage.getItem('live-preview-scroll');
|
|
349
361
|
if (scrollY) {
|
|
350
362
|
scrollTo = {
|
|
@@ -354,10 +366,10 @@ export default class InnerRouter {
|
|
|
354
366
|
}
|
|
355
367
|
// if we have a hash and the route was not visited
|
|
356
368
|
}
|
|
357
|
-
else if (
|
|
358
|
-
((
|
|
359
|
-
|
|
360
|
-
const el = document.getElementById(
|
|
369
|
+
else if (req.hash &&
|
|
370
|
+
((req.origin === 'init' && typeof req.scrollY !== 'number') ||
|
|
371
|
+
req.origin === 'click')) {
|
|
372
|
+
const el = document.getElementById(req.hash.substring(1));
|
|
361
373
|
if (el) {
|
|
362
374
|
scrollTo = {
|
|
363
375
|
intoView: el,
|
|
@@ -367,10 +379,10 @@ export default class InnerRouter {
|
|
|
367
379
|
}
|
|
368
380
|
// restore scroll position
|
|
369
381
|
if (!scrollTo &&
|
|
370
|
-
|
|
371
|
-
typeof
|
|
382
|
+
req.origin !== 'click' &&
|
|
383
|
+
typeof req.scrollY === 'number') {
|
|
372
384
|
scrollTo = {
|
|
373
|
-
top:
|
|
385
|
+
top: req.scrollY,
|
|
374
386
|
behavior: 'instant',
|
|
375
387
|
};
|
|
376
388
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Request from './Request.js';
|
|
2
2
|
import Site from './Site.js';
|
|
3
3
|
export type PageLoaderOptions = {
|
|
4
4
|
debugTiming: boolean;
|
|
@@ -7,7 +7,7 @@ export type LoadResponse = {
|
|
|
7
7
|
success: boolean;
|
|
8
8
|
data: any;
|
|
9
9
|
};
|
|
10
|
-
export type LoadFn = (
|
|
10
|
+
export type LoadFn = (req: Request, site: Site, opts: LoadOptions) => Promise<any> | any;
|
|
11
11
|
export type LoadOptions = {
|
|
12
12
|
setProgress: (num: number) => void;
|
|
13
13
|
};
|
|
@@ -18,7 +18,7 @@ export default class PageLoader<More> {
|
|
|
18
18
|
private debugTiming;
|
|
19
19
|
private preloadedUrls;
|
|
20
20
|
private loadingVersion;
|
|
21
|
-
onLoaded: (resp: LoadResponse,
|
|
21
|
+
onLoaded: (resp: LoadResponse, req: Request, site: Site, more: More) => void;
|
|
22
22
|
onProgress: (loading: boolean, progress?: number) => void;
|
|
23
23
|
loadFn: LoadFn;
|
|
24
24
|
/**
|
|
@@ -31,7 +31,7 @@ export default class PageLoader<More> {
|
|
|
31
31
|
* Discard the current page load if one is happening
|
|
32
32
|
*/
|
|
33
33
|
discard(): void;
|
|
34
|
-
load(
|
|
35
|
-
preload(
|
|
34
|
+
load(req: Request, site: Site, more: More): Promise<void>;
|
|
35
|
+
preload(req: Request, site: Site): Promise<void>;
|
|
36
36
|
}
|
|
37
37
|
//# sourceMappingURL=PageLoader.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PageLoader.d.ts","sourceRoot":"","sources":["../../../../src/routing/PageLoader.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"PageLoader.d.ts","sourceRoot":"","sources":["../../../../src/routing/PageLoader.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,cAAc,CAAC;AACnC,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,MAAM,MAAM,iBAAiB,GAAG;IAC/B,WAAW,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IAC1B,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,GAAG,CAAC;CACV,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG,CACpB,GAAG,EAAE,OAAO,EACZ,IAAI,EAAE,IAAI,EACV,IAAI,EAAE,WAAW,KACb,OAAO,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;AAExB,MAAM,MAAM,WAAW,GAAG;IACzB,WAAW,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;CACnC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,UAAU,CAAC,IAAI;IACnC,OAAO,CAAC,WAAW,CAAU;IAC7B,OAAO,CAAC,aAAa,CAAc;IAEnC,OAAO,CAAC,cAAc,CAAS;IAE/B,QAAQ,EAAE,CACT,IAAI,EAAE,YAAY,EAClB,GAAG,EAAE,OAAO,EACZ,IAAI,EAAE,IAAI,EACV,IAAI,EAAE,IAAI,KACN,IAAI,CAAC;IACV,UAAU,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IAC1D,MAAM,EAAE,MAAM,CAAC;IAEf;;;;OAIG;gBACS,OAAO,EAAE,iBAAiB;IAWtC;;OAEG;IACH,OAAO;IAKD,IAAI,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;IAiCzC,OAAO,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI;CActC"}
|
|
@@ -28,7 +28,7 @@ export default class PageLoader {
|
|
|
28
28
|
this.loadingVersion++;
|
|
29
29
|
this.onProgress(false);
|
|
30
30
|
}
|
|
31
|
-
async load(
|
|
31
|
+
async load(req, site, more) {
|
|
32
32
|
this.onProgress(true);
|
|
33
33
|
const version = ++this.loadingVersion;
|
|
34
34
|
const startTime = this.debugTiming ? Date.now() : null;
|
|
@@ -39,7 +39,7 @@ export default class PageLoader {
|
|
|
39
39
|
};
|
|
40
40
|
const resp = { success: false, data: null };
|
|
41
41
|
try {
|
|
42
|
-
resp.data = await this.loadFn(
|
|
42
|
+
resp.data = await this.loadFn(req, site, { setProgress });
|
|
43
43
|
resp.success = true;
|
|
44
44
|
}
|
|
45
45
|
catch (e) {
|
|
@@ -52,18 +52,18 @@ export default class PageLoader {
|
|
|
52
52
|
if (this.loadingVersion !== version)
|
|
53
53
|
return console.log('route changed quickly, ignoring response');
|
|
54
54
|
this.onProgress(false);
|
|
55
|
-
this.onLoaded(resp,
|
|
55
|
+
this.onLoaded(resp, req, site, more);
|
|
56
56
|
}
|
|
57
57
|
// you don't need to wait on this call
|
|
58
|
-
async preload(
|
|
59
|
-
const url =
|
|
58
|
+
async preload(req, site) {
|
|
59
|
+
const url = req.url.origin + req.url.pathname;
|
|
60
60
|
if (this.preloadedUrls.has(url))
|
|
61
61
|
return;
|
|
62
62
|
this.preloadedUrls.add(url);
|
|
63
63
|
try {
|
|
64
|
-
await this.loadFn(
|
|
64
|
+
await this.loadFn(req, site, { setProgress: () => null });
|
|
65
65
|
}
|
|
66
|
-
catch (
|
|
66
|
+
catch (_e) {
|
|
67
67
|
console.log('preload failed');
|
|
68
68
|
// retry at another time
|
|
69
69
|
this.preloadedUrls.delete(url);
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Barrier } from 'crelte-std/sync';
|
|
2
|
+
import Route, { RouteOrigin } from './Route.js';
|
|
3
|
+
import Site from './Site.js';
|
|
4
|
+
/**
|
|
5
|
+
* Options to create a Request
|
|
6
|
+
*/
|
|
7
|
+
export type RequestOptions = {
|
|
8
|
+
scrollY?: number;
|
|
9
|
+
index?: number;
|
|
10
|
+
origin?: RouteOrigin;
|
|
11
|
+
disableScroll?: boolean;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* A Request is a Route with some extra options
|
|
15
|
+
* you get a Request from the onRequest event or
|
|
16
|
+
* in a loadGlobal function.
|
|
17
|
+
*/
|
|
18
|
+
export default class Request extends Route {
|
|
19
|
+
/**
|
|
20
|
+
* Disable scrolling for this request
|
|
21
|
+
* @default false
|
|
22
|
+
*/
|
|
23
|
+
disableScroll: boolean;
|
|
24
|
+
/** @hidden */
|
|
25
|
+
_renderBarrier: RenderBarrier;
|
|
26
|
+
/**
|
|
27
|
+
* Create a new Request
|
|
28
|
+
*/
|
|
29
|
+
constructor(url: string | URL, site: Site | null, opts?: RequestOptions);
|
|
30
|
+
/**
|
|
31
|
+
* Create a Request from a Route
|
|
32
|
+
*/
|
|
33
|
+
static fromRoute(route: Route): Request;
|
|
34
|
+
/**
|
|
35
|
+
* With delayRender you can make sure that the render waits
|
|
36
|
+
* until you are ready. This is useful for building page transitions.
|
|
37
|
+
*
|
|
38
|
+
* ## Important
|
|
39
|
+
* If you call delayRender you need to call `ready/remove` or the render
|
|
40
|
+
* will never happen
|
|
41
|
+
*
|
|
42
|
+
* ## Example
|
|
43
|
+
* ```
|
|
44
|
+
* import { onRequest } from 'crelte';
|
|
45
|
+
* import { animate } from 'motion';
|
|
46
|
+
*
|
|
47
|
+
* onRequest(async req => {
|
|
48
|
+
* if (req.origin !== 'click' && req.origin !== 'manual') return;
|
|
49
|
+
*
|
|
50
|
+
* const delay = req.delayRender();
|
|
51
|
+
*
|
|
52
|
+
* await animate(plane, { x: '0%' });
|
|
53
|
+
*
|
|
54
|
+
* // wait until the new page is ready to be rendered
|
|
55
|
+
* // if the render was cancelled we return
|
|
56
|
+
* if (await delay.ready()) return;
|
|
57
|
+
*
|
|
58
|
+
* await animate(plane, { x: '100%' });
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
delayRender(): DelayRender;
|
|
63
|
+
/**
|
|
64
|
+
* Create a copy of the request
|
|
65
|
+
*/
|
|
66
|
+
clone(): Request;
|
|
67
|
+
/**
|
|
68
|
+
* Create a Route from the Request
|
|
69
|
+
*/
|
|
70
|
+
toRoute(): Route;
|
|
71
|
+
}
|
|
72
|
+
export declare function isRequest(req: any): req is Request;
|
|
73
|
+
declare class RenderBarrier {
|
|
74
|
+
inner: Barrier<unknown>;
|
|
75
|
+
cancelled: boolean;
|
|
76
|
+
root: DelayRender;
|
|
77
|
+
constructor();
|
|
78
|
+
isOpen(): boolean;
|
|
79
|
+
add(): DelayRender;
|
|
80
|
+
/** @hidden */
|
|
81
|
+
cancel(): void;
|
|
82
|
+
/** @hidden */
|
|
83
|
+
ready(): Promise<boolean>;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* DelayRender is returned by `Request.delayRender`
|
|
87
|
+
*/
|
|
88
|
+
export type DelayRender = {
|
|
89
|
+
/**
|
|
90
|
+
* Call this when you're ready for the render to happen
|
|
91
|
+
* the promise will resolve when the render is done or was cancelled
|
|
92
|
+
*
|
|
93
|
+
* @returns if the render was cancelled
|
|
94
|
+
*/
|
|
95
|
+
ready: () => Promise<boolean>;
|
|
96
|
+
/**
|
|
97
|
+
* If youre not interested when the render happens anymore
|
|
98
|
+
*/
|
|
99
|
+
remove: () => void;
|
|
100
|
+
};
|
|
101
|
+
export {};
|
|
102
|
+
//# sourceMappingURL=Request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Request.d.ts","sourceRoot":"","sources":["../../../../src/routing/Request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,KAAK,EAAE,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,aAAa,CAAC,EAAE,OAAO,CAAC;CACxB,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,OAAO,OAAO,OAAQ,SAAQ,KAAK;IACzC;;;OAGG;IACH,aAAa,EAAE,OAAO,CAAC;IAEvB,cAAc;IACd,cAAc,EAAE,aAAa,CAAC;IAE9B;;OAEG;gBAEF,GAAG,EAAE,MAAM,GAAG,GAAG,EACjB,IAAI,EAAE,IAAI,GAAG,IAAI,EACjB,IAAI,GAAE,cAAmB;IAQ1B;;OAEG;IACH,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,KAAK;IAQ7B;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,WAAW,IAAI,WAAW;IAI1B;;OAEG;IACH,KAAK;IASL;;OAEG;IACH,OAAO;CAOP;AAED,wBAAgB,SAAS,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,IAAI,OAAO,CAElD;AAED,cAAM,aAAa;IAClB,KAAK,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IACxB,SAAS,EAAE,OAAO,CAAC;IACnB,IAAI,EAAE,WAAW,CAAC;;IAQlB,MAAM,IAAI,OAAO;IAIjB,GAAG,IAAI,WAAW;IAclB,cAAc;IACd,MAAM;IAQN,cAAc;IACd,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC;CAGzB;AAED;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG;IACzB;;;;;OAKG;IACH,KAAK,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC;IAE9B;;OAEG;IACH,MAAM,EAAE,MAAM,IAAI,CAAC;CACnB,CAAC"}
|