@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
261 lines • 13 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
import { PlaceholderItem, ImageItem } from "@aurigma/design-atoms-model/Product/Items";
import { ItemUtils } from "./Utils/ItemUtils";
import { ImageEffect } from "./ItemHandlers";
import { JsonColorParser } from "./Serialization";
import _ from "underscore";
var ItemDataHandler = /** @class */ (function () {
function ItemDataHandler(_itemData, _colorPreviewServce, _colorParser) {
this._itemData = _itemData;
this._colorPreviewServce = _colorPreviewServce;
this._colorParser = _colorParser;
}
ItemDataHandler.prototype.applyImage = function (image) {
var changed = this.applyShape(image);
changed = this._applyColor(image, "overlayColor", "fillColor") || changed;
return changed;
};
ItemDataHandler.prototype.applyLine = function (line) {
var changed = this._applyOpacity(line);
changed = this._applyColor(line) || changed;
changed = this._applyVisibility(line) || changed;
return changed;
};
ItemDataHandler.prototype.applyDashedLine = function (dashedLine) {
var changed = this._applyOpacity(dashedLine);
changed = this._applyColor(dashedLine) || changed;
changed = this._applyColor(dashedLine, "altColor", "altColor") || changed;
changed = this._applyVisibility(dashedLine) || changed;
return changed;
};
ItemDataHandler.prototype.applyBaseText = function (text) {
return __awaiter(this, void 0, void 0, function () {
var changed;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
changed = this.applyShape(text);
changed = this._applyColor(text) || changed;
changed = this._applyFont(text) || changed;
changed = this._applyTextProperty(text, "underline") || changed;
changed = this._applyTextProperty(text, "tracking") || changed;
changed = this._applyTextProperty(text, "leading") || changed;
return [4 /*yield*/, this._applyShadow(text)];
case 1:
changed = (_a.sent()) || changed;
changed = this._applyStroke(text) || changed;
return [2 /*return*/, changed];
}
});
});
};
ItemDataHandler.prototype.applyShape = function (shape) {
var changed = this._applyOpacity(shape);
changed = this._applyFillColor(shape) || changed;
changed = this._applyBorderColor(shape) || changed;
changed = this._applyBorderWidth(shape) || changed;
changed = this._applyVisibility(shape) || changed;
return changed;
};
ItemDataHandler.prototype.applyClipart = function (clipart) {
var _this = this;
var changed = this._applyClipartColors(clipart);
clipart.items.forEach(function (item) {
changed = _this._applyVisibility(item) || changed;
});
return changed;
};
ItemDataHandler.prototype.applyBarcode = function (barcode) {
var changed = this.applyShape(barcode);
changed = this._applyBarcodeColor(barcode) || changed;
return changed;
};
ItemDataHandler.prototype.applyPlaceholder = function (placeholder) {
var changed = false;
if (ItemUtils.getContentEffect(placeholder) === ImageEffect.Colorize) {
changed = this._applyColor(placeholder, "contentFillColor", "fillColor") || changed;
this._itemData.fillColor = null;
}
changed = this.applyShape(placeholder) || changed;
changed = this._applyColor(placeholder, "overlayColor") || changed;
return changed;
};
ItemDataHandler.prototype._applyFillColor = function (shape) {
return this._applyColor(shape, "fillColor", "fillColor");
};
ItemDataHandler.prototype._applyClipartColors = function (clipart) {
var _this = this;
var changed = false;
this._itemData.clipartColors.forEach(function (colorGroupColor, index) {
if (colorGroupColor == null)
return;
_this._tryApplyColor(colorGroupColor, function (color) {
clipart.setColor(clipart.colorGroups[index], color);
changed = true;
});
});
return changed;
};
ItemDataHandler.prototype._applyTextFillColor = function (text) {
return this._applyColor(text, "fillColor", "textFillColor");
};
ItemDataHandler.prototype._applyFont = function (text) {
if (this._itemData.font == null)
return false;
var changed = false;
if (this._itemData.font.postScriptName) {
text.font.postScriptName = this._itemData.font.postScriptName;
changed = true;
}
if (this._itemData.font.fauxItalic != null) {
text.font.fauxItalic = this._itemData.font.fauxItalic;
changed = true;
}
if (this._itemData.font.fauxBold != null) {
text.font.fauxBold = this._itemData.font.fauxBold;
changed = true;
}
if (this._itemData.font.size) {
text.font.size = this._itemData.font.size;
changed = true;
}
if (this._itemData.font.allCaps != null) {
text.font.allCaps = this._itemData.font.allCaps;
changed = true;
}
return changed;
};
ItemDataHandler.prototype._applyTextProperty = function (text, propertyName) {
if (this._itemData[propertyName] == null)
return false;
text[propertyName] = this._itemData[propertyName];
return true;
};
ItemDataHandler.prototype._applyShadow = function (text) {
return __awaiter(this, void 0, void 0, function () {
var changed, shadowColorPreview;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this._itemData.shadow == null || text.shadow == null)
return [2 /*return*/, false];
changed = false;
return [4 /*yield*/, this._colorPreviewServce.getPreviewAsync(text.shadow.color)];
case 1:
shadowColorPreview = _a.sent();
if (this._itemData.shadow.color && _.isEqual(this._itemData.shadow.color, shadowColorPreview.getData()))
changed = this._tryApplyColor(this._itemData.shadow.color, function (color) { return text.shadow.color = color; });
if (this._itemData.shadow.angle && this._itemData.shadow.angle !== text.shadow.angle) {
text.shadow.angle = this._itemData.shadow.angle;
changed = true;
}
if (this._itemData.shadow.distance && this._itemData.shadow.distance !== text.shadow.distance) {
text.shadow.distance = this._itemData.shadow.distance;
changed = true;
}
if (this._itemData.shadow.size && this._itemData.shadow.size !== text.shadow.size) {
text.shadow.size = this._itemData.shadow.size;
changed = true;
}
return [2 /*return*/, changed];
}
});
});
};
ItemDataHandler.prototype._applyStroke = function (text) {
if (this._itemData.stroke == null || text.stroke == null)
return false;
var changed = false;
if (this._itemData.stroke.color)
changed = this._tryApplyColor(this._itemData.stroke.color, function (color) { return text.stroke.color = color; });
if (this._itemData.stroke.size && this._itemData.stroke.size !== text.stroke.size) {
text.stroke.size = this._itemData.stroke.size;
changed = true;
}
return changed;
};
ItemDataHandler.prototype._applyOpacity = function (item) {
if (this._itemData.opacity == null)
return false;
item.opacity = this._itemData.opacity;
return true;
};
ItemDataHandler.prototype._applyVisibility = function (item) {
if (this._itemData.visible == null)
return false;
item.visible = this._itemData.visible;
return true;
};
ItemDataHandler.prototype._applyBorderColor = function (shape) {
return this._applyColor(shape, "borderColor", "borderColor");
};
ItemDataHandler.prototype._applyBorderWidth = function (shape) {
if (this._itemData.borderWidth == null)
return false;
shape.borderWidth = this._itemData.borderWidth;
return true;
};
ItemDataHandler.prototype._applyColor = function (item, itemColorProperty, itemDataColorProperty) {
if (itemColorProperty === void 0) { itemColorProperty = "color"; }
if (itemDataColorProperty === void 0) { itemDataColorProperty = "color"; }
if (this._itemData[itemDataColorProperty] == null)
return false;
return this._tryApplyColor(this._itemData[itemDataColorProperty], function (color) {
if (itemColorProperty === "overlayColor" && (item instanceof ImageItem || item instanceof PlaceholderItem))
ItemUtils.setOverlayEffectColor(item, color);
else
item[itemColorProperty] = color;
});
};
ItemDataHandler.prototype._tryApplyColor = function (rawColor, updateFunc) {
var color = typeof rawColor === "string"
? this._colorParser.parse(rawColor)
: JsonColorParser.parse(rawColor);
if (color) {
updateFunc(color);
return true;
}
return false;
};
ItemDataHandler.prototype._applyBarcodeColor = function (barcode) {
return this._applyColor(barcode, "color", "barcodeColor");
};
return ItemDataHandler;
}());
export { ItemDataHandler };
//# sourceMappingURL=ItemDataHandler.js.map