2024-08-05 17:36:41 +00:00
|
|
|
class TextMessage {
|
|
|
|
constructor({ text, onComplete }) {
|
|
|
|
this.text = text;
|
|
|
|
this.onComplete = onComplete;
|
|
|
|
this.element = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
createElement() {
|
|
|
|
this.element = document.createElement("div");
|
|
|
|
this.element.classList.add("TextMessage");
|
|
|
|
|
|
|
|
this.element.innerHTML = (`
|
2024-08-06 15:55:14 +00:00
|
|
|
<p class="TextMessage_p"></p>
|
2024-08-05 17:36:41 +00:00
|
|
|
<button class="TextMessage_button">Next</button>
|
|
|
|
`);
|
|
|
|
|
2024-08-06 15:55:14 +00:00
|
|
|
// typewriter effect
|
|
|
|
this.revealingText = new RevealingText({
|
|
|
|
element: this.element.querySelector(".TextMessage_p"),
|
|
|
|
text: this.text
|
|
|
|
})
|
|
|
|
|
2024-08-05 17:36:41 +00:00
|
|
|
this.element.querySelector("button").addEventListener("click", () => {
|
|
|
|
this.done();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.actionListener = new KeyPressListener("Enter", () => {
|
|
|
|
this.done();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
done() {
|
2024-08-06 15:55:14 +00:00
|
|
|
if (this.revealingText.isDone) {
|
|
|
|
this.element.remove();
|
|
|
|
this.actionListener.unbind();
|
|
|
|
this.onComplete();
|
|
|
|
} else {
|
|
|
|
this.revealingText.warpToDone();
|
|
|
|
}
|
2024-08-05 17:36:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init(container) {
|
|
|
|
this.createElement();
|
|
|
|
container.appendChild(this.element);
|
2024-08-06 15:55:14 +00:00
|
|
|
this.revealingText.init();
|
2024-08-05 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|