"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.computeScale = computeScale; exports.withAutoScale = withAutoScale; var _react = _interopRequireWildcard(require("react")); var _lodash = require("lodash"); var _eui = require("@elastic/eui"); var _with_auto_scale = require("./with_auto_scale.styles"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ const MAX_SCALE = 1; const MIN_SCALE = 0.3; /** * computeScale computes the ratio by which the child needs to shrink in order * to fit into the parent. This function is only exported for testing purposes. */ function computeScale(parent, child, minScale = MIN_SCALE) { if (!parent || !child) { return 1; } const scaleX = parent.clientWidth / child.clientWidth; const scaleY = parent.clientHeight / child.clientHeight; return Math.max(Math.min(MAX_SCALE, Math.min(scaleX, scaleY)), minScale); } function hasAutoscaleProps(props) { if (props.autoScaleParams) { return true; } return false; } function getWrappedComponentProps(props) { if (hasAutoscaleProps(props)) { const { autoScaleParams, renderComplete, ...rest } = props; return rest; } return props; } function withAutoScale(WrappedComponent) { return props => { const { autoScaleParams, renderComplete } = props; const restProps = getWrappedComponentProps(props); // An initial scale of 0 means we always redraw // at least once, which is sub-optimal, but it // prevents an annoying flicker. const [scale, setScale] = (0, _react.useState)(0); const [resized, setResized] = (0, _react.useState)(false); const parentRef = (0, _react.useRef)(null); const childrenRef = (0, _react.useRef)(null); const parentDimensions = (0, _eui.useResizeObserver)(parentRef.current); const scaleFn = (0, _react.useMemo)(() => (0, _lodash.throttle)(() => { const newScale = computeScale({ clientHeight: parentDimensions.height, clientWidth: parentDimensions.width }, childrenRef.current, autoScaleParams === null || autoScaleParams === void 0 ? void 0 : autoScaleParams.minScale); // Prevent an infinite render loop if (scale !== newScale) { setScale(newScale); } if (parentDimensions.height && parentDimensions.width) { setResized(true); } }), [parentDimensions, setScale, scale, autoScaleParams]); (0, _react.useEffect)(() => { scaleFn(); }, [scaleFn]); (0, _react.useLayoutEffect)(() => { if (resized) { renderComplete === null || renderComplete === void 0 ? void 0 : renderComplete(); } }, [renderComplete, resized]); return /*#__PURE__*/_react.default.createElement("div", { ref: parentRef, style: autoScaleParams === null || autoScaleParams === void 0 ? void 0 : autoScaleParams.containerStyles, css: _with_auto_scale.autoScaleWrapperStyle }, /*#__PURE__*/_react.default.createElement("div", { ref: childrenRef, style: { transform: `scale(${scale || 0})`, ...(parentDimensions.width && scale && autoScaleParams !== null && autoScaleParams !== void 0 && autoScaleParams.autoScaleMetricAlignment && (autoScaleParams === null || autoScaleParams === void 0 ? void 0 : autoScaleParams.autoScaleMetricAlignment) !== 'center' ? { position: 'relative', [autoScaleParams.autoScaleMetricAlignment]: (1 - scale) * parentDimensions.width * scale * -1 // The difference of width after scaled } : {}) } }, /*#__PURE__*/_react.default.createElement(WrappedComponent, restProps))); }; }