-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTextMessage.js
50 lines (43 loc) · 1.15 KB
/
TextMessage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class TextMessage {
constructor({ text, onComplete }) {
this.text = text;
this.onComplete = onComplete;
this.element = null;
}
createElement() {
//Create the element
this.element = document.createElement("div");
this.element.classList.add("TextMessage");
this.element.innerHTML = `
<p class="TextMessage_p"></p>
<button class="TextMessage_button">Next</button>
`;
//Init the typewriter effect
this.revealingText = new RevealingText({
element: this.element.querySelector(".TextMessage_p"),
text: this.text,
});
this.element.querySelector("button").addEventListener("click", () => {
//Close the text message
this.done();
});
this.actionListener = new KeyPressListener("Enter", () => {
this.actionListener.unbind();
this.done();
});
}
done() {
if (this.revealingText.isDone) {
this.element.remove();
this.actionListener.unbind();
this.onComplete();
} else {
this.revealingText.warpToDone();
}
}
init(container) {
this.createElement();
container.appendChild(this.element);
this.revealingText.init();
}
}