@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
107 lines • 4.69 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 __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
import { Violation, ViolationState, ViolationInfoResult } from "./Violation";
import { BaseTextItem } from "@aurigma/design-atoms-model/Product/Items";
import { ItemUtils } from "../../../Utils/ItemUtils";
import { ValidationSeverity } from "../../../Viewer/Interfaces";
import { SurfaceContainer } from "@aurigma/design-atoms-model/Product/Container";
var FontSizeViolation = /** @class */ (function (_super) {
__extends(FontSizeViolation, _super);
function FontSizeViolation(_productHandler, _messages, _conf) {
var _a, _b;
var _this = _super.call(this) || this;
_this._productHandler = _productHandler;
_this._messages = _messages;
_this._conf = _conf;
_this._simpleFontSizeRegex = /font-size:\s*([0-9.]+)(pt|px)/gi;
_this._severity = (_b = (_a = _conf.minFontSize) === null || _a === void 0 ? void 0 : _a.severity) !== null && _b !== void 0 ? _b : ValidationSeverity.Warning;
return _this;
}
FontSizeViolation.prototype.isAvailableFor = function (item) {
return item instanceof BaseTextItem;
};
FontSizeViolation.prototype.getViolationInfo = function (item) {
var _a;
if (!ItemUtils.isTextItemHandlerReady(item, this._productHandler))
return ViolationInfoResult.none;
var handler = this._productHandler.getHandler(item);
if (!handler)
return ViolationInfoResult.none;
var parent = item.parentContainer;
var minFontSize = parent instanceof SurfaceContainer
? (_a = parent.printingTechniqueConstraints) === null || _a === void 0 ? void 0 : _a.minFontSize : null;
if (minFontSize == null)
return ViolationInfoResult.none;
var fontSizes = this._getFontSizes(item);
var isTooSmall = fontSizes.some(function (size) { return size < minFontSize; });
if (!isTooSmall) {
return ViolationInfoResult.good;
}
var violationState = this._severity === ValidationSeverity.Error
? ViolationState.Bad
: ViolationState.Warning;
return {
state: violationState,
message: this._messages.fontSizeViolationWarning
};
};
FontSizeViolation.prototype._getFontSizes = function (item) {
var sizes = [];
sizes.push(item.font.size);
if (item.text) {
var markupSizes = this._getFontSizesFromMarkup(item.text);
sizes.push.apply(sizes, __spread(markupSizes));
}
return sizes;
};
FontSizeViolation.prototype._getFontSizesFromMarkup = function (text) {
var sizes = [];
var match;
this._simpleFontSizeRegex.lastIndex = 0;
while ((match = this._simpleFontSizeRegex.exec(text)) !== null) {
var sizeValue = parseFloat(match[1]);
var unit = match[2].toLowerCase();
var sizeInPt = unit === 'px' ? sizeValue * 0.75 : sizeValue;
sizes.push(sizeInPt);
}
return sizes;
};
FontSizeViolation.prototype.getStatePropertyName = function () {
return FontSizeViolation.statePropertyName;
};
FontSizeViolation.statePropertyName = "fontSizeViolationState";
return FontSizeViolation;
}(Violation));
export { FontSizeViolation };
//# sourceMappingURL=FontSizeViolation.js.map