From 78ecdcf91eb2593af01716bf5072695450c16818 Mon Sep 17 00:00:00 2001 From: Miodec Date: Thu, 2 Nov 2023 14:04:39 +0000 Subject: [PATCH] impr(quick restart): add "enter" as an option to quick restart --- backend/src/api/schemas/config-schema.ts | 2 +- .../src/ts/commandline/lists/quick-restart.ts | 8 ++++ frontend/src/ts/config.ts | 6 ++- .../src/ts/controllers/input-controller.ts | 48 ++++++++++++++++++- frontend/src/ts/elements/modes-notice.ts | 6 +++ frontend/src/ts/ready.ts | 2 +- frontend/src/ts/test/test-logic.ts | 7 +++ frontend/src/ts/test/test-words.ts | 5 ++ frontend/src/ts/types/types.d.ts | 2 +- frontend/src/ts/ui.ts | 4 ++ 10 files changed, 84 insertions(+), 6 deletions(-) diff --git a/backend/src/api/schemas/config-schema.ts b/backend/src/api/schemas/config-schema.ts index d2eec4851..7171c691a 100644 --- a/backend/src/api/schemas/config-schema.ts +++ b/backend/src/api/schemas/config-schema.ts @@ -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), diff --git a/frontend/src/ts/commandline/lists/quick-restart.ts b/frontend/src/ts/commandline/lists/quick-restart.ts index 84ff335b2..803a16fbf 100644 --- a/frontend/src/ts/commandline/lists/quick-restart.ts +++ b/frontend/src/ts/commandline/lists/quick-restart.ts @@ -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", diff --git a/frontend/src/ts/config.ts b/frontend/src/ts/config.ts index a0dfb08a5..94242e223 100644 --- a/frontend/src/ts/config.ts +++ b/frontend/src/ts/config.ts @@ -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; } diff --git a/frontend/src/ts/controllers/input-controller.ts b/frontend/src/ts/controllers/input-controller.ts index f95792359..e10f37f8f 100644 --- a/frontend/src/ts/controllers/input-controller.ts +++ b/frontend/src/ts/controllers/input-controller.ts @@ -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; @@ -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) { diff --git a/frontend/src/ts/elements/modes-notice.ts b/frontend/src/ts/elements/modes-notice.ts index 8f0c2ef09..6ea54ed0a 100644 --- a/frontend/src/ts/elements/modes-notice.ts +++ b/frontend/src/ts/elements/modes-notice.ts @@ -57,6 +57,12 @@ export async function update(): Promise { } } + if (TestWords.hasNewline && Config.quickRestart === "enter") { + $(".pageTest #testModesNotice").append( + `
shift + enter to restart
` + ); + } + const customTextName = CustomTextState.getCustomTextName(); const isLong = CustomTextState.isCustomTextLong(); if (Config.mode === "custom" && customTextName !== "" && isLong) { diff --git a/frontend/src/ts/ready.ts b/frontend/src/ts/ready.ts index 837d90e05..fde9fd844 100644 --- a/frontend/src/ts/ready.ts +++ b/frontend/src/ts/ready.ts @@ -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")) { diff --git a/frontend/src/ts/test/test-logic.ts b/frontend/src/ts/test/test-logic.ts index a8b5e07d1..6a78fccf4 100644 --- a/frontend/src/ts/test/test-logic.ts +++ b/frontend/src/ts/test/test-logic.ts @@ -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}`, @@ -537,6 +539,7 @@ export async function init(): Promise { let hasTab = false; let hasNumbers = false; + let hasNewline = false; for (const word of generatedWords) { if (/\t/g.test(word) && !hasTab) { @@ -545,10 +548,14 @@ export async function init(): Promise { 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(); diff --git a/frontend/src/ts/test/test-words.ts b/frontend/src/ts/test/test-words.ts index 16f422d06..09c55dfbf 100644 --- a/frontend/src/ts/test/test-words.ts +++ b/frontend/src/ts/test/test-words.ts @@ -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; @@ -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; } diff --git a/frontend/src/ts/types/types.d.ts b/frontend/src/ts/types/types.d.ts index 6a6d7dbeb..6e9983630 100644 --- a/frontend/src/ts/types/types.d.ts +++ b/frontend/src/ts/types/types.d.ts @@ -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; diff --git a/frontend/src/ts/ui.ts b/frontend/src/ts/ui.ts index b5957a26a..6d483f6ab 100644 --- a/frontend/src/ts/ui.ts +++ b/frontend/src/ts/ui.ts @@ -32,6 +32,10 @@ function updateKeytips(): void { $("footer .keyTips").html(` tab - restart test
esc or ${modifierKey}+shift+p - command line`); + } else if (Config.quickRestart === "enter") { + $("footer .keyTips").html(` + enter - restart test
+ esc or ${modifierKey}+shift+p - command line`); } else { $("footer .keyTips").html(` tab + enter - restart test