fix(app): bound the access list, order labels, test measuring input
This commit is contained in:
1 parent
5ec07af751
commit
03b5041a53
3 files changed
+109
-13
No files matched your search
@@ -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 type { Box, PlacedLabel } from '@/anatomy/accessLabels'
|
||||||
import { LABEL_H, LABEL_MAX_W } from '@/anatomy/accessLabels'
|
import { LABEL_H, LABEL_MAX_W } from '@/anatomy/accessLabels'
|
||||||
import Icon from '@/components/ui/Icon'
|
import Icon from '@/components/ui/Icon'
|
||||||
@@ -22,12 +23,23 @@ export interface AccessLabelsProps {
|
|||||||
const PILL =
|
const PILL =
|
||||||
'inline-flex items-center gap-1 rounded-full border bg-white px-2 text-xs ' +
|
'inline-flex items-center gap-1 rounded-full border bg-white px-2 text-xs ' +
|
||||||
'leading-none text-ink'
|
'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 }
|
const CHIP_STYLE = { maxWidth: LABEL_MAX_W, height: LABEL_H }
|
||||||
|
|
||||||
function boxStyle(b: Box) {
|
function boxStyle(b: Box) {
|
||||||
return { left: b.x, top: b.y, width: b.w, height: b.h }
|
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<string, Access> }) {
|
function Leaders({ placed, byId }: { placed: PlacedLabel[]; byId: Map<string, Access> }) {
|
||||||
return (
|
return (
|
||||||
<svg aria-hidden="true" className="pointer-events-none absolute inset-0 h-full w-full">
|
<svg aria-hidden="true" className="pointer-events-none absolute inset-0 h-full w-full">
|
||||||
@@ -71,7 +83,7 @@ function MeasureRow(props: { accesses: Access[]; count: number; row: RefObject<H
|
|||||||
<Twin key={a.id} access={a} />
|
<Twin key={a.id} access={a} />
|
||||||
))}
|
))}
|
||||||
<span data-measure="more" className={`${PILL} border-border`} style={{ height: LABEL_H }}>
|
<span data-measure="more" className={`${PILL} border-border`} style={{ height: LABEL_H }}>
|
||||||
{t('canvas.access.more').replace('{count}', String(props.count))}
|
{t('canvas.access.more').replace('{count}', () => String(props.count))}
|
||||||
<Icon name="chevron-down" className="h-3 w-3" />
|
<Icon name="chevron-down" className="h-3 w-3" />
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
@@ -80,16 +92,18 @@ function MeasureRow(props: { accesses: Access[]; count: number; row: RefObject<H
|
|||||||
|
|
||||||
interface MoreProps {
|
interface MoreProps {
|
||||||
box: Box
|
box: Box
|
||||||
|
areaHeight: number
|
||||||
items: Access[]
|
items: Access[]
|
||||||
onActivate: AccessLabelsProps['onActivate']
|
onActivate: AccessLabelsProps['onActivate']
|
||||||
}
|
}
|
||||||
|
|
||||||
/** « Autres vues (N) » and its list. Escape and an outside press close it. */
|
/** « Autres vues (N) » and its list. Escape and an outside press close it. */
|
||||||
function MoreViews({ box, items, onActivate }: MoreProps) {
|
function MoreViews({ box, areaHeight, items, onActivate }: MoreProps) {
|
||||||
const t = useT()
|
const t = useT()
|
||||||
const [open, setOpen] = useState(false)
|
const [open, setOpen] = useState(false)
|
||||||
const root = useRef<HTMLDivElement | null>(null)
|
const root = useRef<HTMLDivElement | null>(null)
|
||||||
const trigger = useRef<HTMLButtonElement | null>(null)
|
const trigger = useRef<HTMLButtonElement | null>(null)
|
||||||
|
const fit = listFit(box, areaHeight)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!open) return
|
if (!open) return
|
||||||
@@ -100,6 +114,11 @@ function MoreViews({ box, items, onActivate }: MoreProps) {
|
|||||||
return () => document.removeEventListener('pointerdown', onPress)
|
return () => document.removeEventListener('pointerdown', onPress)
|
||||||
}, [open])
|
}, [open])
|
||||||
|
|
||||||
|
const onBlur = (event: FocusEvent) => {
|
||||||
|
const next = event.relatedTarget
|
||||||
|
if (!(next instanceof Node && root.current?.contains(next))) setOpen(false)
|
||||||
|
}
|
||||||
|
|
||||||
const onKeyDown = (event: KeyboardEvent) => {
|
const onKeyDown = (event: KeyboardEvent) => {
|
||||||
if (!open || event.key !== 'Escape') return
|
if (!open || event.key !== 'Escape') return
|
||||||
// Neither the bilan's detail window nor the editor's deselect may see this Escape.
|
// 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) {
|
|||||||
<div
|
<div
|
||||||
ref={root}
|
ref={root}
|
||||||
onKeyDown={onKeyDown}
|
onKeyDown={onKeyDown}
|
||||||
|
onBlur={onBlur}
|
||||||
className="pointer-events-none absolute"
|
className="pointer-events-none absolute"
|
||||||
style={{ left: box.x, top: box.y, width: box.w }}
|
style={{ left: box.x, top: box.y, width: box.w }}
|
||||||
>
|
>
|
||||||
@@ -124,17 +144,20 @@ function MoreViews({ box, items, onActivate }: MoreProps) {
|
|||||||
className={`${PILL} pointer-events-auto border-border`}
|
className={`${PILL} pointer-events-auto border-border`}
|
||||||
style={{ height: box.h, width: box.w }}
|
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))}
|
||||||
<Icon name="chevron-down" className="h-3 w-3" />
|
<Icon name="chevron-down" className="h-3 w-3" />
|
||||||
</button>
|
</button>
|
||||||
{open && (
|
{open && (
|
||||||
<ul className="pointer-events-auto absolute right-0 top-full z-10 mt-1 flex w-max max-w-[220px] flex-col gap-1 rounded-md border border-border bg-white p-1 shadow">
|
<ul
|
||||||
|
className={`pointer-events-auto absolute right-0 z-10 flex w-max max-w-[220px] flex-col gap-1 overflow-y-auto rounded-md border border-border bg-white p-1 shadow ${fit.up ? 'bottom-full mb-1' : 'top-full mt-1'}`}
|
||||||
|
style={{ maxHeight: fit.maxHeight }}
|
||||||
|
>
|
||||||
{items.map((a) => (
|
{items.map((a) => (
|
||||||
<li key={a.id}>
|
<li key={a.id}>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
title={a.name}
|
title={a.name}
|
||||||
aria-label={t('canvas.access.open').replace('{name}', a.name)}
|
aria-label={t('canvas.access.open').replace('{name}', () => a.name)}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setOpen(false)
|
setOpen(false)
|
||||||
onActivate(a.id, a.opensViewId)
|
onActivate(a.id, a.opensViewId)
|
||||||
@@ -158,10 +181,13 @@ function MoreViews({ box, items, onActivate }: MoreProps) {
|
|||||||
*/
|
*/
|
||||||
export default function AccessLabels(props: AccessLabelsProps) {
|
export default function AccessLabels(props: AccessLabelsProps) {
|
||||||
const t = useT()
|
const t = useT()
|
||||||
const { accesses, layout, measureRow } = useAccessLabels(props)
|
const { accesses, layout, areaHeight, measureRow } = useAccessLabels(props)
|
||||||
if (props.hidden) return null
|
if (props.hidden) return null
|
||||||
const byId = new Map(accesses.map((a) => [a.id, a]))
|
const byId = new Map(accesses.map((a) => [a.id, a]))
|
||||||
const shown = (layout?.placed ?? []).filter((p) => byId.has(p.id))
|
const rank = new Map(accesses.map((a, i) => [a.id, i]))
|
||||||
|
const shown = (layout?.placed ?? [])
|
||||||
|
.filter((p) => byId.has(p.id))
|
||||||
|
.sort((p, q) => (rank.get(p.id) ?? 0) - (rank.get(q.id) ?? 0))
|
||||||
const overflow = (layout?.overflow ?? []).flatMap((id) => byId.get(id) ?? [])
|
const overflow = (layout?.overflow ?? []).flatMap((id) => byId.get(id) ?? [])
|
||||||
return (
|
return (
|
||||||
<div data-testid="access-labels" className="pointer-events-none absolute inset-0 overflow-hidden">
|
<div data-testid="access-labels" className="pointer-events-none absolute inset-0 overflow-hidden">
|
||||||
@@ -174,7 +200,7 @@ export default function AccessLabels(props: AccessLabelsProps) {
|
|||||||
key={p.id}
|
key={p.id}
|
||||||
type="button"
|
type="button"
|
||||||
title={a.name}
|
title={a.name}
|
||||||
aria-label={t('canvas.access.open').replace('{name}', a.name)}
|
aria-label={t('canvas.access.open').replace('{name}', () => a.name)}
|
||||||
onClick={() => props.onActivate(a.id, a.opensViewId)}
|
onClick={() => props.onActivate(a.id, a.opensViewId)}
|
||||||
className={`${PILL} pointer-events-auto absolute`}
|
className={`${PILL} pointer-events-auto absolute`}
|
||||||
style={{ ...CHIP_STYLE, ...boxStyle(p.box), borderColor: a.color }}
|
style={{ ...CHIP_STYLE, ...boxStyle(p.box), borderColor: a.color }}
|
||||||
@@ -184,7 +210,7 @@ export default function AccessLabels(props: AccessLabelsProps) {
|
|||||||
</button>
|
</button>
|
||||||
)
|
)
|
||||||
})}
|
})}
|
||||||
{layout?.button && <MoreViews box={layout.button} items={overflow} onActivate={props.onActivate} />}
|
{layout?.button && <MoreViews box={layout.button} areaHeight={areaHeight} items={overflow} onActivate={props.onActivate} />}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -28,6 +28,8 @@ export interface AccessLabelsInput {
|
|||||||
export interface AccessLabelsState {
|
export interface AccessLabelsState {
|
||||||
accesses: Access[]
|
accesses: Access[]
|
||||||
layout: LabelLayout | null
|
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. */
|
/** The hidden row holding one measuring twin per label, then the list button's. */
|
||||||
measureRow: RefObject<HTMLDivElement | null>
|
measureRow: RefObject<HTMLDivElement | null>
|
||||||
}
|
}
|
||||||
@@ -139,8 +141,11 @@ export function useAccessLabels(input: AccessLabelsInput): AccessLabelsState {
|
|||||||
const measureRow = useRef<HTMLDivElement | null>(null)
|
const measureRow = useRef<HTMLDivElement | null>(null)
|
||||||
const [accesses, setAccesses] = useState<Access[]>([])
|
const [accesses, setAccesses] = useState<Access[]>([])
|
||||||
const [layout, setLayout] = useState<LabelLayout | null>(null)
|
const [layout, setLayout] = useState<LabelLayout | null>(null)
|
||||||
|
const [areaHeight, setAreaHeight] = useState(0)
|
||||||
|
|
||||||
// Pass 1: the drawing's accesses, so the measuring row can render them.
|
// 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(() => {
|
useLayoutEffect(() => {
|
||||||
setAccesses(hidden ? [] : readAccesses(drawing.current, locale))
|
setAccesses(hidden ? [] : readAccesses(drawing.current, locale))
|
||||||
}, [drawing, hidden, layoutKey, locale, tick])
|
}, [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.
|
// Pass 2: the row is in the DOM for these accesses; measure and place.
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
setLayout(hidden ? null : computeLayout(input, accesses, measureRow.current))
|
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.
|
// `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
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [accesses, hidden, layoutKey, locale, tick])
|
}, [accesses, hidden, layoutKey, locale, tick])
|
||||||
|
|
||||||
return { accesses, layout, measureRow }
|
return { accesses, layout, areaHeight, measureRow }
|
||||||
}
|
}
|
||||||
@@ -18,8 +18,11 @@ const NAMES: Record<string, [string, string]> = {
|
|||||||
a1: ['Tête', 'Head'],
|
a1: ['Tête', 'Head'],
|
||||||
a2: ['Membre antérieur', 'Foreleg'],
|
a2: ['Membre antérieur', 'Foreleg'],
|
||||||
a3: [LONG, LONG],
|
a3: [LONG, LONG],
|
||||||
|
a4: ['Prix $& net', 'Price $& net'],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let boxHeight = 300
|
||||||
|
|
||||||
function rect(x: number, y: number, w: number, h: number): DOMRect {
|
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() {} }
|
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. */
|
/** happy-dom has no layout: boxes come from the element's role in the test. */
|
||||||
function stubLayout() {
|
function stubLayout() {
|
||||||
vi.spyOn(Element.prototype, 'getBoundingClientRect').mockImplementation(function (this: Element) {
|
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')
|
const id = this.getAttribute('data-anno-id')
|
||||||
if (id) return rect(110, 120, 60, 40)
|
if (id) return rect(110, 120, 60, 40)
|
||||||
if (this.getAttribute('data-measure') === 'more') return rect(0, 0, 90, 20)
|
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) {
|
function Harness({ ids = ['a1', 'a2'], hidden = false, onActivate = () => {} }: HarnessProps) {
|
||||||
const box = useRef<HTMLDivElement | null>(null)
|
const box = useRef<HTMLDivElement | null>(null)
|
||||||
const drawing = useRef<SVGSVGElement | null>(null)
|
const drawing = useRef<SVGSVGElement | null>(null)
|
||||||
|
const zoom = useRef<HTMLDivElement | null>(null)
|
||||||
|
const point = '<circle data-anno-id="p1" data-anno-kind="point" fill="transparent"/>'
|
||||||
return (
|
return (
|
||||||
<div ref={box} data-testid="box">
|
<div ref={box} data-testid="box">
|
||||||
<svg ref={drawing} dangerouslySetInnerHTML={{ __html: accessMarkup(ids) }} />
|
<svg ref={drawing} dangerouslySetInnerHTML={{ __html: accessMarkup(ids) + point }} />
|
||||||
|
<div data-handle="{}" />
|
||||||
|
<div ref={zoom} data-testid="zoom" />
|
||||||
<AccessLabels
|
<AccessLabels
|
||||||
box={box}
|
box={box}
|
||||||
drawing={drawing}
|
drawing={drawing}
|
||||||
layoutKey="v1"
|
layoutKey="v1"
|
||||||
|
reserved={[zoom]}
|
||||||
hidden={hidden}
|
hidden={hidden}
|
||||||
onActivate={onActivate}
|
onActivate={onActivate}
|
||||||
/>
|
/>
|
||||||
@@ -82,6 +93,7 @@ function Harness({ ids = ['a1', 'a2'], hidden = false, onActivate = () => {} }:
|
|||||||
}
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
boxHeight = 300
|
||||||
useLocaleStore.setState({ locale: 'fr' })
|
useLocaleStore.setState({ locale: 'fr' })
|
||||||
stubLayout()
|
stubLayout()
|
||||||
place.mockReturnValue(layoutOf())
|
place.mockReturnValue(layoutOf())
|
||||||
@@ -107,6 +119,29 @@ describe('AccessLabels', () => {
|
|||||||
expect(input.button).toEqual({ w: 90, h: 20 })
|
expect(input.button).toEqual({ w: 90, h: 20 })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('measures the point disc, the handle and the reserved box relative to the box', () => {
|
||||||
|
render(<Harness />)
|
||||||
|
const input = place.mock.calls.at(-1)?.[0]
|
||||||
|
expect(input.obstacles).toEqual([
|
||||||
|
{ cx: 145, cy: 45, r: 15 },
|
||||||
|
{ cx: 62, cy: 62, r: 16 },
|
||||||
|
])
|
||||||
|
expect(input.reserved).toEqual([{ x: 290, y: 10, w: 50, h: 30 }])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('renders in access order whatever order the placement returns', () => {
|
||||||
|
place.mockReturnValue(layoutOf({ placed: layoutOf().placed.reverse() }))
|
||||||
|
render(<Harness />)
|
||||||
|
const titles = screen.getAllByRole('button').map((b) => b.getAttribute('title'))
|
||||||
|
expect(titles).toEqual(['Tête', 'Membre antérieur'])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('keeps « $& » in a name literal', () => {
|
||||||
|
place.mockReturnValue(layoutOf({ placed: [{ id: 'a4', box: { x: 5, y: 5, w: 80, h: 20 }, leader: null }] }))
|
||||||
|
render(<Harness ids={['a4']} />)
|
||||||
|
expect(screen.getByRole('button', { name: 'Ouvrir la vue « Prix $& net »' })).toBeInTheDocument()
|
||||||
|
})
|
||||||
|
|
||||||
it('names the buttons with the locale name and « Ouvrir la vue »', () => {
|
it('names the buttons with the locale name and « Ouvrir la vue »', () => {
|
||||||
render(<Harness />)
|
render(<Harness />)
|
||||||
expect(screen.getByRole('button', { name: 'Ouvrir la vue « Tête »' })).toBeInTheDocument()
|
expect(screen.getByRole('button', { name: 'Ouvrir la vue « Tête »' })).toBeInTheDocument()
|
||||||
@@ -187,6 +222,35 @@ describe('AccessLabels, « Autres vues »', () => {
|
|||||||
expect(more).toHaveFocus()
|
expect(more).toHaveFocus()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('bounds the list to the room below and scrolls', async () => {
|
||||||
|
boxHeight = 80
|
||||||
|
overflowing()
|
||||||
|
render(<Harness ids={['a1', 'a2', 'a3']} />)
|
||||||
|
await userEvent.click(screen.getByRole('button', { name: 'Autres vues (3)' }))
|
||||||
|
const list = screen.getByRole('list')
|
||||||
|
expect(list).toHaveStyle({ maxHeight: '46px' })
|
||||||
|
expect(list).toHaveClass('overflow-y-auto', 'top-full')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('opens upward when under three rows fit below', async () => {
|
||||||
|
place.mockReturnValue(
|
||||||
|
layoutOf({ overflow: ['a1', 'a2', 'a3'], button: { x: 300, y: 200, w: 90, h: 20 } }),
|
||||||
|
)
|
||||||
|
render(<Harness ids={['a1', 'a2', 'a3']} />)
|
||||||
|
await userEvent.click(screen.getByRole('button', { name: 'Autres vues (3)' }))
|
||||||
|
const list = screen.getByRole('list')
|
||||||
|
expect(list).toHaveClass('bottom-full')
|
||||||
|
expect(list).toHaveStyle({ maxHeight: '192px' })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('closes when the focus leaves it', async () => {
|
||||||
|
overflowing()
|
||||||
|
render(<Harness ids={['a1', 'a2', 'a3']} />)
|
||||||
|
await userEvent.click(screen.getByRole('button', { name: 'Autres vues (3)' }))
|
||||||
|
await userEvent.tab({ shift: true })
|
||||||
|
expect(screen.queryAllByRole('listitem')).toHaveLength(0)
|
||||||
|
})
|
||||||
|
|
||||||
it('closes on a press outside', async () => {
|
it('closes on a press outside', async () => {
|
||||||
overflowing()
|
overflowing()
|
||||||
render(<Harness ids={['a1', 'a2', 'a3']} />)
|
render(<Harness ids={['a1', 'a2', 'a3']} />)
|
||||||
|
|||||||
Reference in new issue
Block a user