diff --git a/apps/app/e2e/shell/editor-annotations.spec.ts b/apps/app/e2e/shell/editor-annotations.spec.ts index 717f9e8b..906fd0a4 100644 --- a/apps/app/e2e/shell/editor-annotations.spec.ts +++ b/apps/app/e2e/shell/editor-annotations.spec.ts @@ -54,27 +54,49 @@ const tool = (page: Page, id: string) => const scrollWidth = (page: Page) => page.evaluate(() => document.documentElement.scrollWidth) -/** The bubble sits in the window, under the top bar, under its button and clear of the other. */ +const intersects = (a: Box, b: Box) => + a.x < b.x + b.width && + b.x < a.x + a.width && + a.y < b.y + b.height && + b.y < a.y + a.height + +interface Box { + x: number + y: number + width: number + height: number +} + +/** + * The bubble sits in the window, under the top bar, right under its button (within 8 px + * unless the bar pushes it down), and over neither « Défaire » nor the strip's commands. + */ async function expectBubbleBeside( page: Page, bubble: Locator, button: Locator, other: Locator, ) { - const box = await bubble.boundingBox() - const own = await button.boundingBox() - const next = await other.boundingBox() - const bar = await page.locator('header').first().boundingBox() + const boxes = await Promise.all([ + bubble.boundingBox(), + button.boundingBox(), + other.boundingBox(), + page.locator('header').first().boundingBox(), + page.locator('[data-testid="edit-strip"] > :last-child').boundingBox(), + ]) const view = page.viewportSize() - if (!box || !own || !next || !bar || !view) + const [box, own, next, bar, commands] = boxes + if (!box || !own || !next || !bar || !commands || !view) throw new Error('a box is missing') expect(box.x).toBeGreaterThanOrEqual(0) expect(box.x + box.width).toBeLessThanOrEqual(view.width) expect(box.y + box.height).toBeLessThanOrEqual(view.height) expect(box.y).toBeGreaterThanOrEqual(bar.y + bar.height) expect(box.y).toBeGreaterThanOrEqual(own.y + own.height) - const apart = box.y >= next.y + next.height || box.x >= next.x + next.width - expect(apart).toBe(true) + expect(box.y - (own.y + own.height)).toBeLessThanOrEqual(8) + expect(box.width).toBeLessThanOrEqual(288) + expect(intersects(box, next)).toBe(false) + expect(intersects(box, commands)).toBe(false) } interface Names { @@ -288,6 +310,8 @@ test.describe('editor annotations (A3)', () => { // The resize's own reflow (a breakpoint) settles before the press is measured. await page.waitForTimeout(300) await redo.evaluate((el) => el.scrollIntoView({ block: 'center' })) + // Its scroll event lands before the press, which a scroll would close. + await page.waitForTimeout(150) const widthBefore = await scrollWidth(page) await expectNoShift(page, async () => { await pressAtBox(page, redo) @@ -298,6 +322,17 @@ test.describe('editor annotations (A3)', () => { await expectAccessible(page, `the bubble open at ${width}`) await expectNoShift(page, () => page.keyboard.press('Escape')) await expect(bubble).toHaveCount(0) + // Scrolled, the strip stuck under the bar: the keyboard press keeps the same rules. + await page.evaluate(() => window.scrollTo(0, 150)) + await redo.focus() + await page.waitForTimeout(150) + await page.keyboard.press('Enter') + await expect(bubble).toHaveText(reason) + await expectBubbleBeside(page, bubble, redo, undo) + await page.keyboard.press('Escape') + await expect(bubble).toHaveCount(0) + await page.evaluate(() => window.scrollTo(0, 0)) + await page.waitForTimeout(150) } await expect(page.getByTestId('gesture-message')).toHaveCount(0) // Escape order: the bubble first, alone; the next Escape asks about the creation. diff --git a/apps/app/src/components/editor/HeldReasonBubble.tsx b/apps/app/src/components/editor/HeldReasonBubble.tsx index f890d857..d32723fe 100644 --- a/apps/app/src/components/editor/HeldReasonBubble.tsx +++ b/apps/app/src/components/editor/HeldReasonBubble.tsx @@ -11,38 +11,50 @@ interface Place { maxWidth: number } +/** Left edge of the annotations strip's command box while the strip is on screen. */ +function stripCommandsLeft(): number | null { + for (const strip of document.querySelectorAll('[data-testid="edit-strip"]')) { + const commands = strip.lastElementChild?.getBoundingClientRect() + if (strip.getBoundingClientRect().width > 0 && commands) + return commands.left + } + return null +} + /** - * Under the button, its right edge on the button's, never past the window's edges. Read - * from the button's box on every scroll and resize so the bubble follows it. + * Under the button, its right edge on the button's, never past the window's edges. With the + * strip on screen the bubble ends left of the strip's commands, which its text may name + * (« Annuler le tracé », « Abandonner »): it lies over the strip's notice, never over them. */ function placeUnder(anchor: HTMLElement): Place { const box = anchor.getBoundingClientRect() const width = document.documentElement.clientWidth - const right = Math.max(EDGE, width - box.right) + const commands = stripCommandsLeft() + const edge = + commands === null ? box.right : Math.min(box.right, commands - GAP) + const right = Math.max(EDGE, width - edge) return { top: box.bottom + GAP, right, maxWidth: width - right - EDGE } } +/** Measured once at open and on a resize; a scroll closes the bubble instead. */ function usePlace(anchor: HTMLElement | null): Place | null { const [place, setPlace] = useState(null) useLayoutEffect(() => { if (!anchor) return const measure = () => setPlace(placeUnder(anchor)) measure() - window.addEventListener('scroll', measure, true) window.addEventListener('resize', measure) - return () => { - window.removeEventListener('scroll', measure, true) - window.removeEventListener('resize', measure) - } + return () => window.removeEventListener('resize', measure) }, [anchor]) return place } /** - * The reason a held « Défaire » / « Refaire » gives, in a small bubble under its button. Out - * of the flow (a portal to the body, `fixed`), so nothing of the page moves; z-20 puts it - * over the sticky strip and the pinned drawing column and under the top bar (z-30, and the bubble never goes up past it) and the - * modal dialogs (top layer). It never takes a press: one beside it works as usual. + * The reason a held « Défaire » / « Refaire » gives, in a small bubble under its button. + * Out of the flow (a portal to the body, `fixed`), so nothing of the page moves. z-20 puts + * it over the sticky strip and the pinned drawing column, under the top bar (z-30), which + * it never rises past, and under the modal dialogs (top layer). It takes no press. Hidden + * from assistive technology: the button's always-mounted status region announces the text. */ export default function HeldReasonBubble({ anchor, @@ -55,14 +67,14 @@ export default function HeldReasonBubble({ if (!place) return null return createPortal(