/** * Home page ts/js * * @module */ import * as libfewd from './libfewd.js' //------------------------------------------------------------------ // page element stuff //------------------------------------------------------------------ main(); function main () : void { let ielt : HTMLInputElement = document.getElementById('wfc-input') as HTMLInputElement ; let oelt : HTMLTextAreaElement = document.getElementById('wfc-output') as HTMLTextAreaElement ; let cb_resize : HTMLInputElement = document.getElementById('auto-resize-output') as HTMLInputElement ; let cb_scroll : HTMLInputElement = document.getElementById('auto-scroll') as HTMLInputElement ; let MAX_OELT_HEIGHT : number = 300; ielt.addEventListener('keydown', function(e: KeyboardEvent) { on_input_key(e, ielt, oelt, cb_resize, cb_scroll, MAX_OELT_HEIGHT); } ); } // when user hits any key async function on_input_key (evt : KeyboardEvent, ielt : HTMLInputElement, oelt : HTMLTextAreaElement, cb_resize : HTMLInputElement, cb_scroll : HTMLInputElement, max_height : number) : Promise { if (evt.key === 'Enter') { // don't do default thing evt.preventDefault(); // grab contents let contents : string = ielt.value; let trimmed : string = contents.trim(); let nonempty : boolean = trimmed.length > 0; // if contents are nonempty if (nonempty) { // clear input ielt.value = ''; // put in output oelt.value += '> ' + trimmed + '\n'; oelt.hidden = false; // query backend for result let result : wfcout = await fetch_wfcin(trimmed); if (result.ok) oelt.value += result.result; else oelt.value += result.error; oelt.value += '\n'; // auto-resize libfewd.auto_resize(cb_resize, oelt, max_height); libfewd.auto_scroll_to_bottom(cb_scroll, oelt); } } } //------------------------------------------------------------------ // wfc api //------------------------------------------------------------------ type ok_err = {ok: true, result: t} | {ok: false, error: string}; type wfcin = {wfcin: string}; type wfcout = ok_err; function assert (condition : boolean, fail_msg : string) : void { if(!condition) throw new Error(fail_msg); } async function fetch_wfcin (user_line : string) : Promise { let req_body_obj = {wfcin: user_line}; let req_body_str = JSON.stringify(req_body_obj); let req_options = {method: 'POST', headers: {'content-type': 'application/json'}, body: req_body_str}; // default result = somehow neither branch of code below was run(?) // putting this here so ts doesn't chimp out let result: wfcout = {ok : false, error : 'IT DO BE LIKE THAT MISTA STANCIL'}; try { let response : Response = await fetch('/wfcin', req_options); if (response.ok) result = await response.json() as wfcout; else { console.log('bad http response:', response); result = {ok: false, error: 'BAD HTTP RESPONSE'}; } } catch (x: any) { console.log('network error:', x); result = {ok: false, error: 'NETWORK ERROR'}; } return result; }