Skip to content

Commit

Permalink
Update behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilyZhang777 committed Apr 22, 2024
1 parent c3c380a commit 292e9a0
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 130 deletions.
49 changes: 36 additions & 13 deletions static/js/formatters-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,31 +358,54 @@ export function image(simpleOrComplexImage = {}, desiredSize = '200x', atLeastAs
*/
function _getImageFormatOptions(desiredSize, atLeastAsLarge, fullSizeWidth, fullSizeHeight) {
let desiredDims = desiredSize.split('x');
let formatOptions = [];
const desiredWidthProvided = desiredDims[0] !== '' && desiredDims[0] !== '1';
const desiredHeightProvided = desiredDims[1] !== '' && desiredDims[1] !== '1';

if (desiredDims[0] !== '') {
const desiredWidth = Number.parseInt(desiredDims[0]);
// both dimensions are not provided, return original image
if (!desiredWidthProvided && !desiredHeightProvided) {
return '';
}

const originalRatio =
(!!fullSizeWidth && !!fullSizeHeight) ? (fullSizeWidth / fullSizeHeight) : undefined;
let desiredWidth;
let desiredHeight;
let formatOptions = ['fit=contain'];
if (desiredWidthProvided) {
desiredWidth = Number.parseInt(desiredDims[0]);
if (Number.isNaN(desiredWidth)) {
throw new Error("Invalid width specified");
}

formatOptions.push(`width=${desiredWidth}`);
} else if (!atLeastAsLarge && fullSizeWidth) {
formatOptions.push(`width=${fullSizeWidth}`);
}

if (desiredDims[1] !== '') {
const desiredHeight = Number.parseInt(desiredDims[1]);
if (desiredHeightProvided) {
desiredHeight = Number.parseInt(desiredDims[1]);
if (Number.isNaN(desiredHeight)) {
throw new Error("Invalid height specified");
}
}

// only width is provided
if (desiredWidthProvided && !desiredHeightProvided) {
formatOptions.push(`width=${desiredWidth}`);

return `/${formatOptions.join(',')}`;
}

// only height is provided
if (!desiredWidthProvided && desiredHeightProvided) {
formatOptions.push(`height=${desiredHeight}`);
} else if (!atLeastAsLarge && fullSizeHeight) {
formatOptions.push(`height=${fullSizeHeight}`);

return `/${formatOptions.join(',')}`;
}

formatOptions.push(`fit=${atLeastAsLarge ? 'cover' : 'contain'}`);
// both dimensions are provided
if (atLeastAsLarge && !!originalRatio) {
formatOptions.push(`width=${Math.max(desiredWidth, Math.round(desiredHeight * originalRatio))}`);
formatOptions.push(`height=${Math.max(desiredHeight, Math.round(desiredWidth / originalRatio))}`);
} else {
formatOptions.push(`width=${desiredWidth}`);
formatOptions.push(`height=${desiredHeight}`);
}

return `/${formatOptions.join(',')}`;
}
Expand Down
4 changes: 3 additions & 1 deletion test-site/scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ set_working_dir_to_test_site () {

set_working_dir_to_test_site

npx jambo build && npx grunt webpack
npx jambo build
chmod u+x ./public/static/node_modules/esbuild/bin/esbuild
npx grunt webpack

0 comments on commit 292e9a0

Please sign in to comment.