Files
ontime/apps/client/src/views/teleprompter/teleprompter.scroll.ts
T
Claude 6589bcceee fix(views): make boolean view params work, and retune the prompter
The params editor could not represent a boolean which defaults to true.
Two bugs stacked:

- ParamInput fell back with `isStringBoolean(param) ?? defaultValue`, but
  isStringBoolean answers false for an absent param rather than nothing, so
  the ?? never fired and every switch opened off whatever its option said.
- An unchecked checkbox is absent from the form data rather than present and
  false, so switching one off wrote no param and the parser fell back to the
  default the user was trying to leave.

Either one alone is invisible while every boolean defaults to false, which is
why this surfaced with the teleprompter. Together they made the whole panel
look inert: the switch showed off, the view showed on, and Apply did nothing.

Prompter changes from the review:

- Speed is calibrated against the reading rate rather than picked for feel.
  30 lines per minute was about 300 words per minute, roughly twice a
  broadcast read; the default is now 12, measured at 129 wpm on the default
  column. The ceiling comes down from 200 to 40 so the arrows stay useful.
- Smaller, denser defaults: 40px over 1.3 line height in an 80% column, which
  is 21 lines on a 1080p screen where the old defaults gave 11.
- The reading line is a marker one line tall in the gutter beside the text,
  replacing the rule across the words and the pair of margin arrows. The
  option is a boolean now that there is one style rather than three.
- Space always drives the transport. It was deferring to whichever control
  had focus, so a prompter stopped responding to the pedal after anyone
  touched a button; the overlay drops focus after a pointer press instead.
  Enter still activates a focused control, so the overlay stays keyboard
  operable.
- The help dialog is laid out as the rundown shortcuts panel is, down to the
  Kbd keycaps and the grouping, and no longer explains foot pedals.

Headings stay on the same left rail as the script rather than centred: they
are signposts for the operator, and a second alignment would give the eye
something new to find at every segment change.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cb8RVPNQ2ETPJxdy4b8CHf
2026-08-18 12:00:25 +00:00

109 lines
3.9 KiB
TypeScript

/**
* Pure scroll arithmetic for the teleprompter.
*
* Everything here is side effect free so it can be unit tested without a DOM.
* The hook that owns the requestAnimationFrame loop is the only caller.
*/
/**
* Lines per minute.
*
* The default is calibrated against the reading rate rather than picked for
* feel: broadcast presenters read at 140-160 words per minute and conference
* talent slower still, and at the default column width a line carries a dozen
* or so words. Twelve lines per minute lands in that band. The ceiling is set
* where the text stops being readable at all, not at the fastest the loop can
* physically scroll, so the arrow keys stay useful across their whole range.
*/
export const MIN_SPEED = 1;
export const MAX_SPEED = 40;
export const DEFAULT_SPEED = 12;
/** how much one speed adjustment moves, shared by the keymap and the overlay */
export const SPEED_STEP = 1;
export const SPEED_STEP_COARSE = 5;
/** font size multiplier applied on top of the configured size by the +/- keys */
const MIN_FONT_SCALE = 0.4;
const MAX_FONT_SCALE = 3;
export const FONT_SCALE_STEP = 0.1;
/**
* requestAnimationFrame is suspended in background tabs, so the timestamp can
* jump by minutes when the view becomes visible again.
* We clamp the frame delta so a resume can never teleport the script.
*/
export const MAX_FRAME_DELTA_MS = 100;
/** how aggressively an eased jump converges on its target, per second */
const CATCH_UP_RATE = 8;
/** below this distance an eased jump is considered arrived */
const CATCH_UP_EPSILON = 0.5;
export function clamp(value: number, min: number, max: number): number {
if (Number.isNaN(value)) return min;
return Math.min(Math.max(value, min), max);
}
export function clampSpeed(value: number): number {
return clamp(value, MIN_SPEED, MAX_SPEED);
}
export function clampFontScale(value: number): number {
return clamp(value, MIN_FONT_SCALE, MAX_FONT_SCALE);
}
/**
* Converts a speed in lines per minute into pixels per second.
* Lines per minute is the unit prompter operators think in, and it is
* independent of font size, which is why it is what we persist.
*/
export function linesPerMinuteToPxPerSecond(linesPerMinute: number, lineHeightPx: number): number {
return (linesPerMinute / 60) * lineHeightPx;
}
/**
* Clamps a raw frame delta and converts it to seconds.
* @param deltaMs milliseconds since the previous frame
*/
export function frameDeltaSeconds(deltaMs: number): number {
if (!Number.isFinite(deltaMs) || deltaMs < 0) return 0;
return Math.min(deltaMs, MAX_FRAME_DELTA_MS) / 1000;
}
/**
* Advances the scroll position at a constant rate.
*
* The position is kept as a float by the caller: at readable prompter speeds the
* per frame delta is well under a pixel, so rounding on every frame would stall
* the scroll entirely. We return the exact float and let the DOM round on write.
*/
export function advance(
position: number,
pxPerSecond: number,
deltaSeconds: number,
maxScroll: number,
): { position: number; atEnd: boolean } {
const next = clamp(position + pxPerSecond * deltaSeconds, 0, Math.max(maxScroll, 0));
// a document which does not overflow is never "at the end": it may simply not
// have been measured yet, and stopping playback on that would be wrong
return { position: next, atEnd: maxScroll > 0 && next >= maxScroll };
}
/**
* Moves `current` towards `target` with an exponential ease.
*
* Framerate independent: the same wall clock duration produces the same curve
* regardless of how many frames it was sampled over.
*/
export function easeCatchUp(current: number, target: number, deltaSeconds: number): number {
if (deltaSeconds <= 0) return current;
const next = target + (current - target) * Math.exp(-CATCH_UP_RATE * deltaSeconds);
return Math.abs(next - target) < CATCH_UP_EPSILON ? target : next;
}
export function hasArrived(current: number, target: number): boolean {
return Math.abs(current - target) < CATCH_UP_EPSILON;
}