From 6edd5b36cf7f09b8b146f57a880eb919c5fdad7f Mon Sep 17 00:00:00 2001 From: Maze Winther Date: Sun, 6 Jul 2025 22:51:11 +0200 Subject: feat: main track and ordering tracks --- apps/web/src/types/timeline.ts | 48 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'apps/web/src/types') diff --git a/apps/web/src/types/timeline.ts b/apps/web/src/types/timeline.ts index daada718..7079f091 100644 --- a/apps/web/src/types/timeline.ts +++ b/apps/web/src/types/timeline.ts @@ -1,5 +1,4 @@ import { MediaType } from "@/stores/media-store"; -import { TimelineTrack } from "@/stores/timeline-store"; export type TrackType = "media" | "text" | "audio"; @@ -77,3 +76,50 @@ export interface TextItemDragData { } export type DragData = MediaItemDragData | TextItemDragData; + +export interface TimelineTrack { + id: string; + name: string; + type: TrackType; + elements: TimelineElement[]; + muted?: boolean; + isMain?: boolean; +} + +export function sortTracksByOrder(tracks: TimelineTrack[]): TimelineTrack[] { + return [...tracks].sort((a, b) => { + // Audio tracks always go to bottom + if (a.type === "audio" && b.type !== "audio") return 1; + if (b.type === "audio" && a.type !== "audio") return -1; + + // Main track goes above audio but below other tracks + if (a.isMain && !b.isMain && b.type !== "audio") return 1; + if (b.isMain && !a.isMain && a.type !== "audio") return -1; + + // Within same category, maintain creation order + return 0; + }); +} + +export function getMainTrack(tracks: TimelineTrack[]): TimelineTrack | null { + return tracks.find((track) => track.isMain) || null; +} + +export function ensureMainTrack(tracks: TimelineTrack[]): TimelineTrack[] { + const hasMainTrack = tracks.some((track) => track.isMain); + + if (!hasMainTrack) { + // Create main track if it doesn't exist + const mainTrack: TimelineTrack = { + id: crypto.randomUUID(), + name: "Main Track", + type: "media", + elements: [], + muted: false, + isMain: true, + }; + return [mainTrack, ...tracks]; + } + + return tracks; +} -- cgit v1.3.1