collective page images refactoring

This commit is contained in:
tmshv committed 2020-03-03 05:58:35 +03:00
1 parent 087a4b6172
commit a14db0e8da
6 files changed
+68 -66

No files matched your search

+19 -18
View File
@@ -1,14 +1,15 @@
import Head from 'next/head' import Head from 'next/head'
import { App } from 'src/components/App' import { App } from 'src/components/App'
import { Meta } from 'src/components/Meta' import { Meta } from 'src/components/Meta'
import { CollectiveImage } from 'src/components/CollectiveImage'
import { meta } from 'src/lib/meta' import { meta } from 'src/lib/meta'
import { IMeta, IPage, ImageDefinition } from 'src/types' import { IMeta, IPage, ImageSize } from 'src/types'
import { createApiUrl, requestGet } from 'src/next-lib' import { createApiUrl, requestGet } from 'src/next-lib'
import { NextPage, NextPageContext } from 'next' import { NextPage, NextPageContext } from 'next'
import { CardGrid } from 'src/components/CardGrid' import { CardGrid } from 'src/components/CardGrid'
import { Card } from 'src/components/Card' import { Card } from 'src/components/Card'
import { Block } from 'src/components/Block' import { Block } from 'src/components/Block'
import { Image } from 'src/components/Image'
import { imageSrcSet } from 'src/lib/image'
const persons = [ const persons = [
'/teacher/mg-timasheva', '/teacher/mg-timasheva',
@@ -38,7 +39,6 @@ type CardData = {
type Props = { type Props = {
title: string title: string
image: string
meta: IMeta meta: IMeta
data: CardData[] data: CardData[]
} }
@@ -49,6 +49,9 @@ const Page: NextPage<Props> = props => {
return return
} }
const src = 'https://art.shlisselburg.org/static/img/collective.jpg'
const srcSet = imageSrcSet(src, [ImageSize.big, ImageSize.large, ImageSize.medium, ImageSize.small])
return ( return (
<App <App
showAuthor={true} showAuthor={true}
@@ -59,14 +62,17 @@ const Page: NextPage<Props> = props => {
<Meta meta={props.meta} /> <Meta meta={props.meta} />
</Head> </Head>
{!props.image ? null : ( <Image
<CollectiveImage src={src}
data={props.image} srcSet={srcSet}
style={{ style={{
marginBottom: 'var(--size-l)', marginBottom: 'var(--size-l)',
}} }}
/> imgStyle={{
)} width: '100%',
borderRadius: 'var(--radius)',
}}
/>
<CardGrid style={{ <CardGrid style={{
marginBottom: 'var(--size-xl)', marginBottom: 'var(--size-xl)',
@@ -100,10 +106,6 @@ const Page: NextPage<Props> = props => {
export const unstable_getStaticProps = async (ctx: NextPageContext) => { export const unstable_getStaticProps = async (ctx: NextPageContext) => {
const title = 'Преподаватели Шлиссельбургской ДХШ' const title = 'Преподаватели Шлиссельбургской ДХШ'
const imageFile = 'Images/HudozkaCollective2017.jpg'
const resImage = await requestGet<ImageDefinition>(createApiUrl(`/api/image?file=${imageFile}`), null)
const image = resImage?.artifacts.large
const pages = await Promise.all(persons const pages = await Promise.all(persons
.map(page => requestGet<IPage>(createApiUrl(`/api/page?page=${page}`), null)) .map(page => requestGet<IPage>(createApiUrl(`/api/page?page=${page}`), null))
) )
@@ -116,16 +118,15 @@ export const unstable_getStaticProps = async (ctx: NextPageContext) => {
title: x.title, title: x.title,
caption: x.description, caption: x.description,
href: x.url, href: x.url,
imageSrc: image.src,
imageAlt: title, imageAlt: title,
imageSrcSet: image.set.join(' ') imageSrc: image.src,
imageSrcSet: imageSrcSet(image.src, [ImageSize.medium, ImageSize.small])
} }
}) })
return { return {
props: { props: {
data, data,
image,
title, title,
meta: meta({ meta: meta({
title, title,
-22
View File
@@ -1,22 +0,0 @@
import React from 'react'
import { Image } from './Image'
export const CollectiveImage = (props) => (
<div
style={props.style}
>
<Image
src={props.data.src}
alt={props.data.alt}
set={props.data.set}
opa={false}
style={{
marginBottom: 'var(--double-margin)',
}}
imgStyle={{
width: '100%',
borderRadius: 'var(--radius)',
}}
/>
</div>
)
+9 -26
View File
@@ -1,40 +1,23 @@
import * as React from 'react' import './styles.css'
import cx from 'classnames' import { memo } from 'react'
export interface IImageProps { export type ImageProps = {
style?: React.CSSProperties style?: React.CSSProperties
imgStyle?: React.CSSProperties imgStyle?: React.CSSProperties
src: string src: string
alt: string alt?: string
opa: boolean srcSet?: string
set: Array<{
url: string
density: number
}>
} }
export const Image: React.FC<IImageProps> = React.memo(props => { export const Image: React.FC<ImageProps> = memo(props => {
const srcSet = props.set
.map(({ url, density }) => `${url} ${density}x`)
.join(' ')
return ( return (
<picture style={props.style}> <picture className={'image'} style={props.style}>
<style jsx>{`
picture img {
display: block;
}
`}</style>
<img <img
style={props.imgStyle} style={props.imgStyle}
className={cx({
opa: props.opa
})}
alt={props.alt} alt={props.alt}
src={props.src} src={props.src}
srcSet={srcSet} srcSet={props.srcSet}
/> />
</picture > </picture>
) )
}) })
+7
View File
@@ -0,0 +1,7 @@
.image {
display: block;
}
.image img {
display: block;
}
+25
View File
@@ -0,0 +1,25 @@
import { ImageSize } from 'src/types'
export type ResizeOptions = {
width: number
height: number
}
export function resizeImage(src: string, options: ResizeOptions): string {
return `https://images.weserv.nl/?url=${src}&w=${options.width}&h=${options.height}&n=-1`
}
export function imageSrcSet(src: string, sizes: ImageSize[]): string {
const set = sizes
.map(x => ({
value: x,
href: resizeImage(src, {
width: x,
height: x,
}),
}))
return set
.map(x => `${x.href} ${x.value}w`)
.join(', ')
}
+8
View File
@@ -61,6 +61,14 @@ export interface ITag {
href: string href: string
} }
export enum ImageSize {
big = 3000,
large = 1500,
medium = 1000,
small = 500,
thumbnail = 200,
}
export type ImageArtifact = { export type ImageArtifact = {
width: number width: number
height: number height: number