feat(editor): cancel adding a reference by button or Escape
Draft tile gets « Annuler l'ajout »; Escape cancels from non-text targets and from a pristine text field, never while composing or already handled. Nothing reaches the document, history untouched, focus back on the add button.
This commit is contained in:
1 parent
1e19479853
commit
d1779928a9
10 files changed
+1138
-524
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useEffect, useState, type KeyboardEvent } from 'react'
|
||||
import type { ReferenceType } from '@faunosteo/core-wasm/types'
|
||||
import ActionButton from '@/components/editor/fields/ActionButton'
|
||||
import DraftField from '@/components/editor/fields/DraftField'
|
||||
@@ -17,14 +17,27 @@ interface Props {
|
||||
focusSerial: number
|
||||
/** The reference was created in one modification, under this id. */
|
||||
onAdded: (id: string) => void
|
||||
/** « Annuler l'ajout »: the draft goes, nothing was written. */
|
||||
onCancel: () => void
|
||||
}
|
||||
|
||||
const isTextTarget = (target: EventTarget) =>
|
||||
target instanceof HTMLTextAreaElement || target instanceof HTMLInputElement
|
||||
const isPristine = (form: typeof EMPTY) =>
|
||||
Object.values(form).every((v) => v === '')
|
||||
const isComposing = (event: KeyboardEvent) =>
|
||||
event.nativeEvent.isComposing || event.keyCode === 229
|
||||
|
||||
/**
|
||||
* The draft tile of « Ajouter une référence » (board 24 E): type « Choisir… » and citation
|
||||
* required, DOI and URL optional. Nothing reaches the document before « Ajouter la
|
||||
* référence », which inserts the reference as one modification.
|
||||
*/
|
||||
export default function NewReferenceTile({ focusSerial, onAdded }: Props) {
|
||||
export default function NewReferenceTile({
|
||||
focusSerial,
|
||||
onAdded,
|
||||
onCancel,
|
||||
}: Props) {
|
||||
const t = useT()
|
||||
const edit = useEditCommand()
|
||||
const [form, setForm] = useState(EMPTY)
|
||||
@@ -63,9 +76,18 @@ export default function NewReferenceTile({ focusSerial, onAdded }: Props) {
|
||||
})
|
||||
if (added) onAdded(created)
|
||||
}
|
||||
// Escape cancels from anywhere in the tile; a text field with typed text keeps it.
|
||||
const escape = (event: KeyboardEvent) => {
|
||||
if (event.key !== 'Escape' || event.defaultPrevented) return
|
||||
if (isComposing(event)) return
|
||||
if (isTextTarget(event.target) && !isPristine(form)) return
|
||||
event.preventDefault()
|
||||
onCancel()
|
||||
}
|
||||
return (
|
||||
<section
|
||||
aria-label={t('editor.reference.new')}
|
||||
onKeyDown={escape}
|
||||
className="rounded-lg border border-[var(--color-border)] bg-[var(--color-surface)]"
|
||||
>
|
||||
<div className="flex items-center gap-3 p-3">
|
||||
@@ -110,6 +132,9 @@ export default function NewReferenceTile({ focusSerial, onAdded }: Props) {
|
||||
<ActionButton readOnly={false} variant="primary" onClick={create}>
|
||||
{t('editor.reference.add')}
|
||||
</ActionButton>
|
||||
<ActionButton readOnly={false} variant="secondary" onClick={onCancel}>
|
||||
{t('editor.references.add_cancel')}
|
||||
</ActionButton>
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('editor.references.add_help')}
|
||||
</p>
|
||||
|
||||
@@ -6,6 +6,7 @@ import Select from '@/components/ui/Select'
|
||||
import { usePlural } from '@/editor/plural'
|
||||
import { REFERENCE_SORTS, type ReferenceSort } from '@/editor/references/sort'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { ADD_BUTTON_ID } from './addButton'
|
||||
|
||||
interface Props {
|
||||
query: string
|
||||
@@ -90,6 +91,7 @@ export default function ReferenceToolbar(props: Props) {
|
||||
<div className="flex flex-wrap items-center justify-between gap-3">
|
||||
<CountLine shown={props.shown} total={props.total} />
|
||||
<ActionButton
|
||||
id={ADD_BUTTON_ID}
|
||||
readOnly={!props.canAdd}
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
/** The add button's id: « Annuler l'ajout » gives it the focus back. */
|
||||
export const ADD_BUTTON_ID = 'tab-reference-add'
|
||||
@@ -10,6 +10,7 @@ import { referenceMatches } from '@/editor/references/filter'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import { tileKeys, useTileState } from './useTileState'
|
||||
import { useReferenceView } from './useReferenceView'
|
||||
import { ADD_BUTTON_ID } from './addButton'
|
||||
|
||||
const NO_CITERS = new Map<string, never[]>()
|
||||
|
||||
@@ -95,6 +96,11 @@ export function useReferenceActions(
|
||||
tiles.reveal(`${id}#0`)
|
||||
later({ kind: 'reference', id })
|
||||
},
|
||||
// C2: nothing was written; the search and sort reset by the add stay as they are.
|
||||
cancelAdd: () => {
|
||||
setDraft(null)
|
||||
document.getElementById(ADD_BUTTON_ID)?.focus()
|
||||
},
|
||||
edit: (position: number) => {
|
||||
if (tiles.startEdit(keys[position])) shown.markEdited(keys[position])
|
||||
},
|
||||
|
||||
@@ -86,7 +86,11 @@ function Library({ readOnly, navigate, focus, resetSignal }: Props) {
|
||||
/>
|
||||
<EditOnly readOnly={readOnly}>
|
||||
{act.draft !== null && (
|
||||
<NewReferenceTile focusSerial={act.draft} onAdded={act.added} />
|
||||
<NewReferenceTile
|
||||
focusSerial={act.draft}
|
||||
onAdded={act.added}
|
||||
onCancel={act.cancelAdd}
|
||||
/>
|
||||
)}
|
||||
</EditOnly>
|
||||
</>
|
||||
|
||||
Reference in new issue
Block a user