Skip to content

Commit 37d2e8a

Browse files
committed
Test hidden native cue
1 parent 99ecaa8 commit 37d2e8a

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

dotcom-rendering/src/components/LoopVideo.importable.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export const LoopVideo = ({
172172
threshold: VISIBILITY_THRESHOLD,
173173
});
174174

175-
const subtitles = useSubtitles({
175+
const { activeCue, subtitlesAvailable } = useSubtitles({
176176
video: vidRef.current,
177177
playerState,
178178
currentTime,
@@ -503,8 +503,8 @@ export const LoopVideo = ({
503503
vidRef.current.getBoundingClientRect().height ||
504504
vidRef.current.clientHeight ||
505505
height;
506-
const percentFromTop =
507-
((videoHeight - pxFromBottom) / videoHeight) * 100;
506+
let percentFromTop = ((videoHeight - pxFromBottom) / videoHeight) * 100;
507+
percentFromTop = Math.min(98, Math.max(0, percentFromTop));
508508

509509
for (const cue of Array.from(track.cues)) {
510510
if (cue instanceof VTTCue) {
@@ -666,8 +666,9 @@ export const LoopVideo = ({
666666
showPlayIcon={showPlayIcon}
667667
subtitleSource={subtitleSource}
668668
subtitleSize={subtitleSize}
669-
subtitles={subtitles}
669+
subtitles={activeCue}
670670
showNativeCaptions={false}
671+
subtitlesAvailable={subtitlesAvailable}
671672
/>
672673
</figure>
673674
);

dotcom-rendering/src/components/LoopVideoPlayer.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ type Props = {
128128
subtitleSize: SubtitleSize;
129129
subtitles?: ActiveCue | null;
130130
showNativeCaptions?: boolean;
131+
subtitlesAvailable?: boolean;
131132
};
132133

133134
/**
@@ -164,6 +165,7 @@ export const LoopVideoPlayer = forwardRef(
164165
subtitleSize,
165166
subtitles,
166167
showNativeCaptions = false,
168+
subtitlesAvailable,
167169
}: Props,
168170
ref: React.ForwardedRef<HTMLVideoElement>,
169171
) => {
@@ -228,9 +230,9 @@ export const LoopVideoPlayer = forwardRef(
228230
)}
229231
{FallbackImageComponent}
230232
</video>
231-
{!!subtitles?.text && (
233+
{subtitlesAvailable && (
232234
<SubtitleOverlay
233-
text={subtitles.text}
235+
text={subtitles?.text ?? ''}
234236
subtitleSize={subtitleSize}
235237
/>
236238
)}

dotcom-rendering/src/lib/useSubtitles.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ export type ActiveCue = {
1111
startTime: number;
1212
endTime: number;
1313
text: string;
14-
};
14+
} | null;
1515

1616
export const useSubtitles = ({
1717
video,
1818
playerState,
1919
currentTime,
20-
}: Props): ActiveCue | null => {
20+
}: Props): { activeCue: ActiveCue; subtitlesAvailable: boolean } => {
2121
const [activeTrack, setActiveTrack] = useState<TextTrack | null>(null);
22-
const [activeCue, setActiveCue] = useState<ActiveCue | null>(null);
22+
const [activeCue, setActiveCue] = useState<ActiveCue>(null);
23+
const [subtitlesAvailable, setSubtitlesAvailable] = useState(false);
24+
2325
// only show subtitles if the video is actively playing or if its paused
2426
const shouldShow = playerState === 'PLAYING' || currentTime > 0;
2527

@@ -29,16 +31,22 @@ export const useSubtitles = ({
2931
const textTracks = video.textTracks;
3032

3133
const setTrackFromList = () => {
32-
const track = textTracks[0];
33-
// We currently only support one text track per video, so we are ok to access [0] here. If we add additional languages, this will need updating.
34-
if (!track) return;
35-
34+
const track = textTracks[0] ?? null;
3635
setActiveTrack(track);
36+
37+
// Check if the <track> tag has finished loading
38+
const el = video.querySelector('track');
39+
if (el instanceof HTMLTrackElement && el.readyState === 2) {
40+
setSubtitlesAvailable(true);
41+
}
3742
};
3843

39-
//Get Text track as soon as the video element is available.
4044
setTrackFromList();
41-
// TODO:: are there changes we need to listen for that might change the text track?
45+
46+
const changeListener = () => setTrackFromList();
47+
textTracks.addEventListener('change', changeListener);
48+
49+
return () => textTracks.removeEventListener('change', changeListener);
4250
}, [video]);
4351

4452
useEffect(() => {
@@ -52,9 +60,12 @@ export const useSubtitles = ({
5260
// if we have a track and can show it, hide the native track
5361
track.mode = 'hidden';
5462

63+
console.log('track.mode ', track.mode);
64+
5565
const onCueChange = () => {
5666
const list = track.activeCues;
5767
if (!list || list.length === 0) {
68+
setActiveTrack(null);
5869
return;
5970
}
6071
const cue = list[0] as VTTCue;
@@ -68,9 +79,9 @@ export const useSubtitles = ({
6879
onCueChange();
6980
return () => {
7081
track.removeEventListener('cuechange', onCueChange);
71-
track.mode = 'showing';
82+
// track.mode = 'showing';
7283
};
7384
}, [activeTrack, shouldShow]);
7485

75-
return activeCue;
86+
return { activeCue, subtitlesAvailable };
7687
};

0 commit comments

Comments
 (0)