@machinemetrics/mm-react-embeddable 0.4.9 → 0.9.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.
package/README.md
ADDED
|
@@ -2,29 +2,29 @@ import b, { createContext as P, useState as g, useRef as M, useCallback as y, us
|
|
|
2
2
|
import f from "prop-types";
|
|
3
3
|
import C from "styled-components";
|
|
4
4
|
const w = P(null), W = ({ children: n }) => {
|
|
5
|
-
const [a, i] = g({}),
|
|
6
|
-
i((s) => ({ ...s, [t]: { origin:
|
|
5
|
+
const [a, i] = g({}), e = M({}), u = y((t, r, o) => {
|
|
6
|
+
i((s) => ({ ...s, [t]: { origin: r, frameRef: o } }));
|
|
7
7
|
}, []);
|
|
8
8
|
m(() => {
|
|
9
|
-
const t = (
|
|
9
|
+
const t = (r) => {
|
|
10
10
|
var s, l;
|
|
11
|
-
if (!((s =
|
|
11
|
+
if (!((s = r == null ? void 0 : r.data) != null && s.instanceId))
|
|
12
12
|
return;
|
|
13
|
-
(((l =
|
|
13
|
+
(((l = e.current[r.data.instanceId]) == null ? void 0 : l[r.data.type]) || []).forEach((E) => E(r.data));
|
|
14
14
|
};
|
|
15
15
|
return window.addEventListener("message", t), () => {
|
|
16
16
|
window.removeEventListener("message", t);
|
|
17
17
|
};
|
|
18
18
|
}, [a]);
|
|
19
19
|
const d = y(
|
|
20
|
-
(t,
|
|
20
|
+
(t, r, o) => {
|
|
21
21
|
const s = a[t];
|
|
22
22
|
if (!s) {
|
|
23
23
|
console.warn(`Origin not set for id: ${t}. Unable to send message.`);
|
|
24
24
|
return;
|
|
25
25
|
}
|
|
26
26
|
s.frameRef.current.contentWindow.postMessage(
|
|
27
|
-
{ type:
|
|
27
|
+
{ type: r, ...o, instanceId: t },
|
|
28
28
|
s.origin
|
|
29
29
|
);
|
|
30
30
|
},
|
|
@@ -32,14 +32,14 @@ const w = P(null), W = ({ children: n }) => {
|
|
|
32
32
|
), c = {
|
|
33
33
|
registerFrame: u,
|
|
34
34
|
sendMessage: d,
|
|
35
|
-
addListener: (t,
|
|
36
|
-
|
|
35
|
+
addListener: (t, r, o) => (e.current[t] || (e.current[t] = {}), e.current[t][r] || (e.current[t][r] = []), e.current[t][r].push(o), () => {
|
|
36
|
+
e.current[t][r] = e.current[t][r].filter((s) => s !== o);
|
|
37
37
|
})
|
|
38
38
|
};
|
|
39
39
|
return /* @__PURE__ */ b.createElement(w.Provider, { value: c }, n);
|
|
40
40
|
}, $ = (n, a, i) => {
|
|
41
|
-
const
|
|
42
|
-
if (
|
|
41
|
+
const e = R(w);
|
|
42
|
+
if (e === null)
|
|
43
43
|
throw new Error(
|
|
44
44
|
"useEmbeddedEffect must be used within a MMEmbeddedWrapperProvider"
|
|
45
45
|
);
|
|
@@ -48,13 +48,13 @@ const w = P(null), W = ({ children: n }) => {
|
|
|
48
48
|
u.current = i;
|
|
49
49
|
}, [i]), m(() => {
|
|
50
50
|
if (!(!n || !a))
|
|
51
|
-
return
|
|
51
|
+
return e.addListener(
|
|
52
52
|
n,
|
|
53
53
|
a,
|
|
54
54
|
(...d) => u.current(...d)
|
|
55
55
|
);
|
|
56
|
-
}, [
|
|
57
|
-
|
|
56
|
+
}, [e, n, a]), y((d, c) => e.sendMessage(n, d, c), [
|
|
57
|
+
e,
|
|
58
58
|
n
|
|
59
59
|
]);
|
|
60
60
|
};
|
|
@@ -70,15 +70,15 @@ const j = C.iframe`
|
|
|
70
70
|
id: n,
|
|
71
71
|
genericFrameFallback: a,
|
|
72
72
|
src: i,
|
|
73
|
-
path:
|
|
73
|
+
path: e,
|
|
74
74
|
onInit: u = () => {
|
|
75
75
|
},
|
|
76
76
|
...d
|
|
77
77
|
}) => {
|
|
78
|
-
const c = M(), [t,
|
|
78
|
+
const c = M(), [t, r] = g(), [o, s] = g(0), { registerFrame: l, sendMessage: E } = R(w), [h, I] = g(), [x, L] = g(!1);
|
|
79
79
|
return m(() => {
|
|
80
|
-
|
|
81
|
-
}, [
|
|
80
|
+
e !== void 0 && (console.log(e), I(e));
|
|
81
|
+
}, [e]), m(() => {
|
|
82
82
|
h === void 0 || !o || E(n, "navigate", { path: h });
|
|
83
83
|
}, [h, o, n, E]), m(() => {
|
|
84
84
|
!i || !n || !c || (async () => {
|
|
@@ -86,11 +86,11 @@ const j = C.iframe`
|
|
|
86
86
|
l(n, p.origin, c), u();
|
|
87
87
|
try {
|
|
88
88
|
if ((await (await fetch(`${p.origin}/mm-app-manifest.json`)).json()).manifestVersion)
|
|
89
|
-
|
|
89
|
+
r(`${p.origin}/mm-embedded-interop?id=${n}`);
|
|
90
90
|
else
|
|
91
91
|
throw Error();
|
|
92
92
|
} catch {
|
|
93
|
-
a ? (
|
|
93
|
+
a ? (r(i), s(1)) : L(!0);
|
|
94
94
|
}
|
|
95
95
|
})();
|
|
96
96
|
}, [i, n, l, c]), $(n, "ready", () => s((p) => p + 1)), x ? /* @__PURE__ */ b.createElement(
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
(function(o,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):(o=typeof globalThis<"u"?globalThis:o||self,e(o["mm-react-embeddable"]={},o.React,o.PropTypes,o.styled))})(this,function(o,e,m,C){"use strict";const g=e.createContext(null),y=({children:s})=>{const[f,u]=e.useState({}),r=e.useRef({}),l=e.useCallback((n,t,d)=>{u(i=>({...i,[n]:{origin:t,frameRef:d}}))},[]);e.useEffect(()=>{const n=t=>{var i,p;if(!((i=t==null?void 0:t.data)!=null&&i.instanceId))return;(((p=r.current[t.data.instanceId])==null?void 0:p[t.data.type])||[]).forEach(b=>b(t.data))};return window.addEventListener("message",n),()=>{window.removeEventListener("message",n)}},[f]);const c=e.useCallback((n,t,d)=>{const i=f[n];if(!i){console.warn(`Origin not set for id: ${n}. Unable to send message.`);return}i.frameRef.current.contentWindow.postMessage({type:t,...d,instanceId:n},i.origin)},[f]),a={registerFrame:l,sendMessage:c,addListener:(n,t,d)=>(r.current[n]||(r.current[n]={}),r.current[n][t]||(r.current[n][t]=[]),r.current[n][t].push(d),()=>{r.current[n][t]=r.current[n][t].filter(i=>i!==d)})};return e.createElement(g.Provider,{value:a},s)},M=(s,f,u)=>{const r=e.useContext(g);if(r===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddedWrapperProvider");const l=e.useRef(u);return e.useEffect(()=>{l.current=u},[u]),e.useEffect(()=>{if(!(!s||!f))return r.addListener(s,f,(...c)=>l.current(...c))},[r,s,f]),e.useCallback((c,a)=>r.sendMessage(s,c,a),[r,s])};y.propTypes={children:m.node.isRequired};const I=C.iframe`
|
|
2
|
+
width: 100%;
|
|
3
|
+
height: 100%;
|
|
4
|
+
border: none;
|
|
5
|
+
visibility: ${s=>s.isready?"visible":"hidden"};
|
|
6
|
+
`,w=({id:s,genericFrameFallback:f,src:u,path:r,onInit:l=()=>{},...c})=>{const a=e.useRef(),[n,t]=e.useState(),[d,i]=e.useState(0),{registerFrame:p,sendMessage:b}=e.useContext(g),[h,x]=e.useState(),[L,S]=e.useState(!1);return e.useEffect(()=>{r!==void 0&&(console.log(r),x(r))},[r]),e.useEffect(()=>{h===void 0||!d||b(s,"navigate",{path:h})},[h,d,s,b]),e.useEffect(()=>{!u||!s||!a||(async()=>{const E=new URL(u);p(s,E.origin,a),l();try{if((await(await fetch(`${E.origin}/mm-app-manifest.json`)).json()).manifestVersion)t(`${E.origin}/mm-embedded-interop?id=${s}`);else throw Error()}catch{f?(t(u),i(1)):S(!0)}})()},[u,s,p,a]),M(s,"ready",()=>i(E=>E+1)),L?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(I,{isready:d,ref:a,src:n,...c})};w.propTypes={id:m.string,genericFrameFallback:m.bool,src:m.string.isRequired,path:m.string,onInit:m.func},o.MMEmbeddedWrapper=w,o.MMEmbeddedWrapperProvider=y,o.useEmbeddedEffect=M,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,20 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@machinemetrics/mm-react-embeddable",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"module": "dist/mm-react-embeddable.es.js",
|
|
6
|
-
"main": "dist/mm-react-embeddable.cjs.js",
|
|
7
6
|
"source": "src/index.js",
|
|
8
7
|
"type": "module",
|
|
9
8
|
"engines": {
|
|
10
9
|
"node": ">=18"
|
|
11
10
|
},
|
|
12
|
-
"exports": {
|
|
13
|
-
".": {
|
|
14
|
-
"import": "./dist/mm-react-embeddable.es.js",
|
|
15
|
-
"require": "./dist/mm-react-embeddable.cjs.js"
|
|
16
|
-
}
|
|
17
|
-
},
|
|
18
11
|
"scripts": {
|
|
19
12
|
"build": "vite build",
|
|
20
13
|
"start": "vite build --watch"
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),l=require("prop-types"),P=require("styled-components"),g=e.createContext(null),M=({children:n})=>{const[c,i]=e.useState({}),r=e.useRef({}),f=e.useCallback((s,t,o)=>{i(a=>({...a,[s]:{origin:t,frameRef:o}}))},[]);e.useEffect(()=>{const s=t=>{var a,m;if(!((a=t==null?void 0:t.data)!=null&&a.instanceId))return;(((m=r.current[t.data.instanceId])==null?void 0:m[t.data.type])||[]).forEach(E=>E(t.data))};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[c]);const u=e.useCallback((s,t,o)=>{const a=c[s];if(!a){console.warn(`Origin not set for id: ${s}. Unable to send message.`);return}a.frameRef.current.contentWindow.postMessage({type:t,...o,instanceId:s},a.origin)},[c]),d={registerFrame:f,sendMessage:u,addListener:(s,t,o)=>(r.current[s]||(r.current[s]={}),r.current[s][t]||(r.current[s][t]=[]),r.current[s][t].push(o),()=>{r.current[s][t]=r.current[s][t].filter(a=>a!==o)})};return e.createElement(g.Provider,{value:d},n)},h=(n,c,i)=>{const r=e.useContext(g);if(r===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddedWrapperProvider");const f=e.useRef(i);return e.useEffect(()=>{f.current=i},[i]),e.useEffect(()=>{if(!(!n||!c))return r.addListener(n,c,(...u)=>f.current(...u))},[r,n,c]),e.useCallback((u,d)=>r.sendMessage(n,u,d),[r,n])};M.propTypes={children:l.node.isRequired};const x=P.iframe`
|
|
2
|
-
width: 100%;
|
|
3
|
-
height: 100%;
|
|
4
|
-
border: none;
|
|
5
|
-
visibility: ${n=>n.isready?"visible":"hidden"};
|
|
6
|
-
`,y=({id:n,genericFrameFallback:c,src:i,path:r,onInit:f=()=>{},...u})=>{const d=e.useRef(),[s,t]=e.useState(),[o,a]=e.useState(0),{registerFrame:m,sendMessage:E}=e.useContext(g),[b,w]=e.useState(),[R,C]=e.useState(!1);return e.useEffect(()=>{r!==void 0&&w(r)},[r]),e.useEffect(()=>{b===void 0||!o||E(n,"navigate",{path:b})},[b,o,n,E]),e.useEffect(()=>{!i||!n||!d||(async()=>{const p=new URL(i);m(n,p.origin,d),f();try{if((await(await fetch(`${p.origin}/mm-app-manifest.json`)).json()).manifestVersion)t(`${p.origin}/mm-embedded-interop?id=${n}`);else throw Error()}catch{c?(t(i),a(1)):C(!0)}})()},[i,n,m,d]),h(n,"ready",()=>a(p=>p+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(x,{isready:o,ref:d,src:s,...u})};y.propTypes={id:l.string,genericFrameFallback:l.bool,src:l.string.isRequired,path:l.string,onInit:l.func};exports.MMEmbeddedWrapper=y;exports.MMEmbeddedWrapperProvider=M;exports.useEmbeddedEffect=h;
|