217 lines
7.1 KiB
TypeScript
217 lines
7.1 KiB
TypeScript
import type { ElementKey, Reference } from '@faunosteo/core-wasm/types'
|
|
import ActionButton from '@/components/editor/fields/ActionButton'
|
|
import Icon from '@/components/ui/Icon'
|
|
import { usePlural } from '@/editor/plural'
|
|
import type { CiterItem } from '@/editor/references/citations'
|
|
import { useT } from '@/i18n/useT'
|
|
import CitersList from './CitersList'
|
|
import ReferenceFacts from './ReferenceFacts'
|
|
import type { ReferenceHost } from './referenceTypes'
|
|
|
|
/** What a tile asks of the library that owns its state. */
|
|
export interface TileHandlers {
|
|
toggle: () => void
|
|
/** « 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
|
|
}
|
|
|
|
/** What a tile of the citations panel adds: the finding it belongs to. */
|
|
export interface PanelTile {
|
|
current: ElementKey
|
|
}
|
|
|
|
export interface TileProps {
|
|
host: ReferenceHost
|
|
/** Set for the citations panel: « ce constat » marks. */
|
|
panel?: PanelTile
|
|
reference: Reference
|
|
/** Position in the whole bibliography, not in a filtered list. */
|
|
position: number
|
|
count: number
|
|
citers: readonly CiterItem[]
|
|
open: boolean
|
|
readOnly: boolean
|
|
/** ↑ / ↓ only while neither the search nor a sort narrows or reorders the list. */
|
|
movable: boolean
|
|
/** The tab's pinned tile the current search misses: it says so under its header. */
|
|
outsideSearch?: boolean
|
|
on: TileHandlers
|
|
}
|
|
|
|
/** DOM id prefix of a tile: host, position and id keep duplicates distinct (P31). */
|
|
export const tileBase = (
|
|
host: ReferenceHost,
|
|
position: number,
|
|
reference: Reference,
|
|
) => `${host}-reference-${position}-${reference.id}`
|
|
|
|
/** The meta column (type, count) from 640 px: the panel gives it a fixed 132 px. */
|
|
const META_COLUMN = {
|
|
tab: 'sm:grid-cols-[1.25rem_1fr_auto]',
|
|
panel: 'sm:grid-cols-[1.25rem_1fr_8.25rem]',
|
|
} as const
|
|
|
|
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 }: { base: string; props: TileProps }) {
|
|
const t = useT()
|
|
const plural = usePlural()
|
|
const { reference, open, citers } = props
|
|
const cited = citers.length
|
|
const key = JSON.stringify({ kind: 'reference', id: reference.id })
|
|
return (
|
|
<button
|
|
type="button"
|
|
id={`${base}-header`}
|
|
data-focus-key={key}
|
|
data-tile-header
|
|
aria-expanded={open}
|
|
aria-controls={`${base}-body`}
|
|
aria-describedby={props.outsideSearch ? `${base}-outside` : undefined}
|
|
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 ? 'chevron-down' : 'chevron-right'} size={18} />
|
|
<span
|
|
className={`min-w-0 break-words text-sm ${open ? '' : 'line-clamp-2'}`}
|
|
>
|
|
{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">
|
|
<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')
|
|
: plural('editor.references.cited', cited)}
|
|
</span>
|
|
</span>
|
|
</button>
|
|
)
|
|
}
|
|
|
|
/** The panel's one action (P7): « Modifier ». Removing a citation is the edit form's. */
|
|
function PanelActions({ props }: { props: TileProps }) {
|
|
const t = useT()
|
|
return (
|
|
<ActionButton
|
|
readOnly={false}
|
|
variant="secondary"
|
|
size="sm"
|
|
onClick={(event) => props.on.edit(event.currentTarget)}
|
|
>
|
|
{t('editor.references.edit')}
|
|
</ActionButton>
|
|
)
|
|
}
|
|
|
|
/** « Modifier », « Supprimer » (uncited only, else the hint), « Monter » / « Descendre ». */
|
|
function TileActions({ props }: { props: TileProps }) {
|
|
const t = useT()
|
|
const { position, count, citers, readOnly, movable, on } = props
|
|
if (readOnly) return null
|
|
if (props.panel) return <PanelActions props={props} />
|
|
return (
|
|
<>
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<ActionButton
|
|
readOnly={false}
|
|
variant="secondary"
|
|
size="sm"
|
|
onClick={(event) => on.edit(event.currentTarget)}
|
|
>
|
|
{t('editor.references.edit')}
|
|
</ActionButton>
|
|
{citers.length === 0 && on.remove && (
|
|
<ActionButton
|
|
readOnly={false}
|
|
variant="ghost"
|
|
size="sm"
|
|
className="text-[var(--color-danger-strong)]"
|
|
onClick={on.remove}
|
|
>
|
|
{t('editor.action.delete')}
|
|
</ActionButton>
|
|
)}
|
|
{movable && on.move && (
|
|
<>
|
|
<ActionButton
|
|
readOnly={false}
|
|
variant="secondary"
|
|
size="sm"
|
|
disabled={position === 0}
|
|
onClick={() => on.move?.(position - 1)}
|
|
>
|
|
{t('editor.action.move_up')}
|
|
</ActionButton>
|
|
<ActionButton
|
|
readOnly={false}
|
|
variant="secondary"
|
|
size="sm"
|
|
disabled={position >= count - 1}
|
|
onClick={() => on.move?.(position + 1)}
|
|
>
|
|
{t('editor.action.move_down')}
|
|
</ActionButton>
|
|
</>
|
|
)}
|
|
</div>
|
|
{citers.length > 0 && (
|
|
<p className="text-sm text-[var(--color-ink-muted)]">
|
|
{t('editor.references.delete_hint')}
|
|
</p>
|
|
)}
|
|
</>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* 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, on } = props
|
|
const t = useT()
|
|
const base = tileBase(host, position, reference)
|
|
return (
|
|
<li
|
|
data-reference-tile
|
|
className="rounded-lg border border-[var(--color-border)] bg-[var(--color-surface)]"
|
|
>
|
|
<TileHeader base={base} props={props} />
|
|
{props.outsideSearch && (
|
|
<p
|
|
id={`${base}-outside`}
|
|
className="px-3 pb-2 text-xs text-[var(--color-ink-muted)]"
|
|
>
|
|
{t('editor.references.outside_search')}
|
|
</p>
|
|
)}
|
|
{open && (
|
|
<div
|
|
id={`${base}-body`}
|
|
className="space-y-3 border-t border-[var(--color-border)] p-3"
|
|
>
|
|
<ReferenceFacts reference={reference} />
|
|
<CitersList
|
|
citers={props.citers}
|
|
current={props.panel?.current}
|
|
onGoTo={on.goTo}
|
|
/>
|
|
<TileActions props={props} />
|
|
</div>
|
|
)}
|
|
</li>
|
|
)
|
|
}
|