diff options
| author | Zaka <149817565+ZakaHaceCosas@users.noreply.github.com> | 2025-08-15 23:01:18 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-15 23:01:18 +0200 |
| commit | 7e4cd767622e3e1a09e76770ce800f4a541cf0e8 (patch) | |
| tree | a7cc92bc6d1fe1686da096cb67e308cedc731f5a /apps/web/src/lib | |
| parent | 033a9aa1bcc06ee82d611ac2c7deb5575c2fa803 (diff) | |
| download | opencut-7e4cd767622e3e1a09e76770ce800f4a541cf0e8.tar.gz | |
feat: add an input so the user can go to a specific timestamp (#530)
* feat: add an input so the user can go to a specific timestamp
* fix: use TimeCode type for EditableTimecodeProps
* cleanup
---------
Co-authored-by: Maze Winther <mazewinther@gmail.com>
Diffstat (limited to 'apps/web/src/lib')
| -rw-r--r-- | apps/web/src/lib/time.ts | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/apps/web/src/lib/time.ts b/apps/web/src/lib/time.ts index f24c5961..20e2ff6d 100644 --- a/apps/web/src/lib/time.ts +++ b/apps/web/src/lib/time.ts @@ -1,10 +1,14 @@ // Time-related utility functions +import { DEFAULT_FPS } from "@/stores/project-store"; + +export type TimeCode = "MM:SS" | "HH:MM:SS" | "HH:MM:SS:CS" | "HH:MM:SS:FF"; + // Helper function to format time in various formats (MM:SS, HH:MM:SS, HH:MM:SS:CS, HH:MM:SS:FF) export const formatTimeCode = ( timeInSeconds: number, - format: "MM:SS" | "HH:MM:SS" | "HH:MM:SS:CS" | "HH:MM:SS:FF" = "HH:MM:SS:CS", - fps = 30 + format: TimeCode = "HH:MM:SS:CS", + fps = DEFAULT_FPS ): string => { const hours = Math.floor(timeInSeconds / 3600); const minutes = Math.floor((timeInSeconds % 3600) / 60); @@ -26,8 +30,8 @@ export const formatTimeCode = ( export const parseTimeCode = ( timeCode: string, - format: "MM:SS" | "HH:MM:SS" | "HH:MM:SS:CS" | "HH:MM:SS:FF" = "HH:MM:SS:CS", - fps = 30 + format: TimeCode = "HH:MM:SS:CS", + fps = DEFAULT_FPS ): number | null => { if (!timeCode || typeof timeCode !== "string") return null; @@ -116,3 +120,18 @@ export const parseTimeCode = ( return null; }; + +export const guessTimeCodeFormat = (timeCode: string): TimeCode | null => { + if (!timeCode || typeof timeCode !== "string") return null; + + const numbers = timeCode.split(":"); + + if (!numbers.every((n) => !isNaN(Number(n)))) return null; + + if (numbers.length === 2) return "MM:SS"; + if (numbers.length === 3) return "HH:MM:SS"; + // todo: how to tell frames apart from cs? + if (numbers.length === 4) return "HH:MM:SS:FF"; + + return null; +}; |
