╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ script.js ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
276 for (const el of els) { 277 right = Math.max(right, el.getBoundingClientRect().right - c.left + container.scrollLeft); 278 } 279 280 // vertical is the priority axis. keep the viewport in place when it 281 // already overlaps a tall widget; otherwise reveal its nearest edge. 282 if (bottom - top > container.clientHeight) { 283 container.scrollTop = Math.min(Math.max(container.scrollTop, top), bottom - container.clientHeight); 284 } else if (top < container.scrollTop) { 285 container.scrollTop = top; 286 } else if (bottom > container.scrollTop + container.clientHeight) { 287 container.scrollTop = bottom - container.clientHeight; 288 } 289 // horizontal-only scrolls are rows of controls, so reveal either 290 // edge. other scrolls reveal only the row's start; chasing varying 291 // right edges while moving vertically would yank their view sideways. 292 if (left < container.scrollLeft) { 293 container.scrollLeft = left; 294 } else if (container.dataset.scrollDirection === "horiz" && right > container.scrollLeft + container.clientWidth) { 295 container.scrollLeft = right - container.clientWidth; 296 } 297 }, 298 }, 299 }; 300 301 function sendTextInputValue(focusId, value) { 302 const bytes = encoder.encode(value); 303 const ptr = wasmInstance.exports._alloc(bytes.length); 304 // an empty allocation's pointer lies outside the buffer, so a view onto it 305 // would throw and leave the wasm holding the old value 306 if (bytes.length > 0) { 307 new Uint8Array(wasmInstance.exports.memory.buffer, ptr, bytes.length).set(bytes); 308 } 309 wasmInstance.exports._setTextInputValue(focusId, ptr, bytes.length); 310 wasmInstance.exports._tick(minRows(), maxCols()); 311 } 312 313 // upload as soon as the file dialog returns, so attaching is one click. the 314 // file is the whole request body, so its name travels in a header. 315 document.addEventListener("change", (event) => { 316 const t = event.target; 317 if (!t || t.tagName !== "INPUT" || t.type !== "file" || !t.dataset.action) return; 318 const file = t.files[0]; 319 if (!file) return; 320 fetch(t.dataset.action, { 321 method: "POST", 322 headers: { "x-attachment-name": encodeURIComponent(file.name) }, 323 body: file, 324 }).then((res) => { 325 if (res.ok) window.location.reload(); 326 else res.text().then((message) => console.error("upload failed:", message)); 327 }); 328 }); 329 330 WebAssembly.instantiateStreaming(fetch("/haxy.wasm"), importObject).then(async (result) => { 331 wasmInstance = result.instance; 332 333 // wait for the @font-face to load before measuring cell width, otherwise 334 // we get the fallback font's metrics. 335 if (document.fonts && document.fonts.ready) { 336 await document.fonts.ready; 337 } 338 339 // the page is embedded as raw json in a script tag. 340 // encode to utf-8 bytes and hand it to the wasm. 341 const jsonBytes = encoder.encode(pageJson); 342 const ptr = wasmInstance.exports._alloc(jsonBytes.length); 343 new Uint8Array(wasmInstance.exports.memory.buffer, ptr, jsonBytes.length).set(jsonBytes); 344 wasmInstance.exports._init(ptr, jsonBytes.length, minRows(), maxCols()); 345 346 document.addEventListener("keydown", (event) => { 347 // let the browser handle modifier combos. the TUI only uses unmodified keys, 348 // so intercepting these would just break normal browser navigation. 349 if (event.altKey || event.ctrlKey || event.metaKey) return; 350 // when a form element (text input or submit button) owns focus, the 351 // browser handles typing and Tab natively (and Enter on the button 352 // submits). we only forward keys into the TUI when focus is elsewhere. 353 // exception: arrow up/down on an input unfocuses it and forwards 354 // the event so the TUI can move focus between widgets. 355 if (document.activeElement) { 356 const tag = document.activeElement.tagName; 357 const isArrow = event.key === "ArrowUp" || event.key === "ArrowDown" || 358 event.key === "ArrowLeft" || event.key === "ArrowRight"; 359 // a file picker is an input but behaves like a button: every key 360 // but an arrow is native (enter opens its dialog), and an arrow 361 // navigates away from it 362 if (tag === "INPUT" && document.activeElement.type === "file") { 363 if (!isArrow) return; 364 document.activeElement.blur(); 365 } else if (tag === "INPUT" && document.activeElement.readOnly && 366 ["Enter", "ArrowLeft", "ArrowRight"].includes(event.key)) { 367 event.preventDefault(); 368 if (event.key === "Enter") { 369 document.activeElement.select(); 370 return; 371 } 372 wasmInstance.exports._onKeyDown(event.keyCode); 373 wasmInstance.exports._tick(minRows(), maxCols()); 374 return; 375 } else if (tag === "INPUT" && event.key === "Enter") { 376 // enter in a form's text input never submits it; only the 377 // submit button does. an input outside a form has no submit 378 // button, so the TUI handles its enter instead. 379 event.preventDefault(); 380 if (!document.activeElement.form) { 381 wasmInstance.exports._onKeyDown(event.keyCode); 382 wasmInstance.exports._tick(minRows(), maxCols()); 383 } 384 return; 385 } else if (tag === "INPUT" && !document.activeElement.form && !document.activeElement.readOnly && 386 (event.key === "Tab" && !event.shiftKey || 387 event.key === "ArrowRight" && caretAtEnd(document.activeElement))) { 388 // an input outside a form (a search box) reaches the widgets 389 // beside it: Tab, and ArrowRight once the caret is at the end. 390 // blurred first, so the next key isn't the input's; the tick 391 // refocuses it if the TUI focus stayed. 392 event.preventDefault(); 393 document.activeElement.blur(); 394 wasmInstance.exports._onKeyDown(event.keyCode); 395 wasmInstance.exports._tick(minRows(), maxCols()); 396 return; 397 } else if (tag === "INPUT" && (event.key === "ArrowUp" || event.key === "ArrowDown")) { 398 // up/down moves between widgets; left/right stay in the input 399 document.activeElement.blur(); 400 } else if (tag === "BUTTON" && isArrow) { 401 // arrows navigate away from a focused submit button 402 document.activeElement.blur(); 403 } else if (tag === "INPUT" || tag === "BUTTON" || tag === "TEXTAREA") { 404 // Enter (submit / newline), typing, Tab, and a textarea's 405 // arrows — handled natively by the browser 406 return; 407 } 408 } 409 if ((event.key === "ArrowUp" || event.key === "ArrowDown") && scrollFocusedWidget(event.key === "ArrowDown")) { 410 event.preventDefault(); 411 return; 412 } 413 if ((event.key === "PageUp" || event.key === "PageDown") && pageFocusedScroll(event.key === "PageDown")) { 414 event.preventDefault(); 415 return; 416 } 417 if (["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "PageUp", "PageDown", "Home", "End"].includes(event.key)) { 418 event.preventDefault(); 419 } 420 wasmInstance.exports._onKeyDown(event.keyCode); 421 wasmInstance.exports._tick(minRows(), maxCols()); 422 }); 423 424 // delegated listeners survive cell-layer replacement. the controls 425 // themselves are retained inside #grid. 426 document.addEventListener("input", (event) => { 427 const t = event.target; 428 if (!t || (t.tagName !== "INPUT" && t.tagName !== "TEXTAREA") || !t.dataset.focusId) return; 429 sendTextInputValue(Number(t.dataset.focusId), t.value); 430 }); 431 432 document.addEventListener("focusin", (event) => { 433 const t = event.target; 434 if (!t || !t.dataset || !t.dataset.focusId) return; 435 // sync the wasm focus to wherever the browser moved focus 436 wasmInstance.exports._setFocus(Number(t.dataset.focusId)); 437 // keyed controls survive a redraw before click. cell links live in the 438 // replaceable backdrop, so their click handler redraws after activation. 439 if (nodeKey(t)?.startsWith("control:")) wasmInstance.exports._tick(minRows(), maxCols()); 440 }); 441 442 grid.addEventListener("click", (event) => { 443 if (event.target.matches("input[readonly]")) { 444 wasmInstance.exports._tick(minRows(), maxCols()); 445 return; 446 } 447 // a link inside a cell's text opens in a new tab on its own 448 if (event.target.closest("a.cell-link")) return; 449 const span = event.target.closest(".clickable"); 450 if (!span) return; 451 // leave modified / non-left clicks (open-in-new-tab, etc.) to the browser 452 if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return; 453 // wasm routes the click: an in-page link updates focus and re-renders, 454 // a cross-page one calls _navigate. suppress the <a>'s own navigation so 455 // an in-page link doesn't also trigger a redundant full page load. 456 event.preventDefault(); 457 const focusId = Number(span.dataset.focusId); 458 wasmInstance.exports._onMouseClick(focusId); 459 wasmInstance.exports._tick(minRows(), maxCols()); 460 }); 461 462 let resizeTimer = null; 463 window.addEventListener("resize", () => { 464 clearTimeout(resizeTimer); 465 resizeTimer = setTimeout(() => { 466 wasmInstance.exports._tick(minRows(), maxCols()); 467 }, 100); 468 }); 469 }); 470