@hellotext/hellotext 2.1.0 → 2.1.1

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.
@@ -45,6 +45,8 @@ let _default = /*#__PURE__*/function (_Controller) {
45
45
  this.onAgentOnline = this.onAgentOnline.bind(this);
46
46
  this.onMessageReaction = this.onMessageReaction.bind(this);
47
47
  this.onScroll = this.onScroll.bind(this);
48
+ this.onOutboundMessageSent = this.onOutboundMessageSent.bind(this);
49
+ this.broadcastChannel = new BroadcastChannel(`hellotext--webchat--${this.idValue}`);
48
50
  _get(_getPrototypeOf(_default.prototype), "initialize", this).call(this);
49
51
  }
50
52
  }, {
@@ -69,15 +71,43 @@ let _default = /*#__PURE__*/function (_Controller) {
69
71
  this.openValue = true;
70
72
  }
71
73
  _hellotext.default.eventEmitter.dispatch('webchat:mounted');
74
+ this.broadcastChannel.addEventListener('message', this.onOutboundMessageSent);
72
75
  _get(_getPrototypeOf(_default.prototype), "connect", this).call(this);
73
76
  }
74
77
  }, {
75
78
  key: "disconnect",
76
79
  value: function disconnect() {
80
+ this.broadcastChannel.removeEventListener('message', this.onOutboundMessageSent);
77
81
  this.messagesContainerTarget.removeEventListener('scroll', this.onScroll);
82
+ this.broadcastChannel.close();
78
83
  this.floatingUICleanup();
79
84
  _get(_getPrototypeOf(_default.prototype), "disconnect", this).call(this);
80
85
  }
86
+ }, {
87
+ key: "onOutboundMessageSent",
88
+ value: function onOutboundMessageSent(event) {
89
+ const {
90
+ data
91
+ } = event;
92
+ const callbacks = {
93
+ 'message:sent': data => {
94
+ const element = new DOMParser().parseFromString(data.element, 'text/html').body.firstElementChild;
95
+ this.messagesContainerTarget.appendChild(element);
96
+ element.scrollIntoView({
97
+ behavior: 'instant'
98
+ });
99
+ },
100
+ 'message:failed': data => {
101
+ var _this$messagesContain;
102
+ (_this$messagesContain = this.messagesContainerTarget.querySelector(`#${data.id}`)) === null || _this$messagesContain === void 0 ? void 0 : _this$messagesContain.classList.add('failed');
103
+ }
104
+ };
105
+ if (callbacks[data.type]) {
106
+ callbacks[data.type](data);
107
+ } else {
108
+ console.log(`Unhandled message event: ${data.type}`);
109
+ }
110
+ }
81
111
  }, {
82
112
  key: "onScroll",
83
113
  value: async function onScroll() {
@@ -253,6 +283,7 @@ let _default = /*#__PURE__*/function (_Controller) {
253
283
  });
254
284
  formData.append('session', _hellotext.default.session);
255
285
  const element = this.messageTemplateTarget.cloneNode(true);
286
+ element.id = `hellotext--webchat--${this.idValue}--message--${Date.now()}`;
256
287
  element.classList.add('received');
257
288
  element.style.removeProperty('display');
258
289
  element.setAttribute('data-hellotext--webchat-target', 'message');
@@ -267,6 +298,10 @@ let _default = /*#__PURE__*/function (_Controller) {
267
298
  element.scrollIntoView({
268
299
  behavior: 'smooth'
269
300
  });
301
+ this.broadcastChannel.postMessage({
302
+ type: 'message:sent',
303
+ element: element.outerHTML
304
+ });
270
305
  this.inputTarget.value = '';
271
306
  this.files = [];
272
307
  this.attachmentContainerTarget.style.display = 'none';
@@ -274,6 +309,10 @@ let _default = /*#__PURE__*/function (_Controller) {
274
309
  this.inputTarget.focus();
275
310
  const response = await this.messagesAPI.create(formData);
276
311
  if (response.failed) {
312
+ this.broadcastChannel.postMessage({
313
+ type: 'message:failed',
314
+ id: element.id
315
+ });
277
316
  return element.classList.add('failed');
278
317
  }
279
318
  const data = await response.json();
@@ -43,6 +43,8 @@ var _default = /*#__PURE__*/function (_Controller) {
43
43
  this.onAgentOnline = this.onAgentOnline.bind(this);
44
44
  this.onMessageReaction = this.onMessageReaction.bind(this);
45
45
  this.onScroll = this.onScroll.bind(this);
46
+ this.onOutboundMessageSent = this.onOutboundMessageSent.bind(this);
47
+ this.broadcastChannel = new BroadcastChannel("hellotext--webchat--".concat(this.idValue));
46
48
  _get(_getPrototypeOf(_default.prototype), "initialize", this).call(this);
47
49
  }
48
50
  }, {
@@ -67,15 +69,43 @@ var _default = /*#__PURE__*/function (_Controller) {
67
69
  this.openValue = true;
68
70
  }
69
71
  Hellotext.eventEmitter.dispatch('webchat:mounted');
72
+ this.broadcastChannel.addEventListener('message', this.onOutboundMessageSent);
70
73
  _get(_getPrototypeOf(_default.prototype), "connect", this).call(this);
71
74
  }
72
75
  }, {
73
76
  key: "disconnect",
74
77
  value: function disconnect() {
78
+ this.broadcastChannel.removeEventListener('message', this.onOutboundMessageSent);
75
79
  this.messagesContainerTarget.removeEventListener('scroll', this.onScroll);
80
+ this.broadcastChannel.close();
76
81
  this.floatingUICleanup();
77
82
  _get(_getPrototypeOf(_default.prototype), "disconnect", this).call(this);
78
83
  }
84
+ }, {
85
+ key: "onOutboundMessageSent",
86
+ value: function onOutboundMessageSent(event) {
87
+ var {
88
+ data
89
+ } = event;
90
+ var callbacks = {
91
+ 'message:sent': data => {
92
+ var element = new DOMParser().parseFromString(data.element, 'text/html').body.firstElementChild;
93
+ this.messagesContainerTarget.appendChild(element);
94
+ element.scrollIntoView({
95
+ behavior: 'instant'
96
+ });
97
+ },
98
+ 'message:failed': data => {
99
+ var _this$messagesContain;
100
+ (_this$messagesContain = this.messagesContainerTarget.querySelector("#".concat(data.id))) === null || _this$messagesContain === void 0 ? void 0 : _this$messagesContain.classList.add('failed');
101
+ }
102
+ };
103
+ if (callbacks[data.type]) {
104
+ callbacks[data.type](data);
105
+ } else {
106
+ console.log("Unhandled message event: ".concat(data.type));
107
+ }
108
+ }
79
109
  }, {
80
110
  key: "onScroll",
81
111
  value: function () {
@@ -257,6 +287,7 @@ var _default = /*#__PURE__*/function (_Controller) {
257
287
  });
258
288
  formData.append('session', Hellotext.session);
259
289
  var element = this.messageTemplateTarget.cloneNode(true);
290
+ element.id = "hellotext--webchat--".concat(this.idValue, "--message--").concat(Date.now());
260
291
  element.classList.add('received');
261
292
  element.style.removeProperty('display');
262
293
  element.setAttribute('data-hellotext--webchat-target', 'message');
@@ -271,6 +302,10 @@ var _default = /*#__PURE__*/function (_Controller) {
271
302
  element.scrollIntoView({
272
303
  behavior: 'smooth'
273
304
  });
305
+ this.broadcastChannel.postMessage({
306
+ type: 'message:sent',
307
+ element: element.outerHTML
308
+ });
274
309
  this.inputTarget.value = '';
275
310
  this.files = [];
276
311
  this.attachmentContainerTarget.style.display = 'none';
@@ -278,6 +313,10 @@ var _default = /*#__PURE__*/function (_Controller) {
278
313
  this.inputTarget.focus();
279
314
  var response = yield this.messagesAPI.create(formData);
280
315
  if (response.failed) {
316
+ this.broadcastChannel.postMessage({
317
+ type: 'message:failed',
318
+ id: element.id
319
+ });
281
320
  return element.classList.add('failed');
282
321
  }
283
322
  var data = yield response.json();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hellotext/hellotext",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "Hellotext JavaScript Client",
5
5
  "source": "src/index.js",
6
6
  "main": "lib/index.cjs",
@@ -62,6 +62,9 @@ export default class extends Controller {
62
62
 
63
63
  this.onScroll = this.onScroll.bind(this)
64
64
 
65
+ this.onOutboundMessageSent = this.onOutboundMessageSent.bind(this)
66
+ this.broadcastChannel = new BroadcastChannel(`hellotext--webchat--${this.idValue}`)
67
+
65
68
  super.initialize()
66
69
  }
67
70
 
@@ -90,16 +93,44 @@ export default class extends Controller {
90
93
  }
91
94
 
92
95
  Hellotext.eventEmitter.dispatch('webchat:mounted')
96
+ this.broadcastChannel.addEventListener('message', this.onOutboundMessageSent)
97
+
93
98
  super.connect()
94
99
  }
95
100
 
96
101
  disconnect() {
102
+ this.broadcastChannel.removeEventListener('message', this.onOutboundMessageSent)
97
103
  this.messagesContainerTarget.removeEventListener('scroll', this.onScroll)
104
+
105
+ this.broadcastChannel.close()
98
106
  this.floatingUICleanup()
99
107
 
100
108
  super.disconnect()
101
109
  }
102
110
 
111
+ onOutboundMessageSent(event) {
112
+ const { data } = event
113
+
114
+ const callbacks = {
115
+ 'message:sent': data => {
116
+ const element = new DOMParser().parseFromString(data.element, 'text/html').body
117
+ .firstElementChild
118
+
119
+ this.messagesContainerTarget.appendChild(element)
120
+ element.scrollIntoView({ behavior: 'instant' })
121
+ },
122
+ 'message:failed': data => {
123
+ this.messagesContainerTarget.querySelector(`#${data.id}`)?.classList.add('failed')
124
+ },
125
+ }
126
+
127
+ if (callbacks[data.type]) {
128
+ callbacks[data.type](data)
129
+ } else {
130
+ console.log(`Unhandled message event: ${data.type}`)
131
+ }
132
+ }
133
+
103
134
  async onScroll() {
104
135
  if (
105
136
  this.messagesContainerTarget.scrollTop > 300 ||
@@ -307,6 +338,8 @@ export default class extends Controller {
307
338
 
308
339
  const element = this.messageTemplateTarget.cloneNode(true)
309
340
 
341
+ element.id = `hellotext--webchat--${this.idValue}--message--${Date.now()}`
342
+
310
343
  element.classList.add('received')
311
344
  element.style.removeProperty('display')
312
345
 
@@ -324,6 +357,11 @@ export default class extends Controller {
324
357
  this.messagesContainerTarget.appendChild(element)
325
358
  element.scrollIntoView({ behavior: 'smooth' })
326
359
 
360
+ this.broadcastChannel.postMessage({
361
+ type: 'message:sent',
362
+ element: element.outerHTML,
363
+ })
364
+
327
365
  this.inputTarget.value = ''
328
366
  this.files = []
329
367
  this.attachmentContainerTarget.style.display = 'none'
@@ -334,6 +372,11 @@ export default class extends Controller {
334
372
  const response = await this.messagesAPI.create(formData)
335
373
 
336
374
  if (response.failed) {
375
+ this.broadcastChannel.postMessage({
376
+ type: 'message:failed',
377
+ id: element.id,
378
+ })
379
+
337
380
  return element.classList.add('failed')
338
381
  }
339
382