mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-11-17 14:19:40 +08:00
replaced try catch with optional operators and return
This commit is contained in:
parent
ac68c543b8
commit
3ff21797dd
1 changed files with 84 additions and 85 deletions
|
|
@ -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('<letter style="opacity: 0;">_</letter>');
|
||||
//insert temporary character so the caret will work in zen mode
|
||||
let activeWordEmpty = $("#words .active").children().length == 0;
|
||||
if (activeWordEmpty) {
|
||||
$("#words .active").append('<letter style="opacity: 0;">_</letter>');
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue