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