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.

90 lines 3.58 kB
import { Violation, ViolationState, ViolationInfoResult, } from "./Violation"; import { SurfaceContainer } from "@aurigma/design-atoms-model"; import { RectangleF } from "@aurigma/design-atoms-model/Math/RectangleF"; export class MaxArtworkAreaViolation extends Violation { constructor(_productHandler, _messages) { super(); this._productHandler = _productHandler; this._messages = _messages; } isAvailableFor(item) { return true; } getStatePropertyName() { return MaxArtworkAreaViolation.statePropertyName; } getDataPropertyName() { return MaxArtworkAreaViolation.dataPropertyName; } getViolationInfo(item) { if (!(item.parentContainer instanceof SurfaceContainer)) { return ViolationInfoResult.none; } const boundsList = item.parentContainer.items .toArray() .map((containerItem) => { const handler = this._productHandler.getHandler(containerItem); return handler.bounds; }); if (boundsList.length === 0) return ViolationInfoResult.good; const constraints = item.parentContainer.constraints.maxArtworkSize; if (constraints == null) return ViolationInfoResult.none; const overallBounds = RectangleF.getOverallBounds(boundsList); const state = this._isExceedConstraints(overallBounds, constraints) ? ViolationState.Bad : ViolationState.Good; const message = state === ViolationState.Bad ? this._messages.maxArtworkSizeWarning : null; return { state, message, data: { overallBounds, constraints } }; } _isExceedConstraints(overallBounds, constraints) { if (constraints.width == null || constraints.height == null) { return false; } if (constraints.shapeType === "ellipse") { if (constraints.width == constraints.height) { return this._isExceedForCircle(overallBounds, constraints); } else { return this._isExceedForEllipce(overallBounds, constraints); } } else if (constraints.shapeType === "rectangle") { return this._isExceedForRectangle(overallBounds, constraints); } return false; } _isExceedForEllipce(overallBounds, constraints) { const a = constraints.width / 2; const b = constraints.height / 2; const x = overallBounds.width / 2; const y = overallBounds.height / 2; const formulaResult = (x * x) / (a * a) + (y * y) / (b * b); return formulaResult > 1; } _isExceedForCircle(overallBounds, constraints) { const diametr = Math.sqrt(overallBounds.width ** 2 + overallBounds.height ** 2); if (diametr > constraints.width) { return true; } if (diametr > constraints.height) { return true; } return false; } _isExceedForRectangle(overallBounds, constraints) { if (overallBounds.width > constraints.width) { return true; } if (overallBounds.height > constraints.height) { return true; } return false; } } MaxArtworkAreaViolation.statePropertyName = "maxArtworkAreaViolationState"; MaxArtworkAreaViolation.dataPropertyName = "maxArtworkAreaViolationData"; //# sourceMappingURL=MaxArtworkAreaViolation.js.map