feat(editor): keep edit-only forms across a width change

Edit-only parts (creation forms, submission note, crop group, annotation
edit body) mount at the first editable render and stay mounted, hidden
and detached from canvases, while read-only. Guards: the viewport hook
commits the focused field before the tier crosses 1 024 px, the route
releases focus left in a hidden part and treats a store out of step as
read-only, delete and discard confirmations close when the editor turns
read-only, a drag is dropped while the edit body is hidden. Read-only
twins take an id prefix.

R-24: no existing assertion rewritten; editor-no-loss.test.tsx and two
editor-shell Chromium cases added.
This commit is contained in:
bchanot committed 2026-11-06 21:22:10 +01:00
1 parent 9d89daed19
commit c57af6441f
23 files changed
+883 -108

No files matched your search

@@ -16,6 +16,8 @@ interface Props {
doc: AnatomicalSheetDocument
readOnly: boolean
edit: EditCommandApi
/** Put before every field id (the read-only twin of the panel). */
idPrefix?: string
}
/** One `update` carrying the whole annotation with `patch` applied. */
@@ -37,7 +39,7 @@ const noneOption = (label: string) => ({ value: '', label })
function NameFields(props: Props) {
const t = useT()
const send = useSend(props)
const { annotation, edit, readOnly } = props
const { annotation, edit, readOnly, idPrefix = '' } = props
const isLink = annotation.kind === 'detail_trigger'
const rename = (locale: 'fr' | 'en') => (value: string) => {
const name = {
@@ -55,7 +57,7 @@ function NameFields(props: Props) {
{(['fr', 'en'] as const).map((locale) => (
<TextField
key={locale}
id={`annotation-name-${locale}`}
id={`${idPrefix}annotation-name-${locale}`}
label={t(`editor.annotations.name_${locale}`)}
value={annotation.name?.[locale] ?? ''}
onCommit={rename(locale)}
@@ -72,7 +74,7 @@ export default function AnnotationFields(props: Props) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const send = useSend(props)
const { annotation, doc, readOnly, edit } = props
const { annotation, doc, readOnly, edit, idPrefix = '' } = props
const categories = doc.categories.map((c) => ({
value: c.id,
label: c.name[locale],
@@ -85,7 +87,7 @@ export default function AnnotationFields(props: Props) {
<div className="grid gap-3 sm:grid-cols-2">
<NameFields {...props} />
<SelectField
id="annotation-category"
id={`${idPrefix}annotation-category`}
label={t('editor.annotations.category')}
value={annotation.category_id}
options={categories}
@@ -94,7 +96,7 @@ export default function AnnotationFields(props: Props) {
error={edit.fieldError('category')}
/>
<TextField
id="annotation-color"
id={`${idPrefix}annotation-color`}
label={t('editor.annotations.color')}
value={annotation.color ?? ''}
onCommit={(value) => send('color', { color: nullable(value) })}
@@ -103,7 +105,7 @@ export default function AnnotationFields(props: Props) {
error={edit.fieldError('color')}
/>
<SelectField
id="annotation-laterality"
id={`${idPrefix}annotation-laterality`}
label={t('editor.annotations.laterality')}
value={annotation.laterality}
options={sides}
@@ -120,7 +122,7 @@ export default function AnnotationFields(props: Props) {
)
}
function OpensField({ annotation, doc, readOnly, edit }: Props) {
function OpensField({ annotation, doc, readOnly, edit, idPrefix = '' }: Props) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const send = useSend({ annotation, doc, readOnly, edit })
@@ -135,7 +137,7 @@ function OpensField({ annotation, doc, readOnly, edit }: Props) {
}
return (
<SelectField
id="annotation-opens"
id={`${idPrefix}annotation-opens`}
label={t('editor.annotations.opens')}
value={annotation.opens_view_id}
options={options}
@@ -150,7 +152,7 @@ function LinkFields(props: Props) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const send = useSend(props)
const { annotation, doc, readOnly, edit } = props
const { annotation, doc, readOnly, edit, idPrefix = '' } = props
if (annotation.kind === 'detail_trigger') return null
const regions = [
noneOption(t('editor.annotations.none')),
@@ -166,7 +168,7 @@ function LinkFields(props: Props) {
return (
<>
<SelectField
id="annotation-region"
id={`${idPrefix}annotation-region`}
label={t('editor.annotations.region')}
value={annotation.vertebral_region_code ?? ''}
options={regions}
@@ -177,7 +179,7 @@ function LinkFields(props: Props) {
error={edit.fieldError('region')}
/>
<SelectField
id="annotation-zone"
id={`${idPrefix}annotation-zone`}
label={t('editor.annotations.zone')}
value={annotation.finding_zone_id ?? ''}
options={zones}
@@ -23,6 +23,7 @@ import {
type ViewModel,
} from '@/editor/annotations/useViewModel'
import type { AnnotationSection } from '@/editor/navigation'
import { usePlaceVisible } from '@/lib/placeVisibility'
import type { EditorPlaceProps } from '@/editor/tabs'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
@@ -213,6 +214,8 @@ function CanvasStage({
placement: Placement
}) {
const locale = useLocaleStore((s) => s.locale)
// Hidden (the editor is read-only): no drag in flight survives, none is committed.
const shown = usePlaceVisible()
const { doc, ui, dispatch, model, frame, selected } = scene
const view = doc.views.find((v) => v.id === scene.viewId)
const blocked = !model.measured || doc.categories.length === 0
@@ -223,7 +226,7 @@ function CanvasStage({
viewName={view.name[locale]}
tool={ui.tool}
selectedId={ui.selectedId}
moveTarget={moveTargetOf(selected, model, ui.tool)}
moveTarget={shown ? moveTargetOf(selected, model, ui.tool) : null}
overlay={overlayOf(ui, selected, frame)}
tools={{
tool: ui.tool,
@@ -21,6 +21,8 @@ export interface OverviewPanelProps {
viewId: string
/** Read-only modes only consult: the entries read « Voir … ». */
readOnly: boolean
/** Put before every id (the read-only twin beside the kept edit panel). */
idPrefix?: string
onSelectView: (viewId: string) => void
onOpenSection: (section: AnnotationSection, opener: HTMLElement) => void
}
@@ -91,10 +93,11 @@ function ViewRow({
function ViewsCard(props: OverviewPanelProps) {
const t = useT()
const { doc, viewId, readOnly, onSelectView, onOpenSection } = props
const id = `${props.idPrefix ?? ''}overview-views`
const counts = useOverviewCounts(doc)
return (
<section aria-labelledby="overview-views" className={CARD}>
<h3 id="overview-views" className={TITLE}>
<section aria-labelledby={id} className={CARD}>
<h3 id={id} className={TITLE}>
{t('editor.overview.views')}
</h3>
<ul>
@@ -126,13 +129,11 @@ function CategoriesCard(props: OverviewPanelProps) {
const plural = usePlural()
const locale = useLocaleStore((s) => s.locale)
const { doc, readOnly, onOpenSection } = props
const id = `${props.idPrefix ?? ''}overview-categories`
const counts = useOverviewCounts(doc)
return (
<section aria-labelledby="overview-categories" className={CARD}>
<h3
id="overview-categories"
className={`${TITLE} flex items-center justify-between`}
>
<section aria-labelledby={id} className={CARD}>
<h3 id={id} className={`${TITLE} flex items-center justify-between`}>
{t('editor.overview.categories')}
<span
data-testid="overview-categories-count"
@@ -37,6 +37,8 @@ export interface PropertiesPanelProps {
dispatch: Dispatch<UiAction>
context: CommitContext
navigate: EditorPlaceProps['navigate']
/** Put before every id, for a twin of the panel that lives beside another one. */
idPrefix?: string
}
/** What still names this annotation as invalid: never hidden, never shown as valid. */
@@ -73,6 +75,7 @@ export default function PropertiesPanel(props: PropertiesPanelProps) {
doc={doc}
readOnly={readOnly}
edit={edit}
idPrefix={props.idPrefix}
/>
<GeometrySection {...props} />
<StatusActions annotation={annotation} readOnly={readOnly} edit={edit} />
@@ -94,7 +97,7 @@ function GeometrySection(props: PropertiesPanelProps) {
{isEditable(geometry) ? (
<GeometryGroup
key={annotation.id}
idPrefix="annotation"
idPrefix={`${props.idPrefix ?? ''}annotation`}
geometry={geometry}
locked={locked}
onApply={(next) => applyGeometry(annotation, next, props.context)}
@@ -2,6 +2,7 @@ import { useState } from 'react'
import DeleteDialog, {
type DeleteTarget,
} from '@/components/editor/DeleteDialog'
import EditOnly from '@/components/editor/EditOnly'
import RegionForm from '@/components/editor/vertebrae/RegionForm'
import RegionRow from '@/components/editor/vertebrae/RegionRow'
import { useRowFocus } from '@/editor/focusRequest'
@@ -48,7 +49,11 @@ export default function RegionsSection({
/>
))}
</ul>
{doc && !readOnly && <RegionForm />}
{doc && (
<EditOnly readOnly={readOnly}>
<RegionForm />
</EditOnly>
)}
<DeleteDialog target={deleting} onClose={() => setDeleting(null)} />
</div>
)
@@ -2,6 +2,7 @@ import { useState } from 'react'
import DeleteDialog, {
type DeleteTarget,
} from '@/components/editor/DeleteDialog'
import EditOnly from '@/components/editor/EditOnly'
import DetailViewForm from '@/components/editor/views/DetailViewForm'
import DetailViewRow from '@/components/editor/views/DetailViewRow'
import MainViewCard from '@/components/editor/views/MainViewCard'
@@ -61,7 +62,11 @@ export default function ViewsSection({
/>
))}
</ul>
{doc && !readOnly && <DetailViewForm views={views} />}
{doc && (
<EditOnly readOnly={readOnly}>
<DetailViewForm views={views} />
</EditOnly>
)}
<DeleteDialog target={deleting} onClose={() => setDeleting(null)} />
</div>
)