forked from pulsejet/memories
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
242 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
import PhotoSwipe from "photoswipe"; | ||
|
||
/** | ||
* Check if slide has video content | ||
* | ||
* @param {Slide|Content} content Slide or Content object | ||
* @returns Boolean | ||
*/ | ||
function isVideoContent(content): boolean { | ||
return content?.data?.type === "video"; | ||
} | ||
|
||
class VideoContentSetup { | ||
constructor(lightbox: PhotoSwipe, private options) { | ||
this.initLightboxEvents(lightbox); | ||
lightbox.on("init", () => { | ||
this.initPswpEvents(lightbox); | ||
}); | ||
} | ||
|
||
initLightboxEvents(lightbox: PhotoSwipe) { | ||
lightbox.on("contentLoad", this.onContentLoad.bind(this)); | ||
lightbox.on("contentDestroy", this.onContentDestroy.bind(this)); | ||
lightbox.on("contentActivate", this.onContentActivate.bind(this)); | ||
lightbox.on("contentDeactivate", this.onContentDeactivate.bind(this)); | ||
lightbox.on("contentAppend", this.onContentAppend.bind(this)); | ||
lightbox.on("contentResize", this.onContentResize.bind(this)); | ||
|
||
lightbox.addFilter( | ||
"isKeepingPlaceholder", | ||
this.isKeepingPlaceholder.bind(this) | ||
); | ||
lightbox.addFilter("isContentZoomable", this.isContentZoomable.bind(this)); | ||
lightbox.addFilter( | ||
"useContentPlaceholder", | ||
this.useContentPlaceholder.bind(this) | ||
); | ||
|
||
lightbox.addFilter("domItemData", (itemData, element, linkEl) => { | ||
return itemData; | ||
}); | ||
} | ||
|
||
initPswpEvents(pswp: PhotoSwipe) { | ||
// Prevent draggin when pointer is in bottom part of the video | ||
// todo: add option for this | ||
pswp.on("pointerDown", (e) => { | ||
const slide = pswp.currSlide; | ||
if (isVideoContent(slide) && this.options.preventDragOffset) { | ||
const origEvent = e.originalEvent; | ||
if (origEvent.type === "pointerdown") { | ||
const videoHeight = Math.ceil(slide.height * slide.currZoomLevel); | ||
const verticalEnding = videoHeight + slide.bounds.center.y; | ||
const pointerYPos = origEvent.pageY - pswp.offset.y; | ||
if ( | ||
pointerYPos > verticalEnding - this.options.preventDragOffset && | ||
pointerYPos < verticalEnding | ||
) { | ||
e.preventDefault(); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
// do not append video on nearby slides | ||
pswp.on("appendHeavy", (e) => { | ||
if (isVideoContent(e.slide) && !e.slide.isActive) { | ||
e.preventDefault(); | ||
} | ||
}); | ||
|
||
pswp.on("close", () => { | ||
if (isVideoContent(pswp.currSlide.content)) { | ||
// Switch from zoom to fade closing transition, | ||
// as zoom transition is choppy for videos | ||
if ( | ||
!pswp.options.showHideAnimationType || | ||
pswp.options.showHideAnimationType === "zoom" | ||
) { | ||
pswp.options.showHideAnimationType = "fade"; | ||
} | ||
|
||
// pause video when closing | ||
this.pauseVideo(pswp.currSlide.content); | ||
} | ||
}); | ||
} | ||
|
||
onContentDestroy({ content }) { | ||
if (isVideoContent(content)) { | ||
if (content._videoPosterImg) { | ||
content._videoPosterImg.onload = content._videoPosterImg.onerror = null; | ||
content._videoPosterImg = null; | ||
} | ||
} | ||
} | ||
|
||
onContentResize(e) { | ||
if (isVideoContent(e.content)) { | ||
e.preventDefault(); | ||
|
||
const width = e.width; | ||
const height = e.height; | ||
const content = e.content; | ||
|
||
if (content.element) { | ||
content.element.style.width = width + "px"; | ||
content.element.style.height = height + "px"; | ||
} | ||
|
||
if (content.slide && content.slide.placeholder) { | ||
// override placeholder size, so it more accurately matches the video | ||
const placeholderElStyle = content.slide.placeholder.element.style; | ||
placeholderElStyle.transform = "none"; | ||
placeholderElStyle.width = width + "px"; | ||
placeholderElStyle.height = height + "px"; | ||
} | ||
} | ||
} | ||
|
||
isKeepingPlaceholder(isZoomable, content) { | ||
if (isVideoContent(content)) { | ||
return false; | ||
} | ||
return isZoomable; | ||
} | ||
|
||
isContentZoomable(isZoomable, content) { | ||
if (isVideoContent(content)) { | ||
return false; | ||
} | ||
return isZoomable; | ||
} | ||
|
||
onContentActivate({ content }) { | ||
if (isVideoContent(content) && this.options.autoplay) { | ||
this.playVideo(content); | ||
} | ||
} | ||
|
||
onContentDeactivate({ content }) { | ||
if (isVideoContent(content)) { | ||
this.pauseVideo(content); | ||
} | ||
} | ||
|
||
onContentAppend(e) { | ||
if (isVideoContent(e.content)) { | ||
e.preventDefault(); | ||
e.content.isAttached = true; | ||
e.content.appendImage(); | ||
} | ||
} | ||
|
||
onContentLoad(e) { | ||
const content = e.content; // todo: videocontent | ||
|
||
if (!isVideoContent(e.content)) { | ||
return; | ||
} | ||
|
||
// stop default content load | ||
e.preventDefault(); | ||
|
||
if (content.element) { | ||
return; | ||
} | ||
|
||
content.state = "loading"; | ||
content.type = "video"; // TODO: move this to pswp core? | ||
|
||
content.element = document.createElement("video"); | ||
|
||
if (this.options.videoAttributes) { | ||
for (let key in this.options.videoAttributes) { | ||
content.element.setAttribute( | ||
key, | ||
this.options.videoAttributes[key] || "" | ||
); | ||
} | ||
} | ||
|
||
content.element.setAttribute("poster", content.data.msrc); | ||
|
||
this.preloadVideoPoster(content, content.data.msrc); | ||
|
||
content.element.style.position = "absolute"; | ||
content.element.style.left = 0; | ||
content.element.style.top = 0; | ||
|
||
if (content.data.src) { | ||
content.element.src = content.data.src; | ||
} | ||
} | ||
|
||
preloadVideoPoster(content, src) { | ||
if (!content._videoPosterImg && src) { | ||
content._videoPosterImg = new Image(); | ||
content._videoPosterImg.src = src; | ||
if (content._videoPosterImg.complete) { | ||
content.onLoaded(); | ||
} else { | ||
content._videoPosterImg.onload = content._videoPosterImg.onerror = | ||
() => { | ||
content.onLoaded(); | ||
}; | ||
} | ||
} | ||
} | ||
|
||
playVideo(content) { | ||
if (content.element) { | ||
content.element.play(); | ||
} | ||
} | ||
|
||
pauseVideo(content) { | ||
if (content.element) { | ||
content.element.pause(); | ||
} | ||
} | ||
|
||
useContentPlaceholder(usePlaceholder, content) { | ||
if (isVideoContent(content)) { | ||
return true; | ||
} | ||
return usePlaceholder; | ||
} | ||
} | ||
|
||
export default VideoContentSetup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters