Skip to content

Commit

Permalink
Fixed keyboard controls and normalized them to use scrub
Browse files Browse the repository at this point in the history
  • Loading branch information
deusprogrammer committed Nov 10, 2023
1 parent 33f0dd8 commit 23390f0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 44 deletions.
2 changes: 2 additions & 0 deletions src/renderer/components/ClipList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export default ({
</td>
<td>
<button
title="i"
onClick={() => {
onClipsChange(
'edit',
Expand Down Expand Up @@ -171,6 +172,7 @@ export default ({
</td>
<td>
<button
title="o"
onClick={() => {
onClipsChange(
'edit',
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/components/SubtitleList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export default ({
</td>
<td>
<button
title="i"
onClick={() => {
onSubsChange(
'edit',
Expand Down Expand Up @@ -240,6 +241,7 @@ export default ({
</td>
<td>
<button
title="o"
onClick={() => {
onSubsChange(
'edit',
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/components/TimeLine.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default ({
onClick={() => {
onSliderPositionChange(
Math.max(
0,
0 + offset,
currentSliderPosition + offset - 1000
)
);
Expand All @@ -105,7 +105,7 @@ export default ({
onClick={() => {
onSliderPositionChange(
Math.max(
0,
0 + offset,
currentSliderPosition + offset - 1000 / 60
)
);
Expand Down
78 changes: 36 additions & 42 deletions src/renderer/routes/editor/AdvancedEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ let AdvancedEditor = () => {
isPlaying,
defaultClipSize,
videoLength,
offset,
actualVideoLength,
};
const keyboardHandler = useCallback((event) => {
if (isActiveElementInput()) {
Expand Down Expand Up @@ -120,58 +122,42 @@ let AdvancedEditor = () => {
break;
}
case 'ArrowLeft': {
setCurrentSliderPosition((currentSliderPosition) =>
Math.max(0, currentSliderPosition - 1000)
);
setCurrentPosition(
scrub(
Math.max(
0,
stateRef.current.currentSliderPosition / 1000 - 1
0 + stateRef.current.offset,
stateRef.current.currentSliderPosition - 1000
)
);

break;
}
case 'ArrowRight': {
setCurrentSliderPosition((currentSliderPosition) =>
scrub(
Math.min(
stateRef.current.videoLength * 1000,
currentSliderPosition + 1000
)
);
setCurrentPosition(
Math.min(
stateRef.current.videoLength,
stateRef.current.currentSliderPosition / 1000 + 1
stateRef.current.currentSliderPosition + 1000,
stateRef.current.videoLength * 1000 +
stateRef.current.offset
)
);

break;
}
case ';': {
setCurrentSliderPosition((currentSliderPosition) =>
Math.max(0, currentSliderPosition - 1000 / 60)
);
setCurrentPosition(
scrub(
Math.max(
0,
stateRef.current.currentSliderPosition / 1000 - 1 / 60
0 + stateRef.current.offset,
stateRef.current.currentSliderPosition - 1000 / 60
)
);

break;
}
case "'": {
setCurrentSliderPosition((currentSliderPosition) =>
scrub(
Math.min(
stateRef.current.videoLength * 1000,
currentSliderPosition + 1000 / 60
)
);
setCurrentPosition(
Math.min(
stateRef.current.videoLength,
stateRef.current.currentSliderPosition / 1000 + 1 / 60
stateRef.current.currentSliderPosition + 1000 / 60,
stateRef.current.videoLength * 1000 +
stateRef.current.offset
)
);

Expand All @@ -184,7 +170,9 @@ let AdvancedEditor = () => {
'edit',
{
...currentSubObject,
startTime: stateRef.current.currentSliderPosition,
startTime:
stateRef.current.currentSliderPosition -
stateRef.current.offset,
},
stateRef.current.currentSub
);
Expand All @@ -197,7 +185,9 @@ let AdvancedEditor = () => {
'edit',
{
...currentSubObject,
endTime: stateRef.current.currentSliderPosition,
endTime:
stateRef.current.currentSliderPosition -
stateRef.current.offset,
},
stateRef.current.currentSub
);
Expand All @@ -206,15 +196,13 @@ let AdvancedEditor = () => {
case '[': {
let currentSubObject =
stateRef.current.subs[stateRef.current.currentSub];
setCurrentSliderPosition(currentSubObject.startTime);
setCurrentPosition(currentSubObject.startTime / 1000);
scrub(currentSubObject.startTime + stateRef.current.offset);
break;
}
case ']': {
let currentSubObject =
stateRef.current.subs[stateRef.current.currentSub];
setCurrentSliderPosition(currentSubObject.endTime);
setCurrentPosition(currentSubObject.endTime / 1000);
scrub(currentSubObject.endTime + stateRef.current.offset);
break;
}
case 'w': {
Expand All @@ -228,9 +216,12 @@ let AdvancedEditor = () => {
case 'n':
subChangeHandler('add', {
rowIndex: stateRef.current.currentRow,
startTime: parseInt(stateRef.current.currentSliderPosition),
startTime:
parseInt(stateRef.current.currentSliderPosition) -
stateRef.current.offset,
endTime:
parseInt(stateRef.current.currentSliderPosition) +
parseInt(stateRef.current.currentSliderPosition) -
stateRef.current.offset +
stateRef.current.defaultClipSize,
text: '',
type: 'subtitle',
Expand All @@ -251,13 +242,14 @@ let AdvancedEditor = () => {
break;
}
event.stopPropagation();
event.preventDefault();
});

const getCurrentIndex = () => {
let index = subs.findIndex((subtitle) => {
return (
currentSliderPosition > subtitle.startTime &&
currentSliderPosition < subtitle.endTime
currentSliderPosition > subtitle.startTime + offset &&
currentSliderPosition < subtitle.endTime + offset
);
});

Expand Down Expand Up @@ -404,10 +396,12 @@ let AdvancedEditor = () => {
let scrub = (milliseconds) => {
if (milliseconds < 0) {
milliseconds = 0;
} else if (milliseconds > actualVideoLength * 1000) {
milliseconds = videoLength * 1000;
} else if (milliseconds > stateRef.current.actualVideoLength * 1000) {
milliseconds = stateRef.current.videoLength * 1000;
}

console.log('SCRUB TO ' + milliseconds);

setCurrentPosition(milliseconds / 1000);
setCurrentSliderPosition(milliseconds);
setIsPlaying(false);
Expand Down

0 comments on commit 23390f0

Please sign in to comment.