@machinemetrics/mm-react-embeddable 0.9.11 → 0.9.13
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
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),b=require("prop-types"),h=require("styled-components"),g=e.createContext(null),w=({children:r})=>{const[n,i]=e.useState({}),o=e.useRef({}),u=e.useCallback((s,t,c)=>{i(a=>({...a,[s]:{origin:t,frameRef:c}}))},[]);e.useEffect(()=>{const s=t=>{var a,l;if(!((a=t==null?void 0:t.data)!=null&&a.instanceId))return;(((l=o.current[t.data.instanceId])==null?void 0:l[t.data.type])||[]).forEach(M=>M(t.data))};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[n]);const d=e.useCallback((s,t,c)=>{const a=n[s];if(!a){console.warn(`Origin not set for id: ${s}. Unable to send message.`);return}a.frameRef.current.contentWindow.postMessage({type:t,...c,instanceId:s},a.origin)},[n]),f={testIsEmbeddable:R,registerFrame:u,sendMessage:d,addListener:(s,t,c)=>(o.current[s]||(o.current[s]={}),o.current[s][t]||(o.current[s][t]=[]),o.current[s][t].push(c),()=>{o.current[s][t]=o.current[s][t].filter(a=>a!==c)})};return e.createElement(g.Provider,{value:f},r)},E={},R=async r=>{try{const n=new URL(r).origin;if(E[n]!==void 0)return E[n];if(!(await(await fetch(`${n}/mm-app-manifest.json`)).json()).version)throw new Error("Invalid manifest");return E[n]=!0,!0}catch{}return E[origin]=!1,!1},I=(r,n,i)=>{const o=e.useContext(g);if(o===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const u=e.useRef(i);return e.useEffect(()=>{u.current=i},[i]),e.useEffect(()=>{if(!(!r||!n))return o.addListener(r,n,(...d)=>u.current(...d))},[o,r,n]),e.useCallback((d,f)=>o.sendMessage(r,d,f),[o,r])};w.propTypes={children:b.node.isRequired};const P=h.default||h,S=P.iframe`
|
|
2
2
|
width: 100%;
|
|
3
3
|
height: 100%;
|
|
4
4
|
border: none;
|
|
5
|
-
visibility: ${
|
|
6
|
-
`,
|
|
5
|
+
visibility: ${r=>r.isready?"visible":"hidden"};
|
|
6
|
+
`,C=({id:r,src:n,path:i,onInit:o=()=>{},...u})=>{const d=e.useRef(),[f,s]=e.useState(),[t,c]=e.useState(0),{registerFrame:a,sendMessage:l,testIsEmbeddable:M}=e.useContext(g),[v,x]=e.useState(!0),[p,L]=e.useState();return e.useEffect(()=>{i!==void 0&&L(i)},[i]),e.useEffect(()=>{p===void 0||!t||l(r,"navigate",{path:p})},[p,t,r,l]),e.useEffect(()=>{!n||!r||!d||(async()=>{const m=M(n);if(x(m),m){const y=new URL(n);a(r,y.origin,d),o(),s(`${y.origin}/mm-embedded-interop?id=${r}`);return}})()},[n,r,a,d]),I(r,"ready",()=>c(m=>m+1)),v?e.createElement(S,{isready:t,ref:d,src:f,...u}):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."))};C.propTypes={id:b.string,src:b.string.isRequired,path:b.string,onInit:b.func};exports.MMEmbeddableZone=C;exports.MMEmbeddableZoneContext=g;exports.MMEmbeddableZoneProvider=w;exports.testIsEmbeddable=R;exports.useEmbeddedEffect=I;
|
|
@@ -1,104 +1,102 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import E, { createContext as $, useState as m, useRef as M, useCallback as w, useEffect as u, useContext as x } from "react";
|
|
2
|
+
import b from "prop-types";
|
|
3
3
|
import L from "styled-components";
|
|
4
|
-
const
|
|
5
|
-
const [
|
|
6
|
-
|
|
7
|
-
}, [])
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return u[e];
|
|
13
|
-
if (!(await (await fetch(`${e}/mm-app-manifest.json`)).json()).version)
|
|
14
|
-
throw new Error("Invalid manifest");
|
|
15
|
-
return p((c) => ({ ...c, [e]: !0 })), !0;
|
|
16
|
-
} catch {
|
|
17
|
-
a(!1);
|
|
18
|
-
}
|
|
19
|
-
return p((e) => ({ ...e, [origin]: !1 })), !1;
|
|
20
|
-
},
|
|
21
|
-
[u]
|
|
22
|
-
);
|
|
23
|
-
m(() => {
|
|
24
|
-
const t = (e) => {
|
|
25
|
-
var s, c;
|
|
26
|
-
if (!((s = e == null ? void 0 : e.data) != null && s.instanceId))
|
|
4
|
+
const R = $(null), F = ({ children: t }) => {
|
|
5
|
+
const [r, i] = m({}), s = M({}), c = w((n, e, d) => {
|
|
6
|
+
i((o) => ({ ...o, [n]: { origin: e, frameRef: d } }));
|
|
7
|
+
}, []);
|
|
8
|
+
u(() => {
|
|
9
|
+
const n = (e) => {
|
|
10
|
+
var o, l;
|
|
11
|
+
if (!((o = e == null ? void 0 : e.data) != null && o.instanceId))
|
|
27
12
|
return;
|
|
28
|
-
(((
|
|
13
|
+
(((l = s.current[e.data.instanceId]) == null ? void 0 : l[e.data.type]) || []).forEach((h) => h(e.data));
|
|
29
14
|
};
|
|
30
|
-
return window.addEventListener("message",
|
|
31
|
-
window.removeEventListener("message",
|
|
15
|
+
return window.addEventListener("message", n), () => {
|
|
16
|
+
window.removeEventListener("message", n);
|
|
32
17
|
};
|
|
33
|
-
}, [
|
|
34
|
-
const
|
|
35
|
-
(
|
|
36
|
-
const
|
|
37
|
-
if (!
|
|
38
|
-
console.warn(`Origin not set for id: ${
|
|
18
|
+
}, [r]);
|
|
19
|
+
const a = w(
|
|
20
|
+
(n, e, d) => {
|
|
21
|
+
const o = r[n];
|
|
22
|
+
if (!o) {
|
|
23
|
+
console.warn(`Origin not set for id: ${n}. Unable to send message.`);
|
|
39
24
|
return;
|
|
40
25
|
}
|
|
41
|
-
|
|
42
|
-
{ type: e, ...
|
|
43
|
-
|
|
26
|
+
o.frameRef.current.contentWindow.postMessage(
|
|
27
|
+
{ type: e, ...d, instanceId: n },
|
|
28
|
+
o.origin
|
|
44
29
|
);
|
|
45
30
|
},
|
|
46
|
-
[
|
|
47
|
-
),
|
|
48
|
-
testIsEmbeddable:
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
n.current[t][e] = n.current[t][e].filter((s) => s !== o);
|
|
31
|
+
[r]
|
|
32
|
+
), f = {
|
|
33
|
+
testIsEmbeddable: U,
|
|
34
|
+
registerFrame: c,
|
|
35
|
+
sendMessage: a,
|
|
36
|
+
addListener: (n, e, d) => (s.current[n] || (s.current[n] = {}), s.current[n][e] || (s.current[n][e] = []), s.current[n][e].push(d), () => {
|
|
37
|
+
s.current[n][e] = s.current[n][e].filter((o) => o !== d);
|
|
54
38
|
})
|
|
55
39
|
};
|
|
56
|
-
return /* @__PURE__ */
|
|
57
|
-
},
|
|
58
|
-
|
|
59
|
-
|
|
40
|
+
return /* @__PURE__ */ E.createElement(R.Provider, { value: f }, t);
|
|
41
|
+
}, p = {}, U = async (t) => {
|
|
42
|
+
try {
|
|
43
|
+
const r = new URL(t).origin;
|
|
44
|
+
if (p[r] !== void 0)
|
|
45
|
+
return p[r];
|
|
46
|
+
if (!(await (await fetch(`${r}/mm-app-manifest.json`)).json()).version)
|
|
47
|
+
throw new Error("Invalid manifest");
|
|
48
|
+
return p[r] = !0, !0;
|
|
49
|
+
} catch {
|
|
50
|
+
}
|
|
51
|
+
return p[origin] = !1, !1;
|
|
52
|
+
}, Z = (t, r, i) => {
|
|
53
|
+
const s = x(R);
|
|
54
|
+
if (s === null)
|
|
60
55
|
throw new Error(
|
|
61
56
|
"useEmbeddedEffect must be used within a MMEmbeddableZoneProvider"
|
|
62
57
|
);
|
|
63
|
-
const
|
|
64
|
-
return
|
|
65
|
-
|
|
66
|
-
}, [
|
|
67
|
-
if (!(!
|
|
68
|
-
return
|
|
58
|
+
const c = M(i);
|
|
59
|
+
return u(() => {
|
|
60
|
+
c.current = i;
|
|
61
|
+
}, [i]), u(() => {
|
|
62
|
+
if (!(!t || !r))
|
|
63
|
+
return s.addListener(
|
|
64
|
+
t,
|
|
69
65
|
r,
|
|
70
|
-
|
|
71
|
-
(...a) => f.current(...a)
|
|
66
|
+
(...a) => c.current(...a)
|
|
72
67
|
);
|
|
73
|
-
}, [
|
|
74
|
-
(a,
|
|
75
|
-
[
|
|
68
|
+
}, [s, t, r]), w(
|
|
69
|
+
(a, f) => s.sendMessage(t, a, f),
|
|
70
|
+
[s, t]
|
|
76
71
|
);
|
|
77
72
|
};
|
|
78
|
-
|
|
79
|
-
children:
|
|
73
|
+
F.propTypes = {
|
|
74
|
+
children: b.node.isRequired
|
|
80
75
|
};
|
|
81
|
-
const
|
|
76
|
+
const j = L.default || L, A = j.iframe`
|
|
82
77
|
width: 100%;
|
|
83
78
|
height: 100%;
|
|
84
79
|
border: none;
|
|
85
|
-
visibility: ${(
|
|
86
|
-
`,
|
|
87
|
-
}, ...
|
|
88
|
-
const a =
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}, [
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
80
|
+
visibility: ${(t) => t.isready ? "visible" : "hidden"};
|
|
81
|
+
`, T = ({ id: t, src: r, path: i, onInit: s = () => {
|
|
82
|
+
}, ...c }) => {
|
|
83
|
+
const a = M(), [f, n] = m(), [e, d] = m(0), { registerFrame: o, sendMessage: l, testIsEmbeddable: h } = x(
|
|
84
|
+
R
|
|
85
|
+
), [v, P] = m(!0), [y, C] = m();
|
|
86
|
+
return u(() => {
|
|
87
|
+
i !== void 0 && C(i);
|
|
88
|
+
}, [i]), u(() => {
|
|
89
|
+
y === void 0 || !e || l(t, "navigate", { path: y });
|
|
90
|
+
}, [y, e, t, l]), u(() => {
|
|
91
|
+
!r || !t || !a || (async () => {
|
|
92
|
+
const g = h(r);
|
|
93
|
+
if (P(g), g) {
|
|
94
|
+
const I = new URL(r);
|
|
95
|
+
o(t, I.origin, a), s(), n(`${I.origin}/mm-embedded-interop?id=${t}`);
|
|
98
96
|
return;
|
|
99
97
|
}
|
|
100
98
|
})();
|
|
101
|
-
}, [
|
|
99
|
+
}, [r, t, o, a]), Z(t, "ready", () => d((g) => g + 1)), v ? /* @__PURE__ */ E.createElement(A, { isready: e, ref: a, src: f, ...c }) : /* @__PURE__ */ E.createElement(
|
|
102
100
|
"div",
|
|
103
101
|
{
|
|
104
102
|
style: {
|
|
@@ -108,18 +106,19 @@ const F = L.default || L, U = F.iframe`
|
|
|
108
106
|
justifyContent: "center"
|
|
109
107
|
}
|
|
110
108
|
},
|
|
111
|
-
/* @__PURE__ */
|
|
109
|
+
/* @__PURE__ */ E.createElement("div", { style: { textAlign: "center" } }, "Application must adhere to the MM App Manifest specification to be embedded.")
|
|
112
110
|
);
|
|
113
111
|
};
|
|
114
|
-
|
|
115
|
-
id:
|
|
116
|
-
src:
|
|
117
|
-
path:
|
|
118
|
-
onInit:
|
|
112
|
+
T.propTypes = {
|
|
113
|
+
id: b.string,
|
|
114
|
+
src: b.string.isRequired,
|
|
115
|
+
path: b.string,
|
|
116
|
+
onInit: b.func
|
|
119
117
|
};
|
|
120
118
|
export {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
119
|
+
T as MMEmbeddableZone,
|
|
120
|
+
R as MMEmbeddableZoneContext,
|
|
121
|
+
F as MMEmbeddableZoneProvider,
|
|
122
|
+
U as testIsEmbeddable,
|
|
123
|
+
Z as useEmbeddedEffect
|
|
125
124
|
};
|
|
@@ -1,6 +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,
|
|
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,l,M){"use strict";const E=e.createContext(null),w=({children:n})=>{const[r,u]=e.useState({}),i=e.useRef({}),c=e.useCallback((s,t,a)=>{u(d=>({...d,[s]:{origin:t,frameRef:a}}))},[]);e.useEffect(()=>{const s=t=>{var d,b;if(!((d=t==null?void 0:t.data)!=null&&d.instanceId))return;(((b=i.current[t.data.instanceId])==null?void 0:b[t.data.type])||[]).forEach(y=>y(t.data))};return window.addEventListener("message",s),()=>{window.removeEventListener("message",s)}},[r]);const f=e.useCallback((s,t,a)=>{const d=r[s];if(!d){console.warn(`Origin not set for id: ${s}. Unable to send message.`);return}d.frameRef.current.contentWindow.postMessage({type:t,...a,instanceId:s},d.origin)},[r]),m={testIsEmbeddable:I,registerFrame:c,sendMessage:f,addListener:(s,t,a)=>(i.current[s]||(i.current[s]={}),i.current[s][t]||(i.current[s][t]=[]),i.current[s][t].push(a),()=>{i.current[s][t]=i.current[s][t].filter(d=>d!==a)})};return e.createElement(E.Provider,{value:m},n)},g={},I=async n=>{try{const r=new URL(n).origin;if(g[r]!==void 0)return g[r];if(!(await(await fetch(`${r}/mm-app-manifest.json`)).json()).version)throw new Error("Invalid manifest");return g[r]=!0,!0}catch{}return g[origin]=!1,!1},C=(n,r,u)=>{const i=e.useContext(E);if(i===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const c=e.useRef(u);return e.useEffect(()=>{c.current=u},[u]),e.useEffect(()=>{if(!(!n||!r))return i.addListener(n,r,(...f)=>c.current(...f))},[i,n,r]),e.useCallback((f,m)=>i.sendMessage(n,f,m),[i,n])};w.propTypes={children:l.node.isRequired};const S=(M.default||M).iframe`
|
|
2
2
|
width: 100%;
|
|
3
3
|
height: 100%;
|
|
4
4
|
border: none;
|
|
5
|
-
visibility: ${
|
|
6
|
-
`,v=({id:
|
|
5
|
+
visibility: ${n=>n.isready?"visible":"hidden"};
|
|
6
|
+
`,v=({id:n,src:r,path:u,onInit:i=()=>{},...c})=>{const f=e.useRef(),[m,s]=e.useState(),[t,a]=e.useState(0),{registerFrame:d,sendMessage:b,testIsEmbeddable:y}=e.useContext(E),[Z,x]=e.useState(!0),[h,j]=e.useState();return e.useEffect(()=>{u!==void 0&&j(u)},[u]),e.useEffect(()=>{h===void 0||!t||b(n,"navigate",{path:h})},[h,t,n,b]),e.useEffect(()=>{!r||!n||!f||(async()=>{const p=y(r);if(x(p),p){const L=new URL(r);d(n,L.origin,f),i(),s(`${L.origin}/mm-embedded-interop?id=${n}`);return}})()},[r,n,d,f]),C(n,"ready",()=>a(p=>p+1)),Z?e.createElement(S,{isready:t,ref:f,src:m,...c}):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."))};v.propTypes={id:l.string,src:l.string.isRequired,path:l.string,onInit:l.func},o.MMEmbeddableZone=v,o.MMEmbeddableZoneContext=E,o.MMEmbeddableZoneProvider=w,o.testIsEmbeddable=I,o.useEmbeddedEffect=C,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@machinemetrics/mm-react-embeddable",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.13",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"module": "dist/mm-react-embeddable.es.js",
|
|
6
6
|
"main": "dist/mm-react-embeddable.cjs.js",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"node": ">=18"
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|
|
19
|
-
"build": "vite build
|
|
19
|
+
"build": "vite build",
|
|
20
20
|
"start": "vite build --watch"
|
|
21
21
|
},
|
|
22
22
|
"files": [
|