diff --git a/apps/app/src/components/editor/annotations/EditableAnnotations.tsx b/apps/app/src/components/editor/annotations/EditableAnnotations.tsx index 31e0692b..83453d91 100644 --- a/apps/app/src/components/editor/annotations/EditableAnnotations.tsx +++ b/apps/app/src/components/editor/annotations/EditableAnnotations.tsx @@ -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(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, +) { 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(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, diff --git a/apps/app/src/components/editor/annotations/OverviewPanel.tsx b/apps/app/src/components/editor/annotations/OverviewPanel.tsx index d689afdc..e49bed3d 100644 --- a/apps/app/src/components/editor/annotations/OverviewPanel.tsx +++ b/apps/app/src/components/editor/annotations/OverviewPanel.tsx @@ -129,8 +129,17 @@ function CategoriesCard(props: OverviewPanelProps) { const counts = useOverviewCounts(doc) return (
-

+

{t('editor.overview.categories')} + + {doc.categories.length} +

{doc.categories.length === 0 && (

diff --git a/apps/app/src/components/editor/tabs/AnnotationsTab.tsx b/apps/app/src/components/editor/tabs/AnnotationsTab.tsx index 32ca9603..10bff770 100644 --- a/apps/app/src/components/editor/tabs/AnnotationsTab.tsx +++ b/apps/app/src/components/editor/tabs/AnnotationsTab.tsx @@ -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 ( + + ) +} + +/** 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 ( - - ) - } - const body = { doc, navigate, onOpenSection, focus } + const { readOnly, navigate, onOpenSection, focus } = props + const body = { navigate, onOpenSection, focus } + const ready = doc && preview return (

- {doc && preview && readOnly && ( - - )} - {doc && preview && !readOnly && ( - - )} + {ready && readOnly && } + {ready && !readOnly && }
) } +/** + * 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' ? ( + + ) : ( + + ) +} + /** The shown view is gone (an undo elsewhere): back to the main view, selection cleared. */ function useViewFallback( doc: AnatomicalSheetDocument, diff --git a/apps/app/src/editor/annotations/focusTarget.ts b/apps/app/src/editor/annotations/focusTarget.ts index e098a086..86075ee2 100644 --- a/apps/app/src/editor/annotations/focusTarget.ts +++ b/apps/app/src/editor/annotations/focusTarget.ts @@ -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, + 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(() => { diff --git a/apps/app/src/editor/useEditorNavigation.ts b/apps/app/src/editor/useEditorNavigation.ts index 5d9b6daa..9dea2266 100644 --- a/apps/app/src/editor/useEditorNavigation.ts +++ b/apps/app/src/editor/useEditorNavigation.ts @@ -107,17 +107,14 @@ interface Openers { section: RefObject } -/** The commands of the workspace: each commits the typed text, then moves. */ -function useMoves( - dispatch: Dispatch, - 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) { + const { live, announce } = useLiveRegion() const [move, setMove] = useState(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) { + const outcomes = useRef(new Map()) + 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()) + const { move, go, live, announce } = useGo(dispatch) const openers = useRef({ 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), + } } diff --git a/apps/app/tests/unit/editor-sections.test.tsx b/apps/app/tests/unit/editor-sections.test.tsx index 7a41ef9e..f90ea76e 100644 --- a/apps/app/tests/unit/editor-sections.test.tsx +++ b/apps/app/tests/unit/editor-sections.test.tsx @@ -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()