@sealpixel/ui 1.0.0-rc.15 → 1.0.0-rc.16

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.
@@ -1,5 +1,6 @@
1
1
  import { svg } from 'lit';
2
2
  import { TRASH_PATH } from '../icons.js';
3
+ import { focusEditor } from './keys.js';
3
4
  /**
4
5
  * A trash button floating above a selected element, rendered inside a tool
5
6
  * overlay in image pixel coordinates. It is sized through `zoom`, so it stays
@@ -20,6 +21,11 @@ export function deleteHandle(box, stage, label, onDelete) {
20
21
  };
21
22
  const act = (e) => {
22
23
  e.stopPropagation();
24
+ // The button goes with the element: a keyboard press hands focus to the
25
+ // editor first, so Tab does not start over from the top of the page.
26
+ const g = e.currentTarget;
27
+ if (g.matches(':focus'))
28
+ focusEditor(g);
23
29
  onDelete();
24
30
  };
25
31
  return svg `<g data-interactive data-action="delete" role="button" tabindex="0" aria-label=${label}
@@ -1 +1 @@
1
- {"version":3,"file":"delete-handle.js","sourceRoot":"","sources":["../../src/tools/delete-handle.ts"],"names":[],"mappings":"AACA,OAAO,EAA0B,GAAG,EAAE,MAAM,KAAK,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAC1B,GAAS,EACT,KAAsD,EACtD,KAAa,EACb,QAAoB;IAEpB,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,GAAG,KAAK,CAAC;IAC5C,MAAM,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;IACpB,MAAM,GAAG,GAAG,EAAE,GAAG,IAAI,CAAC;IACtB,MAAM,EAAE,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAClD,IAAI,EAAE,GAAG,GAAG,CAAC,CAAC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AAC,CAAC;QACnB,IAAI,CAAC,GAAG,EAAE;YAAE,EAAE,GAAG,CAAC,CAAC;QACnB,IAAI,CAAC,GAAG,EAAE;YAAE,EAAE,GAAG,CAAC,CAAC;IACrB,CAAC;IACD,OAAO,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC;AACrF,CAAC;AAED,uCAAuC;AACvC,MAAM,UAAU,IAAI,CAAC,CAAO,EAAE,CAAS,EAAE,CAAS;IAChD,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,GAAG,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;AAC9E,CAAC;AAED,SAAS,KAAK,CAAC,CAAS,EAAE,EAAU,EAAE,EAAU;IAC9C,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AACvC,CAAC","sourcesContent":["import type { Rect } from '@sealpixel/core';\nimport { type SVGTemplateResult, svg } from 'lit';\nimport { TRASH_PATH } from '../icons.js';\n\n/**\n * A trash button floating above a selected element, rendered inside a tool\n * overlay in image pixel coordinates. It is sized through `zoom`, so it stays\n * 32 px on screen at every zoom level, and moves below the element when the\n * image edge leaves no room above. Enter and Space trigger it from the keyboard.\n */\nexport function deleteHandle(\n box: Rect,\n stage: { width: number; height: number; zoom: number },\n label: string,\n onDelete: () => void,\n): SVGTemplateResult {\n const { zoom, width: W, height: H } = stage;\n const r = 16 / zoom;\n const gap = 10 / zoom;\n const cx = clamp(box.x + box.width / 2, r, W - r);\n let cy = box.y - gap - r;\n if (cy - r < 0) cy = Math.min(box.y + box.height + gap + r, H - r);\n const stop = (e: Event) => {\n e.stopPropagation();\n e.preventDefault();\n };\n const act = (e: Event) => {\n e.stopPropagation();\n onDelete();\n };\n return svg`<g data-interactive data-action=\"delete\" role=\"button\" tabindex=\"0\" aria-label=${label}\n transform=${`translate(${cx} ${cy})`} style=\"cursor:pointer\"\n @pointerdown=${stop} @click=${act}\n @keydown=${(e: KeyboardEvent) => {\n if (e.key === 'Enter' || e.key === ' ') act(e);\n }}>\n <circle r=${r} fill=\"var(--sealpixel-color-surface)\" stroke=\"var(--sealpixel-color-border)\" stroke-width=${1 / zoom} />\n <g transform=${`scale(${1 / zoom}) translate(-9 -9) scale(0.75)`} fill=\"none\" stroke=\"var(--sealpixel-color-danger)\" stroke-width=\"1.75\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path d=${TRASH_PATH} /></g>\n </g>`;\n}\n\n/** Axis-aligned box around normalized points. */\nexport function pointsBox(points: readonly (readonly [number, number])[]): Rect {\n let x0 = 1;\n let y0 = 1;\n let x1 = 0;\n let y1 = 0;\n for (const [x, y] of points) {\n if (x < x0) x0 = x;\n if (y < y0) y0 = y;\n if (x > x1) x1 = x;\n if (y > y1) y1 = y;\n }\n return { x: x0, y: y0, width: Math.max(0, x1 - x0), height: Math.max(0, y1 - y0) };\n}\n\n/** Normalized rect to image pixels. */\nexport function toPx(r: Rect, W: number, H: number): Rect {\n return { x: r.x * W, y: r.y * H, width: r.width * W, height: r.height * H };\n}\n\nfunction clamp(v: number, lo: number, hi: number): number {\n return v < lo ? lo : v > hi ? hi : v;\n}\n"]}
1
+ {"version":3,"file":"delete-handle.js","sourceRoot":"","sources":["../../src/tools/delete-handle.ts"],"names":[],"mappings":"AACA,OAAO,EAA0B,GAAG,EAAE,MAAM,KAAK,CAAC;AAClD,OAAO,EAAE,UAA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AE,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,GAAG,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;AAC9E,CAAC;AAED,SAAS,KAAK,CAAC,CAAS,EAAE,EAAU,EAAE,EAAU;IAC9C,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AACvC,CAAC","sourcesContent":["import type { Rect } from '@sealpixel/core';\nimport { type SVGTemplateResult, svg } from 'lit';\nimport { TRASH_PATH } from '../icons.js';\nimport { focusEditor } from './keys.js';\n\n/**\n * A trash button floating above a selected element, rendered inside a tool\n * overlay in image pixel coordinates. It is sized through `zoom`, so it stays\n * 32 px on screen at every zoom level, and moves below the element when the\n * image edge leaves no room above. Enter and Space trigger it from the keyboard.\n */\nexport function deleteHandle(\n box: Rect,\n stage: { width: number; height: number; zoom: number },\n label: string,\n onDelete: () => void,\n): SVGTemplateResult {\n const { zoom, width: W, height: H } = stage;\n const r = 16 / zoom;\n const gap = 10 / zoom;\n const cx = clamp(box.x + box.width / 2, r, W - r);\n let cy = box.y - gap - r;\n if (cy - r < 0) cy = Math.min(box.y + box.height + gap + r, H - r);\n const stop = (e: Event) => {\n e.stopPropagation();\n e.preventDefault();\n };\n const act = (e: Event) => {\n e.stopPropagation();\n // The button goes with the element: a keyboard press hands focus to the\n // editor first, so Tab does not start over from the top of the page.\n const g = e.currentTarget as Element;\n if (g.matches(':focus')) focusEditor(g);\n onDelete();\n };\n return svg`<g data-interactive data-action=\"delete\" role=\"button\" tabindex=\"0\" aria-label=${label}\n transform=${`translate(${cx} ${cy})`} style=\"cursor:pointer\"\n @pointerdown=${stop} @click=${act}\n @keydown=${(e: KeyboardEvent) => {\n if (e.key === 'Enter' || e.key === ' ') act(e);\n }}>\n <circle r=${r} fill=\"var(--sealpixel-color-surface)\" stroke=\"var(--sealpixel-color-border)\" stroke-width=${1 / zoom} />\n <g transform=${`scale(${1 / zoom}) translate(-9 -9) scale(0.75)`} fill=\"none\" stroke=\"var(--sealpixel-color-danger)\" stroke-width=\"1.75\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path d=${TRASH_PATH} /></g>\n </g>`;\n}\n\n/** Axis-aligned box around normalized points. */\nexport function pointsBox(points: readonly (readonly [number, number])[]): Rect {\n let x0 = 1;\n let y0 = 1;\n let x1 = 0;\n let y1 = 0;\n for (const [x, y] of points) {\n if (x < x0) x0 = x;\n if (y < y0) y0 = y;\n if (x > x1) x1 = x;\n if (y > y1) y1 = y;\n }\n return { x: x0, y: y0, width: Math.max(0, x1 - x0), height: Math.max(0, y1 - y0) };\n}\n\n/** Normalized rect to image pixels. */\nexport function toPx(r: Rect, W: number, H: number): Rect {\n return { x: r.x * W, y: r.y * H, width: r.width * W, height: r.height * H };\n}\n\nfunction clamp(v: number, lo: number, hi: number): number {\n return v < lo ? lo : v > hi ? hi : v;\n}\n"]}
@@ -1,4 +1,5 @@
1
1
  import { svg } from 'lit';
2
+ import { focusEditor } from './keys.js';
2
3
  /**
3
4
  * A row of round buttons floating above an element in a tool overlay, sized
4
5
  * through `zoom` so they stay 32 px on screen. They move below the element
@@ -22,6 +23,11 @@ export function handleRow(box, stage, handles) {
22
23
  return svg `${handles.map((h, i) => {
23
24
  const act = (e) => {
24
25
  e.stopPropagation();
26
+ // The row goes with the element: a keyboard press hands focus to the
27
+ // editor first, so Tab does not start over from the top of the page.
28
+ const g = e.currentTarget;
29
+ if (g.matches(':focus'))
30
+ focusEditor(g);
25
31
  h.onActivate();
26
32
  };
27
33
  return svg `<g data-interactive data-action=${h.action} role="button" tabindex="0" aria-label=${h.label}
@@ -1 +1 @@
1
- {"version":3,"file":"handles.js","sourceRoot":"","sources":["../../src/tools/handles.ts"],"names":[],"mappings":"AACA,OAAO,EAA0B,GAAG,EAAE,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,eAAe,EAAE,CAAC;YACpB,CAAC,CAAC,UAAU,EAAE,CAAC;QACjB,CAAC,CAAC;QACF,OAAO,GAAG,CAAA,mCAAmC,CAAC,CAAC,MAAM,0CAA0C,CAAC,CAAC,KAAK;oBACtF,aAAa,MAAM,GAAG,CAAC,GAAG,IAAI,IAAI,EAAE,GAAG;uBACpC,IAAI,WAAW,GAAG;mBACtB,CAAC,CAAgB,EAAE,EAAE;YAC9B,IAAI,CAAC,CAAC,GAAG,KAAK,OAAO,IAAI,CAAC,CAAC,GAAG,KAAK,GAAG;gBAAE,GAAG,CAAC,CAAC,CAAC,CAAC;QACjD,CAAC;kBACS,CAAC,8FAA8F,CAAC,GAAG,IAAI;qBACpG,SAAS,CAAC,GAAG,IAAI,gCAAgC,uBAAuB,CAAC,CAAC,KAAK,4EAA4E,CAAC,CAAC,IAAI;SAC7K,CAAC;IACR,CAAC,CAAC,EAAE,CAAC;AACP,CAAC;AAED,8CAA8C;AAC9C,MAAM,CAAC,MAAM,UAAU,GAAG,kBAAkB,CAAC;AAC7C,MAAM,CAAC,MAAM,UAAU,GAAG,sBAAsB,CAAC;AAEjD,SAAS,KAAK,CAAC,CAAS,EAAE,EAAU,EAAE,EAAU;IAC9C,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AACvC,CAAC","sourcesContent":["import type { Rect } from '@sealpixel/core';\nimport { type SVGTemplateResult, svg } from 'lit';\n\nexport interface HandleSpec {\n /** Icon path on the 24 px grid, drawn with `stroke`. */\n path: string;\n label: string;\n /** CSS colour of the icon stroke. */\n color: string;\n /** `data-action` value, so tests and hosts can find the button. */\n action: string;\n onActivate: () => void;\n}\n\n/**\n * A row of round buttons floating above an element in a tool overlay, sized\n * through `zoom` so they stay 32 px on screen. They move below the element\n * when the image edge leaves no room above. Enter and Space trigger them from\n * the keyboard. `deleteHandle` is the one-button ancestor of this.\n */\nexport function handleRow(\n box: Rect,\n stage: { width: number; height: number; zoom: number },\n handles: readonly HandleSpec[],\n): SVGTemplateResult {\n const { zoom, width: W, height: H } = stage;\n const r = 16 / zoom;\n const gap = 10 / zoom;\n const step = r * 2 + 6 / zoom;\n const total = step * handles.length - 6 / zoom;\n const startX = clamp(box.x + box.width / 2 - total / 2 + r, r, Math.max(r, W - total + r));\n let cy = box.y - gap - r;\n if (cy - r < 0) cy = Math.min(box.y + box.height + gap + r, H - r);\n const stop = (e: Event) => {\n e.stopPropagation();\n e.preventDefault();\n };\n return svg`${handles.map((h, i) => {\n const act = (e: Event) => {\n e.stopPropagation();\n h.onActivate();\n };\n return svg`<g data-interactive data-action=${h.action} role=\"button\" tabindex=\"0\" aria-label=${h.label}\n transform=${`translate(${startX + i * step} ${cy})`} style=\"cursor:pointer\"\n @pointerdown=${stop} @click=${act}\n @keydown=${(e: KeyboardEvent) => {\n if (e.key === 'Enter' || e.key === ' ') act(e);\n }}>\n <circle r=${r} fill=\"var(--sealpixel-color-surface)\" stroke=\"var(--sealpixel-color-border)\" stroke-width=${1 / zoom} />\n <g transform=${`scale(${1 / zoom}) translate(-9 -9) scale(0.75)`} fill=\"none\" stroke=${h.color} stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path d=${h.path} /></g>\n </g>`;\n })}`;\n}\n\n/** Check mark and cross on the 24 px grid. */\nexport const CHECK_PATH = 'M4 12.5l5 5L20 7';\nexport const CROSS_PATH = 'M6 6l12 12M18 6L6 18';\n\nfunction clamp(v: number, lo: number, hi: number): number {\n return v < lo ? lo : v > hi ? hi : v;\n}\n"]}
1
+ {"version":3,"file":"handles.js","sourceRoot":"","sources":["../../src/tools/handles.ts"],"names":[],"mappings":"AACA,OAAO,EAA0B,GAAG,EAAE,MAAM,KAAK,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAaxC;;;;;GAKG;AACH,MAAM,UAA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ACrE,MAAM,CAAC,GAAG,CAAC,CAAC,aAAwB,CAAC;YACrC,IAAI,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC;gBAAE,WAAW,CAAC,CAAC,CAAC,CAAC;YACxC,CAAC,CAAC,UAAU,EAAE,CAAC;QACjB,CAAC,CAAC;QACF,OAAO,GAAG,CAAA,mCAAmC,CAAC,CAAC,MAAM,0CAA0C,CAAC,CAAC,KAAK;oBACtF,aAAa,MAAM,GAAG,CAAC,GAAG,IAAI,IAAI,EAAE,GAAG;uBACpC,IAAI,WAAW,GAAG;mBACtB,CAAC,CAAgB,EAAE,EAAE;YAC9B,IAAI,CAAC,CAAC,GAAG,KAAK,OAAO,IAAI,CAAC,CAAC,GAAG,KAAK,GAAG;gBAAE,GAAG,CAAC,CAAC,CAAC,CAAC;QACjD,CAAC;kBACS,CAAC,8FAA8F,CAAC,GAAG,IAAI;qBACpG,SAAS,CAAC,GAAG,IAAI,gCAAgC,uBAAuB,CAAC,CAAC,KAAK,4EAA4E,CAAC,CAAC,IAAI;SAC7K,CAAC;IACR,CAAC,CAAC,EAAE,CAAC;AACP,CAAC;AAED,8CAA8C;AAC9C,MAAM,CAAC,MAAM,UAAU,GAAG,kBAAkB,CAAC;AAC7C,MAAM,CAAC,MAAM,UAAU,GAAG,sBAAsB,CAAC;AAEjD,SAAS,KAAK,CAAC,CAAS,EAAE,EAAU,EAAE,EAAU;IAC9C,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AACvC,CAAC","sourcesContent":["import type { Rect } from '@sealpixel/core';\nimport { type SVGTemplateResult, svg } from 'lit';\nimport { focusEditor } from './keys.js';\n\nexport interface HandleSpec {\n /** Icon path on the 24 px grid, drawn with `stroke`. */\n path: string;\n label: string;\n /** CSS colour of the icon stroke. */\n color: string;\n /** `data-action` value, so tests and hosts can find the button. */\n action: string;\n onActivate: () => void;\n}\n\n/**\n * A row of round buttons floating above an element in a tool overlay, sized\n * through `zoom` so they stay 32 px on screen. They move below the element\n * when the image edge leaves no room above. Enter and Space trigger them from\n * the keyboard. `deleteHandle` is the one-button ancestor of this.\n */\nexport function handleRow(\n box: Rect,\n stage: { width: number; height: number; zoom: number },\n handles: readonly HandleSpec[],\n): SVGTemplateResult {\n const { zoom, width: W, height: H } = stage;\n const r = 16 / zoom;\n const gap = 10 / zoom;\n const step = r * 2 + 6 / zoom;\n const total = step * handles.length - 6 / zoom;\n const startX = clamp(box.x + box.width / 2 - total / 2 + r, r, Math.max(r, W - total + r));\n let cy = box.y - gap - r;\n if (cy - r < 0) cy = Math.min(box.y + box.height + gap + r, H - r);\n const stop = (e: Event) => {\n e.stopPropagation();\n e.preventDefault();\n };\n return svg`${handles.map((h, i) => {\n const act = (e: Event) => {\n e.stopPropagation();\n // The row goes with the element: a keyboard press hands focus to the\n // editor first, so Tab does not start over from the top of the page.\n const g = e.currentTarget as Element;\n if (g.matches(':focus')) focusEditor(g);\n h.onActivate();\n };\n return svg`<g data-interactive data-action=${h.action} role=\"button\" tabindex=\"0\" aria-label=${h.label}\n transform=${`translate(${startX + i * step} ${cy})`} style=\"cursor:pointer\"\n @pointerdown=${stop} @click=${act}\n @keydown=${(e: KeyboardEvent) => {\n if (e.key === 'Enter' || e.key === ' ') act(e);\n }}>\n <circle r=${r} fill=\"var(--sealpixel-color-surface)\" stroke=\"var(--sealpixel-color-border)\" stroke-width=${1 / zoom} />\n <g transform=${`scale(${1 / zoom}) translate(-9 -9) scale(0.75)`} fill=\"none\" stroke=${h.color} stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path d=${h.path} /></g>\n </g>`;\n })}`;\n}\n\n/** Check mark and cross on the 24 px grid. */\nexport const CHECK_PATH = 'M4 12.5l5 5L20 7';\nexport const CROSS_PATH = 'M6 6l12 12M18 6L6 18';\n\nfunction clamp(v: number, lo: number, hi: number): number {\n return v < lo ? lo : v > hi ? hi : v;\n}\n"]}
@@ -0,0 +1,59 @@
1
+ import { ref } from 'lit/directives/ref.js';
2
+ /** The part of the stage a keyboard move needs: size in preview pixels and the screen zoom. */
3
+ export interface KeyStage {
4
+ width: number;
5
+ height: number;
6
+ zoom: number;
7
+ }
8
+ /**
9
+ * One screen pixel, ten with Shift, as a delta in the unit space of the
10
+ * stage; null for any other key or an empty stage. A screen pixel is what the
11
+ * user sees move, so the step stays visible at every zoom.
12
+ */
13
+ export declare function nudgeDelta(key: string, shift: boolean, stage: KeyStage): [number, number] | null;
14
+ export interface ElementKeyActions {
15
+ /** Focus, Enter and Space. */
16
+ select: () => void;
17
+ /** Every arrow keydown, repeats included. */
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;
21
+ /** Delete and Backspace. `el` is the element, so focus can move on before it goes. */
22
+ remove: (el: Element) => void;
23
+ /** Escape. True when it deselected or cancelled something, so the key is consumed. */
24
+ escape: () => boolean;
25
+ }
26
+ /**
27
+ * Listeners for one focusable element in a tool overlay. The element itself
28
+ * listens, so a key means this element only while it has focus: arrows on a
29
+ * toolbar button still switch tools, Backspace in a text field still edits
30
+ * text, and Cmd/Ctrl+Z still bubbles to the editor.
31
+ */
32
+ export declare function elementKeys(stage: KeyStage, a: ElementKeyActions): {
33
+ keydown: (e: KeyboardEvent) => void;
34
+ keyup: (e: KeyboardEvent) => void;
35
+ blur: () => void;
36
+ focus: () => void;
37
+ };
38
+ /**
39
+ * A `ref` that focuses the element on the render where `pending.focusPending`
40
+ * is its id, then clears the flag. A tool sets the flag when it adds an
41
+ * element or hands the selection on after a deletion, so the keys work on the
42
+ * element the user just made or is now looking at. A new element is still in
43
+ * Lit's fragment when the ref runs, so its focus waits for the commit.
44
+ *
45
+ * The ref also catches the element leaving while it has focus: an undo, a
46
+ * pointer press on the trash button or a tool switch. Lit calls the callback
47
+ * with `undefined` before it removes the node, and also when it merely swaps
48
+ * the callback on a re-render, so the check waits a microtask: a removed
49
+ * element has left the DOM by then and focus has fallen to the body, a kept
50
+ * one is still connected.
51
+ */
52
+ export declare function focusWhen(pending: {
53
+ focusPending: string | null;
54
+ }, id: string): ReturnType<typeof ref>;
55
+ /**
56
+ * Focuses the editor. Used before a focused button leaves the DOM, so Tab
57
+ * continues inside the editor instead of from the top of the page.
58
+ */
59
+ export declare function focusEditor(el: Element): void;
@@ -0,0 +1,120 @@
1
+ import { ref } from 'lit/directives/ref.js';
2
+ const ARROWS = {
3
+ ArrowLeft: [-1, 0],
4
+ ArrowRight: [1, 0],
5
+ ArrowUp: [0, -1],
6
+ ArrowDown: [0, 1],
7
+ };
8
+ /**
9
+ * One screen pixel, ten with Shift, as a delta in the unit space of the
10
+ * stage; null for any other key or an empty stage. A screen pixel is what the
11
+ * user sees move, so the step stays visible at every zoom.
12
+ */
13
+ export function nudgeDelta(key, shift, stage) {
14
+ const dir = ARROWS[key];
15
+ if (!dir || !stage.width || !stage.height || !stage.zoom)
16
+ return null;
17
+ const px = shift ? 10 : 1;
18
+ return [(dir[0] * px) / (stage.zoom * stage.width), (dir[1] * px) / (stage.zoom * stage.height)];
19
+ }
20
+ /**
21
+ * Listeners for one focusable element in a tool overlay. The element itself
22
+ * listens, so a key means this element only while it has focus: arrows on a
23
+ * toolbar button still switch tools, Backspace in a text field still edits
24
+ * text, and Cmd/Ctrl+Z still bubbles to the editor.
25
+ */
26
+ export function elementKeys(stage, a) {
27
+ return {
28
+ keydown: (e) => {
29
+ // Alt, Ctrl and Meta belong to the browser, the OS and screen readers;
30
+ // Cmd/Ctrl+Z bubbles on to the editor.
31
+ if (e.altKey || e.ctrlKey || e.metaKey)
32
+ return;
33
+ const delta = nudgeDelta(e.key, e.shiftKey, stage);
34
+ if (delta) {
35
+ e.preventDefault();
36
+ a.move(delta[0], delta[1]);
37
+ return;
38
+ }
39
+ if (e.repeat)
40
+ return;
41
+ if (e.key === 'Delete' || e.key === 'Backspace') {
42
+ e.preventDefault();
43
+ a.remove(e.currentTarget);
44
+ }
45
+ else if (e.key === 'Enter' || e.key === ' ') {
46
+ e.preventDefault();
47
+ a.select();
48
+ }
49
+ else if (e.key === 'Escape' && a.escape()) {
50
+ e.stopPropagation();
51
+ }
52
+ },
53
+ keyup: (e) => {
54
+ if (e.key in ARROWS)
55
+ a.commit();
56
+ },
57
+ blur: () => a.commit(),
58
+ focus: () => a.select(),
59
+ };
60
+ }
61
+ /**
62
+ * A `ref` that focuses the element on the render where `pending.focusPending`
63
+ * is its id, then clears the flag. A tool sets the flag when it adds an
64
+ * element or hands the selection on after a deletion, so the keys work on the
65
+ * element the user just made or is now looking at. A new element is still in
66
+ * Lit's fragment when the ref runs, so its focus waits for the commit.
67
+ *
68
+ * The ref also catches the element leaving while it has focus: an undo, a
69
+ * pointer press on the trash button or a tool switch. Lit calls the callback
70
+ * with `undefined` before it removes the node, and also when it merely swaps
71
+ * the callback on a re-render, so the check waits a microtask: a removed
72
+ * element has left the DOM by then and focus has fallen to the body, a kept
73
+ * one is still connected.
74
+ */
75
+ export function focusWhen(pending, id) {
76
+ let seen;
77
+ return ref((el) => {
78
+ if (!el) {
79
+ const gone = seen;
80
+ seen = undefined;
81
+ const host = gone && tabHost(gone);
82
+ if (!host)
83
+ return;
84
+ queueMicrotask(() => {
85
+ if (!gone.isConnected && document.activeElement === document.body) {
86
+ host.focus({ preventScroll: true });
87
+ }
88
+ });
89
+ return;
90
+ }
91
+ seen = el;
92
+ if (pending.focusPending !== id)
93
+ return;
94
+ pending.focusPending = null;
95
+ const focus = () => el.focus({ preventScroll: true });
96
+ if (el.isConnected)
97
+ focus();
98
+ else
99
+ queueMicrotask(focus);
100
+ });
101
+ }
102
+ /** The nearest shadow host that carries a `tabindex`: the editor. */
103
+ function tabHost(el) {
104
+ let root = el.getRootNode();
105
+ while (root instanceof ShadowRoot) {
106
+ const host = root.host;
107
+ if (host.hasAttribute('tabindex'))
108
+ return host;
109
+ root = host.getRootNode();
110
+ }
111
+ return null;
112
+ }
113
+ /**
114
+ * Focuses the editor. Used before a focused button leaves the DOM, so Tab
115
+ * continues inside the editor instead of from the top of the page.
116
+ */
117
+ export function focusEditor(el) {
118
+ tabHost(el)?.focus({ preventScroll: true });
119
+ }
120
+ //# sourceMappingURL=keys.js.map
@@ -0,0 +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,GAAqC;IAC/C,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAClB,UAAU,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;IAClB,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAChB,SAAS,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;CAClB,CAAC;AAEF;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,GAAW,EAAE,KAAc,EAAE,KAAe;IACrE,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;IACxB,IAAI,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI;QAAE,OAAO,IAAI,CAAC;IACtE,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1B,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;AACnG,CAAC;AAeD;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CA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C,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACvB,IAAI,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC;YAAE,OAAO,IAAmB,CAAC;QAC9D,IAAI,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;IAC5B,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,EAAW;IACrC,OAAO,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,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,2 +1,12 @@
1
+ import type { Rect } from '@sealpixel/core';
1
2
  import type { UiTool } from '../tool.js';
3
+ type Region = {
4
+ kind: 'rect';
5
+ rect: Rect;
6
+ } | {
7
+ kind: 'path';
8
+ points: [number, number][];
9
+ };
10
+ export declare function translateRegion(region: Region, dx: number, dy: number): Region;
2
11
  export declare const redactTool: UiTool;
12
+ export {};
@@ -4,6 +4,7 @@ import { icons } from '../icons.js';
4
4
  import { cropClipPath } from './crop-mask.js';
5
5
  import { deleteHandle, pointsBox, toPx } from './delete-handle.js';
6
6
  import { opId } from './finetune.js';
7
+ import { elementKeys, focusEditor, focusWhen } from './keys.js';
7
8
  import { corners, describe, resize, slide, snapAngle, transformRegion, } from './region-transform.js';
8
9
  /** Redact coordinates live in image space: show the image before crop and resize. */
9
10
  function beforeCrop(log) {
@@ -25,8 +26,9 @@ const state = {
25
26
  drawing: null,
26
27
  drag: null,
27
28
  transform: null,
29
+ focusPending: null,
28
30
  };
29
- function translateRegion(region, dx, dy) {
31
+ export function translateRegion(region, dx, dy) {
30
32
  if (region.kind === 'rect') {
31
33
  const q = region.rect;
32
34
  return {
@@ -64,6 +66,7 @@ export const redactTool = {
64
66
  state.drag = null;
65
67
  state.transform = null;
66
68
  state.selected = null;
69
+ state.focusPending = null;
67
70
  ctx.clipPreview(null);
68
71
  ctx.session.setPreviewTransform(null);
69
72
  },
@@ -136,10 +139,22 @@ export const redactTool = {
136
139
  state.drawing.points.push(q);
137
140
  ctx.refresh();
138
141
  };
139
- const up = () => {
142
+ const cleanup = () => {
140
143
  window.removeEventListener('pointermove', move);
141
144
  window.removeEventListener('pointerup', up);
142
145
  window.removeEventListener('pointercancel', up);
146
+ window.removeEventListener('keydown', cancel);
147
+ };
148
+ // Escape while drawing drops the shape: nothing reaches the log.
149
+ const cancel = (ev) => {
150
+ if (ev.key !== 'Escape')
151
+ return;
152
+ state.drawing = null;
153
+ cleanup();
154
+ ctx.refresh();
155
+ };
156
+ const up = () => {
157
+ cleanup();
143
158
  const d = state.drawing;
144
159
  state.drawing = null;
145
160
  if (!d)
@@ -161,12 +176,14 @@ export const redactTool = {
161
176
  const amount = ctx.options('redact')?.defaultAmount ?? state.amount;
162
177
  ctx.dispatch({ type: 'redact.add', id, region, mode: state.mode, amount });
163
178
  state.selected = id;
179
+ state.focusPending = id;
164
180
  }
165
181
  ctx.refresh();
166
182
  };
167
183
  window.addEventListener('pointermove', move);
168
184
  window.addEventListener('pointerup', up);
169
185
  window.addEventListener('pointercancel', up);
186
+ window.addEventListener('keydown', cancel);
170
187
  };
171
188
  const options = ctx.options('redact') ?? REDACT_DEFAULT_OPTIONS;
172
189
  const shown = (r) => state.transform?.id === r.id
@@ -174,7 +191,7 @@ export const redactTool = {
174
191
  : state.drag?.id === r.id
175
192
  ? state.drag.region
176
193
  : r.region;
177
- const shape = (r) => {
194
+ const shape = (r, i) => {
178
195
  const selected = state.selected === r.id;
179
196
  const dragging = state.drag?.id === r.id || state.transform?.id === r.id;
180
197
  const region = shown(r);
@@ -185,6 +202,8 @@ export const redactTool = {
185
202
  e.stopPropagation();
186
203
  e.preventDefault();
187
204
  state.selected = r.id;
205
+ // preventDefault above keeps the browser from focusing the region.
206
+ e.currentTarget.focus({ preventScroll: true });
188
207
  const start = toN(e);
189
208
  state.drag = { id: r.id, region: r.region, start };
190
209
  const move = (ev) => {
@@ -209,14 +228,69 @@ export const redactTool = {
209
228
  window.addEventListener('pointerup', up);
210
229
  window.addEventListener('pointercancel', up);
211
230
  };
231
+ // The keyboard moves the region through the same transient drag as the
232
+ // pointer and commits one update when the key is released.
233
+ const commit = () => {
234
+ const d = state.drag;
235
+ if (!d?.keys || d.id !== r.id)
236
+ return;
237
+ state.drag = null;
238
+ if (JSON.stringify(d.region) !== JSON.stringify(r.region)) {
239
+ ctx.dispatch({ type: 'redact.update', id: opId(), regionId: r.id, region: d.region });
240
+ }
241
+ ctx.refresh();
242
+ };
243
+ const keys = elementKeys(ctx.stage, {
244
+ select: () => {
245
+ if (state.selected === r.id)
246
+ return;
247
+ state.selected = r.id;
248
+ ctx.refresh();
249
+ },
250
+ move: (dx, dy) => {
251
+ if (state.drawing || state.transform || (state.drag && !state.drag.keys))
252
+ return;
253
+ const base = state.drag?.id === r.id ? state.drag.region : r.region;
254
+ state.drag = {
255
+ id: r.id,
256
+ region: translateRegion(base, dx, dy),
257
+ start: [0, 0],
258
+ keys: true,
259
+ };
260
+ ctx.refresh();
261
+ },
262
+ commit,
263
+ remove: (el) => {
264
+ commit();
265
+ // Focus moves on before the region goes, so Tab stays in the editor.
266
+ const next = doc.regions[i + 1] ?? doc.regions[i - 1] ?? null;
267
+ state.selected = next?.id ?? null;
268
+ state.focusPending = next?.id ?? null;
269
+ if (!next)
270
+ focusEditor(el);
271
+ ctx.dispatch({ type: 'redact.remove', id: opId(), regionId: r.id });
272
+ },
273
+ escape: () => {
274
+ // Mid-drawing the key belongs to the drawing: the canvas listener drops it.
275
+ if (state.drawing)
276
+ return false;
277
+ const open = state.selected !== null || !!state.drag?.keys;
278
+ if (state.drag?.keys)
279
+ state.drag = null;
280
+ state.selected = null;
281
+ ctx.refresh();
282
+ return open;
283
+ },
284
+ });
285
+ const label = ctx.t('redact.region', { n: i + 1 });
212
286
  if (region.kind === 'rect') {
213
287
  const q = region.rect;
214
- return svg `<rect data-interactive data-region=${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} />`;
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} />`;
215
289
  }
216
290
  const d = region.points
217
291
  .map((p, i) => `${i === 0 ? 'M' : 'L'}${p[0] * W} ${p[1] * H}`)
218
292
  .join(' ');
219
- return svg `<path data-interactive data-region=${r.id} d=${`${d} Z`} fill=${fill} stroke=${stroke} stroke-width=${sw} stroke-dasharray=${`${4 / zoom} ${3 / zoom}`} style="cursor:move" @pointerdown=${pick} />`;
293
+ return svg `<path data-interactive data-region=${r.id} tabindex="0" role="button" aria-pressed=${selected ? 'true' : 'false'} aria-label=${label} ${focusWhen(state, r.id)} d=${`${d} Z`} 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} />`;
220
294
  };
221
295
  const selectedRegion = doc.regions.find((r) => r.id === state.selected) ?? null;
222
296
  const regionBox = (r) => {
@@ -248,6 +322,7 @@ export const redactTool = {
248
322
  const stem = 28 / zoom;
249
323
  const bottom = mid(c[2], c[3]);
250
324
  const knob = [bottom[0] - sin * stem, bottom[1] + cos * stem];
325
+ const degrees = Math.round((frame.rotation * 180) / Math.PI);
251
326
  const hr = 7 / zoom;
252
327
  const begin = (handle) => (e) => {
253
328
  e.stopPropagation();
@@ -300,7 +375,7 @@ export const redactTool = {
300
375
  return svg `
301
376
  <line x1=${bottom[0]} y1=${bottom[1]} x2=${knob[0]} y2=${knob[1]} stroke="var(--sealpixel-color-accent)" stroke-width=${sw} pointer-events="none" />
302
377
  ${at.map(([id, p]) => svg `<circle data-interactive data-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)} />`)}
303
- <circle data-interactive data-handle="rotate" role="slider" aria-label=${ctx.t('redact.rotate')} 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')} />
378
+ <circle data-interactive data-handle="rotate" 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')} />
304
379
  `;
305
380
  };
306
381
  const drawing = state.drawing;