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

+26 -19
View File
@@ -11,9 +11,10 @@ import {
} from '@/components/ui/afterModalsClosed'
import { focusActiveTab } from '../focusRequest'
import { LAYERS_TOP_DOWN } from '../leftDraft'
import { sameView, type EditorView } from '../navigation'
import { citationsOfView, sameView, type EditorView } from '../navigation'
import {
headingOf,
layersAbove,
shownPlace,
useMoveText,
type EditorNavigation,
@@ -211,23 +212,26 @@ function sameLayers(a: EditorView, b: EditorView): boolean {
)
}
/** The finding dialog a view shows, without its citations dialog: that one holds no form. */
/** The finding dialog a view shows, without its citations dialog (a layer of its own). */
function dialogOf({ finding }: EditorView): unknown {
return finding && 'key' in finding ? { key: finding.key } : finding
}
const same = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b)
/**
* The layers holding a form that a POP from `from` to `to` takes away, top down: the
* citations dialog alone is no such layer, so a Back that only closes it asks nothing.
* The layers that a POP from `from` to `to` takes away, top down: the reference dialog
* changed, the citations dialog closed or shown for another finding, the finding dialog
* changed. Each is asked; one holding no draft (no guard, or a clean one) lets it go.
*/
function leavingLayers(from: EditorView, to: EditorView): LayerName[] {
const same = (a: unknown, b: unknown) =>
JSON.stringify(a) === JSON.stringify(b)
const reference = from.reference && !same(from.reference, to.reference)
const finding = from.finding && !same(dialogOf(from), dialogOf(to))
return LAYERS_TOP_DOWN.filter((l) =>
l === 'reference' ? reference : finding,
)
const cited = citationsOfView(from)
const leaving = {
reference: !!from.reference && !same(from.reference, to.reference),
citations: !!cited && !same(cited, citationsOfView(to)),
finding: !!from.finding && !same(dialogOf(from), dialogOf(to)),
}
return LAYERS_TOP_DOWN.filter((layer) => leaving[layer])
}
/**
@@ -249,12 +253,14 @@ function refusal(
return leaving.find((layer) => !nav.guards.ask(layer, false).allowed) ?? null
}
/** The finding dialog alone: the clean citations and reference dialogs over it go. */
function bareFinding(view: EditorView): EditorView {
/** `view` without the dialogs over `layer`: the clean layers above the one that asks. */
function upTo(view: EditorView, layer: LayerName): EditorView {
const above = layersAbove(layer)
const { finding } = view
const shown =
finding && 'key' in finding ? { ...finding, citations: false } : finding
return { ...view, finding: shown, reference: null }
const closes = above.includes('citations') && finding && 'key' in finding
const shown = closes ? { ...finding, citations: false } : finding
const reference = above.includes('reference') ? null : view.reference
return { ...view, finding: shown, reference }
}
interface PopContext {
@@ -278,15 +284,16 @@ function landOnForeign(ctx: PopContext): Landing | null {
/**
* A POP refused (see `refusal`): the view stays, pushed again above the entry landed on.
* When the finding dialog refused, the clean citations and reference dialogs over it close
* first, so its question is not hidden under them. True when refused.
* The clean dialogs over the layer that refused close first (the reference dialog over a
* citations draft; both over a finding draft), so its question is not hidden under them.
* True when refused.
*/
function refusePop(ctx: PopContext, landed: HistoryEntry | null): boolean {
const refused = refusal(ctx, landed)
if (!refused) return false
const { book, writer, view } = ctx
if (landed) adopt(book, landed)
const kept = refused === 'finding' ? bareFinding(view) : view
const kept = refused === 'modal' ? view : upTo(view, refused)
if (!sameView(kept, view)) ctx.nav.restoreView(kept)
writer.push(kept)
return true
+26 -20
View File
@@ -1,11 +1,13 @@
import { useCallback, useEffect, useRef } from 'react'
import { useLocation } from 'react-router-dom'
import { topDirtyLayer } from '../leftDraft'
import type {
EditorNavigation,
Guards,
LayerExit,
LayerName,
import { citationsOfView } from '../navigation'
import {
layersAbove,
type EditorNavigation,
type Guards,
type LayerExit,
type LayerName,
} from '../useEditorNavigation'
/** The part of a Navigation API `navigate` event this guard reads (not in the DOM types). */
@@ -41,10 +43,11 @@ function leavesEditor(event: NavigateEventLike, path: string): boolean {
interface Watch {
guards: Guards
/**
* Closes the citations dialog and a reference dialog over it: they lie over the finding
* dialog, so its question would be hidden under them.
* Closes the dialogs over `layer` (clean: it is the top dirty one), so its question is
* not hidden under them: the reference dialog over the citations dialog, both over the
* finding dialog.
*/
clearAbove: () => void
clearAbove: (layer: LayerName) => void
path: string
/** The key of the jump the user confirmed: it passes, and leaves no flag. */
passing: { current: string | null }
@@ -72,15 +75,14 @@ function jumpTo({ api, key, watch }: Jump, keep: () => void): void {
}
/**
* `layer` asks « Abandonner les modifications ? » and holds the jump. Its « Abandonner »
* closes it, with the citations dialog it may have been opened from (so the next question
* is not hidden under the citations dialog), and the next dirty layer under it asks in turn; the
* last one lets the jump go. Any « Continuer la saisie » drops the jump. The finding
* dialog asks once the layers over it are closed (they are clean: it is the top dirty one).
* `layer` asks « Abandonner les modifications ? » and holds the jump, once the layers over
* it are closed (they are clean: it is the top dirty one). Its « Abandonner » closes it,
* and the next dirty layer under it asks in turn; the last one lets the jump go. Any
* « Continuer la saisie » drops the jump.
*/
function askLeave(jump: Jump, layer: LayerName): void {
const { guards, clearAbove } = jump.watch
if (layer === 'finding') clearAbove()
clearAbove(layer)
guards.ask(layer, false)
guards.holdLeave({
layer,
@@ -113,13 +115,17 @@ type LeaveNav = Pick<
'guards' | 'state' | 'closeCitations' | 'references'
>
/** Closes the citations and reference dialogs of the navigation `latest` holds, if open. */
function clearerOf(latest: { current: LeaveNav }): () => void {
return () => {
/**
* Closes the open dialogs over `layer` of the navigation `latest` holds. Forced: they hold
* no draft, or one just abandoned (the state read may still show it, one commit behind).
*/
function clearerOf(latest: { current: LeaveNav }): (layer: LayerName) => void {
return (layer) => {
const { state, closeCitations, references } = latest.current
const { finding } = state
if (finding && 'key' in finding && finding.citations) closeCitations()
if (state.reference) references.close(true)
const above = layersAbove(layer)
if (above.includes('reference') && state.reference) references.close(true)
const cited = above.includes('citations') && citationsOfView(state)
if (cited) closeCitations(true)
}
}
+8 -2
View File
@@ -2,7 +2,12 @@ import { useEffect, useRef, useState } from 'react'
import type { HistoryOwner } from './history/entry'
import { fingerprintOf, randomHex } from './history/fingerprint'
import type { NavigationState } from './navigation'
import type { EditorNavigation, Guards, LayerName } from './useEditorNavigation'
import {
LAYERS,
type EditorNavigation,
type Guards,
type LayerName,
} from './useEditorNavigation'
/**
* A dirty dialog lost when the editor goes (a Back of several entries the browser does
@@ -24,7 +29,7 @@ export const leftDraftKey = ({ sheetId, tag }: HistoryOwner) =>
`${PREFIX}${sheetId}:${tag}`
/** The top layer first: the dialog the user was typing in when they left. */
export const LAYERS_TOP_DOWN: readonly LayerName[] = ['reference', 'finding']
export const LAYERS_TOP_DOWN: readonly LayerName[] = [...LAYERS].reverse()
/**
* The top layer whose form holds changes, or the first one under `below`; a forced ask
@@ -55,6 +60,7 @@ function elementOf(state: NavigationState, layer: LayerName): unknown {
? { kind: 'reference', id: reference.edit }
: null
}
// The citations dialog's element is the finding whose citations it edits.
const { finding } = state
if (!finding) return null
return 'key' in finding ? finding.key : finding.create.parent
+7 -2
View File
@@ -291,9 +291,14 @@ function openReference(state: NavigationState, id: string): NavigationState {
return shown({ ...state, reference: { edit: id } })
}
/** The creation form of a reference: only the References tab adds one. */
/**
* The creation form of a reference: over the References tab, or over the citations dialog
* (its edit mode cites the new reference, Rev 5), which keeps its tab.
*/
function createReference(state: NavigationState): NavigationState {
return shown({ ...state, tab: 'references', reference: { create: true } })
const reference = { create: true } as const
if (citationsOfView(state)) return shown({ ...state, reference })
return shown({ ...state, tab: 'references', reference })
}
export function navigationReducer(
@@ -0,0 +1,41 @@
/**
* The draft of the references dialog's edit mode (Rev 5): what the user added and removed,
* never a whole list. It is applied to the element's CURRENT citations at save, so a
* change made elsewhere meanwhile (an undo, a reload) is kept, never overwritten.
*/
export interface CitationEdits {
added: readonly string[]
removed: readonly string[]
}
export const NO_EDITS: CitationEdits = { added: [], removed: [] }
export const sameEdits = (a: CitationEdits, b: CitationEdits) =>
JSON.stringify(a) === JSON.stringify(b)
/** `current` with the removals dropped and the additions appended, each id once. */
export function applyEdits(
current: readonly string[],
edits: CitationEdits,
): string[] {
const kept = current.filter((id) => !edits.removed.includes(id))
return [...kept, ...edits.added.filter((id) => !kept.includes(id))]
}
/** Cites `id`: undoes its removal, else adds it. */
export function addId(edits: CitationEdits, id: string): CitationEdits {
if (edits.removed.includes(id)) {
return { ...edits, removed: edits.removed.filter((other) => other !== id) }
}
if (edits.added.includes(id)) return edits
return { ...edits, added: [...edits.added, id] }
}
/** Removes `id`: undoes its addition, else records the removal. */
export function removeId(edits: CitationEdits, id: string): CitationEdits {
if (edits.added.includes(id)) {
return { ...edits, added: edits.added.filter((other) => other !== id) }
}
if (edits.removed.includes(id)) return edits
return { ...edits, removed: [...edits.removed, id] }
}
+39 -19
View File
@@ -53,8 +53,12 @@ export interface LeaveAnswer {
}
export type CanLeave = (forced: boolean) => LeaveAnswer
/** The layers that can hold a form the user must settle before leaving, top one last. */
const LAYERS = ['finding', 'reference'] as const
/**
* The layers that can hold a form the user must settle before leaving, in stacking order
* (top one last): the finding dialog, the citations dialog over it (its edit mode, Rev 5),
* the reference dialog over either.
*/
export const LAYERS = ['finding', 'citations', 'reference'] as const
export type LayerName = (typeof LAYERS)[number]
/** One guard per layer (R2F-1): each layer registers its own check while it is mounted. */
@@ -132,8 +136,11 @@ export interface EditorNavigation {
replaceFinding: (key: ElementKey | null, text?: string) => void
/** Opens the citations dialog of a family or a node; any other key is ignored. */
openCitations: (citedBy: ElementKey, opener?: Opener) => void
/** A user close of the citations dialog. */
closeCitations: () => boolean
/**
* A close of the citations dialog: a user one is refused by its draft (it asks first),
* a forced one goes (the draft is dropped).
*/
closeCitations: (forced?: boolean) => boolean
/** « X références », where the focus returns once the citations dialog is closed. */
citationsTrigger: () => HTMLElement | null
/**
@@ -386,7 +393,7 @@ function useFindingClosing({ dispatch, announce, guards, mark }: LayerContext) {
/** The citations dialog over the finding dialog: opened for a family or a node only. */
function useCitationsLayer(ctx: LayerContext, openers: Openers) {
const { dispatch, mark } = ctx
const { dispatch, guards, mark } = ctx
const openCitations = useCallback(
(citedBy: ElementKey, opener?: Opener) => {
if (!isCitable(citedBy)) return
@@ -397,35 +404,48 @@ function useCitationsLayer(ctx: LayerContext, openers: Openers) {
},
[openers, dispatch, mark],
)
const closeCitations = useCallback(() => {
mark()
dispatch({ type: 'close_citations' })
return true
}, [dispatch, mark])
const closeCitations = useCallback(
(forced = false) => {
if (!guards.ask('citations', forced).allowed) return false
mark()
dispatch({ type: 'close_citations' })
return true
},
[guards, dispatch, mark],
)
return { openCitations, closeCitations }
}
/** The layers stacked over `layer`, top one last. */
export const layersAbove = (layer: LayerName): LayerName[] =>
LAYERS.slice(LAYERS.indexOf(layer) + 1)
/**
* Asks every layer, top down, before a move that leaves them all; true when none refused.
* When the finding dialog is the top one to refuse, the clean dialogs over it close first
* so its question is the one in view (as a refused Back, `bareFinding`).
* The clean dialogs over the top one to refuse close first so its question is the one in
* view (as a refused Back, `refusePop`).
*/
function useAskLeaving(ctx: LayerContext, state: NavigationState) {
const { guards, dispatch, mark } = ctx
const cited = citationsOfView(state) !== null
const citations = citationsOfView(state) !== null
const reference = state.reference !== null
return useCallback((): boolean => {
const refused = [...LAYERS]
.reverse()
.filter((layer) => !guards.ask(layer, false).allowed)
if (refused[0] !== 'finding' || !(cited || reference)) {
return refused.length === 0
}
if (refused.length === 0) return true
const above = layersAbove(refused[0])
const shown = { finding: false, citations, reference }
if (!above.some((layer) => shown[layer])) return false
mark()
if (reference) dispatch({ type: 'close_reference' })
if (cited) dispatch({ type: 'close_citations' })
if (above.includes('reference') && reference) {
dispatch({ type: 'close_reference' })
}
if (above.includes('citations') && citations) {
dispatch({ type: 'close_citations' })
}
return false
}, [guards, dispatch, mark, cited, reference])
}, [guards, dispatch, mark, citations, reference])
}
/** « Aller à » and « Tout afficher » leave every layer: a layer in edit may refuse. */
+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,
}