/* * Zeilen des Markeneditors: Eingabe von Zeiten, Anzeige, Sortierung, Umwandlung für den Server. * * Reine Logik ohne DOM (marker-rows.test.js). Der Server prüft endgültig (FilmMarkers::normalize); hier geht es um * Eingabekomfort: Komma als Dezimalzeichen, Zeiten als „m:ss“ und eine stabile Reihenfolge. */ let counter = 0; /** Zeit aus einer Eingabe: „12,5“, „12.5“, „1:05“, „1:05,5“. Gibt null zurück, wenn nichts Brauchbares dasteht. */ export function toSeconds(input) { if (typeof input === 'number') { return Number.isFinite(input) && input >= 0 ? input : null; } const text = String(input ?? '').trim().replace(',', '.'); if (text === '') { return null; } const clock = text.match(/^(\d+):([0-5]?\d(?:\.\d+)?)$/); if (clock) { return Number(clock[1]) * 60 + Number(clock[2]); } if (!/^\d+(\.\d+)?$/.test(text)) { return null; } return Number(text); } /** „1:05,5“ für 65,5 Sekunden; unter einer Minute „0:07“ mit Zehntel nur, wenn es eines gibt. */ export function formatSeconds(seconds) { if (!Number.isFinite(seconds) || seconds < 0) { return '–'; } const rounded = Math.round(seconds * 10) / 10; const minutes = Math.floor(rounded / 60); const rest = rounded - minutes * 60; const whole = Math.floor(rest); const tenth = Math.round((rest - whole) * 10); return `${minutes}:${String(whole).padStart(2, '0')}${tenth > 0 ? ',' + tenth : ''}`; } /** Zeit für das Eingabefeld: Sekunden mit einer Nachkommastelle, ohne überflüssige Null, mit Komma. */ export function secondsField(seconds) { const rounded = Math.round(seconds * 10) / 10; return String(rounded).replace('.', ','); } /** Zeilen aus der Markenliste des Servers (jede Zeile bekommt einen Schlüssel für die Anzeige). */ export function fromMarkers(markers) { return (markers ?? []).map((marker) => ({ key: `m${(counter += 1)}`, t: secondsField(marker.t), label: marker.label ?? '', stop: marker.stop === true, prompt: marker.prompt ?? '', })); } /** Neue Zeile an der Stelle `seconds`. */ export function newRow(seconds) { return { key: `m${(counter += 1)}`, t: secondsField(seconds), label: '', stop: false, prompt: '', }; } /** Nach Zeit ordnen; Zeilen mit unlesbarer Zeit bleiben unten in ihrer Reihenfolge, damit sie nicht wandern. */ export function sortRows(rows) { const timed = rows.map((row, index) => ({ row, index, t: toSeconds(row.t) })); return timed .sort((a, b) => { if (a.t === null && b.t === null) { return a.index - b.index; } if (a.t === null) { return 1; } if (b.t === null) { return -1; } return a.t - b.t || a.index - b.index; }) .map((entry) => entry.row); } /** Was an den Server geht: Zeit als Zahl (oder, wenn unlesbar, der Text, damit der Server den Fehler benennt). */ export function toPayload(rows) { return rows.map((row) => { const seconds = toSeconds(row.t); return { t: seconds === null ? String(row.t ?? '') : seconds, label: String(row.label ?? ''), stop: row.stop === true, prompt: row.stop === true ? String(row.prompt ?? '') : '', }; }); } /** Zeilen als Marken für die Vorschau: nur gültige Zeiten, noch ohne Prüfung der Abstände (das macht parseMarkers). */ export function toPreviewMarkers(rows) { return rows .map((row) => ({ t: toSeconds(row.t), label: row.label, stop: row.stop === true, prompt: row.prompt })) .filter((marker) => marker.t !== null); }