Enable free positioning for timeline clips (#9)
* Add comprehensive GEO search foundation * Enable free positioning for timeline clips --------- Co-authored-by: haixin.yang <haixin.yang@weimob.com>
This commit is contained in:
1 parent
b261b47008
commit
bacf0804b1
17 files changed
+297
-64
No files matched your search
@@ -57,6 +57,7 @@ export function createAudioTrackActions(d) {
|
||||
d.setMusicUrl(nextUrl);
|
||||
d.setMusicName(nextName);
|
||||
d.setMusicDuration(duration || 0);
|
||||
d.setMusicStart(0);
|
||||
d.setMusicPeaks(nextPeaks);
|
||||
d.setSelectedTrack("music");
|
||||
d.setActiveTool("audio");
|
||||
@@ -129,6 +130,7 @@ export function createAudioTrackActions(d) {
|
||||
d.setMusicUrl("");
|
||||
d.setMusicName("");
|
||||
d.setMusicDuration(0);
|
||||
d.setMusicStart(0);
|
||||
d.setMusicPeaks([]);
|
||||
d.setCurrentTime((time) => Math.min(time, Math.max(
|
||||
d.audioBlob ? d.audioDuration : 0,
|
||||
|
||||
+2
-1
@@ -714,6 +714,7 @@ export async function exportBrowserVideo({
|
||||
sourceAudioSegments = [],
|
||||
musicBlob,
|
||||
musicVolume = 0.35,
|
||||
musicStart = 0,
|
||||
text,
|
||||
captionSegments,
|
||||
duration,
|
||||
@@ -839,7 +840,7 @@ export async function exportBrowserVideo({
|
||||
: sourceAudioBlob
|
||||
? [{ blob: sourceAudioBlob, volume: sourceAudioVolume, role: "source", start: Math.max(0, sourceAudioStart || 0) }]
|
||||
: []),
|
||||
musicBlob ? { blob: musicBlob, volume: musicVolume, role: "music", start: 0 } : null,
|
||||
musicBlob ? { blob: musicBlob, volume: musicVolume, role: "music", start: Math.max(0, musicStart || 0) } : null,
|
||||
].filter(Boolean);
|
||||
|
||||
if (audioInputs.length) {
|
||||
|
||||
@@ -24,7 +24,7 @@ export function createPlaybackControls(deps) {
|
||||
if (audio) audio.currentTime = getTimelineTrackLocalTime(clamped, segment.start, segment.duration);
|
||||
});
|
||||
if (deps.sourceAudioRef.current) deps.sourceAudioRef.current.currentTime = getSourceState(clamped).sourceTime;
|
||||
if (deps.musicRef.current) deps.musicRef.current.currentTime = clamped;
|
||||
if (deps.musicRef.current) deps.musicRef.current.currentTime = getTimelineTrackLocalTime(clamped, deps.musicStart, deps.musicDuration);
|
||||
};
|
||||
const getTimelineTimeFromClientX = (clientX) => {
|
||||
const rect = deps.trackScrollRef.current?.getBoundingClientRect(); const duration = deps.timelineDurationRef.current;
|
||||
@@ -59,7 +59,7 @@ export function createPlaybackControls(deps) {
|
||||
source.playbackRate = sourceState.playbackRate;
|
||||
playIf(source, sourceState.active);
|
||||
}
|
||||
if (music && deps.musicUrl) { music.currentTime = Math.max(0, Math.min(deps.musicDuration || timelineTime, timelineTime)); playIf(music, timelineTime <= (deps.musicDuration || timelineTime)); }
|
||||
if (music && deps.musicUrl) { const active = isTimelineTimeInsideTrack(timelineTime, deps.musicStart, deps.musicDuration); music.currentTime = getTimelineTrackLocalTime(timelineTime, deps.musicStart, deps.musicDuration); playIf(music, active); }
|
||||
if (video && deps.previewVisualType === "video") {
|
||||
const index = getVisualSegmentIndexAtTime(deps.visualSegments, timelineTime);
|
||||
const range = deps.visualTimeline[Math.max(0, index)] ?? deps.currentVisualRange;
|
||||
|
||||
@@ -75,6 +75,19 @@ export function getTimedSegmentsEnd(segments) {
|
||||
);
|
||||
}
|
||||
|
||||
export function packTimedSegmentsIntoLanes(segments) {
|
||||
const lanes = [];
|
||||
[...segments].sort((a, b) => (a.start || 0) - (b.start || 0) || String(a.id).localeCompare(String(b.id))).forEach((segment) => {
|
||||
const laneIndex = lanes.findIndex((lane) => {
|
||||
const last = lane.at(-1);
|
||||
return !last || (last.start || 0) + (last.duration || 0) <= (segment.start || 0) + 0.001;
|
||||
});
|
||||
if (laneIndex >= 0) lanes[laneIndex].push(segment);
|
||||
else lanes.push([segment]);
|
||||
});
|
||||
return lanes.length ? lanes : [[]];
|
||||
}
|
||||
|
||||
export function getTimedSegmentIndexAtTime(segments, time) {
|
||||
const safeTime = Math.max(0, Number.isFinite(time) ? time : 0);
|
||||
for (let index = segments.length - 1; index >= 0; index -= 1) {
|
||||
@@ -110,6 +123,23 @@ export function reorderTimelineItems(items, fromIndex, toIndex) {
|
||||
return nextItems;
|
||||
}
|
||||
|
||||
export function moveTimedCaptionSegment(segments, segmentId, start, end) {
|
||||
const safeStart = Math.max(0, Number(start) || 0);
|
||||
const safeEnd = Math.max(safeStart + MIN_TIMED_CAPTION_SECONDS, Number(end) || safeStart);
|
||||
return segments.map((segment) =>
|
||||
segment.id === segmentId ? { ...segment, start: safeStart, end: safeEnd } : segment,
|
||||
);
|
||||
}
|
||||
|
||||
export function materializeCaptionTimings(segments, targetDuration) {
|
||||
const timeline = getCaptionTimeline(segments, targetDuration);
|
||||
return segments.map((segment, index) => ({
|
||||
...segment,
|
||||
start: timeline[index]?.start ?? 0,
|
||||
end: timeline[index]?.end ?? Math.max(MIN_TIMED_CAPTION_SECONDS, timeline[index]?.start ?? 0),
|
||||
}));
|
||||
}
|
||||
|
||||
export function getVisualSegmentTimeline(segments) {
|
||||
let cursor = 0;
|
||||
return segments.map((segment) => {
|
||||
|
||||
@@ -9,6 +9,9 @@ import {
|
||||
getTimedSegmentsEnd,
|
||||
getVisualSegmentIndexAtTime,
|
||||
getVisualSegmentTimeline,
|
||||
moveTimedCaptionSegment,
|
||||
materializeCaptionTimings,
|
||||
packTimedSegmentsIntoLanes,
|
||||
reorderTimelineItems,
|
||||
} from "./timeline.js";
|
||||
import {
|
||||
@@ -81,6 +84,40 @@ describe("timeline primitives", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("moves one timed caption without changing its duration or neighboring captions", () => {
|
||||
const source = [
|
||||
{ id: "a", text: "first", start: 1, end: 2.5 },
|
||||
{ id: "b", text: "second", start: 4, end: 5 },
|
||||
];
|
||||
const moved = moveTimedCaptionSegment(source, "a", 6, 7.5);
|
||||
expect(moved[0]).toMatchObject({ id: "a", start: 6, end: 7.5 });
|
||||
expect(moved[0].end - moved[0].start).toBe(1.5);
|
||||
expect(moved[1]).toEqual(source[1]);
|
||||
expect(source[0]).toMatchObject({ start: 1, end: 2.5 });
|
||||
});
|
||||
|
||||
it("materializes contiguous untimed captions before free-position dragging", () => {
|
||||
const source = createCaptionSegments("第一条。第二条。");
|
||||
const materialized = materializeCaptionTimings(source, 8);
|
||||
expect(materialized).toHaveLength(2);
|
||||
expect(materialized[0].start).toBe(0);
|
||||
expect(materialized[0].end).toBeCloseTo(materialized[1].start, 8);
|
||||
const moved = moveTimedCaptionSegment(materialized, materialized[1].id, 6, 8);
|
||||
expect(moved[1]).toMatchObject({ start: 6, end: 8 });
|
||||
expect(moved[1].start - moved[0].end).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("packs overlapping free-position clips into additional lanes", () => {
|
||||
const lanes = packTimedSegmentsIntoLanes([
|
||||
{ id: "a", start: 0, duration: 4 },
|
||||
{ id: "b", start: 2, duration: 3 },
|
||||
{ id: "c", start: 5, duration: 1 },
|
||||
]);
|
||||
expect(lanes).toHaveLength(2);
|
||||
expect(lanes[0].map((item) => item.id)).toEqual(["a", "c"]);
|
||||
expect(lanes[1].map((item) => item.id)).toEqual(["b"]);
|
||||
});
|
||||
|
||||
it("clamps sticker start and duration to timeline limits", () => {
|
||||
const sticker = createStickerSegment({ id: "spark", src: "/spark.png" }, -5, 0.1);
|
||||
expect(sticker.start).toBe(0);
|
||||
|
||||
@@ -1,6 +1,28 @@
|
||||
import { DEFAULT_STICKER_SEGMENT_SECONDS, MAX_TIMELINE_DURATION_SECONDS, MIN_VISUAL_SEGMENT_SECONDS } from "../config/editor.js";
|
||||
|
||||
export function createTimelineMoveControls(d) {
|
||||
const startSingleTrackMove = (event, track) => {
|
||||
if (event.button !== 0) return;
|
||||
const isSource = track === "source";
|
||||
const clipDuration = isSource ? d.sourceAudioDuration : d.musicDuration;
|
||||
const start = isSource ? d.sourceAudioStart : d.musicStart;
|
||||
if (!(clipDuration > 0) || d.trackLocks[track]) return void d.notify(`${isSource ? "视频原声" : "音乐"}轨已锁定,无法移动`);
|
||||
const rect = d.trackScrollRef.current?.getBoundingClientRect(); const duration = d.timelineDurationRef.current || 10;
|
||||
if (!rect) return;
|
||||
event.preventDefault(); event.stopPropagation(); d.setSelectedTrack(track); d.setActiveTool("audio");
|
||||
const startX = event.clientX; let moved = false; let latest = start;
|
||||
const move = (e) => {
|
||||
if (!moved && Math.abs(e.clientX - startX) < 4) return;
|
||||
moved = true; e.preventDefault();
|
||||
latest = Math.max(0, Math.min(MAX_TIMELINE_DURATION_SECONDS - clipDuration, start + ((e.clientX - startX) / Math.max(rect.width, 1)) * duration));
|
||||
if (isSource) { d.setSourceAudioLinked(false); d.setSourceAudioStart(latest); }
|
||||
else d.setMusicStart(latest);
|
||||
d.setTimelineHorizon((value) => Math.max(value, Math.ceil((latest + clipDuration + 5) / 10) * 10));
|
||||
};
|
||||
const cleanup = () => { removeEventListener("pointermove", move); removeEventListener("pointerup", up); removeEventListener("pointercancel", cleanup); };
|
||||
const up = () => { cleanup(); if (moved) { d.seekTo(latest); d.notify(`${isSource ? "视频原声" : "音乐"}片段位置已调整`); } };
|
||||
addEventListener("pointermove", move, { passive: false }); addEventListener("pointerup", up); addEventListener("pointercancel", cleanup);
|
||||
};
|
||||
const startAudioSegmentMove = (event, id = "") => {
|
||||
if (event.button !== 0) return;
|
||||
const segment = d.audioSegments.find((item) => item.id === id); if (!segment) return;
|
||||
@@ -54,5 +76,10 @@ export function createTimelineMoveControls(d) {
|
||||
};
|
||||
addEventListener("pointermove", move, { passive: false }); addEventListener("pointerup", up); addEventListener("pointercancel", cleanup);
|
||||
};
|
||||
return { startAudioSegmentMove, startStickerSegmentMove };
|
||||
return {
|
||||
startAudioSegmentMove,
|
||||
startMusicMove: (event) => startSingleTrackMove(event, "music"),
|
||||
startSourceAudioMove: (event) => startSingleTrackMove(event, "source"),
|
||||
startStickerSegmentMove,
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getSegmentStartTime, getVisualSegmentTimeline, reorderTimelineItems } from "./timeline.js";
|
||||
import { getVisualSegmentTimeline, materializeCaptionTimings, moveTimedCaptionSegment, reorderTimelineItems } from "./timeline.js";
|
||||
|
||||
export function createTimelineReorderControls(d) {
|
||||
const getTimelineReorderIndex = (track, x, y) => {
|
||||
@@ -21,17 +21,44 @@ export function createTimelineReorderControls(d) {
|
||||
const next = reorderTimelineItems(source, fromIndex, toIndex);
|
||||
d.commitVisualSegments(next, "已调整视觉片段顺序", toIndex); d.seekTo(getVisualSegmentTimeline(next)[toIndex]?.start ?? 0); return;
|
||||
}
|
||||
if (track === "caption" && d.captionSegments.length >= 2) {
|
||||
const next = reorderTimelineItems(d.captionSegments, fromIndex, toIndex);
|
||||
d.commitCaptionSegments(next, "已调整字幕片段顺序", toIndex);
|
||||
d.seekTo(getSegmentStartTime(next, toIndex, d.captionTargetDuration));
|
||||
}
|
||||
};
|
||||
const startTimelineClipDrag = (event, track, segmentId, index) => {
|
||||
if (event.button !== 0 || event.target.closest(".image-resize-handle")) return;
|
||||
if (d.trackLocks[track]) return void d.notify(track === "image" ? "图片轨已锁定,无法拖动片段" : "字幕轨已锁定,无法拖动片段");
|
||||
if (track === "image") { d.setSelectedTrack("image"); d.setSelectedVisualSegmentId(segmentId); }
|
||||
else { d.setSelectedTrack("caption"); d.setSelectedSegmentId(segmentId); }
|
||||
const materializedCaptions = track === "caption" ? materializeCaptionTimings(d.captionSegments, d.captionTargetDuration) : [];
|
||||
const caption = materializedCaptions[index];
|
||||
if (caption) {
|
||||
event.preventDefault(); event.stopPropagation();
|
||||
const duration = Math.max(0.2, caption.end - caption.start);
|
||||
const initial = { track, mode: "move", segmentId, fromIndex: index, startX: event.clientX, startY: event.clientY,
|
||||
originalStart: caption.start, originalEnd: caption.end, previewStart: caption.start, previewEnd: caption.end,
|
||||
previewSegments: materializedCaptions, dragging: false };
|
||||
d.timelineClipDragRef.current = initial; d.setTimelineClipDrag(initial);
|
||||
const move = (e) => {
|
||||
const state = d.timelineClipDragRef.current; if (!state || state.segmentId !== segmentId) return;
|
||||
if (!state.dragging && Math.hypot(e.clientX - state.startX, e.clientY - state.startY) < 4) return;
|
||||
const trackElement = document.querySelector('[data-timeline-reorder-track="caption"]');
|
||||
const width = Math.max(1, trackElement?.getBoundingClientRect().width || 1);
|
||||
const delta = ((e.clientX - state.startX) / width) * d.timelineDuration;
|
||||
const previewStart = Math.max(0, Math.min(d.timelineDuration - duration, state.originalStart + delta));
|
||||
const previewEnd = previewStart + duration;
|
||||
const previewSegments = moveTimedCaptionSegment(materializedCaptions, segmentId, previewStart, previewEnd);
|
||||
const next = { ...state, previewStart, previewEnd, previewSegments, dragging: true };
|
||||
d.timelineClipDragRef.current = next; d.setTimelineClipDrag(next);
|
||||
};
|
||||
const up = () => {
|
||||
removeEventListener("pointermove", move); removeEventListener("pointerup", up);
|
||||
const state = d.timelineClipDragRef.current; d.timelineClipDragRef.current = null; d.setTimelineClipDrag(null);
|
||||
if (!state?.dragging) return;
|
||||
d.suppressTimelineClipClickRef.current = segmentId;
|
||||
setTimeout(() => { if (d.suppressTimelineClipClickRef.current === segmentId) d.suppressTimelineClipClickRef.current = ""; }, 120);
|
||||
d.commitCaptionSegments(state.previewSegments, "已移动字幕片段", index); d.seekTo(state.previewStart);
|
||||
};
|
||||
addEventListener("pointermove", move); addEventListener("pointerup", up, { once: true });
|
||||
return;
|
||||
}
|
||||
const count = track === "image" ? d.renderedVisualSegments.length : d.captionSegments.length;
|
||||
if (count < 2) return;
|
||||
event.preventDefault(); event.stopPropagation();
|
||||
|
||||
@@ -38,11 +38,13 @@ export function createTimelineViewModel(d) {
|
||||
: renderedVisualSegments;
|
||||
const renderedVisualTimeline = getVisualSegmentTimeline(displayedVisualSegments);
|
||||
const displayedCaptionSegments = activeTimelineClipDrag?.track === "caption"
|
||||
? reorderTimelineItems(
|
||||
d.captionSegments,
|
||||
activeTimelineClipDrag.fromIndex,
|
||||
activeTimelineClipDrag.overIndex,
|
||||
)
|
||||
? activeTimelineClipDrag.mode === "move"
|
||||
? activeTimelineClipDrag.previewSegments
|
||||
: reorderTimelineItems(
|
||||
d.captionSegments,
|
||||
activeTimelineClipDrag.fromIndex,
|
||||
activeTimelineClipDrag.overIndex,
|
||||
)
|
||||
: d.captionSegments;
|
||||
const displayedCaptionTimeline = activeTimelineClipDrag?.track === "caption"
|
||||
? getCaptionTimeline(displayedCaptionSegments, d.captionTargetDuration)
|
||||
@@ -65,6 +67,9 @@ export function createTimelineViewModel(d) {
|
||||
const musicClipPercent = d.musicBlob && d.timelineDuration > 0
|
||||
? Math.max(0.01, Math.min(100, (d.musicDuration / d.timelineDuration) * 100))
|
||||
: 0;
|
||||
const musicStartPercent = d.musicBlob && d.timelineDuration > 0
|
||||
? Math.max(0, Math.min(100, (d.musicStart / d.timelineDuration) * 100))
|
||||
: 0;
|
||||
const exportPercent = Math.max(0, Math.min(100, Math.round(d.exportProgress)));
|
||||
const previewFrameStyle = d.previewFrameSize.width > 0 && d.previewFrameSize.height > 0
|
||||
? {
|
||||
@@ -77,7 +82,7 @@ export function createTimelineViewModel(d) {
|
||||
return {
|
||||
activeTimelineClipDrag, audioClipPercent, displayedCaptionSegments,
|
||||
displayedCaptionTimeline, displayedVisualSegments, exportPercent, musicClipPercent,
|
||||
playheadPercent, previewFrameStyle, previewRatio, progressPercent,
|
||||
musicStartPercent, playheadPercent, previewFrameStyle, previewRatio, progressPercent,
|
||||
renderedVisualSegments, renderedVisualTimeline, showStickerTrack,
|
||||
sourceAudioClipPercent, sourceAudioStartPercent,
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user