Actualizar sigpro.ts

This commit is contained in:
2026-04-07 01:44:19 +02:00
parent 53f7b7980d
commit 8204fd7fd1

View File

@@ -187,7 +187,7 @@ export function $<T>(initial: T | (() => T), storageKey?: string): Signal<T> {
if (storageKey) { if (storageKey) {
try { try {
localStorage.setItem(storageKey, JSON.stringify(value)); localStorage.setItem(storageKey, JSON.stringify(value));
} catch (e) {} } catch (e) { }
} }
triggerUpdate(subscribers); triggerUpdate(subscribers);
} }
@@ -229,7 +229,7 @@ export function $$<T extends object>(object: T, cache = new WeakMap()): T {
export function Watch(target: (() => any) | any[], callback?: () => void): CleanupFn { export function Watch(target: (() => any) | any[], callback?: () => void): CleanupFn {
const isExplicit = isArr(target); const isExplicit = isArr(target);
const cb = isExplicit ? callback! : (target as () => void); const cb = isExplicit ? callback! : (target as () => void);
if (!isFunc(cb)) return () => {}; if (!isFunc(cb)) return () => { };
const owner = currentOwner; const owner = currentOwner;
const runner = (() => { const runner = (() => {
@@ -407,7 +407,6 @@ export function Tag(tag: string, props: any = {}, children: any = []): HTMLEleme
return el; return el;
} }
// Componente If con soporte de transiciones
export function If( export function If(
condition: (() => boolean) | boolean, condition: (() => boolean) | boolean,
thenVal: any, thenVal: any,
@@ -448,7 +447,6 @@ export function If(
return container; return container;
} }
// Componente For con keyed rendering
export function For<T>( export function For<T>(
source: (() => T[]) | T[], source: (() => T[]) | T[],
renderFn: (item: T, index: number) => any, renderFn: (item: T, index: number) => any,
@@ -472,8 +470,8 @@ export function For<T>(
if (!view) { if (!view) {
const result = renderFn(item, i); const result = renderFn(item, i);
view = result instanceof Node view = result instanceof Node
? { container: result, destroy: () => { cleanupNode(result); result.remove(); } } ? { container: result, destroy: () => { cleanupNode(result); result.remove(); } }
: Render(() => result); : Render(() => result);
} }
@@ -498,17 +496,12 @@ export function For<T>(
return container; return container;
} }
// Router con soporte mejorado export const Router = Object.assign(
export const Router = { (routes) => {
params: $({} as Record<string, string>),
to: (path: string) => { window.location.hash = path.replace(/^#?\/?/, "#/"); },
back: () => window.history.back(),
path: () => window.location.hash.replace(/^#/, "") || "/",
outlet: (routes: Array<{ path: string; component: Component }>) => {
const currentPath = $(Router.path()); const currentPath = $(Router.path());
window.addEventListener("hashchange", () => currentPath(Router.path())); window.addEventListener("hashchange", () => currentPath(Router.path()));
const outlet = Tag("div", { class: "router-outlet" }); const outlet = Tag("div", { class: "router-outlet" });
let currentView: Runtime | null = null; let currentView = null;
Watch(currentPath, async () => { Watch(currentPath, async () => {
const path = currentPath(); const path = currentPath();
@@ -521,9 +514,9 @@ export const Router = {
if (route) { if (route) {
let comp = route.component; let comp = route.component;
if (isFunc(comp) && comp.toString().includes('import')) { if (isFunc(comp) && comp.toString().includes('import')) {
comp = (await (comp as any)()).default || (await (comp as any)()); comp = (await comp()).default || (await comp());
} }
const params: Record<string, string> = {}; const params = {};
route.path.split("/").filter(Boolean).forEach((part, i) => { route.path.split("/").filter(Boolean).forEach((part, i) => {
if (part.startsWith(":")) params[part.slice(1)] = path.split("/").filter(Boolean)[i]; if (part.startsWith(":")) params[part.slice(1)] = path.split("/").filter(Boolean)[i];
}); });
@@ -535,7 +528,13 @@ export const Router = {
}); });
return outlet; return outlet;
}, },
}; {
params: $({}),
to: (path) => { window.location.hash = path.replace(/^#?\/?/, "#/"); },
back: () => window.history.back(),
path: () => window.location.hash.replace(/^#/, "") || "/",
}
);
export function Mount(component: Component | (() => any), target: string | HTMLElement): Runtime | undefined { export function Mount(component: Component | (() => any), target: string | HTMLElement): Runtime | undefined {
const targetEl = typeof target === "string" ? doc.querySelector(target) : target; const targetEl = typeof target === "string" ? doc.querySelector(target) : target;