319 lines
8.7 KiB
TypeScript
319 lines
8.7 KiB
TypeScript
import { useState } from 'react'
|
|
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
|
|
import Button from '@/components/ui/Button'
|
|
import Input from '@/components/ui/Input'
|
|
import {
|
|
changedFieldNames,
|
|
fieldsOf,
|
|
parseFields,
|
|
sameAsStored,
|
|
withVertexAfter,
|
|
withoutVertex,
|
|
type EditableGeometry,
|
|
type GroupFields,
|
|
} from '@/editor/annotations/geometryEdit'
|
|
import { usePlural } from '@/editor/plural'
|
|
import { useT } from '@/i18n/useT'
|
|
|
|
/** What applying the group answers: kept, or the words that say why not. */
|
|
export type ApplyResult = { ok: true } | { ok: false; messages: string[] }
|
|
|
|
export interface GeometryGroupProps {
|
|
idPrefix: string
|
|
/** The stored (or pending) geometry the fields start from. */
|
|
geometry: EditableGeometry
|
|
/** Proposes the WHOLE group; the core's verdict comes back as the result. */
|
|
onApply: (geometry: EditableGeometry) => ApplyResult
|
|
/** Fields shown as text: read-only mode or an unmeasured view. */
|
|
locked: boolean
|
|
}
|
|
|
|
type Fields = GroupFields
|
|
type PolygonFields = Extract<GroupFields, { type: 'polygon' }>
|
|
|
|
interface GroupState {
|
|
key: string
|
|
draft: Fields
|
|
messages: string[]
|
|
}
|
|
|
|
/** The typed fields, reset whenever the stored value changes (undo, restore, a move). */
|
|
function useGroupState(geometry: EditableGeometry) {
|
|
const key = JSON.stringify(fieldsOf(geometry))
|
|
const fresh = (): GroupState => ({
|
|
key,
|
|
draft: fieldsOf(geometry),
|
|
messages: [],
|
|
})
|
|
const [state, setState] = useState<GroupState>(fresh)
|
|
if (state.key !== key) setState(fresh())
|
|
return {
|
|
state,
|
|
set: (patch: Partial<GroupState>) => setState({ ...state, ...patch }),
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The geometry fields as ONE group held in component state (plan « Repair through a
|
|
* temporary form »): Enter or leaving a field proposes the whole group as one update;
|
|
* a refusal keeps the typed values marked « non appliqué » with the core's message and
|
|
* the document unchanged. Selecting another element unmounts it: an unapplied group was
|
|
* never in the document and is dropped (no commit on unmount).
|
|
*/
|
|
export default function GeometryGroup(props: GeometryGroupProps) {
|
|
const { state, set } = useGroupState(props.geometry)
|
|
const propose = (next: Fields) => {
|
|
if (sameAsStored(next, props.geometry))
|
|
return set({ draft: next, messages: [] })
|
|
const parsed = parseFields(next)
|
|
if (!parsed.ok) return set({ draft: next, messages: ['number'] })
|
|
const result = props.onApply(parsed.geometry)
|
|
set({ draft: next, messages: result.ok ? [] : result.messages })
|
|
}
|
|
return (
|
|
<GroupBody
|
|
idPrefix={props.idPrefix}
|
|
locked={props.locked}
|
|
geometry={props.geometry}
|
|
draft={state.draft}
|
|
messages={state.messages}
|
|
pending={changedFieldNames(state.draft, props.geometry)}
|
|
onChange={(draft) => set({ draft })}
|
|
onPropose={propose}
|
|
onRevert={() => set({ draft: fieldsOf(props.geometry), messages: [] })}
|
|
/>
|
|
)
|
|
}
|
|
|
|
interface BodyProps extends Omit<GeometryGroupProps, 'onApply'> {
|
|
draft: Fields
|
|
pending: Set<string>
|
|
messages: string[]
|
|
onChange: (fields: Fields) => void
|
|
onPropose: (fields: Fields) => void
|
|
onRevert: () => void
|
|
}
|
|
|
|
function GroupBody(props: BodyProps) {
|
|
const t = useT()
|
|
const { draft, pending, messages } = props
|
|
const unapplied = pending.size > 0 && messages.length > 0
|
|
return (
|
|
<fieldset className="space-y-2">
|
|
<legend className="text-sm font-medium">
|
|
{t(
|
|
draft.type === 'circle'
|
|
? 'editor.annotations.geometry.circle'
|
|
: 'editor.annotations.geometry.polygon',
|
|
)}
|
|
</legend>
|
|
{draft.type === 'circle' ? (
|
|
<CircleRows {...props} fields={draft} />
|
|
) : (
|
|
<PolygonRows {...props} fields={draft} />
|
|
)}
|
|
{unapplied && <Unapplied {...props} />}
|
|
</fieldset>
|
|
)
|
|
}
|
|
|
|
function Unapplied({ messages, onRevert }: BodyProps) {
|
|
const t = useT()
|
|
const words = messages.map((m) =>
|
|
m === 'number' ? t('editor.annotations.geometry.number') : m,
|
|
)
|
|
return (
|
|
<div role="alert" className="space-y-1 text-sm">
|
|
<p className="font-medium text-[var(--color-danger-strong)]">
|
|
{t('editor.annotations.geometry.unapplied')}
|
|
</p>
|
|
{words.map((word) => (
|
|
<p key={word} className="text-[var(--color-danger-strong)]">
|
|
{word}
|
|
</p>
|
|
))}
|
|
<Button type="button" variant="secondary" size="sm" onClick={onRevert}>
|
|
{t('editor.annotations.geometry.revert')}
|
|
</Button>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function NumberInput({
|
|
id,
|
|
label,
|
|
value,
|
|
unapplied,
|
|
locked,
|
|
onType,
|
|
onApply,
|
|
}: {
|
|
id: string
|
|
label: string
|
|
value: string
|
|
unapplied: boolean
|
|
locked: boolean
|
|
onType: (value: string) => void
|
|
onApply: () => void
|
|
}) {
|
|
const t = useT()
|
|
if (locked) {
|
|
return (
|
|
<p className="text-sm">
|
|
<span className="font-medium">{label}</span> {value}
|
|
</p>
|
|
)
|
|
}
|
|
const hint = unapplied
|
|
? t('editor.annotations.geometry.unapplied_tag')
|
|
: undefined
|
|
return (
|
|
<FormField id={id} label={label} hint={hint}>
|
|
<Input
|
|
id={id}
|
|
inputMode="decimal"
|
|
value={value}
|
|
aria-invalid={unapplied}
|
|
aria-describedby={fieldDescribedBy(id, hint)}
|
|
onChange={(e) => onType(e.target.value)}
|
|
onBlur={onApply}
|
|
onKeyDown={(e) => {
|
|
if (e.key === 'Enter' && !e.nativeEvent.isComposing) onApply()
|
|
}}
|
|
/>
|
|
</FormField>
|
|
)
|
|
}
|
|
|
|
type RowsProps<F extends Fields> = BodyProps & { fields: F }
|
|
|
|
function CircleRows(props: RowsProps<Extract<Fields, { type: 'circle' }>>) {
|
|
const t = useT()
|
|
const { fields, pending, locked, idPrefix } = props
|
|
return (
|
|
<div className="grid grid-cols-3 gap-2">
|
|
{(['cx', 'cy', 'r'] as const).map((key) => (
|
|
<NumberInput
|
|
key={key}
|
|
id={`${idPrefix}-${key}`}
|
|
label={t(`editor.annotations.geometry.${key}`)}
|
|
value={fields[key]}
|
|
unapplied={pending.has(key)}
|
|
locked={locked}
|
|
onType={(value) => props.onChange({ ...fields, [key]: value })}
|
|
onApply={() => props.onPropose(fields)}
|
|
/>
|
|
))}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function PolygonRows(props: RowsProps<PolygonFields>) {
|
|
const [selected, setSelected] = useState(0)
|
|
const { fields } = props
|
|
const change = (index: number, axis: 'x' | 'y', value: string) =>
|
|
props.onChange({
|
|
type: 'polygon',
|
|
rows: fields.rows.map((row, i) =>
|
|
i === index ? { ...row, [axis]: value } : row,
|
|
),
|
|
})
|
|
const plural = usePlural()
|
|
return (
|
|
<div className="space-y-2">
|
|
<p className="text-sm text-[var(--color-ink-muted)]">
|
|
{plural('editor.annotations.geometry.vertices', fields.rows.length)}
|
|
</p>
|
|
{fields.rows.map((row, index) => (
|
|
<VertexRow
|
|
key={index}
|
|
index={index}
|
|
row={row}
|
|
group={props}
|
|
onType={(axis, value) => change(index, axis, value)}
|
|
onFocus={() => setSelected(index)}
|
|
/>
|
|
))}
|
|
{!props.locked && (
|
|
<VertexButtons
|
|
fields={fields}
|
|
selected={selected}
|
|
onApply={props.onPropose}
|
|
/>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function VertexRow({
|
|
index,
|
|
row,
|
|
group,
|
|
onType,
|
|
onFocus,
|
|
}: {
|
|
index: number
|
|
row: { x: string; y: string }
|
|
group: RowsProps<PolygonFields>
|
|
onType: (axis: 'x' | 'y', value: string) => void
|
|
onFocus: () => void
|
|
}) {
|
|
const t = useT()
|
|
return (
|
|
<div
|
|
className="grid grid-cols-[2rem_1fr_1fr] items-end gap-2"
|
|
onFocus={onFocus}
|
|
>
|
|
<span className="pb-2 text-sm">{index + 1}</span>
|
|
{(['x', 'y'] as const).map((axis) => (
|
|
<NumberInput
|
|
key={axis}
|
|
id={`${group.idPrefix}-${axis}${index}`}
|
|
label={t(`editor.annotations.geometry.vertex_${axis}`).replace(
|
|
'{n}',
|
|
String(index + 1),
|
|
)}
|
|
value={row[axis]}
|
|
unapplied={group.pending.has(`${axis}${index}`)}
|
|
locked={group.locked}
|
|
onType={(value) => onType(axis, value)}
|
|
onApply={() => group.onPropose(group.fields)}
|
|
/>
|
|
))}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function VertexButtons({
|
|
fields,
|
|
selected,
|
|
onApply,
|
|
}: {
|
|
fields: PolygonFields
|
|
selected: number
|
|
onApply: (fields: Fields) => void
|
|
}) {
|
|
const t = useT()
|
|
return (
|
|
<div className="flex flex-wrap gap-2">
|
|
<Button
|
|
type="button"
|
|
variant="secondary"
|
|
size="sm"
|
|
onClick={() => onApply(withVertexAfter(fields, selected))}
|
|
>
|
|
{t('editor.annotations.geometry.add_vertex')}
|
|
</Button>
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="sm"
|
|
disabled={fields.rows.length <= 3}
|
|
onClick={() => onApply(withoutVertex(fields, selected))}
|
|
>
|
|
{t('editor.action.remove')}
|
|
</Button>
|
|
</div>
|
|
)
|
|
}
|