@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
67 lines • 3.19 kB
JavaScript
import { BaseItemsCommand } from "./BaseItemsCommand";
import { maybe } from "tsmonad";
import { PlaceholderItem, Size } from "@aurigma/design-atoms-model/Product/Items";
import { Matrix, RectangleF } from "@aurigma/design-atoms-model/Math";
import { ArgumentException, NotImplementedException } from "@aurigma/design-atoms-model/Exception";
import { isEmpty, parsePercentOrPointValue } from "@aurigma/design-atoms-model/Utils/Utils";
export class TranslateItemsCommand extends BaseItemsCommand {
constructor(productHandler, historyArgs, args) {
super(productHandler, historyArgs, args);
this._convertedArgs = args.itemIds != null ?
this._convertExternalConfig(args)
: args;
}
async _executeCommandBody() {
const items = this._getTargetItems(this._convertedArgs.items, this._convertedArgs.query, this._convertedArgs.queryOptions);
const matrix = this._getTransformMatrix(items);
items.forEach(i => {
const updateItem = (item) => {
this._productHandler.getHandler(item).transformByMatrix(matrix, false);
};
updateItem(i);
if (i instanceof PlaceholderItem)
i.updateContentAndFrames(updateItem);
});
}
_getTransformMatrix(items) {
const parentSize = this._getParentRectangle(items);
const translateXInPoint = parsePercentOrPointValue(this._args.translateX, parentSize.width);
const translateYInPoint = parsePercentOrPointValue(this._args.translateY, parentSize.height);
const transformMatrix = new Matrix();
transformMatrix.translate(translateXInPoint, translateYInPoint);
return transformMatrix;
}
_getParentRectangle(items) {
return maybe(items[0])
.map(item => item.parentContainer)
.map(container => ({ surface: container.parentComponent, container: container }))
.map(({ surface, container }) => ({ printArea: surface.printAreas.get(0), container: container }))
.map(({ printArea, container }) => maybe(container.region).caseOf({
just: (region) => RectangleF.union(printArea.bounds, region),
nothing: () => printArea.bounds
})).caseOf({
just: result => new Size(result.width, result.height),
nothing: () => new Size(100, 100)
});
}
_convertExternalConfig(externalConfig) {
if (isEmpty(externalConfig.itemIds))
throw new ArgumentException("TranslateItemsCommand: itemIds cannot be null.");
const items = this._productHandler.currentSurface
.getAllItems({ ignoreMockups: false, flatGroupItems: true })
.toArray()
.filter(i => externalConfig.itemIds.includes(i.id));
return {
items: items,
translateX: externalConfig.translateX,
translateY: externalConfig.translateY
};
}
redo() {
throw new NotImplementedException();
}
undo() {
throw new NotImplementedException();
}
}
//# sourceMappingURL=TranslateItemsCommand.js.map