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,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,
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
|
||||
Reference in new issue
Block a user