Skip to content

Commit

Permalink
Enter confirms subtitle track selection
Browse files Browse the repository at this point in the history
  • Loading branch information
pooky-programs committed Jun 23, 2024
1 parent 49b1cbb commit 35717d1
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions extension/src/ui/components/VideoDataSyncDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import makeStyles from '@material-ui/styles/makeStyles';
import Switch from '@material-ui/core/Switch';
import LabelWithHoverEffect from '@project/common/components/LabelWithHoverEffect';
import { ConfirmedVideoDataSubtitleTrack, VideoDataSubtitleTrack } from '@project/common';
import React, { useEffect, useState } from 'react';
import React, { useRef, useEffect, useState, FormEvent } from 'react';
import { useTranslation } from 'react-i18next';

const createClasses = makeStyles((theme) => ({
Expand Down Expand Up @@ -82,6 +82,8 @@ export default function VideoDataSyncDialog({
const [shouldRememberTrackChoices, setShouldRememberTrackChoices] = React.useState(false);
const trimmedName = name.trim();
const classes = createClasses();
const [isDropdownActive, setIsDropdownActive] = useState(false);
const okButtonRef = useRef<HTMLButtonElement>(null);

useEffect(() => {
if (open) {
Expand Down Expand Up @@ -130,7 +132,23 @@ export default function VideoDataSyncDialog({
});
}, [suggestedName, selectedSubtitles, subtitles]);

function handleOkButtonClick() {
useEffect(() => {
function handleKeyDown(event: KeyboardEvent) {
if (!isDropdownActive && okButtonRef.current && (event.key === 'Enter' || event.key === 'NumpadEnter')) {
event.preventDefault();
event.stopImmediatePropagation();
okButtonRef.current.click();
}
}
if (open) {
document.addEventListener('keydown', handleKeyDown);
return () => {
document.removeEventListener('keydown', handleKeyDown);
};
}
}, [open, isDropdownActive]);

function handleSubmit() {
const selectedSubtitleTracks: ConfirmedVideoDataSubtitleTrack[] = allSelectedSubtitleTracks();
onConfirm(selectedSubtitleTracks, shouldRememberTrackChoices);
}
Expand Down Expand Up @@ -186,6 +204,8 @@ export default function VideoDataSyncDialog({
return newSelectedSubtitles;
})
}
onFocus={() => setIsDropdownActive(true)}
onBlur={() => setIsDropdownActive(false)}
>
{subtitles.map((subtitle) => (
<MenuItem value={subtitle.url} key={subtitle.url}>
Expand Down Expand Up @@ -264,7 +284,7 @@ export default function VideoDataSyncDialog({
<Button disabled={disabled} onClick={() => onOpenFile()}>
{t('action.openFiles')}
</Button>
<Button disabled={!trimmedName || disabled} onClick={handleOkButtonClick}>
<Button disabled={!trimmedName || disabled} ref={okButtonRef} onClick={handleSubmit}>
{t('action.ok')}
</Button>
</DialogActions>
Expand Down

0 comments on commit 35717d1

Please sign in to comment.