2017-06-27 04:43:53 -07:00
|
|
|
import classNames from 'classnames';
|
2022-07-20 04:56:44 -07:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React, { PureComponent } from 'react';
|
2018-05-20 12:38:12 -07:00
|
|
|
import { LoadingBar } from 'react-redux-loading-bar';
|
2018-04-08 14:15:25 -07:00
|
|
|
import ZoomableImage from './zoomable_image';
|
2017-05-31 08:07:25 -07:00
|
|
|
|
2022-07-20 04:56:44 -07:00
|
|
|
export default class ImageLoader extends PureComponent {
|
2017-05-31 08:07:25 -07:00
|
|
|
|
|
|
|
static propTypes = {
|
2017-06-15 05:10:41 -07:00
|
|
|
alt: PropTypes.string,
|
2023-02-26 11:13:27 -08:00
|
|
|
lang: PropTypes.string,
|
2017-05-31 08:07:25 -07:00
|
|
|
src: PropTypes.string.isRequired,
|
2018-04-08 14:06:33 -07:00
|
|
|
previewSrc: PropTypes.string,
|
2017-07-08 08:21:59 -07:00
|
|
|
width: PropTypes.number,
|
|
|
|
height: PropTypes.number,
|
2018-04-08 14:15:25 -07:00
|
|
|
onClick: PropTypes.func,
|
2020-11-02 12:16:38 -08:00
|
|
|
zoomButtonHidden: PropTypes.bool,
|
2023-02-03 11:52:07 -08:00
|
|
|
};
|
2017-05-31 08:07:25 -07:00
|
|
|
|
2017-06-15 05:10:41 -07:00
|
|
|
static defaultProps = {
|
|
|
|
alt: '',
|
2023-02-26 11:13:27 -08:00
|
|
|
lang: '',
|
2017-07-08 08:21:59 -07:00
|
|
|
width: null,
|
|
|
|
height: null,
|
2017-06-15 05:10:41 -07:00
|
|
|
};
|
|
|
|
|
2017-05-31 08:07:25 -07:00
|
|
|
state = {
|
|
|
|
loading: true,
|
|
|
|
error: false,
|
2018-05-20 12:38:12 -07:00
|
|
|
width: null,
|
2023-02-03 11:52:07 -08:00
|
|
|
};
|
2017-05-31 08:07:25 -07:00
|
|
|
|
2017-06-27 04:43:53 -07:00
|
|
|
removers = [];
|
2018-04-08 14:15:25 -07:00
|
|
|
canvas = null;
|
2017-06-27 04:43:53 -07:00
|
|
|
|
|
|
|
get canvasContext() {
|
|
|
|
if (!this.canvas) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
this._canvasContext = this._canvasContext || this.canvas.getContext('2d');
|
|
|
|
return this._canvasContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
this.loadImage(this.props);
|
|
|
|
}
|
|
|
|
|
2022-07-20 04:56:44 -07:00
|
|
|
UNSAFE_componentWillReceiveProps (nextProps) {
|
2017-06-27 04:43:53 -07:00
|
|
|
if (this.props.src !== nextProps.src) {
|
|
|
|
this.loadImage(nextProps);
|
|
|
|
}
|
2017-05-31 08:07:25 -07:00
|
|
|
}
|
|
|
|
|
2018-04-08 14:15:25 -07:00
|
|
|
componentWillUnmount () {
|
|
|
|
this.removeEventListeners();
|
|
|
|
}
|
|
|
|
|
2017-06-27 04:43:53 -07:00
|
|
|
loadImage (props) {
|
|
|
|
this.removeEventListeners();
|
|
|
|
this.setState({ loading: true, error: false });
|
|
|
|
Promise.all([
|
2018-04-08 14:06:33 -07:00
|
|
|
props.previewSrc && this.loadPreviewCanvas(props),
|
2017-07-08 08:21:59 -07:00
|
|
|
this.hasSize() && this.loadOriginalImage(props),
|
|
|
|
].filter(Boolean))
|
2017-06-27 04:43:53 -07:00
|
|
|
.then(() => {
|
|
|
|
this.setState({ loading: false, error: false });
|
|
|
|
this.clearPreviewCanvas();
|
|
|
|
})
|
|
|
|
.catch(() => this.setState({ loading: false, error: true }));
|
2017-05-31 08:07:25 -07:00
|
|
|
}
|
|
|
|
|
2017-06-27 04:43:53 -07:00
|
|
|
loadPreviewCanvas = ({ previewSrc, width, height }) => new Promise((resolve, reject) => {
|
2017-05-31 08:07:25 -07:00
|
|
|
const image = new Image();
|
2017-06-27 04:43:53 -07: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);
|
2023-02-03 11:52:07 -08:00
|
|
|
});
|
2017-06-06 10:30:17 -07:00
|
|
|
|
2017-06-27 04:43:53 -07:00
|
|
|
clearPreviewCanvas () {
|
|
|
|
const { width, height } = this.canvas;
|
|
|
|
this.canvasContext.clearRect(0, 0, width, height);
|
|
|
|
}
|
2017-06-06 10:30:17 -07:00
|
|
|
|
2017-06-27 04:43:53 -07: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-05-31 08:07:25 -07:00
|
|
|
image.src = src;
|
2017-06-27 04:43:53 -07:00
|
|
|
this.removers.push(removeEventListeners);
|
|
|
|
});
|
2017-06-06 10:30:17 -07:00
|
|
|
|
2017-06-27 04:43:53 -07:00
|
|
|
removeEventListeners () {
|
|
|
|
this.removers.forEach(listeners => listeners());
|
|
|
|
this.removers = [];
|
2017-05-31 08:07:25 -07:00
|
|
|
}
|
|
|
|
|
2017-07-08 08:21:59 -07:00
|
|
|
hasSize () {
|
|
|
|
const { width, height } = this.props;
|
|
|
|
return typeof width === 'number' && typeof height === 'number';
|
|
|
|
}
|
|
|
|
|
2017-06-27 04:43:53 -07:00
|
|
|
setCanvasRef = c => {
|
|
|
|
this.canvas = c;
|
2018-05-20 12:38:12 -07:00
|
|
|
if (c) this.setState({ width: c.offsetWidth });
|
2023-02-03 11:52:07 -08:00
|
|
|
};
|
2017-06-27 04:43:53 -07:00
|
|
|
|
|
|
|
render () {
|
2023-02-26 11:13:27 -08:00
|
|
|
const { alt, lang, src, width, height, onClick } = this.props;
|
2017-06-23 07:05:04 -07:00
|
|
|
const { loading } = this.state;
|
2017-05-31 08:07:25 -07:00
|
|
|
|
2017-06-27 04:43:53 -07:00
|
|
|
const className = classNames('image-loader', {
|
|
|
|
'image-loader--loading': loading,
|
2017-07-08 08:21:59 -07:00
|
|
|
'image-loader--amorphous': !this.hasSize(),
|
2017-06-27 04:43:53 -07:00
|
|
|
});
|
|
|
|
|
2017-06-06 10:30:17 -07:00
|
|
|
return (
|
2017-06-27 04:43:53 -07:00
|
|
|
<div className={className}>
|
2018-04-08 14:15:25 -07:00
|
|
|
{loading ? (
|
2022-07-20 04:56:44 -07:00
|
|
|
<>
|
|
|
|
<div className='loading-bar__container' style={{ width: this.state.width || width }}>
|
|
|
|
<LoadingBar className='loading-bar' loading={1} />
|
|
|
|
</div>
|
|
|
|
<canvas
|
|
|
|
className='image-loader__preview-canvas'
|
|
|
|
ref={this.setCanvasRef}
|
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
/>
|
|
|
|
</>
|
2018-04-08 14:15:25 -07:00
|
|
|
) : (
|
|
|
|
<ZoomableImage
|
|
|
|
alt={alt}
|
2023-02-26 11:13:27 -08:00
|
|
|
lang={lang}
|
2018-04-08 14:15:25 -07:00
|
|
|
src={src}
|
|
|
|
onClick={onClick}
|
2020-11-02 12:16:38 -08:00
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
zoomButtonHidden={this.props.zoomButtonHidden}
|
2018-04-08 14:15:25 -07:00
|
|
|
/>
|
2017-06-27 04:43:53 -07:00
|
|
|
)}
|
2017-06-06 10:30:17 -07:00
|
|
|
</div>
|
|
|
|
);
|
2017-05-31 08:07:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|