Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[2532] Allows border to move on all parent node sizes
Bug: #2532 Signed-off-by: Florian ROUËNÉ <florian.rouene@obeosoft.com>
- Loading branch information
Showing
5 changed files
with
78 additions
and
32 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
50 changes: 50 additions & 0 deletions
50
...ams/frontend/sirius-components-diagrams-reactflow/src/renderer/border/useBorderChange.tsx
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,50 @@ | ||
/******************************************************************************* | ||
* 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 { useCallback } from 'react'; | ||
import { Node, NodeChange, useReactFlow, XYPosition } from 'reactflow'; | ||
import { UseBorderChangeValue } from './useBorderChange.types'; | ||
|
||
const isNewPositionInsideIsParent = (newNodePosition: XYPosition, movedNode: Node, parentNode: Node): boolean => { | ||
if (movedNode.width && movedNode.height && parentNode?.positionAbsolute && parentNode.width && parentNode.height) { | ||
return ( | ||
parentNode.positionAbsolute.x < newNodePosition.x + movedNode.width && | ||
parentNode.positionAbsolute.x + parentNode.width > newNodePosition.x && | ||
parentNode.positionAbsolute.y < newNodePosition.y + movedNode.height && | ||
parentNode.positionAbsolute.y + parentNode.height > newNodePosition.y | ||
); | ||
} | ||
return false; | ||
}; | ||
|
||
export const useBorderChange = (): UseBorderChangeValue => { | ||
const { getNodes } = useReactFlow(); | ||
|
||
const onBorderChange = useCallback((changes: NodeChange[]): NodeChange[] => { | ||
return changes.map((change) => { | ||
if (change.type === 'position' && change.positionAbsolute) { | ||
const movedNode = getNodes().find((node) => change.id === node.id); | ||
if (movedNode?.data.isBorderNode) { | ||
const parentNode = getNodes().find((node) => movedNode?.parentNode === node.id); | ||
if (parentNode && isNewPositionInsideIsParent(change.positionAbsolute, movedNode, parentNode)) { | ||
//Invalid position, reset to the initial one | ||
change.position = movedNode.position; | ||
change.positionAbsolute = movedNode.positionAbsolute; | ||
} | ||
} | ||
} | ||
return change; | ||
}); | ||
}, []); | ||
|
||
return { onBorderChange }; | ||
}; |
17 changes: 17 additions & 0 deletions
17
...rontend/sirius-components-diagrams-reactflow/src/renderer/border/useBorderChange.types.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,17 @@ | ||
/******************************************************************************* | ||
* 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 { NodeChange } from 'reactflow'; | ||
|
||
export interface UseBorderChangeValue { | ||
onBorderChange: (changes: NodeChange[]) => NodeChange[]; | ||
} |
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