static-cms/src/components/Widgets/ImageControl.js

119 lines
2.9 KiB
JavaScript
Raw Normal View History

import React, { PropTypes } from 'react';
2016-06-07 21:27:34 -03:00
import { truncateMiddle } from '../../lib/textHelper';
2016-06-10 14:01:14 -03:00
import MediaProxy from '../../valueObjects/MediaProxy';
2016-05-30 16:55:32 -07:00
2016-06-07 20:15:28 -03:00
const MAX_DISPLAY_LENGTH = 50;
2016-05-30 16:55:32 -07:00
export default class ImageControl extends React.Component {
constructor(props) {
super(props);
2016-05-30 16:55:32 -07:00
this.handleChange = this.handleChange.bind(this);
this.handleFileInputRef = this.handleFileInputRef.bind(this);
this.handleClick = this.handleClick.bind(this);
this.handleDragEnter = this.handleDragEnter.bind(this);
this.handleDragOver = this.handleDragOver.bind(this);
this.renderImageName = this.renderImageName.bind(this);
}
handleFileInputRef(el) {
this._fileInput = el;
}
handleClick(e) {
this._fileInput.click();
}
handleDragEnter(e) {
e.stopPropagation();
e.preventDefault();
}
handleDragOver(e) {
e.stopPropagation();
e.preventDefault();
2016-05-30 16:55:32 -07:00
}
handleChange(e) {
e.stopPropagation();
e.preventDefault();
2016-06-10 14:01:14 -03:00
const fileList = e.dataTransfer ? e.dataTransfer.files : e.target.files;
const files = [...fileList];
const imageType = /^image\//;
2016-06-05 23:37:19 -03:00
// Iterate through the list of files and return the first image on the list
const file = files.find((currentFile) => {
if (imageType.test(currentFile.type)) {
return currentFile;
}
});
2016-06-10 14:01:14 -03:00
this.props.onRemoveMedia(this.props.value);
if (file) {
2016-06-10 14:01:14 -03:00
const mediaProxy = new MediaProxy(file.name, file);
this.props.onAddMedia(mediaProxy);
2016-07-19 17:11:22 -03:00
this.props.onChange(mediaProxy.path);
2016-06-10 00:16:01 -03:00
} else {
this.props.onChange(null);
}
}
renderImageName() {
2016-06-10 14:01:14 -03:00
if (!this.props.value) return null;
if (this.value instanceof MediaProxy) {
2016-07-19 17:11:22 -03:00
return truncateMiddle(this.props.value.path, MAX_DISPLAY_LENGTH);
2016-06-10 14:01:14 -03:00
} else {
return truncateMiddle(this.props.value, MAX_DISPLAY_LENGTH);
}
2016-06-10 00:16:01 -03:00
2016-05-30 16:55:32 -07:00
}
render() {
const imageName = this.renderImageName();
return (
<div
onDragEnter={this.handleDragEnter}
onDragOver={this.handleDragOver}
onDrop={this.handleChange}
>
<span style={styles.imageUpload} onClick={this.handleClick}>
{imageName ? imageName : 'Click here to upload from your file browser, or drag an image directly into this box from your desktop'}
</span>
<input
type="file"
accept="image/*"
onChange={this.handleChange}
style={styles.input}
ref={this.handleFileInputRef}
/>
</div>
);
2016-05-30 16:55:32 -07:00
}
}
const styles = {
input: {
display: 'none'
},
imageUpload: {
backgroundColor: '#fff',
textAlign: 'center',
color: '#888',
padding: '20px',
display: 'block',
margin: '10px',
border: '1px dashed #eee',
cursor: 'pointer',
fontSize: '12px'
}
};
ImageControl.propTypes = {
onAddMedia: PropTypes.func.isRequired,
onChange: PropTypes.func.isRequired,
onRemoveMedia: PropTypes.func.isRequired,
value: PropTypes.node,
};