@hackbuild/ai-views 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +93 -2
- package/dist/chat.d.ts +91 -0
- package/dist/chat.js +237 -0
- package/dist/dom.d.ts +38 -0
- package/dist/dom.js +67 -0
- package/dist/elements.d.ts +42 -0
- package/dist/elements.js +378 -0
- package/dist/frame.d.ts +55 -0
- package/dist/frame.js +40 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +10 -0
- package/dist/jobs.d.ts +26 -0
- package/dist/jobs.js +49 -0
- package/dist/memory.d.ts +26 -0
- package/dist/memory.js +36 -0
- package/dist/mount-chat.d.ts +38 -0
- package/dist/mount-chat.js +177 -0
- package/dist/mount-frame.d.ts +45 -0
- package/dist/mount-frame.js +237 -0
- package/dist/mount-mask.d.ts +109 -0
- package/dist/mount-mask.js +446 -0
- package/dist/mount-model.d.ts +27 -0
- package/dist/mount-model.js +122 -0
- package/dist/mount-transcribe.d.ts +42 -0
- package/dist/mount-transcribe.js +203 -0
- package/dist/pick.d.ts +132 -0
- package/dist/pick.js +254 -0
- package/dist/rows.d.ts +36 -0
- package/dist/rows.js +147 -0
- package/dist/single/elements.iife.js +9568 -0
- package/dist/single/elements.js +9542 -0
- package/dist/single/index.iife.js +6814 -0
- package/dist/single/index.js +6778 -0
- package/dist/single/vue.iife.js +11334 -0
- package/dist/single/vue.js +11316 -0
- package/dist/transcribe.d.ts +80 -0
- package/dist/transcribe.js +175 -0
- package/dist/vue.d.ts +259 -0
- package/dist/vue.js +199 -0
- package/how.ts +26 -0
- package/package.json +61 -4
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MaskStudio, after the earlier mask studio: drop an image, press a thing
|
|
3
|
+
* to cut it out (a click segment model such as SlimSAM), fix the mask by
|
|
4
|
+
* hand with the wand, brush and lasso of hackbuild-ui maskEditor (x swaps
|
|
5
|
+
* add and erase, [ and ] size the brush, ctrl z undoes), zoom and fit, and
|
|
6
|
+
* export the mask, a matte or the cutout. With a segment everything model
|
|
7
|
+
* (FastSAM) one press finds every object; each lands in the found list and
|
|
8
|
+
* a press adds it to the mask. Models are functions the page passes, so
|
|
9
|
+
* stubs run the studio with no weights.
|
|
10
|
+
*/
|
|
11
|
+
import { INSTANCE_COLOURS, MASK_TOOLS, maskEditor, strokeIcon } from "@virgilvox/hackbuild-ui/vanilla";
|
|
12
|
+
import { ICON, download, esc, listeners, show } from "./dom.js";
|
|
13
|
+
const ZOOMS = [0.5, 0.75, 1, 1.5, 2, 3, 4];
|
|
14
|
+
/** Pixels of a drawable, for a model. */
|
|
15
|
+
function pixelsOf(src) {
|
|
16
|
+
const c = document.createElement("canvas");
|
|
17
|
+
c.width = src.width;
|
|
18
|
+
c.height = src.height;
|
|
19
|
+
const g = c.getContext("2d");
|
|
20
|
+
g.drawImage(src, 0, 0);
|
|
21
|
+
const d = g.getImageData(0, 0, c.width, c.height);
|
|
22
|
+
return { width: d.width, height: d.height, data: d.data };
|
|
23
|
+
}
|
|
24
|
+
/** A 48 px wide preview of a matte, as a data url. */
|
|
25
|
+
function thumbOf(img) {
|
|
26
|
+
const c = document.createElement("canvas");
|
|
27
|
+
c.width = img.width;
|
|
28
|
+
c.height = img.height;
|
|
29
|
+
c.getContext("2d")?.putImageData(img, 0, 0);
|
|
30
|
+
const t = document.createElement("canvas");
|
|
31
|
+
t.width = 48;
|
|
32
|
+
t.height = Math.max(1, Math.round((48 * img.height) / Math.max(1, img.width)));
|
|
33
|
+
t.getContext("2d")?.drawImage(c, 0, 0, t.width, t.height);
|
|
34
|
+
return t.toDataURL("image/png");
|
|
35
|
+
}
|
|
36
|
+
export function mountMaskStudio(host, o = {}) {
|
|
37
|
+
let models = o.models ?? {};
|
|
38
|
+
const l = listeners();
|
|
39
|
+
host.innerHTML =
|
|
40
|
+
`<div class="hb-lay-empty" data-empty><span class="hb-lay-empty-name">${esc(o.title ?? "drop an image")}</span><p class="hb-lay-empty-note">${esc(o.note ?? "press a thing to cut it out. the picture stays on this machine.")}</p><div class="hb-lay-empty-acts"><button type="button" class="hb-btn hb-btn-go" data-pick>open an image</button>${o.sampleLabel ? `<button type="button" class="hb-btn" data-sample>${esc(o.sampleLabel)}</button>` : ""}</div></div>` +
|
|
41
|
+
`<div class="hb-mask is-fill" data-studio hidden><div class="hb-mask-well"><div class="hb-mask-frame"><canvas class="hb-mask-canvas" tabindex="0" role="img" aria-label="image and mask"></canvas><p class="hb-mask-hint" aria-live="polite"></p></div></div></div>` +
|
|
42
|
+
`<input type="file" accept="image/*" hidden>`;
|
|
43
|
+
const empty = host.querySelector("[data-empty]");
|
|
44
|
+
const studio = host.querySelector("[data-studio]");
|
|
45
|
+
const well = host.querySelector(".hb-mask-well");
|
|
46
|
+
const frame = host.querySelector(".hb-mask-frame");
|
|
47
|
+
const canvas = host.querySelector("canvas");
|
|
48
|
+
const hint = host.querySelector(".hb-mask-hint");
|
|
49
|
+
const input = host.querySelector("input[type=file]");
|
|
50
|
+
const subs = new Set();
|
|
51
|
+
let tool = models.segment ? "point" : "wand";
|
|
52
|
+
let zoom = 1;
|
|
53
|
+
let busy = "";
|
|
54
|
+
let error = "";
|
|
55
|
+
let pixels = null;
|
|
56
|
+
let found = [];
|
|
57
|
+
let saved = [];
|
|
58
|
+
let savedSeq = 0;
|
|
59
|
+
let ed = null;
|
|
60
|
+
const state = () => ({
|
|
61
|
+
ready: !!ed?.ready,
|
|
62
|
+
tool,
|
|
63
|
+
mode: ed?.mode ?? "add",
|
|
64
|
+
size: ed?.size ?? 24,
|
|
65
|
+
tolerance: ed?.tolerance ?? 6,
|
|
66
|
+
canUndo: !!ed?.canUndo,
|
|
67
|
+
canRedo: !!ed?.canRedo,
|
|
68
|
+
coverage: ed?.coverage ?? 0,
|
|
69
|
+
zoom,
|
|
70
|
+
busy,
|
|
71
|
+
found,
|
|
72
|
+
saved: saved.map(({ bytes: _b, ...rest }) => rest),
|
|
73
|
+
width: ed?.width ?? 0,
|
|
74
|
+
height: ed?.height ?? 0,
|
|
75
|
+
error,
|
|
76
|
+
});
|
|
77
|
+
const emit = () => {
|
|
78
|
+
const s = state();
|
|
79
|
+
hint.textContent = s.busy || s.error || (tool === "point" ? "press a thing to cut it out. x takes it away instead." : (MASK_TOOLS.find((t) => t.id === tool)?.hint ?? ""));
|
|
80
|
+
for (const fn of subs)
|
|
81
|
+
fn(s);
|
|
82
|
+
};
|
|
83
|
+
const editor = maskEditor(canvas, { tool: tool === "point" ? "wand" : tool, onError: (e) => ((error = `the image did not load: ${e.message}`), emit()) });
|
|
84
|
+
l.add(editor.subscribe((s) => ((ed = s), emit())));
|
|
85
|
+
// the well fills the subject (.hb-mask.is-fill); at fit the frame is as large as the well holds at the image's ratio
|
|
86
|
+
const applyZoom = () => {
|
|
87
|
+
const ratio = ed && ed.height ? ed.width / ed.height : 4 / 3;
|
|
88
|
+
const fitW = Math.max(1, Math.min(well.clientWidth, well.clientHeight * ratio));
|
|
89
|
+
studio.style.setProperty("--hb-mask-ratio", String(ratio));
|
|
90
|
+
studio.style.setProperty("--hb-mask-w", `${Math.round(fitW * zoom)}px`);
|
|
91
|
+
studio.classList.toggle("is-zoomed", zoom > 1);
|
|
92
|
+
};
|
|
93
|
+
const ro = typeof ResizeObserver === "function" ? new ResizeObserver(() => applyZoom()) : null;
|
|
94
|
+
ro?.observe(well);
|
|
95
|
+
l.add(() => ro?.disconnect());
|
|
96
|
+
// a press with the point tool asks the click segment model, before the editor sees it
|
|
97
|
+
const cut = async (x, y, erase) => {
|
|
98
|
+
if (!ed?.ready || !pixels || !models.segment)
|
|
99
|
+
return;
|
|
100
|
+
busy = "cutting it out";
|
|
101
|
+
error = "";
|
|
102
|
+
emit();
|
|
103
|
+
try {
|
|
104
|
+
const m = await models.segment(pixels, { x, y });
|
|
105
|
+
if (!m)
|
|
106
|
+
return;
|
|
107
|
+
const cur = editor.mask();
|
|
108
|
+
for (let i = 0; i < cur.length; i++)
|
|
109
|
+
cur[i] = erase ? (m[i] ? 0 : cur[i]) : m[i] || cur[i] ? 255 : 0;
|
|
110
|
+
editor.setMask(cur);
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
error = `the cut failed: ${err instanceof Error ? err.message : String(err)}`;
|
|
114
|
+
}
|
|
115
|
+
finally {
|
|
116
|
+
busy = "";
|
|
117
|
+
emit();
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
const onPoint = (e) => {
|
|
121
|
+
if (tool !== "point")
|
|
122
|
+
return;
|
|
123
|
+
e.stopImmediatePropagation();
|
|
124
|
+
e.preventDefault();
|
|
125
|
+
const pe = e;
|
|
126
|
+
const r = canvas.getBoundingClientRect();
|
|
127
|
+
if (!ed?.ready)
|
|
128
|
+
return;
|
|
129
|
+
const x = Math.round(((pe.clientX - r.left) / r.width) * ed.width);
|
|
130
|
+
const y = Math.round(((pe.clientY - r.top) / r.height) * ed.height);
|
|
131
|
+
void cut(x, y, (ed.mode === "erase") !== pe.shiftKey);
|
|
132
|
+
};
|
|
133
|
+
// capture, so it runs before the editor's own handler on the canvas
|
|
134
|
+
canvas.addEventListener("pointerdown", onPoint, true);
|
|
135
|
+
l.add(() => canvas.removeEventListener("pointerdown", onPoint, true));
|
|
136
|
+
const take = (f) => {
|
|
137
|
+
if (!f)
|
|
138
|
+
return;
|
|
139
|
+
const url = URL.createObjectURL(f);
|
|
140
|
+
void studioApi.setImage(url).finally(() => URL.revokeObjectURL(url));
|
|
141
|
+
};
|
|
142
|
+
l.on(input, "change", () => {
|
|
143
|
+
take(input.files?.[0]);
|
|
144
|
+
input.value = "";
|
|
145
|
+
});
|
|
146
|
+
l.on(host, "dragover", (e) => e.preventDefault());
|
|
147
|
+
l.on(host, "drop", (e) => {
|
|
148
|
+
e.preventDefault();
|
|
149
|
+
take(e.dataTransfer?.files?.[0]);
|
|
150
|
+
});
|
|
151
|
+
l.on(empty, "click", (e) => {
|
|
152
|
+
const t = e.target;
|
|
153
|
+
if (t.closest("[data-pick]"))
|
|
154
|
+
input.click();
|
|
155
|
+
else if (t.closest("[data-sample]"))
|
|
156
|
+
o.onSample?.();
|
|
157
|
+
});
|
|
158
|
+
const studioApi = {
|
|
159
|
+
async setImage(src) {
|
|
160
|
+
error = "";
|
|
161
|
+
found = [];
|
|
162
|
+
saved = [];
|
|
163
|
+
let drawable = null;
|
|
164
|
+
if (typeof src === "string") {
|
|
165
|
+
const img = new Image();
|
|
166
|
+
img.crossOrigin = "anonymous";
|
|
167
|
+
img.src = src;
|
|
168
|
+
await img.decode();
|
|
169
|
+
drawable = img;
|
|
170
|
+
}
|
|
171
|
+
else if ("data" in src && !(src instanceof HTMLCanvasElement)) {
|
|
172
|
+
pixels = src;
|
|
173
|
+
}
|
|
174
|
+
else
|
|
175
|
+
drawable = src;
|
|
176
|
+
if (drawable)
|
|
177
|
+
pixels = pixelsOf(drawable);
|
|
178
|
+
show(empty, false);
|
|
179
|
+
// .hb-mask sets display, so the hidden attribute would not hide it
|
|
180
|
+
show(studio, true);
|
|
181
|
+
await editor.setImage(pixels);
|
|
182
|
+
zoom = 1;
|
|
183
|
+
applyZoom();
|
|
184
|
+
emit();
|
|
185
|
+
canvas.focus({ preventScroll: true });
|
|
186
|
+
},
|
|
187
|
+
setTool(t) {
|
|
188
|
+
if (t === "point" && !models.segment)
|
|
189
|
+
return;
|
|
190
|
+
tool = t;
|
|
191
|
+
if (t !== "point")
|
|
192
|
+
editor.setTool(t);
|
|
193
|
+
canvas.dataset.tool = t;
|
|
194
|
+
emit();
|
|
195
|
+
},
|
|
196
|
+
setMode: (m) => editor.setMode(m),
|
|
197
|
+
setSize: (px) => editor.setSize(px),
|
|
198
|
+
setTolerance: (p) => editor.setTolerance(p),
|
|
199
|
+
undo: () => void editor.undo(),
|
|
200
|
+
redo: () => void editor.redo(),
|
|
201
|
+
invert: () => editor.invert(),
|
|
202
|
+
clear: () => editor.clear(),
|
|
203
|
+
expand: (px) => editor.expand(px),
|
|
204
|
+
shrink: (px) => editor.shrink(px),
|
|
205
|
+
smooth: (r) => editor.smooth(r),
|
|
206
|
+
save(name) {
|
|
207
|
+
if (!ed?.ready || !ed.coverage)
|
|
208
|
+
return;
|
|
209
|
+
const bytes = editor.mask();
|
|
210
|
+
const id = ++savedSeq;
|
|
211
|
+
saved = [{ id, name: name ?? `mask ${id}`, thumb: thumbOf(editor.toImageData("matte")), coverage: ed.coverage, bytes }, ...saved];
|
|
212
|
+
emit();
|
|
213
|
+
},
|
|
214
|
+
restore(id) {
|
|
215
|
+
const m = saved.find((x) => x.id === id);
|
|
216
|
+
if (m && ed?.ready && m.bytes.length === ed.width * ed.height)
|
|
217
|
+
editor.setMask(m.bytes);
|
|
218
|
+
},
|
|
219
|
+
forget(id) {
|
|
220
|
+
saved = saved.filter((x) => x.id !== id);
|
|
221
|
+
emit();
|
|
222
|
+
},
|
|
223
|
+
mask: (kind = "alpha") => (ed?.ready ? editor.toImageData(kind) : null),
|
|
224
|
+
zoom(dir) {
|
|
225
|
+
if (dir === 0)
|
|
226
|
+
zoom = 1;
|
|
227
|
+
else {
|
|
228
|
+
const i = ZOOMS.indexOf(zoom);
|
|
229
|
+
zoom = ZOOMS[Math.max(0, Math.min(ZOOMS.length - 1, i + dir))];
|
|
230
|
+
}
|
|
231
|
+
applyZoom();
|
|
232
|
+
emit();
|
|
233
|
+
},
|
|
234
|
+
async everything() {
|
|
235
|
+
if (!models.everything || !pixels)
|
|
236
|
+
return;
|
|
237
|
+
busy = "finding every object";
|
|
238
|
+
error = "";
|
|
239
|
+
emit();
|
|
240
|
+
try {
|
|
241
|
+
const list = await models.everything(pixels);
|
|
242
|
+
found = list.map((f, i) => ({ ...f, on: false, colour: INSTANCE_COLOURS[i % INSTANCE_COLOURS.length] }));
|
|
243
|
+
}
|
|
244
|
+
catch (e) {
|
|
245
|
+
error = `segment everything failed: ${e instanceof Error ? e.message : String(e)}`;
|
|
246
|
+
}
|
|
247
|
+
finally {
|
|
248
|
+
busy = "";
|
|
249
|
+
emit();
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
cutAt: (x, y, mode) => cut(x, y, (mode ?? ed?.mode) === "erase"),
|
|
253
|
+
toggleFound(i) {
|
|
254
|
+
const f = found[i];
|
|
255
|
+
if (!f)
|
|
256
|
+
return;
|
|
257
|
+
found = found.map((x, k) => (k === i ? { ...x, on: !x.on } : x));
|
|
258
|
+
const cur = editor.mask();
|
|
259
|
+
for (let p = 0; p < cur.length; p++)
|
|
260
|
+
if (f.mask[p])
|
|
261
|
+
cur[p] = f.on ? 0 : 255;
|
|
262
|
+
editor.setMask(cur);
|
|
263
|
+
},
|
|
264
|
+
async exportAs(kind) {
|
|
265
|
+
if (!ed?.ready)
|
|
266
|
+
return null;
|
|
267
|
+
return editor.toBlob(kind);
|
|
268
|
+
},
|
|
269
|
+
get hasEverything() {
|
|
270
|
+
return !!models.everything;
|
|
271
|
+
},
|
|
272
|
+
get hasSegment() {
|
|
273
|
+
return !!models.segment;
|
|
274
|
+
},
|
|
275
|
+
state,
|
|
276
|
+
subscribe(fn) {
|
|
277
|
+
subs.add(fn);
|
|
278
|
+
fn(state());
|
|
279
|
+
return () => subs.delete(fn);
|
|
280
|
+
},
|
|
281
|
+
setModels(m) {
|
|
282
|
+
const hadSegment = !!models.segment;
|
|
283
|
+
models = m;
|
|
284
|
+
if (m.segment && !hadSegment) {
|
|
285
|
+
tool = "point";
|
|
286
|
+
canvas.dataset.tool = "point";
|
|
287
|
+
}
|
|
288
|
+
if (tool === "point" && !m.segment)
|
|
289
|
+
studioApi.setTool("wand");
|
|
290
|
+
emit();
|
|
291
|
+
},
|
|
292
|
+
pick: () => input.click(),
|
|
293
|
+
dispose() {
|
|
294
|
+
l.off();
|
|
295
|
+
editor.dispose();
|
|
296
|
+
subs.clear();
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
canvas.dataset.tool = tool;
|
|
300
|
+
emit();
|
|
301
|
+
return studioApi;
|
|
302
|
+
}
|
|
303
|
+
/** The tool rail of a studio: point (when a click model is set), wand, brush, lasso, and segment everything (when that model is set). */
|
|
304
|
+
export function mountMaskTools(host, studio) {
|
|
305
|
+
const l = listeners();
|
|
306
|
+
const draw = (s) => {
|
|
307
|
+
const tools = [
|
|
308
|
+
...(studio.hasSegment ? [{ id: "point", label: "cut out a thing", key: "a", icon: "crosshairs" }] : []),
|
|
309
|
+
...MASK_TOOLS.map((t) => ({ id: t.id, label: t.label, key: t.key, icon: t.icon })),
|
|
310
|
+
];
|
|
311
|
+
host.innerHTML = tools.map((t) => `<button type="button" class="hb-iconbtn" data-tool="${t.id}" aria-label="${esc(t.label)} (${t.key})" title="${esc(t.label)} (${t.key})" aria-pressed="${s.tool === t.id}" aria-keyshortcuts="${t.key}">${strokeIcon(t.icon, { size: 18 })}</button>`).join("");
|
|
312
|
+
};
|
|
313
|
+
l.on(host, "click", (e) => {
|
|
314
|
+
const t = e.target.closest("[data-tool]");
|
|
315
|
+
if (t)
|
|
316
|
+
studio.setTool(t.dataset.tool);
|
|
317
|
+
});
|
|
318
|
+
l.on(document, "keydown", (e) => {
|
|
319
|
+
const ke = e;
|
|
320
|
+
const typing = !!ke.target.closest?.("input,textarea,[contenteditable]");
|
|
321
|
+
if (!typing && !ke.ctrlKey && !ke.metaKey && studio.state().ready) {
|
|
322
|
+
if (ke.key === "+" || ke.key === "=")
|
|
323
|
+
return studio.zoom(1);
|
|
324
|
+
if (ke.key === "-")
|
|
325
|
+
return studio.zoom(-1);
|
|
326
|
+
if (ke.key === "0")
|
|
327
|
+
return studio.zoom(0);
|
|
328
|
+
}
|
|
329
|
+
if (ke.key === "a" && !ke.ctrlKey && !ke.metaKey && studio.hasSegment && !ke.target.closest?.("input,textarea,[contenteditable]"))
|
|
330
|
+
studio.setTool("point");
|
|
331
|
+
});
|
|
332
|
+
l.add(studio.subscribe(draw));
|
|
333
|
+
return { dispose: () => l.off() };
|
|
334
|
+
}
|
|
335
|
+
/** The verb row on the canvas: undo and redo once there is a change, zoom once there is an image. */
|
|
336
|
+
export function mountMaskVerbs(host, studio) {
|
|
337
|
+
const l = listeners();
|
|
338
|
+
let last = "";
|
|
339
|
+
const draw = (s) => {
|
|
340
|
+
const html = s.ready
|
|
341
|
+
? `${s.canUndo ? `<button type="button" class="hb-iconbtn" data-v="undo" aria-label="undo" title="undo (ctrl z)">${ICON.undo}</button>` : ""}${s.canRedo ? `<button type="button" class="hb-iconbtn" data-v="redo" aria-label="redo" title="redo (ctrl shift z)">${ICON.redo}</button>` : ""}` +
|
|
342
|
+
// zoom out and fit show once zoomed in, so the row stays short
|
|
343
|
+
(s.zoom !== 1 ? `<button type="button" class="hb-iconbtn" data-v="out" aria-label="zoom out" title="zoom out (-)">${ICON.zoomOut}</button><button type="button" class="hb-iconbtn" data-v="fit" aria-label="fit, now ${Math.round(s.zoom * 100)} percent" title="fit (0)">${ICON.fit}</button>` : "") +
|
|
344
|
+
`<button type="button" class="hb-iconbtn" data-v="in" aria-label="zoom in" title="zoom in (+)">${ICON.zoomIn}</button>`
|
|
345
|
+
: "";
|
|
346
|
+
if (html !== last)
|
|
347
|
+
host.innerHTML = last = html;
|
|
348
|
+
host.hidden = !html;
|
|
349
|
+
};
|
|
350
|
+
l.on(host, "click", (e) => {
|
|
351
|
+
const v = e.target.closest("[data-v]")?.dataset.v;
|
|
352
|
+
if (v === "undo")
|
|
353
|
+
studio.undo();
|
|
354
|
+
else if (v === "redo")
|
|
355
|
+
studio.redo();
|
|
356
|
+
else if (v === "in")
|
|
357
|
+
studio.zoom(1);
|
|
358
|
+
else if (v === "out")
|
|
359
|
+
studio.zoom(-1);
|
|
360
|
+
else if (v === "fit")
|
|
361
|
+
studio.zoom(0);
|
|
362
|
+
});
|
|
363
|
+
l.add(studio.subscribe(draw));
|
|
364
|
+
return { dispose: () => l.off() };
|
|
365
|
+
}
|
|
366
|
+
/** The rack parts: what was found, the mask tools' settings, and the exports once there is a mask. */
|
|
367
|
+
export function mountMaskRack(host, studio, part, o = {}) {
|
|
368
|
+
const l = listeners();
|
|
369
|
+
let last = "";
|
|
370
|
+
const draw = (s) => {
|
|
371
|
+
let html = "";
|
|
372
|
+
if (part === "found") {
|
|
373
|
+
html = s.found.length
|
|
374
|
+
? `<div class="hb-inst"><ul class="hb-inst-list">${s.found.map((f, i) => `<li class="hb-inst-row${f.on ? " is-on" : ""}" style="--hb-inst-c:${f.colour}"><button type="button" class="hb-inst-pick" data-found="${i}" aria-pressed="${f.on}"><span class="hb-inst-sw"></span><span class="hb-inst-name">${esc(f.label)}</span><span class="hb-inst-score">${Math.round(f.score * 100)}%</span></button></li>`).join("")}</ul></div>`
|
|
375
|
+
: `<p class="hb-lay-empty-note">${s.ready ? (studio.hasEverything ? "find every object at once, or press a thing on the picture." : "press a thing to cut it out.") : "nothing yet. open an image or try the sample."}</p>` +
|
|
376
|
+
(s.ready && studio.hasEverything && !s.busy ? `<button type="button" class="hb-btn hb-btn-go" data-all>find every object</button>` : "");
|
|
377
|
+
html += s.ready ? `<div class="hb-kv"><span class="hb-label">mask keeps</span><span class="hb-readout">${Math.round(s.coverage * 100)}%</span></div>` : "";
|
|
378
|
+
}
|
|
379
|
+
else if (part === "mask") {
|
|
380
|
+
html =
|
|
381
|
+
`<div class="hb-seg is-toggle" role="group" aria-label="mode">${["add", "erase"].map((m) => `<button type="button" data-mode="${m}" class="${s.mode === m ? "is-on" : ""}" aria-pressed="${s.mode === m}">${m}<span class="hb-mask-key" aria-hidden="true">x</span></button>`).join("")}</div>` +
|
|
382
|
+
(s.tool === "brush" || s.tool === "eraser" ? `<label class="hb-field"><span class="hb-label">brush, ${s.size} px</span><input class="hb-range" type="range" min="4" max="160" step="2" value="${s.size}" data-size></label>` : "") +
|
|
383
|
+
(s.tool === "wand" || s.tool === "flood" ? `<label class="hb-field"><span class="hb-label">reach, ${s.tolerance}</span><input class="hb-range" type="range" min="0" max="100" step="1" value="${s.tolerance}" data-tol></label>` : "") +
|
|
384
|
+
(s.coverage > 0 ? `<div class="hb-toolbar"><button type="button" class="hb-btn" data-op="smooth">smooth</button><button type="button" class="hb-btn" data-op="expand">expand</button><button type="button" class="hb-btn" data-op="shrink">shrink</button><button type="button" class="hb-btn" data-op="invert">invert</button><button type="button" class="hb-btn" data-op="clear">clear</button></div>` : "");
|
|
385
|
+
}
|
|
386
|
+
else if (part === "saved") {
|
|
387
|
+
html =
|
|
388
|
+
(s.coverage > 0 ? `<div class="hb-toolbar"><button type="button" class="hb-btn" data-op="save">save this mask</button></div>` : "") +
|
|
389
|
+
(s.saved.length
|
|
390
|
+
? `<div class="hb-inst"><ul class="hb-inst-list">${s.saved.map((m) => `<li class="hb-inst-row"><button type="button" class="hb-inst-pick" data-restore="${m.id}"><img class="hb-inst-sw" src="${m.thumb}" alt=""><span class="hb-inst-name">${esc(m.name)}</span><span class="hb-inst-score">${Math.round(m.coverage * 100)}%</span></button><button type="button" class="hb-inst-act" data-forget="${m.id}" aria-label="forget ${esc(m.name)}" title="forget">${ICON.x}</button></li>`).join("")}</ul></div>`
|
|
391
|
+
: `<p class="hb-lay-empty-note">no saved masks. save one to come back to it.</p>`);
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
html =
|
|
395
|
+
s.coverage > 0
|
|
396
|
+
? `<div class="hb-toolbar" role="group" aria-label="export"><button type="button" class="hb-btn" data-ex="cutout">cutout png</button><button type="button" class="hb-btn" data-ex="alpha">mask png</button><button type="button" class="hb-btn" data-ex="matte">matte png</button></div>`
|
|
397
|
+
: `<p class="hb-lay-empty-note">exports show once there is a mask.</p>`;
|
|
398
|
+
}
|
|
399
|
+
if (html !== last)
|
|
400
|
+
host.innerHTML = last = html;
|
|
401
|
+
};
|
|
402
|
+
l.on(host, "click", (e) => {
|
|
403
|
+
const t = e.target;
|
|
404
|
+
const f = t.closest("[data-found]");
|
|
405
|
+
if (f)
|
|
406
|
+
return studio.toggleFound(Number(f.dataset.found));
|
|
407
|
+
const m = t.closest("[data-mode]");
|
|
408
|
+
if (m)
|
|
409
|
+
return studio.setMode(m.dataset.mode);
|
|
410
|
+
const op = t.closest("[data-op]")?.dataset.op;
|
|
411
|
+
if (op === "invert")
|
|
412
|
+
return studio.invert();
|
|
413
|
+
if (op === "clear")
|
|
414
|
+
return studio.clear();
|
|
415
|
+
if (op === "smooth")
|
|
416
|
+
return studio.smooth();
|
|
417
|
+
if (op === "expand")
|
|
418
|
+
return studio.expand();
|
|
419
|
+
if (op === "shrink")
|
|
420
|
+
return studio.shrink();
|
|
421
|
+
if (op === "save")
|
|
422
|
+
return studio.save();
|
|
423
|
+
const restore = t.closest("[data-restore]");
|
|
424
|
+
if (restore)
|
|
425
|
+
return studio.restore(Number(restore.dataset.restore));
|
|
426
|
+
const forget = t.closest("[data-forget]");
|
|
427
|
+
if (forget)
|
|
428
|
+
return studio.forget(Number(forget.dataset.forget));
|
|
429
|
+
const ex = t.closest("[data-ex]");
|
|
430
|
+
if (ex) {
|
|
431
|
+
const kind = ex.dataset.ex;
|
|
432
|
+
return void studio.exportAs(kind).then((b) => b && download(`${o.name ?? "mask"}-${kind}.png`, b));
|
|
433
|
+
}
|
|
434
|
+
if (t.closest("[data-all]"))
|
|
435
|
+
void studio.everything();
|
|
436
|
+
});
|
|
437
|
+
l.on(host, "input", (e) => {
|
|
438
|
+
const i = e.target;
|
|
439
|
+
if (i.dataset.size !== undefined)
|
|
440
|
+
studio.setSize(Number(i.value));
|
|
441
|
+
if (i.dataset.tol !== undefined)
|
|
442
|
+
studio.setTolerance(Number(i.value));
|
|
443
|
+
});
|
|
444
|
+
l.add(studio.subscribe(draw));
|
|
445
|
+
return { dispose: () => l.off() };
|
|
446
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ModelSelect in the DOM: the closed line (or the bar chip) with its one
|
|
3
|
+
* pink "load, <size>", and the sheet in a drawer with search, the three
|
|
4
|
+
* filters, grouped rows, options and the sheet's own "your own" forms. Rows come from
|
|
5
|
+
* useModelPick; hackbuild-ui modelSelect, modelSheet and drawer draw them.
|
|
6
|
+
*/
|
|
7
|
+
import { type ModelRow } from "@virgilvox/hackbuild-ui/vanilla";
|
|
8
|
+
import { type Mounted } from "./dom.js";
|
|
9
|
+
import type { JobsPick, ModelPick } from "./pick.js";
|
|
10
|
+
/** The sheet of one pick, in a drawer on the body. Opens on `open()`. */
|
|
11
|
+
export declare function pickSheet(pick: ModelPick): Mounted & {
|
|
12
|
+
open(): void;
|
|
13
|
+
};
|
|
14
|
+
export interface ModelSelectOptions {
|
|
15
|
+
/** "line" (default): the one line and the pink load. "chip": the one model chip of the bar. */
|
|
16
|
+
variant?: "line" | "chip";
|
|
17
|
+
/** Draw the pink load. Off on a gate, whose own button is the pink action. */
|
|
18
|
+
go?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/** The closed picker over a host; pressing it opens the sheet. */
|
|
21
|
+
export declare function mountModelSelect(host: HTMLElement, pick: ModelPick, o?: ModelSelectOptions): Mounted;
|
|
22
|
+
/** The words of a gate's one pink button for a pick: "load, 1.7 gb", then "ready". */
|
|
23
|
+
export declare function gateWords(row: ModelRow | undefined): string;
|
|
24
|
+
/** Several jobs: one line per job, presets with totals, and one pink "load all, <total>". */
|
|
25
|
+
export declare function mountModelJobs(host: HTMLElement, jobs: JobsPick, o?: {
|
|
26
|
+
go?: boolean;
|
|
27
|
+
}): Mounted;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ModelSelect in the DOM: the closed line (or the bar chip) with its one
|
|
3
|
+
* pink "load, <size>", and the sheet in a drawer with search, the three
|
|
4
|
+
* filters, grouped rows, options and the sheet's own "your own" forms. Rows come from
|
|
5
|
+
* useModelPick; hackbuild-ui modelSelect, modelSheet and drawer draw them.
|
|
6
|
+
*/
|
|
7
|
+
import { canLoad, drawer, lineWords, loadLabel, modelSelect, modelSheet } from "@virgilvox/hackbuild-ui/vanilla";
|
|
8
|
+
import { esc, listeners } from "./dom.js";
|
|
9
|
+
/** The sheet of one pick, in a drawer on the body. Opens on `open()`. */
|
|
10
|
+
export function pickSheet(pick) {
|
|
11
|
+
const doc = document;
|
|
12
|
+
const root = doc.createElement("div");
|
|
13
|
+
root.className = "hb-drawer hb";
|
|
14
|
+
const s0 = pick.snapshot();
|
|
15
|
+
root.innerHTML = `<div class="hb-drawer-panel" role="dialog" aria-modal="true"><div class="hb-drawer-hd"><span>${esc(s0.title)}</span><button type="button" class="hb-iconbtn" aria-label="close" data-hb-close><svg class="hb-icon is-stroke" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M6 6l12 12M18 6 6 18"/></svg></button></div><div class="hb-msheet-host"></div></div>`;
|
|
16
|
+
doc.body.append(root);
|
|
17
|
+
const body = root.querySelector(".hb-msheet-host");
|
|
18
|
+
const sheet = modelSheet(body, {
|
|
19
|
+
rows: s0.rows,
|
|
20
|
+
chosen: s0.chosen,
|
|
21
|
+
title: s0.title,
|
|
22
|
+
onChoose: (id) => pick.choose(id),
|
|
23
|
+
onLoad: (id) => void pick.load(id).catch(() => undefined),
|
|
24
|
+
onCancel: (id) => pick.cancel(id),
|
|
25
|
+
onRemove: (id) => void pick.remove(id),
|
|
26
|
+
onOption: (id, k, v) => pick.option(id, k, v),
|
|
27
|
+
onCustom: (kind, values) => void pick.custom(kind, values),
|
|
28
|
+
});
|
|
29
|
+
const l = listeners();
|
|
30
|
+
const handle = drawer(root, { side: "auto", breakpoint: 641, label: s0.title });
|
|
31
|
+
l.add(pick.subscribe((s) => sheet.update(s.rows, s.chosen)));
|
|
32
|
+
return {
|
|
33
|
+
open: () => handle.open(),
|
|
34
|
+
dispose() {
|
|
35
|
+
l.off();
|
|
36
|
+
sheet.dispose();
|
|
37
|
+
handle.dispose();
|
|
38
|
+
root.remove();
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function chipWords(s) {
|
|
43
|
+
const w = lineWords(s.row, s.empty);
|
|
44
|
+
return w.state ? `${w.name}, ${w.state}` : w.name;
|
|
45
|
+
}
|
|
46
|
+
/** The closed picker over a host; pressing it opens the sheet. */
|
|
47
|
+
export function mountModelSelect(host, pick, o = {}) {
|
|
48
|
+
const sheet = pickSheet(pick);
|
|
49
|
+
host.classList.add("hb-msel-host");
|
|
50
|
+
const l = listeners();
|
|
51
|
+
if (o.variant === "chip") {
|
|
52
|
+
const draw = (s) => {
|
|
53
|
+
const words = chipWords(s);
|
|
54
|
+
host.innerHTML = `<button type="button" class="hb-msel-chip${s.row?.state ? ` is-${s.row.state}` : ""}" aria-haspopup="dialog" aria-label="model: ${esc(words)}"><span>${esc(words)}</span></button>`;
|
|
55
|
+
};
|
|
56
|
+
l.on(host, "click", (e) => {
|
|
57
|
+
if (e.target.closest(".hb-msel-chip"))
|
|
58
|
+
sheet.open();
|
|
59
|
+
});
|
|
60
|
+
l.add(pick.subscribe(draw));
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
const inner = document.createElement("div");
|
|
64
|
+
host.replaceChildren(inner);
|
|
65
|
+
const s0 = pick.snapshot();
|
|
66
|
+
const sel = modelSelect(inner, { rows: s0.rows, chosen: s0.chosen, go: o.go ?? true, empty: s0.empty, onOpen: () => sheet.open(), onLoad: (id) => void pick.load(id).catch(() => undefined) });
|
|
67
|
+
l.add(pick.subscribe((s) => sel.update(s.rows, s.chosen)));
|
|
68
|
+
l.add(() => sel.dispose());
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
dispose() {
|
|
72
|
+
l.off();
|
|
73
|
+
sheet.dispose();
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** The words of a gate's one pink button for a pick: "load, 1.7 gb", then "ready". */
|
|
78
|
+
export function gateWords(row) {
|
|
79
|
+
return row && canLoad(row) ? loadLabel(row) : row?.state === "loaded" ? "ready" : loadLabel(row);
|
|
80
|
+
}
|
|
81
|
+
/** Several jobs: one line per job, presets with totals, and one pink "load all, <total>". */
|
|
82
|
+
export function mountModelJobs(host, jobs, o = {}) {
|
|
83
|
+
const sheets = new Map([...jobs.picks].map(([job, p]) => [job, pickSheet(p)]));
|
|
84
|
+
host.classList.add("hb-msel-host");
|
|
85
|
+
const draw = () => {
|
|
86
|
+
const presets = jobs.presets();
|
|
87
|
+
const all = jobs.all();
|
|
88
|
+
host.innerHTML =
|
|
89
|
+
`<div class="hb-msel-jobs">` +
|
|
90
|
+
(presets.length ? `<div class="hb-msel-presets" role="group" aria-label="presets">${presets.map((p) => `<button type="button" class="hb-chip" data-preset="${esc(p.id)}">${esc(p.label)}, ${esc(p.total)}</button>`).join("")}</div>` : "") +
|
|
91
|
+
jobs
|
|
92
|
+
.lines()
|
|
93
|
+
.map((j) => {
|
|
94
|
+
const w = lineWords(j.row, "no model");
|
|
95
|
+
return `<div class="hb-msel-job"><span class="hb-msel-joblabel">${esc(j.label)}${j.required ? "<b> required</b>" : ""}</span><button type="button" class="hb-msel-line" data-job="${j.job}" aria-haspopup="dialog" aria-label="${esc(j.label)}: ${esc(w.name)}"><span class="hb-msel-name">${esc(w.name)}</span>${w.meta ? `<span class="hb-msel-meta">${esc(w.meta)}</span>` : ""}${w.state ? `<span class="hb-msel-state${j.row?.state === "loaded" ? " is-loaded" : ""}">${esc(w.state)}</span>` : ""}</button></div>`;
|
|
96
|
+
})
|
|
97
|
+
.join("") +
|
|
98
|
+
((o.go ?? true) && all.ids.length ? `<div class="hb-msel"><button type="button" class="hb-btn hb-btn-go" data-all>${esc(all.label)}</button></div>` : "") +
|
|
99
|
+
`</div>`;
|
|
100
|
+
};
|
|
101
|
+
const l = listeners();
|
|
102
|
+
l.on(host, "click", (e) => {
|
|
103
|
+
const t = e.target;
|
|
104
|
+
const job = t.closest("[data-job]")?.dataset.job;
|
|
105
|
+
if (job)
|
|
106
|
+
return sheets.get(job)?.open();
|
|
107
|
+
const pre = t.closest("[data-preset]")?.dataset.preset;
|
|
108
|
+
if (pre)
|
|
109
|
+
return jobs.applyPreset(pre);
|
|
110
|
+
if (t.closest("[data-all]"))
|
|
111
|
+
void jobs.loadAll();
|
|
112
|
+
});
|
|
113
|
+
l.add(jobs.subscribe(draw));
|
|
114
|
+
draw();
|
|
115
|
+
return {
|
|
116
|
+
dispose() {
|
|
117
|
+
l.off();
|
|
118
|
+
for (const s of sheets.values())
|
|
119
|
+
s.dispose();
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TranscribeDrop, TranscriptView and their rack parts in the DOM, after the
|
|
3
|
+
* earlier transcription tool: the drop zone is the page until a file is in
|
|
4
|
+
* it, then the transcript is. Lines carry their time, edit in place (one
|
|
5
|
+
* undo step each), and export as txt, srt, vtt and json once there is text.
|
|
6
|
+
*/
|
|
7
|
+
import { type Mounted } from "./dom.js";
|
|
8
|
+
import type { Channel, TranscribeQueue } from "./transcribe.js";
|
|
9
|
+
export interface TranscriptViewOptions {
|
|
10
|
+
/** Speaker fields beside each line. Off by default: they are inputs, and a subject keeps few controls. */
|
|
11
|
+
speakers?: boolean;
|
|
12
|
+
/** Times as buttons that seek a player. */
|
|
13
|
+
onSeek?: (seconds: number) => void;
|
|
14
|
+
/** Show dropped lines in place. */
|
|
15
|
+
showDropped?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export declare function transcriptHtml(c: Channel | undefined, o?: TranscriptViewOptions): string;
|
|
18
|
+
/** The selected channel's transcript over a host. */
|
|
19
|
+
export declare function mountTranscriptView(host: HTMLElement, queue: TranscribeQueue, o?: TranscriptViewOptions): Mounted;
|
|
20
|
+
export interface TranscribeDropOptions extends TranscriptViewOptions {
|
|
21
|
+
accept?: string;
|
|
22
|
+
title?: string;
|
|
23
|
+
note?: string;
|
|
24
|
+
pickLabel?: string;
|
|
25
|
+
/** A sample a person can try with no file; the page queues it. */
|
|
26
|
+
sampleLabel?: string;
|
|
27
|
+
onSample?: () => void;
|
|
28
|
+
}
|
|
29
|
+
export interface TranscribeDropHandle extends Mounted {
|
|
30
|
+
/** Open the file picker: the add files verb. */
|
|
31
|
+
pick(): void;
|
|
32
|
+
}
|
|
33
|
+
/** The drop zone that becomes the transcript. */
|
|
34
|
+
export declare function mountTranscribeDrop(host: HTMLElement, queue: TranscribeQueue, o?: TranscribeDropOptions): TranscribeDropHandle;
|
|
35
|
+
/** The queue as hackbuild-ui strips: name, state, figures, progress, remove; run and stop. */
|
|
36
|
+
export declare function mountTranscribeQueue(host: HTMLElement, queue: TranscribeQueue): Mounted;
|
|
37
|
+
/** Exports and undo for the selected transcript. Nothing shows until there is text; undo shows once there is a change. */
|
|
38
|
+
/** `go` makes the first export the one pink action: the next thing to do once there is text. */
|
|
39
|
+
export declare function mountTranscriptExports(host: HTMLElement, queue: TranscribeQueue, o?: {
|
|
40
|
+
save?: (name: string, text: string, type: string) => void;
|
|
41
|
+
go?: boolean;
|
|
42
|
+
}): Mounted;
|