@machinemetrics/mm-react-embeddable 0.9.2 → 0.9.3
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.
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("react"),l=require("prop-types"),I=require("styled-components"),p=e.createContext(null),M=({children:n})=>{const[c,i]=e.useState({}),t=e.useRef({}),f=e.useCallback((s,r,a)=>{i(o=>({...o,[s]:{origin:r,frameRef:a}}))},[]);e.useEffect(()=>{const s=r=>{var o,m;if(!((o=r==null?void 0:r.data)!=null&&o.instanceId))return;(((m=t.current[r.data.instanceId])==null?void 0:m[r.data.type])||[]).forEach(g=>g(r.data))};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[c]);const d=e.useCallback((s,r,a)=>{const o=c[s];if(!o){console.warn(`Origin not set for id: ${s}. Unable to send message.`);return}o.frameRef.current.contentWindow.postMessage({type:r,...a,instanceId:s},o.origin)},[c]),u={registerFrame:f,sendMessage:d,addListener:(s,r,a)=>(t.current[s]||(t.current[s]={}),t.current[s][r]||(t.current[s][r]=[]),t.current[s][r].push(a),()=>{t.current[s][r]=t.current[s][r].filter(o=>o!==a)})};return e.createElement(p.Provider,{value:u},n)},h=(n,c,i)=>{const t=e.useContext(p);if(t===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const f=e.useRef(i);return e.useEffect(()=>{f.current=i},[i]),e.useEffect(()=>{if(!(!n||!c))return t.addListener(n,c,(...d)=>f.current(...d))},[t,n,c]),e.useCallback((d,u)=>t.sendMessage(n,d,u),[t,n])};M.propTypes={children:l.node.isRequired};const L=I.iframe`
|
|
2
2
|
width: 100%;
|
|
3
3
|
height: 100%;
|
|
4
4
|
border: none;
|
|
5
5
|
visibility: ${n=>n.isready?"visible":"hidden"};
|
|
6
|
-
`,
|
|
6
|
+
`,y=({id:n,genericFrameFallback:c,src:i,path:t,onInit:f=()=>{},...d})=>{const u=e.useRef(),[s,r]=e.useState(),[a,o]=e.useState(0),{registerFrame:m,sendMessage:g}=e.useContext(MMEmbeddedWrapperContext),[E,w]=e.useState(),[R,x]=e.useState(!1);return e.useEffect(()=>{t!==void 0&&(console.log(t),w(t))},[t]),e.useEffect(()=>{E===void 0||!a||g(n,"navigate",{path:E})},[E,a,n,g]),e.useEffect(()=>{!i||!n||!u||(async()=>{const b=new URL(i);m(n,b.origin,u),f();try{if((await(await fetch(`${b.origin}/mm-app-manifest.json`)).json()).manifestVersion)r(`${b.origin}/mm-embedded-interop?id=${n}`);else throw Error()}catch{c?(r(i),o(1)):x(!0)}})()},[i,n,m,u]),h(n,"ready",()=>o(b=>b+1)),R?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.")):e.createElement(L,{isready:a,ref:u,src:s,...d})};y.propTypes={id:l.string,genericFrameFallback:l.bool,src:l.string.isRequired,path:l.string,onInit:l.func};const P={MMEmbeddableZone:y,MMEmbeddableZoneProvider:M,useEmbeddedEffect:h};exports.default=P;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import E, { createContext as
|
|
1
|
+
import E, { createContext as Z, useState as b, useRef as M, useCallback as y, useEffect as l, useContext as w } from "react";
|
|
2
2
|
import f from "prop-types";
|
|
3
|
-
import
|
|
4
|
-
const R =
|
|
3
|
+
import j from "styled-components";
|
|
4
|
+
const R = Z(null), x = ({ children: n }) => {
|
|
5
5
|
const [a, i] = b({}), e = M({}), u = y((r, t, o) => {
|
|
6
6
|
i((s) => ({ ...s, [r]: { origin: t, frameRef: o } }));
|
|
7
7
|
}, []);
|
|
@@ -37,7 +37,7 @@ const R = F(null), $ = ({ children: n }) => {
|
|
|
37
37
|
})
|
|
38
38
|
};
|
|
39
39
|
return /* @__PURE__ */ E.createElement(R.Provider, { value: c }, n);
|
|
40
|
-
},
|
|
40
|
+
}, I = (n, a, i) => {
|
|
41
41
|
const e = w(R);
|
|
42
42
|
if (e === null)
|
|
43
43
|
throw new Error(
|
|
@@ -58,15 +58,15 @@ const R = F(null), $ = ({ children: n }) => {
|
|
|
58
58
|
[e, n]
|
|
59
59
|
);
|
|
60
60
|
};
|
|
61
|
-
|
|
61
|
+
x.propTypes = {
|
|
62
62
|
children: f.node.isRequired
|
|
63
63
|
};
|
|
64
|
-
const
|
|
64
|
+
const v = j.iframe`
|
|
65
65
|
width: 100%;
|
|
66
66
|
height: 100%;
|
|
67
67
|
border: none;
|
|
68
68
|
visibility: ${(n) => n.isready ? "visible" : "hidden"};
|
|
69
|
-
`,
|
|
69
|
+
`, L = ({
|
|
70
70
|
id: n,
|
|
71
71
|
genericFrameFallback: a,
|
|
72
72
|
src: i,
|
|
@@ -75,9 +75,9 @@ const j = P.iframe`
|
|
|
75
75
|
},
|
|
76
76
|
...d
|
|
77
77
|
}) => {
|
|
78
|
-
const c = M(), [r, t] = b(), [o, s] = b(0), { registerFrame: m, sendMessage: g } = w(MMEmbeddedWrapperContext), [h,
|
|
78
|
+
const c = M(), [r, t] = b(), [o, s] = b(0), { registerFrame: m, sendMessage: g } = w(MMEmbeddedWrapperContext), [h, C] = b(), [F, P] = b(!1);
|
|
79
79
|
return l(() => {
|
|
80
|
-
e !== void 0 && (console.log(e),
|
|
80
|
+
e !== void 0 && (console.log(e), C(e));
|
|
81
81
|
}, [e]), l(() => {
|
|
82
82
|
h === void 0 || !o || g(n, "navigate", { path: h });
|
|
83
83
|
}, [h, o, n, g]), l(() => {
|
|
@@ -90,10 +90,10 @@ const j = P.iframe`
|
|
|
90
90
|
else
|
|
91
91
|
throw Error();
|
|
92
92
|
} catch {
|
|
93
|
-
a ? (t(i), s(1)) :
|
|
93
|
+
a ? (t(i), s(1)) : P(!0);
|
|
94
94
|
}
|
|
95
95
|
})();
|
|
96
|
-
}, [i, n, m, c]),
|
|
96
|
+
}, [i, n, m, c]), I(n, "ready", () => s((p) => p + 1)), F ? /* @__PURE__ */ E.createElement(
|
|
97
97
|
"div",
|
|
98
98
|
{
|
|
99
99
|
style: {
|
|
@@ -104,17 +104,20 @@ const j = P.iframe`
|
|
|
104
104
|
}
|
|
105
105
|
},
|
|
106
106
|
/* @__PURE__ */ E.createElement("div", { style: { textAlign: "center" } }, "Application must adhere to the MM App Manifest specification to be embedded.")
|
|
107
|
-
) : /* @__PURE__ */ E.createElement(
|
|
107
|
+
) : /* @__PURE__ */ E.createElement(v, { isready: o, ref: c, src: r, ...d });
|
|
108
108
|
};
|
|
109
|
-
|
|
109
|
+
L.propTypes = {
|
|
110
110
|
id: f.string,
|
|
111
111
|
genericFrameFallback: f.bool,
|
|
112
112
|
src: f.string.isRequired,
|
|
113
113
|
path: f.string,
|
|
114
114
|
onInit: f.func
|
|
115
115
|
};
|
|
116
|
+
const W = {
|
|
117
|
+
MMEmbeddableZone: L,
|
|
118
|
+
MMEmbeddableZoneProvider: x,
|
|
119
|
+
useEmbeddedEffect: I
|
|
120
|
+
};
|
|
116
121
|
export {
|
|
117
|
-
|
|
118
|
-
$ as MMEmbeddableZoneProvider,
|
|
119
|
-
Z as useEmbeddedEffect
|
|
122
|
+
W as default
|
|
120
123
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
(function(
|
|
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,l,x){"use strict";const h=e.createContext(null),y=({children:s})=>{const[f,d]=e.useState({}),t=e.useRef({}),m=e.useCallback((r,n,i)=>{d(o=>({...o,[r]:{origin:n,frameRef:i}}))},[]);e.useEffect(()=>{const r=n=>{var o,b;if(!((o=n==null?void 0:n.data)!=null&&o.instanceId))return;(((b=t.current[n.data.instanceId])==null?void 0:b[n.data.type])||[]).forEach(E=>E(n.data))};return window.addEventListener("message",r),()=>{window.removeEventListener("message",r)}},[f]);const c=e.useCallback((r,n,i)=>{const o=f[r];if(!o){console.warn(`Origin not set for id: ${r}. Unable to send message.`);return}o.frameRef.current.contentWindow.postMessage({type:n,...i,instanceId:r},o.origin)},[f]),a={registerFrame:m,sendMessage:c,addListener:(r,n,i)=>(t.current[r]||(t.current[r]={}),t.current[r][n]||(t.current[r][n]=[]),t.current[r][n].push(i),()=>{t.current[r][n]=t.current[r][n].filter(o=>o!==i)})};return e.createElement(h.Provider,{value:a},s)},M=(s,f,d)=>{const t=e.useContext(h);if(t===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const m=e.useRef(d);return e.useEffect(()=>{m.current=d},[d]),e.useEffect(()=>{if(!(!s||!f))return t.addListener(s,f,(...c)=>m.current(...c))},[t,s,f]),e.useCallback((c,a)=>t.sendMessage(s,c,a),[t,s])};y.propTypes={children:l.node.isRequired};const C=x.iframe`
|
|
2
2
|
width: 100%;
|
|
3
3
|
height: 100%;
|
|
4
4
|
border: none;
|
|
5
5
|
visibility: ${s=>s.isready?"visible":"hidden"};
|
|
6
|
-
`,w=({id:s,genericFrameFallback:f,src:
|
|
6
|
+
`,w=({id:s,genericFrameFallback:f,src:d,path:t,onInit:m=()=>{},...c})=>{const a=e.useRef(),[r,n]=e.useState(),[i,o]=e.useState(0),{registerFrame:b,sendMessage:E}=e.useContext(MMEmbeddedWrapperContext),[g,L]=e.useState(),[S,F]=e.useState(!1);return e.useEffect(()=>{t!==void 0&&(console.log(t),L(t))},[t]),e.useEffect(()=>{g===void 0||!i||E(s,"navigate",{path:g})},[g,i,s,E]),e.useEffect(()=>{!d||!s||!a||(async()=>{const p=new URL(d);b(s,p.origin,a),m();try{if((await(await fetch(`${p.origin}/mm-app-manifest.json`)).json()).manifestVersion)n(`${p.origin}/mm-embedded-interop?id=${s}`);else throw Error()}catch{f?(n(d),o(1)):F(!0)}})()},[d,s,b,a]),M(s,"ready",()=>o(p=>p+1)),S?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.")):e.createElement(C,{isready:i,ref:a,src:r,...c})};w.propTypes={id:l.string,genericFrameFallback:l.bool,src:l.string.isRequired,path:l.string,onInit:l.func};const I={MMEmbeddableZone:w,MMEmbeddableZoneProvider:y,useEmbeddedEffect:M};u.default=I,Object.defineProperties(u,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|