fix(editor): Task 3 round 1, literal journey, pin, limits, notice
This commit is contained in:
1 parent
8b42816270
commit
3bb3603115
9 files changed
+295
-97
No files matched your search
@@ -8,8 +8,8 @@ export type DiscardTarget = 'form' | 'dialog'
|
||||
|
||||
/**
|
||||
* The layer's guard (the finding dialog's, or the reference dialog's) while a form holds
|
||||
* changes (R2F-3, R2F-5): a user close that reaches the layer (✕, Escape, scrim, Back) is refused and the form asks « Abandonner les
|
||||
* modifications ? » (`confirming`). A forced close (the element vanished) goes ahead and
|
||||
* changes (R2F-3, R2F-5): a user close that reaches the layer (✕, Escape, scrim, Back)
|
||||
* is refused and the form asks « Abandonner les modifications ? » (`confirming`). A forced close (the element vanished) goes ahead and
|
||||
* says the draft was dropped (BDR-115); a form with nothing typed says nothing.
|
||||
* `askToLeave` is the form's own « Annuler ». The browser's unload is guarded too.
|
||||
*/
|
||||
|
||||
@@ -89,6 +89,15 @@ const positionOf = (all: readonly Reference[], key: ElementKey) =>
|
||||
|
||||
type Show = (key: ElementKey) => 'found' | 'none'
|
||||
|
||||
/** Whether the finding filter or the search hides `reference` from the list. */
|
||||
function hiddenBy(lib: ReferenceLibraryState, reference: Reference) {
|
||||
const cited = lib.finding && citedIds(lib.finding)
|
||||
return {
|
||||
filtered: !!cited && !cited.includes(reference.id),
|
||||
matches: referenceMatches(reference, lib.shown.view.query),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -102,14 +111,13 @@ function useShowReference(
|
||||
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: '' })
|
||||
const hidden = hiddenBy(lib, lib.all[at])
|
||||
if (hidden.matches && !hidden.filtered) {
|
||||
return focusRowLater(key) ? 'found' : 'none'
|
||||
}
|
||||
if (hidden.filtered) layer.clearFilter()
|
||||
if (!hidden.matches) lib.shown.update({ query: '' })
|
||||
later(key)
|
||||
return 'found'
|
||||
}
|
||||
@@ -122,14 +130,21 @@ function useReferenceFocus(focus: FocusRequest | undefined, show: Show) {
|
||||
)
|
||||
}
|
||||
|
||||
/** The notice of a reference just added: shown until « Afficher », or until it is gone. */
|
||||
/**
|
||||
* The notice of a reference just added: shown until « Afficher », until it is gone, or
|
||||
* until the user leaves the References tab (it would be stale on their return).
|
||||
*/
|
||||
function useAddedNotice(
|
||||
all: readonly Reference[],
|
||||
added: ReferenceLayer['added'],
|
||||
show: Show,
|
||||
) {
|
||||
const [dismissed, setDismissed] = useState(0)
|
||||
const visible = usePlaceVisible()
|
||||
const reference = added && all.find((r) => r.id === added.id)
|
||||
if (added && !visible && dismissed !== added.serial) {
|
||||
setDismissed(added.serial)
|
||||
}
|
||||
if (!added || !reference || added.serial === dismissed) return null
|
||||
return {
|
||||
citation: reference.citation,
|
||||
|
||||
@@ -5,16 +5,21 @@ import type { CiterItem } from '@/editor/references/citations'
|
||||
import {
|
||||
isSheetView,
|
||||
shownPositions,
|
||||
type Pin,
|
||||
type ReferenceView,
|
||||
} from '@/editor/references/view'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
|
||||
const INITIAL: ReferenceView = { query: '', sort: 'sheet' }
|
||||
|
||||
/** The tile kept in the shown set, by key, and the index it is shown at (P23). */
|
||||
/**
|
||||
* The tile kept in the shown set, by key, and the index it is shown at (P23); it is valid
|
||||
* for the `view` it was pinned under only, so any search or sort change ends it.
|
||||
*/
|
||||
interface Pinned {
|
||||
key: string
|
||||
index: number
|
||||
view: ReferenceView
|
||||
}
|
||||
|
||||
/** The bibliography the view reads: references, one tile key per position, citers. */
|
||||
@@ -24,6 +29,73 @@ interface Shown {
|
||||
citers: ReadonlyMap<string, readonly CiterItem[]>
|
||||
}
|
||||
|
||||
/** The search and the sort; the search is emptied once per `resetSignal` bump. */
|
||||
function useViewState(resetSignal: number) {
|
||||
const [state, setState] = useState({ view: INITIAL, seen: resetSignal })
|
||||
if (state.seen !== resetSignal) {
|
||||
setState({ view: { ...state.view, query: '' }, seen: resetSignal })
|
||||
}
|
||||
const update = (patch: Partial<ReferenceView>) =>
|
||||
setState((now) => ({ ...now, view: { ...now.view, ...patch } }))
|
||||
return [state.view, update] as const
|
||||
}
|
||||
|
||||
/** The positions to show for a pin (or none): the filter, the search, the sort, the pin. */
|
||||
function useOrder(
|
||||
shown: Shown,
|
||||
view: ReferenceView,
|
||||
cited: ReadonlySet<string> | null,
|
||||
) {
|
||||
const locale = useLocaleStore((state) => state.locale)
|
||||
const context = {
|
||||
locale,
|
||||
citedCount: (id: string) => shown.citers.get(id)?.length ?? 0,
|
||||
}
|
||||
return (pin: Pin | null) =>
|
||||
shownPositions(shown.all, view, context, pin, cited)
|
||||
}
|
||||
|
||||
/**
|
||||
* The pin of the tile the dialog edits. Only a shown tile is pinned (a reference just
|
||||
* created is not part of the list), never under a filter (R2F-6: a tile outside it never
|
||||
* shows, and a pin of the filtered session must not outlive it).
|
||||
*/
|
||||
function usePin(
|
||||
keys: readonly string[],
|
||||
editingKey: string | null,
|
||||
view: ReferenceView,
|
||||
order: (pin: Pin | null) => number[],
|
||||
filtered: boolean,
|
||||
) {
|
||||
const [pinned, setPinned] = useState<Pinned | null>(null)
|
||||
const edited = editingKey === null ? -1 : keys.indexOf(editingKey)
|
||||
const plain = order(null)
|
||||
const current = pinned?.view === view && pinned.key === editingKey
|
||||
if (filtered && pinned !== null) setPinned(null)
|
||||
else if (!filtered && plain.includes(edited) && !current) {
|
||||
setPinned({ key: editingKey as string, index: plain.indexOf(edited), view })
|
||||
}
|
||||
const at = pinned === null ? -1 : keys.indexOf(pinned.key)
|
||||
const live = !filtered && pinned?.view === view && at >= 0
|
||||
return live && !isSheetView(view)
|
||||
? { position: at, index: Math.max(0, pinned?.index ?? 0) }
|
||||
: null
|
||||
}
|
||||
|
||||
/** How many references match (the filter, then the search), and how many the filter holds. */
|
||||
function countsOf(
|
||||
all: readonly Reference[],
|
||||
query: string,
|
||||
cited: ReadonlySet<string> | null,
|
||||
) {
|
||||
const inFilter = (r: Reference) => cited === null || cited.has(r.id)
|
||||
// The count is the matches: a pinned tile the search misses is shown, not counted.
|
||||
const matchCount = all.filter(
|
||||
(r) => inFilter(r) && referenceMatches(r, query),
|
||||
).length
|
||||
return { matchCount, total: all.filter(inFilter).length }
|
||||
}
|
||||
|
||||
/**
|
||||
* The search, the sort and the resulting tile order of the Références tab. All three are
|
||||
* display state: nothing here reaches the document (BDR-114), and the search is a personal
|
||||
@@ -34,53 +106,23 @@ interface Shown {
|
||||
* The tile the dialog edits (`editingKey`) is pinned at the index it was shown at, whatever
|
||||
* the search or the sort, and stays pinned after the dialog closes until the next search
|
||||
* or sort change, so the tile the user just edited never vanishes under their focus (P23,
|
||||
* I1). With a filter nothing is pinned: a tile outside it never shows.
|
||||
* I1). With a filter nothing is pinned.
|
||||
*/
|
||||
export function useReferenceView(
|
||||
{ all, keys, citers }: Shown,
|
||||
shown: Shown,
|
||||
editingKey: string | null,
|
||||
resetSignal: number,
|
||||
cited: ReadonlySet<string> | null,
|
||||
) {
|
||||
const locale = useLocaleStore((state) => state.locale)
|
||||
const [view, setView] = useState(INITIAL)
|
||||
const [seen, setSeen] = useState(resetSignal)
|
||||
const [pinned, setPinned] = useState<Pinned | null>(null)
|
||||
if (seen !== resetSignal) {
|
||||
setSeen(resetSignal)
|
||||
setView({ ...view, query: '' })
|
||||
setPinned(null)
|
||||
}
|
||||
const context = {
|
||||
locale,
|
||||
citedCount: (id: string) => citers.get(id)?.length ?? 0,
|
||||
}
|
||||
const plain = shownPositions(all, view, context, null, cited)
|
||||
const editedAt = editingKey === null ? -1 : keys.indexOf(editingKey)
|
||||
// Only a tile that is shown is pinned: a reference just created is not part of the list.
|
||||
if (plain.includes(editedAt) && pinned?.key !== editingKey) {
|
||||
setPinned({ key: editingKey as string, index: plain.indexOf(editedAt) })
|
||||
}
|
||||
const pinAt = pinned === null ? -1 : keys.indexOf(pinned.key)
|
||||
const pin =
|
||||
pinned !== null && pinAt >= 0 && !isSheetView(view, cited !== null)
|
||||
? { position: pinAt, index: Math.max(0, pinned.index) }
|
||||
: null
|
||||
const positions = shownPositions(all, view, context, pin, cited)
|
||||
const inFilter = (r: Reference) => cited === null || cited.has(r.id)
|
||||
// The count is the matches: a pinned tile the search misses is shown, not counted.
|
||||
const matchCount = all.filter(
|
||||
(r) => inFilter(r) && referenceMatches(r, view.query),
|
||||
).length
|
||||
const [view, update] = useViewState(resetSignal)
|
||||
const order = useOrder(shown, view, cited)
|
||||
const filtered = cited !== null
|
||||
const pin = usePin(shown.keys, editingKey, view, order, filtered)
|
||||
return {
|
||||
view,
|
||||
positions,
|
||||
matchCount,
|
||||
total: all.filter(inFilter).length,
|
||||
filtered: cited !== null,
|
||||
update: (patch: Partial<ReferenceView>) => {
|
||||
setPinned(null)
|
||||
setView((current) => ({ ...current, ...patch }))
|
||||
},
|
||||
update,
|
||||
positions: order(pin),
|
||||
filtered,
|
||||
...countsOf(shown.all, view.query, cited),
|
||||
}
|
||||
}
|
||||
@@ -74,37 +74,50 @@ interface BodyProps {
|
||||
props: Props
|
||||
}
|
||||
|
||||
/** The notice, the empty and no-result lines, the hint and the tiles. */
|
||||
function LibraryBody({ lib, act, props }: BodyProps) {
|
||||
/** The notice, the empty and no-result lines and the reorder hint. */
|
||||
function LibraryNotices(body: BodyProps) {
|
||||
const t = useT()
|
||||
const [deleting, setDeleting] = useState<DeleteTarget | null>(null)
|
||||
const { all, shown } = lib
|
||||
const sheetView = isSheetView(shown.view, shown.filtered)
|
||||
const { shown } = body.lib
|
||||
const reorderable =
|
||||
!body.props.readOnly && !isSheetView(shown.view, shown.filtered)
|
||||
return (
|
||||
<>
|
||||
{act.notice && (
|
||||
<AddedNotice citation={act.notice.citation} onShow={act.notice.show} />
|
||||
{body.act.notice && (
|
||||
<AddedNotice
|
||||
citation={body.act.notice.citation}
|
||||
onShow={body.act.notice.show}
|
||||
/>
|
||||
)}
|
||||
{all.length === 0 && <EmptyLine />}
|
||||
{all.length > 0 && shown.positions.length === 0 && (
|
||||
<NoResult query={shown.view.query} onClear={act.clearSearch} />
|
||||
{body.lib.all.length === 0 && <EmptyLine />}
|
||||
{body.lib.all.length > 0 && shown.positions.length === 0 && (
|
||||
<NoResult query={shown.view.query} onClear={body.act.clearSearch} />
|
||||
)}
|
||||
{!props.readOnly && !sheetView && shown.positions.length > 0 && (
|
||||
{reorderable && shown.positions.length > 0 && (
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('editor.references.reorder_hint')}
|
||||
</p>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
/** The tiles and the delete dialog they open. */
|
||||
function LibraryTiles(body: BodyProps) {
|
||||
const [deleting, setDeleting] = useState<DeleteTarget | null>(null)
|
||||
const { lib } = body
|
||||
return (
|
||||
<>
|
||||
<ReferenceTiles
|
||||
positions={shown.positions}
|
||||
all={all}
|
||||
positions={lib.shown.positions}
|
||||
all={lib.all}
|
||||
keys={lib.keys}
|
||||
citers={lib.citers}
|
||||
tiles={lib.tiles}
|
||||
query={shown.view.query}
|
||||
readOnly={props.readOnly}
|
||||
movable={sheetView}
|
||||
navigate={props.navigate}
|
||||
onEdit={act.edit}
|
||||
query={lib.shown.view.query}
|
||||
readOnly={body.props.readOnly}
|
||||
movable={isSheetView(lib.shown.view, lib.shown.filtered)}
|
||||
navigate={body.props.navigate}
|
||||
onEdit={body.act.edit}
|
||||
onDelete={(reference) =>
|
||||
setDeleting({
|
||||
key: { kind: 'reference', id: reference.id },
|
||||
@@ -128,23 +141,27 @@ function Library(props: Props) {
|
||||
const searchRef = useRef<HTMLInputElement>(null)
|
||||
const act = useReferenceActions(lib, props.focus, props.layer, searchRef)
|
||||
const chip = useFilterChip(lib, props.layer)
|
||||
const { shown } = lib
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<ReferenceToolbar
|
||||
query={shown.view.query}
|
||||
onQuery={(query) => shown.update({ query })}
|
||||
query={lib.shown.view.query}
|
||||
onQuery={(query) => lib.shown.update({ query })}
|
||||
onClear={act.clearSearch}
|
||||
searchRef={searchRef}
|
||||
sort={shown.view.sort}
|
||||
onSort={(sort) => shown.update({ sort })}
|
||||
shown={shown.matchCount}
|
||||
total={shown.total}
|
||||
sort={lib.shown.view.sort}
|
||||
onSort={(sort) => lib.shown.update({ sort })}
|
||||
shown={lib.shown.matchCount}
|
||||
total={lib.shown.total}
|
||||
canAdd={!props.readOnly}
|
||||
onAdd={act.startAdd}
|
||||
filter={chip}
|
||||
/>
|
||||
{lib.loaded && <LibraryBody lib={lib} act={act} props={props} />}
|
||||
{lib.loaded && (
|
||||
<>
|
||||
<LibraryNotices lib={lib} act={act} props={props} />
|
||||
<LibraryTiles lib={lib} act={act} props={props} />
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -20,11 +20,6 @@ export interface ModalDialogProps {
|
||||
* move that closed it.
|
||||
*/
|
||||
returnFocus?: (() => HTMLElement | null) | null
|
||||
/**
|
||||
* Escape typed where this answers true is swallowed: no close request. For a part that
|
||||
* still edits inline (the citations sheet's tiles) and keeps Escape for the field.
|
||||
*/
|
||||
ignoreEscape?: (target: EventTarget) => boolean
|
||||
/**
|
||||
* A view layer of the editor (R2F-1): its state is in the browser history, so a Back
|
||||
* may close it. Any other modal open refuses a Back.
|
||||
@@ -105,7 +100,6 @@ function restoreFocus(
|
||||
export default function ModalDialog({
|
||||
onClose,
|
||||
returnFocus,
|
||||
ignoreEscape,
|
||||
layer,
|
||||
labelledBy,
|
||||
label,
|
||||
@@ -161,7 +155,7 @@ export default function ModalDialog({
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
// A held key repeats: only the first press asks.
|
||||
if (event.repeat || ignoreEscape?.(event.target)) return
|
||||
if (event.repeat) return
|
||||
onClose()
|
||||
}
|
||||
const onCancel = (event: SyntheticEvent<HTMLDialogElement>) => {
|
||||
|
||||
@@ -57,7 +57,8 @@ function useOpening({ dispatch, mark }: LayerContext, held: RefObject<Opener>) {
|
||||
}
|
||||
|
||||
/** The moves that take it away: a user close, and the replace that is no Back step. */
|
||||
function useClosing({ dispatch, announce, guards, mark }: LayerContext) {
|
||||
function useClosing(ctx: LayerContext) {
|
||||
const { dispatch, announce, guards, mark } = ctx
|
||||
const close = useCallback(
|
||||
(forced = false) => {
|
||||
if (!guards.ask('reference', forced).allowed) return false
|
||||
@@ -82,17 +83,19 @@ function useClosing({ dispatch, announce, guards, mark }: LayerContext) {
|
||||
}
|
||||
|
||||
/** A creation that succeeded: its entry becomes the reference's own, then it closes. */
|
||||
function useCreated(
|
||||
{ dispatch, mark }: LayerContext,
|
||||
replace: ReferenceMoves['replace'],
|
||||
) {
|
||||
function useCreated(ctx: LayerContext, replace: ReferenceMoves['replace']) {
|
||||
const { dispatch, mark } = ctx
|
||||
const [added, setAdded] = useState<AddedReference | null>(null)
|
||||
const created = useCallback(
|
||||
(id: string) => {
|
||||
replace(id)
|
||||
setAdded((last) => ({ id, serial: (last?.serial ?? 0) + 1 }))
|
||||
// After the commit that wrote the replace: the entry is the reference's own before
|
||||
// the close goes back over it, so a Forward never reopens a creation.
|
||||
// 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
|
||||
// never reopens a creation. Called from anywhere else, the replace and the close
|
||||
// would merge into one view change (a Back, and the entry keeps its creation).
|
||||
// The microtask also keeps `mark()` from overwriting the replace hint early.
|
||||
queueMicrotask(() => {
|
||||
mark()
|
||||
dispatch({ type: 'close_reference' })
|
||||
@@ -104,10 +107,8 @@ function useCreated(
|
||||
}
|
||||
|
||||
/** The filter's two moves: remove it by replace, return to the finding it names. */
|
||||
function useFilter(
|
||||
{ dispatch, announce, mark }: LayerContext,
|
||||
trace: MoveTrace,
|
||||
) {
|
||||
function useFilter(ctx: LayerContext, trace: MoveTrace) {
|
||||
const { dispatch, announce, mark } = ctx
|
||||
const clearFilter = useCallback(
|
||||
(text?: string) => {
|
||||
if (text) announce(text)
|
||||
|
||||
Reference in new issue
Block a user