homemanager-fe/src/modules/house-planner/clipboard.ts

45 lines
1.1 KiB
TypeScript

import deepUnref from '../../utils/deep-unref';
import { LayerObject } from './interfaces';
export class HousePlannerCanvasClipboard {
public storedObjects: LayerObject[] = [];
public wasCutOperation = false;
storeToClipboard(items: LayerObject[], cut = false) {
this.wasCutOperation = cut;
// If we are cutting, we store the object by reference and return it all in its original state
if (cut) {
this.storedObjects = [...items];
return;
}
this.storedObjects = [
...items.map((item) => {
const itemCopy = {
...deepUnref(item),
visible: true,
name: item.name + ' Copy',
};
delete itemCopy.databaseId;
return itemCopy;
}),
];
}
getFromClipboard(newId: number, selected = true): LayerObject[] {
if (this.wasCutOperation) {
const unalteredList = [...this.storedObjects];
this.storeToClipboard(this.storedObjects, false);
return unalteredList;
}
const newObjects = deepUnref(this.storedObjects);
return newObjects.map((item, index) => ({
...item,
id: newId + index,
selected,
}));
}
}