fix(app): a focus request is consumed once, not replayed on tab return
This commit is contained in:
1 parent
4f223e04cd
commit
3d589e1556
14 files changed
+99
-30
No files matched your search
@@ -30,7 +30,7 @@ import {
|
||||
storeCanEdit,
|
||||
} from '@/editor/mode'
|
||||
import { leaveGuardArmed, useEditorStore } from '@/editor/state/editorStore'
|
||||
import type { FocusRequest } from '@/editor/focusRequest'
|
||||
import { FocusConsumedContext, type FocusRequest } from '@/editor/focusRequest'
|
||||
import type { EditorTabId, EditorTabProps } from '@/editor/tabs'
|
||||
import { useUndoShortcuts } from '@/editor/useUndoShortcuts'
|
||||
import { useTabTier } from '@/editor/useViewport'
|
||||
@@ -80,6 +80,8 @@ function DraftEditor({
|
||||
const tier = useTabTier()
|
||||
const [tab, setTab] = useState<EditorTabId>(FIRST_TAB)
|
||||
const [focus, setFocus] = useState<FocusRequest | undefined>()
|
||||
const clearFocus = (serial: number) =>
|
||||
setFocus((prev) => (prev?.serial === serial ? undefined : prev))
|
||||
const goTo: EditorTabProps['onGoTo'] = (next, key) => {
|
||||
setTab(next)
|
||||
setFocus((prev) => ({ key, serial: (prev?.serial ?? 0) + 1 }))
|
||||
@@ -129,14 +131,16 @@ function DraftEditor({
|
||||
<CopyIssueNotice />
|
||||
<EditorTabs active={tab} tier={tier} onSelect={setTab} />
|
||||
<div id={PANEL_ID} role="tabpanel" aria-label={t(`editor.tab.${tab}`)}>
|
||||
<EditorTabBody
|
||||
tab={tab}
|
||||
readOnly={readOnly}
|
||||
onSelectTab={setTab}
|
||||
focus={focus}
|
||||
onGoTo={goTo}
|
||||
reloadSheet={reloadSheet}
|
||||
/>
|
||||
<FocusConsumedContext value={clearFocus}>
|
||||
<EditorTabBody
|
||||
tab={tab}
|
||||
readOnly={readOnly}
|
||||
onSelectTab={setTab}
|
||||
focus={focus}
|
||||
onGoTo={goTo}
|
||||
reloadSheet={reloadSheet}
|
||||
/>
|
||||
</FocusConsumedContext>
|
||||
</div>
|
||||
<ConflictDialog />
|
||||
</div>
|
||||
|
||||
@@ -5,7 +5,6 @@ import type {
|
||||
} from '@faunosteo/core-wasm/types'
|
||||
import type { CommitContext } from '@/editor/annotations/annotationCommit'
|
||||
import { annotationFocus } from '@/editor/annotations/focusTarget'
|
||||
import { useFocusRequest } from '@/editor/focusRequest'
|
||||
import { keyboardPending, type NewKind } from '@/editor/annotations/creation'
|
||||
import { parseFrame, type Frame } from '@/editor/annotations/frame'
|
||||
import { isEditable } from '@/editor/annotations/geometryEdit'
|
||||
@@ -23,6 +22,7 @@ import {
|
||||
useViewModel,
|
||||
type ViewModel,
|
||||
} from '@/editor/annotations/useViewModel'
|
||||
import { useFocusRequest } from '@/editor/focusRequest'
|
||||
import type { EditorTabProps } from '@/editor/tabs'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { FOCUS_CLASS, focusAttrs } from '@/editor/focusRequest'
|
||||
import { useState } from 'react'
|
||||
import type {
|
||||
AnatomicalSheetDocument,
|
||||
@@ -10,12 +9,15 @@ import TextField from '@/components/editor/fields/TextField'
|
||||
import DeleteDialog, {
|
||||
type DeleteTarget,
|
||||
} from '@/components/editor/DeleteDialog'
|
||||
import { FOCUS_CLASS, focusAttrs } from '@/editor/focusRequest'
|
||||
import { isBlank, nullable } from '@/editor/sheetDocument'
|
||||
import { useEditCommand } from '@/editor/useEditCommand'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
|
||||
const CARD = 'rounded-lg border border-[var(--color-border)] p-3'
|
||||
|
||||
/** Annotation categories: list, rename, colour, add, delete with dependents. */
|
||||
export default function CategoriesSection({
|
||||
doc,
|
||||
@@ -78,7 +80,7 @@ function CategoryRow({
|
||||
return (
|
||||
<li
|
||||
{...focusAttrs({ kind: 'category', id: category.id })}
|
||||
className={`rounded-lg border border-[var(--color-border)] p-3 ${FOCUS_CLASS}`}
|
||||
className={`${CARD} ${FOCUS_CLASS}`}
|
||||
>
|
||||
<div className="grid gap-3 md:grid-cols-3">
|
||||
{(['fr', 'en'] as const).map((locale) => (
|
||||
|
||||
@@ -9,6 +9,8 @@ import { useEditCommand } from '@/editor/useEditCommand'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { REFERENCE_TYPES } from './referenceTypes'
|
||||
|
||||
const CARD = 'space-y-3 rounded-lg border border-[var(--color-border)] p-3'
|
||||
|
||||
/** One reference: fields commit one by one, ↑ / ↓ reorder, delete asks the core. */
|
||||
export default function ReferenceRow({
|
||||
reference,
|
||||
@@ -38,10 +40,7 @@ export default function ReferenceRow({
|
||||
}))
|
||||
const prefix = `reference-${reference.id}`
|
||||
return (
|
||||
<li
|
||||
{...focusAttrs(key)}
|
||||
className={`space-y-3 rounded-lg border border-[var(--color-border)] p-3 ${FOCUS_CLASS}`}
|
||||
>
|
||||
<li {...focusAttrs(key)} className={`${CARD} ${FOCUS_CLASS}`}>
|
||||
<div className="grid gap-3 md:grid-cols-2">
|
||||
<SelectField
|
||||
id={`${prefix}-type`}
|
||||
|
||||
@@ -9,8 +9,8 @@ import PropertiesPanel from '@/components/editor/annotations/PropertiesPanel'
|
||||
import ViewSwitcher from '@/components/editor/annotations/ViewSwitcher'
|
||||
import SelectionTag from '@/components/editor/annotations/SelectionTag'
|
||||
import { annotationFocus } from '@/editor/annotations/focusTarget'
|
||||
import { useFocusRequest } from '@/editor/focusRequest'
|
||||
import { mainViewId, useViewModel } from '@/editor/annotations/useViewModel'
|
||||
import { useFocusRequest } from '@/editor/focusRequest'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import { useEditorStore } from '@/editor/state/editorStore'
|
||||
import type { EditorTabProps } from '@/editor/tabs'
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import AttributionSection from '@/components/editor/general/AttributionSection'
|
||||
import CategoriesSection from '@/components/editor/general/CategoriesSection'
|
||||
import TextsSection from '@/components/editor/general/TextsSection'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import { useRowFocus } from '@/editor/focusRequest'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import type { EditorTabProps } from '@/editor/tabs'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@ import DeleteDialog, {
|
||||
} from '@/components/editor/DeleteDialog'
|
||||
import ReferenceForm from '@/components/editor/references/ReferenceForm'
|
||||
import ReferenceRow from '@/components/editor/references/ReferenceRow'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import { useRowFocus } from '@/editor/focusRequest'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import type { EditorTabProps } from '@/editor/tabs'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@ import DeleteDialog, {
|
||||
} from '@/components/editor/DeleteDialog'
|
||||
import RegionForm from '@/components/editor/vertebrae/RegionForm'
|
||||
import RegionRow from '@/components/editor/vertebrae/RegionRow'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import { useRowFocus } from '@/editor/focusRequest'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import type { EditorTabProps } from '@/editor/tabs'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
|
||||
@@ -5,8 +5,8 @@ import DeleteDialog, {
|
||||
import DetailViewForm from '@/components/editor/views/DetailViewForm'
|
||||
import DetailViewRow from '@/components/editor/views/DetailViewRow'
|
||||
import MainViewCard from '@/components/editor/views/MainViewCard'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import { useRowFocus } from '@/editor/focusRequest'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import type { EditorTabProps } from '@/editor/tabs'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { FOCUS_CLASS, focusAttrs } from '@/editor/focusRequest'
|
||||
import { useState } from 'react'
|
||||
import type { VertebralRegion } from '@faunosteo/core-wasm/types'
|
||||
import ActionButton from '@/components/editor/fields/ActionButton'
|
||||
import MoveButtons from '@/components/editor/fields/MoveButtons'
|
||||
import ReadOnlyValue from '@/components/editor/fields/ReadOnlyValue'
|
||||
import TextField from '@/components/editor/fields/TextField'
|
||||
import { FOCUS_CLASS, focusAttrs } from '@/editor/focusRequest'
|
||||
import { isBlank } from '@/editor/sheetDocument'
|
||||
import { useEditCommand } from '@/editor/useEditCommand'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
@@ -12,6 +12,8 @@ import { useT } from '@/i18n/useT'
|
||||
import RangeFields from './RangeFields'
|
||||
import { parseRange, wholeNumber } from './numbers'
|
||||
|
||||
const CARD = 'space-y-3 rounded-lg border border-[var(--color-border)] p-3'
|
||||
|
||||
const textOf = (value: number | undefined): string =>
|
||||
value === undefined ? '' : String(value)
|
||||
|
||||
@@ -52,7 +54,7 @@ export default function RegionRow({
|
||||
return (
|
||||
<li
|
||||
{...focusAttrs({ kind: 'region', code: region.code })}
|
||||
className={`space-y-3 rounded-lg border border-[var(--color-border)] p-3 ${FOCUS_CLASS}`}
|
||||
className={`${CARD} ${FOCUS_CLASS}`}
|
||||
>
|
||||
<div className="grid gap-3 md:grid-cols-4">
|
||||
<ReadOnlyValue
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { FOCUS_CLASS, focusAttrs } from '@/editor/focusRequest'
|
||||
import type { DetailView, SheetView } from '@faunosteo/core-wasm/types'
|
||||
import ActionButton from '@/components/editor/fields/ActionButton'
|
||||
import SelectField from '@/components/editor/fields/SelectField'
|
||||
import { FOCUS_CLASS, focusAttrs } from '@/editor/focusRequest'
|
||||
import { useViewUpdate } from '@/editor/useViewUpdate'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
@@ -9,6 +9,8 @@ import CropEditor from './CropEditor'
|
||||
import ViewDrawing from './ViewDrawing'
|
||||
import ViewNames from './ViewNames'
|
||||
|
||||
const CARD = 'space-y-3 rounded-lg border border-[var(--color-border)] p-3'
|
||||
|
||||
/** One detail view: names, parent, own drawing, crop (one crop canvas open at a time). */
|
||||
export default function DetailViewRow({
|
||||
view,
|
||||
@@ -38,7 +40,7 @@ export default function DetailViewRow({
|
||||
<li
|
||||
aria-label={view.name[locale]}
|
||||
{...focusAttrs({ kind: 'view', id: view.id })}
|
||||
className={`space-y-3 rounded-lg border border-[var(--color-border)] p-3 ${FOCUS_CLASS}`}
|
||||
className={`${CARD} ${FOCUS_CLASS}`}
|
||||
>
|
||||
<div className="grid gap-3 md:grid-cols-3">
|
||||
<ViewNames view={view} readOnly={readOnly} views={edit} />
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { FOCUS_CLASS, focusAttrs } from '@/editor/focusRequest'
|
||||
import type { MainView } from '@faunosteo/core-wasm/types'
|
||||
import { FOCUS_CLASS, focusAttrs } from '@/editor/focusRequest'
|
||||
import { useViewUpdate } from '@/editor/useViewUpdate'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import ViewDrawing from './ViewDrawing'
|
||||
import ViewNames from './ViewNames'
|
||||
|
||||
const CARD = 'space-y-3 rounded-lg border border-[var(--color-border)] p-3'
|
||||
|
||||
/** The main view: its names and its drawing (state, detail, « Choisir une illustration »). */
|
||||
export default function MainViewCard({
|
||||
view,
|
||||
@@ -19,7 +21,7 @@ export default function MainViewCard({
|
||||
<section
|
||||
aria-labelledby="views-main-title"
|
||||
{...focusAttrs({ kind: 'view', id: view.id })}
|
||||
className={`space-y-3 rounded-lg border border-[var(--color-border)] p-3 ${FOCUS_CLASS}`}
|
||||
className={`${CARD} ${FOCUS_CLASS}`}
|
||||
>
|
||||
<h3 id="views-main-title" className="text-base font-semibold">
|
||||
{t('editor.views.main_title')}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { createContext, useContext, useEffect, useRef } from 'react'
|
||||
import type { ElementKey } from '@faunosteo/core-wasm/types'
|
||||
|
||||
/** « Aller à … »: the element to show and a serial so each request is consumed once. */
|
||||
@@ -19,17 +19,24 @@ export function focusAttrs(key: ElementKey) {
|
||||
export const FOCUS_CLASS =
|
||||
'focus:outline focus:outline-2 focus:outline-[var(--color-accent)]'
|
||||
|
||||
/** Runs `apply` once per request serial, after the tab has rendered. */
|
||||
/** The route's answer to a consumed request: it clears it, so a remounted tab never replays. */
|
||||
export const FocusConsumedContext = createContext<(serial: number) => void>(
|
||||
() => undefined,
|
||||
)
|
||||
|
||||
/** Runs `apply` once per request, after the tab has rendered, then reports it consumed. */
|
||||
export function useFocusRequest(
|
||||
focus: FocusRequest | undefined,
|
||||
apply: (key: ElementKey) => void,
|
||||
): void {
|
||||
const handled = useRef<number | null>(null)
|
||||
const consumed = useContext(FocusConsumedContext)
|
||||
useEffect(() => {
|
||||
if (!focus || handled.current === focus.serial) return
|
||||
handled.current = focus.serial
|
||||
apply(focus.key)
|
||||
})
|
||||
consumed(focus.serial)
|
||||
}, [focus, apply, consumed])
|
||||
}
|
||||
|
||||
/** Scrolls to and focuses the row tagged with `key`. */
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
/** Lot J fix: « Aller à … » seeds each tab's own selection once. */
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { useState } from 'react'
|
||||
import { cleanup, render, screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import type {
|
||||
AnatomicalSheetDocument,
|
||||
ElementKey,
|
||||
@@ -9,6 +11,7 @@ import FindingsTab from '@/components/editor/tabs/FindingsTab'
|
||||
import ReferencesTab from '@/components/editor/tabs/ReferencesTab'
|
||||
import VertebraeTab from '@/components/editor/tabs/VertebraeTab'
|
||||
import AnnotationsTab from '@/components/editor/tabs/AnnotationsTab'
|
||||
import { FocusConsumedContext, type FocusRequest } from '@/editor/focusRequest'
|
||||
import { issueTarget } from '@/editor/verification'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { edit, insert, point } from '../helpers/editorFixtures'
|
||||
@@ -192,3 +195,51 @@ describe('issueTarget', () => {
|
||||
expect(issueTarget(d, 'nothing', 'fr')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
/** The route's contract: one tab mounted at a time, the request cleared once consumed. */
|
||||
function Harness() {
|
||||
const [tab, setTab] = useState<'references' | 'vertebrae'>('references')
|
||||
const [focus, setFocus] = useState<FocusRequest | undefined>({
|
||||
key: { kind: 'reference', id: 'ref-a' },
|
||||
serial: 1,
|
||||
})
|
||||
const clear = (serial: number) =>
|
||||
setFocus((prev) => (prev?.serial === serial ? undefined : prev))
|
||||
const Body = tab === 'references' ? ReferencesTab : VertebraeTab
|
||||
return (
|
||||
<FocusConsumedContext value={clear}>
|
||||
<button
|
||||
onClick={() =>
|
||||
setTab(tab === 'references' ? 'vertebrae' : 'references')
|
||||
}
|
||||
>
|
||||
switch
|
||||
</button>
|
||||
<Body {...base} focus={focus} />
|
||||
</FocusConsumedContext>
|
||||
)
|
||||
}
|
||||
|
||||
describe('a consumed request', () => {
|
||||
it('is not replayed when the tab is left and reopened', async () => {
|
||||
const text = insert(baseText(), {
|
||||
kind: 'reference',
|
||||
value: {
|
||||
id: 'ref-a',
|
||||
type: 'book',
|
||||
citation: 'Alpha',
|
||||
url: null,
|
||||
doi: null,
|
||||
},
|
||||
})
|
||||
await openReady({ text })
|
||||
const user = userEvent.setup()
|
||||
render(<Harness />)
|
||||
expect(document.activeElement?.getAttribute('data-focus-key')).toContain(
|
||||
'ref-a',
|
||||
)
|
||||
await user.click(screen.getByText('switch'))
|
||||
await user.click(screen.getByText('switch'))
|
||||
expect(document.activeElement?.getAttribute('data-focus-key')).toBeNull()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user