diff --git a/apps/app/e2e/shell/editor-annotations.spec.ts b/apps/app/e2e/shell/editor-annotations.spec.ts index 0c78faed..717f9e8b 100644 --- a/apps/app/e2e/shell/editor-annotations.spec.ts +++ b/apps/app/e2e/shell/editor-annotations.spec.ts @@ -1,5 +1,5 @@ import { fr } from '@/i18n/fr' -import type { Page, TestInfo } from '@playwright/test' +import type { Locator, Page, TestInfo } from '@playwright/test' import { expect, expectAccessible, test } from '../support/browser' import { dropLateReplays, routeToSandbox } from '../support/sandbox' import { @@ -51,6 +51,32 @@ const tool = (page: Page, id: string) => exact: true, }) +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. */ +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 view = page.viewportSize() + if (!box || !own || !next || !bar || !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) +} + interface Names { fr: string en: string @@ -222,7 +248,7 @@ test.describe('editor annotations (A3)', () => { ).toBe('zone-a') }) - test('a held « Refaire » says why: description on focus, message on press, nothing moves', async ({ + test('a held « Refaire » says why: description on focus, a bubble on press at three sizes, nothing moves', async ({ page, requests, }) => { @@ -251,11 +277,38 @@ test.describe('editor annotations (A3)', () => { dict['editor.history.hold_creation'], ) await expectAccessible(page, 'a held « Refaire » during a creation') - await expectNoShift(page, async () => { - await pressAtBox(page, redo) - await expect(page.getByTestId('gesture-line')).toContainText(reason) - }) - await expect(page.getByTestId('gesture-message')).toHaveCount(1) + const bubble = page.getByTestId('history-hold-bubble') + const undo = historyButton(page, dict, 'undo') + for (const [width, height] of [ + [1024, 768], + [1180, 820], + [1280, 800], + ]) { + await page.setViewportSize({ width, height }) + // 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' })) + const widthBefore = await scrollWidth(page) + await expectNoShift(page, async () => { + await pressAtBox(page, redo) + await expect(bubble).toHaveText(reason) + }) + await expectBubbleBeside(page, bubble, redo, undo) + expect(await scrollWidth(page)).toBe(widthBefore) + await expectAccessible(page, `the bubble open at ${width}`) + await expectNoShift(page, () => page.keyboard.press('Escape')) + await expect(bubble).toHaveCount(0) + } + await expect(page.getByTestId('gesture-message')).toHaveCount(0) + // Escape order: the bubble first, alone; the next Escape asks about the creation. + await redo.focus() + await page.keyboard.press('Enter') + await expect(bubble).toHaveText(reason) + await page.keyboard.press('Escape') + await expect(bubble).toHaveCount(0) + await expect(page.getByRole('dialog')).toHaveCount(0) + await page.keyboard.press('Escape') + await expect(page.getByRole('dialog')).toHaveCount(1) }) test('a mouse click on « Refaire » during an arrow series says why; « Défaire » cancels and keeps the redo', async ({ @@ -293,7 +346,7 @@ test.describe('editor annotations (A3)', () => { // bar, then press it at its box (the mouse events are real). await redo.evaluate((el) => el.scrollIntoView({ block: 'center' })) await pressAtBox(page, redo) - await expect(page.getByTestId('gesture-line')).toContainText( + await expect(page.getByTestId('history-hold-bubble')).toHaveText( dict['editor.history.hold_pending'], ) await expect(handle).toBeFocused() diff --git a/apps/app/src/components/editor/EditorTabs.tsx b/apps/app/src/components/editor/EditorTabs.tsx index 21514c99..4c0a743d 100644 --- a/apps/app/src/components/editor/EditorTabs.tsx +++ b/apps/app/src/components/editor/EditorTabs.tsx @@ -254,7 +254,7 @@ export default function EditorTabs({ {more.length > 0 && ( )} - + ) } diff --git a/apps/app/src/components/editor/HeldReasonBubble.tsx b/apps/app/src/components/editor/HeldReasonBubble.tsx new file mode 100644 index 00000000..f890d857 --- /dev/null +++ b/apps/app/src/components/editor/HeldReasonBubble.tsx @@ -0,0 +1,73 @@ +import { useLayoutEffect, useState, type CSSProperties } from 'react' +import { createPortal } from 'react-dom' + +/** Gap between the button and its bubble, and the space kept from the window's edge. */ +const GAP = 4 +const EDGE = 8 + +interface Place { + top: number + right: number + maxWidth: number +} + +/** + * 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. + */ +function placeUnder(anchor: HTMLElement): Place { + const box = anchor.getBoundingClientRect() + const width = document.documentElement.clientWidth + const right = Math.max(EDGE, width - box.right) + return { top: box.bottom + GAP, right, maxWidth: width - right - EDGE } +} + +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) + } + }, [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. + */ +export default function HeldReasonBubble({ + anchor, + text, +}: { + anchor: HTMLElement | null + text: string +}) { + const place = usePlace(anchor) + if (!place) return null + return createPortal( +
+ {text} +
, + document.body, + ) +} diff --git a/apps/app/src/components/editor/HistoryButtons.tsx b/apps/app/src/components/editor/HistoryButtons.tsx index 94b44d19..50fa7516 100644 --- a/apps/app/src/components/editor/HistoryButtons.tsx +++ b/apps/app/src/components/editor/HistoryButtons.tsx @@ -12,12 +12,13 @@ import { cancelPending, hasPending, holdsRedo, - pressHeldHistory, useAnnotationWork, type HistoryHold, } from '@/editor/annotations/workStore' import { useT } from '@/i18n/useT' import EditorGlyph from './EditorGlyph' +import HeldReasonBubble from './HeldReasonBubble' +import { useHeldBubble } from './useHeldBubble' type Direction = 'undo' | 'redo' @@ -95,6 +96,7 @@ function usePress( direction: Direction, readOnly: boolean, left: RefObject, + bubble: { show: () => void; close: () => void }, ) { const forget = () => { left.current = null @@ -103,6 +105,7 @@ function usePress( const onBlur = (event: FocusEvent) => { const pair = event.currentTarget.parentElement if (!pair?.contains(event.relatedTarget as Node | null)) forget() + bubble.close() } const onPointerLeave = (event: PointerEvent) => { if (event.pointerType === 'mouse') forget() @@ -121,7 +124,7 @@ function usePress( const onClick = () => { // A12, I-1: a held button does nothing but say why; an inactive one does nothing. const hold = holdNow(direction) - if (hold) return pressHeldHistory(hold) + if (hold) return bubble.show() if (inactive(direction, useAnnotationWork.getState())) return const field = left.current forget() @@ -150,9 +153,11 @@ function usePress( function HistoryButton({ direction, + tab, left, }: { direction: Direction + tab: string left: RefObject }) { const t = useT() @@ -160,11 +165,14 @@ function HistoryButton({ const steppable = useEditorStore(direction === 'undo' ? canUndo : canRedo) const hold = useAnnotationWork((s) => holdOf(direction, s, steppable)) const name = t(`editor.history.${direction}_title`) - const press = usePress(direction, readOnly, left) + const button = useRef(null) + const bubble = useHeldBubble(hold !== null, tab, button) + const press = usePress(direction, readOnly, left, bubble) const reasonId = `history-hold-${direction}` return ( <>