refactor(store): simplify setElement

This commit is contained in:
moklick
2020-11-24 18:21:36 +01:00
parent c00cfca02c
commit 52a73d68a0
3 changed files with 38 additions and 24 deletions
+11 -19
View File
@@ -213,30 +213,22 @@ export const storeModel: StoreModel = {
// update existing element
if (storeElementIndex !== -1) {
const storeElement = state.elements[storeElementIndex];
let storeElement = state.elements[storeElementIndex];
if (isNode(storeElement)) {
let storeNode = storeElement as Node;
if (isNode(state.elements[storeElementIndex])) {
const propNode = el as Node;
const storeNode = state.elements[storeElementIndex] as Node;
const positionChanged =
storeNode.position.x !== propNode.position.x || storeNode.position.y !== propNode.position.y;
state.elements[storeElementIndex] = {
...storeNode,
...propNode,
};
if (positionChanged) {
(state.elements[storeElementIndex] as Node) = {
...storeElement,
...el,
__rf: {
...storeNode.__rf,
position: propNode.position,
},
position: propNode.position,
};
} else {
state.elements[storeElementIndex] = {
...storeElement,
...el,
};
(state.elements[storeElementIndex] as Node).__rf.position = propNode.position;
}
// we reset the elements dimensions here in order to force a re-calculation of the bounds.
// When the type of a node changes it is possible that the number or positions of handles changes too.
@@ -252,7 +244,7 @@ export const storeModel: StoreModel = {
}
} else {
// add new element
state.elements.push(parseElement({ ...el }));
state.elements.push(parseElement(el));
}
});
}),