refactor(editor): split B1 navigation hooks, category count on the overview card
This commit is contained in:
1 parent
bc2dba7c14
commit
ca184da4d9
6 files changed
+166
-82
No files matched your search
@@ -78,12 +78,13 @@ function useViewFallback(
|
|||||||
}, [doc, shown, dispatch])
|
}, [doc, shown, dispatch])
|
||||||
}
|
}
|
||||||
|
|
||||||
function useScene({ doc, navigate, onOpenSection, focus }: Props): Scene {
|
/** « Aller à » an annotation: the request, with what it needs of the drawing's state. */
|
||||||
const [ui, dispatch] = useReducer(uiReducer, initialUi)
|
function useSceneRequest(
|
||||||
const [root, setRoot] = useState<RootAttributes | null>(null)
|
{ doc, focus }: Props,
|
||||||
const viewId = ui.viewId ?? mainViewId(doc)
|
ui: UiState,
|
||||||
const model = useViewModel(doc, viewId)
|
viewId: string,
|
||||||
const own = model.measured && root?.viewId === viewId
|
dispatch: Dispatch<UiAction>,
|
||||||
|
) {
|
||||||
useViewFallback(doc, ui.viewId, dispatch)
|
useViewFallback(doc, ui.viewId, dispatch)
|
||||||
useAnnotationRequest(focus, doc, {
|
useAnnotationRequest(focus, doc, {
|
||||||
viewId,
|
viewId,
|
||||||
@@ -94,6 +95,16 @@ function useScene({ doc, navigate, onOpenSection, focus }: Props): Scene {
|
|||||||
dispatch({ type: 'select', id })
|
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 {
|
return {
|
||||||
doc,
|
doc,
|
||||||
ui,
|
ui,
|
||||||
|
|||||||
@@ -129,8 +129,17 @@ function CategoriesCard(props: OverviewPanelProps) {
|
|||||||
const counts = useOverviewCounts(doc)
|
const counts = useOverviewCounts(doc)
|
||||||
return (
|
return (
|
||||||
<section aria-labelledby="overview-categories" className={CARD}>
|
<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')}
|
{t('editor.overview.categories')}
|
||||||
|
<span
|
||||||
|
data-testid="overview-categories-count"
|
||||||
|
className="font-normal text-[var(--color-ink-muted)]"
|
||||||
|
>
|
||||||
|
{doc.categories.length}
|
||||||
|
</span>
|
||||||
</h3>
|
</h3>
|
||||||
{doc.categories.length === 0 && (
|
{doc.categories.length === 0 && (
|
||||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||||
|
|||||||
@@ -26,29 +26,29 @@ export interface AnnotationsTabProps extends EditorPlaceProps {
|
|||||||
onBack: () => void
|
onBack: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** A full-width section, once the document is loaded. */
|
||||||
* Annotations: the drawing workspace (canvas with its tools, the overview or the
|
function SectionPlace(props: AnnotationsTabProps) {
|
||||||
* properties and creation panels), or one of its full-width sections. Read-only, the
|
const doc = useSheetDocument()
|
||||||
* same view is browsable (canvas, list, properties as text) with no tool and no action.
|
if (!doc || props.section === 'drawing') return null
|
||||||
*/
|
return (
|
||||||
export default function AnnotationsTab(props: AnnotationsTabProps) {
|
<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 t = useT()
|
||||||
const doc = useSheetDocument()
|
const doc = useSheetDocument()
|
||||||
const preview = useEditorStore((s) => s.preview)
|
const preview = useEditorStore((s) => s.preview)
|
||||||
const { section, readOnly, focus, navigate, onOpenSection, onBack } = props
|
const { readOnly, navigate, onOpenSection, focus } = props
|
||||||
if (section !== 'drawing') {
|
const body = { navigate, onOpenSection, focus }
|
||||||
if (!doc) return null
|
const ready = doc && preview
|
||||||
return (
|
|
||||||
<AnnotationSection
|
|
||||||
section={section}
|
|
||||||
doc={doc}
|
|
||||||
readOnly={readOnly}
|
|
||||||
focus={focus}
|
|
||||||
onBack={onBack}
|
|
||||||
/>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
const body = { doc, navigate, onOpenSection, focus }
|
|
||||||
return (
|
return (
|
||||||
<section
|
<section
|
||||||
data-place="annotations.drawing"
|
data-place="annotations.drawing"
|
||||||
@@ -56,16 +56,25 @@ export default function AnnotationsTab(props: AnnotationsTabProps) {
|
|||||||
aria-label={t('editor.tab.annotations')}
|
aria-label={t('editor.tab.annotations')}
|
||||||
className="min-h-24 space-y-3"
|
className="min-h-24 space-y-3"
|
||||||
>
|
>
|
||||||
{doc && preview && readOnly && (
|
{ready && readOnly && <ReadOnlyAnnotations {...body} doc={doc} />}
|
||||||
<ReadOnlyAnnotations {...body} doc={doc} />
|
{ready && !readOnly && <EditableAnnotations {...body} doc={doc} />}
|
||||||
)}
|
|
||||||
{doc && preview && !readOnly && (
|
|
||||||
<EditableAnnotations {...body} doc={doc} />
|
|
||||||
)}
|
|
||||||
</section>
|
</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. */
|
/** The shown view is gone (an undo elsewhere): back to the main view, selection cleared. */
|
||||||
function useViewFallback(
|
function useViewFallback(
|
||||||
doc: AnatomicalSheetDocument,
|
doc: AnatomicalSheetDocument,
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import type {
|
|||||||
} from '@faunosteo/core-wasm/types'
|
} from '@faunosteo/core-wasm/types'
|
||||||
import {
|
import {
|
||||||
AnnounceContext,
|
AnnounceContext,
|
||||||
|
type FocusOutcome,
|
||||||
useFocusRequest,
|
useFocusRequest,
|
||||||
visibleElement,
|
visibleElement,
|
||||||
type FocusRequest,
|
type FocusRequest,
|
||||||
@@ -39,6 +40,33 @@ const BUSY_TARGET = {
|
|||||||
|
|
||||||
const focusFirst = (selector: string) => visibleElement(selector)?.focus()
|
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
|
* « 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
|
* 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
|
const found = target.key ? annotationFocus(doc, target.key) : null
|
||||||
if (!found) return 'none'
|
if (!found) return 'none'
|
||||||
if (drawing.busy) {
|
if (drawing.busy) {
|
||||||
announce(t(`editor.live.${drawing.busy}_kept`))
|
return keepBusy(
|
||||||
focusFirst(BUSY_TARGET[drawing.busy])
|
drawing.busy,
|
||||||
return 'kept'
|
announce,
|
||||||
}
|
t(`editor.live.${drawing.busy}_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)
|
|
||||||
}
|
}
|
||||||
|
showAnnotation(found, drawing, awaiting)
|
||||||
return 'found'
|
return 'found'
|
||||||
})
|
})
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
@@ -107,17 +107,14 @@ interface Openers {
|
|||||||
section: RefObject<HTMLElement | null>
|
section: RefObject<HTMLElement | null>
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The commands of the workspace: each commits the typed text, then moves. */
|
type Go = (action: NavigationAction, kind: MoveKind, text: string) => void
|
||||||
function useMoves(
|
|
||||||
dispatch: Dispatch<NavigationAction>,
|
/** The one way to move: commit the typed text, announce, dispatch, record the move. */
|
||||||
announce: (text: string) => void,
|
function useGo(dispatch: Dispatch<NavigationAction>) {
|
||||||
openers: Openers,
|
const { live, announce } = useLiveRegion()
|
||||||
) {
|
|
||||||
const t = useT()
|
|
||||||
const moveText = useMoveText()
|
|
||||||
const [move, setMove] = useState<Move | null>(null)
|
const [move, setMove] = useState<Move | null>(null)
|
||||||
const go = useCallback(
|
const go: Go = useCallback(
|
||||||
(action: NavigationAction, kind: MoveKind, text: string) => {
|
(action, kind, text) => {
|
||||||
commitFocusedField()
|
commitFocusedField()
|
||||||
announce(text)
|
announce(text)
|
||||||
dispatch(action)
|
dispatch(action)
|
||||||
@@ -125,6 +122,13 @@ function useMoves(
|
|||||||
},
|
},
|
||||||
[announce, dispatch],
|
[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(
|
const navigate = useCallback(
|
||||||
(target: EditorTarget, opener?: Opener) => {
|
(target: EditorTarget, opener?: Opener) => {
|
||||||
if (target.place === 'references') {
|
if (target.place === 'references') {
|
||||||
@@ -137,7 +141,8 @@ function useMoves(
|
|||||||
const openSection = useCallback(
|
const openSection = useCallback(
|
||||||
(section: AnnotationSection, opener?: Opener) => {
|
(section: AnnotationSection, opener?: Opener) => {
|
||||||
openers.section.current = opener ?? null
|
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({ type: 'open_section', section }, 'section', text)
|
||||||
},
|
},
|
||||||
[go, openers, t],
|
[go, openers, t],
|
||||||
@@ -146,6 +151,12 @@ function useMoves(
|
|||||||
const text = `${t('editor.tab.annotations')} · ${t('editor.place.drawing')}`
|
const text = `${t('editor.tab.annotations')} · ${t('editor.place.drawing')}`
|
||||||
go({ type: 'back_to_drawing' }, 'back', text)
|
go({ type: 'back_to_drawing' }, 'back', text)
|
||||||
}, [go, t])
|
}, [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(
|
const openReferences = useCallback(
|
||||||
(citedBy: ElementKey | null, opener?: Opener) => {
|
(citedBy: ElementKey | null, opener?: Opener) => {
|
||||||
openers.references.current = opener ?? null
|
openers.references.current = opener ?? null
|
||||||
@@ -158,14 +169,7 @@ function useMoves(
|
|||||||
const text = t('editor.live.references_closed')
|
const text = t('editor.live.references_closed')
|
||||||
go({ type: 'close_references' }, 'references_closed', text)
|
go({ type: 'close_references' }, 'references_closed', text)
|
||||||
}, [go, t])
|
}, [go, t])
|
||||||
return {
|
return { openReferences, closeReferences }
|
||||||
move,
|
|
||||||
navigate,
|
|
||||||
openSection,
|
|
||||||
backToDrawing,
|
|
||||||
openReferences,
|
|
||||||
closeReferences,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** « Retour au dessin »: the entry that opened the section, else the canvas box. */
|
/** « 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)
|
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`. */
|
/** Where the focus lands after a programmatic move; never `body`. */
|
||||||
function landingOf(move: Move, state: NavigationState): HTMLElement | null {
|
function landingOf(move: Move, state: NavigationState): HTMLElement | null {
|
||||||
if (move.kind === 'references') return headingOf('references')
|
if (move.kind === 'references') return headingOf('references')
|
||||||
@@ -199,12 +209,7 @@ function useMoveFocus(
|
|||||||
if (!move || handled.current === move) return
|
if (!move || handled.current === move) return
|
||||||
handled.current = move
|
handled.current = move
|
||||||
if (move.kind === 'references_closed') {
|
if (move.kind === 'references_closed') {
|
||||||
const opener = openers.references.current
|
referencesBack(openers.references.current)?.focus()
|
||||||
const target =
|
|
||||||
opener && isShown(opener)
|
|
||||||
? opener
|
|
||||||
: document.getElementById(REFERENCES_BUTTON_ID)
|
|
||||||
target?.focus()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const serial = state.focus?.serial
|
const serial = state.focus?.serial
|
||||||
@@ -219,6 +224,26 @@ function useMoveFocus(
|
|||||||
}, [move, state, outcomes, dispatch, openers])
|
}, [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). */
|
/** Navigation of the draft editor: the reducer plus the DOM side (focus, live region). */
|
||||||
export function useEditorNavigation(initialTab: EditorTabId): EditorNavigation {
|
export function useEditorNavigation(initialTab: EditorTabId): EditorNavigation {
|
||||||
const [state, dispatch] = useReducer(
|
const [state, dispatch] = useReducer(
|
||||||
@@ -226,21 +251,20 @@ export function useEditorNavigation(initialTab: EditorTabId): EditorNavigation {
|
|||||||
initialTab,
|
initialTab,
|
||||||
initialNavigation,
|
initialNavigation,
|
||||||
)
|
)
|
||||||
const { live, announce } = useLiveRegion()
|
const { move, go, live, announce } = useGo(dispatch)
|
||||||
const outcomes = useRef(new Map<number, FocusOutcome>())
|
|
||||||
const openers = useRef<Openers>({
|
const openers = useRef<Openers>({
|
||||||
references: { current: null },
|
references: { current: null },
|
||||||
section: { current: null },
|
section: { current: null },
|
||||||
}).current
|
}).current
|
||||||
const moves = useMoves(dispatch, announce, openers)
|
const receipts = useReceipts(dispatch)
|
||||||
const consumed = useCallback((serial: number, outcome: FocusOutcome) => {
|
useMoveFocus(move, state, receipts.outcomes, dispatch, openers)
|
||||||
outcomes.current.set(serial, outcome)
|
return {
|
||||||
dispatch({ type: 'focus_consumed', serial })
|
state,
|
||||||
}, [])
|
live,
|
||||||
const selectTab = useCallback((tab: EditorTabId) => {
|
announce,
|
||||||
commitFocusedField()
|
consumed: receipts.consumed,
|
||||||
dispatch({ type: 'select_tab', tab })
|
selectTab: receipts.selectTab,
|
||||||
}, [])
|
...useSectionMoves(go, openers),
|
||||||
useMoveFocus(moves.move, state, outcomes, dispatch, openers)
|
...useReferenceMoves(go, openers),
|
||||||
return { state, live, selectTab, consumed, announce, ...moves }
|
}
|
||||||
}
|
}
|
||||||
@@ -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 () => {
|
it('the regions entry is there with no role code, counting the regions', async () => {
|
||||||
mountDraft()
|
mountDraft()
|
||||||
await ready()
|
await ready()
|
||||||
|
|||||||
Reference in new issue
Block a user