Skip to content

Commit 31b31f6

Browse files
author
Jones Warren
committed
feat:support onError
1 parent f02b7df commit 31b31f6

File tree

7 files changed

+11203
-75
lines changed

7 files changed

+11203
-75
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ examples
55
.gitignore
66
webpack.config.js
77
.package-lock.json
8-
./node_modules
8+
./node_modules
9+
node_modules

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ static propTypes = {
9595
//when the image loaded successfully
9696
loaded: PropTypes.func,
9797
//onImageClick
98-
onImageClick: PropTypes.func
98+
onImageClick: PropTypes.func,
99+
onError:PropTypes.func,
99100
};
100101

101102
static defaultProps = {

dist/components/ProgressiveLazyImage.js

Lines changed: 74 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,54 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6+
exports["default"] = void 0;
67

7-
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8+
var _react = _interopRequireDefault(require("react"));
89

9-
var _react = require("react");
10+
var _reactDom = _interopRequireDefault(require("react-dom"));
1011

11-
var _react2 = _interopRequireDefault(_react);
12+
var _propTypes = _interopRequireDefault(require("prop-types"));
1213

13-
var _reactDom = require("react-dom");
14+
var _intersectionObserverPolyfill = _interopRequireDefault(require("intersection-observer-polyfill"));
1415

15-
var _reactDom2 = _interopRequireDefault(_reactDom);
16+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
1617

17-
var _propTypes = require("prop-types");
18+
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
1819

19-
var _propTypes2 = _interopRequireDefault(_propTypes);
20-
21-
var _intersectionObserverPolyfill = require("intersection-observer-polyfill");
20+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2221

23-
var _intersectionObserverPolyfill2 = _interopRequireDefault(_intersectionObserverPolyfill);
22+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
2423

25-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
2625

27-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
26+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
2827

29-
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
28+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
3029

31-
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } //inspirations from https://github.com/FormidableLabs/react-progressive-image
30+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
3231

33-
//这是intersection的polyfill
32+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
3433

34+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
3535

36-
var ProgressiveLazyImage = function (_React$Component) {
36+
var ProgressiveLazyImage =
37+
/*#__PURE__*/
38+
function (_React$Component) {
3739
_inherits(ProgressiveLazyImage, _React$Component);
3840

3941
function ProgressiveLazyImage(props) {
40-
_classCallCheck(this, ProgressiveLazyImage);
42+
var _this;
4143

42-
var _this = _possibleConstructorReturn(this, (ProgressiveLazyImage.__proto__ || Object.getPrototypeOf(ProgressiveLazyImage)).apply(this, arguments));
44+
_classCallCheck(this, ProgressiveLazyImage);
4345

46+
_this = _possibleConstructorReturn(this, _getPrototypeOf(ProgressiveLazyImage).apply(this, arguments));
4447
_this.state = _this._getInitialState(props);
45-
_this.loadImage = _this.loadImage.bind(_this);
46-
_this.onLoad = _this.onLoad.bind(_this);
47-
_this.setImageWithDelay = _this.setImageWithDelay.bind(_this);
48-
_this.setImage = _this.setImage.bind(_this);
49-
// this.checkAndLoad = this.checkAndLoad.bind(this);
50-
_this.insideViewportCb = _this.insideViewportCb.bind(_this);
48+
_this.loadImage = _this.loadImage.bind(_assertThisInitialized(_this));
49+
_this.onLoad = _this.onLoad.bind(_assertThisInitialized(_this));
50+
_this.setImageWithDelay = _this.setImageWithDelay.bind(_assertThisInitialized(_this));
51+
_this.setImage = _this.setImage.bind(_assertThisInitialized(_this)); // this.checkAndLoad = this.checkAndLoad.bind(this);
52+
53+
_this.insideViewportCb = _this.insideViewportCb.bind(_assertThisInitialized(_this));
5154
return _this;
5255
}
5356

@@ -83,16 +86,17 @@ var ProgressiveLazyImage = function (_React$Component) {
8386
}, {
8487
key: "componentDidMount",
8588
value: function componentDidMount() {
86-
var _props = this.props,
87-
src = _props.src,
88-
needLazyUtilInViewPort = _props.needLazyUtilInViewPort,
89-
canLoadRightNow = _props.canLoadRightNow;
89+
var _this$props = this.props,
90+
src = _this$props.src,
91+
needLazyUtilInViewPort = _this$props.needLazyUtilInViewPort,
92+
canLoadRightNow = _this$props.canLoadRightNow;
9093

9194
if (needLazyUtilInViewPort) {
9295
//https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
9396
try {
94-
var node = _reactDom2.default.findDOMNode(this);
95-
this.observer = new _intersectionObserverPolyfill2.default(this.insideViewportCb);
97+
var node = _reactDom["default"].findDOMNode(this);
98+
99+
this.observer = new _intersectionObserverPolyfill["default"](this.insideViewportCb);
96100
this.observer.observe(node);
97101
} catch (err) {
98102
console.log("err in finding node", err);
@@ -108,11 +112,12 @@ var ProgressiveLazyImage = function (_React$Component) {
108112
value: function componentDidUpdate(prevProps) {
109113
var _this3 = this;
110114

111-
var src = this.props.src;
112-
// We only invalidate the current image if the src has changed.
115+
var src = this.props.src; // We only invalidate the current image if the src has changed.
113116

114117
if (src !== prevProps.src) {
115-
this.setState({ loading: true }, function () {
118+
this.setState({
119+
loading: true
120+
}, function () {
116121
_this3.loadImage(src);
117122
});
118123
}
@@ -129,13 +134,19 @@ var ProgressiveLazyImage = function (_React$Component) {
129134
this.image.onload = null;
130135
this.image.onerror = null;
131136
}
137+
132138
var image = new Image();
133139
this.image = image;
134140
image.onload = this.onLoad;
141+
135142
image.onerror = function (e) {
136143
console.error("image on error", e);
137-
_this4.setState({ error: true });
144+
145+
_this4.setState({
146+
error: true
147+
});
138148
};
149+
139150
image.src = src;
140151
}
141152
}, {
@@ -145,9 +156,11 @@ var ProgressiveLazyImage = function (_React$Component) {
145156
this.image.onload = null;
146157
this.image.onerror = null;
147158
}
159+
148160
if (this.delayTask) {
149161
clearTimeout(this.delayTask);
150162
}
163+
151164
if (this.observer) {
152165
this.observer = null;
153166
}
@@ -192,40 +205,43 @@ var ProgressiveLazyImage = function (_React$Component) {
192205
}, {
193206
key: "render",
194207
value: function render() {
195-
var _state = this.state,
196-
showingImage = _state.showingImage,
197-
loading = _state.loading,
198-
error = _state.error;
199-
var _props2 = this.props,
200-
placeholder = _props2.placeholder,
201-
className = _props2.className,
202-
onImageClick = _props2.onImageClick;
203-
204-
return loading || error ? placeholder : _react2.default.createElement("img", { className: className, src: showingImage, onClick: onImageClick });
208+
var _this$state = this.state,
209+
showingImage = _this$state.showingImage,
210+
loading = _this$state.loading,
211+
error = _this$state.error;
212+
var _this$props2 = this.props,
213+
placeholder = _this$props2.placeholder,
214+
className = _this$props2.className,
215+
onImageClick = _this$props2.onImageClick,
216+
onError = _this$props2.onError;
217+
return loading || error ? placeholder : _react["default"].createElement("img", {
218+
className: className,
219+
src: showingImage,
220+
onClick: onImageClick,
221+
onError: onError
222+
});
205223
}
206224
}]);
207225

208226
return ProgressiveLazyImage;
209-
}(_react2.default.Component);
210-
211-
exports.default = ProgressiveLazyImage;
212-
227+
}(_react["default"].Component);
213228

229+
exports["default"] = ProgressiveLazyImage;
214230
ProgressiveLazyImage.propTypes = {
215-
src: _propTypes2.default.string.isRequired,
231+
src: _propTypes["default"].string.isRequired,
216232
//默认图片占位
217-
placeholder: _propTypes2.default.object.isRequired,
218-
delay: _propTypes2.default.number,
219-
className: _propTypes2.default.string,
233+
placeholder: _propTypes["default"].object.isRequired,
234+
delay: _propTypes["default"].number,
235+
className: _propTypes["default"].string,
220236
//在viewport里面才加载
221-
needLazyUtilInViewPort: _propTypes2.default.bool,
237+
needLazyUtilInViewPort: _propTypes["default"].bool,
222238
//目前是否可以马上加载,用于漫画列表加载时候的判断
223-
canLoadRightNow: _propTypes2.default.bool,
224-
id: _propTypes2.default.number,
225-
loaded: _propTypes2.default.func,
226-
onImageClick: _propTypes2.default.func
239+
canLoadRightNow: _propTypes["default"].bool,
240+
id: _propTypes["default"].number,
241+
loaded: _propTypes["default"].func,
242+
onImageClick: _propTypes["default"].func,
243+
onError: _propTypes["default"].func
227244
};
228-
229245
ProgressiveLazyImage.defaultProps = {
230246
needLazyUtilInViewPort: true,
231247
canLoadRightNow: true

dist/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6+
exports["default"] = void 0;
67

7-
var _ProgressiveLazyImage = require("./components/ProgressiveLazyImage");
8+
var _ProgressiveLazyImage = _interopRequireDefault(require("./components/ProgressiveLazyImage"));
89

9-
var _ProgressiveLazyImage2 = _interopRequireDefault(_ProgressiveLazyImage);
10+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
1011

11-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12-
13-
exports.default = _ProgressiveLazyImage2.default;
12+
var _default = _ProgressiveLazyImage["default"];
13+
exports["default"] = _default;

0 commit comments

Comments
 (0)