feat(editor): edit mode of the references dialog

« Modifier » turns the references dialog into a draft of adds and removes,
saved as one update on the current citations. A new layer, citations,
guards it between the finding and the reference dialogs: close, Back,
POP refusal and the leave cascade ask it. « Modifier la référence » and
« Nouvelle référence » stack the reference dialog over it; a creation it
started joins its draft.
This commit is contained in:
bchanot committed 2026-11-15 16:39:43 +01:00
1 parent d9dbcf1609
commit 3dc5820e3a
29 files changed
+1489 -243

No files matched your search

+39 -14
View File
@@ -1,4 +1,4 @@
import { useCallback, useRef, useState, type RefObject } from 'react'
import { useCallback, useRef, useState } from 'react'
import type { ElementKey } from '@faunosteo/core-wasm/types'
import type { LayerContext, MoveTrace } from './useEditorNavigation'
@@ -10,12 +10,19 @@ export interface AddedReference {
type Opener = HTMLElement | null | undefined
/** What a creation started from the citations dialog's edit form does with the new id. */
export type CiteCreated = (id: string) => void
/** What the reference dialog (R2F-5) and the References tab's filter (R2F-6) do to the view. */
export interface ReferenceMoves {
/** The dialog of reference `id`, over the place the view shows. */
open: (id: string, opener?: Opener) => void
/** The creation dialog, over the References tab. */
start: (opener?: Opener) => void
/**
* The creation dialog, over the References tab or the citations dialog. `cite` is the
* citations edit form that started it (Rev 5): the new id goes to its draft, and the
* library shows no « added » notice for it.
*/
start: (opener?: Opener, cite?: CiteCreated) => void
/** A user close; false when the form refused it (a dirty draft asks first). */
close: (forced?: boolean) => boolean
/**
@@ -35,19 +42,27 @@ export interface ReferenceMoves {
backToFinding: (key: ElementKey) => void
}
/** What the open dialog was started from: its opener, and a citing form for a creation. */
interface Held {
opener: Opener
cite: CiteCreated | null
}
/** The moves that add the dialog: an edit of a reference, a creation. */
function useOpening({ dispatch, mark }: LayerContext, held: RefObject<Opener>) {
function useOpening({ dispatch, mark }: LayerContext, held: Held) {
const open = useCallback(
(id: string, opener?: Opener) => {
held.current = opener
held.opener = opener
held.cite = null
mark()
dispatch({ type: 'open_reference', id })
},
[dispatch, mark, held],
)
const start = useCallback(
(opener?: Opener) => {
held.current = opener
(opener?: Opener, cite?: CiteCreated) => {
held.opener = opener
held.cite = cite ?? null
mark()
dispatch({ type: 'create_reference' })
},
@@ -89,14 +104,24 @@ function useClosing(ctx: LayerContext) {
return { close: useClose(ctx), replace: useReplace(ctx) }
}
/** A creation that succeeded: its entry becomes the reference's own, then it closes. */
function useCreated(ctx: LayerContext, replace: ReferenceMoves['replace']) {
/**
* A creation that succeeded: its entry becomes the reference's own, then it closes. The
* citing form that started it takes the id (no notice); else the library says it.
*/
function useCreated(
ctx: LayerContext,
replace: ReferenceMoves['replace'],
held: Held,
) {
const { dispatch, mark } = ctx
const [added, setAdded] = useState<AddedReference | null>(null)
const created = useCallback(
(id: string) => {
const citing = held.cite
held.cite = null
replace(id)
setAdded((last) => ({ id, serial: (last?.serial ?? 0) + 1 }))
if (citing) citing(id)
else setAdded((last) => ({ id, serial: (last?.serial ?? 0) + 1 }))
// Precondition: the caller is a discrete event (the form's submit). React then
// renders the replace and runs the history effect before this microtask, so the
// entry is the reference's own before the close goes back over it and a Forward
@@ -108,7 +133,7 @@ function useCreated(ctx: LayerContext, replace: ReferenceMoves['replace']) {
dispatch({ type: 'close_reference' })
})
},
[replace, mark, dispatch],
[replace, mark, dispatch, held],
)
return { created, added }
}
@@ -139,16 +164,16 @@ export function useReferenceMoves(
ctx: LayerContext,
trace: MoveTrace,
): ReferenceMoves {
const held = useRef<Opener>(null)
const held = useRef<Held>({ opener: null, cite: null }).current
const opener = useCallback((): HTMLElement | null => {
const element = held.current
const element = held.opener
return element?.isConnected && !element.closest('[hidden]') ? element : null
}, [held])
const closing = useClosing(ctx)
return {
...useOpening(ctx, held),
...closing,
...useCreated(ctx, closing.replace),
...useCreated(ctx, closing.replace, held),
...useFilter(ctx, trace),
opener,
}