Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions e2e/annotate.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,123 @@ A wide row.
expect(found.outside).toBe(true);
expect(found.red).toBeGreaterThan(0);
});

test("Hide marks hides the marks and arrows without zooming, and Copy still draws them", async ({
page,
context,
}) => {
await context.grantPermissions(["clipboard-read", "clipboard-write"]);
await open(
page,
`::screenshot{src="/before.png" alt="Filter toolbar" viewport="640x250"}
:::annotation{x=4.5 y=28.7 w=40 h=44.4 color="red"}
The toolbar wraps.
:::

:::annotation{x=60 y=40 w=10 h=10 side="right"}
Apply sits low.
:::
::`,
);
const figure = page.locator("main .ca-screenshot");
const toggle = figure.locator(".ca-hide");
const marks = figure.locator(":is(.ca-mark,.ca-arrow)");
await expect(marks).toHaveCount(3);
// Like Copy, it shows on hover, beside Copy.
await expect(toggle).toHaveCSS("opacity", "0");
await figure.locator(".ca-frame > img:not(.ca-overlay)").hover();
await expect(toggle).toHaveCSS("opacity", "1");
const [hide, copy] = [await toggle.boundingBox(), await figure.locator(".ca-copy").boundingBox()];
expect(hide!.x + hide!.width).toBeLessThan(copy!.x);

await expect(toggle.locator(".ca-eye")).toBeVisible();
await expect(toggle.locator(".ca-eye-off")).toBeHidden();
await toggle.click();
await expect(toggle.locator("input")).toBeChecked();
// The icon says the marks are hidden.
await expect(toggle.locator(".ca-eye")).toBeHidden();
await expect(toggle.locator(".ca-eye-off")).toBeVisible();
for (const mark of await marks.all()) await expect(mark).toBeHidden();
await expect(page.locator(".cz-dialog")).toHaveCount(0);
// It stays while the marks are hidden, so the reader can bring them back.
await page.mouse.move(0, 0);
await expect(toggle).toHaveCSS("opacity", "1");

await figure.locator(".ca-copy").click();
await expect(figure.locator(".ca-copy-status")).toHaveText("Copied");
const [copied, expected] = await figure.evaluate(async (node) => {
const [item] = await navigator.clipboard.read();
const bitmap = await createImageBitmap(await item!.getType("image/png"));
const box = node.getBoundingClientRect();
const stamp = node.querySelector(".ca-mark .ca-stamp")!;
const at = stamp.getBoundingClientRect();
const scale = bitmap.width / (box.width + 32);
const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
const context = canvas.getContext("2d")!;
context.drawImage(bitmap, 0, 0);
const x = Math.round((at.left + at.width / 2 - box.left + 16) * scale);
const y = Math.round((at.top + 2 - box.top + 16) * scale);
const pixel = [...context.getImageData(x, y, 1, 1).data].slice(0, 3);
context.fillStyle = getComputedStyle(stamp).backgroundColor;
context.fillRect(0, 0, 1, 1);
return [pixel, [...context.getImageData(0, 0, 1, 1).data].slice(0, 3)];
});
for (const [index, channel] of copied.entries())
expect(Math.abs(channel - expected[index]!)).toBeLessThan(24);

// A zoomed copy keeps the reader's choice, and the keyboard brings the marks back.
await figure.locator(".ca-stage").click({ position: { x: 10, y: 10 } });
await expect(page.locator(".cz-item .ca-mark")).toHaveCount(2);
for (const mark of await page.locator(".cz-item .ca-mark").all()) await expect(mark).toBeHidden();
const zoomedToggle = page.locator(".cz-item .ca-hide input");
// Native dialog focus wrapping varies; check that forward Tab reaches Hide marks.
for (let tabs = 0; tabs < 4; tabs++) {
await page.keyboard.press("Tab");
if (await zoomedToggle.evaluate((input) => input === document.activeElement)) break;
}
await expect(zoomedToggle).toBeFocused();
Comment thread
vite-hub[bot] marked this conversation as resolved.
Resolved
await page.keyboard.press("Space");
await expect(zoomedToggle).not.toBeChecked();
for (const mark of await page.locator(".cz-item .ca-mark").all())
await expect(mark).toBeVisible();
await page.keyboard.press("Shift+Tab");
await expect(page.locator(".cz-close")).toBeFocused();
await page.keyboard.press("Tab");
await expect(zoomedToggle).toBeFocused();
await page.keyboard.press("Escape");
await expect(page.locator(".cz-dialog")).toHaveCount(0);
await toggle.locator("input").focus();
await page.keyboard.press("Space");
await expect(toggle.locator("input")).not.toBeChecked();
for (const mark of await marks.all()) await expect(mark).toBeVisible();
});

test("hovering a mark or its note fades the other marks", async ({ page }) => {
await open(
page,
`::screenshot{src="/after.png" alt="Filter toolbar on one row" viewport="640x250"}
:::annotation{x=5 y=30 w=15 h=20}
Search.
:::

:::annotation{x=40 y=30 w=15 h=20}
Chips.
:::

:::annotation{x=75 y=30 w=15 h=20}
Apply.
:::
::`,
);
const mark = (n: number) => page.locator(`main .ca-mark[data-n="${n}"]`);
for (const n of [1, 2, 3]) await expect(mark(n)).toHaveCSS("opacity", "1");
await page.locator('main .ca-notes li[data-n="2"]').hover();
await expect(mark(2)).toHaveCSS("opacity", "1");
await expect(mark(1)).toHaveCSS("opacity", "0.35");
await expect(mark(3)).toHaveCSS("opacity", "0.35");
await mark(3).hover();
await expect(mark(3)).toHaveCSS("opacity", "1");
await expect(mark(2)).toHaveCSS("opacity", "0.35");
await page.mouse.move(0, 0);
for (const n of [1, 2, 3]) await expect(mark(n)).toHaveCSS("opacity", "1");
});
4 changes: 3 additions & 1 deletion packages/annotate/docs/components/screenshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Extra filters collapse into **+2 filters**.
- Each [`annotation`](/annotate/components/annotation) inside it draws one mark and its note.
- A click anywhere on the screenshot zooms it, with its notes beside it.
- The Copy button copies it as a PNG with its marks and notes drawn in.
- The Hide marks toggle beside it hides the marks and arrows, so readers can see what's under them.
- Hovering a mark or its note fades the other marks.

## Usage

Expand Down Expand Up @@ -120,7 +122,7 @@ Copying needs setup in two cases:

The image keeps its `alt` text. Each numbered mark is an image named by its number and note, such as "Annotation 1: Extra filters collapse into +2 filters." A side note's mark is named "Side note:" and its note. The arrows are hidden from screen readers.

The image is one tab stop that opens the [zoomed view](/zoom). The Copy button is labelled "Copy the screenshot with its marks", and a status announces "Copied" once the PNG is on the clipboard.
The image is one tab stop that opens the [zoomed view](/zoom). The Copy button is labelled "Copy the screenshot with its marks", and a status announces "Copied" once the PNG is on the clipboard. The Hide marks toggle is a checkbox labelled "Hide marks", and works without the script.

### Keyboard interactions

Expand Down
10 changes: 5 additions & 5 deletions packages/annotate/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,10 @@ Annotate reads these [shared tokens](/guide/theming):

| Token | Used for |
| --- | --- |
| `--comark-background` | The Copy button, the slider's line and grip, the ring around stamps |
| `--comark-foreground` | The Copy button's icon, the slider's grip, the status after a copy |
| `--comark-border` | The outline around screenshots, the Copy button, the slider's grip |
| `--comark-accent` | The hovered Copy button |
| `--comark-background` | The Copy button, the Hide marks toggle, the slider's line and grip, the ring around stamps |
| `--comark-foreground` | The icons on Copy and Hide marks, the slider's grip, the status after a copy |
| `--comark-border` | The outline around screenshots, the Copy button, the Hide marks toggle, the slider's grip |
| `--comark-accent` | The hovered Copy button and Hide marks toggle |
| `--comark-ring` | Focus rings |
| `--comark-radius` | Corners of screenshots and notes |
| `--comark-shadow` | The slider's handle and grip |
Expand Down Expand Up @@ -344,7 +344,7 @@ A screenshot's or compare's `ink` sets `--ca-color`, `--ca-solid`, and `--ca-ink
- Every block sits in a `.ca`. A screenshot's `figure.ca-screenshot` is inside it, and a compare is `.ca.ca-compare`, plus `.ca-slider` with `mode="slider"`.
- A screenshot holds `.ca-heading` with its title in `.ca-label`, then `.ca-stage` with the image in `.ca-frame`, and a `figcaption` with the notes in `ol.ca-notes`. It has `.ca-left`, `.ca-right`, `.ca-top`, or `.ca-bottom` when it has side notes there.
- A mark is a `.ca-mark` with its shape as a class, such as `.ca-box` or `.ca-circle`, and its number in `.ca-stamp`. A side note is a `.ca-callout` with its arrow in `.ca-arrow`. A mark, its arrow, and its note share a `data-n`.
- The Copy button is `.ca-copy`. A slider's divider is `.ca-handle`, with its knob in `.ca-grip`.
- The Copy button is `.ca-copy`, and the Hide marks toggle is `label.ca-hide`. A slider's divider is `.ca-handle`, with its knob in `.ca-grip`.

### Example

Expand Down
7 changes: 6 additions & 1 deletion packages/annotate/src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ const COPY =
`<svg class="ca-copy-idle" viewBox="0 0 24 24" aria-hidden="true"><rect x="8" y="8" width="14" height="14" rx="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>` +
`<svg class="ca-copy-done" viewBox="0 0 24 24" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg></button>` +
`<span class="ca-copy-status" role="status"></span>`;
// A checkbox, so readers can hide the marks to see what's under them without the script.
const HIDE =
`<label class="ca-hide" title="Hide marks"><input type="checkbox" aria-label="Hide marks">` +
Comment thread
vite-hub[bot] marked this conversation as resolved.
Resolved
`<svg class="ca-eye" viewBox="0 0 24 24" aria-hidden="true"><path d="M2.062 12.348a1 1 0 0 1 0-.696 10.75 10.75 0 0 1 19.876 0 1 1 0 0 1 0 .696 10.75 10.75 0 0 1-19.876 0"/><circle cx="12" cy="12" r="3"/></svg>` +
`<svg class="ca-eye-off" viewBox="0 0 24 24" aria-hidden="true"><path d="M10.733 5.076a10.744 10.744 0 0 1 11.205 6.575 1 1 0 0 1 0 .696 10.747 10.747 0 0 1-1.444 2.49"/><path d="M14.084 14.158a3 3 0 0 1-4.242-4.242"/><path d="M17.479 17.499a10.75 10.75 0 0 1-15.417-5.151 1 1 0 0 1 0-.696 10.75 10.75 0 0 1 4.446-5.143"/><path d="m2 2 20 20"/></svg></label>`;
const HEAD_DOWN = `<svg class="ca-arrow-head" viewBox="0 0 12 12"><path d="M1.5 3.5Q5 7 6 11Q7 7 10.5 3.5" vector-effect="non-scaling-stroke"/></svg>`;
// And rising from a note below it.
const ARROW_UP = `<svg class="ca-arrow-line" viewBox="0 0 24 100" preserveAspectRatio="none"><path d="M17 98C2 74 3 34 12 2" vector-effect="non-scaling-stroke"/></svg>`;
Expand Down Expand Up @@ -304,7 +309,7 @@ async function parts(
return {
sides: sided.map((annotation) => ` ca-${annotation.side}`),
heading: heading && `<div class="ca-heading">${heading}</div>`,
frame: `<div class="ca-frame">${body ?? `<img src="${attr(shot.src)}" alt="${attr(shot.alt)}"${sized} loading="lazy" decoding="async">`}${pins.join("")}${arrows.join("")}${copy ? COPY : ""}</div>`,
frame: `<div class="ca-frame">${body ?? `<img src="${attr(shot.src)}" alt="${attr(shot.alt)}"${sized} loading="lazy" decoding="async">`}${pins.join("")}${arrows.join("")}${copy ? `${pins.length ? HIDE : ""}${COPY}` : ""}</div>`,
callouts,
notes: caption || notes ? `${caption}${notes ? `<ol class="ca-notes">${notes}</ol>` : ""}` : "",
};
Expand Down
6 changes: 4 additions & 2 deletions packages/annotate/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export function start(): () => void {
const copying = "clipboard" in navigator && "ClipboardItem" in window;
if (copying) root.setAttribute("data-annotate-copy", "");
const PAD = 16;
const SKIP = ".ca-copy,.ca-copy-status,.ca-overlay";
const SKIP = ".ca-copy,.ca-copy-status,.ca-overlay,.ca-hide";
type Box = [x: number, y: number, width: number, height: number];
type Paint = (context: CanvasRenderingContext2D, source: HTMLImageElement) => void;
type Made = { key: string; blob: Blob; url: string };
Expand Down Expand Up @@ -708,7 +708,9 @@ export function start(): () => void {
: null))
: null;
// Zoom copies the figure as it was, a moment's "Copied" included; a thumbnail is a button and gets no button inside.
for (const part of document.querySelectorAll(".cz-thumb :is(.ca-copy,.ca-copy-status)"))
for (const part of document.querySelectorAll(
".cz-thumb :is(.ca-copy,.ca-copy-status,.ca-hide)",
))
part.remove();
for (const button of document.querySelectorAll(".cz-dialog .ca-copy[data-state]"))
button.removeAttribute("data-state");
Expand Down
19 changes: 18 additions & 1 deletion packages/annotate/src/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ export const css = `@layer comark{${[
".ca-copy:focus-visible{outline:none;box-shadow:0 0 0 3px color-mix(in srgb,var(--_comark-ring) 50%,transparent)}",
".ca-screenshot:is(:hover,:focus-visible,:has(:focus-visible)) .ca-copy,.ca-copy:focus-visible,.ca-copy[data-state],.cz-dialog .ca-copy{opacity:1}",
"@media (hover:none){[data-annotate-copy] .ca-copy{opacity:1}}",
// Hide marks shows where Copy does, beside it, and stays while the marks are hidden so the reader can bring them back.
// Hidden marks keep their paint, so Copy still draws them.
".ca-hide{position:absolute;top:.5rem;right:.5rem;display:grid;place-items:center;width:1.75rem;height:1.75rem;box-sizing:border-box;border:1px solid var(--_comark-border);border-radius:calc(var(--_comark-radius) - 4px);background:var(--_comark-background);color:var(--_comark-foreground);cursor:pointer;opacity:0;transition:opacity .15s ease,background .15s ease}",
"[data-annotate-copy] .ca-frame:has(>.ca-copy)>.ca-hide{right:2.75rem}",
".ca-frame:has(>.ca-hide)>.ca-copy-status{right:5rem}",
".ca-hide:hover{background:var(--_comark-accent)}",
".ca-hide input{position:absolute;inset:0;width:100%;height:100%;margin:0;opacity:0;cursor:pointer}",
".ca-hide:has(:focus-visible){box-shadow:0 0 0 3px color-mix(in srgb,var(--_comark-ring) 50%,transparent)}",
".ca-screenshot:is(:hover,:focus-visible,:has(:focus-visible)) .ca-hide,.ca-hide:has(:checked),.cz-dialog .ca-hide{opacity:1}",
"@media (hover:none){.ca-hide{opacity:1}}",
".ca-hide svg{width:.875rem;height:.875rem;fill:none;stroke:currentColor;stroke-width:2;stroke-linecap:round;stroke-linejoin:round}",
".ca-hide input:checked~.ca-eye,.ca-hide input:not(:checked)~.ca-eye-off{display:none}",
".ca-frame:has(>.ca-hide>:checked)>:is(.ca-mark,.ca-arrow){visibility:hidden}",
".ca-copy svg{width:.875rem;height:.875rem;fill:none;stroke:currentColor;stroke-width:2;stroke-linecap:round;stroke-linejoin:round}",
".ca-copy-done,.ca-copy[data-state=done] .ca-copy-idle{display:none}",
".ca-copy[data-state=done] .ca-copy-done{display:block}",
Expand All @@ -99,8 +112,12 @@ export const css = `@layer comark{${[
".ca-frame .ca-overlay[data-menu]{pointer-events:auto;opacity:1}",
// A copy paints the marks at rest, and nothing animates while it reads them.
"[data-ca-capture] *{transition:none!important}",
"[data-ca-capture=still] *{--ca-on:0!important}",
"[data-ca-capture=still] *{--ca-on:0!important;--ca-dim:0!important}",
".ca-mark{position:absolute;color:var(--ca-color);text-decoration:none;outline:none}",
// While a mark or its note is lit, the other marks fade, so it stands out among many.
`.ca:has([data-n]:is(:hover,:focus-visible,:target)){--ca-dim:1}`,
`.cz-dialog:has(${zoomed} [data-n]:is(:hover,:focus-visible)) ${zoomed}{--ca-dim:1}`,
".ca-mark,.ca-arrow{opacity:calc(1 - .65 * var(--ca-dim,0) * (1 - var(--ca-on,0)));transition:opacity .15s ease}",
// Strokes take the mark's full color so they read at a glance; the numbers sit beside the marks, so only a thin line
// covers the screenshot. Hovering a mark or its note thickens it.
".ca-mark,.ca-arrow{--ca-stroke:var(--ca-color)}",
Expand Down
21 changes: 20 additions & 1 deletion packages/annotate/test/annotate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ describe("comark-annotate", () => {
).toHaveLength(3);
// In the frame, after the marks, so it sits on the image's corner.
expect(html).toMatch(
/<div class="ca-frame"><img src="a.png"[^>]*><span class="ca-mark[^]*?<\/span><button type="button" class="ca-copy"[^]*?<\/button><span class="ca-copy-status" role="status"><\/span><\/div>/,
/<div class="ca-frame"><img src="a.png"[^>]*><span class="ca-mark[^]*?<\/span><label class="ca-hide"[^]*?<\/label><button type="button" class="ca-copy"[^]*?<\/button><span class="ca-copy-status" role="status"><\/span><\/div>/,
);
expect(html.match(/<script>\(function start\(\) \{/g)).toHaveLength(1);
expect(styles(html)).toContain(".ca-copy{display:none}");
Expand All @@ -317,6 +317,25 @@ describe("comark-annotate", () => {
expect(slider.html).not.toContain('class="ca-copy"');
});

it("gives every screenshot with marks a Hide marks checkbox that works without the script", async () => {
const { html } = await run(
`::screenshot{src="a.png" alt="A"}\n:::annotation{x=10 y=10 w=10 h=10}\nNote\n:::\n::\n\n::screenshot{src="b.png" alt="B"}\n::\n\n${compare}`,
);
expect(
html.match(
/<label class="ca-hide" title="Hide marks"><input type="checkbox" aria-label="Hide marks">/g,
),
).toHaveLength(3);
expect(html).not.toMatch(/<img src="b.png"[^>]*><label class="ca-hide"/);
expect(styles(html)).toContain(
".ca-frame:has(>.ca-hide>:checked)>:is(.ca-mark,.ca-arrow){visibility:hidden}",
);
expect(styles(html)).not.toMatch(/\[data-annotate-[a-z]+\] \.ca-hide\{/);

const slider = await run(compare.replace("::compare", '::compare{mode="slider"}'));
expect(slider.html).not.toContain('class="ca-hide"');
});

it("points from a note above the image without drawing a mark", async () => {
const { issues, html } = await run(
`::screenshot{src="a.png" alt="A"}\n:::annotation{x=40 y=50 w=20 h=10 mark="none" side="top"}\nStart with what failed.\n:::\n::`,
Expand Down
Loading