// clip-card.mjs — a clip block's video, in the editor's card (scenes and // clips spec §2, §4). // // The card's file picker ([slides-clip-input]) takes an MP4 or WebM; // Insert → Clip adds the block and opens it at once (commands.mjs). The // upload is uploads.mjs's uploadClip: the first frame drawn on a canvas // and uploaded as the poster, then the video naming it, and one batch // (inspector-ops.mjs's registerClipOps) registering both and pointing // the block at the video, so the clip lands with one undo. The status // line ([slides-clip-status], role="status") says how it went in the // catalog's words: the upload's own (uploading, done, failed) and a // clip's refusals with the file's name (data-text-refused, // data-text-refused-big on the picker). // // mountClipCard({root, getSlideId, getBlock, commit, uploadClip, labels}) // → {pick(), chose(file), destroy()}. import { clipMessage } from './uploads.mjs' import { registerClipOps } from './inspector-ops.mjs' export function mountClipCard ({ root, getSlideId, getBlock, commit, uploadClip, labels = {} }) { const input = root.querySelector('[slides-clip-input]') const status = root.querySelector('[slides-clip-status]') if (!input) return { pick () {}, chose () {}, destroy () {} } const words = { clip: input.dataset.textRefused, clipBig: input.dataset.textRefusedBig, failed: labels.failed } function say (state, text) { if (!status) return status.dataset.state = state status.textContent = text || '' } async function chose (file) { // The block as it is when the file is chosen: the upload lands on // it even if another is selected by the time it is done. const block = getBlock() const slideId = getSlideId() if (!file || !block || block.type !== 'clip') return say('uploading', labels.uploading) try { await uploadClip(file, { applyOp: (video, poster) => commit(registerClipOps(video, poster, slideId, block.id)) }) } catch (err) { say('error', clipMessage(err, words, file.name || '')) return } finally { input.value = '' } say('done', labels.done) } function onChange (e) { if (e.target !== input) return const file = input.files && input.files[0] if (file) chose(file) } root.addEventListener('change', onChange) return { pick () { say('', '') if (!input.disabled) input.click() }, chose, destroy () { root.removeEventListener('change', onChange) } } }