@financial-times/custom-code-component 1.1.1 → 1.1.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,4 +1,4 @@
|
|
|
1
|
-
class
|
|
1
|
+
class f extends HTMLElement {
|
|
2
2
|
constructor() {
|
|
3
3
|
super(...arguments), this.RESERVED_ATTRS = /* @__PURE__ */ new Set([
|
|
4
4
|
"iframe",
|
|
@@ -15,60 +15,58 @@ class l extends HTMLElement {
|
|
|
15
15
|
return ["path", "version", "env", "data-component-props"];
|
|
16
16
|
}
|
|
17
17
|
async mount() {
|
|
18
|
-
var r;
|
|
19
|
-
const e = this.getAttribute("path"), s = this.
|
|
18
|
+
var a, r, m;
|
|
19
|
+
const e = this.getAttribute("path"), s = this.getAttribute("version"), d = (a = this.getAttribute("env")) == null ? void 0 : a.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 [o,
|
|
25
|
-
if (!o || !
|
|
24
|
+
const [o, c, i] = e.split("/").reverse();
|
|
25
|
+
if (!o || !c || !i)
|
|
26
26
|
return;
|
|
27
|
-
const
|
|
27
|
+
const l = d ? `http://localhost:5173/src/${o}/index.jsx` : `https://www.ft.com/__component/${i}/${c}${s ? `@${s}` : "@latest"}/${o}/${o}.js`;
|
|
28
28
|
try {
|
|
29
29
|
const n = await import(
|
|
30
30
|
/* webpackIgnore: true */
|
|
31
|
-
|
|
31
|
+
l
|
|
32
32
|
/* @vite-ignore */
|
|
33
|
-
),
|
|
33
|
+
), h = 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 (this.hasAttribute("iframe")) {
|
|
36
|
+
if ((r = this.shadowRoot) == null || r.replaceChildren(), this.hasAttribute("iframe")) {
|
|
37
37
|
const t = document.createElement("iframe");
|
|
38
38
|
t.addEventListener("load", () => {
|
|
39
|
-
const
|
|
39
|
+
const p = n.default(
|
|
40
40
|
t.contentDocument,
|
|
41
|
-
{ ...
|
|
41
|
+
{ ...u, data: h },
|
|
42
42
|
...this.children
|
|
43
43
|
);
|
|
44
|
-
|
|
45
|
-
}), this.
|
|
46
|
-
} else
|
|
47
|
-
this.
|
|
48
|
-
const t = await n.default(
|
|
44
|
+
p && (this.unmount = p);
|
|
45
|
+
}), (m = this.shadowRoot) == null || m.append(t);
|
|
46
|
+
} else
|
|
47
|
+
this.unmount = await n.default(
|
|
49
48
|
this.shadowRoot,
|
|
50
|
-
{ ...
|
|
49
|
+
{ ...u, data: h },
|
|
51
50
|
...this.children
|
|
52
51
|
);
|
|
53
|
-
t && (this.unmount = t);
|
|
54
|
-
}
|
|
55
52
|
} catch (n) {
|
|
56
53
|
console.info(`<custom-code-component> uncaught error from ${e}: `, n);
|
|
57
54
|
}
|
|
58
55
|
}
|
|
59
56
|
connectedCallback() {
|
|
60
|
-
this.
|
|
57
|
+
const e = this.getAttribute("shadow-open") == "false" ? "closed" : "open";
|
|
58
|
+
this.attachShadow({ mode: e }), this.mount();
|
|
61
59
|
}
|
|
62
60
|
attributeChangedCallback() {
|
|
63
61
|
this.mount();
|
|
64
62
|
}
|
|
65
63
|
disconnectedCallback() {
|
|
66
64
|
const e = this.getAttribute("path");
|
|
67
|
-
console.info(`<custom-code-component:${e}> disconnected`), this.unmount();
|
|
65
|
+
console.info(`<custom-code-component:${e}> disconnected`), typeof this.unmount == "function" && this.unmount();
|
|
68
66
|
}
|
|
69
67
|
}
|
|
70
|
-
const
|
|
71
|
-
customElements && !customElements.get("custom-code-component") &&
|
|
68
|
+
const b = () => customElements.define("custom-code-component", f);
|
|
69
|
+
customElements && !customElements.get("custom-code-component") && b();
|
|
72
70
|
export {
|
|
73
|
-
|
|
71
|
+
b as init
|
|
74
72
|
};
|
package/package.json
CHANGED
|
@@ -24,7 +24,6 @@ class FTCustomCodeComponent extends HTMLElement {
|
|
|
24
24
|
|
|
25
25
|
async mount() {
|
|
26
26
|
const path = this.getAttribute("path");
|
|
27
|
-
const mode = this.hasAttribute("shadow-open") ? "open" : "closed";
|
|
28
27
|
const componentVersionRange = this.getAttribute("version");
|
|
29
28
|
const useLocalVersion = this.getAttribute("env")
|
|
30
29
|
?.toLowerCase()
|
|
@@ -60,6 +59,9 @@ class FTCustomCodeComponent extends HTMLElement {
|
|
|
60
59
|
.map((attribute) => [attribute.name, attribute.value])
|
|
61
60
|
);
|
|
62
61
|
|
|
62
|
+
// Clear old children
|
|
63
|
+
this.shadowRoot?.replaceChildren();
|
|
64
|
+
|
|
63
65
|
if (this.hasAttribute("iframe")) {
|
|
64
66
|
const mountPoint = document.createElement("iframe");
|
|
65
67
|
|
|
@@ -73,18 +75,13 @@ class FTCustomCodeComponent extends HTMLElement {
|
|
|
73
75
|
if (unmount) this.unmount = unmount;
|
|
74
76
|
});
|
|
75
77
|
|
|
76
|
-
this.
|
|
78
|
+
this.shadowRoot?.append(mountPoint);
|
|
77
79
|
} else {
|
|
78
|
-
|
|
79
|
-
else this.shadowRoot.innerHTML = "";
|
|
80
|
-
|
|
81
|
-
const unmount = await App.default(
|
|
80
|
+
this.unmount = await App.default(
|
|
82
81
|
this.shadowRoot,
|
|
83
82
|
{ ...extraProps, data },
|
|
84
83
|
...this.children
|
|
85
84
|
);
|
|
86
|
-
|
|
87
|
-
if (unmount) this.unmount = unmount;
|
|
88
85
|
}
|
|
89
86
|
} catch (e) {
|
|
90
87
|
console.info(`<custom-code-component> uncaught error from ${path}: `, e);
|
|
@@ -92,6 +89,10 @@ class FTCustomCodeComponent extends HTMLElement {
|
|
|
92
89
|
}
|
|
93
90
|
|
|
94
91
|
connectedCallback() {
|
|
92
|
+
const mode =
|
|
93
|
+
this.getAttribute("shadow-open") == "false" ? "closed" : "open";
|
|
94
|
+
|
|
95
|
+
this.attachShadow({ mode });
|
|
95
96
|
this.mount();
|
|
96
97
|
}
|
|
97
98
|
|
|
@@ -102,7 +103,7 @@ class FTCustomCodeComponent extends HTMLElement {
|
|
|
102
103
|
disconnectedCallback() {
|
|
103
104
|
const path = this.getAttribute("path");
|
|
104
105
|
console.info(`<custom-code-component:${path}> disconnected`);
|
|
105
|
-
this.unmount();
|
|
106
|
+
if (typeof this.unmount === "function") this.unmount();
|
|
106
107
|
}
|
|
107
108
|
}
|
|
108
109
|
|