feat(app): editor Annotations tab, core-decided placement, repair rules
This commit is contained in:
1 parent
92c595caa1
commit
26097852ff
37 files changed
+5001
-6
No files matched your search
@@ -0,0 +1,284 @@
|
||||
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 { 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' }>
|
||||
|
||||
/**
|
||||
* 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 stored = fieldsOf(props.geometry)
|
||||
const storedKey = JSON.stringify(stored)
|
||||
const [draft, setDraft] = useState<Fields>(stored)
|
||||
const [seen, setSeen] = useState(storedKey)
|
||||
const [messages, setMessages] = useState<string[]>([])
|
||||
// A new stored value (undo, restore, an accepted move) replaces what was typed.
|
||||
if (seen !== storedKey) {
|
||||
setSeen(storedKey)
|
||||
setDraft(stored)
|
||||
setMessages([])
|
||||
}
|
||||
const apply = (next: Fields) => {
|
||||
setDraft(next)
|
||||
if (sameAsStored(next, props.geometry)) return setMessages([])
|
||||
const parsed = parseFields(next)
|
||||
if (!parsed.ok) return setMessages(['number'])
|
||||
const result = props.onApply(parsed.geometry)
|
||||
setMessages(result.ok ? [] : result.messages)
|
||||
}
|
||||
const revert = () => {
|
||||
setDraft(stored)
|
||||
setMessages([])
|
||||
}
|
||||
const pending = changedFieldNames(draft, props.geometry)
|
||||
const view = { ...props, draft, pending, messages }
|
||||
return (
|
||||
<GroupBody
|
||||
{...view}
|
||||
onChange={setDraft}
|
||||
onPropose={apply}
|
||||
onRevert={revert}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
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 t = useT()
|
||||
const [selected, setSelected] = useState(0)
|
||||
const { fields, locked } = 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,
|
||||
),
|
||||
})
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('editor.annotations.geometry.vertices').replace(
|
||||
'{count}',
|
||||
String(fields.rows.length),
|
||||
)}
|
||||
</p>
|
||||
{fields.rows.map((row, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className="grid grid-cols-[2rem_1fr_1fr] items-end gap-2"
|
||||
onFocus={() => setSelected(index)}
|
||||
>
|
||||
<span className="pb-2 text-sm">{index + 1}</span>
|
||||
{(['x', 'y'] as const).map((axis) => (
|
||||
<NumberInput
|
||||
key={axis}
|
||||
id={`${props.idPrefix}-${axis}${index}`}
|
||||
label={t(`editor.annotations.geometry.vertex_${axis}`).replace(
|
||||
'{n}',
|
||||
String(index + 1),
|
||||
)}
|
||||
value={row[axis]}
|
||||
unapplied={props.pending.has(`${axis}${index}`)}
|
||||
locked={locked}
|
||||
onType={(value) => change(index, axis, value)}
|
||||
onApply={() => props.onPropose(fields)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
))}
|
||||
{!locked && (
|
||||
<VertexButtons
|
||||
fields={fields}
|
||||
selected={selected}
|
||||
onApply={props.onPropose}
|
||||
/>
|
||||
)}
|
||||
</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>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user