feat(app): Désélectionner and the Escape priority (R1, D6, A5, A6)

« Désélectionner » (secondary sm, deselect glyph) sits left of « Supprimer » in a
bordered row of the properties panel, read-only too, hidden while a tracing runs.
It commits the typed field, clears the selection, focuses the row (else the drawing)
and announces. Escape deselects through one document listener per visible body
(not in a text field, only inside the workspace); G1: row focus only from the panel.
« Plus » and the references panel close first; a tracing cancel marks its Escape.

No existing assertion rewritten.
This commit is contained in:
bchanot committed 2026-11-12 12:45:45 +01:00
1 parent 7dda61d2ae
commit bbb1668406
14 files changed
+633 -42

No files matched your search

@@ -0,0 +1,98 @@
import { useContext, useEffect, useRef } from 'react'
import { flushSync } from 'react-dom'
import {
AnnounceContext,
commitFocusedField,
focusRow,
isTextField,
visibleElement,
} from '@/editor/focusRequest'
import { CANVAS_BOX } from '@/editor/useEditorNavigation'
import { useT } from '@/i18n/useT'
/** The drawing's focusable box of the shown body, when there is one. */
export function focusDrawing(): boolean {
const box = visibleElement(CANVAS_BOX)
box?.focus({ preventScroll: true })
return box !== null
}
/** The row of the annotation that was open; the drawing when a filter hides the row. */
export function focusRowOrDrawing(id: string): boolean {
return focusRow({ kind: 'annotation', id }) || focusDrawing()
}
/**
* Deselecting: `id` is the RESOLVED selection (never the raw id, which can name a removed
* annotation). The button always moves the focus to the row; Escape does only when it was
* pressed inside the properties panel, whose focused element is about to leave the page.
* Anywhere else the focus stays where it is. Both answer whether something was deselected.
*/
export function useDeselect(id: string | null, clear: () => void) {
const t = useT()
const tRef = useRef(t)
tRef.current = t
const announce = useContext(AnnounceContext)
const run = (focus: boolean) => {
if (id === null) return false
commitFocusedField()
flushSync(clear)
if (focus) focusRowOrDrawing(id)
announce(tRef.current('editor.annotations.deselected'))
return true
}
return {
byButton: () => run(true),
byKey: (fromPanel: boolean) => run(fromPanel),
}
}
/** Whether a key event happened in the editor workspace (or with nothing focused). */
function inWorkspace(target: EventTarget | null): boolean {
if (target === document.body) return true
return (
target instanceof Element &&
target.closest('[data-editor-workspace]') !== null
)
}
/**
* While `active`, Escape on the document runs `onEscape`, which answers whether it acted.
* A key already handled (dialogs, the references panel, « Plus »), one typed in a field or
* outside the workspace is left alone.
*/
export function useEscapeDeselect(
active: boolean,
onEscape: (event: KeyboardEvent) => boolean,
): void {
const handler = useRef(onEscape)
handler.current = onEscape
useEffect(() => {
if (!active) return
const onKey = (event: KeyboardEvent) => {
if (event.key !== 'Escape' || event.defaultPrevented) return
if (event.isComposing || isTextField(event.target)) return
if (!inWorkspace(event.target)) return
if (handler.current(event)) event.preventDefault()
}
document.addEventListener('keydown', onKey)
return () => document.removeEventListener('keydown', onKey)
}, [active])
}
/**
* A body's Escape: deselects `id` while `active`. Returns the ref of the panel column, which
* tells a key pressed inside the panel (focus follows the row) from one pressed elsewhere.
*/
export function useSelectionEscape(
id: string | null,
clear: () => void,
active: boolean,
) {
const asideRef = useRef<HTMLElement>(null)
const deselect = useDeselect(id, clear)
useEscapeDeselect(active, (event) =>
deselect.byKey(asideRef.current?.contains(event.target as Node) ?? false),
)
return asideRef
}
@@ -79,7 +79,10 @@ function tracingActions(input: PlacementInput) {
const onTraceKey = (event: KeyboardEvent) => {
if (!ui.tracing) return
if (event.key === 'Enter') closeTracing()
if (event.key === 'Escape') dispatch({ type: 'trace_cancel' })
if (event.key === 'Escape') {
event.preventDefault()
dispatch({ type: 'trace_cancel' })
}
}
return { closeTracing, trace, onTraceKey }
}
+13 -5
View File
@@ -102,6 +102,17 @@ export function useRowFocus(focus: FocusRequest | undefined): void {
)
}
/** A field that takes typed text (or a choice): Escape there belongs to the field. */
export function isTextField(target: EventTarget | null): boolean {
return (
target instanceof HTMLInputElement ||
target instanceof HTMLTextAreaElement ||
target instanceof HTMLSelectElement ||
(target instanceof HTMLElement &&
target.closest('[contenteditable]') !== null)
)
}
/**
* Commits the text being typed before a place is left: blurs the focused field, so its
* `TextField` commits on blur. Safari and iPadOS do not move the focus to a tapped button,
@@ -109,11 +120,8 @@ export function useRowFocus(focus: FocusRequest | undefined): void {
*/
export function commitFocusedField(): boolean {
const active = document.activeElement
const isField =
active instanceof HTMLInputElement ||
active instanceof HTMLTextAreaElement ||
active instanceof HTMLSelectElement
if (isField) active.blur()
const isField = isTextField(active)
if (isField) (active as HTMLElement).blur()
return isField
}
+1 -1
View File
@@ -67,7 +67,7 @@ const isShown = (element: HTMLElement | null): boolean =>
const headingOf = (place: PlaceName) =>
visibleElement(`[data-place="${place}"] [data-place-heading]`)
const CANVAS_BOX =
export const CANVAS_BOX =
'[data-place="annotations.drawing"] [role="group"][tabindex="0"]'
/** The tab's button, or « Plus » while the tab sits in the closed list. */