@lytjs/router 6.4.0 → 6.6.0
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/README.md +463 -0
- package/dist/index.cjs +1062 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +284 -0
- package/dist/index.d.ts +284 -0
- package/dist/index.mjs +1050 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +55 -55
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1062 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var reactivity = require('@lytjs/reactivity');
|
|
4
|
+
|
|
5
|
+
// src/types.ts
|
|
6
|
+
function locationQueryToSearchParams(query) {
|
|
7
|
+
const searchParams = new URLSearchParams();
|
|
8
|
+
for (const [key, value] of Object.entries(query)) {
|
|
9
|
+
if (value === null) {
|
|
10
|
+
continue;
|
|
11
|
+
}
|
|
12
|
+
if (Array.isArray(value)) {
|
|
13
|
+
for (const v of value) {
|
|
14
|
+
if (v !== null) {
|
|
15
|
+
searchParams.append(key, v);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
} else {
|
|
19
|
+
searchParams.append(key, value);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return searchParams;
|
|
23
|
+
}
|
|
24
|
+
var NavigationFailureType = /* @__PURE__ */ ((NavigationFailureType2) => {
|
|
25
|
+
NavigationFailureType2[NavigationFailureType2["aborted"] = 1] = "aborted";
|
|
26
|
+
NavigationFailureType2[NavigationFailureType2["cancelled"] = 2] = "cancelled";
|
|
27
|
+
NavigationFailureType2[NavigationFailureType2["duplicated"] = 4] = "duplicated";
|
|
28
|
+
return NavigationFailureType2;
|
|
29
|
+
})(NavigationFailureType || {});
|
|
30
|
+
|
|
31
|
+
// src/matcher.ts
|
|
32
|
+
var PARAM_RE = /^:(\w+)(\??)?(\.\.\.)?$/;
|
|
33
|
+
var WILDCARD_RE = /^\*$/;
|
|
34
|
+
function tokenizePath(path) {
|
|
35
|
+
const segments = path.split("/");
|
|
36
|
+
const tokens = [];
|
|
37
|
+
for (const segment of segments) {
|
|
38
|
+
if (!segment) continue;
|
|
39
|
+
const paramMatch = segment.match(PARAM_RE);
|
|
40
|
+
if (paramMatch) {
|
|
41
|
+
const [, name, optional, repeatable] = paramMatch;
|
|
42
|
+
if (name) {
|
|
43
|
+
tokens.push({
|
|
44
|
+
type: "param",
|
|
45
|
+
name,
|
|
46
|
+
repeatable: repeatable === "...",
|
|
47
|
+
optional: optional === "?"
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (WILDCARD_RE.test(segment)) {
|
|
53
|
+
tokens.push({ type: "wildcard", value: "*" });
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
tokens.push({ type: "static", value: segment });
|
|
57
|
+
}
|
|
58
|
+
return tokens;
|
|
59
|
+
}
|
|
60
|
+
function scoreRoute(tokens) {
|
|
61
|
+
let score = 0;
|
|
62
|
+
for (const token of tokens) {
|
|
63
|
+
switch (token.type) {
|
|
64
|
+
case "static":
|
|
65
|
+
score += 3;
|
|
66
|
+
break;
|
|
67
|
+
case "param":
|
|
68
|
+
score += token.optional ? 1 : 2;
|
|
69
|
+
break;
|
|
70
|
+
case "wildcard":
|
|
71
|
+
score += 0;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return score;
|
|
76
|
+
}
|
|
77
|
+
function matchPath(pathname, tokens, strict = false) {
|
|
78
|
+
const pathSegments = pathname.split("/").filter(Boolean);
|
|
79
|
+
const params = {};
|
|
80
|
+
let matched = true;
|
|
81
|
+
let i = 0;
|
|
82
|
+
for (const token of tokens) {
|
|
83
|
+
if (i >= pathSegments.length) {
|
|
84
|
+
if (token.type === "param" && token.optional) continue;
|
|
85
|
+
if (token.type === "wildcard") continue;
|
|
86
|
+
matched = false;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
const segment = pathSegments[i];
|
|
90
|
+
switch (token.type) {
|
|
91
|
+
case "static":
|
|
92
|
+
if (segment !== token.value) {
|
|
93
|
+
matched = false;
|
|
94
|
+
}
|
|
95
|
+
i++;
|
|
96
|
+
break;
|
|
97
|
+
case "param":
|
|
98
|
+
if (token.repeatable) {
|
|
99
|
+
params[token.name] = pathSegments.slice(i);
|
|
100
|
+
i = pathSegments.length;
|
|
101
|
+
} else if (segment !== void 0) {
|
|
102
|
+
params[token.name] = segment;
|
|
103
|
+
i++;
|
|
104
|
+
}
|
|
105
|
+
break;
|
|
106
|
+
case "wildcard":
|
|
107
|
+
params[token.value] = pathSegments.slice(i).join("/");
|
|
108
|
+
i = pathSegments.length;
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
if (!matched) break;
|
|
112
|
+
}
|
|
113
|
+
if (matched && i < pathSegments.length) {
|
|
114
|
+
matched = false;
|
|
115
|
+
}
|
|
116
|
+
if (!strict && matched && pathSegments.length === 0 && tokens.length === 0) {
|
|
117
|
+
matched = true;
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
matched,
|
|
121
|
+
params,
|
|
122
|
+
path: "/" + pathSegments.slice(0, i).join("/"),
|
|
123
|
+
score: scoreRoute(tokens)
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function normalizeRouteRecord(record, parent) {
|
|
127
|
+
const tokens = tokenizePath(record.path);
|
|
128
|
+
const fullPath = parent ? parent.record.path + (record.path.startsWith("/") ? record.path : "/" + record.path) : record.path;
|
|
129
|
+
const normalized = {
|
|
130
|
+
path: fullPath,
|
|
131
|
+
name: record.name ?? null,
|
|
132
|
+
meta: record.meta ?? {},
|
|
133
|
+
children: [],
|
|
134
|
+
beforeEnter: record.beforeEnter,
|
|
135
|
+
props: record.props ?? false,
|
|
136
|
+
component: record.component,
|
|
137
|
+
components: "components" in record ? record.components : void 0
|
|
138
|
+
};
|
|
139
|
+
const matcher = {
|
|
140
|
+
record: normalized,
|
|
141
|
+
tokens,
|
|
142
|
+
score: scoreRoute(tokens),
|
|
143
|
+
children: [],
|
|
144
|
+
parent
|
|
145
|
+
};
|
|
146
|
+
if (record.children) {
|
|
147
|
+
for (const child of record.children) {
|
|
148
|
+
const childMatcher = normalizeRouteRecord(child, matcher);
|
|
149
|
+
matcher.children.push(childMatcher);
|
|
150
|
+
normalized.children.push(childMatcher.record);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return matcher;
|
|
154
|
+
}
|
|
155
|
+
function flattenMatchers(matchers) {
|
|
156
|
+
const result = [];
|
|
157
|
+
function walk(matcher, parentTokens = []) {
|
|
158
|
+
const combinedTokens = [...parentTokens, ...matcher.tokens];
|
|
159
|
+
const matcherWithCombinedTokens = {
|
|
160
|
+
...matcher,
|
|
161
|
+
tokens: combinedTokens
|
|
162
|
+
};
|
|
163
|
+
result.push(matcherWithCombinedTokens);
|
|
164
|
+
for (const child of matcher.children) {
|
|
165
|
+
walk(child, combinedTokens);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
for (const matcher of matchers) {
|
|
169
|
+
walk(matcher);
|
|
170
|
+
}
|
|
171
|
+
return result;
|
|
172
|
+
}
|
|
173
|
+
function parseQuery(queryString) {
|
|
174
|
+
const query = {};
|
|
175
|
+
if (!queryString) return query;
|
|
176
|
+
const search = queryString.startsWith("?") ? queryString.slice(1) : queryString;
|
|
177
|
+
const pairs = search.split("&");
|
|
178
|
+
for (const pair of pairs) {
|
|
179
|
+
if (!pair) continue;
|
|
180
|
+
const [key, value] = pair.split("=");
|
|
181
|
+
const decodedKey = decodeURIComponent(key);
|
|
182
|
+
const decodedValue = value !== void 0 ? decodeURIComponent(value) : void 0;
|
|
183
|
+
if (decodedKey in query) {
|
|
184
|
+
const existing = query[decodedKey];
|
|
185
|
+
if (Array.isArray(existing)) {
|
|
186
|
+
if (decodedValue !== void 0) {
|
|
187
|
+
existing.push(decodedValue);
|
|
188
|
+
}
|
|
189
|
+
} else if (decodedValue !== void 0) {
|
|
190
|
+
if (existing === null) {
|
|
191
|
+
query[decodedKey] = [decodedValue];
|
|
192
|
+
} else {
|
|
193
|
+
query[decodedKey] = [existing, decodedValue];
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
} else if (decodedValue !== void 0) {
|
|
197
|
+
query[decodedKey] = decodedValue;
|
|
198
|
+
} else {
|
|
199
|
+
query[decodedKey] = null;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return query;
|
|
203
|
+
}
|
|
204
|
+
function stringifyQuery(query) {
|
|
205
|
+
const pairs = [];
|
|
206
|
+
for (const [key, value] of Object.entries(query)) {
|
|
207
|
+
if (value === null || value === void 0) {
|
|
208
|
+
pairs.push(encodeURIComponent(key));
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
if (Array.isArray(value)) {
|
|
212
|
+
for (const v of value) {
|
|
213
|
+
pairs.push(`${encodeURIComponent(key)}=${v === null ? "" : encodeURIComponent(v)}`);
|
|
214
|
+
}
|
|
215
|
+
} else {
|
|
216
|
+
pairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return pairs.length ? `?${pairs.join("&")}` : "";
|
|
220
|
+
}
|
|
221
|
+
function resolveFullPath(path, query = {}, hash = "") {
|
|
222
|
+
const queryString = stringifyQuery(query);
|
|
223
|
+
const hashString = hash.startsWith("#") ? hash : hash ? `#${hash}` : "";
|
|
224
|
+
return `${path}${queryString}${hashString}`;
|
|
225
|
+
}
|
|
226
|
+
function parseFullPath(fullPath) {
|
|
227
|
+
const hashIndex = fullPath.indexOf("#");
|
|
228
|
+
const queryIndex = fullPath.indexOf("?");
|
|
229
|
+
let path;
|
|
230
|
+
let queryString = "";
|
|
231
|
+
let hash = "";
|
|
232
|
+
if (hashIndex > -1) {
|
|
233
|
+
hash = fullPath.slice(hashIndex);
|
|
234
|
+
path = fullPath.slice(0, hashIndex);
|
|
235
|
+
} else {
|
|
236
|
+
path = fullPath;
|
|
237
|
+
}
|
|
238
|
+
if (queryIndex > -1) {
|
|
239
|
+
queryString = path.slice(queryIndex);
|
|
240
|
+
path = path.slice(0, queryIndex);
|
|
241
|
+
}
|
|
242
|
+
return {
|
|
243
|
+
path: path || "/",
|
|
244
|
+
query: parseQuery(queryString),
|
|
245
|
+
hash: hash.replace(/^#/, "")
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// src/location.ts
|
|
250
|
+
function resolveLocation(raw, currentLocation, router) {
|
|
251
|
+
if (typeof raw === "string") {
|
|
252
|
+
const { path: path2, query: query2, hash: hash2 } = parseFullPath(raw);
|
|
253
|
+
return { path: path2, query: query2, hash: hash2 };
|
|
254
|
+
}
|
|
255
|
+
const { name, path, query = {}, hash = "", params } = raw;
|
|
256
|
+
if (name !== void 0 && name !== null && router) {
|
|
257
|
+
const resolved = router.resolveName(name, params);
|
|
258
|
+
if (resolved) {
|
|
259
|
+
return {
|
|
260
|
+
path: resolved.path,
|
|
261
|
+
query,
|
|
262
|
+
hash,
|
|
263
|
+
params
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
if (path !== void 0) {
|
|
268
|
+
const { path: parsedPath, query: parsedQuery, hash: parsedHash } = parseFullPath(path);
|
|
269
|
+
return {
|
|
270
|
+
path: parsedPath,
|
|
271
|
+
query: { ...parsedQuery, ...query },
|
|
272
|
+
hash: hash || parsedHash,
|
|
273
|
+
params
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
return {
|
|
277
|
+
path: currentLocation?.path || "/",
|
|
278
|
+
query,
|
|
279
|
+
hash,
|
|
280
|
+
params
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
function isSameRouteLocation(a, b) {
|
|
284
|
+
if (!a || !b) return false;
|
|
285
|
+
return a.path === b.path && a.hash === b.hash && JSON.stringify(a.query) === JSON.stringify(b.query);
|
|
286
|
+
}
|
|
287
|
+
function createRouteLocation(path, query = {}, hash = "", params = {}) {
|
|
288
|
+
return {
|
|
289
|
+
name: null,
|
|
290
|
+
path,
|
|
291
|
+
fullPath: resolveFullPath(path, query, hash),
|
|
292
|
+
query,
|
|
293
|
+
hash,
|
|
294
|
+
params,
|
|
295
|
+
matched: [],
|
|
296
|
+
meta: {}
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// src/composables/useRouter.ts
|
|
301
|
+
var currentRouter = null;
|
|
302
|
+
function setCurrentRouter(router) {
|
|
303
|
+
currentRouter = router;
|
|
304
|
+
}
|
|
305
|
+
function useRouter() {
|
|
306
|
+
if (!currentRouter) {
|
|
307
|
+
throw new Error(
|
|
308
|
+
"[@lytjs/router] No active router instance. Make sure to call app.use(router) before using useRouter()."
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
return currentRouter;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// src/router.ts
|
|
315
|
+
function createRouter(options) {
|
|
316
|
+
const matchers = options.routes.map((route) => normalizeRouteRecord(route));
|
|
317
|
+
const flatMatchers = flattenMatchers(matchers);
|
|
318
|
+
const currentRoute = reactivity.signal({
|
|
319
|
+
name: null,
|
|
320
|
+
path: "/",
|
|
321
|
+
fullPath: "/",
|
|
322
|
+
query: {},
|
|
323
|
+
hash: "",
|
|
324
|
+
params: {},
|
|
325
|
+
matched: [],
|
|
326
|
+
meta: {}
|
|
327
|
+
});
|
|
328
|
+
const beforeEachGuards = [];
|
|
329
|
+
const beforeResolveGuards = [];
|
|
330
|
+
const afterEachHooks = [];
|
|
331
|
+
let readyResolve = null;
|
|
332
|
+
const readyPromise = new Promise((resolve) => {
|
|
333
|
+
readyResolve = resolve;
|
|
334
|
+
});
|
|
335
|
+
let pendingNavigation = null;
|
|
336
|
+
function resolveRoute(path) {
|
|
337
|
+
let bestMatch = null;
|
|
338
|
+
let bestScore = -1;
|
|
339
|
+
let bestParams = {};
|
|
340
|
+
for (const matcher of flatMatchers) {
|
|
341
|
+
const result = matchPath(path, matcher.tokens, options.strict);
|
|
342
|
+
if (result.matched && result.score > bestScore) {
|
|
343
|
+
bestScore = result.score;
|
|
344
|
+
bestMatch = matcher;
|
|
345
|
+
bestParams = result.params;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (!bestMatch) {
|
|
349
|
+
return { matched: [], params: {}, meta: {} };
|
|
350
|
+
}
|
|
351
|
+
const matched = [];
|
|
352
|
+
let current = bestMatch;
|
|
353
|
+
while (current) {
|
|
354
|
+
matched.unshift(current);
|
|
355
|
+
current = current.parent;
|
|
356
|
+
}
|
|
357
|
+
const meta = {};
|
|
358
|
+
for (const m of matched) {
|
|
359
|
+
Object.assign(meta, m.record.meta);
|
|
360
|
+
}
|
|
361
|
+
return { matched, params: bestParams, meta };
|
|
362
|
+
}
|
|
363
|
+
function resolveName(name, params) {
|
|
364
|
+
for (const matcher of flatMatchers) {
|
|
365
|
+
if (matcher.record.name === name) {
|
|
366
|
+
let path = matcher.record.path;
|
|
367
|
+
if (params) {
|
|
368
|
+
for (const [key, value] of Object.entries(params)) {
|
|
369
|
+
const paramValue = Array.isArray(value) ? value.join("/") : value;
|
|
370
|
+
path = path.replace(`:${key}`, paramValue);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return { path };
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
return null;
|
|
377
|
+
}
|
|
378
|
+
async function navigate(to, replace = false) {
|
|
379
|
+
const resolved = resolveLocation(to, currentRoute(), { resolveName });
|
|
380
|
+
const targetPath = resolved.path;
|
|
381
|
+
if (typeof to !== "string" && to.name !== null && to.name !== void 0 && !resolveName(to.name, to.params)) {
|
|
382
|
+
return {
|
|
383
|
+
type: 1 /* aborted */,
|
|
384
|
+
from: currentRoute(),
|
|
385
|
+
to: currentRoute()
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
const { matched, params, meta } = resolveRoute(targetPath);
|
|
389
|
+
if (matched.length === 0) {
|
|
390
|
+
return {
|
|
391
|
+
type: 1 /* aborted */,
|
|
392
|
+
from: currentRoute(),
|
|
393
|
+
to: currentRoute()
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
const lastMatched = matched[matched.length - 1];
|
|
397
|
+
const targetLocation = {
|
|
398
|
+
name: lastMatched?.record?.name ?? null,
|
|
399
|
+
path: targetPath,
|
|
400
|
+
fullPath: resolved.path + (Object.keys(resolved.query).length ? "?" + locationQueryToSearchParams(resolved.query).toString() : "") + (resolved.hash ? `#${resolved.hash}` : ""),
|
|
401
|
+
query: resolved.query,
|
|
402
|
+
hash: resolved.hash,
|
|
403
|
+
params,
|
|
404
|
+
matched: matched.map((m) => m.record),
|
|
405
|
+
meta
|
|
406
|
+
};
|
|
407
|
+
if (isSameRouteLocation(currentRoute(), targetLocation)) {
|
|
408
|
+
return {
|
|
409
|
+
type: 4 /* duplicated */,
|
|
410
|
+
from: currentRoute(),
|
|
411
|
+
to: targetLocation
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
const from = currentRoute();
|
|
415
|
+
const fromMatched = currentRoute().matched.map((record) => {
|
|
416
|
+
return flatMatchers.find((m) => m.record === record);
|
|
417
|
+
}).filter((m) => m !== void 0);
|
|
418
|
+
const failure = await runGuardSequence(targetLocation, from, matched, fromMatched);
|
|
419
|
+
if (failure) {
|
|
420
|
+
return failure;
|
|
421
|
+
}
|
|
422
|
+
if (replace) {
|
|
423
|
+
await options.history.replace(targetLocation);
|
|
424
|
+
} else {
|
|
425
|
+
await options.history.push(targetLocation);
|
|
426
|
+
}
|
|
427
|
+
currentRoute.set(targetLocation);
|
|
428
|
+
if (options.scrollBehavior) {
|
|
429
|
+
const savedPosition = replace ? null : null;
|
|
430
|
+
const scrollPosition = options.scrollBehavior(targetLocation, from, savedPosition);
|
|
431
|
+
if (scrollPosition) {
|
|
432
|
+
if (typeof window !== "undefined") {
|
|
433
|
+
window.scrollTo(scrollPosition);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
for (const hook of afterEachHooks) {
|
|
438
|
+
hook(targetLocation, from);
|
|
439
|
+
}
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
async function runGuardSequence(to, from, matched, fromMatched) {
|
|
443
|
+
for (const matcher of fromMatched) {
|
|
444
|
+
const component = matcher.record.component;
|
|
445
|
+
if (component && component.beforeRouteLeave) {
|
|
446
|
+
const result = await component.beforeRouteLeave(to, from, noop);
|
|
447
|
+
if (result === false || result instanceof Error) {
|
|
448
|
+
return { type: 1 /* aborted */, from, to };
|
|
449
|
+
}
|
|
450
|
+
if (typeof result === "string" || typeof result === "object" && result !== null) {
|
|
451
|
+
return navigate(result, true);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
for (const guard of beforeEachGuards) {
|
|
456
|
+
const result = await guard(to, from, noop);
|
|
457
|
+
if (result === false || result instanceof Error) {
|
|
458
|
+
return {
|
|
459
|
+
type: 1 /* aborted */,
|
|
460
|
+
from,
|
|
461
|
+
to
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
if (typeof result === "string" || typeof result === "object" && result !== null) {
|
|
465
|
+
return navigate(result, true);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
for (const matcher of matched) {
|
|
469
|
+
if (matcher.record.beforeEnter) {
|
|
470
|
+
const result = await matcher.record.beforeEnter(to, from, noop);
|
|
471
|
+
if (result === false || result instanceof Error) {
|
|
472
|
+
return {
|
|
473
|
+
type: 1 /* aborted */,
|
|
474
|
+
from,
|
|
475
|
+
to
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
if (typeof result === "string" || typeof result === "object" && result !== null) {
|
|
479
|
+
return navigate(result, true);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
for (const guard of beforeResolveGuards) {
|
|
484
|
+
const result = await guard(to, from, noop);
|
|
485
|
+
if (result === false || result instanceof Error) {
|
|
486
|
+
return {
|
|
487
|
+
type: 1 /* aborted */,
|
|
488
|
+
from,
|
|
489
|
+
to
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
if (typeof result === "string" || typeof result === "object" && result !== null) {
|
|
493
|
+
return navigate(result, true);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
function noop() {
|
|
499
|
+
}
|
|
500
|
+
options.history.listen((to, _from, info) => {
|
|
501
|
+
if (info.type === "pop") {
|
|
502
|
+
const { matched, params, meta } = resolveRoute(to.path);
|
|
503
|
+
const newLocation = {
|
|
504
|
+
...to,
|
|
505
|
+
params,
|
|
506
|
+
matched: matched.map((m) => m.record),
|
|
507
|
+
meta: { ...meta, ...to.meta }
|
|
508
|
+
};
|
|
509
|
+
currentRoute.set(newLocation);
|
|
510
|
+
}
|
|
511
|
+
});
|
|
512
|
+
const router = {
|
|
513
|
+
currentRoute,
|
|
514
|
+
options,
|
|
515
|
+
resolveName,
|
|
516
|
+
async push(to) {
|
|
517
|
+
if (pendingNavigation) return pendingNavigation;
|
|
518
|
+
try {
|
|
519
|
+
pendingNavigation = navigate(to, false);
|
|
520
|
+
return await pendingNavigation;
|
|
521
|
+
} finally {
|
|
522
|
+
pendingNavigation = null;
|
|
523
|
+
}
|
|
524
|
+
},
|
|
525
|
+
async replace(to) {
|
|
526
|
+
if (pendingNavigation) return pendingNavigation;
|
|
527
|
+
try {
|
|
528
|
+
pendingNavigation = navigate(to, true);
|
|
529
|
+
return await pendingNavigation;
|
|
530
|
+
} finally {
|
|
531
|
+
pendingNavigation = null;
|
|
532
|
+
}
|
|
533
|
+
},
|
|
534
|
+
go(delta) {
|
|
535
|
+
options.history.go(delta);
|
|
536
|
+
},
|
|
537
|
+
back() {
|
|
538
|
+
this.go(-1);
|
|
539
|
+
},
|
|
540
|
+
forward() {
|
|
541
|
+
this.go(1);
|
|
542
|
+
},
|
|
543
|
+
beforeEach(guard) {
|
|
544
|
+
beforeEachGuards.push(guard);
|
|
545
|
+
return () => {
|
|
546
|
+
const index = beforeEachGuards.indexOf(guard);
|
|
547
|
+
if (index > -1) beforeEachGuards.splice(index, 1);
|
|
548
|
+
};
|
|
549
|
+
},
|
|
550
|
+
afterEach(hook) {
|
|
551
|
+
afterEachHooks.push(hook);
|
|
552
|
+
return () => {
|
|
553
|
+
const index = afterEachHooks.indexOf(hook);
|
|
554
|
+
if (index > -1) afterEachHooks.splice(index, 1);
|
|
555
|
+
};
|
|
556
|
+
},
|
|
557
|
+
beforeResolve(guard) {
|
|
558
|
+
beforeResolveGuards.push(guard);
|
|
559
|
+
return () => {
|
|
560
|
+
const index = beforeResolveGuards.indexOf(guard);
|
|
561
|
+
if (index > -1) beforeResolveGuards.splice(index, 1);
|
|
562
|
+
};
|
|
563
|
+
},
|
|
564
|
+
install(app) {
|
|
565
|
+
setCurrentRouter(router);
|
|
566
|
+
if (app.config?.globalProperties) {
|
|
567
|
+
app.config.globalProperties.$router = router;
|
|
568
|
+
}
|
|
569
|
+
if (app.provide) {
|
|
570
|
+
app.provide("__lytjs_router__", router);
|
|
571
|
+
}
|
|
572
|
+
const initialLocation = options.history.location;
|
|
573
|
+
const { matched, params, meta } = resolveRoute(initialLocation.path);
|
|
574
|
+
currentRoute.set({
|
|
575
|
+
...initialLocation,
|
|
576
|
+
params,
|
|
577
|
+
matched: matched.map((m) => m.record),
|
|
578
|
+
meta: { ...meta, ...initialLocation.meta }
|
|
579
|
+
});
|
|
580
|
+
readyResolve?.();
|
|
581
|
+
},
|
|
582
|
+
async isReady() {
|
|
583
|
+
return readyPromise;
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
return router;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// src/history.ts
|
|
590
|
+
function createBaseHistory(base) {
|
|
591
|
+
const listeners = [];
|
|
592
|
+
const currentLocation = createRouteLocation(base);
|
|
593
|
+
return {
|
|
594
|
+
listeners,
|
|
595
|
+
currentLocation,
|
|
596
|
+
pushState(location, _data) {
|
|
597
|
+
currentLocation.path = location.path;
|
|
598
|
+
currentLocation.fullPath = location.fullPath;
|
|
599
|
+
currentLocation.query = location.query;
|
|
600
|
+
currentLocation.hash = location.hash;
|
|
601
|
+
currentLocation.params = location.params;
|
|
602
|
+
currentLocation.matched = location.matched;
|
|
603
|
+
currentLocation.meta = location.meta;
|
|
604
|
+
currentLocation.name = location.name;
|
|
605
|
+
},
|
|
606
|
+
replaceState(location, _data) {
|
|
607
|
+
currentLocation.path = location.path;
|
|
608
|
+
currentLocation.fullPath = location.fullPath;
|
|
609
|
+
currentLocation.query = location.query;
|
|
610
|
+
currentLocation.hash = location.hash;
|
|
611
|
+
currentLocation.params = location.params;
|
|
612
|
+
currentLocation.matched = location.matched;
|
|
613
|
+
currentLocation.meta = location.meta;
|
|
614
|
+
currentLocation.name = location.name;
|
|
615
|
+
},
|
|
616
|
+
triggerListeners(to, from, info) {
|
|
617
|
+
for (const listener of listeners) {
|
|
618
|
+
listener(to, from, info);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
function createWebHistory(base = "") {
|
|
624
|
+
const normalizedBase = base.startsWith("/") ? base : `/${base}`;
|
|
625
|
+
const { listeners, currentLocation, pushState, replaceState, triggerListeners } = createBaseHistory(normalizedBase);
|
|
626
|
+
function getCurrentUrl() {
|
|
627
|
+
const { pathname, search, hash: hash2 } = window.location;
|
|
628
|
+
const path2 = pathname.startsWith(normalizedBase) ? pathname.slice(normalizedBase.length) || "/" : pathname;
|
|
629
|
+
return `${path2}${search}${hash2}`;
|
|
630
|
+
}
|
|
631
|
+
const initialUrl = getCurrentUrl();
|
|
632
|
+
const { path, query, hash } = parseFullPath(initialUrl);
|
|
633
|
+
currentLocation.path = path;
|
|
634
|
+
currentLocation.fullPath = initialUrl;
|
|
635
|
+
currentLocation.query = query;
|
|
636
|
+
currentLocation.hash = hash;
|
|
637
|
+
let popStateHandler = null;
|
|
638
|
+
function setupListeners() {
|
|
639
|
+
popStateHandler = (e) => {
|
|
640
|
+
const url = getCurrentUrl();
|
|
641
|
+
const { path: path2, query: query2, hash: hash2 } = parseFullPath(url);
|
|
642
|
+
const to = createRouteLocation(path2, query2, hash2);
|
|
643
|
+
const from = { ...currentLocation };
|
|
644
|
+
pushState(to, e.state);
|
|
645
|
+
triggerListeners(to, from, { type: "pop", direction: "unknown", delta: 0 });
|
|
646
|
+
};
|
|
647
|
+
window.addEventListener("popstate", popStateHandler);
|
|
648
|
+
}
|
|
649
|
+
function destroy() {
|
|
650
|
+
if (popStateHandler) {
|
|
651
|
+
window.removeEventListener("popstate", popStateHandler);
|
|
652
|
+
popStateHandler = null;
|
|
653
|
+
}
|
|
654
|
+
listeners.length = 0;
|
|
655
|
+
}
|
|
656
|
+
function push(to) {
|
|
657
|
+
const routeLocation = typeof to === "string" ? parseFullPath(to) : to;
|
|
658
|
+
const path2 = routeLocation.path || "/";
|
|
659
|
+
const query2 = routeLocation.query || {};
|
|
660
|
+
const hash2 = routeLocation.hash || "";
|
|
661
|
+
const fullPath = resolveFullPath(path2, query2, hash2);
|
|
662
|
+
const from = { ...currentLocation };
|
|
663
|
+
const location = createRouteLocation(path2, query2, hash2);
|
|
664
|
+
pushState(location);
|
|
665
|
+
window.history.pushState(
|
|
666
|
+
{ ...window.history.state, current: fullPath },
|
|
667
|
+
"",
|
|
668
|
+
`${normalizedBase}${fullPath}`
|
|
669
|
+
);
|
|
670
|
+
triggerListeners(location, from, { type: "push", direction: "forward", delta: 1 });
|
|
671
|
+
return Promise.resolve();
|
|
672
|
+
}
|
|
673
|
+
function replace(to) {
|
|
674
|
+
const routeLocation = typeof to === "string" ? parseFullPath(to) : to;
|
|
675
|
+
const path2 = routeLocation.path || "/";
|
|
676
|
+
const query2 = routeLocation.query || {};
|
|
677
|
+
const hash2 = routeLocation.hash || "";
|
|
678
|
+
const fullPath = resolveFullPath(path2, query2, hash2);
|
|
679
|
+
const from = { ...currentLocation };
|
|
680
|
+
const location = createRouteLocation(path2, query2, hash2);
|
|
681
|
+
replaceState(location);
|
|
682
|
+
window.history.replaceState(
|
|
683
|
+
{ ...window.history.state, current: fullPath },
|
|
684
|
+
"",
|
|
685
|
+
`${normalizedBase}${fullPath}`
|
|
686
|
+
);
|
|
687
|
+
triggerListeners(location, from, { type: "replace", direction: "unknown", delta: 0 });
|
|
688
|
+
return Promise.resolve();
|
|
689
|
+
}
|
|
690
|
+
function go(delta) {
|
|
691
|
+
window.history.go(delta);
|
|
692
|
+
}
|
|
693
|
+
function listen(callback) {
|
|
694
|
+
listeners.push(callback);
|
|
695
|
+
return () => {
|
|
696
|
+
const index = listeners.indexOf(callback);
|
|
697
|
+
if (index > -1) listeners.splice(index, 1);
|
|
698
|
+
};
|
|
699
|
+
}
|
|
700
|
+
setupListeners();
|
|
701
|
+
return {
|
|
702
|
+
get location() {
|
|
703
|
+
return currentLocation;
|
|
704
|
+
},
|
|
705
|
+
get state() {
|
|
706
|
+
return window.history.state;
|
|
707
|
+
},
|
|
708
|
+
base: normalizedBase,
|
|
709
|
+
push,
|
|
710
|
+
replace,
|
|
711
|
+
go,
|
|
712
|
+
listen,
|
|
713
|
+
destroy
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
function createWebHashHistory(base = "") {
|
|
717
|
+
const normalizedBase = base.startsWith("/") ? base : `/${base}`;
|
|
718
|
+
const { listeners, currentLocation, pushState, replaceState, triggerListeners } = createBaseHistory(normalizedBase);
|
|
719
|
+
function getHash() {
|
|
720
|
+
const href = window.location.href;
|
|
721
|
+
const hashIndex = href.indexOf("#");
|
|
722
|
+
return hashIndex === -1 ? "" : href.slice(hashIndex + 1);
|
|
723
|
+
}
|
|
724
|
+
function getCurrentUrl() {
|
|
725
|
+
const hash2 = getHash();
|
|
726
|
+
if (normalizedBase !== "/" && hash2.startsWith(normalizedBase)) {
|
|
727
|
+
return hash2.slice(normalizedBase.length) || "/";
|
|
728
|
+
}
|
|
729
|
+
return hash2 || "/";
|
|
730
|
+
}
|
|
731
|
+
const initialUrl = getCurrentUrl();
|
|
732
|
+
const { path, query, hash } = parseFullPath(initialUrl);
|
|
733
|
+
currentLocation.path = path;
|
|
734
|
+
currentLocation.fullPath = initialUrl;
|
|
735
|
+
currentLocation.query = query;
|
|
736
|
+
currentLocation.hash = hash;
|
|
737
|
+
let hashChangeHandler = null;
|
|
738
|
+
function setupListeners() {
|
|
739
|
+
hashChangeHandler = () => {
|
|
740
|
+
const url = getCurrentUrl();
|
|
741
|
+
const { path: path2, query: query2, hash: hash2 } = parseFullPath(url);
|
|
742
|
+
const to = createRouteLocation(path2, query2, hash2);
|
|
743
|
+
const from = { ...currentLocation };
|
|
744
|
+
pushState(to);
|
|
745
|
+
triggerListeners(to, from, { type: "pop", direction: "unknown", delta: 0 });
|
|
746
|
+
};
|
|
747
|
+
window.addEventListener("hashchange", hashChangeHandler);
|
|
748
|
+
}
|
|
749
|
+
function destroy() {
|
|
750
|
+
if (hashChangeHandler) {
|
|
751
|
+
window.removeEventListener("hashchange", hashChangeHandler);
|
|
752
|
+
hashChangeHandler = null;
|
|
753
|
+
}
|
|
754
|
+
listeners.length = 0;
|
|
755
|
+
}
|
|
756
|
+
function push(to) {
|
|
757
|
+
const routeLocation = typeof to === "string" ? parseFullPath(to) : to;
|
|
758
|
+
const path2 = routeLocation.path || "/";
|
|
759
|
+
const query2 = routeLocation.query || {};
|
|
760
|
+
const hash2 = routeLocation.hash || "";
|
|
761
|
+
const fullPath = resolveFullPath(path2, query2, hash2);
|
|
762
|
+
const from = { ...currentLocation };
|
|
763
|
+
const location = createRouteLocation(path2, query2, hash2);
|
|
764
|
+
pushState(location);
|
|
765
|
+
window.location.hash = `${normalizedBase}${fullPath}`;
|
|
766
|
+
triggerListeners(location, from, { type: "push", direction: "forward", delta: 1 });
|
|
767
|
+
return Promise.resolve();
|
|
768
|
+
}
|
|
769
|
+
function replace(to) {
|
|
770
|
+
const routeLocation = typeof to === "string" ? parseFullPath(to) : to;
|
|
771
|
+
const path2 = routeLocation.path || "/";
|
|
772
|
+
const query2 = routeLocation.query || {};
|
|
773
|
+
const hash2 = routeLocation.hash || "";
|
|
774
|
+
const fullPath = resolveFullPath(path2, query2, hash2);
|
|
775
|
+
const from = { ...currentLocation };
|
|
776
|
+
const location = createRouteLocation(path2, query2, hash2);
|
|
777
|
+
replaceState(location);
|
|
778
|
+
const url = window.location.href;
|
|
779
|
+
const hashIndex = url.indexOf("#");
|
|
780
|
+
const base2 = hashIndex === -1 ? url : url.slice(0, hashIndex);
|
|
781
|
+
window.location.replace(`${base2}#${normalizedBase}${fullPath}`);
|
|
782
|
+
triggerListeners(location, from, { type: "replace", direction: "unknown", delta: 0 });
|
|
783
|
+
return Promise.resolve();
|
|
784
|
+
}
|
|
785
|
+
function go(delta) {
|
|
786
|
+
window.history.go(delta);
|
|
787
|
+
}
|
|
788
|
+
function listen(callback) {
|
|
789
|
+
listeners.push(callback);
|
|
790
|
+
return () => {
|
|
791
|
+
const index = listeners.indexOf(callback);
|
|
792
|
+
if (index > -1) listeners.splice(index, 1);
|
|
793
|
+
};
|
|
794
|
+
}
|
|
795
|
+
setupListeners();
|
|
796
|
+
return {
|
|
797
|
+
get location() {
|
|
798
|
+
return currentLocation;
|
|
799
|
+
},
|
|
800
|
+
get state() {
|
|
801
|
+
return null;
|
|
802
|
+
},
|
|
803
|
+
base: normalizedBase,
|
|
804
|
+
push,
|
|
805
|
+
replace,
|
|
806
|
+
go,
|
|
807
|
+
listen,
|
|
808
|
+
destroy
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
function createMemoryHistory(initial = "/") {
|
|
812
|
+
const { listeners, currentLocation, pushState, replaceState, triggerListeners } = createBaseHistory("/");
|
|
813
|
+
const entries = [initial];
|
|
814
|
+
let currentIndex = 0;
|
|
815
|
+
const { path, query, hash } = parseFullPath(initial);
|
|
816
|
+
currentLocation.path = path;
|
|
817
|
+
currentLocation.fullPath = initial;
|
|
818
|
+
currentLocation.query = query;
|
|
819
|
+
currentLocation.hash = hash;
|
|
820
|
+
function push(to) {
|
|
821
|
+
const { path: path2, query: query2, hash: hash2 } = typeof to === "string" ? parseFullPath(to) : { path: to.path || "/", query: to.query || {}, hash: to.hash || "" };
|
|
822
|
+
const fullPath = resolveFullPath(path2, query2, hash2);
|
|
823
|
+
const from = { ...currentLocation };
|
|
824
|
+
const location = createRouteLocation(path2, query2, hash2);
|
|
825
|
+
pushState(location);
|
|
826
|
+
entries.splice(currentIndex + 1);
|
|
827
|
+
entries.push(fullPath);
|
|
828
|
+
currentIndex = entries.length - 1;
|
|
829
|
+
triggerListeners(location, from, { type: "push", direction: "forward", delta: 1 });
|
|
830
|
+
return Promise.resolve();
|
|
831
|
+
}
|
|
832
|
+
function replace(to) {
|
|
833
|
+
const { path: path2, query: query2, hash: hash2 } = typeof to === "string" ? parseFullPath(to) : { path: to.path || "/", query: to.query || {}, hash: to.hash || "" };
|
|
834
|
+
const fullPath = resolveFullPath(path2, query2, hash2);
|
|
835
|
+
const from = { ...currentLocation };
|
|
836
|
+
const location = createRouteLocation(path2, query2, hash2);
|
|
837
|
+
replaceState(location);
|
|
838
|
+
entries[currentIndex] = fullPath;
|
|
839
|
+
triggerListeners(location, from, { type: "replace", direction: "unknown", delta: 0 });
|
|
840
|
+
return Promise.resolve();
|
|
841
|
+
}
|
|
842
|
+
function go(delta) {
|
|
843
|
+
const nextIndex = currentIndex + delta;
|
|
844
|
+
if (nextIndex >= 0 && nextIndex < entries.length) {
|
|
845
|
+
const from = { ...currentLocation };
|
|
846
|
+
currentIndex = nextIndex;
|
|
847
|
+
const { path: path2, query: query2, hash: hash2 } = parseFullPath(entries[currentIndex]);
|
|
848
|
+
const to = createRouteLocation(path2, query2, hash2);
|
|
849
|
+
pushState(to);
|
|
850
|
+
triggerListeners(to, from, {
|
|
851
|
+
type: "pop",
|
|
852
|
+
direction: delta < 0 ? "back" : "forward",
|
|
853
|
+
delta
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
function listen(callback) {
|
|
858
|
+
listeners.push(callback);
|
|
859
|
+
return () => {
|
|
860
|
+
const index = listeners.indexOf(callback);
|
|
861
|
+
if (index > -1) listeners.splice(index, 1);
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
function destroy() {
|
|
865
|
+
listeners.length = 0;
|
|
866
|
+
}
|
|
867
|
+
return {
|
|
868
|
+
get location() {
|
|
869
|
+
return currentLocation;
|
|
870
|
+
},
|
|
871
|
+
get state() {
|
|
872
|
+
return null;
|
|
873
|
+
},
|
|
874
|
+
base: "",
|
|
875
|
+
push,
|
|
876
|
+
replace,
|
|
877
|
+
go,
|
|
878
|
+
listen,
|
|
879
|
+
destroy
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
// src/composables/useRoute.ts
|
|
884
|
+
function useRoute() {
|
|
885
|
+
const router = useRouter();
|
|
886
|
+
return router.currentRoute;
|
|
887
|
+
}
|
|
888
|
+
function useLink(options) {
|
|
889
|
+
const router = useRouter();
|
|
890
|
+
const route = useRoute();
|
|
891
|
+
const targetLocation = reactivity.computedSignal(() => {
|
|
892
|
+
return resolveLocation(options.to, route());
|
|
893
|
+
});
|
|
894
|
+
const isActive = reactivity.computedSignal(() => {
|
|
895
|
+
const target = targetLocation();
|
|
896
|
+
return route().path.startsWith(target.path);
|
|
897
|
+
});
|
|
898
|
+
const isExactActive = reactivity.computedSignal(() => {
|
|
899
|
+
return isSameRouteLocation(route(), targetLocation());
|
|
900
|
+
});
|
|
901
|
+
const href = reactivity.computedSignal(() => {
|
|
902
|
+
const loc = targetLocation();
|
|
903
|
+
const path = loc.path;
|
|
904
|
+
const query = Object.keys(loc.query).length ? "?" + locationQueryToSearchParams(loc.query).toString() : "";
|
|
905
|
+
const hash = loc.hash ? `#${loc.hash}` : "";
|
|
906
|
+
return path + query + hash;
|
|
907
|
+
});
|
|
908
|
+
function navigate(e) {
|
|
909
|
+
if (e) {
|
|
910
|
+
if (e.ctrlKey || e.metaKey || e.altKey || e.shiftKey) return;
|
|
911
|
+
if (e.defaultPrevented) return;
|
|
912
|
+
e.preventDefault();
|
|
913
|
+
}
|
|
914
|
+
if (options.replace) {
|
|
915
|
+
router.replace(options.to);
|
|
916
|
+
} else {
|
|
917
|
+
router.push(options.to);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
return {
|
|
921
|
+
route: targetLocation,
|
|
922
|
+
href,
|
|
923
|
+
isActive,
|
|
924
|
+
isExactActive,
|
|
925
|
+
navigate
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
var RouterView = {
|
|
929
|
+
name: "RouterView",
|
|
930
|
+
props: {
|
|
931
|
+
name: {
|
|
932
|
+
type: String,
|
|
933
|
+
default: "default"
|
|
934
|
+
}
|
|
935
|
+
},
|
|
936
|
+
setup(props) {
|
|
937
|
+
const route = useRoute();
|
|
938
|
+
const matchedComponent = reactivity.computedSignal(() => {
|
|
939
|
+
const currentRoute = route();
|
|
940
|
+
const matched = currentRoute.matched;
|
|
941
|
+
for (const record of matched) {
|
|
942
|
+
if (record.components && record.components[props.name]) {
|
|
943
|
+
return { component: record.components[props.name], record };
|
|
944
|
+
}
|
|
945
|
+
if (record.component && props.name === "default") {
|
|
946
|
+
return { component: record.component, record };
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
return null;
|
|
950
|
+
});
|
|
951
|
+
return () => {
|
|
952
|
+
const matched = matchedComponent();
|
|
953
|
+
if (!matched) return null;
|
|
954
|
+
const { component, record } = matched;
|
|
955
|
+
const resolvedComponent = typeof component === "function" ? component() : component;
|
|
956
|
+
let routeProps = {};
|
|
957
|
+
const currentRoute = route();
|
|
958
|
+
if (record.props) {
|
|
959
|
+
if (typeof record.props === "function") {
|
|
960
|
+
routeProps = record.props(currentRoute);
|
|
961
|
+
} else if (typeof record.props === "object") {
|
|
962
|
+
routeProps = record.props;
|
|
963
|
+
} else if (record.props === true) {
|
|
964
|
+
routeProps = currentRoute.params;
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
if (resolvedComponent && typeof resolvedComponent === "object" && "setup" in resolvedComponent) {
|
|
968
|
+
return { ...resolvedComponent, props: { ...routeProps, ...resolvedComponent.props } };
|
|
969
|
+
}
|
|
970
|
+
return resolvedComponent;
|
|
971
|
+
};
|
|
972
|
+
}
|
|
973
|
+
};
|
|
974
|
+
var RouterLink = {
|
|
975
|
+
name: "RouterLink",
|
|
976
|
+
props: {
|
|
977
|
+
to: {
|
|
978
|
+
type: String,
|
|
979
|
+
required: true
|
|
980
|
+
},
|
|
981
|
+
replace: {
|
|
982
|
+
type: Boolean,
|
|
983
|
+
default: false
|
|
984
|
+
},
|
|
985
|
+
activeClass: {
|
|
986
|
+
type: String,
|
|
987
|
+
default: "router-link-active"
|
|
988
|
+
},
|
|
989
|
+
exactActiveClass: {
|
|
990
|
+
type: String,
|
|
991
|
+
default: "router-link-exact-active"
|
|
992
|
+
},
|
|
993
|
+
ariaCurrentValue: {
|
|
994
|
+
type: String,
|
|
995
|
+
default: "page"
|
|
996
|
+
}
|
|
997
|
+
},
|
|
998
|
+
setup(props, {
|
|
999
|
+
slots
|
|
1000
|
+
}) {
|
|
1001
|
+
const router = useRouter();
|
|
1002
|
+
const route = useRoute();
|
|
1003
|
+
const targetLocation = reactivity.computedSignal(() => {
|
|
1004
|
+
return resolveLocation(props.to, route());
|
|
1005
|
+
});
|
|
1006
|
+
const isActive = reactivity.computedSignal(() => {
|
|
1007
|
+
const target = targetLocation();
|
|
1008
|
+
return route().path.startsWith(target.path);
|
|
1009
|
+
});
|
|
1010
|
+
const isExactActive = reactivity.computedSignal(() => {
|
|
1011
|
+
return isSameRouteLocation(route(), targetLocation());
|
|
1012
|
+
});
|
|
1013
|
+
const href = reactivity.computedSignal(() => {
|
|
1014
|
+
const loc = targetLocation();
|
|
1015
|
+
const path = loc.path;
|
|
1016
|
+
const query = Object.keys(loc.query).length ? "?" + locationQueryToSearchParams(loc.query).toString() : "";
|
|
1017
|
+
const hash = loc.hash ? `#${loc.hash}` : "";
|
|
1018
|
+
return path + query + hash;
|
|
1019
|
+
});
|
|
1020
|
+
function handleClick(event) {
|
|
1021
|
+
if (event.ctrlKey || event.metaKey || event.altKey || event.shiftKey) return;
|
|
1022
|
+
if (event.defaultPrevented) return;
|
|
1023
|
+
if (event.button !== void 0 && event.button !== 0) return;
|
|
1024
|
+
event.preventDefault();
|
|
1025
|
+
if (props.replace) {
|
|
1026
|
+
router.replace(props.to);
|
|
1027
|
+
} else {
|
|
1028
|
+
router.push(props.to);
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
return () => {
|
|
1032
|
+
const classes = [];
|
|
1033
|
+
if (isActive()) classes.push(props.activeClass);
|
|
1034
|
+
if (isExactActive()) classes.push(props.exactActiveClass);
|
|
1035
|
+
const linkData = {
|
|
1036
|
+
tag: "a",
|
|
1037
|
+
props: {
|
|
1038
|
+
href: href(),
|
|
1039
|
+
class: classes.join(" "),
|
|
1040
|
+
"aria-current": isExactActive() ? props.ariaCurrentValue : void 0,
|
|
1041
|
+
onClick: handleClick
|
|
1042
|
+
},
|
|
1043
|
+
children: slots?.default ? slots.default({ isActive: isActive(), isExactActive: isExactActive() }) : null
|
|
1044
|
+
};
|
|
1045
|
+
return linkData;
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
1048
|
+
};
|
|
1049
|
+
|
|
1050
|
+
exports.NavigationFailureType = NavigationFailureType;
|
|
1051
|
+
exports.RouterLink = RouterLink;
|
|
1052
|
+
exports.RouterView = RouterView;
|
|
1053
|
+
exports.createMemoryHistory = createMemoryHistory;
|
|
1054
|
+
exports.createRouter = createRouter;
|
|
1055
|
+
exports.createWebHashHistory = createWebHashHistory;
|
|
1056
|
+
exports.createWebHistory = createWebHistory;
|
|
1057
|
+
exports.setCurrentRouter = setCurrentRouter;
|
|
1058
|
+
exports.useLink = useLink;
|
|
1059
|
+
exports.useRoute = useRoute;
|
|
1060
|
+
exports.useRouter = useRouter;
|
|
1061
|
+
//# sourceMappingURL=index.cjs.map
|
|
1062
|
+
//# sourceMappingURL=index.cjs.map
|