@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
165 lines • 8.75 kB
JavaScript
import { Item, ImageItem, PlaceholderItem, DashedLineItem, LineItem, BaseTextItem, BarcodeItem, ShapeItem, ClipartItem } from "@aurigma/design-atoms-model/Product/Items";
import { ItemData } from "@aurigma/design-atoms-model/Product/ItemsData";
import { StrokeSettingsData as StrokeSettings } from "@aurigma/design-atoms-model/Product/ItemsData";
import { ShadowSettingsData as ShadowSettings } from "@aurigma/design-atoms-model/Product/ItemsData";
import { ItemDataHandler } from "../../ItemDataHandler";
import { ItemUtils } from "../../Utils/ItemUtils";
import { applyNullableValue } from "@aurigma/design-atoms-model/Utils/Utils";
export class ProductThemeApplier {
constructor(_colorPreviewService, _colorParser) {
this._colorPreviewService = _colorPreviewService;
this._colorParser = _colorParser;
}
async apply(product, theme, themeBinding) {
if (!product) {
return false;
}
let changed = false;
for (let item of product.getAllItems()) {
changed = await this.applyToItem(item, theme, themeBinding) || changed;
}
return changed;
}
async applyToItem(item, theme, themeBinding) {
const targetThemeBinding = themeBinding || item.themeBinding;
if (!item || !theme || !targetThemeBinding) {
return false;
}
const itemData = this._getItemDataByItemType(item, theme, targetThemeBinding);
const itemDataHandler = new ItemDataHandler(itemData, this._colorPreviewService, this._colorParser);
if (item instanceof ImageItem) {
return itemDataHandler.applyImage(item);
}
if (item instanceof PlaceholderItem) {
return this._applyToPlaceholder(item, theme, targetThemeBinding);
}
if (item instanceof DashedLineItem) {
return itemDataHandler.applyDashedLine(item);
}
if (item instanceof LineItem) {
return itemDataHandler.applyLine(item);
}
if (item instanceof BaseTextItem) {
return await itemDataHandler.applyBaseText(item);
}
if (item instanceof BarcodeItem) {
return itemDataHandler.applyBarcode(item);
}
if (item instanceof ShapeItem) {
return itemDataHandler.applyShape(item);
}
if (item instanceof ClipartItem) {
return itemDataHandler.applyClipart(item);
}
}
_getItemDataByItemType(item, theme, themeBinding) {
if (item instanceof ImageItem)
return this._getMergedItemData(theme, themeBinding, themeBinding.img);
if (item instanceof LineItem)
return this._getMergedItemData(theme, themeBinding, themeBinding.line);
if (item instanceof BaseTextItem)
return this._getMergedItemData(theme, themeBinding, themeBinding.text);
if (item instanceof BarcodeItem)
return this._getMergedItemData(theme, themeBinding, themeBinding.barcode);
return this._getMergedItemData(theme, themeBinding);
}
async _applyToPlaceholder(placeholder, theme, themeBinding) {
let changed = false;
let itemData = this._getMergedItemData(theme, themeBinding);
if (itemData.fillColor != null && ItemUtils.isBgContainerItem(placeholder)) {
ItemUtils.clearImageContent(placeholder);
}
if (placeholder.content instanceof Item) {
changed = await this.applyToItem(placeholder.content, theme, themeBinding);
}
if (placeholder.topFrames != null) {
for (let frame of placeholder.topFrames) {
changed = await this.applyToItem(frame, theme) || changed;
}
}
if (placeholder.bottomFrames != null) {
for (let frame of placeholder.bottomFrames) {
changed = await this.applyToItem(frame, theme) || changed;
}
}
return new ItemDataHandler(itemData, this._colorPreviewService, this._colorParser).applyPlaceholder(placeholder) || changed;
}
_getMergedItemData(theme, themeBinding, colorBindingName = null) {
let mergedItemData = new ItemData();
if (themeBinding.styles) {
let stylesItemData = themeBinding.styles.map(style => this._getThemeItemData(style, theme));
for (let styleItemData of stylesItemData) {
if (styleItemData) {
mergedItemData = this._applyStyle(mergedItemData, styleItemData);
}
}
}
return this._applyTHBMColor(theme, themeBinding, colorBindingName, mergedItemData);
}
_applyStyle(itemData, style) {
itemData.color = applyNullableValue(itemData.color, style.color);
itemData.fillColor = applyNullableValue(itemData.fillColor, style.fillColor);
itemData.borderColor = applyNullableValue(itemData.borderColor, style.borderColor);
itemData.borderWidth = applyNullableValue(itemData.borderWidth, style.borderWidth);
itemData.altColor = applyNullableValue(itemData.altColor, style.altColor);
itemData.leading = applyNullableValue(itemData.leading, style.leading);
itemData.tracking = applyNullableValue(itemData.tracking, style.tracking);
itemData.underline = applyNullableValue(itemData.underline, style.underline);
itemData.horizontalLineColor = applyNullableValue(itemData.horizontalLineColor, style.horizontalLineColor);
itemData.verticalLineColor = applyNullableValue(itemData.verticalLineColor, style.verticalLineColor);
itemData.barcodeColor = applyNullableValue(itemData.barcodeColor, style.barcodeColor);
itemData.opacity = applyNullableValue(itemData.opacity, style.opacity);
itemData.font = this._applySettings(itemData.font, style.font);
itemData.visible = applyNullableValue(itemData.visible, style.visible);
return itemData;
}
_applyTHBMColor(theme, themeBinding, colorBindingName, itemData) {
itemData.fillColor = applyNullableValue(itemData.fillColor, this._getTHMBColor(themeBinding.fill, theme));
itemData.textFillColor = applyNullableValue(itemData.textFillColor, this._getTHMBColor(themeBinding.textFill, theme));
itemData.borderColor = applyNullableValue(itemData.borderColor, this._getTHMBColor(themeBinding.border, theme));
itemData.color = applyNullableValue(itemData.color, this._getTHMBColor(colorBindingName, theme));
itemData.altColor = applyNullableValue(itemData.altColor, this._getTHMBColor(themeBinding.altline, theme));
itemData.horizontalLineColor = applyNullableValue(itemData.horizontalLineColor, this._getTHMBColor(themeBinding.horizontal, theme));
itemData.verticalLineColor = applyNullableValue(itemData.verticalLineColor, this._getTHMBColor(themeBinding.vertical, theme));
itemData.barcodeColor = applyNullableValue(itemData.barcodeColor, this._getTHMBColor(themeBinding.barcode, theme));
if (themeBinding.shadow != null) {
if (itemData.shadow == null)
itemData.shadow = new ShadowSettings();
const color = this._colorParser.parse(this._getTHMBColor(themeBinding.shadow, theme));
itemData.shadow.color = applyNullableValue(itemData.shadow.color, color.getData());
}
if (themeBinding.stroke != null) {
if (itemData.stroke == null)
itemData.stroke = new StrokeSettings();
const color = this._colorParser.parse(this._getTHMBColor(themeBinding.stroke, theme));
itemData.stroke.color = applyNullableValue(itemData.stroke.color, color.getData());
}
itemData.clipartColors = themeBinding.clipartColors != null
? themeBinding.clipartColors.map(colorGroupTheme => this._getTHMBColor(colorGroupTheme, theme))
: [];
return itemData;
}
_applySettings(applied, notApplied) {
if (notApplied == null)
return applied;
if (applied == null)
return notApplied;
for (let property of Object.getOwnPropertyNames(notApplied)) {
let value = notApplied[property];
if (value != null)
applied[property] = value;
}
return applied;
}
_getThemeItemData(key, theme) {
if (!key)
return null;
return theme[key];
}
_getTHMBColor(colorKey, theme) {
if (!colorKey)
return null;
return theme[colorKey];
}
}
//# sourceMappingURL=ProductThemeApplier.js.map