Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[2595] Call the layout on node move and resize
- Call the layout when the move or the resize has finished. - The layout is called only if the move is not a drop Bug: #2595 Signed-off-by: Guillaume Coutable <guillaume.coutable@obeo.fr>
- Loading branch information
1 parent
155ea2c
commit f6e3db8
Showing
16 changed files
with
312 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...irius-components-diagrams-reactflow/src/renderer/layout-events/useLayoutOnBoundsChange.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Obeo. | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
import { Node, NodeChange, useReactFlow } from 'reactflow'; | ||
import { EdgeData, NodeData } from '../DiagramRenderer.types'; | ||
import { useDropNode } from '../dropNode/useDropNode'; | ||
import { RawDiagram } from '../layout/layout.types'; | ||
import { useLayout } from '../layout/useLayout'; | ||
import { useSynchronizeLayoutData } from '../layout/useSynchronizeLayoutData'; | ||
import { DiagramNodeType } from '../node/NodeTypes.types'; | ||
import { UseLayoutOnBoundsChangeValue } from './useLayoutOnBoundsChange.types'; | ||
|
||
export const useLayoutOnBoundsChange = (refreshEventPayloadId: string): UseLayoutOnBoundsChangeValue => { | ||
const { getEdges, setNodes, setEdges } = useReactFlow<NodeData, EdgeData>(); | ||
const { layout } = useLayout(); | ||
const { hasDroppedNodeParentChanged } = useDropNode(); | ||
const { synchronizeLayoutData } = useSynchronizeLayoutData(); | ||
|
||
const isMoveFinished = (change: NodeChange): boolean => { | ||
return ( | ||
change.type === 'position' && | ||
typeof change.dragging === 'boolean' && | ||
!change.dragging && | ||
!hasDroppedNodeParentChanged() | ||
); | ||
}; | ||
const isResizeFinished = (change: NodeChange): boolean => | ||
change.type === 'dimensions' && typeof change.resizing === 'boolean' && !change.resizing; | ||
|
||
const isBoundsChangeFinished = (changes: NodeChange[]): NodeChange | undefined => { | ||
return changes.find((change) => isMoveFinished(change) || isResizeFinished(change)); | ||
}; | ||
|
||
const layoutOnBoundsChange = (changes: NodeChange[], nodes: Node<NodeData, DiagramNodeType>[]): void => { | ||
const change = isBoundsChangeFinished(changes); | ||
if (change) { | ||
const diagramToLayout: RawDiagram = { | ||
nodes, | ||
edges: getEdges(), | ||
}; | ||
|
||
layout(diagramToLayout, diagramToLayout, (laidOutDiagram) => { | ||
nodes.map((node) => { | ||
const existingNode = laidOutDiagram.nodes.find((laidoutNode) => laidoutNode.id === node.id); | ||
if (existingNode) { | ||
return { | ||
...node, | ||
position: existingNode.position, | ||
width: existingNode.width, | ||
height: existingNode.height, | ||
style: { | ||
...node.style, | ||
width: `${existingNode.width}px`, | ||
height: `${existingNode.height}px`, | ||
}, | ||
}; | ||
} | ||
return node; | ||
}); | ||
setNodes(nodes); | ||
setEdges(laidOutDiagram.edges); | ||
const finalDiagram: RawDiagram = { | ||
nodes: nodes, | ||
edges: laidOutDiagram.edges, | ||
}; | ||
|
||
synchronizeLayoutData(refreshEventPayloadId, finalDiagram); | ||
}); | ||
} | ||
}; | ||
|
||
return { layoutOnBoundsChange }; | ||
}; |
Oops, something went wrong.