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