From 231563d49b464baea24e414cec4aa73be19f46b2 Mon Sep 17 00:00:00 2001 From: Galla Date: Sun, 21 Apr 2024 12:41:35 -0400 Subject: [PATCH 1/9] Allowing hotkeys to work when board is not selected --- app/Components/Sudoku Board/SudokuBoard.tsx | 34 +++++++++++++-------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/app/Components/Sudoku Board/SudokuBoard.tsx b/app/Components/Sudoku Board/SudokuBoard.tsx index 818a015b..fd394925 100644 --- a/app/Components/Sudoku Board/SudokuBoard.tsx +++ b/app/Components/Sudoku Board/SudokuBoard.tsx @@ -434,11 +434,30 @@ const SudokuBoard = (props: SudokuBoardProps) => { }; const handleKeyDown = (event: any) => { + const inputValue = event.nativeEvent.key; + + if (inputValue == "u" || inputValue == "U") { + const isUndoButtonDisabled = + sudokuBoard.actionHistory == null || + sudokuBoard.actionHistory.length == 0; + if (!isUndoButtonDisabled) { + undo(); + } + } else if (inputValue == "p" || inputValue == "P") { + handlePause(sudokuBoard, navigation); + } else if ( + inputValue == "t" || + inputValue == "T" || + inputValue == "n" || + inputValue == "N" + ) { + toggleNoteMode(); + } + if (sudokuBoard.selectedCell == null) { return; } - const inputValue = event.nativeEvent.key; if (/^[1-9]$/.test(inputValue)) { updateCellEntry(parseInt(inputValue, 10)); } else if ( @@ -449,18 +468,7 @@ const SudokuBoard = (props: SudokuBoardProps) => { inputValue == "E" // e and E are for erase ) { eraseSelected(); - } else if (inputValue == "u" || inputValue == "U") { - undo(); - } else if (inputValue == "p" || inputValue == "P") { - handlePause(sudokuBoard, navigation); - } else if ( - inputValue == "t" || - inputValue == "T" || - inputValue == "n" || - inputValue == "N" - ) { - toggleNoteMode(); - } else if (sudokuBoard.selectedCell) { + } else { let newCol = sudokuBoard.selectedCell.c; let newRow = sudokuBoard.selectedCell.r; switch (inputValue) { From c98288bfbd775728c430b52c52411ccfd3efe488 Mon Sep 17 00:00:00 2001 From: Galla Date: Sat, 27 Apr 2024 11:51:11 -0400 Subject: [PATCH 2/9] updating tests --- e2e/web/specs/buttons-and-shortcuts.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/e2e/web/specs/buttons-and-shortcuts.spec.ts b/e2e/web/specs/buttons-and-shortcuts.spec.ts index cc093d91..d018d24b 100644 --- a/e2e/web/specs/buttons-and-shortcuts.spec.ts +++ b/e2e/web/specs/buttons-and-shortcuts.spec.ts @@ -10,7 +10,6 @@ test.describe("pause", () => { let capital = key === "P" ? "capital " : ""; test("pause button: " + capital + key, async ({ resumeGame }) => { const sudokuBoard = new SudokuBoardComponent(resumeGame); - await sudokuBoard.cell[0][0].click(); if (key === "button") { await sudokuBoard.pause.click(); } else { @@ -85,6 +84,7 @@ test.describe("undo", () => { await sudokuBoard.cell[7][7].click(); await sudokuBoard.cell[7][7].press("1"); await sudokuBoard.cellHasValue(7, 7, "1"); + await sudokuBoard.cell[7][7].click(); if (key === "button") { await sudokuBoard.undo.click(); } else { @@ -296,13 +296,13 @@ test.describe("toggle notes", () => { let capital = key === "N" || key === "T" ? "capital " : ""; test("toggle notes: " + capital + key, async ({ resumeGame }) => { const sudokuBoard = new SudokuBoardComponent(resumeGame); - await sudokuBoard.cell[7][7].click(); if (key === "button") { await sudokuBoard.note.click(); } else { await sudokuBoard.page.keyboard.press(key); } await sudokuBoard.cellHasValue(7, 7, "0"); + await sudokuBoard.cell[7][7].click(); await sudokuBoard.page.keyboard.press("1"); await sudokuBoard.cellHasNotes(7, 7, "1"); if (key === "button") { From c0f591dd037d173f7482a2a6993d46ffc7ef9273 Mon Sep 17 00:00:00 2001 From: Galla Date: Sat, 27 Apr 2024 12:17:05 -0400 Subject: [PATCH 3/9] reverting test changes --- e2e/web/specs/buttons-and-shortcuts.spec.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/e2e/web/specs/buttons-and-shortcuts.spec.ts b/e2e/web/specs/buttons-and-shortcuts.spec.ts index d018d24b..d3ca1a6a 100644 --- a/e2e/web/specs/buttons-and-shortcuts.spec.ts +++ b/e2e/web/specs/buttons-and-shortcuts.spec.ts @@ -10,6 +10,7 @@ test.describe("pause", () => { let capital = key === "P" ? "capital " : ""; test("pause button: " + capital + key, async ({ resumeGame }) => { const sudokuBoard = new SudokuBoardComponent(resumeGame); + await sudokuBoard.cell[0][0].click(); if (key === "button") { await sudokuBoard.pause.click(); } else { @@ -84,7 +85,6 @@ test.describe("undo", () => { await sudokuBoard.cell[7][7].click(); await sudokuBoard.cell[7][7].press("1"); await sudokuBoard.cellHasValue(7, 7, "1"); - await sudokuBoard.cell[7][7].click(); if (key === "button") { await sudokuBoard.undo.click(); } else { @@ -296,6 +296,7 @@ test.describe("toggle notes", () => { let capital = key === "N" || key === "T" ? "capital " : ""; test("toggle notes: " + capital + key, async ({ resumeGame }) => { const sudokuBoard = new SudokuBoardComponent(resumeGame); + await sudokuBoard.cell[7][7].click(); if (key === "button") { await sudokuBoard.note.click(); } else { From f8364ecd92cc5f5e926cd14d8e6a5a9959bcd863 Mon Sep 17 00:00:00 2001 From: Galla Date: Sat, 27 Apr 2024 12:51:47 -0400 Subject: [PATCH 4/9] ignoring ts error --- app/Components/Sudoku Board/SudokuBoard.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/Components/Sudoku Board/SudokuBoard.tsx b/app/Components/Sudoku Board/SudokuBoard.tsx index fd394925..2c5cade9 100644 --- a/app/Components/Sudoku Board/SudokuBoard.tsx +++ b/app/Components/Sudoku Board/SudokuBoard.tsx @@ -572,9 +572,12 @@ const SudokuBoard = (props: SudokuBoardProps) => { ); }; + console.log("HELLO FRIENDS"); + return ( Date: Sat, 27 Apr 2024 12:59:51 -0400 Subject: [PATCH 5/9] removing test update --- e2e/web/specs/buttons-and-shortcuts.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/e2e/web/specs/buttons-and-shortcuts.spec.ts b/e2e/web/specs/buttons-and-shortcuts.spec.ts index d3ca1a6a..cc093d91 100644 --- a/e2e/web/specs/buttons-and-shortcuts.spec.ts +++ b/e2e/web/specs/buttons-and-shortcuts.spec.ts @@ -303,7 +303,6 @@ test.describe("toggle notes", () => { await sudokuBoard.page.keyboard.press(key); } await sudokuBoard.cellHasValue(7, 7, "0"); - await sudokuBoard.cell[7][7].click(); await sudokuBoard.page.keyboard.press("1"); await sudokuBoard.cellHasNotes(7, 7, "1"); if (key === "button") { From 467d9fe88b84bb5fe143dc024c8aba244d4f1755 Mon Sep 17 00:00:00 2001 From: Galla Date: Sat, 27 Apr 2024 13:01:27 -0400 Subject: [PATCH 6/9] removing console log --- app/Components/Sudoku Board/SudokuBoard.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/Components/Sudoku Board/SudokuBoard.tsx b/app/Components/Sudoku Board/SudokuBoard.tsx index 2c5cade9..a84acf75 100644 --- a/app/Components/Sudoku Board/SudokuBoard.tsx +++ b/app/Components/Sudoku Board/SudokuBoard.tsx @@ -572,8 +572,6 @@ const SudokuBoard = (props: SudokuBoardProps) => { ); }; - console.log("HELLO FRIENDS"); - return ( Date: Sun, 28 Apr 2024 16:57:40 -0400 Subject: [PATCH 7/9] switching to switch statement --- app/Components/Sudoku Board/SudokuBoard.tsx | 124 +++++++++++--------- 1 file changed, 67 insertions(+), 57 deletions(-) diff --git a/app/Components/Sudoku Board/SudokuBoard.tsx b/app/Components/Sudoku Board/SudokuBoard.tsx index a84acf75..42a94f44 100644 --- a/app/Components/Sudoku Board/SudokuBoard.tsx +++ b/app/Components/Sudoku Board/SudokuBoard.tsx @@ -433,73 +433,83 @@ const SudokuBoard = (props: SudokuBoardProps) => { ]; }; + /** + * When a user presses a key down, do the desired action + * @param event onKeyDown event for react-native-web documented here: https://necolas.github.io/react-native-web/docs/interactions/#keyboardevent-props-api + * @returns void + */ const handleKeyDown = (event: any) => { const inputValue = event.nativeEvent.key; - if (inputValue == "u" || inputValue == "U") { - const isUndoButtonDisabled = - sudokuBoard.actionHistory == null || - sudokuBoard.actionHistory.length == 0; - if (!isUndoButtonDisabled) { - undo(); - } - } else if (inputValue == "p" || inputValue == "P") { - handlePause(sudokuBoard, navigation); - } else if ( - inputValue == "t" || - inputValue == "T" || - inputValue == "n" || - inputValue == "N" - ) { - toggleNoteMode(); + switch (inputValue) { + case "u": + case "U": + const isUndoButtonDisabled = + sudokuBoard.actionHistory == null || + sudokuBoard.actionHistory.length == 0; + if (!isUndoButtonDisabled) { + undo(); + } + return; + case "p": + case "P": + handlePause(sudokuBoard, navigation); + return; + case "t": + case "T": + case "n": + case "N": + toggleNoteMode(); + default: + break; } if (sudokuBoard.selectedCell == null) { return; } - if (/^[1-9]$/.test(inputValue)) { - updateCellEntry(parseInt(inputValue, 10)); - } else if ( - inputValue == "Delete" || - inputValue == "Backspace" || - inputValue == "0" || - inputValue == "e" || - inputValue == "E" // e and E are for erase - ) { - eraseSelected(); - } else { - let newCol = sudokuBoard.selectedCell.c; - let newRow = sudokuBoard.selectedCell.r; - switch (inputValue) { - case "ArrowLeft": - case "a": - case "A": - newCol = wrapDigit(newCol - 1); - break; - case "ArrowRight": - case "d": - case "D": - newCol = wrapDigit(newCol + 1); - break; - case "ArrowUp": - case "w": - case "W": - newRow = wrapDigit(newRow - 1); - break; - case "ArrowDown": - case "s": - case "S": - newRow = wrapDigit(newRow + 1); - break; - default: - return; - } - setSudokuBoard({ - ...sudokuBoard, - selectedCell: { r: newRow, c: newCol }, - }); + let newCol = sudokuBoard.selectedCell.c; + let newRow = sudokuBoard.selectedCell.r; + switch (inputValue) { + case /^[1-9]$/.test(inputValue): + updateCellEntry(parseInt(inputValue, 10)); + return; + case "Delete": + case "Backspace": + case "0": + case "e": + case "E": + eraseSelected(); + return; + // below cases do not return to allow for update of selected cell + // todo create function for updating selectedCell for below cases to call + case "ArrowLeft": + case "a": + case "A": + newCol = wrapDigit(newCol - 1); + break; + case "ArrowRight": + case "d": + case "D": + newCol = wrapDigit(newCol + 1); + break; + case "ArrowUp": + case "w": + case "W": + newRow = wrapDigit(newRow - 1); + break; + case "ArrowDown": + case "s": + case "S": + newRow = wrapDigit(newRow + 1); + break; + default: + return; } + setSudokuBoard({ + ...sudokuBoard, + selectedCell: { r: newRow, c: newCol }, + }); }; const renderPuzzle = () => { From b940b8868ce3f68db998d93c4309af5bdcb1ef39 Mon Sep 17 00:00:00 2001 From: Galla Date: Sun, 28 Apr 2024 17:04:29 -0400 Subject: [PATCH 8/9] Updating to using switch statements --- app/Components/Sudoku Board/SudokuBoard.tsx | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/Components/Sudoku Board/SudokuBoard.tsx b/app/Components/Sudoku Board/SudokuBoard.tsx index 42a94f44..31599354 100644 --- a/app/Components/Sudoku Board/SudokuBoard.tsx +++ b/app/Components/Sudoku Board/SudokuBoard.tsx @@ -441,6 +441,8 @@ const SudokuBoard = (props: SudokuBoardProps) => { const handleKeyDown = (event: any) => { const inputValue = event.nativeEvent.key; + console.log(inputValue); + switch (inputValue) { case "u": case "U": @@ -460,6 +462,7 @@ const SudokuBoard = (props: SudokuBoardProps) => { case "n": case "N": toggleNoteMode(); + return; default: break; } @@ -468,12 +471,14 @@ const SudokuBoard = (props: SudokuBoardProps) => { return; } + if (/^[1-9]$/.test(inputValue)) { + updateCellEntry(parseInt(inputValue, 10)); + return; + } + let newCol = sudokuBoard.selectedCell.c; let newRow = sudokuBoard.selectedCell.r; switch (inputValue) { - case /^[1-9]$/.test(inputValue): - updateCellEntry(parseInt(inputValue, 10)); - return; case "Delete": case "Backspace": case "0": From 63813eaa1c277f85d978b9f1c8120b3dd57dac78 Mon Sep 17 00:00:00 2001 From: Galla Date: Sun, 28 Apr 2024 17:06:29 -0400 Subject: [PATCH 9/9] removing console log --- app/Components/Sudoku Board/SudokuBoard.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/Components/Sudoku Board/SudokuBoard.tsx b/app/Components/Sudoku Board/SudokuBoard.tsx index 31599354..55eae92a 100644 --- a/app/Components/Sudoku Board/SudokuBoard.tsx +++ b/app/Components/Sudoku Board/SudokuBoard.tsx @@ -441,8 +441,6 @@ const SudokuBoard = (props: SudokuBoardProps) => { const handleKeyDown = (event: any) => { const inputValue = event.nativeEvent.key; - console.log(inputValue); - switch (inputValue) { case "u": case "U":