-
Notifications
You must be signed in to change notification settings - Fork 284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for densities #634
base: v0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import defu from 'defu' | |
import { hasProtocol, parseURL, joinURL, withLeadingSlash } from 'ufo' | ||
import type { ImageOptions, ImageSizesOptions, CreateImageOptions, ResolvedImage, MapToStatic, ImageCTX, $Img } from '../types/image' | ||
import { imageMeta } from './utils/meta' | ||
import { parseSize } from './utils' | ||
import { parseDensities, parseSize } from './utils' | ||
import { useStaticImageMap } from './utils/static-map' | ||
|
||
export function createImage (globalOptions: CreateImageOptions, nuxtContext: any) { | ||
|
@@ -161,8 +161,10 @@ function getPreset (ctx: ImageCTX, name?: string): ImageOptions { | |
function getSizes (ctx: ImageCTX, input: string, opts: ImageSizesOptions) { | ||
const width = parseSize(opts.modifiers?.width) | ||
const height = parseSize(opts.modifiers?.height) | ||
const densities = opts.densities ? parseDensities(opts.densities) : [1, 2] | ||
const hwRatio = (width && height) ? height / width : 0 | ||
const variants = [] | ||
const sizeVariants = [] | ||
const srcVariants = [] | ||
|
||
const sizes: Record<string, string> = {} | ||
|
||
|
@@ -195,25 +197,45 @@ function getSizes (ctx: ImageCTX, input: string, opts: ImageSizesOptions) { | |
_cWidth = Math.round((_cWidth / 100) * screenMaxWidth) | ||
} | ||
const _cHeight = hwRatio ? Math.round(_cWidth * hwRatio) : height | ||
variants.push({ | ||
sizeVariants.push({ | ||
width: _cWidth, | ||
size, | ||
screenMaxWidth, | ||
media: `(max-width: ${screenMaxWidth}px)`, | ||
src: ctx.$img!(input, { ...opts.modifiers, width: _cWidth, height: _cHeight }, opts) | ||
}) | ||
|
||
if (densities) { | ||
for (const density of densities) { | ||
srcVariants.push({ | ||
width: _cWidth * density, | ||
src: ctx.$img!(input, { ...opts.modifiers, width: _cWidth * density, height: _cHeight ? _cHeight * density : undefined }, opts) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
variants.sort((v1, v2) => v1.screenMaxWidth - v2.screenMaxWidth) | ||
sizeVariants.sort((v1, v2) => v1.screenMaxWidth - v2.screenMaxWidth) | ||
|
||
// Remove duplicate size variants, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about using const sizeVariantsUnique = [...new Set(sizeVariants)] |
||
let previousSize = '' | ||
|
||
const defaultVar = variants[variants.length - 1] | ||
if (defaultVar) { | ||
defaultVar.media = '' | ||
// Loop in reverse order to allow safe deletion | ||
for (let i = sizeVariants.length - 1; i >= 0; i--) { | ||
const sizeVariant = sizeVariants[i] | ||
if (sizeVariant.media === previousSize) { | ||
sizeVariants.splice(i, 1) | ||
} | ||
previousSize = sizeVariant.size | ||
} | ||
|
||
srcVariants.sort((v1, v2) => v1.width - v2.width) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: shouldn't srcVariants also be de-duplicated? |
||
|
||
const defaultVar = srcVariants[srcVariants.length - 1] | ||
|
||
return { | ||
sizes: variants.map(v => `${v.media ? v.media + ' ' : ''}${v.size}`).join(', '), | ||
srcset: variants.map(v => `${v.src} ${v.width}w`).join(', '), | ||
sizes: sizeVariants.map(v => `${v.media ? v.media + ' ' : ''}${v.size}`).join(', '), | ||
srcset: srcVariants.map(v => `${v.src} ${v.width}w`).join(', '), | ||
src: defaultVar?.src | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,3 +91,10 @@ export function parseSize (input: string | number | undefined = '') { | |
} | ||
} | ||
} | ||
|
||
export function parseDensities (input: string | undefined = '') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be more strictly typed to return How to deal with invalid input format, maybe it should throw/print some WARN messages and return |
||
if (!input.length) { | ||
return undefined | ||
} | ||
return input.split(' ').map(size => parseInt(size.replace('x', ''), 10)) | ||
} |
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.
this would mean a default of
1x 2x
?Not sure if 2x should be added by default. This could lead to confusion for many users. 🤔
Further, it would change the behaviour on upgrade.
Suggested improvement: allow to configure the default globally via nuxt.config.