Skip to content
This repository was archived by the owner on Jan 19, 2023. It is now read-only.

Commit b37f2cb

Browse files
committed
Perform some API fixes on satya-dash's proposal
1 parent 50a87fe commit b37f2cb

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ nextSrcThumbnail | string | | | Thumbnail image url c
9292
onCloseRequest | func | | yes | Close window event. Should change the parent state such that the lightbox is not rendered
9393
onMovePrevRequest | func | empty function | | Move to previous image event. Should change the parent state such that props.prevSrc becomes props.mainSrc, props.mainSrc becomes props.nextSrc, etc.
9494
onMoveNextRequest | func | empty function | | Move to next image event. Should change the parent state such that props.nextSrc becomes props.mainSrc, props.mainSrc becomes props.prevSrc, etc.
95-
onLoadError | func | error object | | Will listen to the error if image is not loading
95+
onImageLoadError | func | empty function | | Called when an image fails to load.<div>`(imageSrc: string, srcType: string, errorEvent: object): void`</div>
9696
discourageDownloads | bool | `false` | | Enable download discouragement (prevents [right-click -> Save Image As...])
9797
animationDisabled | bool | `false` | | Disable all animation
9898
animationOnKeyInput | bool | `false` | | Disable animation on actions performed with keyboard shortcuts

src/examples/cats/app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ const App = React.createClass({
100100
movePrev() {
101101
this.setState({ index: (this.state.index + images.length - 1) % images.length });
102102
},
103+
onImageLoadError(imageSrc, _srcType, errorEvent) {
104+
console.error(`Could not load image at ${imageSrc}`, errorEvent);
105+
},
103106
render() {
104107
let lightbox;
105108
if (this.state.isOpen) {
@@ -116,6 +119,7 @@ const App = React.createClass({
116119
onCloseRequest={this.closeLightbox}
117120
onMovePrevRequest={this.movePrev}
118121
onMoveNextRequest={this.moveNext}
122+
onImageLoadError={this.onImageLoadError}
119123

120124
imageTitle={titles[this.state.index]}
121125
imageCaption={captions[this.state.index]}

src/react-image-lightbox.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -705,21 +705,21 @@ class ReactImageLightbox extends Component {
705705
}
706706

707707
// Load image from src and call callback with image width and height on load
708-
loadImage(imageSrc, callback) {
708+
loadImage(srcType, imageSrc, done) {
709709
// Return the image info if it is already cached
710710
if (this.isImageLoaded(imageSrc)) {
711711
setTimeout(() => {
712-
callback(null, this.imageCache[imageSrc].width, this.imageCache[imageSrc].height);
712+
done();
713713
}, 1);
714714
return;
715715
}
716716

717717
const that = this;
718718
const inMemoryImage = new Image();
719719

720-
inMemoryImage.onerror = () => {
721-
this.props.onLoadError(new Error('image load error'));
722-
callback('image load error');
720+
inMemoryImage.onerror = (errorEvent) => {
721+
this.props.onImageLoadError(imageSrc, srcType, errorEvent);
722+
done(errorEvent);
723723
};
724724

725725
inMemoryImage.onload = function onLoad() {
@@ -729,20 +729,17 @@ class ReactImageLightbox extends Component {
729729
height: this.height,
730730
};
731731

732-
callback(null, this.width, this.height);
732+
done();
733733
};
734734

735735
inMemoryImage.src = imageSrc;
736736
}
737737

738738
// Load all images and their thumbnails
739739
loadAllImages(props = this.props) {
740-
const generateImageLoadedCallback = (srcType, imageSrc) => (err) => {
740+
const generateLoadDoneCallback = (srcType, imageSrc) => (err) => {
741741
// Give up showing image on error
742742
if (err) {
743-
if (window.console) {
744-
window.console.warn(err);
745-
}
746743
return;
747744
}
748745

@@ -762,7 +759,7 @@ class ReactImageLightbox extends Component {
762759

763760
// Load unloaded images
764761
if (props[type] && !this.isImageLoaded(props[type])) {
765-
this.loadImage(props[type], generateImageLoadedCallback(type, props[type]));
762+
this.loadImage(type, props[type], generateLoadDoneCallback(type, props[type]));
766763
}
767764
});
768765
}
@@ -1237,8 +1234,9 @@ ReactImageLightbox.propTypes = {
12371234
// props.mainSrc becomes props.prevSrc, etc.
12381235
onMoveNextRequest: PropTypes.func,
12391236

1240-
//Listener if any error occurs while loading the image
1241-
onLoadError: PropTypes.func,
1237+
// Called when an image fails to load
1238+
// (imageSrc: string, srcType: string, errorEvent: object): void
1239+
onImageLoadError: PropTypes.func,
12421240

12431241
//-----------------------------
12441242
// Download discouragement settings
@@ -1309,7 +1307,7 @@ ReactImageLightbox.propTypes = {
13091307
ReactImageLightbox.defaultProps = {
13101308
onMovePrevRequest: () => {},
13111309
onMoveNextRequest: () => {},
1312-
onLoadError: () => {},
1310+
onImageLoadError: () => {},
13131311

13141312
discourageDownloads: false,
13151313

0 commit comments

Comments
 (0)