UNPKG

@aurigma/design-atoms

Version:

Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.

115 lines 6.73 kB
var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; import { flatten } from "@aurigma/design-atoms-model"; import { extractGroupContent, getPlaceholderContent, isNonVectorImageHandler } from "../../../../ItemHandlers/HandlerUtils"; import { getResizedImageAxisDpi, minByAbs } from "../../../../Utils/Math"; import { getSelectionAreaToImageSizeRatio, isIncreasingResize } from "./Utils"; import { Section } from "./Section"; import { AxisResizeData } from "./AxisResizeData"; import { ItemUtils } from "../../../../Utils/ItemUtils"; var MinDpiResizeConstraint = /** @class */ (function () { function MinDpiResizeConstraint() { } MinDpiResizeConstraint.prototype.applyTo = function (selection, resizeData, minDpi) { var result = selection.clone(); if (resizeData.rectangleItemHandlers == null || !isIncreasingResize(selection, resizeData)) { return result; } var imageHandlers = this._getHandlersToConstrain(resizeData.rectangleItemHandlers); if (imageHandlers.length == 0) { return result; } result = this._applyToX(selection, resizeData, imageHandlers, minDpi); result = this._applyToY(result, resizeData, imageHandlers, minDpi); return result; }; MinDpiResizeConstraint.prototype._applyToX = function (selection, resizeData, imageHandlers, minDpi) { return this._applyToAxis(selection, resizeData, imageHandlers, minDpi, function (size) { return size.width; }, function (resolution) { return resolution.dpiX; }, AxisResizeData.fromAxisX, Section.fromAxisX, function (rectangle, section) { rectangle.width = section.length; rectangle.centerX = section.center; }); }; MinDpiResizeConstraint.prototype._applyToY = function (selection, resizeData, imageHandlers, minDpi) { return this._applyToAxis(selection, resizeData, imageHandlers, minDpi, function (size) { return size.height; }, function (resolution) { return resolution.dpiY; }, AxisResizeData.fromAxisY, Section.fromAxisY, function (rectangle, section) { rectangle.height = section.length; rectangle.centerY = section.center; }); }; MinDpiResizeConstraint.prototype._applyToAxis = function (selection, resizeData, imageHandlers, minDpi, axisSize, axisDpi, axisResizeData, axisSection, applySection) { var result = selection.clone(); var _a = this._getMinDpiAfterResizeImageInfo(imageHandlers, axisSize(selection), resizeData, axisSize, axisDpi), handler = _a.handler, dpi = _a.dpi, sizeRatioToSelection = _a.sizeRatioToSelection; if (handler.startRectangle == null) return result; var source = handler.item.source; var sourceSize = axisSize(ItemUtils.getImageSizeInPoints(source)); var startLeastDpi = getResizedImageAxisDpi(sourceSize, axisDpi(source), axisSize(resizeData.startRectangle) / sizeRatioToSelection); if (dpi < minDpi) { var constrained = this._constrainAxis(axisResizeData(resizeData, selection), axisSection(handler.startRectangle), { length: sourceSize, dpi: axisDpi(source), lengthRatioToSelection: sizeRatioToSelection }, startLeastDpi, minDpi); applySection(result, constrained); } return result; }; MinDpiResizeConstraint.prototype._constrainAxis = function (resizeData, startAxis, imageInfo, startDpi, minDpi) { var result = resizeData.target; var isWithinStartSize = Math.abs(resizeData.target.length) < Math.abs(resizeData.start.length); if (startDpi < minDpi) { if (!isWithinStartSize) { result = new Section(resizeData.start.length * Math.sign(resizeData.target.length), resizeData.start.center * Math.sign(resizeData.target.length)); } return result; } var minDpiLength = imageInfo.length * imageInfo.lengthRatioToSelection * Math.sign(resizeData.target.length) * imageInfo.dpi / minDpi; var resizeDirection = Math.sign(resizeData.target.center - startAxis.center); result = new Section(minByAbs(minDpiLength, resizeData.target.length), startAxis.center + (minDpiLength - startAxis.length) / 2 * resizeDirection); return result; }; MinDpiResizeConstraint.prototype._getMinDpiAfterResizeImageInfo = function (imageHandlers, selectionSize, resizeData, axisSize, sourceDpi) { var e_1, _a; var result = { handler: imageHandlers[0], dpi: Infinity, sizeRatioToSelection: 0 }; try { for (var imageHandlers_1 = __values(imageHandlers), imageHandlers_1_1 = imageHandlers_1.next(); !imageHandlers_1_1.done; imageHandlers_1_1 = imageHandlers_1.next()) { var handler = imageHandlers_1_1.value; var sizeRatioToSelection = axisSize(getSelectionAreaToImageSizeRatio(resizeData, handler)); var afterResizeHeight = selectionSize / sizeRatioToSelection; var source = handler.item.source; var sourceSize = axisSize(ItemUtils.getImageSizeInPoints(source)); var dpi = getResizedImageAxisDpi(sourceSize, sourceDpi(source), afterResizeHeight); if (dpi < result.dpi) { result.handler = handler; result.dpi = dpi; result.sizeRatioToSelection = sizeRatioToSelection; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (imageHandlers_1_1 && !imageHandlers_1_1.done && (_a = imageHandlers_1.return)) _a.call(imageHandlers_1); } finally { if (e_1) throw e_1.error; } } return result; }; MinDpiResizeConstraint.prototype._getHandlersToConstrain = function (handlers) { return flatten(handlers.map(extractGroupContent)) .map(getPlaceholderContent) .filter(isNonVectorImageHandler) .map(function (handler) { return handler; }); }; return MinDpiResizeConstraint; }()); export { MinDpiResizeConstraint }; //# sourceMappingURL=MinDpiResizeConstraint.js.map