// THE REEL STRIP'S DRAG AND ZOOM, as arithmetic (Play the cut, ReelStrip.tsx): // where a dragged cell would land and in which section, which section labels // fit at the zoom shown, how fast the strip pans at its edge while dragging, // and when a zoomed strip follows the playhead. Pure: no DOM, no React, so it // is unit-tested (stripDrag.test.mjs); the zoom itself is the bench's // (benchZoom.mjs), on cut seconds instead of source seconds. // // Sections are the deck's markers (sectionEdit.mjs sectionModel), passed in // as `sectionOf` (clip id -> section index) and `sections` ({index, label}); // nothing here knows how many there may be. /** * Each item's section: a clip's own, a card's that of the clip before it * (else the one after), as sectionModel's entrySection stands a card. * * @param {Array<{ id: string, clip?: unknown }>} items * @param {Record | Map} sectionOf * @returns {number[]} */ export function itemSections(items, sectionOf) { const get = (id) => (sectionOf instanceof Map ? sectionOf.get(id) : sectionOf?.[id]); const out = items.map((it) => (it.clip ? get(it.id) : undefined)); let last; for (let i = 0; i < out.length; i += 1) { if (out[i] !== undefined) last = out[i]; else if (last !== undefined) out[i] = last; } let next; for (let i = out.length - 1; i >= 0; i -= 1) { if (items[i].clip && get(items[i].id) !== undefined) next = get(items[i].id); else if (out[i] === undefined) out[i] = next ?? 0; } return out.map((s) => s ?? 0); } /** "§3 Leaves it open" -- a section in the drop's words; a subsection "§4 Leaves it open › Why". */ export const sectionName = (sections, k) => { const s = (sections ?? []).find((x) => x.index === k); const words = s?.level === 2 ? [s.within, s.label].filter(Boolean).join(" › ") : s?.label; return `§${k}${words ? ` ${words}` : k === 0 ? " (the opening)" : ""}`; }; /** * Where a drag of `dragged` (ids) would land with the pointer at cut second * `t`: next to the cell under the pointer -- its left half before it, its * right half after it -- in that cell's section. So at a seam the two halves * either side are the end of §k and the start of §k+1: the same slot, the * section said. Null over the dragged cells themselves (nothing would move). * * `before` is the id the dragged entries land before (null: the end), what * the place op takes; `at` the cut second of the insertion line; `refused` * the words when the drop would leave a section with no clip. * * @param {Array<{ id: string, from: number, to: number, clip?: unknown }>} items * @param {number[]} sectionsOfItems itemSections(items, …) * @param {Array<{ index: number, label: string | null }>} sections * @param {string[]} dragged * @param {number} t */ export function dropSlot(items, sectionsOfItems, sections, dragged, t) { if (!items.length) return null; const moving = new Set(dragged); let j = items.findIndex((it) => t >= it.from && t < it.to); if (j < 0) j = t < items[0].from ? 0 : items.length - 1; if (moving.has(items[j].id)) return null; const it = items[j]; const side = t < (it.from + it.to) / 2 ? "before" : "after"; const section = sectionsOfItems[j]; // The entry the dragged ones land before: this cell, or the next not being dragged. let k = side === "before" ? j : j + 1; while (k < items.length && moving.has(items[k].id)) k += 1; const before = k < items.length ? items[k].id : null; // Already there: the entry before the slot (skipping the dragged) is the last dragged one's // predecessor, and the section is its own. const draggedIdx = items.map((x, i) => (moving.has(x.id) ? i : -1)).filter((i) => i >= 0); const firstDragged = draggedIdx[0] ?? -1; let p = k - 1; while (p >= 0 && moving.has(items[p].id)) p -= 1; const prevId = p >= 0 ? items[p].id : null; let q = firstDragged - 1; while (q >= 0 && moving.has(items[q].id)) q -= 1; const origPrev = q >= 0 ? items[q].id : null; const sameSection = draggedIdx.every((i) => !items[i].clip || sectionsOfItems[i] === section); const noop = firstDragged >= 0 && prevId === origPrev && sameSection && draggedIdx.every((i, n) => n === 0 || i === draggedIdx[n - 1] + 1); // A section emptied: every clip of some section is being dragged out of it. let refused = null; const left = new Map(); items.forEach((x, i) => { if (!x.clip || moving.has(x.id)) return; left.set(sectionsOfItems[i], (left.get(sectionsOfItems[i]) ?? 0) + 1); }); for (const i of draggedIdx) { const s = sectionsOfItems[i]; if (items[i].clip && s !== section && !left.get(s)) { refused = `${sectionName(sections, s)} would have no clip left — remove its boundary first, or move another clip into it`; break; } } const at = side === "before" ? it.from : it.to; const where = before ? `before ${before}` : "at the end"; return { over: it.id, side, before, section, at, noop, refused, words: noop ? "already here" : `→ ${sectionName(sections, section)}, ${side === "after" ? `after ${it.id}` : where}`, seam: side === "after" ? sectionsOfItems[j + 1] !== undefined && sectionsOfItems[j + 1] !== section : j > 0 && sectionsOfItems[j - 1] !== section, prev: prevId, }; } /** * Which labels fit, left to right: each `{x, w}` (px) is shown when it starts * at least `gap` px after the last shown one ends. Returns the indexes shown. * * @param {Array<{ x: number, w: number }>} labels in x order * @param {number} [gap] */ export function labelsThatFit(labels, gap = 6) { const shown = []; let end = -Infinity; labels.forEach((l, i) => { if (l.x >= end + gap) { shown.push(i); end = l.x + l.w; } }); return shown; } /** * The pan speed while dragging near an edge, in widths of what is shown per * second: 0 away from the edges, growing to `max` at (and past) the edge. * * @param {number} x the pointer, px from the strip's left * @param {number} width the strip's width, px * @param {{ edge?: number, max?: number }} [o] */ export function edgeSpeed(x, width, { edge = 48, max = 1.2 } = {}) { if (!(width > 0)) return 0; if (x < edge) return -max * Math.min(1, (edge - x) / edge); if (x > width - edge) return max * Math.min(1, (x - (width - edge)) / edge); return 0; } /** * A zoomed strip follows the playhead: once `t` leaves what is shown (or * comes within `margin` of its right edge while playing), the view pages on * so `t` sits a fifth of the way in. Null when it need not move. * * @param {{ from: number, to: number }} vis * @param {{ from: number, to: number }} whole * @param {number} t * @param {{ margin?: number }} [o] fraction of the view */ export function followVis(vis, whole, t, { margin = 0.04 } = {}) { const span = vis.to - vis.from; if (!(span > 0) || span >= whole.to - whole.from - 1e-6) return null; if (t >= vis.from && t <= vis.to - span * margin) return null; let from = t - span * 0.2; from = Math.max(whole.from, Math.min(whole.to - span, from)); return { from, to: from + span }; }