@rettangoli/fe 1.1.1 → 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/README.md +1 -1
- package/package.json +1 -1
- package/src/web/componentDom.js +65 -6
package/README.md
CHANGED
package/package.json
CHANGED
package/src/web/componentDom.js
CHANGED
|
@@ -12,13 +12,61 @@ const COMMON_LINK_STYLE_TEXT = `
|
|
|
12
12
|
}
|
|
13
13
|
`;
|
|
14
14
|
|
|
15
|
+
const RENDER_TARGET_ATTR = "data-rtgl-render-target";
|
|
16
|
+
const RENDER_TARGET_FLAG = "__rtglRenderTarget";
|
|
17
|
+
|
|
18
|
+
const hasRenderTargetAttr = (node) => {
|
|
19
|
+
if (!node || typeof node !== "object") {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (typeof node.getAttribute === "function") {
|
|
24
|
+
return node.getAttribute(RENDER_TARGET_ATTR) !== null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return node[RENDER_TARGET_FLAG] === true;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const markRenderTarget = (node) => {
|
|
31
|
+
if (!node || typeof node !== "object") {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (typeof node.setAttribute === "function") {
|
|
36
|
+
node.setAttribute(RENDER_TARGET_ATTR, "");
|
|
37
|
+
} else {
|
|
38
|
+
node[RENDER_TARGET_FLAG] = true;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const findExistingRenderTarget = (shadow) => {
|
|
43
|
+
if (!shadow || typeof shadow !== "object") {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (typeof shadow.querySelector === "function") {
|
|
48
|
+
return shadow.querySelector(`[${RENDER_TARGET_ATTR}]`) ?? shadow.firstElementChild;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (Array.isArray(shadow.childNodes)) {
|
|
52
|
+
return shadow.childNodes.find(hasRenderTargetAttr) ?? shadow.childNodes[0];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (Array.isArray(shadow.children)) {
|
|
56
|
+
return shadow.children.find(hasRenderTargetAttr) ?? shadow.children[0];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return shadow.firstElementChild;
|
|
60
|
+
};
|
|
61
|
+
|
|
15
62
|
export const initializeComponentDom = ({
|
|
16
63
|
host,
|
|
17
64
|
cssText,
|
|
18
65
|
createStyleSheet = () => new CSSStyleSheet(),
|
|
19
66
|
createElement = (tagName) => document.createElement(tagName),
|
|
20
67
|
}) => {
|
|
21
|
-
const
|
|
68
|
+
const existingShadow = host.shadowRoot;
|
|
69
|
+
const shadow = existingShadow ?? host.attachShadow({ mode: "open" });
|
|
22
70
|
|
|
23
71
|
const commonStyleSheet = createStyleSheet();
|
|
24
72
|
commonStyleSheet.replaceSync(COMMON_LINK_STYLE_TEXT);
|
|
@@ -33,11 +81,22 @@ export const initializeComponentDom = ({
|
|
|
33
81
|
|
|
34
82
|
shadow.adoptedStyleSheets = adoptedStyleSheets;
|
|
35
83
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
84
|
+
let renderTarget = findExistingRenderTarget(shadow);
|
|
85
|
+
|
|
86
|
+
if (!renderTarget) {
|
|
87
|
+
renderTarget = createElement("div");
|
|
88
|
+
renderTarget.style.cssText = "display: contents;";
|
|
89
|
+
markRenderTarget(renderTarget);
|
|
90
|
+
shadow.appendChild(renderTarget);
|
|
91
|
+
} else {
|
|
92
|
+
renderTarget.style.cssText = "display: contents;";
|
|
93
|
+
if (!hasRenderTargetAttr(renderTarget)) {
|
|
94
|
+
markRenderTarget(renderTarget);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (renderTarget.parentNode !== shadow) {
|
|
99
|
+
shadow.appendChild(renderTarget);
|
|
41
100
|
}
|
|
42
101
|
host.style.display = "contents";
|
|
43
102
|
|