@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
146 lines • 8.63 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import { Violation, ViolationState, ViolationInfoResult } from "./Violation";
import { PlaceholderItem, ImageItem } from "@aurigma/design-atoms-model/Product/Items";
import { ItemUtils } from "../../../Utils/ItemUtils";
import { ImageViolationSettings } from "@aurigma/design-atoms-model/Product/Items/ImageViolationSettings";
import { EqualsOfFloatNumbers } from "@aurigma/design-atoms-model/Math";
import { SurfaceContainer } from "@aurigma/design-atoms-model/Product/Container";
var ImageQualityViolation = /** @class */ (function (_super) {
__extends(ImageQualityViolation, _super);
function ImageQualityViolation(_productHandler, config, _messages, _renderingDpi, _eventManager) {
var _this = _super.call(this) || this;
_this._productHandler = _productHandler;
_this._messages = _messages;
_this._renderingDpi = _renderingDpi;
_this._eventManager = _eventManager;
_this._dpiValuePlaceholder = "[DPI_VALUE_PLACEHOLDER]";
_this._getQualityInfo = function (imageItem, targetDpi) {
var imageItemHandler = _this._productHandler.getHandler(imageItem);
if (imageItemHandler == null)
return ViolationInfoResult.none;
var renderingToSourceImageDpiRatio = _this._getRenderingToSourceImageDpiRatio(imageItemHandler, targetDpi);
if (renderingToSourceImageDpiRatio == null)
return ViolationInfoResult.none;
var currentDpi = targetDpi * renderingToSourceImageDpiRatio;
var data = { dpiValue: currentDpi };
var renderingToSourceImageDpiRatioPct = renderingToSourceImageDpiRatio * 100;
if (_this._qualityLevels.bad - renderingToSourceImageDpiRatioPct > 0.001)
return { state: ViolationState.Bad, data: data, message: _this._getMessage(ViolationState.Bad, currentDpi) };
if (_this._qualityLevels.warning - renderingToSourceImageDpiRatioPct > 0.001)
return { state: ViolationState.Warning, data: data, message: _this._getMessage(ViolationState.Warning, currentDpi) };
return { state: ViolationState.Good, data: data };
};
_this._getRenderingToSourceImageDpiRatio = function (imageItemHandler, targetDpi) {
var rect = imageItemHandler.rectangle;
var source = imageItemHandler.item.source;
var renderingWidth = rect.width * targetDpi / source.dpiX;
var renderingHeight = rect.height * targetDpi / source.dpiY;
var sourceSize = ItemUtils.getImageSizeInPoints(source);
var renderingToSourceImageDpiRatio = Math.min(sourceSize.width / renderingWidth, sourceSize.height / renderingHeight);
return renderingToSourceImageDpiRatio;
};
_this._checkDpiChanging = function (newInfo, oldViolationInfo, item) {
var _a, _b;
var oldDpiValue = (_a = oldViolationInfo === null || oldViolationInfo === void 0 ? void 0 : oldViolationInfo.data[_this.getDataPropertyName()]) === null || _a === void 0 ? void 0 : _a.dpiValue;
var newDpiValue = (_b = newInfo === null || newInfo === void 0 ? void 0 : newInfo.data) === null || _b === void 0 ? void 0 : _b.dpiValue;
if (oldDpiValue == null || EqualsOfFloatNumbers(oldDpiValue, newDpiValue))
return;
_this._eventManager.imageDpiChangedEvent.notify({
oldValue: oldDpiValue,
newValue: newDpiValue,
item: item,
state: newInfo.state
});
};
_this._qualityLevels = __assign({ bad: 100, warning: 100 }, (config === null || config === void 0 ? void 0 : config.qualityLevels) || {});
if (_this._qualityLevels.warning < _this._qualityLevels.bad) {
console.warn("violationWarningsSettings.qualityMeter.qualityLevels.warning cannot be less than violationWarningsSettings.qualityMeter.qualityLevels.bad.");
}
return _this;
}
Object.defineProperty(ImageQualityViolation.prototype, "renderingDpi", {
set: function (value) { this._renderingDpi = value; },
enumerable: true,
configurable: true
});
ImageQualityViolation.prototype.isAvailableFor = function (item) {
return item instanceof ImageItem || item instanceof PlaceholderItem;
};
// review для ImageQualityViolation я бы сделал наследника IImageQualityViolationInfo extends IViolationInfo
// который бы включал в себя более подробную информацию
ImageQualityViolation.prototype.getViolationInfo = function (item, oldViolationInfo) {
var _a, _b;
if (item instanceof PlaceholderItem && item.isStubOrEmpty)
return ViolationInfoResult.good;
var image = ItemUtils.getImageItem(item);
if (image == null || ((_a = image.source) === null || _a === void 0 ? void 0 : _a.id) == null)
return ViolationInfoResult.none;
// TODO: Revisit DPI validation logic for PDF/AI files.
// - PDFs can be vector-based, raster-based, or hybrid
// - AI files may contain embedded raster images
// Current solution may incorrectly skip DPI checks for mixed-content files
var isPdfOrAi = image.source.id.toLowerCase().endsWith(".pdf") || image.source.id.toLowerCase().endsWith(".ai");
if (((_b = image.source) === null || _b === void 0 ? void 0 : _b.isVector) || isPdfOrAi)
return ViolationInfoResult.good;
var imageViolationSettings = item.violationSettings instanceof ImageViolationSettings ? item.violationSettings : null;
if (imageViolationSettings == null || !imageViolationSettings.allowImageQuality)
return ViolationInfoResult.good;
var targetDpi = this._getTargetDpi(item);
var newInfo = this._getQualityInfo(image, targetDpi);
this._checkDpiChanging(newInfo, oldViolationInfo, item);
return newInfo;
};
/**
* Retrieves the target DPI from the container constraints if specified.
* If no constraint is set, it falls back to the default `_renderingDpi`.
*/
ImageQualityViolation.prototype._getTargetDpi = function (item) {
var _a, _b;
var parent = item.parentContainer;
if (parent instanceof SurfaceContainer) {
return (_b = (_a = parent.printingTechniqueConstraints) === null || _a === void 0 ? void 0 : _a.targetDpi) !== null && _b !== void 0 ? _b : this._renderingDpi;
}
return this._renderingDpi;
};
ImageQualityViolation.prototype._getMessage = function (state, dpiValue) {
var roundedDpi = Math.round(dpiValue).toString();
var templateMessage = state === ViolationState.Bad ?
this._messages.badImageResolution :
this._messages.warningImageResolution;
return templateMessage.replace(this._dpiValuePlaceholder, roundedDpi);
};
ImageQualityViolation.prototype.getStatePropertyName = function () {
return ImageQualityViolation.statePropertyName;
};
ImageQualityViolation.prototype.getDataPropertyName = function () {
return ImageQualityViolation.dataPropertyName;
};
ImageQualityViolation.dataPropertyName = "qualityViolationData";
ImageQualityViolation.statePropertyName = "qualityViolationState";
return ImageQualityViolation;
}(Violation));
export { ImageQualityViolation };
//# sourceMappingURL=ImageQualityViolation.js.map