fix(editor): never drop a refused draft silently on a read-only flip

Drop slot is tile-local state; the edit view cleanup reports every pending
field (FIELDS order) only when some are pending. The tile shows a warning
note and says it once in the live region; it ends on toggle, Terminer or a
newer drop. LeaveGuard returns all refused fields (forced panel close names
them all). Pin follows editingKey through read-only; reset re-pins it.
Read-only toggle ends edit and collapses. Flip focus goes to the tile header.

R-24 mapping:
- view test "keeps its index ... read-only (M3)": renamed, same assertion
- view test "is suspended in read-only ...": rewritten as "stays pinned in
  read-only" (pin kept, tile still shown after a search); toggle test added
- reset test extended: tile in edit stays pinned through the reset
- new editor-references-flip.test.tsx covers (a)-(g); (h) is a stated limit
This commit is contained in:
bchanot committed 2026-11-13 11:11:51 +01:00
1 parent 52aac41472
commit 3303530717
15 files changed
+543 -94

No files matched your search

@@ -1,7 +1,8 @@
import { useRef, type RefObject } from 'react'
import { useContext, useEffect, useRef, useState, type RefObject } from 'react'
import type { ElementKey, Reference } from '@faunosteo/core-wasm/types'
import ActionButton from '@/components/editor/fields/ActionButton'
import Icon from '@/components/ui/Icon'
import { AnnounceContext } from '@/editor/focusRequest'
import { usePlural } from '@/editor/plural'
import type { CiterItem } from '@/editor/references/citations'
import { useT } from '@/i18n/useT'
@@ -9,7 +10,7 @@ import CitersList from './CitersList'
import ReferenceEditor from './ReferenceEditor'
import ReferenceFacts from './ReferenceFacts'
import type { ReferenceHost } from './referenceTypes'
import { useDropNote, type Field, type LeaveGuard } from './useReferenceEdit'
import { useDroppedText, type Field, type LeaveGuard } from './useReferenceEdit'
/** What a tile asks of the library that owns its state. */
export interface TileHandlers {
@@ -71,10 +72,12 @@ function TileHeader({
base,
props,
headerRef,
onToggle,
}: {
base: string
props: TileProps
headerRef: RefObject<HTMLButtonElement | null>
onToggle: () => void
}) {
const t = useT()
const plural = usePlural()
@@ -87,9 +90,10 @@ function TileHeader({
type="button"
id={`${base}-header`}
data-focus-key={key}
data-tile-header
aria-expanded={open || editing}
aria-controls={`${base}-body`}
onClick={props.on.toggle}
onClick={onToggle}
className={`grid min-h-11 w-full grid-cols-[1.25rem_1fr] items-start gap-x-3 gap-y-1 rounded-lg p-3 text-left ${META_COLUMN[props.host]} ${BUTTON_RING}`}
>
<Icon
@@ -216,6 +220,40 @@ function TileActions({ props }: { props: TileProps }) {
)
}
/**
* The refused drafts a read-only flip dropped, as the tile keeps them: said once in the
* polite live region, shown by the tile until its next action. An edit view that ends on
* purpose (« Terminer », another tile's « Modifier ») ends the note; a flip does not.
*/
function useDroppedDrafts(editing: boolean, readOnly: boolean) {
const droppedText = useDroppedText()
const announce = useContext(AnnounceContext)
const [dropped, setDropped] = useState<readonly Field[] | null>(null)
const [wasEditing, setWasEditing] = useState(editing)
if (wasEditing !== editing) {
setWasEditing(editing)
if (wasEditing && !readOnly) setDropped(null)
}
const said = useRef<readonly Field[] | null>(null)
useEffect(() => {
if (dropped === null || said.current === dropped) return
said.current = dropped
announce(droppedText(dropped))
})
return [dropped, setDropped] as const
}
/** « Saisie non appliquée : … »: plain text, the live region says it once elsewhere. */
function DroppedNote({ fields }: { fields: readonly Field[] }) {
const droppedText = useDroppedText()
return (
<p className="flex items-start gap-2 rounded-md bg-amber-50 p-2 text-sm text-amber-800">
<Icon name="alert" size={16} className="mt-0.5 shrink-0" />
{droppedText(fields)}
</p>
)
}
/**
* One reference as a collapsible tile (boards 23, 24): closed, open (facts, citers,
* actions), or in edit (fields, citers, « Terminer »). Read-only: the facts and the
@@ -226,14 +264,25 @@ export default function ReferenceTile(props: TileProps) {
const t = useT()
const base = tileBase(host, position, reference)
const headerRef = useRef<HTMLButtonElement>(null)
const dropped = useRef<Field | null>(null)
useDropNote(props.readOnly, dropped)
const [dropped, setDropped] = useDroppedDrafts(editing, props.readOnly)
const done = () => {
if (on.done()) headerRef.current?.focus()
}
const toggle = () => {
setDropped(null)
on.toggle()
}
return (
<li className="rounded-lg border border-[var(--color-border)] bg-[var(--color-surface)]">
<TileHeader base={base} props={props} headerRef={headerRef} />
<li
data-reference-tile
className="rounded-lg border border-[var(--color-border)] bg-[var(--color-surface)]"
>
<TileHeader
base={base}
props={props}
headerRef={headerRef}
onToggle={toggle}
/>
{(open || editing) && (
<div
id={`${base}-body`}
@@ -246,7 +295,7 @@ export default function ReferenceTile(props: TileProps) {
hooks={{
registerLeave: on.registerLeave,
takeFocus: on.takeFocus,
dropped,
onDrop: setDropped,
}}
/>
) : (
@@ -257,6 +306,7 @@ export default function ReferenceTile(props: TileProps) {
current={props.panel?.current}
onGoTo={on.goTo}
/>
{dropped && <DroppedNote fields={dropped} />}
{editing ? (
<ActionButton
readOnly={false}