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:
1 parent
52aac41472
commit
3303530717
15 files changed
+543
-94
No files matched your search
@@ -163,7 +163,7 @@ function EntryTile({
|
||||
readOnly={readOnly}
|
||||
movable={false}
|
||||
on={{
|
||||
toggle: () => tiles.toggle(key),
|
||||
toggle: () => tiles.toggle(key, readOnly),
|
||||
edit: () => void tiles.startEdit(key),
|
||||
done: tiles.finish,
|
||||
goTo: (target) => navigate(targetOfKey(target)),
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -50,7 +50,7 @@ export default function ReferenceTiles(props: Props) {
|
||||
readOnly={props.readOnly}
|
||||
movable={props.movable}
|
||||
on={{
|
||||
toggle: () => tiles.toggle(key),
|
||||
toggle: () => tiles.toggle(key, props.readOnly),
|
||||
edit: () => props.onEdit(position),
|
||||
done: tiles.finish,
|
||||
remove: () => props.onDelete(reference),
|
||||
|
||||
@@ -7,7 +7,7 @@ import { citationEntries, findingPath } from '@/editor/references/panel'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import { useEditCommand } from '@/editor/useEditCommand'
|
||||
import type { CanLeave } from '@/editor/useEditorNavigation'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useDroppedText } from './useReferenceEdit'
|
||||
import { tileKeys, useTileState } from './useTileState'
|
||||
|
||||
export interface PanelHooks {
|
||||
@@ -45,18 +45,13 @@ function useLeaveCheck(
|
||||
tiles: ReturnType<typeof useTileState>,
|
||||
register: PanelHooks['registerCanLeave'],
|
||||
) {
|
||||
const t = useT()
|
||||
const droppedText = useDroppedText()
|
||||
useEffect(() => {
|
||||
register((forced) => {
|
||||
const refused = tiles.leave()
|
||||
if (refused === null) return { allowed: true }
|
||||
if (refused.length === 0) return { allowed: true }
|
||||
if (!forced) return { allowed: false }
|
||||
const field = t(`editor.reference.${refused}`)
|
||||
const note = t('editor.references.draft_dropped').replace(
|
||||
'{field}',
|
||||
() => field,
|
||||
)
|
||||
return { allowed: true, note }
|
||||
return { allowed: true, note: droppedText(refused) }
|
||||
})
|
||||
return () => register(null)
|
||||
})
|
||||
|
||||
@@ -1,13 +1,5 @@
|
||||
import {
|
||||
useContext,
|
||||
useEffect,
|
||||
useLayoutEffect,
|
||||
useRef,
|
||||
useState,
|
||||
type RefObject,
|
||||
} from 'react'
|
||||
import { useEffect, useLayoutEffect, useRef, useState } from 'react'
|
||||
import type { Reference, ReferenceType } from '@faunosteo/core-wasm/types'
|
||||
import { AnnounceContext } from '@/editor/focusRequest'
|
||||
import { isBlank, nullable } from '@/editor/sheetDocument'
|
||||
import { useEditCommand } from '@/editor/useEditCommand'
|
||||
import { useT } from '@/i18n/useT'
|
||||
@@ -18,17 +10,18 @@ export type Field = (typeof FIELDS)[number]
|
||||
type Drafts = Record<Field, string>
|
||||
|
||||
/**
|
||||
* What the guard calls on a leave: null when every pending field was applied, else the
|
||||
* field that refused (its draft stays, the field is focused).
|
||||
* What the guard calls on a leave: every pending field is tried; the fields that refused
|
||||
* (their drafts stay, the first one is focused), empty when all were applied.
|
||||
*/
|
||||
export type LeaveGuard = () => Field | null
|
||||
export type LeaveGuard = () => Field[]
|
||||
|
||||
/** What the tile hands its edit view (the library owns the state). */
|
||||
export interface EditHooks {
|
||||
registerLeave: (guard: LeaveGuard | null) => void
|
||||
/** True once per « Modifier »: the mount focuses Citation, a remount never does. */
|
||||
takeFocus: () => boolean
|
||||
dropped: RefObject<Field | null>
|
||||
/** The view unmounted with these refused drafts; stable (it runs from a cleanup). */
|
||||
onDrop: (fields: readonly Field[]) => void
|
||||
}
|
||||
|
||||
const committedOf = (reference: Reference): Drafts => ({
|
||||
@@ -62,12 +55,12 @@ function useDrafts(reference: Reference) {
|
||||
return [drafts, setDraft] as const
|
||||
}
|
||||
|
||||
/** The first field whose draft differs from `reference`, null when none does. */
|
||||
const pendingField = (reference: Reference, drafts: Drafts) =>
|
||||
FIELDS.find(
|
||||
/** The fields whose draft differs from `reference`, in `FIELDS` order. */
|
||||
const pendingFields = (reference: Reference, drafts: Drafts) =>
|
||||
FIELDS.filter(
|
||||
(field) =>
|
||||
normalized(field, drafts[field]) !== committedOf(reference)[field],
|
||||
) ?? null
|
||||
)
|
||||
|
||||
/**
|
||||
* The reference as the document holds it now, and the drafts as typed. A blur right after
|
||||
@@ -109,14 +102,14 @@ function useWriter(reference: Reference, drafts: Drafts) {
|
||||
return { edit, applied, typed, update, apply, pending }
|
||||
}
|
||||
|
||||
/** The view's three effects: its guard, the focus ticket and the dropped-draft slot. */
|
||||
/** The view's three effects: its guard, the focus ticket and the dropped-draft report. */
|
||||
function useEditLifecycle(
|
||||
base: string,
|
||||
hooks: EditHooks,
|
||||
leave: LeaveGuard,
|
||||
writer: Pick<ReturnType<typeof useWriter>, 'applied' | 'typed'>,
|
||||
) {
|
||||
const { registerLeave, takeFocus, dropped } = hooks
|
||||
const { registerLeave, takeFocus, onDrop } = hooks
|
||||
const { applied, typed } = writer
|
||||
useEffect(() => {
|
||||
registerLeave(leave)
|
||||
@@ -125,21 +118,23 @@ function useEditLifecycle(
|
||||
useEffect(() => {
|
||||
if (takeFocus()) document.getElementById(`${base}-citation`)?.focus()
|
||||
}, [base, takeFocus])
|
||||
// Never clears a report (a StrictMode remount runs this cleanup with nothing pending).
|
||||
useLayoutEffect(
|
||||
() => () => {
|
||||
dropped.current = pendingField(applied.current, typed.current)
|
||||
const pending = pendingFields(applied.current, typed.current)
|
||||
if (pending.length > 0) onDrop(pending)
|
||||
},
|
||||
[applied, typed, dropped],
|
||||
[applied, typed, onDrop],
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The write path of one reference's edit view. A field applies to the draft when it
|
||||
* loses the focus (`commitField`); `leave` re-applies every field whose text differs from
|
||||
* the document, stops at the first refusal, shows it and focuses that field (P22, P22').
|
||||
* the document, shows each refusal and focuses the first refused field (P22, P22').
|
||||
* `base` is the DOM id prefix of the tile; `registerLeave` hands `leave` to the library.
|
||||
* `takeFocus` says whether this mount is a user's « Modifier » (it focuses Citation) and
|
||||
* `dropped` receives the field of a refused draft the view is unmounted with (M1).
|
||||
* `onDrop` receives the fields of the refused drafts the view is unmounted with (M1).
|
||||
*/
|
||||
export function useReferenceEdit(
|
||||
reference: Reference,
|
||||
@@ -150,12 +145,11 @@ export function useReferenceEdit(
|
||||
const writer = useWriter(reference, drafts)
|
||||
const { edit, applied, apply, pending } = writer
|
||||
const leave: LeaveGuard = () => {
|
||||
for (const field of FIELDS.filter(pending)) {
|
||||
if (apply(field)) continue
|
||||
document.getElementById(`${base}-${field}`)?.focus()
|
||||
return field
|
||||
const refused = FIELDS.filter(pending).filter((field) => !apply(field))
|
||||
if (refused.length > 0) {
|
||||
document.getElementById(`${base}-${refused[0]}`)?.focus()
|
||||
}
|
||||
return null
|
||||
return refused
|
||||
}
|
||||
useEditLifecycle(base, hooks, leave, writer)
|
||||
return {
|
||||
@@ -168,22 +162,13 @@ export function useReferenceEdit(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A read-only flip unmounts the edit view of the tile in edit: a refused draft it held
|
||||
* (left in `dropped` by the view) is lost, so the live region says which field (M1).
|
||||
*/
|
||||
export function useDropNote(
|
||||
readOnly: boolean,
|
||||
dropped: RefObject<Field | null>,
|
||||
) {
|
||||
/** « Saisie non appliquée : Citation, Adresse web (URL) »: the words for dropped drafts. */
|
||||
export function useDroppedText(): (fields: readonly Field[]) => string {
|
||||
const t = useT()
|
||||
const announce = useContext(AnnounceContext)
|
||||
useEffect(() => {
|
||||
if (!readOnly || dropped.current === null) return
|
||||
const field = t(`editor.reference.${dropped.current}`)
|
||||
dropped.current = null
|
||||
announce(
|
||||
t('editor.references.draft_dropped').replace('{field}', () => field),
|
||||
return (fields) => {
|
||||
const names = fields.map((field) => t(`editor.reference.${field}`))
|
||||
return t('editor.references.draft_dropped').replace('{field}', () =>
|
||||
names.join(', '),
|
||||
)
|
||||
}, [readOnly, dropped, t, announce])
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import { useReferenceView } from './useReferenceView'
|
||||
const NO_CITERS = new Map<string, never[]>()
|
||||
|
||||
/** The document's references with the tab's tile state and display order. */
|
||||
export function useReferenceLibrary(readOnly: boolean, resetSignal: number) {
|
||||
export function useReferenceLibrary(resetSignal: number) {
|
||||
const doc = useSheetDocument()
|
||||
const all: readonly Reference[] = doc?.references ?? []
|
||||
const keys = tileKeys(all)
|
||||
@@ -23,7 +23,6 @@ export function useReferenceLibrary(readOnly: boolean, resetSignal: number) {
|
||||
const shown = useReferenceView(
|
||||
{ all, keys, citers },
|
||||
tiles.editingKey,
|
||||
readOnly,
|
||||
resetSignal,
|
||||
)
|
||||
return { loaded: doc !== null, all, keys, tiles, citers, shown }
|
||||
|
||||
@@ -30,28 +30,32 @@ interface Shown {
|
||||
* afficher ») empties the search and the sort once per bump; the tab stays mounted (A2).
|
||||
* The tile that entered edit is pinned at the index it was shown at, whatever the search
|
||||
* or the sort, and stays pinned after « Terminer » until the next search or sort change,
|
||||
* so the tile the user just edited never vanishes under their focus (P23, I1). While
|
||||
* read-only the pin is suspended and its index kept (M3). `editingKey` is the tile in
|
||||
* edit, if any.
|
||||
* so the tile the user just edited never vanishes under their focus (P23, I1). A
|
||||
* read-only flip keeps the pin: the tile in edit stays shown across it, with its note.
|
||||
* `editingKey` is the tile in edit, if any.
|
||||
*/
|
||||
export function useReferenceView(
|
||||
{ all, keys, citers }: Shown,
|
||||
editingKey: string | null,
|
||||
readOnly: boolean,
|
||||
resetSignal: number,
|
||||
) {
|
||||
const locale = useLocaleStore((state) => state.locale)
|
||||
const [view, setView] = useState(INITIAL)
|
||||
const [seen, setSeen] = useState(resetSignal)
|
||||
const [pinned, setPinned] = useState<Pinned | null>(null)
|
||||
// The reset view is the sheet order: the tile in edit sits at its own position.
|
||||
const pinEdited = (): Pinned | null =>
|
||||
editingKey === null
|
||||
? null
|
||||
: { key: editingKey, index: Math.max(0, keys.indexOf(editingKey)) }
|
||||
if (seen !== resetSignal) {
|
||||
setSeen(resetSignal)
|
||||
setView(INITIAL)
|
||||
setPinned(null)
|
||||
setPinned(pinEdited())
|
||||
}
|
||||
const pinAt = pinned === null ? -1 : keys.indexOf(pinned.key)
|
||||
const pin =
|
||||
!readOnly && pinned !== null && pinAt >= 0 && !isSheetView(view)
|
||||
pinned !== null && pinAt >= 0 && !isSheetView(view)
|
||||
? { position: pinAt, index: pinned.index }
|
||||
: null
|
||||
const context = {
|
||||
@@ -66,9 +70,7 @@ export function useReferenceView(
|
||||
// A new search or sort: the tile in edit stays where it is shown, any other pin ends.
|
||||
const pinAfterChange = (): Pinned | null => {
|
||||
if (pinned === null || pinned.key !== editingKey) return null
|
||||
return readOnly
|
||||
? pinned
|
||||
: { key: pinned.key, index: pinAtShown(pinned.key) }
|
||||
return { key: pinned.key, index: pinAtShown(pinned.key) }
|
||||
}
|
||||
return {
|
||||
view,
|
||||
@@ -80,7 +82,7 @@ export function useReferenceView(
|
||||
},
|
||||
reset: () => {
|
||||
setView(INITIAL)
|
||||
setPinned(null)
|
||||
setPinned(pinEdited())
|
||||
},
|
||||
/** The tile `key` enters edit: remember where it is shown. */
|
||||
markEdited: (key: string) => setPinned({ key, index: pinAtShown(key) }),
|
||||
|
||||
@@ -35,16 +35,20 @@ export function useTileState(keys: readonly string[] | null) {
|
||||
// The tile a « Modifier » just opened: its edit view focuses Citation once, a remount
|
||||
// after a read-only interval does not (M1).
|
||||
const focusTicket = useRef<string | null>(null)
|
||||
/** Applies the tile in edit; the field that refused, null when nothing is left to apply. */
|
||||
const leave = (): Field | null => guard.current?.() ?? null
|
||||
/** Applies the tile in edit; the fields that refused, none when nothing is left to apply. */
|
||||
const leave = (): Field[] => guard.current?.() ?? []
|
||||
const open = (key: string) => setOpenKeys((prev) => new Set(prev).add(key))
|
||||
const finish = () => {
|
||||
if (leave() !== null) return false
|
||||
if (leave().length > 0) return false
|
||||
setEditingKey(null)
|
||||
return true
|
||||
}
|
||||
const toggle = (key: string) => {
|
||||
if (editingKey === key) return void finish()
|
||||
// Read-only, the edit view is gone: a toggle ends the edit state and collapses.
|
||||
const toggle = (key: string, readOnly = false) => {
|
||||
if (editingKey === key) {
|
||||
if (!readOnly) return void finish()
|
||||
setEditingKey(null)
|
||||
}
|
||||
setOpenKeys((prev) => {
|
||||
const next = new Set(prev)
|
||||
if (!next.delete(key)) next.add(key)
|
||||
@@ -52,7 +56,7 @@ export function useTileState(keys: readonly string[] | null) {
|
||||
})
|
||||
}
|
||||
const startEdit = (key: string) => {
|
||||
if (editingKey !== null && editingKey !== key && leave() !== null)
|
||||
if (editingKey !== null && editingKey !== key && leave().length > 0)
|
||||
return false
|
||||
focusTicket.current = key
|
||||
setEditingKey(key)
|
||||
|
||||
@@ -36,7 +36,7 @@ type Props = Pick<EditorPlaceProps, 'readOnly' | 'navigate' | 'focus'> & {
|
||||
*/
|
||||
function Library({ readOnly, navigate, focus, resetSignal }: Props) {
|
||||
const t = useT()
|
||||
const lib = useReferenceLibrary(readOnly, resetSignal)
|
||||
const lib = useReferenceLibrary(resetSignal)
|
||||
const searchRef = useRef<HTMLInputElement>(null)
|
||||
const act = useReferenceActions(lib, focus, searchRef)
|
||||
const [deleting, setDeleting] = useState<DeleteTarget | null>(null)
|
||||
|
||||
Reference in new issue
Block a user