╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ script.js ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
76 currentOverlay = html; 77 overlay.innerHTML = html; 78 }, 79 }, 80 }; 81 82 function sendTextInputValue(focusId, value) { 83 const bytes = encoder.encode(value); 84 const ptr = wasmInstance.exports._alloc(bytes.length); 85 new Uint8Array(wasmInstance.exports.memory.buffer, ptr, bytes.length).set(bytes); 86 wasmInstance.exports._setTextInputValue(focusId, ptr, bytes.length); 87 wasmInstance.exports._tick(minRows(), maxCols()); 88 } 89 90 WebAssembly.instantiateStreaming(fetch("haxy.wasm"), importObject).then(async (result) => { 91 wasmInstance = result.instance; 92 93 // wait for the @font-face to load before measuring cell width, otherwise 94 // we get the fallback font's metrics. 95 if (document.fonts && document.fonts.ready) { 96 await document.fonts.ready; 97 } 98 99 // the page is embedded as base64 so it can sit inside the host html 100 // without worrying about characters that would terminate the script tag. 101 // decode here and hand the raw json bytes to the wasm. 102 const jsonBytes = Uint8Array.from(atob(pageJsonBase64), (c) => c.charCodeAt(0)); 103 const ptr = wasmInstance.exports._alloc(jsonBytes.length); 104 new Uint8Array(wasmInstance.exports.memory.buffer, ptr, jsonBytes.length).set(jsonBytes); 105 wasmInstance.exports._start(ptr, jsonBytes.length, minRows(), maxCols()); 106 107 document.addEventListener("keydown", (event) => { 108 // when a form element (text input or submit button) owns focus, 109 // the browser handles typing, Enter-to-submit, and Tab natively. 110 // we only forward keys into the TUI when focus is elsewhere. 111 if (document.activeElement) { 112 const tag = document.activeElement.tagName; 113 if (tag === "INPUT" || tag === "BUTTON") return; 114 } 115 if (["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "PageUp", "PageDown", "Home", "End"].includes(event.key)) { 116 event.preventDefault(); 117 } 118 wasmInstance.exports._onKeyDown(event.keyCode); 119 wasmInstance.exports._tick(minRows(), maxCols()); 120 }); 121 122 // listeners on document (not #grid) since the form lives outside #grid; 123 // input/focus events from form elements still bubble up to document. 124 document.addEventListener("input", (event) => { 125 const t = event.target; 126 if (!t || t.tagName !== "INPUT" || !t.dataset.focusId) return; 127 sendTextInputValue(Number(t.dataset.focusId), t.value); 128 }); 129 130 document.addEventListener("focusin", (event) => { 131 const t = event.target; 132 if (!t || !t.dataset || !t.dataset.focusId) return; 133 wasmInstance.exports._onMouseClick(Number(t.dataset.focusId)); 134 wasmInstance.exports._tick(minRows(), maxCols()); 135 }); 136 137 grid.addEventListener("click", (event) => { 138 const span = event.target.closest(".clickable"); 139 if (!span) return; 140 const focusId = Number(span.dataset.focusId); 141 wasmInstance.exports._onMouseClick(focusId); 142 wasmInstance.exports._tick(minRows(), maxCols()); 143 }); 144 145 let resizeTimer = null; 146 window.addEventListener("resize", () => { 147 clearTimeout(resizeTimer); 148 resizeTimer = setTimeout(() => { 149 wasmInstance.exports._tick(minRows(), maxCols()); 150 }, 100); 151 }); 152 }); 153