1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-22 16:55:23 +01:00
deka-dom-el/dist/esm-with-signals.js

397 lines
9.5 KiB
JavaScript
Raw Normal View History

2023-09-13 13:20:00 +02:00
// src/signals-common.js
var x = {
isTextContent(e) {
return typeof e == "string";
},
processReactiveAttribute(e, t, r, n) {
return r;
}
};
function _(e, t = !0) {
return t ? Object.assign(x, e) : (Object.setPrototypeOf(e, x), e);
}
function N(e) {
return x.isPrototypeOf(e) && e !== x ? e : x;
}
// src/helpers.js
2023-09-11 18:32:05 +02:00
function m(e) {
2023-09-13 13:20:00 +02:00
return typeof e > "u";
}
function w(e) {
let t = typeof e;
return t !== "object" ? t : e === null ? "null" : Object.prototype.toString.call(e);
}
function j(e, t) {
if (!e || !(e instanceof AbortSignal))
return !0;
if (!e.aborted)
return e.addEventListener("abort", t), function() {
e.removeEventListener("abort", t);
};
}
2023-09-13 13:20:00 +02:00
// src/dom-common.js
var b = new Map(JSON.parse('[["#text,textContent",true],["HTMLElement,textContent",true],["HTMLElement,className",true]]')), T = { setDelete: $ };
function $(e, t, r) {
Reflect.set(e, t, r), m(r) && e.getAttribute(t) === "undefined" && e.removeAttribute(t);
}
// src/dom.js
2023-09-13 13:20:00 +02:00
var S = "html";
function oe(e) {
return S = e === "svg" ? "http://www.w3.org/2000/svg" : e, {
append(t) {
2023-09-13 13:20:00 +02:00
return S = "html", t;
}
};
}
2023-09-13 13:20:00 +02:00
function ce(e, t, ...r) {
let n = N(this), o;
2023-09-11 18:32:05 +02:00
switch (n.isTextContent(t) && (t = { textContent: t }), !0) {
case typeof e == "function": {
2023-09-11 18:32:05 +02:00
o = e(t || void 0, (i) => i ? (r.unshift(i), void 0) : o);
break;
}
case e === "#text":
2023-09-13 13:20:00 +02:00
o = y(document.createTextNode(""), t);
2023-09-08 20:18:58 +02:00
break;
case e === "<>":
2023-09-13 13:20:00 +02:00
o = y(document.createDocumentFragment(), t);
break;
2023-09-13 13:20:00 +02:00
case S !== "html":
o = y(document.createElementNS(S, e), t);
break;
2023-09-06 14:54:28 +02:00
case !o:
2023-09-13 13:20:00 +02:00
o = y(document.createElement(e), t);
}
2023-09-11 18:32:05 +02:00
return r.forEach((a) => a(o)), o;
}
2023-09-13 13:20:00 +02:00
var { setDelete: D } = T;
function y(e, ...t) {
let r = N(this);
if (!t.length)
return e;
2023-09-13 13:20:00 +02:00
let n = e instanceof SVGElement, o = (n ? W : M).bind(null, e, "Attribute");
2023-09-11 18:32:05 +02:00
return Object.entries(Object.assign({}, ...t)).forEach(function a([i, s]) {
s = r.processReactiveAttribute(e, i, s, a);
let [h] = i;
if (h === "=")
2023-09-11 18:32:05 +02:00
return o(i.slice(1), s);
if (h === ".")
2023-09-13 13:20:00 +02:00
return D(e, i.slice(1), s);
2023-09-11 18:32:05 +02:00
if (typeof s == "object")
switch (i) {
case "style":
2023-09-13 13:20:00 +02:00
return O(s, M.bind(null, e.style, "Property"));
case "dataset":
2023-09-13 13:20:00 +02:00
return O(s, D.bind(null, e.dataset));
case "ariaset":
2023-09-13 13:20:00 +02:00
return O(s, (E, v) => o("aria-" + E, v));
case "classList":
2023-09-13 13:20:00 +02:00
return q(e, s);
default:
2023-09-11 18:32:05 +02:00
return Reflect.set(e, i, s);
}
2023-09-11 18:32:05 +02:00
if (/(aria|data)([A-Z])/.test(i))
return i = i.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(), o(i, s);
switch (i) {
2023-09-08 15:57:51 +02:00
case "href":
2023-09-11 18:32:05 +02:00
return o(i, s);
case "xlink:href":
2023-09-11 18:32:05 +02:00
return o(i, s, "http://www.w3.org/1999/xlink");
case "textContent":
2023-09-11 18:32:05 +02:00
if (!n)
break;
2023-09-11 18:32:05 +02:00
return e.appendChild(document.createTextNode(s));
}
2023-09-13 13:20:00 +02:00
return J(e, i) ? D(e, i, s) : o(i, s);
}), e;
}
2023-09-13 13:20:00 +02:00
function q(e, t) {
return typeof t != "object" || O(
t,
2023-09-11 18:32:05 +02:00
(r, n) => e.classList.toggle(r, n === -1 ? void 0 : !!n)
), e;
}
2023-09-13 13:20:00 +02:00
function se(e) {
return Array.from(e.children).forEach((t) => t.remove()), e;
}
2023-09-13 13:20:00 +02:00
function J(e, t) {
2023-09-11 18:32:05 +02:00
let r = "HTMLElement," + t;
2023-09-13 13:20:00 +02:00
if (e instanceof HTMLElement && b.has(r))
return b.get(r);
2023-09-11 18:32:05 +02:00
let n = e.nodeName + "," + t;
2023-09-13 13:20:00 +02:00
if (b.has(n))
return b.get(n);
let [o, a, i] = H(e, t), s = !m(o.set);
return (!s || a) && b.set(i === HTMLElement.prototype ? r : n, s), s;
2023-09-06 14:54:28 +02:00
}
2023-09-13 13:20:00 +02:00
function H(e, t, r = 0) {
2023-09-06 14:54:28 +02:00
if (e = Object.getPrototypeOf(e), !e)
2023-09-11 18:32:05 +02:00
return [{}, r, e];
let n = Object.getOwnPropertyDescriptor(e, t);
2023-09-13 13:20:00 +02:00
return n ? [n, r, e] : H(e, t, r + 1);
2023-09-06 14:54:28 +02:00
}
2023-09-13 13:20:00 +02:00
function O(e, t) {
2023-09-11 18:32:05 +02:00
return Object.entries(e).forEach(([r, n]) => t(r, n));
}
2023-09-13 13:20:00 +02:00
function M(e, t, r, n) {
return e[(m(n) ? "remove" : "set") + t](r, n);
}
2023-09-13 13:20:00 +02:00
function W(e, t, r, n, o = null) {
return e[(m(n) ? "remove" : "set") + t + "NS"](o, r, n);
}
// src/events.js
2023-09-13 13:20:00 +02:00
function ae(e, t, ...r) {
2023-09-11 18:32:05 +02:00
let n = r.length ? new CustomEvent(t, { detail: r[0] }) : new Event(t);
return e.dispatchEvent(n);
}
2023-09-13 13:20:00 +02:00
function k(e, t, r) {
return function(o) {
2023-09-11 18:32:05 +02:00
return o.addEventListener(e, t, r), o;
};
}
2023-09-13 13:20:00 +02:00
var C = I();
k.connected = function(e, t) {
2023-09-11 18:32:05 +02:00
return function(n) {
2023-09-13 13:20:00 +02:00
return typeof n.connectedCallback == "function" ? (n.addEventListener("dde:connected", e, t), n) : (j(t && t.signal, () => C.offConnected(n, e)) && (n.isConnected ? e(new Event("dde:connected")) : C.onConnected(n, e)), n);
};
};
2023-09-13 13:20:00 +02:00
k.disconnected = function(e, t) {
2023-09-11 18:32:05 +02:00
return function(n) {
2023-09-13 13:20:00 +02:00
return typeof n.disconnectedCallback == "function" ? (n.addEventListener("dde:disconnected", e, t), n) : (j(t && t.signal, () => C.offDisconnected(n, e)) && C.onDisconnected(n, e), n);
};
};
2023-09-13 13:20:00 +02:00
function I() {
2023-09-11 18:32:05 +02:00
let e = /* @__PURE__ */ new Map(), t = !1, r = new MutationObserver(function(c) {
for (let f of c)
if (f.type === "childList") {
if (E(f.addedNodes, !0)) {
2023-09-11 18:32:05 +02:00
i();
continue;
}
2023-09-11 18:32:05 +02:00
v(f.removedNodes, !0) && i();
}
});
return {
onConnected(c, f) {
a(), o(c).connected.push(f);
},
offConnected(c, f) {
if (!e.has(c))
return;
2023-09-11 18:32:05 +02:00
let u = e.get(c), p = u.connected;
p.splice(p.indexOf(f), 1), n(c, u);
},
onDisconnected(c, f) {
a(), o(c).disconnected.push(f);
},
offDisconnected(c, f) {
if (!e.has(c))
return;
2023-09-11 18:32:05 +02:00
let u = e.get(c), p = u.disconnected;
p.splice(p.indexOf(f), 1), n(c, u);
}
};
2023-09-11 18:32:05 +02:00
function n(c, f) {
f.connected.length || f.disconnected.length || (e.delete(c), i());
}
function o(c) {
if (e.has(c))
return e.get(c);
let f = { connected: [], disconnected: [] };
return e.set(c, f), f;
}
function a() {
2023-09-11 18:32:05 +02:00
t || (t = !0, r.observe(document.body, { childList: !0, subtree: !0 }));
}
function i() {
2023-09-11 18:32:05 +02:00
!t || e.size || (t = !1, r.disconnect());
}
function s() {
return new Promise(function(c) {
(requestIdleCallback || requestAnimationFrame)(c);
});
}
async function h(c) {
2023-09-11 18:32:05 +02:00
e.size > 30 && await s();
let f = [];
if (!(c instanceof Node))
return f;
2023-09-07 17:35:23 +02:00
for (let u of e.keys())
u === c || !(u instanceof Node) || c.contains(u) && f.push(u);
return f;
}
function E(c, f) {
2023-09-07 17:35:23 +02:00
for (let u of c) {
if (f && h(u).then(E), !e.has(u))
continue;
2023-09-11 18:32:05 +02:00
let p = e.get(u);
2023-09-13 13:20:00 +02:00
return p.connected.forEach((A) => A(u)), p.connected.length = 0, p.disconnected.length || e.delete(u), !0;
}
return !1;
}
function v(c, f) {
2023-09-07 17:35:23 +02:00
for (let u of c) {
if (f && h(u).then(v), !e.has(u))
continue;
2023-09-11 18:32:05 +02:00
let p = e.get(u);
2023-09-13 13:20:00 +02:00
return p.disconnected.forEach((A) => A(u)), p.connected.length = 0, p.disconnected.length = 0, e.delete(u), !0;
}
return !1;
}
}
// index.js
[HTMLElement, DocumentFragment].forEach((e) => {
let { append: t } = e.prototype;
2023-09-11 18:32:05 +02:00
e.prototype.append = function(...r) {
return t.apply(this, r), this;
};
});
// src/signals-lib.js
var d = Symbol.for("Signal");
2023-09-13 13:20:00 +02:00
function L(e) {
try {
return Reflect.has(e, d);
} catch {
return !1;
}
}
2023-09-11 18:32:05 +02:00
var l = /* @__PURE__ */ new WeakMap();
function g(e, t) {
if (typeof e != "function")
2023-09-13 13:20:00 +02:00
return z(e, t);
if (L(e))
return e;
2023-09-13 13:20:00 +02:00
let r = z(""), n = () => r(e());
return l.set(n, /* @__PURE__ */ new Set([r])), G(n), r;
}
2023-09-11 18:32:05 +02:00
g.action = function(e, t, ...r) {
let n = e[d], { actions: o } = n;
if (!o || !Reflect.has(o, t))
throw new Error(`'${e}' has no action with name '${t}'!`);
2023-09-11 18:32:05 +02:00
if (o[t].apply(n, r), n.skip)
return Reflect.deleteProperty(n, "skip");
n.listeners.forEach((a) => a(n.value));
};
2023-09-11 18:32:05 +02:00
g.on = function e(t, r, n = {}) {
let { signal: o } = n;
2023-09-07 17:35:23 +02:00
if (!(o && o.aborted)) {
if (Array.isArray(t))
2023-09-11 18:32:05 +02:00
return t.forEach((a) => e(a, r, n));
2023-09-13 13:20:00 +02:00
P(t, r), o && o.addEventListener("abort", () => U(t, r));
2023-09-07 17:35:23 +02:00
}
};
2023-09-11 18:32:05 +02:00
g.symbols = {
signal: d,
2023-09-07 17:35:23 +02:00
onclear: Symbol.for("Signal.onclear")
};
2023-09-11 18:32:05 +02:00
g.clear = function(...e) {
for (let r of e) {
Reflect.deleteProperty(r, "toJSON");
let n = r[d], { onclear: o } = g.symbols;
n.actions && n.actions[o] && n.actions[o].call(n), t(r, n), Reflect.deleteProperty(r, d);
}
2023-09-11 18:32:05 +02:00
function t(r, n) {
n.listeners.forEach((o) => {
if (n.listeners.delete(o), !l.has(o))
return;
2023-09-11 18:32:05 +02:00
let a = l.get(o);
a.delete(r), !(a.size > 1) && (g.clear(...a), l.delete(o));
});
2023-09-07 17:35:23 +02:00
}
};
2023-09-11 18:32:05 +02:00
g.el = function(e, t) {
let r = document.createComment("<#reactive>"), n = document.createComment("</#reactive>"), o = document.createDocumentFragment();
o.append(r, n);
let a = (i) => {
if (!r.parentNode || !n.parentNode)
2023-09-13 13:20:00 +02:00
return U(e, a);
2023-09-11 18:32:05 +02:00
let s = t(i);
Array.isArray(s) || (s = [s]);
let h = r;
for (; (h = r.nextSibling) !== n; )
2023-09-08 20:18:58 +02:00
h.remove();
2023-09-11 18:32:05 +02:00
r.after(...s);
2023-09-08 20:18:58 +02:00
};
2023-09-13 13:20:00 +02:00
return P(e, a), a(e()), o;
2023-09-08 20:18:58 +02:00
};
2023-09-13 13:20:00 +02:00
var F = {
isTextContent(e) {
2023-09-13 13:20:00 +02:00
return w(e) === "string" || L(e) && w(X(e)) === "string";
},
2023-09-11 18:32:05 +02:00
processReactiveAttribute(e, t, r, n) {
2023-09-13 13:20:00 +02:00
return L(r) ? (P(r, (o) => n([t, o])), r()) : r;
}
};
2023-09-13 13:20:00 +02:00
function z(e, t) {
let r = (...n) => n.length ? Q(r, n[0]) : K(r);
return B(r, e, t);
}
2023-09-13 13:20:00 +02:00
var Z = Object.assign(/* @__PURE__ */ Object.create(null), {
stopPropagation() {
this.skip = !0;
}
});
2023-09-13 13:20:00 +02:00
function B(e, t, r) {
return w(r) !== "[object Object]" && (r = {}), e[d] = {
value: t,
2023-09-11 18:32:05 +02:00
actions: r,
listeners: /* @__PURE__ */ new Set()
2023-09-13 13:20:00 +02:00
}, e.toJSON = () => e(), Object.setPrototypeOf(e[d], Z), e;
}
2023-09-13 13:20:00 +02:00
var R = [];
function G(e) {
let t = function() {
2023-09-13 13:20:00 +02:00
R.push(t), e(), R.pop();
};
2023-09-11 18:32:05 +02:00
l.has(e) && (l.set(t, l.get(e)), l.delete(e)), t();
}
2023-09-13 13:20:00 +02:00
function V() {
return R[R.length - 1];
}
2023-09-13 13:20:00 +02:00
function K(e) {
if (!e[d])
return;
2023-09-13 13:20:00 +02:00
let { value: t, listeners: r } = e[d], n = V();
2023-09-11 18:32:05 +02:00
return n && r.add(n), l.has(n) && l.get(n).add(e), t;
}
2023-09-13 13:20:00 +02:00
function Q(e, t) {
if (!e[d])
return;
2023-09-11 18:32:05 +02:00
let r = e[d];
if (r.value !== t)
return r.value = t, r.listeners.forEach((n) => n(t)), t;
}
2023-09-13 13:20:00 +02:00
function X(e) {
return e[d].value;
}
2023-09-13 13:20:00 +02:00
function P(e, t) {
if (e[d])
return e[d].listeners.add(t);
}
2023-09-13 13:20:00 +02:00
function U(e, t) {
if (e[d])
return e[d].listeners.delete(t);
}
// src/signals.js
2023-09-13 13:20:00 +02:00
_(F);
export {
2023-09-11 18:32:05 +02:00
g as S,
2023-09-13 13:20:00 +02:00
y as assign,
q as classListDeclarative,
ce as createElement,
ae as dispatchEvent,
ce as el,
se as empty,
L as isSignal,
oe as namespace,
k as on,
_ as registerReactivity
};