refactor(editor): split B1 navigation hooks, category count on the overview card

This commit is contained in:
bchanot committed 2026-11-06 20:31:26 +01:00
1 parent bc2dba7c14
commit ca184da4d9
6 files changed
+166 -82

No files matched your search

+34 -11
View File
@@ -5,6 +5,7 @@ import type {
} from '@faunosteo/core-wasm/types'
import {
AnnounceContext,
type FocusOutcome,
useFocusRequest,
visibleElement,
type FocusRequest,
@@ -39,6 +40,33 @@ const BUSY_TARGET = {
const focusFirst = (selector: string) => visibleElement(selector)?.focus()
/** A creation or tracing in progress keeps the request: focus its panel, say why. */
function keepBusy(
busy: NonNullable<DrawingState['busy']>,
announce: (text: string) => void,
say: string,
): FocusOutcome {
announce(say)
focusFirst(BUSY_TARGET[busy])
return 'kept'
}
/** Shows the annotation: at once when it is already shown, else after the next render. */
function showAnnotation(
found: { viewId: string; annotationId: string },
drawing: DrawingState,
awaiting: { current: string | null },
): void {
const same =
drawing.selectedId === found.annotationId && drawing.viewId === found.viewId
if (same) {
focusFirst('[data-properties-heading]')
return
}
awaiting.current = found.annotationId
drawing.show(found.viewId, found.annotationId)
}
/**
* « Aller à » an annotation on the drawing: shows its view, selects it, then focuses the
* properties heading once it has rendered. While a creation or a tracing is in progress
@@ -57,18 +85,13 @@ export function useAnnotationRequest(
const found = target.key ? annotationFocus(doc, target.key) : null
if (!found) return 'none'
if (drawing.busy) {
announce(t(`editor.live.${drawing.busy}_kept`))
focusFirst(BUSY_TARGET[drawing.busy])
return 'kept'
}
const same =
drawing.selectedId === found.annotationId &&
drawing.viewId === found.viewId
if (same) focusFirst('[data-properties-heading]')
else {
awaiting.current = found.annotationId
drawing.show(found.viewId, found.annotationId)
return keepBusy(
drawing.busy,
announce,
t(`editor.live.${drawing.busy}_kept`),
)
}
showAnnotation(found, drawing, awaiting)
return 'found'
})
useEffect(() => {
+62 -38
View File
@@ -107,17 +107,14 @@ interface Openers {
section: RefObject<HTMLElement | null>
}
/** The commands of the workspace: each commits the typed text, then moves. */
function useMoves(
dispatch: Dispatch<NavigationAction>,
announce: (text: string) => void,
openers: Openers,
) {
const t = useT()
const moveText = useMoveText()
type Go = (action: NavigationAction, kind: MoveKind, text: string) => void
/** The one way to move: commit the typed text, announce, dispatch, record the move. */
function useGo(dispatch: Dispatch<NavigationAction>) {
const { live, announce } = useLiveRegion()
const [move, setMove] = useState<Move | null>(null)
const go = useCallback(
(action: NavigationAction, kind: MoveKind, text: string) => {
const go: Go = useCallback(
(action, kind, text) => {
commitFocusedField()
announce(text)
dispatch(action)
@@ -125,6 +122,13 @@ function useMoves(
},
[announce, dispatch],
)
return { move, go, live, announce }
}
/** Tab and section moves: « Aller à », a section entry, « Retour au dessin ». */
function useSectionMoves(go: Go, openers: Openers) {
const t = useT()
const moveText = useMoveText()
const navigate = useCallback(
(target: EditorTarget, opener?: Opener) => {
if (target.place === 'references') {
@@ -137,7 +141,8 @@ function useMoves(
const openSection = useCallback(
(section: AnnotationSection, opener?: Opener) => {
openers.section.current = opener ?? null
const text = `${t('editor.tab.annotations')} · ${t(`editor.place.${section}`)}`
const place = t(`editor.place.${section}`)
const text = `${t('editor.tab.annotations')} · ${place}`
go({ type: 'open_section', section }, 'section', text)
},
[go, openers, t],
@@ -146,6 +151,12 @@ function useMoves(
const text = `${t('editor.tab.annotations')} · ${t('editor.place.drawing')}`
go({ type: 'back_to_drawing' }, 'back', text)
}, [go, t])
return { navigate, openSection, backToDrawing }
}
/** The references panel moves: open (optionally filtered) and close. */
function useReferenceMoves(go: Go, openers: Openers) {
const t = useT()
const openReferences = useCallback(
(citedBy: ElementKey | null, opener?: Opener) => {
openers.references.current = opener ?? null
@@ -158,14 +169,7 @@ function useMoves(
const text = t('editor.live.references_closed')
go({ type: 'close_references' }, 'references_closed', text)
}, [go, t])
return {
move,
navigate,
openSection,
backToDrawing,
openReferences,
closeReferences,
}
return { openReferences, closeReferences }
}
/** « Retour au dessin »: the entry that opened the section, else the canvas box. */
@@ -176,6 +180,12 @@ function backTarget(opener: HTMLElement | null): HTMLElement | null {
return again || visibleElement(CANVAS_BOX)
}
/** Panel closed: the opener when it is shown, else the header button. */
function referencesBack(opener: HTMLElement | null): HTMLElement | null {
if (opener && isShown(opener)) return opener
return document.getElementById(REFERENCES_BUTTON_ID)
}
/** Where the focus lands after a programmatic move; never `body`. */
function landingOf(move: Move, state: NavigationState): HTMLElement | null {
if (move.kind === 'references') return headingOf('references')
@@ -199,12 +209,7 @@ function useMoveFocus(
if (!move || handled.current === move) return
handled.current = move
if (move.kind === 'references_closed') {
const opener = openers.references.current
const target =
opener && isShown(opener)
? opener
: document.getElementById(REFERENCES_BUTTON_ID)
target?.focus()
referencesBack(openers.references.current)?.focus()
return
}
const serial = state.focus?.serial
@@ -219,6 +224,26 @@ function useMoveFocus(
}, [move, state, outcomes, dispatch, openers])
}
/** Tab selection, and the outcomes the places report for each focus request. */
function useReceipts(dispatch: Dispatch<NavigationAction>) {
const outcomes = useRef(new Map<number, FocusOutcome>())
const consumed = useCallback(
(serial: number, outcome: FocusOutcome) => {
outcomes.current.set(serial, outcome)
dispatch({ type: 'focus_consumed', serial })
},
[dispatch],
)
const selectTab = useCallback(
(tab: EditorTabId) => {
commitFocusedField()
dispatch({ type: 'select_tab', tab })
},
[dispatch],
)
return { outcomes, consumed, selectTab }
}
/** Navigation of the draft editor: the reducer plus the DOM side (focus, live region). */
export function useEditorNavigation(initialTab: EditorTabId): EditorNavigation {
const [state, dispatch] = useReducer(
@@ -226,21 +251,20 @@ export function useEditorNavigation(initialTab: EditorTabId): EditorNavigation {
initialTab,
initialNavigation,
)
const { live, announce } = useLiveRegion()
const outcomes = useRef(new Map<number, FocusOutcome>())
const { move, go, live, announce } = useGo(dispatch)
const openers = useRef<Openers>({
references: { current: null },
section: { current: null },
}).current
const moves = useMoves(dispatch, announce, openers)
const consumed = useCallback((serial: number, outcome: FocusOutcome) => {
outcomes.current.set(serial, outcome)
dispatch({ type: 'focus_consumed', serial })
}, [])
const selectTab = useCallback((tab: EditorTabId) => {
commitFocusedField()
dispatch({ type: 'select_tab', tab })
}, [])
useMoveFocus(moves.move, state, outcomes, dispatch, openers)
return { state, live, selectTab, consumed, announce, ...moves }
const receipts = useReceipts(dispatch)
useMoveFocus(move, state, receipts.outcomes, dispatch, openers)
return {
state,
live,
announce,
consumed: receipts.consumed,
selectTab: receipts.selectTab,
...useSectionMoves(go, openers),
...useReferenceMoves(go, openers),
}
}