fix(editor): one smallest radius for keys and drag

This commit is contained in:
bchanot committed 2026-11-23 14:11:00 +01:00
1 parent ce76ff98fc
commit 251a517b99
4 files changed
+62 -10

No files matched your search

@@ -65,7 +65,8 @@ export function circleFromDrag(
}
/** D5, D21: `circle` with the radius `r`, at least `minR` and no more than the centre's room
* in `bounds` (the centre stays), then kept inside them. */
* in `bounds` (the centre stays), then kept inside them. A centre outside `bounds`, or with
* less room than `minR`, leaves the circle as it is: no radius is proposed. */
export function withRadius(
circle: Circle,
r: number,
@@ -78,6 +79,7 @@ export function withRadius(
circle.cy - bounds.y,
bounds.y + bounds.h - circle.cy,
)
if (room < minR) return circle
return circleInside(
{ ...circle, r: Math.min(Math.max(r, minR), room) },
bounds,
@@ -4,6 +4,9 @@ import { useEditorStore } from '../state/editorStore'
import { commitFocusedSeries } from './drawingGuard'
import { moveStep, round2, type Frame } from './frame'
import { withRadius } from './accessShape'
import { canvasSurface } from './frameDraw'
import { MIN_FRAME_PX } from './frameBox'
import { drawingPerClientPx } from './gesture'
import type { Drop } from './usePlacement'
import {
setSeriesPreview,
@@ -52,8 +55,11 @@ function step(input: PointEditInput, direction: Direction, big: boolean) {
stepSelection(target.id, target.geometry, { x: x * size, y: y * size })
}
/** D21: the smallest radius a key series reaches; the core judges the rest. */
const MIN_KEY_RADIUS = 0.01
/** D21: the drag's floor for a mouse (half the smallest frame side), in drawing units. */
function minKeyRadius(): number {
const perPx = drawingPerClientPx(canvasSurface()?.overlay.current ?? null)
return (MIN_FRAME_PX.mouse / 2) * (perPx ?? 1)
}
/** One key step of the radius, as a `size` series: grown or shrunk, kept in the frame. */
function stepRadius(input: PointEditInput, grow: boolean, big: boolean) {
@@ -65,7 +71,7 @@ function stepRadius(input: PointEditInput, grow: boolean, big: boolean) {
const circle = withRadius(
shown,
round2(shown.r + size),
MIN_KEY_RADIUS,
minKeyRadius(),
frame,
)
setSeriesPreview(target.id, circle, 'size')
@@ -7,6 +7,7 @@ import {
defaultCircle,
presetTarget,
rectanglePolygon,
withRadius,
} from '@/editor/annotations/accessShape'
import type { Frame } from '@/editor/annotations/frame'
import { initialUi, uiReducer } from '@/editor/annotations/uiState'
@@ -149,3 +150,22 @@ describe('the shape choice and the preset target', () => {
expect(presetTarget(null, children)).toBe('')
})
})
describe('withRadius (D21)', () => {
const circle = { type: 'circle', cx: 50, cy: 50, r: 10 } as const
it('floors at the minimum and caps at the centre room', () => {
expect(withRadius(circle, 1, 8, BOUNDS).r).toBe(8)
expect(withRadius(circle, 90, 8, BOUNDS)).toEqual({ ...circle, r: 50 })
})
it('a centre outside the bounds proposes no radius', () => {
const out = { ...circle, cx: 150 }
expect(withRadius(out, 20, 8, BOUNDS)).toEqual(out)
})
it('a centre with less room than the minimum proposes no radius', () => {
const edge = { ...circle, cx: 3 }
expect(withRadius(edge, 20, 8, BOUNDS)).toEqual(edge)
})
})
+30 -6
View File
@@ -846,7 +846,8 @@ describe('the radius handle (D5, D21)', () => {
fireEvent.pointerMove(el, { ...mouse, clientX: x, clientY: y })
fireEvent.pointerUp(el, { ...mouse, buttons: 0, clientX: x, clientY: y })
}
const linked = () => withLink(withPoints(point('a', 50, 50, 5)), 'l')
const linked = (r = 6) =>
withLink(withPoints(point('a', 50, 50, 5)), 'l', { cx: 70, cy: 70, r })
it('shows for a selected circle access only, as a button « Rayon de … »', async () => {
await mount(linked())
@@ -888,6 +889,29 @@ describe('the radius handle (D5, D21)', () => {
expect(geometryOf('l')).toMatchObject({ r: 8 })
})
it('the touch drag stops at 22 units (44 px / 2)', async () => {
await mount(linked())
await select(1)
const el = radiusHit()
const touch = { ...mouse, pointerType: 'touch' }
fireEvent.pointerDown(el, { ...touch, clientX: 76, clientY: 70 })
fireEvent.pointerMove(el, { ...touch, clientX: 80, clientY: 70 })
fireEvent.pointerMove(el, { ...touch, clientX: 90, clientY: 70 })
fireEvent.pointerUp(el, { ...touch, buttons: 0, clientX: 90, clientY: 70 })
expect(geometryOf('l')).toMatchObject({ r: 22 })
})
it('keys stop at the drag floor (8 units), not below', async () => {
await mount(linked(20))
await select(1)
radius().focus()
for (let i = 0; i < 3; i++) {
fireEvent.keyDown(radius(), { key: 'ArrowDown', shiftKey: true })
}
fireEvent.keyDown(radius(), { key: 'Enter' })
expect(geometryOf('l')).toMatchObject({ r: 8 })
})
it('a radius that leaves the frame is clamped, the centre kept', async () => {
await mount(linked())
await select(1)
@@ -896,27 +920,27 @@ describe('the radius handle (D5, D21)', () => {
})
it('arrows grow and shrink a series, Shift ×10, Enter commits once', async () => {
await mount(linked())
await mount(linked(20))
await select(1)
const before = depth()
radius().focus()
fireEvent.keyDown(radius(), { key: 'ArrowRight' })
fireEvent.keyDown(radius(), { key: 'ArrowUp', shiftKey: true })
expect(geometryOf('l')).toMatchObject({ r: 6 })
expect(geometryOf('l')).toMatchObject({ r: 20 })
expect(depth()).toBe(before)
fireEvent.keyDown(radius(), { key: 'ArrowLeft' })
fireEvent.keyDown(radius(), { key: 'Enter' })
expect(geometryOf('l')).toMatchObject({ cx: 70, cy: 70, r: 11 })
expect(geometryOf('l')).toMatchObject({ cx: 70, cy: 70, r: 25 })
expect(depth()).toBe(before + 1)
expect(message()).toHaveTextContent('Taille modifiée')
})
it('shrinking with ArrowDown, then leaving the handle, commits (BDR-050)', async () => {
await mount(linked())
await mount(linked(20))
await select(1)
fireEvent.keyDown(radius(), { key: 'ArrowDown', shiftKey: true })
fireEvent.blur(radius())
expect(geometryOf('l')).toMatchObject({ r: 1 })
expect(geometryOf('l')).toMatchObject({ r: 15 })
})
it('a radius the core refuses keeps the old circle and shows the refusal', async () => {