fix(editor): focus follows the action after a touch commit
This commit is contained in:
1 parent
df46390bdb
commit
03e85a7c67
5 files changed
+135
-4
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { useEffect, useRef } from 'react'
|
||||
import Button from '@/components/ui/Button'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
@@ -18,8 +19,18 @@ export default function TracingCommands({
|
||||
onCancel,
|
||||
}: TracingCommandsProps) {
|
||||
const t = useT()
|
||||
const box = useRef<HTMLDivElement>(null)
|
||||
// The command that started the tracing (« Redessiner ») left with the strip's swap: the
|
||||
// focus goes here, as long as nothing else holds it (never over a dialog or a field).
|
||||
useEffect(() => {
|
||||
const active = document.activeElement
|
||||
if (active === null || active === document.body) {
|
||||
box.current?.focus({ preventScroll: true })
|
||||
}
|
||||
}, [])
|
||||
return (
|
||||
<div
|
||||
ref={box}
|
||||
tabIndex={-1}
|
||||
data-testid="tracing-commands"
|
||||
className="flex items-center gap-2 outline-none"
|
||||
|
||||
@@ -6,15 +6,18 @@ import {
|
||||
} from './workStore'
|
||||
|
||||
/**
|
||||
* A keyboard series or a size change still pending (no blur yet: a mouse Back button)
|
||||
* commits now, through its own blur (BDR-050): leaving never drops it.
|
||||
* A keyboard series or a size change still pending (no blur yet: a mouse Back button, a tap
|
||||
* that focuses nothing) commits now through the commit its element runs on blur (BDR-050).
|
||||
* The blur handler is run by a `focusout` sent to the element, never by `blur()`: the focus
|
||||
* stays where it is instead of falling to `body`.
|
||||
*/
|
||||
export function commitFocusedSeries(): void {
|
||||
const { preview } = useAnnotationWork.getState()
|
||||
if (!preview || preview.source === 'move') return
|
||||
const active = document.activeElement
|
||||
const series = active instanceof HTMLElement || active instanceof SVGElement
|
||||
if (series && active.closest('[data-series]')) active.blur()
|
||||
if (!series || !active.closest('[data-series]')) return
|
||||
active.dispatchEvent(new FocusEvent('focusout', { bubbles: true }))
|
||||
}
|
||||
|
||||
/** An unstarted mode ends; a pending series that is not the mode's stays. */
|
||||
|
||||
@@ -200,10 +200,26 @@ const BLOCK_DRAGS = new Set([
|
||||
'drag_vertex',
|
||||
])
|
||||
|
||||
/** The vertex or point handle a press holds takes the focus a tap does not give it (Safari). */
|
||||
function focusPressed(f: Feed, target: PressTarget): void {
|
||||
if (target.kind !== 'handle') return
|
||||
const { handle } = target
|
||||
if (handle.kind !== 'vertex' && handle.kind !== 'point') return
|
||||
const selector =
|
||||
handle.kind === 'vertex'
|
||||
? `[data-vertex="${handle.index}"]`
|
||||
: '[data-testid="point-handle"]'
|
||||
const element = f.input.viewport.current?.querySelector(selector)
|
||||
if (element instanceof SVGElement) element.focus({ preventScroll: true })
|
||||
}
|
||||
|
||||
function applyEffect(f: Feed, effect: PressEffect, event: PointerEvent | null) {
|
||||
if (effect?.type === 'drag_cancel') f.drag.cancel()
|
||||
if (!event || !effect) return
|
||||
if (effect.type === 'click') onClick(f, effect.target, event)
|
||||
if (effect.type === 'click') {
|
||||
onClick(f, effect.target, event)
|
||||
focusPressed(f, effect.target)
|
||||
}
|
||||
if (effect.type === 'drag_end') f.drag.drop(clientOf(event))
|
||||
if (effect.type === 'drag_start' && BLOCK_DRAGS.has(effect.action.type)) {
|
||||
const { action } = effect
|
||||
@@ -211,6 +227,7 @@ function applyEffect(f: Feed, effect: PressEffect, event: PointerEvent | null) {
|
||||
const mode = action.type === 'drag_mode'
|
||||
const finger = pointerKindOf(event.pointerType) === 'touch'
|
||||
const { pressedAt } = f.machine.current
|
||||
focusPressed(f, f.machine.current.target)
|
||||
f.drag.start(pressedAt, clientOf(event), mode, vertex, finger)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -748,3 +748,48 @@ describe('Safari: « Déplacer le point » clicked with the focus left on the sl
|
||||
expect(useAnnotationWork.getState().mode?.kind).toBe('move')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Safari: the focus after a press that commits a pending change', () => {
|
||||
const tapDrag = (el: Element) => {
|
||||
const init = {
|
||||
pointerId: 1,
|
||||
isPrimary: true,
|
||||
button: 0,
|
||||
buttons: 1,
|
||||
pointerType: 'mouse',
|
||||
}
|
||||
fireEvent.pointerDown(el, { ...init, clientX: 50, clientY: 50 })
|
||||
fireEvent.pointerMove(el, { ...init, clientX: 80, clientY: 80 })
|
||||
}
|
||||
|
||||
it('a press on the point handle commits the slider change and focuses the handle', async () => {
|
||||
await mount(withPoints(point('a', 50, 50, 5), point('b', 20, 20, 8)))
|
||||
await select()
|
||||
act(() => slider().focus())
|
||||
fireEvent.keyDown(slider(), { key: 'PageUp' })
|
||||
const before = depth()
|
||||
tapDrag(handle().querySelector('[data-handle]')!)
|
||||
expect(depth()).toBe(before + 1)
|
||||
expect(document.activeElement).toBe(handle())
|
||||
})
|
||||
|
||||
it('a press on the handle commits its arrow series and keeps the handle focused', async () => {
|
||||
await mount(withPoints(point('a', 50, 50, 5)))
|
||||
await select()
|
||||
act(() => handle().focus())
|
||||
fireEvent.keyDown(handle(), { key: 'ArrowRight' })
|
||||
const before = depth()
|
||||
tapDrag(handle().querySelector('[data-handle]')!)
|
||||
expect(depth()).toBe(before + 1)
|
||||
expect(document.activeElement).toBe(handle())
|
||||
})
|
||||
|
||||
it('« Redessiner » and « Désélectionner » never leave the focus on body', async () => {
|
||||
await mount(withPoints(point('a', 50, 50, 5)))
|
||||
await select()
|
||||
act(() => slider().focus())
|
||||
fireEvent.keyDown(slider(), { key: 'PageUp' })
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Désélectionner' }))
|
||||
expect(document.activeElement).not.toBe(document.body)
|
||||
})
|
||||
})
|
||||
@@ -1292,3 +1292,58 @@ describe('Safari: a strip command clicked with the focus left on a keyed vertex'
|
||||
expect(depth()).toBe(before + 1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Safari: the focus after a press that commits a pending series', () => {
|
||||
// A tap that moves no focus (jsdom moves none either): the commit must not send the focus
|
||||
// to body, and the press lands it where the same action sends it from the keyboard.
|
||||
async function keyedVertex() {
|
||||
await mount(squareText())
|
||||
await select()
|
||||
clickHandle('Sommet 1 de N', [20, 20])
|
||||
act(() => vertex(1).focus())
|
||||
fireEvent.keyDown(vertex(1), { key: 'ArrowRight' })
|
||||
expect(useAnnotationWork.getState().preview?.source).toBe('keys')
|
||||
}
|
||||
|
||||
it('a press on another vertex commits the series and focuses that vertex', async () => {
|
||||
await keyedVertex()
|
||||
const before = depth()
|
||||
clickHandle('Sommet 2 de N', [80, 20])
|
||||
expect(depth()).toBe(before + 1)
|
||||
expect(pointsOf('z')![0]).toEqual([20.5, 20])
|
||||
expect(document.activeElement).toBe(vertex(2))
|
||||
})
|
||||
|
||||
it('a press on the interior keeps the focus on the handle that held the series', async () => {
|
||||
await keyedVertex()
|
||||
const before = depth()
|
||||
pressAt(marker('z'), 50, 50)
|
||||
expect(depth()).toBe(before + 1)
|
||||
expect(document.activeElement).toBe(vertex(1))
|
||||
})
|
||||
|
||||
it('a press on « + » commits the series and keeps a vertex focused', async () => {
|
||||
await keyedVertex()
|
||||
clickHandle('Ajouter un sommet après le sommet 1 de N', [50, 20])
|
||||
expect(document.activeElement).not.toBe(document.body)
|
||||
expect(document.activeElement?.getAttribute('data-vertex')).not.toBeNull()
|
||||
})
|
||||
|
||||
it('« Redessiner » and « Déplacer le sommet » never leave it on body', async () => {
|
||||
await keyedVertex()
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Déplacer le sommet' }))
|
||||
expect(document.activeElement).not.toBe(document.body)
|
||||
cleanup()
|
||||
resetStore()
|
||||
resetWork()
|
||||
await keyedVertex()
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Redessiner' }))
|
||||
expect(document.activeElement).not.toBe(document.body)
|
||||
})
|
||||
|
||||
it('« Désélectionner » focuses the row, as its key does', async () => {
|
||||
await keyedVertex()
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Désélectionner' }))
|
||||
expect(document.activeElement).not.toBe(document.body)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user