Add config option for autoplay videos

This commit is contained in:
Alan Grainger
2026-06-11 16:12:00 +12:00
parent 46438f1c74
commit 652f2903ce
6 changed files with 56 additions and 20 deletions
+28
View File
@@ -163,6 +163,34 @@ export function initLightbox () {
}
})
// Video autoplay (off by default, ipp.lightbox.autoPlayVideos). PhotoSwipe
// fires contentActivate/contentDeactivate as slides become / stop being the
// current slide, including the initial slide on open.
if (state.lightboxConfig.autoPlayVideos) {
state.lightbox.on('contentActivate', ({ content }: { content: { element?: HTMLElement } }) => {
const video = content.element?.querySelector('video')
if (video) {
video.play().catch(() => {
// Browsers block unmuted autoplay without a user gesture (e.g. a
// deep link straight to a video slide) - retry muted.
video.muted = true
video.play().catch(() => {})
})
}
})
}
// Pause and rewind videos when sliding away so audio doesn't bleed into
// the neighbouring slides (PhotoSwipe keeps them mounted). Applies whether
// playback started automatically or manually.
state.lightbox.on('contentDeactivate', ({ content }: { content: { element?: HTMLElement } }) => {
const video = content.element?.querySelector('video')
if (video) {
video.pause()
video.currentTime = 0
}
})
// Optional control hiding (defaults match Immich's behavior)
const docEl = document.documentElement
if (!state.lightboxConfig.showArrows) docEl.classList.add('pswp-no-arrows')