From 9a7a588e923419e969918826a1efd831b1a29106 Mon Sep 17 00:00:00 2001 From: bchanot Date: Mon, 23 Nov 2026 16:55:17 +0100 Subject: [PATCH] feat(editor): parent view and open view commands --- .../annotations/EditableAnnotations.tsx | 20 +- .../editor/annotations/SceneStrip.tsx | 28 +++ .../editor/annotations/ViewCommands.tsx | 4 + .../editor/annotations/useViewNavigation.ts | 35 +++ .../unit/editor-access-commands.test.tsx | 2 +- apps/app/tests/unit/editor-history.test.tsx | 38 ++- .../unit/editor-view-navigation.test.tsx | 223 ++++++++++++++++++ 7 files changed, 347 insertions(+), 3 deletions(-) create mode 100644 apps/app/src/editor/annotations/useViewNavigation.ts create mode 100644 apps/app/tests/unit/editor-view-navigation.test.tsx diff --git a/apps/app/src/components/editor/annotations/EditableAnnotations.tsx b/apps/app/src/components/editor/annotations/EditableAnnotations.tsx index 236a0c62..aa06d55f 100644 --- a/apps/app/src/components/editor/annotations/EditableAnnotations.tsx +++ b/apps/app/src/components/editor/annotations/EditableAnnotations.tsx @@ -59,6 +59,10 @@ import { useViewModel, type ViewModel, } from '@/editor/annotations/useViewModel' +import { + useViewNavigation, + type ViewNavigation, +} from '@/editor/annotations/useViewNavigation' import type { AnnotationSection } from '@/editor/navigation' import { usePlaceVisible } from '@/lib/placeVisibility' import type { EditorPlaceProps } from '@/editor/tabs' @@ -324,6 +328,19 @@ function CanvasColumn({ ) } +/** + * The second click or double click on a selected annotation: an access opens its view + * (D14), a point or a zone its properties dialog. + */ +function openFromDrawing(id: string, scene: Scene, navigation: ViewNavigation) { + const annotation = scene.model.annotations.find((a) => a.id === id) + const opened = + annotation?.kind === 'detail_trigger' && annotation.opens_view_id + if (opened && scene.doc.views.some((view) => view.id === opened)) { + navigation.openView(opened, null) + } else scene.onOpen(id, 'drawing') +} + /** The handle of the movable selected circle, named « Point {nom} » (« Accès {nom} »). */ function useHandle(target: Annotation | null, doc: AnatomicalSheetDocument) { const t = useT() @@ -410,6 +427,7 @@ function CanvasStage({ const free = creation === null const zone = useZoneHandle(scene, work, shown) const subject = pressSubject(scene, work) + const navigation = useViewNavigation(scene, dispatch) const view = doc.views.find((v) => v.id === scene.viewId) // A frame being drawn or adjusted keeps the drawing: no creation tool starts over it. const framing = useAnnotationWork((s) => s.frame !== null) @@ -447,7 +465,7 @@ function CanvasStage({ }} onRoot={scene.setRoot} onSelect={(id) => free && dispatch({ type: 'select', id })} - onOpen={(id) => free && scene.onOpen(id, 'drawing')} + onOpen={(id) => free && openFromDrawing(id, scene, navigation)} onPlace={work.placement.onPlace} accessShape={ui.accessShape} onShape={work.placement.onShape} diff --git a/apps/app/src/components/editor/annotations/SceneStrip.tsx b/apps/app/src/components/editor/annotations/SceneStrip.tsx index 65c3f461..0be018cb 100644 --- a/apps/app/src/components/editor/annotations/SceneStrip.tsx +++ b/apps/app/src/components/editor/annotations/SceneStrip.tsx @@ -9,6 +9,7 @@ import { startFrame } from '@/editor/annotations/frameWork' import { guardedPress } from '@/editor/annotations/guardedPress' import { beginRedraw } from '@/editor/annotations/redraw' import { stripLeadOf } from '@/editor/annotations/stripLead' +import { useViewNavigation } from '@/editor/annotations/useViewNavigation' import { useGeometryIssues } from '@/editor/annotations/useGeometryIssues' import type { GestureMessage as ShownMessage } from '@/editor/annotations/useGestureMessage' import { @@ -16,6 +17,7 @@ import { type FrameWork, } from '@/editor/annotations/workStore' import { targetOfKey } from '@/editor/navigation' +import type { Locale } from '@/i18n/types' import { useT } from '@/i18n/useT' import { useLocaleStore } from '@/state/localeStore' import type { Scene, Work } from './EditableAnnotations' @@ -148,12 +150,20 @@ function showsAccessCommands( return isEditable(target.geometry) } +/** The view an access opens, null when it names none that exists. */ +function openedViewOf(annotation: Annotation, scene: Scene): string | null { + if (annotation.kind !== 'detail_trigger') return null + const id = annotation.opens_view_id + return scene.doc.views.some((view) => view.id === id) ? id : null +} + /** The access's commands, or the move mode's bar while it runs on the access. */ function AccessStrip({ annotation, work, scene }: AccessStripProps) { const inMode = useAnnotationWork( (s) => s.mode?.kind === 'move' && s.mode.annotationId === annotation.id, ) const { edit } = work + const navigation = useViewNavigation(scene, scene.dispatch) if (inMode) { return ( navigation.openView(opened, button) + } onMove={edit.startMove} onRedraw={ polygon ? () => beginRedraw(annotation.id, scene.dispatch) : undefined @@ -224,6 +240,10 @@ function ShapeCommands({ ) } +function parentName(scene: Scene, id: string | null, locale: Locale) { + return scene.doc.views.find((view) => view.id === id)?.name[locale] +} + /** * « Nouvelle vue de détail »: the frame work opens on this view (behind the drawing guard). * Pressed by the keyboard, the default frame is placed at the visible centre at once. @@ -274,6 +294,8 @@ function SceneCommands({ dispatch({ type: 'select', id: null }), ) const framing = useAnnotationWork((s) => s.frame) + const navigation = useViewNavigation(scene, dispatch) + const locale = useLocaleStore((s) => s.locale) if (framing) return // The dialog shown: its own actions; hidden after it was shown: board 04 C. if (creation?.dialog) return null @@ -328,6 +350,12 @@ function SceneCommands({ if (ui.tool === 'select' && !creation) { return ( navigation.goToParent(button) + } + parentTitle={parentName(scene, navigation.parentId, locale)} onNew={(button, keyboard) => newView(scene, work, button, keyboard)} /> ) diff --git a/apps/app/src/components/editor/annotations/ViewCommands.tsx b/apps/app/src/components/editor/annotations/ViewCommands.tsx index b28cc071..6d4a8dd1 100644 --- a/apps/app/src/components/editor/annotations/ViewCommands.tsx +++ b/apps/app/src/components/editor/annotations/ViewCommands.tsx @@ -7,6 +7,8 @@ export interface ViewCommandsProps { /** `keyboard`: pressed by Enter or Space, the frame is then placed at the centre. */ onNew: (button: HTMLElement, keyboard: boolean) => void onParent?: (button: HTMLElement) => void + /** The parent view's name, the button's title (board 09). */ + parentTitle?: string onRecrop?: (button: HTMLElement) => void } @@ -18,6 +20,7 @@ export interface ViewCommandsProps { export default function ViewCommands({ onNew, onParent, + parentTitle, onRecrop, }: ViewCommandsProps) { const t = useT() @@ -29,6 +32,7 @@ export default function ViewCommands({ size="sm" variant="secondary" className={COMMAND} + title={parentTitle} onClick={(event) => onParent(event.currentTarget)} > {t('editor.annotations.view.parent')} diff --git a/apps/app/src/editor/annotations/useViewNavigation.ts b/apps/app/src/editor/annotations/useViewNavigation.ts new file mode 100644 index 00000000..be6a3ea7 --- /dev/null +++ b/apps/app/src/editor/annotations/useViewNavigation.ts @@ -0,0 +1,35 @@ +import type { Dispatch } from 'react' +import type { Scene } from '@/components/editor/annotations/EditableAnnotations' +import { guardDrawing } from './drawingGuard' +import { guardedPress } from './guardedPress' +import type { UiAction } from './uiState' + +export interface ViewNavigation { + /** Shows `viewId` on the drawing, behind the R-12 guard; the chip row follows (A14). */ + openView: (viewId: string, control: HTMLElement | null) => void + /** The parent of the view shown, or null on the main view. */ + parentId: string | null + goToParent: (control: HTMLElement | null) => void +} + +/** + * The two ways to reach another view from the drawing, « Vue parente » and « Ouvrir la + * vue » (D14). The view stays a UI state: no history entry is pushed (D15). + */ +export function useViewNavigation( + scene: Scene, + dispatch: Dispatch, +): ViewNavigation { + const shown = scene.doc.views.find((view) => view.id === scene.viewId) + const parentId = shown?.kind === 'detail' ? shown.parent_view_id : null + const openView = (viewId: string, control: HTMLElement | null) => { + const show = () => dispatch({ type: 'view', viewId }) + if (control) guardedPress(control, show) + else if (guardDrawing(show)) show() + } + return { + openView, + parentId, + goToParent: (control) => parentId !== null && openView(parentId, control), + } +} diff --git a/apps/app/tests/unit/editor-access-commands.test.tsx b/apps/app/tests/unit/editor-access-commands.test.tsx index f446fa35..821ea3b2 100644 --- a/apps/app/tests/unit/editor-access-commands.test.tsx +++ b/apps/app/tests/unit/editor-access-commands.test.tsx @@ -143,7 +143,7 @@ describe('a selected access', () => { const has = (name: string) => within(strip).queryByRole('button', { name }) expect(has("Déplacer l'accès")).not.toBeNull() expect(has('Propriétés')).not.toBeNull() - expect(has('Ouvrir la vue')).toBeNull() + expect(has('Ouvrir la vue')).not.toBeNull() expect(has('Recadrer la vue')).toBeNull() expect(has('Supprimer')).toBeNull() expect(has('Déplacer la zone')).toBeNull() diff --git a/apps/app/tests/unit/editor-history.test.tsx b/apps/app/tests/unit/editor-history.test.tsx index 709de793..e300f324 100644 --- a/apps/app/tests/unit/editor-history.test.tsx +++ b/apps/app/tests/unit/editor-history.test.tsx @@ -6,6 +6,7 @@ import { fireEvent, screen, waitFor, + within, } from '@testing-library/react' import userEvent from '@testing-library/user-event' import type { Location, NavigateFunction } from 'react-router-dom' @@ -13,7 +14,7 @@ import { ENTRY_KEY, type HistoryEntry } from '@/editor/history/entry' import { resolveSessionTag } from '@/editor/history/sessionTag' import { defaultView } from '@/editor/navigation' import { useLocaleStore } from '@/state/localeStore' -import { emptyText } from '../helpers/editorFixtures' +import { drawnText, emptyText, insert } from '../helpers/editorFixtures' import { resetStore, serverHas, SHEET, USER } from '../helpers/editorHarness' import { mountDraft, setWidth } from '../helpers/editorRoute' @@ -264,6 +265,41 @@ describe('a Back that must not unmount what is on screen', () => { }) }) +describe('the drawing view is not a history layer (D15)', () => { + const withDetail = () => + insert(drawnText(), { + kind: 'view', + value: { + kind: 'detail', + id: 'view-det', + name: { fr: 'Tête', en: 'Head' }, + svg_asset_id: null, + view_box: { min_x: 10, min_y: 10, width: 80, height: 80 }, + parent_view_id: 'view-main', + }, + }) + + it('changing the view writes no entry, and Back leaves the tab, not the view', async () => { + serverHas(withDetail(), 3, false) + const user = userEvent.setup() + mountDraft({ probe, entries: ['/app/admin', path] }) + await ready() + await user.click(tab('Annotations')) + await waitFor(() => expect(written()?.view.tab).toBe('annotations')) + const entry = written() + const toolbar = await screen.findByRole('toolbar', { name: 'Vues' }) + await user.click(within(toolbar).getByRole('button', { name: /Tête/ })) + await waitFor(() => + expect( + within(toolbar).getByRole('button', { name: /Tête/ }), + ).toHaveAttribute('aria-pressed', 'true'), + ) + expect(written()).toEqual(entry) + back() + await waitFor(() => expect(selected()).not.toBe('Annotations')) + }) +}) + describe('StrictMode', () => { it('writes the first entry once', async () => { mountDraft({ probe, strict: true }) diff --git a/apps/app/tests/unit/editor-view-navigation.test.tsx b/apps/app/tests/unit/editor-view-navigation.test.tsx new file mode 100644 index 00000000..c2fa73e6 --- /dev/null +++ b/apps/app/tests/unit/editor-view-navigation.test.tsx @@ -0,0 +1,223 @@ +/** U2 Task 21: « Vue parente », « Ouvrir la vue » and the access's second click (D14, D15). */ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { + cleanup, + render, + renderHook, + screen, + waitFor, + within, +} from '@testing-library/react' +import userEvent from '@testing-library/user-event' +import type { Annotation, Point } from '@faunosteo/core-wasm/types' +import DrawingGuardDialog from '@/components/editor/annotations/DrawingGuardDialog' +import { setFrameBox, startFrame } from '@/editor/annotations/frameWork' +import { useAnnotationWork, resetWork } from '@/editor/annotations/workStore' +import { useViewNavigation } from '@/editor/annotations/useViewNavigation' +import type { Scene } from '@/components/editor/annotations/EditableAnnotations' +import { useLocaleStore } from '@/state/localeStore' +import { LayeredTab } from '../helpers/annotationLayer' +import { drawnText, insert, point } from '../helpers/editorFixtures' +import { openReady, pressAt, resetStore } from '../helpers/editorHarness' + +vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => ({}) })) +vi.mock('@faunosteo/supabase', async (orig) => ({ + ...(await orig()), + getSheetVersion: vi.fn(), + downloadSheetDrawing: vi.fn(), + saveSheetDraft: vi.fn(), + getOfficialSheetAdmin: vi.fn(), + submitSheetReview: vi.fn(), +})) + +const props = { + readOnly: false, + navigate: vi.fn(), + reloadSheet: vi.fn(), + section: 'drawing' as const, + visited: ['drawing'] as const, + onOpenSection: vi.fn(), + onBack: vi.fn(), +} + +const SQUARE: Point[] = [ + [20, 20], + [80, 20], + [80, 80], + [20, 80], +] + +beforeEach(() => { + useLocaleStore.setState({ locale: 'fr' }) + const identity = { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 } + Object.defineProperty(window.SVGElement.prototype, 'getScreenCTM', { + configurable: true, + value: () => ({ ...identity, inverse: () => identity }), + }) +}) +afterEach(() => { + cleanup() + resetStore() + resetWork() +}) + +const detail = (id: string, name: string, parent: string) => ({ + kind: 'view' as const, + value: { + kind: 'detail' as const, + id, + name: { fr: name, en: name }, + svg_asset_id: null, + view_box: { min_x: 10, min_y: 10, width: 80, height: 80 }, + parent_view_id: parent, + }, +}) + +const access = (id: string, viewId: string, opens: string): Annotation => + ({ + id, + view_id: viewId, + category_id: 'cat-a', + laterality: 'unpaired', + geometry: { type: 'polygon', points: SQUARE }, + review_status: 'to_confirm', + color: null, + kind: 'detail_trigger', + name: null, + opens_view_id: opens, + }) as Annotation + +/** main > Tête (access `l` on main) > Œil (access `m` on Tête), plus a point on main. */ +function nested(): string { + let text = insert(drawnText(), detail('view-head', 'Tête', 'view-main')) + text = insert(text, detail('view-eye', 'Œil', 'view-head')) + text = insert(text, point('p', 50, 50, 5)) + text = insert(text, { + kind: 'annotation', + value: access('l', 'view-main', 'view-head'), + }) + return insert(text, { + kind: 'annotation', + value: access('m', 'view-head', 'view-eye'), + }) +} + +async function mount(text = nested()) { + await openReady({ text }) + render( +
+ + +
, + ) + await waitFor(() => + expect( + screen.getByTestId('annotation-canvas-drawing').querySelector('svg'), + ).not.toBeNull(), + ) +} + +const chip = (name: string) => + within(screen.getByRole('toolbar', { name: 'Vues' })).getByRole('button', { + name: new RegExp(name), + }) +const shown = (name: string) => + expect(chip(name)).toHaveAttribute('aria-pressed', 'true') +const marker = (id: string) => + screen + .getByTestId('annotation-canvas-drawing') + .querySelector(`[data-anno-id="${id}"]`)! + +async function selectRow(index: number) { + const list = await screen.findByRole('list', { + name: 'Annotations de la vue', + }) + await userEvent.setup().click(within(list).getAllByRole('button')[index]) +} + +describe('« Vue parente »', () => { + it('is absent on the main view', async () => { + await mount() + expect(screen.queryByRole('button', { name: 'Vue parente' })).toBeNull() + }) + + it('goes to the parent, titled with its name, then to the parent of that', async () => { + await mount() + const user = userEvent.setup() + await user.click(chip('Œil')) + const parent = await screen.findByRole('button', { name: 'Vue parente' }) + expect(parent).toHaveAttribute('title', 'Tête') + await user.click(parent) + await waitFor(() => shown('Tête')) + await user.click(screen.getByRole('button', { name: 'Vue parente' })) + await waitFor(() => shown('Vue principale')) + expect(screen.queryByRole('button', { name: 'Vue parente' })).toBeNull() + }) +}) + +describe('« Ouvrir la vue » and the second click', () => { + it('opens the view the selected access points to', async () => { + await mount() + await selectRow(1) + await userEvent + .setup() + .click(await screen.findByRole('button', { name: 'Ouvrir la vue' })) + await waitFor(() => shown('Tête')) + }) + + it('a second click on a selected access opens its view', async () => { + await mount() + await selectRow(1) + await screen.findByRole('button', { name: 'Ouvrir la vue' }) + pressAt(marker('l'), 50, 50) + await waitFor(() => shown('Tête')) + expect(screen.queryByRole('dialog')).toBeNull() + }) + + it('a second click on a selected point still opens its properties', async () => { + await mount() + await selectRow(0) + await screen.findByRole('button', { name: 'Propriétés' }) + pressAt(marker('p'), 50, 50) + expect(await screen.findByRole('dialog')).toBeInTheDocument() + shown('Vue principale') + }) +}) + +describe('the R-12 guard', () => { + const scene = { + viewId: 'view-head', + doc: { views: [{ id: 'view-head', kind: 'detail', parent_view_id: 'v0' }] }, + } as unknown as Scene + + function navigation(dispatch = vi.fn()) { + const { result } = renderHook(() => useViewNavigation(scene, dispatch)) + return { dispatch, ...result.current } + } + + it('reports the parent of the view shown', () => { + expect(navigation().parentId).toBe('v0') + }) + + it('opens at once when nothing is started', () => { + const { dispatch, openView } = navigation() + openView('v1', null) + expect(dispatch).toHaveBeenCalledWith({ type: 'view', viewId: 'v1' }) + }) + + it('asks first while work is started, and goes on only when allowed', () => { + startFrame({ + purpose: 'create', + viewId: 'view-head', + drawnOn: 'view-head', + box: null, + returnTo: 'view-head', + }) + setFrameBox({ x: 20, y: 20, w: 40, h: 40 }) + const { dispatch, goToParent, openView } = navigation() + openView('v1', null) + goToParent(null) + expect(dispatch).not.toHaveBeenCalled() + expect(useAnnotationWork.getState().question?.work).toBe('frame') + }) +})