180 lines
5.7 KiB
Svelte
180 lines
5.7 KiB
Svelte
<script lang="ts">
|
|
import { api } from "./api"
|
|
|
|
let {
|
|
clip,
|
|
trimStart = $bindable(),
|
|
trimEnd = $bindable(),
|
|
trimPreviewPath = $bindable(),
|
|
trimPreviewBust = $bindable(),
|
|
dragging = $bindable(),
|
|
onAfterChange,
|
|
}: {
|
|
clip: { temp_path: string; duration_secs: number; width: number; height: number }
|
|
trimStart: number
|
|
trimEnd: number
|
|
trimPreviewPath: string | null
|
|
trimPreviewBust: number
|
|
dragging: "start" | "end" | null
|
|
onAfterChange: () => void
|
|
} = $props()
|
|
|
|
let trackEl: HTMLDivElement | null = $state(null)
|
|
let lastTrimPreview = 0
|
|
|
|
function pointerToSecs(e: PointerEvent): number {
|
|
if (!trackEl) return 0
|
|
const rect = trackEl.getBoundingClientRect()
|
|
const x = e.clientX - rect.left
|
|
const ratio = Math.max(0, Math.min(1, x / rect.width))
|
|
return ratio * clip.duration_secs
|
|
}
|
|
|
|
async function updateTrimPreview(secs: number) {
|
|
const now = Date.now()
|
|
if (now - lastTrimPreview < 100) return
|
|
lastTrimPreview = now
|
|
try {
|
|
trimPreviewPath = await api.renderRawPreview(secs)
|
|
trimPreviewBust = Date.now()
|
|
} catch {
|
|
lastTrimPreview = 0
|
|
}
|
|
}
|
|
|
|
function startDrag(which: "start" | "end", e: PointerEvent) {
|
|
dragging = which
|
|
const el = e.currentTarget as HTMLElement
|
|
el.setPointerCapture(e.pointerId)
|
|
void updateTrimPreview(which === "start" ? trimStart : trimEnd)
|
|
}
|
|
|
|
function onDrag(e: PointerEvent) {
|
|
if (!dragging) return
|
|
const secs = pointerToSecs(e)
|
|
if (dragging === "start") {
|
|
trimStart = Math.min(secs, trimEnd - 0.1)
|
|
} else {
|
|
trimEnd = Math.max(secs, trimStart + 0.1)
|
|
}
|
|
void updateTrimPreview(secs)
|
|
}
|
|
|
|
function endDrag(e: PointerEvent) {
|
|
const el = e.currentTarget as HTMLElement
|
|
el.releasePointerCapture(e.pointerId)
|
|
dragging = null
|
|
trimPreviewPath = null
|
|
onAfterChange()
|
|
}
|
|
|
|
function onTrackClick(e: PointerEvent) {
|
|
if (!trackEl) return
|
|
const secs = pointerToSecs(e)
|
|
const distToStart = Math.abs(secs - trimStart)
|
|
const distToEnd = Math.abs(secs - trimEnd)
|
|
if (distToStart < distToEnd) {
|
|
trimStart = Math.min(secs, trimEnd - 0.1)
|
|
} else {
|
|
trimEnd = Math.max(secs, trimStart + 0.1)
|
|
}
|
|
onAfterChange()
|
|
}
|
|
|
|
let trimmedDuration = $derived(Math.max(0.1, trimEnd - trimStart))
|
|
</script>
|
|
|
|
<div class="trim-block">
|
|
<div class="trim-labels">
|
|
<span class="mono">{trimStart.toFixed(2)}s</span>
|
|
<span class="trim-kept">{trimmedDuration.toFixed(2)}s kept</span>
|
|
<span class="mono">{trimEnd.toFixed(2)}s</span>
|
|
</div>
|
|
<div
|
|
class="trim-track"
|
|
bind:this={trackEl}
|
|
onpointerdown={onTrackClick}
|
|
role="none"
|
|
>
|
|
<div class="trim-range" style="left:{(trimStart/clip.duration_secs)*100}%; right:{(1-trimEnd/clip.duration_secs)*100}%"></div>
|
|
<div
|
|
class="trim-handle trim-start"
|
|
style="left:{(trimStart/clip.duration_secs)*100}%"
|
|
role="slider"
|
|
aria-label="trim start"
|
|
aria-valuemin={0}
|
|
aria-valuemax={trimEnd - 0.1}
|
|
aria-valuenow={trimStart}
|
|
tabindex="0"
|
|
onpointerdown={(e) => startDrag("start", e)}
|
|
onpointermove={onDrag}
|
|
onpointerup={endDrag}
|
|
onkeydown={(e) => {
|
|
const step = e.shiftKey ? 0.5 : 0.05
|
|
if (e.key === "ArrowLeft") { trimStart = Math.max(0, trimStart - step); e.preventDefault() }
|
|
else if (e.key === "ArrowRight") { trimStart = Math.min(trimEnd - 0.1, trimStart + step); e.preventDefault() }
|
|
}}
|
|
></div>
|
|
<div
|
|
class="trim-handle trim-end"
|
|
style="left:{(trimEnd/clip.duration_secs)*100}%"
|
|
role="slider"
|
|
aria-label="trim end"
|
|
aria-valuemin={trimStart + 0.1}
|
|
aria-valuemax={clip.duration_secs}
|
|
aria-valuenow={trimEnd}
|
|
tabindex="0"
|
|
onpointerdown={(e) => startDrag("end", e)}
|
|
onpointermove={onDrag}
|
|
onpointerup={endDrag}
|
|
onkeydown={(e) => {
|
|
const step = e.shiftKey ? 0.5 : 0.05
|
|
if (e.key === "ArrowLeft") { trimEnd = Math.max(trimStart + 0.1, trimEnd - step); e.preventDefault() }
|
|
else if (e.key === "ArrowRight") { trimEnd = Math.min(clip.duration_secs, trimEnd + step); e.preventDefault() }
|
|
}}
|
|
></div>
|
|
</div>
|
|
<input type="range" min="0" max={clip.duration_secs} step="0.05" value={trimStart}
|
|
oninput={(e) => { const v = parseFloat((e.currentTarget as HTMLInputElement).value); trimStart = Math.min(v, trimEnd - 0.1) }}
|
|
class="sr-only" aria-label="trim start seconds" />
|
|
<input type="range" min="0" max={clip.duration_secs} step="0.05" value={trimEnd}
|
|
oninput={(e) => { const v = parseFloat((e.currentTarget as HTMLInputElement).value); trimEnd = Math.max(v, trimStart + 0.1) }}
|
|
class="sr-only" aria-label="trim end seconds" />
|
|
</div>
|
|
|
|
<style>
|
|
.trim-block { display: flex; flex-direction: column; gap: 8px; }
|
|
.trim-track {
|
|
position: relative;
|
|
height: 4px;
|
|
background: var(--bg-3);
|
|
border-radius: 2px;
|
|
cursor: pointer;
|
|
}
|
|
.trim-range {
|
|
position: absolute; top: 0; bottom: 0;
|
|
background: var(--accent);
|
|
border-radius: 2px;
|
|
pointer-events: none;
|
|
}
|
|
.trim-handle {
|
|
position: absolute; top: 50%; transform: translate(-50%, -50%);
|
|
width: 12px; height: 22px;
|
|
background: var(--accent);
|
|
border-radius: var(--r-sm);
|
|
cursor: ew-resize;
|
|
z-index: 1;
|
|
touch-action: none;
|
|
}
|
|
.trim-handle:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--accent-glow); }
|
|
.trim-labels {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
font-size: 11px;
|
|
color: var(--text-3);
|
|
font-family: var(--mono);
|
|
}
|
|
.trim-kept { color: var(--accent); }
|
|
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
|
|
</style>
|