@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
200 lines (199 loc) • 7.51 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
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';
import { fg } from '@atlaskit/platform-feature-flags';
export var withImageLoaderOld = function withImageLoaderOld(Wrapped) {
return /*#__PURE__*/function (_Component) {
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 = _callSuper(this, WithImageLoader, [].concat(args));
_defineProperty(_this, "state", {
imageStatus: 'loading'
});
_defineProperty(_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(_this, "onError", function () {
_this.setState({
imageStatus: 'error'
});
});
return _this;
}
_inherits(WithImageLoader, _Component);
return _createClass(WithImageLoader, [{
key: "componentDidMount",
value: function componentDidMount() {
this.fetchImage(this.props);
}
// Ignored via go/ees005
// eslint-disable-next-line react/no-unsafe
}, {
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) {
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
this.img.removeEventListener('load', this.onLoad);
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
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();
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
this.img.addEventListener('load', this.onLoad);
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
this.img.addEventListener('error', this.onError);
}
this.img.src = url;
}
}
}, {
key: "render",
value: function render() {
var imageStatus = this.state.imageStatus;
// Ignored via go/ees005
// eslint-disable-next-line react/jsx-props-no-spreading
return /*#__PURE__*/React.createElement(Wrapped, _extends({}, this.props, {
imageStatus: imageStatus
}));
}
}]);
}(Component);
};
var withImageLoaderNew = function withImageLoaderNew(Wrapped) {
return /*#__PURE__*/function (_Component2) {
function WithImageLoader() {
var _this2;
_classCallCheck(this, WithImageLoader);
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
_this2 = _callSuper(this, WithImageLoader, [].concat(args));
_defineProperty(_this2, "state", {
imageStatus: 'loading'
});
_defineProperty(_this2, "onLoad", function () {
_this2.setState({
imageStatus: 'complete'
});
var onExternalImageLoaded = _this2.props.onExternalImageLoaded;
if (onExternalImageLoaded && _this2.img) {
onExternalImageLoaded({
width: _this2.img.naturalWidth,
height: _this2.img.naturalHeight
});
}
});
_defineProperty(_this2, "onError", function () {
_this2.setState({
imageStatus: 'error'
});
});
return _this2;
}
_inherits(WithImageLoader, _Component2);
return _createClass(WithImageLoader, [{
key: "componentDidMount",
value: function componentDidMount() {
this.fetchImage(this.props);
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(newProps) {
if (newProps.url !== this.props.url) {
this.setState({
imageStatus: 'loading'
});
this.fetchImage(newProps);
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.img) {
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
this.img.removeEventListener('load', this.onLoad);
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
this.img.removeEventListener('error', this.onError);
this.img = null;
}
}
}, {
key: "fetchImage",
value: function fetchImage(_ref2) {
var url = _ref2.url;
if (url) {
if (!this.img) {
this.img = new Image();
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
this.img.addEventListener('load', this.onLoad);
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
this.img.addEventListener('error', this.onError);
}
this.img.src = url;
}
}
}, {
key: "render",
value: function render() {
var imageStatus = this.state.imageStatus;
// Ignored via go/ees005
// eslint-disable-next-line react/jsx-props-no-spreading
return /*#__PURE__*/React.createElement(Wrapped, _extends({}, this.props, {
imageStatus: imageStatus
}));
}
}]);
}(Component);
};
export var withImageLoader = function withImageLoader(Wrapped) {
if (fg('platform_editor_react18_phase2_v2')) {
return withImageLoaderNew(Wrapped);
}
return withImageLoaderOld(Wrapped);
};