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:
1 parent
9d03e0675c
commit
8cd1bda6c2
13 files changed
+513
-100
No files matched your search
@@ -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 }
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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 })
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user