@monster-orange/den-ui 0.0.0-stage → 0.6.6
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.
- package/LICENSE +21 -0
- package/README.md +408 -2
- package/bin/den-ui-parity.mjs +365 -0
- package/inventory/nav-inventory.d.ts +89 -0
- package/inventory/nav-inventory.js +590 -0
- package/inventory/reference/den-chromium-1280.json +5137 -0
- package/inventory/reference/den-chromium-390.json +3653 -0
- package/inventory/reference/den-webkit-1280.json +5137 -0
- package/inventory/reference/den-webkit-390.json +3653 -0
- package/package.json +51 -4
- package/src/base.css +80 -0
- package/src/busy-words.d.ts +2 -0
- package/src/busy-words.js +71 -0
- package/src/busy.css +155 -0
- package/src/busy.d.ts +80 -0
- package/src/busy.js +662 -0
- package/src/dialog.css +103 -0
- package/src/dialog.d.ts +44 -0
- package/src/dialog.js +282 -0
- package/src/fonts/OFL.txt +97 -0
- package/src/fonts/rubik-latin-400-normal.woff2 +0 -0
- package/src/fonts/rubik-latin-500-normal.woff2 +0 -0
- package/src/fonts/rubik-latin-ext-400-normal.woff2 +0 -0
- package/src/fonts/rubik-latin-ext-500-normal.woff2 +0 -0
- package/src/fonts/space-mono-latin-400-normal.woff2 +0 -0
- package/src/fonts/space-mono-latin-ext-400-normal.woff2 +0 -0
- package/src/fonts.css +71 -0
- package/src/icons.d.ts +4 -0
- package/src/icons.js +43 -0
- package/src/kit.d.ts +146 -0
- package/src/kit.js +224 -0
- package/src/report-shot.d.ts +47 -0
- package/src/report-shot.js +255 -0
- package/src/report.css +387 -0
- package/src/report.d.ts +61 -0
- package/src/report.js +909 -0
- package/src/shell-client.d.ts +31 -0
- package/src/shell-client.js +240 -0
- package/src/shell-html.d.ts +91 -0
- package/src/shell-html.js +289 -0
- package/src/shell.css +558 -0
- package/src/strings.d.ts +18 -0
- package/src/strings.js +514 -0
- package/src/time.d.ts +18 -0
- package/src/time.js +254 -0
- package/src/tokens.css +158 -0
- package/src/update-pill.css +32 -0
- package/src/update-pill.d.ts +17 -0
- package/src/update-pill.js +95 -0
- package/src/version.d.ts +7 -0
- package/src/version.js +31 -0
- package/src/whatsnew.css +235 -0
- package/src/whatsnew.d.ts +36 -0
- package/src/whatsnew.js +411 -0
package/src/kit.d.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import type { ChangelogEntry, WhatsNewOptions } from "./whatsnew.js";
|
|
2
|
+
import type { ReportAdapter, ReportOptions } from "./report.js";
|
|
3
|
+
import type {
|
|
4
|
+
RequestState,
|
|
5
|
+
BusyOptions,
|
|
6
|
+
Progress,
|
|
7
|
+
configureBusy,
|
|
8
|
+
} from "./busy.js";
|
|
9
|
+
import type { HydrateOptions, Shell } from "./shell-client.js";
|
|
10
|
+
import type {
|
|
11
|
+
ShellModel,
|
|
12
|
+
shellHtml,
|
|
13
|
+
topBarHtml,
|
|
14
|
+
railHtml,
|
|
15
|
+
navItemHtml,
|
|
16
|
+
versionChipHtml,
|
|
17
|
+
reportButtonHtml,
|
|
18
|
+
reportPrefillOf,
|
|
19
|
+
icon,
|
|
20
|
+
esc,
|
|
21
|
+
} from "./shell-html.js";
|
|
22
|
+
import type { syncPinToggles } from "./shell-client.js";
|
|
23
|
+
import type { UpdatePill } from "./update-pill.js";
|
|
24
|
+
|
|
25
|
+
/** Every call to the product goes through it; the kit names no URL. */
|
|
26
|
+
export interface Adapter extends ReportAdapter {
|
|
27
|
+
changelog?: () => Promise<ChangelogEntry[]>;
|
|
28
|
+
applyUpdate?: () => Promise<unknown>;
|
|
29
|
+
request?: (id: string) => Promise<RequestState | null>;
|
|
30
|
+
}
|
|
31
|
+
export interface KitConfig {
|
|
32
|
+
lang?: string | (() => string);
|
|
33
|
+
product?: { name: string };
|
|
34
|
+
adapter?: Adapter;
|
|
35
|
+
reportOff?: boolean;
|
|
36
|
+
whatsNew?: Omit<WhatsNewOptions, "lang" | "changelog">;
|
|
37
|
+
report?: Omit<ReportOptions, "lang" | "adapter">;
|
|
38
|
+
}
|
|
39
|
+
export interface UpdateSetOptions {
|
|
40
|
+
version?: string;
|
|
41
|
+
message?: string;
|
|
42
|
+
reloadLabel?: string;
|
|
43
|
+
dismissLabel?: string;
|
|
44
|
+
cancelLabel?: string;
|
|
45
|
+
region?: string;
|
|
46
|
+
askFirst?: () => { message: string } | null;
|
|
47
|
+
onReload?: () => void;
|
|
48
|
+
onDismiss?: () => void;
|
|
49
|
+
}
|
|
50
|
+
export interface KitHydrateOptions extends HydrateOptions {
|
|
51
|
+
/** false: the version chip does not open What's new. */
|
|
52
|
+
versionChip?: boolean;
|
|
53
|
+
/** false: no [data-den-report] clicks or ⌘⇧F. */
|
|
54
|
+
report?: boolean;
|
|
55
|
+
}
|
|
56
|
+
export declare function configure(o?: KitConfig): void;
|
|
57
|
+
export declare function t(
|
|
58
|
+
key: string,
|
|
59
|
+
vars?: Record<string, string | number>,
|
|
60
|
+
): string;
|
|
61
|
+
export declare function strings(
|
|
62
|
+
lang: string,
|
|
63
|
+
table: Record<string, string>,
|
|
64
|
+
): void;
|
|
65
|
+
export declare const version: {
|
|
66
|
+
label(
|
|
67
|
+
running: string | null | undefined,
|
|
68
|
+
changelog: { version: string }[],
|
|
69
|
+
): string | null;
|
|
70
|
+
set(label: string): void;
|
|
71
|
+
unread(on: boolean): void;
|
|
72
|
+
};
|
|
73
|
+
export declare const whatsNew: {
|
|
74
|
+
open(): void;
|
|
75
|
+
close(): void;
|
|
76
|
+
load(): Promise<ChangelogEntry[]>;
|
|
77
|
+
setEntries(list: ChangelogEntry[]): void;
|
|
78
|
+
};
|
|
79
|
+
export declare const update: {
|
|
80
|
+
set(o?: UpdateSetOptions): UpdatePill;
|
|
81
|
+
hide(): void;
|
|
82
|
+
};
|
|
83
|
+
export declare const report: {
|
|
84
|
+
open(opts?: {
|
|
85
|
+
prefill?: { kind?: string; title?: string; description?: string };
|
|
86
|
+
/** Focus goes back here on close (default: what opened it). */
|
|
87
|
+
opener?: Element | null;
|
|
88
|
+
draft?: string;
|
|
89
|
+
}): Promise<unknown>;
|
|
90
|
+
button(o?: {
|
|
91
|
+
inFoot?: boolean;
|
|
92
|
+
inTop?: boolean;
|
|
93
|
+
label?: string;
|
|
94
|
+
prefill?: { kind?: string; title?: string; description?: string };
|
|
95
|
+
}): string;
|
|
96
|
+
};
|
|
97
|
+
export declare const busy: {
|
|
98
|
+
run<T = unknown>(
|
|
99
|
+
action: string,
|
|
100
|
+
run: (
|
|
101
|
+
headers: Record<string, string>,
|
|
102
|
+
progress: Progress,
|
|
103
|
+
) => Promise<T> | T,
|
|
104
|
+
opts?: BusyOptions<T>,
|
|
105
|
+
): Promise<T | null>;
|
|
106
|
+
configure: typeof configureBusy;
|
|
107
|
+
};
|
|
108
|
+
export declare function hydrate(
|
|
109
|
+
root: ParentNode,
|
|
110
|
+
opts?: KitHydrateOptions,
|
|
111
|
+
): Shell;
|
|
112
|
+
export declare function mount(
|
|
113
|
+
root: Element,
|
|
114
|
+
model?: ShellModel & { hydrate?: KitHydrateOptions },
|
|
115
|
+
): Shell;
|
|
116
|
+
|
|
117
|
+
export interface DenUIKit {
|
|
118
|
+
configure: typeof configure;
|
|
119
|
+
hydrate: typeof hydrate;
|
|
120
|
+
mount: typeof mount;
|
|
121
|
+
shellHtml: typeof shellHtml;
|
|
122
|
+
topBarHtml: typeof topBarHtml;
|
|
123
|
+
railHtml: typeof railHtml;
|
|
124
|
+
navItemHtml: typeof navItemHtml;
|
|
125
|
+
versionChipHtml: typeof versionChipHtml;
|
|
126
|
+
reportButtonHtml: typeof reportButtonHtml;
|
|
127
|
+
reportPrefillOf: typeof reportPrefillOf;
|
|
128
|
+
syncPinToggles: typeof syncPinToggles;
|
|
129
|
+
icon: typeof icon;
|
|
130
|
+
esc: typeof esc;
|
|
131
|
+
t: typeof t;
|
|
132
|
+
strings: typeof strings;
|
|
133
|
+
version: typeof version;
|
|
134
|
+
whatsNew: typeof whatsNew;
|
|
135
|
+
update: typeof update;
|
|
136
|
+
report: typeof report;
|
|
137
|
+
busy: typeof busy;
|
|
138
|
+
}
|
|
139
|
+
declare const DenUI: DenUIKit;
|
|
140
|
+
export default DenUI;
|
|
141
|
+
|
|
142
|
+
declare global {
|
|
143
|
+
interface Window {
|
|
144
|
+
DenUI?: DenUIKit;
|
|
145
|
+
}
|
|
146
|
+
}
|
package/src/kit.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* den-ui in the page: one entry for the kit's components, also set as
|
|
3
|
+
* window.DenUI. Every call to the product's server goes through the
|
|
4
|
+
* adapter the product gives; the kit names no URL of its own.
|
|
5
|
+
*
|
|
6
|
+
* import DenUI from "/kit/kit.js"; // or window.DenUI after it loads
|
|
7
|
+
* DenUI.configure({
|
|
8
|
+
* lang: "en", // or () => lang
|
|
9
|
+
* product: { name: "Acme" }, // {product} in the kit's words
|
|
10
|
+
* adapter: {
|
|
11
|
+
* changelog: () => Promise<entries>, What's new
|
|
12
|
+
* applyUpdate: () => Promise<unknown>, the update pill's Reload
|
|
13
|
+
* fileReport: (payload) => Promise<{ number, url } | { error }>,
|
|
14
|
+
* request: (id) => Promise<{ state, result?, error? }>, Check again
|
|
15
|
+
* ...the Report form's fuller calls (report.js)
|
|
16
|
+
* },
|
|
17
|
+
* report: { busy, mine, page, screenshot, library }, Report's options
|
|
18
|
+
* reportOff: false, // hides every Report button
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* DenUI.hydrate(root, opts) binds the shell shell-html.js drew (on the
|
|
22
|
+
* server, or by mount); see shell-client.js.
|
|
23
|
+
* The version chip opens What's new; any
|
|
24
|
+
* [data-den-report] and ⌘⇧F open Report.
|
|
25
|
+
* DenUI.mount(root, model) draws shellHtml(model) into root, then
|
|
26
|
+
* hydrates it (a page with no server render)
|
|
27
|
+
* DenUI.shellHtml(model) the markup (shell-html.js)
|
|
28
|
+
* DenUI.version.set(label), .unread(on), .label(running, changelog)
|
|
29
|
+
* DenUI.whatsNew.open(), .close(), .load(), .setEntries(list)
|
|
30
|
+
* DenUI.update.set({ version, message, reloadLabel, dismissLabel,
|
|
31
|
+
* cancelLabel, askFirst?, onDismiss? }), .hide()
|
|
32
|
+
* DenUI.report.open({ prefill, opener }), .button({ inFoot })
|
|
33
|
+
* focus goes back to opener (else the
|
|
34
|
+
* control that opened it) on close
|
|
35
|
+
* DenUI.busy.run(action, run, opts) the working pop-up (busy.js)
|
|
36
|
+
* DenUI.strings(lang, table) the product's own words, or its versions
|
|
37
|
+
* of the kit's; DenUI.t(key, vars)
|
|
38
|
+
*/
|
|
39
|
+
import { addStrings, kitT, setProduct } from "./strings.js";
|
|
40
|
+
import {
|
|
41
|
+
esc,
|
|
42
|
+
icon,
|
|
43
|
+
navItemHtml,
|
|
44
|
+
railHtml,
|
|
45
|
+
reportButtonHtml,
|
|
46
|
+
reportPrefillOf,
|
|
47
|
+
shellHtml,
|
|
48
|
+
topBarHtml,
|
|
49
|
+
versionChipHtml,
|
|
50
|
+
} from "./shell-html.js";
|
|
51
|
+
import { hydrateShell, syncPinToggles } from "./shell-client.js";
|
|
52
|
+
import { setUnread, setVersion, versionLabel } from "./version.js";
|
|
53
|
+
import {
|
|
54
|
+
closeWhatsNew,
|
|
55
|
+
configureWhatsNew,
|
|
56
|
+
loadWhatsNew,
|
|
57
|
+
openWhatsNew,
|
|
58
|
+
setEntries,
|
|
59
|
+
} from "./whatsnew.js";
|
|
60
|
+
import { configureReport, openReport } from "./report.js";
|
|
61
|
+
import { showUpdate } from "./update-pill.js";
|
|
62
|
+
import { busyAction, configureBusy } from "./busy.js";
|
|
63
|
+
|
|
64
|
+
const state = {
|
|
65
|
+
lang: "en",
|
|
66
|
+
adapter: {},
|
|
67
|
+
reportOff: false,
|
|
68
|
+
pill: null,
|
|
69
|
+
};
|
|
70
|
+
const lang = () =>
|
|
71
|
+
typeof state.lang === "function" ? state.lang() : state.lang;
|
|
72
|
+
|
|
73
|
+
export function configure(o = {}) {
|
|
74
|
+
if (o.lang !== undefined) state.lang = o.lang;
|
|
75
|
+
if (o.adapter) state.adapter = o.adapter;
|
|
76
|
+
if (o.reportOff !== undefined) state.reportOff = !!o.reportOff;
|
|
77
|
+
if (o.product) setProduct(o.product.name);
|
|
78
|
+
const ad = state.adapter;
|
|
79
|
+
configureWhatsNew({
|
|
80
|
+
lang,
|
|
81
|
+
changelog: ad.changelog || null,
|
|
82
|
+
...(o.whatsNew || {}),
|
|
83
|
+
});
|
|
84
|
+
const r = o.report || {};
|
|
85
|
+
configureReport({ lang, adapter: ad, ...r });
|
|
86
|
+
if (ad.request) configureBusy({ request: ad.request });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function t(key, vars) {
|
|
90
|
+
return kitT(lang(), key, vars);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function strings(l, table) {
|
|
94
|
+
addStrings(l, table);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export const version = {
|
|
98
|
+
label: versionLabel,
|
|
99
|
+
set: (label) => setVersion(label),
|
|
100
|
+
unread: (on) => setUnread(on),
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export const whatsNew = {
|
|
104
|
+
open: openWhatsNew,
|
|
105
|
+
close: closeWhatsNew,
|
|
106
|
+
load: loadWhatsNew,
|
|
107
|
+
setEntries,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export const update = {
|
|
111
|
+
/** Shows the pill for a new version (Reload: adapter.applyUpdate()). */
|
|
112
|
+
set(o = {}) {
|
|
113
|
+
update.hide();
|
|
114
|
+
state.pill = showUpdate({
|
|
115
|
+
message: o.message ?? kitT(lang(), "kit.update.msg"),
|
|
116
|
+
reloadLabel: o.reloadLabel ?? kitT(lang(), "kit.update.reload"),
|
|
117
|
+
dismissLabel: o.dismissLabel ?? kitT(lang(), "kit.update.notNow"),
|
|
118
|
+
cancelLabel: o.cancelLabel ?? kitT(lang(), "kit.update.cancel"),
|
|
119
|
+
region: o.region ?? kitT(lang(), "kit.update.region"),
|
|
120
|
+
askFirst: o.askFirst,
|
|
121
|
+
onReload:
|
|
122
|
+
o.onReload ||
|
|
123
|
+
(() =>
|
|
124
|
+
Promise.resolve(state.adapter.applyUpdate?.()).then(() =>
|
|
125
|
+
location.reload(),
|
|
126
|
+
)),
|
|
127
|
+
onDismiss: () => {
|
|
128
|
+
state.pill = null;
|
|
129
|
+
o.onDismiss?.();
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
return state.pill;
|
|
133
|
+
},
|
|
134
|
+
hide() {
|
|
135
|
+
state.pill?.hide();
|
|
136
|
+
state.pill = null;
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export const report = {
|
|
141
|
+
open: (opts = {}) =>
|
|
142
|
+
state.reportOff ? Promise.resolve(null) : openReport(opts),
|
|
143
|
+
button: (o = {}) =>
|
|
144
|
+
state.reportOff ? "" : reportButtonHtml({ ...o, lang: lang() }),
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
export const busy = {
|
|
148
|
+
run: (action, run, opts) => busyAction(action, run, opts),
|
|
149
|
+
configure: configureBusy,
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/** Binds the drawn shell, the chip and Report (see the top). */
|
|
153
|
+
export function hydrate(root, opts = {}) {
|
|
154
|
+
const shell = hydrateShell(root, { lang, ...opts });
|
|
155
|
+
const chip = root.querySelector("#version-pill");
|
|
156
|
+
if (chip && opts.versionChip !== false)
|
|
157
|
+
chip.addEventListener("click", () => whatsNew.open());
|
|
158
|
+
if (opts.report !== false) {
|
|
159
|
+
document.addEventListener("click", (e) => {
|
|
160
|
+
const b =
|
|
161
|
+
e.target && e.target.closest
|
|
162
|
+
? e.target.closest("[data-den-report]")
|
|
163
|
+
: null;
|
|
164
|
+
if (!b) return;
|
|
165
|
+
e.preventDefault();
|
|
166
|
+
const prefill = reportPrefillOf(b);
|
|
167
|
+
void report.open(prefill ? { prefill, opener: b } : { opener: b });
|
|
168
|
+
});
|
|
169
|
+
// ⌘⇧F (Ctrl+Shift+F) opens Report from anywhere, over a dialog too.
|
|
170
|
+
document.addEventListener(
|
|
171
|
+
"keydown",
|
|
172
|
+
(e) => {
|
|
173
|
+
if (
|
|
174
|
+
(e.metaKey || e.ctrlKey) &&
|
|
175
|
+
e.shiftKey &&
|
|
176
|
+
!e.altKey &&
|
|
177
|
+
(e.key === "F" || e.key === "f" || e.code === "KeyF") &&
|
|
178
|
+
!state.reportOff
|
|
179
|
+
) {
|
|
180
|
+
e.preventDefault();
|
|
181
|
+
void report.open();
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
true,
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
return shell;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function mount(root, model = {}) {
|
|
191
|
+
root.innerHTML = shellHtml({
|
|
192
|
+
lang: lang(),
|
|
193
|
+
report: !state.reportOff,
|
|
194
|
+
...model,
|
|
195
|
+
});
|
|
196
|
+
return hydrate(root, model.hydrate || {});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const DenUI = {
|
|
200
|
+
configure,
|
|
201
|
+
hydrate,
|
|
202
|
+
mount,
|
|
203
|
+
shellHtml,
|
|
204
|
+
topBarHtml,
|
|
205
|
+
railHtml,
|
|
206
|
+
navItemHtml,
|
|
207
|
+
versionChipHtml,
|
|
208
|
+
reportButtonHtml,
|
|
209
|
+
reportPrefillOf,
|
|
210
|
+
syncPinToggles,
|
|
211
|
+
icon,
|
|
212
|
+
esc,
|
|
213
|
+
t,
|
|
214
|
+
strings,
|
|
215
|
+
version,
|
|
216
|
+
whatsNew,
|
|
217
|
+
update,
|
|
218
|
+
report,
|
|
219
|
+
busy,
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
if (typeof window !== "undefined") window.DenUI = DenUI;
|
|
223
|
+
|
|
224
|
+
export default DenUI;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** A mask: a rectangle in fractions of the image (0..1). */
|
|
2
|
+
export interface Mask {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
w: number;
|
|
6
|
+
h: number;
|
|
7
|
+
}
|
|
8
|
+
export declare function configureShots(o?: { library?: string }): void;
|
|
9
|
+
/** The page as the person sees it, or null (never throws). */
|
|
10
|
+
export declare function capture(o?: {
|
|
11
|
+
seconds?: number;
|
|
12
|
+
skip?: (node: Node) => boolean;
|
|
13
|
+
}): Promise<HTMLCanvasElement | null>;
|
|
14
|
+
export declare function masked(
|
|
15
|
+
canvas: HTMLCanvasElement,
|
|
16
|
+
masks: Mask[],
|
|
17
|
+
): HTMLCanvasElement;
|
|
18
|
+
export declare function rectOf(
|
|
19
|
+
a: { x: number; y: number },
|
|
20
|
+
b: { x: number; y: number },
|
|
21
|
+
): Mask;
|
|
22
|
+
export declare function rectOfPercent(
|
|
23
|
+
x: number,
|
|
24
|
+
y: number,
|
|
25
|
+
w: number,
|
|
26
|
+
h: number,
|
|
27
|
+
): Mask;
|
|
28
|
+
export declare function toBlob(canvas: HTMLCanvasElement): Promise<Blob | null>;
|
|
29
|
+
export declare function saveAndCopy(
|
|
30
|
+
canvas: HTMLCanvasElement,
|
|
31
|
+
name?: string,
|
|
32
|
+
): Promise<{ saved: boolean; copied: boolean }>;
|
|
33
|
+
export declare function canvasOf(
|
|
34
|
+
src: string | Blob,
|
|
35
|
+
): Promise<HTMLCanvasElement | null>;
|
|
36
|
+
export declare function keepShot(
|
|
37
|
+
id: string,
|
|
38
|
+
canvas: HTMLCanvasElement | null,
|
|
39
|
+
masks: Mask[],
|
|
40
|
+
noShot: boolean,
|
|
41
|
+
): Promise<boolean>;
|
|
42
|
+
export declare function loadShot(id: string): Promise<{
|
|
43
|
+
canvas: HTMLCanvasElement | null;
|
|
44
|
+
masks: Mask[];
|
|
45
|
+
noShot: boolean;
|
|
46
|
+
} | null>;
|
|
47
|
+
export declare function dropShot(id: string): Promise<void>;
|
|
@@ -0,0 +1,255 @@
|
|
|
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
|
+
* The page as shown now, as a canvas the size of the window, or null.
|
|
57
|
+
* `skip(el)`: elements to leave out (the form).
|
|
58
|
+
*/
|
|
59
|
+
export async function capture({ seconds = 15, skip = () => false } = {}) {
|
|
60
|
+
try {
|
|
61
|
+
const h2i = await Promise.race([loadLib(), timeout(seconds * 1000)]);
|
|
62
|
+
const root = document.documentElement;
|
|
63
|
+
const canvas = await Promise.race([
|
|
64
|
+
h2i.toCanvas(root, {
|
|
65
|
+
width: window.innerWidth,
|
|
66
|
+
height: window.innerHeight,
|
|
67
|
+
pixelRatio: 1,
|
|
68
|
+
// The page's own fonts are already there: not fetched again.
|
|
69
|
+
skipFonts: true,
|
|
70
|
+
backgroundColor: getComputedStyle(document.body).backgroundColor,
|
|
71
|
+
filter: (n) => !(n.nodeType === 1 && skip(n)),
|
|
72
|
+
}),
|
|
73
|
+
timeout(seconds * 1000),
|
|
74
|
+
]);
|
|
75
|
+
return canvas && canvas.width > 0 && canvas.height > 0 ? canvas : null;
|
|
76
|
+
} catch {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** A copy of `canvas` with each mask (fractions of it) filled black. */
|
|
82
|
+
export function masked(canvas, masks) {
|
|
83
|
+
const out = document.createElement("canvas");
|
|
84
|
+
out.width = canvas.width;
|
|
85
|
+
out.height = canvas.height;
|
|
86
|
+
const g = out.getContext("2d");
|
|
87
|
+
g.drawImage(canvas, 0, 0);
|
|
88
|
+
// Black in every theme: a mask hides, it is not a theme colour.
|
|
89
|
+
g.fillStyle = "black";
|
|
90
|
+
for (const m of masks)
|
|
91
|
+
g.fillRect(
|
|
92
|
+
Math.floor(m.x * out.width),
|
|
93
|
+
Math.floor(m.y * out.height),
|
|
94
|
+
Math.ceil(m.w * out.width),
|
|
95
|
+
Math.ceil(m.h * out.height),
|
|
96
|
+
);
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** A mask from two points (fractions), the right way round, clamped. */
|
|
101
|
+
export function rectOf(a, b) {
|
|
102
|
+
const c = (v) => Math.min(1, Math.max(0, v));
|
|
103
|
+
const x = c(Math.min(a.x, b.x));
|
|
104
|
+
const y = c(Math.min(a.y, b.y));
|
|
105
|
+
return {
|
|
106
|
+
x,
|
|
107
|
+
y,
|
|
108
|
+
w: c(Math.max(a.x, b.x)) - x,
|
|
109
|
+
h: c(Math.max(a.y, b.y)) - y,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** A mask from the keyboard form: percentages, clamped to the image. */
|
|
114
|
+
export function rectOfPercent(x, y, w, h) {
|
|
115
|
+
const n = (v) => Math.min(100, Math.max(0, Number(v) || 0)) / 100;
|
|
116
|
+
const r = { x: n(x), y: n(y), w: n(w), h: n(h) };
|
|
117
|
+
r.w = Math.min(r.w, 1 - r.x);
|
|
118
|
+
r.h = Math.min(r.h, 1 - r.y);
|
|
119
|
+
return r;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function toBlob(canvas) {
|
|
123
|
+
return new Promise((ok) => canvas.toBlob(ok, "image/png"));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* After sending: the masked screenshot copied (where the browser allows
|
|
128
|
+
* it) and saved as a file. Which of the two worked.
|
|
129
|
+
*/
|
|
130
|
+
export async function saveAndCopy(canvas, name = "report-screenshot.png") {
|
|
131
|
+
const blob = await toBlob(canvas);
|
|
132
|
+
const out = { copied: false, saved: false };
|
|
133
|
+
if (!blob) return out;
|
|
134
|
+
try {
|
|
135
|
+
if (navigator.clipboard?.write && typeof ClipboardItem === "function") {
|
|
136
|
+
await navigator.clipboard.write([
|
|
137
|
+
new ClipboardItem({ "image/png": blob }),
|
|
138
|
+
]);
|
|
139
|
+
out.copied = true;
|
|
140
|
+
}
|
|
141
|
+
} catch {
|
|
142
|
+
/* not allowed here: the file is still saved */
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
const url = URL.createObjectURL(blob);
|
|
146
|
+
const a = document.createElement("a");
|
|
147
|
+
a.href = url;
|
|
148
|
+
a.download = name;
|
|
149
|
+
a.hidden = true;
|
|
150
|
+
document.body.append(a);
|
|
151
|
+
a.click();
|
|
152
|
+
a.remove();
|
|
153
|
+
setTimeout(() => URL.revokeObjectURL(url), 60_000);
|
|
154
|
+
out.saved = true;
|
|
155
|
+
} catch {
|
|
156
|
+
/* no download here */
|
|
157
|
+
}
|
|
158
|
+
return out;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** An image (a Blob or a data: URL) back as a canvas, or null. */
|
|
162
|
+
export async function canvasOf(src) {
|
|
163
|
+
let url = "";
|
|
164
|
+
try {
|
|
165
|
+
url = typeof src === "string" ? src : URL.createObjectURL(src);
|
|
166
|
+
const img = new Image();
|
|
167
|
+
await new Promise((ok, no) => {
|
|
168
|
+
img.onload = ok;
|
|
169
|
+
img.onerror = no;
|
|
170
|
+
img.src = url;
|
|
171
|
+
});
|
|
172
|
+
const c = document.createElement("canvas");
|
|
173
|
+
c.width = img.naturalWidth;
|
|
174
|
+
c.height = img.naturalHeight;
|
|
175
|
+
c.getContext("2d").drawImage(img, 0, 0);
|
|
176
|
+
return c.width && c.height ? c : null;
|
|
177
|
+
} catch {
|
|
178
|
+
return null;
|
|
179
|
+
} finally {
|
|
180
|
+
if (url && typeof src !== "string") URL.revokeObjectURL(url);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/*
|
|
185
|
+
* Finish later's screenshot stays in this browser (IndexedDB), keyed by
|
|
186
|
+
* the draft's id, with its masks: the draft's text is on the product's server, the
|
|
187
|
+
* picture never is. Storage off or full: the draft keeps its text only.
|
|
188
|
+
*/
|
|
189
|
+
const DB = "den-report";
|
|
190
|
+
const STORE = "shots";
|
|
191
|
+
|
|
192
|
+
function openDb() {
|
|
193
|
+
return new Promise((ok, no) => {
|
|
194
|
+
const r = indexedDB.open(DB, 1);
|
|
195
|
+
r.onupgradeneeded = () => r.result.createObjectStore(STORE);
|
|
196
|
+
r.onsuccess = () => ok(r.result);
|
|
197
|
+
r.onerror = () => no(r.error);
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async function inStore(mode, f) {
|
|
202
|
+
const db = await openDb();
|
|
203
|
+
try {
|
|
204
|
+
return await new Promise((ok, no) => {
|
|
205
|
+
const tx = db.transaction(STORE, mode);
|
|
206
|
+
const req = f(tx.objectStore(STORE));
|
|
207
|
+
tx.oncomplete = () => ok(req.result);
|
|
208
|
+
tx.onerror = () => no(tx.error);
|
|
209
|
+
tx.onabort = () => no(tx.error);
|
|
210
|
+
});
|
|
211
|
+
} finally {
|
|
212
|
+
db.close();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Keeps a draft's screenshot (the original) and its masks. */
|
|
217
|
+
export async function keepShot(id, canvas, masks, noShot) {
|
|
218
|
+
try {
|
|
219
|
+
const blob = await toBlob(canvas);
|
|
220
|
+
if (!blob) return false;
|
|
221
|
+
await inStore("readwrite", (s) =>
|
|
222
|
+
s.put({ blob, masks, noShot: !!noShot }, id),
|
|
223
|
+
);
|
|
224
|
+
return true;
|
|
225
|
+
} catch {
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** A kept screenshot: { canvas, masks, noShot }, or null. */
|
|
231
|
+
export async function loadShot(id) {
|
|
232
|
+
try {
|
|
233
|
+
const v = await inStore("readonly", (s) => s.get(id));
|
|
234
|
+
if (!v || !v.blob) return null;
|
|
235
|
+
const canvas = await canvasOf(v.blob);
|
|
236
|
+
return canvas
|
|
237
|
+
? {
|
|
238
|
+
canvas,
|
|
239
|
+
masks: Array.isArray(v.masks) ? v.masks : [],
|
|
240
|
+
noShot: !!v.noShot,
|
|
241
|
+
}
|
|
242
|
+
: null;
|
|
243
|
+
} catch {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Forgets a draft's screenshot (sent or discarded). */
|
|
249
|
+
export async function dropShot(id) {
|
|
250
|
+
try {
|
|
251
|
+
await inStore("readwrite", (s) => s.delete(id));
|
|
252
|
+
} catch {
|
|
253
|
+
/* nothing kept */
|
|
254
|
+
}
|
|
255
|
+
}
|