refactor(nodes): always render nodes that are dragged closes #477

This commit is contained in:
moklick
2020-09-11 17:12:09 +02:00
parent d17e6ebaea
commit 8a0a928e17
6 changed files with 33 additions and 21 deletions
+4
View File
@@ -154,6 +154,10 @@ export default (NodeComponent: ComponentType<NodeComponentProps>) => {
} }
setDragging(false); setDragging(false);
updateNodePosDiff({
id,
isDragging: false,
});
onNodeDragStop?.(event as MouseEvent, node); onNodeDragStop?.(event as MouseEvent, node);
}, },
+7
View File
@@ -73,6 +73,13 @@ export default ({
const onStop = useCallback( const onStop = useCallback(
(event: MouseEvent) => { (event: MouseEvent) => {
selectedNodes?.forEach((node) => {
updateNodePosDiff({
id: node.id,
isDragging: false,
});
});
onSelectionDragStop?.(event, selectedNodes); onSelectionDragStop?.(event, selectedNodes);
}, },
[selectedNodes, onSelectionDragStop] [selectedNodes, onSelectionDragStop]
+1
View File
@@ -49,6 +49,7 @@ function renderNode(
data={node.data} data={node.data}
xPos={node.__rf.position.x} xPos={node.__rf.position.x}
yPos={node.__rf.position.y} yPos={node.__rf.position.y}
isDragging={node.__rf.isDragging}
onClick={props.onElementClick} onClick={props.onElementClick}
onMouseEnter={props.onNodeMouseEnter} onMouseEnter={props.onNodeMouseEnter}
onMouseMove={props.onNodeMouseMove} onMouseMove={props.onNodeMouseMove}
+15 -18
View File
@@ -227,11 +227,9 @@ export const storeModel: StoreModel = {
state.elements.forEach((n) => { state.elements.forEach((n) => {
if (n.id === id && isNode(n)) { if (n.id === id && isNode(n)) {
n.__rf = { n.__rf.width = dimensions.width;
...n.__rf, n.__rf.height = dimensions.height;
...dimensions, n.__rf.handleBounds = handleBounds;
handleBounds,
};
} }
}); });
}), }),
@@ -249,24 +247,21 @@ export const storeModel: StoreModel = {
state.elements.forEach((n) => { state.elements.forEach((n) => {
if (n.id === id && isNode(n)) { if (n.id === id && isNode(n)) {
n.__rf = { n.__rf.position = position;
...n.__rf,
position,
};
} }
}); });
}), }),
updateNodePosDiff: action((state, { id, diff }) => { updateNodePosDiff: action((state, { id, diff = null, isDragging = true }) => {
state.elements.forEach((n) => { state.elements.forEach((n) => {
if (n.id === id && isNode(n)) { if (n.id === id && isNode(n)) {
n.__rf = { if (diff) {
...n.__rf, n.__rf.position = {
position: {
x: n.__rf.position.x + diff.x, x: n.__rf.position.x + diff.x,
y: n.__rf.position.y + diff.y, y: n.__rf.position.y + diff.y,
}, };
}; }
n.__rf.isDragging = isDragging;
} }
}); });
}), }),
@@ -316,7 +311,7 @@ export const storeModel: StoreModel = {
if (!selectedNodes) { if (!selectedNodes) {
state.selectionActive = false; state.selectionActive = false;
state.userSelectionRect = { ...state.userSelectionRect, draw: false }; state.userSelectionRect.draw = false;
state.nodesSelectionActive = false; state.nodesSelectionActive = false;
state.selectedElements = null; state.selectedElements = null;
@@ -328,7 +323,7 @@ export const storeModel: StoreModel = {
state.nodesSelectionActive = true; state.nodesSelectionActive = true;
state.selectedNodesBbox = selectedNodesBbox; state.selectedNodesBbox = selectedNodesBbox;
state.userSelectionRect = { ...state.userSelectionRect, draw: false }; state.userSelectionRect.draw = false;
state.selectionActive = false; state.selectionActive = false;
}), }),
@@ -454,7 +449,9 @@ export const storeModel: StoreModel = {
// we need to sync the d3 zoom transform with the fitted transform // we need to sync the d3 zoom transform with the fitted transform
d3Selection.property('__zoom', fittedTransform); d3Selection.property('__zoom', fittedTransform);
state.transform = [fittedTransform.x, fittedTransform.y, fittedTransform.k]; state.transform[0] = fittedTransform.x;
state.transform[1] = fittedTransform.y;
state.transform[2] = fittedTransform.k;
}), }),
zoomTo: action((state, zoomLevel) => { zoomTo: action((state, zoomLevel) => {
+3 -1
View File
@@ -174,6 +174,7 @@ export interface WrapNodeProps {
isInitialized?: boolean; isInitialized?: boolean;
snapToGrid?: boolean; snapToGrid?: boolean;
snapGrid?: [number, number]; snapGrid?: [number, number];
isDragging?: boolean;
} }
export type FitViewParams = { export type FitViewParams = {
@@ -271,7 +272,8 @@ export type NodePosUpdate = {
export type NodeDiffUpdate = { export type NodeDiffUpdate = {
id: ElementId; id: ElementId;
diff: XYPosition; diff?: XYPosition;
isDragging?: boolean;
}; };
export type FlowTransform = { export type FlowTransform = {
+3 -2
View File
@@ -126,6 +126,7 @@ export const parseElement = (element: Node | Edge): Node | Edge => {
width: null, width: null,
height: null, height: null,
handleBounds: {}, handleBounds: {},
isDragging: false,
}, },
} as Node; } as Node;
}; };
@@ -182,13 +183,13 @@ export const getNodesInside = (
height: rect.height / tScale, height: rect.height / tScale,
}); });
return nodes.filter(({ __rf: { position, width, height } }) => { return nodes.filter(({ __rf: { position, width, height, isDragging } }) => {
const nBox = rectToBox({ ...position, width, height }); const nBox = rectToBox({ ...position, width, height });
const xOverlap = Math.max(0, Math.min(rBox.x2, nBox.x2) - Math.max(rBox.x, nBox.x)); const xOverlap = Math.max(0, Math.min(rBox.x2, nBox.x2) - Math.max(rBox.x, nBox.x));
const yOverlap = Math.max(0, Math.min(rBox.y2, nBox.y2) - Math.max(rBox.y, nBox.y)); const yOverlap = Math.max(0, Math.min(rBox.y2, nBox.y2) - Math.max(rBox.y, nBox.y));
const overlappingArea = xOverlap * yOverlap; const overlappingArea = xOverlap * yOverlap;
if (width === null || height === null) { if (width === null || height === null || isDragging) {
// at the beginnning all nodes have width & height === 0 // at the beginnning all nodes have width & height === 0
return true; return true;
} }