From 73d27f67db863e4060f784f1006d455e77fe4466 Mon Sep 17 00:00:00 2001 From: Miodec Date: Wed, 13 Jan 2021 03:27:59 +0000 Subject: [PATCH] custom text newline support --- src/js/script.js | 103 +++++++++++++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 35 deletions(-) diff --git a/src/js/script.js b/src/js/script.js index 56ff92331..da5580d91 100644 --- a/src/js/script.js +++ b/src/js/script.js @@ -914,11 +914,15 @@ function showWords() { let wordsHTML = ""; for (let i = 0; i < wordsList.length; i++) { - wordsHTML += "
"; - for (let c = 0; c < wordsList[i].length; c++) { - wordsHTML += "" + wordsList[i].charAt(c) + ""; + if(wordsList[i] === "\n"){ + wordsHTML += "
"; + }else{ + wordsHTML += "
"; + for (let c = 0; c < wordsList[i].length; c++) { + wordsHTML += "" + wordsList[i].charAt(c) + ""; + } + wordsHTML += "
"; } - wordsHTML += "
"; } $("#words").html(wordsHTML); @@ -3366,8 +3370,14 @@ function toggleResultWordsDisplay() { async function loadWordsHistory() { $("#resultWordsHistory .words").empty(); let wordsHTML = ""; + let newlineoffset = 0; for (let i = 0; i < inputHistory.length + 2; i++) { let input = inputHistory[i]; + let word = wordsList[i + newlineoffset]; + if(word === "\n"){ + newlineoffset++; + word = wordsList[i + newlineoffset]; + } let wordEl = ""; try { if (input === "") throw new Error("empty input word"); @@ -3382,33 +3392,33 @@ async function loadWordsHistory() { } if (i === inputHistory.length - 1) { //last word - let word = { + let wordstats = { correct: 0, incorrect: 0, missed: 0, }; - for (let c = 0; c < wordsList[i].length; c++) { + for (let c = 0; c < word.length; c++) { if (c < inputHistory[i].length) { //on char that still has a word list pair - if (inputHistory[i][c] == wordsList[i][c]) { - word.correct++; + if (inputHistory[i][c] == word[c]) { + wordstats.correct++; } else { - word.incorrect++; + wordstats.incorrect++; } } else { //on char that is extra - word.missed++; + wordstats.missed++; } } - if (word.incorrect !== 0 || config.mode !== "time") { - if (input !== wordsList[i]) { + if (wordstats.incorrect !== 0 || config.mode !== "time") { + if (input !== word) { wordEl = `
`; } } } else { - if (input !== wordsList[i]) { + if (input !== word) { wordEl = `
`; @@ -3416,12 +3426,12 @@ async function loadWordsHistory() { } let loop; - if (input.length > wordsList[i].length) { + if (input.length > word.length) { //input is longer - extra characters possible (loop over input) loop = input.length; } else { //input is shorter or equal (loop over word list) - loop = wordsList[i].length; + loop = word.length; } for (let c = 0; c < loop; c++) { @@ -3439,26 +3449,26 @@ async function loadWordsHistory() { ) { extraCorrected = "extraCorrected"; } - if (wordsList[i][c] !== undefined) { - if (input[c] === wordsList[i][c]) { + if (word[c] !== undefined) { + if (input[c] === word[c]) { if (correctedChar === input[c] || correctedChar === undefined) { - wordEl += `${wordsList[i][c]}`; + wordEl += `${word[c]}`; } else { wordEl += `` + - wordsList[i][c] + + word[c] + ""; } } else { if (input[c] === currentInput) { wordEl += - "" + wordsList[i][c] + ""; + `` + word[c] + ""; } else if (input[c] === undefined) { - wordEl += "" + wordsList[i][c] + ""; + wordEl += "" + word[c] + ""; } else { wordEl += `` + - wordsList[i][c] + + word[c] + ""; } } @@ -3470,8 +3480,8 @@ async function loadWordsHistory() { } catch (e) { try { wordEl = "
"; - for (let c = 0; c < wordsList[i].length; c++) { - wordEl += "" + wordsList[i][c] + ""; + for (let c = 0; c < word.length; c++) { + wordEl += "" + word[c] + ""; } wordEl += "
"; } catch {} @@ -3792,7 +3802,9 @@ function showCustomTextPopup() { .css("opacity", 0) .removeClass("hidden") .animate({ opacity: 1 }, 100, () => { - $("#customTextPopup textarea").val(customText.join(" ")); + let newtext = customText.join(" "); + newtext = newtext.replace(/ \n /g,"\n"); + $("#customTextPopup textarea").val(newtext); $("#customTextPopup .wordcount input").val(customTextWordCount); $("#customTextPopup textarea").focus(); }); @@ -3839,8 +3851,12 @@ $("#customTextPopup textarea").keypress((e) => { $("#customTextPopup .button").click(() => { let text = $("#customTextPopup textarea").val(); text = text.trim(); - text = text.replace(/[\n\r\t ]/gm, " "); + text = text.replace(/[\r\t]/gm, " "); text = text.replace(/ +/gm, " "); + text = text.replace(/(\r\n)+/g,"\r\n"); + text = text.replace(/(\n)+/g,"\n"); + text = text.replace(/(\r)+/g,"\r"); + text = text.replace(/( *(\r\n|\r|\n) *)/g," \n "); if ($("#customTextPopup .typographyCheck input").prop("checked")) { text = Misc.cleanTypographySymbols(text); } @@ -4583,9 +4599,13 @@ $(document).keydown(function (event) { handleBackspace(event); } - //space - if (event.key === " " || (activeFunBox == "58008" && event.key === "Enter")) { - handleSpace(event); + if(event.key === "Enter" && activeFunBox === "58008"){ + event.key = " "; + } + + //space or enter + if (event.key === " " || event.key === "Enter"){ + handleSpace(event, (event.key === "Enter" ? true : false)); } handleAlpha(event); @@ -4643,6 +4663,7 @@ function handleBackspace(event) { inputHistory.length > 0 && currentWordElementIndex > 0 ) { + //if nothing is inputted and its not the first word if ( (inputHistory[currentWordIndex - 1] == wordsList[currentWordIndex - 1] && !config.freedomMode) || @@ -4664,6 +4685,9 @@ function handleBackspace(event) { } } currentWordIndex--; + if(wordsList[currentWordIndex] === "\n"){ + currentWordIndex--; + } currentWordElementIndex--; updateActiveElement(); updateWordElement(!config.blindMode); @@ -4701,9 +4725,12 @@ function handleBackspace(event) { updateCaretPosition(); } -function handleSpace(event) { +function handleSpace(event, isEnter) { if (!testActive) return; if (currentInput === "") return; + let nextWord = wordsList[currentWordIndex + 1]; + // if ((isEnter && nextWord !== "\n") && (isEnter && activeFunBox !== "58008")) return; + // if (!isEnter && nextWord === "\n") return; event.preventDefault(); let currentWord = wordsList[currentWordIndex]; if (activeFunBox === "layoutfluid" && config.mode !== "time") { @@ -4721,7 +4748,8 @@ function handleSpace(event) { } if (config.blindMode) $("#words .word.active letter").addClass("correct"); dontInsertSpace = true; - if (currentWord == currentInput) { + let correctSpaceEnter = ((isEnter && nextWord === "\n") || (!isEnter && nextWord !== "\n")); + if (currentWord == currentInput && correctSpaceEnter) { //correct word if ( paceCaret !== null && @@ -4762,7 +4790,7 @@ function handleSpace(event) { } accuracyStats.incorrect++; let cil = currentInput.length; - if (cil < wordsList[currentWordIndex].length) { + // if (cil <= wordsList[currentWordIndex].length) { if (cil >= currentCorrected.length) { currentCorrected += "_"; } else { @@ -4771,14 +4799,14 @@ function handleSpace(event) { "_" + currentCorrected.substring(cil + 1); } - } - if (config.stopOnError != "off") { + // } + if (config.stopOnError != "off" || !correctSpaceEnter) { if (config.difficulty == "expert" || config.difficulty == "master") { //failed due to diff when pressing space failTest(); return; } - if (config.stopOnError == "word") { + if (config.stopOnError == "word" || !correctSpaceEnter) { currentInput += " "; updateWordElement(true); updateCaretPosition(); @@ -4805,9 +4833,14 @@ function handleSpace(event) { } } + correctedHistory.push(currentCorrected); currentCorrected = ""; + if(nextWord === "\n"){ + currentWordIndex++; + } + if (!config.showAllLines || config.mode == "time") { let currentTop = Math.floor( document.querySelectorAll("#words .word")[currentWordElementIndex - 1]