Improved handling when an embedded image cannot be loaded

Этот коммит содержится в:
hmhealey
2016-02-23 16:35:15 -05:00
родитель 9b50fb8553
Коммит b2e333e83c

Просмотреть файл

@@ -5,8 +5,12 @@ export default class PostImageEmbed extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.handleLoadComplete = this.handleLoadComplete.bind(this);
this.handleLoadError = this.handleLoadError.bind(this);
this.state = { this.state = {
loaded: false loaded: false,
errored: false
}; };
} }
@@ -17,7 +21,8 @@ export default class PostImageEmbed extends React.Component {
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
if (nextProps.link !== this.props.link) { if (nextProps.link !== this.props.link) {
this.setState({ this.setState({
loaded: false loaded: false,
errored: false
}); });
} }
} }
@@ -30,15 +35,29 @@ export default class PostImageEmbed extends React.Component {
loadImg(src) { loadImg(src) {
const img = new Image(); const img = new Image();
img.onload = () => { img.onload = this.handleLoadComplete;
this.setState({ img.onerror = this.handleLoadError;
loaded: true
});
};
img.src = src; img.src = src;
} }
handleLoadComplete() {
this.setState({
loaded: true
});
}
handleLoadError() {
this.setState({
errored: true,
loaded: true
});
}
render() { render() {
if (this.state.errored) {
return null;
}
if (!this.state.loaded) { if (!this.state.loaded) {
return ( return (
<img <img