| 1 | // clip-card.mjs — a clip block's video, in the editor's card (scenes and |
| 2 | // clips spec §2, §4). |
| 3 | // |
| 4 | // The card's file picker ([slides-clip-input]) takes an MP4 or WebM; |
| 5 | // Insert → Clip adds the block and opens it at once (commands.mjs). The |
| 6 | // upload is uploads.mjs's uploadClip: the first frame drawn on a canvas |
| 7 | // and uploaded as the poster, then the video naming it, and one batch |
| 8 | // (inspector-ops.mjs's registerClipOps) registering both and pointing |
| 9 | // the block at the video, so the clip lands with one undo. The status |
| 10 | // line ([slides-clip-status], role="status") says how it went in the |
| 11 | // catalog's words: the upload's own (uploading, done, failed) and a |
| 12 | // clip's refusals with the file's name (data-text-refused, |
| 13 | // data-text-refused-big on the picker). |
| 14 | // |
| 15 | // mountClipCard({root, getSlideId, getBlock, commit, uploadClip, labels}) |
| 16 | // → {pick(), chose(file), destroy()}. |
| 17 | import { clipMessage } from './uploads.mjs' |
| 18 | import { registerClipOps } from './inspector-ops.mjs' |
| 19 | |
| 20 | export function mountClipCard ({ root, getSlideId, getBlock, commit, uploadClip, labels = {} }) { |
| 21 | const input = root.querySelector('[slides-clip-input]') |
| 22 | const status = root.querySelector('[slides-clip-status]') |
| 23 | if (!input) return { pick () {}, chose () {}, destroy () {} } |
| 24 | const words = { clip: input.dataset.textRefused, clipBig: input.dataset.textRefusedBig, failed: labels.failed } |
| 25 | |
| 26 | function say (state, text) { |
| 27 | if (!status) return |
| 28 | status.dataset.state = state |
| 29 | status.textContent = text || '' |
| 30 | } |
| 31 | |
| 32 | async function chose (file) { |
| 33 | // The block as it is when the file is chosen: the upload lands on |
| 34 | // it even if another is selected by the time it is done. |
| 35 | const block = getBlock() |
| 36 | const slideId = getSlideId() |
| 37 | if (!file || !block || block.type !== 'clip') return |
| 38 | say('uploading', labels.uploading) |
| 39 | try { |
| 40 | await uploadClip(file, { applyOp: (video, poster) => commit(registerClipOps(video, poster, slideId, block.id)) }) |
| 41 | } catch (err) { |
| 42 | say('error', clipMessage(err, words, file.name || '')) |
| 43 | return |
| 44 | } finally { |
| 45 | input.value = '' |
| 46 | } |
| 47 | say('done', labels.done) |
| 48 | } |
| 49 | |
| 50 | function onChange (e) { |
| 51 | if (e.target !== input) return |
| 52 | const file = input.files && input.files[0] |
| 53 | if (file) chose(file) |
| 54 | } |
| 55 | root.addEventListener('change', onChange) |
| 56 | |
| 57 | return { |
| 58 | pick () { |
| 59 | say('', '') |
| 60 | if (!input.disabled) input.click() |
| 61 | }, |
| 62 | chose, |
| 63 | destroy () { root.removeEventListener('change', onChange) } |
| 64 | } |
| 65 | } |
| 66 | |