refactor: 💨

This commit is contained in:
codytseng 2025-04-13 10:30:34 +08:00
parent c95444748a
commit f7087517e4
3 changed files with 65 additions and 56 deletions

View file

@ -0,0 +1,48 @@
class VideoManagerService {
static instance: VideoManagerService
private currentVideo: HTMLVideoElement | null = null
constructor() {
if (!VideoManagerService.instance) {
VideoManagerService.instance = this
}
return VideoManagerService.instance
}
async enterPiP(video: HTMLVideoElement) {
if (this.currentVideo && this.currentVideo !== video) {
await this.exitPiP(this.currentVideo)
}
if ('requestPictureInPicture' in video) {
await video.requestPictureInPicture()
} else if ('webkitSetPresentationMode' in video) {
;(video as any).webkitSetPresentationMode('picture-in-picture')
}
}
async exitPiP(video: HTMLVideoElement) {
video.pause()
if (document.pictureInPictureElement === video) {
await document.exitPictureInPicture()
} else if ('webkitSetPresentationMode' in video) {
;(video as any).webkitSetPresentationMode('inline')
}
if (this.currentVideo === video) {
this.currentVideo = null
}
}
async playVideo(video: HTMLVideoElement) {
if (this.currentVideo && this.currentVideo !== video) {
await this.exitPiP(this.currentVideo)
}
this.currentVideo = video
video.play()
}
}
const instance = new VideoManagerService()
export default instance

View file

@ -1,38 +0,0 @@
class VideoManager {
private static currentVideo: HTMLVideoElement | null = null
static async enterPiP(video: HTMLVideoElement) {
if (VideoManager.currentVideo && VideoManager.currentVideo !== video) {
await VideoManager.exitPiP(VideoManager.currentVideo)
}
if ('requestPictureInPicture' in video) {
await video.requestPictureInPicture()
} else if ('webkitSetPresentationMode' in video) {
;(video as any).webkitSetPresentationMode('picture-in-picture')
}
}
static async exitPiP(video: HTMLVideoElement) {
video.pause()
if (document.pictureInPictureElement === video) {
await document.exitPictureInPicture()
} else if ('webkitSetPresentationMode' in video) {
;(video as any).webkitSetPresentationMode('inline')
}
if (VideoManager.currentVideo === video) {
VideoManager.currentVideo = null
}
}
static async playVideo(video: HTMLVideoElement) {
if (VideoManager.currentVideo && VideoManager.currentVideo !== video) {
await VideoManager.exitPiP(VideoManager.currentVideo)
}
VideoManager.currentVideo = video
video.play()
}
}
export default VideoManager