refactor(selection): mouse handling

This commit is contained in:
moklick
2019-07-24 17:12:44 +02:00
parent 5fc84bf12c
commit d3929c19a6
+18 -20
View File
@@ -31,8 +31,8 @@ export default () => {
function onMouseDown(evt) { function onMouseDown(evt) {
const mousePos = getMousePosition(evt); const mousePos = getMousePosition(evt);
setRect((r) => ({ setRect((currentRect) => ({
...r, ...currentRect,
startX: mousePos.x, startX: mousePos.x,
startY: mousePos.y, startY: mousePos.y,
x: mousePos.x, x: mousePos.x,
@@ -44,20 +44,20 @@ export default () => {
} }
function onMouseMove(evt) { function onMouseMove(evt) {
setRect((r) => { setRect((currentRect) => {
if (!r.draw) { if (!currentRect.draw) {
return r; return currentRect;
} }
const mousePos = getMousePosition(evt); const mousePos = getMousePosition(evt);
const negativeX = mousePos.x < r.startX; const negativeX = mousePos.x < currentRect.startX;
const negativeY = mousePos.y < r.startY; const negativeY = mousePos.y < currentRect.startY;
const nextRect = { const nextRect = {
...r, ...currentRect,
x: negativeX ? mousePos.x : r.x, x: negativeX ? mousePos.x : currentRect.x,
y: negativeY ? mousePos.y : r.y, y: negativeY ? mousePos.y : currentRect.y,
width: negativeX ? r.startX - mousePos.x : mousePos.x - r.startX, width: negativeX ? currentRect.startX - mousePos.x : mousePos.x - currentRect.startX,
height: negativeY ? r.startY - mousePos.y : mousePos.y - r.startY, height: negativeY ? currentRect.startY - mousePos.y : mousePos.y - currentRect.startY,
}; };
dispatch(updateSelection(nextRect)); dispatch(updateSelection(nextRect));
@@ -67,16 +67,14 @@ export default () => {
} }
function onMouseUp() { function onMouseUp() {
setRect((r) => { setRect((currentRect) => {
const nextRect = { dispatch(setNodesSelection({ isActive: true, selection: currentRect }));
...r,
fixed: true
};
dispatch(setNodesSelection({ isActive: true, selection: nextRect }));
dispatch(setSelection(false)); dispatch(setSelection(false));
return nextRect; return {
...currentRect,
draw: false
};
}); });
} }