UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

93 lines 3.9 kB
import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } import React, { Component } from 'react'; export var withImageLoader = function withImageLoader(Wrapped) { return /*#__PURE__*/function (_Component) { _inherits(WithImageLoader, _Component); var _super = _createSuper(WithImageLoader); function WithImageLoader() { var _this; _classCallCheck(this, WithImageLoader); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _defineProperty(_assertThisInitialized(_this), "state", { imageStatus: 'loading' }); _defineProperty(_assertThisInitialized(_this), "onLoad", function () { _this.setState({ imageStatus: 'complete' }); var onExternalImageLoaded = _this.props.onExternalImageLoaded; if (onExternalImageLoaded && _this.img) { onExternalImageLoaded({ width: _this.img.naturalWidth, height: _this.img.naturalHeight }); } }); _defineProperty(_assertThisInitialized(_this), "onError", function () { _this.setState({ imageStatus: 'error' }); }); return _this; } _createClass(WithImageLoader, [{ key: "componentDidMount", value: function componentDidMount() { this.fetchImage(this.props); } }, { key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(nextProps) { if (nextProps.url !== this.props.url) { this.setState({ imageStatus: 'loading' }); this.fetchImage(nextProps); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { if (this.img) { this.img.removeEventListener('load', this.onLoad); this.img.removeEventListener('error', this.onError); this.img = null; } } }, { key: "fetchImage", value: function fetchImage(_ref) { var url = _ref.url; if (url) { if (!this.img) { this.img = new Image(); this.img.addEventListener('load', this.onLoad); this.img.addEventListener('error', this.onError); } this.img.src = url; } } }, { key: "render", value: function render() { var imageStatus = this.state.imageStatus; return /*#__PURE__*/React.createElement(Wrapped, _extends({}, this.props, { imageStatus: imageStatus })); } }]); return WithImageLoader; }(Component); };