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 type { ElementKey } from '@faunosteo/core-wasm/types'
|
||||||
import ActionButton from '@/components/editor/fields/ActionButton'
|
import ActionButton from '@/components/editor/fields/ActionButton'
|
||||||
import { useItemMode } from '@/components/editor/findings/useItemMode'
|
import { useItemMode } from '@/components/editor/findings/useItemMode'
|
||||||
@@ -55,7 +55,7 @@ interface BodyProps {
|
|||||||
mode: Mode
|
mode: Mode
|
||||||
props: CitationsDialogProps
|
props: CitationsDialogProps
|
||||||
/** The edit mode's status line (a draft creation, a pending one removed; Rev 6). */
|
/** 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. */
|
/** 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
|
* 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).
|
* 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'>) {
|
function Body(body: Omit<BodyProps, 'onNotice'>) {
|
||||||
const t = useT()
|
const t = useT()
|
||||||
const { mode } = body
|
const { mode } = body
|
||||||
const [notice, setNotice] = useState('')
|
const line = useNotice(mode.editing)
|
||||||
if (!mode.editing && notice !== '') setNotice('')
|
|
||||||
return (
|
return (
|
||||||
<section
|
<section
|
||||||
id="editor-citations-panel"
|
id="editor-citations-panel"
|
||||||
@@ -121,14 +149,15 @@ function Body(body: Omit<BodyProps, 'onNotice'>) {
|
|||||||
data-place="citations"
|
data-place="citations"
|
||||||
aria-labelledby={CITATIONS_TITLE_ID}
|
aria-labelledby={CITATIONS_TITLE_ID}
|
||||||
className="space-y-3"
|
className="space-y-3"
|
||||||
|
ref={line.place}
|
||||||
>
|
>
|
||||||
{mode.editing ? (
|
{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)]">
|
<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>
|
</p>
|
||||||
</section>
|
</section>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -21,7 +21,9 @@ import { useSheetDocument } from '@/editor/sheetDocument'
|
|||||||
import { useEditCommand } from '@/editor/useEditCommand'
|
import { useEditCommand } from '@/editor/useEditCommand'
|
||||||
import type { Guards } from '@/editor/useEditorNavigation'
|
import type { Guards } from '@/editor/useEditorNavigation'
|
||||||
import type { ReferenceMoves } from '@/editor/useReferenceMoves'
|
import type { ReferenceMoves } from '@/editor/useReferenceMoves'
|
||||||
|
import type { Locale } from '@/i18n/types'
|
||||||
import { useT } from '@/i18n/useT'
|
import { useT } from '@/i18n/useT'
|
||||||
|
import { useLocaleStore } from '@/state/localeStore'
|
||||||
import CitationsDraftList, {
|
import CitationsDraftList, {
|
||||||
CITE_ID,
|
CITE_ID,
|
||||||
useCitableOptions,
|
useCitableOptions,
|
||||||
@@ -35,8 +37,11 @@ export interface CitationsEditFormProps {
|
|||||||
references: Pick<ReferenceMoves, 'open' | 'start'>
|
references: Pick<ReferenceMoves, 'open' | 'start'>
|
||||||
/** Back to the view mode; `saved` is true when a command was written. */
|
/** Back to the view mode; `saved` is true when a command was written. */
|
||||||
onDone: (saved: boolean) => void
|
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. */
|
/** « Abandonner » after a close request: the draft goes, so does the dialog. */
|
||||||
onDiscardClose: () => void
|
onDiscardClose: () => void
|
||||||
}
|
}
|
||||||
@@ -67,14 +72,19 @@ function saveCommands(
|
|||||||
return citationCommands(draft, ids, update, () => newElementId('ref'))
|
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(
|
function savedError(
|
||||||
t: Translate,
|
t: Translate,
|
||||||
|
locale: Locale,
|
||||||
error: string | undefined,
|
error: string | undefined,
|
||||||
draft: CitationEdits,
|
draft: CitationEdits,
|
||||||
) {
|
) {
|
||||||
if (!error || draft.created.length === 0) return error
|
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)}`
|
return `${error} ${t('editor.citations.pending_kept').replace('{citation}', citations)}`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,6 +94,7 @@ function savedError(
|
|||||||
*/
|
*/
|
||||||
function useCitationsSave(props: CitationsEditFormProps, form: Draft) {
|
function useCitationsSave(props: CitationsEditFormProps, form: Draft) {
|
||||||
const t = useT()
|
const t = useT()
|
||||||
|
const locale = useLocaleStore((s) => s.locale)
|
||||||
const { item, ids } = useCurrent(props.citedBy)
|
const { item, ids } = useCurrent(props.citedBy)
|
||||||
const edit = useEditCommand()
|
const edit = useEditCommand()
|
||||||
const save = () => {
|
const save = () => {
|
||||||
@@ -100,14 +111,14 @@ function useCitationsSave(props: CitationsEditFormProps, form: Draft) {
|
|||||||
return {
|
return {
|
||||||
shown: applyEdits(ids, form.draft),
|
shown: applyEdits(ids, form.draft),
|
||||||
save,
|
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
|
* « 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
|
* 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) {
|
function useCreation(props: CitationsEditFormProps, form: Draft) {
|
||||||
const t = useT()
|
const t = useT()
|
||||||
@@ -123,9 +134,12 @@ function useCreation(props: CitationsEditFormProps, form: Draft) {
|
|||||||
const cite = (value: Reference) => {
|
const cite = (value: Reference) => {
|
||||||
if (!alive.current) return
|
if (!alive.current) return
|
||||||
const now = latest.current
|
const now = latest.current
|
||||||
|
const replaced = now.draft.created.some((r) => r.id === value.id)
|
||||||
now.set(putCreated(now.draft, value))
|
now.set(putCreated(now.draft, value))
|
||||||
const text = t('editor.citations.created_draft')
|
const key = replaced
|
||||||
props.onNotice(text.replace('{citation}', value.citation))
|
? 'editor.citations.pending_updated'
|
||||||
|
: 'editor.citations.created_draft'
|
||||||
|
props.onNotice(t(key).replace('{citation}', value.citation), true)
|
||||||
}
|
}
|
||||||
return (opener: HTMLElement, initial?: Reference) =>
|
return (opener: HTMLElement, initial?: Reference) =>
|
||||||
props.references.start(opener, cite, initial)
|
props.references.start(opener, cite, initial)
|
||||||
|
|||||||
@@ -49,7 +49,8 @@ function useReturnTarget(layer: ReferenceLayer) {
|
|||||||
return useCallback((): HTMLElement | null => {
|
return useCallback((): HTMLElement | null => {
|
||||||
const shown = last.current
|
const shown = last.current
|
||||||
if (!shown) return null
|
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)
|
if ('create' in shown)
|
||||||
return opener() ?? document.getElementById(ADD_BUTTON_ID)
|
return opener() ?? document.getElementById(ADD_BUTTON_ID)
|
||||||
const wanted = JSON.stringify({ kind: 'reference', id: shown.edit })
|
const wanted = JSON.stringify({ kind: 'reference', id: shown.edit })
|
||||||
|
|||||||
@@ -303,7 +303,8 @@ function openReference(state: NavigationState, id: string): NavigationState {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* The creation form of a reference: over the References tab, or over the citations dialog
|
* The creation form of a reference: over the References tab, or over the citations dialog
|
||||||
* (its edit mode cites the new reference, Rev 5), which keeps its tab.
|
* (a draft creation of its edit mode: the value joins the draft, nothing is written,
|
||||||
|
* Rev 6), which keeps its tab.
|
||||||
*/
|
*/
|
||||||
function createReference(state: NavigationState): NavigationState {
|
function createReference(state: NavigationState): NavigationState {
|
||||||
const reference = { create: true } as const
|
const reference = { create: true } as const
|
||||||
|
|||||||
@@ -1239,6 +1239,7 @@ export const en: Dict = {
|
|||||||
'editor.citations.pending_choice': 'Reference chosen but not cited yet: choose « Cite » or go back to the empty choice.',
|
'editor.citations.pending_choice': 'Reference chosen but not cited yet: choose « Cite » or go back to the empty choice.',
|
||||||
'editor.citations.created_draft': 'New reference cited in the draft: {citation}. It will be saved with the citations.',
|
'editor.citations.created_draft': 'New reference cited in the draft: {citation}. It will be saved with the citations.',
|
||||||
'editor.citations.pending_removed': 'New reference removed: {citation}',
|
'editor.citations.pending_removed': 'New reference removed: {citation}',
|
||||||
|
'editor.citations.pending_updated': 'New reference edited: {citation}',
|
||||||
'editor.citations.pending': 'New',
|
'editor.citations.pending': 'New',
|
||||||
'editor.citations.pending_kept': 'New reference not saved, still in the draft: {citation}',
|
'editor.citations.pending_kept': 'New reference not saved, still in the draft: {citation}',
|
||||||
'editor.references.panel_count_one': '{count} reference cited by this finding',
|
'editor.references.panel_count_one': '{count} reference cited by this finding',
|
||||||
|
|||||||
@@ -1270,6 +1270,7 @@ export const fr: Dict = {
|
|||||||
'editor.citations.pending_choice': 'Référence choisie mais pas encore citée : choisissez « Citer » ou revenez au choix vide.',
|
'editor.citations.pending_choice': 'Référence choisie mais pas encore citée : choisissez « Citer » ou revenez au choix vide.',
|
||||||
'editor.citations.created_draft': 'Nouvelle référence citée dans le brouillon : {citation}. Elle sera enregistrée avec les citations.',
|
'editor.citations.created_draft': 'Nouvelle référence citée dans le brouillon : {citation}. Elle sera enregistrée avec les citations.',
|
||||||
'editor.citations.pending_removed': 'Nouvelle référence retirée : {citation}',
|
'editor.citations.pending_removed': 'Nouvelle référence retirée : {citation}',
|
||||||
|
'editor.citations.pending_updated': 'Nouvelle référence modifiée : {citation}',
|
||||||
'editor.citations.pending': 'Nouvelle',
|
'editor.citations.pending': 'Nouvelle',
|
||||||
'editor.citations.pending_kept': 'Nouvelle référence non enregistrée, toujours dans le brouillon : {citation}',
|
'editor.citations.pending_kept': 'Nouvelle référence non enregistrée, toujours dans le brouillon : {citation}',
|
||||||
'editor.references.panel_count_one': '{count} référence citée par ce constat',
|
'editor.references.panel_count_one': '{count} référence citée par ce constat',
|
||||||
|
|||||||
@@ -4,12 +4,14 @@ import {
|
|||||||
act,
|
act,
|
||||||
cleanup,
|
cleanup,
|
||||||
fireEvent,
|
fireEvent,
|
||||||
|
render,
|
||||||
screen,
|
screen,
|
||||||
waitFor,
|
waitFor,
|
||||||
within,
|
within,
|
||||||
} from '@testing-library/react'
|
} from '@testing-library/react'
|
||||||
import userEvent from '@testing-library/user-event'
|
import userEvent from '@testing-library/user-event'
|
||||||
import type { Location, NavigateFunction } from 'react-router-dom'
|
import type { Location, NavigateFunction } from 'react-router-dom'
|
||||||
|
import CitationsEditForm from '@/components/editor/references/CitationsEditForm'
|
||||||
import { flushAfterModals } from '@/components/ui/afterModalsClosed'
|
import { flushAfterModals } from '@/components/ui/afterModalsClosed'
|
||||||
import { ENTRY_KEY, type HistoryEntry } from '@/editor/history/entry'
|
import { ENTRY_KEY, type HistoryEntry } from '@/editor/history/entry'
|
||||||
import {
|
import {
|
||||||
@@ -21,6 +23,7 @@ import { useLocaleStore } from '@/state/localeStore'
|
|||||||
import { edit, insert } from '../helpers/editorFixtures'
|
import { edit, insert } from '../helpers/editorFixtures'
|
||||||
import {
|
import {
|
||||||
baseText,
|
baseText,
|
||||||
|
openReady,
|
||||||
resetStore,
|
resetStore,
|
||||||
serverHas,
|
serverHas,
|
||||||
SHEET,
|
SHEET,
|
||||||
@@ -648,6 +651,9 @@ describe('the reference dialog over the edit mode', () => {
|
|||||||
await waitFor(() => expect(referenceDialog()).toBeUndefined())
|
await waitFor(() => expect(referenceDialog()).toBeUndefined())
|
||||||
expect(rows()).toEqual(['ref-a', 'ref-c', pendingId])
|
expect(rows()).toEqual(['ref-a', 'ref-c', pendingId])
|
||||||
expect(within(pendingRow()).getByText('Neuf 2')).toBeVisible()
|
expect(within(pendingRow()).getByText('Neuf 2')).toBeVisible()
|
||||||
|
await waitFor(() =>
|
||||||
|
expect(status()).toBe('Nouvelle référence modifiée : Neuf 2'),
|
||||||
|
)
|
||||||
expect(library()).not.toContain('Neuf 2')
|
expect(library()).not.toContain('Neuf 2')
|
||||||
await user.click(button('Enregistrer'))
|
await user.click(button('Enregistrer'))
|
||||||
expect(library().filter((c) => c.startsWith('Neuf'))).toEqual(['Neuf 2'])
|
expect(library().filter((c) => c.startsWith('Neuf'))).toEqual(['Neuf 2'])
|
||||||
@@ -730,6 +736,92 @@ describe('the reference dialog over the edit mode', () => {
|
|||||||
expect(library()).not.toContain('Neuf')
|
expect(library()).not.toContain('Neuf')
|
||||||
expect(undos()).toBe(0)
|
expect(undos()).toBe(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('a refused save names several pending references as a list of the locale', async () => {
|
||||||
|
const user = await mount()
|
||||||
|
await editCitations(user)
|
||||||
|
await user.click(button('Nouvelle référence'))
|
||||||
|
await createPending(user, 'Neuf')
|
||||||
|
await user.click(button('Nouvelle référence'))
|
||||||
|
await createPending(user, 'Vieux')
|
||||||
|
minted.push('ref-a', 'ref-c', 'ref-a', 'ref-c')
|
||||||
|
await user.click(button('Enregistrer'))
|
||||||
|
const alert = inCitations().getByRole('alert')
|
||||||
|
expect(alert).toHaveTextContent('Neuf et Vieux')
|
||||||
|
expect(alert).not.toHaveTextContent(';')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('the creation notice is set once the reference dialog is gone, never under it', async () => {
|
||||||
|
const user = await mount()
|
||||||
|
await editCitations(user)
|
||||||
|
await user.click(button('Nouvelle référence'))
|
||||||
|
await waitFor(() => expect(referenceDialog()).toBeDefined())
|
||||||
|
await user.selectOptions(inReference().getByLabelText('Type'), 'book')
|
||||||
|
await user.type(inReference().getByLabelText('Citation'), 'Neuf')
|
||||||
|
const seen: string[] = []
|
||||||
|
const observer = new MutationObserver(() => {
|
||||||
|
if (referenceDialog()) seen.push(status() ?? '')
|
||||||
|
})
|
||||||
|
observer.observe(document.body, {
|
||||||
|
subtree: true,
|
||||||
|
childList: true,
|
||||||
|
characterData: true,
|
||||||
|
})
|
||||||
|
await user.click(
|
||||||
|
inReference().getByRole('button', { name: 'Ajouter la référence' }),
|
||||||
|
)
|
||||||
|
await waitFor(() => expect(referenceDialog()).toBeUndefined())
|
||||||
|
await waitFor(() => expect(status()).toContain('Neuf'))
|
||||||
|
observer.disconnect()
|
||||||
|
expect(seen.filter((text) => text !== '')).toEqual([])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('a save of the edit form when its element is unreadable', () => {
|
||||||
|
it('is a refusal on the save line: the draft stays, nothing written', async () => {
|
||||||
|
await openReady({ text: sheetText() })
|
||||||
|
const pending = {
|
||||||
|
id: 'ref-p',
|
||||||
|
type: 'book',
|
||||||
|
citation: 'Neuf',
|
||||||
|
doi: null,
|
||||||
|
url: null,
|
||||||
|
} as const
|
||||||
|
const start = vi.fn(
|
||||||
|
(_opener: unknown, cite?: (value: typeof pending) => void) =>
|
||||||
|
cite?.(pending),
|
||||||
|
)
|
||||||
|
const guards = {
|
||||||
|
register: vi.fn(),
|
||||||
|
ask: () => ({ allowed: true }),
|
||||||
|
askAll: () => ({ allowed: true }),
|
||||||
|
holdLeave: vi.fn(),
|
||||||
|
takeLeave: () => null,
|
||||||
|
}
|
||||||
|
const onDone = vi.fn()
|
||||||
|
const user = userEvent.setup()
|
||||||
|
render(
|
||||||
|
<CitationsEditForm
|
||||||
|
citedBy={{ ...nodeKey, path: ['fcat-a', 'fnode-gone'] }}
|
||||||
|
readOnly={false}
|
||||||
|
guards={guards}
|
||||||
|
references={{ open: vi.fn(), start }}
|
||||||
|
onDone={onDone}
|
||||||
|
onNotice={vi.fn()}
|
||||||
|
onDiscardClose={vi.fn()}
|
||||||
|
/>,
|
||||||
|
)
|
||||||
|
await user.click(screen.getByRole('button', { name: 'Nouvelle référence' }))
|
||||||
|
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
||||||
|
const alert = screen.getByRole('alert')
|
||||||
|
expect(alert).toHaveTextContent("Ce constat n'existe plus")
|
||||||
|
expect(alert).toHaveTextContent(
|
||||||
|
'Nouvelle référence non enregistrée, toujours dans le brouillon : Neuf',
|
||||||
|
)
|
||||||
|
expect(onDone).not.toHaveBeenCalled()
|
||||||
|
expect(screen.getByText('Neuf')).toBeVisible()
|
||||||
|
expect(undos()).toBe(0)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('a finding draft and a references draft on the same element', () => {
|
describe('a finding draft and a references draft on the same element', () => {
|
||||||
|
|||||||
Reference in new issue
Block a user