/* * Markeneditor im Medienmanager (Reiter „Marken“): Alpine-Komponente `lufMarkerEditor`. * * Dünn gehalten: Zeilenlogik steht in marker-rows.js, Halten an Marken in autostop.js (dieselbe Umsetzung wie später im * Mitglieder-Player), Binden der Quelle in lazy.js. Die Zeilen bleiben im Browser, bis „Marken speichern“ den * Arbeitsstand über Livewire schreibt (ManagesFilmMarkers::saveMarkers). */ import { bindAutostop } from './autostop.js'; import { formatSeconds, fromMarkers, newRow, sortRows, toPayload, toPreviewMarkers, toSeconds, secondsField } from './marker-rows.js'; import { parseMarkers } from './markers.js'; const BIND_WAIT_MS = 4000; export function markerEditor(config) { return { rows: fromMarkers(config.rows), token: config.token ?? null, canEdit: config.canEdit === true, hasDraft: config.hasDraft === true, dirty: false, saving: false, message: '', failed: false, confirmDiscard: false, stopped: null, position: 0, duration: null, autoStop: true, video: null, player: null, init() { this.video = this.$root.querySelector('video'); if (!this.video) { return; } this.player = bindAutostop(this.video, this.previewMarkers(), { autoStop: this.autoStop, onStop: (marker) => { this.stopped = marker; }, onResume: () => { this.stopped = null; }, }); this.video.addEventListener('timeupdate', () => { this.position = this.video.currentTime; }); this.video.addEventListener('loadedmetadata', () => { this.duration = this.video.duration; }); this.video.addEventListener('durationchange', () => { this.duration = this.video.duration; }); this.$watch('rows', () => this.player.setMarkers(this.previewMarkers()), { deep: true }); this.$watch('autoStop', (value) => this.player.setAutoStop(value)); }, previewMarkers() { return parseMarkers(toPreviewMarkers(this.rows)); }, // ---- Anzeige ---- clock(seconds) { return formatSeconds(seconds); }, rowClock(row) { const seconds = toSeconds(row.t); return seconds === null ? 'Zeit unlesbar' : formatSeconds(seconds); }, beyondEnd(row) { const seconds = toSeconds(row.t); return seconds !== null && this.duration !== null && Number.isFinite(this.duration) && seconds > this.duration; }, get hasBeyondEnd() { return this.rows.some((row) => this.beyondEnd(row)); }, get status() { if (this.dirty) { return 'Ungespeicherte Änderungen'; } return this.hasDraft ? 'Arbeitsstand, noch nicht veröffentlicht' : 'Entspricht der neuesten Fassung'; }, // ---- Bearbeiten ---- touch() { if (!this.canEdit) { return; } this.dirty = true; this.message = ''; this.failed = false; // Dem Server melden, dass Zeilen offen sind: „Neue Fassung anlegen“ lehnt dann ab statt sie zu verlieren. this.$wire.markerDirty = true; }, add() { this.rows = sortRows([...this.rows, newRow(this.video ? this.video.currentTime : 0)]); this.touch(); }, remove(key) { this.rows = this.rows.filter((row) => row.key !== key); this.touch(); }, useCurrent(row) { if (!this.video) { return; } row.t = secondsField(this.video.currentTime); this.rows = sortRows(this.rows); this.touch(); }, tidy() { this.rows = sortRows(this.rows); }, // ---- Film ---- /** Die Quelle ohne Abspielen binden (Springen soll den Film nicht starten). */ async ensureBound() { const waited = Date.now(); while (!this.video.lufPlayer && Date.now() - waited < BIND_WAIT_MS) { await new Promise((resolve) => setTimeout(resolve, 50)); } await this.video.lufPlayer?.start({ play: false }); }, async jump(row) { const seconds = toSeconds(row.t); if (!this.video || seconds === null) { return; } await this.ensureBound(); const go = () => { this.video.currentTime = seconds; }; if (this.video.readyState >= 1) { go(); } else { this.video.addEventListener('loadedmetadata', go, { once: true }); } }, async resume() { await this.player?.resume(); }, // ---- Speichern ---- async save() { if (!this.canEdit || this.saving) { return; } this.saving = true; this.message = ''; try { const result = await this.$wire.saveMarkers(toPayload(this.rows)); this.failed = result?.ok !== true; this.message = result?.message ?? 'Das Speichern hat nicht geantwortet.'; if (result?.ok === true) { this.rows = fromMarkers(result.markers); this.token = result.token ?? null; this.hasDraft = true; this.dirty = false; } } catch (error) { this.failed = true; this.message = 'Das Speichern ist fehlgeschlagen. Die Eingaben bleiben stehen; versuche es noch einmal.'; } finally { this.saving = false; } }, async discard() { if (!this.canEdit || this.saving) { return; } this.saving = true; this.confirmDiscard = false; try { const result = await this.$wire.discardMarkers(); this.failed = result?.ok !== true; this.message = result?.message ?? ''; if (result?.ok === true) { this.rows = fromMarkers(result.markers); this.token = null; this.hasDraft = false; this.dirty = false; } } catch (error) { this.failed = true; this.message = 'Das Verwerfen ist fehlgeschlagen. Versuche es noch einmal.'; } finally { this.saving = false; } }, }; } // Alpine kann vor oder nach diesem Skript starten: in beiden Fällen anmelden. Der Reiter rendert erst nach einem Klick, // also nach dem Start; ein Aufruf über die Adresse öffnet den Reiter deshalb nicht (ViewContentElement::mount). function register(Alpine) { Alpine.data('lufMarkerEditor', markerEditor); } if (typeof window !== 'undefined') { if (window.Alpine) { register(window.Alpine); } document.addEventListener('alpine:init', () => register(window.Alpine)); }