diff --git a/gulpfile.js b/gulpfile.js index 3c0a82e16..842b1144c 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -100,6 +100,7 @@ const refactoredSrc = [ "./src/js/shift-tracker.js", "./src/js/test/test-stats.js", "./src/js/theme-colors.js", + "./src/js/test/out-of-focus.js", ]; //legacy files diff --git a/src/js/global-dependencies.js b/src/js/global-dependencies.js index 847505c5f..227011d20 100644 --- a/src/js/global-dependencies.js +++ b/src/js/global-dependencies.js @@ -38,3 +38,4 @@ import * as CustomText from "./custom-text"; import * as ShiftTracker from "./shift-tracker"; import * as TestStats from "./test-stats"; import * as ThemeColors from "./theme-colors"; +import * as OutOfFocus from "./out-of-focus"; diff --git a/src/js/misc.js b/src/js/misc.js index 714246dde..16aab71ab 100644 --- a/src/js/misc.js +++ b/src/js/misc.js @@ -691,3 +691,10 @@ export function canQuickRestart(mode, words, time, CustomText) { return false; } } + +export function clearTimeouts(timeouts) { + timeouts.forEach((to) => { + clearTimeout(to); + to = null; + }); +} diff --git a/src/js/script.js b/src/js/script.js index df56f6ded..079d26982 100644 --- a/src/js/script.js +++ b/src/js/script.js @@ -1793,7 +1793,7 @@ function showResult(difficultyFailed = false) { } $("#result .stats .key .bottom").text(testtime + "s"); $("#words").removeClass("blurred"); - $(".outOfFocusWarning").addClass("hidden"); + OutOfFocus.hide(); $("#result .stats .key .bottom").text( stats.correctChars + stats.correctSpaces + @@ -4635,32 +4635,16 @@ $("#wordsInput").keypress((event) => { event.preventDefault(); }); -let outOfFocusTimeouts = []; - -function clearTimeouts(timeouts) { - timeouts.forEach((to) => { - clearTimeout(to); - to = null; - }); -} - $("#wordsInput").on("focus", () => { if (!resultVisible && config.showOutOfFocusWarning) { - $("#words").css("transition", "none").removeClass("blurred"); - $(".outOfFocusWarning").addClass("hidden"); - clearTimeouts(outOfFocusTimeouts); + OutOfFocus.hide(); } showCaret(); }); $("#wordsInput").on("focusout", () => { if (!resultVisible && config.showOutOfFocusWarning) { - outOfFocusTimeouts.push( - setTimeout(() => { - $("#words").css("transition", "0.25s").addClass("blurred"); - $(".outOfFocusWarning").removeClass("hidden"); - }, 1000) - ); + OutOfFocus.show(); } hideCaret(); }); diff --git a/src/js/test/out-of-focus.js b/src/js/test/out-of-focus.js new file mode 100644 index 000000000..11c30703d --- /dev/null +++ b/src/js/test/out-of-focus.js @@ -0,0 +1,18 @@ +import * as Misc from "./misc"; + +let outOfFocusTimeouts = []; + +export function hide() { + $("#words").css("transition", "none").removeClass("blurred"); + $(".outOfFocusWarning").addClass("hidden"); + Misc.clearTimeouts(outOfFocusTimeouts); +} + +export function show() { + outOfFocusTimeouts.push( + setTimeout(() => { + $("#words").css("transition", "0.25s").addClass("blurred"); + $(".outOfFocusWarning").removeClass("hidden"); + }, 1000) + ); +} diff --git a/src/js/userconfig.js b/src/js/userconfig.js index 374ab4dd5..27995cec9 100644 --- a/src/js/userconfig.js +++ b/src/js/userconfig.js @@ -358,9 +358,7 @@ function setAlwaysShowCPM(val, nosave) { function toggleShowOutOfFocusWarning() { config.showOutOfFocusWarning = !config.showOutOfFocusWarning; if (!config.showOutOfFocusWarning) { - $("#words").css("transition", "none").removeClass("blurred"); - $(".outOfFocusWarning").addClass("hidden"); - clearTimeouts(outOfFocusTimeouts); + OutOfFocus.hide(); } saveConfigToCookie(); } @@ -371,9 +369,7 @@ function setShowOutOfFocusWarning(val, nosave) { } config.showOutOfFocusWarning = val; if (!config.showOutOfFocusWarning) { - $("#words").css("transition", "none").removeClass("blurred"); - $(".outOfFocusWarning").addClass("hidden"); - clearTimeouts(outOfFocusTimeouts); + OutOfFocus.hide(); } if (!nosave) saveConfigToCookie(); }