@blackbirdjs/component 0.1.2 → 0.1.4
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/package.json +1 -1
- package/src/component.js +49 -10
package/package.json
CHANGED
package/src/component.js
CHANGED
|
@@ -10,6 +10,9 @@ export class BlackbirdComponent extends HTMLElement {
|
|
|
10
10
|
// Add an internal register array to capture cleanup tokens safely
|
|
11
11
|
#unsubscribers = [];
|
|
12
12
|
|
|
13
|
+
// A strict private guard to track if mounting has already occurred
|
|
14
|
+
#isMounted = false;
|
|
15
|
+
|
|
13
16
|
constructor() {
|
|
14
17
|
super();
|
|
15
18
|
this.attachShadow({ mode: 'open' });
|
|
@@ -26,25 +29,61 @@ export class BlackbirdComponent extends HTMLElement {
|
|
|
26
29
|
this.localStore.set(cleanKey, newValue);
|
|
27
30
|
}
|
|
28
31
|
|
|
29
|
-
connectedCallback() {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
async connectedCallback() {
|
|
33
|
+
// MOUNT GUARD: If this instance has already run its template loading setup, block it instantly.
|
|
34
|
+
if (this.#isMounted) return;
|
|
35
|
+
this.#isMounted = true;
|
|
36
|
+
|
|
37
|
+
// Check if the developer provided an external template file path string
|
|
38
|
+
const path = this.constructor.templatePath;
|
|
39
|
+
|
|
40
|
+
if (path) {
|
|
41
|
+
try {
|
|
42
|
+
const response = await fetch(path);
|
|
43
|
+
|
|
44
|
+
// If the path is broken (404, 500, etc.), do not parse it!
|
|
45
|
+
if (!response) {
|
|
46
|
+
throw new Error(`Server responded with status: ${response.status}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const htmlText = await response.text();
|
|
50
|
+
|
|
51
|
+
if (htmlText.includes('<html') && htmlText.includes(this.tagName.toLowerCase())) {
|
|
52
|
+
throw new Error(`SPA Fallback detected. Dev server returned index.html instead of template asset.`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Convert the fetched raw text string into browser-executable DOM elements
|
|
56
|
+
const parser = new DOMParser();
|
|
57
|
+
const doc = parser.parseFromString(htmlText, 'text/html');
|
|
32
58
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
59
|
+
// Append it cleanly inside the isolated Shadow DOM
|
|
60
|
+
this.shadowRoot.innerHTML = doc.documentElement.innerHTML;
|
|
61
|
+
} catch (err) {
|
|
62
|
+
console.error(`[Blackbird] Failed to fetch external template at: ${path}\n`, err);
|
|
63
|
+
}
|
|
36
64
|
}
|
|
37
65
|
|
|
38
|
-
//
|
|
39
|
-
|
|
40
|
-
|
|
66
|
+
// Fall back to <template id="..."> matching rule if no path exists
|
|
67
|
+
if (!this.shadowRoot.innerHTML) {
|
|
68
|
+
const templateId = this.getAttribute('template');
|
|
69
|
+
const template = document.getElementById(templateId);
|
|
70
|
+
|
|
71
|
+
if (!template) {
|
|
72
|
+
console.error(`[Blackbird] Template with id "${templateId}" not found.`);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Changed contentEditable to template.content
|
|
77
|
+
const clone = template.content.cloneNode(true);
|
|
78
|
+
this.shadowRoot.appendChild(clone);
|
|
79
|
+
}
|
|
41
80
|
|
|
42
81
|
this._hydrateInitialAttributes();
|
|
43
82
|
this._compileDOM();
|
|
44
83
|
}
|
|
45
84
|
|
|
46
85
|
// Automatically clean up memory when the component leaves the screen
|
|
47
|
-
disconnectedCallback() {
|
|
86
|
+
async disconnectedCallback() {
|
|
48
87
|
this.#unsubscribers.forEach(unsubscribe => unsubscribe());
|
|
49
88
|
this.#unsubscribers = [];
|
|
50
89
|
}
|