Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cue at current time for subtitles #1367

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/globalKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,9 @@ export const KEYMAP: IKeyMap = {
name: "subtitleList.deleteSegment",
key: "Shift+Alt+d",
},
addCue: {
name: "subtitleList.addCue",
key: "Shift+Alt+e",
},
},
};
3 changes: 2 additions & 1 deletion src/i18n/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@
"addSegmentBelow": "Add segment below",
"jumpToSegmentAbove": "Jump to segment above",
"jumpToSegmentBelow": "Jump to segment below",
"deleteSegment": "Delete segment"
"deleteSegment": "Delete segment",
"addCue": "Add segment at current time"
},

"subtitleVideoArea": {
Expand Down
27 changes: 27 additions & 0 deletions src/main/SubtitleTimeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect, useRef, useState } from "react";
import { css } from "@emotion/react";
import { SegmentsList as CuttingSegmentsList, Waveforms } from "./Timeline";
import {
addCueAtIndex,
selectCurrentlyAt,
selectSelectedSubtitleById,
selectSelectedSubtitleId,
Expand All @@ -25,6 +26,7 @@ import { ThemedTooltip } from "./Tooltip";
import { useTranslation } from "react-i18next";
import { useHotkeys } from "react-hotkeys-hook";
import { KEYMAP } from "../globalKeys";
import { shallowEqual } from "react-redux";

/**
* Copy-paste of the timeline in Video.tsx, so that we can make some small adjustments,
Expand All @@ -39,6 +41,7 @@ const SubtitleTimeline: React.FC = () => {
const dispatch = useAppDispatch();
const duration = useAppSelector(selectDuration);
const currentlyAt = useAppSelector(selectCurrentlyAt);
const subtitleId = useAppSelector(selectSelectedSubtitleId, shallowEqual);

const { ref, width = 1 } = useResizeObserver<HTMLDivElement>();
const refTop = useRef<HTMLElement>(null);
Expand Down Expand Up @@ -71,6 +74,17 @@ const SubtitleTimeline: React.FC = () => {

const [keyboardJumpDelta, setKeyboardJumpDelta] = useState(1000); // In milliseconds. For keyboard navigation

// Callback for adding subtitle segment by hotkey
const addCue = (time: number) => {
dispatch(addCueAtIndex({
identifier: subtitleId,
cueIndex: -1,
text: "",
startTime: time,
endTime: time + 5000,
}));
};

// Callbacks for keyboard controls
// TODO: Better increases and decreases than ten intervals
// TODO: Additional helpful controls (e.g. jump to start/end of segment/next segment)
Expand All @@ -94,6 +108,11 @@ const SubtitleTimeline: React.FC = () => {
() => setKeyboardJumpDelta(keyboardJumpDelta => Math.max(keyboardJumpDelta / 10, 1)),
{}, [keyboardJumpDelta]
);
useHotkeys(
KEYMAP.subtitleList.addCue.key,
() => addCue(currentlyAt),
{}, [currentlyAt]
);

// Callback for the scroll container
const onEndScroll = (e: ScrollEvent) => {
Expand All @@ -102,6 +121,14 @@ const SubtitleTimeline: React.FC = () => {
const offsetX = refTop.current.scrollLeft;
const scrollLeftMax = (refTop.current.scrollWidth - refTop.current.clientWidth);
dispatch(setCurrentlyAt((offsetX / scrollLeftMax) * (duration)));

// Blur active element after scrolling, to ensure hotkeys are working
// This is a little hack to work around focus getting stuck in textarea elements from the subtitle list
try {
(document.activeElement as HTMLElement).blur();
} catch (_e) {
console.error("Tried to blur active element, but active element cannot be blurred.");
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/redux/subtitleSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export const subtitleSlice = createSlice({
state.subtitles[action.payload.identifier].cues.splice(0, 0, cue);
}

if (action.payload.cueIndex >= 0 ||
if (action.payload.cueIndex >= 0 &&
action.payload.cueIndex < state.subtitles[action.payload.identifier].cues.length) {
state.subtitles[action.payload.identifier].cues.splice(action.payload.cueIndex, 0, cue);
}
Expand Down
Loading