Skip to content

Commit

Permalink
fix: better sort video and caption elements. (#942)
Browse files Browse the repository at this point in the history
  • Loading branch information
crhallberg authored Oct 19, 2023
1 parent f503281 commit ca04992
Showing 1 changed file with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MediaElementExtensionEvents } from "../../extensions/uv-mediaelement-ex
import { CenterPanel } from "../uv-shared-module/CenterPanel";
import { IMediaElementExtension } from "../../extensions/uv-mediaelement-extension/IMediaElementExtension";
import { sanitize } from "../../../../Utils";
import { MediaType } from "@iiif/vocabulary/dist-commonjs/";
import { MediaType, RenderingFormat } from "@iiif/vocabulary/dist-commonjs/";
import {
AnnotationBody,
Canvas,
Expand Down Expand Up @@ -94,10 +94,19 @@ export class MediaElementCenterPanel extends CenterPanel {

if (renderings && renderings.length) {
canvas.getRenderings().forEach((rendering: Rendering) => {
sources.push({
type: rendering.getFormat().toString(),
src: rendering.id,
});
if (this.isTypeMedia(rendering)) {
sources.push({
type: rendering.getFormat().toString(),
src: rendering.id,
});
}

if (this.isTypeCaption(rendering)) {
subtitles.push({
label: rendering.getLabel().getValue() ?? rendering.getFormat().toString(),
id: rendering.id,
});
}
});
} else {
const formats: AnnotationBody[] | null = this.extension.getMediaFormats(
Expand All @@ -106,18 +115,23 @@ export class MediaElementCenterPanel extends CenterPanel {

if (formats && formats.length) {
formats.forEach((format: AnnotationBody) => {
const type: MediaType | null = format.getFormat();
const type = format.getFormat();

// Add any additional subtitle types if required.
if (type && type.toString() === "text/vtt") {
subtitles.push(format.__jsonld);
} else if (type) {
if (type === null) {
return;
}

if (this.isTypeMedia(format)) {
sources.push({
label: format.__jsonld.label ? format.__jsonld.label : "",
type: type.toString(),
src: format.id,
});
}

if (this.isTypeCaption(format)) {
subtitles.push(format.__jsonld);
}
});
}
}
Expand All @@ -128,6 +142,10 @@ export class MediaElementCenterPanel extends CenterPanel {
);

// Add VTT subtitles/captions.
if (subtitles.length > 0) {
// Show captions options popover for better interface feedback
subtitles.unshift({ id: "none" });
}
for (const subtitle of subtitles) {
this.$media.append(
$(`<track label="${subtitle.label}" kind="subtitles" srclang="${
Expand Down Expand Up @@ -276,6 +294,36 @@ export class MediaElementCenterPanel extends CenterPanel {
this.extensionHost.publish(Events.LOAD);
}

// audio/video
isTypeMedia(element: Rendering | AnnotationBody) {
const type: RenderingFormat | MediaType | null = element.getFormat();

if (type === null) {
return false;
}

const typeStr = type.toString();
const typeGroup = typeStr.split("/")[0];

return typeGroup === "audio" || typeGroup === "video";
}

// vtt, srt, csv
isTypeCaption(element: Rendering | AnnotationBody) {
const type: RenderingFormat | MediaType | null = element.getFormat();

if (type === null) {
return false;
}

const captionTypes = new Set<String>([
"text/vtt",
"text/srt",
]);

return captionTypes.has(type.toString());
}

isVideo(): boolean {
return (<IMediaElementExtension>this.extension).isVideo();
}
Expand Down

0 comments on commit ca04992

Please sign in to comment.