Files
faunosteo/apps/app/src/editor/annotations/frame.ts
T
bchanot 463e0a2621 fix(editor): guard keeps pending series, mode end said, R-9 hook
- guardDrawing commits a keyboard or size series the focus still holds
  (its blur) and ends only the mode: a pending series is never dropped.
- A started move ended by a selection change is said (R-13).
- handleSize: no module-level state; useHandleSizes reads the device
  once per handle layer. The layer reads ResizeObserver contentRect.
- Dead kind check removed in SelectionPointCommands; long lines wrapped
  (code, doc).
- e2e: the zoom check waits for the transform to change; the move-mode
  arrow clicks are under expectNoShift.
2026-11-17 19:25:04 +01:00

171 lines
5.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Proposal arithmetic for a new point (plan « Default point and the frame », C26). The
* page only PROPOSES values; the core decides every one of them on the candidate
* document. Nothing here copies a core rule (no visibility constant, no preview size,
* no twin radius, no edge tolerance).
*/
export interface Frame {
x: number
y: number
w: number
h: number
}
export interface XY {
x: number
y: number
}
export interface CircleShape {
cx: number
cy: number
r: number
}
export type PointProposal =
| { kind: 'outside' }
| { kind: 'circle'; circle: CircleShape; shrunk: boolean }
| { kind: 'corrected'; circle: CircleShape }
/** Float noise of `v × 100` must not move a value across a hundredth. */
const EPSILON = 1e-9
export const round2 = (value: number): number => Math.round(value * 100) / 100
const floor2 = (value: number): number =>
Math.floor(value * 100 + EPSILON) / 100
const ceil2 = (value: number): number => Math.ceil(value * 100 - EPSILON) / 100
/**
* F from the composed root's `viewBox` attribute TEXT (the property `baseVal` is held in
* f32). Null when it is not four finite numbers or the span is not positive: the caller
* then has no frame, never the fallback `0 0 100 100`.
*/
export function parseFrame(viewBox: string | null): Frame | null {
if (viewBox === null) return null
const parts = viewBox
.trim()
.split(/[\s,]+/)
.map(Number)
if (parts.length !== 4 || !parts.every(Number.isFinite)) return null
const [x, y, w, h] = parts
return w > 0 && h > 0 ? { x, y, w, h } : null
}
/**
* 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,
frame.x + frame.w - at.x,
at.y - frame.y,
frame.y + frame.h - at.y,
)
}
/** One axis of candidate C: clamped inward on the hundredth grid, else exactly. */
function clampAxis(value: number, low: number, high: number): number {
const inwardLow = ceil2(low)
const inwardHigh = floor2(high)
if (inwardLow <= inwardHigh) {
return Math.min(Math.max(value, inwardLow), inwardHigh)
}
return Math.min(Math.max(value, low), high)
}
/**
* 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,
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),
r,
}
}
/**
* 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,
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 = fittedRadius(frame, radius)
if (distance >= r0) {
return {
kind: 'circle',
circle: { cx: at.x, cy: at.y, r: r0 },
shrunk: false,
}
}
const smaller = floor2(distance)
if (smaller < 0.01) {
return { kind: 'corrected', circle: correctedCircle(frame, at, radius) }
}
return {
kind: 'circle',
circle: { cx: at.x, cy: at.y, r: smaller },
shrunk: true,
}
}
/** The three vertices of a keyboard-created zone or link: 10 % of the smaller side. */
export function centreTriangle(frame: Frame): [number, number][] {
const side = 0.1 * Math.min(frame.w, frame.h)
const cx = frame.x + frame.w / 2
const cy = frame.y + frame.h / 2
const corners: [number, number][] = [
[cx, cy - side / 2],
[cx + side / 2, cy + side / 2],
[cx - side / 2, cy + side / 2],
]
return corners.map(([x, y]) => [round2(x), round2(y)])
}
/**
* 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, 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 =>
a.cx === b.cx && a.cy === b.cy && a.r === b.r