From c705ce86d394199cb20765fa2a865b4ac524801b Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Wed, 28 Jun 2023 17:04:07 -0400 Subject: [PATCH] Fix time input for casting (#94) --- CHANGELOG.md | 6 +++ playlet-web/src/lib/VideoStartAt.svelte | 51 +++++++++++++++++-------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0a61706..2b59f5eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +- Video duration formatting in cast dialog + ## [0.10.1] - 2023-06-28 ### Added diff --git a/playlet-web/src/lib/VideoStartAt.svelte b/playlet-web/src/lib/VideoStartAt.svelte index 4c3ef210..77ef62a1 100644 --- a/playlet-web/src/lib/VideoStartAt.svelte +++ b/playlet-web/src/lib/VideoStartAt.svelte @@ -9,7 +9,7 @@ $: { if (document.activeElement !== textInputElemet) { - timestampText = getTimeStampText(timestamp); + timestampText = getFormattedTime(timestamp); } } @@ -19,27 +19,38 @@ if (timestampText === "") { return 0; } - const match = /^(\d{1,3}):(\d{1,2})$/.exec(timestampText); + + const match = /^((\d+):)?(\d{1,2}):(\d{1,2})$/.exec(timestampText); if (match) { - const minutes = parseInt(match[1]); - const seconds = parseInt(match[2]); - if (seconds < 60) { - return minutes * 60 + seconds; + const hours = match[2] ? parseInt(match[2]) : 0; + const minutes = parseInt(match[3]); + const seconds = parseInt(match[4]); + + if (minutes < 60 && seconds < 60) { + return hours * 3600 + minutes * 60 + seconds; } } } - function getTimeStampText(timestamp) { - if (timestamp === undefined) { - return ""; + function getFormattedTime(length) { + if (length === undefined) { + return undefined; } - const minutes = Math.floor(timestamp / 60); - const remainingSeconds = timestamp % 60; + const hours = Math.floor(length / 3600); + const minutes = Math.floor((length / 60) % 60); + const seconds = length % 60; - const formattedMinutes = String(minutes); - const formattedSeconds = String(remainingSeconds).padStart(2, "0"); + const secondsString = seconds < 10 ? `0${seconds}` : seconds.toString(); + const minutesString = + minutes < 10 && hours > 0 ? `0${minutes}` : minutes.toString(); - return `${formattedMinutes}:${formattedSeconds}`; + let formattedTime = minutesString + ":" + secondsString; + + if (hours > 0) { + formattedTime = hours.toString() + ":" + formattedTime; + } + + return formattedTime; } @@ -52,11 +63,19 @@ { - timestamp = getTimeStamp(e.currentTarget.value); + let newTimestamp = getTimeStamp(e.currentTarget.value); + if (newTimestamp !== undefined && lengthSeconds) { + newTimestamp = Math.min(newTimestamp, lengthSeconds); + } + timestamp = newTimestamp; + }} + on:change={(e) => { + timestampText = getFormattedTime(timestamp) ?? "0:00"; + timestamp = getTimeStamp(timestampText); }} /> {#if checked}