╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ script.js ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
100 wasmInstance.exports._setTextInputValue(focusId, ptr, bytes.length); 101 wasmInstance.exports._tick(minRows(), maxCols()); 102 } 103 104 WebAssembly.instantiateStreaming(fetch("haxy.wasm"), importObject).then(async (result) => { 105 wasmInstance = result.instance; 106 107 // wait for the @font-face to load before measuring cell width, otherwise 108 // we get the fallback font's metrics. 109 if (document.fonts && document.fonts.ready) { 110 await document.fonts.ready; 111 } 112 113 // the page is embedded as base64 so it can sit inside the host html 114 // without worrying about characters that would terminate the script tag. 115 // decode here and hand the raw json bytes to the wasm. 116 const jsonBytes = Uint8Array.from(atob(pageJsonBase64), (c) => c.charCodeAt(0)); 117 const ptr = wasmInstance.exports._alloc(jsonBytes.length); 118 new Uint8Array(wasmInstance.exports.memory.buffer, ptr, jsonBytes.length).set(jsonBytes); 119 wasmInstance.exports._init(ptr, jsonBytes.length, minRows(), maxCols()); 120 121 document.addEventListener("keydown", (event) => { 122 // when a form element (text input or submit button) owns focus, 123 // the browser handles typing, Enter-to-submit, and Tab natively. 124 // we only forward keys into the TUI when focus is elsewhere. 125 // exception: arrow up/down on an input unfocuses it and forwards 126 // the event so the TUI can move focus between widgets. 127 if (document.activeElement) { 128 const tag = document.activeElement.tagName; 129 if (tag === "INPUT" && (event.key === "ArrowUp" || event.key === "ArrowDown")) { 130 document.activeElement.blur(); 131 } else if (tag === "INPUT" || tag === "BUTTON") { 132 return; 133 } 134 } 135 if (["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "PageUp", "PageDown", "Home", "End"].includes(event.key)) { 136 event.preventDefault(); 137 } 138 wasmInstance.exports._onKeyDown(event.keyCode); 139 wasmInstance.exports._tick(minRows(), maxCols()); 140 }); 141 142 // listeners on document (not #grid) since the form lives outside #grid; 143 // input/focus events from form elements still bubble up to document. 144 document.addEventListener("input", (event) => { 145 const t = event.target; 146 if (!t || t.tagName !== "INPUT" || !t.dataset.focusId) return; 147 sendTextInputValue(Number(t.dataset.focusId), t.value); 148 }); 149 150 document.addEventListener("focusin", (event) => { 151 const t = event.target; 152 if (!t || !t.dataset || !t.dataset.focusId) return; 153 wasmInstance.exports._onMouseClick(Number(t.dataset.focusId)); 154 wasmInstance.exports._tick(minRows(), maxCols()); 155 }); 156 157 grid.addEventListener("click", (event) => { 158 const span = event.target.closest(".clickable"); 159 if (!span) return; 160 const focusId = Number(span.dataset.focusId); 161 wasmInstance.exports._onMouseClick(focusId); 162 wasmInstance.exports._tick(minRows(), maxCols()); 163 }); 164 165 let resizeTimer = null; 166 window.addEventListener("resize", () => { 167 clearTimeout(resizeTimer); 168 resizeTimer = setTimeout(() => { 169 wasmInstance.exports._tick(minRows(), maxCols()); 170 }, 100); 171 }); 172 }); 173