feat(editor): reference dialog, reading tiles and finding filter in the history
R2F-5 and R2F-6: the reference dialog is a view layer above the References tab or the citations sheet (local draft, one command per changed save, creation entry becomes the reference's own). Tiles only read. « Tout afficher » filters the library by the finding, « Revenir au constat » pops or pushes. Inline tile edit, draft tile and their guards removed.
This commit is contained in:
1 parent
e2d020bd3d
commit
8031e0078f
28 files changed
+1072
-1512
No files matched your search
@@ -7,27 +7,69 @@ import {
|
||||
type FocusRequest,
|
||||
} from '@/editor/focusRequest'
|
||||
import { citersOf } from '@/editor/references/citations'
|
||||
import { referenceMatches } from '@/editor/references/filter'
|
||||
import {
|
||||
citedIds,
|
||||
citingFinding,
|
||||
referenceMatches,
|
||||
} from '@/editor/references/filter'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import type { ReferenceLayer } from '@/editor/tabs'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { usePlaceVisible } from '@/lib/placeVisibility'
|
||||
import { tileKeys, useTileState } from './useTileState'
|
||||
import { useReferenceView } from './useReferenceView'
|
||||
import { ADD_BUTTON_ID } from './addButton'
|
||||
|
||||
const NO_CITERS = new Map<string, never[]>()
|
||||
|
||||
/** The document's references with the tab's tile state and display order. */
|
||||
export function useReferenceLibrary(resetSignal: number) {
|
||||
/**
|
||||
* The finding the filter names, read from the current document, with the ids it cites. A
|
||||
* filter whose finding is gone (an undo, a reload, a Back onto a dead entry) is removed by
|
||||
* replace and says so once (R2F-6).
|
||||
*/
|
||||
function useFilterFinding(layer: ReferenceLayer) {
|
||||
const t = useT()
|
||||
const doc = useSheetDocument()
|
||||
const finding = doc ? citingFinding(doc, layer.filter) : null
|
||||
const gone = doc !== null && layer.filter !== null && finding === null
|
||||
useEffect(() => {
|
||||
if (gone) layer.clearFilter(t('editor.references.filter_gone'))
|
||||
// The wording and the move are stable per render; the vanished finding is the trigger.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [gone])
|
||||
return { finding, cited: finding ? new Set(citedIds(finding)) : null }
|
||||
}
|
||||
|
||||
/** The tile key of the reference the dialog edits, while this tab is the place shown. */
|
||||
function useEditedKey(layer: ReferenceLayer): string | null {
|
||||
const shown = usePlaceVisible()
|
||||
const { view } = layer
|
||||
return shown && view && 'edit' in view ? `${view.edit}#0` : null
|
||||
}
|
||||
|
||||
/** The document's references, one tile key each, the tile state and who cites what. */
|
||||
function useBibliography() {
|
||||
const doc = useSheetDocument()
|
||||
const all: readonly Reference[] = doc?.references ?? []
|
||||
const keys = tileKeys(all)
|
||||
const tiles = useTileState(doc ? keys : null)
|
||||
const citers = doc ? citersOf(doc) : NO_CITERS
|
||||
return { loaded: doc !== null, all, keys, tiles, citers }
|
||||
}
|
||||
|
||||
/** The document's references with the tab's tile state and display order. */
|
||||
export function useReferenceLibrary(
|
||||
resetSignal: number,
|
||||
layer: ReferenceLayer,
|
||||
) {
|
||||
const bibliography = useBibliography()
|
||||
const filter = useFilterFinding(layer)
|
||||
const shown = useReferenceView(
|
||||
{ all, keys, citers },
|
||||
tiles.editingKey,
|
||||
bibliography,
|
||||
useEditedKey(layer),
|
||||
resetSignal,
|
||||
filter.cited,
|
||||
)
|
||||
return { loaded: doc !== null, all, keys, tiles, citers, shown }
|
||||
return { ...bibliography, shown, finding: filter.finding }
|
||||
}
|
||||
export type ReferenceLibraryState = ReturnType<typeof useReferenceLibrary>
|
||||
|
||||
@@ -45,65 +87,76 @@ function useLateFocus() {
|
||||
const positionOf = (all: readonly Reference[], key: ElementKey) =>
|
||||
all.findIndex((r) => key.kind === 'reference' && r.id === key.id)
|
||||
|
||||
type Show = (key: ElementKey) => 'found' | 'none'
|
||||
|
||||
/**
|
||||
* « Aller à » a reference: its tile is opened, the search is cleared (only it, P3) when it
|
||||
* hides the tile, and the tile's button is focused after the commit that shows it (P8).
|
||||
* Shows a reference's tile: opened, the search cleared (only it, P3) and the finding
|
||||
* filter removed when they hide it, its button focused after the commit that shows it
|
||||
* (P8). `none` when the sheet holds no such reference.
|
||||
*/
|
||||
function useReferenceFocus(
|
||||
focus: FocusRequest | undefined,
|
||||
function useShowReference(
|
||||
lib: ReferenceLibraryState,
|
||||
later: (key: ElementKey) => void,
|
||||
) {
|
||||
const { all, keys, tiles, shown } = lib
|
||||
useFocusRequest(focus, (target) => {
|
||||
const at = target.key === null ? -1 : positionOf(all, target.key)
|
||||
if (target.key === null || at < 0) return 'none'
|
||||
tiles.reveal(keys[at])
|
||||
if (referenceMatches(all[at], shown.view.query)) {
|
||||
return focusRowLater(target.key) ? 'found' : 'none'
|
||||
}
|
||||
shown.update({ query: '' })
|
||||
later(target.key)
|
||||
layer: ReferenceLayer,
|
||||
): Show {
|
||||
const later = useLateFocus()
|
||||
return (key) => {
|
||||
const at = positionOf(lib.all, key)
|
||||
if (at < 0) return 'none'
|
||||
const reference = lib.all[at]
|
||||
lib.tiles.reveal(lib.keys[at])
|
||||
const cited = lib.finding && citedIds(lib.finding)
|
||||
const filtered = !!cited && !cited.includes(reference.id)
|
||||
const matches = referenceMatches(reference, lib.shown.view.query)
|
||||
if (matches && !filtered) return focusRowLater(key) ? 'found' : 'none'
|
||||
if (filtered) layer.clearFilter()
|
||||
if (!matches) lib.shown.update({ query: '' })
|
||||
later(key)
|
||||
return 'found'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/** What the library's controls do: search, « Ajouter une référence », edit and « Aller à ». */
|
||||
export function useReferenceActions(
|
||||
lib: ReferenceLibraryState,
|
||||
focus: FocusRequest | undefined,
|
||||
searchRef: RefObject<HTMLInputElement | null>,
|
||||
/** « Aller à » a reference: its tile is shown and focused. */
|
||||
function useReferenceFocus(focus: FocusRequest | undefined, show: Show) {
|
||||
useFocusRequest(focus, (target) =>
|
||||
target.key === null ? 'none' : show(target.key),
|
||||
)
|
||||
}
|
||||
|
||||
/** The notice of a reference just added: shown until « Afficher », or until it is gone. */
|
||||
function useAddedNotice(
|
||||
all: readonly Reference[],
|
||||
added: ReferenceLayer['added'],
|
||||
show: Show,
|
||||
) {
|
||||
const { keys, tiles, shown } = lib
|
||||
const [draft, setDraft] = useState<number | null>(null)
|
||||
const later = useLateFocus()
|
||||
useReferenceFocus(focus, lib, later)
|
||||
const [dismissed, setDismissed] = useState(0)
|
||||
const reference = added && all.find((r) => r.id === added.id)
|
||||
if (!added || !reference || added.serial === dismissed) return null
|
||||
return {
|
||||
draft,
|
||||
clearSearch: () => {
|
||||
shown.update({ query: '' })
|
||||
searchRef.current?.focus()
|
||||
},
|
||||
// P24 (and P22): leave the tile in edit first, then a clean view and the draft.
|
||||
startAdd: () => {
|
||||
if (!tiles.finish()) return
|
||||
shown.reset(null)
|
||||
setDraft((serial) => (serial ?? 0) + 1)
|
||||
},
|
||||
// P24: the new tile is shown whatever was typed meanwhile, so the view is reset.
|
||||
added: (id: string) => {
|
||||
setDraft(null)
|
||||
shown.reset(tiles.editingKey)
|
||||
tiles.reveal(`${id}#0`)
|
||||
later({ kind: 'reference', id })
|
||||
},
|
||||
// C2: nothing was written; the search and sort reset by the add stay as they are.
|
||||
cancelAdd: () => {
|
||||
setDraft(null)
|
||||
document.getElementById(ADD_BUTTON_ID)?.focus()
|
||||
},
|
||||
edit: (position: number) => {
|
||||
if (tiles.startEdit(keys[position])) shown.markEdited(keys[position])
|
||||
citation: reference.citation,
|
||||
show: () => {
|
||||
show({ kind: 'reference', id: added.id })
|
||||
setDismissed(added.serial)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/** What the library's controls do: search, « Ajouter une référence », « Modifier », « Aller à ». */
|
||||
export function useReferenceActions(
|
||||
lib: ReferenceLibraryState,
|
||||
focus: FocusRequest | undefined,
|
||||
layer: ReferenceLayer,
|
||||
searchRef: RefObject<HTMLInputElement | null>,
|
||||
) {
|
||||
const show = useShowReference(lib, layer)
|
||||
useReferenceFocus(focus, show)
|
||||
return {
|
||||
notice: useAddedNotice(lib.all, layer.added, show),
|
||||
clearSearch: () => {
|
||||
lib.shown.update({ query: '' })
|
||||
searchRef.current?.focus()
|
||||
},
|
||||
startAdd: (opener: HTMLElement) => layer.start(opener),
|
||||
edit: (position: number, opener: HTMLElement) =>
|
||||
layer.open(lib.all[position].id, opener),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user