From 0e8caaa47c051a9bfabd72ecb7d9f1b5abf2238a Mon Sep 17 00:00:00 2001 From: Jack Date: Wed, 2 Dec 2020 13:38:23 +0000 Subject: [PATCH] fixed line scrolling too early in some cases. fixed #656 --- src/js/script.js | 174 ++++++++++++++++++++++++----------------------- 1 file changed, 89 insertions(+), 85 deletions(-) diff --git a/src/js/script.js b/src/js/script.js index e190abe1d..cf65f8295 100644 --- a/src/js/script.js +++ b/src/js/script.js @@ -4331,91 +4331,6 @@ $(document).keydown((event) => { if (currentInput === "") return; event.preventDefault(); let currentWord = wordsList[currentWordIndex]; - if (!config.showAllLines || config.mode == "time") { - let currentTop = Math.floor( - document.querySelectorAll("#words .word")[currentWordElementIndex] - .offsetTop - ); - let nextTop; - try { - nextTop = Math.floor( - document.querySelectorAll("#words .word")[ - currentWordElementIndex + 1 - ].offsetTop - ); - } catch (e) { - nextTop = 0; - } - - if ((nextTop > currentTop || activeWordJumped) && !lineTransition) { - //last word of the line - if (currentTestLine > 0) { - let hideBound = currentTop; - if (activeWordJumped) { - hideBound = activeWordTopBeforeJump; - } - activeWordJumped = false; - - let toHide = []; - let wordElements = $("#words .word"); - for (let i = 0; i < currentWordElementIndex + 1; i++) { - if ($(wordElements[i]).hasClass("hidden")) continue; - let forWordTop = Math.floor(wordElements[i].offsetTop); - if (forWordTop < hideBound - 10) { - toHide.push($($("#words .word")[i])); - } - } - const wordHeight = $(document.querySelector(".word")).outerHeight( - true - ); - if (config.smoothLineScroll && toHide.length > 0) { - lineTransition = true; - $("#words").prepend( - `
` - ); - $("#words .smoothScroller").animate( - { - height: 0, - }, - 125, - () => { - $("#words .smoothScroller").remove(); - } - ); - $("#paceCaret").animate( - { - top: - document.querySelector("#paceCaret").offsetTop - wordHeight, - }, - 125 - ); - $("#words").animate( - { - marginTop: `-${wordHeight}px`, - }, - 125, - () => { - activeWordTop = document.querySelector("#words .active") - .offsetTop; - - currentWordElementIndex -= toHide.length; - lineTransition = false; - toHide.forEach((el) => el.remove()); - $("#words").css("marginTop", "0"); - } - ); - } else { - toHide.forEach((el) => el.remove()); - currentWordElementIndex -= toHide.length; - $("#paceCaret").css({ - top: - document.querySelector("#paceCaret").offsetTop - wordHeight, - }); - } - } - currentTestLine++; - } - } //end of line wrap if (activeFunBox === "layoutfluid" && config.mode !== "time") { const layouts = ["qwerty", "dvorak", "colemak"]; let index = 0; @@ -4520,8 +4435,97 @@ $(document).keydown((event) => { currentKeypress.count++; currentKeypress.words.push(currentWordIndex); } + correctedHistory.push(currentCorrected); currentCorrected = ""; + + if (!config.showAllLines || config.mode == "time") { + let currentTop = Math.floor( + document.querySelectorAll("#words .word")[currentWordElementIndex - 1] + .offsetTop + ); + let nextTop; + try { + nextTop = Math.floor( + document.querySelectorAll("#words .word")[currentWordElementIndex] + .offsetTop + ); + } catch (e) { + nextTop = 0; + } + + if ((nextTop > currentTop || activeWordJumped) && !lineTransition) { + //last word of the line + if (currentTestLine > 0) { + let hideBound = currentTop; + if (activeWordJumped) { + hideBound = activeWordTopBeforeJump; + } + activeWordJumped = false; + + let toHide = []; + let wordElements = $("#words .word"); + for (let i = 0; i < currentWordElementIndex; i++) { + if ($(wordElements[i]).hasClass("hidden")) continue; + let forWordTop = Math.floor(wordElements[i].offsetTop); + if (forWordTop < hideBound - 10) { + toHide.push($($("#words .word")[i])); + } + } + const wordHeight = $(document.querySelector(".word")).outerHeight( + true + ); + if (config.smoothLineScroll && toHide.length > 0) { + lineTransition = true; + $("#words").prepend( + `
` + ); + $("#words .smoothScroller").animate( + { + height: 0, + }, + 125, + () => { + $("#words .smoothScroller").remove(); + } + ); + $("#paceCaret").animate( + { + top: + document.querySelector("#paceCaret").offsetTop - wordHeight, + }, + 125 + ); + $("#words").animate( + { + marginTop: `-${wordHeight}px`, + }, + 125, + () => { + activeWordTop = document.querySelector("#words .active") + .offsetTop; + + currentWordElementIndex -= toHide.length; + lineTransition = false; + toHide.forEach((el) => el.remove()); + $("#words").css("marginTop", "0"); + } + ); + } else { + toHide.forEach((el) => el.remove()); + currentWordElementIndex -= toHide.length; + $("#paceCaret").css({ + top: + document.querySelector("#paceCaret").offsetTop - wordHeight, + }); + } + } + currentTestLine++; + } + } //end of line wrap + + updateCaretPosition(); + if (config.keymapMode === "react") { flashPressedKeymapKey(event.code, true); } else if (config.keymapMode === "next") {