fix(editor): Task 3 round 2, usePin split, comment wrap
This commit is contained in:
1 parent
3bb3603115
commit
8a60128311
3 files changed
+47
-30
No files matched your search
@@ -9,8 +9,9 @@ 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
|
||||
* says the draft was dropped (BDR-115); a form with nothing typed says nothing.
|
||||
* 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.
|
||||
*/
|
||||
export function useDirtyGuard(
|
||||
|
||||
@@ -55,31 +55,43 @@ function useOrder(
|
||||
shownPositions(shown.all, view, context, pin, cited)
|
||||
}
|
||||
|
||||
/** What the pin reads: the tiles, the edited one, the view, the filter, the list unpinned. */
|
||||
interface PinInput {
|
||||
keys: readonly string[]
|
||||
editingKey: string | null
|
||||
view: ReferenceView
|
||||
filtered: boolean
|
||||
plain: readonly number[]
|
||||
}
|
||||
|
||||
/**
|
||||
* 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).
|
||||
* The pin after this render. 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,
|
||||
) {
|
||||
function nextPin(pinned: Pinned | null, input: PinInput): Pinned | null {
|
||||
if (input.filtered) return null
|
||||
const { editingKey, view } = input
|
||||
const edited = editingKey === null ? -1 : input.keys.indexOf(editingKey)
|
||||
const kept = pinned?.view === view && pinned.key === editingKey
|
||||
if (kept || !input.plain.includes(edited)) return pinned
|
||||
return { key: editingKey as string, index: input.plain.indexOf(edited), view }
|
||||
}
|
||||
|
||||
/** The pin the list applies: the pinned tile, in the view it was pinned under. */
|
||||
function pinOf(pinned: Pinned | null, input: PinInput): Pin | null {
|
||||
const at = pinned === null ? -1 : input.keys.indexOf(pinned.key)
|
||||
const live = pinned?.view === input.view && at >= 0
|
||||
if (!live || isSheetView(input.view)) return null
|
||||
return { position: at, index: Math.max(0, pinned.index) }
|
||||
}
|
||||
|
||||
/** The pin of the tile the dialog edits, kept until the next search or sort change. */
|
||||
function usePin(input: PinInput) {
|
||||
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
|
||||
const next = nextPin(pinned, input)
|
||||
if (next !== pinned) setPinned(next)
|
||||
return pinOf(next, input)
|
||||
}
|
||||
|
||||
/** How many references match (the filter, then the search), and how many the filter holds. */
|
||||
@@ -117,7 +129,13 @@ export function useReferenceView(
|
||||
const [view, update] = useViewState(resetSignal)
|
||||
const order = useOrder(shown, view, cited)
|
||||
const filtered = cited !== null
|
||||
const pin = usePin(shown.keys, editingKey, view, order, filtered)
|
||||
const pin = usePin({
|
||||
keys: shown.keys,
|
||||
editingKey,
|
||||
view,
|
||||
filtered,
|
||||
plain: order(null),
|
||||
})
|
||||
return {
|
||||
view,
|
||||
update,
|
||||
|
||||
@@ -464,10 +464,9 @@ describe('the added-reference notice', () => {
|
||||
it('goes when the user leaves the References tab', async () => {
|
||||
await openReady({ text: sortText() })
|
||||
const user = userEvent.setup()
|
||||
let nav!: EditorNavigation
|
||||
const { rerender } = render(
|
||||
<PlaceVisibleContext value>
|
||||
<ReferencesHost onNav={(n) => (nav = n)} />
|
||||
<ReferencesHost />
|
||||
</PlaceVisibleContext>,
|
||||
)
|
||||
await user.click(
|
||||
@@ -484,16 +483,15 @@ describe('the added-reference notice', () => {
|
||||
)
|
||||
rerender(
|
||||
<PlaceVisibleContext value={false}>
|
||||
<ReferencesHost onNav={(n) => (nav = n)} />
|
||||
<ReferencesHost />
|
||||
</PlaceVisibleContext>,
|
||||
)
|
||||
rerender(
|
||||
<PlaceVisibleContext value>
|
||||
<ReferencesHost onNav={(n) => (nav = n)} />
|
||||
<ReferencesHost />
|
||||
</PlaceVisibleContext>,
|
||||
)
|
||||
expect(screen.queryByText(/Référence ajoutée/)).toBeNull()
|
||||
expect(nav).toBeDefined()
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in new issue
Block a user