Skip to content

Commit

Permalink
Merge pull request #4509 from cadriel/feature/file-thumbnails
Browse files Browse the repository at this point in the history
(feat) implements image thumbnails for the File type
  • Loading branch information
jossmac authored Feb 27, 2018
2 parents a6237b5 + 4058435 commit b847d19
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
1 change: 1 addition & 0 deletions fields/types/Type.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var DEFAULT_OPTION_KEYS = [
'collapse',
'dependsOn',
'autoCleanup',
'thumb',
];

/**
Expand Down
44 changes: 37 additions & 7 deletions fields/types/file/FileField.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '../../../admin/client/App/elemental';
import FileChangeMessage from '../../components/FileChangeMessage';
import HiddenFileInput from '../../components/HiddenFileInput';
import ImageThumbnail from '../../components/ImageThumbnail';

let uploadInc = 1000;

Expand All @@ -31,6 +32,7 @@ module.exports = Field.create({
label: PropTypes.string,
note: PropTypes.string,
path: PropTypes.string.isRequired,
thumb: PropTypes.bool,
value: PropTypes.shape({
filename: PropTypes.string,
// TODO: these are present but not used in the UI,
Expand Down Expand Up @@ -73,6 +75,13 @@ module.exports = Field.create({
? this.state.userSelectedFile.name
: this.props.value.filename;
},
getFileUrl () {
return this.props.value && this.props.value.url;
},
isImage () {
const href = this.props.value ? this.props.value.url : undefined;
return href && href.match(/\.(jpeg|jpg|gif|png|svg)$/i) != null;
},

// ==============================
// METHODS
Expand Down Expand Up @@ -190,23 +199,44 @@ module.exports = Field.create({
return null;
}
},
renderImagePreview () {
const imageSource = this.getFileUrl();
return (
<ImageThumbnail
component="a"
href={imageSource}
target="__blank"
style={{ float: 'left', marginRight: '1em', maxWidth: '50%' }}
>
<img src={imageSource} style={{ 'max-height': 100, 'max-width': '100%' }} />
</ImageThumbnail>
);
},
renderUI () {
const { label, note, path } = this.props;
const { label, note, path, thumb } = this.props;
const isImage = this.isImage();
const hasFile = this.hasFile();

const previews = (
<div style={(isImage && thumb) ? { marginBottom: '1em' } : null}>
{isImage && thumb && this.renderImagePreview()}
{hasFile && this.renderFileNameAndChangeMessage()}
</div>
);
const buttons = (
<div style={this.hasFile() ? { marginTop: '1em' } : null}>
<div style={hasFile ? { marginTop: '1em' } : null}>
<Button onClick={this.triggerFileBrowser}>
{this.hasFile() ? 'Change' : 'Upload'} File
{hasFile ? 'Change' : 'Upload'} File
</Button>
{this.hasFile() && this.renderClearButton()}
{hasFile && this.renderClearButton()}
</div>
);

return (
<div data-field-name={path} data-field-type="file">
<FormField label={label} htmlFor={path}>
{this.shouldRenderField() ? (
<div>
{this.hasFile() && this.renderFileNameAndChangeMessage()}
{previews}
{buttons}
<HiddenFileInput
key={this.state.uploadFieldPath}
Expand All @@ -218,7 +248,7 @@ module.exports = Field.create({
</div>
) : (
<div>
{this.hasFile()
{hasFile
? this.renderFileNameAndChangeMessage()
: <FormInput noedit>no file</FormInput>}
</div>
Expand Down

0 comments on commit b847d19

Please sign in to comment.