witchday/TextMessage.js

47 lines
999 B
JavaScript
Raw Permalink Normal View History

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>
<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
})
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();
}
}
init(container) {
this.createElement();
container.appendChild(this.element);
2024-08-06 15:55:14 +00:00
this.revealingText.init();
}
}