feat(web): use input types as cells
This commit is contained in:
+11
-3
@@ -15,7 +15,7 @@
|
|||||||
} from "./solver/types.ts";
|
} from "./solver/types.ts";
|
||||||
import SolverOutput from "./components/SolverOutput.svelte";
|
import SolverOutput from "./components/SolverOutput.svelte";
|
||||||
import { FolderGit2 } from "@lucide/svelte";
|
import { FolderGit2 } from "@lucide/svelte";
|
||||||
("@lucide/svelte");
|
import { tick } from "svelte";
|
||||||
|
|
||||||
let guesses = $state([
|
let guesses = $state([
|
||||||
{
|
{
|
||||||
@@ -41,6 +41,7 @@
|
|||||||
let best_words: Promise<EvaluateWordsResult> | null = $state(null);
|
let best_words: Promise<EvaluateWordsResult> | null = $state(null);
|
||||||
let best_words_progress: EvaluateWordsProgress | null = $state(null);
|
let best_words_progress: EvaluateWordsProgress | null = $state(null);
|
||||||
|
|
||||||
|
let w_lines: WordleLine[] = [];
|
||||||
let cursor = $state(0);
|
let cursor = $state(0);
|
||||||
|
|
||||||
async function reset() {
|
async function reset() {
|
||||||
@@ -72,8 +73,10 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleKey(event: KeyboardEvent) {
|
async function handleKey(event: KeyboardEvent) {
|
||||||
if (event.key === "Enter" && event.shiftKey) {
|
if (/^[a-zA-Z ]$/.test(event.key)) {
|
||||||
|
w_lines[guesses.length - 1].focus();
|
||||||
|
} else if (event.key === "Enter" && event.shiftKey) {
|
||||||
best_words = solver_evaluate_words(5, (p) => {
|
best_words = solver_evaluate_words(5, (p) => {
|
||||||
best_words_progress = p;
|
best_words_progress = p;
|
||||||
});
|
});
|
||||||
@@ -105,6 +108,10 @@
|
|||||||
Color.GREY,
|
Color.GREY,
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await tick();
|
||||||
|
|
||||||
|
w_lines[w_lines.length - 1].focus();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -154,6 +161,7 @@
|
|||||||
{#each guesses as g, i (g)}
|
{#each guesses as g, i (g)}
|
||||||
<WordleLine
|
<WordleLine
|
||||||
active={i === guesses.length - 1}
|
active={i === guesses.length - 1}
|
||||||
|
bind:this={w_lines[i]}
|
||||||
bind:cursor
|
bind:cursor
|
||||||
bind:letters={guesses[i].letters}
|
bind:letters={guesses[i].letters}
|
||||||
bind:colors={guesses[i].colors}
|
bind:colors={guesses[i].colors}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
}: {
|
}: {
|
||||||
data: Promise<EvaluateWordsResult>;
|
data: Promise<EvaluateWordsResult>;
|
||||||
progress: EvaluateWordsProgress | null;
|
progress: EvaluateWordsProgress | null;
|
||||||
insert_guess: (word: string[]) => void | null;
|
insert_guess: ((word: string[]) => void) | null;
|
||||||
} = $props();
|
} = $props();
|
||||||
|
|
||||||
function renderMath(txt: string | null) {
|
function renderMath(txt: string | null) {
|
||||||
|
|||||||
@@ -15,19 +15,16 @@
|
|||||||
info_gained = null,
|
info_gained = null,
|
||||||
} = $props();
|
} = $props();
|
||||||
|
|
||||||
|
let inputs: HTMLInputElement[] = [];
|
||||||
|
|
||||||
|
export function focus() {
|
||||||
|
inputs[cursor].focus();
|
||||||
|
}
|
||||||
|
|
||||||
function handleKey(event: KeyboardEvent) {
|
function handleKey(event: KeyboardEvent) {
|
||||||
if (!active) {
|
if (!active) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (/^[a-zA-Z]$/.test(event.key)) {
|
|
||||||
letters[cursor] = event.key.toUpperCase();
|
|
||||||
|
|
||||||
if (cursor < 4) {
|
|
||||||
cursor++;
|
|
||||||
} else {
|
|
||||||
cursor = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event.key === "Backspace") {
|
if (event.key === "Backspace") {
|
||||||
if (letters[cursor] === "" && cursor > 0) {
|
if (letters[cursor] === "" && cursor > 0) {
|
||||||
@@ -41,10 +38,12 @@
|
|||||||
|
|
||||||
if (event.key === "ArrowLeft") {
|
if (event.key === "ArrowLeft") {
|
||||||
cursor = Math.max(0, cursor - 1);
|
cursor = Math.max(0, cursor - 1);
|
||||||
|
inputs[cursor].focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.key === "ArrowRight") {
|
if (event.key === "ArrowRight") {
|
||||||
cursor = Math.min(4, cursor + 1);
|
cursor = Math.min(4, cursor + 1);
|
||||||
|
inputs[cursor].focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.key === " ") {
|
if (event.key === " ") {
|
||||||
@@ -57,18 +56,39 @@
|
|||||||
|
|
||||||
<div class="flex justify-center gap-2">
|
<div class="flex justify-center gap-2">
|
||||||
{#each letters as letter, i}
|
{#each letters as letter, i}
|
||||||
<div
|
<input
|
||||||
|
disabled={!active}
|
||||||
|
tabindex="-1"
|
||||||
class="
|
class="
|
||||||
flex h-16 w-16 items-center justify-center
|
flex h-16 w-16 items-center justify-center
|
||||||
rounded-lg border-2
|
rounded-lg border-2
|
||||||
text-3xl font-bold
|
text-3xl font-bold
|
||||||
transition-all
|
transition-all text-center caret-transparent
|
||||||
|
focus:outline-none
|
||||||
"
|
"
|
||||||
|
bind:this={inputs[i]}
|
||||||
|
bind:value={letters[i]}
|
||||||
class:border-blue-500={i === cursor && active}
|
class:border-blue-500={i === cursor && active}
|
||||||
class:border-zinc-700={i !== cursor || !active}
|
class:border-zinc-700={i !== cursor || !active}
|
||||||
class:bg-green-600={colors[i] === Color.GREEN}
|
class:bg-green-600={colors[i] === Color.GREEN}
|
||||||
class:bg-yellow-600={colors[i] === Color.YELLOW}
|
class:bg-yellow-600={colors[i] === Color.YELLOW}
|
||||||
class:bg-grey-600={colors[i] === Color.GREY}
|
class:bg-grey-600={colors[i] === Color.GREY}
|
||||||
|
onbeforeinput={(event) => {
|
||||||
|
if (event.inputType === "insertText") {
|
||||||
|
event.preventDefault();
|
||||||
|
let key = event.data ?? "";
|
||||||
|
|
||||||
|
if (/^[a-zA-Z]$/.test(key)) {
|
||||||
|
letters[i] = key.toUpperCase();
|
||||||
|
if (cursor < 4) {
|
||||||
|
cursor++;
|
||||||
|
} else {
|
||||||
|
cursor = 0;
|
||||||
|
}
|
||||||
|
inputs[cursor].focus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}}
|
||||||
onclick={() => {
|
onclick={() => {
|
||||||
if (!active) {
|
if (!active) {
|
||||||
return;
|
return;
|
||||||
@@ -79,9 +99,7 @@
|
|||||||
cursor = i;
|
cursor = i;
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
>
|
/>
|
||||||
{letter}
|
|
||||||
</div>
|
|
||||||
{/each}
|
{/each}
|
||||||
<span
|
<span
|
||||||
class="w-16 text-sm text-zinc-500 self-center flex items-center justify-center"
|
class="w-16 text-sm text-zinc-500 self-center flex items-center justify-center"
|
||||||
|
|||||||
Reference in New Issue
Block a user