Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[2581] Allow custom tools to control their creation position
Bug: #2581 Signed-off-by: Florian ROUËNÉ <florian.rouene@obeosoft.com>
- Loading branch information
Showing
4 changed files
with
59 additions
and
0 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
40 changes: 40 additions & 0 deletions
40
...sirius-components-diagrams-reactflow/src/renderer/palette/usePaletteReferencePosition.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,40 @@ | ||
/******************************************************************************* | ||
* 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 { useViewport } from 'reactflow'; | ||
import { UsePaletteReferencePositionValue } from './usePaletteReferencePosition.types'; | ||
import { useDiagramElementPalette } from './useDiagramElementPalette'; | ||
import { useDiagramPalette } from './useDiagramPalette'; | ||
|
||
export const usePaletteReferencePosition = (): UsePaletteReferencePositionValue => { | ||
const { x: viewportX, y: viewportY, zoom: viewportZoom } = useViewport(); | ||
const { x: paletteElementX, y: paletteElementY, isOpened: isPaletteElementOpened } = useDiagramElementPalette(); | ||
const { x: paletteX, y: paletteY, isOpened: isPaletteOpened } = useDiagramPalette(); | ||
|
||
let x: number | null = null; | ||
let y: number | null = null; | ||
|
||
if (viewportZoom !== 0 && isPaletteOpened && paletteX && paletteY) { | ||
x = (paletteX - viewportX) / viewportZoom; | ||
y = (paletteY - viewportY) / viewportZoom; | ||
} | ||
|
||
if (viewportZoom !== 0 && isPaletteElementOpened && paletteElementX && paletteElementY) { | ||
x = (paletteElementX - viewportX) / viewportZoom; | ||
y = (paletteElementY - viewportY) / viewportZoom; | ||
} | ||
|
||
return { | ||
x, | ||
y, | ||
}; | ||
}; |
16 changes: 16 additions & 0 deletions
16
...s-components-diagrams-reactflow/src/renderer/palette/usePaletteReferencePosition.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,16 @@ | ||
/******************************************************************************* | ||
* 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 | ||
*******************************************************************************/ | ||
export interface UsePaletteReferencePositionValue { | ||
x: number | null; | ||
y: number | null; | ||
} |