fix(editor): free paths never reshaped on attach, category zone chosen explicitly

This commit is contained in:
bchanot committed 2026-11-04 22:14:59 +01:00
1 parent f1175bb3a1
commit a69eba569b
13 files changed
+333 -117

No files matched your search

@@ -3,13 +3,18 @@ import type { PendingFinding } from '@/components/editor/findings/FindingCreateF
import { canAddNode, type FindingItem } from '@/editor/findings/tree'
import { useT } from '@/i18n/useT'
/** « Ajouter une zone / une catégorie / un constat »: each starts a creation form. */
/**
* « Ajouter une zone / une catégorie / un constat »: each starts a creation form. Only
* the node depends on the selection; a category names its zone in its own form (C46).
*/
export default function AddBar({
selected,
hasZone,
readOnly,
onStart,
}: {
selected: FindingItem | undefined
hasZone: boolean
readOnly: boolean
onStart: (pending: PendingFinding) => void
}) {
@@ -28,8 +33,8 @@ export default function AddBar({
readOnly={readOnly}
variant="secondary"
size="sm"
disabled={!selected}
onClick={() => onStart({ kind: 'category', parent: selected ?? null })}
disabled={!hasZone}
onClick={() => onStart({ kind: 'category', parent: null })}
>
{t('editor.findings.add_category')}
</ActionButton>
@@ -42,6 +47,11 @@ export default function AddBar({
>
{t('editor.findings.add_node')}
</ActionButton>
{!hasZone && !readOnly && (
<p className="basis-full text-sm text-[var(--color-ink-muted)]">
{t('editor.findings.add_category_no_zone')}
</p>
)}
</div>
)
}
@@ -7,9 +7,13 @@ import type {
import ActionButton from '@/components/editor/fields/ActionButton'
import DraftField from '@/components/editor/fields/DraftField'
import SelectField from '@/components/editor/fields/SelectField'
import type { FindingItem, FindingKind } from '@/editor/findings/tree'
import {
buildItems,
type FindingItem,
type FindingKind,
} from '@/editor/findings/tree'
import type { IdPrefix } from '@/editor/ids'
import { elementLabel, isBlank, useSheetDocument } from '@/editor/sheetDocument'
import { isBlank, useSheetDocument } from '@/editor/sheetDocument'
import { useEditCommand } from '@/editor/useEditCommand'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
@@ -23,7 +27,7 @@ export const CATEGORY_KINDS: readonly FindingCategoryKind[] = [
export interface PendingFinding {
kind: FindingKind
/** The zone, category or node the new element goes under; null for a zone. */
/** The category or node a node goes under; null for a zone and a category. */
parent: FindingItem | null
}
@@ -37,18 +41,24 @@ export default function FindingCreateForm({
onCancel,
}: {
pending: PendingFinding
onCreated: (id: string) => void
onCreated: (id: string, parent: FindingItem | null) => void
onCancel: () => void
}) {
const t = useT()
const title = useFormTitle(pending)
const edit = useEditCommand()
const zones = useZoneChoice()
const title = useFormTitle(pending, zones.chosen)
const [fr, setFr] = useState('')
const [en, setEn] = useState('')
const [kind, setKind] = useState('')
const create = () => {
const parent = pending.kind === 'category' ? zones.chosen : pending.parent
const missingParent = pending.kind === 'category' && !parent
const incomplete =
isBlank(fr) || isBlank(en) || (pending.kind === 'category' && !kind)
isBlank(fr) ||
isBlank(en) ||
missingParent ||
(pending.kind === 'category' && !kind)
if (incomplete) {
edit.reject('form', 'editor.findings.required')
return
@@ -56,14 +66,16 @@ export default function FindingCreateForm({
let createdId = ''
const added = edit.runNew('form', PREFIX[pending.kind], (id) => {
createdId = id
return insertCommand(pending, id, { fr: fr.trim(), en: en.trim() }, kind)
const name = { fr: fr.trim(), en: en.trim() }
return insertCommand({ ...pending, parent }, id, name, kind)
})
if (added) onCreated(createdId)
if (added) onCreated(createdId, parent)
}
return (
<div className="space-y-2 rounded-lg border border-dashed border-[var(--color-border)] p-3">
<p className="text-sm font-medium">{title}</p>
<div className="grid gap-3 md:grid-cols-2">
{pending.kind === 'category' && <ZoneSelect zones={zones} />}
{pending.kind === 'category' && (
<SelectField
id="finding-new-kind"
@@ -109,19 +121,58 @@ export default function FindingCreateForm({
)
}
/** A category lands at zone level, so its title names the zone, whatever is selected. */
function useFormTitle(pending: PendingFinding): string {
const t = useT()
interface ZoneChoice {
zones: readonly FindingItem[]
zoneId: string
setZoneId: (id: string) => void
chosen: FindingItem | null
}
/** The zones to choose from; with exactly one, it is preselected, otherwise nothing is. */
function useZoneChoice(): ZoneChoice {
const doc = useSheetDocument()
const zones = doc ? buildItems(doc).filter((i) => i.kind === 'zone') : []
const [zoneId, setZoneId] = useState(
zones.length === 1 ? zones[0].zoneId : '',
)
const chosen = zones.find((zone) => zone.zoneId === zoneId) ?? null
return { zones, zoneId, setZoneId, chosen }
}
function ZoneSelect({ zones }: { zones: ZoneChoice }) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
if (pending.kind !== 'category') return t(`editor.findings.new.${pending.kind}`)
if (!doc || !pending.parent) return t('editor.findings.add_category')
const zone = elementLabel(
doc,
{ kind: 'zone', id: pending.parent.zoneId },
locale,
return (
<SelectField
id="finding-new-zone"
label={t('editor.findings.zone')}
value={zones.zoneId}
options={[
{ value: '', label: t('editor.field.choose') },
...zones.zones.map((zone) => ({
value: zone.zoneId,
label: zone.name[locale],
})),
]}
onCommit={zones.setZoneId}
/>
)
}
/** A category lands at zone level: its title names the zone chosen in the form. */
function useFormTitle(
pending: PendingFinding,
zone: FindingItem | null,
): string {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
if (pending.kind !== 'category')
return t(`editor.findings.new.${pending.kind}`)
if (!zone) return t('editor.findings.add_category')
return t('editor.findings.new.category_in_zone').replace(
'{zone}',
zone.name[locale],
)
return t('editor.findings.new.category_in_zone').replace('{zone}', zone)
}
const PREFIX: Record<FindingKind, IdPrefix> = {
@@ -146,7 +197,7 @@ function insertCommand(
if (pending.kind === 'category') {
return {
op: 'insert',
parent: { kind: 'zone', id: pending.parent?.zoneId ?? '' },
parent: pending.parent?.key,
element: {
kind: 'finding_category',
value: {
@@ -72,10 +72,10 @@ export default function FindingsTab({ readOnly, focus }: EditorTabProps) {
setSelectedId(found.item.id)
setFocusId({ id: found.item.id })
})
const created = (id: string) => {
const created = (id: string, parent: FindingItem | null) => {
if (!pending) return
if (pending.parent) toggle(pending.parent.id, true)
setSelectedId(createdItemId(pending.kind, pending.parent, id))
if (parent) toggle(parent.id, true)
setSelectedId(createdItemId(pending.kind, parent, id))
setPending(null)
}
const askDelete = (item: FindingItem) => {
@@ -96,7 +96,12 @@ export default function FindingsTab({ readOnly, focus }: EditorTabProps) {
aria-label={t('editor.tab.findings')}
className="min-h-24 space-y-3"
>
<AddBar selected={selected} readOnly={readOnly} onStart={setPending} />
<AddBar
selected={selected}
hasZone={items.some((item) => item.kind === 'zone')}
readOnly={readOnly}
onStart={setPending}
/>
{pending && !readOnly && (
<FindingCreateForm
key={`${pending.kind}-${pending.parent?.id ?? ''}`}
@@ -11,7 +11,6 @@ import {
blockedIds,
initialGeometries,
listedAnnotations,
polygonReplacement,
type RepairOffer,
type RepairResult,
} from '@/editor/repair'
@@ -119,7 +118,6 @@ function RepairForm({
key={annotation.id}
annotation={annotation}
geometry={state.geometries[annotation.id]}
offer={offer}
messages={refused?.byAnnotation[annotation.id]}
onChange={(geometry) => state.change(annotation.id, geometry)}
onPending={state.report}
@@ -155,7 +153,6 @@ function Refusal({ general }: { general: string | null }) {
interface RowProps {
annotation: Annotation
geometry: Geometry | undefined
offer: RepairOffer
messages: string[] | undefined
onChange: (geometry: Geometry) => void
onPending: (id: string, pending: boolean) => void
@@ -164,7 +161,6 @@ interface RowProps {
function RepairRow({
annotation,
geometry,
offer,
messages,
onChange,
onPending,
@@ -194,11 +190,7 @@ function RepairRow({
}}
/>
) : (
<PathBlock
onConvert={() =>
onChange(polygonReplacement(offer.frames[annotation.view_id]))
}
/>
<PathBlock />
)}
{messages && messages.length > 0 && (
<ul
@@ -214,16 +206,11 @@ function RepairRow({
)
}
function PathBlock({ onConvert }: { onConvert: () => void }) {
function PathBlock() {
const t = useT()
return (
<div className="space-y-2">
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.views.repair.path_blocked')}
</p>
<Button type="button" variant="secondary" size="sm" onClick={onConvert}>
{t('editor.annotations.replace_polygon')}
</Button>
</div>
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.views.repair.path_blocked')}
</p>
)
}
+2 -12
View File
@@ -13,12 +13,7 @@ import {
type CommitContext,
} from './annotations/annotationCommit'
import { isEditable } from './annotations/geometryEdit'
import {
centreTriangle,
correctedCircle,
parseFrame,
type Frame,
} from './annotations/frame'
import { correctedCircle, parseFrame, type Frame } from './annotations/frame'
import {
geometryAccept,
rejectedDrawingFault,
@@ -60,7 +55,7 @@ function clampedVertex(frame: Frame, [x, y]: Point): Point {
/**
* A proposal in the new frame: a point goes through the creation arithmetic (candidate
* C), a polygon gets each vertex clamped into the frame. A path has no numbers to move:
* it is returned as it is, and the user converts it.
* it is returned as it is and blocks the attach (owner decision, R25).
*/
export function proposeGeometry(frame: Frame, geometry: Geometry): Geometry {
if (geometry.type === 'circle') {
@@ -74,11 +69,6 @@ export function proposeGeometry(frame: Frame, geometry: Geometry): Geometry {
return geometry
}
/** « Remplacer par un polygone » inside the temporary form: a triangle to edit. */
export function polygonReplacement(frame: Frame): Geometry {
return { type: 'polygon', points: centreTriangle(frame) }
}
/** The annotations the refusal lists, in document order. */
export function listedAnnotations(
offer: RepairOffer,
+7 -2
View File
@@ -1394,7 +1394,8 @@ export const en: Dict = {
'editor.views.repair.body_one': '{count} annotation falls outside the new frame of this drawing. The proposed values are computed in that frame. Nothing is applied before "Attach and apply": the drawing and the new values are then applied together, and a single Undo restores them together.',
'editor.views.repair.body_other': '{count} annotations fall outside the new frame of this drawing. The proposed values are computed in that frame. Nothing is applied before "Attach and apply": the drawing and the new values are then applied together, and a single Undo restores them together.',
'editor.views.repair.row': '{kind} "{name}"',
'editor.views.repair.path_blocked': 'This path has no coordinates to correct here. Replace it with a polygon to be able to attach the drawing.',
'editor.views.repair.path_blocked':
"This free path is kept as it is. The drawing cannot be attached while it falls outside the new frame: the editor cannot yet correct the points of a free path.",
'editor.views.repair.refused': 'Nothing was applied: the document check refuses these values.',
'editor.views.repair.more_outside': 'Other elements would also fall outside the new frame: {names}. Nothing was applied. Cancel, fix them, then choose the drawing again.',
'editor.views.repair.not_measurable': 'With this drawing, a view holding annotations would not be measurable. Nothing was applied.',
@@ -1453,7 +1454,11 @@ export const en: Dict = {
'editor.findings.kind.consultation_reason': 'Reason for consultation',
'editor.findings.kind.anamnesis': 'Anamnesis',
'editor.findings.kind.custom': 'Custom',
'editor.findings.required': 'Fill in the name in French and English (and the type of a category).',
'editor.findings.required':
"Fill in the name in French and in English (and the zone and the type of a category).",
'editor.findings.zone': "Zone",
'editor.findings.add_category_no_zone':
"Create a zone first to add a category.",
'editor.findings.create': 'Create',
'editor.findings.cancel': 'Cancel',
'editor.findings.empty': 'No findings zone yet.',
+7 -2
View File
@@ -1425,7 +1425,8 @@ export const fr: Dict = {
'editor.views.repair.body_one': "{count} annotation sort du nouveau cadre de cette illustration. Les valeurs proposées sont calculées dans ce cadre. Rien n'est appliqué avant « Attacher et appliquer » : l'illustration et les nouvelles valeurs sont alors appliquées ensemble, et un seul Annuler les restaure ensemble.",
'editor.views.repair.body_other': "{count} annotations sortent du nouveau cadre de cette illustration. Les valeurs proposées sont calculées dans ce cadre. Rien n'est appliqué avant « Attacher et appliquer » : l'illustration et les nouvelles valeurs sont alors appliquées ensemble, et un seul Annuler les restaure ensemble.",
'editor.views.repair.row': '{kind} « {name} »',
'editor.views.repair.path_blocked': "Ce tracé n'a pas de coordonnées à corriger ici. Remplacez-le par un polygone pour pouvoir attacher l'illustration.",
'editor.views.repair.path_blocked':
"Ce tracé libre est conservé tel quel. L'illustration ne peut pas être attachée tant qu'il sort du nouveau cadre : l'éditeur ne sait pas encore corriger les points d'un tracé libre.",
'editor.views.repair.refused': "Rien n'a été appliqué : le contrôle du document refuse ces valeurs.",
'editor.views.repair.more_outside': "D'autres éléments sortiraient aussi du nouveau cadre : {names}. Rien n'a été appliqué. Annulez, corrigez-les, puis choisissez de nouveau l'illustration.",
'editor.views.repair.not_measurable': "Avec cette illustration, une vue qui porte des annotations ne serait pas mesurable. Rien n'a été appliqué.",
@@ -1484,7 +1485,11 @@ export const fr: Dict = {
'editor.findings.kind.consultation_reason': 'Motif de consultation',
'editor.findings.kind.anamnesis': 'Anamnèse',
'editor.findings.kind.custom': 'Personnalisée',
'editor.findings.required': "Renseignez le nom en français et en anglais (et le type d'une catégorie).",
'editor.findings.required':
"Renseignez le nom en français et en anglais (et la zone et le type d'une catégorie).",
'editor.findings.zone': "Zone",
'editor.findings.add_category_no_zone':
"Créez d'abord une zone pour ajouter une catégorie.",
'editor.findings.create': 'Créer',
'editor.findings.cancel': 'Annuler',
'editor.findings.empty': "Aucune zone de constats pour l'instant.",