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