refactor(app): split the crop group hook, closed reason in drawing refusals
This commit is contained in:
1 parent
1bbf97e65b
commit
7ea9619aa0
3 files changed
+82
-25
No files matched your search
@@ -23,13 +23,24 @@ export interface CropGroup {
|
||||
const differs = (a: BoxFields, b: BoxFields) =>
|
||||
BOX_KEYS.some((key) => a[key] !== b[key])
|
||||
|
||||
type Proposal =
|
||||
| { kind: 'invalid'; reason: string }
|
||||
| { kind: 'same' }
|
||||
| { kind: 'change'; box: ViewBox | null }
|
||||
|
||||
function proposalOf(draft: BoxFields, box: ViewBox | null): Proposal {
|
||||
const parsed = parseBox(draft)
|
||||
if (!parsed.ok) return { kind: 'invalid', reason: parsed.reason }
|
||||
return sameBox(parsed.box, box)
|
||||
? { kind: 'same' }
|
||||
: { kind: 'change', box: parsed.box }
|
||||
}
|
||||
|
||||
/**
|
||||
* The four `view_box` fields as one group: `apply` proposes all four together under the
|
||||
* geometry check; a refused group stays typed and `refused`. A new saved box (undo, reload,
|
||||
* restore, an accepted apply) replaces what was typed; unmounting applies a pending group.
|
||||
* The typed fields and the refused flag. A new saved box (undo, reload, restore, an
|
||||
* accepted apply) replaces what was typed.
|
||||
*/
|
||||
export function useCropGroup(viewId: string, box: ViewBox | null): CropGroup {
|
||||
const views = useViewUpdate()
|
||||
function useCropDraft(box: ViewBox | null) {
|
||||
const saved = fieldsOf(box)
|
||||
const [draft, setDraft] = useState<BoxFields>(saved)
|
||||
const [seen, setSeen] = useState<BoxFields>(saved)
|
||||
@@ -39,35 +50,50 @@ export function useCropGroup(viewId: string, box: ViewBox | null): CropGroup {
|
||||
setDraft(saved)
|
||||
setRefused(false)
|
||||
}
|
||||
const apply = (): void => {
|
||||
const parsed = parseBox(draft)
|
||||
if (!parsed.ok) {
|
||||
views.reject('crop', `editor.views.crop.${parsed.reason}`)
|
||||
setRefused(true)
|
||||
} else if (sameBox(parsed.box, box)) {
|
||||
views.clear()
|
||||
setRefused(false)
|
||||
} else {
|
||||
const done = views.update('crop', viewId, { view_box: parsed.box }, true)
|
||||
setRefused(!done)
|
||||
}
|
||||
}
|
||||
const latest = useRef({ draft, saved, apply })
|
||||
return { saved, draft, setDraft, refused, setRefused }
|
||||
}
|
||||
|
||||
/** Runs the latest `flush` once, when the component unmounts with `pending` true. */
|
||||
function useFlushOnUnmount(pending: boolean, flush: () => void): void {
|
||||
const latest = useRef({ pending, flush })
|
||||
useEffect(() => {
|
||||
latest.current = { draft, saved, apply }
|
||||
latest.current = { pending, flush }
|
||||
})
|
||||
useEffect(
|
||||
() => () => {
|
||||
if (differs(latest.current.draft, latest.current.saved)) {
|
||||
latest.current.apply()
|
||||
}
|
||||
if (latest.current.pending) latest.current.flush()
|
||||
},
|
||||
[],
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The four `view_box` fields as one group: `apply` proposes all four together under the
|
||||
* geometry check; a refused group stays typed and `refused`. Unmounting applies a pending
|
||||
* group.
|
||||
*/
|
||||
export function useCropGroup(viewId: string, box: ViewBox | null): CropGroup {
|
||||
const views = useViewUpdate()
|
||||
const form = useCropDraft(box)
|
||||
const { saved, draft, setDraft, setRefused } = form
|
||||
const apply = (): void => {
|
||||
const proposal = proposalOf(draft, box)
|
||||
if (proposal.kind === 'invalid') {
|
||||
views.reject('crop', `editor.views.crop.${proposal.reason}`)
|
||||
setRefused(true)
|
||||
} else if (proposal.kind === 'same') {
|
||||
views.clear()
|
||||
setRefused(false)
|
||||
} else {
|
||||
const patch = { view_box: proposal.box }
|
||||
setRefused(!views.update('crop', viewId, patch, true))
|
||||
}
|
||||
}
|
||||
useFlushOnUnmount(differs(draft, saved), apply)
|
||||
const shown = parseBox(draft)
|
||||
return {
|
||||
draft,
|
||||
refused,
|
||||
refused: form.refused,
|
||||
views,
|
||||
rectangle: shown.ok ? shown.box : box,
|
||||
setField: (key, value) => setDraft({ ...draft, [key]: value }),
|
||||
|
||||
Reference in new issue
Block a user