feat(editor): drawing guard before navigations drop a started move (R-12)

The move mode lives in the work store with its document stamp. guardDrawing
ends an unstarted mode at once and asks « Abandonner le déplacement ? »
(DrawingGuardDialog, at workspace level) once work is started, before a
tab, a section, « Aller à », Back and a history jump out of the editor;
the leave guard and sign-out count started work. An undo from the header,
Ctrl+Z or the message only cancels a pending mode or preview (R-13), and a
document change drops a stale mode, keeping a started one to be said.
ConfirmDialog takes an optional body. Strings for Task 4 added.
This commit is contained in:
bchanot committed 2026-11-17 18:26:49 +01:00
1 parent 8cd1bda6c2
commit 2882821661
18 files changed
+465 -37

No files matched your search

@@ -0,0 +1,27 @@
import { endMode, guardWork, useAnnotationWork } from './workStore'
/**
* 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) {
endMode()
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()
}
+69 -8
View File
@@ -22,44 +22,105 @@ export interface Creation {
/** The dialog is shown; false while the shape is replaced or the trace resumed. */
dialog: boolean
}
/** A move mode or a redraw; `started` once the position differs or a vertex is placed (R-12). */
/**
* A move mode or a redraw, stamped with the document text it started on; `started` once
* the position differs or a vertex is placed (R-12).
*/
export interface ModeWork {
kind: 'move' | 'redraw'
annotationId: string
started: boolean
base: string
}
export type GuardWork = 'creation' | 'move' | 'redraw' | null
/** A navigation held by the drawing guard (R-12): what it asks about, and the move itself. */
export interface DrawingQuestion {
work: Exclude<GuardWork, null>
proceed: () => void
}
interface Work {
creation: Creation | null
mode: ModeWork | null
preview: Preview | null
chosen: Chosen | null
/** A started mode a document change dropped (R-13): the tab says so, then clears it. */
dropped: ModeWork['kind'] | null
question: DrawingQuestion | null
}
const NO_WORK: Work = {
creation: null,
mode: null,
preview: null,
chosen: null,
dropped: null,
question: null,
}
export const useAnnotationWork = create<Work>(() => NO_WORK)
export type GuardWork = 'creation' | 'move' | 'redraw' | null
/** R-12: what a navigation must ask about; an unstarted mode asks nothing and simply ends. */
export function guardWork(work: Work): GuardWork {
if (work.creation !== null) return 'creation'
return work.mode?.started ? work.mode.kind : null
}
/** The end of a mode without a question (unstarted, or « Abandonner » answered). */
/** The end of a mode without a question (unstarted, « Annuler », or « Abandonner »). */
export function endMode(): void {
useAnnotationWork.setState({ mode: null, preview: null })
}
/** R-13: drop what was computed on another document text; the caller passes the current text. */
/** « Déplacer … »: the mode on `annotationId`, nothing moved yet. */
export function startMove(annotationId: string, base: string): void {
useAnnotationWork.setState({
mode: { kind: 'move', annotationId, started: false, base },
preview: null,
chosen: null,
})
}
const sameGeometry = (a: Geometry, b: Geometry) =>
JSON.stringify(a) === JSON.stringify(b)
/** The move mode's target at `geometry`; started while it differs from `stored`. */
export function setModePreview(geometry: Geometry, stored: Geometry): void {
const { mode } = useAnnotationWork.getState()
if (mode?.kind !== 'move') return
const started = !sameGeometry(geometry, stored)
const { annotationId, base } = mode
useAnnotationWork.setState({
mode: { ...mode, started },
preview: { annotationId, geometry, base, source: 'move' },
})
}
/**
* R-13: an undo while a mode, a slider or a keyboard series is pending only cancels it.
* Answers whether there was something to cancel.
*/
export function cancelPending(): boolean {
const { mode, preview } = useAnnotationWork.getState()
if (mode === null && preview === null) return false
endMode()
return true
}
const fresh = <T extends { base: string }>(item: T | null, text: string) =>
item && item.base !== text ? null : item
/**
* R-13: drop what was computed on another document text; the caller passes the current
* text. A started mode dropped this way is kept in `dropped`, to be said.
*/
export function dropStale(text: string): void {
useAnnotationWork.setState((work) => ({
preview: work.preview && work.preview.base !== text ? null : work.preview,
chosen: work.chosen && work.chosen.base !== text ? null : work.chosen,
}))
useAnnotationWork.setState((work) => {
const mode = fresh(work.mode, text)
const lost = work.mode !== null && mode === null && work.mode.started
return {
mode,
preview: mode === null && work.mode ? null : fresh(work.preview, text),
chosen: fresh(work.chosen, text),
dropped: lost ? work.mode!.kind : work.dropped,
}
})
}
/** Another sheet opened, or the editor closed: nothing of the previous one survives. */