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