|
| 1 | +const fragmentDecorators = new WeakMap(); |
| 2 | + |
| 3 | +export function appendChild(n,c) { n.appendChild(c); } |
| 4 | +export function appendBefore(n,i) { n.before(i); } |
| 5 | +export function removeNode(n) { n.remove(); } |
| 6 | +export function replaceNode(o,n) { o.replaceWith(n); } |
| 7 | +export function emptyNode() { return document.createTextNode(""); } |
| 8 | +export function fragment() |
| 9 | +{ |
| 10 | + let f = document.createDocumentFragment(); |
| 11 | + f.append("", ""); |
| 12 | + return f; |
| 13 | +}; |
| 14 | +export function fragmentDecorate(f) { |
| 15 | + fragmentDecorators.set(f, [f.firstChild, f.lastChild]); |
| 16 | + return f.lastChild; |
| 17 | +} |
| 18 | +export function fragmentUnmount(f) |
| 19 | +{ |
| 20 | + let [b, e] = fragmentDecorators.get(f); |
| 21 | + while (b.nextSibling !== e) f.appendChild(b.nextSibling); |
| 22 | + f.appendChild(e); |
| 23 | + f.insertBefore(b, f.firstChild); |
| 24 | +} |
| 25 | +export function fragmentReplace(f,n) |
| 26 | +{ |
| 27 | + let [b, e] = fragmentDecorators.get(f); |
| 28 | + while (b.nextSibling !== e) f.appendChild(b.nextSibling); |
| 29 | + b.replaceWith(n); |
| 30 | + f.appendChild(e); |
| 31 | + f.insertBefore(b, f.firstChild); |
| 32 | +} |
| 33 | +export function setTextContent(n,t) { n.textContent = t; } |
| 34 | +export function setAttribute(n,a,v) { n.setAttribute(a, v); } |
| 35 | + |
| 36 | +export function setChecked(n,v) { if (n.checked !== v) n.checked = v; } |
| 37 | +export function setClassName(n,v) { n.className = v; } |
| 38 | +export function setHref(n,v) { n.href = v; } |
| 39 | +export function setStyle(n,v) { n.style = v; } |
| 40 | +export function setValue(n,v) { n.value = v; } |
| 41 | + |
| 42 | +export function addClass(n,v) { n.classList.add(v); } |
| 43 | +export function removeClass(n,v) { n.classList.remove(v); } |
| 44 | +export function replaceClass(n,o,v) { n.classList.replace(o,v); } |
| 45 | +export function toggleClass(n,c,v) { n.classList.toggle(c,v); } |
| 46 | + |
| 47 | +export function makeEventHandler(c,f) { return (e) => koboldCallback(e,c,f); } |
| 48 | +export function checkEventHandler() { if (typeof koboldCallback !== "function") console.error( |
| 49 | +`Missing \`koboldCallback\` in global scope. |
| 50 | +Add the following to your Trunk.toml: |
| 51 | +
|
| 52 | +[build] |
| 53 | +pattern_script = "<script type=\\"module\\">import init, { koboldCallback } from '{base}{js}';init('{base}{wasm}');window.koboldCallback = koboldCallback;</script>" |
| 54 | +`) } |
0 commit comments