Skip to content

Commit

Permalink
[2581] Allow custom tools to control their creation position
Browse files Browse the repository at this point in the history
Bug: #2581
Signed-off-by: Florian ROUËNÉ <florian.rouene@obeosoft.com>
  • Loading branch information
frouene authored and pcdavid committed Nov 27, 2023
1 parent 155ea2c commit a7a7fcc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Expand Up @@ -121,6 +121,7 @@ It uses our algorithm instead of elk.js to perform the arrange-all.
- https://github.com/eclipse-sirius/sirius-web/issues/2625[#2625] [diagram] Fix the line style computation on edge and border node.
- https://github.com/eclipse-sirius/sirius-web/issues/2633[#2633] [diagram] Make sure the grid is visible when enabled, even when moving a non-droppable node
- https://github.com/eclipse-sirius/sirius-web/issues/2635[#2635] [diagram] Fix an error when switching between react flow representations.
- https://github.com/eclipse-sirius/sirius-web/issues/2581[#2581] [diagram] Fix an issue where nodes created by custom tool are always on the default position.

=== New Features

Expand Down
Expand Up @@ -45,3 +45,5 @@ export { DiagramPaletteToolContext } from './renderer/palette/DiagramPaletteTool
export { DiagramPaletteToolContribution } from './renderer/palette/DiagramPaletteToolContribution';
export type { DiagramPaletteToolContributionComponentProps } from './renderer/palette/DiagramPaletteToolContribution.types';
export { DiagramRepresentation } from './representation/DiagramRepresentation';
export { useLayout } from './renderer/layout/useLayout';
export { usePaletteReferencePosition } from './renderer/palette/usePaletteReferencePosition';
@@ -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,
};
};
@@ -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;
}

0 comments on commit a7a7fcc

Please sign in to comment.