Skip to content

Commit

Permalink
feat: make it possible to choose time display format in player (#270)
Browse files Browse the repository at this point in the history
* feat: make it possible to choose time display format in player

* chore: remove log
  • Loading branch information
justusmattern27 authored Sep 10, 2024
1 parent a45fa56 commit 7381c9d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
6 changes: 5 additions & 1 deletion packages/player-react/src/controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,22 @@ export function Controls({
setPlaying,
currentTime,
setForcedTime,
timeDisplayFormat,
}: {
duration: number;
playing: boolean;
setPlaying: (playing: boolean) => void;
currentTime: number;
setForcedTime: (currentTime: number) => void;
timeDisplayFormat: 'MM:SS' | 'MM:SS.m' | 'MM:SS.mm';
}) {
return (
<div className="p-text-white p-p-4 p-flex-col p-space-y-2 p-bg-gradient-to-t p-from-gray-500 p-to-transparent">
<div className="p-flex p-space-x-3 p-items-center">
<PlayPause playing={playing} setPlaying={setPlaying} />
<span>{getFormattedTime(currentTime, duration)}</span>
<span>
{getFormattedTime(currentTime, duration, timeDisplayFormat)}
</span>
</div>
<Timeline
currentTime={currentTime}
Expand Down
5 changes: 4 additions & 1 deletion packages/player-react/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface PlayerProps {
width?: number;
height?: number;
quality?: number;
timeDisplayFormat?: 'MM:SS' | 'MM:SS.mm' | 'MM:SS.m';

onDurationChange?: (duration: number) => void;
onTimeUpdate?: (currentTime: number) => void;
Expand All @@ -55,6 +56,7 @@ export function Player({
width = undefined,
height = undefined,
quality = undefined,
timeDisplayFormat = 'MM:SS',

onDurationChange = () => {},
onTimeUpdate = () => {},
Expand All @@ -79,7 +81,7 @@ export function Player({
*/
useEffect(() => {
const diff = Math.abs(currentTime - currentTimeState);
if (diff > 0.05) {
if (diff > 0.03) {
setForcedTime(currentTime);
}
}, [currentTime]);
Expand Down Expand Up @@ -180,6 +182,7 @@ export function Player({
setPlaying={setPlaying}
currentTime={currentTimeState}
setForcedTime={setForcedTime}
timeDisplayFormat={timeDisplayFormat}
/>
</div>
</div>
Expand Down
21 changes: 17 additions & 4 deletions packages/player-react/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
export function getFormattedTime(
timeInSeconds: number,
absoluteTimeInSeconds: number,
timeDisplayFormat: 'MM:SS' | 'MM:SS.mm' | 'MM:SS.m',
) {
function toFormattedTime(timeInSeconds: number) {
const minutes = Math.floor(timeInSeconds / 60);
const seconds = Math.floor(timeInSeconds % 60)
.toString()
.padStart(2, '0');
return `${minutes}:${seconds}`;
const milliseconds = Math.floor((timeInSeconds % 1) * 1000)
.toString()
.padStart(3, '0');

if (timeDisplayFormat === 'MM:SS') {
return `${minutes}:${seconds}`;
}

if (timeDisplayFormat === 'MM:SS.m') {
return `${minutes}:${seconds}.${milliseconds[0]}`;
}

if (timeDisplayFormat === 'MM:SS.mm') {
return `${minutes}:${seconds}.${milliseconds.slice(0, 2)}`;
}
}

return `${toFormattedTime(timeInSeconds)} / ${toFormattedTime(
absoluteTimeInSeconds,
)}`;
return `${toFormattedTime(timeInSeconds)} / ${toFormattedTime(absoluteTimeInSeconds)}`;
}

export function shouldShowControls(
Expand Down

0 comments on commit 7381c9d

Please sign in to comment.