fix(editor): draft notice after the reference dialog, locale list
This commit is contained in:
1 parent
1d119c3684
commit
ee7912768c
7 files changed
+156
-17
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { useState } from 'react'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import type { ElementKey } from '@faunosteo/core-wasm/types'
|
||||
import ActionButton from '@/components/editor/fields/ActionButton'
|
||||
import { useItemMode } from '@/components/editor/findings/useItemMode'
|
||||
@@ -55,7 +55,7 @@ interface BodyProps {
|
||||
mode: Mode
|
||||
props: CitationsDialogProps
|
||||
/** The edit mode's status line (a draft creation, a pending one removed; Rev 6). */
|
||||
onNotice: (text: string) => void
|
||||
onNotice: (text: string, whenBack?: boolean) => void
|
||||
}
|
||||
|
||||
/** The edit mode: where the finding sits, then the draft form. */
|
||||
@@ -105,6 +105,35 @@ function ViewBody({ citedBy, mode, props }: BodyProps) {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The edit mode's status line (Rev 6), empty once the edit mode ends. A notice from the
|
||||
* reference dialog above (`whenBack`) waits for the focus to come back into this dialog:
|
||||
* under that modal this one is inert, and a line set there may never be spoken. A native
|
||||
* `focusin`: the browser gives the focus back inside React's commit (the dialog's
|
||||
* `close()` in an unmount), where React's own handlers do not run.
|
||||
*/
|
||||
function useNotice(editing: boolean) {
|
||||
const [notice, setNotice] = useState('')
|
||||
const waiting = useRef('')
|
||||
const place = useRef<HTMLElement>(null)
|
||||
if (!editing && notice !== '') setNotice('')
|
||||
useEffect(() => {
|
||||
const section = place.current
|
||||
const back = () => {
|
||||
const text = waiting.current
|
||||
waiting.current = ''
|
||||
if (text !== '') setNotice(text)
|
||||
}
|
||||
section?.addEventListener('focusin', back)
|
||||
return () => section?.removeEventListener('focusin', back)
|
||||
}, [])
|
||||
const say = (text: string, whenBack = false) => {
|
||||
if (whenBack) waiting.current = text
|
||||
else setNotice(text)
|
||||
}
|
||||
return { notice, place, say }
|
||||
}
|
||||
|
||||
/**
|
||||
* The place of the dialog's content, in both modes, then its status line: what a save
|
||||
* says, or what the edit mode's draft just did (never the editor's live region, Rev 6).
|
||||
@@ -112,8 +141,7 @@ function ViewBody({ citedBy, mode, props }: BodyProps) {
|
||||
function Body(body: Omit<BodyProps, 'onNotice'>) {
|
||||
const t = useT()
|
||||
const { mode } = body
|
||||
const [notice, setNotice] = useState('')
|
||||
if (!mode.editing && notice !== '') setNotice('')
|
||||
const line = useNotice(mode.editing)
|
||||
return (
|
||||
<section
|
||||
id="editor-citations-panel"
|
||||
@@ -121,14 +149,15 @@ function Body(body: Omit<BodyProps, 'onNotice'>) {
|
||||
data-place="citations"
|
||||
aria-labelledby={CITATIONS_TITLE_ID}
|
||||
className="space-y-3"
|
||||
ref={line.place}
|
||||
>
|
||||
{mode.editing ? (
|
||||
<EditBody {...body} onNotice={setNotice} />
|
||||
<EditBody {...body} onNotice={line.say} />
|
||||
) : (
|
||||
<ViewBody {...body} onNotice={setNotice} />
|
||||
<ViewBody {...body} onNotice={line.say} />
|
||||
)}
|
||||
<p role="status" className="text-sm text-[var(--color-ink-muted)]">
|
||||
{notice || (mode.saved ? t('editor.findings.saved') : '')}
|
||||
{line.notice || (mode.saved ? t('editor.findings.saved') : '')}
|
||||
</p>
|
||||
</section>
|
||||
)
|
||||
|
||||
@@ -21,7 +21,9 @@ import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import { useEditCommand } from '@/editor/useEditCommand'
|
||||
import type { Guards } from '@/editor/useEditorNavigation'
|
||||
import type { ReferenceMoves } from '@/editor/useReferenceMoves'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import CitationsDraftList, {
|
||||
CITE_ID,
|
||||
useCitableOptions,
|
||||
@@ -35,8 +37,11 @@ export interface CitationsEditFormProps {
|
||||
references: Pick<ReferenceMoves, 'open' | 'start'>
|
||||
/** Back to the view mode; `saved` is true when a command was written. */
|
||||
onDone: (saved: boolean) => void
|
||||
/** What the draft did that the dialog's status line says (a draft creation, Rev 6). */
|
||||
onNotice: (text: string) => void
|
||||
/**
|
||||
* What the draft did that the dialog's status line says (Rev 6); `whenBack`: said once
|
||||
* the focus is back in this dialog (a value from the reference dialog over it).
|
||||
*/
|
||||
onNotice: (text: string, whenBack?: boolean) => void
|
||||
/** « Abandonner » after a close request: the draft goes, so does the dialog. */
|
||||
onDiscardClose: () => void
|
||||
}
|
||||
@@ -67,14 +72,19 @@ function saveCommands(
|
||||
return citationCommands(draft, ids, update, () => newElementId('ref'))
|
||||
}
|
||||
|
||||
/** A refused save with references made in the draft: the line names them, still pending. */
|
||||
/**
|
||||
* A refused save with references made in the draft: the line names them, still pending,
|
||||
* as a list of the interface's locale.
|
||||
*/
|
||||
function savedError(
|
||||
t: Translate,
|
||||
locale: Locale,
|
||||
error: string | undefined,
|
||||
draft: CitationEdits,
|
||||
) {
|
||||
if (!error || draft.created.length === 0) return error
|
||||
const citations = draft.created.map((r) => r.citation).join(' ; ')
|
||||
const list = new Intl.ListFormat(locale, { type: 'conjunction' })
|
||||
const citations = list.format(draft.created.map((r) => r.citation))
|
||||
return `${error} ${t('editor.citations.pending_kept').replace('{citation}', citations)}`
|
||||
}
|
||||
|
||||
@@ -84,6 +94,7 @@ function savedError(
|
||||
*/
|
||||
function useCitationsSave(props: CitationsEditFormProps, form: Draft) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const { item, ids } = useCurrent(props.citedBy)
|
||||
const edit = useEditCommand()
|
||||
const save = () => {
|
||||
@@ -100,14 +111,14 @@ function useCitationsSave(props: CitationsEditFormProps, form: Draft) {
|
||||
return {
|
||||
shown: applyEdits(ids, form.draft),
|
||||
save,
|
||||
error: savedError(t, edit.fieldError('save'), form.draft),
|
||||
error: savedError(t, locale, edit.fieldError('save'), form.draft),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* « Nouvelle référence », or « Modifier la référence » of a pending one (Rev 6): the
|
||||
* reference dialog over this one writes nothing; its value joins the draft, only while
|
||||
* this form is still there, and the status line says so.
|
||||
* this form is still there, and the status line says so once that dialog is gone.
|
||||
*/
|
||||
function useCreation(props: CitationsEditFormProps, form: Draft) {
|
||||
const t = useT()
|
||||
@@ -123,9 +134,12 @@ function useCreation(props: CitationsEditFormProps, form: Draft) {
|
||||
const cite = (value: Reference) => {
|
||||
if (!alive.current) return
|
||||
const now = latest.current
|
||||
const replaced = now.draft.created.some((r) => r.id === value.id)
|
||||
now.set(putCreated(now.draft, value))
|
||||
const text = t('editor.citations.created_draft')
|
||||
props.onNotice(text.replace('{citation}', value.citation))
|
||||
const key = replaced
|
||||
? 'editor.citations.pending_updated'
|
||||
: 'editor.citations.created_draft'
|
||||
props.onNotice(t(key).replace('{citation}', value.citation), true)
|
||||
}
|
||||
return (opener: HTMLElement, initial?: Reference) =>
|
||||
props.references.start(opener, cite, initial)
|
||||
|
||||
@@ -49,7 +49,8 @@ function useReturnTarget(layer: ReferenceLayer) {
|
||||
return useCallback((): HTMLElement | null => {
|
||||
const shown = last.current
|
||||
if (!shown) return null
|
||||
// A creation over the citations dialog returns to its « Nouvelle référence ».
|
||||
// A draft creation over the citations dialog returns to its opener: « Nouvelle
|
||||
// référence », or the pending row's « Modifier la référence ».
|
||||
if ('create' in shown)
|
||||
return opener() ?? document.getElementById(ADD_BUTTON_ID)
|
||||
const wanted = JSON.stringify({ kind: 'reference', id: shown.edit })
|
||||
|
||||
Reference in new issue
Block a user