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

@@ -9,6 +9,7 @@ export type GlyphName =
| 'undo'
| 'redo'
| 'search'
| 'deselect'
/** The editor's one glyph table (24-unit grid strokes); `book` and `close` are in `Icon`. */
const GLYPHS: Record<GlyphName, string> = {
@@ -22,6 +23,8 @@ const GLYPHS: Record<GlyphName, string> = {
undo: 'M9 7L4 12l5 5M4 12h11a5 5 0 0 1 0 10',
redo: 'M15 7l5 5-5 5M20 12H9a5 5 0 0 0 0 10',
search: 'M11 4a7 7 0 1 0 0 14 7 7 0 0 0 0-14zM20 20l-4-4',
deselect:
'M4 8V5a1 1 0 0 1 1-1h3M16 4h3a1 1 0 0 1 1 1v3M20 16v3a1 1 0 0 1-1 1h-3M8 20H5a1 1 0 0 1-1-1v-3M9 9l6 6M15 9l-6 6',
}
/** A decorative glyph of the editor, drawn in the current text colour. */
+24 -1
View File
@@ -1,4 +1,4 @@
import { useState, type KeyboardEvent } from 'react'
import { useEffect, useRef, useState, type KeyboardEvent } from 'react'
import { useEditorStore } from '@/editor/state/editorStore'
import { usePlural } from '@/editor/plural'
import {
@@ -90,6 +90,28 @@ interface MoreTabsProps {
onSelect: (tab: EditorTabId) => void
}
/**
* While « Plus » is open, Escape closes it and returns the focus to it. Captured on the
* document so the key is marked handled before any body's deselection sees it; a modal
* dialog keeps its own Escape.
*/
function useMoreEscape(open: boolean, close: () => void) {
const closeRef = useRef(close)
closeRef.current = close
useEffect(() => {
if (!open) return
const onKey = (event: globalThis.KeyboardEvent) => {
if (event.key !== 'Escape' || event.isComposing) return
if (document.querySelector('dialog[open]')) return
event.preventDefault()
closeRef.current()
document.getElementById(MORE_BUTTON_ID)?.focus()
}
document.addEventListener('keydown', onKey, true)
return () => document.removeEventListener('keydown', onKey, true)
}, [open])
}
/** Below 640 px: « Plus » and its disclosure of the tabs that do not fit. */
function MoreTabs({ more, active, onSelect }: MoreTabsProps) {
const t = useT()
@@ -98,6 +120,7 @@ function MoreTabs({ more, active, onSelect }: MoreTabsProps) {
onSelect(tab)
setOpen(false)
}
useMoreEscape(open, () => setOpen(false))
return (
<>
<button
@@ -148,7 +148,16 @@ export default function EditorWorkspace(props: Props) {
: nav.openReferences(null, opener)
return (
<EditingState readOnly={readOnly} referencesOpen={state.references.open}>
<div className="space-y-4">
<div
className="space-y-4"
data-editor-workspace
onKeyDown={(event) => {
if (event.key !== 'Escape' || event.defaultPrevented) return
if (event.nativeEvent.isComposing || !state.references.open) return
event.preventDefault()
nav.closeReferences()
}}
>
<DraftHeader
sheet={sheet}
mode={mode}
@@ -8,6 +8,7 @@ import { useAnnotationRequest } from '@/editor/annotations/focusTarget'
import { keyboardPending, type NewKind } from '@/editor/annotations/creation'
import { parseFrame, type Frame } from '@/editor/annotations/frame'
import { isEditable } from '@/editor/annotations/geometryEdit'
import { useSelectionEscape } from '@/editor/annotations/deselect'
import { usePlacement } from '@/editor/annotations/usePlacement'
import {
initialUi,
@@ -130,10 +131,17 @@ export default function EditableAnnotations(props: Props) {
const scene = useScene(props)
const context = useCommitContext()
const placement = usePlacement({ ...scene, context })
const { ui, selected } = scene
const visible = usePlaceVisible()
const asideRef = useSelectionEscape(
selected?.id ?? null,
() => scene.dispatch({ type: 'select', id: null }),
visible && selected !== undefined && ui.tracing === null && !ui.pending,
)
return (
<div className="grid gap-4 lg:grid-cols-[minmax(0,1fr)_22rem]">
<CanvasColumn scene={scene} placement={placement} />
<aside>
<aside ref={asideRef}>
<SidePanel scene={scene} context={context} />
</aside>
</div>
@@ -293,6 +301,7 @@ function SidePanel({
context={context}
navigate={scene.navigate}
onShowAll={() => dispatch({ type: 'select', id: null })}
busy={ui.tracing !== null}
/>
)
}
@@ -13,6 +13,8 @@ import {
updateGeometry,
type CommitContext,
} from '@/editor/annotations/annotationCommit'
import { useDeselect } from '@/editor/annotations/deselect'
import EditorGlyph from '@/components/editor/EditorGlyph'
import { isEditable } from '@/editor/annotations/geometryEdit'
import type { UiAction } from '@/editor/annotations/uiState'
import { geometryErrorsById } from '@/editor/issues'
@@ -41,6 +43,8 @@ export interface PropertiesPanelProps {
navigate: EditorPlaceProps['navigate']
/** Clears the selection: the overview shows again. */
onShowAll: () => void
/** A tracing runs: the selection is not left from here. */
busy?: boolean
/** Put before every id, for a twin of the panel that lives beside another one. */
idPrefix?: string
}
@@ -84,7 +88,10 @@ export default function PropertiesPanel(props: PropertiesPanelProps) {
/>
<GeometrySection {...props} />
<StatusActions annotation={annotation} readOnly={readOnly} edit={edit} />
<Deletion id={annotation.id} name={name} readOnly={readOnly} />
<div className="flex flex-wrap gap-2 border-t border-[var(--color-border)] pt-2.5">
<DeselectButton annotationId={annotation.id} {...props} />
<Deletion id={annotation.id} name={name} readOnly={readOnly} />
</div>
</section>
)
}
@@ -107,6 +114,34 @@ function ShowAll({ id, onShowAll }: { id: string; onShowAll: () => void }) {
)
}
/** Leaves the annotation open: the overview shows again, the focus on its row. */
function DeselectButton({
annotationId,
onShowAll,
busy,
}: { annotationId: string } & Pick<
PropertiesPanelProps,
'onShowAll' | 'busy'
>) {
const t = useT()
const { byButton } = useDeselect(annotationId, onShowAll)
if (busy) return null
return (
<Button
type="button"
variant="secondary"
size="sm"
className="gap-2"
// Keeps the focus in a field being typed, so its blur commit runs in byButton.
onMouseDown={(event) => event.preventDefault()}
onClick={byButton}
>
<EditorGlyph name="deselect" size={16} />
{t('editor.annotations.deselect')}
</Button>
)
}
function GeometrySection(props: PropertiesPanelProps) {
const t = useT()
const { annotation, readOnly, geometryLocked } = props
@@ -11,9 +11,11 @@ import SelectionTag from '@/components/editor/annotations/SelectionTag'
import EditOnly from '@/components/editor/EditOnly'
import KeptPlace from '@/components/editor/KeptPlace'
import { ownedBy } from '@/editor/focusRequest'
import { useSelectionEscape } from '@/editor/annotations/deselect'
import { useAnnotationRequest } from '@/editor/annotations/focusTarget'
import { mainViewId, useViewModel } from '@/editor/annotations/useViewModel'
import type { AnnotationSection as SectionId } from '@/editor/navigation'
import { usePlaceVisible } from '@/lib/placeVisibility'
import { useSheetDocument } from '@/editor/sheetDocument'
import { useEditorStore } from '@/editor/state/editorStore'
import type { EditorPlaceProps } from '@/editor/tabs'
@@ -163,6 +165,12 @@ function ReadOnlyAnnotations({
const model = useViewModel(doc, viewId)
const view = doc.views.find((v) => v.id === viewId)
const selected = model.annotations.find((a) => a.id === selectedId)
const visible = usePlaceVisible()
const asideRef = useSelectionEscape(
selected?.id ?? null,
() => setSelectedId(null),
visible && selected !== undefined,
)
// A view of its own has its own list: the filter and the selection start over.
const showView = (id: string) => {
setViewId(id)
@@ -173,11 +181,7 @@ function ReadOnlyAnnotations({
return (
<div className="grid gap-4 lg:grid-cols-[minmax(0,1fr)_22rem]">
<div className="min-w-0 space-y-3">
<ViewSwitcher
views={doc.views}
current={viewId}
onSelect={showView}
/>
<ViewSwitcher views={doc.views} current={viewId} onSelect={showView} />
{selected && (
<SelectionTag
kind={selected.kind}
@@ -193,32 +197,34 @@ function ReadOnlyAnnotations({
onAnnotation={setSelectedId}
/>
</div>
{selected ? (
<PropertiesPanel
key={selected.id}
annotation={selected}
doc={doc}
idPrefix={READ_ONLY_IDS}
readOnly
geometryLocked={!model.measured}
dispatch={() => undefined}
context={{ t: (key) => key, locale }}
navigate={navigate}
onShowAll={() => setSelectedId(null)}
/>
) : (
<OverviewPanel
doc={doc}
viewId={viewId}
idPrefix={READ_ONLY_IDS}
readOnly
filter={filter}
onFilter={setFilter}
onSelectView={showView}
onSelectAnnotation={setSelectedId}
onOpenSection={onOpenSection}
/>
)}
<aside ref={asideRef}>
{selected ? (
<PropertiesPanel
key={selected.id}
annotation={selected}
doc={doc}
idPrefix={READ_ONLY_IDS}
readOnly
geometryLocked={!model.measured}
dispatch={() => undefined}
context={{ t: (key) => key, locale }}
navigate={navigate}
onShowAll={() => setSelectedId(null)}
/>
) : (
<OverviewPanel
doc={doc}
viewId={viewId}
idPrefix={READ_ONLY_IDS}
readOnly
filter={filter}
onFilter={setFilter}
onSelectView={showView}
onSelectAnnotation={setSelectedId}
onOpenSection={onOpenSection}
/>
)}
</aside>
</div>
)
}
@@ -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. */
+2
View File
@@ -1225,6 +1225,8 @@ export const en: Dict = {
'editor.overview.uncategorised': 'No category',
'editor.overview.no_match': 'No annotation matches.',
'editor.annotations.show_all': 'All annotations',
'editor.annotations.deselect': 'Deselect',
'editor.annotations.deselected': 'No annotation selected.',
'editor.references.open': 'References',
'editor.references.close': 'Close the references',
'editor.references.search': 'Search a reference',
+2
View File
@@ -1256,6 +1256,8 @@ export const fr: Dict = {
'editor.overview.uncategorised': 'Sans catégorie',
'editor.overview.no_match': 'Aucune annotation ne correspond.',
'editor.annotations.show_all': 'Toutes les annotations',
'editor.annotations.deselect': 'Désélectionner',
'editor.annotations.deselected': 'Aucune annotation sélectionnée.',
'editor.references.open': 'Références',
'editor.references.close': 'Fermer les références',
'editor.references.search': 'Rechercher une référence',