fix(editor): focus the first refused field with its own error

A user leave with several refused fields focused the first one while
the last refusal owned the single error slot. The leave now focuses
the first refused field, then re-applies it so its message shows (I1).
The add flow no longer re-pins the tile finish() just ended (M3).
This commit is contained in:
bchanot committed 2026-11-13 14:49:17 +01:00
1 parent 78fb2d076e
commit 94f8a8bf27
4 files changed
+134 -35

No files matched your search

@@ -131,7 +131,8 @@ function useEditLifecycle(
/**
* 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, shows each refusal and focuses the first refused field (P22, P22').
* the document, focuses the first refused field and shows that field's refusal (the view
* has one error slot; the other refused drafts stay as typed) (P22, P22', I1).
* `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
* `onDrop` receives the fields of the refused drafts the view is unmounted with (M1).
@@ -146,9 +147,12 @@ export function useReferenceEdit(
const { edit, applied, apply, pending } = writer
const leave: LeaveGuard = () => {
const refused = FIELDS.filter(pending).filter((field) => !apply(field))
if (refused.length > 0) {
document.getElementById(`${base}-${refused[0]}`)?.focus()
}
if (refused.length === 0) return refused
// The focus move runs the blur commit of the field that held it, which writes the
// one error slot; re-applying the first refusal last makes the focused field the
// one that shows its message (I1). Refused again, it writes nothing.
document.getElementById(`${base}-${refused[0]}`)?.focus()
apply(refused[0])
return refused
}
useEditLifecycle(base, hooks, leave, writer)
@@ -86,13 +86,13 @@ export function useReferenceActions(
// P24 (and P22): leave the tile in edit first, then a clean view and the draft.
startAdd: () => {
if (!tiles.finish()) return
shown.reset()
shown.reset(null)
setDraft((serial) => (serial ?? 0) + 1)
},
// P24: the new tile is shown whatever was typed meanwhile, so the view is reset.
added: (id: string) => {
setDraft(null)
shown.reset()
shown.reset(tiles.editingKey)
tiles.reveal(`${id}#0`)
later({ kind: 'reference', id })
},
@@ -44,14 +44,14 @@ export function useReferenceView(
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 || !keys.includes(editingKey)
const pinEdited = (key: string | null): Pinned | null =>
key === null || !keys.includes(key)
? null
: { key: editingKey, index: keys.indexOf(editingKey) }
: { key, index: keys.indexOf(key) }
if (seen !== resetSignal) {
setSeen(resetSignal)
setView(INITIAL)
setPinned(pinEdited())
setPinned(pinEdited(editingKey))
}
const pinAt = pinned === null ? -1 : keys.indexOf(pinned.key)
const pin =
@@ -80,9 +80,13 @@ export function useReferenceView(
setPinned(pinAfterChange())
setView((current) => ({ ...current, ...patch }))
},
reset: () => {
/**
* The clean view; `keep` is the tile still in edit, passed by the caller because a
* `finish()` in the same handler has not reached this render's `editingKey` (M3).
*/
reset: (keep: string | null) => {
setView(INITIAL)
setPinned(pinEdited())
setPinned(pinEdited(keep))
},
/** The tile `key` enters edit: remember where it is shown. */
markEdited: (key: string) => setPinned({ key, index: pinAtShown(key) }),