diff --git a/e2e/annotate.e2e.ts b/e2e/annotate.e2e.ts index 90122e5c..f1aed730 100644 --- a/e2e/annotate.e2e.ts +++ b/e2e/annotate.e2e.ts @@ -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(); + 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"); +}); diff --git a/packages/annotate/docs/components/screenshot.md b/packages/annotate/docs/components/screenshot.md index dbba3f54..724fdcdc 100644 --- a/packages/annotate/docs/components/screenshot.md +++ b/packages/annotate/docs/components/screenshot.md @@ -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 @@ -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 diff --git a/packages/annotate/docs/index.md b/packages/annotate/docs/index.md index 8f885f2c..bb0dbf64 100644 --- a/packages/annotate/docs/index.md +++ b/packages/annotate/docs/index.md @@ -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 | @@ -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 diff --git a/packages/annotate/src/html.ts b/packages/annotate/src/html.ts index af730987..c0895498 100644 --- a/packages/annotate/src/html.ts +++ b/packages/annotate/src/html.ts @@ -96,6 +96,11 @@ const COPY = `` + `` + ``; +// A checkbox, so readers can hide the marks to see what's under them without the script. +const HIDE = + ``; const HEAD_DOWN = ``; // And rising from a note below it. const ARROW_UP = ``; @@ -304,7 +309,7 @@ async function parts( return { sides: sided.map((annotation) => ` ca-${annotation.side}`), heading: heading && `
]*>