This commit is contained in:
181
dist/sigpro-ui.js
vendored
181
dist/sigpro-ui.js
vendored
@@ -92,7 +92,7 @@ var spui = (() => {
|
||||
val: () => val
|
||||
});
|
||||
|
||||
// src/sigpro.js
|
||||
// node_modules/sigpro/dist/sigpro.esm.js
|
||||
var isFunc = (f) => typeof f === "function";
|
||||
var isObj = (o) => o && typeof o === "object";
|
||||
var isArr = Array.isArray;
|
||||
@@ -108,7 +108,8 @@ var spui = (() => {
|
||||
var XLINK_NS = "http://www.w3.org/1999/xlink";
|
||||
var SVG_TAGS = new Set("svg,path,circle,rect,line,polyline,polygon,g,defs,text,textPath,tspan,use,symbol,image,marker,ellipse".split(","));
|
||||
var dispose = (eff) => {
|
||||
if (!eff || eff._disposed) return;
|
||||
if (!eff || eff._disposed)
|
||||
return;
|
||||
eff._disposed = true;
|
||||
const stack = [eff];
|
||||
while (stack.length) {
|
||||
@@ -128,7 +129,8 @@ var spui = (() => {
|
||||
}
|
||||
};
|
||||
var onUnmount = (fn) => {
|
||||
if (activeOwner) (activeOwner._cleanups ||= /* @__PURE__ */ new Set()).add(fn);
|
||||
if (activeOwner)
|
||||
(activeOwner._cleanups ||= /* @__PURE__ */ new Set()).add(fn);
|
||||
};
|
||||
var untrack = (fn) => {
|
||||
const p = activeEffect;
|
||||
@@ -141,8 +143,10 @@ var spui = (() => {
|
||||
};
|
||||
var createEffect = (fn, isComputed = false) => {
|
||||
const effect = () => {
|
||||
if (effect._disposed) return;
|
||||
if (effect._deps) effect._deps.forEach((s) => s.delete(effect));
|
||||
if (effect._disposed)
|
||||
return;
|
||||
if (effect._deps)
|
||||
effect._deps.forEach((s) => s.delete(effect));
|
||||
if (effect._cleanups) {
|
||||
effect._cleanups.forEach((c) => c());
|
||||
effect._cleanups.clear();
|
||||
@@ -165,15 +169,19 @@ var spui = (() => {
|
||||
effect._depth = activeEffect ? activeEffect._depth + 1 : 0;
|
||||
effect._mounts = [];
|
||||
effect._parent = activeOwner;
|
||||
if (activeOwner) (activeOwner._children ||= /* @__PURE__ */ new Set()).add(effect);
|
||||
if (activeOwner)
|
||||
(activeOwner._children ||= /* @__PURE__ */ new Set()).add(effect);
|
||||
return effect;
|
||||
};
|
||||
var flush = () => {
|
||||
if (isFlushing) return;
|
||||
if (isFlushing)
|
||||
return;
|
||||
isFlushing = true;
|
||||
const sorted = Array.from(effectQueue).sort((a, b) => a._depth - b._depth);
|
||||
effectQueue.clear();
|
||||
for (const e of sorted) if (!e._disposed) e();
|
||||
for (const e of sorted)
|
||||
if (!e._disposed)
|
||||
e();
|
||||
isFlushing = false;
|
||||
};
|
||||
var trackUpdate = (subs, trigger = false) => {
|
||||
@@ -183,16 +191,19 @@ var spui = (() => {
|
||||
} else if (trigger && subs.size > 0) {
|
||||
let hasQueue = false;
|
||||
for (const e of subs) {
|
||||
if (e === activeEffect || e._disposed) continue;
|
||||
if (e === activeEffect || e._disposed)
|
||||
continue;
|
||||
if (e._isComputed) {
|
||||
e._dirty = true;
|
||||
if (e._subs) trackUpdate(e._subs, true);
|
||||
if (e._subs)
|
||||
trackUpdate(e._subs, true);
|
||||
} else {
|
||||
effectQueue.add(e);
|
||||
hasQueue = true;
|
||||
}
|
||||
}
|
||||
if (hasQueue && !isFlushing && batchDepth === 0) queueMicrotask(flush);
|
||||
if (hasQueue && !isFlushing && batchDepth === 0)
|
||||
queueMicrotask(flush);
|
||||
}
|
||||
};
|
||||
var $ = (val2, key = null) => {
|
||||
@@ -224,16 +235,18 @@ var spui = (() => {
|
||||
computed._disposed = false;
|
||||
return computed;
|
||||
}
|
||||
if (key) try {
|
||||
val2 = JSON.parse(localStorage.getItem(key)) ?? val2;
|
||||
} catch (e) {
|
||||
}
|
||||
if (key)
|
||||
try {
|
||||
val2 = JSON.parse(localStorage.getItem(key)) ?? val2;
|
||||
} catch (e) {
|
||||
}
|
||||
return (...args) => {
|
||||
if (args.length) {
|
||||
const next = isFunc(args[0]) ? args[0](val2) : args[0];
|
||||
if (!Object.is(val2, next)) {
|
||||
val2 = next;
|
||||
if (key) localStorage.setItem(key, JSON.stringify(val2));
|
||||
if (key)
|
||||
localStorage.setItem(key, JSON.stringify(val2));
|
||||
trackUpdate(subs, true);
|
||||
}
|
||||
}
|
||||
@@ -255,22 +268,27 @@ var spui = (() => {
|
||||
return () => dispose(effect);
|
||||
};
|
||||
var cleanupNode = (node) => {
|
||||
if (!node) return;
|
||||
if (!node)
|
||||
return;
|
||||
if (node._cleanups) {
|
||||
node._cleanups.forEach((fn) => fn());
|
||||
node._cleanups.clear();
|
||||
}
|
||||
if (node._ownerEffect) dispose(node._ownerEffect);
|
||||
if (node.childNodes) node.childNodes.forEach((n) => cleanupNode(n));
|
||||
if (node._ownerEffect)
|
||||
dispose(node._ownerEffect);
|
||||
if (node.childNodes)
|
||||
node.childNodes.forEach((n) => cleanupNode(n));
|
||||
};
|
||||
var DANGEROUS_PROTOCOL = /^\s*(javascript|data|vbscript):/i;
|
||||
var DANGEROUS_URI_ATTRS = /* @__PURE__ */ new Set(["src", "href", "formaction", "action", "background", "code", "archive"]);
|
||||
var isDangerousAttr = (key) => DANGEROUS_URI_ATTRS.has(key) || key.startsWith("on");
|
||||
var validateAttr = (key, val2) => {
|
||||
if (val2 == null || val2 === false) return null;
|
||||
if (val2 == null || val2 === false)
|
||||
return null;
|
||||
if (isDangerousAttr(key)) {
|
||||
const sVal = String(val2);
|
||||
if (DANGEROUS_PROTOCOL.test(sVal)) return "#";
|
||||
if (DANGEROUS_PROTOCOL.test(sVal))
|
||||
return "#";
|
||||
}
|
||||
return val2;
|
||||
};
|
||||
@@ -290,7 +308,8 @@ var spui = (() => {
|
||||
});
|
||||
effect();
|
||||
const result = effect._result;
|
||||
if (result == null) return null;
|
||||
if (result == null)
|
||||
return null;
|
||||
const node = result instanceof Node || isArr(result) && result.every((n) => n instanceof Node) ? result : doc.createTextNode(String(result));
|
||||
const attach = (n) => {
|
||||
if (isObj(n) && !n._isRuntime) {
|
||||
@@ -325,11 +344,16 @@ var spui = (() => {
|
||||
} else if (isFunc(v)) {
|
||||
const effect = createEffect(() => {
|
||||
const val2 = validateAttr(k, v());
|
||||
if (k === "class") el.className = val2 || "";
|
||||
else if (val2 == null) el.removeAttribute(k);
|
||||
else if (k === "style" && typeof val2 === "string") el.setAttribute("style", val2);
|
||||
else if (k in el && !isSVG) el[k] = val2;
|
||||
else el.setAttribute(k, val2 === true ? "" : val2);
|
||||
if (k === "class")
|
||||
el.className = val2 || "";
|
||||
else if (val2 == null)
|
||||
el.removeAttribute(k);
|
||||
else if (k === "style" && typeof val2 === "string")
|
||||
el.setAttribute("style", val2);
|
||||
else if (k in el && !isSVG)
|
||||
el[k] = val2;
|
||||
else
|
||||
el.setAttribute(k, val2 === true ? "" : val2);
|
||||
});
|
||||
effect();
|
||||
el._cleanups.add(() => dispose(effect));
|
||||
@@ -341,14 +365,18 @@ var spui = (() => {
|
||||
} else {
|
||||
const val2 = validateAttr(k, v);
|
||||
if (val2 != null) {
|
||||
if (k === "style" && typeof val2 === "string") el.setAttribute("style", val2);
|
||||
else if (k in el && !isSVG) el[k] = val2;
|
||||
else el.setAttribute(k, val2 === true ? "" : val2);
|
||||
if (k === "style" && typeof val2 === "string")
|
||||
el.setAttribute("style", val2);
|
||||
else if (k in el && !isSVG)
|
||||
el[k] = val2;
|
||||
else
|
||||
el.setAttribute(k, val2 === true ? "" : val2);
|
||||
}
|
||||
}
|
||||
}
|
||||
const append = (c) => {
|
||||
if (isArr(c)) return c.forEach(append);
|
||||
if (isArr(c))
|
||||
return c.forEach(append);
|
||||
if (isFunc(c)) {
|
||||
const anchor = doc.createTextNode("");
|
||||
el.appendChild(anchor);
|
||||
@@ -357,15 +385,20 @@ var spui = (() => {
|
||||
const res = c();
|
||||
const next = (isArr(res) ? res : [res]).map(ensureNode);
|
||||
currentNodes.forEach((n) => {
|
||||
if (n._isRuntime) n.destroy();
|
||||
else cleanupNode(n);
|
||||
if (n.parentNode) n.remove();
|
||||
if (n._isRuntime)
|
||||
n.destroy();
|
||||
else
|
||||
cleanupNode(n);
|
||||
if (n.parentNode)
|
||||
n.remove();
|
||||
});
|
||||
let ref = anchor;
|
||||
for (let i = next.length - 1; i >= 0; i--) {
|
||||
const node = next[i];
|
||||
if (node.parentNode !== ref.parentNode) ref.parentNode?.insertBefore(node, ref);
|
||||
if (node._mounts) node._mounts.forEach((fn) => fn());
|
||||
if (node.parentNode !== ref.parentNode)
|
||||
ref.parentNode?.insertBefore(node, ref);
|
||||
if (node._mounts)
|
||||
node._mounts.forEach((fn) => fn());
|
||||
ref = node;
|
||||
}
|
||||
currentNodes = next;
|
||||
@@ -376,7 +409,8 @@ var spui = (() => {
|
||||
} else {
|
||||
const node = ensureNode(c);
|
||||
el.appendChild(node);
|
||||
if (node._mounts) node._mounts.forEach((fn) => fn());
|
||||
if (node._mounts)
|
||||
node._mounts.forEach((fn) => fn());
|
||||
}
|
||||
};
|
||||
append(children);
|
||||
@@ -392,7 +426,8 @@ var spui = (() => {
|
||||
activeOwner = { _cleanups: cleanups };
|
||||
activeEffect = null;
|
||||
const processResult = (result) => {
|
||||
if (!result) return;
|
||||
if (!result)
|
||||
return;
|
||||
if (result._isRuntime) {
|
||||
cleanups.add(result.destroy);
|
||||
container.appendChild(result.container);
|
||||
@@ -422,20 +457,17 @@ var spui = (() => {
|
||||
const anchor = doc.createTextNode("");
|
||||
const root = h("div", { style: "display:contents" }, [anchor]);
|
||||
let currentView = null;
|
||||
watch(
|
||||
() => !!(isFunc(cond) ? cond() : cond),
|
||||
(show) => {
|
||||
if (currentView) {
|
||||
currentView.destroy();
|
||||
currentView = null;
|
||||
}
|
||||
const content = show ? SIP : NOP;
|
||||
if (content) {
|
||||
currentView = render(() => isFunc(content) ? content() : content);
|
||||
root.insertBefore(currentView.container, anchor);
|
||||
}
|
||||
watch(() => !!(isFunc(cond) ? cond() : cond), (show) => {
|
||||
if (currentView) {
|
||||
currentView.destroy();
|
||||
currentView = null;
|
||||
}
|
||||
);
|
||||
const content = show ? SIP : NOP;
|
||||
if (content) {
|
||||
currentView = render(() => isFunc(content) ? content() : content);
|
||||
root.insertBefore(currentView.container, anchor);
|
||||
}
|
||||
});
|
||||
onUnmount(() => currentView?.destroy());
|
||||
return root;
|
||||
};
|
||||
@@ -451,8 +483,10 @@ var spui = (() => {
|
||||
const item = newItems[i];
|
||||
const key = keyField ? item?.[keyField] ?? i : item?.id ?? i;
|
||||
let view = cache.get(key);
|
||||
if (!view) view = render(() => itemFn(item, i));
|
||||
else cache.delete(key);
|
||||
if (!view)
|
||||
view = render(() => itemFn(item, i));
|
||||
else
|
||||
cache.delete(key);
|
||||
nextCache.set(key, view);
|
||||
nextOrder.push(view);
|
||||
}
|
||||
@@ -461,49 +495,20 @@ var spui = (() => {
|
||||
for (let i = nextOrder.length - 1; i >= 0; i--) {
|
||||
const view = nextOrder[i];
|
||||
const node = view.container;
|
||||
if (node.nextSibling !== lastRef) root.insertBefore(node, lastRef);
|
||||
if (node.nextSibling !== lastRef)
|
||||
root.insertBefore(node, lastRef);
|
||||
lastRef = node;
|
||||
}
|
||||
cache = nextCache;
|
||||
});
|
||||
return root;
|
||||
};
|
||||
var router = (routes) => {
|
||||
const getHash = () => window.location.hash.slice(1) || "/";
|
||||
const path = $(getHash());
|
||||
const handler = () => path(getHash());
|
||||
window.addEventListener("hashchange", handler);
|
||||
onUnmount(() => window.removeEventListener("hashchange", handler));
|
||||
const hook = h("div", { class: "router-hook" });
|
||||
let currentView = null;
|
||||
watch([path], () => {
|
||||
const cur = path();
|
||||
const route = routes.find((r) => {
|
||||
const p1 = r.path.split("/").filter(Boolean);
|
||||
const p2 = cur.split("/").filter(Boolean);
|
||||
return p1.length === p2.length && p1.every((p, i) => p[0] === ":" || p === p2[i]);
|
||||
}) || routes.find((r) => r.path === "*");
|
||||
if (route) {
|
||||
currentView?.destroy();
|
||||
const params = {};
|
||||
route.path.split("/").filter(Boolean).forEach((p, i) => {
|
||||
if (p[0] === ":") params[p.slice(1)] = cur.split("/").filter(Boolean)[i];
|
||||
});
|
||||
router.params(params);
|
||||
currentView = render(() => isFunc(route.component) ? route.component(params) : route.component);
|
||||
hook.replaceChildren(currentView.container);
|
||||
}
|
||||
});
|
||||
return hook;
|
||||
};
|
||||
router.params = $({});
|
||||
router.to = (p) => window.location.hash = p.replace(/^#?\/?/, "#/");
|
||||
router.back = () => window.history.back();
|
||||
router.path = () => window.location.hash.replace(/^#/, "") || "/";
|
||||
var mount = (comp, target) => {
|
||||
const t2 = typeof target === "string" ? doc.querySelector(target) : target;
|
||||
if (!t2) return;
|
||||
if (MOUNTED_NODES.has(t2)) MOUNTED_NODES.get(t2).destroy();
|
||||
if (!t2)
|
||||
return;
|
||||
if (MOUNTED_NODES.has(t2))
|
||||
MOUNTED_NODES.get(t2).destroy();
|
||||
const inst = render(isFunc(comp) ? comp : () => comp);
|
||||
t2.replaceChildren(inst.container);
|
||||
MOUNTED_NODES.set(t2, inst);
|
||||
|
||||
Reference in New Issue
Block a user