feat(editor): the held reason shows in a bubble by its button

This commit is contained in:
bchanot committed 2026-11-19 20:50:56 +01:00
1 parent 031826dced
commit 49f0144729
11 files changed
+313 -79

No files matched your search

@@ -572,13 +572,13 @@ describe('why a button is held (point 6)', () => {
'Refaire est suspendu pendant la modification en cours : annulez-la avec Défaire pour pouvoir refaire.'
const STORED = { type: 'circle' as const, cx: 1, cy: 2, r: 3 }
afterEach(() => act(() => resetWork()))
const line = () => screen.getByTestId('gesture-line')
const bubble = () => screen.getByTestId('history-hold-bubble')
const reason = (button: HTMLElement) => {
const id = button.getAttribute('aria-describedby')
return id ? document.getElementById(id)?.textContent : null
}
const noMessage = () =>
expect(within(line()).queryByTestId('gesture-message')).toBeNull()
expect(screen.queryByTestId('history-hold-bubble')).toBeNull()
async function openAnnotations() {
const user = userEvent.setup()
mountDraft()
@@ -639,17 +639,17 @@ describe('why a button is held (point 6)', () => {
expect(reason(redoButton())).toBe(REDO_CREATION)
expect(disabled(redoButton())).toBe(true)
await user.click(redoButton())
expect(line()).toHaveTextContent(REDO_CREATION)
const first = within(line()).getByTestId('gesture-message')
expect(bubble()).toHaveTextContent(REDO_CREATION)
const first = bubble()
redoButton().focus()
await user.keyboard('{Enter}')
expect(line()).toHaveTextContent(REDO_CREATION)
expect(within(line()).getByTestId('gesture-message')).not.toBe(first)
expect(bubble()).toHaveTextContent(REDO_CREATION)
expect(bubble()).not.toBe(first)
await user.keyboard(' ')
expect(within(line()).getAllByTestId('gesture-message')).toHaveLength(1)
expect(screen.getAllByTestId('history-hold-bubble')).toHaveLength(1)
undoButton().focus()
await user.keyboard('{Enter}')
expect(line()).toHaveTextContent(CREATION)
expect(bubble()).toHaveTextContent(CREATION)
})
it('pending work holds redo only, with its own reason', async () => {
@@ -659,7 +659,7 @@ describe('why a button is held (point 6)', () => {
expect(reason(redoButton())).toBe(PENDING)
expect(reason(undoButton())).toBeNull()
await user.click(redoButton())
expect(line()).toHaveTextContent(PENDING)
expect(bubble()).toHaveTextContent(PENDING)
})
it('a mouse press on a held « Refaire » leaves the focused pending work alone', async () => {
@@ -674,7 +674,7 @@ describe('why a button is held (point 6)', () => {
expect(handle).toHaveFocus()
expect(useAnnotationWork.getState().preview).not.toBeNull()
expect(store.getState().redo).toHaveLength(1)
expect(line()).toHaveTextContent(PENDING)
expect(bubble()).toHaveTextContent(PENDING)
handle.remove()
})
@@ -733,12 +733,12 @@ describe('why a button is held (point 6)', () => {
create()
undoButton().focus()
await user.keyboard(' ')
expect(line()).toHaveTextContent(CREATION)
expect(bubble()).toHaveTextContent(CREATION)
act(() => resetWork())
pend()
redoButton().focus()
await user.keyboard('{Enter}')
expect(line()).toHaveTextContent(PENDING)
expect(bubble()).toHaveTextContent(PENDING)
})
it('say it in English', async () => {
@@ -759,7 +759,7 @@ describe('why a button is held (point 6)', () => {
'Unavailable during a creation: create the annotation or discard it.',
)
await user.click(redo)
expect(line()).toHaveTextContent('Redo is paused during a creation')
expect(bubble()).toHaveTextContent('Redo is paused during a creation')
act(() => resetWork())
pend()
expect(reason(redo)).toBe(
@@ -767,7 +767,7 @@ describe('why a button is held (point 6)', () => {
)
})
it('on another tab a held « Refaire » keeps its description, and a press does nothing', async () => {
it('on another tab a held « Refaire » says why in its bubble too, and nothing is committed', async () => {
const user = await openAnnotations()
stacks()
await user.click(screen.getByRole('tab', { name: 'Général' }))
@@ -775,15 +775,96 @@ describe('why a button is held (point 6)', () => {
const before = store.getState().documentText
await user.click(redoButton())
expect(reason(redoButton())).toBe(PENDING)
expect(bubble()).toHaveTextContent(PENDING)
expect(useAnnotationWork.getState().preview).not.toBeNull()
expect(store.getState().documentText).toBe(before)
expect(store.getState().redo).toHaveLength(1)
noMessage()
// Back on Annotations, the press made elsewhere is not shown in the line.
// A tab change closes it, and a later press shows it again.
await user.click(screen.getByRole('tab', { name: 'Annotations' }))
noMessage()
})
describe('the bubble', () => {
async function heldRedo() {
const user = await openAnnotations()
stacks()
pend()
await user.click(redoButton())
expect(bubble()).toHaveAttribute('role', 'status')
return user
}
it('is portalled out of the row, so nothing in the layout can move', async () => {
await heldRedo()
expect(screen.getByTestId('history-buttons')).not.toContainElement(
bubble(),
)
expect(bubble().parentElement).toBe(document.body)
expect(bubble().style.position || bubble().className).toMatch(/fixed/)
})
it('closes when the hold ends', async () => {
await heldRedo()
act(() => useAnnotationWork.setState({ preview: null }))
noMessage()
pend()
noMessage()
})
it('closes when the focus leaves the button', async () => {
const user = await openAnnotations()
stacks()
pend()
redoButton().focus()
await user.keyboard('{Enter}')
expect(bubble()).toBeInTheDocument()
await user.tab()
noMessage()
})
for (const [kind, keys] of [
['mouse', '[MouseLeft]'],
['touch', '[TouchA]'],
] as const) {
it(`closes on a ${kind} press elsewhere, not on the button itself`, async () => {
const user = await heldRedo()
await user.pointer({ keys, target: redoButton() })
expect(bubble()).toBeInTheDocument()
await user.pointer({ keys, target: document.body })
noMessage()
})
}
it('Escape closes it first and alone, the pending work stays', async () => {
const user = await openAnnotations()
stacks()
pend()
redoButton().focus()
await user.keyboard('{Enter}')
await user.keyboard('{Escape}')
noMessage()
expect(useAnnotationWork.getState().preview).not.toBeNull()
})
it('sits under the pressed button, its right edge on the button, inside the window', async () => {
const user = await openAnnotations()
stacks()
pend()
const rect = { top: 10, bottom: 50, left: 900, right: 960 } as DOMRect
vi.spyOn(redoButton(), 'getBoundingClientRect').mockReturnValue(rect)
Object.defineProperty(document.documentElement, 'clientWidth', {
configurable: true,
value: 1024,
})
await user.click(redoButton())
expect(bubble()).toHaveTextContent(PENDING)
expect(bubble().style.getPropertyValue('--bubble-top')).toBe('54px')
expect(bubble().style.right).toBe('64px')
expect(bubble().style.maxWidth).toBe('min(22rem, 952px)')
delete (document.documentElement as { clientWidth?: number }).clientWidth
})
})
it('give no reason when nothing is pending, nothing to redo or read-only', async () => {
const user = await openAnnotations()
expect(reason(redoButton())).toBeNull()