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: {
...props.handles,
scale: gestures.framing.framing.scale,
pan: {
x: gestures.framing.framing.x,
y: gestures.framing.framing.y,
},
dragged: gestures.drag,
},
}}
@@ -223,7 +227,11 @@ interface LayerProps {
root: RootAttributes | null
transform: string
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'
import type { Geometry } from '@faunosteo/core-wasm/types'
import { parseFrame } from '@/editor/annotations/frame'
import { visibleBounds } from '@/editor/annotations/removeBubble'
import {
useHandleSizes,
type HandleSizes,
@@ -16,6 +17,7 @@ import {
import { useAnnotationWork } from '@/editor/annotations/workStore'
import ZoneHandles, {
shownInserts,
type Scale,
type ZoneHandle,
type ZoneKeys,
} from './ZoneHandles'
@@ -33,6 +35,8 @@ export interface HandleLayerProps {
ratio: string
/** The framing's zoom: handles keep their CSS px size under it. */
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
onKeyDown: (event: KeyboardEvent<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
* before layout (0 px), one unit per px.
* shortcut: assumes a `meet` ratio (the core's default); a `none` ratio would need one
* factor per axis. Upgrade: read both factors when a drawing ships `none`.
* shortcut: assumes a `meet` ratio (the core's default), here and in `visibleBounds`; a
* `none` ratio would need one factor per axis. Upgrade: read both factors when a drawing
* ships `none`.
*/
function unitsPerPx(
viewBox: string,
@@ -90,10 +95,15 @@ function unitsPerPx(
*/
export default function HandleLayer(props: HandleLayerProps) {
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 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)
useReportHidden(zone, { unit, sizes }, props.onInsertsHidden)
useReportHidden(zone, scale, props.onInsertsHidden)
return (
<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.
key={zone.annotationId}
zone={zone}
scale={{ unit, sizes }}
scale={scale}
keys={props.zoneKeys}
/>
)}
@@ -139,7 +149,7 @@ function useShownZone(
function useReportHidden(
zone: ZoneHandle | null,
scale: Parameters<typeof shownInserts>[1],
scale: Pick<Scale, 'unit' | 'sizes'>,
report: (hidden: boolean) => void,
) {
const hidden = zone ? shownInserts(zone.points, scale).includes(false) : false
@@ -7,7 +7,9 @@ import {
} from 'react'
import type { Point } from '@faunosteo/core-wasm/types'
import type { HandleSizes } from '@/editor/annotations/handleSize'
import type { Frame } from '@/editor/annotations/frame'
import { MIN_VERTICES, showsInsert } from '@/editor/annotations/polygonEdit'
import { removeBubbleCentre } from '@/editor/annotations/removeBubble'
import type { ZoneEdit } from '@/editor/annotations/useZoneEdit'
import { useAnnotationWork } from '@/editor/annotations/workStore'
import { useT } from '@/i18n/useT'
@@ -29,17 +31,24 @@ export type ZoneKeys = Pick<
| 'onRemoveKey'
>
interface Scale {
export interface Scale {
/** Drawing units per CSS px. */
unit: number
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) =>
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. */
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) =>
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). */
function Remove({
at,
centre,
index,
disabled,
label,
scale,
onKeyDown,
}: {
at: Point
centre: Point
index: number
disabled: boolean
label: string
@@ -203,8 +212,7 @@ function Remove({
const hitH = sizes.bubble.h * unit
const w = sizes.bubbleMark.w * unit
const h = sizes.bubbleMark.h * unit
const cx = at[0]
const cy = at[1] - (sizes.vertex / 2 + 6) * unit - hitH / 2
const [cx, cy] = centre
const fill = disabled
? 'var(--color-ink-muted)'
: 'var(--color-danger-strong)'
@@ -390,7 +398,7 @@ export default function ZoneHandles({
))}
{chosen !== null && (
<Remove
at={points[chosen]}
centre={removeBubbleCentre({ points, inserts, ...scale }, chosen)}
index={chosen}
disabled={points.length <= MIN_VERTICES}
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 })
})
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 () => {
await mount(squareText([...SQUARE, [10, 50]]))
await select()