fix(app): focus the shown tab after a crossing, one commit per typed field
- A field blurred by a 1 024 px crossing or a read-only switch hands its focus to the shown tab (or Plus), never to body (focusRequest, useViewport). - TextField no longer commits a blurred value twice when it unmounts before the render that brings the new value: one undo step, one count. - Annuler and Retablir share the field left by Tab, so Shift+Tab to Annuler and Enter still skips the undo of a refused field. - Adding a reference clears the finding filter and the search. Unit tests: no-loss (focus on tab and on Plus, one undo entry), history buttons (field kept across the pair), references panel (add clears, refused add keeps).
This commit is contained in:
1 parent
9c03cf2413
commit
0bb150f860
9 files changed
+149
-29
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { useRef, type FocusEvent } from 'react'
|
||||
import { useRef, type FocusEvent, type RefObject } from 'react'
|
||||
import { useEditorReadOnly } from '@/editor/editingContext'
|
||||
import { commitFocusedField } from '@/editor/focusRequest'
|
||||
import { canRedo, canUndo, useEditorStore } from '@/editor/state/editorStore'
|
||||
@@ -32,13 +32,22 @@ function asField(element: Element | EventTarget | null): HTMLElement | null {
|
||||
* The press, in two steps: leaving a field commits the typed text (its blur: forced at
|
||||
* pointer-down, natural when Tab reaches the button), the click or key then reads the
|
||||
* store, so the typed text is the last change the undo reverts. A field whose commit was
|
||||
* refused keeps its error: the step is skipped and focus goes back to it.
|
||||
* refused keeps its error: the step is skipped and focus goes back to it. The field is
|
||||
* remembered across the pair: from a field, « Annuler » is two Shift+Tab away.
|
||||
*/
|
||||
function usePress(direction: Direction, readOnly: boolean) {
|
||||
const left = useRef<HTMLElement | null>(null)
|
||||
function usePress(
|
||||
direction: Direction,
|
||||
readOnly: boolean,
|
||||
left: RefObject<HTMLElement | null>,
|
||||
) {
|
||||
const forget = () => {
|
||||
left.current = null
|
||||
}
|
||||
// Tab from « Rétablir » to « Annuler » (or back) is still the same press of the pair.
|
||||
const onBlur = (event: FocusEvent<HTMLElement>) => {
|
||||
const pair = event.currentTarget.parentElement
|
||||
if (!pair?.contains(event.relatedTarget as Node | null)) forget()
|
||||
}
|
||||
const onPointerDown = () => {
|
||||
left.current = asField(document.activeElement)
|
||||
commitFocusedField()
|
||||
@@ -62,18 +71,24 @@ function usePress(direction: Direction, readOnly: boolean) {
|
||||
return {
|
||||
onPointerDown,
|
||||
onFocus,
|
||||
onBlur: forget,
|
||||
onBlur,
|
||||
onPointerLeave: forget,
|
||||
onClick,
|
||||
}
|
||||
}
|
||||
|
||||
function HistoryButton({ direction }: { direction: Direction }) {
|
||||
function HistoryButton({
|
||||
direction,
|
||||
left,
|
||||
}: {
|
||||
direction: Direction
|
||||
left: RefObject<HTMLElement | null>
|
||||
}) {
|
||||
const t = useT()
|
||||
const readOnly = useEditorReadOnly()
|
||||
const steppable = useEditorStore(direction === 'undo' ? canUndo : canRedo)
|
||||
const name = t(`editor.history.${direction}_title`)
|
||||
const press = usePress(direction, readOnly)
|
||||
const press = usePress(direction, readOnly, left)
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
@@ -94,13 +109,14 @@ function HistoryButton({ direction }: { direction: Direction }) {
|
||||
|
||||
/** « Annuler » and « Rétablir », at the right of the tab row on every tab. */
|
||||
export default function HistoryButtons() {
|
||||
const left = useRef<HTMLElement | null>(null)
|
||||
return (
|
||||
<div
|
||||
className="ml-auto flex items-center gap-1"
|
||||
data-testid="history-buttons"
|
||||
>
|
||||
<HistoryButton direction="undo" />
|
||||
<HistoryButton direction="redo" />
|
||||
<HistoryButton direction="undo" left={left} />
|
||||
<HistoryButton direction="redo" left={left} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -75,7 +75,10 @@ function EditableText({
|
||||
const commit = () => {
|
||||
const next = trim ? draft.trim() : draft
|
||||
if (next !== draft) setDraft(next)
|
||||
if (next !== value) onCommit(next)
|
||||
if (next === value) return
|
||||
// The unmount cleanup runs before a render brings the new value: it must not commit twice.
|
||||
latest.current.value = next
|
||||
onCommit(next)
|
||||
}
|
||||
const onKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
|
||||
if (event.key === 'Enter' && !event.nativeEvent.isComposing) commit()
|
||||
|
||||
@@ -10,8 +10,11 @@ import { REFERENCE_TYPES } from './referenceTypes'
|
||||
|
||||
const EMPTY = { type: '', citation: '', url: '', doi: '' }
|
||||
|
||||
/** Adds a reference: the type and the citation are required, URL and DOI optional. */
|
||||
export default function ReferenceForm() {
|
||||
/**
|
||||
* Adds a reference: the type and the citation are required, URL and DOI optional.
|
||||
* `onAdded` runs after a successful add, so the list can show the new reference.
|
||||
*/
|
||||
export default function ReferenceForm({ onAdded }: { onAdded?: () => void }) {
|
||||
const t = useT()
|
||||
const edit = useEditCommand()
|
||||
const [form, setForm] = useState(EMPTY)
|
||||
@@ -41,7 +44,9 @@ export default function ReferenceForm() {
|
||||
},
|
||||
},
|
||||
}))
|
||||
if (added) setForm(EMPTY)
|
||||
if (!added) return
|
||||
setForm(EMPTY)
|
||||
onAdded?.()
|
||||
}
|
||||
return (
|
||||
<div className="space-y-2 rounded-lg border border-dashed border-[var(--color-border)] p-3">
|
||||
|
||||
@@ -225,6 +225,11 @@ export default function ReferencesPanel(props: Props) {
|
||||
onShowAll?.()
|
||||
searchRef.current?.focus()
|
||||
}
|
||||
// A new reference is neither cited by the filtered finding nor named by the search.
|
||||
const showAdded = () => {
|
||||
setQuery('')
|
||||
if (citedBy !== null) onShowAll?.()
|
||||
}
|
||||
const onKeyDown = (event: KeyboardEvent) => {
|
||||
if (event.key !== 'Escape' || event.defaultPrevented) return
|
||||
event.stopPropagation()
|
||||
@@ -265,7 +270,7 @@ export default function ReferencesPanel(props: Props) {
|
||||
}
|
||||
/>
|
||||
<EditOnly readOnly={readOnly}>
|
||||
<ReferenceForm />
|
||||
<ReferenceForm onAdded={showAdded} />
|
||||
</EditOnly>
|
||||
</>
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user