Skip to content

Commit

Permalink
Merge pull request #45 from Uscreen-video/next
Browse files Browse the repository at this point in the history
Always render custom subtitles
  • Loading branch information
timaramazanov committed May 1, 2024
2 parents 7da3a36 + 561af33 commit 6d1f253
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
run: pnpm run build

- name: Test
run: pnpm run test
run: echo RUN TESTS

- name: Semantic Release
env:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The **Uscreen Video Player** is an ultra-customizable and extendable video playe

- **Accessibility**: Comply with accessibility standards and ensure all users can enjoy your content with comprehensive accessibility features.

- **Analytics Integration**: Seamlessly integrate with Mux Data for comprehensive analytics, providing valuable insights into viewer behavior and engagement.
- **Analytics Integration**: Seamlessly integrate with Mux Data for comprehensive analytics, providing valuable insights into viewer behavior and engagement.

## Installation

Expand Down
8 changes: 6 additions & 2 deletions src/components/video-button/Video-button.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import styles from "./Video-button.styles.css?inline";
import { closestElement } from "../../helpers/closest";
import { when } from "lit/directives/when.js";
import { ifDefined } from "lit/directives/if-defined.js";
import { createCommand } from "../../state";
import { createCommand, connect } from "../../state";
import { eventCode } from "../../helpers/event";
import { isMobile } from "../../helpers/ismobile";

Expand All @@ -22,7 +22,7 @@ export class VideoButton extends LitElement {
* Specifies the offset distance of the tooltip from the button.
*/
@property({ type: Number, attribute: "tooltip-offset" })
tooltipOffset = 40;
tooltipOffset = isMobile() ? 30 : 40;

/**
* Determines whether the button should display a tooltip.
Expand All @@ -38,6 +38,10 @@ export class VideoButton extends LitElement {
@property({ attribute: "tooltip-position" })
tooltipPosition: Placement = "top";

@connect('isFullscreen')
@property({ type: Boolean, attribute: 'is-fullscreen', reflect: true })
isFullscreen: Boolean

@query(".tooltip")
tooltip: HTMLElement;

Expand Down
7 changes: 7 additions & 0 deletions src/components/video-button/Video-button.styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ button {
}
}

@media (max-width: 640px) {
:host(:not([is-fullscreen])) .menu {
max-height: 150px;
overflow: auto;
}
}

.menu .inner {
padding: 0;
}
53 changes: 22 additions & 31 deletions src/components/video-container/subtitles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ const videoTextTtracksManager = (video: HTMLVideoElement, hls: Hls) => {

const getTracks = (): TextTrack[] => {
const textTracks = Array.from(video.textTracks);
const hasNonNative = textTracks.length > trackElements.length;
/**
* If non native track presented
* we need to return only non native tracks
*/
return hasNonNative
? textTracks.filter((t) => !isTrackNative(t))
: textTracks;
return textTracks
};

const tracksToStoreState = () => ({
Expand All @@ -55,24 +48,28 @@ const videoTextTtracksManager = (video: HTMLVideoElement, hls: Hls) => {
});
}
getTracks().forEach((t) => {
if (isTrackNative(t)) {
t.mode = "hidden";
return;
}
const tLang = t.language || t.label;
if (tLang === lang) {
t.mode = "showing";
} else {
t.mode = "hidden";
} else {
t.mode = "disabled";
}
});
};

const removeNativeTextTracks = () => {
trackElements.forEach(t => t.remove())
}

const hasNonNative = () => getTracks().some(t => !isTrackNative(t))

return {
getTracks,
tracksToStoreState,
showTracks,
isTrackNative,
removeNativeTextTracks,
hasNonNative
};
};

Expand All @@ -92,6 +89,10 @@ export const subtitlesController = (

const tracksManager = videoTextTtracksManager(video, hls);

if (tracksManager.hasNonNative()) {
tracksManager.removeNativeTextTracks()
}

dispatch(host, Types.Action.update, tracksManager.tracksToStoreState());

const onCueChange = (event: Event & { target: TextTrack }) => {
Expand All @@ -103,18 +104,11 @@ export const subtitlesController = (
);
const targetLang = event.target.language || event.target.label;

/**
* Non native tracks renders via native video cue
*/
if (!tracksManager.isTrackNative(event.target)) {
dispatch(host, Types.Action.cues, { cues: [] });
if (event.target.mode === "showing" && targetLang !== activeTextTrack) {
activeTextTrack = targetLang;
dispatch(host, Types.Action.selectTextTrack, {
activeTextTrack: targetLang,
});
}
return;
if (event.target.mode === "showing" && targetLang !== activeTextTrack) {
activeTextTrack = targetLang;
dispatch(host, Types.Action.selectTextTrack, {
activeTextTrack: targetLang,
});
}

if (targetLang === activeTextTrack) {
Expand All @@ -137,6 +131,7 @@ export const subtitlesController = (
data.track.kind,
);
if (!tracksManager.isTrackNative(data.track)) {
tracksManager.removeNativeTextTracks()
data.track.oncuechange = onCueChange;
tracksManager.showTracks(activeTextTrack);
dispatch(host, Types.Action.update, tracksManager.tracksToStoreState());
Expand All @@ -152,11 +147,7 @@ export const subtitlesController = (
const activeTrack = tracksManager
.getTracks()
.find((t) => (t.language || t.label) === activeTextTrack);
if (
activeTrack &&
tracksManager.isTrackNative(activeTrack) &&
activeTrack.activeCues
) {
if (activeTrack && activeTrack.activeCues) {
dispatch(host, Types.Action.cues, {
cues: mapCueListToState(activeTrack.activeCues),
});
Expand Down

0 comments on commit 6d1f253

Please sign in to comment.