node and browser bindings for gleam
at master 172 lines 3.0 kB view raw
1import { Ok, Error } from "./gleam.mjs"; 2 3export function self() { 4 return globalThis; 5} 6 7export function alert(message) { 8 window.alert(message); 9} 10 11export function prompt(message, defaultValue) { 12 let text = window.prompt(message, defaultValue); 13 if (text !== null) { 14 return new Ok(text); 15 } else { 16 return new Error(); 17 } 18} 19 20export function addEventListener(type, listener) { 21 return window.addEventListener(type, listener); 22} 23 24export function document(window) { 25 return window.document; 26} 27 28export async function requestWakeLock() { 29 try { 30 return new Ok(await window.navigator.wakeLock.request("screen")); 31 } catch (error) { 32 return new Error(error.toString()); 33 } 34} 35 36export function location() { 37 return window.location.href; 38} 39 40export function locationOf(w) { 41 try { 42 return new Ok(w.location.href); 43 } catch (error) { 44 return new Error(error.toString()); 45 } 46} 47 48export function setLocation(w, url) { 49 w.location.href = url; 50} 51 52export function origin() { 53 return window.location.origin; 54} 55 56export function pathname() { 57 return window.location.pathname; 58} 59 60export function reload() { 61 return window.location.reload(); 62} 63 64export function reloadOf(w) { 65 return w.location.reload(); 66} 67 68export function focus(w) { 69 return w.focus(); 70} 71 72export function getHash() { 73 const hash = window.location.hash; 74 if (hash == "") { 75 return new Error(); 76 } 77 78 return new Ok(decodeURIComponent(hash.slice(1))); 79} 80 81export function getSearch() { 82 const search = window.location.search; 83 if (search == "") { 84 return new Error(); 85 } 86 87 return new Ok(decodeURIComponent(search.slice(1))); 88} 89 90export function innerHeight(w) { 91 return w.innerHeight; 92} 93 94export function innerWidth(w) { 95 return w.innerWidth; 96} 97 98export function outerHeight(w) { 99 return w.outerHeight; 100} 101 102export function outerWidth(w) { 103 return w.outerWidth; 104} 105 106export function screenX(w) { 107 return w.screenX; 108} 109 110export function screenY(w) { 111 return w.screenY; 112} 113 114export function screenTop(w) { 115 return w.screenTop; 116} 117 118export function screenLeft(w) { 119 return w.screenLeft; 120} 121 122export function scrollX(w) { 123 return w.scrollX; 124} 125 126export function scrollY(w) { 127 return w.scrollY; 128} 129 130export function open(url, target, features) { 131 try { 132 return new Ok(window.open(url, target, features)); 133 } catch (error) { 134 return new Error(error.toString()); 135 } 136} 137 138export function close(w) { 139 w.close(); 140} 141 142export function closed(w) { 143 return w.closed; 144} 145 146export function queueMicrotask(callback) { 147 return window.queueMicrotask(callback); 148} 149 150export function requestAnimationFrame(callback) { 151 return window.requestAnimationFrame(callback); 152} 153 154export function cancelAnimationFrame(callback) { 155 return window.cancelAnimationFrame(callback); 156} 157 158export function eval_(string) { 159 try { 160 return new Ok(eval(string)); 161 } catch (error) { 162 return new Error(error.toString()); 163 } 164} 165 166export async function import_(string) { 167 try { 168 return new Ok(await import(string)); 169 } catch (error) { 170 return new Error(error.toString()); 171 } 172}