Files
faunosteo/apps/app/src/editor/annotations/dialogOpening.ts
T
bchanot ebcf2723a0 fix(editor): overview rows ask first, no empty save, board 31 icons
U2 Task 6 review by self:

- The overview stays shown during a selection (A3): its rows and view
  rows now go through the drawing guard, so a started move or redraw
  asks before a click there drops it (R-12). Was a regression: the
  column was unreachable while selected before Task 6.
- The edit form compares and merges trimmed names and colour: a space
  typed alone is no change and writes no `update`.
- A layer opened with no opener (reload, Forward) returns the focus
  to its row on close.
- Board 31 A: icons on « Modifier », « Modifier la forme » (the
  shape's glyph), « Marquer comme relu »; « Supprimer » as red text
  with its trash icon.
- Tests: overview row and view row ask during a started move; a row
  asks during a started redraw; whitespace-only save; unnamed link
  read rows; focus back to the row with no opener. Dead helpers
  removed (tests/helpers openProperties, e2e propertiesDialog).

Mapping: the point-edit « Enter on the selected row … asks first »
test (which replaced « Toutes les annotations asks first ») keeps its
place; the overview-row path it did not cover now has its own test.
2026-11-17 22:17:20 +01:00

106 lines
3.8 KiB
TypeScript

import { useEffect, useMemo, useRef, type RefObject } from 'react'
import type { AnatomicalSheetDocument } from '@faunosteo/core-wasm/types'
import { visibleElement } from '../focusRequest'
import { MIN_EDIT_WIDTH } from '../mode'
import type { AnnotationLayer } from '../tabs'
import { CANVAS_BOX } from '../useEditorNavigation'
import { guardDrawing } from './drawingGuard'
/** What opened the properties dialog: the strip's « Propriétés », or the drawing or the list. */
export type OpenedBy = 'button' | 'drawing'
/** Opens the properties dialog of `id` (A1). */
export type OpenDialog = (id: string, by: OpenedBy) => void
/**
* The column the dialog sits over (A2), resolved when measured: the right column of the
* body shown (`data-annotation-aside`: `edit`, or `read` while read-only) at 1 024 px and
* wider, none below (stacked layout: the dialog is centred, R-14).
*/
function columnAnchor(readOnly: boolean): RefObject<HTMLElement | null> {
const column = `[data-annotation-aside="${readOnly ? 'read' : 'edit'}"]`
return {
get current() {
if (window.innerWidth < MIN_EDIT_WIDTH) return null
return visibleElement(column)
},
}
}
/** The visible row of the annotation `id` in the overview. */
function rowOf(id: string): HTMLElement | null {
const wanted = JSON.stringify({ kind: 'annotation', id })
return visibleElement('[data-focus-key]', (row) => {
return row.dataset.focusKey === wanted
})
}
/**
* The dialog's way in, out and place (board 32): an opening first commits a key series and
* asks started drawing work (R-12); once closed, the focus goes back to « Propriétés »
* when it opened it, else to the annotation's row, or the drawing when a filter hides it.
* The anchor is measured again when the editor turns read-only or back.
*/
export function useDialogOpening(
layer: AnnotationLayer | undefined,
readOnly: boolean,
) {
const opened = useRef<{ id: string; by: OpenedBy } | null>(null)
const latest = useRef(layer)
latest.current = layer
// A layer opened by a reload or a Forward has no opener: its row is the way back.
const shown = layer?.view?.id
if (shown && opened.current?.id !== shown) {
opened.current = { id: shown, by: 'drawing' }
}
const open: OpenDialog = (id, by) => {
const go = () => {
opened.current = { id, by }
latest.current?.open(id)
}
if (guardDrawing(go)) go()
}
const returnFocus = () => {
const last = opened.current
if (!last) return null
const button =
last.by === 'button' && visibleElement('[data-properties-button]')
return button || rowOf(last.id) || visibleElement(CANVAS_BOX)
}
// A new object per flip: the dialog measures its place again (no resize fires).
const anchor = useMemo(() => columnAnchor(readOnly), [readOnly])
return { open, returnFocus, anchor }
}
/** What a body lets the layer do with its selection. */
export interface LayerSelection {
viewId: string
selectedId: string | null
show: (viewId: string, annotationId: string) => void
clear: () => void
}
/**
* The selection follows the layer (a Back, a Forward, a reload): its annotation is shown
* on its view and selected; a layer whose annotation is gone leaves nothing selected.
*/
export function useLayerFollow(
layerId: string | null,
doc: AnatomicalSheetDocument,
body: LayerSelection,
): void {
const latest = useRef(body)
latest.current = body
const { viewId, selectedId } = body
useEffect(() => {
if (layerId === null) return
const found = doc.annotations.find((a) => a.id === layerId)
if (!found) {
if (selectedId !== null) latest.current.clear()
return
}
if (found.id === selectedId && found.view_id === viewId) return
latest.current.show(found.view_id, found.id)
}, [layerId, doc, viewId, selectedId])
}