╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ script.js ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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