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.

384 lines 21.3 kB
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 __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 }; } }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; import { BaseProductCommand } from "./BaseProductCommand"; import { Configuration } from "@aurigma/design-atoms-model/Configuration"; import { NotImplementedException } from "@aurigma/design-atoms-model/Exception"; import { RotatedRectangleF, Path, RectangleF } from "@aurigma/design-atoms-model/Math"; import { ImageMetaData, ImageItem, RectangleItem, PlaceholderItem, ResizeMode } from "@aurigma/design-atoms-model/Product/Items"; import { ItemUtils } from "../../Utils/ItemUtils"; import { Color, RgbColors } from "@aurigma/design-atoms-model/Colors"; import { ProductThemeApplier } from "../../Services/ProductTheme/ProductThemeApplier"; import { PrintAreaBoundsType } from "@aurigma/design-atoms-model/Product/PrintAreaBoundsType"; import { SetImageMetadataCommand } from "../ItemsCommands/SetImageMetadataCommand"; import { ItemsCommand, SurfaceCommand } from "@aurigma/design-atoms-interfaces"; var ChangeBackgroundCommand = /** @class */ (function (_super) { __extends(ChangeBackgroundCommand, _super); function ChangeBackgroundCommand(_productHandler, historyArgs, product, args, _commandManager, _productThemeManager, _canvas, _eventManager) { var _this = _super.call(this, product, historyArgs, args) || this; _this._productHandler = _productHandler; _this._commandManager = _commandManager; _this._productThemeManager = _productThemeManager; _this._canvas = _canvas; _this._eventManager = _eventManager; return _this; } ChangeBackgroundCommand.prototype._executeCommandBody = function () { return __awaiter(this, void 0, void 0, function () { var isImageMeta; var _this = this; return __generator(this, function (_a) { isImageMeta = this._args.data instanceof ImageMetaData; return [2 /*return*/, this.changeBackground(this._args.surfaces, isImageMeta ? "image" : "solidColor", isImageMeta ? function (bgPh) { if (bgPh instanceof PlaceholderItem) _this._setBgImage(bgPh, _this._args.data); } : function (bgRec) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(bgRec instanceof PlaceholderItem || bgRec instanceof RectangleItem)) return [3 /*break*/, 2]; return [4 /*yield*/, this._selectBgRectangleColor(bgRec, this._args.data)]; case 1: _a.sent(); _a.label = 2; case 2: return [2 /*return*/]; } }); }); }, isImageMeta ? null : this._args.data)]; }); }); }; ChangeBackgroundCommand.prototype.changeBackground = function (surfaces, type, changeBgItem, value) { return __awaiter(this, void 0, void 0, function () { var bgPlaceholders, bgPlaceholders_1, bgPlaceholders_1_1, bgPlaceholder, contentItem, e_1_1; var e_1, _a; var _this = this; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, , 9, 10]); bgPlaceholders = void 0; switch (type) { case "image": bgPlaceholders = surfaces .map(function (s) { return _this.getOrCreateBgItems(s, type); }) .reduce(function (prev, cur) { return prev.concat(cur); }, []); break; case "solidColor": bgPlaceholders = surfaces .map(function (s) { return _this.getOrCreateBgItems(s, type); }) .reduce(function (prev, cur) { return prev.concat(cur); }, []); break; default: throw new NotImplementedException(); } _b.label = 1; case 1: _b.trys.push([1, 6, 7, 8]); bgPlaceholders_1 = __values(bgPlaceholders), bgPlaceholders_1_1 = bgPlaceholders_1.next(); _b.label = 2; case 2: if (!!bgPlaceholders_1_1.done) return [3 /*break*/, 5]; bgPlaceholder = bgPlaceholders_1_1.value; if (!(bgPlaceholder instanceof PlaceholderItem || bgPlaceholder instanceof RectangleItem)) return [3 /*break*/, 4]; if (bgPlaceholder instanceof PlaceholderItem) { ItemUtils.clearImageContent(bgPlaceholder); contentItem = bgPlaceholder.content; if (value instanceof Color) { if (!value.equals(bgPlaceholder.fillColor)) { bgPlaceholder.themeBinding.fill = null; contentItem.themeBinding.fill = null; } bgPlaceholder.fillColor = value; } else if (value != null) { bgPlaceholder.themeBinding.fill = value.title; contentItem.themeBinding.fill = value.title; } } return [4 /*yield*/, changeBgItem(bgPlaceholder)]; case 3: _b.sent(); _b.label = 4; case 4: bgPlaceholders_1_1 = bgPlaceholders_1.next(); return [3 /*break*/, 2]; case 5: return [3 /*break*/, 8]; case 6: e_1_1 = _b.sent(); e_1 = { error: e_1_1 }; return [3 /*break*/, 8]; case 7: try { if (bgPlaceholders_1_1 && !bgPlaceholders_1_1.done && (_a = bgPlaceholders_1.return)) _a.call(bgPlaceholders_1); } finally { if (e_1) throw e_1.error; } return [7 /*endfinally*/]; case 8: ItemUtils.updateItems(bgPlaceholders, this._productHandler, this._canvas); return [2 /*return*/, bgPlaceholders]; case 9: return [7 /*endfinally*/]; case 10: return [2 /*return*/]; } }); }); }; ChangeBackgroundCommand.prototype.getOrCreateBgItems = function (surface, type) { var e_2, _a; var _this = this; var createPlaceholder = function (rec) { var ph = new PlaceholderItem(rec); ph.contentResizeMode = ResizeMode.Fill; ph.content = new ImageItem(); var contentHandler = _this._productHandler.getHandler(ph.content); contentHandler.setRectangle(RotatedRectangleF.fromRectangleF(rec)); return ph; }; var createRectangle = function (rec) { var recBg = new RectangleItem(rec); var recBgHandler = _this._productHandler.getHandler(recBg); recBgHandler.setRectangle(RotatedRectangleF.fromRectangleF(rec)); recBg.borderWidth = 0; return recBg; }; var isNormalizedContent = function (bgContainerItems) { return bgContainerItems.every(function (i) { return i instanceof PlaceholderItem || i instanceof RectangleItem; }); }; var bgContainer = surface .containers .first(function (c) { return c.name === Configuration.BG_CONTAINER_NAME; }); var bgItems = bgContainer.items.toArray(); if (bgItems.length === 0) { var printArea = surface.printAreas.getItem(0); var ph = createPlaceholder(printArea.getBounds(PrintAreaBoundsType.Bleed)); this._addItem(surface, bgContainer, ph, false); return [ph]; } else if (surface.printAreas.length === bgItems.length) { var phs = []; for (var i = 0; i < surface.printAreas.length; i++) { var printArea = surface.printAreas.getItem(i); var bgItem = bgItems[i]; if (bgItem instanceof ImageItem) { var bgImg = bgItem; var frontendPermissions = bgImg.frontendPermissions; this._commandManager.execute(ItemsCommand.deleteItems, { items: [bgImg], force: true }); var ph = createPlaceholder(printArea.getBounds(PrintAreaBoundsType.Bleed)); ph.frontendPermissions = frontendPermissions; this._addItem(surface, bgContainer, ph, false); phs.push(ph); } else if (bgItem instanceof PlaceholderItem) { var bgPh = bgItem; //Reset paths, rectangles of shape background if (type !== Configuration.BG_SOLIDCOLOR) { var rectangle = printArea.getBounds(PrintAreaBoundsType.Bleed); var path = Path.rectangle(rectangle.left, rectangle.top, rectangle.width, rectangle.height); var changed = false; if (!bgPh.sourcePath.isEqual(path)) { bgPh.sourcePath = path; changed = true; } if (!RectangleF.isEqual(bgPh.sourceRectangle, rectangle)) { bgPh.transform.clear(); bgPh.sourceRectangle = rectangle; changed = true; } if (changed && bgPh.transform.translateX !== 0 || bgPh.transform.translateY !== 0) { bgPh.transform.setTranslateX(0); bgPh.transform.setTranslateY(0); } var rotatedRectangle = RotatedRectangleF.fromRectangleF(rectangle); var contentHandler = this._productHandler.getHandler(bgPh.content); if (!contentHandler.getTransformedRectangle().equals(rotatedRectangle)) { contentHandler.setTransformedRectangle(rotatedRectangle, true); changed = true; } if (changed) { ItemUtils.updateItems(bgItems, this._productHandler, this._canvas, { redraw: false }); } } else if (bgPh.content instanceof ImageItem) { bgPh.transform.clear(); bgPh.sourceRectangle = printArea.getBounds(PrintAreaBoundsType.Bleed); } phs.push(bgPh); } } return phs; } else if (isNormalizedContent(bgItems)) { //workaround для BUG 1817 - Folding Line отображается на Hi-res var solidColorType_1 = Configuration.BG_SOLIDCOLOR; if (type === solidColorType_1) { var solidColorRec = bgItems.find(function (i) { return i.name === solidColorType_1; }); if (solidColorRec == null) { var printArea = surface.printAreas.getItem(0); var commonBgPlaceholder = createRectangle(printArea.getBounds(PrintAreaBoundsType.Bleed)); commonBgPlaceholder.name = solidColorType_1; this._addItem(surface, bgContainer, commonBgPlaceholder, false); } } if (this._historyArgs.history != null) this._historyArgs.history.pause(); bgItems = bgContainer.items.toArray(); if (isNormalizedContent(bgItems)) { try { for (var bgItems_1 = __values(bgItems), bgItems_1_1 = bgItems_1.next(); !bgItems_1_1.done; bgItems_1_1 = bgItems_1.next()) { var bg = bgItems_1_1.value; var noShowNPrint = bg.name === solidColorType_1; bg.visualizationPermissions.noPrint = type === solidColorType_1 ? !noShowNPrint : noShowNPrint; bg.visualizationPermissions.noShow = type === solidColorType_1 ? !noShowNPrint : noShowNPrint; } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (bgItems_1_1 && !bgItems_1_1.done && (_a = bgItems_1.return)) _a.call(bgItems_1); } finally { if (e_2) throw e_2.error; } } } ItemUtils.updateItems(bgItems, this._productHandler, this._canvas, { redraw: true }); if (this._historyArgs.history != null) this._historyArgs.history.resume(); return bgItems; } else { throw new NotImplementedException("Containers contains unexpected content"); } }; ChangeBackgroundCommand.prototype._addItem = function (surface, container, item, selectOnCanvas) { this._commandManager.execute(SurfaceCommand.addItems, { surface: surface, targetContainer: container, items: [item], selectOnCanvas: false }); }; ChangeBackgroundCommand.prototype._setBgImage = function (bgPh, imageData) { var _a; var imageItem = bgPh.content; var imageItemHandler = this._productHandler.getHandler(imageItem); imageItemHandler.setTransformedRectangle(this._getBgImageRectangle(bgPh, imageData)); bgPh.fillColor = RgbColors.transparent; imageItem.fillColor = RgbColors.transparent; if (imageItem.overlayEffect != null) { if (imageItem.imagePermissions.allowKeepOverlayColor) ItemUtils.setOverlayEffectColor(imageItem, imageItem.overlayEffect.color); else imageItem.overlayEffect.color = null; } SetImageMetadataCommand.setDataToImage(imageData, imageItem); (_a = this._eventManager) === null || _a === void 0 ? void 0 : _a.imageContentChangedEvent.fire(bgPh); }; ChangeBackgroundCommand.prototype._getBgImageRectangle = function (ph, imageData) { var newWidth; var newHeight; var phHandler = this._productHandler.getHandler(ph); var phRectangle = phHandler.getTransformedRectangle(false); var widthToHeightImage = imageData.size.width / imageData.size.height; var widthToHeightPlaceholder = phRectangle.width / phRectangle.height; if (widthToHeightImage < widthToHeightPlaceholder) { newWidth = phRectangle.width; newHeight = newWidth / widthToHeightImage; } else { newHeight = phRectangle.height; newWidth = newHeight * widthToHeightImage; } return new RotatedRectangleF(phRectangle.centerX, phRectangle.centerY, newWidth, newHeight, 0); }; ChangeBackgroundCommand.prototype._selectBgRectangleColor = function (bgRec, value) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(value instanceof Color)) return [3 /*break*/, 1]; bgRec.fillColor = value; return [3 /*break*/, 3]; case 1: if (!(value != null)) return [3 /*break*/, 3]; bgRec.themeBinding.fill = value.title; return [4 /*yield*/, new ProductThemeApplier(this._canvas.viewer.colorPreviewService, this._canvas.viewer.colorParser) .applyToItem(bgRec, this._productThemeManager.currentProductTheme)]; case 2: _a.sent(); _a.label = 3; case 3: return [2 /*return*/]; } }); }); }; ChangeBackgroundCommand.prototype.redo = function () { throw new NotImplementedException(); }; ChangeBackgroundCommand.prototype.undo = function () { throw new NotImplementedException(); }; return ChangeBackgroundCommand; }(BaseProductCommand)); export { ChangeBackgroundCommand }; //# sourceMappingURL=ChangeBackgroundCommand.js.map