@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
69 lines • 3.61 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 UngroupItemsCommand extends BaseItemsCommand {
constructor(productHandler, historyArgs, args, _commandManager) {
super(productHandler, historyArgs, args);
this._commandManager = _commandManager;
}
async _executeCommandBody() {
const { keepNestedGroups } = this._args;
const items = this._getTargetItems(this._args.items, this._args.query, this._args.queryOptions);
await this._ungroupItems(items, keepNestedGroups);
}
async _ungroupItems(items, keepNestedGroups = false) {
await this._commandManager.execute(SelectionCommand.clearSelection);
const { history } = this._historyArgs;
if (items.length === 0)
return;
if (!keepNestedGroups && items.every(item => item instanceof GroupItem && item.groupItemPermissions.allowUngroup)) {
history.pause();
items.forEach((item) => this._ungroupItems(item.items.toArray(), true));
history.resume();
return;
}
if (!items.every((item, _i, [firstItem]) => item.parentGroupItem != null && item.parentGroupItem === firstItem.parentGroupItem))
return;
history.pause();
const itemsFromGroup = await this._extractItemsFromGroupItem(items);
history.resume();
await this._commandManager.execute(SelectionCommand.selectItems, { items: itemsFromGroup });
}
async _extractItemsFromGroupItem(items) {
const groupItem = items[0].parentGroupItem;
if (groupItem.items.toArray().filter(item => !items.includes(item)).length < 2)
items = groupItem.items.toArray();
const itemsFromGroup = items.map(item => item.clone()).reverse();
const { parentContainer: targetContainer, parentGroupItem: targetGroupItem } = groupItem;
const targetItemsArray = targetGroupItem != null ? targetGroupItem.items.toArray() : targetContainer.items.toArray();
const targetIndex = targetItemsArray.indexOf(groupItem);
let targetOrderIndex = null;
if (targetGroupItem instanceof LayoutItem) {
const targetGroupitemsOrders = targetGroupItem.getItemsOrder();
if (targetGroupitemsOrders) {
const groupItemId = targetGroupitemsOrders.indexOf(groupItem.id);
targetOrderIndex = groupItemId;
}
}
await this._commandManager.execute(ItemsCommand.deleteItems, { items: items, force: true, autoUngroup: false, autoParentGroupDelete: true });
if (groupItem.items.length === 0)
await this._commandManager.execute(ItemsCommand.deleteItems, { items: [groupItem], force: true, autoUngroup: false });
await this._commandManager.execute(SurfaceCommand.addItems, {
targetContainer: targetContainer,
items: itemsFromGroup,
index: targetIndex,
orderIndex: targetOrderIndex,
selectOnCanvas: false,
targetGroupItem: targetGroupItem
});
return itemsFromGroup;
}
redo() {
throw new NotImplementedException();
}
undo() {
throw new NotImplementedException();
}
}
//# sourceMappingURL=UngroupItemsCommand.js.map