refactor(store): simplify setElement
This commit is contained in:
+11
-19
@@ -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));
|
||||
}
|
||||
});
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user