Skip to content

Commit

Permalink
impr(quick restart): add "enter" as an option to quick restart
Browse files Browse the repository at this point in the history
  • Loading branch information
Miodec committed Nov 2, 2023
1 parent b843cbe commit 78ecdcf
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 6 deletions.
2 changes: 1 addition & 1 deletion backend/src/api/schemas/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const CONFIG_SCHEMA = joi.object({
showLiveWpm: joi.boolean(),
showTimerProgress: joi.boolean(),
smoothCaret: joi.string().valid("off", "slow", "medium", "fast"),
quickRestart: joi.string().valid("off", "tab", "esc"),
quickRestart: joi.string().valid("off", "tab", "esc", "enter"),
punctuation: joi.boolean(),
numbers: joi.boolean(),
words: joi.number().min(0),
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/ts/commandline/lists/quick-restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ const subgroup: MonkeyTypes.CommandsSubgroup = {
title: "Quick restart...",
configKey: "quickRestart",
list: [
{
id: "changeQuickRestartEnter",
display: "enter",
configValue: "enter",
exec: (): void => {
UpdateConfig.setQuickRestartMode("enter");
},
},
{
id: "changeQuickRestartTab",
display: "tab",
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/ts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1196,11 +1196,13 @@ export function setSmoothLineScroll(mode: boolean, nosave?: boolean): boolean {

//quick restart
export function setQuickRestartMode(
mode: "off" | "esc" | "tab",
mode: "off" | "esc" | "tab" | "enter",
nosave?: boolean
): boolean {
if (
!isConfigValueValid("quick restart mode", mode, [["off", "esc", "tab"]])
!isConfigValueValid("quick restart mode", mode, [
["off", "esc", "tab", "enter"],
])
) {
return false;
}
Expand Down
48 changes: 47 additions & 1 deletion frontend/src/ts/controllers/input-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ function handleTab(event: JQuery.KeyDownEvent, popupVisible: boolean): void {
const modalVisible: boolean =
Misc.isPopupVisible("commandLineWrapper") || popupVisible;

if (Config.quickRestart === "esc") {
if (Config.quickRestart === "esc" || Config.quickRestart === "enter") {
// dont do anything special
if (modalVisible) return;

Expand Down Expand Up @@ -899,6 +899,52 @@ $(document).on("keydown", async (event) => {
});
}

//enter
if (event.key === "Enter" && Config.quickRestart === "enter") {
const modalVisible: boolean =
Misc.isPopupVisible("commandLineWrapper") || popupVisible;

if (modalVisible) return;

// change page if not on test page
if (ActivePage.get() !== "test") {
navigate("/");
return;
}

if (TestUI.resultVisible) {
TestLogic.restart({
event,
});
return;
}

if (Config.mode === "zen") {
//do nothing
} else if (
!TestWords.hasNewline ||
(TestWords.hasNewline && event.shiftKey)
) {
// in case we are in a long test, setting manual restart
if (event.shiftKey) {
ManualRestart.set();
} else {
ManualRestart.reset();
}

//otherwise restart
TestLogic.restart({
event,
});
} else {
handleChar("\n", TestInput.input.current.length);
setWordsInput(" " + TestInput.input.current);
if (Config.tapeMode !== "off") {
TestUI.scrollTape();
}
}
}

if (!allowTyping) return;

if (!event.originalEvent?.isTrusted || TestUI.testRestarting) {
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/ts/elements/modes-notice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ export async function update(): Promise<void> {
}
}

if (TestWords.hasNewline && Config.quickRestart === "enter") {
$(".pageTest #testModesNotice").append(
`<div class="textButton noInteraction"><i class="fas fa-level-down-alt fa-rotate-90"></i>shift + enter to restart</div>`
);
}

const customTextName = CustomTextState.getCustomTextName();
const isLong = CustomTextState.isCustomTextLong();
if (Config.mode === "custom" && customTextName !== "" && isLong) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/ts/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ $(document).ready(() => {
CookiePopup.check();

$("body").css("transition", "background .25s, transform .05s");
if (Config.quickRestart === "tab" || Config.quickRestart === "esc") {
if (Config.quickRestart !== "off") {
$("#restartTestButton").addClass("hidden");
}
if (!window.localStorage.getItem("merchbannerclosed")) {
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/ts/test/test-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ export function restart(options = {} as RestartOptions): void {
message = "Press shift + tab or use your mouse to confirm.";
} else if (Config.quickRestart === "esc") {
message = "Press shift + escape or use your mouse to confirm.";
} else if (Config.quickRestart === "enter") {
message = "Press shift + enter or use your mouse to confirm.";
}
Notifications.add(
`Quick restart disabled in long tests. ${message}`,
Expand Down Expand Up @@ -537,6 +539,7 @@ export async function init(): Promise<void> {

let hasTab = false;
let hasNumbers = false;
let hasNewline = false;

for (const word of generatedWords) {
if (/\t/g.test(word) && !hasTab) {
Expand All @@ -545,10 +548,14 @@ export async function init(): Promise<void> {
if (/\d/g.test(word) && !hasNumbers) {
hasNumbers = true;
}
if (/\n/g.test(word) && !hasNewline) {
hasNewline = true;
}
}

TestWords.setHasTab(hasTab);
TestWords.setHasNumbers(hasNumbers);
TestWords.setHasNewline(hasNewline);

if (beforeHasNumbers !== hasNumbers) {
Keymap.refresh();
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/ts/test/test-words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Words {

export const words = new Words();
export let hasTab = false;
export let hasNewline = false;
export let hasNumbers = false;
export let randomQuote = null as unknown as MonkeyTypes.Quote;

Expand All @@ -77,6 +78,10 @@ export function setHasTab(tf: boolean): void {
hasTab = tf;
}

export function setHasNewline(tf: boolean): void {
hasNewline = tf;
}

export function setHasNumbers(tf: boolean): void {
hasNumbers = tf;
}
2 changes: 1 addition & 1 deletion frontend/src/ts/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ declare namespace MonkeyTypes {
showLiveWpm: boolean;
showTimerProgress: boolean;
smoothCaret: SmoothCaretMode;
quickRestart: "off" | "esc" | "tab";
quickRestart: "off" | "esc" | "tab" | "enter";
punctuation: boolean;
numbers: boolean;
words: WordsModes;
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/ts/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ function updateKeytips(): void {
$("footer .keyTips").html(`
<key>tab</key> - restart test<br>
<key>esc</key> or <key>${modifierKey}</key>+<key>shift</key>+<key>p</key> - command line`);
} else if (Config.quickRestart === "enter") {
$("footer .keyTips").html(`
<key>enter</key> - restart test<br>
<key>esc</key> or <key>${modifierKey}</key>+<key>shift</key>+<key>p</key> - command line`);
} else {
$("footer .keyTips").html(`
<key>tab</key> + <key>enter</key> - restart test<br>
Expand Down

0 comments on commit 78ecdcf

Please sign in to comment.