embeddedaichatux 1.7.1 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/EmbeddedChat.js +110 -12
- package/package.json +1 -1
package/EmbeddedChat.js
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
this.serverUrl = this.options.serverUrl || 'https://embedgpt.chat/';
|
|
13
13
|
this.height = options.height || 600; // Default height
|
|
14
14
|
this.width = options.width || 320; // Default width
|
|
15
|
+
this.marginBottom = options.marginBottom || 0;
|
|
15
16
|
this.enableAnimation = options.enableAnimation !== false; // Default to true if not provided
|
|
16
17
|
this.minimizedHeight = `${this.height * 0.3}px`; // 30% of the full height
|
|
17
18
|
this.mouseInsideChat = false;
|
|
@@ -23,7 +24,7 @@
|
|
|
23
24
|
|
|
24
25
|
this.positionStyle = this.containerDiv.dataset.position === 'in-place' ?
|
|
25
26
|
`width:100%; height:${this.height}px; ${transitionStyle}` :
|
|
26
|
-
`position: fixed; right: 1em; bottom: 0; width: ${this.width}px;
|
|
27
|
+
`position: fixed; right: 1em; bottom: 0; width: ${this.width}px; z-index: 100000; max-width:90vw; max-height: 90vh; ${transitionStyle}`;
|
|
27
28
|
this.mode = options.mode || 'Chat'; // default to 'chat'
|
|
28
29
|
this.minimizeOnScroll = false; // Default to false
|
|
29
30
|
if (this.mode === 'ContactForm') {
|
|
@@ -89,6 +90,11 @@
|
|
|
89
90
|
this.minimizedIframe = this.containerDiv.querySelector("#embedded-chat-minimized");
|
|
90
91
|
|
|
91
92
|
await this.waitForIframeLoad(this.iframe);
|
|
93
|
+
|
|
94
|
+
if (this.mode === 'ContactForm') {
|
|
95
|
+
this.iframe.style.maxWidth = "100%";
|
|
96
|
+
this.iframe.style.display = "block";
|
|
97
|
+
}
|
|
92
98
|
}
|
|
93
99
|
|
|
94
100
|
addEventListeners() {
|
|
@@ -106,6 +112,24 @@
|
|
|
106
112
|
this.minimizeOnScrollAction();
|
|
107
113
|
}
|
|
108
114
|
});
|
|
115
|
+
|
|
116
|
+
window.addEventListener("load", () => {
|
|
117
|
+
this.postResizeMessage();
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
window.addEventListener("resize", () => {
|
|
121
|
+
this.postResizeMessage();
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
postResizeMessage() {
|
|
126
|
+
if (this.iframe?.contentWindow) {
|
|
127
|
+
this.iframe.contentWindow.postMessage({
|
|
128
|
+
message: "resize",
|
|
129
|
+
width: window.innerWidth, // Ensure correct size
|
|
130
|
+
height: window.innerHeight
|
|
131
|
+
}, "*");
|
|
132
|
+
}
|
|
109
133
|
}
|
|
110
134
|
|
|
111
135
|
handleMessage(e) {
|
|
@@ -117,18 +141,23 @@
|
|
|
117
141
|
this.minimizeOnScroll = minimizeValue === true || minimizeValue === "true";
|
|
118
142
|
}
|
|
119
143
|
|
|
120
|
-
// Collect updates for locale, width, and scale
|
|
121
144
|
if (e.data.locale) {
|
|
122
145
|
updates.locale = e.data.locale;
|
|
123
146
|
}
|
|
124
147
|
if (e.data.width) {
|
|
125
148
|
updates.width = e.data.width;
|
|
126
149
|
}
|
|
150
|
+
if (e.data.height) {
|
|
151
|
+
updates.height = e.data.height;
|
|
152
|
+
}
|
|
153
|
+
if (e.data.marginBottom) {
|
|
154
|
+
updates.marginBottom = e.data.marginBottom;
|
|
155
|
+
}
|
|
127
156
|
if (e.data.scale) {
|
|
128
157
|
updates.scale = e.data.scale;
|
|
129
158
|
}
|
|
130
159
|
|
|
131
|
-
// Apply iframe updates
|
|
160
|
+
// Apply iframe updates in one go
|
|
132
161
|
if (Object.keys(updates).length > 0) {
|
|
133
162
|
this.applyIframeUpdates(updates);
|
|
134
163
|
}
|
|
@@ -142,6 +171,9 @@
|
|
|
142
171
|
} else if (e.data.message === "show" || e.data.message === "maximize") {
|
|
143
172
|
const animate = e.data.animate === true;
|
|
144
173
|
this.showMaximized(animate);
|
|
174
|
+
} else if (e.data.message === "navigate" && e.data.url) {
|
|
175
|
+
// Handle the navigate message
|
|
176
|
+
window.location.href = e.data.url;
|
|
145
177
|
}
|
|
146
178
|
}
|
|
147
179
|
|
|
@@ -198,7 +230,7 @@
|
|
|
198
230
|
}
|
|
199
231
|
}
|
|
200
232
|
|
|
201
|
-
applyIframeUpdates({ locale, width, scale } = {}) {
|
|
233
|
+
applyIframeUpdates({ locale, width, height, marginBottom, scale } = {}) {
|
|
202
234
|
// Update locale if provided and different (but do not update the iframe URL)
|
|
203
235
|
if (locale && typeof locale === 'string' && locale !== this.locale) {
|
|
204
236
|
this.locale = locale;
|
|
@@ -237,10 +269,82 @@
|
|
|
237
269
|
}
|
|
238
270
|
console.log(`Scale applied: ${scale}`);
|
|
239
271
|
}
|
|
240
|
-
}
|
|
241
272
|
|
|
273
|
+
// Handle height and marginBottom
|
|
274
|
+
let parsedHeight = parseInt(height, 10);
|
|
275
|
+
let parsedMarginBottom = parseInt(marginBottom, 10);
|
|
276
|
+
|
|
277
|
+
// Default to existing values if not provided or invalid
|
|
278
|
+
if (isNaN(parsedHeight) || parsedHeight <= 0) {
|
|
279
|
+
parsedHeight = this.height;
|
|
280
|
+
}
|
|
281
|
+
if (isNaN(parsedMarginBottom) || parsedMarginBottom < 0) {
|
|
282
|
+
parsedMarginBottom = (typeof this.marginBottom === 'number') ? this.marginBottom : 0;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Clamp so that height + marginBottom doesn't exceed 90% of viewport
|
|
286
|
+
const viewportHeight = window.innerHeight;
|
|
287
|
+
if (parsedHeight + parsedMarginBottom > 0.9 * viewportHeight) {
|
|
288
|
+
parsedHeight = Math.max(0, 0.9 * viewportHeight - parsedMarginBottom);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Only apply if there's an actual change
|
|
292
|
+
if (parsedHeight !== this.height || parsedMarginBottom !== this.marginBottom) {
|
|
293
|
+
this.height = parsedHeight;
|
|
294
|
+
this.marginBottom = parsedMarginBottom;
|
|
295
|
+
this.minimizedHeight = `${this.height * 0.3}px`;
|
|
296
|
+
|
|
297
|
+
console.log(`Height updated to: ${this.height}px, marginBottom: ${this.marginBottom}px`);
|
|
298
|
+
|
|
299
|
+
// Apply the updated height/marginBottom if not ContactForm
|
|
300
|
+
if (this.mode !== 'ContactForm') {
|
|
301
|
+
if (this.containerDiv.dataset.position === 'in-place') {
|
|
302
|
+
// "in-place" uses full width, sets containerDiv height
|
|
303
|
+
this.containerDiv.style.height = `${this.height}px`;
|
|
304
|
+
if (this.iframe) {
|
|
305
|
+
this.iframe.style.height = `${this.height}px`;
|
|
306
|
+
}
|
|
307
|
+
if (this.minimizedIframe) {
|
|
308
|
+
this.minimizedIframe.style.height = this.minimizedHeight;
|
|
309
|
+
}
|
|
310
|
+
} else {
|
|
311
|
+
// position: fixed (typical floating chat)
|
|
312
|
+
// Update bottom offset, main height
|
|
313
|
+
if (this.iframe) {
|
|
314
|
+
this.iframe.style.height = `${this.height}px`;
|
|
315
|
+
this.iframe.style.bottom = `${this.marginBottom}px`;
|
|
316
|
+
}
|
|
317
|
+
if (this.minimizedIframe) {
|
|
318
|
+
this.minimizedIframe.style.height = this.minimizedHeight;
|
|
319
|
+
this.minimizedIframe.style.bottom = `${this.marginBottom}px`;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
} else {
|
|
323
|
+
// ContactForm mode
|
|
324
|
+
this.containerDiv.style.height = `${this.height}px`;
|
|
325
|
+
if (this.iframe) {
|
|
326
|
+
this.iframe.style.height = `${this.height}px`;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
242
331
|
|
|
243
332
|
showMaximized(animate = false) {
|
|
333
|
+
const schedulePostMessage = () => {
|
|
334
|
+
if (this.iframe.contentWindow) {
|
|
335
|
+
this.iframe.contentWindow.postMessage({ message: "show" }, "*");
|
|
336
|
+
} else {
|
|
337
|
+
// Schedule a single retry with a delay
|
|
338
|
+
setTimeout(() => {
|
|
339
|
+
if (this.iframe.contentWindow) {
|
|
340
|
+
this.iframe.contentWindow.postMessage({ message: "show" }, "*");
|
|
341
|
+
} else {
|
|
342
|
+
console.warn("iframe contentWindow is not available.");
|
|
343
|
+
}
|
|
344
|
+
}, 500);
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
|
|
244
348
|
if (animate) {
|
|
245
349
|
this.animateMaximize();
|
|
246
350
|
}
|
|
@@ -251,17 +355,11 @@
|
|
|
251
355
|
|
|
252
356
|
this.iframe.style.display = "block";
|
|
253
357
|
|
|
254
|
-
if (this.isSafari() && !this.hasRefreshed) {
|
|
255
|
-
const currentSrc = this.iframe.src;
|
|
256
|
-
this.iframe.src = ''; // Temporarily clear the src
|
|
257
|
-
this.iframe.src = currentSrc; // Reset to the original src to trigger a reload
|
|
258
|
-
this.hasRefreshed = true; // Prevent endless loop
|
|
259
|
-
}
|
|
260
358
|
|
|
261
359
|
this.iframe.style.height = `${this.height}px`;
|
|
262
360
|
this.iframe.style.opacity = '1';
|
|
263
361
|
|
|
264
|
-
|
|
362
|
+
schedulePostMessage();
|
|
265
363
|
}
|
|
266
364
|
}
|
|
267
365
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "embeddedaichatux",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "A lightweight and customizable embedded AI chat UI component that seamlessly integrates into web applications, offering minimized and expanded views, with iframe-based content rendering.",
|
|
5
5
|
"main": "EmbeddedChat.js",
|
|
6
6
|
"scripts": {
|