feat(editor): kept-mounted tabs, sections and references panel

Each visited tab, Annotations section and the references panel stay
mounted in a KeptPlace (hidden, nested visibility). Composed canvases
detach while hidden. One JSON.parse per document text. Focus requests
reach only their owning place.

Rewritten assertions (R-24):
- editor-route.test.tsx:127: annotations testid toBeNull -> not.toBeVisible
- editor-sections.test.tsx: place('annotations.drawing') toBeNull -> not.toBeVisible
- editor-journey.spec.ts:311: page-wide getByLabel('Nom (FR)') -> scoped to place 'general'
- editor-tabs-lists.test.tsx: ReferencesPanel `open` prop dropped (the wrapper hides it)
- editor-annotations-tab, editor-focus-request, editor-sections tests: props gain `visited`
No assertion removed.
This commit is contained in:
bchanot committed 2026-11-06 20:50:40 +01:00
1 parent ca184da4d9
commit 6689827a8d
18 files changed
+847 -81

No files matched your search

@@ -1,4 +1,5 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import { usePlaceVisible } from '@/components/editor/KeptPlace'
import Button from '@/components/ui/Button'
import Notice from '@/components/ui/Notice'
import { coreRuntime, useCoreStopped } from '@/core/runtime'
@@ -60,6 +61,7 @@ export default function CatalogueCanvas({
const [attempt, setAttempt] = useState(0)
const [failure, setFailure] = useState<CanvasFailure | null>(null)
const stopped = useCoreStopped()
const visible = usePlaceVisible()
const { framing, zoomIn, zoomOut, reset, swallowClick, handlers } = useFraming(
viewport,
viewId,
@@ -74,17 +76,19 @@ export default function CatalogueCanvas({
useEffect(() => {
const element = host.current
if (!element) return
// One composed copy of a view in the page (kept places): the ids repeat otherwise.
if (!visible) return element.replaceChildren()
const build = buildView(preview, viewId)
setFailure(build.ok ? null : build.failure)
element.replaceChildren(...(build.ok ? [build.svg] : []))
}, [preview, viewId, attempt])
}, [preview, viewId, attempt, visible])
useEffect(() => {
host.current?.querySelectorAll<Element>(ANNOTATED).forEach((el) => {
if (el.getAttribute('data-anno-id') === selectedId) el.setAttribute('data-state', 'current')
else el.removeAttribute('data-state')
})
}, [selectedId, preview, viewId, attempt])
}, [selectedId, preview, viewId, attempt, visible])
useEffect(() => {
const element = host.current
@@ -1,4 +1,5 @@
import type { ComponentType } from 'react'
import { ownedBy } from '@/editor/focusRequest'
import type { AnnotationSection } from '@/editor/navigation'
import {
panelDomId,
@@ -6,6 +7,7 @@ import {
type EditorTabId,
} from '@/editor/tabs'
import { useT } from '@/i18n/useT'
import KeptPlace from './KeptPlace'
import AnnotationsTab from './tabs/AnnotationsTab'
import FindingsTab from './tabs/FindingsTab'
import GeneralTab from './tabs/GeneralTab'
@@ -23,31 +25,55 @@ const BODIES: Record<
/** What only the Annotations tab needs beyond the common place props. */
export interface AnnotationNav {
section: AnnotationSection
/** The sections shown at least once: each stays mounted. */
visited: readonly AnnotationSection[]
onOpenSection: (section: AnnotationSection, opener: HTMLElement) => void
onBack: () => void
}
interface Props extends EditorPlaceProps {
tab: EditorTabId
/** The tabs shown at least once: each stays mounted, hidden while another is shown. */
visited: readonly EditorTabId[]
annotations: AnnotationNav
}
/** The panel of the active tab; a place unmounts when left (kept alive in Lot B2). */
export default function EditorTabBody({ tab, annotations, ...props }: Props) {
function TabPanel({
id,
active,
annotations,
...props
}: Omit<Props, 'tab' | 'visited'> & { id: EditorTabId; active: boolean }) {
const t = useT()
const Body = tab === 'annotations' ? null : BODIES[tab]
const Body = id === 'annotations' ? null : BODIES[id]
return (
<div
id={panelDomId(tab)}
<KeptPlace
name={Body ? id : undefined}
visible={active}
id={panelDomId(id)}
role="tabpanel"
aria-label={t(`editor.tab.${tab}`)}
data-place={Body ? tab : undefined}
aria-label={t(`editor.tab.${id}`)}
>
{Body ? (
<Body {...props} />
<Body {...props} focus={ownedBy(props.focus, id)} />
) : (
<AnnotationsTab {...props} {...annotations} />
)}
</div>
</KeptPlace>
)
}
/**
* The panels of the tabs shown so far: a tab mounts at its first visit and stays mounted,
* hidden while another is shown, so nothing typed or selected is lost on a tab change.
* A focus request reaches only the place its target names.
*/
export default function EditorTabBody({ tab, visited, ...props }: Props) {
return (
<>
{visited.map((id) => (
<TabPanel key={id} id={id} active={id === tab} {...props} />
))}
</>
)
}
@@ -4,11 +4,9 @@ import {
AnnounceContext,
FocusConsumedContext,
ownedBy,
type PlaceName,
} from '@/editor/focusRequest'
import { saveFailureMessage } from '@/editor/messages'
import type { EditorMode } from '@/editor/mode'
import type { NavigationState } from '@/editor/navigation'
import { useEditorStore } from '@/editor/state/editorStore'
import { initialTab } from '@/editor/tabs'
import { useEditorNavigation } from '@/editor/useEditorNavigation'
@@ -19,6 +17,7 @@ import CopyIssueNotice from './CopyIssueNotice'
import DraftHeader from './DraftHeader'
import EditorTabBody from './EditorTabBody'
import EditorTabs from './EditorTabs'
import KeptPlace from './KeptPlace'
import ModeNotice from './ModeNotice'
import RecoveryNotice from './RecoveryNotice'
import ReferencesPanel from './references/ReferencesPanel'
@@ -48,11 +47,6 @@ function SaveFailureNotice() {
)
}
/** The `data-place` the shown tab and section name. */
function shownPlace({ tab, section }: NavigationState): PlaceName {
return tab === 'annotations' ? `annotations.${section}` : tab
}
/**
* The draft page once the version is ready: header, notices, tab row, the references
* panel and the shown place. Owns the navigation state, so the first tab is known at
@@ -85,21 +79,24 @@ export default function EditorWorkspace(props: Props) {
<FocusConsumedContext value={nav.consumed}>
<AnnounceContext value={nav.announce}>
{state.referencesMounted && (
<ReferencesPanel
readOnly={readOnly}
focus={ownedBy(state.focus, 'references')}
open={state.references.open}
onClose={nav.closeReferences}
/>
<KeptPlace name="references" visible={state.references.open}>
<ReferencesPanel
readOnly={readOnly}
focus={ownedBy(state.focus, 'references')}
onClose={nav.closeReferences}
/>
</KeptPlace>
)}
<EditorTabBody
tab={state.tab}
visited={state.visitedTabs}
readOnly={readOnly}
navigate={nav.navigate}
focus={ownedBy(state.focus, shownPlace(state))}
focus={state.focus}
reloadSheet={reloadSheet}
annotations={{
section: state.section,
visited: state.visitedSections,
onOpenSection: nav.openSection,
onBack: nav.backToDrawing,
}}
@@ -0,0 +1,39 @@
import {
createContext,
useContext,
type HTMLAttributes,
type ReactNode,
} from 'react'
const PlaceVisibleContext = createContext(true)
/**
* Whether the place around this component is shown: every enclosing `KeptPlace` is
* visible. Outside any `KeptPlace` (the catalogue pages) it is true.
*/
export function usePlaceVisible(): boolean {
return useContext(PlaceVisibleContext)
}
interface Props extends HTMLAttributes<HTMLDivElement> {
/** The `data-place` name; omitted for a wrapper that only nests places (tab panel). */
name?: string
visible: boolean
children: ReactNode
}
/**
* A place that stays mounted while hidden: `hidden` leaves the accessibility tree and keeps
* every component state (Tailwind's preflight makes `[hidden]` `display: none`). Nested
* places compose: a place is visible only when all the places around it are.
*/
export default function KeptPlace({ name, visible, children, ...rest }: Props) {
const parent = usePlaceVisible()
return (
<PlaceVisibleContext value={parent && visible}>
<div {...rest} data-place={name} hidden={!visible}>
{children}
</div>
</PlaceVisibleContext>
)
}
@@ -30,7 +30,6 @@ export default function AnnotationSection({
const headingId = `annotation-section-${section}-title`
return (
<section
data-place={`annotations.${section}`}
aria-labelledby={headingId}
className="space-y-4"
>
@@ -11,19 +11,17 @@ import type { EditorPlaceProps } from '@/editor/tabs'
import { useT } from '@/i18n/useT'
interface Props extends Pick<EditorPlaceProps, 'readOnly' | 'focus'> {
open: boolean
onClose: () => void
}
/**
* The references side panel, minimal until Lot D: the sheet's bibliography, cited by id
* from the findings, opened by the header button. Not a dialog: it keeps the page usable.
* Mounted at the first opening and kept (hidden) afterwards.
* The workspace mounts it at the first opening and keeps it (hidden) afterwards.
*/
export default function ReferencesPanel({
readOnly,
focus,
open,
onClose,
}: Props) {
const t = useT()
@@ -39,10 +37,8 @@ export default function ReferencesPanel({
return (
<section
id="editor-references-panel"
data-place="references"
data-testid="editor-references-panel"
aria-labelledby="editor-references-title"
hidden={!open}
onKeyDown={onKeyDown}
className="space-y-3 rounded-xl border border-[var(--color-border)] p-4"
>
@@ -10,6 +10,8 @@ import PropertiesPanel from '@/components/editor/annotations/PropertiesPanel'
import AnnotationSection from '@/components/editor/annotations/sections/AnnotationSection'
import ViewSwitcher from '@/components/editor/annotations/ViewSwitcher'
import SelectionTag from '@/components/editor/annotations/SelectionTag'
import KeptPlace from '@/components/editor/KeptPlace'
import { ownedBy } from '@/editor/focusRequest'
import { useAnnotationRequest } from '@/editor/annotations/focusTarget'
import { mainViewId, useViewModel } from '@/editor/annotations/useViewModel'
import type { AnnotationSection as SectionId } from '@/editor/navigation'
@@ -22,21 +24,30 @@ import { useLocaleStore } from '@/state/localeStore'
export interface AnnotationsTabProps extends EditorPlaceProps {
/** The section shown: the drawing workspace, or a full-width section. */
section: SectionId
/** The sections shown at least once: each stays mounted. */
visited: readonly SectionId[]
onOpenSection: (section: SectionId, opener: HTMLElement) => void
onBack: () => void
}
/** A full-width section, once the document is loaded. */
function SectionPlace(props: AnnotationsTabProps) {
function SectionPlace({
section,
readOnly,
focus,
onBack,
}: Pick<AnnotationsTabProps, 'readOnly' | 'focus' | 'onBack'> & {
section: Exclude<SectionId, 'drawing'>
}) {
const doc = useSheetDocument()
if (!doc || props.section === 'drawing') return null
if (!doc) return null
return (
<AnnotationSection
section={props.section}
section={section}
doc={doc}
readOnly={props.readOnly}
focus={props.focus}
onBack={props.onBack}
readOnly={readOnly}
focus={focus}
onBack={onBack}
/>
)
}
@@ -51,7 +62,6 @@ function DrawingPlace(props: AnnotationsTabProps) {
const ready = doc && preview
return (
<section
data-place="annotations.drawing"
data-testid="editor-tab-annotations"
aria-label={t('editor.tab.annotations')}
className="min-h-24 space-y-3"
@@ -62,16 +72,33 @@ function DrawingPlace(props: AnnotationsTabProps) {
)
}
/** One kept section: the drawing workspace or a full-width section. */
function SectionBody(props: AnnotationsTabProps & { id: SectionId }) {
const { id, ...rest } = props
const focus = ownedBy(props.focus, `annotations.${id}`)
if (id === 'drawing') return <DrawingPlace {...rest} focus={focus} />
return <SectionPlace {...rest} section={id} focus={focus} />
}
/**
* 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.
* Each section shown so far stays mounted, hidden while another is shown.
*/
export default function AnnotationsTab(props: AnnotationsTabProps) {
return props.section === 'drawing' ? (
<DrawingPlace {...props} />
) : (
<SectionPlace {...props} />
return (
<>
{props.visited.map((id) => (
<KeptPlace
key={id}
name={`annotations.${id}`}
visible={id === props.section}
>
<SectionBody {...props} id={id} />
</KeptPlace>
))}
</>
)
}
@@ -1,7 +1,8 @@
import { useEffect, useRef, useState } from 'react'
import { useEffect, useRef, useState, type RefObject } from 'react'
import type { ViewBox } from '@faunosteo/core-wasm/types'
import Notice from '@/components/ui/Notice'
import { buildView } from '@/components/catalogue/CatalogueCanvas'
import { usePlaceVisible } from '@/components/editor/KeptPlace'
import { useEditorStore } from '@/editor/state/editorStore'
import { useT } from '@/i18n/useT'
@@ -10,6 +11,35 @@ interface Frame {
ratio: string
}
/** Builds the parent view into `host` while its place is shown, and reads its frame. */
function useParentFrame(
parentId: string,
host: RefObject<HTMLDivElement | null>,
): Frame | 'failed' | null {
const preview = useEditorStore((s) => s.preview)
const handleVersion = useEditorStore((s) => s.handleVersion)
const visible = usePlaceVisible()
const [frame, setFrame] = useState<Frame | 'failed' | null>(null)
useEffect(() => {
const element = host.current
if (!element || !preview) return
// One composed copy of a view in the page: the ids repeat otherwise.
if (!visible) return element.replaceChildren()
const built = buildView(preview, parentId)
element.replaceChildren(...(built.ok ? [built.svg] : []))
setFrame(
built.ok
? {
viewBox: built.svg.getAttribute('viewBox') ?? '0 0 100 100',
ratio:
built.svg.getAttribute('preserveAspectRatio') ?? 'xMidYMid meet',
}
: 'failed',
)
}, [host, preview, parentId, handleVersion, visible])
return frame
}
/**
* The parent view as the core composes it, with the crop rectangle over it. The overlay
* is page-drawn and holds numbers only (one rectangle in the composed root's own
@@ -23,25 +53,8 @@ export default function CropCanvas({
box: ViewBox | null
}) {
const t = useT()
const preview = useEditorStore((s) => s.preview)
const handleVersion = useEditorStore((s) => s.handleVersion)
const host = useRef<HTMLDivElement | null>(null)
const [frame, setFrame] = useState<Frame | 'failed' | null>(null)
useEffect(() => {
const element = host.current
if (!element || !preview) return
const built = buildView(preview, parentId)
element.replaceChildren(...(built.ok ? [built.svg] : []))
setFrame(
built.ok
? {
viewBox: built.svg.getAttribute('viewBox') ?? '0 0 100 100',
ratio:
built.svg.getAttribute('preserveAspectRatio') ?? 'xMidYMid meet',
}
: 'failed',
)
}, [preview, parentId, handleVersion])
const frame = useParentFrame(parentId, host)
return (
<div className="space-y-2">
<div