1x case handling
This commit is contained in:
@@ -8,23 +8,31 @@ export interface Props {
|
|||||||
width: number;
|
width: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface SrcSetInfo {
|
||||||
|
srcSet: string;
|
||||||
|
bestSrc: string;
|
||||||
|
}
|
||||||
|
|
||||||
export class Picture extends React.PureComponent<Props, {}> {
|
export class Picture extends React.PureComponent<Props, {}> {
|
||||||
static displayName = "Picture";
|
static displayName = "Picture";
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const src = `img/600/${this.props.image.src}`;
|
const srcSet = this._srcset();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<img
|
<img
|
||||||
onClick={this.props.onClick}
|
onClick={this.props.onClick}
|
||||||
srcSet={this._srcset()}
|
srcSet={srcSet.srcSet}
|
||||||
src={src}
|
src={srcSet.bestSrc}
|
||||||
width={this.props.width + "px"}
|
width={this.props.width + "px"}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private _srcset = (): string => {
|
private _srcset = (): SrcSetInfo => {
|
||||||
const srcs: string[] = [];
|
const srcs: string[] = [];
|
||||||
|
let bestSize = 1600;
|
||||||
|
let bestRatio = Infinity;
|
||||||
|
|
||||||
Model.SIZES.forEach(size => {
|
Model.SIZES.forEach(size => {
|
||||||
const width =
|
const width =
|
||||||
@@ -36,13 +44,18 @@ export class Picture extends React.PureComponent<Props, {}> {
|
|||||||
|
|
||||||
if (scale >= 1) {
|
if (scale >= 1) {
|
||||||
srcs.push(`img/${size}/${this.props.image.src} ${scale}x`);
|
srcs.push(`img/${size}/${this.props.image.src} ${scale}x`);
|
||||||
|
if (scale < bestRatio) {
|
||||||
|
bestSize = size;
|
||||||
|
bestRatio = scale;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (srcs.length === 0) {
|
srcs.push(`img/${bestSize}/${this.props.image.src} 1x`);
|
||||||
return `img/1600/${this.props.image.src} 1x`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return srcs.join(",");
|
return {
|
||||||
|
srcSet: srcs.join(","),
|
||||||
|
bestSrc: `img/${bestSize}/${this.props.image.src}`
|
||||||
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user