1
0
Fork 0
mirror of https://github.com/sussy-code/smov.git synced 2024-12-25 15:27:40 +01:00
smov/src/components/player/display/base.ts

84 lines
2.2 KiB
TypeScript
Raw Normal View History

import fscreen from "fscreen";
2023-09-30 20:57:00 +02:00
import {
DisplayInterface,
DisplayInterfaceEvents,
} from "@/components/player/display/displayInterface";
import { Source } from "@/components/player/hooks/usePlayer";
import {
canFullscreen,
canFullscreenAnyElement,
canWebkitFullscreen,
} from "@/utils/detectFeatures";
2023-09-30 20:57:00 +02:00
import { makeEmitter } from "@/utils/events";
export function makeVideoElementDisplayInterface(): DisplayInterface {
const { emit, on, off } = makeEmitter<DisplayInterfaceEvents>();
let source: Source | null = null;
let videoElement: HTMLVideoElement | null = null;
let containerElement: HTMLElement | null = null;
let isFullscreen = false;
2023-09-30 20:57:00 +02:00
function setSource() {
if (!videoElement || !source) return;
videoElement.src = source.url;
videoElement.addEventListener("play", () => emit("play", undefined));
videoElement.addEventListener("pause", () => emit("pause", undefined));
}
function fullscreenChange() {
isFullscreen =
!!document.fullscreenElement || // other browsers
!!(document as any).webkitFullscreenElement; // safari
}
fscreen.addEventListener("fullscreenchange", fullscreenChange);
2023-09-30 20:57:00 +02:00
return {
on,
off,
destroy: () => {
fscreen.removeEventListener("fullscreenchange", fullscreenChange);
},
2023-09-30 20:57:00 +02:00
load(newSource) {
source = newSource;
setSource();
},
processVideoElement(video) {
videoElement = video;
setSource();
},
processContainerElement(container) {
containerElement = container;
},
2023-09-30 20:57:00 +02:00
pause() {
videoElement?.pause();
},
play() {
videoElement?.play();
},
toggleFullscreen() {
if (isFullscreen) {
isFullscreen = false;
emit("fullscreen", isFullscreen);
if (!fscreen.fullscreenElement) return;
fscreen.exitFullscreen();
return;
}
// enter fullscreen
isFullscreen = true;
emit("fullscreen", isFullscreen);
if (!canFullscreen() || fscreen.fullscreenElement) return;
if (canFullscreenAnyElement()) {
if (containerElement) fscreen.requestFullscreen(containerElement);
return;
}
if (canWebkitFullscreen()) {
if (videoElement) (videoElement as any).webkitEnterFullscreen();
}
},
2023-09-30 20:57:00 +02:00
};
}