@monster-orange/den-ui 0.5.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +515 -1
  2. package/bin/den-ui-parity.mjs +440 -0
  3. package/inventory/fonts-dir.cjs +8 -0
  4. package/inventory/fonts-dir.mjs +10 -0
  5. package/inventory/nav-inventory.d.ts +133 -0
  6. package/inventory/nav-inventory.js +839 -0
  7. package/inventory/reference/den-chromium-1280.json +5388 -0
  8. package/inventory/reference/den-chromium-390.json +3834 -0
  9. package/inventory/reference/den-webkit-1280.json +5388 -0
  10. package/inventory/reference/den-webkit-390.json +3834 -0
  11. package/package.json +40 -3
  12. package/src/busy-words.d.ts +2 -0
  13. package/src/busy-words.js +6 -0
  14. package/src/busy.css +155 -0
  15. package/src/busy.d.ts +80 -0
  16. package/src/busy.js +105 -35
  17. package/src/dialog.css +103 -0
  18. package/src/dialog.d.ts +44 -0
  19. package/src/dialog.js +95 -30
  20. package/src/icons.d.ts +4 -0
  21. package/src/icons.js +43 -0
  22. package/src/kit.d.ts +217 -0
  23. package/src/kit.js +299 -0
  24. package/src/marks/den.svg +6 -0
  25. package/src/report-shot.d.ts +47 -0
  26. package/src/report-shot.js +437 -0
  27. package/src/report.css +387 -0
  28. package/src/report.d.ts +61 -0
  29. package/src/report.js +909 -0
  30. package/src/shell-client.d.ts +149 -0
  31. package/src/shell-client.js +820 -0
  32. package/src/shell-html.d.ts +305 -0
  33. package/src/shell-html.js +903 -0
  34. package/src/shell.css +1010 -0
  35. package/src/strings.d.ts +18 -0
  36. package/src/strings.js +565 -0
  37. package/src/time.d.ts +18 -0
  38. package/src/time.js +14 -2
  39. package/src/tokens.css +81 -67
  40. package/src/update-pill.d.ts +17 -0
  41. package/src/version.d.ts +7 -0
  42. package/src/version.js +31 -0
  43. package/src/whatsnew.css +235 -0
  44. package/src/whatsnew.d.ts +36 -0
  45. package/src/whatsnew.js +411 -0
@@ -0,0 +1,437 @@
1
+ /**
2
+ * den-ui: the Report form's screenshot, and its mask.
3
+ *
4
+ * capture() takes the page as the person sees it, with any dialog, drawer
5
+ * or menu that is open, before the form opens (the form itself is left
6
+ * out). It uses html-to-image (MIT; the product serves it and names its
7
+ * URL with configureShots({ library })), loaded the first time. It never throws: a
8
+ * page that can't be captured, or that takes longer than `seconds`, gives
9
+ * null, and the form offers to send without one.
10
+ *
11
+ * Masks are rectangles in fractions of the image (0..1), so they hold at
12
+ * any size the image is shown at. masked() draws them in black onto a
13
+ * copy: what is sent is the copy, never the original.
14
+ *
15
+ * The screenshot never goes to the product's server. GitHub's API can't attach an
16
+ * image, so after sending it is saved as a file and copied, and the person
17
+ * pastes it into the issue.
18
+ */
19
+
20
+ let LIB = "";
21
+ let lib = null;
22
+
23
+ /** Where html-to-image is (a URL on the product's own origin). */
24
+ export function configureShots({ library } = {}) {
25
+ if (library && library !== LIB) {
26
+ LIB = library;
27
+ lib = null;
28
+ }
29
+ }
30
+
31
+ function loadLib() {
32
+ if (lib) return lib;
33
+ lib = new Promise((ok, no) => {
34
+ if (window.htmlToImage) return ok(window.htmlToImage);
35
+ if (!LIB) return no(new Error("html-to-image: no library configured"));
36
+ const sc = document.createElement("script");
37
+ sc.src = LIB;
38
+ sc.onload = () =>
39
+ window.htmlToImage
40
+ ? ok(window.htmlToImage)
41
+ : no(new Error("html-to-image"));
42
+ sc.onerror = () => no(new Error("html-to-image"));
43
+ document.head.append(sc);
44
+ });
45
+ lib.catch(() => (lib = null));
46
+ return lib;
47
+ }
48
+
49
+ function timeout(ms) {
50
+ return new Promise((_, no) =>
51
+ setTimeout(() => no(new Error("capture timed out")), ms),
52
+ );
53
+ }
54
+
55
+ /*
56
+ * Scroll (issue 295). html-to-image clones the page, and a clone starts at
57
+ * scrollTop 0: the window and every scrolled pane would show their top.
58
+ * So, before cloning, each scrolled element's children (and each sticky
59
+ * element) are tagged with a data attribute; after cloning, the SVG is
60
+ * edited so each tagged clone sits where the person sees it:
61
+ *
62
+ * - the window's scroll moves the root clone up (position: relative, top
63
+ * and left), so fixed parts (the top bar, the rail) stay put;
64
+ * - a scrolled element's children move up by its scrollTop/scrollLeft
65
+ * (position: relative, or their own top/left for positioned ones); its
66
+ * bare text is wrapped in a span that moves the same way. Nested panes
67
+ * add up, since each moves inside its parent;
68
+ * - a sticky element becomes position: relative with the offset sticky
69
+ * gave it on screen (measured by setting it static for one synchronous
70
+ * read, so nothing is painted).
71
+ *
72
+ * The image stays the window's size: what is on screen, not the page.
73
+ */
74
+ const TAG = "data-den-shot";
75
+
76
+ const px = (v) => {
77
+ const n = parseFloat(v);
78
+ return Number.isFinite(n) ? n : 0;
79
+ };
80
+
81
+ /**
82
+ * What to change in the clone: [{ el, css, text }], one per live element.
83
+ * `css` is appended to the clone's style; `text` ({ dx, dy }) moves the
84
+ * element's bare text.
85
+ */
86
+ function scrollPlan(skip) {
87
+ const se = document.scrollingElement || document.documentElement;
88
+ const steps = new Map(); // el -> { dx, dy, sticky, clip, text }
89
+ const step = (el) => {
90
+ let m = steps.get(el);
91
+ if (!m) {
92
+ m = { dx: 0, dy: 0, sticky: false, clip: false, text: null };
93
+ steps.set(el, m);
94
+ }
95
+ return m;
96
+ };
97
+ for (const el of document.body ? document.body.querySelectorAll("*") : []) {
98
+ if (el === se || skip(el)) continue;
99
+ if (getComputedStyle(el).position === "sticky") {
100
+ // Where sticky put it, against where it would be without: read with
101
+ // position static for one synchronous moment (nothing is painted).
102
+ const before = el.getBoundingClientRect();
103
+ const v = el.style.getPropertyValue("position");
104
+ const pr = el.style.getPropertyPriority("position");
105
+ el.style.setProperty("position", "static", "important");
106
+ const flat = el.getBoundingClientRect();
107
+ if (v) el.style.setProperty("position", v, pr);
108
+ else el.style.removeProperty("position");
109
+ const m = step(el);
110
+ m.sticky = true;
111
+ m.dx += before.left - flat.left;
112
+ m.dy += before.top - flat.top;
113
+ }
114
+ const st = el.scrollTop;
115
+ const sl = el.scrollLeft;
116
+ if (!st && !sl) continue;
117
+ // A clone can't scroll: no scrollbar (it would show the top), and its
118
+ // children move instead.
119
+ step(el).clip = true;
120
+ for (const c of el.childNodes) {
121
+ if (c.nodeType === 3 && c.nodeValue.trim())
122
+ step(el).text = { dx: -sl, dy: -st };
123
+ if (c.nodeType !== 1 || skip(c)) continue;
124
+ if (getComputedStyle(c).position === "fixed") continue;
125
+ const m = step(c);
126
+ m.dx -= sl;
127
+ m.dy -= st;
128
+ }
129
+ }
130
+ const plan = [];
131
+ for (const [el, m] of steps) {
132
+ let css = m.clip ? "overflow:hidden;" : "";
133
+ if (m.dx || m.dy || m.sticky) {
134
+ const cs = getComputedStyle(el);
135
+ if (cs.position === "absolute" || cs.position === "relative")
136
+ css +=
137
+ `top:${px(cs.top) + m.dy}px;bottom:${px(cs.bottom) - m.dy}px;` +
138
+ `left:${px(cs.left) + m.dx}px;right:${px(cs.right) - m.dx}px;`;
139
+ else
140
+ css += `position:relative;top:${m.dy}px;left:${m.dx}px;bottom:auto;right:auto;`;
141
+ // Sticky parts stay above the content that scrolls under them, now
142
+ // that the moved content is positioned too.
143
+ if (m.sticky && cs.zIndex === "auto") css += "z-index:1;";
144
+ }
145
+ if (css || m.text) plan.push({ el, css, text: m.text });
146
+ }
147
+ return plan;
148
+ }
149
+
150
+ /** The SVG (a data: URL) with each tagged clone changed as planned. */
151
+ function applyPlan(url, plan) {
152
+ const head = "data:image/svg+xml;charset=utf-8,";
153
+ const doc = new DOMParser().parseFromString(
154
+ decodeURIComponent(url.slice(head.length)),
155
+ "image/svg+xml",
156
+ );
157
+ if (doc.getElementsByTagName("parsererror").length) return url;
158
+ const byTag = new Map();
159
+ for (const n of doc.querySelectorAll(`[${TAG}]`)) {
160
+ byTag.set(n.getAttribute(TAG), n);
161
+ n.removeAttribute(TAG);
162
+ }
163
+ plan.forEach((step, i) => {
164
+ const n = byTag.get(String(i));
165
+ if (!n) return;
166
+ // Last in the attribute, so it wins over the copied computed style.
167
+ if (step.css)
168
+ n.setAttribute("style", `${n.getAttribute("style") || ""};${step.css}`);
169
+ if (!step.text) return;
170
+ for (const c of [...n.childNodes]) {
171
+ if (c.nodeType !== 3 || !c.nodeValue.trim()) continue;
172
+ const span = doc.createElementNS(n.namespaceURI, "span");
173
+ span.setAttribute(
174
+ "style",
175
+ `position:relative;top:${step.text.dy}px;left:${step.text.dx}px;`,
176
+ );
177
+ n.replaceChild(span, c);
178
+ span.append(c);
179
+ }
180
+ });
181
+ return head + encodeURIComponent(new XMLSerializer().serializeToString(doc));
182
+ }
183
+
184
+ function imageOf(url) {
185
+ return new Promise((ok, no) => {
186
+ const img = new Image();
187
+ img.onload = () =>
188
+ img
189
+ .decode()
190
+ .catch(() => {})
191
+ .then(() => requestAnimationFrame(() => ok(img)));
192
+ img.onerror = no;
193
+ img.decoding = "async";
194
+ img.src = url;
195
+ });
196
+ }
197
+
198
+ /**
199
+ * The page as shown now, as a canvas the size of the window, or null.
200
+ * `skip(el)`: elements to leave out (the form).
201
+ */
202
+ export async function capture({ seconds = 15, skip = () => false } = {}) {
203
+ let plan = [];
204
+ try {
205
+ const h2i = await Promise.race([loadLib(), timeout(seconds * 1000)]);
206
+ const root = document.documentElement;
207
+ const width = window.innerWidth;
208
+ const height = window.innerHeight;
209
+ const backgroundColor = getComputedStyle(document.body).backgroundColor;
210
+ plan = scrollPlan(skip);
211
+ // The window's scroll: the whole clone moves up (and left). Not
212
+ // html-to-image's `style` option: its inset-block, copied from the
213
+ // computed style, comes later in the attribute and wins over top.
214
+ const x = window.scrollX;
215
+ const y = window.scrollY;
216
+ if (x || y)
217
+ plan.push({
218
+ el: root,
219
+ css: `position:relative;top:${-y}px;left:${-x}px;bottom:auto;right:auto;`,
220
+ });
221
+ plan.forEach((step, i) => step.el.setAttribute(TAG, String(i)));
222
+ const opts = {
223
+ width,
224
+ height,
225
+ pixelRatio: 1,
226
+ // The page's own fonts are already there: not fetched again.
227
+ skipFonts: true,
228
+ backgroundColor,
229
+ filter: (n) => !(n.nodeType === 1 && skip(n)),
230
+ };
231
+ let canvas;
232
+ if (!plan.length) {
233
+ canvas = await Promise.race([
234
+ h2i.toCanvas(root, opts),
235
+ timeout(seconds * 1000),
236
+ ]);
237
+ } else {
238
+ const svg = await Promise.race([
239
+ h2i.toSvg(root, opts),
240
+ timeout(seconds * 1000),
241
+ ]);
242
+ for (const step of plan) step.el.removeAttribute(TAG);
243
+ const img = await Promise.race([
244
+ imageOf(applyPlan(svg, plan)),
245
+ timeout(seconds * 1000),
246
+ ]);
247
+ canvas = document.createElement("canvas");
248
+ canvas.width = width;
249
+ canvas.height = height;
250
+ const g = canvas.getContext("2d");
251
+ g.fillStyle = backgroundColor;
252
+ g.fillRect(0, 0, width, height);
253
+ g.drawImage(img, 0, 0, width, height);
254
+ }
255
+ return canvas && canvas.width > 0 && canvas.height > 0 ? canvas : null;
256
+ } catch {
257
+ return null;
258
+ } finally {
259
+ for (const step of plan) step.el.removeAttribute(TAG);
260
+ }
261
+ }
262
+
263
+ /** A copy of `canvas` with each mask (fractions of it) filled black. */
264
+ export function masked(canvas, masks) {
265
+ const out = document.createElement("canvas");
266
+ out.width = canvas.width;
267
+ out.height = canvas.height;
268
+ const g = out.getContext("2d");
269
+ g.drawImage(canvas, 0, 0);
270
+ // Black in every theme: a mask hides, it is not a theme colour.
271
+ g.fillStyle = "black";
272
+ for (const m of masks)
273
+ g.fillRect(
274
+ Math.floor(m.x * out.width),
275
+ Math.floor(m.y * out.height),
276
+ Math.ceil(m.w * out.width),
277
+ Math.ceil(m.h * out.height),
278
+ );
279
+ return out;
280
+ }
281
+
282
+ /** A mask from two points (fractions), the right way round, clamped. */
283
+ export function rectOf(a, b) {
284
+ const c = (v) => Math.min(1, Math.max(0, v));
285
+ const x = c(Math.min(a.x, b.x));
286
+ const y = c(Math.min(a.y, b.y));
287
+ return {
288
+ x,
289
+ y,
290
+ w: c(Math.max(a.x, b.x)) - x,
291
+ h: c(Math.max(a.y, b.y)) - y,
292
+ };
293
+ }
294
+
295
+ /** A mask from the keyboard form: percentages, clamped to the image. */
296
+ export function rectOfPercent(x, y, w, h) {
297
+ const n = (v) => Math.min(100, Math.max(0, Number(v) || 0)) / 100;
298
+ const r = { x: n(x), y: n(y), w: n(w), h: n(h) };
299
+ r.w = Math.min(r.w, 1 - r.x);
300
+ r.h = Math.min(r.h, 1 - r.y);
301
+ return r;
302
+ }
303
+
304
+ export function toBlob(canvas) {
305
+ return new Promise((ok) => canvas.toBlob(ok, "image/png"));
306
+ }
307
+
308
+ /**
309
+ * After sending: the masked screenshot copied (where the browser allows
310
+ * it) and saved as a file. Which of the two worked.
311
+ */
312
+ export async function saveAndCopy(canvas, name = "report-screenshot.png") {
313
+ const blob = await toBlob(canvas);
314
+ const out = { copied: false, saved: false };
315
+ if (!blob) return out;
316
+ try {
317
+ if (navigator.clipboard?.write && typeof ClipboardItem === "function") {
318
+ await navigator.clipboard.write([
319
+ new ClipboardItem({ "image/png": blob }),
320
+ ]);
321
+ out.copied = true;
322
+ }
323
+ } catch {
324
+ /* not allowed here: the file is still saved */
325
+ }
326
+ try {
327
+ const url = URL.createObjectURL(blob);
328
+ const a = document.createElement("a");
329
+ a.href = url;
330
+ a.download = name;
331
+ a.hidden = true;
332
+ document.body.append(a);
333
+ a.click();
334
+ a.remove();
335
+ setTimeout(() => URL.revokeObjectURL(url), 60_000);
336
+ out.saved = true;
337
+ } catch {
338
+ /* no download here */
339
+ }
340
+ return out;
341
+ }
342
+
343
+ /** An image (a Blob or a data: URL) back as a canvas, or null. */
344
+ export async function canvasOf(src) {
345
+ let url = "";
346
+ try {
347
+ url = typeof src === "string" ? src : URL.createObjectURL(src);
348
+ const img = new Image();
349
+ await new Promise((ok, no) => {
350
+ img.onload = ok;
351
+ img.onerror = no;
352
+ img.src = url;
353
+ });
354
+ const c = document.createElement("canvas");
355
+ c.width = img.naturalWidth;
356
+ c.height = img.naturalHeight;
357
+ c.getContext("2d").drawImage(img, 0, 0);
358
+ return c.width && c.height ? c : null;
359
+ } catch {
360
+ return null;
361
+ } finally {
362
+ if (url && typeof src !== "string") URL.revokeObjectURL(url);
363
+ }
364
+ }
365
+
366
+ /*
367
+ * Finish later's screenshot stays in this browser (IndexedDB), keyed by
368
+ * the draft's id, with its masks: the draft's text is on the product's server, the
369
+ * picture never is. Storage off or full: the draft keeps its text only.
370
+ */
371
+ const DB = "den-report";
372
+ const STORE = "shots";
373
+
374
+ function openDb() {
375
+ return new Promise((ok, no) => {
376
+ const r = indexedDB.open(DB, 1);
377
+ r.onupgradeneeded = () => r.result.createObjectStore(STORE);
378
+ r.onsuccess = () => ok(r.result);
379
+ r.onerror = () => no(r.error);
380
+ });
381
+ }
382
+
383
+ async function inStore(mode, f) {
384
+ const db = await openDb();
385
+ try {
386
+ return await new Promise((ok, no) => {
387
+ const tx = db.transaction(STORE, mode);
388
+ const req = f(tx.objectStore(STORE));
389
+ tx.oncomplete = () => ok(req.result);
390
+ tx.onerror = () => no(tx.error);
391
+ tx.onabort = () => no(tx.error);
392
+ });
393
+ } finally {
394
+ db.close();
395
+ }
396
+ }
397
+
398
+ /** Keeps a draft's screenshot (the original) and its masks. */
399
+ export async function keepShot(id, canvas, masks, noShot) {
400
+ try {
401
+ const blob = await toBlob(canvas);
402
+ if (!blob) return false;
403
+ await inStore("readwrite", (s) =>
404
+ s.put({ blob, masks, noShot: !!noShot }, id),
405
+ );
406
+ return true;
407
+ } catch {
408
+ return false;
409
+ }
410
+ }
411
+
412
+ /** A kept screenshot: { canvas, masks, noShot }, or null. */
413
+ export async function loadShot(id) {
414
+ try {
415
+ const v = await inStore("readonly", (s) => s.get(id));
416
+ if (!v || !v.blob) return null;
417
+ const canvas = await canvasOf(v.blob);
418
+ return canvas
419
+ ? {
420
+ canvas,
421
+ masks: Array.isArray(v.masks) ? v.masks : [],
422
+ noShot: !!v.noShot,
423
+ }
424
+ : null;
425
+ } catch {
426
+ return null;
427
+ }
428
+ }
429
+
430
+ /** Forgets a draft's screenshot (sent or discarded). */
431
+ export async function dropShot(id) {
432
+ try {
433
+ await inStore("readwrite", (s) => s.delete(id));
434
+ } catch {
435
+ /* nothing kept */
436
+ }
437
+ }