From 3ff21797dd72f8b780bc317720c206de1f26625a Mon Sep 17 00:00:00 2001 From: Miodec Date: Mon, 31 Jan 2022 23:23:34 +0100 Subject: [PATCH] replaced try catch with optional operators and return --- src/js/test/caret.js | 169 +++++++++++++++++++++---------------------- 1 file changed, 84 insertions(+), 85 deletions(-) diff --git a/src/js/test/caret.js b/src/js/test/caret.js index 127b1c307..33856aab2 100644 --- a/src/js/test/caret.js +++ b/src/js/test/caret.js @@ -44,92 +44,91 @@ export async function updatePosition() { if (currentLetterIndex == -1) { currentLetterIndex = 0; } - try { - //insert temporary character so the caret will work in zen mode - let activeWordEmpty = $("#words .active").children().length == 0; - if (activeWordEmpty) { - $("#words .active").append('_'); + //insert temporary character so the caret will work in zen mode + let activeWordEmpty = $("#words .active").children().length == 0; + if (activeWordEmpty) { + $("#words .active").append('_'); + } + + let currentWordNodeList = document + ?.querySelector("#words .active") + ?.querySelectorAll("letter"); + + if (!currentWordNodeList) return; + + let currentLetter = currentWordNodeList[currentLetterIndex]; + if (inputLen > currentWordNodeList.length) { + currentLetter = currentWordNodeList[currentWordNodeList.length - 1]; + } + + if (Config.mode != "zen" && $(currentLetter).length == 0) return; + const currentLanguage = await Misc.getCurrentLanguage(); + const isLanguageLeftToRight = currentLanguage.leftToRight; + let currentLetterPosLeft = isLanguageLeftToRight + ? currentLetter.offsetLeft + : currentLetter.offsetLeft + $(currentLetter).width(); + let currentLetterPosTop = currentLetter.offsetTop; + let letterHeight = $(currentLetter).height(); + let newTop = 0; + let newLeft = 0; + + newTop = currentLetterPosTop - Math.round(letterHeight / 5); + if (inputLen == 0) { + newLeft = isLanguageLeftToRight + ? currentLetterPosLeft - caret.width() / 2 + : currentLetterPosLeft + caret.width() / 2; + } else { + newLeft = isLanguageLeftToRight + ? currentLetterPosLeft + $(currentLetter).width() - caret.width() / 2 + : currentLetterPosLeft - $(currentLetter).width() + caret.width() / 2; + } + + let smoothlinescroll = $("#words .smoothScroller").height(); + if (smoothlinescroll === undefined) smoothlinescroll = 0; + + if (Config.smoothCaret) { + caret.stop(true, false).animate( + { + top: newTop - smoothlinescroll, + left: newLeft, + }, + TestTimer.slowTimer ? 0 : 100 + ); + } else { + caret.stop(true, true).animate( + { + top: newTop - smoothlinescroll, + left: newLeft, + }, + 0 + ); + } + + if (Config.showAllLines) { + let browserHeight = window.innerHeight; + let middlePos = browserHeight / 2 - $("#caret").outerHeight() / 2; + let contentHeight = document.body.scrollHeight; + + if ( + newTop >= middlePos && + contentHeight > browserHeight && + TestLogic.active + ) { + let newscrolltop = newTop - middlePos / 2; + // console.log('---------'); + // console.log(newTop); + // console.log(middlePos); + // console.log(browserHeight); + // console.log(contentHeight); + window.scrollTo({ + left: 0, + top: newscrolltop, + behavior: "smooth", + }); } - - let currentWordNodeList = document - .querySelector("#words .active") - .querySelectorAll("letter"); - let currentLetter = currentWordNodeList[currentLetterIndex]; - if (inputLen > currentWordNodeList.length) { - currentLetter = currentWordNodeList[currentWordNodeList.length - 1]; - } - - if (Config.mode != "zen" && $(currentLetter).length == 0) return; - const currentLanguage = await Misc.getCurrentLanguage(); - const isLanguageLeftToRight = currentLanguage.leftToRight; - let currentLetterPosLeft = isLanguageLeftToRight - ? currentLetter.offsetLeft - : currentLetter.offsetLeft + $(currentLetter).width(); - let currentLetterPosTop = currentLetter.offsetTop; - let letterHeight = $(currentLetter).height(); - let newTop = 0; - let newLeft = 0; - - newTop = currentLetterPosTop - Math.round(letterHeight / 5); - if (inputLen == 0) { - newLeft = isLanguageLeftToRight - ? currentLetterPosLeft - caret.width() / 2 - : currentLetterPosLeft + caret.width() / 2; - } else { - newLeft = isLanguageLeftToRight - ? currentLetterPosLeft + $(currentLetter).width() - caret.width() / 2 - : currentLetterPosLeft - $(currentLetter).width() + caret.width() / 2; - } - - let smoothlinescroll = $("#words .smoothScroller").height(); - if (smoothlinescroll === undefined) smoothlinescroll = 0; - - if (Config.smoothCaret) { - caret.stop(true, false).animate( - { - top: newTop - smoothlinescroll, - left: newLeft, - }, - TestTimer.slowTimer ? 0 : 100 - ); - } else { - caret.stop(true, true).animate( - { - top: newTop - smoothlinescroll, - left: newLeft, - }, - 0 - ); - } - - if (Config.showAllLines) { - let browserHeight = window.innerHeight; - let middlePos = browserHeight / 2 - $("#caret").outerHeight() / 2; - let contentHeight = document.body.scrollHeight; - - if ( - newTop >= middlePos && - contentHeight > browserHeight && - TestLogic.active - ) { - let newscrolltop = newTop - middlePos / 2; - // console.log('---------'); - // console.log(newTop); - // console.log(middlePos); - // console.log(browserHeight); - // console.log(contentHeight); - window.scrollTo({ - left: 0, - top: newscrolltop, - behavior: "smooth", - }); - } - } - if (activeWordEmpty) { - $("#words .active").children().remove(); - } - } catch (e) { - console.log("could not move caret: " + e.message); + } + if (activeWordEmpty) { + $("#words .active").children().remove(); } }