feat(editor): a reference made in the citations draft saves with it
This commit is contained in:
1 parent
8c3fcca0b9
commit
70eb2fcbb4
15 files changed
+671
-162
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
import type { ElementKey } from '@faunosteo/core-wasm/types'
|
||||
import type { ElementKey, Reference } from '@faunosteo/core-wasm/types'
|
||||
import type { NavigationState } from './navigation'
|
||||
import type { LayerContext, MoveTrace } from './useEditorNavigation'
|
||||
|
||||
@@ -11,8 +11,17 @@ 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 a creation started from the citations dialog's edit form does with its value
|
||||
* (Rev 6): the reference joins that form's draft, nothing is written.
|
||||
*/
|
||||
export type CiteCreated = (value: Reference) => void
|
||||
|
||||
/** A draft creation: the citing form, and the pending reference modified (null: a new one). */
|
||||
export interface Drafting {
|
||||
initial: Reference | null
|
||||
cite: CiteCreated
|
||||
}
|
||||
|
||||
/** What the reference dialog (R2F-5) and the References tab's filter (R2F-6) do to the view. */
|
||||
export interface ReferenceMoves {
|
||||
@@ -20,10 +29,12 @@ export interface ReferenceMoves {
|
||||
open: (id: string, 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.
|
||||
* citations edit form that started it (Rev 6): the dialog writes nothing and hands the
|
||||
* value to its draft; `initial` is the pending reference it modifies.
|
||||
*/
|
||||
start: (opener?: Opener, cite?: CiteCreated) => void
|
||||
start: (opener?: Opener, cite?: CiteCreated, initial?: Reference) => void
|
||||
/** The open dialog's draft creation, read from what started it; null for any other. */
|
||||
drafting: () => Drafting | null
|
||||
/** A user close; false when the form refused it (a dirty draft asks first). */
|
||||
close: (forced?: boolean) => boolean
|
||||
/**
|
||||
@@ -32,7 +43,10 @@ export interface ReferenceMoves {
|
||||
* said once the dialogs are closed.
|
||||
*/
|
||||
replace: (id: string | null, text?: string) => void
|
||||
/** A creation succeeded: its entry becomes the reference's own, then the dialog closes. */
|
||||
/**
|
||||
* A library creation succeeded: its entry becomes the reference's own, then the dialog
|
||||
* closes and the library says it.
|
||||
*/
|
||||
created: (id: string) => void
|
||||
added: AddedReference | null
|
||||
/** The element that opened the dialog, while it is still on the page. */
|
||||
@@ -44,12 +58,19 @@ export interface ReferenceMoves {
|
||||
}
|
||||
|
||||
/**
|
||||
* What the open dialog was started from: its opener, and a citing form for a creation
|
||||
* (forgotten once the dialog closes or is replaced).
|
||||
* What the open dialog was started from: its opener, and for a draft creation the citing
|
||||
* form and the pending value (both forgotten once the dialog closes or is replaced).
|
||||
*/
|
||||
interface Held {
|
||||
opener: Opener
|
||||
cite: CiteCreated | null
|
||||
initial: Reference | null
|
||||
}
|
||||
|
||||
/** No draft creation any more: the citing form and its pending value go together. */
|
||||
function forget(held: Held) {
|
||||
held.cite = null
|
||||
held.initial = null
|
||||
}
|
||||
|
||||
/** The moves that add the dialog: an edit of a reference, a creation. */
|
||||
@@ -57,16 +78,17 @@ function useOpening({ dispatch, mark }: LayerContext, held: Held) {
|
||||
const open = useCallback(
|
||||
(id: string, opener?: Opener) => {
|
||||
held.opener = opener
|
||||
held.cite = null
|
||||
forget(held)
|
||||
mark()
|
||||
dispatch({ type: 'open_reference', id })
|
||||
},
|
||||
[dispatch, mark, held],
|
||||
)
|
||||
const start = useCallback(
|
||||
(opener?: Opener, cite?: CiteCreated) => {
|
||||
(opener?: Opener, cite?: CiteCreated, initial?: Reference) => {
|
||||
held.opener = opener
|
||||
held.cite = cite ?? null
|
||||
held.initial = cite ? (initial ?? null) : null
|
||||
mark()
|
||||
dispatch({ type: 'create_reference' })
|
||||
},
|
||||
@@ -80,7 +102,7 @@ function useClose({ dispatch, guards, mark }: LayerContext, held: Held) {
|
||||
return useCallback(
|
||||
(forced = false) => {
|
||||
if (!guards.ask('reference', forced).allowed) return false
|
||||
held.cite = null
|
||||
forget(held)
|
||||
mark()
|
||||
dispatch({ type: 'close_reference' })
|
||||
return true
|
||||
@@ -94,7 +116,7 @@ function useReplace(ctx: LayerContext, held: Held) {
|
||||
const { dispatch, announce, guards, mark } = ctx
|
||||
return useCallback(
|
||||
(id: string | null, text?: string) => {
|
||||
held.cite = null
|
||||
forget(held)
|
||||
const note = text ? guards.ask('reference', true).note : undefined
|
||||
if (text) announce(note ? `${note}. ${text}` : text)
|
||||
mark(true)
|
||||
@@ -112,23 +134,16 @@ function useClosing(ctx: LayerContext, held: Held) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* A library creation that succeeded: its entry becomes the reference's own, then it
|
||||
* closes, and the library says it. A draft creation never comes here (it writes nothing).
|
||||
*/
|
||||
function useCreated(
|
||||
ctx: LayerContext,
|
||||
replace: ReferenceMoves['replace'],
|
||||
held: Held,
|
||||
) {
|
||||
function useCreated(ctx: LayerContext, replace: ReferenceMoves['replace']) {
|
||||
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)
|
||||
if (citing) citing(id)
|
||||
else setAdded((last) => ({ id, serial: (last?.serial ?? 0) + 1 }))
|
||||
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
|
||||
@@ -140,7 +155,7 @@ function useCreated(
|
||||
dispatch({ type: 'close_reference' })
|
||||
})
|
||||
},
|
||||
[replace, mark, dispatch, held],
|
||||
[replace, mark, dispatch],
|
||||
)
|
||||
return { created, added }
|
||||
}
|
||||
@@ -172,12 +187,17 @@ export function useReferenceMoves(
|
||||
trace: MoveTrace,
|
||||
view: NavigationState['reference'],
|
||||
): ReferenceMoves {
|
||||
const held = useRef<Held>({ opener: null, cite: null }).current
|
||||
const held = useRef<Held>({ opener: null, cite: null, initial: null }).current
|
||||
const creating = view !== null && 'create' in view
|
||||
// A Back that removes the creation dialog leaves no citing form to answer.
|
||||
useEffect(() => {
|
||||
if (!creating) held.cite = null
|
||||
if (!creating) forget(held)
|
||||
}, [creating, held])
|
||||
const drafting = useCallback(
|
||||
(): Drafting | null =>
|
||||
held.cite ? { initial: held.initial, cite: held.cite } : null,
|
||||
[held],
|
||||
)
|
||||
const opener = useCallback((): HTMLElement | null => {
|
||||
const element = held.opener
|
||||
return element?.isConnected && !element.closest('[hidden]') ? element : null
|
||||
@@ -186,8 +206,9 @@ export function useReferenceMoves(
|
||||
return {
|
||||
...useOpening(ctx, held),
|
||||
...closing,
|
||||
...useCreated(ctx, closing.replace, held),
|
||||
...useCreated(ctx, closing.replace),
|
||||
...useFilter(ctx, trace),
|
||||
opener,
|
||||
drafting,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user