fix(editor): one history entry per reference edit, focus kept
Final review fixes. « Terminer » and every panel close re-committed the edited field against a stale value, adding a second identical history entry so the first « Défaire » seemed to do nothing: the tile now reads the latest applied value (tests assert the undo-length delta). A tile leaving the search on « Terminer » stays pinned until the next view change and keeps focus; « Ajouter la référence » resets search and sort and focuses the new tile. Also: refused draft dropped on a read-only flip is announced; arrow keys respect a refused tab switch; pinned index kept across read-only; the panel announces « Références citées »; search text inserted literally; « Aller à » names its citer; the edit header's name starts with « Terminer. »; undo removing the focused panel tile moves focus per P20; no-result announced once; docs corrected. Gates: typecheck, lint, test:run (app 1510), build, e2e:ui 251; e2e:shell 16 specs (admin and editor-equine excluded): 117 passed / 108 skipped / 1 failed (crossing:211 stuck on the MFA step-up screen before the editor), rerun alone 11 passed; full-row guard: no pre-existing row changed in any table.
This commit is contained in:
1 parent
2182307c62
commit
1bf27014b3
21 files changed
+677
-136
No files matched your search
@@ -1,7 +1,16 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import type { Reference } from '@faunosteo/core-wasm/types'
|
||||
import {
|
||||
useContext,
|
||||
useEffect,
|
||||
useLayoutEffect,
|
||||
useRef,
|
||||
useState,
|
||||
type RefObject,
|
||||
} 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'
|
||||
|
||||
/** The text fields of the edit view, in the order the leave guard tries them. */
|
||||
export const FIELDS = ['citation', 'doi', 'url'] as const
|
||||
@@ -14,6 +23,14 @@ type Drafts = Record<Field, string>
|
||||
*/
|
||||
export type LeaveGuard = () => Field | null
|
||||
|
||||
/** 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>
|
||||
}
|
||||
|
||||
const committedOf = (reference: Reference): Drafts => ({
|
||||
citation: reference.citation,
|
||||
doi: reference.doi ?? '',
|
||||
@@ -45,62 +62,128 @@ 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(
|
||||
(field) =>
|
||||
normalized(field, drafts[field]) !== committedOf(reference)[field],
|
||||
) ?? null
|
||||
|
||||
/**
|
||||
* 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').
|
||||
* `base` is the DOM id prefix of the tile; `registerLeave` hands `leave` to the library.
|
||||
* The reference as the document holds it now, and the drafts as typed. A blur right after
|
||||
* an apply (« Terminer » focusing the header, a close) runs before the next render, so
|
||||
* the rendered `reference` is stale there: `applied` advances with every apply and is
|
||||
* replaced when the document hands a new reference (undo, reload) (C1).
|
||||
*/
|
||||
export function useReferenceEdit(
|
||||
reference: Reference,
|
||||
base: string,
|
||||
registerLeave: (guard: LeaveGuard | null) => void,
|
||||
) {
|
||||
function useAppliedReference(reference: Reference, drafts: Drafts) {
|
||||
const applied = useRef(reference)
|
||||
const typed = useRef(drafts)
|
||||
useLayoutEffect(() => {
|
||||
applied.current = reference
|
||||
}, [reference])
|
||||
useLayoutEffect(() => {
|
||||
typed.current = drafts
|
||||
})
|
||||
return { applied, typed }
|
||||
}
|
||||
|
||||
/** What the view does to the document: `update`, `apply` a draft, which drafts are `pending`. */
|
||||
function useWriter(reference: Reference, drafts: Drafts) {
|
||||
const edit = useEditCommand()
|
||||
const [drafts, setDraft] = useDrafts(reference)
|
||||
const key = { kind: 'reference', id: reference.id } as const
|
||||
const update = (field: string, value: Reference) =>
|
||||
edit.run(field, {
|
||||
op: 'update',
|
||||
key,
|
||||
element: { kind: 'reference', value },
|
||||
})
|
||||
const apply = (current: Reference, field: Field): Reference | null => {
|
||||
const draft = drafts[field]
|
||||
if (field === 'citation' && isBlank(draft)) {
|
||||
edit.reject(field, 'editor.field.required')
|
||||
return null
|
||||
}
|
||||
const next = patched(current, field, draft)
|
||||
return update(field, next) ? next : null
|
||||
const { applied, typed } = useAppliedReference(reference, drafts)
|
||||
const update = (field: string, value: Reference) => {
|
||||
const key = { kind: 'reference', id: reference.id } as const
|
||||
const element = { kind: 'reference', value } as const
|
||||
const done = edit.run(field, { op: 'update', key, element })
|
||||
if (done) applied.current = value
|
||||
return done
|
||||
}
|
||||
const pending = (current: Reference, field: Field) =>
|
||||
normalized(field, drafts[field]) !== committedOf(current)[field]
|
||||
const leave: LeaveGuard = () => {
|
||||
let current = reference
|
||||
for (const field of FIELDS.filter((name) => pending(current, name))) {
|
||||
const next = apply(current, field)
|
||||
if (!next) {
|
||||
document.getElementById(`${base}-${field}`)?.focus()
|
||||
return field
|
||||
}
|
||||
current = next
|
||||
const apply = (field: Field): boolean => {
|
||||
if (field === 'citation' && isBlank(drafts[field])) {
|
||||
return edit.reject(field, 'editor.field.required')
|
||||
}
|
||||
return null
|
||||
return update(field, patched(applied.current, field, drafts[field]))
|
||||
}
|
||||
const pending = (field: Field) =>
|
||||
normalized(field, drafts[field]) !== committedOf(applied.current)[field]
|
||||
return { edit, applied, typed, update, apply, pending }
|
||||
}
|
||||
|
||||
/** The view's three effects: its guard, the focus ticket and the dropped-draft slot. */
|
||||
function useEditLifecycle(
|
||||
base: string,
|
||||
hooks: EditHooks,
|
||||
leave: LeaveGuard,
|
||||
writer: Pick<ReturnType<typeof useWriter>, 'applied' | 'typed'>,
|
||||
) {
|
||||
const { registerLeave, takeFocus, dropped } = hooks
|
||||
const { applied, typed } = writer
|
||||
useEffect(() => {
|
||||
registerLeave(leave)
|
||||
return () => registerLeave(null)
|
||||
})
|
||||
useEffect(() => {
|
||||
document.getElementById(`${base}-citation`)?.focus()
|
||||
}, [base])
|
||||
if (takeFocus()) document.getElementById(`${base}-citation`)?.focus()
|
||||
}, [base, takeFocus])
|
||||
useLayoutEffect(
|
||||
() => () => {
|
||||
dropped.current = pendingField(applied.current, typed.current)
|
||||
},
|
||||
[applied, typed, dropped],
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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').
|
||||
* `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).
|
||||
*/
|
||||
export function useReferenceEdit(
|
||||
reference: Reference,
|
||||
base: string,
|
||||
hooks: EditHooks,
|
||||
) {
|
||||
const [drafts, setDraft] = useDrafts(reference)
|
||||
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
|
||||
}
|
||||
return null
|
||||
}
|
||||
useEditLifecycle(base, hooks, leave, writer)
|
||||
return {
|
||||
edit,
|
||||
drafts,
|
||||
setDraft,
|
||||
commitField: (field: Field) =>
|
||||
pending(reference, field) && apply(reference, field),
|
||||
setType: (value: Reference) => update('type', value),
|
||||
commitField: (field: Field) => pending(field) && apply(field),
|
||||
setType: (type: ReferenceType) =>
|
||||
writer.update('type', { ...applied.current, type }),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>,
|
||||
) {
|
||||
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),
|
||||
)
|
||||
}, [readOnly, dropped, t, announce])
|
||||
}
|
||||
Reference in new issue
Block a user