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,14 +1,19 @@
|
||||
import type { EditCommand, Reference } from '@faunosteo/core-wasm/types'
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* `created` (Rev 6): the references made in this draft, absent from the document; each
|
||||
* id is in `added` as long as it is in `created`.
|
||||
*/
|
||||
export interface CitationEdits {
|
||||
added: readonly string[]
|
||||
removed: readonly string[]
|
||||
created: readonly Reference[]
|
||||
}
|
||||
|
||||
export const NO_EDITS: CitationEdits = { added: [], removed: [] }
|
||||
export const NO_EDITS: CitationEdits = { added: [], removed: [], created: [] }
|
||||
|
||||
export const sameEdits = (a: CitationEdits, b: CitationEdits) =>
|
||||
JSON.stringify(a) === JSON.stringify(b)
|
||||
@@ -31,11 +36,53 @@ export function addId(edits: CitationEdits, id: string): CitationEdits {
|
||||
return { ...edits, added: [...edits.added, id] }
|
||||
}
|
||||
|
||||
/** Removes `id`: undoes its addition, else records the removal. */
|
||||
/** Removes `id`: undoes its addition (a created reference goes with it), else records it. */
|
||||
export function removeId(edits: CitationEdits, id: string): CitationEdits {
|
||||
if (edits.added.includes(id)) {
|
||||
return { ...edits, added: edits.added.filter((other) => other !== id) }
|
||||
return {
|
||||
...edits,
|
||||
added: edits.added.filter((other) => other !== id),
|
||||
created: edits.created.filter((reference) => reference.id !== id),
|
||||
}
|
||||
}
|
||||
if (edits.removed.includes(id)) return edits
|
||||
return { ...edits, removed: [...edits.removed, id] }
|
||||
}
|
||||
|
||||
/** A reference made in the draft: cited under its id, or replacing the pending one. */
|
||||
export function putCreated(
|
||||
edits: CitationEdits,
|
||||
value: Reference,
|
||||
): CitationEdits {
|
||||
if (edits.created.some((reference) => reference.id === value.id)) {
|
||||
const created = edits.created.map((reference) =>
|
||||
reference.id === value.id ? value : reference,
|
||||
)
|
||||
return { ...edits, created }
|
||||
}
|
||||
return { ...addId(edits, value.id), created: [...edits.created, value] }
|
||||
}
|
||||
|
||||
/**
|
||||
* The save's commands, in order: an `insert` per created reference under a minted id,
|
||||
* then `update(ids)` with the citations applied to `current`, the created ids remapped.
|
||||
*/
|
||||
export function citationCommands(
|
||||
edits: CitationEdits,
|
||||
current: readonly string[],
|
||||
update: (ids: string[]) => EditCommand,
|
||||
mint: () => string,
|
||||
): EditCommand[] {
|
||||
const fresh = new Map(edits.created.map((r) => [r.id, mint()]))
|
||||
const inserts = edits.created.map(
|
||||
(r): EditCommand => ({
|
||||
op: 'insert',
|
||||
element: {
|
||||
kind: 'reference',
|
||||
value: { ...r, id: fresh.get(r.id) ?? r.id },
|
||||
},
|
||||
}),
|
||||
)
|
||||
const ids = applyEdits(current, edits).map((id) => fresh.get(id) ?? id)
|
||||
return [...inserts, update(ids)]
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import type { EditorTarget, EditorView, FocusRequest } from './navigation'
|
||||
import { unmeasuredViews } from './issues'
|
||||
import type { TabTier } from './useViewport'
|
||||
import type { Guards } from './useEditorNavigation'
|
||||
import type { AddedReference } from './useReferenceMoves'
|
||||
import type { AddedReference, ReferenceMoves } from './useReferenceMoves'
|
||||
|
||||
export const EDITOR_TABS = [
|
||||
'general',
|
||||
@@ -64,7 +64,9 @@ export interface ReferenceLayer {
|
||||
filter: ElementKey | null
|
||||
guards: Guards
|
||||
open: (id: string, opener?: HTMLElement | null) => void
|
||||
start: (opener?: HTMLElement | null) => void
|
||||
start: ReferenceMoves['start']
|
||||
/** The open dialog's draft creation (over the citations edit mode, Rev 6); else null. */
|
||||
drafting: ReferenceMoves['drafting']
|
||||
close: (forced?: boolean) => boolean
|
||||
replace: (id: string | null, text?: string) => void
|
||||
created: (id: string) => void
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { useState } from 'react'
|
||||
import type { EditCommand } from '@faunosteo/core-wasm/types'
|
||||
import type { CommitOutcome } from './state/editorTypes'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { applyEdit } from './core/editOps'
|
||||
import { newElementId, type IdPrefix } from './ids'
|
||||
import { refusalMessage } from './refusal'
|
||||
import { useEditorStore } from './state/editorStore'
|
||||
@@ -21,11 +23,24 @@ export interface EditCommandApi {
|
||||
prefix: IdPrefix,
|
||||
build: (id: string) => EditCommand,
|
||||
) => boolean
|
||||
/**
|
||||
* Commits `build()` as one batch (one undo step); `build` is called once more when the
|
||||
* core answers `duplicate_key` (it mints its new ids again).
|
||||
*/
|
||||
runBatch: (field: string, build: () => readonly EditCommand[]) => boolean
|
||||
/** Dry-runs one command on the current document text: nothing is stored. */
|
||||
check: (field: string, command: EditCommand) => boolean
|
||||
/** A refusal that never reached the core (a required field left blank). */
|
||||
reject: (field: string, messageKey: string) => false
|
||||
clear: () => void
|
||||
}
|
||||
|
||||
/** The core refused an insert because its new id is taken: worth one more mint. */
|
||||
const isClash = (outcome: CommitOutcome) =>
|
||||
!outcome.ok &&
|
||||
outcome.reason === 'core' &&
|
||||
outcome.failure.kind === 'duplicate_key'
|
||||
|
||||
/** The write path of an editor panel: commit once, keep the refusal next to the control. */
|
||||
export function useEditCommand(): EditCommandApi {
|
||||
const t = useT()
|
||||
@@ -34,27 +49,42 @@ export function useEditCommand(): EditCommandApi {
|
||||
const attempt = (command: EditCommand) =>
|
||||
useEditorStore.getState().commit(command)
|
||||
|
||||
const run = (field: string, command: EditCommand): boolean => {
|
||||
const outcome = attempt(command)
|
||||
const run = (field: string, command: EditCommand): boolean =>
|
||||
settle(field, attempt(command))
|
||||
|
||||
const settle = (field: string, outcome: CommitOutcome): boolean => {
|
||||
setError(outcome.ok ? null : { field, message: refusalMessage(t, outcome) })
|
||||
return outcome.ok
|
||||
}
|
||||
|
||||
const runNew: EditCommandApi['runNew'] = (field, prefix, build) => {
|
||||
let outcome = attempt(build(newElementId(prefix)))
|
||||
const clash =
|
||||
!outcome.ok &&
|
||||
outcome.reason === 'core' &&
|
||||
outcome.failure.kind === 'duplicate_key'
|
||||
if (clash) outcome = attempt(build(newElementId(prefix)))
|
||||
setError(outcome.ok ? null : { field, message: refusalMessage(t, outcome) })
|
||||
return outcome.ok
|
||||
if (isClash(outcome)) outcome = attempt(build(newElementId(prefix)))
|
||||
return settle(field, outcome)
|
||||
}
|
||||
|
||||
const runBatch: EditCommandApi['runBatch'] = (field, build) => {
|
||||
const store = useEditorStore.getState()
|
||||
let outcome = store.commitBatch(build())
|
||||
if (isClash(outcome)) outcome = store.commitBatch(build())
|
||||
return settle(field, outcome)
|
||||
}
|
||||
|
||||
const check: EditCommandApi['check'] = (field, command) => {
|
||||
const text = useEditorStore.getState().documentText
|
||||
const edited = applyEdit(text, command)
|
||||
const outcome: CommitOutcome = edited.ok
|
||||
? { ok: true }
|
||||
: { ok: false, reason: 'core', failure: edited }
|
||||
return settle(field, outcome)
|
||||
}
|
||||
|
||||
return {
|
||||
fieldError: (field) => (error?.field === field ? error.message : undefined),
|
||||
run,
|
||||
runNew,
|
||||
runBatch,
|
||||
check,
|
||||
reject: (field, messageKey) => {
|
||||
setError({ field, message: t(messageKey) })
|
||||
return false
|
||||
|
||||
@@ -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