crelte 0.3.0 → 0.3.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 +12 -0
- package/dist/Crelte.d.ts.map +1 -1
- package/dist/Crelte.js +12 -0
- package/dist/CrelteRequest.d.ts +0 -4
- package/dist/CrelteRequest.d.ts.map +1 -1
- package/dist/CrelteRequest.js +0 -4
- package/dist/entry/EntryRouter.d.ts +30 -0
- package/dist/entry/EntryRouter.d.ts.map +1 -0
- package/dist/entry/EntryRouter.js +45 -0
- package/dist/entry/index.d.ts +32 -0
- package/dist/entry/index.d.ts.map +1 -0
- package/dist/entry/index.js +31 -0
- package/dist/index.d.ts +2 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -6
- package/dist/init/client.d.ts +1 -1
- package/dist/init/client.d.ts.map +1 -1
- package/dist/init/client.js +4 -3
- package/dist/init/server.d.ts.map +1 -1
- package/dist/init/server.js +3 -2
- package/dist/init/shared.d.ts +3 -1
- package/dist/init/shared.d.ts.map +1 -1
- package/dist/init/shared.js +47 -26
- package/dist/routing/InnerRouter.d.ts +1 -10
- package/dist/routing/InnerRouter.d.ts.map +1 -1
- package/dist/routing/InnerRouter.js +20 -21
- package/dist/routing/Request.d.ts +2 -0
- package/dist/routing/Request.d.ts.map +1 -1
- package/dist/routing/Request.js +9 -0
- package/dist/routing/Route.d.ts +67 -1
- package/dist/routing/Route.d.ts.map +1 -1
- package/dist/routing/Route.js +98 -2
- package/dist/routing/Router.d.ts +49 -12
- package/dist/routing/Router.d.ts.map +1 -1
- package/dist/routing/Router.js +82 -28
- package/dist/routing/index.d.ts +2 -2
- package/dist/routing/index.d.ts.map +1 -1
- package/dist/utils.d.ts +2 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +8 -0
- package/package.json +9 -4
- package/src/Crelte.ts +15 -0
- package/src/CrelteRequest.ts +0 -4
- package/src/entry/EntryRouter.ts +71 -0
- package/src/entry/index.ts +48 -0
- package/src/index.ts +2 -11
- package/src/init/client.ts +10 -3
- package/src/init/server.ts +9 -2
- package/src/init/shared.ts +78 -28
- package/src/routing/InnerRouter.ts +29 -30
- package/src/routing/Request.ts +11 -0
- package/src/routing/Route.ts +107 -2
- package/src/routing/Router.ts +110 -31
- package/src/routing/index.ts +2 -1
- package/src/utils.ts +10 -0
|
@@ -1 +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;
|
|
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;AAG7B;;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,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC9B,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,OAAO,OAAO,OAAQ,SAAQ,KAAK;IACzC;;;OAGG;IACH,aAAa,EAAE,OAAO,CAAC;IAEvB;;;OAGG;IACH,eAAe,EAAE,OAAO,CAAC;IAEzB;;OAEG;IACH,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAE1B,cAAc;IACd,cAAc,EAAE,aAAa,CAAC;IAE9B;;OAEG;gBACS,GAAG,EAAE,MAAM,GAAG,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,GAAE,cAAmB;IASpE;;OAEG;IACH,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,GAAE,cAAmB;IAWxD;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,WAAW,IAAI,WAAW;IAI1B;;OAEG;IACH,KAAK;IAYL;;OAEG;IACH,OAAO;IAUP,cAAc;IACd,WAAW,CAAC,IAAI,GAAE,cAAmB;CASrC;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"}
|
package/dist/routing/Request.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Barrier } from 'crelte-std/sync';
|
|
2
2
|
import Route from './Route.js';
|
|
3
|
+
import { objClone } from '../utils.js';
|
|
3
4
|
/**
|
|
4
5
|
* A Request is a Route with some extra options
|
|
5
6
|
* you get a Request from the onRequest event or
|
|
@@ -40,6 +41,8 @@ export default class Request extends Route {
|
|
|
40
41
|
scrollY: route.scrollY ?? undefined,
|
|
41
42
|
index: route.index,
|
|
42
43
|
origin: route.origin,
|
|
44
|
+
state: route._state,
|
|
45
|
+
context: route._context,
|
|
43
46
|
...opts,
|
|
44
47
|
});
|
|
45
48
|
}
|
|
@@ -82,6 +85,8 @@ export default class Request extends Route {
|
|
|
82
85
|
scrollY: this.scrollY ?? undefined,
|
|
83
86
|
index: this.index,
|
|
84
87
|
origin: this.origin,
|
|
88
|
+
state: objClone(this._state),
|
|
89
|
+
context: this._context,
|
|
85
90
|
disableScroll: this.disableScroll,
|
|
86
91
|
statusCode: this.statusCode ?? undefined,
|
|
87
92
|
});
|
|
@@ -94,6 +99,8 @@ export default class Request extends Route {
|
|
|
94
99
|
scrollY: this.scrollY ?? undefined,
|
|
95
100
|
index: this.index,
|
|
96
101
|
origin: this.origin,
|
|
102
|
+
state: objClone(this._state),
|
|
103
|
+
context: this._context,
|
|
97
104
|
});
|
|
98
105
|
}
|
|
99
106
|
/** @hidden */
|
|
@@ -101,6 +108,8 @@ export default class Request extends Route {
|
|
|
101
108
|
this.scrollY = opts.scrollY ?? this.scrollY;
|
|
102
109
|
this.index = opts.index ?? this.index;
|
|
103
110
|
this.origin = opts.origin ?? this.origin;
|
|
111
|
+
this._state = opts.state ?? this._state;
|
|
112
|
+
this._context = opts.context ?? this._context;
|
|
104
113
|
this.disableScroll = opts.disableScroll ?? this.disableScroll;
|
|
105
114
|
this.statusCode = opts.statusCode ?? this.statusCode;
|
|
106
115
|
}
|
package/dist/routing/Route.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ export type RouteOptions = {
|
|
|
3
3
|
scrollY?: number;
|
|
4
4
|
index?: number;
|
|
5
5
|
origin?: RouteOrigin;
|
|
6
|
+
state?: Record<string, any>;
|
|
7
|
+
context?: Record<string, any>;
|
|
6
8
|
};
|
|
7
9
|
/**
|
|
8
10
|
* RouteOrigin represents the origin of a route.
|
|
@@ -59,6 +61,23 @@ export default class Route {
|
|
|
59
61
|
* The origin of this route, See [[RouteOrigin]]
|
|
60
62
|
*/
|
|
61
63
|
origin: RouteOrigin;
|
|
64
|
+
/**
|
|
65
|
+
* @hidden
|
|
66
|
+
* State data that can be used to store additional information
|
|
67
|
+
*/
|
|
68
|
+
_state: Record<string, any>;
|
|
69
|
+
/**
|
|
70
|
+
* @hidden
|
|
71
|
+
* Any data that should be passed to onRoute and onRequest handlers
|
|
72
|
+
* or exchanged between loadData's
|
|
73
|
+
* This context is not persistant and should be considered "valid"
|
|
74
|
+
* only for the current request / route
|
|
75
|
+
*
|
|
76
|
+
* ## Note
|
|
77
|
+
* Consider using state instead. This will not be cloned in the clone
|
|
78
|
+
* call so will always be the same object
|
|
79
|
+
*/
|
|
80
|
+
_context: Record<string, any>;
|
|
62
81
|
/**
|
|
63
82
|
* Creates a new Route
|
|
64
83
|
*/
|
|
@@ -120,6 +139,17 @@ export default class Route {
|
|
|
120
139
|
* ```
|
|
121
140
|
*/
|
|
122
141
|
get hash(): string;
|
|
142
|
+
/**
|
|
143
|
+
* Set the hash of the route
|
|
144
|
+
*
|
|
145
|
+
* ## Example
|
|
146
|
+
* ```
|
|
147
|
+
* const route = new Route('https://example.com/foo/bar/', null);
|
|
148
|
+
* route.hash = '#hash';
|
|
149
|
+
* console.log(route.url.href); // 'https://example.com/foo/bar/#hash'
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
set hash(hash: string);
|
|
123
153
|
/**
|
|
124
154
|
* Checks if there are previous routes which would allow it to go back
|
|
125
155
|
*/
|
|
@@ -135,7 +165,8 @@ export default class Route {
|
|
|
135
165
|
*/
|
|
136
166
|
getSearchParam(key: string): string | null;
|
|
137
167
|
/**
|
|
138
|
-
* Sets the search param or removes it if the value is null or
|
|
168
|
+
* Sets the search param or removes it if the value is null, undefined or an
|
|
169
|
+
* empty string
|
|
139
170
|
*
|
|
140
171
|
* ## Example
|
|
141
172
|
* ```
|
|
@@ -148,6 +179,38 @@ export default class Route {
|
|
|
148
179
|
* ```
|
|
149
180
|
*/
|
|
150
181
|
setSearchParam(key: string, value?: string | number | null): void;
|
|
182
|
+
/**
|
|
183
|
+
* Returns a state value if it exists.
|
|
184
|
+
*/
|
|
185
|
+
getState<T = any>(key: string): T | null;
|
|
186
|
+
/**
|
|
187
|
+
* Sets a state value.
|
|
188
|
+
* If the value is null or undefined, the key will be removed.
|
|
189
|
+
*
|
|
190
|
+
* ## When to use state
|
|
191
|
+
* State is used to store additional information that persists across route changes.
|
|
192
|
+
* The State is only available in the client code since it is stored using window.history.
|
|
193
|
+
*
|
|
194
|
+
* Consider using setSearchParam instead to enable server side rendering.
|
|
195
|
+
*/
|
|
196
|
+
setState<T>(key: string, value: T | null | undefined): void;
|
|
197
|
+
/**
|
|
198
|
+
* Returns a context value if it exists.
|
|
199
|
+
*/
|
|
200
|
+
getContext<T = any>(key: string): T | null;
|
|
201
|
+
/**
|
|
202
|
+
* Sets a context value.
|
|
203
|
+
* If the value is null or undefined, the key will be removed.
|
|
204
|
+
*
|
|
205
|
+
* ## When to use context
|
|
206
|
+
* Context is used to pass data to onRoute and onRequest handlers or exchange data between loadData calls.
|
|
207
|
+
* This context is not persistent and should be considered valid only for the current request/route.
|
|
208
|
+
* The context is not cloned in the clone call and will be the same object.
|
|
209
|
+
*/
|
|
210
|
+
setContext<T>(key: string, value: T | null | undefined): void;
|
|
211
|
+
/**
|
|
212
|
+
* Returns true if the route is in live preview mode
|
|
213
|
+
*/
|
|
151
214
|
inLivePreview(): boolean;
|
|
152
215
|
/**
|
|
153
216
|
* Returns if the site matches the url
|
|
@@ -158,6 +221,9 @@ export default class Route {
|
|
|
158
221
|
*
|
|
159
222
|
* This checks all properties of the url but search params do not have to be
|
|
160
223
|
* in the same order
|
|
224
|
+
*
|
|
225
|
+
* ## Note
|
|
226
|
+
* This does not check the state or context
|
|
161
227
|
*/
|
|
162
228
|
eq(route: Route | null): boolean | null;
|
|
163
229
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Route.d.ts","sourceRoot":"","sources":["../../../../src/routing/Route.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Route.d.ts","sourceRoot":"","sources":["../../../../src/routing/Route.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,MAAM,WAAW,CAAC;AAG7B,MAAM,MAAM,YAAY,GAAG;IAC1B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC9B,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,WAAW,GACpB,MAAM,GACN,QAAQ,GACR,OAAO,GACP,KAAK,GACL,SAAS,GACT,MAAM,CAAC;AAEV;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,KAAK;IACzB;;OAEG;IACH,GAAG,EAAE,GAAG,CAAC;IAET;;;;;;;;;OASG;IACH,IAAI,EAAE,IAAI,CAAC;IAEX;;;;;;;;;OASG;IACH,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAEvB;;;OAGG;IACH,KAAK,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,MAAM,EAAE,WAAW,CAAC;IAEpB;;;OAGG;IACH,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAE5B;;;;;;;;;;OAUG;IACH,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAE9B;;OAEG;gBACS,GAAG,EAAE,MAAM,GAAG,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,GAAE,YAAiB;IAWlE;;;;;;;;;;;;;;;OAeG;IACH,IAAI,GAAG,IAAI,MAAM,CAQhB;IAED;;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IAI1B;;OAEG;IACH,KAAK;IAUL,cAAc;IACd,cAAc,CAAC,KAAK,EAAE,GAAG;IAYzB,cAAc;IACd,QAAQ,IAAI,GAAG;CASf"}
|
package/dist/routing/Route.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { objClone } from '../utils.js';
|
|
1
2
|
import { trimSlashEnd } from './utils.js';
|
|
2
3
|
/**
|
|
3
4
|
* A Route contains information about the current page for example the url and
|
|
@@ -39,6 +40,23 @@ export default class Route {
|
|
|
39
40
|
* The origin of this route, See [[RouteOrigin]]
|
|
40
41
|
*/
|
|
41
42
|
origin;
|
|
43
|
+
/**
|
|
44
|
+
* @hidden
|
|
45
|
+
* State data that can be used to store additional information
|
|
46
|
+
*/
|
|
47
|
+
_state;
|
|
48
|
+
/**
|
|
49
|
+
* @hidden
|
|
50
|
+
* Any data that should be passed to onRoute and onRequest handlers
|
|
51
|
+
* or exchanged between loadData's
|
|
52
|
+
* This context is not persistant and should be considered "valid"
|
|
53
|
+
* only for the current request / route
|
|
54
|
+
*
|
|
55
|
+
* ## Note
|
|
56
|
+
* Consider using state instead. This will not be cloned in the clone
|
|
57
|
+
* call so will always be the same object
|
|
58
|
+
*/
|
|
59
|
+
_context;
|
|
42
60
|
/**
|
|
43
61
|
* Creates a new Route
|
|
44
62
|
*/
|
|
@@ -48,6 +66,8 @@ export default class Route {
|
|
|
48
66
|
this.scrollY = opts.scrollY ?? null;
|
|
49
67
|
this.index = opts.index ?? 0;
|
|
50
68
|
this.origin = opts.origin ?? 'manual';
|
|
69
|
+
this._state = opts.state ?? {};
|
|
70
|
+
this._context = opts.context ?? {};
|
|
51
71
|
}
|
|
52
72
|
/**
|
|
53
73
|
* Returns the uri of the route
|
|
@@ -119,6 +139,19 @@ export default class Route {
|
|
|
119
139
|
get hash() {
|
|
120
140
|
return this.url.hash;
|
|
121
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Set the hash of the route
|
|
144
|
+
*
|
|
145
|
+
* ## Example
|
|
146
|
+
* ```
|
|
147
|
+
* const route = new Route('https://example.com/foo/bar/', null);
|
|
148
|
+
* route.hash = '#hash';
|
|
149
|
+
* console.log(route.url.href); // 'https://example.com/foo/bar/#hash'
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
set hash(hash) {
|
|
153
|
+
this.url.hash = hash;
|
|
154
|
+
}
|
|
122
155
|
/**
|
|
123
156
|
* Checks if there are previous routes which would allow it to go back
|
|
124
157
|
*/
|
|
@@ -138,7 +171,8 @@ export default class Route {
|
|
|
138
171
|
return this.search.get(key);
|
|
139
172
|
}
|
|
140
173
|
/**
|
|
141
|
-
* Sets the search param or removes it if the value is null or
|
|
174
|
+
* Sets the search param or removes it if the value is null, undefined or an
|
|
175
|
+
* empty string
|
|
142
176
|
*
|
|
143
177
|
* ## Example
|
|
144
178
|
* ```
|
|
@@ -151,13 +185,66 @@ export default class Route {
|
|
|
151
185
|
* ```
|
|
152
186
|
*/
|
|
153
187
|
setSearchParam(key, value) {
|
|
154
|
-
|
|
188
|
+
const deleteValue = typeof value === 'undefined' ||
|
|
189
|
+
value === null ||
|
|
190
|
+
(typeof value === 'string' && value === '');
|
|
191
|
+
if (!deleteValue) {
|
|
155
192
|
this.search.set(key, value);
|
|
156
193
|
}
|
|
157
194
|
else {
|
|
158
195
|
this.search.delete(key);
|
|
159
196
|
}
|
|
160
197
|
}
|
|
198
|
+
/**
|
|
199
|
+
* Returns a state value if it exists.
|
|
200
|
+
*/
|
|
201
|
+
getState(key) {
|
|
202
|
+
return this._state[key] ?? null;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Sets a state value.
|
|
206
|
+
* If the value is null or undefined, the key will be removed.
|
|
207
|
+
*
|
|
208
|
+
* ## When to use state
|
|
209
|
+
* State is used to store additional information that persists across route changes.
|
|
210
|
+
* The State is only available in the client code since it is stored using window.history.
|
|
211
|
+
*
|
|
212
|
+
* Consider using setSearchParam instead to enable server side rendering.
|
|
213
|
+
*/
|
|
214
|
+
setState(key, value) {
|
|
215
|
+
if (typeof value === 'undefined' || value === null) {
|
|
216
|
+
delete this._state[key];
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
this._state[key] = value;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Returns a context value if it exists.
|
|
224
|
+
*/
|
|
225
|
+
getContext(key) {
|
|
226
|
+
return this._context[key] ?? null;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Sets a context value.
|
|
230
|
+
* If the value is null or undefined, the key will be removed.
|
|
231
|
+
*
|
|
232
|
+
* ## When to use context
|
|
233
|
+
* Context is used to pass data to onRoute and onRequest handlers or exchange data between loadData calls.
|
|
234
|
+
* This context is not persistent and should be considered valid only for the current request/route.
|
|
235
|
+
* The context is not cloned in the clone call and will be the same object.
|
|
236
|
+
*/
|
|
237
|
+
setContext(key, value) {
|
|
238
|
+
if (typeof value === 'undefined' || value === null) {
|
|
239
|
+
delete this._context[key];
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
this._context[key] = value;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Returns true if the route is in live preview mode
|
|
247
|
+
*/
|
|
161
248
|
inLivePreview() {
|
|
162
249
|
return !!this.search.get('x-craft-live-preview');
|
|
163
250
|
}
|
|
@@ -179,6 +266,9 @@ export default class Route {
|
|
|
179
266
|
*
|
|
180
267
|
* This checks all properties of the url but search params do not have to be
|
|
181
268
|
* in the same order
|
|
269
|
+
*
|
|
270
|
+
* ## Note
|
|
271
|
+
* This does not check the state or context
|
|
182
272
|
*/
|
|
183
273
|
eq(route) {
|
|
184
274
|
return (route &&
|
|
@@ -227,6 +317,8 @@ export default class Route {
|
|
|
227
317
|
scrollY: this.scrollY ?? undefined,
|
|
228
318
|
index: this.index,
|
|
229
319
|
origin: this.origin,
|
|
320
|
+
state: objClone(this._state),
|
|
321
|
+
context: this._context,
|
|
230
322
|
});
|
|
231
323
|
}
|
|
232
324
|
/** @hidden */
|
|
@@ -235,6 +327,9 @@ export default class Route {
|
|
|
235
327
|
this.scrollY = state.route.scrollY;
|
|
236
328
|
if (typeof state?.route?.index === 'number')
|
|
237
329
|
this.index = state.route.index;
|
|
330
|
+
if (typeof state?.state === 'object' && state.state !== null) {
|
|
331
|
+
this._state = state.state;
|
|
332
|
+
}
|
|
238
333
|
}
|
|
239
334
|
/** @hidden */
|
|
240
335
|
_toState() {
|
|
@@ -243,6 +338,7 @@ export default class Route {
|
|
|
243
338
|
scrollY: this.scrollY,
|
|
244
339
|
index: this.index,
|
|
245
340
|
},
|
|
341
|
+
state: this._state,
|
|
246
342
|
};
|
|
247
343
|
}
|
|
248
344
|
}
|
package/dist/routing/Router.d.ts
CHANGED
|
@@ -7,6 +7,17 @@ export type RouterOptions = {
|
|
|
7
7
|
preloadOnMouseOver?: boolean;
|
|
8
8
|
debugTiming?: boolean;
|
|
9
9
|
};
|
|
10
|
+
/**
|
|
11
|
+
* Allows to easely modify a Request
|
|
12
|
+
*
|
|
13
|
+
* If you return `false` the request will be aborted
|
|
14
|
+
*
|
|
15
|
+
* ## Example
|
|
16
|
+
* ```
|
|
17
|
+
* router.replace(req => (req.hash = ''));
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export type UpdateRequest = (req: Request) => boolean | null | undefined | void;
|
|
10
21
|
/**
|
|
11
22
|
* internal only
|
|
12
23
|
*/
|
|
@@ -106,19 +117,29 @@ export default class Router {
|
|
|
106
117
|
* // the following page will be opened https://example.com/de/foo/bar
|
|
107
118
|
* ```
|
|
108
119
|
*/
|
|
109
|
-
open(target: string | URL | Route | Request, opts?: RequestOptions): void;
|
|
120
|
+
open(target: string | URL | Route | Request | UpdateRequest, opts?: RequestOptions): void;
|
|
110
121
|
/**
|
|
111
122
|
* This pushes the new route without triggering a new pageload
|
|
112
123
|
*
|
|
113
124
|
* You can use this when using pagination for example change the route object
|
|
114
|
-
* (search argument) and then call
|
|
125
|
+
* (search argument) and then call push
|
|
115
126
|
*
|
|
116
127
|
* ## Note
|
|
117
128
|
* This will always set the origin to 'push'
|
|
118
129
|
* And will clear the scrollY value if you not provide a new one via the `opts`
|
|
119
130
|
* This will disableLoadData by default if you not provide an override via the `opts`
|
|
120
131
|
*
|
|
121
|
-
* ## Example
|
|
132
|
+
* ## Example using the update function
|
|
133
|
+
* ```
|
|
134
|
+
* import { getRouter } from 'crelte';
|
|
135
|
+
*
|
|
136
|
+
* const router = getRouter();
|
|
137
|
+
*
|
|
138
|
+
* const page = 1;
|
|
139
|
+
* router.push(req => req.setSearchParam('page', page || null));
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* ## Example using the route object
|
|
122
143
|
* ```
|
|
123
144
|
* import { getRouter } from 'crelte';
|
|
124
145
|
*
|
|
@@ -127,25 +148,35 @@ export default class Router {
|
|
|
127
148
|
* const page = 1;
|
|
128
149
|
* const route = router.route.get();
|
|
129
150
|
* route.setSearchParam('page', page > 0 ? page : null);
|
|
130
|
-
* router.
|
|
151
|
+
* router.push(route);
|
|
131
152
|
* ```
|
|
132
153
|
*/
|
|
133
|
-
push(route: Route | Request, opts?: RequestOptions): void;
|
|
154
|
+
push(route: Route | Request | UpdateRequest, opts?: RequestOptions): void;
|
|
134
155
|
/**
|
|
135
156
|
* @deprecated use push instead
|
|
136
157
|
*/
|
|
137
158
|
pushState(route: Route | Request): void;
|
|
138
159
|
/**
|
|
139
|
-
* This replaces the state of the route without triggering
|
|
160
|
+
* This replaces the state of the route without triggering a new pageload
|
|
140
161
|
*
|
|
141
162
|
* You can use this when using some filters for example a search filter
|
|
142
163
|
*
|
|
143
164
|
* ## Note
|
|
144
165
|
* This will always set the origin to 'replace'
|
|
145
|
-
* And will clear the scrollY value if you
|
|
146
|
-
* This will disableLoadData by default if you
|
|
166
|
+
* And will clear the scrollY value if you don't provide a new one via the `opts`
|
|
167
|
+
* This will disableLoadData by default if you don't provide an override via the `opts`
|
|
147
168
|
*
|
|
148
|
-
* ## Example
|
|
169
|
+
* ## Example using the update function
|
|
170
|
+
* ```
|
|
171
|
+
* import { getRouter } from 'crelte';
|
|
172
|
+
*
|
|
173
|
+
* const router = getRouter();
|
|
174
|
+
*
|
|
175
|
+
* const search = 'foo';
|
|
176
|
+
* router.replace(req => req.setSearchParam('search', search));
|
|
177
|
+
* ```
|
|
178
|
+
*
|
|
179
|
+
* ## Example using the route object
|
|
149
180
|
* ```
|
|
150
181
|
* import { getRouter } from 'crelte';
|
|
151
182
|
*
|
|
@@ -153,11 +184,11 @@ export default class Router {
|
|
|
153
184
|
*
|
|
154
185
|
* const search = 'foo';
|
|
155
186
|
* const route = router.route.get();
|
|
156
|
-
* route.setSearchParam('search', search
|
|
157
|
-
* router.
|
|
187
|
+
* route.setSearchParam('search', search);
|
|
188
|
+
* router.replace(route);
|
|
158
189
|
* ```
|
|
159
190
|
*/
|
|
160
|
-
replace(route: Route | Request, opts?: RequestOptions): void;
|
|
191
|
+
replace(route: Route | Request | UpdateRequest, opts?: RequestOptions): void;
|
|
161
192
|
/**
|
|
162
193
|
* @deprecated use replace instead
|
|
163
194
|
*/
|
|
@@ -209,6 +240,12 @@ export default class Router {
|
|
|
209
240
|
private _onLoaded;
|
|
210
241
|
private _onNothingLoaded;
|
|
211
242
|
private _onProgress;
|
|
243
|
+
/**
|
|
244
|
+
* Transforms a target to a request
|
|
245
|
+
*
|
|
246
|
+
* returns null if the request was canceled by the update request
|
|
247
|
+
*/
|
|
248
|
+
private targetOrUpdateToRequest;
|
|
212
249
|
}
|
|
213
250
|
export {};
|
|
214
251
|
//# sourceMappingURL=Router.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Router.d.ts","sourceRoot":"","sources":["../../../../src/routing/Router.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,IAAI,EAAE,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAElD,OAAmB,EAA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