Skip to content

Commit aad96f1

Browse files
committed
Fixed bug of Waiting after Writing all at once, minor improvements
1 parent 918c9a3 commit aad96f1

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

WriteIt.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ class WriteItNode {
206206
// TODO: Check that matching closing bracket is present!
207207
i = waitTempIndex;
208208
let secToWait = this.text.substring(i + 2, this.text.indexOf('}', i));
209-
this.waitIndex["default"][i] = secToWait;
209+
this.waitIndex["default"][i] = Number(secToWait);
210210
// this might return -1
211211
this.text = this.text.replace(this.text.substring(i, this.text.indexOf("}", i) + 1), "");
212-
temp += 2;
212+
temp += 1;
213213
} else if (writeTempIndex > -1 && (writeTempIndex < waitTempIndex || waitTempIndex == -1)) {
214214
// set write.
215215
i = writeTempIndex;
@@ -223,10 +223,13 @@ class WriteItNode {
223223
this.writeAllTextAtOnceIndex["default"][i + length] = i;
224224
}
225225
this.text = this.text.replace(this.text.substring(i, endIndex + 1), this.text.substring(i + 2, endIndex));
226-
temp += endIndex + 1;
226+
temp += endIndex;
227227
}
228228
}
229229
}
230+
console.log(this.text);
231+
console.log(this.waitIndex);
232+
console.log(this.writeAllTextAtOnceIndex);
230233
if (this.node.hasAttribute(WriteItJS.WRITEIT_REPLACE_NEXT)) {
231234
// Loop for all texts in WRITEIT_REPLACE_NEXT and also parse them.
232235
for (let iterator = 0; iterator < this.originalTexts.length; iterator++) {
@@ -352,44 +355,58 @@ class WriteItNode {
352355
* Adds/Removes a letter from "html";
353356
*/
354357
animate() {
358+
// return if animation is paused or stopped.
355359
if (this.timeout == -1) {
356360
return;
357361
}
358-
if (this.waitIndex[this.textsIndex == -1 ? "default" : this.textsIndex][this.index] != undefined && this.wait == false && (!this.reverse || this.node.hasAttribute(WriteItJS.WRITEIT_WAIT_IN_REVERSE))) {
359-
let waitingTime = this.waitIndex[this.textsIndex == -1 ? "default" : this.textsIndex][this.index];
360-
this.wait = true;
361-
this.node.innerHTML = this.text.substring(0, this.index + (this.reverse ? -1 : 1)) + this.writeitChar;
362-
this.index += this.reverse ? -1 : 1;
362+
// Wait if we need to wait at these position.
363+
if (this.waitIndex[this.textsIndex < 0 ? "default" : this.textsIndex][this.index] != undefined && !this.wait) {
364+
let secsToWait = this.waitIndex[this.textsIndex < 0 ? "default" : this.textsIndex][this.index];
365+
this.node.innerHTML = this.text.substring(0, this.index); // + this.writeitChar;
366+
if (this.reverse && this.node.hasAttribute(WriteItJS.WRITEIT_WRITE_ALL_IN_REVERSE)) {
367+
secsToWait = secsToWait * 1000;
368+
this.index --;
369+
} else if (!this.reverse) {
370+
secsToWait = secsToWait * 1000;
371+
this.index ++;
372+
} else {
373+
secsToWait = 0;
374+
}
363375
if (this.index <= 0 || this.index >= this.text.length) {
364376
this.handleIterationEnd();
365377
return;
366378
} else {
367379
// this.node.innerHTML += this.writeitChar;
368380
}
369-
this.timeout = this.setTimeout(() => { this.wait = false; this.animate(); }, waitingTime * 1000);
370-
return;
371-
} else if (this.wait == true) {
381+
this.timeout = this.setTimeout(() => { this.wait = false; this.animate(); }, secsToWait);
372382
return;
373383
}
384+
// return if already waiting!
385+
if(this.wait) return;
386+
387+
// Write all text at once.
374388
if (this.writeAllTextAtOnceIndex[this.textsIndex < 0 ? "default" : this.textsIndex][this.index] != undefined) {
375389
let destinationIndex = this.writeAllTextAtOnceIndex[this.textsIndex < 0 ? "default" : this.textsIndex][this.index];
376390
if (this.reverse && destinationIndex < this.index) {
377391
this.node.innerHTML = this.text.substring(0, destinationIndex);
378-
this.index = this.node.innerHTML.length - 1;
392+
this.index = destinationIndex - 1;
379393
} else if (!this.reverse && destinationIndex > this.index) {
380394
this.node.innerHTML = this.text.substring(0, destinationIndex);
381-
this.index = this.node.innerHTML.length;
395+
this.index = destinationIndex + 1;
382396
}
383397
if (this.index <= 0 || this.index >= this.text.length) {
384398
this.handleIterationEnd();
385399
return;
386400
} else {
387401
this.node.innerHTML += this.writeitChar;
388402
}
403+
this.nextIteration();
404+
return;
389405
}
390406

407+
391408
// Browser may have added ending tag so ignore it.
392-
let str = this.node.innerHTML.substring(0, this.index);
409+
let str = this.text.substring(0, this.index);
393410

394411
// Add HTML without writeit.
395412
this.node.innerHTML = str;

docs/terminal-app/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<canvas id="canvas"></canvas>
6565
</div>
6666
<div id="terminal">
67-
<p writeit-char="<span style='color: green'>▉</span>" writeit-animate writeit-next="#p1">$`<span style="color: green;">hacker@mypotatopc</span>:<span style='color: blue'>~</span>` ls</p>
67+
<!-- <p writeit-char="<span style='color: green'>▉</span>" writeit-animate writeit-next="#p1">$`<span style="color: green;">hacker@mypotatopc</span>:<span style='color: blue'>~</span>` ls</p>
6868
<div writeit-char="<span style='color: green'>▉</span>" writeit-animate writeit-has-prev writeit-hidden id="p1" writeit-next="#p2">$`
6969
<p>WriteIt.js</p>
7070
<p>Blind's Partner</p>
@@ -92,9 +92,9 @@
9292
<p>WriteIt.min.js</p>
9393
<p>search.sh</p>`
9494
</div>
95-
<p writeit-char="<span style='color: green'>▉</span>" writeit-speed="10"writeit-animate writeit-has-prev writeit-hidden id="p11" writeit-next="#p12">$`<span style="color: green;">hacker@mypotatopc</span>:<span style='color: blue'>WriteIt.js</span>` sudo ./search.sh</p>
96-
<div writeit-char="<span style='color: green'>▉</span>" writeit-speed="10"writeit-animate writeit-has-prev writeit-hidden id="p12" writeit-no-blink>
97-
<span>$`Crunching the internet to find the best text writing javascript library`</span> <span>.${1}.${1}.${1}.</span><br>
95+
<p writeit-char="<span style='color: green'>▉</span>" writeit-speed="10"writeit-animate writeit-has-prev writeit-hidden id="p11" writeit-next="#p12">$`<span style="color: green;">hacker@mypotatopc</span>:<span style='color: blue'>WriteIt.js</span>` sudo ./search.sh</p> -->
96+
<div writeit-char="<span style='color: green'>▉</span>" writeit-speed="10"writeit-animate writeit-hidden id="p12" writeit-no-blink>
97+
<span>$`Crunching the internet to find the best text writing javascript library` .${1}.${1}.${1}.</span><br>
9898
<p><span style="color: green;">WriteIt.js</span> is the best text writing javascript library!</p>
9999
<div>$`
100100
/***<br>

0 commit comments

Comments
 (0)