import { endMode, guardWork, useAnnotationWork } from './workStore' /** * A keyboard series or a size change still pending (no blur yet: a mouse Back button) * commits now, through its own blur (BDR-050): leaving never drops it. */ export function commitFocusedSeries(): void { const { preview } = useAnnotationWork.getState() if (!preview || preview.source === 'move') return const active = document.activeElement const series = active instanceof HTMLElement || active instanceof SVGElement if (series && active.closest('[data-series]')) active.blur() } /** An unstarted mode ends; a pending series that is not the mode's stays. */ function endModeOnly(): void { useAnnotationWork.setState((work) => ({ mode: null, preview: work.preview?.source === 'move' ? null : work.preview, })) } /** * The drawing guard (R-12), asked by every navigation that would drop the drawing's work: * a tab, a section, « Aller à », a view chip, a tool, Back, leaving the editor. Nothing * started: an unstarted mode ends at once and the caller goes on (true). Work started: * the question is shown (`DrawingGuardDialog`) and `proceed` waits for « Abandonner » * (false). */ export function guardDrawing(proceed: () => void): boolean { const work = guardWork(useAnnotationWork.getState()) if (work === null) { commitFocusedSeries() endModeOnly() return true } useAnnotationWork.setState({ question: { work, proceed } }) return false } /** « Continuer » keeps the work as it is; « Abandonner » drops it and makes the move. */ export function answerDrawing(discard: boolean): void { const { question } = useAnnotationWork.getState() useAnnotationWork.setState({ question: null }) if (!discard || !question) return endMode() question.proceed() }