File tree Expand file tree Collapse file tree 2 files changed +20
-45
lines changed
Expand file tree Collapse file tree 2 files changed +20
-45
lines changed Original file line number Diff line number Diff line change @@ -34,47 +34,14 @@ function handleKey(e: KeyboardEvent): void {
3434 }
3535
3636 if (e .key === " ArrowRight" ) {
37- if (search .selected === blocks .length - 1 ) {
38- if (search .world !== 4 ) {
39- search .nextWorld ();
40- search .selected = 1 ;
41- }
42- } else {
43- search .selected += 1 ;
44- }
37+ search .selected += 1 ;
4538 } else if (e .key === " ArrowLeft" ) {
46- if (search .selected === 0 ) {
47- if (search .world !== 1 ) {
48- search .prevWorld ();
49- search .selected = blocks .length - 1 ;
50- }
51- } else {
52- search .selected -= 1 ;
53- }
39+ search .selected -= 1 ;
5440 } else if (e .key === " Enter" || e .key === " " ) {
5541 goto (` /game?stage=${search .world }-${search .selected } ` );
5642 } else if (e .key === " Escape" ) {
5743 goto (" /" );
5844 e .preventDefault ();
59- if (search .selected === blocks .length - 1 ) {
60- if (search .world !== 4 ) {
61- search .nextWorld ();
62- search .selected = 1 ;
63- }
64- } else {
65- search .selected += 1 ;
66- }
67- } else if (e .key === " ArrowLeft" ) {
68- if (search .selected === 0 ) {
69- if (search .world !== 1 ) {
70- search .prevWorld ();
71- search .selected = blocks .length - 1 ;
72- }
73- } else {
74- search .selected -= 1 ;
75- }
76- } else if (e .key === " Enter" || e .key === " " ) {
77- goto (blocks [search .selected ].link );
7845 }
7946}
8047function handleKeyUp() {
Original file line number Diff line number Diff line change @@ -10,8 +10,8 @@ const WORLD_STAGES_MAP = new Map([
1010] ) ;
1111
1212export class SearchParamState {
13- #world: number = $state ( 1 ) ;
14- #selected: number | null = $state ( null ) ;
13+ #world: number = $state ( 1 ) ; // starts from 1
14+ #selected: number | null = $state ( null ) ; // starts from 1
1515 maxStage : number = $derived ( WORLD_STAGES_MAP . get ( this . #world) ?? 0 ) ;
1616 constructor ( private defaultWorld : number ) {
1717 if ( ! browser ) {
@@ -49,21 +49,29 @@ export class SearchParamState {
4949 }
5050
5151 set selected ( val : number | null ) {
52- this . #selected = val ;
5352 if ( ! browser ) return ;
5453 if ( val === null ) return ;
5554
5655 if ( val > ( WORLD_STAGES_MAP . get ( this . #world) ?? 0 ) ) {
57- this . #selected = 1 ;
58- this . nextWorld ( ) ;
59- }
60- if ( val < 1 ) {
61- this . prevWorld ( ) ;
62- this . #selected = WORLD_STAGES_MAP . get ( this . #world) ?? 0 ;
56+ if ( this . #world === MAX_WORLD ) {
57+ // do not change
58+ } else {
59+ this . #selected = 1 ;
60+ this . nextWorld ( ) ;
61+ }
62+ } else if ( val < 1 ) {
63+ if ( this . #world === 1 ) {
64+ // do not change
65+ } else {
66+ this . prevWorld ( ) ;
67+ this . #selected = WORLD_STAGES_MAP . get ( this . #world) ?? 0 ;
68+ }
69+ } else {
70+ this . #selected = val ;
6371 }
6472
6573 const url = new URL ( window . location . href ) ;
66- url . searchParams . set ( "selected" , String ( val ) ) ;
74+ url . searchParams . set ( "selected" , String ( this . #selected ) ) ;
6775 replaceState ( url . toString ( ) , { } ) ;
6876 }
6977
You can’t perform that action at this time.
0 commit comments