refactor(node-changes): add dragging flag closes #2171

This commit is contained in:
moklick
2022-05-26 18:40:39 +02:00
parent 101f5d25d0
commit b6eb0acb30
6 changed files with 30 additions and 17 deletions
+13 -9
View File
@@ -42,7 +42,7 @@ function useDrag({
const lastPos = useRef<{ x: number | null; y: number | null }>({ x: null, y: null }); const lastPos = useRef<{ x: number | null; y: number | null }>({ x: null, y: null });
const parentPos = useRef<XYPosition>({ x: 0, y: 0 }); const parentPos = useRef<XYPosition>({ x: 0, y: 0 });
// returns the mouse position projected to the RF coordinate system // returns the pointer position projected to the RF coordinate system
const getPointerPosition = useCallback(({ sourceEvent }: UseDragEvent) => { const getPointerPosition = useCallback(({ sourceEvent }: UseDragEvent) => {
const { transform, snapGrid, snapToGrid } = store.getState(); const { transform, snapGrid, snapToGrid } = store.getState();
const x = sourceEvent.touches ? sourceEvent.touches[0].clientX : sourceEvent.clientX; const x = sourceEvent.touches ? sourceEvent.touches[0].clientX : sourceEvent.clientX;
@@ -104,7 +104,7 @@ function useDrag({
updatePosition(n, pointerPos, nodeInternals, nodeExtent) updatePosition(n, pointerPos, nodeInternals, nodeExtent)
); );
updateNodePositions(dragItems.current); updateNodePositions(dragItems.current, true, true);
setDragging(true); setDragging(true);
if (onDrag) { if (onDrag) {
@@ -118,13 +118,17 @@ function useDrag({
} }
event.on('end', (event) => { event.on('end', (event) => {
if (onStop && dragItems.current) { if (dragItems.current) {
const [currentNode, nodes] = getEventHandlerParams({ updateNodePositions(dragItems.current, false, false);
nodeId,
dragItems: dragItems.current, if (onStop) {
nodeInternals, const [currentNode, nodes] = getEventHandlerParams({
}); nodeId,
onStop(event.sourceEvent as MouseEvent, currentNode, nodes); dragItems: dragItems.current,
nodeInternals,
});
onStop(event.sourceEvent as MouseEvent, currentNode, nodes);
}
} }
}); });
}) })
+9 -6
View File
@@ -89,19 +89,22 @@ const createStore = () =>
onNodesChange?.(changes); onNodesChange?.(changes);
} }
}, },
updateNodePositions: (nodeDragItems: NodeDragItem[]) => { updateNodePositions: (nodeDragItems: NodeDragItem[], positionChanged = true, dragging = false) => {
const { onNodesChange, nodeInternals, hasDefaultNodes } = get(); const { onNodesChange, nodeInternals, hasDefaultNodes } = get();
if (hasDefaultNodes || onNodesChange) { if (hasDefaultNodes || onNodesChange) {
const changes: NodePositionChange[] = []; const changes = nodeDragItems.map((node) => {
nodeDragItems.forEach((node) => {
const change: NodePositionChange = { const change: NodePositionChange = {
id: node.id, id: node.id,
type: 'position', type: 'position',
position: node.position, dragging,
}; };
changes.push(change);
if (positionChanged) {
change.position = node.position;
}
return change;
}); });
if (changes?.length) { if (changes?.length) {
+1
View File
@@ -13,6 +13,7 @@ export type NodePositionChange = {
id: string; id: string;
type: 'position'; type: 'position';
position?: XYPosition; position?: XYPosition;
dragging?: boolean;
}; };
export type NodeSelectionChange = { export type NodeSelectionChange = {
+1 -1
View File
@@ -190,7 +190,7 @@ export type ReactFlowActions = {
setEdges: (edges: Edge[]) => void; setEdges: (edges: Edge[]) => void;
setDefaultNodesAndEdges: (nodes?: Node[], edges?: Edge[]) => void; setDefaultNodesAndEdges: (nodes?: Node[], edges?: Edge[]) => void;
updateNodeDimensions: (updates: NodeDimensionUpdate[]) => void; updateNodeDimensions: (updates: NodeDimensionUpdate[]) => void;
updateNodePositions: (nodeDragItems: NodeDragItem[]) => void; updateNodePositions: (nodeDragItems: NodeDragItem[], positionChanged: boolean, dragging: boolean) => void;
resetSelectedElements: () => void; resetSelectedElements: () => void;
unselectNodesAndEdges: (params?: UnselectNodesAndEdgesParams) => void; unselectNodesAndEdges: (params?: UnselectNodesAndEdgesParams) => void;
addSelectedNodes: (nodeIds: string[]) => void; addSelectedNodes: (nodeIds: string[]) => void;
+1
View File
@@ -117,4 +117,5 @@ export type NodeDragItem = {
height?: number | null; height?: number | null;
extent?: 'parent' | CoordinateExtent; extent?: 'parent' | CoordinateExtent;
parentNode?: string; parentNode?: string;
dragging?: boolean;
}; };
+5 -1
View File
@@ -49,7 +49,7 @@ function applyChanges(changes: any[], elements: any[]): any[] {
if (changes.some((c) => c.type === 'reset')) { if (changes.some((c) => c.type === 'reset')) {
return changes.filter((c) => c.type === 'reset').map((c) => c.item); return changes.filter((c) => c.type === 'reset').map((c) => c.item);
} }
console.log(changes);
const initElements: any[] = changes.filter((c) => c.type === 'add').map((c) => c.item); const initElements: any[] = changes.filter((c) => c.type === 'add').map((c) => c.item);
return elements.reduce((res: any[], item: any) => { return elements.reduce((res: any[], item: any) => {
@@ -68,6 +68,10 @@ function applyChanges(changes: any[], elements: any[]): any[] {
updateItem.position = currentChange.position; updateItem.position = currentChange.position;
} }
if (typeof currentChange.dragging !== 'undefined') {
updateItem.dragging = currentChange.dragging;
}
if (updateItem.expandParent) { if (updateItem.expandParent) {
handleParentExpand(res, updateItem); handleParentExpand(res, updateItem);
} }