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

@@ -78,12 +78,13 @@ function useViewFallback(
}, [doc, shown, dispatch])
}
function useScene({ doc, navigate, onOpenSection, focus }: Props): Scene {
const [ui, dispatch] = useReducer(uiReducer, initialUi)
const [root, setRoot] = useState<RootAttributes | null>(null)
const viewId = ui.viewId ?? mainViewId(doc)
const model = useViewModel(doc, viewId)
const own = model.measured && root?.viewId === viewId
/** « Aller à » an annotation: the request, with what it needs of the drawing's state. */
function useSceneRequest(
{ doc, focus }: Props,
ui: UiState,
viewId: string,
dispatch: Dispatch<UiAction>,
) {
useViewFallback(doc, ui.viewId, dispatch)
useAnnotationRequest(focus, doc, {
viewId,
@@ -94,6 +95,16 @@ function useScene({ doc, navigate, onOpenSection, focus }: Props): Scene {
dispatch({ type: 'select', id })
},
})
}
function useScene(props: Props): Scene {
const { doc, navigate, onOpenSection } = props
const [ui, dispatch] = useReducer(uiReducer, initialUi)
const [root, setRoot] = useState<RootAttributes | null>(null)
const viewId = ui.viewId ?? mainViewId(doc)
const model = useViewModel(doc, viewId)
const own = model.measured && root?.viewId === viewId
useSceneRequest(props, ui, viewId, dispatch)
return {
doc,
ui,
@@ -129,8 +129,17 @@ function CategoriesCard(props: OverviewPanelProps) {
const counts = useOverviewCounts(doc)
return (
<section aria-labelledby="overview-categories" className={CARD}>
<h3 id="overview-categories" className={TITLE}>
<h3
id="overview-categories"
className={`${TITLE} flex items-center justify-between`}
>
{t('editor.overview.categories')}
<span
data-testid="overview-categories-count"
className="font-normal text-[var(--color-ink-muted)]"
>
{doc.categories.length}
</span>
</h3>
{doc.categories.length === 0 && (
<p className="text-sm text-[var(--color-ink-muted)]">
@@ -26,29 +26,29 @@ export interface AnnotationsTabProps extends EditorPlaceProps {
onBack: () => void
}
/**
* Annotations: the drawing workspace (canvas with its tools, the overview or the
* properties and creation panels), or one of its full-width sections. Read-only, the
* same view is browsable (canvas, list, properties as text) with no tool and no action.
*/
export default function AnnotationsTab(props: AnnotationsTabProps) {
/** A full-width section, once the document is loaded. */
function SectionPlace(props: AnnotationsTabProps) {
const doc = useSheetDocument()
if (!doc || props.section === 'drawing') return null
return (
<AnnotationSection
section={props.section}
doc={doc}
readOnly={props.readOnly}
focus={props.focus}
onBack={props.onBack}
/>
)
}
/** The drawing workspace: edit or read-only body of the same view. */
function DrawingPlace(props: AnnotationsTabProps) {
const t = useT()
const doc = useSheetDocument()
const preview = useEditorStore((s) => s.preview)
const { section, readOnly, focus, navigate, onOpenSection, onBack } = props
if (section !== 'drawing') {
if (!doc) return null
return (
<AnnotationSection
section={section}
doc={doc}
readOnly={readOnly}
focus={focus}
onBack={onBack}
/>
)
}
const body = { doc, navigate, onOpenSection, focus }
const { readOnly, navigate, onOpenSection, focus } = props
const body = { navigate, onOpenSection, focus }
const ready = doc && preview
return (
<section
data-place="annotations.drawing"
@@ -56,16 +56,25 @@ export default function AnnotationsTab(props: AnnotationsTabProps) {
aria-label={t('editor.tab.annotations')}
className="min-h-24 space-y-3"
>
{doc && preview && readOnly && (
<ReadOnlyAnnotations {...body} doc={doc} />
)}
{doc && preview && !readOnly && (
<EditableAnnotations {...body} doc={doc} />
)}
{ready && readOnly && <ReadOnlyAnnotations {...body} doc={doc} />}
{ready && !readOnly && <EditableAnnotations {...body} doc={doc} />}
</section>
)
}
/**
* Annotations: the drawing workspace (canvas with its tools, the overview or the
* properties and creation panels), or one of its full-width sections. Read-only, the
* same view is browsable (canvas, list, properties as text) with no tool and no action.
*/
export default function AnnotationsTab(props: AnnotationsTabProps) {
return props.section === 'drawing' ? (
<DrawingPlace {...props} />
) : (
<SectionPlace {...props} />
)
}
/** The shown view is gone (an undo elsewhere): back to the main view, selection cleared. */
function useViewFallback(
doc: AnatomicalSheetDocument,
+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),
}
}
@@ -91,6 +91,14 @@ describe('the overview entries', () => {
},
)
it('the Catégories card header shows the number of categories', async () => {
mountDraft()
await ready()
expect(screen.getByTestId('overview-categories-count')).toHaveTextContent(
'1',
)
})
it('the regions entry is there with no role code, counting the regions', async () => {
mountDraft()
await ready()