avoid uncorrected typos at end of string

This commit is contained in:
John Bowdre 2024-08-02 16:19:45 -05:00
parent 42dabaa2f4
commit 797560c7a3

View file

@ -72,54 +72,17 @@ var typos = {
async function typo(element, text) {
var buffer = '';
var typo_active = false;
var tag_active = false;
var typing_typos = (element.dataset.typoChance) ? element.dataset.typoChance : 10;
var typing_speed = (element.dataset.typingDelay) ? element.dataset.typingDelay : 50;
var typing_jitter = (element.dataset.typingJitter) ? element.dataset.typingJitter : 15;
for (var i = 0; i < text.length; i++) {
// Get the letter that were supposed to type
// Get the letter that we're supposed to type
letter = text.charAt(i);
// TODO: actual support for html or markup or whatever
/*
// Handle elements/markup
if(letter == '<' && (
text.charAt(i+1) == 's' ||
text.charAt(i+1) == 'p' ||
text.charAt(i+1) == 'a' ||
text.charAt(i+1) == '/' ||
text.charAt(i+1) == 'i')
) {
tag_active = true;
}
if(tag_active) {
buffer = buffer + letter;
element.innerHTML = buffer;
if(letter == '>' && (
text.charAt(i-1) == 'n' ||
text.charAt(i-1) == 'a' ||
text.charAt(i-1) == 'p' ||
text.charAt(i+1) == '"' ||
text.charAt(i+1) == '/')
) {
tag_active = false;
await sleep(typing_speed);
}
continue;
}
*/
// Trigger a typo
if(chance(typing_typos) && typo_active === false && i > 1) {
if(chance(typing_typos) && typo_active === false && i > 1 && i < text.length - 3) {
if(typeof typos[letter] !== 'undefined') {
// Swap the letter with a random typo
typo = typos[letter][Math.floor(Math.random() * typos[letter].length)];
@ -130,7 +93,7 @@ async function typo(element, text) {
element.innerHTML = buffer;
typo_active = true;
var seed = num_between(2,5);
var seed = num_between(2,5); // Reduced max seed to ensure correction
realization_delay = seed;
realization_delay_counter = seed;
}
@ -153,13 +116,10 @@ async function typo(element, text) {
await sleep(delay);
if(typo_active) {
realization_delay_counter--;
if(realization_delay_counter == 0) {
for (var k = 0; k < seed+1; k++) {
// Pause at realization of typo
await sleep(typing_jitter);
@ -182,8 +142,20 @@ async function typo(element, text) {
}
}
// Whatever you do here will happen when the typing is finished
//do_something();
// Ensure any remaining typo is corrected
if(typo_active) {
await sleep(typing_jitter * 2);
for (var k = 0; k < seed+1; k++) {
buffer = buffer.substring(0, buffer.length - 1);
element.innerHTML = buffer;
await sleep(30);
}
for (var k = 0; k < seed; k++) {
buffer += text.charAt(text.length - seed + k);
element.innerHTML = buffer;
await sleep(typing_speed);
}
}
return new Promise(resolve => setTimeout(resolve, 1));
}