feat(editor): new points take the view's most used size (A5, R-10)

proposePoint, correctedCircle and centreCircle take the wanted radius,
fitted to the frame; pendingForClick, keyboardPending and the C26 test
(correctionFor) use it. The size comes from newPointSize: the strip's
choice, else the most used radius of the view, else defaultRadius.
pointSize.ts (stops, snap, range) and escape.ts (board 32 order) land
with their tests; the repair dialog keeps defaultRadius (D1).

Rewritten assertions: editor-frame.test.ts calls now pass
defaultRadius(frame) explicitly, same expected values.
This commit is contained in:
bchanot committed 2026-11-17 18:20:58 +01:00
1 parent 9d03e0675c
commit 8cd1bda6c2
13 files changed
+513 -100

No files matched your search

@@ -8,6 +8,7 @@ import type { CommitContext } from '@/editor/annotations/annotationCommit'
import { useAnnotationRequest } from '@/editor/annotations/focusTarget'
import { keyboardPending, type NewKind } from '@/editor/annotations/creation'
import { parseFrame, type Frame } from '@/editor/annotations/frame'
import { newPointSize } from '@/editor/annotations/pointSize'
import { isEditable } from '@/editor/annotations/geometryEdit'
import { useSelectionEscape } from '@/editor/annotations/deselect'
import { usePlacement } from '@/editor/annotations/usePlacement'
@@ -200,7 +201,7 @@ function CanvasColumn({
<KeyboardAdd
disabled={!model.measured || noCategory || scene.frame === null}
linkDisabled={model.children.length === 0}
onAdd={(kind) => addAtCentre(kind, scene.frame, dispatch)}
onAdd={(kind) => addAtCentre(kind, scene)}
/>
</GestureMessage>
</div>
@@ -249,11 +250,11 @@ function CanvasStage({
function addAtCentre(
kind: NewKind,
frame: Frame | null,
dispatch: Dispatch<UiAction>,
{ frame, doc, viewId, ui, dispatch }: Scene,
) {
if (!frame) return
dispatch({ type: 'pending_new', pending: keyboardPending(kind, frame) })
const size = newPointSize(doc.annotations, viewId, frame, ui.size)
dispatch({ type: 'pending_new', pending: keyboardPending(kind, frame, size) })
}
/** The creation panel while an element is pending, else the selection's properties. */
+23 -8
View File
@@ -1,6 +1,6 @@
import {
correctedCircle,
defaultRadius,
fittedRadius,
sameCircle,
type CircleShape,
type Frame,
@@ -19,22 +19,24 @@ const OUT_OF_FRAME = 'annotation.geometry.out_of_frame'
/**
* Candidate C is offered when the core refused the page's own proposal (its radius still
* `proposedRadius`) and that proposal was either a reduced radius (B) or refused
* `out_of_frame` (A at a float edge). A radius the user typed is an explicit value, and
* an A refused `too_small` (r0 already capped) offers nothing: the core's issue is shown.
* `proposedRadius`) and that proposal was either a reduced radius (B, under the wanted
* size `wantedRadius` fitted to F, R-10) or refused `out_of_frame` (A at a float edge). A
* radius the user typed is an explicit value, and an A refused `too_small` (r0 already
* capped) offers nothing: the core's issue is shown.
*/
export function correctionFor(
frame: Frame,
attempted: CircleShape,
refusedCodes: readonly string[],
proposedRadius: number | undefined,
wantedRadius: number,
): Correction | null {
if (proposedRadius === undefined || attempted.r !== proposedRadius)
return null
const reduced = attempted.r < defaultRadius(frame)
const reduced = attempted.r < fittedRadius(frame, wantedRadius)
if (!reduced && !refusedCodes.includes(OUT_OF_FRAME)) return null
const click = { x: attempted.cx, y: attempted.cy }
const circle = correctedCircle(frame, click)
const circle = correctedCircle(frame, click, wantedRadius)
return sameCircle(circle, attempted) ? null : { click, circle }
}
@@ -46,16 +48,29 @@ export type PointAttempt =
export type InsertCircle = (circle: CircleShape) => CommitResult
/** The radius the page proposed, and the size the new point asked for (A5). */
export interface PointRadii {
proposed: number | undefined
wanted: number
}
/** « Créer »: insert the pending circle; on a refusal, offer C (never insert it unasked). */
export function attemptPoint(
frame: Frame,
circle: CircleShape,
insert: InsertCircle,
proposedRadius: number | undefined,
radii: PointRadii,
): PointAttempt {
const result = insert(circle)
if (result.ok) return { status: 'created', id: result.id }
const correction = correctionFor(frame, circle, result.codes, proposedRadius)
const { proposed, wanted } = radii
const correction = correctionFor(
frame,
circle,
result.codes,
proposed,
wanted,
)
return correction
? { status: 'correction', correction }
: { status: 'refused', messages: result.messages }
+29 -24
View File
@@ -8,7 +8,7 @@ import type {
import {
centreCircle,
centreTriangle,
defaultRadius,
fittedRadius,
proposePoint,
round2,
type Frame,
@@ -27,6 +27,8 @@ export interface PendingElement {
correction?: Correction | null
/** The radius the page proposed; a radius typed since is the user's own value. */
proposedRadius?: number
/** The size the new point asked for (A5): the strip's size, before any fitting. */
wantedRadius?: number
}
/** The pending element with a geometry the user typed: C and the proposal lapse with it. */
@@ -84,14 +86,22 @@ export function directChildren(
)
}
/** The pending element a keyboard « Ajouter … » button holds at the centre of F. */
export function keyboardPending(kind: NewKind, frame: Frame): PendingElement {
/**
* The pending element a keyboard « Ajouter … » button holds at the centre of F; a point
* takes `radius`, the size of a new point (A5).
*/
export function keyboardPending(
kind: NewKind,
frame: Frame,
radius: number,
): PendingElement {
if (kind === 'point') {
const circle = centreCircle(frame)
const circle = centreCircle(frame, radius)
return {
kind,
geometry: { type: 'circle', ...circle },
proposedRadius: circle.r,
wantedRadius: radius,
}
}
return { kind, geometry: { type: 'polygon', points: centreTriangle(frame) } }
@@ -155,32 +165,27 @@ export type ClickOutcome =
| { kind: 'pending'; pending: PendingElement }
/**
* The pending point a click with the point tool holds: A or B at the click, or, when
* neither fits, the click with r0 and candidate C waiting for the user's confirmation.
* The pending point a click with the point tool holds, at the size `radius` (A5): A or B
* at the click, or, when neither fits, the click with r0 and candidate C waiting for the
* user's confirmation.
*/
export function pendingForClick(frame: Frame, click: XY): ClickOutcome {
const proposal = proposePoint(frame, click)
export function pendingForClick(
frame: Frame,
click: XY,
radius: number,
): ClickOutcome {
const proposal = proposePoint(frame, click, radius)
if (proposal.kind === 'outside') return { kind: 'outside' }
const point = { kind: 'point' as const, wantedRadius: radius }
if (proposal.kind === 'circle') {
const geometry = { type: 'circle' as const, ...proposal.circle }
const pending = {
kind: 'point' as const,
geometry,
proposedRadius: proposal.circle.r,
}
const pending = { ...point, geometry, proposedRadius: proposal.circle.r }
return { kind: 'pending', pending }
}
const at = { x: round2(click.x), y: round2(click.y) }
const geometry = {
type: 'circle' as const,
cx: at.x,
cy: at.y,
r: defaultRadius(frame),
}
const r = fittedRadius(frame, radius)
const geometry = { type: 'circle' as const, cx: at.x, cy: at.y, r }
const correction = { click: at, circle: proposal.circle }
const pending = { kind: 'point' as const, geometry, correction }
return {
kind: 'pending',
pending: { ...pending, proposedRadius: geometry.r },
}
const pending = { ...point, geometry, correction, proposedRadius: r }
return { kind: 'pending', pending }
}
@@ -16,7 +16,7 @@ import {
type CreationForm,
type PendingElement,
} from './creation'
import type { Frame } from './frame'
import { defaultRadius, type Frame } from './frame'
import type { EditableGeometry } from './geometryEdit'
/** What « Créer » or the confirmation of C works from. */
@@ -51,11 +51,16 @@ export function createAttempt(
const insert = inserterOf(input, form)
const { geometry } = pending
if (pending.kind === 'point' && geometry.type === 'circle' && frame) {
const radii = {
proposed: pending.proposedRadius,
// A point the page did not propose (none today) keeps the frame's fallback size.
wanted: pending.wantedRadius ?? defaultRadius(frame),
}
return attemptPoint(
frame,
geometry,
(c) => insert({ type: 'circle', ...c }),
pending.proposedRadius,
radii,
)
}
const result = insert(geometry)
+29
View File
@@ -0,0 +1,29 @@
export interface EscapeInput {
/** A keyboard series or a slider not yet committed (outside a move mode). */
previewPending: boolean
creating: boolean
/** A redraw or a move mode. */
inMode: boolean
chosenVertex: number | null
selectedId: string | null
}
export type EscapeAction =
| 'drop_preview'
| 'ask_creation'
| 'drop_mode'
| 'unchoose'
| 'deselect'
| null
/**
* Board 32 rows 3 to 6, one situation per press (rows 1 and 2, a dialog and a side panel,
* consume it first).
*/
export function escapeAction(s: EscapeInput): EscapeAction {
if (s.previewPending) return 'drop_preview'
if (s.creating) return 'ask_creation'
if (s.inMode) return 'drop_mode'
if (s.chosenVertex !== null) return 'unchoose'
return s.selectedId !== null ? 'deselect' : null
}
+35 -14
View File
@@ -52,12 +52,20 @@ export function parseFrame(viewBox: string | null): Frame | null {
return w > 0 && h > 0 ? { x, y, w, h } : null
}
/** r0 = min(12 + 0.03 × max(w, h), min(w, h) / 2), floored to 0.01. */
/**
* The fallback size of a new point in a view without any point (A5):
* r0 = min(12 + 0.03 × max(w, h), min(w, h) / 2), floored to 0.01.
*/
export function defaultRadius(frame: Frame): number {
const wanted = 12 + 0.03 * Math.max(frame.w, frame.h)
return floor2(Math.min(wanted, Math.min(frame.w, frame.h) / 2))
}
/** The size a new point asks for, capped so that it fits F at all: min(radius, min(w, h) / 2). */
export function fittedRadius(frame: Frame, radius: number): number {
return floor2(Math.min(radius, Math.min(frame.w, frame.h) / 2))
}
function distanceToEdge(frame: Frame, at: XY): number {
return Math.min(
at.x - frame.x,
@@ -78,13 +86,17 @@ function clampAxis(value: number, low: number, high: number): number {
}
/**
* Candidate C: radius r0, the centre clamped per axis into [x + r0, x + w − r0] and
* [y + r0, y + h − r0], rounded inward to 0.01. When that rounding leaves an empty
* Candidate C: radius r0 (the wanted `radius`, fitted to F), the centre clamped per axis
* into [x + r0, x + w − r0] and [y + r0, y + h − r0], rounded inward to 0.01. When that rounding leaves an empty
* interval (r0 = min(w, h) / 2 with an origin off the grid) the exact clamped value is
* used: the core accepts any decimals.
*/
export function correctedCircle(frame: Frame, at: XY): CircleShape {
const r = defaultRadius(frame)
export function correctedCircle(
frame: Frame,
at: XY,
radius: number,
): CircleShape {
const r = fittedRadius(frame, radius)
return {
cx: clampAxis(round2(at.x), frame.x + r, frame.x + frame.w - r),
cy: clampAxis(round2(at.y), frame.y + r, frame.y + frame.h - r),
@@ -93,16 +105,21 @@ export function correctedCircle(frame: Frame, at: XY): CircleShape {
}
/**
* The proposal at a click (c rounded to 0.01, d its distance to the nearest edge of F):
* The proposal at a click (c rounded to 0.01, d its distance to the nearest edge of F,
* r0 the wanted `radius` fitted to F):
* A (c, r0) when d ≥ r0; B (c, floor₀.₀₁(d)) when that radius is at least 0.01, else C
* at once; nothing outside F (the edge is inclusive).
*/
export function proposePoint(frame: Frame, click: XY): PointProposal {
export function proposePoint(
frame: Frame,
click: XY,
radius: number,
): PointProposal {
if (distanceToEdge(frame, click) < 0) return { kind: 'outside' }
const at = { x: round2(click.x), y: round2(click.y) }
// A click inside F whose rounding lands outside it has no room: straight to C.
const distance = Math.max(distanceToEdge(frame, at), 0)
const r0 = defaultRadius(frame)
const r0 = fittedRadius(frame, radius)
if (distance >= r0) {
return {
kind: 'circle',
@@ -112,7 +129,7 @@ export function proposePoint(frame: Frame, click: XY): PointProposal {
}
const smaller = floor2(distance)
if (smaller < 0.01) {
return { kind: 'corrected', circle: correctedCircle(frame, at) }
return { kind: 'corrected', circle: correctedCircle(frame, at, radius) }
}
return {
kind: 'circle',
@@ -138,11 +155,15 @@ export function centreTriangle(frame: Frame): [number, number][] {
* Where a keyboard-created point sits: the centre of F with r0. Clamped as candidate C is,
* so it fits even with an origin off the 0.01 grid and r0 = min(w, h) / 2.
*/
export function centreCircle(frame: Frame): CircleShape {
return correctedCircle(frame, {
x: frame.x + frame.w / 2,
y: frame.y + frame.h / 2,
})
export function centreCircle(frame: Frame, radius: number): CircleShape {
const centre = { x: frame.x + frame.w / 2, y: frame.y + frame.h / 2 }
return correctedCircle(frame, centre, radius)
}
/** R-11: one arrow = 0.5 % of the frame's longer side (0.01 at least); Shift ×10 on keys. */
export function moveStep(frame: Frame, big: boolean): number {
const step = Math.max(0.01, round2(Math.max(frame.w, frame.h) * 0.005))
return big ? round2(step * 10) : step
}
export const sameCircle = (a: CircleShape, b: CircleShape): boolean =>
@@ -0,0 +1,80 @@
import type { Annotation } from '@faunosteo/core-wasm/types'
import { defaultRadius, round2, type Frame } from './frame'
const pointRadii = (
annotations: readonly Annotation[],
viewId: string,
): number[] =>
annotations.flatMap((a) =>
a.kind === 'point' && a.view_id === viewId && a.geometry?.type === 'circle'
? [a.geometry.r]
: [],
)
/** The slider's stops: the distinct radii of the view's points, ascending (links excluded). */
export function sizeStops(
annotations: readonly Annotation[],
viewId: string,
): number[] {
return [...new Set(pointRadii(annotations, viewId))].sort((a, b) => a - b)
}
/** A5: the radius used most in the view, the smaller on a tie; null without any point. */
export function mostUsedSize(
annotations: readonly Annotation[],
viewId: string,
): number | null {
const counts = new Map<number, number>()
for (const r of pointRadii(annotations, viewId))
counts.set(r, (counts.get(r) ?? 0) + 1)
let best: [number, number] | null = null
for (const [r, n] of counts) {
if (!best || n > best[1] || (n === best[1] && r < best[0])) best = [r, n]
}
return best?.[0] ?? null
}
/** The nearest stop within `reach` drawing units, else the value on the 0.01 grid. */
export function snapSize(
value: number,
stops: readonly number[],
reach: number,
): number {
let nearest: number | null = null
for (const stop of stops) {
const closer =
nearest === null || Math.abs(stop - value) < Math.abs(nearest - value)
if (Math.abs(stop - value) <= reach && closer) nearest = stop
}
return nearest ?? round2(value)
}
/**
* A5: the size of a new point on every path (click, C26, keyboard): the strip's choice,
* else the view's most used size, else F's fallback.
*/
export function newPointSize(
annotations: readonly Annotation[],
viewId: string,
frame: Frame,
chosen: number | null,
): number {
return chosen ?? mostUsedSize(annotations, viewId) ?? defaultRadius(frame)
}
export interface SizeRange {
low: number
high: number
}
/** R-10: around the stops when there are some, around the fallback radius otherwise. */
export function sizeRange(
stops: readonly number[],
minimum: number | null,
fallback: number,
): SizeRange {
const smallest = stops.length > 0 ? stops[0] : fallback
const largest = stops.length > 0 ? stops[stops.length - 1] : fallback
const base = minimum === null ? smallest : Math.min(smallest, minimum)
return { low: round2(Math.max(0.01, base / 2)), high: round2(2 * largest) }
}
@@ -22,6 +22,8 @@ export interface UiState {
hint: string | null
/** The overview's « Filtrer par nom » term: it outlives a selection, not a view. */
filter: string
/** The size the strip's slider chose for the next point (A5); null: the view's own. */
size: number | null
}
export type UiAction =
@@ -37,6 +39,7 @@ export type UiAction =
| { type: 'trace_cancel' }
| { type: 'hint'; hint: string | null }
| { type: 'filter'; term: string }
| { type: 'size'; size: number | null }
export const initialUi: UiState = {
viewId: null,
@@ -47,6 +50,7 @@ export const initialUi: UiState = {
tracing: null,
hint: null,
filter: '',
size: null,
}
const TOOL_OF: Record<Tracing['target'], ToolId> = {
@@ -125,5 +129,7 @@ export function uiReducer(state: UiState, action: UiAction): UiState {
return { ...state, hint: action.hint }
case 'filter':
return { ...state, filter: action.term }
case 'size':
return { ...state, size: action.size }
}
}
@@ -8,6 +8,7 @@ import { useT } from '@/i18n/useT'
import { updateGeometry, type CommitContext } from './annotationCommit'
import { pendingForClick, type NewKind } from './creation'
import { round2, type Frame, type XY } from './frame'
import { newPointSize } from './pointSize'
import type { ToolId, Tracing, UiAction, UiState } from './uiState'
import type { GestureMessages } from './useGestureMessage'
@@ -25,6 +26,7 @@ export interface PlacementInput {
dispatch: Dispatch<UiAction>
frame: Frame | null
doc: AnatomicalSheetDocument
viewId: string
context: CommitContext
/** The line under the drawing: a kept drop says so with « Défaire », a refused one in red. */
message: Pick<GestureMessages, 'show'>
@@ -128,7 +130,9 @@ export function usePlacement(input: PlacementInput) {
if (ui.tool === 'zone' || ui.tool === 'link')
return tracing.trace(ui.tool, at)
if (ui.tool !== 'point' || !frame) return
const outcome = pendingForClick(frame, at)
const { doc, viewId } = input
const size = newPointSize(doc.annotations, viewId, frame, ui.size)
const outcome = pendingForClick(frame, at, size)
if (outcome.kind === 'pending') {
return dispatch({ type: 'pending_new', pending: outcome.pending })
}
+11 -2
View File
@@ -13,7 +13,12 @@ import {
type CommitContext,
} from './annotations/annotationCommit'
import { isEditable } from './annotations/geometryEdit'
import { correctedCircle, parseFrame, type Frame } from './annotations/frame'
import {
correctedCircle,
defaultRadius,
parseFrame,
type Frame,
} from './annotations/frame'
import {
geometryAccept,
rejectedDrawingFault,
@@ -60,7 +65,11 @@ function clampedVertex(frame: Frame, [x, y]: Point): Point {
export function proposeGeometry(frame: Frame, geometry: Geometry): Geometry {
if (geometry.type === 'circle') {
const at = { x: geometry.cx, y: geometry.cy }
return { type: 'circle', ...correctedCircle(frame, at) }
// The repair keeps the frame's own size (D1: unchanged until U5).
return {
type: 'circle',
...correctedCircle(frame, at, defaultRadius(frame)),
}
}
if (geometry.type === 'polygon') {
const points = geometry.points.map((p) => clampedVertex(frame, p))
@@ -0,0 +1,165 @@
/** U2 Task 4: point sizes (A5, R-10), the Escape order (board 32) and the move step (R-11). */
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import type {
Annotation,
AnatomicalSheetDocument,
} from '@faunosteo/core-wasm/types'
import { escapeAction, type EscapeInput } from '@/editor/annotations/escape'
import {
centreCircle,
correctedCircle,
moveStep,
proposePoint,
type Frame,
} from '@/editor/annotations/frame'
import {
mostUsedSize,
sizeRange,
sizeStops,
snapSize,
} from '@/editor/annotations/pointSize'
import { correctionFor } from '@/editor/annotations/correction'
import { keyboardPending, pendingForClick } from '@/editor/annotations/creation'
const DRAFT = resolve(
fileURLToPath(import.meta.url),
'../../../../../supabase/seed/anatomical-sheets/equine/draft/document.json',
)
const draft = JSON.parse(readFileSync(DRAFT, 'utf8')) as AnatomicalSheetDocument
const circle = (
id: string,
r: number,
kind: Annotation['kind'] = 'point',
): Annotation =>
({
id,
kind,
view_id: 'v',
geometry: { type: 'circle', cx: 0, cy: 0, r },
}) as Annotation
describe('point sizes on the bundled draft (A5, R-10)', () => {
it('main view stops 30 and 34, the most used 34; the head 27', () => {
expect(sizeStops(draft.annotations, 'main')).toEqual([30, 34])
expect(mostUsedSize(draft.annotations, 'main')).toBe(34)
expect(sizeStops(draft.annotations, 'head')).toEqual([27])
expect(mostUsedSize(draft.annotations, 'head')).toBe(27)
})
it('the main view range runs from 13.25 to 68 with the core minimum 26.5', () => {
const stops = sizeStops(draft.annotations, 'main')
expect(sizeRange(stops, 26.5, 74)).toEqual({ low: 13.25, high: 68 })
expect(sizeRange(stops, null, 74)).toEqual({ low: 15, high: 68 })
expect(sizeRange([], null, 20)).toEqual({ low: 10, high: 40 })
})
})
describe('mostUsedSize and sizeStops', () => {
it('a tie goes to the smaller; links never count; no point gives null', () => {
const tie = [
circle('a', 30),
circle('b', 34),
circle('c', 34),
circle('d', 30),
]
expect(mostUsedSize(tie, 'v')).toBe(30)
const links = [circle('a', 30), circle('l1', 50, 'detail_trigger')]
expect(sizeStops(links, 'v')).toEqual([30])
expect(mostUsedSize([circle('l', 50, 'detail_trigger')], 'v')).toBeNull()
expect(mostUsedSize(draft.annotations, 'nowhere')).toBeNull()
})
})
describe('snapSize', () => {
it('snaps to the nearest stop within the reach, the edge inclusive', () => {
expect(snapSize(31, [30, 34], 1)).toBe(30)
expect(snapSize(31.01, [30, 34], 1)).toBe(31.01)
expect(snapSize(32.5, [30, 34], 2)).toBe(34)
expect(snapSize(32, [30, 34], 2)).toBe(30)
expect(snapSize(20.123, [], 5)).toBe(20.12)
expect(snapSize(20.123, [30], 0)).toBe(20.12)
})
})
describe('escapeAction (board 32 rows 3 to 6)', () => {
const none: EscapeInput = {
previewPending: false,
creating: false,
inMode: false,
chosenVertex: null,
selectedId: null,
}
it('consumes one situation per press, in the board order', () => {
const all = { ...none, previewPending: true, creating: true, inMode: true }
const full = { ...all, chosenVertex: 2, selectedId: 'a' }
expect(escapeAction(full)).toBe('drop_preview')
expect(escapeAction({ ...full, previewPending: false })).toBe(
'ask_creation',
)
const mode = { ...full, previewPending: false, creating: false }
expect(escapeAction(mode)).toBe('drop_mode')
expect(escapeAction({ ...mode, inMode: false })).toBe('unchoose')
expect(escapeAction({ ...none, selectedId: 'a' })).toBe('deselect')
expect(escapeAction(none)).toBeNull()
})
})
describe('moveStep (R-11)', () => {
const f = (w: number, h: number): Frame => ({ x: 0, y: 0, w, h })
it('0.5 % of the longer side, 0.01 at least, Shift ×10', () => {
expect(moveStep(f(1200, 900), false)).toBe(6)
expect(moveStep(f(1200, 900), true)).toBe(60)
expect(moveStep(f(300, 300), false)).toBe(1.5)
expect(moveStep(f(1, 0.5), false)).toBe(0.01)
expect(moveStep(f(1, 0.5), true)).toBe(0.1)
})
})
describe('A5: every new point takes the given size', () => {
const frame: Frame = { x: 0, y: 0, w: 1000, h: 800 }
it('a click, the C26 candidate and the keyboard point use it', () => {
const clicked = pendingForClick(frame, { x: 500, y: 400 }, 34)
expect(clicked).toMatchObject({
kind: 'pending',
pending: { geometry: { r: 34 }, proposedRadius: 34, wantedRadius: 34 },
})
const corner = pendingForClick(frame, { x: 0, y: 0 }, 34)
expect(corner).toMatchObject({
pending: { correction: { circle: { cx: 34, cy: 34, r: 34 } } },
})
expect(keyboardPending('point', frame, 34).geometry).toEqual({
type: 'circle',
cx: 500,
cy: 400,
r: 34,
})
})
it('the arithmetic caps it at half the smaller side', () => {
const small: Frame = { x: 0, y: 0, w: 40, h: 30 }
expect(centreCircle(small, 34)).toEqual({ cx: 20, cy: 15, r: 15 })
expect(correctedCircle(small, { x: 0, y: 0 }, 34).r).toBe(15)
expect(proposePoint(frame, { x: 500, y: 400 }, 34)).toMatchObject({
circle: { r: 34 },
shrunk: false,
})
})
it('C26 tests a reduced radius against the given size, not the frame default', () => {
const near = { cx: 20, cy: 400, r: 20 }
const codes = ['annotation.geometry.too_small']
expect(correctionFor(frame, near, codes, 20, 34)?.circle).toEqual({
cx: 34,
cy: 400,
r: 34,
})
// At the given size: no correction for a too_small verdict.
const full = { cx: 500, cy: 400, r: 34 }
expect(correctionFor(frame, full, codes, 34, 34)).toBeNull()
})
})
@@ -1883,6 +1883,7 @@ describe('the strip', () => {
dispatch,
frame: null,
doc,
viewId: 'main',
context: { t: (key) => key, locale: 'fr' },
message: { show: vi.fn() },
}),
+117 -45
View File
@@ -67,33 +67,51 @@ describe('proposePoint', () => {
const frame = f(0, 0, 40, 30)
it('keeps the click with r0 when it is r0 or more from every edge (A)', () => {
expect(proposePoint(frame, { x: 20.004, y: 15.006 })).toEqual({
expect(
proposePoint(frame, { x: 20.004, y: 15.006 }, defaultRadius(frame)),
).toEqual({
kind: 'circle',
circle: { cx: 20, cy: 15.01, r: 13.2 },
shrunk: false,
})
expect(proposePoint(frame, { x: 13.2, y: 15 })).toMatchObject({
expect(
proposePoint(frame, { x: 13.2, y: 15 }, defaultRadius(frame)),
).toMatchObject({
shrunk: false,
})
})
it('keeps the click with a smaller radius when it is nearer an edge (B)', () => {
expect(proposePoint(frame, { x: 13.19, y: 15 })).toEqual({
expect(
proposePoint(frame, { x: 13.19, y: 15 }, defaultRadius(frame)),
).toEqual({
kind: 'circle',
circle: { cx: 13.19, cy: 15, r: 13.19 },
shrunk: true,
})
expect(proposePoint(frame, { x: 5.555, y: 15 })).toMatchObject({
expect(
proposePoint(frame, { x: 5.555, y: 15 }, defaultRadius(frame)),
).toMatchObject({
circle: { cx: 5.56, r: 5.56 },
})
})
it('goes straight to C when B would be under 0.01, and nothing outside F', () => {
expect(proposePoint(frame, { x: 0, y: 0 }).kind).toBe('corrected')
expect(proposePoint(frame, { x: 40, y: 15 }).kind).toBe('corrected')
expect(proposePoint(frame, { x: 0.004, y: 15 }).kind).toBe('corrected')
expect(proposePoint(frame, { x: -0.1, y: 15 })).toEqual({ kind: 'outside' })
expect(proposePoint(frame, { x: 20, y: 30.01 })).toEqual({
expect(proposePoint(frame, { x: 0, y: 0 }, defaultRadius(frame)).kind).toBe(
'corrected',
)
expect(
proposePoint(frame, { x: 40, y: 15 }, defaultRadius(frame)).kind,
).toBe('corrected')
expect(
proposePoint(frame, { x: 0.004, y: 15 }, defaultRadius(frame)).kind,
).toBe('corrected')
expect(
proposePoint(frame, { x: -0.1, y: 15 }, defaultRadius(frame)),
).toEqual({ kind: 'outside' })
expect(
proposePoint(frame, { x: 20, y: 30.01 }, defaultRadius(frame)),
).toEqual({
kind: 'outside',
})
})
@@ -101,24 +119,42 @@ describe('proposePoint', () => {
describe('correctedCircle (C)', () => {
it('clamps per axis with r0, inward on the hundredth grid', () => {
expect(correctedCircle(f(0, 0, 40, 30), { x: 0, y: 0 })).toEqual({
expect(
correctedCircle(
f(0, 0, 40, 30),
{ x: 0, y: 0 },
defaultRadius(f(0, 0, 40, 30)),
),
).toEqual({
cx: 13.2,
cy: 13.2,
r: 13.2,
})
expect(correctedCircle(f(0, 0, 40, 30), { x: 40, y: 30 })).toEqual({
expect(
correctedCircle(
f(0, 0, 40, 30),
{ x: 40, y: 30 },
defaultRadius(f(0, 0, 40, 30)),
),
).toEqual({
cx: 26.8,
cy: 16.8,
r: 13.2,
})
expect(correctedCircle(f(0.003, 0, 40, 30), { x: 0.003, y: 15 }).cx).toBe(
13.21,
)
expect(
correctedCircle(
f(0.003, 0, 40, 30),
{ x: 0.003, y: 15 },
defaultRadius(f(0.003, 0, 40, 30)),
).cx,
).toBe(13.21)
})
it('uses the exact clamped value when the grid leaves an empty interval', () => {
const frame = f(0.003, 0.004, 20, 20)
expect(correctedCircle(frame, { x: 0.003, y: 0.004 })).toEqual({
expect(
correctedCircle(frame, { x: 0.003, y: 0.004 }, defaultRadius(frame)),
).toEqual({
cx: 10.003,
cy: 10.004,
r: 10,
@@ -128,7 +164,9 @@ describe('correctedCircle (C)', () => {
describe('keyboard proposals', () => {
it('holds a point at the centre of F with r0 and a triangle of 10 % of the smaller side', () => {
expect(centreCircle(f(-50, 20, 40, 30))).toEqual({
expect(
centreCircle(f(-50, 20, 40, 30), defaultRadius(f(-50, 20, 40, 30))),
).toEqual({
cx: -30,
cy: 35,
r: 13.2,
@@ -147,12 +185,23 @@ describe('correctionFor', () => {
it('offers C for a refused B or an A refused out_of_frame, never for A too_small', () => {
const reduced = { cx: 13.19, cy: 15, r: 13.19 }
expect(
correctionFor(frame, reduced, ['annotation.geometry.too_small'], 13.19),
correctionFor(
frame,
reduced,
['annotation.geometry.too_small'],
13.19,
defaultRadius(frame),
),
).not.toBeNull()
const edge = { cx: 5, cy: 15, r: 13.2 }
expect(
correctionFor(frame, edge, ['annotation.geometry.out_of_frame'], 13.2)
?.circle.cx,
correctionFor(
frame,
edge,
['annotation.geometry.out_of_frame'],
13.2,
defaultRadius(frame),
)?.circle.cx,
).toBe(13.2)
expect(
correctionFor(
@@ -160,6 +209,7 @@ describe('correctionFor', () => {
{ cx: 20, cy: 15, r: 13.2 },
['annotation.geometry.too_small'],
13.2,
defaultRadius(frame),
),
).toBeNull()
})
@@ -167,7 +217,13 @@ describe('correctionFor', () => {
it('offers nothing when C is the circle that was just refused', () => {
const circle = { cx: 13.2, cy: 13.2, r: 13.2 }
expect(
correctionFor(frame, circle, ['annotation.geometry.out_of_frame'], 13.2),
correctionFor(
frame,
circle,
['annotation.geometry.out_of_frame'],
13.2,
defaultRadius(frame),
),
).toBeNull()
})
})
@@ -210,16 +266,14 @@ describe.each(FRAMES)('a point placed in the frame %s', (viewBox) => {
const frame = frameOf(bed(viewBox))!
for (const click of clicksOf(frame)) {
const b = bed(viewBox)
const proposal = proposePoint(frame, click)
const proposal = proposePoint(frame, click, defaultRadius(frame))
expect(proposal.kind).not.toBe('outside')
if (proposal.kind === 'outside') continue
const insert = circleInserter(b)
const attempt = attemptPoint(
frame,
proposal.circle,
insert,
proposal.circle.r,
)
const attempt = attemptPoint(frame, proposal.circle, insert, {
proposed: proposal.circle.r,
wanted: defaultRadius(frame),
})
expect(attempt).toMatchObject({ status: 'created' })
expect(currentErrors(b).size).toBe(0)
}
@@ -227,7 +281,11 @@ describe.each(FRAMES)('a point placed in the frame %s', (viewBox) => {
it('a corrected proposal is inserted only through the confirmation', () => {
const frame = frameOf(bed(viewBox))!
const proposal = proposePoint(frame, { x: frame.x, y: frame.y })
const proposal = proposePoint(
frame,
{ x: frame.x, y: frame.y },
defaultRadius(frame),
)
if (proposal.kind !== 'corrected')
throw new Error('corner must be corrected')
const b = bed(viewBox)
@@ -253,14 +311,16 @@ describe('a very elongated frame', () => {
it('refuses 1000 × 30 on too_small with the core message and creates nothing', () => {
const b = bed('0 0 1000 30')
const frame = frameOf(b)!
const proposal = proposePoint(frame, { x: 500, y: 15 })
if (proposal.kind !== 'circle') throw new Error('proposal')
const attempt = attemptPoint(
const proposal = proposePoint(
frame,
proposal.circle,
circleInserter(b),
proposal.circle.r,
{ x: 500, y: 15 },
defaultRadius(frame),
)
if (proposal.kind !== 'circle') throw new Error('proposal')
const attempt = attemptPoint(frame, proposal.circle, circleInserter(b), {
proposed: proposal.circle.r,
wanted: defaultRadius(frame),
})
expect(attempt.status).toBe('refused')
expect(b.preview.document).toBe(b.text)
})
@@ -277,16 +337,21 @@ describe('a detail crop', () => {
it('places a point inside the crop and corrects one on its edge, never outside it', () => {
const b = cropBed()
const frame = frameOf(b, 'view-det')!
const inside = proposePoint(frame, { x: 150, y: 350 })
const inside = proposePoint(frame, { x: 150, y: 350 }, defaultRadius(frame))
if (inside.kind !== 'circle') throw new Error('proposal')
const insert = circleInserter(b, 'view-det')
expect(
attemptPoint(frame, inside.circle, insert, inside.circle.r),
attemptPoint(frame, inside.circle, insert, {
proposed: inside.circle.r,
wanted: defaultRadius(frame),
}),
).toMatchObject({
status: 'created',
})
expect(proposePoint(frame, { x: 99, y: 350 })).toEqual({ kind: 'outside' })
const edge = proposePoint(frame, { x: 100, y: 200 })
expect(
proposePoint(frame, { x: 99, y: 350 }, defaultRadius(frame)),
).toEqual({ kind: 'outside' })
const edge = proposePoint(frame, { x: 100, y: 200 }, defaultRadius(frame))
expect(edge.kind).toBe('corrected')
expect(currentErrors(b).size).toBe(0)
})
@@ -298,8 +363,12 @@ describe('a radius the user typed (H5)', () => {
it('is never replaced by a corrected position: the core issue is shown', () => {
const typed = { cx: 20, cy: 15, r: 1 }
const codes = ['annotation.geometry.too_small']
expect(correctionFor(frame, typed, codes, 13.2)).toBeNull()
expect(correctionFor(frame, typed, codes, undefined)).toBeNull()
expect(
correctionFor(frame, typed, codes, 13.2, defaultRadius(frame)),
).toBeNull()
expect(
correctionFor(frame, typed, codes, undefined, defaultRadius(frame)),
).toBeNull()
})
it('on the core: a typed too-small radius creates nothing and keeps the message', () => {
@@ -309,7 +378,7 @@ describe('a radius the user typed (H5)', () => {
big,
{ cx: 2500, cy: 2500, r: 1 },
circleInserter(b),
defaultRadius(big),
{ proposed: defaultRadius(big), wanted: defaultRadius(big) },
)
expect(attempt).toMatchObject({ status: 'refused' })
expect(b.preview.document).toBe(b.text)
@@ -325,13 +394,16 @@ describe('the keyboard point (H4)', () => {
])('fits the frame %s by construction, with no correction', (viewBox) => {
const b = bed(viewBox)
const frame = frameOf(b)!
const circle = centreCircle(frame)
const circle = centreCircle(frame, defaultRadius(frame))
const slack = 1e-9 // float noise of x + w / 2 − r only; the core decides below
expect(circle.cx - circle.r).toBeGreaterThanOrEqual(frame.x - slack)
expect(circle.cy - circle.r).toBeGreaterThanOrEqual(frame.y - slack)
expect(circle.cx + circle.r).toBeLessThanOrEqual(frame.x + frame.w + slack)
expect(circle.cy + circle.r).toBeLessThanOrEqual(frame.y + frame.h + slack)
const attempt = attemptPoint(frame, circle, circleInserter(b), circle.r)
const attempt = attemptPoint(frame, circle, circleInserter(b), {
proposed: circle.r,
wanted: defaultRadius(frame),
})
expect(attempt.status).not.toBe('correction')
if (viewBox.endsWith('1000 30')) return
expect(attempt).toMatchObject({ status: 'created' })
@@ -345,13 +417,13 @@ describe.each(['0 0 1000 30', '0 0 4000 200'])(
const frame = frameOf(bed(viewBox))!
for (const click of clicksOf(frame)) {
const b = bed(viewBox)
const proposal = proposePoint(frame, click)
const proposal = proposePoint(frame, click, defaultRadius(frame))
if (proposal.kind === 'outside') continue
const attempt = attemptPoint(
frame,
proposal.circle,
circleInserter(b),
proposal.circle.r,
{ proposed: proposal.circle.r, wanted: defaultRadius(frame) },
)
expect(['created', 'refused', 'correction']).toContain(attempt.status)
expect(currentErrors(b).size).toBe(0)