╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ script.js ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
65 const positions = {}; 66 for (const el of grid.querySelectorAll(".scroll[data-scroll-id]")) { 67 positions[el.dataset.scrollId] = { top: el.scrollTop, left: el.scrollLeft }; 68 } 69 grid.innerHTML = html; 70 for (const el of grid.querySelectorAll(".scroll[data-scroll-id]")) { 71 const p = positions[el.dataset.scrollId]; 72 if (p) { el.scrollTop = p.top; el.scrollLeft = p.left; } 73 } 74 }, 75 _replaceState: function (ptr, len) { 76 const url = readWasmString(ptr, len); 77 // passive same-page URL changes. replaceState changes the URL 78 // without adding a history entry, so the back button isn't 79 // cluttered with unnecessary entries. 80 if (window.location.pathname !== url) { 81 history.replaceState({}, "", url); 82 } 83 }, 84 _setOverlay: function (ptr, len) { 85 const html = readWasmString(ptr, len); 86 // diff against the previous overlay HTML; an unchanged overlay 87 // means no layout/structure has shifted (typing alone doesn't 88 // produce a diff because we deliberately omit the `value` 89 // attribute on inputs — the browser tracks that). skipping the 90 // innerHTML assignment keeps the live <form> alive across the 91 // mousedown→focusin→tick→click sequence, which is what lets 92 // the very first click on the submit button actually submit. 93 if (html === currentOverlay) return; 94 currentOverlay = html; 95 overlay.innerHTML = html; 96 }, 97 _focusInput: function (id) { 98 const target = overlay.querySelector(`[data-focus-id="${id}"]`); 99 if (target && document.activeElement !== target) target.focus(); 100 }, 101 _navigate: function (ptr, len) { 102 // a real browser navigation to another top-level page (the wasm 103 // can't render it client-side without server data). 104 window.location.assign(readWasmString(ptr, len)); 105 }, 106 _scrollToFocus: function (id) { 107 // bring the focused widget into view within its own .scroll div. it's 108 // several spans (one per row) sharing this id, in top-to-bottom order, 109 // so the first gives its top/left edge and the last its bottom. 110 const els = grid.querySelectorAll(`[data-focus-id="${id}"]`); 111 if (els.length === 0) return; 112 const container = els[0].closest(".scroll"); 113 if (!container) return; 114 115 const c = container.getBoundingClientRect(); 116 const first = els[0].getBoundingClientRect(); 117 const last = els[els.length - 1].getBoundingClientRect(); 118 const top = first.top - c.top + container.scrollTop; 119 const bottom = last.bottom - c.top + container.scrollTop; 120 const left = first.left - c.left + container.scrollLeft; 121 122 // vertical is the priority axis: fit the widget, preferring its top 123 // when it's taller than the viewport. 124 if (top < container.scrollTop || bottom - top > container.clientHeight) { 125 container.scrollTop = top; 126 } else if (bottom > container.scrollTop + container.clientHeight) { 127 container.scrollTop = bottom - container.clientHeight; 128 } 129 // horizontal: only reveal the row's start, never chase the right edge, 130 // so moving down through varying-width rows doesn't yank the view 131 // sideways. (a hidden-overflow x axis stays at 0, so this no-ops.) 132 if (left < container.scrollLeft) container.scrollLeft = left; 133 }, 134 }, 135 }; 136 137 function sendTextInputValue(focusId, value) { 138 const bytes = encoder.encode(value); 139 const ptr = wasmInstance.exports._alloc(bytes.length); 140 new Uint8Array(wasmInstance.exports.memory.buffer, ptr, bytes.length).set(bytes); 141 wasmInstance.exports._setTextInputValue(focusId, ptr, bytes.length); 142 wasmInstance.exports._tick(minRows(), maxCols()); 143 } 144 145 function sendEnter(form) { 146 const btn = form.querySelector("button[type=submit][data-focus-id]"); 147 if (!btn) return; 148 wasmInstance.exports._onMouseClick(Number(btn.dataset.focusId)); 149 wasmInstance.exports._onKeyDown(13); 150 wasmInstance.exports._tick(minRows(), maxCols()); 151 } 152 153 WebAssembly.instantiateStreaming(fetch("/haxy.wasm"), importObject).then(async (result) => { 154 wasmInstance = result.instance; 155 156 // wait for the @font-face to load before measuring cell width, otherwise 157 // we get the fallback font's metrics. 158 if (document.fonts && document.fonts.ready) { 159 await document.fonts.ready; 160 } 161 162 // the page is embedded as base64 so it can sit inside the host html 163 // without worrying about characters that would terminate the script tag. 164 // decode here and hand the raw json bytes to the wasm. 165 const jsonBytes = Uint8Array.from(atob(pageJsonBase64), (c) => c.charCodeAt(0)); 166 const ptr = wasmInstance.exports._alloc(jsonBytes.length); 167 new Uint8Array(wasmInstance.exports.memory.buffer, ptr, jsonBytes.length).set(jsonBytes); 168 wasmInstance.exports._init(ptr, jsonBytes.length, minRows(), maxCols()); 169 170 document.addEventListener("keydown", (event) => { 171 // let the browser handle modifier combos. the TUI only uses unmodified keys, 172 // so intercepting these would just break normal browser navigation. 173 if (event.altKey || event.ctrlKey || event.metaKey) return; 174 // when a form element (text input or submit button) owns focus, 175 // the browser handles typing, Enter-to-submit, and Tab natively. 176 // we only forward keys into the TUI when focus is elsewhere. 177 // exception: arrow up/down on an input unfocuses it and forwards 178 // the event so the TUI can move focus between widgets. 179 if (document.activeElement) { 180 const tag = document.activeElement.tagName; 181 const isArrow = event.key === "ArrowUp" || event.key === "ArrowDown" || 182 event.key === "ArrowLeft" || event.key === "ArrowRight"; 183 if (tag === "INPUT" && (event.key === "ArrowUp" || event.key === "ArrowDown")) { 184 // up/down moves between widgets; left/right stay in the input 185 document.activeElement.blur(); 186 } else if (tag === "BUTTON" && isArrow) { 187 // arrows navigate away from a focused submit button 188 document.activeElement.blur(); 189 } else if (tag === "INPUT" || tag === "BUTTON") { 190 // Enter (submit), typing, Tab — handled natively by the browser 191 return; 192 } 193 } 194 if (["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "PageUp", "PageDown", "Home", "End"].includes(event.key)) { 195 event.preventDefault(); 196 } 197 wasmInstance.exports._onKeyDown(event.keyCode); 198 wasmInstance.exports._tick(minRows(), maxCols()); 199 }); 200 201 // listeners on document (not #grid) since the form lives outside #grid; 202 // input/focus events from form elements still bubble up to document. 203 document.addEventListener("input", (event) => { 204 const t = event.target; 205 if (!t || t.tagName !== "INPUT" || !t.dataset.focusId) return; 206 sendTextInputValue(Number(t.dataset.focusId), t.value); 207 }); 208 209 document.addEventListener("focusin", (event) => { 210 const t = event.target; 211 if (!t || !t.dataset || !t.dataset.focusId) return; 212 // sync the wasm focus to wherever the browser moved focus 213 wasmInstance.exports._setFocus(Number(t.dataset.focusId)); 214 // a click's mousedown fires this focusin before the click event. ticking 215 // here would rebuild #grid and destroy the node before its click lands, 216 // swallowing the navigation and forcing a second click — so don't 217 // re-render for a grid element. overlay controls live outside #grid and 218 // survive the diff, so they still tick. 219 if (!grid.contains(t)) wasmInstance.exports._tick(minRows(), maxCols()); 220 }); 221 222 grid.addEventListener("click", (event) => { 223 const span = event.target.closest(".clickable"); 224 if (!span) return; 225 // leave modified / non-left clicks (open-in-new-tab, etc.) to the browser 226 if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return; 227 // wasm routes the click: an in-page link just moves focus and re-renders, 228 // a cross-page one calls _navigate. suppress the <a>'s own navigation so 229 // an in-page link doesn't also trigger a redundant full page load. 230 event.preventDefault(); 231 const focusId = Number(span.dataset.focusId); 232 wasmInstance.exports._onMouseClick(focusId); 233 wasmInstance.exports._tick(minRows(), maxCols()); 234 }); 235 236 let resizeTimer = null; 237 window.addEventListener("resize", () => { 238 clearTimeout(resizeTimer); 239 resizeTimer = setTimeout(() => { 240 wasmInstance.exports._tick(minRows(), maxCols()); 241 }, 100); 242 }); 243 }); 244