Skip to content

Commit 73f2b62

Browse files
committed
searchParamがステージ選択に反映されていなかったのを修正
1 parent 2c52384 commit 73f2b62

File tree

5 files changed

+64
-54
lines changed

5 files changed

+64
-54
lines changed

routes/stage-select/+page.svelte

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import "@/ui-components/menu/menu.css";
55
import { goto } from "$app/navigation";
66
import { MAX_WORLD, SearchParamState } from "./params.svelte.ts";
77
8-
const search = new SearchParamState(1);
8+
const search = $state(new SearchParamState());
9+
onMount(() => {
10+
const params = new URLSearchParams(window.location.search);
11+
search.world = Number(params.get("world") || "1");
12+
search.selected = Number(params.get("selected") || "1");
13+
});
914
1015
const blocks = $derived(
1116
new Array(search.maxStage).fill(null).map((_, idx) => ({
@@ -75,23 +80,24 @@ onMount(() => {
7580
<button
7681
class={[
7782
"appearance-none focus:outline-none px-4 select-none cursor-pointer",
78-
search.world <= 1 && 'invisible',
79-
"hover:-translate-y-1 hover:text-gray-700 active:translate-y-0 active:text-black"]}
80-
onclick={() => search.world--}
81-
disabled={search.world <= 1}
83+
(search.world === null || search.world <= 1) && "invisible",
84+
"hover:-translate-y-1 hover:text-gray-700 active:translate-y-0 active:text-black",
85+
]}
86+
onclick={() => search.world !== null && search.world--}
87+
disabled={search.world === null || search.world <= 1}
8288
>
8389
&lt;
8490
</button>
8591
<span>World {search.world}</span>
8692
<!-- 右矢印ボタン -->
8793
<button
8894
aria-label="次のワールド"
89-
onclick={() => search.world++}
90-
disabled={search.world >= MAX_WORLD}
95+
onclick={() => search.world !== null && search.world++}
96+
disabled={search.world === null || search.world >= MAX_WORLD}
9197
class={[
9298
"appearance-none focus:outline-none px-4 select-none cursor-pointer",
93-
search.world >= MAX_WORLD && 'invisible',
94-
"hover:-translate-y-1 hover:text-gray-700 active:translate-y-0 active:text-black"
99+
(search.world === null || search.world >= MAX_WORLD) && "invisible",
100+
"hover:-translate-y-1 hover:text-gray-700 active:translate-y-0 active:text-black",
95101
]}
96102
>
97103
&gt;
@@ -101,15 +107,15 @@ onMount(() => {
101107
<div class="flex justify-center items-center grow-1">
102108
<div role="button" tabindex="0" class="flex outline-none items-center">
103109
{#each blocks as block, idx}
104-
{@const stage = idx + 1}
110+
{@const stage = idx + 1}
105111
<button
106112
type="button"
107113
class={`appearance-none focus:outline-none bg-white border-6 pt-8 pb-6 pl-8 pr-6 transition-colors duration-200 text-7xl cursor-pointer ${
108114
search.selected === stage
109115
? "border-red-500 ring ring-red-500 bg-amber-100!"
110116
: "border-base"
111117
}`}
112-
onmouseenter={() => search.selected = stage}
118+
onmouseenter={() => (search.selected = stage)}
113119
onclick={() => goto(block.link)}
114120
>
115121
{block.label}
@@ -129,11 +135,14 @@ onMount(() => {
129135
{#if search.selected !== null}
130136
{#key search.world}
131137
{#each blocks as block, idx}
132-
{@const stage = idx + 1}
138+
{@const stage = idx + 1}
133139
<img
134140
src={block.thumbnail}
135141
alt=""
136-
class={["h-full skeleton", stage !== search.selected && "hidden"]}
142+
class={[
143+
"h-full skeleton",
144+
stage !== search.selected && "hidden",
145+
]}
137146
/>
138147
{/each}
139148
{/key}
@@ -143,17 +152,12 @@ onMount(() => {
143152
<div
144153
class="flex-none w-70 max-h-full bg-white/90 p-4 m-4 rounded-lg border-2"
145154
>
146-
<div>
147-
Click,
148-
</div>
149-
<div>
150-
Press <Key key="Enter" enabled />
151-
</div>
152-
<div>or Press <Key key="Space" enabled />
153-
</div>
154-
<div>
155-
To Start
156-
</div>
155+
<div>Click,</div>
156+
<div>
157+
Press <Key key="Enter" enabled />
158+
</div>
159+
<div>or Press <Key key="Space" enabled /></div>
160+
<div>To Start</div>
157161
</div>
158162
</div>
159163
</div>

routes/stage-select/params.svelte.ts

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,32 @@ const WORLD_STAGES_MAP = new Map([
1010
]);
1111

1212
export class SearchParamState {
13-
#world: number = $state(1); // starts from 1
13+
#world: number | null = $state(null); // starts from 1
1414
#selected: number | null = $state(null); // starts from 1
15-
maxStage: number = $derived(WORLD_STAGES_MAP.get(this.#world) ?? 0);
16-
constructor(private defaultWorld: number) {
17-
if (!browser) {
18-
this.#world = this.defaultWorld;
19-
this.#selected = null;
20-
return;
21-
}
22-
const params = new URLSearchParams(window.location.href);
23-
this.#world = Number(params.get("world") || defaultWorld);
24-
this.#selected = Number(params.get("selected") || null);
15+
maxStage: number = $derived(WORLD_STAGES_MAP.get(this.#world ?? 0) ?? 0);
16+
constructor() {
17+
this.#world = null;
18+
this.#selected = null;
2519
}
2620

27-
get world(): number {
21+
get world(): number | null {
2822
return this.#world;
2923
}
30-
set world(val: number) {
31-
this.#world = val;
24+
set world(val: number | null) {
25+
console.log("world", val);
3226
if (!browser) return;
3327

28+
if (val === null) return;
3429
if (val > MAX_WORLD) return;
3530
if (val < 1) return;
31+
this.#world = val;
3632

3733
const nextMaxStage = WORLD_STAGES_MAP.get(val) ?? 0;
3834
if (this.#selected !== null && this.#selected > nextMaxStage) {
3935
this.#selected = nextMaxStage;
4036
}
4137

42-
const url = new URL(window.location.href);
43-
url.searchParams.set("world", String(val));
44-
replaceState(url.toString(), {});
38+
this.updateURL();
4539
}
4640

4741
get selected(): number | null {
@@ -52,35 +46,47 @@ export class SearchParamState {
5246
if (!browser) return;
5347
if (val === null) return;
5448

55-
if (val > (WORLD_STAGES_MAP.get(this.#world) ?? 0)) {
49+
if (val > (WORLD_STAGES_MAP.get(this.#world ?? 0) ?? 0)) {
5650
if (this.#world === MAX_WORLD) {
57-
// do not change
51+
// keep old value
5852
} else {
5953
this.#selected = 1;
6054
this.nextWorld();
6155
}
6256
} else if (val < 1) {
6357
if (this.#world === 1) {
64-
// do not change
58+
// keep old value
6559
} else {
6660
this.prevWorld();
67-
this.#selected = WORLD_STAGES_MAP.get(this.#world) ?? 0;
61+
this.#selected = WORLD_STAGES_MAP.get(this.#world ?? 0) ?? 0;
6862
}
6963
} else {
7064
this.#selected = val;
7165
}
7266

73-
const url = new URL(window.location.href);
74-
url.searchParams.set("selected", String(this.#selected));
75-
replaceState(url.toString(), {});
67+
this.updateURL();
7668
}
7769

7870
nextWorld() {
79-
this.world = Math.min(MAX_WORLD, this.world + 1);
71+
this.world = Math.min(MAX_WORLD, (this.world ?? 0) + 1);
8072
this.selected = 1;
73+
this.updateURL();
8174
}
8275
prevWorld() {
83-
this.world = Math.max(1, this.world - 1);
84-
this.selected = WORLD_STAGES_MAP.get(this.world) ?? 0;
76+
this.world = Math.max(1, (this.world ?? 0) - 1);
77+
this.selected = WORLD_STAGES_MAP.get(this.world ?? 0) ?? 0;
78+
this.updateURL();
79+
}
80+
81+
updateURL() {
82+
const url = new URL(window.location.href);
83+
url.searchParams.set("world", String(this.#world));
84+
url.searchParams.set("selected", String(this.#selected));
85+
try {
86+
replaceState(url.toString(), {});
87+
} catch (e) {
88+
// what do you mean "Cannot call replaceState(...) before router is initialized"
89+
console.warn("Failed to update URL:", e);
90+
}
8591
}
8692
}

src/ui-components/menu/GameOverMenu.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ $effect(() => {
7575
>
7676
Restart
7777
</button>
78-
<a class="btn modal-btn" href="/stage-select?w={stageNum.split("-")[0]}&s={stageNum.split("-")[1]}" bind:this={btn2} tabindex="2"> Back to Stage Select </a>
78+
<a class="btn modal-btn" href="/stage-select?world={stageNum.split("-")[0]}&selected={stageNum.split("-")[1]}" bind:this={btn2} tabindex="2"> Back to Stage Select </a>
7979
</div>
8080
</dialog>

src/ui-components/menu/GoalMenu.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,6 @@ onDestroy(() => {
8585
>
8686
Restart
8787
</button>
88-
<a class="btn modal-btn" href="/stage-select?w={stageNum.split("-")[0]}&s={stageNum.split("-")[1]}" bind:this={btn3} tabindex="3" > Back to Stage Select </a>
88+
<a class="btn modal-btn" href="/stage-select?world={stageNum.split("-")[0]}&selected={stageNum.split("-")[1]}" bind:this={btn3} tabindex="3"> Back to Stage Select </a>
8989
</div>
9090
</dialog>

src/ui-components/menu/PauseMenu.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@ $effect(() => {
7171
<button class="btn modal-btn" type="submit" bind:this={btn1} tabindex="1">Resume</button>
7272
</form>
7373
<button class="btn modal-btn" onclick={() => onreset()} bind:this={btn2} tabindex="2">Restart</button>
74-
<a class="btn modal-btn" href="/stage-select?w={stageNum.split("-")[0]}&s={stageNum.split("-")[1]}" bind:this={btn3} tabindex="3"> Back to Stage Select </a>
74+
<a class="btn modal-btn" href="/stage-select?world={stageNum.split("-")[0]}&selected={stageNum.split("-")[1]}" bind:this={btn3} tabindex="3"> Back to Stage Select </a>
7575
</div>
7676
</dialog>

0 commit comments

Comments
 (0)