2017-06-01 00:07:25 +09:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-06-27 20:43:53 +09:00
|
|
|
import classNames from 'classnames';
|
2017-06-01 00:07:25 +09:00
|
|
|
|
2017-06-24 02:36:54 +09:00
|
|
|
export default class ImageLoader extends React.PureComponent {
|
2017-06-01 00:07:25 +09:00
|
|
|
|
|
|
|
static propTypes = {
|
2017-06-15 21:10:41 +09:00
|
|
|
alt: PropTypes.string,
|
2017-06-01 00:07:25 +09:00
|
|
|
src: PropTypes.string.isRequired,
|
2017-11-25 23:41:45 +09:00
|
|
|
previewSrc: PropTypes.string,
|
2017-07-09 00:21:59 +09:00
|
|
|
width: PropTypes.number,
|
|
|
|
height: PropTypes.number,
|
2017-06-01 00:07:25 +09:00
|
|
|
}
|
|
|
|
|
2017-06-15 21:10:41 +09:00
|
|
|
static defaultProps = {
|
|
|
|
alt: '',
|
2017-07-09 00:21:59 +09:00
|
|
|
width: null,
|
|
|
|
height: null,
|
2017-06-15 21:10:41 +09:00
|
|
|
};
|
|
|
|
|
2017-06-01 00:07:25 +09:00
|
|
|
state = {
|
|
|
|
loading: true,
|
|
|
|
error: false,
|
|
|
|
}
|
|
|
|
|
2017-06-27 20:43:53 +09:00
|
|
|
removers = [];
|
|
|
|
|
|
|
|
get canvasContext() {
|
|
|
|
if (!this.canvas) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
this._canvasContext = this._canvasContext || this.canvas.getContext('2d');
|
|
|
|
return this._canvasContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
this.loadImage(this.props);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
if (this.props.src !== nextProps.src) {
|
|
|
|
this.loadImage(nextProps);
|
|
|
|
}
|
2017-06-01 00:07:25 +09:00
|
|
|
}
|
|
|
|
|
2017-06-27 20:43:53 +09:00
|
|
|
loadImage (props) {
|
|
|
|
this.removeEventListeners();
|
|
|
|
this.setState({ loading: true, error: false });
|
|
|
|
Promise.all([
|
2017-11-25 23:41:45 +09:00
|
|
|
props.previewSrc && this.loadPreviewCanvas(props),
|
2017-07-09 00:21:59 +09:00
|
|
|
this.hasSize() && this.loadOriginalImage(props),
|
|
|
|
].filter(Boolean))
|
2017-06-27 20:43:53 +09:00
|
|
|
.then(() => {
|
|
|
|
this.setState({ loading: false, error: false });
|
|
|
|
this.clearPreviewCanvas();
|
|
|
|
})
|
|
|
|
.catch(() => this.setState({ loading: false, error: true }));
|
2017-06-01 00:07:25 +09:00
|
|
|
}
|
|
|
|
|
2017-06-27 20:43:53 +09:00
|
|
|
loadPreviewCanvas = ({ previewSrc, width, height }) => new Promise((resolve, reject) => {
|
2017-06-01 00:07:25 +09:00
|
|
|
const image = new Image();
|
2017-06-27 20:43:53 +09:00
|
|
|
const removeEventListeners = () => {
|
|
|
|
image.removeEventListener('error', handleError);
|
|
|
|
image.removeEventListener('load', handleLoad);
|
|
|
|
};
|
|
|
|
const handleError = () => {
|
|
|
|
removeEventListeners();
|
|
|
|
reject();
|
|
|
|
};
|
|
|
|
const handleLoad = () => {
|
|
|
|
removeEventListeners();
|
|
|
|
this.canvasContext.drawImage(image, 0, 0, width, height);
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
image.addEventListener('error', handleError);
|
|
|
|
image.addEventListener('load', handleLoad);
|
|
|
|
image.src = previewSrc;
|
|
|
|
this.removers.push(removeEventListeners);
|
|
|
|
})
|
2017-06-07 02:30:17 +09:00
|
|
|
|
2017-06-27 20:43:53 +09:00
|
|
|
clearPreviewCanvas () {
|
|
|
|
const { width, height } = this.canvas;
|
|
|
|
this.canvasContext.clearRect(0, 0, width, height);
|
|
|
|
}
|
2017-06-07 02:30:17 +09:00
|
|
|
|
2017-06-27 20:43:53 +09:00
|
|
|
loadOriginalImage = ({ src }) => new Promise((resolve, reject) => {
|
|
|
|
const image = new Image();
|
|
|
|
const removeEventListeners = () => {
|
|
|
|
image.removeEventListener('error', handleError);
|
|
|
|
image.removeEventListener('load', handleLoad);
|
|
|
|
};
|
|
|
|
const handleError = () => {
|
|
|
|
removeEventListeners();
|
|
|
|
reject();
|
|
|
|
};
|
|
|
|
const handleLoad = () => {
|
|
|
|
removeEventListeners();
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
image.addEventListener('error', handleError);
|
|
|
|
image.addEventListener('load', handleLoad);
|
2017-06-01 00:07:25 +09:00
|
|
|
image.src = src;
|
2017-06-27 20:43:53 +09:00
|
|
|
this.removers.push(removeEventListeners);
|
|
|
|
});
|
2017-06-07 02:30:17 +09:00
|
|
|
|
2017-06-27 20:43:53 +09:00
|
|
|
removeEventListeners () {
|
|
|
|
this.removers.forEach(listeners => listeners());
|
|
|
|
this.removers = [];
|
2017-06-01 00:07:25 +09:00
|
|
|
}
|
|
|
|
|
2017-07-09 00:21:59 +09:00
|
|
|
hasSize () {
|
|
|
|
const { width, height } = this.props;
|
|
|
|
return typeof width === 'number' && typeof height === 'number';
|
|
|
|
}
|
|
|
|
|
2017-06-27 20:43:53 +09:00
|
|
|
setCanvasRef = c => {
|
|
|
|
this.canvas = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { alt, src, width, height } = this.props;
|
2017-06-23 23:05:04 +09:00
|
|
|
const { loading } = this.state;
|
2017-06-01 00:07:25 +09:00
|
|
|
|
2017-06-27 20:43:53 +09:00
|
|
|
const className = classNames('image-loader', {
|
|
|
|
'image-loader--loading': loading,
|
2017-07-09 00:21:59 +09:00
|
|
|
'image-loader--amorphous': !this.hasSize(),
|
2017-06-27 20:43:53 +09:00
|
|
|
});
|
|
|
|
|
2017-06-07 02:30:17 +09:00
|
|
|
return (
|
2017-06-27 20:43:53 +09:00
|
|
|
<div className={className}>
|
|
|
|
<canvas
|
|
|
|
className='image-loader__preview-canvas'
|
2017-06-07 02:30:17 +09:00
|
|
|
width={width}
|
|
|
|
height={height}
|
2017-06-27 20:43:53 +09:00
|
|
|
ref={this.setCanvasRef}
|
2017-07-10 01:49:07 +09:00
|
|
|
style={{ opacity: loading ? 1 : 0 }}
|
2017-06-07 02:30:17 +09:00
|
|
|
/>
|
|
|
|
|
2017-06-27 20:43:53 +09:00
|
|
|
{!loading && (
|
2017-06-15 21:10:41 +09:00
|
|
|
<img
|
2017-06-27 20:43:53 +09:00
|
|
|
alt={alt}
|
|
|
|
className='image-loader__img'
|
|
|
|
src={src}
|
|
|
|
width={width}
|
|
|
|
height={height}
|
2017-06-07 02:30:17 +09:00
|
|
|
/>
|
2017-06-27 20:43:53 +09:00
|
|
|
)}
|
2017-06-07 02:30:17 +09:00
|
|
|
</div>
|
|
|
|
);
|
2017-06-01 00:07:25 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|