diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index e6e3ab7475..6d3a7ba973 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -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 diff --git a/packages/diagrams/frontend/sirius-components-diagrams-reactflow/src/index.ts b/packages/diagrams/frontend/sirius-components-diagrams-reactflow/src/index.ts index e05792d10a..ce7c5f850b 100644 --- a/packages/diagrams/frontend/sirius-components-diagrams-reactflow/src/index.ts +++ b/packages/diagrams/frontend/sirius-components-diagrams-reactflow/src/index.ts @@ -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'; diff --git a/packages/diagrams/frontend/sirius-components-diagrams-reactflow/src/renderer/palette/usePaletteReferencePosition.tsx b/packages/diagrams/frontend/sirius-components-diagrams-reactflow/src/renderer/palette/usePaletteReferencePosition.tsx new file mode 100644 index 0000000000..7c85aea2ea --- /dev/null +++ b/packages/diagrams/frontend/sirius-components-diagrams-reactflow/src/renderer/palette/usePaletteReferencePosition.tsx @@ -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, + }; +}; diff --git a/packages/diagrams/frontend/sirius-components-diagrams-reactflow/src/renderer/palette/usePaletteReferencePosition.types.ts b/packages/diagrams/frontend/sirius-components-diagrams-reactflow/src/renderer/palette/usePaletteReferencePosition.types.ts new file mode 100644 index 0000000000..5f2fba0a02 --- /dev/null +++ b/packages/diagrams/frontend/sirius-components-diagrams-reactflow/src/renderer/palette/usePaletteReferencePosition.types.ts @@ -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; +}