svelte-spa-history-router 2.1.1 → 2.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/README.md +6 -2
- package/example/dist/Query.js +36 -13
- package/example/dist/Query.js.map +1 -1
- package/package.json +2 -1
- package/src/index.d.ts +40 -0
- package/src/link.js +7 -1
- package/src/push.js +6 -0
- package/src/stores.js +21 -5
- package/src/types.ts +6 -0
package/README.md
CHANGED
|
@@ -24,7 +24,7 @@ $ yarn add svelte-spa-history-router
|
|
|
24
24
|
|
|
25
25
|
## Usage
|
|
26
26
|
|
|
27
|
-
Import `
|
|
27
|
+
Import `Router` and put into your main component (typically App.svelte).
|
|
28
28
|
|
|
29
29
|
For example:
|
|
30
30
|
|
|
@@ -184,9 +184,13 @@ store to detect URL changes (including query string or hash)
|
|
|
184
184
|
|
|
185
185
|
## ChangeLog
|
|
186
186
|
|
|
187
|
+
### 2.1.2 (2024-04-29)
|
|
188
|
+
|
|
189
|
+
* Support types [PR10](https://github.com/ykrods/svelte-spa-history-router/pull/10)
|
|
190
|
+
|
|
187
191
|
### 2.1.1 (2024-01-13)
|
|
188
192
|
|
|
189
|
-
* Support Types
|
|
193
|
+
* ~~Support Types~~ Add typecheck [PR9](https://github.com/ykrods/svelte-spa-history-router/pull/9)
|
|
190
194
|
|
|
191
195
|
### 2.1.0 (2021-04-29)
|
|
192
196
|
|
package/example/dist/Query.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { S as SvelteComponentDev, i as init, s as safe_not_equal, d as dispatch_dev, v as validate_store, y as currentURL, c as component_subscribe, a as validate_slots, l as link, b as space, e as element, t as text, h as attr_dev, f as add_location, j as insert_dev, k as append_dev, p as action_destroyer, m as set_data_dev, n as noop, o as detach_dev, z as run_all } from './main2.js';
|
|
1
|
+
import { S as SvelteComponentDev, i as init, s as safe_not_equal, d as dispatch_dev, v as validate_store, y as currentURL, c as component_subscribe, a as validate_slots, l as link, g as globals, b as space, e as element, t as text, h as attr_dev, f as add_location, j as insert_dev, k as append_dev, p as action_destroyer, m as set_data_dev, n as noop, o as detach_dev, z as run_all } from './main2.js';
|
|
2
2
|
|
|
3
3
|
/* Query.svelte generated by Svelte v4.2.8 */
|
|
4
|
+
|
|
5
|
+
const { console: console_1 } = globals;
|
|
4
6
|
const file = "Query.svelte";
|
|
5
7
|
|
|
6
8
|
function create_fragment(ctx) {
|
|
@@ -33,14 +35,14 @@ function create_fragment(ctx) {
|
|
|
33
35
|
a1.textContent = "bar";
|
|
34
36
|
document.title = "query";
|
|
35
37
|
attr_dev(span, "id", "name");
|
|
36
|
-
add_location(span, file,
|
|
37
|
-
add_location(p, file,
|
|
38
|
+
add_location(span, file, 14, 11, 311);
|
|
39
|
+
add_location(p, file, 14, 2, 302);
|
|
38
40
|
attr_dev(a0, "href", "/query?name=foo");
|
|
39
|
-
add_location(a0, file,
|
|
41
|
+
add_location(a0, file, 15, 2, 349);
|
|
40
42
|
attr_dev(a1, "href", "/query?name=bar");
|
|
41
|
-
add_location(a1, file,
|
|
43
|
+
add_location(a1, file, 16, 2, 394);
|
|
42
44
|
attr_dev(div, "class", "query");
|
|
43
|
-
add_location(div, file,
|
|
45
|
+
add_location(div, file, 13, 0, 280);
|
|
44
46
|
},
|
|
45
47
|
l: function claim(nodes) {
|
|
46
48
|
throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option");
|
|
@@ -97,18 +99,31 @@ function instance($$self, $$props, $$invalidate) {
|
|
|
97
99
|
let name;
|
|
98
100
|
let $currentURL;
|
|
99
101
|
validate_store(currentURL, 'currentURL');
|
|
100
|
-
component_subscribe($$self, currentURL, $$value => $$invalidate(
|
|
102
|
+
component_subscribe($$self, currentURL, $$value => $$invalidate(2, $currentURL = $$value));
|
|
101
103
|
let { $$slots: slots = {}, $$scope } = $$props;
|
|
102
104
|
validate_slots('Query', slots, []);
|
|
103
|
-
|
|
105
|
+
let { article = null } = $$props;
|
|
106
|
+
console.log(article);
|
|
107
|
+
const writable_props = ['article'];
|
|
104
108
|
|
|
105
109
|
Object.keys($$props).forEach(key => {
|
|
106
|
-
if (!~writable_props.indexOf(key) && key.slice(0, 2) !== '$$' && key !== 'slot')
|
|
110
|
+
if (!~writable_props.indexOf(key) && key.slice(0, 2) !== '$$' && key !== 'slot') console_1.warn(`<Query> was created with unknown prop '${key}'`);
|
|
107
111
|
});
|
|
108
112
|
|
|
109
|
-
$$self
|
|
113
|
+
$$self.$$set = $$props => {
|
|
114
|
+
if ('article' in $$props) $$invalidate(1, article = $$props.article);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
$$self.$capture_state = () => ({
|
|
118
|
+
link,
|
|
119
|
+
currentURL,
|
|
120
|
+
article,
|
|
121
|
+
name,
|
|
122
|
+
$currentURL
|
|
123
|
+
});
|
|
110
124
|
|
|
111
125
|
$$self.$inject_state = $$props => {
|
|
126
|
+
if ('article' in $$props) $$invalidate(1, article = $$props.article);
|
|
112
127
|
if ('name' in $$props) $$invalidate(0, name = $$props.name);
|
|
113
128
|
};
|
|
114
129
|
|
|
@@ -117,18 +132,18 @@ function instance($$self, $$props, $$invalidate) {
|
|
|
117
132
|
}
|
|
118
133
|
|
|
119
134
|
$$self.$$.update = () => {
|
|
120
|
-
if ($$self.$$.dirty & /*$currentURL*/
|
|
135
|
+
if ($$self.$$.dirty & /*$currentURL*/ 4) {
|
|
121
136
|
$$invalidate(0, name = $currentURL.searchParams.get("name") || 'unknown');
|
|
122
137
|
}
|
|
123
138
|
};
|
|
124
139
|
|
|
125
|
-
return [name, $currentURL];
|
|
140
|
+
return [name, article, $currentURL];
|
|
126
141
|
}
|
|
127
142
|
|
|
128
143
|
class Query extends SvelteComponentDev {
|
|
129
144
|
constructor(options) {
|
|
130
145
|
super(options);
|
|
131
|
-
init(this, options, instance, create_fragment, safe_not_equal, {});
|
|
146
|
+
init(this, options, instance, create_fragment, safe_not_equal, { article: 1 });
|
|
132
147
|
|
|
133
148
|
dispatch_dev("SvelteRegisterComponent", {
|
|
134
149
|
component: this,
|
|
@@ -137,6 +152,14 @@ class Query extends SvelteComponentDev {
|
|
|
137
152
|
id: create_fragment.name
|
|
138
153
|
});
|
|
139
154
|
}
|
|
155
|
+
|
|
156
|
+
get article() {
|
|
157
|
+
throw new Error("<Query>: Props cannot be read directly from the component instance unless compiling with 'accessors: true' or '<svelte:options accessors/>'");
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
set article(value) {
|
|
161
|
+
throw new Error("<Query>: Props cannot be set directly on the component instance unless compiling with 'accessors: true' or '<svelte:options accessors/>'");
|
|
162
|
+
}
|
|
140
163
|
}
|
|
141
164
|
|
|
142
165
|
export { Query as default };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Query.js","sources":["../Query.svelte"],"sourcesContent":["<script>\n import { link } from \"../src/link.js\";\n import { currentURL } from \"../src/stores.js\";\n\n $: name = $currentURL.searchParams.get(\"name\") || 'unknown';\n</script>\n<svelte:head>\n <title>query</title>\n</svelte:head>\n<div class=\"query\">\n <p>name: <span id=\"name\">{ name }</span></p>\n <a use:link href=\"/query?name=foo\">foo</a>\n <a use:link href=\"/query?name=bar\">bar</a>\n</div>\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Query.js","sources":["../Query.svelte"],"sourcesContent":["<script>\n import { link } from \"../src/link.js\";\n import { currentURL } from \"../src/stores.js\";\n\n export let article = null;\n\n console.log(article);\n\n $: name = $currentURL.searchParams.get(\"name\") || 'unknown';\n</script>\n<svelte:head>\n <title>query</title>\n</svelte:head>\n<div class=\"query\">\n <p>name: <span id=\"name\">{ name }</span></p>\n <a use:link href=\"/query?name=foo\">foo</a>\n <a use:link href=\"/query?name=bar\">bar</a>\n</div>\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;aAcK,QAAM,CAAA,CAAA;;sBAAkB,GAAI,CAAA,CAAA,CAAA,CAAA,CAAA;;;;;;;;;;;;;;;;;;;;;;;GADjC,UAIM,CAAA,MAAA,EAAA,GAAA,EAAA,MAAA,CAAA,CAAA;GAHJ,UAA4C,CAAA,GAAA,EAAA,CAAA,CAAA,CAAA;;GAAnC,UAA+B,CAAA,CAAA,EAAA,IAAA,CAAA,CAAA;;;GACxC,UAA0C,CAAA,GAAA,EAAA,EAAA,CAAA,CAAA;;GAC1C,UAA0C,CAAA,GAAA,EAAA,EAAA,CAAA,CAAA;;;;;;;;;;;;qDAFf,GAAI,CAAA,CAAA,CAAA,CAAA,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAVpB,CAAA,IAAA,EAAA,OAAO,GAAG,IAAI,EAAA,GAAA,OAAA,CAAA;CAEzB,OAAO,CAAC,GAAG,CAAC,OAAO,CAAA,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAElB,YAAA,CAAA,CAAA,EAAE,IAAI,GAAG,WAAW,CAAC,YAAY,CAAC,GAAG,CAAC,MAAM,CAAA,IAAK,SAAS,CAAA,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-spa-history-router",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.2",
|
|
4
4
|
"description": "History base router for Svelte SPA",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"svelte": "src/index.js",
|
|
8
|
+
"types": "src/index.d.ts",
|
|
8
9
|
"scripts": {
|
|
9
10
|
"example:build": "cd example && rollup -c",
|
|
10
11
|
"example:build:watch": "cd example && rollup -c -w",
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
///<reference types="svelte" />
|
|
2
|
+
|
|
3
|
+
import { SvelteComponent, ComponentType } from 'svelte'
|
|
4
|
+
|
|
5
|
+
export { Route, RouteParams, Redirection } from "./types.ts"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Router component
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* <script>
|
|
12
|
+
* import { Router } from "svelte-spa-history-router";
|
|
13
|
+
*
|
|
14
|
+
* import Top from "./Top.svelte"
|
|
15
|
+
* import NotFound from "./NotFound.svelte"
|
|
16
|
+
*
|
|
17
|
+
* const routes = [
|
|
18
|
+
* { path: "/", component: Top },
|
|
19
|
+
* { path: "/posts/(?<postId>.*)", resolver: () => import("./Article.svelte") },
|
|
20
|
+
* { path: ".*", component: NotFound },
|
|
21
|
+
* ];
|
|
22
|
+
* </script>
|
|
23
|
+
* <Router {routes}/>
|
|
24
|
+
*/
|
|
25
|
+
export class Router extends SvelteComponent<{
|
|
26
|
+
routes: Route[]
|
|
27
|
+
}> {}
|
|
28
|
+
|
|
29
|
+
export { link } from "./link.js";
|
|
30
|
+
export { push } from "./push.js";
|
|
31
|
+
export { routeParams, currentURL } from "./stores.js";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* This is special function used in resolvers.
|
|
35
|
+
* Normally, you use `push()` to change the url.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} to
|
|
38
|
+
* @return {Redirection}
|
|
39
|
+
*/
|
|
40
|
+
export function redirect(to: string): Redirection;
|
package/src/link.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { push } from "./push.js";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Svelte action to make `<a>` work as navigation of svelte-spa-history-router
|
|
5
|
+
*
|
|
6
|
+
* @param {HTMLElement} node - target `<a>`
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* <a use:link href="/">top</a>
|
|
5
11
|
*/
|
|
6
12
|
export function link(node) {
|
|
7
13
|
/**
|
package/src/push.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { currentPath, currentURL } from "./stores.js";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
+
* Navigate to next page programmatically
|
|
5
|
+
*
|
|
4
6
|
* @param {string} next
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* <button on:click={ () => push(`posts/${id}`) }>next</button>
|
|
5
11
|
*/
|
|
6
12
|
export function push(next) {
|
|
7
13
|
window.history.pushState({}, "", next);
|
package/src/stores.js
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
import { writable, derived } from "svelte/store";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* @
|
|
5
|
-
* @typedef { import("
|
|
4
|
+
* @template T
|
|
5
|
+
* @typedef { import("svelte/store").Writable<T> } Writable
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* @template T
|
|
10
|
-
* @typedef { import("svelte/store").
|
|
10
|
+
* @typedef { import("svelte/store").Readable<T> } Readable
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* @typedef { import("./types.ts").Route } Route
|
|
15
|
+
* @typedef { import("./types.ts").RouteParams } RouteParams
|
|
16
|
+
* @typedef { import("./types.ts").CurrentURL } CurrentURL
|
|
17
|
+
* @typedef { import("./route_state.js").RouteState } RouteState
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
|
|
13
21
|
/**
|
|
14
22
|
* Store to detect changes (internal use).
|
|
15
23
|
* @type { Writable<String> }
|
|
@@ -22,12 +30,20 @@ export const currentPath = writable(window.location.pathname);
|
|
|
22
30
|
*/
|
|
23
31
|
export const routeState = writable(null);
|
|
24
32
|
|
|
25
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Store of captured variables in path
|
|
35
|
+
*
|
|
36
|
+
* @type Readable<RouteParams>
|
|
37
|
+
*/
|
|
26
38
|
export const routeParams = derived(routeState, ($routeState) => {
|
|
27
39
|
return $routeState ? $routeState.params || {} : {};
|
|
28
40
|
});
|
|
29
41
|
|
|
30
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Store of current location (to detect changes of query or hash)
|
|
44
|
+
*
|
|
45
|
+
* @type CurrentURL
|
|
46
|
+
*/
|
|
31
47
|
export const currentURL = (() => {
|
|
32
48
|
function getCurrent() {
|
|
33
49
|
return new URL(window.location.href);
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ComponentType } from "svelte";
|
|
2
|
+
import type { Readable } from "svelte/store";
|
|
2
3
|
|
|
3
4
|
import type { RouteState } from "./route_state.js";
|
|
4
5
|
export type { RouteState } from "./route_state.js";
|
|
@@ -19,6 +20,11 @@ export type Redirection = {
|
|
|
19
20
|
redirect: string,
|
|
20
21
|
}
|
|
21
22
|
|
|
23
|
+
export interface CurrentURL extends Readable<URL> {
|
|
24
|
+
set: (url: URL) => void
|
|
25
|
+
setCurrent: () => void
|
|
26
|
+
}
|
|
27
|
+
|
|
22
28
|
type ResolverFunc = (routeState: RouteState) => PromiseLike<ComponentType | ComponentModule | Redirection> | ComponentType | Redirection
|
|
23
29
|
|
|
24
30
|
export type Route = {
|