From 03b5041a5319e7a3fcb99823bfbcb42505f868f9 Mon Sep 17 00:00:00 2001 From: bchanot Date: Mon, 23 Nov 2026 10:39:16 +0100 Subject: [PATCH] fix(app): bound the access list, order labels, test measuring input --- .../src/components/canvas/AccessLabels.tsx | 46 ++++++++++--- .../src/components/canvas/useAccessLabels.ts | 8 ++- .../unit/components.AccessLabels.test.tsx | 68 ++++++++++++++++++- 3 files changed, 109 insertions(+), 13 deletions(-) diff --git a/apps/app/src/components/canvas/AccessLabels.tsx b/apps/app/src/components/canvas/AccessLabels.tsx index 3bf92816..8d019928 100644 --- a/apps/app/src/components/canvas/AccessLabels.tsx +++ b/apps/app/src/components/canvas/AccessLabels.tsx @@ -1,4 +1,5 @@ -import { useEffect, useRef, useState, type KeyboardEvent, type RefObject } from 'react' +import { useEffect, useRef, useState, type FocusEvent, + type KeyboardEvent, type RefObject } from 'react' import type { Box, PlacedLabel } from '@/anatomy/accessLabels' import { LABEL_H, LABEL_MAX_W } from '@/anatomy/accessLabels' import Icon from '@/components/ui/Icon' @@ -22,12 +23,23 @@ export interface AccessLabelsProps { const PILL = 'inline-flex items-center gap-1 rounded-full border bg-white px-2 text-xs ' + 'leading-none text-ink' +const LIST_GAP = 4 +const LIST_ROW = 28 +const LIST_MIN_ROWS = 3 const CHIP_STYLE = { maxWidth: LABEL_MAX_W, height: LABEL_H } function boxStyle(b: Box) { return { left: b.x, top: b.y, width: b.w, height: b.h } } +/** Under the button, bounded to the room left; above it when under three rows fit. */ +function listFit(box: Box, areaHeight: number) { + const below = areaHeight - (box.y + box.h) - 2 * LIST_GAP + const above = box.y - 2 * LIST_GAP + const up = below < LIST_MIN_ROWS * LIST_ROW && above > below + return { up, maxHeight: Math.max(up ? above : below, 0) } +} + function Leaders({ placed, byId }: { placed: PlacedLabel[]; byId: Map }) { return ( - {t('canvas.access.more').replace('{count}', String(props.count))} + {t('canvas.access.more').replace('{count}', () => String(props.count))} @@ -80,16 +92,18 @@ function MeasureRow(props: { accesses: Access[]; count: number; row: RefObject(null) const trigger = useRef(null) + const fit = listFit(box, areaHeight) useEffect(() => { if (!open) return @@ -100,6 +114,11 @@ function MoreViews({ box, items, onActivate }: MoreProps) { return () => document.removeEventListener('pointerdown', onPress) }, [open]) + const onBlur = (event: FocusEvent) => { + const next = event.relatedTarget + if (!(next instanceof Node && root.current?.contains(next))) setOpen(false) + } + const onKeyDown = (event: KeyboardEvent) => { if (!open || event.key !== 'Escape') return // Neither the bilan's detail window nor the editor's deselect may see this Escape. @@ -113,6 +132,7 @@ function MoreViews({ box, items, onActivate }: MoreProps) {
@@ -124,17 +144,20 @@ function MoreViews({ box, items, onActivate }: MoreProps) { className={`${PILL} pointer-events-auto border-border`} style={{ height: box.h, width: box.w }} > - {t('canvas.access.more').replace('{count}', String(items.length))} + {t('canvas.access.more').replace('{count}', () => String(items.length))} {open && ( -
    +
      {items.map((a) => (
    • ) })} - {layout?.button && } + {layout?.button && }
) } diff --git a/apps/app/src/components/canvas/useAccessLabels.ts b/apps/app/src/components/canvas/useAccessLabels.ts index 280d5dd0..da8175c1 100644 --- a/apps/app/src/components/canvas/useAccessLabels.ts +++ b/apps/app/src/components/canvas/useAccessLabels.ts @@ -28,6 +28,8 @@ export interface AccessLabelsInput { export interface AccessLabelsState { accesses: Access[] layout: LabelLayout | null + /** The box's height, for the list under « Autres vues ». */ + areaHeight: number /** The hidden row holding one measuring twin per label, then the list button's. */ measureRow: RefObject } @@ -139,8 +141,11 @@ export function useAccessLabels(input: AccessLabelsInput): AccessLabelsState { const measureRow = useRef(null) const [accesses, setAccesses] = useState([]) const [layout, setLayout] = useState(null) + const [areaHeight, setAreaHeight] = useState(0) // Pass 1: the drawing's accesses, so the measuring row can render them. + // `setAccesses` must always get a new array: the first display needs pass 2 to rerun + // once the row is rendered, so never bail out on equal content. useLayoutEffect(() => { setAccesses(hidden ? [] : readAccesses(drawing.current, locale)) }, [drawing, hidden, layoutKey, locale, tick]) @@ -148,9 +153,10 @@ export function useAccessLabels(input: AccessLabelsInput): AccessLabelsState { // Pass 2: the row is in the DOM for these accesses; measure and place. useLayoutEffect(() => { setLayout(hidden ? null : computeLayout(input, accesses, measureRow.current)) + setAreaHeight(input.box.current?.getBoundingClientRect().height ?? 0) // `input` is read as of this render; its refs are stable and its changes ride the keys. // eslint-disable-next-line react-hooks/exhaustive-deps }, [accesses, hidden, layoutKey, locale, tick]) - return { accesses, layout, measureRow } + return { accesses, layout, areaHeight, measureRow } } diff --git a/apps/app/tests/unit/components.AccessLabels.test.tsx b/apps/app/tests/unit/components.AccessLabels.test.tsx index 7de9b17f..d7ff946a 100644 --- a/apps/app/tests/unit/components.AccessLabels.test.tsx +++ b/apps/app/tests/unit/components.AccessLabels.test.tsx @@ -18,8 +18,11 @@ const NAMES: Record = { a1: ['Tête', 'Head'], a2: ['Membre antérieur', 'Foreleg'], a3: [LONG, LONG], + a4: ['Prix $& net', 'Price $& net'], } +let boxHeight = 300 + function rect(x: number, y: number, w: number, h: number): DOMRect { return { x, y, left: x, top: y, width: w, height: h, right: x + w, bottom: y + h, toJSON() {} } } @@ -27,7 +30,10 @@ function rect(x: number, y: number, w: number, h: number): DOMRect { /** happy-dom has no layout: boxes come from the element's role in the test. */ function stubLayout() { vi.spyOn(Element.prototype, 'getBoundingClientRect').mockImplementation(function (this: Element) { - if (this.getAttribute('data-testid') === 'box') return rect(10, 20, 400, 300) + if (this.getAttribute('data-testid') === 'box') return rect(10, 20, 400, boxHeight) + if (this.getAttribute('data-anno-kind') === 'point') return rect(150, 60, 10, 10) + if (this.hasAttribute('data-handle')) return rect(60, 70, 24, 24) + if (this.getAttribute('data-testid') === 'zoom') return rect(300, 30, 50, 30) const id = this.getAttribute('data-anno-id') if (id) return rect(110, 120, 60, 40) if (this.getAttribute('data-measure') === 'more') return rect(0, 0, 90, 20) @@ -67,13 +73,18 @@ interface HarnessProps { function Harness({ ids = ['a1', 'a2'], hidden = false, onActivate = () => {} }: HarnessProps) { const box = useRef(null) const drawing = useRef(null) + const zoom = useRef(null) + const point = '' return (
- + +
+