feat(editor): edit mode of the references dialog
« Modifier » turns the references dialog into a draft of adds and removes, saved as one update on the current citations. A new layer, citations, guards it between the finding and the reference dialogs: close, Back, POP refusal and the leave cascade ask it. « Modifier la référence » and « Nouvelle référence » stack the reference dialog over it; a creation it started joins its draft.
This commit is contained in:
1 parent
d9dbcf1609
commit
3dc5820e3a
29 files changed
+1489
-243
No files matched your search
@@ -118,6 +118,8 @@ function Places({ nav, readOnly, reloadSheet }: PlacesProps) {
|
||||
onShowAll={() => cited && nav.showAllReferences(cited)}
|
||||
navigate={nav.navigate}
|
||||
returnFocus={nav.citationsTrigger}
|
||||
guards={nav.guards}
|
||||
references={nav.references}
|
||||
/>
|
||||
<ReferenceDialog layer={references} readOnly={readOnly} />
|
||||
</>
|
||||
|
||||
@@ -10,10 +10,19 @@ import { useT } from '@/i18n/useT'
|
||||
const BOX =
|
||||
'space-y-2 rounded-lg border border-[var(--color-border)] bg-[var(--color-surface-strong)] p-3'
|
||||
|
||||
/** True when no open dialog lies over `host`: each open one is `host` or holds it. */
|
||||
/**
|
||||
* True when no open dialog lies over `host`: each open one is `host`, holds it, or comes
|
||||
* before it in the page (the editor's layers are stacked in tree order: the references
|
||||
* dialog after the finding dialog, the reference dialog after both).
|
||||
*/
|
||||
const onTop = (host: Element) =>
|
||||
[...document.querySelectorAll('dialog[open]')].every(
|
||||
(dialog) => dialog === host || dialog.contains(host),
|
||||
(dialog) =>
|
||||
dialog === host ||
|
||||
dialog.contains(host) ||
|
||||
!!(
|
||||
host.compareDocumentPosition(dialog) & Node.DOCUMENT_POSITION_PRECEDING
|
||||
),
|
||||
)
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import Icon from '@/components/ui/Icon'
|
||||
import ActionButton from '@/components/editor/fields/ActionButton'
|
||||
import type { FindingItem } from '@/editor/findings/tree'
|
||||
import { usePlural } from '@/editor/plural'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
interface CitationsProps {
|
||||
@@ -38,42 +36,6 @@ function CitationsTrigger({
|
||||
)
|
||||
}
|
||||
|
||||
/** A list of cited references; removable only where `remove` is given. */
|
||||
export function CitationList({
|
||||
ids,
|
||||
readOnly,
|
||||
remove,
|
||||
}: {
|
||||
ids: readonly string[]
|
||||
readOnly: boolean
|
||||
remove?: (id: string) => void
|
||||
}) {
|
||||
const t = useT()
|
||||
const doc = useSheetDocument()
|
||||
const citation = (id: string) =>
|
||||
doc?.references.find((r) => r.id === id)?.citation ?? id
|
||||
return (
|
||||
<ul className="space-y-1">
|
||||
{ids.map((id) => (
|
||||
<li key={id} className="flex items-center gap-2 text-sm">
|
||||
<span className="min-w-0 flex-1">{citation(id)}</span>
|
||||
{remove && (
|
||||
<ActionButton
|
||||
readOnly={readOnly}
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
aria-label={`${t('editor.action.remove')} ${citation(id)}`}
|
||||
onClick={() => remove(id)}
|
||||
>
|
||||
{t('editor.action.remove')}
|
||||
</ActionButton>
|
||||
)}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* « Références citées » and « X références » of a family or a node, the same in the view
|
||||
* and the edit mode (Rev 5): the count is the document's, the citations are read and
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import FindingCreateForm, {
|
||||
type PendingFinding,
|
||||
} from '@/components/editor/findings/FindingCreateForm'
|
||||
import FindingEditForm from '@/components/editor/findings/FindingEditForm'
|
||||
import FindingView from '@/components/editor/findings/FindingView'
|
||||
import { useItemMode } from '@/components/editor/findings/useItemMode'
|
||||
import Dialog from '@/components/ui/Dialog'
|
||||
import { usePlaceVisible } from '@/lib/placeVisibility'
|
||||
import type { FindingItem, FindingKind } from '@/editor/findings/tree'
|
||||
@@ -33,38 +33,11 @@ interface ItemBodyProps {
|
||||
|
||||
const EDIT_BUTTON_ID = 'finding-edit-button'
|
||||
|
||||
/** After the form closes its button is gone: « Modifier » takes the focus back. */
|
||||
function useFocusEditButton(editing: boolean) {
|
||||
const wanted = useRef(false)
|
||||
useEffect(() => {
|
||||
if (editing || !wanted.current) return
|
||||
wanted.current = false
|
||||
document.getElementById(EDIT_BUTTON_ID)?.focus()
|
||||
}, [editing])
|
||||
return () => {
|
||||
wanted.current = true
|
||||
}
|
||||
}
|
||||
|
||||
/** The local mode of the one dialog: view or edit, and whether a save was just made. */
|
||||
function useItemMode() {
|
||||
const [mode, setMode] = useState({ editing: false, saved: false })
|
||||
const returnToEdit = useFocusEditButton(mode.editing)
|
||||
return {
|
||||
...mode,
|
||||
startEdit: () => setMode({ editing: true, saved: false }),
|
||||
done: (saved: boolean) => {
|
||||
returnToEdit()
|
||||
setMode({ editing: false, saved })
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/** View mode, or the edit form: a local mode of the one dialog, no history entry. */
|
||||
function ItemBody(props: ItemBodyProps) {
|
||||
const { layer } = props
|
||||
const t = useT()
|
||||
const mode = useItemMode()
|
||||
const mode = useItemMode(EDIT_BUTTON_ID)
|
||||
const showReferences = (opener: HTMLElement) =>
|
||||
layer.openCitations(props.item.key, opener)
|
||||
return (
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
import SelectField from '@/components/editor/fields/SelectField'
|
||||
import { CitationList } from '@/components/editor/findings/FindingCitations'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
interface Props {
|
||||
/** The draft's cited ids: what the form shows. */
|
||||
ids: readonly string[]
|
||||
readOnly: boolean
|
||||
onChange: (ids: string[]) => void
|
||||
}
|
||||
|
||||
/** The references the draft does not cite yet, after the « choose » placeholder. */
|
||||
function useCitableOptions(ids: readonly string[]) {
|
||||
const t = useT()
|
||||
const doc = useSheetDocument()
|
||||
const options = (doc?.references ?? [])
|
||||
.filter((r) => !ids.includes(r.id))
|
||||
.map((r) => ({ value: r.id, label: r.citation }))
|
||||
return [{ value: '', label: t('editor.field.choose') }, ...options]
|
||||
}
|
||||
|
||||
/**
|
||||
* The cited references of a draft: the list, each « Retirer », and « Citer une référence ».
|
||||
* Nothing is written before « Enregistrer ». Mounted by no form since Rev 5 Task 11: the
|
||||
* references dialog's edit mode (Task 12) takes it over.
|
||||
*/
|
||||
export default function FindingEditCitations(props: Props) {
|
||||
const { ids, readOnly, onChange } = props
|
||||
const t = useT()
|
||||
const options = useCitableOptions(ids)
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
{ids.length === 0 ? (
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('editor.findings.references_none')}
|
||||
</p>
|
||||
) : (
|
||||
<CitationList
|
||||
ids={ids}
|
||||
readOnly={readOnly}
|
||||
remove={(id) => onChange(ids.filter((other) => other !== id))}
|
||||
/>
|
||||
)}
|
||||
{!readOnly && (
|
||||
<SelectField
|
||||
id="finding-add-reference"
|
||||
label={t('editor.findings.reference_add')}
|
||||
value=""
|
||||
options={options}
|
||||
onCommit={(id) => id !== '' && onChange([...ids, id])}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
|
||||
/** After the form closes its button is gone: « Modifier » (`buttonId`) takes the focus back. */
|
||||
function useFocusEditButton(editing: boolean, buttonId: string) {
|
||||
const wanted = useRef(false)
|
||||
useEffect(() => {
|
||||
if (editing || !wanted.current) return
|
||||
wanted.current = false
|
||||
document.getElementById(buttonId)?.focus()
|
||||
}, [editing, buttonId])
|
||||
return () => {
|
||||
wanted.current = true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The local mode of a dialog (the finding dialog, the references dialog): view or edit,
|
||||
* and whether a save was just made. No history entry. `buttonId` is its « Modifier ».
|
||||
*/
|
||||
export function useItemMode(buttonId: string) {
|
||||
const [mode, setMode] = useState({ editing: false, saved: false })
|
||||
const returnToEdit = useFocusEditButton(mode.editing, buttonId)
|
||||
return {
|
||||
...mode,
|
||||
startEdit: () => setMode({ editing: true, saved: false }),
|
||||
done: (saved: boolean) => {
|
||||
returnToEdit()
|
||||
setMode({ editing: false, saved })
|
||||
},
|
||||
/** Back to the view mode, nothing said: the dialog shows another element. */
|
||||
reset: () => setMode({ editing: false, saved: false }),
|
||||
}
|
||||
}
|
||||
@@ -1,49 +1,149 @@
|
||||
import { 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'
|
||||
import Dialog from '@/components/ui/Dialog'
|
||||
import { useEditorReadOnly } from '@/editor/editingContext'
|
||||
import type { EditorTarget } from '@/editor/navigation'
|
||||
import { findingPath } from '@/editor/references/panel'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import type { Guards } from '@/editor/useEditorNavigation'
|
||||
import type { ReferenceMoves } from '@/editor/useReferenceMoves'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import CitationsPanel from './CitationsPanel'
|
||||
import CitationsEditForm from './CitationsEditForm'
|
||||
import CitationsPanel, { FindingPath } from './CitationsPanel'
|
||||
import { CITATIONS_TITLE_ID } from './useCitationsPanel'
|
||||
|
||||
export interface CitationsDialogProps {
|
||||
/** The family or finding node whose citations it shows; null while it is closed. */
|
||||
citedBy: ElementKey | null
|
||||
/** A user close (✕, Escape, the backdrop). */
|
||||
onClose: () => boolean
|
||||
/** A close: a user one (✕, Escape, the backdrop) is refused by a draft; forced goes. */
|
||||
onClose: (forced?: boolean) => boolean
|
||||
/** « Tout afficher dans l'onglet Références ». */
|
||||
onShowAll: () => void
|
||||
/** « Aller à » a citing finding or a reference. */
|
||||
navigate: (target: EditorTarget) => void
|
||||
/** « X références » of the finding dialog under it, where the focus returns. */
|
||||
returnFocus: () => HTMLElement | null
|
||||
guards: Guards
|
||||
/** The reference dialog over this one (the edit mode's edit and creation). */
|
||||
references: Pick<ReferenceMoves, 'open' | 'start'>
|
||||
}
|
||||
|
||||
/** The dialog's « Modifier »: the focus comes back to it after the edit mode. */
|
||||
const EDIT_BUTTON_ID = 'citations-edit-button'
|
||||
|
||||
/** The local mode, back to the view for each element the dialog shows (no history entry). */
|
||||
function useCitationsMode(citedBy: ElementKey | null) {
|
||||
const shownFor = citedBy && JSON.stringify(citedBy)
|
||||
const mode = useItemMode(EDIT_BUTTON_ID)
|
||||
const [modeFor, setModeFor] = useState(shownFor)
|
||||
if (modeFor !== shownFor) {
|
||||
setModeFor(shownFor)
|
||||
mode.reset()
|
||||
}
|
||||
return { ...mode, shownFor }
|
||||
}
|
||||
|
||||
type Mode = ReturnType<typeof useCitationsMode>
|
||||
|
||||
interface BodyProps {
|
||||
citedBy: ElementKey
|
||||
mode: Mode
|
||||
props: CitationsDialogProps
|
||||
}
|
||||
|
||||
/** The edit mode: where the finding sits, then the draft form. */
|
||||
function EditBody({ citedBy, mode, props }: BodyProps) {
|
||||
const doc = useSheetDocument()
|
||||
const readOnly = useEditorReadOnly()
|
||||
return (
|
||||
<>
|
||||
<FindingPath path={doc ? findingPath(doc, citedBy) : []} />
|
||||
<CitationsEditForm
|
||||
key={mode.shownFor}
|
||||
citedBy={citedBy}
|
||||
readOnly={readOnly}
|
||||
guards={props.guards}
|
||||
references={props.references}
|
||||
onDone={mode.done}
|
||||
onDiscardClose={() => props.onClose(true)}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
/** The view mode: the cited references, then « Modifier » (edit tier only). */
|
||||
function ViewBody({ citedBy, mode, props }: BodyProps) {
|
||||
const t = useT()
|
||||
const readOnly = useEditorReadOnly()
|
||||
return (
|
||||
<>
|
||||
<CitationsPanel
|
||||
citedBy={citedBy}
|
||||
onShowAll={props.onShowAll}
|
||||
navigate={props.navigate}
|
||||
/>
|
||||
<div className="flex justify-end">
|
||||
<ActionButton
|
||||
id={EDIT_BUTTON_ID}
|
||||
readOnly={readOnly}
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={mode.startEdit}
|
||||
>
|
||||
{t('editor.citations.edit')}
|
||||
</ActionButton>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
/** The place of the dialog's content, in both modes, then what a save says. */
|
||||
function Body(body: BodyProps) {
|
||||
const t = useT()
|
||||
const { mode } = body
|
||||
return (
|
||||
<section
|
||||
id="editor-citations-panel"
|
||||
data-testid="editor-citations-panel"
|
||||
data-place="citations"
|
||||
aria-labelledby={CITATIONS_TITLE_ID}
|
||||
className="space-y-3"
|
||||
>
|
||||
{mode.editing ? <EditBody {...body} /> : <ViewBody {...body} />}
|
||||
<p role="status" className="text-sm text-[var(--color-ink-muted)]">
|
||||
{mode.saved ? t('editor.findings.saved') : ''}
|
||||
</p>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The references dialog of a family or a finding node (Rev 5): a modal layer over the
|
||||
* finding dialog. The host sits after the tab body in the tree, so it is shown after the
|
||||
* finding dialog and lies on top of it; Escape, ✕ and the backdrop close it only. The
|
||||
* view mode reads the cited references.
|
||||
* view mode reads the cited references; its « Modifier » turns it into a form (local
|
||||
* mode, no history entry) whose draft the layer `citations` guards.
|
||||
*/
|
||||
export default function CitationsDialog(props: CitationsDialogProps) {
|
||||
const { citedBy, onClose } = props
|
||||
const t = useT()
|
||||
const mode = useCitationsMode(citedBy)
|
||||
const title = mode.editing
|
||||
? 'editor.citations.edit_title'
|
||||
: 'editor.references.dialog_title'
|
||||
return (
|
||||
<Dialog
|
||||
open={citedBy !== null}
|
||||
layer
|
||||
size="lg"
|
||||
title={t('editor.references.dialog_title')}
|
||||
title={t(title)}
|
||||
titleId={CITATIONS_TITLE_ID}
|
||||
returnFocus={props.returnFocus}
|
||||
onClose={() => (onClose() ? undefined : 'kept')}
|
||||
>
|
||||
{citedBy && (
|
||||
<CitationsPanel
|
||||
citedBy={citedBy}
|
||||
onShowAll={props.onShowAll}
|
||||
navigate={props.navigate}
|
||||
/>
|
||||
)}
|
||||
{citedBy && <Body citedBy={citedBy} mode={mode} props={props} />}
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
import { useEffect, useRef } from 'react'
|
||||
import SelectField from '@/components/editor/fields/SelectField'
|
||||
import Button from '@/components/ui/Button'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
export interface DraftListProps {
|
||||
/** The draft's cited ids, in the order they will be saved. */
|
||||
ids: readonly string[]
|
||||
readOnly: boolean
|
||||
onAdd: (id: string) => void
|
||||
onRemove: (id: string) => void
|
||||
/** « Modifier la référence »: the reference dialog over this one. */
|
||||
onEditReference: (id: string, opener: HTMLElement) => void
|
||||
/** « Nouvelle référence »: the creation dialog over this one. */
|
||||
onNew: (opener: HTMLElement) => void
|
||||
}
|
||||
|
||||
/** The add choice: its id, so the focus can land on it once the last row is removed. */
|
||||
const ADD_ID = 'citations-add-reference'
|
||||
|
||||
/** The references the draft does not cite yet, after the « choose » placeholder. */
|
||||
function useCitableOptions(ids: readonly string[]) {
|
||||
const t = useT()
|
||||
const doc = useSheetDocument()
|
||||
const options = (doc?.references ?? [])
|
||||
.filter((r) => !ids.includes(r.id))
|
||||
.map((r) => ({ value: r.id, label: r.citation }))
|
||||
return [{ value: '', label: t('editor.field.choose') }, ...options]
|
||||
}
|
||||
|
||||
/**
|
||||
* A removed row takes its buttons with it: the focus goes to the row now at its place,
|
||||
* else the one before, else the add choice.
|
||||
*/
|
||||
function useRemovalFocus(ids: readonly string[]) {
|
||||
const list = useRef<HTMLUListElement>(null)
|
||||
const removedAt = useRef<number | null>(null)
|
||||
useEffect(() => {
|
||||
const at = removedAt.current
|
||||
if (at === null) return
|
||||
removedAt.current = null
|
||||
const rows = [...(list.current?.children ?? [])]
|
||||
const row = rows[at] ?? rows[at - 1]
|
||||
const target =
|
||||
row?.querySelector('button') ?? document.getElementById(ADD_ID)
|
||||
target?.focus()
|
||||
}, [ids])
|
||||
return { list, removing: (at: number) => (removedAt.current = at) }
|
||||
}
|
||||
|
||||
interface RowProps {
|
||||
id: string
|
||||
props: DraftListProps
|
||||
onRemove: () => void
|
||||
}
|
||||
|
||||
/** One cited id: its citation (or « Référence introuvable »), its edit and its removal. */
|
||||
function DraftRow({ id, props, onRemove }: RowProps) {
|
||||
const t = useT()
|
||||
const doc = useSheetDocument()
|
||||
const citation = doc?.references.find((r) => r.id === id)?.citation
|
||||
const shown = citation ?? `${t('editor.references.missing')} ${id}`
|
||||
return (
|
||||
<li data-citation-row={id} className="flex flex-wrap items-center gap-2">
|
||||
<span className="min-w-0 flex-1 text-sm">{shown}</span>
|
||||
{citation !== undefined && (
|
||||
<Button
|
||||
type="button"
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
disabled={props.readOnly}
|
||||
aria-label={`${t('editor.citations.edit_reference')} ${citation}`}
|
||||
onClick={(event) => props.onEditReference(id, event.currentTarget)}
|
||||
>
|
||||
{t('editor.citations.edit_reference')}
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
disabled={props.readOnly}
|
||||
aria-label={`${t('editor.action.remove')} ${citation ?? id}`}
|
||||
onClick={onRemove}
|
||||
>
|
||||
{t('editor.action.remove')}
|
||||
</Button>
|
||||
</li>
|
||||
)
|
||||
}
|
||||
|
||||
/** « Citer une référence » among those not cited yet, and « Nouvelle référence ». */
|
||||
function AddRow({ props }: { props: DraftListProps }) {
|
||||
const t = useT()
|
||||
const options = useCitableOptions(props.ids)
|
||||
return (
|
||||
<div className="flex flex-wrap items-end gap-2">
|
||||
{!props.readOnly && (
|
||||
<div className="min-w-0 flex-1">
|
||||
<SelectField
|
||||
id={ADD_ID}
|
||||
label={t('editor.findings.reference_add')}
|
||||
value=""
|
||||
options={options}
|
||||
onCommit={(id) => id !== '' && props.onAdd(id)}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<Button
|
||||
type="button"
|
||||
variant="secondary"
|
||||
disabled={props.readOnly}
|
||||
onClick={(event) => props.onNew(event.currentTarget)}
|
||||
>
|
||||
{t('editor.citations.new_reference')}
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The cited references of the draft (the references dialog's edit mode, Rev 5): each with
|
||||
* « Modifier la référence » and « Retirer » (a dangling id too), then the ways to cite one.
|
||||
* Nothing is written before « Enregistrer ». Read only: every control off.
|
||||
*/
|
||||
export default function CitationsDraftList(props: DraftListProps) {
|
||||
const { ids } = props
|
||||
const t = useT()
|
||||
const focus = useRemovalFocus(ids)
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
{ids.length === 0 && (
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('editor.findings.references_none')}
|
||||
</p>
|
||||
)}
|
||||
<ul ref={focus.list} className="space-y-2">
|
||||
{ids.map((id, at) => (
|
||||
<DraftRow
|
||||
key={id}
|
||||
id={id}
|
||||
props={props}
|
||||
onRemove={() => {
|
||||
focus.removing(at)
|
||||
props.onRemove(id)
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
</ul>
|
||||
<AddRow props={props} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
import { useEffect, useRef } from 'react'
|
||||
import type { ElementKey } from '@faunosteo/core-wasm/types'
|
||||
import { FormActions } from '@/components/editor/findings/DiscardConfirm'
|
||||
import ReadOnlyNotice from '@/components/editor/findings/ReadOnlyNotice'
|
||||
import { useFormDraft } from '@/components/editor/findings/useDirtyGuard'
|
||||
import { patchElement } from '@/editor/findings/tree'
|
||||
import {
|
||||
addId,
|
||||
applyEdits,
|
||||
NO_EDITS,
|
||||
removeId,
|
||||
sameEdits,
|
||||
type CitationEdits,
|
||||
} from '@/editor/references/citationEdits'
|
||||
import { citedIds, citingFinding } from '@/editor/references/filter'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import { useEditCommand } from '@/editor/useEditCommand'
|
||||
import type { Guards } from '@/editor/useEditorNavigation'
|
||||
import type { ReferenceMoves } from '@/editor/useReferenceMoves'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import CitationsDraftList from './CitationsDraftList'
|
||||
|
||||
export interface CitationsEditFormProps {
|
||||
citedBy: ElementKey
|
||||
readOnly: boolean
|
||||
guards: Guards
|
||||
/** The reference dialog over this one: an edit, or a creation cited in the draft. */
|
||||
references: Pick<ReferenceMoves, 'open' | 'start'>
|
||||
/** Back to the view mode; `saved` is true when a command was written. */
|
||||
onDone: (saved: boolean) => void
|
||||
/** « Abandonner » after a close request: the draft goes, so does the dialog. */
|
||||
onDiscardClose: () => void
|
||||
}
|
||||
|
||||
/** The family or node as the document holds it now, and the ids it cites now. */
|
||||
function useCurrent(citedBy: ElementKey) {
|
||||
const doc = useSheetDocument()
|
||||
const item = doc ? citingFinding(doc, citedBy) : null
|
||||
return { item, ids: item ? citedIds(item) : [] }
|
||||
}
|
||||
|
||||
const sameIds = (a: readonly string[], b: readonly string[]) =>
|
||||
a.length === b.length && a.every((id, at) => id === b[at])
|
||||
|
||||
type Draft = ReturnType<typeof useFormDraft<CitationEdits>>
|
||||
|
||||
/** The save: the draft applied to the current citations, one `update`, none unchanged. */
|
||||
function useCitationsSave(props: CitationsEditFormProps, form: Draft) {
|
||||
const { item, ids } = useCurrent(props.citedBy)
|
||||
const edit = useEditCommand()
|
||||
const save = () => {
|
||||
const next = applyEdits(ids, form.draft)
|
||||
if (!item || sameIds(next, ids)) return props.onDone(false)
|
||||
const element = patchElement(item, { reference_ids: next })
|
||||
const command = { op: 'update', key: item.key, element } as const
|
||||
if (edit.run('save', command)) props.onDone(true)
|
||||
}
|
||||
return {
|
||||
shown: applyEdits(ids, form.draft),
|
||||
save,
|
||||
error: edit.fieldError('save'),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* « Nouvelle référence »: the creation dialog over this one; the new id joins the draft,
|
||||
* only while this form is still there (an abandoned draft cites nothing).
|
||||
*/
|
||||
function useCreation(props: CitationsEditFormProps, form: Draft) {
|
||||
const latest = useRef(form)
|
||||
latest.current = form
|
||||
const alive = useRef(true)
|
||||
useEffect(() => {
|
||||
alive.current = true
|
||||
return () => {
|
||||
alive.current = false
|
||||
}
|
||||
}, [])
|
||||
return (opener: HTMLElement) =>
|
||||
props.references.start(opener, (id) => {
|
||||
const now = latest.current
|
||||
if (alive.current) now.set(addId(now.draft, id))
|
||||
})
|
||||
}
|
||||
|
||||
/** The form's state: the draft, its guard, its save, its cancel and its creation. */
|
||||
function useCitationsForm(props: CitationsEditFormProps) {
|
||||
const form = useFormDraft(NO_EDITS, props.guards, sameEdits, 'citations')
|
||||
const saving = useCitationsSave(props, form)
|
||||
const cancel = () =>
|
||||
form.dirty && !props.readOnly
|
||||
? form.guard.askToLeave()
|
||||
: props.onDone(false)
|
||||
return { ...form, ...saving, cancel, create: useCreation(props, form) }
|
||||
}
|
||||
|
||||
/**
|
||||
* The references dialog's edit mode (Rev 5): a draft of the citations (cite, remove, a new
|
||||
* reference cited), one `update` on « Enregistrer » (none when nothing changed), nothing on
|
||||
* « Annuler » or a close. The draft's layer is `citations`: a dirty one is asked before a
|
||||
* close, a Back or a leave. Read only: the draft stays, only « Abandonner » is left.
|
||||
*/
|
||||
export default function CitationsEditForm(props: CitationsEditFormProps) {
|
||||
const t = useT()
|
||||
const form = useCitationsForm(props)
|
||||
const { guard } = form
|
||||
const discard =
|
||||
guard.confirming === 'dialog'
|
||||
? props.onDiscardClose
|
||||
: () => props.onDone(false)
|
||||
return (
|
||||
<form
|
||||
data-draft-form
|
||||
className="space-y-3"
|
||||
noValidate
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault()
|
||||
form.save()
|
||||
}}
|
||||
>
|
||||
{props.readOnly && <ReadOnlyNotice />}
|
||||
<CitationsDraftList
|
||||
ids={form.shown}
|
||||
readOnly={props.readOnly}
|
||||
onAdd={(id) => form.set(addId(form.draft, id))}
|
||||
onRemove={(id) => form.set(removeId(form.draft, id))}
|
||||
onEditReference={props.references.open}
|
||||
onNew={form.create}
|
||||
/>
|
||||
{form.error && (
|
||||
<p role="alert" className="text-sm text-[var(--color-danger-strong)]">
|
||||
{form.error}
|
||||
</p>
|
||||
)}
|
||||
<FormActions
|
||||
confirming={guard.confirming}
|
||||
readOnly={props.readOnly}
|
||||
submitLabel={t('editor.findings.save')}
|
||||
onCancel={form.cancel}
|
||||
onKeepTyping={guard.keepTyping}
|
||||
onDiscard={guard.discard(discard)}
|
||||
/>
|
||||
</form>
|
||||
)
|
||||
}
|
||||
@@ -6,11 +6,7 @@ import type { CitationEntry } from '@/editor/references/panel'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import ReferenceTile from './ReferenceTile'
|
||||
import {
|
||||
CITATIONS_TITLE_ID,
|
||||
useCitationsPanel,
|
||||
type PanelHooks,
|
||||
} from './useCitationsPanel'
|
||||
import { useCitationsPanel, type PanelHooks } from './useCitationsPanel'
|
||||
|
||||
interface Props extends PanelHooks {
|
||||
/** « Tout afficher dans l'onglet Références ». */
|
||||
@@ -23,7 +19,7 @@ const RING =
|
||||
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)]'
|
||||
|
||||
/** « Zone › Famille › **Constat** »: where the finding sits, the finding itself in bold. */
|
||||
function FindingPath({ path }: { path: readonly LocalizedText[] }) {
|
||||
export function FindingPath({ path }: { path: readonly LocalizedText[] }) {
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const names = path.map((name) => name[locale])
|
||||
const own = names.pop()
|
||||
@@ -143,10 +139,10 @@ function Entries({ panel, props }: { panel: Panel; props: Props }) {
|
||||
}
|
||||
|
||||
/**
|
||||
* The citations of one family or finding node, the body of the references dialog
|
||||
* The citations of one family or finding node, the view mode of the references dialog
|
||||
* (CitationsDialog): where it sits, the references it cites as tiles, the dangling ids
|
||||
* named, « Tout afficher » for the whole library. The view mode: no search, add, remove,
|
||||
* order or « Modifier » (P7).
|
||||
* named, « Tout afficher » for the whole library. No search, add, remove, order or
|
||||
* « Modifier » on a tile (P7): the dialog's own « Modifier » opens the edit mode.
|
||||
*/
|
||||
export default function CitationsPanel(props: Props) {
|
||||
const { onShowAll } = props
|
||||
@@ -154,13 +150,7 @@ export default function CitationsPanel(props: Props) {
|
||||
const panel = useCitationsPanel(props)
|
||||
const count = panel.entries.length
|
||||
return (
|
||||
<section
|
||||
id="editor-citations-panel"
|
||||
data-testid="editor-citations-panel"
|
||||
data-place="citations"
|
||||
aria-labelledby={CITATIONS_TITLE_ID}
|
||||
className="space-y-3"
|
||||
>
|
||||
<>
|
||||
<FindingPath path={panel.path} />
|
||||
<Summary count={count} onShowAll={onShowAll} />
|
||||
{count === 0 && (
|
||||
@@ -169,6 +159,6 @@ export default function CitationsPanel(props: Props) {
|
||||
</p>
|
||||
)}
|
||||
<Entries panel={panel} props={props} />
|
||||
</section>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -41,7 +41,7 @@ function useGoneRule(layer: ReferenceLayer) {
|
||||
}, [view, doc, layer, t])
|
||||
}
|
||||
|
||||
/** Where a closed dialog returns the focus: the tile's header, or the add button. */
|
||||
/** Where a closed dialog returns the focus: its opener, else the tile header or « Ajouter ». */
|
||||
function useReturnTarget(layer: ReferenceLayer) {
|
||||
const last = useRef(layer.view)
|
||||
if (layer.view) last.current = layer.view
|
||||
@@ -49,7 +49,9 @@ function useReturnTarget(layer: ReferenceLayer) {
|
||||
return useCallback((): HTMLElement | null => {
|
||||
const shown = last.current
|
||||
if (!shown) return null
|
||||
if ('create' in shown) return document.getElementById(ADD_BUTTON_ID)
|
||||
// A creation over the citations dialog returns to its « Nouvelle référence ».
|
||||
if ('create' in shown)
|
||||
return opener() ?? document.getElementById(ADD_BUTTON_ID)
|
||||
const wanted = JSON.stringify({ kind: 'reference', id: shown.edit })
|
||||
return (
|
||||
opener() ??
|
||||
@@ -104,10 +106,10 @@ function useOnSaved(
|
||||
}
|
||||
|
||||
/**
|
||||
* The reference dialog (R2F-5): one `Dialog` over the References tab or the citations
|
||||
* sheet, a form for a reference's type, DOI, citation and URL, or for a new one. The view
|
||||
* layer of the browser history drives it; it is no part of a kept place, so it is never a
|
||||
* native modal inside a hidden one.
|
||||
* The reference dialog (R2F-5): one `Dialog` over the References tab or the references
|
||||
* dialog's edit mode (Rev 5), a form for a reference's type, DOI, citation and URL, or for
|
||||
* a new one. The view layer of the browser history drives it; it is no part of a kept
|
||||
* place, so it is never a native modal inside a hidden one.
|
||||
*/
|
||||
export default function ReferenceDialog({
|
||||
layer,
|
||||
|
||||
Reference in new issue
Block a user