@ziila/sdk 0.1.7
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/entries/index.d.ts +3 -0
- package/dist/index.cjs.js +1 -0
- package/dist/index.esm.js +331 -0
- package/dist/src/ConfirmDialog.d.ts +15 -0
- package/dist/src/EventBus.d.ts +9 -0
- package/dist/src/MessageBridge.d.ts +13 -0
- package/dist/src/ZiilaWidget.d.ts +19 -0
- package/dist/src/__tests__/EventBus.test.d.ts +1 -0
- package/dist/src/__tests__/MessageBridge.test.d.ts +1 -0
- package/dist/src/__tests__/ZiilaWidget.test.d.ts +1 -0
- package/dist/src/__tests__/i18n.test.d.ts +1 -0
- package/dist/src/__tests__/theme.test.d.ts +1 -0
- package/dist/src/__tests__/themeStyles.test.d.ts +1 -0
- package/dist/src/__tests__/utils.test.d.ts +1 -0
- package/dist/src/i18n.d.ts +5 -0
- package/dist/src/theme.d.ts +2 -0
- package/dist/src/themeStyles.d.ts +5 -0
- package/dist/src/types.d.ts +101 -0
- package/dist/src/utils.d.ts +2 -0
- package/dist/widget.iife.js +1 -0
- package/package.json +42 -0
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { ZiilaWidget } from '../src/ZiilaWidget';
|
|
2
|
+
export { isZiilaError } from '../src/utils';
|
|
3
|
+
export type { ThemeTokens, ThemeStyles, ElementStyle, StyleStates, StyleDecls, ThemeElement, ZiilaConfig, WidgetInitConfig, ZiilaEvent, ZiilaCommand, EventPayloadMap, CommandPayloadMap, AddToCartPayload, ProductViewedPayload, SearchPayload, ErrorPayload, ParentMessage, WidgetMessage, HandshakeMessage, } from '../src/types';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=class{constructor(e,t){this.iframe=null,this.port=null,this.handleHandshake=e=>{let t=this.iframe?.contentWindow;if(!t||e.source!==t||e.origin!==new URL(this.url).origin)return;let n=e.data;if(typeof n!=`object`||!n||n.type!==`HANDSHAKE`)return;let r=new MessageChannel;t.postMessage({type:`HANDSHAKE_ACK`},new URL(this.url).origin,[r.port2]),this.port=r.port1,this.port.start(),window.removeEventListener(`message`,this.handleHandshake),this.onPortReady(r.port1)},this.onPortReady=e,this.url=t??`https://widget.ziila.com`}mount(e){this.iframe=document.createElement(`iframe`),this.iframe.src=this.url,this.iframe.title=`Ziila Shopping Assistant`,this.iframe.style.cssText=`width:100%;height:100%;border:none;`,e.appendChild(this.iframe),window.addEventListener(`message`,this.handleHandshake)}send(e){this.port?.postMessage(e)}destroy(){window.removeEventListener(`message`,this.handleHandshake),this.port?.close(),this.iframe?.remove(),this.iframe=null,this.port=null}},t=class{constructor(){this.handlers=new Map}on(e,t){return this.handlers.has(e)||this.handlers.set(e,new Set),this.handlers.get(e).add(t),()=>this.off(e,t)}off(e,t){this.handlers.get(e)?.delete(t)}emit(e,t){this.handlers.get(e)?.forEach(e=>e(t))}destroy(){this.handlers.clear()}},n=`position:fixed;inset:0;z-index:2147483647;display:flex;align-items:center;justify-content:center;background:rgba(0,0,0,0.4);padding:24px;font-family:system-ui,-apple-system,sans-serif;`,r=`width:100%;max-width:24rem;border-radius:12px;background:#fff;padding:16px;box-shadow:2px 4px 2px rgba(0,0,0,0.1);display:flex;flex-direction:column;gap:16px;`,i=`display:flex;justify-content:flex-end;`,a=`border:none;background:#f0f0f0;border-radius:12px;width:32px;height:32px;display:flex;align-items:center;justify-content:center;cursor:pointer;font-size:14px;color:#555;line-height:1;padding:0;`,o=`display:flex;flex-direction:column;gap:16px;align-items:center;`,ee=`margin:0;font-size:24px;font-weight:700;color:#195f6b;text-align:center;`,s=`margin:0;font-size:16px;color:#2d2d2d;font-weight:500;line-height:27px;text-align:center;max-width:345px;`,c=`display:flex;gap:16px;padding:0 16px;width:100%;box-sizing:border-box;`,te=`flex:1;border:1px solid #195f6b;border-radius:8px;background:#fff;color:#195f6b;padding:16px;font-size:16px;font-weight:500;cursor:pointer;`,ne=`flex:1;border:none;border-radius:8px;background:#cc0000;color:#fff;padding:16px;font-size:16px;font-weight:500;cursor:pointer;`,l=class{constructor(){this.overlay=null,this.pendingResolve=null,this.previousFocus=null}show(e){return this.pendingResolve?.(!1),this.pendingResolve=null,this.close(),new Promise(t=>{this.pendingResolve=t;let l=e=>{this.pendingResolve=null,this.close(),t(e)},u=document.createElement(`div`);u.style.cssText=n;let d=document.createElement(`div`);d.setAttribute(`role`,`dialog`),d.setAttribute(`aria-modal`,`true`),d.setAttribute(`aria-labelledby`,`ziila-confirm-title`),d.style.cssText=r,d.dir=e.dir;let f=document.createElement(`div`);f.style.cssText=i;let p=document.createElement(`button`);p.type=`button`,p.setAttribute(`aria-label`,e.closeLabel),p.textContent=`✕`,p.style.cssText=a,p.addEventListener(`click`,()=>l(!1)),f.appendChild(p);let m=document.createElement(`div`);m.style.cssText=o;let h=document.createElement(`h2`);h.id=`ziila-confirm-title`,h.textContent=e.title,h.style.cssText=ee;let g=document.createElement(`p`);g.textContent=e.message,g.style.cssText=s;let _=document.createElement(`button`);_.type=`button`,_.textContent=e.cancelLabel,_.style.cssText=te,_.addEventListener(`click`,()=>l(!1));let v=document.createElement(`button`);v.type=`button`,v.textContent=e.confirmLabel,v.style.cssText=ne,v.addEventListener(`click`,()=>l(!0));let y=document.createElement(`div`);y.style.cssText=c,y.append(_,v),m.append(h,g,y),d.append(f,m),u.appendChild(d);let b=[p,_,v];u.addEventListener(`keydown`,e=>{if(e.key===`Escape`){l(!1);return}e.key===`Tab`&&(e.preventDefault(),b[(b.indexOf(document.activeElement)+(e.shiftKey?-1:1)+b.length)%b.length].focus())}),this.previousFocus=document.activeElement,this.overlay=u,document.body.appendChild(u),_.focus()})}close(){this.overlay?.remove(),this.overlay=null,this.previousFocus?.focus(),this.previousFocus=null}},u={primaryColor:[`--primary`,`--primary-color`,`--color-primary`,`--brand`,`--brand-color`,`--accent-color`,`--bs-primary`,`--mui-palette-primary-main`,`--ant-color-primary`],borderRadius:[`--radius`,`--border-radius`,`--rounded`,`--bs-border-radius`]},d=new Set([``,`transparent`,`inherit`,`initial`,`unset`,`none`,`rgba(0, 0, 0, 0)`,`rgb(0, 0, 0)`,`depends on user agent`]);function f(e){return!!e&&!d.has(e.trim().toLowerCase())}function p(e=document){let t=e.defaultView??globalThis,n=t.getComputedStyle(e.documentElement),r=e.body?t.getComputedStyle(e.body):null,i={};r&&(f(r.fontFamily)&&(i.fontFamily=r.fontFamily),f(r.backgroundColor)&&(i.backgroundColor=r.backgroundColor),f(r.color)&&(i.foregroundColor=r.color));for(let[e,t]of Object.entries(u))for(let r of t){let t=n.getPropertyValue(r).trim();if(f(t)){i[e]=t;break}}return i}function m(e,t={}){let n={...p(t.doc??document),...e?.tokens},r={};return Object.keys(n).length>0&&(r.tokens=n),e?.elements&&Object.keys(e.elements).length>0&&(r.elements=e.elements),r}var h={},g=[`en`,`ar`],_=[`baseLocale`],v=[],y,b;function x(e){if(v.length===0)return;let t=typeof e==`string`?e:e.href;if(y===t)return b;let n=new URL(t,`http://dummy.com`),r;for(let e of v)if(new h(e.match,n.href).exec(n.href)){r=e;break}return y=t,b=r,r}function S(e){let t=x(e);return t&&t.exclude!==!0&&Array.isArray(t.strategy)?t.strategy:_}var C=void 0;globalThis.__paraglide=globalThis.__paraglide??{},globalThis.__paraglide.ssr=globalThis.__paraglide.ssr??{};var w=!1,T=()=>{if(C){let e=C?.getStore()?.locale;if(e)return e}let e=_;typeof window<`u`&&window.location?.href&&(e=S(window.location.href));let t=E(e,typeof window<`u`?window.location?.href:void 0);if(t)return w||(w=!0,k(t,{reload:!1})),t;throw Error(`No locale found. Read the docs https://inlang.com/m/gerre34r/library-inlang-paraglideJs/errors#no-locale-found`)};function E(e,t){let n;for(let t of e){if(t===`baseLocale`)n=`en`;else if(N(t)&&M.has(t)){let e=M.get(t);if(e){let t=e.getLocale();if(t instanceof Promise)continue;if(t!==void 0)return j(t)}}let e=A(n);if(e)return e}}var D=e=>{T=e},O=e=>{e?window.location.href=e:window.location.reload()},k=(e,t)=>{let n={reload:!0,...t},r;try{r=T()}catch{}let i=[],a=_;typeof window<`u`&&window.location?.href&&(a=S(window.location.href));for(let t of a)if(t===`baseLocale`)continue;else if(N(t)&&M.has(t)){let n=M.get(t);if(n){let r=n.setLocale(e);r instanceof Promise&&(r=r.catch(e=>{throw Error(`Custom strategy "${t}" setLocale failed.`,{cause:e})}),i.push(r))}}let o=()=>{n.reload&&window.location&&e!==r&&O(void 0)};if(i.length)return Promise.all(i).then(()=>{o()});o()};function A(e){if(typeof e!=`string`)return;let t=e.toLowerCase();for(let e of g)if(e.toLowerCase()===t)return e}function j(e){let t=A(e);if(t)return t;throw Error(`Invalid locale: ${e}. Expected one of: ${g.join(`, `)}`)}var M=new Map;function N(e){return typeof e==`string`&&/^custom-[A-Za-z0-9_-]+$/.test(e)}var P=()=>`End Chat`,F=()=>`إنهاء المحادثة`,I=((e={},t={})=>(t.locale??T())===`en`?P(e):F(e)),L=()=>`Are you sure you want to end this chat?`,R=()=>`هل أنت متأكد أنك تريد إنهاء هذه المحادثة؟`,z=((e={},t={})=>(t.locale??T())===`en`?L(e):R(e)),B=()=>`End Chat`,V=()=>`إنهاء المحادثة`,H=((e={},t={})=>(t.locale??T())===`en`?B(e):V(e)),U=()=>`Cancel`,W=()=>`إلغاء`,G=((e={},t={})=>(t.locale??T())===`en`?U(e):W(e)),re=()=>`Leave Chat`,K=()=>`مغادرة المحادثة`,q=((e={},t={})=>(t.locale??T())===`en`?re(e):K(e)),J=()=>`If you leave now, your chat will end. Are you sure you want to leave?`,Y=()=>`إذا غادرت الآن، ستنتهي محادثتك. هل أنت متأكد أنك تريد المغادرة؟`,X=((e={},t={})=>(t.locale??T())===`en`?J(e):Y(e)),ie=()=>`Leave`,ae=()=>`مغادرة`,oe=((e={},t={})=>(t.locale??T())===`en`?ie(e):ae(e)),se=()=>`Stay`,ce=()=>`البقاء`,le=((e={},t={})=>(t.locale??T())===`en`?se(e):ce(e)),ue=()=>`Close`,de=()=>`إغلاق`,fe=((e={},t={})=>(t.locale??T())===`en`?ue(e):de(e)),pe=new Set(g),Z=`en`;D(()=>Z);function me(){return Z}function Q(e){Z=e&&pe.has(e)?e:`en`}function he(e){return e===`ar`?`rtl`:`ltr`}function ge(e){let t=he(me()),n=fe();return e===`endChat`?{title:I(),message:z(),confirmLabel:H(),cancelLabel:G(),closeLabel:n,dir:t}:{title:q(),message:X(),confirmLabel:oe(),cancelLabel:le(),closeLabel:n,dir:t}}var $=class{get isDirty(){return this._isDirty}constructor(e){this.bridge=null,this.bus=new t,this.confirmDialog=new l,this._isDirty=!1,this.handleBeforeUnload=e=>{this._isDirty&&(e.preventDefault(),e.returnValue=``)},this.handleMessage=e=>{let t=e.data;t.type===`READY`?this.bus.emit(`ready`):t.type===`EVENT`?this.bus.emit(t.event,t.payload):t.type===`DIRTY_STATE`?this._isDirty=t.isDirty:t.type===`CONFIRM_REQUEST`&&this.handleConfirmRequest(t.kind)},this.config=e,Q(e.locale)}mount(){if(this.bridge)return;let t=typeof this.config.container==`string`?document.querySelector(this.config.container):this.config.container;if(!t)throw Error(`Ziila: container not found: ${this.config.container}`);this.bridge=new e(e=>{e.onmessage=this.handleMessage;let{container:t,...n}=this.config,r=m(this.config.theme);this.bridge?.send({type:`INIT`,payload:{...n,theme:r}})},this.config.widgetUrl),this.bridge.mount(t),window.addEventListener(`beforeunload`,this.handleBeforeUnload)}handleConfirmRequest(e){this.confirmDialog.show(ge(e)).then(t=>{this.bridge?.send({type:`CONFIRM_RESULT`,kind:e,confirmed:t})})}on(e,t){return this.bus.on(e,t)}off(e,t){this.bus.off(e,t)}emit(e,t){this.bridge?.send({type:`COMMAND`,command:e,payload:t})}update(e){this.config={...this.config,...e},e.locale!==void 0&&Q(e.locale),this.bridge?.send({type:`UPDATE`,payload:e})}destroy(){window.removeEventListener(`beforeunload`,this.handleBeforeUnload),this.confirmDialog.close(),this.bridge?.destroy(),this.bus.destroy(),this.bridge=null}};function _e(e){return typeof e==`object`&&!!e&&typeof e.code==`string`&&typeof e.message==`string`}exports.ZiilaWidget=$,exports.isZiilaError=_e;
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
var e = class {
|
|
2
|
+
constructor(e, t) {
|
|
3
|
+
this.iframe = null, this.port = null, this.handleHandshake = (e) => {
|
|
4
|
+
let t = this.iframe?.contentWindow;
|
|
5
|
+
if (!t || e.source !== t || e.origin !== new URL(this.url).origin) return;
|
|
6
|
+
let n = e.data;
|
|
7
|
+
if (typeof n != "object" || !n || n.type !== "HANDSHAKE") return;
|
|
8
|
+
let r = new MessageChannel();
|
|
9
|
+
t.postMessage({ type: "HANDSHAKE_ACK" }, new URL(this.url).origin, [r.port2]), this.port = r.port1, this.port.start(), window.removeEventListener("message", this.handleHandshake), this.onPortReady(r.port1);
|
|
10
|
+
}, this.onPortReady = e, this.url = t ?? "https://widget.ziila.com";
|
|
11
|
+
}
|
|
12
|
+
mount(e) {
|
|
13
|
+
this.iframe = document.createElement("iframe"), this.iframe.src = this.url, this.iframe.title = "Ziila Shopping Assistant", this.iframe.style.cssText = "width:100%;height:100%;border:none;", e.appendChild(this.iframe), window.addEventListener("message", this.handleHandshake);
|
|
14
|
+
}
|
|
15
|
+
send(e) {
|
|
16
|
+
this.port?.postMessage(e);
|
|
17
|
+
}
|
|
18
|
+
destroy() {
|
|
19
|
+
window.removeEventListener("message", this.handleHandshake), this.port?.close(), this.iframe?.remove(), this.iframe = null, this.port = null;
|
|
20
|
+
}
|
|
21
|
+
}, t = class {
|
|
22
|
+
constructor() {
|
|
23
|
+
this.handlers = /* @__PURE__ */ new Map();
|
|
24
|
+
}
|
|
25
|
+
on(e, t) {
|
|
26
|
+
return this.handlers.has(e) || this.handlers.set(e, /* @__PURE__ */ new Set()), this.handlers.get(e).add(t), () => this.off(e, t);
|
|
27
|
+
}
|
|
28
|
+
off(e, t) {
|
|
29
|
+
this.handlers.get(e)?.delete(t);
|
|
30
|
+
}
|
|
31
|
+
emit(e, t) {
|
|
32
|
+
this.handlers.get(e)?.forEach((e) => e(t));
|
|
33
|
+
}
|
|
34
|
+
destroy() {
|
|
35
|
+
this.handlers.clear();
|
|
36
|
+
}
|
|
37
|
+
}, n = "position:fixed;inset:0;z-index:2147483647;display:flex;align-items:center;justify-content:center;background:rgba(0,0,0,0.4);padding:24px;font-family:system-ui,-apple-system,sans-serif;", r = "width:100%;max-width:24rem;border-radius:12px;background:#fff;padding:16px;box-shadow:2px 4px 2px rgba(0,0,0,0.1);display:flex;flex-direction:column;gap:16px;", i = "display:flex;justify-content:flex-end;", a = "border:none;background:#f0f0f0;border-radius:12px;width:32px;height:32px;display:flex;align-items:center;justify-content:center;cursor:pointer;font-size:14px;color:#555;line-height:1;padding:0;", o = "display:flex;flex-direction:column;gap:16px;align-items:center;", ee = "margin:0;font-size:24px;font-weight:700;color:#195f6b;text-align:center;", s = "margin:0;font-size:16px;color:#2d2d2d;font-weight:500;line-height:27px;text-align:center;max-width:345px;", c = "display:flex;gap:16px;padding:0 16px;width:100%;box-sizing:border-box;", te = "flex:1;border:1px solid #195f6b;border-radius:8px;background:#fff;color:#195f6b;padding:16px;font-size:16px;font-weight:500;cursor:pointer;", ne = "flex:1;border:none;border-radius:8px;background:#cc0000;color:#fff;padding:16px;font-size:16px;font-weight:500;cursor:pointer;", l = class {
|
|
38
|
+
constructor() {
|
|
39
|
+
this.overlay = null, this.pendingResolve = null, this.previousFocus = null;
|
|
40
|
+
}
|
|
41
|
+
show(e) {
|
|
42
|
+
return this.pendingResolve?.(!1), this.pendingResolve = null, this.close(), new Promise((t) => {
|
|
43
|
+
this.pendingResolve = t;
|
|
44
|
+
let l = (e) => {
|
|
45
|
+
this.pendingResolve = null, this.close(), t(e);
|
|
46
|
+
}, u = document.createElement("div");
|
|
47
|
+
u.style.cssText = n;
|
|
48
|
+
let d = document.createElement("div");
|
|
49
|
+
d.setAttribute("role", "dialog"), d.setAttribute("aria-modal", "true"), d.setAttribute("aria-labelledby", "ziila-confirm-title"), d.style.cssText = r, d.dir = e.dir;
|
|
50
|
+
let f = document.createElement("div");
|
|
51
|
+
f.style.cssText = i;
|
|
52
|
+
let p = document.createElement("button");
|
|
53
|
+
p.type = "button", p.setAttribute("aria-label", e.closeLabel), p.textContent = "✕", p.style.cssText = a, p.addEventListener("click", () => l(!1)), f.appendChild(p);
|
|
54
|
+
let m = document.createElement("div");
|
|
55
|
+
m.style.cssText = o;
|
|
56
|
+
let h = document.createElement("h2");
|
|
57
|
+
h.id = "ziila-confirm-title", h.textContent = e.title, h.style.cssText = ee;
|
|
58
|
+
let g = document.createElement("p");
|
|
59
|
+
g.textContent = e.message, g.style.cssText = s;
|
|
60
|
+
let _ = document.createElement("button");
|
|
61
|
+
_.type = "button", _.textContent = e.cancelLabel, _.style.cssText = te, _.addEventListener("click", () => l(!1));
|
|
62
|
+
let v = document.createElement("button");
|
|
63
|
+
v.type = "button", v.textContent = e.confirmLabel, v.style.cssText = ne, v.addEventListener("click", () => l(!0));
|
|
64
|
+
let y = document.createElement("div");
|
|
65
|
+
y.style.cssText = c, y.append(_, v), m.append(h, g, y), d.append(f, m), u.appendChild(d);
|
|
66
|
+
let b = [
|
|
67
|
+
p,
|
|
68
|
+
_,
|
|
69
|
+
v
|
|
70
|
+
];
|
|
71
|
+
u.addEventListener("keydown", (e) => {
|
|
72
|
+
if (e.key === "Escape") {
|
|
73
|
+
l(!1);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
e.key === "Tab" && (e.preventDefault(), b[(b.indexOf(document.activeElement) + (e.shiftKey ? -1 : 1) + b.length) % b.length].focus());
|
|
77
|
+
}), this.previousFocus = document.activeElement, this.overlay = u, document.body.appendChild(u), _.focus();
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
close() {
|
|
81
|
+
this.overlay?.remove(), this.overlay = null, this.previousFocus?.focus(), this.previousFocus = null;
|
|
82
|
+
}
|
|
83
|
+
}, u = {
|
|
84
|
+
primaryColor: [
|
|
85
|
+
"--primary",
|
|
86
|
+
"--primary-color",
|
|
87
|
+
"--color-primary",
|
|
88
|
+
"--brand",
|
|
89
|
+
"--brand-color",
|
|
90
|
+
"--accent-color",
|
|
91
|
+
"--bs-primary",
|
|
92
|
+
"--mui-palette-primary-main",
|
|
93
|
+
"--ant-color-primary"
|
|
94
|
+
],
|
|
95
|
+
borderRadius: [
|
|
96
|
+
"--radius",
|
|
97
|
+
"--border-radius",
|
|
98
|
+
"--rounded",
|
|
99
|
+
"--bs-border-radius"
|
|
100
|
+
]
|
|
101
|
+
}, d = new Set([
|
|
102
|
+
"",
|
|
103
|
+
"transparent",
|
|
104
|
+
"inherit",
|
|
105
|
+
"initial",
|
|
106
|
+
"unset",
|
|
107
|
+
"none",
|
|
108
|
+
"rgba(0, 0, 0, 0)",
|
|
109
|
+
"rgb(0, 0, 0)",
|
|
110
|
+
"depends on user agent"
|
|
111
|
+
]);
|
|
112
|
+
function f(e) {
|
|
113
|
+
return !!e && !d.has(e.trim().toLowerCase());
|
|
114
|
+
}
|
|
115
|
+
function p(e = document) {
|
|
116
|
+
let t = e.defaultView ?? globalThis, n = t.getComputedStyle(e.documentElement), r = e.body ? t.getComputedStyle(e.body) : null, i = {};
|
|
117
|
+
r && (f(r.fontFamily) && (i.fontFamily = r.fontFamily), f(r.backgroundColor) && (i.backgroundColor = r.backgroundColor), f(r.color) && (i.foregroundColor = r.color));
|
|
118
|
+
for (let [e, t] of Object.entries(u)) for (let r of t) {
|
|
119
|
+
let t = n.getPropertyValue(r).trim();
|
|
120
|
+
if (f(t)) {
|
|
121
|
+
i[e] = t;
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return i;
|
|
126
|
+
}
|
|
127
|
+
//#endregion
|
|
128
|
+
//#region src/themeStyles.ts
|
|
129
|
+
function m(e, t = {}) {
|
|
130
|
+
let n = {
|
|
131
|
+
...p(t.doc ?? document),
|
|
132
|
+
...e?.tokens
|
|
133
|
+
}, r = {};
|
|
134
|
+
return Object.keys(n).length > 0 && (r.tokens = n), e?.elements && Object.keys(e.elements).length > 0 && (r.elements = e.elements), r;
|
|
135
|
+
}
|
|
136
|
+
//#endregion
|
|
137
|
+
//#region src/paraglide/runtime.js
|
|
138
|
+
var h = {}, g = ["en", "ar"], _ = ["baseLocale"], v = [], y, b;
|
|
139
|
+
function x(e) {
|
|
140
|
+
if (v.length === 0) return;
|
|
141
|
+
let t = typeof e == "string" ? e : e.href;
|
|
142
|
+
if (y === t) return b;
|
|
143
|
+
let n = new URL(t, "http://dummy.com"), r;
|
|
144
|
+
for (let e of v) if (new h(e.match, n.href).exec(n.href)) {
|
|
145
|
+
r = e;
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
return y = t, b = r, r;
|
|
149
|
+
}
|
|
150
|
+
function S(e) {
|
|
151
|
+
let t = x(e);
|
|
152
|
+
return t && t.exclude !== !0 && Array.isArray(t.strategy) ? t.strategy : _;
|
|
153
|
+
}
|
|
154
|
+
var C = void 0;
|
|
155
|
+
globalThis.__paraglide = globalThis.__paraglide ?? {}, globalThis.__paraglide.ssr = globalThis.__paraglide.ssr ?? {};
|
|
156
|
+
var w = !1, T = () => {
|
|
157
|
+
if (C) {
|
|
158
|
+
let e = C?.getStore()?.locale;
|
|
159
|
+
if (e) return e;
|
|
160
|
+
}
|
|
161
|
+
let e = _;
|
|
162
|
+
typeof window < "u" && window.location?.href && (e = S(window.location.href));
|
|
163
|
+
let t = E(e, typeof window < "u" ? window.location?.href : void 0);
|
|
164
|
+
if (t) return w || (w = !0, k(t, { reload: !1 })), t;
|
|
165
|
+
throw Error("No locale found. Read the docs https://inlang.com/m/gerre34r/library-inlang-paraglideJs/errors#no-locale-found");
|
|
166
|
+
};
|
|
167
|
+
function E(e, t) {
|
|
168
|
+
let n;
|
|
169
|
+
for (let t of e) {
|
|
170
|
+
if (t === "baseLocale") n = "en";
|
|
171
|
+
else if (N(t) && M.has(t)) {
|
|
172
|
+
let e = M.get(t);
|
|
173
|
+
if (e) {
|
|
174
|
+
let t = e.getLocale();
|
|
175
|
+
if (t instanceof Promise) continue;
|
|
176
|
+
if (t !== void 0) return j(t);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
let e = A(n);
|
|
180
|
+
if (e) return e;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
var D = (e) => {
|
|
184
|
+
T = e;
|
|
185
|
+
}, O = (e) => {
|
|
186
|
+
e ? window.location.href = e : window.location.reload();
|
|
187
|
+
}, k = (e, t) => {
|
|
188
|
+
let n = {
|
|
189
|
+
reload: !0,
|
|
190
|
+
...t
|
|
191
|
+
}, r;
|
|
192
|
+
try {
|
|
193
|
+
r = T();
|
|
194
|
+
} catch {}
|
|
195
|
+
let i = [], a = _;
|
|
196
|
+
typeof window < "u" && window.location?.href && (a = S(window.location.href));
|
|
197
|
+
for (let t of a) if (t === "baseLocale") continue;
|
|
198
|
+
else if (N(t) && M.has(t)) {
|
|
199
|
+
let n = M.get(t);
|
|
200
|
+
if (n) {
|
|
201
|
+
let r = n.setLocale(e);
|
|
202
|
+
r instanceof Promise && (r = r.catch((e) => {
|
|
203
|
+
throw Error(`Custom strategy "${t}" setLocale failed.`, { cause: e });
|
|
204
|
+
}), i.push(r));
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
let o = () => {
|
|
208
|
+
n.reload && window.location && e !== r && O(void 0);
|
|
209
|
+
};
|
|
210
|
+
if (i.length) return Promise.all(i).then(() => {
|
|
211
|
+
o();
|
|
212
|
+
});
|
|
213
|
+
o();
|
|
214
|
+
};
|
|
215
|
+
function A(e) {
|
|
216
|
+
if (typeof e != "string") return;
|
|
217
|
+
let t = e.toLowerCase();
|
|
218
|
+
for (let e of g) if (e.toLowerCase() === t) return e;
|
|
219
|
+
}
|
|
220
|
+
function j(e) {
|
|
221
|
+
let t = A(e);
|
|
222
|
+
if (t) return t;
|
|
223
|
+
throw Error(`Invalid locale: ${e}. Expected one of: ${g.join(", ")}`);
|
|
224
|
+
}
|
|
225
|
+
var M = /* @__PURE__ */ new Map();
|
|
226
|
+
function N(e) {
|
|
227
|
+
return typeof e == "string" && /^custom-[A-Za-z0-9_-]+$/.test(e);
|
|
228
|
+
}
|
|
229
|
+
//#endregion
|
|
230
|
+
//#region src/paraglide/messages/dialog_end_title.js
|
|
231
|
+
var P = () => "End Chat", F = () => "إنهاء المحادثة", I = ((e = {}, t = {}) => (t.locale ?? T()) === "en" ? P(e) : F(e)), L = () => "Are you sure you want to end this chat?", R = () => "هل أنت متأكد أنك تريد إنهاء هذه المحادثة؟", z = ((e = {}, t = {}) => (t.locale ?? T()) === "en" ? L(e) : R(e)), B = () => "End Chat", V = () => "إنهاء المحادثة", H = ((e = {}, t = {}) => (t.locale ?? T()) === "en" ? B(e) : V(e)), U = () => "Cancel", W = () => "إلغاء", G = ((e = {}, t = {}) => (t.locale ?? T()) === "en" ? U(e) : W(e)), re = () => "Leave Chat", K = () => "مغادرة المحادثة", q = ((e = {}, t = {}) => (t.locale ?? T()) === "en" ? re(e) : K(e)), J = () => "If you leave now, your chat will end. Are you sure you want to leave?", Y = () => "إذا غادرت الآن، ستنتهي محادثتك. هل أنت متأكد أنك تريد المغادرة؟", X = ((e = {}, t = {}) => (t.locale ?? T()) === "en" ? J(e) : Y(e)), ie = () => "Leave", ae = () => "مغادرة", oe = ((e = {}, t = {}) => (t.locale ?? T()) === "en" ? ie(e) : ae(e)), se = () => "Stay", ce = () => "البقاء", le = ((e = {}, t = {}) => (t.locale ?? T()) === "en" ? se(e) : ce(e)), ue = () => "Close", de = () => "إغلاق", fe = ((e = {}, t = {}) => (t.locale ?? T()) === "en" ? ue(e) : de(e)), pe = new Set(g), Z = "en";
|
|
232
|
+
D(() => Z);
|
|
233
|
+
function me() {
|
|
234
|
+
return Z;
|
|
235
|
+
}
|
|
236
|
+
function Q(e) {
|
|
237
|
+
Z = e && pe.has(e) ? e : "en";
|
|
238
|
+
}
|
|
239
|
+
function he(e) {
|
|
240
|
+
return e === "ar" ? "rtl" : "ltr";
|
|
241
|
+
}
|
|
242
|
+
//#endregion
|
|
243
|
+
//#region src/ZiilaWidget.ts
|
|
244
|
+
function ge(e) {
|
|
245
|
+
let t = he(me()), n = fe();
|
|
246
|
+
return e === "endChat" ? {
|
|
247
|
+
title: I(),
|
|
248
|
+
message: z(),
|
|
249
|
+
confirmLabel: H(),
|
|
250
|
+
cancelLabel: G(),
|
|
251
|
+
closeLabel: n,
|
|
252
|
+
dir: t
|
|
253
|
+
} : {
|
|
254
|
+
title: q(),
|
|
255
|
+
message: X(),
|
|
256
|
+
confirmLabel: oe(),
|
|
257
|
+
cancelLabel: le(),
|
|
258
|
+
closeLabel: n,
|
|
259
|
+
dir: t
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
var $ = class {
|
|
263
|
+
get isDirty() {
|
|
264
|
+
return this._isDirty;
|
|
265
|
+
}
|
|
266
|
+
constructor(e) {
|
|
267
|
+
this.bridge = null, this.bus = new t(), this.confirmDialog = new l(), this._isDirty = !1, this.handleBeforeUnload = (e) => {
|
|
268
|
+
this._isDirty && (e.preventDefault(), e.returnValue = "");
|
|
269
|
+
}, this.handleMessage = (e) => {
|
|
270
|
+
let t = e.data;
|
|
271
|
+
t.type === "READY" ? this.bus.emit("ready") : t.type === "EVENT" ? this.bus.emit(t.event, t.payload) : t.type === "DIRTY_STATE" ? this._isDirty = t.isDirty : t.type === "CONFIRM_REQUEST" && this.handleConfirmRequest(t.kind);
|
|
272
|
+
}, this.config = e, Q(e.locale);
|
|
273
|
+
}
|
|
274
|
+
mount() {
|
|
275
|
+
if (this.bridge) return;
|
|
276
|
+
let t = typeof this.config.container == "string" ? document.querySelector(this.config.container) : this.config.container;
|
|
277
|
+
if (!t) throw Error(`Ziila: container not found: ${this.config.container}`);
|
|
278
|
+
this.bridge = new e((e) => {
|
|
279
|
+
e.onmessage = this.handleMessage;
|
|
280
|
+
let { container: t, ...n } = this.config, r = m(this.config.theme);
|
|
281
|
+
this.bridge?.send({
|
|
282
|
+
type: "INIT",
|
|
283
|
+
payload: {
|
|
284
|
+
...n,
|
|
285
|
+
theme: r
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
}, this.config.widgetUrl), this.bridge.mount(t), window.addEventListener("beforeunload", this.handleBeforeUnload);
|
|
289
|
+
}
|
|
290
|
+
handleConfirmRequest(e) {
|
|
291
|
+
this.confirmDialog.show(ge(e)).then((t) => {
|
|
292
|
+
this.bridge?.send({
|
|
293
|
+
type: "CONFIRM_RESULT",
|
|
294
|
+
kind: e,
|
|
295
|
+
confirmed: t
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
on(e, t) {
|
|
300
|
+
return this.bus.on(e, t);
|
|
301
|
+
}
|
|
302
|
+
off(e, t) {
|
|
303
|
+
this.bus.off(e, t);
|
|
304
|
+
}
|
|
305
|
+
emit(e, t) {
|
|
306
|
+
this.bridge?.send({
|
|
307
|
+
type: "COMMAND",
|
|
308
|
+
command: e,
|
|
309
|
+
payload: t
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
update(e) {
|
|
313
|
+
this.config = {
|
|
314
|
+
...this.config,
|
|
315
|
+
...e
|
|
316
|
+
}, e.locale !== void 0 && Q(e.locale), this.bridge?.send({
|
|
317
|
+
type: "UPDATE",
|
|
318
|
+
payload: e
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
destroy() {
|
|
322
|
+
window.removeEventListener("beforeunload", this.handleBeforeUnload), this.confirmDialog.close(), this.bridge?.destroy(), this.bus.destroy(), this.bridge = null;
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
//#endregion
|
|
326
|
+
//#region src/utils.ts
|
|
327
|
+
function _e(e) {
|
|
328
|
+
return typeof e == "object" && !!e && typeof e.code == "string" && typeof e.message == "string";
|
|
329
|
+
}
|
|
330
|
+
//#endregion
|
|
331
|
+
export { $ as ZiilaWidget, _e as isZiilaError };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface ConfirmDialogOptions {
|
|
2
|
+
title: string;
|
|
3
|
+
message: string;
|
|
4
|
+
confirmLabel: string;
|
|
5
|
+
cancelLabel: string;
|
|
6
|
+
closeLabel: string;
|
|
7
|
+
dir: 'ltr' | 'rtl';
|
|
8
|
+
}
|
|
9
|
+
export declare class ConfirmDialog {
|
|
10
|
+
private overlay;
|
|
11
|
+
private pendingResolve;
|
|
12
|
+
private previousFocus;
|
|
13
|
+
show(options: ConfirmDialogOptions): Promise<boolean>;
|
|
14
|
+
close(): void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type EventHandler<T = unknown> = (data: T) => void;
|
|
2
|
+
export declare class EventBus {
|
|
3
|
+
private readonly handlers;
|
|
4
|
+
on<T = unknown>(event: string, handler: EventHandler<T>): () => void;
|
|
5
|
+
off<T = unknown>(event: string, handler: EventHandler<T>): void;
|
|
6
|
+
emit(event: string, data?: unknown): void;
|
|
7
|
+
destroy(): void;
|
|
8
|
+
}
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ParentMessage } from './types';
|
|
2
|
+
export declare const WIDGET_URL: string;
|
|
3
|
+
export declare class MessageBridge {
|
|
4
|
+
private iframe;
|
|
5
|
+
private port;
|
|
6
|
+
private readonly onPortReady;
|
|
7
|
+
private readonly url;
|
|
8
|
+
constructor(onPortReady: (port: MessagePort) => void, widgetUrl?: string);
|
|
9
|
+
mount(container: HTMLElement): void;
|
|
10
|
+
private handleHandshake;
|
|
11
|
+
send(message: ParentMessage): void;
|
|
12
|
+
destroy(): void;
|
|
13
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ZiilaConfig, ZiilaEvent, ZiilaCommand, EventPayloadMap, CommandPayloadMap } from './types';
|
|
2
|
+
export declare class ZiilaWidget {
|
|
3
|
+
private config;
|
|
4
|
+
private bridge;
|
|
5
|
+
private readonly bus;
|
|
6
|
+
private readonly confirmDialog;
|
|
7
|
+
private _isDirty;
|
|
8
|
+
get isDirty(): boolean;
|
|
9
|
+
constructor(config: ZiilaConfig);
|
|
10
|
+
mount(): void;
|
|
11
|
+
private handleBeforeUnload;
|
|
12
|
+
private handleMessage;
|
|
13
|
+
private handleConfirmRequest;
|
|
14
|
+
on<K extends ZiilaEvent>(event: K, handler: (data: EventPayloadMap[K]) => void): () => void;
|
|
15
|
+
off<K extends ZiilaEvent>(event: K, handler: (data: EventPayloadMap[K]) => void): void;
|
|
16
|
+
emit<K extends ZiilaCommand>(command: K, payload?: CommandPayloadMap[K]): void;
|
|
17
|
+
update(updates: Partial<Pick<ZiilaConfig, 'locale'>>): void;
|
|
18
|
+
destroy(): void;
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { locales } from './paraglide/runtime';
|
|
2
|
+
export type Locale = (typeof locales)[number];
|
|
3
|
+
export declare function getSdkLocale(): Locale;
|
|
4
|
+
export declare function setSdkLocale(value: string | undefined | null): void;
|
|
5
|
+
export declare function directionForLocale(locale: Locale): 'ltr' | 'rtl';
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
export interface ThemeTokens {
|
|
2
|
+
primaryColor?: string;
|
|
3
|
+
primaryFg?: string;
|
|
4
|
+
backgroundColor?: string;
|
|
5
|
+
foregroundColor?: string;
|
|
6
|
+
borderColor?: string;
|
|
7
|
+
borderRadius?: string;
|
|
8
|
+
fontFamily?: string;
|
|
9
|
+
mutedColor?: string;
|
|
10
|
+
mutedFg?: string;
|
|
11
|
+
}
|
|
12
|
+
export type ThemeElement = 'button' | 'img' | 'input' | 'p';
|
|
13
|
+
export type StyleDecls = Record<string, string>;
|
|
14
|
+
export interface StyleStates {
|
|
15
|
+
base: StyleDecls;
|
|
16
|
+
hover?: StyleDecls;
|
|
17
|
+
focus?: StyleDecls;
|
|
18
|
+
active?: StyleDecls;
|
|
19
|
+
disabled?: StyleDecls;
|
|
20
|
+
}
|
|
21
|
+
export interface ElementStyle {
|
|
22
|
+
variants: {
|
|
23
|
+
default: StyleStates;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export interface ThemeStyles {
|
|
27
|
+
tokens?: Partial<ThemeTokens>;
|
|
28
|
+
elements?: Partial<Record<ThemeElement, ElementStyle>>;
|
|
29
|
+
}
|
|
30
|
+
export interface ZiilaConfig {
|
|
31
|
+
apiKey: string;
|
|
32
|
+
container: string | HTMLElement;
|
|
33
|
+
locale?: string;
|
|
34
|
+
widgetTitle?: string;
|
|
35
|
+
theme?: Partial<ThemeStyles>;
|
|
36
|
+
widgetUrl?: string;
|
|
37
|
+
}
|
|
38
|
+
export interface AddToCartPayload {
|
|
39
|
+
sku: string;
|
|
40
|
+
quantity: number;
|
|
41
|
+
}
|
|
42
|
+
export interface ProductViewedPayload {
|
|
43
|
+
sku: string;
|
|
44
|
+
}
|
|
45
|
+
export interface SearchPayload {
|
|
46
|
+
query: string;
|
|
47
|
+
}
|
|
48
|
+
export interface ErrorPayload {
|
|
49
|
+
code: string;
|
|
50
|
+
message: string;
|
|
51
|
+
}
|
|
52
|
+
export type ZiilaEvent = 'ready' | 'addToCart' | 'productViewed' | 'search' | 'error';
|
|
53
|
+
export type ZiilaCommand = 'clear';
|
|
54
|
+
export interface EventPayloadMap {
|
|
55
|
+
ready: undefined;
|
|
56
|
+
addToCart: AddToCartPayload;
|
|
57
|
+
productViewed: ProductViewedPayload;
|
|
58
|
+
search: SearchPayload;
|
|
59
|
+
error: ErrorPayload;
|
|
60
|
+
}
|
|
61
|
+
export interface CommandPayloadMap {
|
|
62
|
+
clear: undefined;
|
|
63
|
+
}
|
|
64
|
+
export type ConfirmKind = 'endChat' | 'leaveChat';
|
|
65
|
+
export type HandshakeMessage = {
|
|
66
|
+
type: 'HANDSHAKE';
|
|
67
|
+
} | {
|
|
68
|
+
type: 'HANDSHAKE_ACK';
|
|
69
|
+
};
|
|
70
|
+
export type WidgetInitConfig = Omit<ZiilaConfig, 'container'>;
|
|
71
|
+
export type ParentMessage = {
|
|
72
|
+
type: 'INIT';
|
|
73
|
+
payload: WidgetInitConfig;
|
|
74
|
+
} | {
|
|
75
|
+
type: 'COMMAND';
|
|
76
|
+
command: ZiilaCommand;
|
|
77
|
+
payload?: unknown;
|
|
78
|
+
} | {
|
|
79
|
+
type: 'UPDATE';
|
|
80
|
+
payload: Partial<Pick<ZiilaConfig, 'locale'>>;
|
|
81
|
+
} | {
|
|
82
|
+
type: 'THEME_UPDATE';
|
|
83
|
+
payload: Partial<ThemeTokens>;
|
|
84
|
+
} | {
|
|
85
|
+
type: 'CONFIRM_RESULT';
|
|
86
|
+
kind: ConfirmKind;
|
|
87
|
+
confirmed: boolean;
|
|
88
|
+
};
|
|
89
|
+
export type WidgetMessage = {
|
|
90
|
+
type: 'READY';
|
|
91
|
+
} | {
|
|
92
|
+
type: 'EVENT';
|
|
93
|
+
event: ZiilaEvent;
|
|
94
|
+
payload?: unknown;
|
|
95
|
+
} | {
|
|
96
|
+
type: 'DIRTY_STATE';
|
|
97
|
+
isDirty: boolean;
|
|
98
|
+
} | {
|
|
99
|
+
type: 'CONFIRM_REQUEST';
|
|
100
|
+
kind: ConfirmKind;
|
|
101
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(){var e=class{constructor(e,t){this.iframe=null,this.port=null,this.handleHandshake=e=>{let t=this.iframe?.contentWindow;if(!t||e.source!==t||e.origin!==new URL(this.url).origin)return;let n=e.data;if(typeof n!=`object`||!n||n.type!==`HANDSHAKE`)return;let r=new MessageChannel;t.postMessage({type:`HANDSHAKE_ACK`},new URL(this.url).origin,[r.port2]),this.port=r.port1,this.port.start(),window.removeEventListener(`message`,this.handleHandshake),this.onPortReady(r.port1)},this.onPortReady=e,this.url=t??`https://widget.ziila.com`}mount(e){this.iframe=document.createElement(`iframe`),this.iframe.src=this.url,this.iframe.title=`Ziila Shopping Assistant`,this.iframe.style.cssText=`width:100%;height:100%;border:none;`,e.appendChild(this.iframe),window.addEventListener(`message`,this.handleHandshake)}send(e){this.port?.postMessage(e)}destroy(){window.removeEventListener(`message`,this.handleHandshake),this.port?.close(),this.iframe?.remove(),this.iframe=null,this.port=null}},t=class{constructor(){this.handlers=new Map}on(e,t){return this.handlers.has(e)||this.handlers.set(e,new Set),this.handlers.get(e).add(t),()=>this.off(e,t)}off(e,t){this.handlers.get(e)?.delete(t)}emit(e,t){this.handlers.get(e)?.forEach(e=>e(t))}destroy(){this.handlers.clear()}},n=`position:fixed;inset:0;z-index:2147483647;display:flex;align-items:center;justify-content:center;background:rgba(0,0,0,0.4);padding:24px;font-family:system-ui,-apple-system,sans-serif;`,r=`width:100%;max-width:24rem;border-radius:12px;background:#fff;padding:16px;box-shadow:2px 4px 2px rgba(0,0,0,0.1);display:flex;flex-direction:column;gap:16px;`,i=`display:flex;justify-content:flex-end;`,a=`border:none;background:#f0f0f0;border-radius:12px;width:32px;height:32px;display:flex;align-items:center;justify-content:center;cursor:pointer;font-size:14px;color:#555;line-height:1;padding:0;`,o=`display:flex;flex-direction:column;gap:16px;align-items:center;`,ee=`margin:0;font-size:24px;font-weight:700;color:#195f6b;text-align:center;`,s=`margin:0;font-size:16px;color:#2d2d2d;font-weight:500;line-height:27px;text-align:center;max-width:345px;`,c=`display:flex;gap:16px;padding:0 16px;width:100%;box-sizing:border-box;`,te=`flex:1;border:1px solid #195f6b;border-radius:8px;background:#fff;color:#195f6b;padding:16px;font-size:16px;font-weight:500;cursor:pointer;`,ne=`flex:1;border:none;border-radius:8px;background:#cc0000;color:#fff;padding:16px;font-size:16px;font-weight:500;cursor:pointer;`,l=class{constructor(){this.overlay=null,this.pendingResolve=null,this.previousFocus=null}show(e){return this.pendingResolve?.(!1),this.pendingResolve=null,this.close(),new Promise(t=>{this.pendingResolve=t;let l=e=>{this.pendingResolve=null,this.close(),t(e)},u=document.createElement(`div`);u.style.cssText=n;let d=document.createElement(`div`);d.setAttribute(`role`,`dialog`),d.setAttribute(`aria-modal`,`true`),d.setAttribute(`aria-labelledby`,`ziila-confirm-title`),d.style.cssText=r,d.dir=e.dir;let f=document.createElement(`div`);f.style.cssText=i;let p=document.createElement(`button`);p.type=`button`,p.setAttribute(`aria-label`,e.closeLabel),p.textContent=`✕`,p.style.cssText=a,p.addEventListener(`click`,()=>l(!1)),f.appendChild(p);let m=document.createElement(`div`);m.style.cssText=o;let h=document.createElement(`h2`);h.id=`ziila-confirm-title`,h.textContent=e.title,h.style.cssText=ee;let g=document.createElement(`p`);g.textContent=e.message,g.style.cssText=s;let _=document.createElement(`button`);_.type=`button`,_.textContent=e.cancelLabel,_.style.cssText=te,_.addEventListener(`click`,()=>l(!1));let v=document.createElement(`button`);v.type=`button`,v.textContent=e.confirmLabel,v.style.cssText=ne,v.addEventListener(`click`,()=>l(!0));let y=document.createElement(`div`);y.style.cssText=c,y.append(_,v),m.append(h,g,y),d.append(f,m),u.appendChild(d);let b=[p,_,v];u.addEventListener(`keydown`,e=>{if(e.key===`Escape`){l(!1);return}e.key===`Tab`&&(e.preventDefault(),b[(b.indexOf(document.activeElement)+(e.shiftKey?-1:1)+b.length)%b.length].focus())}),this.previousFocus=document.activeElement,this.overlay=u,document.body.appendChild(u),_.focus()})}close(){this.overlay?.remove(),this.overlay=null,this.previousFocus?.focus(),this.previousFocus=null}},u={primaryColor:[`--primary`,`--primary-color`,`--color-primary`,`--brand`,`--brand-color`,`--accent-color`,`--bs-primary`,`--mui-palette-primary-main`,`--ant-color-primary`],borderRadius:[`--radius`,`--border-radius`,`--rounded`,`--bs-border-radius`]},d=new Set([``,`transparent`,`inherit`,`initial`,`unset`,`none`,`rgba(0, 0, 0, 0)`,`rgb(0, 0, 0)`,`depends on user agent`]);function f(e){return!!e&&!d.has(e.trim().toLowerCase())}function p(e=document){let t=e.defaultView??globalThis,n=t.getComputedStyle(e.documentElement),r=e.body?t.getComputedStyle(e.body):null,i={};r&&(f(r.fontFamily)&&(i.fontFamily=r.fontFamily),f(r.backgroundColor)&&(i.backgroundColor=r.backgroundColor),f(r.color)&&(i.foregroundColor=r.color));for(let[e,t]of Object.entries(u))for(let r of t){let t=n.getPropertyValue(r).trim();if(f(t)){i[e]=t;break}}return i}function m(e,t={}){let n={...p(t.doc??document),...e?.tokens},r={};return Object.keys(n).length>0&&(r.tokens=n),e?.elements&&Object.keys(e.elements).length>0&&(r.elements=e.elements),r}var h={},g=[`en`,`ar`],_=[`baseLocale`],v=[],y,b;function x(e){if(v.length===0)return;let t=typeof e==`string`?e:e.href;if(y===t)return b;let n=new URL(t,`http://dummy.com`),r;for(let e of v)if(new h(e.match,n.href).exec(n.href)){r=e;break}return y=t,b=r,r}function S(e){let t=x(e);return t&&t.exclude!==!0&&Array.isArray(t.strategy)?t.strategy:_}var C=void 0;globalThis.__paraglide=globalThis.__paraglide??{},globalThis.__paraglide.ssr=globalThis.__paraglide.ssr??{};var w=!1,T=()=>{if(C){let e=C?.getStore()?.locale;if(e)return e}let e=_;typeof window<`u`&&window.location?.href&&(e=S(window.location.href));let t=re(e,typeof window<`u`?window.location?.href:void 0);if(t)return w||(w=!0,E(t,{reload:!1})),t;throw Error(`No locale found. Read the docs https://inlang.com/m/gerre34r/library-inlang-paraglideJs/errors#no-locale-found`)};function re(e,t){let n;for(let t of e){if(t===`baseLocale`)n=`en`;else if(A(t)&&k.has(t)){let e=k.get(t);if(e){let t=e.getLocale();if(t instanceof Promise)continue;if(t!==void 0)return O(t)}}let e=D(n);if(e)return e}}var ie=e=>{T=e},ae=e=>{e?window.location.href=e:window.location.reload()},E=(e,t)=>{let n={reload:!0,...t},r;try{r=T()}catch{}let i=[],a=_;typeof window<`u`&&window.location?.href&&(a=S(window.location.href));for(let t of a)if(t===`baseLocale`)continue;else if(A(t)&&k.has(t)){let n=k.get(t);if(n){let r=n.setLocale(e);r instanceof Promise&&(r=r.catch(e=>{throw Error(`Custom strategy "${t}" setLocale failed.`,{cause:e})}),i.push(r))}}let o=()=>{n.reload&&window.location&&e!==r&&ae(void 0)};if(i.length)return Promise.all(i).then(()=>{o()});o()};function D(e){if(typeof e!=`string`)return;let t=e.toLowerCase();for(let e of g)if(e.toLowerCase()===t)return e}function O(e){let t=D(e);if(t)return t;throw Error(`Invalid locale: ${e}. Expected one of: ${g.join(`, `)}`)}var k=new Map;function A(e){return typeof e==`string`&&/^custom-[A-Za-z0-9_-]+$/.test(e)}var j=()=>`End Chat`,M=()=>`إنهاء المحادثة`,N=((e={},t={})=>(t.locale??T())===`en`?j(e):M(e)),P=()=>`Are you sure you want to end this chat?`,F=()=>`هل أنت متأكد أنك تريد إنهاء هذه المحادثة؟`,I=((e={},t={})=>(t.locale??T())===`en`?P(e):F(e)),L=()=>`End Chat`,R=()=>`إنهاء المحادثة`,z=((e={},t={})=>(t.locale??T())===`en`?L(e):R(e)),B=()=>`Cancel`,V=()=>`إلغاء`,H=((e={},t={})=>(t.locale??T())===`en`?B(e):V(e)),U=()=>`Leave Chat`,W=()=>`مغادرة المحادثة`,G=((e={},t={})=>(t.locale??T())===`en`?U(e):W(e)),oe=()=>`If you leave now, your chat will end. Are you sure you want to leave?`,K=()=>`إذا غادرت الآن، ستنتهي محادثتك. هل أنت متأكد أنك تريد المغادرة؟`,q=((e={},t={})=>(t.locale??T())===`en`?oe(e):K(e)),se=()=>`Leave`,ce=()=>`مغادرة`,le=((e={},t={})=>(t.locale??T())===`en`?se(e):ce(e)),ue=()=>`Stay`,de=()=>`البقاء`,fe=((e={},t={})=>(t.locale??T())===`en`?ue(e):de(e)),pe=()=>`Close`,me=()=>`إغلاق`,he=((e={},t={})=>(t.locale??T())===`en`?pe(e):me(e)),ge=new Set(g),J=`en`;ie(()=>J);function _e(){return J}function Y(e){J=e&&ge.has(e)?e:`en`}function ve(e){return e===`ar`?`rtl`:`ltr`}function ye(e){let t=ve(_e()),n=he();return e===`endChat`?{title:N(),message:I(),confirmLabel:z(),cancelLabel:H(),closeLabel:n,dir:t}:{title:G(),message:q(),confirmLabel:le(),cancelLabel:fe(),closeLabel:n,dir:t}}var X=class{get isDirty(){return this._isDirty}constructor(e){this.bridge=null,this.bus=new t,this.confirmDialog=new l,this._isDirty=!1,this.handleBeforeUnload=e=>{this._isDirty&&(e.preventDefault(),e.returnValue=``)},this.handleMessage=e=>{let t=e.data;t.type===`READY`?this.bus.emit(`ready`):t.type===`EVENT`?this.bus.emit(t.event,t.payload):t.type===`DIRTY_STATE`?this._isDirty=t.isDirty:t.type===`CONFIRM_REQUEST`&&this.handleConfirmRequest(t.kind)},this.config=e,Y(e.locale)}mount(){if(this.bridge)return;let t=typeof this.config.container==`string`?document.querySelector(this.config.container):this.config.container;if(!t)throw Error(`Ziila: container not found: ${this.config.container}`);this.bridge=new e(e=>{e.onmessage=this.handleMessage;let{container:t,...n}=this.config,r=m(this.config.theme);this.bridge?.send({type:`INIT`,payload:{...n,theme:r}})},this.config.widgetUrl),this.bridge.mount(t),window.addEventListener(`beforeunload`,this.handleBeforeUnload)}handleConfirmRequest(e){this.confirmDialog.show(ye(e)).then(t=>{this.bridge?.send({type:`CONFIRM_RESULT`,kind:e,confirmed:t})})}on(e,t){return this.bus.on(e,t)}off(e,t){this.bus.off(e,t)}emit(e,t){this.bridge?.send({type:`COMMAND`,command:e,payload:t})}update(e){this.config={...this.config,...e},e.locale!==void 0&&Y(e.locale),this.bridge?.send({type:`UPDATE`,payload:e})}destroy(){window.removeEventListener(`beforeunload`,this.handleBeforeUnload),this.confirmDialog.close(),this.bridge?.destroy(),this.bus.destroy(),this.bridge=null}},Z=null,be=window.Ziila?.q??[];function Q(e){Z?.destroy(),Z=new X(e),Z.mount()}var $=function(e){Q(e)};$.on=function(e,t){if(!Z)throw Error(`Ziila: call Ziila(config) before Ziila.on()`);return Z.on(e,t)},$.off=function(e,t){Z?.off(e,t)},$.emit=function(e,t){Z?.emit(e,t)},$.update=function(e){Z?.update(e)},$.mount=function(){Z?.mount()},$.destroy=function(){Z?.destroy(),Z=null},Object.defineProperty($,"isDirty",{get:()=>Z?.isDirty??!1,enumerable:!0,configurable:!1}),window.Ziila=$,be.forEach(Q)})();
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ziila/sdk",
|
|
3
|
+
"version": "0.1.7",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Ziila Shopping Assistant SDK — embeddable widget loader and host-page bridge.",
|
|
6
|
+
"license": "UNLICENSED",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/intellaworld/ziila-shopping-assistant.git",
|
|
10
|
+
"directory": "packages/sdk"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/entries/index.d.ts",
|
|
16
|
+
"module": "./dist/index.esm.js",
|
|
17
|
+
"import": "./dist/index.esm.js",
|
|
18
|
+
"require": "./dist/index.cjs.js",
|
|
19
|
+
"default": "./dist/index.esm.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "vite build --config vite.lib.config.ts && vite build --config vite.embed.config.ts",
|
|
27
|
+
"dev": "vite build --config vite.lib.config.ts --watch --mode development",
|
|
28
|
+
"test": "vitest run",
|
|
29
|
+
"test:watch": "vitest",
|
|
30
|
+
"typecheck": "tsc --noEmit",
|
|
31
|
+
"compile:i18n": "paraglide-js compile --project ../../project.inlang --outdir ./src/paraglide"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@inlang/paraglide-js": "^2.18.2",
|
|
35
|
+
"@types/node": "^24.0.0",
|
|
36
|
+
"jsdom": "^29.0.0",
|
|
37
|
+
"typescript": "^6.0.0",
|
|
38
|
+
"vite": "^8.0.0",
|
|
39
|
+
"vite-plugin-dts": "^5.0.0",
|
|
40
|
+
"vitest": "^4.0.0"
|
|
41
|
+
}
|
|
42
|
+
}
|