jibtalk-core 1.0.3 → 1.0.4
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/dist/jibtalk-core.js +25 -9
- package/dist/jibtalk-core.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/jibtalk-core.js
CHANGED
|
@@ -1453,9 +1453,11 @@ var Q = "\n#jibtalk-sdk-root {\n position: fixed;\n z-index: 9999;\n font-fam
|
|
|
1453
1453
|
primaryColor = "#26A17B";
|
|
1454
1454
|
locale = "en";
|
|
1455
1455
|
constructor(e) {
|
|
1456
|
-
this.apiKey = e.apiKey, this.backendUrl = typeof globalThis < "u" && globalThis.process?.env?.NEXT_PUBLIC_JIBTALK_BACKEND_URL || typeof window < "u" && window.JIBTALK_BACKEND_URL || "http://localhost:7000", this.theme = e.theme || "dark", this.position = e.position || "bottom-right", this.primaryColor = e.primaryColor || "#26A17B", this.locale = e.locale || "en"
|
|
1457
|
-
let t =
|
|
1458
|
-
|
|
1456
|
+
this.apiKey = e.apiKey, this.backendUrl = typeof globalThis < "u" && globalThis.process?.env?.NEXT_PUBLIC_JIBTALK_BACKEND_URL || typeof window < "u" && window.JIBTALK_BACKEND_URL || "http://localhost:7000", this.theme = e.theme || "dark", this.position = e.position || "bottom-right", this.primaryColor = e.primaryColor || "#26A17B", this.locale = e.locale || "en";
|
|
1457
|
+
let t = e.customer;
|
|
1458
|
+
(t == null || t === "null" || t === "") && (t = void 0), this.optionsCustomer = t, this.optionsConvId = e.conversationId, this.optionsTimestamp = e.timestamp, this.optionsSignature = e.signature;
|
|
1459
|
+
let n = typeof window < "u" ? localStorage.getItem("jibtalk_conversation_id") : null;
|
|
1460
|
+
this.currentConversation = n ? { id: n } : null, this.visitorId = t || this.getVisitorId(), this.connect();
|
|
1459
1461
|
}
|
|
1460
1462
|
getVisitorId() {
|
|
1461
1463
|
if (typeof window > "u") return "";
|
|
@@ -1485,15 +1487,23 @@ var Q = "\n#jibtalk-sdk-root {\n position: fixed;\n z-index: 9999;\n font-fam
|
|
|
1485
1487
|
}
|
|
1486
1488
|
initWidget(e, t) {
|
|
1487
1489
|
if (!this.socket) return;
|
|
1488
|
-
let n = { apiKey: this.apiKey }, r =
|
|
1489
|
-
|
|
1490
|
+
let n = { apiKey: this.apiKey }, r = e;
|
|
1491
|
+
(r == null || r === "null" || r === "") && (r = void 0);
|
|
1492
|
+
let i = t || this.currentConversation?.id;
|
|
1493
|
+
if (i && (n.conversationId = i, typeof window < "u" && !r)) {
|
|
1490
1494
|
let e = localStorage.getItem("jibtalk_session_token");
|
|
1491
1495
|
e && (n.sessionToken = e);
|
|
1492
1496
|
}
|
|
1493
|
-
|
|
1497
|
+
r ? (n.customer = r, this.optionsTimestamp && (n.timestamp = this.optionsTimestamp), this.optionsSignature && (n.signature = this.optionsSignature)) : n.visitorId = this.visitorId, this.socket.emit("init_widget", n);
|
|
1494
1498
|
}
|
|
1495
1499
|
open(e) {
|
|
1496
|
-
|
|
1500
|
+
if (!this.isOpen) {
|
|
1501
|
+
if (this.isOpen = !0, this.unreadCount = 0, e) {
|
|
1502
|
+
let t = e.customer;
|
|
1503
|
+
(t == null || t === "null" || t === "") && (t = void 0), t ? (this.visitorId = t, this.optionsCustomer = t) : this.optionsCustomer = void 0, e.conversationId && (this.optionsConvId = e.conversationId), e.timestamp && (this.optionsTimestamp = e.timestamp), e.signature && (this.optionsSignature = e.signature), this.initWidget(t, e.conversationId);
|
|
1504
|
+
}
|
|
1505
|
+
this.emitStateChange();
|
|
1506
|
+
}
|
|
1497
1507
|
}
|
|
1498
1508
|
close() {
|
|
1499
1509
|
this.isOpen && (this.isOpen = !1, this.emitStateChange(), this.emit("conversationClosed", this.currentConversation));
|
|
@@ -1505,7 +1515,9 @@ var Q = "\n#jibtalk-sdk-root {\n position: fixed;\n z-index: 9999;\n font-fam
|
|
|
1505
1515
|
console.log("Core SDK: destroy() called."), this.close(), this.socket &&= (this.socket.disconnect(), null), this.listeners.clear(), this.isConnected = !1, this.emitStateChange(), $ === this && ($ = null);
|
|
1506
1516
|
}
|
|
1507
1517
|
identify(e, t, n) {
|
|
1508
|
-
typeof window > "u"
|
|
1518
|
+
if (typeof window > "u") return;
|
|
1519
|
+
let r = e;
|
|
1520
|
+
(r == null || r === "null" || r === "") && (r = void 0), r ? (this.visitorId = r, this.optionsCustomer = r) : (this.optionsCustomer = void 0, this.visitorId = this.getVisitorId()), t && (this.optionsTimestamp = t), n && (this.optionsSignature = n), this.initWidget(r);
|
|
1509
1521
|
}
|
|
1510
1522
|
setUser(e, t, n) {
|
|
1511
1523
|
this.identify(e, t, n);
|
|
@@ -1571,7 +1583,11 @@ var Q = "\n#jibtalk-sdk-root {\n position: fixed;\n z-index: 9999;\n font-fam
|
|
|
1571
1583
|
});
|
|
1572
1584
|
}
|
|
1573
1585
|
updateOptions(e) {
|
|
1574
|
-
e.theme && (this.theme = e.theme), e.position && (this.position = e.position), e.primaryColor && (this.primaryColor = e.primaryColor), e.locale && (this.locale = e.locale), e.customer !== void 0
|
|
1586
|
+
if (e.theme && (this.theme = e.theme), e.position && (this.position = e.position), e.primaryColor && (this.primaryColor = e.primaryColor), e.locale && (this.locale = e.locale), e.customer !== void 0) {
|
|
1587
|
+
let t = e.customer;
|
|
1588
|
+
(t == null || t === "null" || t === "") && (t = void 0), t ? (this.visitorId = t, this.optionsCustomer = t) : this.optionsCustomer = void 0;
|
|
1589
|
+
}
|
|
1590
|
+
e.conversationId && (this.optionsConvId = e.conversationId), e.timestamp && (this.optionsTimestamp = e.timestamp), e.signature && (this.optionsSignature = e.signature), this.emitStateChange();
|
|
1575
1591
|
}
|
|
1576
1592
|
}, $ = null, Xe = (e) => typeof window > "u" ? null : (!$ && e ? $ = new Ye(e) : $ && e && $.updateOptions(e), $);
|
|
1577
1593
|
//#endregion
|
|
@@ -574,4 +574,4 @@
|
|
|
574
574
|
text-overflow: ellipsis;
|
|
575
575
|
white-space: nowrap;
|
|
576
576
|
}
|
|
577
|
-
`,Ye=()=>{if(typeof document>`u`)return;let e=`jibtalk-sdk-styles`;if(!document.getElementById(e)){let t=document.createElement(`style`);t.id=e,t.textContent=Je,document.head.appendChild(t)}},Xe=class{socket=null;apiKey;backendUrl;visitorId;listeners=new Map;isOpen=!1;isConnected=!1;unreadCount=0;currentConversation=null;messages=[];workspaceName=``;workspaceSlug=``;theme=`dark`;position=`bottom-right`;primaryColor=`#26A17B`;locale=`en`;constructor(e){this.apiKey=e.apiKey,this.backendUrl=typeof globalThis<`u`&&globalThis.process?.env?.NEXT_PUBLIC_JIBTALK_BACKEND_URL||typeof window<`u`&&window.JIBTALK_BACKEND_URL||`http://localhost:7000`,this.theme=e.theme||`dark`,this.position=e.position||`bottom-right`,this.primaryColor=e.primaryColor||`#26A17B`,this.locale=e.locale||`en
|
|
577
|
+
`,Ye=()=>{if(typeof document>`u`)return;let e=`jibtalk-sdk-styles`;if(!document.getElementById(e)){let t=document.createElement(`style`);t.id=e,t.textContent=Je,document.head.appendChild(t)}},Xe=class{socket=null;apiKey;backendUrl;visitorId;listeners=new Map;isOpen=!1;isConnected=!1;unreadCount=0;currentConversation=null;messages=[];workspaceName=``;workspaceSlug=``;theme=`dark`;position=`bottom-right`;primaryColor=`#26A17B`;locale=`en`;constructor(e){this.apiKey=e.apiKey,this.backendUrl=typeof globalThis<`u`&&globalThis.process?.env?.NEXT_PUBLIC_JIBTALK_BACKEND_URL||typeof window<`u`&&window.JIBTALK_BACKEND_URL||`http://localhost:7000`,this.theme=e.theme||`dark`,this.position=e.position||`bottom-right`,this.primaryColor=e.primaryColor||`#26A17B`,this.locale=e.locale||`en`;let t=e.customer;(t==null||t===`null`||t===``)&&(t=void 0),this.optionsCustomer=t,this.optionsConvId=e.conversationId,this.optionsTimestamp=e.timestamp,this.optionsSignature=e.signature;let n=typeof window<`u`?localStorage.getItem(`jibtalk_conversation_id`):null;this.currentConversation=n?{id:n}:null,this.visitorId=t||this.getVisitorId(),this.connect()}getVisitorId(){if(typeof window>`u`)return``;let e=localStorage.getItem(`jibtalk_visitor_id`);return e||=localStorage.getItem(`vyou_visitor_id`),e||(e=`v_`+Math.random().toString(36).substring(2,11),localStorage.setItem(`jibtalk_visitor_id`,e)),e}connect(){typeof window>`u`||(this.socket&&this.socket.disconnect(),this.socket=Q(this.backendUrl),this.socket.on(`connect`,()=>{this.isConnected=!0,this.emit(`connected`),this.emitStateChange();let e=this.optionsCustomer,t=this.optionsConvId;this.initWidget(e,t)}),this.socket.on(`disconnect`,()=>{this.isConnected=!1,this.emit(`disconnected`),this.emitStateChange()}),this.socket.on(`session_ready`,e=>{this.currentConversation=e,this.messages=e.messages||[],this.workspaceName=e.workspaceName||``,this.workspaceSlug=e.workspaceSlug||``,typeof window<`u`&&(e.id&&localStorage.setItem(`jibtalk_conversation_id`,e.id),e.sessionToken&&localStorage.setItem(`jibtalk_session_token`,e.sessionToken)),this.emit(`conversationOpened`,e),this.emitStateChange()}),this.socket.on(`widget_ready`,e=>{this.workspaceName=e.workspaceName||``,this.workspaceSlug=e.workspaceSlug||``,this.currentConversation=null,this.messages=[],e.sessionExpired&&typeof window<`u`&&(localStorage.removeItem(`jibtalk_conversation_id`),localStorage.removeItem(`jibtalk_session_token`)),this.emitStateChange()}),this.socket.on(`receive_message`,e=>{this.messages.push(e),e.sender===`visitor`?this.emit(`messageSent`,e):(this.isOpen||this.unreadCount++,this.emit(`messageReceived`,e)),this.emitStateChange()}),this.socket.on(`typing_started`,e=>{this.emit(`typingStarted`,e)}),this.socket.on(`typing_stopped`,e=>{this.emit(`typingStopped`,e)}),this.socket.on(`error`,e=>{console.error(`JibTalk Socket error:`,e.message||e)}))}initWidget(e,t){if(!this.socket)return;let n={apiKey:this.apiKey},r=e;(r==null||r===`null`||r===``)&&(r=void 0);let i=t||this.currentConversation?.id;if(i&&(n.conversationId=i,typeof window<`u`&&!r)){let e=localStorage.getItem(`jibtalk_session_token`);e&&(n.sessionToken=e)}r?(n.customer=r,this.optionsTimestamp&&(n.timestamp=this.optionsTimestamp),this.optionsSignature&&(n.signature=this.optionsSignature)):n.visitorId=this.visitorId,this.socket.emit(`init_widget`,n)}open(e){if(!this.isOpen){if(this.isOpen=!0,this.unreadCount=0,e){let t=e.customer;(t==null||t===`null`||t===``)&&(t=void 0),t?(this.visitorId=t,this.optionsCustomer=t):this.optionsCustomer=void 0,e.conversationId&&(this.optionsConvId=e.conversationId),e.timestamp&&(this.optionsTimestamp=e.timestamp),e.signature&&(this.optionsSignature=e.signature),this.initWidget(t,e.conversationId)}this.emitStateChange()}}close(){this.isOpen&&(this.isOpen=!1,this.emitStateChange(),this.emit(`conversationClosed`,this.currentConversation))}toggle(){console.log(`Core SDK: toggle() called. Current isOpen status:`,this.isOpen),this.isOpen?this.close():this.open()}destroy(){console.log(`Core SDK: destroy() called.`),this.close(),this.socket&&=(this.socket.disconnect(),null),this.listeners.clear(),this.isConnected=!1,this.emitStateChange(),$===this&&($=null)}identify(e,t,n){if(typeof window>`u`)return;let r=e;(r==null||r===`null`||r===``)&&(r=void 0),r?(this.visitorId=r,this.optionsCustomer=r):(this.optionsCustomer=void 0,this.visitorId=this.getVisitorId()),t&&(this.optionsTimestamp=t),n&&(this.optionsSignature=n),this.initWidget(r)}setUser(e,t,n){this.identify(e,t,n)}logout(){typeof window>`u`||(this.close(),localStorage.removeItem(`jibtalk_visitor_id`),localStorage.removeItem(`vyou_visitor_id`),localStorage.removeItem(`jibtalk_conversation_id`),localStorage.removeItem(`jibtalk_session_token`),this.optionsCustomer=void 0,this.optionsConvId=void 0,this.optionsTimestamp=void 0,this.optionsSignature=void 0,this.visitorId=this.getVisitorId(),this.currentConversation=null,this.messages=[],this.unreadCount=0,this.connect())}sendMessage(e,t,n){!e.trim()&&!n||!this.currentConversation||!this.socket||this.socket.emit(`send_message`,{sessionId:this.currentConversation.id,content:e,sender:`visitor`,visitorId:this.visitorId,parentMessageId:t,file:n})}startConversation(){this.socket&&this.socket.emit(`start_conversation`)}startTyping(){!this.socket||!this.currentConversation||this.socket.emit(`typing_started`,{sessionId:this.currentConversation.id,sender:`visitor`,visitorId:this.visitorId})}stopTyping(){!this.socket||!this.currentConversation||this.socket.emit(`typing_stopped`,{sessionId:this.currentConversation.id,sender:`visitor`,visitorId:this.visitorId})}on(e,t){this.listeners.has(e)||this.listeners.set(e,new Set),this.listeners.get(e).add(t),console.log(`Core SDK: Listener added for event "${e}". Current count:`,this.listeners.get(e)?.size)}off(e,t){this.listeners.has(e)&&(this.listeners.get(e).delete(t),console.log(`Core SDK: Listener removed for event "${e}". Current count:`,this.listeners.get(e)?.size))}emit(e,...t){this.listeners.has(e)?(console.log(`Core SDK: Emitting event "${e}" to`,this.listeners.get(e).size,`listeners.`),this.listeners.get(e).forEach(n=>{try{n(...t)}catch(t){console.error(`Error in event listener for ${e}:`,t)}})):console.log(`Core SDK: Emitting event "${e}" but no listeners exist.`)}emitStateChange(){console.log(`Core SDK: emitStateChange() triggered. isOpen:`,this.isOpen,`isConnected:`,this.isConnected),this.emit(`stateChange`,{isOpen:this.isOpen,isConnected:this.isConnected,unreadCount:this.unreadCount,currentConversation:this.currentConversation,messages:this.messages,theme:this.theme,position:this.position,primaryColor:this.primaryColor,locale:this.locale,workspaceName:this.workspaceName,workspaceSlug:this.workspaceSlug})}updateOptions(e){if(e.theme&&(this.theme=e.theme),e.position&&(this.position=e.position),e.primaryColor&&(this.primaryColor=e.primaryColor),e.locale&&(this.locale=e.locale),e.customer!==void 0){let t=e.customer;(t==null||t===`null`||t===``)&&(t=void 0),t?(this.visitorId=t,this.optionsCustomer=t):this.optionsCustomer=void 0}e.conversationId&&(this.optionsConvId=e.conversationId),e.timestamp&&(this.optionsTimestamp=e.timestamp),e.signature&&(this.optionsSignature=e.signature),this.emitStateChange()}},$=null;e.JibTalkCore=Xe,e.STYLES_CSS=Je,e.getJibTalkInstance=e=>typeof window>`u`?null:(!$&&e?$=new Xe(e):$&&e&&$.updateOptions(e),$),e.injectStyles=Ye});
|