@sealpixel/ui 1.0.0-rc.21 → 1.0.0-rc.23
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/dist/components/editor/empty-state.d.ts +14 -0
- package/dist/components/editor/empty-state.js +38 -0
- package/dist/components/editor/empty-state.js.map +1 -0
- package/dist/components/editor/json-attribute.d.ts +5 -0
- package/dist/components/editor/json-attribute.js +18 -0
- package/dist/components/editor/json-attribute.js.map +1 -0
- package/dist/components/editor/panel-fit.d.ts +20 -0
- package/dist/components/editor/panel-fit.js +60 -0
- package/dist/components/editor/panel-fit.js.map +1 -0
- package/dist/components/editor/styles.d.ts +2 -0
- package/dist/components/editor/styles.js +697 -0
- package/dist/components/editor/styles.js.map +1 -0
- package/dist/components/editor/toast.d.ts +8 -0
- package/dist/components/editor/toast.js +21 -0
- package/dist/components/editor/toast.js.map +1 -0
- package/dist/components/sealpixel-editor.d.ts +9 -44
- package/dist/components/sealpixel-editor.js +45 -888
- package/dist/components/sealpixel-editor.js.map +1 -1
- package/dist/components/sealpixel-stage.js +11 -2
- package/dist/components/sealpixel-stage.js.map +1 -1
- package/dist/controllers/history-shortcuts-controller.d.ts +18 -0
- package/dist/controllers/history-shortcuts-controller.js +68 -0
- package/dist/controllers/history-shortcuts-controller.js.map +1 -0
- package/dist/locales/de.js +16 -0
- package/dist/locales/de.js.map +1 -1
- package/dist/locales/en.js +16 -0
- package/dist/locales/en.js.map +1 -1
- package/dist/locales/es.js +16 -0
- package/dist/locales/es.js.map +1 -1
- package/dist/locales/fr.js +16 -0
- package/dist/locales/fr.js.map +1 -1
- package/dist/locales/ja.js +16 -0
- package/dist/locales/ja.js.map +1 -1
- package/dist/locales/pt.js +16 -0
- package/dist/locales/pt.js.map +1 -1
- package/dist/tools/annotate.d.ts +58 -0
- package/dist/tools/annotate.js +136 -12
- package/dist/tools/annotate.js.map +1 -1
- package/dist/tools/assist.d.ts +3 -0
- package/dist/tools/assist.js +44 -9
- package/dist/tools/assist.js.map +1 -1
- package/dist/tools/crop-mask.d.ts +2 -0
- package/dist/tools/crop-mask.js +11 -0
- package/dist/tools/crop-mask.js.map +1 -1
- package/dist/tools/crop.d.ts +9 -0
- package/dist/tools/crop.js +117 -48
- package/dist/tools/crop.js.map +1 -1
- package/dist/tools/filter.js +41 -19
- package/dist/tools/filter.js.map +1 -1
- package/dist/tools/finetune.js +10 -7
- package/dist/tools/finetune.js.map +1 -1
- package/dist/tools/frame.js +10 -10
- package/dist/tools/frame.js.map +1 -1
- package/dist/tools/instance-state.d.ts +8 -0
- package/dist/tools/instance-state.js +32 -0
- package/dist/tools/instance-state.js.map +1 -0
- package/dist/tools/keys.d.ts +95 -3
- package/dist/tools/keys.js +135 -2
- package/dist/tools/keys.js.map +1 -1
- package/dist/tools/redact.d.ts +18 -10
- package/dist/tools/redact.js +233 -35
- package/dist/tools/redact.js.map +1 -1
- package/dist/tools/region-transform.d.ts +23 -14
- package/dist/tools/region-transform.js +45 -32
- package/dist/tools/region-transform.js.map +1 -1
- package/dist/tools/resize.js +11 -9
- package/dist/tools/resize.js.map +1 -1
- package/dist/tools/sticker.js +7 -3
- package/dist/tools/sticker.js.map +1 -1
- package/package.json +4 -4
package/dist/tools/redact.js
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
import { REDACT_DEFAULT_OPTIONS } from '@sealpixel/plugins';
|
|
2
2
|
import { html, nothing, svg } from 'lit';
|
|
3
3
|
import { icons } from '../icons.js';
|
|
4
|
-
import { cropClipPath } from './crop-mask.js';
|
|
4
|
+
import { cropBox, cropClipPath } from './crop-mask.js';
|
|
5
5
|
import { deleteHandle, pointsBox, toPx } from './delete-handle.js';
|
|
6
6
|
import { opId } from './finetune.js';
|
|
7
|
-
import {
|
|
7
|
+
import { toolState } from './instance-state.js';
|
|
8
|
+
import { announce, elementKeys, focusEditor, focusWhen, newShapeBox, octagonIn, sliderAngle, sliderKeys, viewOf, } from './keys.js';
|
|
8
9
|
import { corners, describe, resize, slide, snapAngle, transformRegion, } from './region-transform.js';
|
|
10
|
+
/**
|
|
11
|
+
* The mode a new region gets: the user's pick while the options still offer
|
|
12
|
+
* it, otherwise `defaultMode`.
|
|
13
|
+
*/
|
|
14
|
+
export function newRegionMode(options, picked) {
|
|
15
|
+
return picked !== null && options.modes.includes(picked) ? picked : options.defaultMode;
|
|
16
|
+
}
|
|
9
17
|
/** Redact coordinates live in image space: show the image before crop and resize. */
|
|
10
18
|
function beforeCrop(log) {
|
|
11
19
|
return {
|
|
@@ -17,27 +25,60 @@ function beforeCrop(log) {
|
|
|
17
25
|
],
|
|
18
26
|
};
|
|
19
27
|
}
|
|
20
|
-
|
|
21
|
-
const state = {
|
|
28
|
+
const createRedactState = () => ({
|
|
22
29
|
shape: 'rect',
|
|
23
|
-
mode:
|
|
30
|
+
mode: null,
|
|
24
31
|
amount: 12,
|
|
32
|
+
feather: null,
|
|
25
33
|
selected: null,
|
|
26
34
|
drawing: null,
|
|
27
35
|
drag: null,
|
|
28
36
|
transform: null,
|
|
37
|
+
angle: null,
|
|
29
38
|
focusPending: null,
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
|
|
39
|
+
});
|
|
40
|
+
/**
|
|
41
|
+
* The soft edge a new region gets: the user's pick while the control is
|
|
42
|
+
* offered, otherwise `defaultFeather`. Kept within `featherRange`.
|
|
43
|
+
*/
|
|
44
|
+
export function newRegionFeather(options, picked) {
|
|
45
|
+
const r = options.featherRange;
|
|
46
|
+
if (!options.feather || picked === null)
|
|
47
|
+
return clamp(options.defaultFeather, r.min, r.max);
|
|
48
|
+
// A pick made under other options lands on a step of this range, as the slider shows it.
|
|
49
|
+
const stepped = r.min + Math.round((picked - r.min) / r.step) * r.step;
|
|
50
|
+
return clamp(stepped, r.min, r.max);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The region moved by `dx`, `dy` in unit space. A turned rect or ellipse is
|
|
54
|
+
* bounded by the corners of its turned box, which needs the preview size `W`
|
|
55
|
+
* by `H`: the turn applies in pixels.
|
|
56
|
+
*/
|
|
57
|
+
export function translateRegion(region, dx, dy, W, H) {
|
|
58
|
+
if (region.kind !== 'path') {
|
|
33
59
|
const q = region.rect;
|
|
60
|
+
if (!region.rotation) {
|
|
61
|
+
return {
|
|
62
|
+
...region,
|
|
63
|
+
rect: {
|
|
64
|
+
...q,
|
|
65
|
+
x: clamp(q.x + dx, 0, 1 - q.width),
|
|
66
|
+
y: clamp(q.y + dy, 0, 1 - q.height),
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
// The turned corners stay in the image, and the rect's own x and y stay
|
|
71
|
+
// in 0..1, as the log requires: a long box turned near a quarter can
|
|
72
|
+
// reach past an edge with its unturned rect while its corners are inside.
|
|
73
|
+
const box = corners(describe(region, W, H)).map((p) => [p[0] / W, p[1] / H]);
|
|
74
|
+
const within = (d, lo, hi) => lo > hi ? 0 : Math.min(hi, Math.max(lo, d));
|
|
75
|
+
const xs = box.map((p) => p[0]);
|
|
76
|
+
const ys = box.map((p) => p[1]);
|
|
77
|
+
const mx = within(dx, Math.max(-Math.min(...xs), -q.x), Math.min(1 - Math.max(...xs), 1 - q.x));
|
|
78
|
+
const my = within(dy, Math.max(-Math.min(...ys), -q.y), Math.min(1 - Math.max(...ys), 1 - q.y));
|
|
34
79
|
return {
|
|
35
|
-
|
|
36
|
-
rect: {
|
|
37
|
-
...q,
|
|
38
|
-
x: clamp(q.x + dx, 0, 1 - q.width),
|
|
39
|
-
y: clamp(q.y + dy, 0, 1 - q.height),
|
|
40
|
-
},
|
|
80
|
+
...region,
|
|
81
|
+
rect: { ...q, x: q.x + mx, y: q.y + my },
|
|
41
82
|
};
|
|
42
83
|
}
|
|
43
84
|
// A path moves as one body: the move stops at the edge instead of pinning
|
|
@@ -62,27 +103,28 @@ export const redactTool = {
|
|
|
62
103
|
ctx.session.setPreviewTransform(beforeCrop);
|
|
63
104
|
},
|
|
64
105
|
deactivate: (ctx) => {
|
|
106
|
+
const state = toolState(ctx, createRedactState);
|
|
65
107
|
state.drawing = null;
|
|
66
108
|
state.drag = null;
|
|
67
109
|
state.transform = null;
|
|
68
110
|
state.selected = null;
|
|
69
111
|
state.focusPending = null;
|
|
112
|
+
state.angle = null;
|
|
70
113
|
ctx.clipPreview(null);
|
|
71
114
|
ctx.session.setPreviewTransform(null);
|
|
72
115
|
},
|
|
73
116
|
panel: (ctx) => {
|
|
117
|
+
const state = toolState(ctx, createRedactState);
|
|
74
118
|
clipToCrop(ctx);
|
|
75
119
|
const doc = ctx.document.redact;
|
|
76
120
|
const options = ctx.options('redact') ?? REDACT_DEFAULT_OPTIONS;
|
|
77
|
-
if (!options.modes.includes(state.mode))
|
|
78
|
-
state.mode = options.defaultMode;
|
|
79
121
|
if (!options.shapes.includes(state.shape))
|
|
80
122
|
state.shape = options.shapes[0] ?? 'rect';
|
|
81
123
|
// The amount is configuration (`defaultAmount`), not a control.
|
|
82
124
|
state.amount = options.defaultAmount;
|
|
83
125
|
const selected = doc.regions.find((r) => r.id === state.selected) ?? null;
|
|
84
126
|
const target = selected ?? null;
|
|
85
|
-
const mode = target?.mode ?? state.mode;
|
|
127
|
+
const mode = target?.mode ?? newRegionMode(options, state.mode);
|
|
86
128
|
const setMode = (m) => {
|
|
87
129
|
state.mode = m;
|
|
88
130
|
if (target)
|
|
@@ -90,6 +132,19 @@ export const redactTool = {
|
|
|
90
132
|
else
|
|
91
133
|
ctx.refresh();
|
|
92
134
|
};
|
|
135
|
+
// The soft edge follows the selected region, like the mode; without a
|
|
136
|
+
// selection it is the edge the next region gets.
|
|
137
|
+
const range = options.featherRange;
|
|
138
|
+
const feather = target
|
|
139
|
+
? clamp(target.feather ?? 0, range.min, range.max)
|
|
140
|
+
: newRegionFeather(options, state.feather);
|
|
141
|
+
const setFeather = (f) => {
|
|
142
|
+
state.feather = f;
|
|
143
|
+
if (target)
|
|
144
|
+
ctx.dispatch({ type: 'redact.update', id: opId(), regionId: target.id, feather: f });
|
|
145
|
+
else
|
|
146
|
+
ctx.refresh();
|
|
147
|
+
};
|
|
93
148
|
// The mode is a setting of the shape, so it sits above it; the shape is the
|
|
94
149
|
// choice and takes the bottom row with the other tools' choosers.
|
|
95
150
|
return html `
|
|
@@ -110,9 +165,22 @@ export const redactTool = {
|
|
|
110
165
|
</div>
|
|
111
166
|
</div>`
|
|
112
167
|
: nothing}
|
|
168
|
+
${options.feather
|
|
169
|
+
? html `<sealpixel-slider
|
|
170
|
+
data-param="feather"
|
|
171
|
+
.label=${ctx.t('redact.feather')}
|
|
172
|
+
.value=${feather}
|
|
173
|
+
.min=${range.min}
|
|
174
|
+
.max=${range.max}
|
|
175
|
+
.step=${range.step}
|
|
176
|
+
.format=${(v) => `${Math.round(v)} px`}
|
|
177
|
+
@commit=${(e) => setFeather(e.detail)}
|
|
178
|
+
></sealpixel-slider>`
|
|
179
|
+
: nothing}
|
|
113
180
|
`;
|
|
114
181
|
},
|
|
115
182
|
overlay: (ctx) => {
|
|
183
|
+
const state = toolState(ctx, createRedactState);
|
|
116
184
|
const doc = ctx.document.redact;
|
|
117
185
|
const { width: W, height: H, zoom } = ctx.stage;
|
|
118
186
|
if (!W || !H)
|
|
@@ -122,6 +190,41 @@ export const redactTool = {
|
|
|
122
190
|
const p = ctx.stage.toImage(e.clientX, e.clientY);
|
|
123
191
|
return [clamp01(p.x / W), clamp01(p.y / H)];
|
|
124
192
|
};
|
|
193
|
+
const add = (region) => {
|
|
194
|
+
const id = opId();
|
|
195
|
+
const options = ctx.options('redact') ?? REDACT_DEFAULT_OPTIONS;
|
|
196
|
+
const amount = ctx.options('redact')?.defaultAmount ?? state.amount;
|
|
197
|
+
const feather = newRegionFeather(options, state.feather);
|
|
198
|
+
ctx.dispatch({
|
|
199
|
+
type: 'redact.add',
|
|
200
|
+
id,
|
|
201
|
+
region,
|
|
202
|
+
mode: newRegionMode(options, state.mode),
|
|
203
|
+
amount,
|
|
204
|
+
// A hard edge leaves the field out, so the op reads as before.
|
|
205
|
+
...(feather > 0 ? { feather } : {}),
|
|
206
|
+
});
|
|
207
|
+
state.selected = id;
|
|
208
|
+
state.focusPending = id;
|
|
209
|
+
};
|
|
210
|
+
// Enter or Space on the focused image adds a region of the current shape
|
|
211
|
+
// in the middle of what is in view; the arrow keys and the handles take
|
|
212
|
+
// it from there.
|
|
213
|
+
const addKey = (e) => {
|
|
214
|
+
if (e.altKey || e.ctrlKey || e.metaKey || e.repeat)
|
|
215
|
+
return;
|
|
216
|
+
if (e.key !== 'Enter' && e.key !== ' ')
|
|
217
|
+
return;
|
|
218
|
+
e.preventDefault();
|
|
219
|
+
if (state.drawing || state.drag || state.transform)
|
|
220
|
+
return;
|
|
221
|
+
const crop = ctx.session.getDocument().crop;
|
|
222
|
+
const box = newShapeBox(W, H, viewOf(e.currentTarget, ctx.stage.toImage), cropBox(crop, W, H));
|
|
223
|
+
add(state.shape === 'path'
|
|
224
|
+
? { kind: 'path', points: octagonIn(box) }
|
|
225
|
+
: { kind: state.shape, rect: box });
|
|
226
|
+
ctx.refresh();
|
|
227
|
+
};
|
|
125
228
|
const down = (e) => {
|
|
126
229
|
if (e.target.hasAttribute('data-region'))
|
|
127
230
|
return;
|
|
@@ -160,24 +263,19 @@ export const redactTool = {
|
|
|
160
263
|
if (!d)
|
|
161
264
|
return;
|
|
162
265
|
let region = null;
|
|
163
|
-
if (state.shape === 'rect') {
|
|
266
|
+
if (state.shape === 'rect' || state.shape === 'ellipse') {
|
|
164
267
|
const x = Math.min(d.start[0], d.current[0]);
|
|
165
268
|
const y = Math.min(d.start[1], d.current[1]);
|
|
166
269
|
const width = Math.abs(d.current[0] - d.start[0]);
|
|
167
270
|
const height = Math.abs(d.current[1] - d.start[1]);
|
|
168
271
|
if (width > 0.01 && height > 0.01)
|
|
169
|
-
region = { kind:
|
|
272
|
+
region = { kind: state.shape, rect: { x, y, width, height } };
|
|
170
273
|
}
|
|
171
274
|
else if (d.points.length >= 3) {
|
|
172
275
|
region = { kind: 'path', points: d.points };
|
|
173
276
|
}
|
|
174
|
-
if (region)
|
|
175
|
-
|
|
176
|
-
const amount = ctx.options('redact')?.defaultAmount ?? state.amount;
|
|
177
|
-
ctx.dispatch({ type: 'redact.add', id, region, mode: state.mode, amount });
|
|
178
|
-
state.selected = id;
|
|
179
|
-
state.focusPending = id;
|
|
180
|
-
}
|
|
277
|
+
if (region)
|
|
278
|
+
add(region);
|
|
181
279
|
ctx.refresh();
|
|
182
280
|
};
|
|
183
281
|
window.addEventListener('pointermove', move);
|
|
@@ -210,7 +308,7 @@ export const redactTool = {
|
|
|
210
308
|
if (!state.drag)
|
|
211
309
|
return;
|
|
212
310
|
const q = toN(ev);
|
|
213
|
-
state.drag.region = translateRegion(r.region, q[0] - start[0], q[1] - start[1]);
|
|
311
|
+
state.drag.region = translateRegion(r.region, q[0] - start[0], q[1] - start[1], W, H);
|
|
214
312
|
ctx.refresh();
|
|
215
313
|
};
|
|
216
314
|
const up = () => {
|
|
@@ -253,7 +351,7 @@ export const redactTool = {
|
|
|
253
351
|
const base = state.drag?.id === r.id ? state.drag.region : r.region;
|
|
254
352
|
state.drag = {
|
|
255
353
|
id: r.id,
|
|
256
|
-
region: translateRegion(base, dx, dy),
|
|
354
|
+
region: translateRegion(base, dx, dy, W, H),
|
|
257
355
|
start: [0, 0],
|
|
258
356
|
keys: true,
|
|
259
357
|
};
|
|
@@ -285,7 +383,11 @@ export const redactTool = {
|
|
|
285
383
|
const label = ctx.t('redact.region', { n: i + 1 });
|
|
286
384
|
if (region.kind === 'rect') {
|
|
287
385
|
const q = region.rect;
|
|
288
|
-
return svg `<rect data-interactive data-region=${r.id} tabindex="0" role="button" aria-pressed=${selected ? 'true' : 'false'} aria-label=${label} ${focusWhen(state, r.id)} x=${q.x * W} y=${q.y * H} width=${q.width * W} height=${q.height * H} fill=${fill} stroke=${stroke} stroke-width=${sw} stroke-dasharray=${`${4 / zoom} ${3 / zoom}`} style="cursor:move" @pointerdown=${pick} @focus=${keys.focus} @blur=${keys.blur} @keydown=${keys.keydown} @keyup=${keys.keyup} />`;
|
|
386
|
+
return svg `<rect data-interactive data-region=${r.id} tabindex="0" role="button" aria-pressed=${selected ? 'true' : 'false'} aria-label=${label} ${focusWhen(state, r.id)} x=${q.x * W} y=${q.y * H} width=${q.width * W} height=${q.height * H} transform=${turnOf(region, W, H)} fill=${fill} stroke=${stroke} stroke-width=${sw} stroke-dasharray=${`${4 / zoom} ${3 / zoom}`} style="cursor:move" @pointerdown=${pick} @focus=${keys.focus} @blur=${keys.blur} @keydown=${keys.keydown} @keyup=${keys.keyup} />`;
|
|
387
|
+
}
|
|
388
|
+
if (region.kind === 'ellipse') {
|
|
389
|
+
const q = region.rect;
|
|
390
|
+
return svg `<ellipse data-interactive data-region=${r.id} tabindex="0" role="button" aria-pressed=${selected ? 'true' : 'false'} aria-label=${label} ${focusWhen(state, r.id)} cx=${(q.x + q.width / 2) * W} cy=${(q.y + q.height / 2) * H} rx=${(q.width / 2) * W} ry=${(q.height / 2) * H} transform=${turnOf(region, W, H)} fill=${fill} stroke=${stroke} stroke-width=${sw} stroke-dasharray=${`${4 / zoom} ${3 / zoom}`} style="cursor:move" @pointerdown=${pick} @focus=${keys.focus} @blur=${keys.blur} @keydown=${keys.keydown} @keyup=${keys.keyup} />`;
|
|
289
391
|
}
|
|
290
392
|
const d = region.points
|
|
291
393
|
.map((p, i) => `${i === 0 ? 'M' : 'L'}${p[0] * W} ${p[1] * H}`)
|
|
@@ -295,7 +397,12 @@ export const redactTool = {
|
|
|
295
397
|
const selectedRegion = doc.regions.find((r) => r.id === state.selected) ?? null;
|
|
296
398
|
const regionBox = (r) => {
|
|
297
399
|
const region = shown(r);
|
|
298
|
-
|
|
400
|
+
if (region.kind === 'path')
|
|
401
|
+
return pointsBox(region.points);
|
|
402
|
+
if (!region.rotation)
|
|
403
|
+
return region.rect;
|
|
404
|
+
// A turned box: the bounds of its turned corners.
|
|
405
|
+
return pointsBox(corners(describe(region, W, H)).map((p) => [p[0] / W, p[1] / H]));
|
|
299
406
|
};
|
|
300
407
|
// Handles on the selected region: eight to resize, one below to turn. A
|
|
301
408
|
// drag maps the pointer through the region's frame and commits one
|
|
@@ -322,8 +429,81 @@ export const redactTool = {
|
|
|
322
429
|
const stem = 28 / zoom;
|
|
323
430
|
const bottom = mid(c[2], c[3]);
|
|
324
431
|
const knob = [bottom[0] - sin * stem, bottom[1] + cos * stem];
|
|
325
|
-
const
|
|
432
|
+
const keyed = state.transform?.keys && state.transform.id === r.id ? state.transform : null;
|
|
433
|
+
// The slider shows the angle the keys set last, not the angle of the
|
|
434
|
+
// stored region, so its value never jumps and Home and End are absolute.
|
|
435
|
+
const angle = keyed?.degrees ??
|
|
436
|
+
sliderAngle(state.angle, r.id, r.region, (describe(r.region, W, H).rotation * 180) / Math.PI);
|
|
437
|
+
const degrees = Math.round(angle);
|
|
326
438
|
const hr = 7 / zoom;
|
|
439
|
+
// The keys on a focused handle drive the same transient transform as a
|
|
440
|
+
// pointer drag, from the frame they reached so far, and the release
|
|
441
|
+
// commits one update. A step that would leave the image is skipped.
|
|
442
|
+
const step = (next, degrees) => {
|
|
443
|
+
if (state.drawing || state.drag || (state.transform && !keyed))
|
|
444
|
+
return;
|
|
445
|
+
const origin = keyed?.origin ?? r.region;
|
|
446
|
+
const from = keyed?.from ?? describe(origin, W, H);
|
|
447
|
+
const to = next(keyed?.frame ?? from);
|
|
448
|
+
const region = transformRegion(origin, from, to, W, H);
|
|
449
|
+
if (!region)
|
|
450
|
+
return;
|
|
451
|
+
state.transform = {
|
|
452
|
+
id: r.id,
|
|
453
|
+
origin,
|
|
454
|
+
from,
|
|
455
|
+
region,
|
|
456
|
+
keys: true,
|
|
457
|
+
frame: to,
|
|
458
|
+
...(degrees === undefined ? {} : { degrees }),
|
|
459
|
+
};
|
|
460
|
+
ctx.refresh();
|
|
461
|
+
};
|
|
462
|
+
const commitKeys = (e) => {
|
|
463
|
+
const t = state.transform;
|
|
464
|
+
if (!t?.keys || t.id !== r.id)
|
|
465
|
+
return;
|
|
466
|
+
state.transform = null;
|
|
467
|
+
if (t.degrees !== undefined) {
|
|
468
|
+
state.angle = { id: r.id, degrees: t.degrees, of: JSON.stringify(t.region) };
|
|
469
|
+
}
|
|
470
|
+
if (JSON.stringify(t.region) !== JSON.stringify(t.origin)) {
|
|
471
|
+
ctx.dispatch({ type: 'redact.update', id: opId(), regionId: r.id, region: t.region });
|
|
472
|
+
const src = ctx.state.source;
|
|
473
|
+
if (src && t.frame && t.frame.rotation === t.from.rotation) {
|
|
474
|
+
const size = describe(t.region, src.width, src.height);
|
|
475
|
+
announce(e.target, ctx.t('stage.size', {
|
|
476
|
+
width: Math.round(size.width),
|
|
477
|
+
height: Math.round(size.height),
|
|
478
|
+
}));
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
ctx.refresh();
|
|
482
|
+
};
|
|
483
|
+
const cancelKeys = () => {
|
|
484
|
+
if (!state.transform?.keys)
|
|
485
|
+
return false;
|
|
486
|
+
state.transform = null;
|
|
487
|
+
ctx.refresh();
|
|
488
|
+
return true;
|
|
489
|
+
};
|
|
490
|
+
const handleKeys = (id) => elementKeys(ctx.stage, {
|
|
491
|
+
select: () => { },
|
|
492
|
+
move: (dx, dy) => step((f) => resize(f, id, dx * W, dy * H, 6 / zoom)),
|
|
493
|
+
commit: commitKeys,
|
|
494
|
+
remove: () => { },
|
|
495
|
+
escape: cancelKeys,
|
|
496
|
+
});
|
|
497
|
+
const turnKeys = sliderKeys({ min: -180, max: 180, step: 1, largeStep: 15 }, {
|
|
498
|
+
// No snap to the quarter here: it would swallow every small step.
|
|
499
|
+
// The step turns the frame by the change of the slider's value.
|
|
500
|
+
set: (next) => {
|
|
501
|
+
const degrees = next(angle);
|
|
502
|
+
step((f) => ({ ...f, rotation: f.rotation + ((degrees - angle) * Math.PI) / 180 }), degrees);
|
|
503
|
+
},
|
|
504
|
+
commit: commitKeys,
|
|
505
|
+
escape: cancelKeys,
|
|
506
|
+
});
|
|
327
507
|
const begin = (handle) => (e) => {
|
|
328
508
|
e.stopPropagation();
|
|
329
509
|
e.preventDefault();
|
|
@@ -374,14 +554,24 @@ export const redactTool = {
|
|
|
374
554
|
};
|
|
375
555
|
return svg `
|
|
376
556
|
<line x1=${bottom[0]} y1=${bottom[1]} x2=${knob[0]} y2=${knob[1]} stroke="var(--sealpixel-color-accent)" stroke-width=${sw} pointer-events="none" />
|
|
377
|
-
${at.map(([id, p]) =>
|
|
378
|
-
|
|
557
|
+
${at.map(([id, p]) => {
|
|
558
|
+
const k = handleKeys(id);
|
|
559
|
+
return svg `<circle data-interactive data-handle=${id} tabindex="0" role="button" aria-label=${ctx.t(`handle.${id}`)} cx=${p[0]} cy=${p[1]} r=${hr} fill="white" stroke="var(--sealpixel-color-accent)" stroke-width=${sw} style=${`cursor:${cursor[id]}`} @pointerdown=${begin(id)} @keydown=${k.keydown} @keyup=${k.keyup} @blur=${k.blur} />`;
|
|
560
|
+
})}
|
|
561
|
+
<circle data-interactive data-handle="rotate" tabindex="0" role="slider" aria-label=${ctx.t('redact.rotate')} aria-valuemin="-180" aria-valuemax="180" aria-valuenow=${degrees} aria-valuetext=${`${degrees}°`} cx=${knob[0]} cy=${knob[1]} r=${hr * 1.15} fill="var(--sealpixel-color-accent)" stroke="white" stroke-width=${sw} style="cursor:grab" @pointerdown=${begin('rotate')} @keydown=${turnKeys.keydown} @keyup=${turnKeys.keyup} @blur=${turnKeys.blur} />
|
|
379
562
|
`;
|
|
380
563
|
};
|
|
381
564
|
const drawing = state.drawing;
|
|
382
565
|
let live = svg ``;
|
|
383
566
|
if (drawing) {
|
|
384
|
-
if (state.shape === '
|
|
567
|
+
if (state.shape === 'ellipse') {
|
|
568
|
+
const x = Math.min(drawing.start[0], drawing.current[0]) * W;
|
|
569
|
+
const y = Math.min(drawing.start[1], drawing.current[1]) * H;
|
|
570
|
+
const rx = (Math.abs(drawing.current[0] - drawing.start[0]) * W) / 2;
|
|
571
|
+
const ry = (Math.abs(drawing.current[1] - drawing.start[1]) * H) / 2;
|
|
572
|
+
live = svg `<ellipse cx=${x + rx} cy=${y + ry} rx=${rx} ry=${ry} fill="rgba(91,141,239,0.2)" stroke="var(--sealpixel-color-accent)" stroke-width=${sw} />`;
|
|
573
|
+
}
|
|
574
|
+
else if (state.shape === 'rect') {
|
|
385
575
|
const x = Math.min(drawing.start[0], drawing.current[0]) * W;
|
|
386
576
|
const y = Math.min(drawing.start[1], drawing.current[1]) * H;
|
|
387
577
|
live = svg `<rect x=${x} y=${y} width=${Math.abs(drawing.current[0] - drawing.start[0]) * W} height=${Math.abs(drawing.current[1] - drawing.start[1]) * H} fill="rgba(91,141,239,0.2)" stroke="var(--sealpixel-color-accent)" stroke-width=${sw} />`;
|
|
@@ -394,7 +584,7 @@ export const redactTool = {
|
|
|
394
584
|
}
|
|
395
585
|
}
|
|
396
586
|
return svg `
|
|
397
|
-
<rect data-interactive data-canvas x="0" y="0" width=${W} height=${H} fill="transparent" style="cursor:crosshair" @pointerdown=${down} />
|
|
587
|
+
<rect data-interactive data-canvas tabindex="0" role="button" aria-label=${ctx.t('redact.add')} x="0" y="0" width=${W} height=${H} fill="transparent" stroke-width=${2 / zoom} style="cursor:crosshair" @pointerdown=${down} @keydown=${addKey} />
|
|
398
588
|
${doc.regions.map(shape)}
|
|
399
589
|
${selectedRegion && options.transform ? transformHandles(selectedRegion) : nothing}
|
|
400
590
|
${selectedRegion
|
|
@@ -407,6 +597,14 @@ export const redactTool = {
|
|
|
407
597
|
`;
|
|
408
598
|
},
|
|
409
599
|
};
|
|
600
|
+
/** The SVG transform that turns a rect or an ellipse about its centre, or nothing. */
|
|
601
|
+
function turnOf(region, W, H) {
|
|
602
|
+
if (region.kind === 'path' || !region.rotation)
|
|
603
|
+
return nothing;
|
|
604
|
+
const cx = (region.rect.x + region.rect.width / 2) * W;
|
|
605
|
+
const cy = (region.rect.y + region.rect.height / 2) * H;
|
|
606
|
+
return `rotate(${(region.rotation * 180) / Math.PI} ${cx} ${cy})`;
|
|
607
|
+
}
|
|
410
608
|
function clamp01(v) {
|
|
411
609
|
return clamp(v, 0, 1);
|
|
412
610
|
}
|