Skip to content

Commit

Permalink
Add code comments and extract delay function out of vue component
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathanjms committed Jun 24, 2023
1 parent 6ee24a4 commit cf8635e
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions src/components/auto-typer-vue/AutoTyperVue.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<script>
import { defineComponent } from "vue";
/**
* Delay by the given amount of milliseconds
* @param {number} ms
* @returns {Promise<void>}
*/
async function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export default defineComponent({
name: "AutoTyperVue",
emits: ["finished"],
Expand Down Expand Up @@ -91,33 +101,39 @@ export default defineComponent({
}
},
methods: {
delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
},
/**
* Start the auto typing
*/
async begin() {
if (typeof this.text === "string") {
this.textFeed = [this.text];
} else {
this.textFeed = [...this.text];
}
await this.delay(this.startDelay);
await delay(this.startDelay);
await this.writeBeginningWord();
this.autoType();
},
/**
* Write the beginning word, if one is provided
*/
async writeBeginningWord() {
if (!this.writtenBeginningWord.length) {
// No word to write, stop here!
return;
}
for (let char of [...this.writtenBeginningWord]) {
this.typedBeginningWord += char;
await this.delay(this.typingDelay);
await delay(this.typingDelay);
}
},
/**
* Auto type the text
*/
async autoType() {
for (let currentWord of this.textFeed) {
await this.writeWord(currentWord);
await this.delay(this.waitBeforeDeleteDelay);
await delay(this.waitBeforeDeleteDelay);
// If we are on the last word, we don't want to delete it if we are not repeating (unless removeAfterRepeat is true)
if (
!this.repeat &&
Expand All @@ -127,24 +143,32 @@ export default defineComponent({
break;
}
await this.removeWord(currentWord);
await this.delay(this.betweenWordDelay);
await delay(this.betweenWordDelay);
}
if (this.repeat) {
this.autoType();
} else {
this.$emit("finished");
}
},
/**
* Write a word on the screen
* @param {string} currentWord
*/
async writeWord(currentWord) {
for (let char of [...currentWord]) {
this.currentText += char;
await this.delay(this.typingDelay);
await delay(this.typingDelay);
}
},
/**
* Remove a word from the screen
* @param {string} currentWord
*/
async removeWord(currentWord) {
for (let i = 0; i < currentWord.length; i++) {
this.currentText = this.currentText.slice(0, -1);
await this.delay(this.deletingDelay);
await delay(this.deletingDelay);
}
},
},
Expand Down

0 comments on commit cf8635e

Please sign in to comment.