Skip to content
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

Fix resizes on image upload #8928

Merged
merged 11 commits into from
Jun 3, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const ImageRenderer = (props) => {
Config.EXPENSIFY.URL_API_ROOT,
);

const imageWidth = htmlAttribs['data-expensify-width'] ? parseInt(htmlAttribs['data-expensify-width'], 10) : undefined;
const imageHeight = htmlAttribs['data-expensify-height'] ? parseInt(htmlAttribs['data-expensify-height'], 10) : undefined;
Comment on lines +47 to +48
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if data-expensify-width is set to "0", it breaks PDF rendering. #33626


return (
<AttachmentModal
allowDownload
Expand All @@ -60,6 +63,8 @@ const ImageRenderer = (props) => {
previewSourceURL={previewSource}
style={styles.webViewStyles.tagStyles.img}
isAuthTokenRequired={isAttachment}
imageWidth={imageWidth}
imageHeight={imageHeight}
/>
</PressableWithoutFocus>
)}
Expand Down
32 changes: 27 additions & 5 deletions src/components/ThumbnailImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,44 @@ const propTypes = {
/** Do the urls require an authToken? */
isAuthTokenRequired: PropTypes.bool.isRequired,

/** Width of the thumbnail image */
imageWidth: PropTypes.number,

/** Height of the thumbnail image */
imageHeight: PropTypes.number,

...windowDimensionsPropTypes,
};

const defaultProps = {
style: {},
imageWidth: 200,
imageHeight: 200,
};

class ThumbnailImage extends PureComponent {
constructor(props) {
super(props);

this.updateImageSize = this.updateImageSize.bind(this);

const {thumbnailWidth, thumbnailHeight} = this.calculateThumbnailImageSize(props.imageWidth, props.imageHeight);
this.state = {
thumbnailWidth: 200,
thumbnailHeight: 200,
thumbnailWidth,
thumbnailHeight,
};
}

updateImageSize({width, height}) {
/**
* Compute the thumbnails width and height given original image dimensions.
*
* @param {Number} width - Width of the original image.
* @param {Number} height - Height of the original image.
* @returns {Object} - Object containing thumbnails width and height.
*/

allroundexperts marked this conversation as resolved.
Show resolved Hide resolved
calculateThumbnailImageSize(width, height) {
if (!width || !height) { return {}; }
allroundexperts marked this conversation as resolved.
Show resolved Hide resolved
allroundexperts marked this conversation as resolved.
Show resolved Hide resolved

// Width of the thumbnail works better as a constant than it does
// a percentage of the screen width since it is relative to each screen
// Note: Clamp minimum width 40px to support touch device
Expand All @@ -54,8 +72,12 @@ class ThumbnailImage extends PureComponent {
} else {
thumbnailScreenHeight = Math.round(thumbnailScreenWidth * aspectRatio);
}
return {thumbnailWidth: thumbnailScreenWidth, thumbnailHeight: Math.max(40, thumbnailScreenHeight)};
}

this.setState({thumbnailWidth: thumbnailScreenWidth, thumbnailHeight: Math.max(40, thumbnailScreenHeight)});
updateImageSize({width, height}) {
allroundexperts marked this conversation as resolved.
Show resolved Hide resolved
const {thumbnailWidth, thumbnailHeight} = this.calculateThumbnailImageSize(width, height);
this.setState({thumbnailWidth, thumbnailHeight});
}

render() {
Expand Down
26 changes: 8 additions & 18 deletions src/pages/home/report/ReportActionItemFragment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {memo} from 'react';
import {ActivityIndicator, ImageBackground, View} from 'react-native';
import {ActivityIndicator, View} from 'react-native';
import PropTypes from 'prop-types';
import Str from 'expensify-common/lib/str';
import reportActionFragmentPropTypes from './reportActionFragmentPropTypes';
Expand Down Expand Up @@ -72,28 +72,18 @@ const ReportActionItemFragment = (props) => {
// If this is an attachment placeholder, return the placeholder component
if (props.isAttachment && props.loading) {
return (
<View style={[styles.chatItemAttachmentPlaceholder]}>
{Str.isImage(props.attachmentInfo.name)
? (
<ImageBackground
source={{uri: props.attachmentInfo.source}}
resizeMode="cover"
imageStyle={[styles.borderBottomRounded, styles.borderTopRounded]}
style={[styles.flex1, styles.justifyContentCenter, styles.alignItemsCenter]}
>
<ActivityIndicator
size="large"
color={themeColors.uploadPreviewActivityIndicator}
/>
</ImageBackground>
) : (
Str.isImage(props.attachmentInfo.name)
? (
<RenderHTML html={`<comment><img src="${props.attachmentInfo.source}"/></comment>`} />
) : (
<View style={[styles.chatItemAttachmentPlaceholder]}>
<ActivityIndicator
size="large"
color={themeColors.textSupporting}
style={[styles.flex1]}
/>
)}
</View>
</View>
)
);
}

Expand Down