@machinemetrics/mm-react-embeddable 1.1.1-0 → 1.1.2-0
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const F=require("react/jsx-runtime"),e=require("react"),R=require("prop-types"),L=require("styled-components"),S=e.createContext(null),P=({children:n})=>{const[i,d]=e.useState({}),[a,l]=e.useState({}),c=e.useRef({}),m=e.useRef({}),u=e.useRef({}),h=e.useCallback((r,s)=>!!u.current[r]?.has(s),[]),w=e.useCallback((r,s,t)=>{d(o=>({...o,[r]:{origin:s,frameRef:t}}))},[]),f=e.useCallback(r=>{d(s=>{const t={...s};return delete t[r],t}),l(s=>{const t={...s};return delete t[r],t}),delete u.current[r]},[]);e.useEffect(()=>{const r=(t=[],o)=>{t.forEach(g=>g(o))},s=t=>{if(!t?.data?.instanceId)return;const{instanceId:o,type:g}=t.data,y=c.current;if(g==="response"){const{callbackId:E,response:Z,error:I}=t.data,v=m.current[E];v&&(v(Z,I),delete m.current[E]);return}if(g==="mark-registered"){const{requestType:E}=t.data;u.current[o]||(u.current[o]=new Set),u.current[o].add(E);return}g==="ready"&&l(E=>({...E,[o]:!0})),r(y[o]?.[g],t.data),r(y["*"]?.[g],t.data),r(y[o]?.["*"],t.data),r(y["*"]?.["*"],t.data)};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[i]);const b=e.useCallback((r,s,t)=>{const o=i[r];if(!o){console.warn(`Origin not set for id: ${r}. Unable to send message.`);return}o?.frameRef?.current?.contentWindow&&o.frameRef.current.contentWindow.postMessage({type:s,...t,instanceId:r},o.origin)},[i]),C=e.useRef(0),q=e.useCallback(()=>`${Date.now()}-${++C.current}`,[]),p=e.useCallback(async(r,s,t={})=>new Promise((o,g)=>{const y=q(),E=(Z,I)=>{delete m.current[y],I?g(I):o(Z)};m.current[y]=E,b(r,"request",{callbackId:y,requestType:s,data:t})}),[b]),k=e.useCallback((r,s)=>{b(r,"load-options",s)},[b]),x=e.useCallback((r,s)=>{b(r,"validate",s)},[b]),M=e.useMemo(()=>({registerFrame:w,unregisterFrame:f,sendMessage:b,sendRequest:p,loadOptions:k,validate:x,hasHandler:h,readyFrames:a,addListener:(r,s,t)=>(c.current[r]||(c.current[r]={}),c.current[r][s]||(c.current[r][s]=[]),c.current[r][s].push(t),()=>{c.current[r][s]=c.current[r][s].filter(o=>o!==t)})}),[w,f,b,p,k,x,h,a]);return F.jsx(S.Provider,{value:M,children:n})},$={},T=async n=>{try{const i=new URL(n).origin;if($[i]!==void 0)return $[i];const a=await(await fetch(`${i}/mm-app-manifest.json`)).json();if(!a.version)throw new Error("Invalid manifest");return $[i]=a,a}catch{}return $[origin]=!1,!1},U=async n=>!!await T(n),j=(n,i,d)=>{const a=e.useContext(S);if(a===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const l=e.useRef(d);e.useEffect(()=>{l.current=d},[d]),e.useEffect(()=>{if(!(!n||!i)){if(i.startsWith("key:")){const c=i.substring(4);return a.readyFrames[n]&&a.sendMessage(n,"subscribe-key",{keyChord:c}),a.addListener(n,`key-pressed:${c}`,(...u)=>l.current(...u))}return a.addListener(n,i,(...c)=>l.current(...c))}},[a,n,i])};P.propTypes={children:R.node.isRequired};const O=L.default||L,W=O.iframe`
|
|
2
2
|
width: 100%;
|
|
3
3
|
height: 100%;
|
|
4
4
|
border: none;
|
|
5
5
|
visibility: ${n=>n.$isReady?"visible":"hidden"};
|
|
6
|
-
`,
|
|
6
|
+
`,A=({id:n,src:i,path:d,onInit:a=()=>{},context:l={},params:c={},...m})=>{if("isConfiguring"in m)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in l)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in l)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const u=e.useRef(),[h,w]=e.useState(),[f,b]=e.useState(0),{registerFrame:C,unregisterFrame:q,sendMessage:p}=e.useContext(S),[k,x]=e.useState(!0),[M,r]=e.useState();e.useEffect(()=>{f&&p(n,"set-context",{context:{...l,colorMode:c.colorMode,isFullScreen:c.isFullScreen}})},[l,c.colorMode,c.isFullScreen,n,f]),e.useEffect(()=>{f&&(p(n,"set-params",{params:c}),p(n,"set-configuring",{isConfiguring:c.isConfiguring}))},[c,n,f]),e.useEffect(()=>{d!==void 0&&r(d?.indexOf("/")===0?d:`/${d}`)},[d]),e.useEffect(()=>{M===void 0||!f||p(n,"navigate",{path:M})},[M,f,n,p]),e.useEffect(()=>{if(!(!i||!n||!u))return(async()=>{const t=await U(i);if(x(t),t){const o=new URL(i);C(n,o.origin,u),a(),w(`${o.origin}/mm-embedded-interop?id=${n}&origin=${window.location.origin}`);return}})(),()=>{q(n)}},[i,n,C,u]),j(n,"ready",()=>b(t=>t+1));const s=e.useRef();return j(n,"heartbeat",()=>{s.current&&clearTimeout(s.current),s.current=setTimeout(()=>{if(u.current){const t=new URL(i);u.current.src=`${t.origin}/mm-embedded-interop?id=${n}&origin=${window.location.origin}`}},6e3)}),e.useEffect(()=>()=>{s.current&&clearTimeout(s.current)},[]),k?F.jsx(W,{$isReady:f,ref:u,src:h,...m}):F.jsx("div",{style:{height:"100%",display:"flex",flex:1,alignItems:"center",justifyContent:"center"},children:F.jsx("div",{style:{textAlign:"center"},children:"Application must adhere to the MM App Manifest specification to be embedded."})})};A.propTypes={id:R.string,src:R.string.isRequired,path:R.string,onInit:R.func};exports.MMEmbeddableZone=A;exports.MMEmbeddableZoneContext=S;exports.MMEmbeddableZoneProvider=P;exports.getAppInfo=T;exports.testIsEmbeddable=U;exports.useEmbeddedEffect=j;
|
|
@@ -1,201 +1,212 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { createContext as O, useState as
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
const
|
|
6
|
-
const [o,
|
|
7
|
-
|
|
8
|
-
}, []), l =
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
return delete
|
|
12
|
-
}),
|
|
13
|
-
const
|
|
14
|
-
return delete
|
|
15
|
-
}), delete
|
|
1
|
+
import { jsx as v } from "react/jsx-runtime";
|
|
2
|
+
import { createContext as O, useState as R, useRef as M, useCallback as w, useEffect as g, useMemo as W, useContext as A } from "react";
|
|
3
|
+
import $ from "prop-types";
|
|
4
|
+
import U from "styled-components";
|
|
5
|
+
const Z = O(null), H = ({ children: t }) => {
|
|
6
|
+
const [o, u] = R({}), [c, d] = R({}), i = M({}), b = M({}), a = M({}), F = w((e, n) => !!a.current[e]?.has(n), []), I = w((e, n, r) => {
|
|
7
|
+
u((s) => ({ ...s, [e]: { origin: n, frameRef: r } }));
|
|
8
|
+
}, []), l = w((e) => {
|
|
9
|
+
u((n) => {
|
|
10
|
+
const r = { ...n };
|
|
11
|
+
return delete r[e], r;
|
|
12
|
+
}), d((n) => {
|
|
13
|
+
const r = { ...n };
|
|
14
|
+
return delete r[e], r;
|
|
15
|
+
}), delete a.current[e];
|
|
16
16
|
}, []);
|
|
17
|
-
|
|
18
|
-
const e = (
|
|
19
|
-
|
|
20
|
-
},
|
|
21
|
-
if (!
|
|
17
|
+
g(() => {
|
|
18
|
+
const e = (r = [], s) => {
|
|
19
|
+
r.forEach((m) => m(s));
|
|
20
|
+
}, n = (r) => {
|
|
21
|
+
if (!r?.data?.instanceId)
|
|
22
22
|
return;
|
|
23
|
-
const { instanceId:
|
|
23
|
+
const { instanceId: s, type: m } = r.data, y = i.current;
|
|
24
24
|
if (m === "response") {
|
|
25
|
-
const { callbackId:
|
|
26
|
-
|
|
25
|
+
const { callbackId: h, response: S, error: L } = r.data, q = b.current[h];
|
|
26
|
+
q && (q(S, L), delete b.current[h]);
|
|
27
27
|
return;
|
|
28
28
|
}
|
|
29
29
|
if (m === "mark-registered") {
|
|
30
|
-
const { requestType:
|
|
31
|
-
|
|
30
|
+
const { requestType: h } = r.data;
|
|
31
|
+
a.current[s] || (a.current[s] = /* @__PURE__ */ new Set()), a.current[s].add(h);
|
|
32
32
|
return;
|
|
33
33
|
}
|
|
34
|
-
m === "ready" &&
|
|
34
|
+
m === "ready" && d((h) => ({ ...h, [s]: !0 })), e(y[s]?.[m], r.data), e(y["*"]?.[m], r.data), e(y[s]?.["*"], r.data), e(y["*"]?.["*"], r.data);
|
|
35
35
|
};
|
|
36
|
-
return window.addEventListener("message",
|
|
37
|
-
window.removeEventListener("message",
|
|
36
|
+
return window.addEventListener("message", n), () => {
|
|
37
|
+
window.removeEventListener("message", n);
|
|
38
38
|
};
|
|
39
39
|
}, [o]);
|
|
40
|
-
const f =
|
|
41
|
-
(e,
|
|
42
|
-
const
|
|
43
|
-
if (!
|
|
40
|
+
const f = w(
|
|
41
|
+
(e, n, r) => {
|
|
42
|
+
const s = o[e];
|
|
43
|
+
if (!s) {
|
|
44
44
|
console.warn(`Origin not set for id: ${e}. Unable to send message.`);
|
|
45
45
|
return;
|
|
46
46
|
}
|
|
47
|
-
|
|
48
|
-
{ type:
|
|
49
|
-
|
|
47
|
+
s?.frameRef?.current?.contentWindow && s.frameRef.current.contentWindow.postMessage(
|
|
48
|
+
{ type: n, ...r, instanceId: e },
|
|
49
|
+
s.origin
|
|
50
50
|
);
|
|
51
51
|
},
|
|
52
52
|
[o]
|
|
53
|
-
), x =
|
|
54
|
-
async (e,
|
|
55
|
-
const
|
|
56
|
-
delete
|
|
53
|
+
), x = M(0), P = w(() => `${Date.now()}-${++x.current}`, []), p = w(
|
|
54
|
+
async (e, n, r = {}) => new Promise((s, m) => {
|
|
55
|
+
const y = P(), h = (S, L) => {
|
|
56
|
+
delete b.current[y], L ? m(L) : s(S);
|
|
57
57
|
};
|
|
58
|
-
|
|
58
|
+
b.current[y] = h, f(e, "request", { callbackId: y, requestType: n, data: r });
|
|
59
59
|
}),
|
|
60
60
|
[f]
|
|
61
|
-
), C =
|
|
62
|
-
(e,
|
|
63
|
-
f(e, "load-options",
|
|
61
|
+
), C = w(
|
|
62
|
+
(e, n) => {
|
|
63
|
+
f(e, "load-options", n);
|
|
64
64
|
},
|
|
65
65
|
[f]
|
|
66
|
-
), k =
|
|
67
|
-
(e,
|
|
68
|
-
f(e, "validate",
|
|
66
|
+
), k = w(
|
|
67
|
+
(e, n) => {
|
|
68
|
+
f(e, "validate", n);
|
|
69
69
|
},
|
|
70
70
|
[f]
|
|
71
|
-
), E =
|
|
71
|
+
), E = W(
|
|
72
72
|
() => ({
|
|
73
|
-
registerFrame:
|
|
73
|
+
registerFrame: I,
|
|
74
74
|
unregisterFrame: l,
|
|
75
75
|
sendMessage: f,
|
|
76
|
-
sendRequest:
|
|
76
|
+
sendRequest: p,
|
|
77
77
|
loadOptions: C,
|
|
78
78
|
validate: k,
|
|
79
|
-
hasHandler:
|
|
80
|
-
readyFrames:
|
|
81
|
-
addListener: (e,
|
|
82
|
-
|
|
79
|
+
hasHandler: F,
|
|
80
|
+
readyFrames: c,
|
|
81
|
+
addListener: (e, n, r) => (i.current[e] || (i.current[e] = {}), i.current[e][n] || (i.current[e][n] = []), i.current[e][n].push(r), () => {
|
|
82
|
+
i.current[e][n] = i.current[e][n].filter((s) => s !== r);
|
|
83
83
|
})
|
|
84
84
|
}),
|
|
85
85
|
[
|
|
86
|
-
|
|
86
|
+
I,
|
|
87
87
|
l,
|
|
88
88
|
f,
|
|
89
|
-
|
|
89
|
+
p,
|
|
90
90
|
C,
|
|
91
91
|
k,
|
|
92
|
-
|
|
93
|
-
|
|
92
|
+
F,
|
|
93
|
+
c
|
|
94
94
|
]
|
|
95
95
|
);
|
|
96
|
-
return /* @__PURE__ */
|
|
97
|
-
},
|
|
96
|
+
return /* @__PURE__ */ v(Z.Provider, { value: E, children: t });
|
|
97
|
+
}, T = {}, B = async (t) => {
|
|
98
98
|
try {
|
|
99
|
-
const o = new URL(
|
|
100
|
-
if (
|
|
101
|
-
return
|
|
102
|
-
const
|
|
103
|
-
if (!
|
|
99
|
+
const o = new URL(t).origin;
|
|
100
|
+
if (T[o] !== void 0)
|
|
101
|
+
return T[o];
|
|
102
|
+
const c = await (await fetch(`${o}/mm-app-manifest.json`)).json();
|
|
103
|
+
if (!c.version)
|
|
104
104
|
throw new Error("Invalid manifest");
|
|
105
|
-
return
|
|
105
|
+
return T[o] = c, c;
|
|
106
106
|
} catch {
|
|
107
107
|
}
|
|
108
|
-
return
|
|
109
|
-
},
|
|
110
|
-
const
|
|
111
|
-
if (
|
|
108
|
+
return T[origin] = !1, !1;
|
|
109
|
+
}, D = async (t) => !!await B(t), j = (t, o, u) => {
|
|
110
|
+
const c = A(Z);
|
|
111
|
+
if (c === null)
|
|
112
112
|
throw new Error(
|
|
113
113
|
"useEmbeddedEffect must be used within a MMEmbeddableZoneProvider"
|
|
114
114
|
);
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}, [
|
|
119
|
-
if (!(!
|
|
115
|
+
const d = M(u);
|
|
116
|
+
g(() => {
|
|
117
|
+
d.current = u;
|
|
118
|
+
}, [u]), g(() => {
|
|
119
|
+
if (!(!t || !o)) {
|
|
120
120
|
if (o.startsWith("key:")) {
|
|
121
|
-
const
|
|
122
|
-
return
|
|
123
|
-
|
|
124
|
-
`key-pressed:${
|
|
125
|
-
(...
|
|
121
|
+
const i = o.substring(4);
|
|
122
|
+
return c.readyFrames[t] && c.sendMessage(t, "subscribe-key", { keyChord: i }), c.addListener(
|
|
123
|
+
t,
|
|
124
|
+
`key-pressed:${i}`,
|
|
125
|
+
(...a) => d.current(...a)
|
|
126
126
|
);
|
|
127
127
|
}
|
|
128
|
-
return
|
|
129
|
-
|
|
128
|
+
return c.addListener(
|
|
129
|
+
t,
|
|
130
130
|
o,
|
|
131
|
-
(...
|
|
131
|
+
(...i) => d.current(...i)
|
|
132
132
|
);
|
|
133
133
|
}
|
|
134
|
-
}, [
|
|
134
|
+
}, [c, t, o]);
|
|
135
135
|
};
|
|
136
|
-
|
|
137
|
-
children:
|
|
136
|
+
H.propTypes = {
|
|
137
|
+
children: $.node.isRequired
|
|
138
138
|
};
|
|
139
|
-
const _ =
|
|
139
|
+
const _ = U.default || U, z = _.iframe`
|
|
140
140
|
width: 100%;
|
|
141
141
|
height: 100%;
|
|
142
142
|
border: none;
|
|
143
|
-
visibility: ${(
|
|
143
|
+
visibility: ${(t) => t.$isReady ? "visible" : "hidden"};
|
|
144
144
|
`, G = ({
|
|
145
|
-
id:
|
|
145
|
+
id: t,
|
|
146
146
|
src: o,
|
|
147
|
-
path:
|
|
148
|
-
onInit:
|
|
147
|
+
path: u,
|
|
148
|
+
onInit: c = () => {
|
|
149
149
|
},
|
|
150
|
-
context:
|
|
151
|
-
params:
|
|
152
|
-
...
|
|
150
|
+
context: d = {},
|
|
151
|
+
params: i = {},
|
|
152
|
+
...b
|
|
153
153
|
}) => {
|
|
154
|
-
if ("isConfiguring" in
|
|
154
|
+
if ("isConfiguring" in b)
|
|
155
155
|
return console.error(
|
|
156
156
|
"isConfiguring must be passed via the params prop to MMEmbeddableZone"
|
|
157
157
|
), null;
|
|
158
|
-
if ("colorMode" in
|
|
158
|
+
if ("colorMode" in d)
|
|
159
159
|
return console.error(
|
|
160
160
|
"colorMode must be passed via the params prop to MMEmbeddableZone"
|
|
161
161
|
), null;
|
|
162
|
-
if ("isFullScreen" in
|
|
162
|
+
if ("isFullScreen" in d)
|
|
163
163
|
return console.error(
|
|
164
164
|
"isFullScreen must be passed via the params prop to MMEmbeddableZone"
|
|
165
165
|
), null;
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
), [C, k] =
|
|
169
|
-
|
|
170
|
-
l &&
|
|
166
|
+
const a = M(), [F, I] = R(), [l, f] = R(0), { registerFrame: x, unregisterFrame: P, sendMessage: p } = A(
|
|
167
|
+
Z
|
|
168
|
+
), [C, k] = R(!0), [E, e] = R();
|
|
169
|
+
g(() => {
|
|
170
|
+
l && p(t, "set-context", {
|
|
171
171
|
context: {
|
|
172
|
-
...
|
|
172
|
+
...d,
|
|
173
173
|
// TODO: Remove this once everything is migrated to params
|
|
174
|
-
colorMode:
|
|
175
|
-
isFullScreen:
|
|
174
|
+
colorMode: i.colorMode,
|
|
175
|
+
isFullScreen: i.isFullScreen
|
|
176
176
|
}
|
|
177
177
|
});
|
|
178
|
-
}, [
|
|
179
|
-
l && (
|
|
180
|
-
}, [
|
|
181
|
-
|
|
182
|
-
}, [
|
|
183
|
-
E === void 0 || !l ||
|
|
184
|
-
}, [E, l,
|
|
185
|
-
if (!(!o || !
|
|
178
|
+
}, [d, i.colorMode, i.isFullScreen, t, l]), g(() => {
|
|
179
|
+
l && (p(t, "set-params", { params: i }), p(t, "set-configuring", { isConfiguring: i.isConfiguring }));
|
|
180
|
+
}, [i, t, l]), g(() => {
|
|
181
|
+
u !== void 0 && e(u?.indexOf("/") === 0 ? u : `/${u}`);
|
|
182
|
+
}, [u]), g(() => {
|
|
183
|
+
E === void 0 || !l || p(t, "navigate", { path: E });
|
|
184
|
+
}, [E, l, t, p]), g(() => {
|
|
185
|
+
if (!(!o || !t || !a))
|
|
186
186
|
return (async () => {
|
|
187
|
-
const r = await
|
|
187
|
+
const r = await D(o);
|
|
188
188
|
if (k(r), r) {
|
|
189
|
-
const
|
|
190
|
-
x(
|
|
191
|
-
`${
|
|
189
|
+
const s = new URL(o);
|
|
190
|
+
x(t, s.origin, a), c(), I(
|
|
191
|
+
`${s.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`
|
|
192
192
|
);
|
|
193
193
|
return;
|
|
194
194
|
}
|
|
195
195
|
})(), () => {
|
|
196
|
-
|
|
196
|
+
P(t);
|
|
197
197
|
};
|
|
198
|
-
}, [o,
|
|
198
|
+
}, [o, t, x, a]), j(t, "ready", () => f((r) => r + 1));
|
|
199
|
+
const n = M();
|
|
200
|
+
return j(t, "heartbeat", () => {
|
|
201
|
+
n.current && clearTimeout(n.current), n.current = setTimeout(() => {
|
|
202
|
+
if (a.current) {
|
|
203
|
+
const r = new URL(o);
|
|
204
|
+
a.current.src = `${r.origin}/mm-embedded-interop?id=${t}&origin=${window.location.origin}`;
|
|
205
|
+
}
|
|
206
|
+
}, 6e3);
|
|
207
|
+
}), g(() => () => {
|
|
208
|
+
n.current && clearTimeout(n.current);
|
|
209
|
+
}, []), C ? /* @__PURE__ */ v(z, { $isReady: l, ref: a, src: F, ...b }) : /* @__PURE__ */ v(
|
|
199
210
|
"div",
|
|
200
211
|
{
|
|
201
212
|
style: {
|
|
@@ -205,21 +216,21 @@ const _ = j.default || j, z = _.iframe`
|
|
|
205
216
|
alignItems: "center",
|
|
206
217
|
justifyContent: "center"
|
|
207
218
|
},
|
|
208
|
-
children: /* @__PURE__ */
|
|
219
|
+
children: /* @__PURE__ */ v("div", { style: { textAlign: "center" }, children: "Application must adhere to the MM App Manifest specification to be embedded." })
|
|
209
220
|
}
|
|
210
221
|
);
|
|
211
222
|
};
|
|
212
223
|
G.propTypes = {
|
|
213
|
-
id:
|
|
214
|
-
src:
|
|
215
|
-
path:
|
|
216
|
-
onInit:
|
|
224
|
+
id: $.string,
|
|
225
|
+
src: $.string.isRequired,
|
|
226
|
+
path: $.string,
|
|
227
|
+
onInit: $.func
|
|
217
228
|
};
|
|
218
229
|
export {
|
|
219
230
|
G as MMEmbeddableZone,
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
231
|
+
Z as MMEmbeddableZoneContext,
|
|
232
|
+
H as MMEmbeddableZoneProvider,
|
|
233
|
+
B as getAppInfo,
|
|
234
|
+
D as testIsEmbeddable,
|
|
235
|
+
j as useEmbeddedEffect
|
|
225
236
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
(function(d,p){typeof exports=="object"&&typeof module<"u"?p(exports,require("react/jsx-runtime"),require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react","prop-types","styled-components"],p):(d=typeof globalThis<"u"?globalThis:d||self,p(d["mm-react-embeddable"]={},d.React,d.React,d.PropTypes,d.styled))})(this,(function(d,p,e,
|
|
1
|
+
(function(d,p){typeof exports=="object"&&typeof module<"u"?p(exports,require("react/jsx-runtime"),require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react","prop-types","styled-components"],p):(d=typeof globalThis<"u"?globalThis:d||self,p(d["mm-react-embeddable"]={},d.React,d.React,d.PropTypes,d.styled))})(this,(function(d,p,e,w,j){"use strict";const C=e.createContext(null),T=({children:r})=>{const[i,f]=e.useState({}),[c,l]=e.useState({}),u=e.useRef({}),y=e.useRef({}),a=e.useRef({}),I=e.useCallback((n,s)=>!!a.current[n]?.has(s),[]),$=e.useCallback((n,s,t)=>{f(o=>({...o,[n]:{origin:s,frameRef:t}}))},[]),b=e.useCallback(n=>{f(s=>{const t={...s};return delete t[n],t}),l(s=>{const t={...s};return delete t[n],t}),delete a.current[n]},[]);e.useEffect(()=>{const n=(t=[],o)=>{t.forEach(g=>g(o))},s=t=>{if(!t?.data?.instanceId)return;const{instanceId:o,type:g}=t.data,M=u.current;if(g==="response"){const{callbackId:h,response:L,error:q}=t.data,P=y.current[h];P&&(P(L,q),delete y.current[h]);return}if(g==="mark-registered"){const{requestType:h}=t.data;a.current[o]||(a.current[o]=new Set),a.current[o].add(h);return}g==="ready"&&l(h=>({...h,[o]:!0})),n(M[o]?.[g],t.data),n(M["*"]?.[g],t.data),n(M[o]?.["*"],t.data),n(M["*"]?.["*"],t.data)};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[i]);const m=e.useCallback((n,s,t)=>{const o=i[n];if(!o){console.warn(`Origin not set for id: ${n}. Unable to send message.`);return}o?.frameRef?.current?.contentWindow&&o.frameRef.current.contentWindow.postMessage({type:s,...t,instanceId:n},o.origin)},[i]),F=e.useRef(0),v=e.useCallback(()=>`${Date.now()}-${++F.current}`,[]),E=e.useCallback(async(n,s,t={})=>new Promise((o,g)=>{const M=v(),h=(L,q)=>{delete y.current[M],q?g(q):o(L)};y.current[M]=h,m(n,"request",{callbackId:M,requestType:s,data:t})}),[m]),S=e.useCallback((n,s)=>{m(n,"load-options",s)},[m]),x=e.useCallback((n,s)=>{m(n,"validate",s)},[m]),R=e.useMemo(()=>({registerFrame:$,unregisterFrame:b,sendMessage:m,sendRequest:E,loadOptions:S,validate:x,hasHandler:I,readyFrames:c,addListener:(n,s,t)=>(u.current[n]||(u.current[n]={}),u.current[n][s]||(u.current[n][s]=[]),u.current[n][s].push(t),()=>{u.current[n][s]=u.current[n][s].filter(o=>o!==t)})}),[$,b,m,E,S,x,I,c]);return p.jsx(C.Provider,{value:R,children:r})},k={},U=async r=>{try{const i=new URL(r).origin;if(k[i]!==void 0)return k[i];const c=await(await fetch(`${i}/mm-app-manifest.json`)).json();if(!c.version)throw new Error("Invalid manifest");return k[i]=c,c}catch{}return k[origin]=!1,!1},A=async r=>!!await U(r),Z=(r,i,f)=>{const c=e.useContext(C);if(c===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const l=e.useRef(f);e.useEffect(()=>{l.current=f},[f]),e.useEffect(()=>{if(!(!r||!i)){if(i.startsWith("key:")){const u=i.substring(4);return c.readyFrames[r]&&c.sendMessage(r,"subscribe-key",{keyChord:u}),c.addListener(r,`key-pressed:${u}`,(...a)=>l.current(...a))}return c.addListener(r,i,(...u)=>l.current(...u))}},[c,r,i])};T.propTypes={children:w.node.isRequired};const W=(j.default||j).iframe`
|
|
2
2
|
width: 100%;
|
|
3
3
|
height: 100%;
|
|
4
4
|
border: none;
|
|
5
|
-
visibility: ${
|
|
6
|
-
`,
|
|
5
|
+
visibility: ${r=>r.$isReady?"visible":"hidden"};
|
|
6
|
+
`,O=({id:r,src:i,path:f,onInit:c=()=>{},context:l={},params:u={},...y})=>{if("isConfiguring"in y)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in l)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in l)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const a=e.useRef(),[I,$]=e.useState(),[b,m]=e.useState(0),{registerFrame:F,unregisterFrame:v,sendMessage:E}=e.useContext(C),[S,x]=e.useState(!0),[R,n]=e.useState();e.useEffect(()=>{b&&E(r,"set-context",{context:{...l,colorMode:u.colorMode,isFullScreen:u.isFullScreen}})},[l,u.colorMode,u.isFullScreen,r,b]),e.useEffect(()=>{b&&(E(r,"set-params",{params:u}),E(r,"set-configuring",{isConfiguring:u.isConfiguring}))},[u,r,b]),e.useEffect(()=>{f!==void 0&&n(f?.indexOf("/")===0?f:`/${f}`)},[f]),e.useEffect(()=>{R===void 0||!b||E(r,"navigate",{path:R})},[R,b,r,E]),e.useEffect(()=>{if(!(!i||!r||!a))return(async()=>{const t=await A(i);if(x(t),t){const o=new URL(i);F(r,o.origin,a),c(),$(`${o.origin}/mm-embedded-interop?id=${r}&origin=${window.location.origin}`);return}})(),()=>{v(r)}},[i,r,F,a]),Z(r,"ready",()=>m(t=>t+1));const s=e.useRef();return Z(r,"heartbeat",()=>{s.current&&clearTimeout(s.current),s.current=setTimeout(()=>{if(a.current){const t=new URL(i);a.current.src=`${t.origin}/mm-embedded-interop?id=${r}&origin=${window.location.origin}`}},6e3)}),e.useEffect(()=>()=>{s.current&&clearTimeout(s.current)},[]),S?p.jsx(W,{$isReady:b,ref:a,src:I,...y}):p.jsx("div",{style:{height:"100%",display:"flex",flex:1,alignItems:"center",justifyContent:"center"},children:p.jsx("div",{style:{textAlign:"center"},children:"Application must adhere to the MM App Manifest specification to be embedded."})})};O.propTypes={id:w.string,src:w.string.isRequired,path:w.string,onInit:w.func},d.MMEmbeddableZone=O,d.MMEmbeddableZoneContext=C,d.MMEmbeddableZoneProvider=T,d.getAppInfo=U,d.testIsEmbeddable=A,d.useEmbeddedEffect=Z,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));
|