@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
110 lines • 4.64 kB
JavaScript
import { NotImplementedException } from "@aurigma/design-atoms-model/Exception";
import { BaseItemsCommand } from "./BaseItemsCommand";
import { ItemUtils } from "../../Utils/ItemUtils";
import { GroupItem, PlaceholderItem, LayoutItem } from "@aurigma/design-atoms-model/Product/Items";
import { SelectionCommand, SurfaceCommand } from "@aurigma/design-atoms-interfaces";
export class CloneItemsCommand extends BaseItemsCommand {
constructor(productHandler, historyArgs, args, _commandManager, _variableItemHelper) {
super(productHandler, historyArgs, args);
this._commandManager = _commandManager;
this._variableItemHelper = _variableItemHelper;
}
async _executeCommandBody() {
const targetItems = this._getTargetItems(this._args.items, this._args.query, this._args.queryOptions);
if (targetItems == null || targetItems.length == 0)
return;
const container = targetItems[0].parentContainer;
const group = targetItems[0].parentGroupItem;
const clones = targetItems.map((item) => this.cloneItem(item));
const indexes = [];
const cloneIndexes = [];
const orderIndexes = [];
const collectIndexes = (item, index) => {
const ind = targetItems.findIndex((_item) => _item === item);
if (ind > -1) {
indexes.push(index);
cloneIndexes.push({ item: clones[ind], index });
if (group instanceof LayoutItem) {
let orderIndex = group.getItemOrderIndex(item);
orderIndexes.push({ clone: clones[ind], orderIndex: orderIndex + 1 });
}
}
};
group ? group.items.toArray().forEach(collectIndexes) : container.items.toArray().forEach(collectIndexes);
const maxIndex = Math.max(...indexes) + 1;
const items = cloneIndexes
.sort((a, b) => a.index - b.index)
.reverse()
.map(({ item }) => item);
if (group) {
if (group instanceof LayoutItem) {
const sortedOrderIndexes = orderIndexes
.sort((a, b) => a.orderIndex - b.orderIndex)
.reverse();
sortedOrderIndexes.forEach(x => group.addItem(x.clone, null, x.orderIndex));
}
else
group.addItems(items, maxIndex);
}
else {
await this._commandManager.execute(SurfaceCommand.addItems, {
surface: container.parentComponent,
targetContainer: container,
items,
selectOnCanvas: true,
index: maxIndex,
ignoreOrderRules: null,
ignoreCanvasRotate: true,
createCloneName: true,
});
}
await this._commandManager.execute(SelectionCommand.selectItems, {
items: clones,
});
}
cloneItem(item) {
var clonedItem = item.clone(true);
let deltaX = 5;
let deltaY = 5;
const angle = this._productHandler.contentAngle;
if (angle > 0) {
if (angle === 90 || angle === 180)
deltaY = -5;
if (angle === 180 || angle === 270)
deltaX = -5;
}
const region = this._productHandler.regionForCurrentContainer;
const rect = ItemUtils.getItemBoundsForViolationCheck(this._productHandler.getHandler(item));
rect.left += deltaX;
rect.top += deltaY;
let diffX = 0;
let diffY = 0;
if (region != null && !region.containsRectangle(rect)) {
diffX = region.right - rect.right;
diffY = region.bottom - rect.bottom;
diffX = diffX < 0 ? diffX : 0;
diffY = diffY < 0 ? diffY : 0;
}
const moveItem = (item) => {
item.transform.move(deltaX + diffX, deltaY + diffY);
if (item instanceof PlaceholderItem)
item.updateContentAndFrames(moveItem);
if (item instanceof GroupItem)
item.applyToItems(moveItem);
};
moveItem(clonedItem);
for (var key in clonedItem) {
if (key.indexOf("$$") != -1) {
delete clonedItem[key];
}
}
return clonedItem;
}
redo() {
throw new NotImplementedException();
}
undo() {
throw new NotImplementedException();
}
}
//# sourceMappingURL=CloneItemsCommand.js.map