-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspeechBubble.js
347 lines (290 loc) · 9.95 KB
/
speechBubble.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
export class SpeechBubble {
constructor(floatingCharacter) {
this.character = floatingCharacter
// Create the wrapper for the character, speech bubble, and blurred background
this.wrapper = document.createElement("div")
this.wrapper.classList.add("speech-bubble-wrapper")
this.character.element.appendChild(this.wrapper)
// Create the blurred background
this.blurBackground = document.createElement("div")
this.blurBackground.classList.add("speech-bubble-blur-background")
this.wrapper.appendChild(this.blurBackground)
// Create the speech bubble element
this.element = document.createElement("div")
this.element.id = "speech-bubble"
this.element.classList.add("speech-bubble")
this.wrapper.appendChild(this.element)
// Create the message container
this.messageContainer = document.createElement("div")
this.messageContainer.classList.add("speech-bubble-message")
this.element.appendChild(this.messageContainer)
// Create the button container (used for both single and multiple buttons)
this.buttonContainer = document.createElement("div")
this.buttonContainer.classList.add("speech-bubble-buttons")
this.wrapper.appendChild(this.buttonContainer)
// Inject CSS for styling
this.injectStyles()
}
// Show with a single button
async show(message, action = "OK") {
this.messageContainer.textContent = "" // Clear any existing text
this.buttonContainer.innerHTML = "" // Clear previous buttons
const button = document.createElement("button")
button.textContent = action
button.classList.add("speech-bubble-button")
this.buttonContainer.appendChild(button)
this.wrapper.style.display = "flex"
this.blurBackground.style.display = "block" // Show the background
// Trigger the scale animation
requestAnimationFrame(() => {
this.wrapper.classList.add("speech-bubble-active")
this.blurBackground.classList.add("blur-active")
})
await this.animateText(message) // Animate the text appearance
return new Promise((resolve) => {
button.addEventListener("click", async () => {
await this.hide()
resolve() // Resolve when button is clicked
})
})
}
// Show with multiple choices
async showWithChoices(message, choices) {
this.messageContainer.textContent = "" // Clear any existing text
this.buttonContainer.innerHTML = "" // Clear previous buttons
this.wrapper.style.display = "flex"
this.blurBackground.style.display = "block" // Show the background
// Trigger the scale animation
requestAnimationFrame(() => {
this.wrapper.classList.add("speech-bubble-active")
this.blurBackground.classList.add("blur-active")
})
this.animateText(message) // Animate the text appearance
return new Promise((resolve) => {
choices.forEach((choice) => {
const button = document.createElement("button")
button.textContent = choice.label
button.classList.add("speech-bubble-button")
this.buttonContainer.appendChild(button)
button.addEventListener("click", async () => {
await this.hide()
resolve(choice.value) // Resolve the promise with the button's value
})
})
})
}
// Show with input field
// Show with input field
// Show with multiline input field
async showWithInput(message, placeholder = "") {
this.messageContainer.textContent = "" // Clear any existing text
this.buttonContainer.innerHTML = "" // Clear previous buttons
const inputContainer = document.createElement("div")
inputContainer.classList.add("speech-bubble-input-container")
const textarea = document.createElement("textarea")
textarea.placeholder = placeholder
textarea.classList.add("speech-bubble-textarea")
inputContainer.appendChild(textarea)
const submitButton = document.createElement("button")
submitButton.textContent = "Submit"
submitButton.classList.add("speech-bubble-button")
//submitButton.disabled = true // Initially disable the button
inputContainer.appendChild(submitButton)
this.buttonContainer.appendChild(inputContainer)
this.wrapper.style.display = "flex"
this.blurBackground.style.display = "block" // Show the background
// Trigger the scale animation
requestAnimationFrame(() => {
this.wrapper.classList.add("speech-bubble-active")
this.blurBackground.classList.add("blur-active")
})
await this.animateText(message) // Animate the text appearance
return new Promise((resolve) => {
let resolved = false // Flag to ensure resolve is called only once
const validateInput = () => {
const userInput = textarea.value.trim()
//submitButton.disabled = userInput === "" // Disable button if input is empty
}
const submitInput = async () => {
const userInput = textarea.value.trim()
if (userInput && !resolved) {
resolved = true // Prevent multiple resolves
await this.hide()
resolve(userInput) // Resolve with the input value
} else {
textarea.classList.add("invalid") // Add an invalid class for visual feedback
}
}
// Handle input change
textarea.addEventListener("input", validateInput)
// Handle button click
submitButton.addEventListener("click", submitInput)
// Handle pressing Enter key
textarea.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
event.preventDefault() // Prevent newline in the textarea
submitInput()
}
})
console.log("Waiting for user input...")
})
}
async animateText(message) {
if (this.currentAnimation) {
// Cancel any ongoing animation
this.currentAnimation.cancelled = true
}
const animationState = { cancelled: false }
this.currentAnimation = animationState
this.messageContainer.textContent = "" // Clear any existing text
for (let i = 0; i < message.length; i++) {
if (animationState.cancelled) {
// Stop the animation if it's cancelled
return
}
this.messageContainer.textContent += message[i]
await new Promise((resolve) => setTimeout(resolve, 20)) // Delay for each character
}
// Clear the animation state once completed
if (this.currentAnimation === animationState) {
this.currentAnimation = null
}
}
remove() {
if (this.element) {
this.element.remove() // Remove the DOM element
}
}
async hide() {
this.wrapper.classList.remove("speech-bubble-active")
this.blurBackground.classList.remove("blur-active")
await new Promise((resolve) => setTimeout(resolve, 300)) // Wait for the animation to complete
this.wrapper.style.display = "none"
}
injectStyles() {
const style = document.createElement("style")
style.textContent = `
.speech-bubble-wrapper {
position: absolute;
top: 0;
left: 120%;
display: none;
opacity: 0;
transform: scale(0.5);
transform-origin: top left;
transition: transform 0.4s ease-in-out, opacity 0.3s ease-in-out;
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 16px;
width: 300px;
z-index: -1;
}
.speech-bubble-active {
opacity: 1;
transform: scale(1);
}
.speech-bubble-blur-background {
position: absolute;
top: -50px;
left: -50px;
width: 300px;
height: 200px;
background-color: rgba(255, 255, 255, 0.7);
filter: blur(15px);
border-radius: 24px;
z-index: -1;
display: none;
transition: opacity 0.3s ease-in-out;
}
.blur-active {
display: block;
opacity: 1;
}
.speech-bubble {
position: relative;
background-color: #EFF0F2;
padding: 20px 24px;
border-radius: 24px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
width: 100%;
box-sizing: border-box;
}
.speech-bubble::after {
content: "";
position: absolute;
top: 20px;
left: -10px;
width: 0;
height: 0;
border-top: 10px solid transparent;
border-bottom: 10px solid transparent;
border-right: 10px solid #EFF0F2;
}
.speech-bubble-message {
color: #000000;
font-size: 18px;
line-height: 1.4;
margin: 0;
text-align: left;
}
.speech-bubble-buttons {
display: flex;
flex-direction: column;
gap: 8px;
margin-top: 0px;
align-items: flex-end;
width: 100%;
}
.speech-bubble-input-container {
display: flex;
gap: 8px;
width: 100%;
margin-top: 0px;
align-items: flex-end;
flex-direction: column;
}
.speech-bubble-textarea {
width: 100%;
height: 150px; /* Set the height for multiline input */
padding: 8px;
border: 4px solid #EFF0F2;
border-radius: 16px;
font-size: 16px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
box-sizing: border-box;
resize: vertical; /* Allow resizing vertically */
background-color: #fff;
resize: none!important;
outline: none;
border-radius: 30px;
padding: 12px 18px;
}
.speech-bubble-textarea:focus {
border-color: #D9FFB0; /* Highlight border on focus */
background-color: #FFF!important;
}
.invalid {
border-color: #E72070;
}
.speech-bubble-button {
padding: 12px 24px;
background-color: #E2FFCC;
color: #000000;
border: none;
border-radius: 24px;
cursor: pointer;
font-size: 18px;
text-align: center;
width: fit-content;
}
.speech-bubble-button:hover {
transform: scale(1.02);
}
.speech-bubble-button:active {
transform: scale(0.98);
}
`
document.head.appendChild(style)
}
}