@wordpress/block-library
Version:
Block library for the WordPress editor.
119 lines (107 loc) • 3.42 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import { createElement } from "@wordpress/element";
/**
* External dependencies
*/
import { noop } from 'lodash';
/**
* WordPress dependencies
*/
import { withGlobalEvents } from '@wordpress/compose';
import { Component } from '@wordpress/element';
/**
* Internal dependencies
*/
import { calculatePreferedImageSize } from './utils';
var ImageSize =
/*#__PURE__*/
function (_Component) {
_inherits(ImageSize, _Component);
function ImageSize() {
var _this;
_classCallCheck(this, ImageSize);
_this = _possibleConstructorReturn(this, _getPrototypeOf(ImageSize).apply(this, arguments));
_this.state = {
width: undefined,
height: undefined
};
_this.bindContainer = _this.bindContainer.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.calculateSize = _this.calculateSize.bind(_assertThisInitialized(_assertThisInitialized(_this)));
return _this;
}
_createClass(ImageSize, [{
key: "bindContainer",
value: function bindContainer(ref) {
this.container = ref;
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
if (this.props.src !== prevProps.src) {
this.setState({
width: undefined,
height: undefined
});
this.fetchImageSize();
}
if (this.props.dirtynessTrigger !== prevProps.dirtynessTrigger) {
this.calculateSize();
}
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
this.fetchImageSize();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.image) {
this.image.onload = noop;
}
}
}, {
key: "fetchImageSize",
value: function fetchImageSize() {
this.image = new window.Image();
this.image.onload = this.calculateSize;
this.image.src = this.props.src;
}
}, {
key: "calculateSize",
value: function calculateSize() {
var _calculatePreferedIma = calculatePreferedImageSize(this.image, this.container),
width = _calculatePreferedIma.width,
height = _calculatePreferedIma.height;
this.setState({
width: width,
height: height
});
}
}, {
key: "render",
value: function render() {
var sizes = {
imageWidth: this.image && this.image.width,
imageHeight: this.image && this.image.height,
containerWidth: this.container && this.container.clientWidth,
containerHeight: this.container && this.container.clientHeight,
imageWidthWithinContainer: this.state.width,
imageHeightWithinContainer: this.state.height
};
return createElement("div", {
ref: this.bindContainer
}, this.props.children(sizes));
}
}]);
return ImageSize;
}(Component);
export default withGlobalEvents({
resize: 'calculateSize'
})(ImageSize);
//# sourceMappingURL=image-size.js.map