-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathessential.js
412 lines (393 loc) · 11.9 KB
/
essential.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Small functions that don't do anything specific to HoloPrint's main functionality
import stripJsonComments from "strip-json-comments";
/** @returns {Element|null} */
export const selectEl = selector => document.querySelector(selector);
/** @returns {NodeListOf<HTMLElement>} */
export const selectEls = selector => document.querySelectorAll(selector);
Element.prototype.selectEl = DocumentFragment.prototype.selectEl = function(query) {
return this.querySelector(query);
};
Element.prototype.selectEls = DocumentFragment.prototype.selectEls = function(query) {
return this.querySelectorAll(query);
};
Element.prototype.getAllChildren = DocumentFragment.prototype.getAllChildren = function() {
let children = [...this.selectEls("*")];
let allChildren = [];
while(children.length) {
let child = children.shift();
allChildren.push(child);
if(child.shadowRoot) {
allChildren.push(...child.shadowRoot.selectEls("*"));
}
}
return allChildren;
};
EventTarget.prototype.onEvent = EventTarget.prototype.addEventListener;
EventTarget.prototype.onEventAndNow = function(type, listener, options) {
listener();
this.addEventListener(type, listener, options);
};
Response.prototype.jsonc = Blob.prototype.jsonc = function() {
return new Promise((res, rej) => {
this.text().then(text => {
let safeText = stripJsonComments(text);
let json;
try {
json = JSON.parse(safeText);
} catch(e) {
rej(e);
return;
}
res(json);
}).catch(e => rej(e));
});
};
Response.prototype.toImage = async function() {
let imageBlob = await this.blob();
let imageUrl = URL.createObjectURL(imageBlob);
let image = new Image();
image.src = imageUrl;
try {
await image.decode();
} catch { // Chrome puts arbitrary limits on decoding images because "an error doesn't necessarily mean that the image was invalid": https://issues.chromium.org/issues/40676514 🤦♂️
return new Promise((res, rej) => { // possibly https://github.com/chromium/chromium/blob/874a0ba26635507d1e847600fd8a512f4a10e1f8/cc/tiles/gpu_image_decode_cache.cc#L91
let image2 = new Image();
image2.onEvent("load", () => {
URL.revokeObjectURL(imageUrl);
res(image2);
});
image2.onEvent("error", e => {
URL.revokeObjectURL(imageUrl);
rej(`Failed to load image from response with status ${this.status} from URL ${this.url}: ${e}`);
});
image2.src = imageUrl;
});
}
URL.revokeObjectURL(imageUrl);
return image;
};
Image.prototype.toImageData = function() {
let can = new OffscreenCanvas(this.width, this.height);
let ctx = can.getContext("2d");
ctx.drawImage(this, 0, 0);
return ctx.getImageData(0, 0, can.width, can.height);
};
ImageData.prototype.toImage = async function() {
let can = new OffscreenCanvas(this.width, this.height);
let ctx = can.getContext("2d");
ctx.putImageData(this, 0, 0);
let blob = await can.convertToBlob();
let imageUrl = URL.createObjectURL(blob);
let image = new Image();
image.src = imageUrl;
try {
await image.decode();
} catch {
return new Promise((res, rej) => {
let image2 = new Image();
image2.onEvent("load", () => {
URL.revokeObjectURL(imageUrl);
res(image2);
});
image2.onEvent("error", e => {
URL.revokeObjectURL(imageUrl);
rej(`Failed to decode ImageData with dimensions ${this.width}x${this.height}: ${e}`);
});
image2.src = imageUrl;
});
}
URL.revokeObjectURL(imageUrl);
return image;
};
Image.prototype.toBlob = async function() {
let can = new OffscreenCanvas(this.width, this.height);
let ctx = can.getContext("2d");
ctx.drawImage(this, 0, 0);
return await can.convertToBlob();
};
Image.prototype.setOpacity = async function(opacity) {
let imageData = this.toImageData();
let data = imageData.data;
for(let i = 0; i < data.length; i += 4) {
data[i + 3] *= opacity;
}
return await imageData.toImage();
};
Image.prototype.addTint = async function(col) {
let imageData = this.toImageData();
let data = imageData.data;
for(let i = 0; i < data.length; i += 4) {
data[i] *= col[0];
data[i + 1] *= col[1];
data[i + 2] *= col[2];
}
return await imageData.toImage();
};
Blob.prototype.toImage = function() {
return new Promise((res, rej) => {
let img = new Image();
let url = URL.createObjectURL(this);
img.onEvent("load", () => {
URL.revokeObjectURL(url);
res(img);
});
img.onEvent("error", e => {
URL.revokeObjectURL(url);
rej(e);
});
img.src = url;
});
};
export const sleep = async time => new Promise(resolve => setTimeout(resolve, time));
export const { min, max, floor, ceil, sqrt, round, abs, PI: pi, exp } = Math;
export const clamp = (n, lowest, highest) => min(max(n, lowest), highest);
export const lerp = (a, b, x) => a + (b - a) * x;
export const nanToUndefined = x => Number.isNaN(x)? undefined : x;
export function range(a, b, c) {
if(b == undefined && c == undefined) {
return (new Array(a + 1)).fill().map((x, i) => i);
} else if(c == undefined) {
return (new Array(b - a + 1)).fill().map((x, i) => i + a);
} else {
return (new Array((b - a) / c + 1)).fill().map((x, i) => i * c + a);
}
}
export function random(arr) {
return arr[~~(Math.random() * arr.length)];
}
/**
* Create a pseudo-enumeration using numbers.
* @param {Array<String>} keys
* @returns {Readonly<Record<String, Number>>}
*/
export function createEnum(keys) {
return Object.freeze(Object.fromEntries(keys.map((key, i) => [key, i])));
}
export function hexColorToClampedTriplet(hexColor) {
let [, r, g, b] = hexColor.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i);
return [r, g, b].map(x => parseInt(x, 16) / 255);
}
export function addOrdinalSuffix(num) {
return num + (num % 10 == 1 && num % 100 != 11? "st" : num % 10 == 2 && num % 100 != 12? "nd" : num % 10 == 3 && num % 100 != 13? "rd" : "th");
}
export function htmlCodeToElement(htmlCode) {
return (new DOMParser()).parseFromString(htmlCode, "text/html").body.firstElementChild;
}
export function stringToImageData(text, textCol = "black", backgroundCol = "white", font = "12px monospace") {
let can = new OffscreenCanvas(0, 20);
let ctx = can.getContext("2d");
ctx.font = font;
can.width = ctx.measureText(text).width;
ctx.fillStyle = backgroundCol;
ctx.fillRect(0, 0, can.width, can.height);
ctx.fillStyle = textCol;
ctx.font = font;
ctx.fillText(text, 0, 15);
return ctx.getImageData(0, 0, can.width, can.height);
}
let translationLanguages = {};
export async function loadTranslationLanguage(language) {
translationLanguages[language] ??= await fetch(`translations/${language}.json`).then(res => res.jsonc()).catch(() => {
console.warn(`Failed to load language ${language} for translations!`);
return {};
});
}
/**
* Looks up a translation from translations/`language`.json
* @param {String} translationKey
* @param {String} language
* @returns {String|undefined}
*/
export function translate(translationKey, language) {
if(!(language in translationLanguages)) {
console.error(`Language ${language} not loaded for translation!`);
return undefined;
}
return translationLanguages[language][translationKey]?.replaceAll(/`([^`]+)`/g, "<code>$1</code>")?.replaceAll(/\[([^\]]+)\]\(([^\)]+)\)/g, `<a href="$2" target="_blank">$1</a>`);
}
export function getStackTrace(e = new Error()) {
return e.stack.split("\n").slice(1).removeFalsies();
}
/**
* Returns the SHA-256 hash of a blob.
* @param {Blob} blob
* @returns {Promise<Uint8Array>}
*/
export async function sha256(blob) {
return new Uint8Array(await crypto.subtle.digest("SHA-256", await blob.arrayBuffer()));
}
export async function sha256text(text) {
return new Uint8Array(await crypto.subtle.digest("SHA-256", (new TextEncoder()).encode(text)));
}
Uint8Array.prototype.toHexadecimalString = function() {
return [...this].map(ch => ch.toString(16).padStart(2, "0")).join("");
};
Array.prototype.removeFalsies = function() {
return this.filter(el => el);
};
export function concatenateFiles(files, name) {
return new File(files, name ?? files.map(file => file.name).join(","));
}
CacheStorage.prototype.clear = async function() {
(await this.keys()).forEach(cacheName => this.delete(cacheName));
};
export async function awaitAllEntries(object) {
await Promise.all(Object.entries(object).map(async ([key, promise]) => {
object[key] = await promise;
}));
return object;
}
/**
* Returns the two factors of a number which are closest to each other.
* @param {Number} n
* @returns {[Number, Number]}
*/
export function closestFactorPair(n) {
let x = ceil(sqrt(n));
while(n % x) x++;
return [x, n / x];
}
export function downloadBlob(blob, fileName) {
let a = document.createElement("a");
let objectURL = URL.createObjectURL(blob);
a.href = objectURL;
a.download = fileName ?? blob.name; // blob will have a name if blob is a File
a.click();
URL.revokeObjectURL(objectURL);
}
export class JSONSet extends Set {
#replacer;
#reviver;
constructor(replacer, reviver) {
super();
this.#replacer = replacer;
this.#reviver = reviver;
}
indexOf(value) { // not part of sets normally! but they keep their order anyway so...
let stringifiedValues = [...super[Symbol.iterator]()];
return stringifiedValues.indexOf(this.#stringify(value));
}
add(value) {
return super.add(this.#stringify(value));
}
delete(value) {
return super.delete(this.#stringify(value));
}
has(value) {
return super.has(this.#stringify(value))
}
[Symbol.iterator]() {
let iter = super[Symbol.iterator]();
return {
next: () => {
let { value, done } = iter.next();
return {
value: done? undefined : this.#parse(value),
done
};
},
[Symbol.iterator]() {
return this;
}
};
}
entries() {
let iter = this[Symbol.iterator]();
return {
next: () => {
let { value, done } = iter.next();
return {
value: done? undefined : [value, value],
done
};
},
[Symbol.iterator]() {
return this;
}
};
}
keys() {
return this[Symbol.iterator]();
}
values() {
return this[Symbol.iterator]();
}
#stringify(value) {
return JSON.stringify(value, this.#replacer);
}
#parse(value) {
return JSON.parse(value, this.#reviver);
}
}
export class JSONMap extends Map { // very barebones
#replacer;
constructor(replacer) {
super();
this.#replacer = replacer;
}
get(key) {
return super.get(this.#stringify(key));
}
has(key) {
return super.has(this.#stringify(key));
}
set(key, value) {
return super.set(this.#stringify(key), value)
}
#stringify(value) {
return JSON.stringify(value, this.#replacer);
}
}
export class CachingFetcher {
cacheName;
#baseUrl;
#cache;
constructor(cacheName, baseUrl = "") {
return (async () => {
this.#cache = await caches.open(cacheName);
this.#baseUrl = baseUrl;
this.cacheName = cacheName;
return this;
})();
}
/**
* Fetches a file, checking first against cache.
* @param {String} url
* @returns {Promise<Response>}
*/
async fetch(url) {
let fullUrl = this.#baseUrl + url;
let cacheLink = `https://cache/${url}`;
let res = await this.#cache.match(cacheLink);
if(!res) {
res = await this.retrieve(fullUrl);
this.#cache.put(cacheLink, res.clone()).catch(e => console.warn(`Failed to save response from ${fullUrl} to cache ${this.cacheName}:`, e));
}
return res;
}
/**
* Actually load a file, for when it's not found in cache.
* @param {String} url
* @returns {Promise<Response>}
*/
async retrieve(url) {
const maxFetchAttempts = 3;
const fetchRetryTimeout = 500; // ms
let lastError;
for(let i = 0; i < maxFetchAttempts; i++) {
try {
return await fetch(url);
} catch(e) {
if(navigator.onLine && e instanceof TypeError && e.message == "Failed to fetch") { // random Chrome issue when fetching many images at the same time. observed when fetching 1600 images at the same time.
console.debug(`Failed to fetch resource at ${url}, trying again in ${fetchRetryTimeout}ms`);
lastError = e;
await sleep(fetchRetryTimeout);
} else {
throw e;
}
}
}
console.error(`Failed to fetch resource at ${url} after ${maxFetchAttempts} attempts...`);
throw lastError;
}
}