Merge pull request #34 from tmshv/fix/default-page-image
Default page image
This commit is contained in:
7 files changed
+48
-66
No files matched your search
@@ -1,6 +1,5 @@
|
|||||||
import Data from './Data'
|
import Data from './Data'
|
||||||
import Image from './Image'
|
import Image from './Image'
|
||||||
import Config from './Config'
|
|
||||||
import { find, findOne, total } from '../lib/store'
|
import { find, findOne, total } from '../lib/store'
|
||||||
import { Tag } from './Tag'
|
import { Tag } from './Tag'
|
||||||
|
|
||||||
@@ -101,13 +100,6 @@ const previewFromImage = (imgs = []) => imgs.length
|
|||||||
? imgs[0]
|
? imgs[0]
|
||||||
: null
|
: null
|
||||||
|
|
||||||
async function resolveDefaultPreview() {
|
|
||||||
const config = await Config.findConfig()
|
|
||||||
if (!config) return null
|
|
||||||
|
|
||||||
return Image.findByFile(config.articleCardDefaultPreview)
|
|
||||||
}
|
|
||||||
|
|
||||||
async function processArticle(article) {
|
async function processArticle(article) {
|
||||||
let preview = null
|
let preview = null
|
||||||
const previewId = article.preview
|
const previewId = article.preview
|
||||||
@@ -116,8 +108,6 @@ async function processArticle(article) {
|
|||||||
|
|
||||||
if (previewId) {
|
if (previewId) {
|
||||||
preview = await Image.findById(previewId)
|
preview = await Image.findById(previewId)
|
||||||
} else {
|
|
||||||
preview = await resolveDefaultPreview()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Article({
|
return new Article({
|
||||||
|
|||||||
@@ -1,40 +0,0 @@
|
|||||||
import Data from './Data'
|
|
||||||
import { findOne } from '../lib/store'
|
|
||||||
|
|
||||||
const store = () => Data.getStore('Settings')
|
|
||||||
|
|
||||||
export default class Config {
|
|
||||||
static async findConfig() {
|
|
||||||
const config = await Config.findById('settings')
|
|
||||||
return config || new Config({} as any)
|
|
||||||
}
|
|
||||||
|
|
||||||
static async findById(id: string) {
|
|
||||||
const data = await findOne(store(), { id })
|
|
||||||
if (!data) return null
|
|
||||||
|
|
||||||
return new Config(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
public hash: any
|
|
||||||
public file: any
|
|
||||||
public server: any
|
|
||||||
public articlesPerPage: any
|
|
||||||
public articleCardDefaultPreview: any
|
|
||||||
public collectiveImage: any
|
|
||||||
public redirect: any
|
|
||||||
|
|
||||||
constructor({ hash, file, ...data }) {
|
|
||||||
this.hash = hash
|
|
||||||
this.file = file
|
|
||||||
this.server = data.server
|
|
||||||
this.articlesPerPage = data.articlesPerPage || 5
|
|
||||||
this.articleCardDefaultPreview = data.articleCardDefaultPreview
|
|
||||||
this.collectiveImage = data.collectiveImage
|
|
||||||
|
|
||||||
const redirect = data.redirect || {}
|
|
||||||
this.redirect = new Map(Object
|
|
||||||
.entries(redirect)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
import Data from './Data'
|
import Data from './Data'
|
||||||
import { find as storeFind, findOne, total } from '../lib/store'
|
import { find as storeFind, findOne, total } from '../lib/store'
|
||||||
import Image from './Image'
|
import Image from './Image'
|
||||||
import Config from './Config'
|
|
||||||
|
|
||||||
const store = () => Data.getStore('Page')
|
const store = () => Data.getStore('Page')
|
||||||
|
|
||||||
@@ -80,13 +79,6 @@ const previewFromImage = (imgs = []) => imgs.length
|
|||||||
? imgs[0]
|
? imgs[0]
|
||||||
: null
|
: null
|
||||||
|
|
||||||
async function resolveDefaultPreview() {
|
|
||||||
const config = await Config.findConfig()
|
|
||||||
if (!config) return null
|
|
||||||
|
|
||||||
return Image.findByFile(config.articleCardDefaultPreview)
|
|
||||||
}
|
|
||||||
|
|
||||||
async function processPage(article) {
|
async function processPage(article) {
|
||||||
let preview = null
|
let preview = null
|
||||||
const previewId = article.preview
|
const previewId = article.preview
|
||||||
@@ -95,8 +87,6 @@ async function processPage(article) {
|
|||||||
|
|
||||||
if (previewId) {
|
if (previewId) {
|
||||||
preview = await Image.findById(previewId)
|
preview = await Image.findById(previewId)
|
||||||
} else {
|
|
||||||
preview = await resolveDefaultPreview()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Page({
|
return new Page({
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ import Article from '../core/Article'
|
|||||||
import { encodeImage } from './image'
|
import { encodeImage } from './image'
|
||||||
|
|
||||||
export function encodeArticle(article: Article) {
|
export function encodeArticle(article: Article) {
|
||||||
|
const preview = article.preview ? encodeImage(article.preview) : null
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...article,
|
...article,
|
||||||
preview: encodeImage(article.preview),
|
preview,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,13 +3,15 @@ import Page from '../core/Page'
|
|||||||
import { Breadcrumb } from '../types'
|
import { Breadcrumb } from '../types'
|
||||||
|
|
||||||
export function encodePage(page: Page, breadcrumb: Breadcrumb) {
|
export function encodePage(page: Page, breadcrumb: Breadcrumb) {
|
||||||
|
const preview = page.preview ? encodeImage(page.preview) : null
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: page.id,
|
id: page.id,
|
||||||
title: page.title,
|
title: page.title,
|
||||||
url: page.url,
|
url: page.url,
|
||||||
data: page.data,
|
data: page.data,
|
||||||
description: page.description,
|
description: page.description,
|
||||||
preview: encodeImage(page.preview),
|
preview,
|
||||||
tokens: page.tokens,
|
tokens: page.tokens,
|
||||||
breadcrumb,
|
breadcrumb,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ module.exports = withCss(withSass({
|
|||||||
exportTrailingSlash: true,
|
exportTrailingSlash: true,
|
||||||
env: {
|
env: {
|
||||||
APP_ARTICLES_PAGE_SIZE: 24,
|
APP_ARTICLES_PAGE_SIZE: 24,
|
||||||
|
APP_CARD_DEFAULT_IMAGE: 'https://art.shlisselburg.org/static/img/main.jpg',
|
||||||
},
|
},
|
||||||
webpack(config) {
|
webpack(config) {
|
||||||
config.resolve.modules.push(__dirname)
|
config.resolve.modules.push(__dirname)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { IArticle } from 'src/types'
|
import { IArticle, ImageDefinition } from 'src/types'
|
||||||
import { Pager } from '../Pager'
|
import { Pager } from '../Pager'
|
||||||
import { CardGrid } from '../CardGrid'
|
import { CardGrid } from '../CardGrid'
|
||||||
import { Card, CardLayout } from '../Card'
|
import { Card, CardLayout } from '../Card'
|
||||||
@@ -6,6 +6,45 @@ import { Date } from './Date'
|
|||||||
import { Block } from '../Block'
|
import { Block } from '../Block'
|
||||||
import { Spacer } from '../Spacer'
|
import { Spacer } from '../Spacer'
|
||||||
|
|
||||||
|
export interface IResizeImage {
|
||||||
|
w: number
|
||||||
|
h: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resizeImage(src: string, options: IResizeImage): string {
|
||||||
|
return `https://images.weserv.nl/?url=${src}&w=${options.w}&h=${options.h}&n=-1`
|
||||||
|
}
|
||||||
|
|
||||||
|
function getImage(image?: ImageDefinition) {
|
||||||
|
if (!image) {
|
||||||
|
const defaultSrc = process.env.APP_CARD_DEFAULT_IMAGE
|
||||||
|
const src = resizeImage(defaultSrc, { w: 400, h: 400 })
|
||||||
|
const set = [
|
||||||
|
{
|
||||||
|
density: 2,
|
||||||
|
href: resizeImage(defaultSrc, { w: 800, h: 800 }),
|
||||||
|
}
|
||||||
|
]
|
||||||
|
const srcSet = set
|
||||||
|
.map(({ href, density }) => `${href} ${density}x`)
|
||||||
|
.join(' ')
|
||||||
|
|
||||||
|
return {
|
||||||
|
src,
|
||||||
|
srcSet,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const preview = image.artifacts.medium
|
||||||
|
const src = preview.src
|
||||||
|
const srcSet = preview.set.map(({ url, density }) => `${url} ${density}x`).join(' ')
|
||||||
|
|
||||||
|
return {
|
||||||
|
src,
|
||||||
|
srcSet,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export type ArticleGridProps = {
|
export type ArticleGridProps = {
|
||||||
articles: IArticle[]
|
articles: IArticle[]
|
||||||
nextPage: number | null
|
nextPage: number | null
|
||||||
@@ -29,9 +68,7 @@ export const ArticleGrid: React.FC<ArticleGridProps> = ({ articles, nextPage, pr
|
|||||||
{articles.map((article, i) => {
|
{articles.map((article, i) => {
|
||||||
const gridColumn = article.featured ? 'span 2' : 'auto'
|
const gridColumn = article.featured ? 'span 2' : 'auto'
|
||||||
const layout: CardLayout = article.featured ? 'featured' : 'simple'
|
const layout: CardLayout = article.featured ? 'featured' : 'simple'
|
||||||
const preview = article.preview.artifacts.medium
|
const { src, srcSet } = getImage(article.preview)
|
||||||
const src = preview.src
|
|
||||||
const srcSet = preview.set.map(({ url, density }) => `${url} ${density}x`).join(' ')
|
|
||||||
|
|
||||||
const content = !article.featured
|
const content = !article.featured
|
||||||
? (
|
? (
|
||||||
|
|||||||
Reference in new issue
Block a user