161 lines
4.6 KiB
TypeScript
161 lines
4.6 KiB
TypeScript
import { useEffect, useRef, useState } from 'react'
|
|
import type { ViewBox } from '@faunosteo/core-wasm/types'
|
|
import ActionButton from '@/components/editor/fields/ActionButton'
|
|
import DraftField from '@/components/editor/fields/DraftField'
|
|
import ReadOnlyValue from '@/components/editor/fields/ReadOnlyValue'
|
|
import {
|
|
BOX_KEYS,
|
|
fieldsOf,
|
|
parseBox,
|
|
sameBox,
|
|
type BoxFields,
|
|
} from '@/editor/viewBox'
|
|
import { useViewUpdate } from '@/editor/useViewUpdate'
|
|
import { useT } from '@/i18n/useT'
|
|
import CropCanvas from './CropCanvas'
|
|
|
|
/**
|
|
* The four `view_box` fields as ONE group: leaving the group or Enter proposes all four
|
|
* together (a frame change under the geometry check). A refused group stays shown,
|
|
* marked « non appliqué », with the refusal naming what it would break.
|
|
*/
|
|
export default function CropEditor({
|
|
viewId,
|
|
parentId,
|
|
box,
|
|
readOnly,
|
|
}: {
|
|
viewId: string
|
|
parentId: string
|
|
box: ViewBox | null
|
|
readOnly: boolean
|
|
}) {
|
|
const t = useT()
|
|
if (readOnly) {
|
|
const fields = fieldsOf(box)
|
|
return (
|
|
<div className="grid gap-3 sm:grid-cols-4">
|
|
{BOX_KEYS.map((key) => (
|
|
<ReadOnlyValue
|
|
key={key}
|
|
id={`view-${viewId}-crop-${key}`}
|
|
label={t(`editor.views.crop.${key}`)}
|
|
value={fields[key]}
|
|
/>
|
|
))}
|
|
</div>
|
|
)
|
|
}
|
|
return <EditableCrop viewId={viewId} parentId={parentId} box={box} />
|
|
}
|
|
|
|
type Gate = { state: 'applied' } | { state: 'refused' }
|
|
|
|
function EditableCrop({
|
|
viewId,
|
|
parentId,
|
|
box,
|
|
}: {
|
|
viewId: string
|
|
parentId: string
|
|
box: ViewBox | null
|
|
}) {
|
|
const t = useT()
|
|
const views = useViewUpdate()
|
|
const saved = fieldsOf(box)
|
|
const [draft, setDraft] = useState<BoxFields>(saved)
|
|
const [seen, setSeen] = useState<BoxFields>(saved)
|
|
const [gate, setGate] = useState<Gate>({ state: 'applied' })
|
|
// A new saved box (undo, reload, restore, an accepted apply) replaces what was typed.
|
|
if (BOX_KEYS.some((key) => seen[key] !== saved[key])) {
|
|
setSeen(saved)
|
|
setDraft(saved)
|
|
setGate({ state: 'applied' })
|
|
}
|
|
const apply = (fields: BoxFields): void => {
|
|
const parsed = parseBox(fields)
|
|
if (!parsed.ok) {
|
|
views.reject('crop', `editor.views.crop.${parsed.reason}`)
|
|
setGate({ state: 'refused' })
|
|
} else if (sameBox(parsed.box, box)) {
|
|
views.clear()
|
|
setGate({ state: 'applied' })
|
|
} else {
|
|
const done = views.update('crop', viewId, { view_box: parsed.box }, true)
|
|
setGate({ state: done ? 'applied' : 'refused' })
|
|
}
|
|
}
|
|
const latest = useRef({ draft, saved, apply })
|
|
useEffect(() => {
|
|
latest.current = { draft, saved, apply }
|
|
})
|
|
useEffect(
|
|
() => () => {
|
|
const pending = latest.current
|
|
if (BOX_KEYS.some((key) => pending.draft[key] !== pending.saved[key])) {
|
|
pending.apply(pending.draft)
|
|
}
|
|
},
|
|
[],
|
|
)
|
|
const shown = parseBox(draft)
|
|
const rectangle = shown.ok ? shown.box : box
|
|
const revert = () => {
|
|
setDraft(saved)
|
|
setGate({ state: 'applied' })
|
|
views.clear()
|
|
}
|
|
return (
|
|
<div className="grid gap-4 md:grid-cols-2">
|
|
<div
|
|
role="group"
|
|
aria-label={t('editor.views.crop.title')}
|
|
className="space-y-3"
|
|
onBlur={(event) => {
|
|
if (!event.currentTarget.contains(event.relatedTarget)) apply(draft)
|
|
}}
|
|
onKeyDown={(event) => {
|
|
if (
|
|
event.key === 'Enter' &&
|
|
event.target instanceof HTMLInputElement
|
|
) {
|
|
apply(draft)
|
|
}
|
|
}}
|
|
>
|
|
<p className="text-sm text-[var(--color-ink-muted)]">
|
|
{t('editor.views.crop.hint')}
|
|
</p>
|
|
<div className="grid grid-cols-2 gap-3">
|
|
{BOX_KEYS.map((key) => (
|
|
<DraftField
|
|
key={key}
|
|
id={`view-${viewId}-crop-${key}`}
|
|
label={t(`editor.views.crop.${key}`)}
|
|
value={draft[key]}
|
|
onChange={(value) => setDraft({ ...draft, [key]: value })}
|
|
/>
|
|
))}
|
|
</div>
|
|
{gate.state === 'refused' && (
|
|
<div role="alert" className="space-y-2 text-sm">
|
|
<p className="font-medium text-[var(--color-danger-strong)]">
|
|
{t('editor.views.crop.not_applied')}
|
|
</p>
|
|
{views.error('crop') && <p>{views.error('crop')}</p>}
|
|
<ActionButton
|
|
readOnly={false}
|
|
variant="ghost"
|
|
size="sm"
|
|
onClick={revert}
|
|
>
|
|
{t('editor.views.crop.revert')}
|
|
</ActionButton>
|
|
</div>
|
|
)}
|
|
</div>
|
|
<CropCanvas parentId={parentId} box={rectangle} />
|
|
</div>
|
|
)
|
|
}
|