Add keyboard layout settings

This commit is contained in:
Lynn
2022-01-27 23:05:19 +01:00
parent 4a07e05759
commit c09db8aea6
4 changed files with 47 additions and 7 deletions

View File

@@ -237,6 +237,7 @@ a:active {
.Settings-setting label { .Settings-setting label {
margin-inline-start: 8px; margin-inline-start: 8px;
margin-inline-end: 8px;
} }
.top-right { .top-right {

View File

@@ -35,6 +35,11 @@ function App() {
const [dark, setDark] = useSetting<boolean>("dark", prefersDark); const [dark, setDark] = useSetting<boolean>("dark", prefersDark);
const [colorBlind, setColorBlind] = useSetting<boolean>("colorblind", false); const [colorBlind, setColorBlind] = useSetting<boolean>("colorblind", false);
const [difficulty, setDifficulty] = useSetting<number>("difficulty", 0); const [difficulty, setDifficulty] = useSetting<number>("difficulty", 0);
const [keyboard, setKeyboard] = useSetting<string>(
"keyboard",
"qwertyuiop-asdfghjkl-BzxcvbnmE"
);
const [enterLeft, setEnterLeft] = useSetting<boolean>("enter-left", false);
useEffect(() => { useEffect(() => {
document.body.className = dark ? "dark" : ""; document.body.className = dark ? "dark" : "";
@@ -131,7 +136,6 @@ function App() {
/> />
<div> <div>
<label htmlFor="difficulty-setting">Difficulty:</label> <label htmlFor="difficulty-setting">Difficulty:</label>
&nbsp;
<strong>{["Normal", "Hard", "Ultra Hard"][difficulty]}</strong> <strong>{["Normal", "Hard", "Ultra Hard"][difficulty]}</strong>
<div <div
style={{ style={{
@@ -151,6 +155,29 @@ function App() {
</div> </div>
</div> </div>
</div> </div>
<div className="Settings-setting">
<label htmlFor="keyboard-setting">Keyboard layout:</label>
<select
name="keyboard-setting"
id="keyboard-setting"
value={keyboard}
onChange={(e) => setKeyboard(e.target.value)}
>
<option value="qwertyuiop-asdfghjkl-BzxcvbnmE">QWERTY</option>
<option value="azertyuiop-qsdfghjklm-BzxcvbnE">AZERTY</option>
<option value="qwertzuiop-asdfghjkl-ByxcvbnmE">QWERTZ</option>
<option value="BpyfgcrlE-aoeuidhtns-qjkxbmwvz">Dvorak</option>
<option value="qwfpgjluy-arstdhneio-BzxcvbkmE">Colemak</option>
</select>
<input
style={{ marginLeft: 20 }}
id="enter-left-setting"
type="checkbox"
checked={enterLeft}
onChange={() => setEnterLeft((x: boolean) => !x)}
/>
<label htmlFor="enter-left-setting">"Enter" on left side</label>
</div>
</div> </div>
)} )}
<Game <Game
@@ -158,6 +185,10 @@ function App() {
hidden={page !== "game"} hidden={page !== "game"}
difficulty={difficulty} difficulty={difficulty}
colorBlind={colorBlind} colorBlind={colorBlind}
keyboardLayout={keyboard.replaceAll(
/[BE]/g,
(x) => (enterLeft ? "EB" : "BE")["BE".indexOf(x)]
)}
/> />
</div> </div>
); );

View File

@@ -26,6 +26,7 @@ interface GameProps {
hidden: boolean; hidden: boolean;
difficulty: Difficulty; difficulty: Difficulty;
colorBlind: boolean; colorBlind: boolean;
keyboardLayout: string;
} }
const targets = targetList.slice(0, targetList.indexOf("murky") + 1); // Words no rarer than this one const targets = targetList.slice(0, targetList.indexOf("murky") + 1); // Words no rarer than this one
@@ -281,7 +282,11 @@ function Game(props: GameProps) {
> >
{hint || `\u00a0`} {hint || `\u00a0`}
</p> </p>
<Keyboard letterInfo={letterInfo} onKey={onKey} /> <Keyboard
layout={props.keyboardLayout}
letterInfo={letterInfo}
onKey={onKey}
/>
{gameState !== GameState.Playing && ( {gameState !== GameState.Playing && (
<p> <p>
<button <button

View File

@@ -1,16 +1,19 @@
import { Clue, clueClass } from "./clue"; import { Clue, clueClass } from "./clue";
interface KeyboardProps { interface KeyboardProps {
layout: string;
letterInfo: Map<string, Clue>; letterInfo: Map<string, Clue>;
onKey: (key: string) => void; onKey: (key: string) => void;
} }
export function Keyboard(props: KeyboardProps) { export function Keyboard(props: KeyboardProps) {
const keyboard = [ const keyboard = props.layout
"q w e r t y u i o p".split(" "), .split("-")
"a s d f g h j k l".split(" "), .map((row) =>
"Backspace z x c v b n m Enter".split(" "), row
]; .split("")
.map((key) => key.replace("B", "Backspace").replace("E", "Enter"))
);
return ( return (
<div className="Game-keyboard" aria-hidden="true"> <div className="Game-keyboard" aria-hidden="true">