copilot-chat-widget 0.1.28 → 0.1.29
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 +57 -80
- package/package.json +1 -1
- package/src/index.js +0 -1
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 v="http://localhost:3000",w=64,C=720,E=({iframeUrl:c,launcherIcon:p})=>{const r=document.querySelector("[data-copilot-widget-root]");r&&r.remove();const a=document.createElement("div");a.setAttribute("data-copilot-widget-root","true");const h=a.attachShadow({mode:"open"});document.body.appendChild(a);const t=document.createElement("button");t.type="button",t.setAttribute("aria-label","Open Copilot chat"),t.innerHTML=`
|
|
2
2
|
<img
|
|
3
|
-
src="${
|
|
3
|
+
src="${p}"
|
|
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
|
|
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 s=document.createElement("div"),b=document.createElement("div"),m=document.createElement("div");Object.assign(s.style,{position:"absolute",bottom:"-14px",right:"28px",width:"30px",height:"20px",pointerEvents:"none",display:"none",zIndex:"1"}),Object.assign(b.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(m.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"}),s.appendChild(b),s.appendChild(m);const l=document.createElement("div");Object.assign(l.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"}),h.appendChild(l);const o=document.createElement("iframe");o.src=c,o.title="Copilot chat widget",o.allow="clipboard-read; clipboard-write; microphone; camera; display-capture",o.setAttribute("scrolling","no"),Object.assign(o.style,{width:"100%",height:"100%",border:"none",display:"block",background:"transparent",overflow:"hidden"}),h.appendChild(l),l.appendChild(o),e.appendChild(s),e.appendChild(l),document.body.appendChild(t),document.body.appendChild(e);let i=!1;const u=()=>{i&&(i=!1,e.style.opacity="0",e.style.transform="scale(0.8) translateY(20px)",s.style.display="none",setTimeout(()=>{e.style.display="none"},250),t.style.transform="scale(1)")},y=d=>{const{data:n,source:f}=d||{};if(n?.type){if(n.type==="CART_CHECKOUT"&&f===o.contentWindow){console.log("[CopilotChat] Received checkout payload from widget:",n);return}if(n.type==="WIDGET_READY"&&f===o.contentWindow){o.contentWindow.postMessage({type:"INIT_WIDGET"},"*");return}n.type==="CHAT_CLOSED"&&u()}};window.addEventListener("message",y),t.onclick=d=>{d.stopPropagation(),i=!i,i?(e.style.display="block",s.style.display="block",requestAnimationFrame(()=>{e.style.opacity="1",e.style.transform="scale(1) translateY(0)"})):u()},document.addEventListener("click",d=>{const n=d.target;n&&i&&!e.contains(n)&&n!==t&&u()});const g={close:u,open:()=>{i||t.click()}};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.close=g.close,window.CopilotChat.open=g.open,window.CopilotChat.controls=g,g};async function x(c={}){if(typeof window>"u"||typeof document>"u")return null;const p=window.CopilotChatConfig||{},r=c.token||p.token||typeof window<"u"&&window.localStorage?.getItem("copilotChatToken")||null;if(!r)return console.error("[CopilotChat] Missing token (provide via loadCopilotChatWidget({ token }) or window.CopilotChatConfig.token)."),null;const a=c.baseUrl||p.baseUrl||v,h=`${a.replace(/\/$/,"")}/api/chat-widget/config?token=${encodeURIComponent(r)}`;try{const t=await fetch(h,{credentials:"omit",mode:"cors"});if(!t.ok)throw new Error(`Server responded with ${t.status}`);const e=await t.json();if(!e?.iframeUrl||!e?.launcherIcon)throw new Error("Received incomplete widget configuration from server.");return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.token=r,window.CopilotChat.baseUrl=a,E({iframeUrl:e.iframeUrl,launcherIcon:e.launcherIcon})}catch(t){return console.error("[CopilotChat] Error during widget bootstrap:",t instanceof Error?t.message:t),null}}exports.default=x;exports.loadCopilotChatWidget=x;
|
package/dist/index.mjs
CHANGED
|
@@ -1,38 +1,15 @@
|
|
|
1
1
|
const C = "http://localhost:3000";
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
r.setAttribute("data-copilot-checkout-toast", "true"), r.innerText = s, Object.assign(r.style, {
|
|
10
|
-
position: "fixed",
|
|
11
|
-
top: "20px",
|
|
12
|
-
left: "50%",
|
|
13
|
-
transform: "translateX(-50%)",
|
|
14
|
-
padding: "12px 16px",
|
|
15
|
-
background: "#0f172a",
|
|
16
|
-
color: "white",
|
|
17
|
-
borderRadius: "12px",
|
|
18
|
-
boxShadow: "0 8px 24px rgba(0,0,0,0.25)",
|
|
19
|
-
zIndex: 2147483647,
|
|
20
|
-
fontSize: "14px",
|
|
21
|
-
fontWeight: "600",
|
|
22
|
-
maxWidth: "420px",
|
|
23
|
-
lineHeight: "1.4",
|
|
24
|
-
textAlign: "center"
|
|
25
|
-
}), document.body.appendChild(r), setTimeout(() => {
|
|
26
|
-
r.remove();
|
|
27
|
-
}, 4500);
|
|
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);
|
|
2
|
+
const x = ({ iframeUrl: c, launcherIcon: p }) => {
|
|
3
|
+
const r = document.querySelector("[data-copilot-widget-root]");
|
|
4
|
+
r && r.remove();
|
|
5
|
+
const a = document.createElement("div");
|
|
6
|
+
a.setAttribute("data-copilot-widget-root", "true");
|
|
7
|
+
const h = a.attachShadow({ mode: "open" });
|
|
8
|
+
document.body.appendChild(a);
|
|
32
9
|
const t = document.createElement("button");
|
|
33
10
|
t.type = "button", t.setAttribute("aria-label", "Open Copilot chat"), t.innerHTML = `
|
|
34
11
|
<img
|
|
35
|
-
src="${
|
|
12
|
+
src="${p}"
|
|
36
13
|
alt="Copilot chat launcher"
|
|
37
14
|
style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
|
|
38
15
|
/>
|
|
@@ -70,8 +47,8 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
|
|
|
70
47
|
opacity: "0",
|
|
71
48
|
transition: "all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"
|
|
72
49
|
});
|
|
73
|
-
const
|
|
74
|
-
Object.assign(
|
|
50
|
+
const s = document.createElement("div"), g = document.createElement("div"), b = document.createElement("div");
|
|
51
|
+
Object.assign(s.style, {
|
|
75
52
|
position: "absolute",
|
|
76
53
|
bottom: "-14px",
|
|
77
54
|
right: "28px",
|
|
@@ -80,7 +57,7 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
|
|
|
80
57
|
pointerEvents: "none",
|
|
81
58
|
display: "none",
|
|
82
59
|
zIndex: "1"
|
|
83
|
-
}), Object.assign(
|
|
60
|
+
}), Object.assign(g.style, {
|
|
84
61
|
position: "absolute",
|
|
85
62
|
bottom: "0",
|
|
86
63
|
left: "0",
|
|
@@ -91,7 +68,7 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
|
|
|
91
68
|
borderLeft: "15px solid transparent",
|
|
92
69
|
borderRight: "15px solid transparent",
|
|
93
70
|
borderTop: "15px solid rgba(15,23,42,0.1)"
|
|
94
|
-
}), Object.assign(
|
|
71
|
+
}), Object.assign(b.style, {
|
|
95
72
|
position: "absolute",
|
|
96
73
|
bottom: "2px",
|
|
97
74
|
left: "0",
|
|
@@ -104,9 +81,9 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
|
|
|
104
81
|
borderTop: "13px solid white",
|
|
105
82
|
boxShadow: "0 6px 16px rgba(15,23,42,0.12)",
|
|
106
83
|
borderRadius: "2px"
|
|
107
|
-
}),
|
|
108
|
-
const
|
|
109
|
-
Object.assign(
|
|
84
|
+
}), s.appendChild(g), s.appendChild(b);
|
|
85
|
+
const l = document.createElement("div");
|
|
86
|
+
Object.assign(l.style, {
|
|
110
87
|
width: "720px",
|
|
111
88
|
maxWidth: "calc(100vw - 48px)",
|
|
112
89
|
height: `${Math.min(720, Math.max(320, window.innerHeight - 140))}px`,
|
|
@@ -116,84 +93,84 @@ const y = ({ iframeUrl: h, launcherIcon: u }) => {
|
|
|
116
93
|
border: "1px solid rgba(15,23,42,0.12)",
|
|
117
94
|
boxShadow: "0 18px 45px rgba(15,23,42,0.16)",
|
|
118
95
|
overflow: "hidden"
|
|
119
|
-
}),
|
|
120
|
-
const
|
|
121
|
-
|
|
96
|
+
}), h.appendChild(l);
|
|
97
|
+
const o = document.createElement("iframe");
|
|
98
|
+
o.src = c, o.title = "Copilot chat widget", o.allow = "clipboard-read; clipboard-write; microphone; camera; display-capture", o.setAttribute("scrolling", "no"), Object.assign(o.style, {
|
|
122
99
|
width: "100%",
|
|
123
100
|
height: "100%",
|
|
124
101
|
border: "none",
|
|
125
102
|
display: "block",
|
|
126
103
|
background: "transparent",
|
|
127
104
|
overflow: "hidden"
|
|
128
|
-
}),
|
|
129
|
-
let
|
|
130
|
-
const
|
|
131
|
-
|
|
105
|
+
}), h.appendChild(l), l.appendChild(o), e.appendChild(s), e.appendChild(l), document.body.appendChild(t), document.body.appendChild(e);
|
|
106
|
+
let i = !1;
|
|
107
|
+
const u = () => {
|
|
108
|
+
i && (i = !1, e.style.opacity = "0", e.style.transform = "scale(0.8) translateY(20px)", s.style.display = "none", setTimeout(() => {
|
|
132
109
|
e.style.display = "none";
|
|
133
110
|
}, 250), t.style.transform = "scale(1)");
|
|
134
|
-
},
|
|
135
|
-
const { data:
|
|
136
|
-
if (
|
|
137
|
-
if (
|
|
138
|
-
console.log("[CopilotChat] Received checkout payload from widget:",
|
|
111
|
+
}, f = (d) => {
|
|
112
|
+
const { data: n, source: m } = d || {};
|
|
113
|
+
if (n?.type) {
|
|
114
|
+
if (n.type === "CART_CHECKOUT" && m === o.contentWindow) {
|
|
115
|
+
console.log("[CopilotChat] Received checkout payload from widget:", n);
|
|
139
116
|
return;
|
|
140
117
|
}
|
|
141
|
-
if (
|
|
142
|
-
|
|
118
|
+
if (n.type === "WIDGET_READY" && m === o.contentWindow) {
|
|
119
|
+
o.contentWindow.postMessage({ type: "INIT_WIDGET" }, "*");
|
|
143
120
|
return;
|
|
144
121
|
}
|
|
145
|
-
|
|
122
|
+
n.type === "CHAT_CLOSED" && u();
|
|
146
123
|
}
|
|
147
124
|
};
|
|
148
|
-
window.addEventListener("message",
|
|
149
|
-
|
|
125
|
+
window.addEventListener("message", f), t.onclick = (d) => {
|
|
126
|
+
d.stopPropagation(), i = !i, i ? (e.style.display = "block", s.style.display = "block", requestAnimationFrame(() => {
|
|
150
127
|
e.style.opacity = "1", e.style.transform = "scale(1) translateY(0)";
|
|
151
|
-
})) :
|
|
152
|
-
}, document.addEventListener("click", (
|
|
153
|
-
const
|
|
154
|
-
|
|
128
|
+
})) : u();
|
|
129
|
+
}, document.addEventListener("click", (d) => {
|
|
130
|
+
const n = d.target;
|
|
131
|
+
n && i && !e.contains(n) && n !== t && u();
|
|
155
132
|
});
|
|
156
|
-
const
|
|
157
|
-
close:
|
|
133
|
+
const w = {
|
|
134
|
+
close: u,
|
|
158
135
|
open: () => {
|
|
159
|
-
|
|
136
|
+
i || t.click();
|
|
160
137
|
}
|
|
161
138
|
};
|
|
162
|
-
return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.close =
|
|
139
|
+
return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.close = w.close, window.CopilotChat.open = w.open, window.CopilotChat.controls = w, w;
|
|
163
140
|
};
|
|
164
|
-
async function
|
|
141
|
+
async function y(c = {}) {
|
|
165
142
|
if (typeof window > "u" || typeof document > "u")
|
|
166
143
|
return null;
|
|
167
|
-
const
|
|
168
|
-
if (!
|
|
144
|
+
const p = window.CopilotChatConfig || {}, r = c.token || p.token || typeof window < "u" && window.localStorage?.getItem("copilotChatToken") || null;
|
|
145
|
+
if (!r)
|
|
169
146
|
return console.error(
|
|
170
147
|
"[CopilotChat] Missing token (provide via loadCopilotChatWidget({ token }) or window.CopilotChatConfig.token)."
|
|
171
148
|
), null;
|
|
172
|
-
const
|
|
173
|
-
|
|
149
|
+
const a = c.baseUrl || p.baseUrl || C, h = `${a.replace(/\/$/, "")}/api/chat-widget/config?token=${encodeURIComponent(
|
|
150
|
+
r
|
|
174
151
|
)}`;
|
|
175
152
|
try {
|
|
176
|
-
const
|
|
153
|
+
const t = await fetch(h, {
|
|
177
154
|
credentials: "omit",
|
|
178
155
|
mode: "cors"
|
|
179
156
|
});
|
|
180
|
-
if (!
|
|
181
|
-
throw new Error(`Server responded with ${
|
|
182
|
-
const
|
|
183
|
-
if (!
|
|
157
|
+
if (!t.ok)
|
|
158
|
+
throw new Error(`Server responded with ${t.status}`);
|
|
159
|
+
const e = await t.json();
|
|
160
|
+
if (!e?.iframeUrl || !e?.launcherIcon)
|
|
184
161
|
throw new Error("Received incomplete widget configuration from server.");
|
|
185
|
-
return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.token =
|
|
186
|
-
iframeUrl:
|
|
187
|
-
launcherIcon:
|
|
162
|
+
return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.token = r, window.CopilotChat.baseUrl = a, x({
|
|
163
|
+
iframeUrl: e.iframeUrl,
|
|
164
|
+
launcherIcon: e.launcherIcon
|
|
188
165
|
});
|
|
189
|
-
} catch (
|
|
166
|
+
} catch (t) {
|
|
190
167
|
return console.error(
|
|
191
168
|
"[CopilotChat] Error during widget bootstrap:",
|
|
192
|
-
|
|
169
|
+
t instanceof Error ? t.message : t
|
|
193
170
|
), null;
|
|
194
171
|
}
|
|
195
172
|
}
|
|
196
173
|
export {
|
|
197
|
-
|
|
198
|
-
|
|
174
|
+
y as default,
|
|
175
|
+
y as loadCopilotChatWidget
|
|
199
176
|
};
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -209,7 +209,6 @@ 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("Data:",data);
|
|
213
212
|
return;
|
|
214
213
|
}
|
|
215
214
|
|