From fd5ccb2f1d81f442b724a64a66d264413cb7f7c0 Mon Sep 17 00:00:00 2001 From: Miodec Date: Tue, 16 Mar 2021 09:21:21 +0000 Subject: [PATCH] added a live-wpm module #495 --- gulpfile.js | 1 + src/js/global-dependencies.js | 1 + src/js/script.js | 75 ++--------------------------------- src/js/test/live-wpm.js | 65 ++++++++++++++++++++++++++++++ src/js/userconfig.js | 10 +++++ 5 files changed, 81 insertions(+), 71 deletions(-) create mode 100644 src/js/test/live-wpm.js diff --git a/gulpfile.js b/gulpfile.js index b9abd269c..79fd601a8 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -113,6 +113,7 @@ const refactoredSrc = [ "./src/js/test/practise-missed.js", "./src/js/test/test-ui.js", "./src/js/test/keymap.js", + "./src/js/test/live-wpm.js", ]; //legacy files diff --git a/src/js/global-dependencies.js b/src/js/global-dependencies.js index 72de2f27d..1fd97e3e6 100644 --- a/src/js/global-dependencies.js +++ b/src/js/global-dependencies.js @@ -41,3 +41,4 @@ import * as AccountIcon from "./account-icon"; import * as PractiseMissed from "./practise-missed"; import * as TestUI from "./test-ui"; import * as Keymap from "./keymap"; +import * as LiveWpm from "./live-wpm"; diff --git a/src/js/script.js b/src/js/script.js index 1868d35c2..ebe43ce6c 100644 --- a/src/js/script.js +++ b/src/js/script.js @@ -1260,7 +1260,7 @@ function showResult(difficultyFailed = false) { testActive = false; Focus.set(false); Caret.hide(); - hideLiveWpm(); + LiveWpm.hide(); hideLiveAcc(); hideTimer(); Keymap.hide(); @@ -2252,7 +2252,7 @@ function startTest() { restartTimer(); showTimer(); $("#liveWpm").text("0"); - showLiveWpm(); + LiveWpm.show(); showLiveAcc(); updateTimer(); clearTimeout(timer); @@ -2281,7 +2281,7 @@ function startTest() { updateTimer(); } let wpmAndRaw = liveWpmAndRaw(); - updateLiveWpm(wpmAndRaw.wpm, wpmAndRaw.raw); + LiveWpm.update(wpmAndRaw.wpm, wpmAndRaw.raw); TestStats.pushToWpmHistory(wpmAndRaw.wpm); TestStats.pushToRawHistory(wpmAndRaw.raw); Monkey.updateFastOpacity(wpmAndRaw.wpm); @@ -2462,7 +2462,7 @@ function restartTest(withSameWordset = false, nosave = false, event) { Focus.set(false); Caret.hide(); testActive = false; - hideLiveWpm(); + LiveWpm.hide(); hideLiveAcc(); hideTimer(); bailout = false; @@ -2808,23 +2808,6 @@ function liveWpmAndRaw() { }; } -function updateLiveWpm(wpm, raw) { - if (!testActive || !Config.showLiveWpm) { - hideLiveWpm(); - } else { - showLiveWpm(); - } - let number = wpm; - if (Config.blindMode) { - number = raw; - } - if (Config.alwaysShowCPM) { - number = Math.round(number * 5); - } - document.querySelector("#miniTimerAndLiveWpm .wpm").innerHTML = number; - document.querySelector("#liveWpm").innerHTML = number; -} - function updateLiveAcc(acc) { if (!testActive || !Config.showLiveAcc) { hideLiveAcc(); @@ -2839,56 +2822,6 @@ function updateLiveAcc(acc) { document.querySelector("#liveAcc").innerHTML = number + "%"; } -function showLiveWpm() { - if (!Config.showLiveWpm) return; - if (!testActive) return; - if (Config.timerStyle === "mini") { - // $("#miniTimerAndLiveWpm .wpm").css("opacity", Config.timerOpacity); - if (!$("#miniTimerAndLiveWpm .wpm").hasClass("hidden")) return; - $("#miniTimerAndLiveWpm .wpm") - .removeClass("hidden") - .css("opacity", 0) - .animate( - { - opacity: Config.timerOpacity, - }, - 125 - ); - } else { - // $("#liveWpm").css("opacity", Config.timerOpacity); - if (!$("#liveWpm").hasClass("hidden")) return; - $("#liveWpm").removeClass("hidden").css("opacity", 0).animate( - { - opacity: Config.timerOpacity, - }, - 125 - ); - } -} - -function hideLiveWpm() { - // $("#liveWpm").css("opacity", 0); - // $("#miniTimerAndLiveWpm .wpm").css("opacity", 0); - $("#liveWpm").animate( - { - opacity: Config.timerOpacity, - }, - 125, - () => { - $("#liveWpm").addClass("hidden"); - } - ); - $("#miniTimerAndLiveWpm .wpm").animate( - { - opacity: Config.timerOpacity, - }, - 125, - () => { - $("#miniTimerAndLiveWpm .wpm").addClass("hidden"); - } - ); -} - function showLiveAcc() { if (!Config.showLiveAcc) return; if (!testActive) return; diff --git a/src/js/test/live-wpm.js b/src/js/test/live-wpm.js new file mode 100644 index 000000000..220610509 --- /dev/null +++ b/src/js/test/live-wpm.js @@ -0,0 +1,65 @@ +import Config from "./config"; + +export function update(wpm, raw) { + // if (!testActive || !Config.showLiveWpm) { + // hideLiveWpm(); + // } else { + // showLiveWpm(); + // } + let number = wpm; + if (Config.blindMode) { + number = raw; + } + if (Config.alwaysShowCPM) { + number = Math.round(number * 5); + } + document.querySelector("#miniTimerAndLiveWpm .wpm").innerHTML = number; + document.querySelector("#liveWpm").innerHTML = number; +} + +export function show() { + if (!Config.showLiveWpm) return; + if (Config.timerStyle === "mini") { + // $("#miniTimerAndLiveWpm .wpm").css("opacity", Config.timerOpacity); + if (!$("#miniTimerAndLiveWpm .wpm").hasClass("hidden")) return; + $("#miniTimerAndLiveWpm .wpm") + .removeClass("hidden") + .css("opacity", 0) + .animate( + { + opacity: Config.timerOpacity, + }, + 125 + ); + } else { + // $("#liveWpm").css("opacity", Config.timerOpacity); + if (!$("#liveWpm").hasClass("hidden")) return; + $("#liveWpm").removeClass("hidden").css("opacity", 0).animate( + { + opacity: Config.timerOpacity, + }, + 125 + ); + } +} + +export function hide() { + $("#liveWpm").animate( + { + opacity: Config.timerOpacity, + }, + 125, + () => { + $("#liveWpm").addClass("hidden"); + } + ); + $("#miniTimerAndLiveWpm .wpm").animate( + { + opacity: Config.timerOpacity, + }, + 125, + () => { + $("#miniTimerAndLiveWpm .wpm").addClass("hidden"); + } + ); +} diff --git a/src/js/userconfig.js b/src/js/userconfig.js index d8070c3c3..8810c122b 100644 --- a/src/js/userconfig.js +++ b/src/js/userconfig.js @@ -586,11 +586,21 @@ function setShowLiveWpm(live, nosave) { live = false; } ConfigSet.showLiveWpm(live); + if (live) { + LiveWpm.show(); + } else { + LiveWpm.hide(); + } if (!nosave) saveConfigToCookie(); } function toggleShowLiveWpm() { ConfigSet.showLiveWpm(!Config.showLiveWpm); + if (Config.showLiveWpm) { + LiveWpm.show(); + } else { + LiveWpm.hide(); + } saveConfigToCookie(); }