-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c6a86d
commit 9da97f7
Showing
7 changed files
with
233 additions
and
97 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
packages/react/src/components/tldex/new-editor/components/RndSubtitle.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import React from "react"; | ||
import { Rnd } from "react-rnd"; | ||
import { useSpecificSubtitle } from "../hooks/subtitles"; | ||
|
||
/** | ||
* Properties for the RndSubtitle component. | ||
* | ||
* @property {string} subtitleId Unique identifier for the subtitle. | ||
* @property {number} startTime Start time of the timeline range for the subtitle. | ||
* @property {number} endTime End time of the timeline range for the subtitle. | ||
* @property {number} containerWidth Width of the container in which the subtitle is displayed. | ||
*/ | ||
interface RndSubtitleProps { | ||
subtitleId: string; | ||
startTime: number; | ||
endTime: number; | ||
containerWidth: number; | ||
} | ||
/** | ||
* RndSubtitle is a resizable and draggable subtitle component for editing video subtitles. | ||
* It utilizes the react-rnd library to allow users to adjust the position | ||
* and duration of a subtitle by dragging or resizing the subtitle box. | ||
* | ||
* Props: | ||
* - subtitleId: Unique identifier for the subtitle. | ||
* - startTime: Start time of the timeline range for the subtitle. | ||
* - endTime: End time of the timeline range for the subtitle. | ||
* - containerWidth: Width of the container in which the subtitle is displayed. | ||
* | ||
* The component provides internal functions to convert time to position and vice versa, | ||
* and handlers to update subtitle start time and duration upon dragging or resizing. | ||
*/ | ||
export const RndSubtitle: React.FC<RndSubtitleProps> = ({ | ||
subtitleId, | ||
startTime, | ||
endTime, | ||
containerWidth, | ||
}) => { | ||
const [subtitle, edit] = useSpecificSubtitle(subtitleId); | ||
|
||
const timeToPosition = (time: number) => { | ||
const timeRange = endTime - startTime; | ||
const position = ((time - startTime) / timeRange) * containerWidth; | ||
return position; | ||
}; | ||
|
||
const positionToTime = (position: number) => { | ||
const timeRange = endTime - startTime; | ||
const time = (position / containerWidth) * timeRange + startTime; | ||
return Math.max(startTime, Math.min(time, endTime)); | ||
}; | ||
|
||
const handleDrag = (_: unknown, d: { x: number; y: number }) => { | ||
const newStartTime = positionToTime(d.x); | ||
edit({ | ||
type: "UPDATE_SUBTITLE", | ||
payload: { | ||
...subtitle, | ||
video_offset: newStartTime, | ||
end: newStartTime + subtitle.duration / 1000, | ||
}, | ||
}); | ||
}; | ||
|
||
const handleResize = (_: unknown, _direction: string, ref: HTMLElement) => { | ||
const newWidth = ref.offsetWidth; | ||
const newDuration = | ||
(newWidth / containerWidth) * (endTime - startTime) * 1000; | ||
edit({ | ||
type: "UPDATE_SUBTITLE", | ||
payload: { | ||
...subtitle, | ||
duration: newDuration, | ||
end: subtitle.video_offset + newDuration / 1000, | ||
}, | ||
}); | ||
}; | ||
|
||
return ( | ||
<Rnd | ||
position={{ | ||
x: timeToPosition(subtitle.video_offset), | ||
y: 15, | ||
}} | ||
size={{ | ||
width: | ||
timeToPosition( | ||
subtitle.video_offset + (subtitle.duration || 3000) / 1000, | ||
) - timeToPosition(subtitle.video_offset), | ||
height: 30, | ||
}} | ||
onDragStop={handleDrag} | ||
onResize={handleResize} | ||
bounds="parent" | ||
className="border border-x-2 border-primary-9" | ||
enableResizing={{ left: true, right: true }} | ||
> | ||
<div | ||
className="h-full w-full cursor-move text-wrap bg-base-3 text-base-12 opacity-80 bg-blend-multiply" | ||
title={subtitle.message} | ||
> | ||
<span className="truncate text-xs text-white">{subtitle.message}</span> | ||
</div> | ||
</Rnd> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.