feat(editor): reference dialog, reading tiles and finding filter in the history
R2F-5 and R2F-6: the reference dialog is a view layer above the References tab or the citations sheet (local draft, one command per changed save, creation entry becomes the reference's own). Tiles only read. « Tout afficher » filters the library by the finding, « Revenir au constat » pops or pushes. Inline tile edit, draft tile and their guards removed.
This commit is contained in:
1 parent
e2d020bd3d
commit
8031e0078f
28 files changed
+1072
-1512
No files matched your search
@@ -1,43 +1,23 @@
|
||||
import {
|
||||
useCallback,
|
||||
useContext,
|
||||
useLayoutEffect,
|
||||
useRef,
|
||||
useState,
|
||||
type RefObject,
|
||||
} from 'react'
|
||||
import type { ElementKey, Reference } from '@faunosteo/core-wasm/types'
|
||||
import ActionButton from '@/components/editor/fields/ActionButton'
|
||||
import Icon from '@/components/ui/Icon'
|
||||
import { AnnounceContext } from '@/editor/focusRequest'
|
||||
import { usePlural } from '@/editor/plural'
|
||||
import type { CiterItem } from '@/editor/references/citations'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import CitersList from './CitersList'
|
||||
import ReferenceEditor from './ReferenceEditor'
|
||||
import ReferenceFacts from './ReferenceFacts'
|
||||
import type { ReferenceHost } from './referenceTypes'
|
||||
import { useDroppedText, type Field, type LeaveGuard } from './useReferenceEdit'
|
||||
|
||||
/** What a tile asks of the library that owns its state. */
|
||||
export interface TileHandlers {
|
||||
toggle: () => void
|
||||
edit: () => void
|
||||
/** Leaves the edit view; false (the view stays) when a field refuses. */
|
||||
done: () => boolean
|
||||
/** « Modifier »: opens the reference dialog; `opener` is where the focus returns. */
|
||||
edit: (opener: HTMLElement) => void
|
||||
/** « Supprimer »: the Références tab only. */
|
||||
remove?: () => void
|
||||
/** « Monter » / « Descendre »: the Références tab only. */
|
||||
move?: (position: number) => void
|
||||
goTo: (key: ElementKey) => void
|
||||
registerLeave: (guard: LeaveGuard | null) => void
|
||||
/** True once after « Modifier »: the edit view then focuses its Citation field. */
|
||||
takeFocus: () => boolean
|
||||
/**
|
||||
* True while the host itself unmounts: the leave that closed it already said the dropped
|
||||
* drafts (the citations panel's forced close), so the tile does not say them again.
|
||||
*/
|
||||
hostLeaving?: () => boolean
|
||||
}
|
||||
|
||||
/** What a tile of the citations panel adds: the finding it belongs to and « Retirer ». */
|
||||
@@ -56,7 +36,6 @@ export interface TileProps {
|
||||
count: number
|
||||
citers: readonly CiterItem[]
|
||||
open: boolean
|
||||
editing: boolean
|
||||
readOnly: boolean
|
||||
/** ↑ / ↓ only while neither the search nor a sort narrows or reorders the list. */
|
||||
movable: boolean
|
||||
@@ -82,58 +61,34 @@ const BUTTON_RING =
|
||||
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)]'
|
||||
|
||||
/** The disclosure button: citation (two lines when closed), type and how often it is cited. */
|
||||
function TileHeader({
|
||||
base,
|
||||
props,
|
||||
headerRef,
|
||||
onToggle,
|
||||
}: {
|
||||
base: string
|
||||
props: TileProps
|
||||
headerRef: RefObject<HTMLButtonElement | null>
|
||||
onToggle: () => void
|
||||
}) {
|
||||
function TileHeader({ base, props }: { base: string; props: TileProps }) {
|
||||
const t = useT()
|
||||
const plural = usePlural()
|
||||
const { reference, open, editing, citers } = props
|
||||
const { reference, open, citers } = props
|
||||
const cited = citers.length
|
||||
const key = JSON.stringify({ kind: 'reference', id: reference.id })
|
||||
return (
|
||||
<button
|
||||
ref={headerRef}
|
||||
type="button"
|
||||
id={`${base}-header`}
|
||||
data-focus-key={key}
|
||||
data-tile-header
|
||||
aria-expanded={open || editing}
|
||||
aria-expanded={open}
|
||||
aria-controls={`${base}-body`}
|
||||
aria-describedby={props.outsideSearch ? `${base}-outside` : undefined}
|
||||
onClick={onToggle}
|
||||
onClick={props.on.toggle}
|
||||
className={`grid min-h-11 w-full grid-cols-[1.25rem_1fr] items-start gap-x-3 gap-y-1 rounded-lg p-3 text-left ${META_COLUMN[props.host]} ${BUTTON_RING}`}
|
||||
>
|
||||
<Icon
|
||||
name={open || editing ? 'chevron-down' : 'chevron-right'}
|
||||
size={18}
|
||||
/>
|
||||
<Icon name={open ? 'chevron-down' : 'chevron-right'} size={18} />
|
||||
<span
|
||||
className={`min-w-0 break-words text-sm ${open || editing ? '' : 'line-clamp-2'} ${editing ? 'font-semibold' : ''}`}
|
||||
className={`min-w-0 break-words text-sm ${open ? '' : 'line-clamp-2'}`}
|
||||
>
|
||||
{editing ? (
|
||||
// In edit this button ends the edit: its name says so, the title stays visible (M7).
|
||||
<>
|
||||
<span className="sr-only">{t('editor.references.done')}. </span>
|
||||
{t('editor.references.edit_title')}
|
||||
</>
|
||||
) : (
|
||||
reference.citation
|
||||
)}
|
||||
{reference.citation}
|
||||
</span>
|
||||
<span className="col-start-2 flex flex-wrap items-center gap-x-3 gap-y-1 text-xs text-[var(--color-ink-muted)] sm:col-start-3 sm:row-start-1 sm:flex-col sm:items-end">
|
||||
{!editing && (
|
||||
<span className="rounded-md bg-[var(--color-surface-strong)] px-2 py-0.5 font-medium text-[var(--color-ink)]">
|
||||
{t(`editor.reference.type.${reference.type}`)}
|
||||
</span>
|
||||
)}
|
||||
<span className="rounded-md bg-[var(--color-surface-strong)] px-2 py-0.5 font-medium text-[var(--color-ink)]">
|
||||
{t(`editor.reference.type.${reference.type}`)}
|
||||
</span>
|
||||
<span>
|
||||
{cited === 0
|
||||
? t('editor.references.uncited')
|
||||
@@ -159,7 +114,7 @@ function PanelActions({
|
||||
readOnly={false}
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={props.on.edit}
|
||||
onClick={(event) => props.on.edit(event.currentTarget)}
|
||||
>
|
||||
{t('editor.references.edit')}
|
||||
</ActionButton>
|
||||
@@ -188,7 +143,7 @@ function TileActions({ props }: { props: TileProps }) {
|
||||
readOnly={false}
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={on.edit}
|
||||
onClick={(event) => on.edit(event.currentTarget)}
|
||||
>
|
||||
{t('editor.references.edit')}
|
||||
</ActionButton>
|
||||
@@ -236,96 +191,21 @@ function TileActions({ props }: { props: TileProps }) {
|
||||
}
|
||||
|
||||
/**
|
||||
* The refused drafts an edit view was unmounted with (a read-only flip, an undo that removes
|
||||
* the reference), as the tile keeps them: shown by the tile until its next action. The drop
|
||||
* is said once in the polite live region from `onDrop` itself, not from an effect: an undo
|
||||
* unmounts the tile with its view, and a dying tile runs no effect (V1). An edit view that
|
||||
* ends on purpose (« Terminer », another tile's « Modifier ») ends the note; a flip does not.
|
||||
*/
|
||||
function useDroppedDrafts(
|
||||
editing: boolean,
|
||||
readOnly: boolean,
|
||||
hostLeaving?: () => boolean,
|
||||
) {
|
||||
const droppedText = useDroppedText()
|
||||
const announce = useContext(AnnounceContext)
|
||||
const [dropped, setDropped] = useState<readonly Field[] | null>(null)
|
||||
const [wasEditing, setWasEditing] = useState(editing)
|
||||
if (wasEditing !== editing) {
|
||||
setWasEditing(editing)
|
||||
if (wasEditing && !readOnly) setDropped(null)
|
||||
}
|
||||
// `onDrop` must stay stable (the view's cleanup depends on it): it reads the latest
|
||||
// render's words and host through this ref.
|
||||
const latest = useRef({ droppedText, announce, hostLeaving })
|
||||
useLayoutEffect(() => {
|
||||
latest.current = { droppedText, announce, hostLeaving }
|
||||
})
|
||||
const onDrop = useCallback((fields: readonly Field[]) => {
|
||||
setDropped(fields)
|
||||
const now = latest.current
|
||||
if (!now.hostLeaving?.()) now.announce(now.droppedText(fields))
|
||||
}, [])
|
||||
return { dropped, setDropped, onDrop }
|
||||
}
|
||||
|
||||
/** The tile's own actions: « Terminer » refocuses the header, a toggle ends the note. */
|
||||
function useTileActions(
|
||||
props: TileProps,
|
||||
headerRef: RefObject<HTMLButtonElement | null>,
|
||||
) {
|
||||
const { on, editing, readOnly } = props
|
||||
const { dropped, setDropped, onDrop } = useDroppedDrafts(
|
||||
editing,
|
||||
readOnly,
|
||||
on.hostLeaving,
|
||||
)
|
||||
return {
|
||||
dropped,
|
||||
onDrop,
|
||||
done: () => {
|
||||
if (on.done()) headerRef.current?.focus()
|
||||
},
|
||||
toggle: () => {
|
||||
setDropped(null)
|
||||
on.toggle()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/** « Saisie non appliquée : … »: plain text, the live region says it once elsewhere. */
|
||||
function DroppedNote({ fields }: { fields: readonly Field[] }) {
|
||||
const droppedText = useDroppedText()
|
||||
return (
|
||||
<p className="flex items-start gap-2 rounded-md bg-amber-50 p-2 text-sm text-amber-800">
|
||||
<Icon name="alert" size={16} className="mt-0.5 shrink-0" />
|
||||
{droppedText(fields)}
|
||||
</p>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* One reference as a collapsible tile (boards 23, 24): closed, open (facts, citers,
|
||||
* actions), or in edit (fields, citers, « Terminer »). Read-only: the facts and the
|
||||
* citers' « Aller à » only, no control that changes the draft.
|
||||
* One reference as a collapsible tile (boards 23, 24): closed, or open (facts, citers,
|
||||
* actions). « Modifier » opens the reference dialog; no field is edited in the tile
|
||||
* (R2F-5). Read-only: the facts and the citers' « Aller à » only, no control that changes
|
||||
* the document.
|
||||
*/
|
||||
export default function ReferenceTile(props: TileProps) {
|
||||
const { host, reference, position, open, editing, on } = props
|
||||
const { host, reference, position, open, on } = props
|
||||
const t = useT()
|
||||
const base = tileBase(host, position, reference)
|
||||
const headerRef = useRef<HTMLButtonElement>(null)
|
||||
const { dropped, onDrop, done, toggle } = useTileActions(props, headerRef)
|
||||
return (
|
||||
<li
|
||||
data-reference-tile
|
||||
className="rounded-lg border border-[var(--color-border)] bg-[var(--color-surface)]"
|
||||
>
|
||||
<TileHeader
|
||||
base={base}
|
||||
props={props}
|
||||
headerRef={headerRef}
|
||||
onToggle={toggle}
|
||||
/>
|
||||
<TileHeader base={base} props={props} />
|
||||
{props.outsideSearch && (
|
||||
<p
|
||||
id={`${base}-outside`}
|
||||
@@ -334,45 +214,18 @@ export default function ReferenceTile(props: TileProps) {
|
||||
{t('editor.references.outside_search')}
|
||||
</p>
|
||||
)}
|
||||
{(open || editing) && (
|
||||
{open && (
|
||||
<div
|
||||
id={`${base}-body`}
|
||||
className="space-y-3 border-t border-[var(--color-border)] p-3"
|
||||
>
|
||||
{editing ? (
|
||||
<ReferenceEditor
|
||||
reference={reference}
|
||||
base={base}
|
||||
hooks={{
|
||||
registerLeave: on.registerLeave,
|
||||
takeFocus: on.takeFocus,
|
||||
onDrop,
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<ReferenceFacts reference={reference} />
|
||||
)}
|
||||
<ReferenceFacts reference={reference} />
|
||||
<CitersList
|
||||
citers={props.citers}
|
||||
current={props.panel?.current}
|
||||
onGoTo={on.goTo}
|
||||
/>
|
||||
{dropped && <DroppedNote fields={dropped} />}
|
||||
{editing ? (
|
||||
<ActionButton
|
||||
readOnly={false}
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
// A press keeps the focus in its field: a refusal shown on blur would move
|
||||
// the button before the click lands. The click applies every field itself.
|
||||
onMouseDown={(event) => event.preventDefault()}
|
||||
onClick={done}
|
||||
>
|
||||
{t('editor.references.done')}
|
||||
</ActionButton>
|
||||
) : (
|
||||
<TileActions props={props} />
|
||||
)}
|
||||
<TileActions props={props} />
|
||||
</div>
|
||||
)}
|
||||
</li>
|
||||
|
||||
Reference in new issue
Block a user