Actualizar sigworkPro.js
This commit is contained in:
109
sigworkPro.js
109
sigworkPro.js
@@ -1,11 +1,10 @@
|
|||||||
const isFunction = (v) => typeof v === 'function';
|
const isFunction = v => typeof v === 'function';
|
||||||
const isNode = (v) => v instanceof Node;
|
const isNode = v => v instanceof Node;
|
||||||
const doc = typeof document !== "undefined" ? document : null;
|
const doc = typeof document !== "undefined" ? document : null;
|
||||||
|
|
||||||
let activeEffect = null;
|
let activeEffect = null;
|
||||||
const pendingEffects = new Set();
|
const pendingEffects = new Set();
|
||||||
let flushScheduled = false;
|
let flushScheduled = false;
|
||||||
|
|
||||||
const nodeDisposers = new WeakMap();
|
const nodeDisposers = new WeakMap();
|
||||||
|
|
||||||
const registerNodeCleanup = (node, disposer) => {
|
const registerNodeCleanup = (node, disposer) => {
|
||||||
@@ -32,7 +31,7 @@ const scheduleFlush = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const disposeEffectTree = (effect) => {
|
const disposeEffectTree = effect => {
|
||||||
if (effect.disposed) return;
|
if (effect.disposed) return;
|
||||||
effect.disposed = true;
|
effect.disposed = true;
|
||||||
const stack = [effect];
|
const stack = [effect];
|
||||||
@@ -53,7 +52,7 @@ const disposeEffectTree = (effect) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const createEffect = (fn) => {
|
const createEffect = fn => {
|
||||||
const effect = {
|
const effect = {
|
||||||
execute: null,
|
execute: null,
|
||||||
dependencies: new Set(),
|
dependencies: new Set(),
|
||||||
@@ -88,7 +87,7 @@ const createEffect = (fn) => {
|
|||||||
|
|
||||||
export const Watch = createEffect;
|
export const Watch = createEffect;
|
||||||
|
|
||||||
export const removeNode = (node) => {
|
export const removeNode = node => {
|
||||||
if (!node) return;
|
if (!node) return;
|
||||||
if (node.childNodes) {
|
if (node.childNodes) {
|
||||||
node.childNodes.forEach(child => removeNode(child));
|
node.childNodes.forEach(child => removeNode(child));
|
||||||
@@ -111,14 +110,14 @@ export const removeNode = (node) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const track = (subs) => {
|
const track = subs => {
|
||||||
if (activeEffect && !activeEffect.disposed) {
|
if (activeEffect && !activeEffect.disposed) {
|
||||||
subs.add(activeEffect);
|
subs.add(activeEffect);
|
||||||
activeEffect.dependencies.add(subs);
|
activeEffect.dependencies.add(subs);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const trigger = (subs) => {
|
const trigger = subs => {
|
||||||
if (!subs) return;
|
if (!subs) return;
|
||||||
for (const eff of subs) {
|
for (const eff of subs) {
|
||||||
if (eff !== activeEffect && !eff.disposed) pendingEffects.add(eff);
|
if (eff !== activeEffect && !eff.disposed) pendingEffects.add(eff);
|
||||||
@@ -135,7 +134,7 @@ export const $ = (initialValue, storageKey) => {
|
|||||||
if (!Object.is(v, cached)) { cached = v; dirty = false; s(v); }
|
if (!Object.is(v, cached)) { cached = v; dirty = false; s(v); }
|
||||||
});
|
});
|
||||||
if (currentComponentContext) currentComponentContext.unmount.push(stop);
|
if (currentComponentContext) currentComponentContext.unmount.push(stop);
|
||||||
const signal = (newVal) => {
|
const signal = newVal => {
|
||||||
if (newVal === undefined) {
|
if (newVal === undefined) {
|
||||||
if (dirty) { cached = initialValue(); dirty = false; s(cached); }
|
if (dirty) { cached = initialValue(); dirty = false; s(cached); }
|
||||||
return s();
|
return s();
|
||||||
@@ -144,18 +143,15 @@ export const $ = (initialValue, storageKey) => {
|
|||||||
};
|
};
|
||||||
return signal;
|
return signal;
|
||||||
}
|
}
|
||||||
|
|
||||||
const subs = new Set();
|
const subs = new Set();
|
||||||
let value = initialValue;
|
let value = initialValue;
|
||||||
|
|
||||||
if (storageKey) {
|
if (storageKey) {
|
||||||
try {
|
try {
|
||||||
const item = localStorage.getItem(storageKey);
|
const item = localStorage.getItem(storageKey);
|
||||||
if (item !== null) value = JSON.parse(item);
|
if (item !== null) value = JSON.parse(item);
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
}
|
}
|
||||||
|
const signal = newVal => {
|
||||||
const signal = (newVal) => {
|
|
||||||
if (newVal === undefined) {
|
if (newVal === undefined) {
|
||||||
track(subs);
|
track(subs);
|
||||||
return value;
|
return value;
|
||||||
@@ -170,12 +166,10 @@ export const $ = (initialValue, storageKey) => {
|
|||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (storageKey) {
|
if (storageKey) {
|
||||||
const sync = Watch(() => { signal(value); });
|
const sync = Watch(() => { signal(value); });
|
||||||
if (currentComponentContext) currentComponentContext.unmount.push(sync);
|
if (currentComponentContext) currentComponentContext.unmount.push(sync);
|
||||||
}
|
}
|
||||||
|
|
||||||
return signal;
|
return signal;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -201,7 +195,7 @@ export const watch = (source, callback) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const untrack = (fn) => {
|
export const untrack = fn => {
|
||||||
const prev = activeEffect;
|
const prev = activeEffect;
|
||||||
activeEffect = null;
|
activeEffect = null;
|
||||||
try { return fn(); } finally { activeEffect = prev; }
|
try { return fn(); } finally { activeEffect = prev; }
|
||||||
@@ -209,8 +203,8 @@ export const untrack = (fn) => {
|
|||||||
|
|
||||||
let currentComponentContext = null;
|
let currentComponentContext = null;
|
||||||
|
|
||||||
export const onMount = (fn) => currentComponentContext?.mount.push(fn);
|
export const onMount = fn => currentComponentContext?.mount.push(fn);
|
||||||
export const onUnmount = (fn) => currentComponentContext?.unmount.push(fn);
|
export const onUnmount = fn => currentComponentContext?.unmount.push(fn);
|
||||||
|
|
||||||
const DANGEROUS_PROTOCOL = /^\s*(javascript|data|vbscript):/i;
|
const DANGEROUS_PROTOCOL = /^\s*(javascript|data|vbscript):/i;
|
||||||
const isDangerousAttr = key => key === 'src' || key === 'href' || key.startsWith('on');
|
const isDangerousAttr = key => key === 'src' || key === 'href' || key.startsWith('on');
|
||||||
@@ -324,7 +318,7 @@ export const Tag = (tag, props = {}, ...children) => {
|
|||||||
registerNodeCleanup(el, stopAttr);
|
registerNodeCleanup(el, stopAttr);
|
||||||
if (/^(INPUT|TEXTAREA|SELECT)$/.test(el.tagName) && (k === 'value' || k === 'checked')) {
|
if (/^(INPUT|TEXTAREA|SELECT)$/.test(el.tagName) && (k === 'value' || k === 'checked')) {
|
||||||
const evType = k === 'checked' ? 'change' : 'input';
|
const evType = k === 'checked' ? 'change' : 'input';
|
||||||
const handler = (e) => v(e.target[k]);
|
const handler = e => v(e.target[k]);
|
||||||
el.addEventListener(evType, handler);
|
el.addEventListener(evType, handler);
|
||||||
onUnmount(() => el.removeEventListener(evType, handler));
|
onUnmount(() => el.removeEventListener(evType, handler));
|
||||||
}
|
}
|
||||||
@@ -336,15 +330,31 @@ export const Tag = (tag, props = {}, ...children) => {
|
|||||||
return el;
|
return el;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const If = ({ when, children }) => {
|
export const If = (cond, thenFn, elseFn = null, hooks = {}) => {
|
||||||
let lastResult = null;
|
let lastResult = null;
|
||||||
let node = null;
|
let node = null;
|
||||||
|
let exitPromise = null;
|
||||||
return () => {
|
return () => {
|
||||||
const condition = !!(isFunction(when) ? when() : when);
|
const condition = !!(isFunction(cond) ? cond() : cond);
|
||||||
if (condition === lastResult) return node;
|
if (condition === lastResult) return node;
|
||||||
if (node) removeNode(node);
|
if (node) {
|
||||||
|
if (hooks.leave) {
|
||||||
|
if (exitPromise && exitPromise.cancel) exitPromise.cancel();
|
||||||
|
const anim = hooks.leave(node);
|
||||||
|
exitPromise = anim;
|
||||||
|
if (anim && anim.finished) {
|
||||||
|
anim.finished.then(() => removeNode(node));
|
||||||
|
} else {
|
||||||
|
removeNode(node);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
removeNode(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
lastResult = condition;
|
lastResult = condition;
|
||||||
node = condition ? children[0] : (children[1] || null);
|
const newNode = condition ? thenFn() : elseFn?.();
|
||||||
|
node = newNode;
|
||||||
|
if (node && hooks.enter) hooks.enter(node);
|
||||||
return node;
|
return node;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -359,9 +369,7 @@ export const For = ({ each, key, children }) => {
|
|||||||
const item = items[i];
|
const item = items[i];
|
||||||
const itemKey = key ? (isFunction(key) ? key(item, i) : item[key]) : i;
|
const itemKey = key ? (isFunction(key) ? key(item, i) : item[key]) : i;
|
||||||
let node = cache.get(itemKey);
|
let node = cache.get(itemKey);
|
||||||
if (!node) {
|
if (!node) node = Tag(children[0], { item, index: i });
|
||||||
node = Tag(children[0], { item, index: i });
|
|
||||||
}
|
|
||||||
newCache.set(itemKey, node);
|
newCache.set(itemKey, node);
|
||||||
nodes.push(node);
|
nodes.push(node);
|
||||||
cache.delete(itemKey);
|
cache.delete(itemKey);
|
||||||
@@ -372,45 +380,12 @@ export const For = ({ each, key, children }) => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const Transition = ({ enter, leave, children }) => {
|
export const Animate = {
|
||||||
const decorate = (el) => {
|
fadeIn: (el, duration = 300) => el.animate([{ opacity: 0 }, { opacity: 1 }], { duration, fill: 'forwards' }),
|
||||||
if (!isNode(el)) return el;
|
fadeOut: (el, duration = 300) => {
|
||||||
if (enter) {
|
const anim = el.animate([{ opacity: 1 }, { opacity: 0 }], { duration, fill: 'forwards' });
|
||||||
const [from, active, to] = enter;
|
return anim;
|
||||||
el._raf = requestAnimationFrame(() => {
|
|
||||||
el.classList.add(active);
|
|
||||||
el._raf = requestAnimationFrame(() => {
|
|
||||||
el.classList.add(from);
|
|
||||||
el.classList.remove(active);
|
|
||||||
el.classList.add(to);
|
|
||||||
const onEnd = () => {
|
|
||||||
el.classList.remove(to, from);
|
|
||||||
el.removeEventListener('transitionend', onEnd);
|
|
||||||
};
|
|
||||||
el.addEventListener('transitionend', onEnd, { once: true });
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
if (leave) {
|
|
||||||
const [from, active, to] = leave;
|
|
||||||
el.leaveTransition = (done) => {
|
|
||||||
el.classList.add(active);
|
|
||||||
el._raf = requestAnimationFrame(() => {
|
|
||||||
el.classList.add(from);
|
|
||||||
el.classList.remove(active);
|
|
||||||
el.classList.add(to);
|
|
||||||
const onEnd = () => {
|
|
||||||
el.classList.remove(to, from);
|
|
||||||
done();
|
|
||||||
};
|
|
||||||
el.addEventListener('transitionend', onEnd, { once: true });
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return el;
|
|
||||||
};
|
|
||||||
const child = children[0];
|
|
||||||
return isFunction(child) ? () => decorate(child()) : decorate(child);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const Router = ({ routes }) => {
|
export const Router = ({ routes }) => {
|
||||||
@@ -443,10 +418,10 @@ export const Router = ({ routes }) => {
|
|||||||
|
|
||||||
Router.params = $({});
|
Router.params = $({});
|
||||||
|
|
||||||
export const navigate = (path) => { window.location.hash = path; };
|
export const navigate = path => { window.location.hash = path; };
|
||||||
export const currentPath = () => window.location.hash.slice(1) || '/';
|
export const currentPath = () => window.location.hash.slice(1) || '/';
|
||||||
|
|
||||||
export const createApp = (Root, rootProps = {}) => (selector) => {
|
export const createApp = (Root, rootProps = {}) => selector => {
|
||||||
const target = typeof selector === 'string' ? doc.querySelector(selector) : selector;
|
const target = typeof selector === 'string' ? doc.querySelector(selector) : selector;
|
||||||
if (target.appUnmount) target.appUnmount();
|
if (target.appUnmount) target.appUnmount();
|
||||||
const app = Tag(Root, rootProps);
|
const app = Tag(Root, rootProps);
|
||||||
@@ -461,4 +436,4 @@ export const createApp = (Root, rootProps = {}) => (selector) => {
|
|||||||
globalThis[tag[0].toUpperCase() + tag.slice(1)] = (props, ...children) => Tag(tag, props, ...children);
|
globalThis[tag[0].toUpperCase() + tag.slice(1)] = (props, ...children) => Tag(tag, props, ...children);
|
||||||
});
|
});
|
||||||
|
|
||||||
export default { $, set, Watch, watch, untrack, Tag, If, For, Transition, Router, createApp, removeNode, navigate, currentPath, onMount, onUnmount };
|
export default { $, set, Watch, watch, untrack, Tag, If, For, Animate, Router, createApp, removeNode, navigate, currentPath, onMount, onUnmount };
|
||||||
Reference in New Issue
Block a user