Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- add state #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Blog.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<script lang="ts">
import type { Post } from './posts';
import {state} from "./lib/Elegua"
export let posts: Post[];

if($state.inputValue === undefined) {
$state.inputValue = ""
}
</script>

<svelte:head>
Expand All @@ -9,6 +14,9 @@
<h1>Blog page</h1>
<p>This is a static route</p>

<p>State only for this page</p>
<input bind:value={$state.inputValue}/>

<h2>Posts</h2>
<p>These will be matched using named routes, i.e. <code>/blog/:slug</code>:</p>
<ul>
Expand Down
9 changes: 8 additions & 1 deletion src/Home.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<script lang="ts">
import { goto, path, url } from './lib/Elegua';
import {goto, path, state, url} from './lib/Elegua'
import * as package_json from '../package.json';

if($state.inputValue === undefined) {
$state.inputValue = ""
}
</script>

<h1>This is the home page.</h1>

<p>State only for this page</p>
<input bind:value={$state.inputValue}/>
<p>
<b
>This is the demo application for <a href="https://github.com/howesteve/elegua">Elegua v{package_json.version}</a>,
Expand Down
15 changes: 11 additions & 4 deletions src/lib/Elegua.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { ComponentType } from 'svelte';
import { derived, get, readable, writable, type Subscriber } from 'svelte/store';
import {derived, get, readable, writable, type Subscriber} from 'svelte/store'
import type {Writable} from 'svelte/store'

export const state: Writable<Record<any, any>> = writable({})

let pathSetter: Subscriber<string>;
export const path = (() => {
Expand Down Expand Up @@ -43,13 +46,13 @@ export const url = (() => {
u.searchParams.set = (name, value) => {
const res = sset.call(u.searchParams, name, value);
set_(u);
history.pushState({}, '', u.toString());
history.pushState(get(state), '', u.toString());
return res;
};
u.searchParams.delete = (name) => {
const res = sdel.call(u.searchParams, name);
set_(u);
history.pushState({}, '', u.toString());
history.pushState(get(state), '', u.toString());
return res;
};
oldUrlSetter(get(url));
Expand All @@ -60,7 +63,10 @@ export const url = (() => {
return {
subscribe,
set: (u: URL) => {
history.pushState(null, '', u);
history.replaceState(get(state), '', get(url));
history.pushState({}, '', u);
state.set({});

urlSetter(u);
},
update
Expand Down Expand Up @@ -185,6 +191,7 @@ window?.addEventListener('load', () => {
const u = new URL(window.location.href);
urlSetter(u);
event.preventDefault();
state.set(event.state);
});

let lastKbdEv: KeyboardEvent|undefined
Expand Down