@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
55 lines • 2.88 kB
JavaScript
import { NotImplementedException } from "@aurigma/design-atoms-model/Exception";
import { BaseItemsCommand } from "./BaseItemsCommand";
import { GroupItem, LayoutItem } from "@aurigma/design-atoms-model/Product/Items";
import { ItemsCommand, SelectionCommand, SurfaceCommand } from "@aurigma/design-atoms-interfaces";
export class GroupItemsCommand extends BaseItemsCommand {
constructor(productHandler, historyArgs, args, _commandManager) {
super(productHandler, historyArgs, args);
this._commandManager = _commandManager;
}
async _executeCommandBody() {
var _a;
const { asLayoutItem } = this._args;
const items = this._getTargetItems(this._args.items, this._args.query, this._args.queryOptions);
if (items == null || items.length === 0)
return;
const [{ parentContainer: targetContainer, parentGroupItem: targetGroupItem }] = items;
const targetItemsArray = (targetGroupItem != null ? targetGroupItem.items : targetContainer.items).toArray();
if (items.some(item => item.parentGroupItem !== targetGroupItem))
return;
const targetIndex = Math.max(...items.map((item) => targetItemsArray.indexOf(item))) - items.length + 1;
const sortedItems = items.sort((item, otherItem) => targetItemsArray.indexOf(item) - targetItemsArray.indexOf(otherItem));
const clonedItems = sortedItems.map(item => item.clone());
const groupItem = asLayoutItem
? new LayoutItem(clonedItems)
: new GroupItem(clonedItems);
groupItem.name = (_a = this._args.name) !== null && _a !== void 0 ? _a : "Group item";
const targetOrderIndex = this._getTargetOrderIndex(groupItem, targetGroupItem);
this._commandManager.execute(SelectionCommand.clearSelection);
this._commandManager.execute(ItemsCommand.deleteItems, { items: items, force: true, autoUngroup: false, autoParentGroupDelete: false });
await this._commandManager.execute(SurfaceCommand.addItems, {
items: [groupItem],
targetContainer: targetContainer,
index: targetIndex,
selectOnCanvas: true,
targetGroupItem: targetGroupItem,
orderIndex: targetOrderIndex
});
}
redo() {
throw new NotImplementedException();
}
undo() {
throw new NotImplementedException();
}
_getTargetOrderIndex(itemToInsert, targetGroupItem) {
if (targetGroupItem instanceof LayoutItem) {
const targetGroupitemsOrders = targetGroupItem.getItemsOrder();
if (targetGroupitemsOrders != null) {
return targetGroupitemsOrders.indexOf(itemToInsert.items.first().id);
}
}
return null;
}
}
//# sourceMappingURL=GroupItemsCommand.js.map