fix(app): annotations tab split, editor-wide undo keys, keyboard point fits, typed radius message

This commit is contained in:
bchanot committed 2026-11-04 14:00:57 +01:00
1 parent 447d0a4272
commit 3f15762d95
25 files changed
+1398 -653

No files matched your search

@@ -30,6 +30,28 @@ export interface GeometryGroupProps {
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;
@@ -38,37 +60,26 @@ type PolygonFields = Extract<GroupFields, { type: 'polygon' }>
* 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 { 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 setMessages(['number'])
if (!parsed.ok) return set({ draft: next, messages: ['number'] })
const result = props.onApply(parsed.geometry)
setMessages(result.ok ? [] : result.messages)
set({ draft: next, messages: 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}
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: [] })}
/>
)
}
@@ -199,7 +210,7 @@ function CircleRows(props: RowsProps<Extract<Fields, { type: 'circle' }>>) {
function PolygonRows(props: RowsProps<PolygonFields>) {
const t = useT()
const [selected, setSelected] = useState(0)
const { fields, locked } = props
const { fields } = props
const change = (index: number, axis: 'x' | 'y', value: string) =>
props.onChange({
type: 'polygon',
@@ -207,39 +218,23 @@ function PolygonRows(props: RowsProps<PolygonFields>) {
i === index ? { ...row, [axis]: value } : row,
),
})
const count = String(fields.rows.length)
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),
)}
{t('editor.annotations.geometry.vertices').replace('{count}', count)}
</p>
{fields.rows.map((row, index) => (
<div
<VertexRow
key={index}
className="grid grid-cols-[2rem_1fr_1fr] items-end gap-2"
index={index}
row={row}
group={props}
onType={(axis, value) => change(index, axis, value)}
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 && (
{!props.locked && (
<VertexButtons
fields={fields}
selected={selected}
@@ -250,6 +245,45 @@ function PolygonRows(props: RowsProps<PolygonFields>) {
)
}
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,