This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 13
adding code to show thumbnails below tool media carousel (#20) #23
Open
kots14
wants to merge
5
commits into
analysis-tools-dev:master
Choose a base branch
from
kots14:enhanced-carousel
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9690be8
adding code to show thumbnails below tool media carousel (#20)
kots14 87e3f83
Merge branch 'master' into enhanced-carousel
kots14 8fd3c2a
updated changes for putting thumbnails at build time
kots14 d0a0a66
Merge branch 'master' into enhanced-carousel
kots14 a9b7cec
adding new yarn.lock after merging master
kots14 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,68 @@ | ||
import { css } from "twin.macro" | ||
export const SliderContentImageStyle = css` | ||
height: inherit; | ||
margin-left: auto; | ||
margin-right: auto; | ||
` | ||
export const SliderStyle = css` | ||
.slick-prev:before, | ||
.slick-next:before { | ||
color: #222425!important; | ||
} | ||
.slick-slide { | ||
padding: 0rem 1rem 0rem 1rem; | ||
} | ||
` | ||
export const SliderContentStyle = css` | ||
margin: 1rem 0rem 1rem 0.1rem; | ||
transition: transform .2s; | ||
height: 8rem; | ||
background: #f7f7f7; | ||
text-align: center; | ||
cursor: pointer; | ||
border-radius: 0.5rem; | ||
|
||
&:hover { | ||
-ms-transform: scale(1.005); | ||
-webkit-transform: scale(1.005); | ||
transform: scale(1.005); | ||
} | ||
|
||
&:focus { | ||
outline: none; | ||
-webkit-box-shadow: 0px 0px 4px 0px rgba(173,173,173,1); | ||
-moz-box-shadow: 0px 0px 4px 0px rgba(173,173,173,1); | ||
box-shadow: 0px 0px 4px 0px rgba(173,173,173,1); | ||
} | ||
` | ||
export const getSliderSetting = noOfElements => { | ||
const maxSlidesToShow = noOfElements < 3 ? 2 : 3; | ||
return { | ||
focusOnSelect: true, | ||
dots: true, | ||
infinite: false, | ||
speed: 500, | ||
slidesToShow: maxSlidesToShow, | ||
slidesToScroll: 1, | ||
responsive: [{ | ||
breakpoint: 1200, | ||
settings: { | ||
slidesToShow: maxSlidesToShow, | ||
slidesToScroll: 1 | ||
} | ||
}, { | ||
breakpoint: 600, | ||
settings: { | ||
slidesToShow: 2, | ||
slidesToScroll: 1 | ||
} | ||
}, { | ||
breakpoint: 480, | ||
settings: { | ||
slidesToShow: 1, | ||
slidesToScroll: 1 | ||
} | ||
} | ||
] | ||
} | ||
} |
This file contains hidden or 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,167 @@ | ||
import React, { useState, useEffect } from "react" | ||
import tw, { css } from "twin.macro" | ||
import ReactPlayer from "react-player/lazy" | ||
import "slick-carousel/slick/slick.css" | ||
import "slick-carousel/slick/slick-theme.css" | ||
import Slider from "react-slick" | ||
import { | ||
SliderContentImageStyle, | ||
SliderStyle, | ||
SliderContentStyle, | ||
getSliderSetting | ||
} from "./main-media-util-helper" | ||
|
||
/** | ||
* | ||
* **Credits** | ||
* Author : yangshun | ||
* Gist link : https://gist.github.com/yangshun/9892961 | ||
*/ | ||
const parseVideo = url => { | ||
url.match(/(http:|https:|)\/\/(player.|www.)?(vimeo\.com|youtu(be\.com|\.be|be\.googleapis\.com))\/(video\/|embed\/|watch\?v=|v\/)?([A-Za-z0-9._%-]*)(&\S+)?/); | ||
|
||
const type = RegExp.$3.indexOf("youtu") > -1 ? "youtube" : | ||
RegExp.$3.indexOf("vimeo") > -1 ? "vimeo" | ||
: undefined | ||
|
||
return { | ||
type: type, | ||
id: RegExp.$6 | ||
} | ||
} | ||
|
||
const getVideoThumbnailUrl = async url => { | ||
const video = parseVideo(url) | ||
if (video.type === "youtube") | ||
return video.id ? "https://img.youtube.com/vi/" + video.id + "/maxresdefault.jpg" : "#" | ||
if (video.type === "vimeo") { | ||
const fetched = (async videoId => { | ||
let result = {} | ||
try { | ||
const response = await fetch("https://vimeo.com/api/v2/video/" + videoId + ".json") | ||
result = await response.json() | ||
return result[0].thumbnail_large | ||
} catch (e) { | ||
console.error("Error while fetching Vimeo video data", e) | ||
} | ||
}) | ||
return fetched(video.id) | ||
} | ||
} | ||
|
||
const renderMainMediaDisplayElement = { | ||
video: video => { | ||
return ( | ||
<div tw="h-full text-center" key={video.key} id={video.key} css={video.css}> | ||
<ReactPlayer url={video.url} width="100%" controls={true}/> | ||
</div> | ||
) | ||
}, | ||
image: image => ( | ||
<a href={image.url} tw="w-full text-center" css={image.css} | ||
key={image.key} id={image.key}> | ||
<img | ||
alt={`Screenshot of ${image.name} website`} | ||
tw="border-4 max-w-full inline-block" | ||
src={image.src} | ||
/> | ||
</a> | ||
), | ||
} | ||
|
||
const renderMainMediaSliderElement = { | ||
video: video => sliderThumbnail(video), | ||
image: image => sliderThumbnail(image) | ||
} | ||
|
||
const sliderThumbnail = props => { | ||
props = props || {} | ||
const src = props.src || props.thumbnailSrc || "#" | ||
const alt = props.name || "Thumbnail" | ||
return ( | ||
<div css={[SliderContentStyle]} | ||
kots14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
id={props.key} | ||
key={props.key} | ||
onClick={props.onClick} | ||
aria-hidden="true"> | ||
<img | ||
alt={alt} | ||
id={props.key + "_img"} | ||
key={props.key + "_img"} | ||
src={src} | ||
css={[ | ||
tw`border-4 max-w-full`, | ||
SliderContentImageStyle | ||
]} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
export const MainMediaUtil = ({data}) => { | ||
const [items] = useState(data) | ||
const [index, setIndex] = useState(0) | ||
const [isRendered, setIsRendered] = useState(false) | ||
const [hasThumbnails, setHasThumbnails] = useState(false) | ||
const sliderSetting = getSliderSetting(items.length) | ||
|
||
const toggleDisplayStatusOfElement = options => { | ||
options = options || {} | ||
const idForElementToDisplay = "#main_media_util_in_display_" + index | ||
const elementToDisplay = document.querySelector(idForElementToDisplay) | ||
elementToDisplay.setAttribute('style', options.style || 'display:block') | ||
|
||
if (isRendered) return | ||
const idForElementToFocus = "#main_media_util_" + index | ||
const elementToFocus = document.querySelector(idForElementToFocus) | ||
elementToFocus.focus({ preventScroll: true }) | ||
setIsRendered(true) | ||
} | ||
|
||
const populateVideoThumbnails = async () => { | ||
items.map(async item => { | ||
if (item.type !== "video") return | ||
const url = await getVideoThumbnailUrl(item.source.url) | ||
const target = document.querySelector("#" + item.source.key + "_img") | ||
target.setAttribute("src", url) | ||
}) | ||
setHasThumbnails(true) | ||
} | ||
|
||
useEffect(() => { | ||
if (items.length > 1) toggleDisplayStatusOfElement() | ||
if (!hasThumbnails) populateVideoThumbnails() | ||
}) | ||
|
||
return items && items.length > 1 ? ( | ||
<> | ||
<div tw="items-center h-full mb-1"> | ||
{items.map((item, itemIndex) => { | ||
item.source.key = "main_media_util_in_display_" + itemIndex | ||
item.source.css = css`display:none;` | ||
return renderMainMediaDisplayElement[item.type](item.source) | ||
})} | ||
</div> | ||
<div tw="items-center h-full ml-2 mr-2"> | ||
<Slider {...sliderSetting} css={[SliderStyle]}> | ||
{items.map((item, itemIndex) => { | ||
item.source.key = "main_media_util_" + itemIndex | ||
item.source.onClick = () => { | ||
if (itemIndex === index) return | ||
toggleDisplayStatusOfElement({style : 'display:none' }) | ||
setIndex(itemIndex) | ||
} | ||
return renderMainMediaSliderElement[item.type](item.source) | ||
})} | ||
</Slider> | ||
</div> | ||
</> | ||
) : ( | ||
<div tw="flex justify-center items-center h-full mb-5 pb-4"> | ||
{items && items.map(item => { | ||
item.source.key = "main_media_util_in_display_0" | ||
return renderMainMediaDisplayElement[item.type](item.source) | ||
})} | ||
</div> | ||
) | ||
} |
This file contains hidden or 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
Binary file modified
BIN
-749 Bytes
(98%)
static/screenshots/github/githubcommetricsgrimoirecmetrics.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eventually I'd love to move the thumbnail creation to build time.
This could be done similarly to the screenshot creation in
gatsby-node.js
.The advantage would be that we don't have to run the thumbnail generation on the client.
I think this is a bit outside of the scope of this PR, so let me know if you want to get this in or if we should tackle that at a later point in time.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mre I'd like to understand whether you want the thumbnails to be downloaded as images or are you referring creation of the thumbnail links at the time of build? Please let me know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally we should download the thumbnails as images yes.
Guess you could copy-and-paste this section in the config and replace
screenshot
withthumbnail
.https://github.com/analysis-tools-dev/website/blob/4d8a0b46e41158fbac61327ced1f6ee49059c21e/gatsby-node.js#L148-L155
Then wherever you need the thumbnail you can use it as a field similar to the screenshot field:
https://github.com/analysis-tools-dev/website/blob/8b82484b73a342618947cf0c48f1340dd11407b3/src/components/tool/main-media.js#L36
Not sure if that's understandable. If not I can add a commit and we can have a look.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mre Can you please help me out with a npm package that can download thumbnails? I'm trying to work on this actually and so far I was unable to find any. Also, please let me know if I can utilise the code from
screenshot.js
for downloading thumbnails - I'm trying this as you suggested previously.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh you can totally use your thumbnail code. You could have a
thumbnail.js
with your code next toscreenshot.js
and then call that code fromgatsby.node.js
like above; so something like:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like I'm in the right path already, I'll get back to you if I'm stuck. Thanks :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mre I've updated the code in this PR. I apologise for the delay.
I tried to put the thumbnail related data under node.resources. However, I couldn't figure it out. Hence, I went with the approach for which you already gave an example. I'm using
image-downloader
package to download the thumbnails at the build time. I hope that this would sort out all the issues.Please let me know if further changes are required.