enigmatic 0.31.0 → 0.33.0
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/public/client.js +71 -128
- package/public/index2.html +8 -2
package/package.json
CHANGED
package/public/client.js
CHANGED
|
@@ -1,137 +1,80 @@
|
|
|
1
|
-
window
|
|
2
|
-
window.$ = document.querySelector.bind(document)
|
|
3
|
-
window.$$ = document.querySelectorAll.bind(document)
|
|
4
|
-
window.$c = (selector) => $0.closest(selector);
|
|
1
|
+
const D = document, W = window, Enc = encodeURIComponent;
|
|
5
2
|
|
|
6
|
-
//
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
});
|
|
18
|
-
}
|
|
3
|
+
// 1. Unified Render Logic (Handles both State & Custom Elements)
|
|
4
|
+
const ren = async (el, v) => {
|
|
5
|
+
const f = W.custom?.[el.tagName.toLowerCase()];
|
|
6
|
+
if (f) {
|
|
7
|
+
const dataAttr = el.getAttribute('data');
|
|
8
|
+
const val = v !== undefined ? v : (dataAttr ? W.state[dataAttr] : undefined);
|
|
9
|
+
try { el.innerHTML = await (f.render || f)(val) } catch(e) { console.error(e) }
|
|
10
|
+
}
|
|
11
|
+
};
|
|
19
12
|
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (typeof value === 'function' || (value && typeof value.render === 'function')) {
|
|
26
|
-
setTimeout(() => {
|
|
27
|
-
$$(prop).forEach(async el => {
|
|
28
|
-
try {
|
|
29
|
-
if (typeof value === 'function') {
|
|
30
|
-
el.innerHTML = await value();
|
|
31
|
-
} else if (value && typeof value.render === 'function') {
|
|
32
|
-
el.innerHTML = value.render();
|
|
33
|
-
}
|
|
34
|
-
} catch (e) {
|
|
35
|
-
console.error('Custom element error:', e);
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}, 0);
|
|
39
|
-
}
|
|
13
|
+
// 2. Proxies setup
|
|
14
|
+
const cProx = new Proxy({}, {
|
|
15
|
+
set(t, p, v) {
|
|
16
|
+
t[p] = v;
|
|
17
|
+
setTimeout(() => W.$$(p).forEach(el => ren(el)), 0);
|
|
40
18
|
return true;
|
|
41
19
|
}
|
|
42
20
|
});
|
|
43
|
-
Object.defineProperty(
|
|
44
|
-
get: () => customProxy,
|
|
45
|
-
set: (val) => {
|
|
46
|
-
Object.keys(val || {}).forEach(key => customProxy[key] = val[key]);
|
|
47
|
-
window.initCustomElements();
|
|
48
|
-
},
|
|
49
|
-
configurable: true
|
|
50
|
-
});
|
|
21
|
+
Object.defineProperty(W, 'custom', { get: () => cProx, set: v => Object.assign(cProx, v) });
|
|
51
22
|
|
|
52
|
-
|
|
53
|
-
set(
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const f = window.custom?.[el.tagName.toLowerCase()];
|
|
58
|
-
if (!f) return;
|
|
59
|
-
if(typeof f === 'function') {
|
|
60
|
-
el.innerHTML = f(value);
|
|
61
|
-
} else if (f && typeof f.render === 'function') {
|
|
62
|
-
el.innerHTML = f.render(value);
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
return true
|
|
23
|
+
const sProx = new Proxy({}, {
|
|
24
|
+
set(o, p, v) {
|
|
25
|
+
o[p] = v;
|
|
26
|
+
W.$$(`[data="${p}"]`).forEach(el => ren(el, v));
|
|
27
|
+
return true;
|
|
66
28
|
}
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
})
|
|
94
|
-
return await res.json()
|
|
95
|
-
}
|
|
96
|
-
window.list = async function() {
|
|
97
|
-
const res = await fetch(`${window.api_url}`, {
|
|
98
|
-
method: 'PROPFIND'
|
|
99
|
-
})
|
|
100
|
-
return await res.json()
|
|
101
|
-
}
|
|
102
|
-
window.download = async function(key) {
|
|
103
|
-
try {
|
|
104
|
-
console.log('Downloading with method DOWNLOAD:', key);
|
|
105
|
-
const res = await fetch(`${window.api_url}/${encodeURIComponent(key)}`, { method: 'PATCH' });
|
|
106
|
-
console.log('Response:', key, res.status, res.statusText);
|
|
107
|
-
if (!res.ok) throw new Error('Download failed');
|
|
108
|
-
const blob = await res.blob();
|
|
109
|
-
const url = URL.createObjectURL(blob);
|
|
110
|
-
const a = document.createElement('a');
|
|
111
|
-
a.href = url;
|
|
112
|
-
a.download = key;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// 3. API & DOM Helpers
|
|
32
|
+
const req = (m, k, b) => fetch(`${W.api_url}/${k ? Enc(k) : ''}`, {
|
|
33
|
+
method: m, body: b instanceof Blob || typeof b === 'string' ? b : JSON.stringify(b)
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
Object.assign(W, {
|
|
37
|
+
$: s => D.querySelector(s),
|
|
38
|
+
$$: s => D.querySelectorAll(s),
|
|
39
|
+
$c: s => $0.closest(s),
|
|
40
|
+
state: sProx,
|
|
41
|
+
get: k => req('GET', k).then(r => r.json()),
|
|
42
|
+
set: (k, v) => req('POST', k, v).then(r => r.json()),
|
|
43
|
+
put: (k, v) => req('PUT', k, v).then(r => r.json()),
|
|
44
|
+
delete: k => req('DELETE', k).then(r => r.json()),
|
|
45
|
+
purge: k => req('PURGE', k).then(r => r.json()),
|
|
46
|
+
list: () => req('PROPFIND').then(r => r.json()),
|
|
47
|
+
login: () => W.location.href = `${W.api_url}/login`,
|
|
48
|
+
logout: () => W.location.href = `${W.api_url}/logout`,
|
|
49
|
+
download: async (k) => {
|
|
50
|
+
const r = await req('PATCH', k);
|
|
51
|
+
if (!r.ok) throw new Error('Download failed');
|
|
52
|
+
const a = D.createElement('a');
|
|
53
|
+
a.href = URL.createObjectURL(await r.blob());
|
|
54
|
+
a.download = k;
|
|
113
55
|
a.click();
|
|
114
|
-
URL.revokeObjectURL(
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
window.login = function() {
|
|
121
|
-
window.location.href = `${window.api_url}/login`
|
|
122
|
-
}
|
|
123
|
-
window.logout = function() {
|
|
124
|
-
window.location.href = `${window.api_url}/logout`
|
|
125
|
-
}
|
|
56
|
+
URL.revokeObjectURL(a.href);
|
|
57
|
+
},
|
|
58
|
+
initCustomElements: () => Object.keys(W.custom || {}).forEach(t => W.$$(t).forEach(el => ren(el)))
|
|
59
|
+
});
|
|
126
60
|
|
|
127
|
-
//
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
61
|
+
// 4. Initialization & Observers
|
|
62
|
+
const boot = () => {
|
|
63
|
+
W.initCustomElements();
|
|
64
|
+
new MutationObserver((mutations) => {
|
|
65
|
+
mutations.forEach(m => {
|
|
66
|
+
m.addedNodes.forEach(node => {
|
|
67
|
+
if (node.nodeType === 1) { // Element node
|
|
68
|
+
const tag = node.tagName?.toLowerCase();
|
|
69
|
+
if (tag && W.custom?.[tag]) ren(node);
|
|
70
|
+
// Also check children
|
|
71
|
+
node.querySelectorAll && Array.from(node.querySelectorAll('*')).forEach(child => {
|
|
72
|
+
const childTag = child.tagName?.toLowerCase();
|
|
73
|
+
if (childTag && W.custom?.[childTag]) ren(child);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}).observe(D.body, { childList: true, subtree: true });
|
|
79
|
+
};
|
|
80
|
+
D.readyState === 'loading' ? D.addEventListener('DOMContentLoaded', boot) : boot();
|
package/public/index2.html
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
<script src='
|
|
1
|
+
<script src='client.js'></script>
|
|
2
2
|
|
|
3
|
-
<
|
|
3
|
+
<script>
|
|
4
|
+
window.api_url = 'http://localhost:3000';
|
|
5
|
+
custom.hw = (data) => `Hello ${data}`;
|
|
6
|
+
state.message = "World";
|
|
7
|
+
</script>
|
|
8
|
+
|
|
9
|
+
<hw data="message"></hw>
|