@financial-times/custom-code-component 1.1.3 → 1.1.5
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/dist/custom-code-component.js +32 -24
- package/package.json +1 -1
- package/src/custom-code-component.ts +26 -12
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
class
|
|
1
|
+
class g extends HTMLElement {
|
|
2
2
|
constructor() {
|
|
3
3
|
super(...arguments), this.RESERVED_ATTRS = /* @__PURE__ */ new Set([
|
|
4
4
|
"iframe",
|
|
@@ -9,48 +9,50 @@ class f extends HTMLElement {
|
|
|
9
9
|
"shadow-open",
|
|
10
10
|
"env"
|
|
11
11
|
]), this.unmount = () => {
|
|
12
|
-
};
|
|
12
|
+
}, this.channel = new MessageChannel();
|
|
13
13
|
}
|
|
14
14
|
static get observedAttributes() {
|
|
15
15
|
return ["path", "version", "env", "data-component-props"];
|
|
16
16
|
}
|
|
17
17
|
async mount() {
|
|
18
|
-
var
|
|
19
|
-
const e = this.getAttribute("path"),
|
|
18
|
+
var r, h, m;
|
|
19
|
+
const e = this.getAttribute("path"), a = this.getAttribute("version"), l = (r = this.getAttribute("env")) == null ? void 0 : r.toLowerCase().startsWith("d");
|
|
20
20
|
if (!e)
|
|
21
21
|
throw new Error(
|
|
22
22
|
"path attribute not specified in <custom-code-component>"
|
|
23
23
|
);
|
|
24
|
-
const [
|
|
25
|
-
if (!
|
|
24
|
+
const [n, i, c] = e.split("/").reverse();
|
|
25
|
+
if (!n || !i || !c)
|
|
26
26
|
return;
|
|
27
|
-
const
|
|
27
|
+
const f = l ? `http://localhost:5173/src/${n}/index.jsx` : `https://www.ft.com/__component/${c}/${i}${a ? `@${a}` : "@latest"}/${n}/${n}.js`;
|
|
28
28
|
try {
|
|
29
|
-
const
|
|
29
|
+
const s = await import(
|
|
30
30
|
/* webpackIgnore: true */
|
|
31
|
-
|
|
31
|
+
f
|
|
32
32
|
/* @vite-ignore */
|
|
33
|
-
),
|
|
33
|
+
), p = JSON.parse(this.getAttribute("data-component-props")), u = Object.fromEntries(
|
|
34
34
|
[...this.attributes].filter((t) => !this.RESERVED_ATTRS.has(t.name)).map((t) => [t.name, t.value])
|
|
35
35
|
);
|
|
36
|
-
if ((
|
|
36
|
+
if ((h = this.shadowRoot) == null || h.replaceChildren(), this.hasAttribute("iframe")) {
|
|
37
37
|
const t = document.createElement("iframe");
|
|
38
38
|
t.addEventListener("load", () => {
|
|
39
|
-
const
|
|
39
|
+
const { unmount: o, onmessage: d } = s.default(
|
|
40
40
|
t.contentDocument,
|
|
41
|
-
{ ...u, data:
|
|
41
|
+
{ ...u, data: p, port: this.channel.port2 },
|
|
42
42
|
...this.children
|
|
43
|
-
);
|
|
44
|
-
|
|
43
|
+
) || {};
|
|
44
|
+
o && (this.unmount = o), d && (this.onmessage = d);
|
|
45
45
|
}), (m = this.shadowRoot) == null || m.append(t);
|
|
46
|
-
} else
|
|
47
|
-
|
|
46
|
+
} else {
|
|
47
|
+
const { unmount: t, onmessage: o } = await s.default(
|
|
48
48
|
this.shadowRoot,
|
|
49
|
-
{ ...u, data:
|
|
49
|
+
{ ...u, data: p, port: this.channel.port2 },
|
|
50
50
|
...this.children
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
) || {};
|
|
52
|
+
t && (this.unmount = t), o && (this.onmessage = o);
|
|
53
|
+
}
|
|
54
|
+
} catch (s) {
|
|
55
|
+
console.info(`<custom-code-component> uncaught error from ${e}: `, s);
|
|
54
56
|
}
|
|
55
57
|
}
|
|
56
58
|
connectedCallback() {
|
|
@@ -64,9 +66,15 @@ class f extends HTMLElement {
|
|
|
64
66
|
const e = this.getAttribute("path");
|
|
65
67
|
console.info(`<custom-code-component:${e}> disconnected`), typeof this.unmount == "function" && this.unmount();
|
|
66
68
|
}
|
|
69
|
+
onmessage() {
|
|
70
|
+
}
|
|
71
|
+
// I'm honestly not sure what to do with this
|
|
72
|
+
postMessage(e) {
|
|
73
|
+
this.channel.port1.postMessage(e);
|
|
74
|
+
}
|
|
67
75
|
}
|
|
68
|
-
const
|
|
69
|
-
customElements && !customElements.get("custom-code-component") &&
|
|
76
|
+
const w = () => customElements.define("custom-code-component", g);
|
|
77
|
+
customElements && !customElements.get("custom-code-component") && w();
|
|
70
78
|
export {
|
|
71
|
-
|
|
79
|
+
w as init
|
|
72
80
|
};
|
package/package.json
CHANGED
|
@@ -20,8 +20,6 @@ class FTCustomCodeComponent extends HTMLElement {
|
|
|
20
20
|
return ["path", "version", "env", "data-component-props"];
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
unmount = () => {};
|
|
24
|
-
|
|
25
23
|
async mount() {
|
|
26
24
|
const path = this.getAttribute("path");
|
|
27
25
|
const componentVersionRange = this.getAttribute("version");
|
|
@@ -66,28 +64,36 @@ class FTCustomCodeComponent extends HTMLElement {
|
|
|
66
64
|
const mountPoint = document.createElement("iframe");
|
|
67
65
|
|
|
68
66
|
mountPoint.addEventListener("load", () => {
|
|
69
|
-
const unmount =
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
67
|
+
const { unmount, onmessage } =
|
|
68
|
+
App.default(
|
|
69
|
+
mountPoint.contentDocument,
|
|
70
|
+
{ ...extraProps, data, port: this.channel.port2 },
|
|
71
|
+
...this.children
|
|
72
|
+
) || {};
|
|
74
73
|
|
|
75
74
|
if (unmount) this.unmount = unmount;
|
|
75
|
+
if (onmessage) this.onmessage = onmessage;
|
|
76
76
|
});
|
|
77
77
|
|
|
78
78
|
this.shadowRoot?.append(mountPoint);
|
|
79
79
|
} else {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
80
|
+
const { unmount, onmessage } =
|
|
81
|
+
(await App.default(
|
|
82
|
+
this.shadowRoot,
|
|
83
|
+
{ ...extraProps, data, port: this.channel.port2 },
|
|
84
|
+
...this.children
|
|
85
|
+
)) || {};
|
|
86
|
+
|
|
87
|
+
if (unmount) this.unmount = unmount;
|
|
88
|
+
if (onmessage) this.onmessage = onmessage;
|
|
85
89
|
}
|
|
86
90
|
} catch (e) {
|
|
87
91
|
console.info(`<custom-code-component> uncaught error from ${path}: `, e);
|
|
88
92
|
}
|
|
89
93
|
}
|
|
90
94
|
|
|
95
|
+
unmount = () => {};
|
|
96
|
+
|
|
91
97
|
connectedCallback() {
|
|
92
98
|
const mode =
|
|
93
99
|
this.getAttribute("shadow-open") == "false" ? "closed" : "open";
|
|
@@ -105,6 +111,14 @@ class FTCustomCodeComponent extends HTMLElement {
|
|
|
105
111
|
console.info(`<custom-code-component:${path}> disconnected`);
|
|
106
112
|
if (typeof this.unmount === "function") this.unmount();
|
|
107
113
|
}
|
|
114
|
+
|
|
115
|
+
channel = new MessageChannel();
|
|
116
|
+
|
|
117
|
+
onmessage() {} // I'm honestly not sure what to do with this
|
|
118
|
+
|
|
119
|
+
postMessage(event: any) {
|
|
120
|
+
this.channel.port1.postMessage(event);
|
|
121
|
+
}
|
|
108
122
|
}
|
|
109
123
|
|
|
110
124
|
// Register the custom element
|