@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
78 lines • 3.91 kB
JavaScript
import { ItemUtils } from "../../../Utils/ItemUtils";
import { Violation, ViolationState, ViolationInfoResult } from "./Violation";
import { Configuration } from "@aurigma/design-atoms-model/Configuration";
import { SafetyLineViolationMode } from "../../../Viewer/Interfaces";
import { PrintAreaBoundsType } from "@aurigma/design-atoms-model/Product/PrintAreaBoundsType";
import { Margin } from "@aurigma/design-atoms-model/Math/Margin";
export class SafetyLineViolation extends Violation {
constructor(_productHandler, _messages, _conf) {
var _a, _b;
super();
this._productHandler = _productHandler;
this._messages = _messages;
this._conf = _conf;
this._mode = (_b = (_a = _conf.safetyLines) === null || _a === void 0 ? void 0 : _a.mode) !== null && _b !== void 0 ? _b : SafetyLineViolationMode.InsideAll;
}
isAvailableFor(item) {
return true;
}
getViolationInfo(item) {
var _a, _b;
if (!ItemUtils.isTextItemHandlerReady(item, this._productHandler))
return ViolationInfoResult.none;
if (!item.violationSettings.allowSafetyLines || ((_a = item.parentContainer) === null || _a === void 0 ? void 0 : _a.name) !== Configuration.MAIN_CONTAINER_NAME)
return ViolationInfoResult.good;
const printAreas = item.parentContainer.parentComponent.printAreas;
const tolerance = (_b = this._conf.tolerance) !== null && _b !== void 0 ? _b : 0.001;
const safetyLineRects = printAreas
.selectMany(p => this._getSafetyRectsFromPrintArea(p)
.map(r => r.getExpanded(new Margin(tolerance)))).toArray();
if (safetyLineRects.length === 0)
return ViolationInfoResult.good;
const itemBounds = ItemUtils.getItemBoundsForViolationCheck(this._productHandler.getHandler(item));
let isWarning = this._mode !== SafetyLineViolationMode.InsideAll;
let breakCycle = false;
for (const sRect of safetyLineRects) {
switch (this._mode) {
case SafetyLineViolationMode.InsideAll:
if (!sRect.containsRectangle(itemBounds)) {
isWarning = true;
breakCycle = true;
}
break;
case SafetyLineViolationMode.PartiallyInsideAny:
if (sRect.containsRectangle(itemBounds)) {
isWarning = false;
breakCycle = true;
}
break;
case SafetyLineViolationMode.InsideAny:
if (sRect.containsRectangle(itemBounds)) {
isWarning = false;
}
else if (sRect.intersectsWith(itemBounds)) {
isWarning = true;
breakCycle = true;
}
break;
}
if (breakCycle)
break;
}
const violationState = isWarning ? ViolationState.Warning : ViolationState.Good;
const message = violationState === ViolationState.Warning ? this._messages.safetyLinesViolationWarning : null;
return { state: violationState, message: message };
}
_getSafetyRectsFromPrintArea(printArea) {
const result = [];
result.push(...printArea.safetyLines.toArray().map(sl => sl.getRectangle(printArea.bounds)));
if (!printArea.bleed.isEmpty || !printArea.slug.isEmpty)
result.push(printArea.getBounds(PrintAreaBoundsType.Trim));
return result;
}
getStatePropertyName() {
return SafetyLineViolation.statePropertyName;
}
}
SafetyLineViolation.statePropertyName = "safetyLineViolationState";
//# sourceMappingURL=SafetyLineViolation.js.map