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

File: Image previews like for CloudinaryImage #4198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion fields/types/file/FileField.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
FormInput,
FormNote,
} from '../../../admin/client/App/elemental';
import ImageThumbnail from '../../components/ImageThumbnail';
import FileChangeMessage from '../../components/FileChangeMessage';
import HiddenFileInput from '../../components/HiddenFileInput';

Expand Down Expand Up @@ -65,6 +66,13 @@ module.exports = Field.create({
hasFile () {
return this.hasExisting() || !!this.state.userSelectedFile;
},
isImage() {
const href = this.props.value ? this.props.value.url : undefined;
return href && href.match(/\.(jpeg|jpg|gif|png)$/i) != null;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can add a svg in there as well…

},
getImageSource() {
return this.props.value && this.props.value.url;
},
hasExisting () {
return this.props.value && !!this.props.value.filename;
},
Expand Down Expand Up @@ -120,6 +128,20 @@ module.exports = Field.create({
// ==============================
// RENDERERS
// ==============================
renderImagePreview () {
const { value } = this.props;

return (
<ImageThumbnail
component="a"
href={this.getImageSource()}
target="__blank"
style={{ float: 'left', marginRight: '1em' }}
>
<img src={this.getImageSource()} style={{ height: 90 }} />
Copy link
Contributor

Choose a reason for hiding this comment

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

it would be cleaner to do const src = this.getImageSource() and then use that in both places

</ImageThumbnail>
);
},

renderFileNameAndChangeMessage () {
const href = this.props.value ? this.props.value.url : undefined;
Expand Down Expand Up @@ -201,12 +223,19 @@ module.exports = Field.create({
</div>
);

const imageContainer = (
<div style={this.isImage() ? { marginBottom: '1em' } : null}>
{this.isImage() && this.renderImagePreview()}
Copy link
Contributor

Choose a reason for hiding this comment

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

Same with isImage

{this.hasFile() && this.renderFileNameAndChangeMessage()}
</div>
);

return (
<div data-field-name={path} data-field-type="file">
<FormField label={label} htmlFor={path}>
{this.shouldRenderField() ? (
<div>
{this.hasFile() && this.renderFileNameAndChangeMessage()}
{imageContainer}
{buttons}
<HiddenFileInput
key={this.state.uploadFieldPath}
Expand Down