@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
210 lines (208 loc) • 9.17 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.withImageLoaderOld = exports.withImageLoader = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(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; })(); }
var withImageLoaderOld = exports.withImageLoaderOld = function withImageLoaderOld(Wrapped) {
return /*#__PURE__*/function (_Component) {
function WithImageLoader() {
var _this;
(0, _classCallCheck2.default)(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));
(0, _defineProperty2.default)(_this, "state", {
imageStatus: 'loading'
});
(0, _defineProperty2.default)(_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
});
}
});
(0, _defineProperty2.default)(_this, "onError", function () {
_this.setState({
imageStatus: 'error'
});
});
return _this;
}
(0, _inherits2.default)(WithImageLoader, _Component);
return (0, _createClass2.default)(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.default.createElement(Wrapped, (0, _extends2.default)({}, this.props, {
imageStatus: imageStatus
}));
}
}]);
}(_react.Component);
};
var withImageLoaderNew = function withImageLoaderNew(Wrapped) {
return /*#__PURE__*/function (_Component2) {
function WithImageLoader() {
var _this2;
(0, _classCallCheck2.default)(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));
(0, _defineProperty2.default)(_this2, "state", {
imageStatus: 'loading'
});
(0, _defineProperty2.default)(_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
});
}
});
(0, _defineProperty2.default)(_this2, "onError", function () {
_this2.setState({
imageStatus: 'error'
});
});
return _this2;
}
(0, _inherits2.default)(WithImageLoader, _Component2);
return (0, _createClass2.default)(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.default.createElement(Wrapped, (0, _extends2.default)({}, this.props, {
imageStatus: imageStatus
}));
}
}]);
}(_react.Component);
};
var withImageLoader = exports.withImageLoader = function withImageLoader(Wrapped) {
if ((0, _platformFeatureFlags.fg)('platform_editor_react18_phase2_v2')) {
return withImageLoaderNew(Wrapped);
}
return withImageLoaderOld(Wrapped);
};