feat(editor): the « − » bubble follows the open side, on screen

This commit is contained in:
bchanot committed 2026-11-20 14:48:43 +01:00
1 parent 98156643eb
commit c469b474cc
4 files changed
+54 -14

No files matched your search

@@ -114,6 +114,10 @@ export default function AnnotationCanvas(props: AnnotationCanvasProps) {
handles: { handles: {
...props.handles, ...props.handles,
scale: gestures.framing.framing.scale, scale: gestures.framing.framing.scale,
pan: {
x: gestures.framing.framing.x,
y: gestures.framing.framing.y,
},
dragged: gestures.drag, dragged: gestures.drag,
}, },
}} }}
@@ -223,7 +227,11 @@ interface LayerProps {
root: RootAttributes | null root: RootAttributes | null
transform: string transform: string
overlay: Omit<OverlayProps, 'viewBox' | 'ratio'> overlay: Omit<OverlayProps, 'viewBox' | 'ratio'>
handles: HandleData & { scale: number; dragged: Geometry | null } handles: HandleData & {
scale: number
pan: { x: number; y: number }
dragged: Geometry | null
}
} }
/** /**
@@ -9,6 +9,7 @@ import {
} from 'react' } from 'react'
import type { Geometry } from '@faunosteo/core-wasm/types' import type { Geometry } from '@faunosteo/core-wasm/types'
import { parseFrame } from '@/editor/annotations/frame' import { parseFrame } from '@/editor/annotations/frame'
import { visibleBounds } from '@/editor/annotations/removeBubble'
import { import {
useHandleSizes, useHandleSizes,
type HandleSizes, type HandleSizes,
@@ -16,6 +17,7 @@ import {
import { useAnnotationWork } from '@/editor/annotations/workStore' import { useAnnotationWork } from '@/editor/annotations/workStore'
import ZoneHandles, { import ZoneHandles, {
shownInserts, shownInserts,
type Scale,
type ZoneHandle, type ZoneHandle,
type ZoneKeys, type ZoneKeys,
} from './ZoneHandles' } from './ZoneHandles'
@@ -33,6 +35,8 @@ export interface HandleLayerProps {
ratio: string ratio: string
/** The framing's zoom: handles keep their CSS px size under it. */ /** The framing's zoom: handles keep their CSS px size under it. */
scale: number scale: number
/** The framing's offset in CSS px: with the zoom, the part of the drawing on screen. */
pan: { x: number; y: number }
handle: PointHandle | null handle: PointHandle | null
onKeyDown: (event: KeyboardEvent<SVGGElement>) => void onKeyDown: (event: KeyboardEvent<SVGGElement>) => void
onBlur: (event: FocusEvent<SVGGElement>) => void onBlur: (event: FocusEvent<SVGGElement>) => void
@@ -69,8 +73,9 @@ function useBoxSize(svg: RefObject<SVGSVGElement | null>) {
/** /**
* Drawing units per CSS px: the box over the frame (`meet`), times the zoom. Unknown * Drawing units per CSS px: the box over the frame (`meet`), times the zoom. Unknown
* before layout (0 px), one unit per px. * before layout (0 px), one unit per px.
* shortcut: assumes a `meet` ratio (the core's default); a `none` ratio would need one * shortcut: assumes a `meet` ratio (the core's default), here and in `visibleBounds`; a
* factor per axis. Upgrade: read both factors when a drawing ships `none`. * `none` ratio would need one factor per axis. Upgrade: read both factors when a drawing
* ships `none`.
*/ */
function unitsPerPx( function unitsPerPx(
viewBox: string, viewBox: string,
@@ -90,10 +95,15 @@ function unitsPerPx(
*/ */
export default function HandleLayer(props: HandleLayerProps) { export default function HandleLayer(props: HandleLayerProps) {
const svg = useRef<SVGSVGElement | null>(null) const svg = useRef<SVGSVGElement | null>(null)
const unit = unitsPerPx(props.viewBox, useBoxSize(svg), props.scale) const size = useBoxSize(svg)
const unit = unitsPerPx(props.viewBox, size, props.scale)
const sizes = useHandleSizes() const sizes = useHandleSizes()
const frame = parseFrame(props.viewBox)
const bounds =
frame && visibleBounds(frame, size, { ...props.pan, scale: props.scale })
const scale: Scale = { unit, sizes, bounds, frame }
const zone = useShownZone(props.zone, props.dragged) const zone = useShownZone(props.zone, props.dragged)
useReportHidden(zone, { unit, sizes }, props.onInsertsHidden) useReportHidden(zone, scale, props.onInsertsHidden)
return ( return (
<svg <svg
ref={svg} ref={svg}
@@ -115,7 +125,7 @@ export default function HandleLayer(props: HandleLayerProps) {
// One group per annotation: a focused vertex never carries over to another polygon. // One group per annotation: a focused vertex never carries over to another polygon.
key={zone.annotationId} key={zone.annotationId}
zone={zone} zone={zone}
scale={{ unit, sizes }} scale={scale}
keys={props.zoneKeys} keys={props.zoneKeys}
/> />
)} )}
@@ -139,7 +149,7 @@ function useShownZone(
function useReportHidden( function useReportHidden(
zone: ZoneHandle | null, zone: ZoneHandle | null,
scale: Parameters<typeof shownInserts>[1], scale: Pick<Scale, 'unit' | 'sizes'>,
report: (hidden: boolean) => void, report: (hidden: boolean) => void,
) { ) {
const hidden = zone ? shownInserts(zone.points, scale).includes(false) : false const hidden = zone ? shownInserts(zone.points, scale).includes(false) : false
@@ -7,7 +7,9 @@ import {
} from 'react' } from 'react'
import type { Point } from '@faunosteo/core-wasm/types' import type { Point } from '@faunosteo/core-wasm/types'
import type { HandleSizes } from '@/editor/annotations/handleSize' import type { HandleSizes } from '@/editor/annotations/handleSize'
import type { Frame } from '@/editor/annotations/frame'
import { MIN_VERTICES, showsInsert } from '@/editor/annotations/polygonEdit' import { MIN_VERTICES, showsInsert } from '@/editor/annotations/polygonEdit'
import { removeBubbleCentre } from '@/editor/annotations/removeBubble'
import type { ZoneEdit } from '@/editor/annotations/useZoneEdit' import type { ZoneEdit } from '@/editor/annotations/useZoneEdit'
import { useAnnotationWork } from '@/editor/annotations/workStore' import { useAnnotationWork } from '@/editor/annotations/workStore'
import { useT } from '@/i18n/useT' import { useT } from '@/i18n/useT'
@@ -29,17 +31,24 @@ export type ZoneKeys = Pick<
| 'onRemoveKey' | 'onRemoveKey'
> >
interface Scale { export interface Scale {
/** Drawing units per CSS px. */ /** Drawing units per CSS px. */
unit: number unit: number
sizes: HandleSizes sizes: HandleSizes
/** Where the « − » bubble may lie: the frame ∩ the part on screen; null: no bound. */
bounds: Frame | null
/** The whole frame; null: none. */
frame: Frame | null
} }
const segmentPx = (a: Point, b: Point, unit: number) => const segmentPx = (a: Point, b: Point, unit: number) =>
Math.hypot(b[0] - a[0], b[1] - a[1]) / unit Math.hypot(b[0] - a[0], b[1] - a[1]) / unit
/** R-9: the « + » of segment `i` (vertex i to the next) shows at the current zoom. */ /** R-9: the « + » of segment `i` (vertex i to the next) shows at the current zoom. */
export function shownInserts(points: readonly Point[], { unit, sizes }: Scale) { export function shownInserts(
points: readonly Point[],
{ unit, sizes }: Pick<Scale, 'unit' | 'sizes'>,
) {
return points.map((p, i) => return points.map((p, i) =>
showsInsert(segmentPx(p, points[(i + 1) % points.length], unit), sizes.hit), showsInsert(segmentPx(p, points[(i + 1) % points.length], unit), sizes.hit),
) )
@@ -184,14 +193,14 @@ function Vertex({
/** The « − » bubble over the chosen vertex (board 05), greyed at three vertices (12 B). */ /** The « − » bubble over the chosen vertex (board 05), greyed at three vertices (12 B). */
function Remove({ function Remove({
at, centre,
index, index,
disabled, disabled,
label, label,
scale, scale,
onKeyDown, onKeyDown,
}: { }: {
at: Point centre: Point
index: number index: number
disabled: boolean disabled: boolean
label: string label: string
@@ -203,8 +212,7 @@ function Remove({
const hitH = sizes.bubble.h * unit const hitH = sizes.bubble.h * unit
const w = sizes.bubbleMark.w * unit const w = sizes.bubbleMark.w * unit
const h = sizes.bubbleMark.h * unit const h = sizes.bubbleMark.h * unit
const cx = at[0] const [cx, cy] = centre
const cy = at[1] - (sizes.vertex / 2 + 6) * unit - hitH / 2
const fill = disabled const fill = disabled
? 'var(--color-ink-muted)' ? 'var(--color-ink-muted)'
: 'var(--color-danger-strong)' : 'var(--color-danger-strong)'
@@ -390,7 +398,7 @@ export default function ZoneHandles({
))} ))}
{chosen !== null && ( {chosen !== null && (
<Remove <Remove
at={points[chosen]} centre={removeBubbleCentre({ points, inserts, ...scale }, chosen)}
index={chosen} index={chosen}
disabled={points.length <= MIN_VERTICES} disabled={points.length <= MIN_VERTICES}
label={named('remove', chosen)} label={named('remove', chosen)}
@@ -417,6 +417,20 @@ describe('a double click on a handle (R-8, carried Task 2 gap)', () => {
expect(chosen()).toMatchObject({ vertex: 1 }) expect(chosen()).toMatchObject({ vertex: 1 })
}) })
it('the « − » bubble of a bottom vertex opens below it, inside the frame', async () => {
await mount(
squareText([
[20, 20],
[80, 20],
[50, 60],
]),
)
await select()
clickHandle('Sommet 3 de N', [50, 60])
const rect = hitOf('Retirer le sommet 3 de N')
expect(Number(rect.getAttribute('y'))).toBeGreaterThan(60)
})
it('on « − »: one removal, never a second', async () => { it('on « − »: one removal, never a second', async () => {
await mount(squareText([...SQUARE, [10, 50]])) await mount(squareText([...SQUARE, [10, 50]]))
await select() await select()