Files
faunosteo/apps/app/src/editor/annotations/pointSize.ts
T
bchanot 8cd1bda6c2 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.
2026-11-17 18:20:58 +01:00

81 lines
2.5 KiB
TypeScript

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) }
}