@sealpixel/ui 1.0.0-rc.20 → 1.0.0-rc.22
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
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The built-in tools are module-level objects, shared by every editor on the
|
|
3
|
+
* page. Their transient state (a selection, a drag, a pick in the panel) must
|
|
4
|
+
* not be: a selection in one editor would show in the other. The editor marks
|
|
5
|
+
* every context it builds with itself as the owner, and a tool keeps one
|
|
6
|
+
* state object per owner.
|
|
7
|
+
*/
|
|
8
|
+
const owners = new WeakMap();
|
|
9
|
+
const states = new WeakMap();
|
|
10
|
+
/** A context that no editor marked, such as one built in a test, shares this owner. */
|
|
11
|
+
const unowned = {};
|
|
12
|
+
/** Marks `ctx` as built by `owner`, so the tools keep their state for that owner. */
|
|
13
|
+
export function bindToolContext(owner, ctx) {
|
|
14
|
+
owners.set(ctx, owner);
|
|
15
|
+
return ctx;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The tool state that `create` makes, once per owner of `ctx`. `create` is
|
|
19
|
+
* the key, so every tool passes the same module-level function on each call.
|
|
20
|
+
*/
|
|
21
|
+
export function toolState(ctx, create) {
|
|
22
|
+
const owner = owners.get(ctx) ?? unowned;
|
|
23
|
+
let byTool = states.get(owner);
|
|
24
|
+
if (!byTool) {
|
|
25
|
+
byTool = new Map();
|
|
26
|
+
states.set(owner, byTool);
|
|
27
|
+
}
|
|
28
|
+
if (!byTool.has(create))
|
|
29
|
+
byTool.set(create, create());
|
|
30
|
+
return byTool.get(create);
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=instance-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"instance-state.js","sourceRoot":"","sources":["../../src/tools/instance-state.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,MAAM,GAAG,IAAI,OAAO,EAAyB,CAAC;AACpD,MAAM,MAAM,GAAG,IAAI,OAAO,EAAuC,CAAC;AAElE,uFAAuF;AACvF,MAAM,OAAO,GAAG,EAAE,CAAC;AAEnB,qFAAqF;AACrF,MAAM,UAAU,eAAe,CAA0B,KAAa,EAAE,GAAM;IAC5E,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACvB,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAI,GAAkB,EAAE,MAAe;IAC9D,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,OAAO,CAAC;IACzC,IAAI,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC/B,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;QACnB,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAC5B,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC;QAAE,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IACtD,OAAO,MAAM,CAAC,GAAG,CAAC,MAAM,CAAM,CAAC;AACjC,CAAC","sourcesContent":["import type { UiToolContext } from '../tool.js';\n\n/**\n * The built-in tools are module-level objects, shared by every editor on the\n * page. Their transient state (a selection, a drag, a pick in the panel) must\n * not be: a selection in one editor would show in the other. The editor marks\n * every context it builds with itself as the owner, and a tool keeps one\n * state object per owner.\n */\nconst owners = new WeakMap<UiToolContext, object>();\nconst states = new WeakMap<object, Map<() => unknown, unknown>>();\n\n/** A context that no editor marked, such as one built in a test, shares this owner. */\nconst unowned = {};\n\n/** Marks `ctx` as built by `owner`, so the tools keep their state for that owner. */\nexport function bindToolContext<C extends UiToolContext>(owner: object, ctx: C): C {\n owners.set(ctx, owner);\n return ctx;\n}\n\n/**\n * The tool state that `create` makes, once per owner of `ctx`. `create` is\n * the key, so every tool passes the same module-level function on each call.\n */\nexport function toolState<S>(ctx: UiToolContext, create: () => S): S {\n const owner = owners.get(ctx) ?? unowned;\n let byTool = states.get(owner);\n if (!byTool) {\n byTool = new Map();\n states.set(owner, byTool);\n }\n if (!byTool.has(create)) byTool.set(create, create());\n return byTool.get(create) as S;\n}\n"]}
|
package/dist/tools/keys.d.ts
CHANGED
|
@@ -16,8 +16,8 @@ export interface ElementKeyActions {
|
|
|
16
16
|
select: () => void;
|
|
17
17
|
/** Every arrow keydown, repeats included. */
|
|
18
18
|
move: (dx: number, dy: number) => void;
|
|
19
|
-
/** An arrow keyup, and blur: the keyup is lost when focus leaves mid-hold. */
|
|
20
|
-
commit: () => void;
|
|
19
|
+
/** An arrow keyup, and blur: the keyup is lost when focus leaves mid-hold. `e` is that event. */
|
|
20
|
+
commit: (e: Event) => void;
|
|
21
21
|
/** Delete and Backspace. `el` is the element, so focus can move on before it goes. */
|
|
22
22
|
remove: (el: Element) => void;
|
|
23
23
|
/** Escape. True when it deselected or cancelled something, so the key is consumed. */
|
|
@@ -32,7 +32,7 @@ export interface ElementKeyActions {
|
|
|
32
32
|
export declare function elementKeys(stage: KeyStage, a: ElementKeyActions): {
|
|
33
33
|
keydown: (e: KeyboardEvent) => void;
|
|
34
34
|
keyup: (e: KeyboardEvent) => void;
|
|
35
|
-
blur: () => void;
|
|
35
|
+
blur: (e: FocusEvent) => void;
|
|
36
36
|
focus: () => void;
|
|
37
37
|
};
|
|
38
38
|
/**
|
|
@@ -57,3 +57,95 @@ export declare function focusWhen(pending: {
|
|
|
57
57
|
* continues inside the editor instead of from the top of the page.
|
|
58
58
|
*/
|
|
59
59
|
export declare function focusEditor(el: Element): void;
|
|
60
|
+
export interface SliderKeyRange {
|
|
61
|
+
min: number;
|
|
62
|
+
max: number;
|
|
63
|
+
/** An arrow key. */
|
|
64
|
+
step: number;
|
|
65
|
+
/** Shift with an arrow key, PageUp and PageDown. */
|
|
66
|
+
largeStep: number;
|
|
67
|
+
}
|
|
68
|
+
export interface SliderKeyActions {
|
|
69
|
+
/** Every value keydown, repeats included. `next` maps the current value to the new one, already clamped. */
|
|
70
|
+
set: (next: (value: number) => number) => void;
|
|
71
|
+
/** The keyup of a value key, and blur. `e` is that event. */
|
|
72
|
+
commit: (e: Event) => void;
|
|
73
|
+
/** Escape. True when it cancelled something, so the key is consumed. */
|
|
74
|
+
escape: () => boolean;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Listeners for an element with `role="slider"` in a tool overlay, with the
|
|
78
|
+
* keys the ARIA slider pattern asks for: Right and Up increase, Left and Down
|
|
79
|
+
* decrease, PageUp and PageDown take a large step, Home and End go to the
|
|
80
|
+
* minimum and the maximum. A held key changes the preview, the release
|
|
81
|
+
* commits, as `elementKeys` does for a move.
|
|
82
|
+
*/
|
|
83
|
+
export declare function sliderKeys(range: SliderKeyRange, a: SliderKeyActions): {
|
|
84
|
+
keydown: (e: KeyboardEvent) => void;
|
|
85
|
+
keyup: (e: KeyboardEvent) => void;
|
|
86
|
+
blur: (e: FocusEvent) => void;
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Writes `text` to the editor's `aria-live` status, so a screen reader says
|
|
90
|
+
* what a key on the stage did. `el` is any element inside the editor.
|
|
91
|
+
*/
|
|
92
|
+
export declare function announce(el: Element, text: string): void;
|
|
93
|
+
/**
|
|
94
|
+
* Where a shape added from the keyboard goes, in unit space: a square on
|
|
95
|
+
* screen, centred on the part of the image that is in view and inside
|
|
96
|
+
* `keep`, with a side of a quarter of that part's shorter side. `view` and
|
|
97
|
+
* `keep` are in preview pixels; null means no limit.
|
|
98
|
+
*/
|
|
99
|
+
export declare function newShapeBox(W: number, H: number, view: {
|
|
100
|
+
x0: number;
|
|
101
|
+
y0: number;
|
|
102
|
+
x1: number;
|
|
103
|
+
y1: number;
|
|
104
|
+
} | null, keep: {
|
|
105
|
+
x: number;
|
|
106
|
+
y: number;
|
|
107
|
+
width: number;
|
|
108
|
+
height: number;
|
|
109
|
+
} | null): {
|
|
110
|
+
x: number;
|
|
111
|
+
y: number;
|
|
112
|
+
width: number;
|
|
113
|
+
height: number;
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* The part of the preview that is on screen, in preview pixels: the box of
|
|
117
|
+
* the stage element that holds `el`, mapped through `toImage`.
|
|
118
|
+
*/
|
|
119
|
+
export declare function viewOf(el: Element, toImage: (x: number, y: number) => {
|
|
120
|
+
x: number;
|
|
121
|
+
y: number;
|
|
122
|
+
}): {
|
|
123
|
+
x0: number;
|
|
124
|
+
y0: number;
|
|
125
|
+
x1: number;
|
|
126
|
+
y1: number;
|
|
127
|
+
} | null;
|
|
128
|
+
/** Eight points on the ellipse inside `box`: the freeform shape a key adds. Unit space. */
|
|
129
|
+
export declare function octagonIn(box: {
|
|
130
|
+
x: number;
|
|
131
|
+
y: number;
|
|
132
|
+
width: number;
|
|
133
|
+
height: number;
|
|
134
|
+
}): [number, number][];
|
|
135
|
+
/**
|
|
136
|
+
* The angle a turn slider showed last, for one element. The log does not
|
|
137
|
+
* keep this angle: a freeform path has no angle at all, and a redact rect
|
|
138
|
+
* turned by a quarter is stored unturned with its sides swapped. The tool
|
|
139
|
+
* keeps it, together with the element as it was then (`of`, as JSON).
|
|
140
|
+
*/
|
|
141
|
+
export interface KeptAngle {
|
|
142
|
+
id: string;
|
|
143
|
+
degrees: number;
|
|
144
|
+
of: string;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* The value of a turn slider in degrees: the kept angle while it belongs to
|
|
148
|
+
* this element and the element did not change since, otherwise `fallback`,
|
|
149
|
+
* the angle of the element's frame.
|
|
150
|
+
*/
|
|
151
|
+
export declare function sliderAngle(kept: KeptAngle | null, id: string, element: unknown, fallback: number): number;
|
package/dist/tools/keys.js
CHANGED
|
@@ -52,9 +52,9 @@ export function elementKeys(stage, a) {
|
|
|
52
52
|
},
|
|
53
53
|
keyup: (e) => {
|
|
54
54
|
if (e.key in ARROWS)
|
|
55
|
-
a.commit();
|
|
55
|
+
a.commit(e);
|
|
56
56
|
},
|
|
57
|
-
blur: () => a.commit(),
|
|
57
|
+
blur: (e) => a.commit(e),
|
|
58
58
|
focus: () => a.select(),
|
|
59
59
|
};
|
|
60
60
|
}
|
|
@@ -117,4 +117,137 @@ function tabHost(el) {
|
|
|
117
117
|
export function focusEditor(el) {
|
|
118
118
|
tabHost(el)?.focus({ preventScroll: true });
|
|
119
119
|
}
|
|
120
|
+
const SLIDER_KEYS = new Set([
|
|
121
|
+
'ArrowLeft',
|
|
122
|
+
'ArrowRight',
|
|
123
|
+
'ArrowUp',
|
|
124
|
+
'ArrowDown',
|
|
125
|
+
'PageUp',
|
|
126
|
+
'PageDown',
|
|
127
|
+
'Home',
|
|
128
|
+
'End',
|
|
129
|
+
]);
|
|
130
|
+
/**
|
|
131
|
+
* Listeners for an element with `role="slider"` in a tool overlay, with the
|
|
132
|
+
* keys the ARIA slider pattern asks for: Right and Up increase, Left and Down
|
|
133
|
+
* decrease, PageUp and PageDown take a large step, Home and End go to the
|
|
134
|
+
* minimum and the maximum. A held key changes the preview, the release
|
|
135
|
+
* commits, as `elementKeys` does for a move.
|
|
136
|
+
*/
|
|
137
|
+
export function sliderKeys(range, a) {
|
|
138
|
+
const clampTo = (v) => Math.min(range.max, Math.max(range.min, v));
|
|
139
|
+
return {
|
|
140
|
+
keydown: (e) => {
|
|
141
|
+
if (e.altKey || e.ctrlKey || e.metaKey)
|
|
142
|
+
return;
|
|
143
|
+
const step = e.shiftKey ? range.largeStep : range.step;
|
|
144
|
+
let next = null;
|
|
145
|
+
if (e.key === 'ArrowRight' || e.key === 'ArrowUp')
|
|
146
|
+
next = (v) => v + step;
|
|
147
|
+
else if (e.key === 'ArrowLeft' || e.key === 'ArrowDown')
|
|
148
|
+
next = (v) => v - step;
|
|
149
|
+
else if (e.key === 'PageUp')
|
|
150
|
+
next = (v) => v + range.largeStep;
|
|
151
|
+
else if (e.key === 'PageDown')
|
|
152
|
+
next = (v) => v - range.largeStep;
|
|
153
|
+
else if (e.key === 'Home')
|
|
154
|
+
next = () => range.min;
|
|
155
|
+
else if (e.key === 'End')
|
|
156
|
+
next = () => range.max;
|
|
157
|
+
if (next) {
|
|
158
|
+
e.preventDefault();
|
|
159
|
+
const to = next;
|
|
160
|
+
a.set((v) => clampTo(to(v)));
|
|
161
|
+
}
|
|
162
|
+
else if (e.key === 'Escape' && a.escape()) {
|
|
163
|
+
e.stopPropagation();
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
keyup: (e) => {
|
|
167
|
+
if (SLIDER_KEYS.has(e.key))
|
|
168
|
+
a.commit(e);
|
|
169
|
+
},
|
|
170
|
+
blur: (e) => a.commit(e),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Writes `text` to the editor's `aria-live` status, so a screen reader says
|
|
175
|
+
* what a key on the stage did. `el` is any element inside the editor.
|
|
176
|
+
*/
|
|
177
|
+
export function announce(el, text) {
|
|
178
|
+
const live = tabHost(el)?.shadowRoot?.querySelector('#live');
|
|
179
|
+
if (live)
|
|
180
|
+
live.textContent = text;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Where a shape added from the keyboard goes, in unit space: a square on
|
|
184
|
+
* screen, centred on the part of the image that is in view and inside
|
|
185
|
+
* `keep`, with a side of a quarter of that part's shorter side. `view` and
|
|
186
|
+
* `keep` are in preview pixels; null means no limit.
|
|
187
|
+
*/
|
|
188
|
+
export function newShapeBox(W, H, view, keep) {
|
|
189
|
+
let x0 = 0;
|
|
190
|
+
let y0 = 0;
|
|
191
|
+
let x1 = W;
|
|
192
|
+
let y1 = H;
|
|
193
|
+
for (const r of [
|
|
194
|
+
view,
|
|
195
|
+
keep && { x0: keep.x, y0: keep.y, x1: keep.x + keep.width, y1: keep.y + keep.height },
|
|
196
|
+
]) {
|
|
197
|
+
if (!r)
|
|
198
|
+
continue;
|
|
199
|
+
const nx0 = Math.max(x0, r.x0);
|
|
200
|
+
const ny0 = Math.max(y0, r.y0);
|
|
201
|
+
const nx1 = Math.min(x1, r.x1);
|
|
202
|
+
const ny1 = Math.min(y1, r.y1);
|
|
203
|
+
// A limit that leaves nothing is ignored: the shape still lands on the image.
|
|
204
|
+
if (nx1 - nx0 < 1 || ny1 - ny0 < 1)
|
|
205
|
+
continue;
|
|
206
|
+
[x0, y0, x1, y1] = [nx0, ny0, nx1, ny1];
|
|
207
|
+
}
|
|
208
|
+
const side = Math.min(x1 - x0, y1 - y0) / 4;
|
|
209
|
+
const cx = (x0 + x1) / 2;
|
|
210
|
+
const cy = (y0 + y1) / 2;
|
|
211
|
+
return { x: (cx - side / 2) / W, y: (cy - side / 2) / H, width: side / W, height: side / H };
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* The part of the preview that is on screen, in preview pixels: the box of
|
|
215
|
+
* the stage element that holds `el`, mapped through `toImage`.
|
|
216
|
+
*/
|
|
217
|
+
export function viewOf(el, toImage) {
|
|
218
|
+
const root = el.getRootNode();
|
|
219
|
+
if (!(root instanceof ShadowRoot))
|
|
220
|
+
return null;
|
|
221
|
+
const r = root.host.getBoundingClientRect();
|
|
222
|
+
if (!r.width || !r.height)
|
|
223
|
+
return null;
|
|
224
|
+
const a = toImage(r.left, r.top);
|
|
225
|
+
const b = toImage(r.right, r.bottom);
|
|
226
|
+
return {
|
|
227
|
+
x0: Math.min(a.x, b.x),
|
|
228
|
+
y0: Math.min(a.y, b.y),
|
|
229
|
+
x1: Math.max(a.x, b.x),
|
|
230
|
+
y1: Math.max(a.y, b.y),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
/** Eight points on the ellipse inside `box`: the freeform shape a key adds. Unit space. */
|
|
234
|
+
export function octagonIn(box) {
|
|
235
|
+
const out = [];
|
|
236
|
+
for (let i = 0; i < 8; i++) {
|
|
237
|
+
const a = ((i + 0.5) * Math.PI) / 4;
|
|
238
|
+
out.push([
|
|
239
|
+
box.x + (box.width / 2) * (1 + Math.cos(a)),
|
|
240
|
+
box.y + (box.height / 2) * (1 + Math.sin(a)),
|
|
241
|
+
]);
|
|
242
|
+
}
|
|
243
|
+
return out;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* The value of a turn slider in degrees: the kept angle while it belongs to
|
|
247
|
+
* this element and the element did not change since, otherwise `fallback`,
|
|
248
|
+
* the angle of the element's frame.
|
|
249
|
+
*/
|
|
250
|
+
export function sliderAngle(kept, id, element, fallback) {
|
|
251
|
+
return kept && kept.id === id && kept.of === JSON.stringify(element) ? kept.degrees : fallback;
|
|
252
|
+
}
|
|
120
253
|
//# sourceMappingURL=keys.js.map
|
package/dist/tools/keys.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keys.js","sourceRoot":"","sources":["../../src/tools/keys.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,uBAAuB,CAAC;AAS5C,MAAM,MAAM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CAAC;AAC9C,CAAC","sourcesContent":["import { ref } from 'lit/directives/ref.js';\n\n/** The part of the stage a keyboard move needs: size in preview pixels and the screen zoom. */\nexport interface KeyStage {\n width: number;\n height: number;\n zoom: number;\n}\n\nconst ARROWS: Record<string, [number, number]> = {\n ArrowLeft: [-1, 0],\n ArrowRight: [1, 0],\n ArrowUp: [0, -1],\n ArrowDown: [0, 1],\n};\n\n/**\n * One screen pixel, ten with Shift, as a delta in the unit space of the\n * stage; null for any other key or an empty stage. A screen pixel is what the\n * user sees move, so the step stays visible at every zoom.\n */\nexport function nudgeDelta(key: string, shift: boolean, stage: KeyStage): [number, number] | null {\n const dir = ARROWS[key];\n if (!dir || !stage.width || !stage.height || !stage.zoom) return null;\n const px = shift ? 10 : 1;\n return [(dir[0] * px) / (stage.zoom * stage.width), (dir[1] * px) / (stage.zoom * stage.height)];\n}\n\nexport interface ElementKeyActions {\n /** Focus, Enter and Space. */\n select: () => void;\n /** Every arrow keydown, repeats included. */\n move: (dx: number, dy: number) => void;\n /** An arrow keyup, and blur: the keyup is lost when focus leaves mid-hold. */\n commit: () => void;\n /** Delete and Backspace. `el` is the element, so focus can move on before it goes. */\n remove: (el: Element) => void;\n /** Escape. True when it deselected or cancelled something, so the key is consumed. */\n escape: () => boolean;\n}\n\n/**\n * Listeners for one focusable element in a tool overlay. The element itself\n * listens, so a key means this element only while it has focus: arrows on a\n * toolbar button still switch tools, Backspace in a text field still edits\n * text, and Cmd/Ctrl+Z still bubbles to the editor.\n */\nexport function elementKeys(stage: KeyStage, a: ElementKeyActions) {\n return {\n keydown: (e: KeyboardEvent) => {\n // Alt, Ctrl and Meta belong to the browser, the OS and screen readers;\n // Cmd/Ctrl+Z bubbles on to the editor.\n if (e.altKey || e.ctrlKey || e.metaKey) return;\n const delta = nudgeDelta(e.key, e.shiftKey, stage);\n if (delta) {\n e.preventDefault();\n a.move(delta[0], delta[1]);\n return;\n }\n if (e.repeat) return;\n if (e.key === 'Delete' || e.key === 'Backspace') {\n e.preventDefault();\n a.remove(e.currentTarget as Element);\n } else if (e.key === 'Enter' || e.key === ' ') {\n e.preventDefault();\n a.select();\n } else if (e.key === 'Escape' && a.escape()) {\n e.stopPropagation();\n }\n },\n keyup: (e: KeyboardEvent) => {\n if (e.key in ARROWS) a.commit();\n },\n blur: () => a.commit(),\n focus: () => a.select(),\n };\n}\n\n/**\n * A `ref` that focuses the element on the render where `pending.focusPending`\n * is its id, then clears the flag. A tool sets the flag when it adds an\n * element or hands the selection on after a deletion, so the keys work on the\n * element the user just made or is now looking at. A new element is still in\n * Lit's fragment when the ref runs, so its focus waits for the commit.\n *\n * The ref also catches the element leaving while it has focus: an undo, a\n * pointer press on the trash button or a tool switch. Lit calls the callback\n * with `undefined` before it removes the node, and also when it merely swaps\n * the callback on a re-render, so the check waits a microtask: a removed\n * element has left the DOM by then and focus has fallen to the body, a kept\n * one is still connected.\n */\nexport function focusWhen(\n pending: { focusPending: string | null },\n id: string,\n): ReturnType<typeof ref> {\n let seen: Element | undefined;\n return ref((el) => {\n if (!el) {\n const gone = seen;\n seen = undefined;\n const host = gone && tabHost(gone);\n if (!host) return;\n queueMicrotask(() => {\n if (!gone.isConnected && document.activeElement === document.body) {\n host.focus({ preventScroll: true });\n }\n });\n return;\n }\n seen = el;\n if (pending.focusPending !== id) return;\n pending.focusPending = null;\n const focus = () => (el as HTMLElement).focus({ preventScroll: true });\n if (el.isConnected) focus();\n else queueMicrotask(focus);\n });\n}\n\n/** The nearest shadow host that carries a `tabindex`: the editor. */\nfunction tabHost(el: Element): HTMLElement | null {\n let root = el.getRootNode();\n while (root instanceof ShadowRoot) {\n const host = root.host;\n if (host.hasAttribute('tabindex')) return host as HTMLElement;\n root = host.getRootNode();\n }\n return null;\n}\n\n/**\n * Focuses the editor. Used before a focused button leaves the DOM, so Tab\n * continues inside the editor instead of from the top of the page.\n */\nexport function focusEditor(el: Element): void {\n tabHost(el)?.focus({ preventScroll: true });\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ke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,EAAE,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;QACtB,EAAE,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;KACvB,CAAC;AACJ,CAAC;AAED,2FAA2F;AAC3F,MAAM,UAAU,SAAS,CAAC,GAKzB;IACC,MAAM,GAAG,GAAuB,EAAE,CAAC;IACnC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC3B,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC;QACpC,GAAG,CAAC,IAAI,CAAC;YACP,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YAC3C,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;SAC7C,CAAC,CAAC;IACL,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAcD;;;;GAIG;AACH,MAAM,UAAU,WAAW,CACzB,IAAsB,EACtB,EAAU,EACV,OAAgB,EAChB,QAAgB;IAEhB,OAAO,IAAI,IAAI,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,IAAI,CAAC,EAAE,KAAK,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC;AACjG,CAAC","sourcesContent":["import { ref } from 'lit/directives/ref.js';\n\n/** The part of the stage a keyboard move needs: size in preview pixels and the screen zoom. */\nexport interface KeyStage {\n width: number;\n height: number;\n zoom: number;\n}\n\nconst ARROWS: Record<string, [number, number]> = {\n ArrowLeft: [-1, 0],\n ArrowRight: [1, 0],\n ArrowUp: [0, -1],\n ArrowDown: [0, 1],\n};\n\n/**\n * One screen pixel, ten with Shift, as a delta in the unit space of the\n * stage; null for any other key or an empty stage. A screen pixel is what the\n * user sees move, so the step stays visible at every zoom.\n */\nexport function nudgeDelta(key: string, shift: boolean, stage: KeyStage): [number, number] | null {\n const dir = ARROWS[key];\n if (!dir || !stage.width || !stage.height || !stage.zoom) return null;\n const px = shift ? 10 : 1;\n return [(dir[0] * px) / (stage.zoom * stage.width), (dir[1] * px) / (stage.zoom * stage.height)];\n}\n\nexport interface ElementKeyActions {\n /** Focus, Enter and Space. */\n select: () => void;\n /** Every arrow keydown, repeats included. */\n move: (dx: number, dy: number) => void;\n /** An arrow keyup, and blur: the keyup is lost when focus leaves mid-hold. `e` is that event. */\n commit: (e: Event) => void;\n /** Delete and Backspace. `el` is the element, so focus can move on before it goes. */\n remove: (el: Element) => void;\n /** Escape. True when it deselected or cancelled something, so the key is consumed. */\n escape: () => boolean;\n}\n\n/**\n * Listeners for one focusable element in a tool overlay. The element itself\n * listens, so a key means this element only while it has focus: arrows on a\n * toolbar button still switch tools, Backspace in a text field still edits\n * text, and Cmd/Ctrl+Z still bubbles to the editor.\n */\nexport function elementKeys(stage: KeyStage, a: ElementKeyActions) {\n return {\n keydown: (e: KeyboardEvent) => {\n // Alt, Ctrl and Meta belong to the browser, the OS and screen readers;\n // Cmd/Ctrl+Z bubbles on to the editor.\n if (e.altKey || e.ctrlKey || e.metaKey) return;\n const delta = nudgeDelta(e.key, e.shiftKey, stage);\n if (delta) {\n e.preventDefault();\n a.move(delta[0], delta[1]);\n return;\n }\n if (e.repeat) return;\n if (e.key === 'Delete' || e.key === 'Backspace') {\n e.preventDefault();\n a.remove(e.currentTarget as Element);\n } else if (e.key === 'Enter' || e.key === ' ') {\n e.preventDefault();\n a.select();\n } else if (e.key === 'Escape' && a.escape()) {\n e.stopPropagation();\n }\n },\n keyup: (e: KeyboardEvent) => {\n if (e.key in ARROWS) a.commit(e);\n },\n blur: (e: FocusEvent) => a.commit(e),\n focus: () => a.select(),\n };\n}\n\n/**\n * A `ref` that focuses the element on the render where `pending.focusPending`\n * is its id, then clears the flag. A tool sets the flag when it adds an\n * element or hands the selection on after a deletion, so the keys work on the\n * element the user just made or is now looking at. A new element is still in\n * Lit's fragment when the ref runs, so its focus waits for the commit.\n *\n * The ref also catches the element leaving while it has focus: an undo, a\n * pointer press on the trash button or a tool switch. Lit calls the callback\n * with `undefined` before it removes the node, and also when it merely swaps\n * the callback on a re-render, so the check waits a microtask: a removed\n * element has left the DOM by then and focus has fallen to the body, a kept\n * one is still connected.\n */\nexport function focusWhen(\n pending: { focusPending: string | null },\n id: string,\n): ReturnType<typeof ref> {\n let seen: Element | undefined;\n return ref((el) => {\n if (!el) {\n const gone = seen;\n seen = undefined;\n const host = gone && tabHost(gone);\n if (!host) return;\n queueMicrotask(() => {\n if (!gone.isConnected && document.activeElement === document.body) {\n host.focus({ preventScroll: true });\n }\n });\n return;\n }\n seen = el;\n if (pending.focusPending !== id) return;\n pending.focusPending = null;\n const focus = () => (el as HTMLElement).focus({ preventScroll: true });\n if (el.isConnected) focus();\n else queueMicrotask(focus);\n });\n}\n\n/** The nearest shadow host that carries a `tabindex`: the editor. */\nfunction tabHost(el: Element): HTMLElement | null {\n let root = el.getRootNode();\n while (root instanceof ShadowRoot) {\n const host = root.host;\n if (host.hasAttribute('tabindex')) return host as HTMLElement;\n root = host.getRootNode();\n }\n return null;\n}\n\n/**\n * Focuses the editor. Used before a focused button leaves the DOM, so Tab\n * continues inside the editor instead of from the top of the page.\n */\nexport function focusEditor(el: Element): void {\n tabHost(el)?.focus({ preventScroll: true });\n}\n\nexport interface SliderKeyRange {\n min: number;\n max: number;\n /** An arrow key. */\n step: number;\n /** Shift with an arrow key, PageUp and PageDown. */\n largeStep: number;\n}\n\nexport interface SliderKeyActions {\n /** Every value keydown, repeats included. `next` maps the current value to the new one, already clamped. */\n set: (next: (value: number) => number) => void;\n /** The keyup of a value key, and blur. `e` is that event. */\n commit: (e: Event) => void;\n /** Escape. True when it cancelled something, so the key is consumed. */\n escape: () => boolean;\n}\n\nconst SLIDER_KEYS = new Set([\n 'ArrowLeft',\n 'ArrowRight',\n 'ArrowUp',\n 'ArrowDown',\n 'PageUp',\n 'PageDown',\n 'Home',\n 'End',\n]);\n\n/**\n * Listeners for an element with `role=\"slider\"` in a tool overlay, with the\n * keys the ARIA slider pattern asks for: Right and Up increase, Left and Down\n * decrease, PageUp and PageDown take a large step, Home and End go to the\n * minimum and the maximum. A held key changes the preview, the release\n * commits, as `elementKeys` does for a move.\n */\nexport function sliderKeys(range: SliderKeyRange, a: SliderKeyActions) {\n const clampTo = (v: number) => Math.min(range.max, Math.max(range.min, v));\n return {\n keydown: (e: KeyboardEvent) => {\n if (e.altKey || e.ctrlKey || e.metaKey) return;\n const step = e.shiftKey ? range.largeStep : range.step;\n let next: ((v: number) => number) | null = null;\n if (e.key === 'ArrowRight' || e.key === 'ArrowUp') next = (v) => v + step;\n else if (e.key === 'ArrowLeft' || e.key === 'ArrowDown') next = (v) => v - step;\n else if (e.key === 'PageUp') next = (v) => v + range.largeStep;\n else if (e.key === 'PageDown') next = (v) => v - range.largeStep;\n else if (e.key === 'Home') next = () => range.min;\n else if (e.key === 'End') next = () => range.max;\n if (next) {\n e.preventDefault();\n const to = next;\n a.set((v) => clampTo(to(v)));\n } else if (e.key === 'Escape' && a.escape()) {\n e.stopPropagation();\n }\n },\n keyup: (e: KeyboardEvent) => {\n if (SLIDER_KEYS.has(e.key)) a.commit(e);\n },\n blur: (e: FocusEvent) => a.commit(e),\n };\n}\n\n/**\n * Writes `text` to the editor's `aria-live` status, so a screen reader says\n * what a key on the stage did. `el` is any element inside the editor.\n */\nexport function announce(el: Element, text: string): void {\n const live = tabHost(el)?.shadowRoot?.querySelector('#live');\n if (live) live.textContent = text;\n}\n\n/**\n * Where a shape added from the keyboard goes, in unit space: a square on\n * screen, centred on the part of the image that is in view and inside\n * `keep`, with a side of a quarter of that part's shorter side. `view` and\n * `keep` are in preview pixels; null means no limit.\n */\nexport function newShapeBox(\n W: number,\n H: number,\n view: { x0: number; y0: number; x1: number; y1: number } | null,\n keep: { x: number; y: number; width: number; height: number } | null,\n): { x: number; y: number; width: number; height: number } {\n let x0 = 0;\n let y0 = 0;\n let x1 = W;\n let y1 = H;\n for (const r of [\n view,\n keep && { x0: keep.x, y0: keep.y, x1: keep.x + keep.width, y1: keep.y + keep.height },\n ]) {\n if (!r) continue;\n const nx0 = Math.max(x0, r.x0);\n const ny0 = Math.max(y0, r.y0);\n const nx1 = Math.min(x1, r.x1);\n const ny1 = Math.min(y1, r.y1);\n // A limit that leaves nothing is ignored: the shape still lands on the image.\n if (nx1 - nx0 < 1 || ny1 - ny0 < 1) continue;\n [x0, y0, x1, y1] = [nx0, ny0, nx1, ny1];\n }\n const side = Math.min(x1 - x0, y1 - y0) / 4;\n const cx = (x0 + x1) / 2;\n const cy = (y0 + y1) / 2;\n return { x: (cx - side / 2) / W, y: (cy - side / 2) / H, width: side / W, height: side / H };\n}\n\n/**\n * The part of the preview that is on screen, in preview pixels: the box of\n * the stage element that holds `el`, mapped through `toImage`.\n */\nexport function viewOf(\n el: Element,\n toImage: (x: number, y: number) => { x: number; y: number },\n): { x0: number; y0: number; x1: number; y1: number } | null {\n const root = el.getRootNode();\n if (!(root instanceof ShadowRoot)) return null;\n const r = root.host.getBoundingClientRect();\n if (!r.width || !r.height) return null;\n const a = toImage(r.left, r.top);\n const b = toImage(r.right, r.bottom);\n return {\n x0: Math.min(a.x, b.x),\n y0: Math.min(a.y, b.y),\n x1: Math.max(a.x, b.x),\n y1: Math.max(a.y, b.y),\n };\n}\n\n/** Eight points on the ellipse inside `box`: the freeform shape a key adds. Unit space. */\nexport function octagonIn(box: {\n x: number;\n y: number;\n width: number;\n height: number;\n}): [number, number][] {\n const out: [number, number][] = [];\n for (let i = 0; i < 8; i++) {\n const a = ((i + 0.5) * Math.PI) / 4;\n out.push([\n box.x + (box.width / 2) * (1 + Math.cos(a)),\n box.y + (box.height / 2) * (1 + Math.sin(a)),\n ]);\n }\n return out;\n}\n\n/**\n * The angle a turn slider showed last, for one element. The log does not\n * keep this angle: a freeform path has no angle at all, and a redact rect\n * turned by a quarter is stored unturned with its sides swapped. The tool\n * keeps it, together with the element as it was then (`of`, as JSON).\n */\nexport interface KeptAngle {\n id: string;\n degrees: number;\n of: string;\n}\n\n/**\n * The value of a turn slider in degrees: the kept angle while it belongs to\n * this element and the element did not change since, otherwise `fallback`,\n * the angle of the element's frame.\n */\nexport function sliderAngle(\n kept: KeptAngle | null,\n id: string,\n element: unknown,\n fallback: number,\n): number {\n return kept && kept.id === id && kept.of === JSON.stringify(element) ? kept.degrees : fallback;\n}\n"]}
|
package/dist/tools/redact.d.ts
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type RedactMode, type RedactOptions } from '@sealpixel/plugins';
|
|
2
2
|
import type { UiTool } from '../tool.js';
|
|
3
|
-
type Region
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
import { type Region } from './region-transform.js';
|
|
4
|
+
/**
|
|
5
|
+
* The mode a new region gets: the user's pick while the options still offer
|
|
6
|
+
* it, otherwise `defaultMode`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function newRegionMode(options: RedactOptions, picked: RedactMode | null): RedactMode;
|
|
9
|
+
/**
|
|
10
|
+
* The soft edge a new region gets: the user's pick while the control is
|
|
11
|
+
* offered, otherwise `defaultFeather`. Kept within `featherRange`.
|
|
12
|
+
*/
|
|
13
|
+
export declare function newRegionFeather(options: RedactOptions, picked: number | null): number;
|
|
14
|
+
/**
|
|
15
|
+
* The region moved by `dx`, `dy` in unit space. A turned rect or ellipse is
|
|
16
|
+
* bounded by the corners of its turned box, which needs the preview size `W`
|
|
17
|
+
* by `H`: the turn applies in pixels.
|
|
18
|
+
*/
|
|
19
|
+
export declare function translateRegion(region: Region, dx: number, dy: number, W: number, H: number): Region;
|
|
11
20
|
export declare const redactTool: UiTool;
|
|
12
|
-
export {};
|