fixed control backspace not interacting properly with punctuation

This commit is contained in:
Jack 2021-07-01 16:52:13 +01:00
parent 40475b5699
commit 3a3a215245
2 changed files with 60 additions and 21 deletions

View file

@ -141,31 +141,59 @@ function handleBackspace(event) {
}
} else {
if (Config.confidenceMode === "max") return;
if (event["ctrlKey"] || event["altKey"]) {
if (event["ctrlKey"] || event["altKey"] || event.metaKey) {
Replay.addReplayEvent("clearWord");
let limiter = " ";
if (
TestLogic.input.current.lastIndexOf("-") >
TestLogic.input.current.lastIndexOf(" ")
)
limiter = "-";
// let limiter = " ";
// if (
// TestLogic.input.current.lastIndexOf("-") >
// TestLogic.input.current.lastIndexOf(" ")
// )
// limiter = "-";
let split = TestLogic.input.current.replace(/ +/g, " ").split(limiter);
if (split[split.length - 1] == "") {
split.pop();
}
let addlimiter = false;
if (split.length > 1) {
addlimiter = true;
}
split.pop();
TestLogic.input.setCurrent(split.join(limiter));
// let split = TestLogic.input.current.replace(/ +/g, " ").split(limiter);
// if (split[split.length - 1] == "") {
// split.pop();
// }
// let addlimiter = false;
// if (split.length > 1) {
// addlimiter = true;
// }
// split.pop();
// TestLogic.input.setCurrent(split.join(limiter));
if (addlimiter) {
TestLogic.input.appendCurrent(limiter);
// if (addlimiter) {
// TestLogic.input.appendCurrent(limiter);
// }
if (/^\W*$/g.test(TestLogic.input.getCurrent())) {
//pop current and previous
TestLogic.input.resetCurrent();
TestLogic.input.popHistory();
TestLogic.corrected.popHistory();
TestUI.updateWordElement(!Config.blindMode);
TestLogic.words.decreaseCurrentIndex();
Replay.addReplayEvent("backWord");
TestUI.setCurrentWordElementIndex(TestUI.currentWordElementIndex - 1);
TestUI.updateActiveElement(true);
Funbox.toggleScript(TestLogic.words.getCurrent());
TestUI.updateWordElement(!Config.blindMode);
TestLogic.input.resetCurrent();
TestLogic.input.popHistory();
TestLogic.corrected.popHistory();
} else {
const regex = new RegExp("\\W", "g");
let input = TestLogic.input.getCurrent();
regex.test(input);
// let puncIndex = regex.lastIndex;
let puncIndex = input.lastIndexOfRegex(/\W/g);
while (/\W/g.test(input.slice(-1))) {
input = input.substring(0, input.length - 1);
}
puncIndex = input.lastIndexOfRegex(/\W/g);
TestLogic.input.setCurrent(input.substring(0, puncIndex + 1));
}
} else if (event.metaKey) {
TestLogic.input.resetCurrent();
} else {
TestLogic.input.setCurrent(
TestLogic.input.current.substring(0, TestLogic.input.current.length - 1)

View file

@ -745,3 +745,14 @@ function countAllKeys(obj) {
keys.forEach((key) => (sum += countAllKeys(obj[key])));
return sum;
}
//https://stackoverflow.com/questions/273789/is-there-a-version-of-javascripts-string-indexof-that-allows-for-regular-expr
export function regexIndexOf(string, regex, startpos) {
var indexOf = string.substring(startpos || 0).search(regex);
return indexOf >= 0 ? indexOf + (startpos || 0) : indexOf;
}
String.prototype.lastIndexOfRegex = function (regex) {
var match = this.match(regex);
return match ? this.lastIndexOf(match[match.length - 1]) : -1;
};