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