fix(editor): announce a draft dropped with its tile
An undo or redo that removes a reference unmounted its tile with the edit view, so the tile's announce effect never ran. The drop now speaks from onDrop itself; the citations panel, unmounting after a forced close that already named the fields, stays quiet.
This commit is contained in:
1 parent
58228ab522
commit
cb1ad47cb0
3 files changed
+170
-16
No files matched your search
@@ -1,4 +1,10 @@
|
||||
import type { KeyboardEvent, RefObject } from 'react'
|
||||
import {
|
||||
useCallback,
|
||||
useLayoutEffect,
|
||||
useRef,
|
||||
type KeyboardEvent,
|
||||
type RefObject,
|
||||
} from 'react'
|
||||
import type { LocalizedText, Reference } from '@faunosteo/core-wasm/types'
|
||||
import ActionButton from '@/components/editor/fields/ActionButton'
|
||||
import Icon from '@/components/ui/Icon'
|
||||
@@ -135,17 +141,35 @@ function MissingRow({
|
||||
|
||||
type Panel = ReturnType<typeof useCitationsPanel>
|
||||
|
||||
/**
|
||||
* True once the panel unmounts (its list with it). It only unmounts after a close its leave check allowed,
|
||||
* which already said any refused draft (`note`). React runs a deleted parent's layout
|
||||
* cleanups before its children's, so a tile's edit view reads true when it drops then.
|
||||
*/
|
||||
function useLeaving() {
|
||||
const leaving = useRef(false)
|
||||
useLayoutEffect(() => {
|
||||
leaving.current = false
|
||||
return () => {
|
||||
leaving.current = true
|
||||
}
|
||||
}, [])
|
||||
return useCallback(() => leaving.current, [])
|
||||
}
|
||||
|
||||
/** One cited reference as a tile (« Modifier », « Retirer de ce constat »). */
|
||||
function EntryTile({
|
||||
entry,
|
||||
reference,
|
||||
panel,
|
||||
props,
|
||||
leaving,
|
||||
}: {
|
||||
entry: CitationEntry
|
||||
reference: Reference
|
||||
panel: Panel
|
||||
props: Props
|
||||
leaving: () => boolean
|
||||
}) {
|
||||
const { tiles, keys, citers, entries, remove } = panel
|
||||
const { readOnly, citedBy, navigate } = props
|
||||
@@ -169,6 +193,7 @@ function EntryTile({
|
||||
goTo: (target) => navigate(targetOfKey(target)),
|
||||
registerLeave: tiles.registerLeave,
|
||||
takeFocus: () => tiles.takeFocus(key),
|
||||
hostLeaving: leaving,
|
||||
}}
|
||||
/>
|
||||
)
|
||||
@@ -176,6 +201,7 @@ function EntryTile({
|
||||
|
||||
/** The cited references as tiles, the dangling ids as rows. */
|
||||
function Entries({ panel, props }: { panel: Panel; props: Props }) {
|
||||
const leaving = useLeaving()
|
||||
return (
|
||||
<ul
|
||||
ref={panel.list}
|
||||
@@ -190,6 +216,7 @@ function Entries({ panel, props }: { panel: Panel; props: Props }) {
|
||||
reference={entry.reference}
|
||||
panel={panel}
|
||||
props={props}
|
||||
leaving={leaving}
|
||||
/>
|
||||
) : (
|
||||
<MissingRow
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
import { useContext, useEffect, useRef, useState, type RefObject } from 'react'
|
||||
import {
|
||||
useCallback,
|
||||
useContext,
|
||||
useLayoutEffect,
|
||||
useRef,
|
||||
useState,
|
||||
type RefObject,
|
||||
} from 'react'
|
||||
import type { ElementKey, Reference } from '@faunosteo/core-wasm/types'
|
||||
import ActionButton from '@/components/editor/fields/ActionButton'
|
||||
import Icon from '@/components/ui/Icon'
|
||||
@@ -26,6 +33,11 @@ export interface TileHandlers {
|
||||
registerLeave: (guard: LeaveGuard | null) => void
|
||||
/** True once after « Modifier »: the edit view then focuses its Citation field. */
|
||||
takeFocus: () => boolean
|
||||
/**
|
||||
* True while the host itself unmounts: the leave that closed it already said the dropped
|
||||
* drafts (the citations panel's forced close), so the tile does not say them again.
|
||||
*/
|
||||
hostLeaving?: () => boolean
|
||||
}
|
||||
|
||||
/** What a tile of the citations panel adds: the finding it belongs to and « Retirer ». */
|
||||
@@ -224,11 +236,17 @@ function TileActions({ props }: { props: TileProps }) {
|
||||
}
|
||||
|
||||
/**
|
||||
* The refused drafts a read-only flip dropped, as the tile keeps them: said once in the
|
||||
* polite live region, shown by the tile until its next action. An edit view that ends on
|
||||
* purpose (« Terminer », another tile's « Modifier ») ends the note; a flip does not.
|
||||
* The refused drafts an edit view was unmounted with (a read-only flip, an undo that removes
|
||||
* the reference), as the tile keeps them: shown by the tile until its next action. The drop
|
||||
* is said once in the polite live region from `onDrop` itself, not from an effect: an undo
|
||||
* unmounts the tile with its view, and a dying tile runs no effect (V1). An edit view that
|
||||
* ends on purpose (« Terminer », another tile's « Modifier ») ends the note; a flip does not.
|
||||
*/
|
||||
function useDroppedDrafts(editing: boolean, readOnly: boolean) {
|
||||
function useDroppedDrafts(
|
||||
editing: boolean,
|
||||
readOnly: boolean,
|
||||
hostLeaving?: () => boolean,
|
||||
) {
|
||||
const droppedText = useDroppedText()
|
||||
const announce = useContext(AnnounceContext)
|
||||
const [dropped, setDropped] = useState<readonly Field[] | null>(null)
|
||||
@@ -237,13 +255,18 @@ function useDroppedDrafts(editing: boolean, readOnly: boolean) {
|
||||
setWasEditing(editing)
|
||||
if (wasEditing && !readOnly) setDropped(null)
|
||||
}
|
||||
const said = useRef<readonly Field[] | null>(null)
|
||||
useEffect(() => {
|
||||
if (dropped === null || said.current === dropped) return
|
||||
said.current = dropped
|
||||
announce(droppedText(dropped))
|
||||
// `onDrop` must stay stable (the view's cleanup depends on it): it reads the latest
|
||||
// render's words and host through this ref.
|
||||
const latest = useRef({ droppedText, announce, hostLeaving })
|
||||
useLayoutEffect(() => {
|
||||
latest.current = { droppedText, announce, hostLeaving }
|
||||
})
|
||||
return [dropped, setDropped] as const
|
||||
const onDrop = useCallback((fields: readonly Field[]) => {
|
||||
setDropped(fields)
|
||||
const now = latest.current
|
||||
if (!now.hostLeaving?.()) now.announce(now.droppedText(fields))
|
||||
}, [])
|
||||
return { dropped, setDropped, onDrop }
|
||||
}
|
||||
|
||||
/** The tile's own actions: « Terminer » refocuses the header, a toggle ends the note. */
|
||||
@@ -252,10 +275,14 @@ function useTileActions(
|
||||
headerRef: RefObject<HTMLButtonElement | null>,
|
||||
) {
|
||||
const { on, editing, readOnly } = props
|
||||
const [dropped, setDropped] = useDroppedDrafts(editing, readOnly)
|
||||
const { dropped, setDropped, onDrop } = useDroppedDrafts(
|
||||
editing,
|
||||
readOnly,
|
||||
on.hostLeaving,
|
||||
)
|
||||
return {
|
||||
dropped,
|
||||
setDropped,
|
||||
onDrop,
|
||||
done: () => {
|
||||
if (on.done()) headerRef.current?.focus()
|
||||
},
|
||||
@@ -287,7 +314,7 @@ export default function ReferenceTile(props: TileProps) {
|
||||
const t = useT()
|
||||
const base = tileBase(host, position, reference)
|
||||
const headerRef = useRef<HTMLButtonElement>(null)
|
||||
const { dropped, setDropped, done, toggle } = useTileActions(props, headerRef)
|
||||
const { dropped, onDrop, done, toggle } = useTileActions(props, headerRef)
|
||||
return (
|
||||
<li
|
||||
data-reference-tile
|
||||
@@ -319,7 +346,7 @@ export default function ReferenceTile(props: TileProps) {
|
||||
hooks={{
|
||||
registerLeave: on.registerLeave,
|
||||
takeFocus: on.takeFocus,
|
||||
onDrop: setDropped,
|
||||
onDrop,
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
|
||||
@@ -345,6 +345,59 @@ describe('a read-only flip without a refused draft', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('an undo that removes a tile holding a refused draft (V1)', () => {
|
||||
const added = (name: string) =>
|
||||
document.querySelector<HTMLInputElement>(`#tab-reference-3-ref-d-${name}`)!
|
||||
|
||||
/** ref-d inserted by one history entry, then its tile in edit. */
|
||||
async function editInserted() {
|
||||
const user = await mount()
|
||||
act(() => {
|
||||
store
|
||||
.getState()
|
||||
.commit({ op: 'insert', element: ref('ref-d', 'Nouvelle') })
|
||||
})
|
||||
expect(history()).toEqual({ undo: 1, redo: 0 })
|
||||
await editTile(user, 'ref-d', /^Nouvelle/)
|
||||
return user
|
||||
}
|
||||
|
||||
/** From a valid place: « Défaire » pressed from a refused field only refocuses it. */
|
||||
async function undoInsertion(user: User) {
|
||||
await user.tab()
|
||||
expect(document.activeElement).not.toHaveAttribute('aria-invalid', 'true')
|
||||
const watch = watchNotes()
|
||||
await user.click(screen.getByRole('button', { name: /Défaire/ }))
|
||||
await flush()
|
||||
expect(header('ref-d')).toBeNull()
|
||||
expect(doc().references.map((r: { id: string }) => r.id)).toEqual([
|
||||
'ref-a',
|
||||
'ref-b',
|
||||
'ref-c',
|
||||
])
|
||||
expect(history()).toEqual({ undo: 0, redo: 1 })
|
||||
return watch
|
||||
}
|
||||
|
||||
it('says the dropped Citation once', async () => {
|
||||
const user = await editInserted()
|
||||
await user.clear(added('citation'))
|
||||
const watch = await undoInsertion(user)
|
||||
expect(watch.seen).toEqual([`${NOTE} : Citation`])
|
||||
watch.stop()
|
||||
})
|
||||
|
||||
it('names both refused fields, once', async () => {
|
||||
const user = await editInserted()
|
||||
await user.clear(added('citation'))
|
||||
await user.click(added('url'))
|
||||
await user.type(added('url'), 'exemple.org')
|
||||
const watch = await undoInsertion(user)
|
||||
expect(watch.seen).toEqual([`${NOTE} : Citation, Adresse web (URL)`])
|
||||
watch.stop()
|
||||
})
|
||||
})
|
||||
|
||||
/** The citations panel of Node A, its Denoix tile in edit. */
|
||||
async function openPanel(user: User) {
|
||||
await user.click(screen.getByRole('tab', { name: 'Constats' }))
|
||||
@@ -416,6 +469,53 @@ describe('a read-only flip in the citations panel', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('an undo that takes a tile holding a refused draft out of the panel (V1)', () => {
|
||||
it('says the dropped Citation once, the panel stays', async () => {
|
||||
const user = await mount()
|
||||
act(() => {
|
||||
store.getState().commit({
|
||||
op: 'update',
|
||||
key: {
|
||||
kind: 'finding',
|
||||
zone_id: 'zone-a',
|
||||
path: ['fcat-a', 'fnode-a'],
|
||||
},
|
||||
element: {
|
||||
kind: 'finding_node',
|
||||
value: {
|
||||
...doc().finding_zones[0].categories[0].children[0],
|
||||
reference_ids: ['ref-a', 'ref-b', 'ref-c'],
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
expect(history()).toEqual({ undo: 1, redo: 0 })
|
||||
await user.click(screen.getByRole('tab', { name: 'Constats' }))
|
||||
await user.click(screen.getByRole('treeitem', { name: /Cat A/ }))
|
||||
await user.keyboard('{ArrowRight}')
|
||||
await user.click(screen.getByRole('treeitem', { name: /Node A/ }))
|
||||
await user.click(
|
||||
within(screen.getByRole('tabpanel', { name: 'Constats' })).getByRole(
|
||||
'button',
|
||||
{ name: '3 références' },
|
||||
),
|
||||
)
|
||||
const panel = await screen.findByTestId('editor-citations-panel')
|
||||
await user.click(within(panel).getByRole('button', { name: /^Code rural/ }))
|
||||
await user.click(within(panel).getByRole('button', { name: 'Modifier' }))
|
||||
await user.clear(field('ref-c', 'citation', 'panel'))
|
||||
await user.tab()
|
||||
const watch = watchNotes()
|
||||
await user.click(screen.getByRole('button', { name: /Défaire/ }))
|
||||
await flush()
|
||||
expect(header('ref-c', 'panel')).toBeNull()
|
||||
expect(screen.getByTestId('editor-citations-panel')).toBe(panel)
|
||||
expect(history()).toEqual({ undo: 0, redo: 1 })
|
||||
expect(watch.seen).toEqual([`${NOTE} : Citation`])
|
||||
watch.stop()
|
||||
})
|
||||
})
|
||||
|
||||
async function typeBadUrlIn(user: User) {
|
||||
const url = field('ref-b', 'url', 'panel')
|
||||
await user.clear(url)
|
||||
|
||||
Reference in new issue
Block a user