copilot-chat-widget 0.1.26 → 0.1.27
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/index.cjs +3 -3
- package/dist/index.mjs +28 -28
- package/package.json +1 -1
- package/src/index.js +0 -2
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const E="http://localhost:3000",w=64,C=720,k=({iframeUrl:h,launcherIcon:u})=>{const d=document.querySelector("[data-copilot-widget-root]");d&&d.remove();const g=s=>{const o=document.querySelector("[data-copilot-checkout-toast]");o&&o.remove();const r=document.createElement("div");r.setAttribute("data-copilot-checkout-toast","true"),r.innerText=s,Object.assign(r.style,{position:"fixed",top:"20px",left:"50%",transform:"translateX(-50%)",padding:"12px 16px",background:"#0f172a",color:"white",borderRadius:"12px",boxShadow:"0 8px 24px rgba(0,0,0,0.25)",zIndex:2147483647,fontSize:"14px",fontWeight:"600",maxWidth:"420px",lineHeight:"1.4",textAlign:"center"}),document.body.appendChild(r),setTimeout(()=>{r.remove()},4500)},c=document.createElement("div");c.setAttribute("data-copilot-widget-root","true");const n=c.attachShadow({mode:"open"});document.body.appendChild(c);const t=document.createElement("button");t.type="button",t.setAttribute("aria-label","Open Copilot chat"),t.innerHTML=`
|
|
2
2
|
<img
|
|
3
|
-
src="${
|
|
3
|
+
src="${u}"
|
|
4
4
|
alt="Copilot chat launcher"
|
|
5
5
|
style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
|
|
6
6
|
/>
|
|
7
|
-
`,Object.assign(t.style,{position:"fixed",bottom:"24px",right:"24px",width:`${w}px`,height:`${w}px`,borderRadius:"50%",border:"none",background:"linear-gradient(135deg, #0078ff, #00c6ff)",color:"white",cursor:"pointer",zIndex:999998,display:"flex",alignItems:"center",justifyContent:"center",boxShadow:"0 6px 14px rgba(0,0,0,0.25)",transition:"all 0.25s ease"}),t.onmouseover=()=>{t.style.transform="scale(1.12)",t.style.boxShadow="0 10px 25px rgba(0,0,0,0.3)"},t.onmouseout=()=>{t.style.transform="scale(1)",t.style.boxShadow="0 6px 14px rgba(0,0,0,0.25)"};const e=document.createElement("div");e.setAttribute("data-copilot-widget-root","true"),Object.assign(e.style,{display:"none",position:"fixed",bottom:`${w+36}px`,right:"24px",zIndex:"999999",transformOrigin:"bottom right",transform:"scale(0.8) translateY(20px)",opacity:"0",transition:"all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"});const l=document.createElement("div"),f=document.createElement("div"),x=document.createElement("div");Object.assign(l.style,{position:"absolute",bottom:"-14px",right:"28px",width:"30px",height:"20px",pointerEvents:"none",display:"none",zIndex:"1"}),Object.assign(f.style,{position:"absolute",bottom:"0",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"15px solid transparent",borderRight:"15px solid transparent",borderTop:"15px solid rgba(15,23,42,0.1)"}),Object.assign(x.style,{position:"absolute",bottom:"2px",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"13px solid transparent",borderRight:"13px solid transparent",borderTop:"13px solid white",boxShadow:"0 6px 16px rgba(15,23,42,0.12)",borderRadius:"2px"}),l.appendChild(f),l.appendChild(x);const
|
|
7
|
+
`,Object.assign(t.style,{position:"fixed",bottom:"24px",right:"24px",width:`${w}px`,height:`${w}px`,borderRadius:"50%",border:"none",background:"linear-gradient(135deg, #0078ff, #00c6ff)",color:"white",cursor:"pointer",zIndex:999998,display:"flex",alignItems:"center",justifyContent:"center",boxShadow:"0 6px 14px rgba(0,0,0,0.25)",transition:"all 0.25s ease"}),t.onmouseover=()=>{t.style.transform="scale(1.12)",t.style.boxShadow="0 10px 25px rgba(0,0,0,0.3)"},t.onmouseout=()=>{t.style.transform="scale(1)",t.style.boxShadow="0 6px 14px rgba(0,0,0,0.25)"};const e=document.createElement("div");e.setAttribute("data-copilot-widget-root","true"),Object.assign(e.style,{display:"none",position:"fixed",bottom:`${w+36}px`,right:"24px",zIndex:"999999",transformOrigin:"bottom right",transform:"scale(0.8) translateY(20px)",opacity:"0",transition:"all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"});const l=document.createElement("div"),f=document.createElement("div"),x=document.createElement("div");Object.assign(l.style,{position:"absolute",bottom:"-14px",right:"28px",width:"30px",height:"20px",pointerEvents:"none",display:"none",zIndex:"1"}),Object.assign(f.style,{position:"absolute",bottom:"0",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"15px solid transparent",borderRight:"15px solid transparent",borderTop:"15px solid rgba(15,23,42,0.1)"}),Object.assign(x.style,{position:"absolute",bottom:"2px",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"13px solid transparent",borderRight:"13px solid transparent",borderTop:"13px solid white",boxShadow:"0 6px 16px rgba(15,23,42,0.12)",borderRadius:"2px"}),l.appendChild(f),l.appendChild(x);const p=document.createElement("div");Object.assign(p.style,{width:`${C}px`,maxWidth:"calc(100vw - 48px)",height:`${Math.min(C,Math.max(320,window.innerHeight-140))}px`,maxHeight:"calc(100vh - 150px)",borderRadius:"20px",background:"white",border:"1px solid rgba(15,23,42,0.12)",boxShadow:"0 18px 45px rgba(15,23,42,0.16)",overflow:"hidden"}),n.appendChild(p);const i=document.createElement("iframe");i.src=h,i.title="Copilot chat widget",i.allow="clipboard-read; clipboard-write; microphone; camera; display-capture",i.setAttribute("scrolling","no"),Object.assign(i.style,{width:"100%",height:"100%",border:"none",display:"block",background:"transparent",overflow:"hidden"}),n.appendChild(p),p.appendChild(i),e.appendChild(l),e.appendChild(p),document.body.appendChild(t),document.body.appendChild(e);let a=!1;const b=()=>{a&&(a=!1,e.style.opacity="0",e.style.transform="scale(0.8) translateY(20px)",l.style.display="none",setTimeout(()=>{e.style.display="none"},250),t.style.transform="scale(1)")},v=s=>{const{data:o,source:r}=s||{};if(o?.type){if(o.type==="CART_CHECKOUT"&&r===i.contentWindow){console.log("[CopilotChat] Received checkout payload from widget:",o),g("Data:");return}if(o.type==="WIDGET_READY"&&r===i.contentWindow){i.contentWindow.postMessage({type:"INIT_WIDGET"},"*");return}o.type==="CHAT_CLOSED"&&b()}};window.addEventListener("message",v),t.onclick=s=>{s.stopPropagation(),a=!a,a?(e.style.display="block",l.style.display="block",requestAnimationFrame(()=>{e.style.opacity="1",e.style.transform="scale(1) translateY(0)"})):b()},document.addEventListener("click",s=>{const o=s.target;o&&a&&!e.contains(o)&&o!==t&&b()});const m={close:b,open:()=>{a||t.click()}};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.close=m.close,window.CopilotChat.open=m.open,window.CopilotChat.controls=m,m};async function y(h={}){if(typeof window>"u"||typeof document>"u")return null;const u=window.CopilotChatConfig||{},d=h.token||u.token||typeof window<"u"&&window.localStorage?.getItem("copilotChatToken")||null;if(!d)return console.error("[CopilotChat] Missing token (provide via loadCopilotChatWidget({ token }) or window.CopilotChatConfig.token)."),null;const g=h.baseUrl||u.baseUrl||E,c=`${g.replace(/\/$/,"")}/api/chat-widget/config?token=${encodeURIComponent(d)}`;try{const n=await fetch(c,{credentials:"omit",mode:"cors"});if(!n.ok)throw new Error(`Server responded with ${n.status}`);const t=await n.json();if(!t?.iframeUrl||!t?.launcherIcon)throw new Error("Received incomplete widget configuration from server.");return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.token=d,window.CopilotChat.baseUrl=g,k({iframeUrl:t.iframeUrl,launcherIcon:t.launcherIcon})}catch(n){return console.error("[CopilotChat] Error during widget bootstrap:",n instanceof Error?n.message:n),null}}exports.default=y;exports.loadCopilotChatWidget=y;
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const C = "http://localhost:3000";
|
|
2
|
-
const y = ({ iframeUrl:
|
|
2
|
+
const y = ({ iframeUrl: h, launcherIcon: u }) => {
|
|
3
3
|
const d = document.querySelector("[data-copilot-widget-root]");
|
|
4
4
|
d && d.remove();
|
|
5
|
-
const
|
|
5
|
+
const g = (s) => {
|
|
6
6
|
const o = document.querySelector("[data-copilot-checkout-toast]");
|
|
7
7
|
o && o.remove();
|
|
8
8
|
const r = document.createElement("div");
|
|
@@ -25,14 +25,14 @@ const y = ({ iframeUrl: u, launcherIcon: g }) => {
|
|
|
25
25
|
}), document.body.appendChild(r), setTimeout(() => {
|
|
26
26
|
r.remove();
|
|
27
27
|
}, 4500);
|
|
28
|
-
},
|
|
29
|
-
|
|
30
|
-
const n =
|
|
31
|
-
document.body.appendChild(
|
|
28
|
+
}, c = document.createElement("div");
|
|
29
|
+
c.setAttribute("data-copilot-widget-root", "true");
|
|
30
|
+
const n = c.attachShadow({ mode: "open" });
|
|
31
|
+
document.body.appendChild(c);
|
|
32
32
|
const t = document.createElement("button");
|
|
33
33
|
t.type = "button", t.setAttribute("aria-label", "Open Copilot chat"), t.innerHTML = `
|
|
34
34
|
<img
|
|
35
|
-
src="${
|
|
35
|
+
src="${u}"
|
|
36
36
|
alt="Copilot chat launcher"
|
|
37
37
|
style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
|
|
38
38
|
/>
|
|
@@ -70,7 +70,7 @@ const y = ({ iframeUrl: u, launcherIcon: g }) => {
|
|
|
70
70
|
opacity: "0",
|
|
71
71
|
transition: "all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"
|
|
72
72
|
});
|
|
73
|
-
const l = document.createElement("div"),
|
|
73
|
+
const l = document.createElement("div"), m = document.createElement("div"), f = document.createElement("div");
|
|
74
74
|
Object.assign(l.style, {
|
|
75
75
|
position: "absolute",
|
|
76
76
|
bottom: "-14px",
|
|
@@ -80,7 +80,7 @@ const y = ({ iframeUrl: u, launcherIcon: g }) => {
|
|
|
80
80
|
pointerEvents: "none",
|
|
81
81
|
display: "none",
|
|
82
82
|
zIndex: "1"
|
|
83
|
-
}), Object.assign(
|
|
83
|
+
}), Object.assign(m.style, {
|
|
84
84
|
position: "absolute",
|
|
85
85
|
bottom: "0",
|
|
86
86
|
left: "0",
|
|
@@ -104,9 +104,9 @@ const y = ({ iframeUrl: u, launcherIcon: g }) => {
|
|
|
104
104
|
borderTop: "13px solid white",
|
|
105
105
|
boxShadow: "0 6px 16px rgba(15,23,42,0.12)",
|
|
106
106
|
borderRadius: "2px"
|
|
107
|
-
}), l.appendChild(
|
|
108
|
-
const
|
|
109
|
-
Object.assign(
|
|
107
|
+
}), l.appendChild(m), l.appendChild(f);
|
|
108
|
+
const p = document.createElement("div");
|
|
109
|
+
Object.assign(p.style, {
|
|
110
110
|
width: "720px",
|
|
111
111
|
maxWidth: "calc(100vw - 48px)",
|
|
112
112
|
height: `${Math.min(720, Math.max(320, window.innerHeight - 140))}px`,
|
|
@@ -116,18 +116,18 @@ const y = ({ iframeUrl: u, launcherIcon: g }) => {
|
|
|
116
116
|
border: "1px solid rgba(15,23,42,0.12)",
|
|
117
117
|
boxShadow: "0 18px 45px rgba(15,23,42,0.16)",
|
|
118
118
|
overflow: "hidden"
|
|
119
|
-
}), n.appendChild(
|
|
119
|
+
}), n.appendChild(p);
|
|
120
120
|
const i = document.createElement("iframe");
|
|
121
|
-
i.src =
|
|
121
|
+
i.src = h, i.title = "Copilot chat widget", i.allow = "clipboard-read; clipboard-write; microphone; camera; display-capture", i.setAttribute("scrolling", "no"), Object.assign(i.style, {
|
|
122
122
|
width: "100%",
|
|
123
123
|
height: "100%",
|
|
124
124
|
border: "none",
|
|
125
125
|
display: "block",
|
|
126
126
|
background: "transparent",
|
|
127
127
|
overflow: "hidden"
|
|
128
|
-
}), n.appendChild(
|
|
128
|
+
}), n.appendChild(p), p.appendChild(i), e.appendChild(l), e.appendChild(p), document.body.appendChild(t), document.body.appendChild(e);
|
|
129
129
|
let a = !1;
|
|
130
|
-
const
|
|
130
|
+
const w = () => {
|
|
131
131
|
a && (a = !1, e.style.opacity = "0", e.style.transform = "scale(0.8) translateY(20px)", l.style.display = "none", setTimeout(() => {
|
|
132
132
|
e.style.display = "none";
|
|
133
133
|
}, 250), t.style.transform = "scale(1)");
|
|
@@ -135,45 +135,45 @@ const y = ({ iframeUrl: u, launcherIcon: g }) => {
|
|
|
135
135
|
const { data: o, source: r } = s || {};
|
|
136
136
|
if (o?.type) {
|
|
137
137
|
if (o.type === "CART_CHECKOUT" && r === i.contentWindow) {
|
|
138
|
-
console.log("[CopilotChat] Received checkout payload from widget:", o),
|
|
138
|
+
console.log("[CopilotChat] Received checkout payload from widget:", o), g("Data:");
|
|
139
139
|
return;
|
|
140
140
|
}
|
|
141
141
|
if (o.type === "WIDGET_READY" && r === i.contentWindow) {
|
|
142
142
|
i.contentWindow.postMessage({ type: "INIT_WIDGET" }, "*");
|
|
143
143
|
return;
|
|
144
144
|
}
|
|
145
|
-
o.type === "CHAT_CLOSED" &&
|
|
145
|
+
o.type === "CHAT_CLOSED" && w();
|
|
146
146
|
}
|
|
147
147
|
};
|
|
148
148
|
window.addEventListener("message", x), t.onclick = (s) => {
|
|
149
149
|
s.stopPropagation(), a = !a, a ? (e.style.display = "block", l.style.display = "block", requestAnimationFrame(() => {
|
|
150
150
|
e.style.opacity = "1", e.style.transform = "scale(1) translateY(0)";
|
|
151
|
-
})) :
|
|
151
|
+
})) : w();
|
|
152
152
|
}, document.addEventListener("click", (s) => {
|
|
153
153
|
const o = s.target;
|
|
154
|
-
o && a && !e.contains(o) && o !== t &&
|
|
154
|
+
o && a && !e.contains(o) && o !== t && w();
|
|
155
155
|
});
|
|
156
|
-
const
|
|
157
|
-
close:
|
|
156
|
+
const b = {
|
|
157
|
+
close: w,
|
|
158
158
|
open: () => {
|
|
159
159
|
a || t.click();
|
|
160
160
|
}
|
|
161
161
|
};
|
|
162
|
-
return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.close =
|
|
162
|
+
return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.close = b.close, window.CopilotChat.open = b.open, window.CopilotChat.controls = b, b;
|
|
163
163
|
};
|
|
164
|
-
async function E(
|
|
164
|
+
async function E(h = {}) {
|
|
165
165
|
if (typeof window > "u" || typeof document > "u")
|
|
166
166
|
return null;
|
|
167
|
-
const
|
|
167
|
+
const u = window.CopilotChatConfig || {}, d = h.token || u.token || typeof window < "u" && window.localStorage?.getItem("copilotChatToken") || null;
|
|
168
168
|
if (!d)
|
|
169
169
|
return console.error(
|
|
170
170
|
"[CopilotChat] Missing token (provide via loadCopilotChatWidget({ token }) or window.CopilotChatConfig.token)."
|
|
171
171
|
), null;
|
|
172
|
-
const
|
|
172
|
+
const g = h.baseUrl || u.baseUrl || C, c = `${g.replace(/\/$/, "")}/api/chat-widget/config?token=${encodeURIComponent(
|
|
173
173
|
d
|
|
174
174
|
)}`;
|
|
175
175
|
try {
|
|
176
|
-
const n = await fetch(
|
|
176
|
+
const n = await fetch(c, {
|
|
177
177
|
credentials: "omit",
|
|
178
178
|
mode: "cors"
|
|
179
179
|
});
|
|
@@ -182,7 +182,7 @@ async function E(u = {}) {
|
|
|
182
182
|
const t = await n.json();
|
|
183
183
|
if (!t?.iframeUrl || !t?.launcherIcon)
|
|
184
184
|
throw new Error("Received incomplete widget configuration from server.");
|
|
185
|
-
return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.token = d, window.CopilotChat.baseUrl =
|
|
185
|
+
return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.token = d, window.CopilotChat.baseUrl = g, y({
|
|
186
186
|
iframeUrl: t.iframeUrl,
|
|
187
187
|
launcherIcon: t.launcherIcon
|
|
188
188
|
});
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -209,9 +209,7 @@ const buildWidget = ({ iframeUrl, launcherIcon }) => {
|
|
|
209
209
|
|
|
210
210
|
if (data.type === "CART_CHECKOUT" && source === iframe.contentWindow) {
|
|
211
211
|
console.log("[CopilotChat] Received checkout payload from widget:", data);
|
|
212
|
-
showCheckoutToast("Checkout message received from chat widget. Check console for payload.");
|
|
213
212
|
showCheckoutToast("Data:",data);
|
|
214
|
-
|
|
215
213
|
return;
|
|
216
214
|
}
|
|
217
215
|
|