Eliminar src/sigpro.js
This commit is contained in:
132
src/sigpro.js
132
src/sigpro.js
@@ -1,132 +0,0 @@
|
||||
/**
|
||||
* SigPro 2.0 - Integrated Reactive Engine
|
||||
*/
|
||||
(() => {
|
||||
/** @type {Function|null} Dependency tracker */
|
||||
let activeEffect = null;
|
||||
|
||||
/**
|
||||
* Reactive Engine ($)
|
||||
* Creates Signals, Computeds, or Effects.
|
||||
* @param {any} initial - Value or reactive function.
|
||||
*/
|
||||
window.$ = (initial) => {
|
||||
const subs = new Set();
|
||||
if (typeof initial === 'function') {
|
||||
let cached;
|
||||
const runner = () => {
|
||||
const prev = activeEffect;
|
||||
activeEffect = runner;
|
||||
try { cached = initial(); } finally { activeEffect = prev; }
|
||||
subs.forEach(s => s());
|
||||
};
|
||||
runner();
|
||||
return () => { (activeEffect && subs.add(activeEffect)); return cached; };
|
||||
}
|
||||
return (...args) => {
|
||||
if (args.length) {
|
||||
const next = typeof args[0] === 'function' ? args[0](initial) : args[0];
|
||||
if (!Object.is(initial, next)) { initial = next; subs.forEach(s => s()); }
|
||||
}
|
||||
(activeEffect && subs.add(activeEffect));
|
||||
return initial;
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Hyperscript Engine ($$)
|
||||
* @param {string} tag - HTML tag.
|
||||
* @param {Object} [props] - Attributes/Events.
|
||||
* @param {any} [children] - Content.
|
||||
*/
|
||||
window.$$ = (tag, props = {}, children = []) => {
|
||||
const el = document.createElement(tag);
|
||||
|
||||
// Logic: If props is NOT a plain object or is a Node/Array/Function,
|
||||
// it's actually the children.
|
||||
if (
|
||||
typeof props !== 'object' ||
|
||||
props instanceof Node ||
|
||||
Array.isArray(props) ||
|
||||
typeof props === 'function'
|
||||
) {
|
||||
children = props;
|
||||
props = {};
|
||||
}
|
||||
|
||||
for (let [key, val] of Object.entries(props)) {
|
||||
if (key.startsWith('on')) {
|
||||
el.addEventListener(key.toLowerCase().slice(2), val);
|
||||
} else if (key.startsWith('$')) {
|
||||
const attr = key.slice(1);
|
||||
if ((attr === 'value' || attr === 'checked') && typeof val === 'function') {
|
||||
const ev = attr === 'checked' ? 'change' : 'input';
|
||||
el.addEventListener(ev, e => val(attr === 'checked' ? e.target.checked : e.target.value));
|
||||
}
|
||||
$(() => {
|
||||
const v = typeof val === 'function' ? val() : val;
|
||||
if (attr === 'value' || attr === 'checked') el[attr] = v;
|
||||
else if (typeof v === 'boolean') el.toggleAttribute(attr, v);
|
||||
else el.setAttribute(attr, v ?? '');
|
||||
});
|
||||
} else el.setAttribute(key, val);
|
||||
}
|
||||
|
||||
const append = (c) => {
|
||||
if (Array.isArray(c)) return c.forEach(append);
|
||||
if (typeof c === 'function') {
|
||||
const node = document.createTextNode('');
|
||||
$(() => {
|
||||
const res = c();
|
||||
if (res instanceof Node) { if (node.parentNode) node.replaceWith(res); }
|
||||
else { node.textContent = res ?? ''; }
|
||||
});
|
||||
return el.appendChild(node);
|
||||
}
|
||||
el.appendChild(c instanceof Node ? c : document.createTextNode(c ?? ''));
|
||||
};
|
||||
append(children);
|
||||
return el;
|
||||
};
|
||||
|
||||
/**
|
||||
* Application Mounter (Inspired by Vue)
|
||||
* @param {HTMLElement|Function} node - Root component.
|
||||
* @param {HTMLElement} [target] - Container element.
|
||||
*/
|
||||
window.mount = (node, target = document.body) => {
|
||||
target.innerHTML = '';
|
||||
const root = typeof node === 'function' ? node() : node;
|
||||
target.appendChild(root);
|
||||
console.log("%c[SigPro] Mounted successfully", "color: #42b883; font-weight: bold;");
|
||||
};
|
||||
|
||||
/**
|
||||
* Integrated Router
|
||||
* @param {Array} routes - Route definitions.
|
||||
*/
|
||||
window.router = (routes) => {
|
||||
const sPath = $(window.location.hash.replace(/^#/, "") || "/");
|
||||
window.addEventListener("hashchange", () => sPath(window.location.hash.replace(/^#/, "") || "/"));
|
||||
|
||||
return div([
|
||||
() => {
|
||||
const current = sPath();
|
||||
const route = routes.find(r => {
|
||||
const rP = r.path.split('/').filter(Boolean);
|
||||
const cP = current.split('/').filter(Boolean);
|
||||
if (rP.length !== cP.length) return false;
|
||||
return rP.every((part, i) => part.startsWith(':') || part === cP[i]);
|
||||
}) || routes.find(r => r.path === "*");
|
||||
|
||||
return route ? (typeof route.component === 'function' ? route.component() : route.component) : h1("404");
|
||||
}
|
||||
]);
|
||||
};
|
||||
|
||||
// Auto-generate tags: div(), h1(), etc.
|
||||
['div', 'span', 'p', 'button', 'h1', 'h2', 'h3', 'ul', 'li', 'a', 'input', 'section', 'main'].forEach(t => {
|
||||
window[t] = (p, c) => $$(t, p, c);
|
||||
});
|
||||
|
||||
})();
|
||||
Reference in New Issue
Block a user