From ed8d2f197903f617a32894ec667b1f9aae47cf85 Mon Sep 17 00:00:00 2001 From: Denis Nikiforov Date: Sun, 17 Nov 2024 02:27:27 +0300 Subject: [PATCH] [4206] Improve performance of diagram viewport change Bug: https://github.com/eclipse-sirius/sirius-web/issues/4206 Signed-off-by: Denis Nikiforov --- CHANGELOG.adoc | 1 + .../src/renderer/core/useViewportZoom.ts | 22 +++++++++++++++++++ .../src/renderer/edge/BendPoint.tsx | 8 +++---- .../src/renderer/layout/useArrangeAll.ts | 13 +++++------ 4 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 packages/diagrams/frontend/sirius-components-diagrams/src/renderer/core/useViewportZoom.ts diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 40bb582ebe0..b6d7befdc46 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -111,6 +111,7 @@ This is now fixed - https://github.com/eclipse-sirius/sirius-web/issues/4261[#4261] [core] Enable image cache - https://github.com/eclipse-sirius/sirius-web/issues/4225[#4225] [emf] Add support for localized enumeration literals in EMF property forms - https://github.com/eclipse-sirius/sirius-web/issues/4227[#4227] [emf] Add support for custom numeric and boolean types in property forms +- https://github.com/eclipse-sirius/sirius-web/issues/4206[#4206] [diagram] Improve performance of diagram viewport change diff --git a/packages/diagrams/frontend/sirius-components-diagrams/src/renderer/core/useViewportZoom.ts b/packages/diagrams/frontend/sirius-components-diagrams/src/renderer/core/useViewportZoom.ts new file mode 100644 index 00000000000..00dd9367d57 --- /dev/null +++ b/packages/diagrams/frontend/sirius-components-diagrams/src/renderer/core/useViewportZoom.ts @@ -0,0 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2024 Obeo and others. + * 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 type { ReactFlowState } from '@xyflow/react'; +import { useStore } from '@xyflow/react'; +import { shallow } from 'zustand/shallow'; + +const viewportZoomSelector = (state: ReactFlowState) => state.transform[2]; + +export function useViewportZoom(): number { + return useStore(viewportZoomSelector, shallow); +} diff --git a/packages/diagrams/frontend/sirius-components-diagrams/src/renderer/edge/BendPoint.tsx b/packages/diagrams/frontend/sirius-components-diagrams/src/renderer/edge/BendPoint.tsx index 59fa814bd0b..956ce19db57 100644 --- a/packages/diagrams/frontend/sirius-components-diagrams/src/renderer/edge/BendPoint.tsx +++ b/packages/diagrams/frontend/sirius-components-diagrams/src/renderer/edge/BendPoint.tsx @@ -10,13 +10,13 @@ * Contributors: * Obeo - initial API and implementation *******************************************************************************/ -import { useRef, RefObject } from 'react'; -import { useViewport } from '@xyflow/react'; +import { RefObject, useRef } from 'react'; import Draggable, { DraggableData } from 'react-draggable'; +import { useViewportZoom } from '../core/useViewportZoom'; import { BendPointProps, TemporaryBendPointProps } from './BendPoint.types'; export const BendPoint = ({ x, y, index, onDrag, onDragStop, onDoubleClick }: BendPointProps) => { - const { zoom } = useViewport(); + const zoom = useViewportZoom(); const nodeRef = useRef(null); return ( @@ -39,7 +39,7 @@ export const BendPoint = ({ x, y, index, onDrag, onDragStop, onDoubleClick }: Be }; export const TemporaryBendPoint = ({ x, y, index, onDrag, onDragStop }: TemporaryBendPointProps) => { - const { zoom } = useViewport(); + const zoom = useViewportZoom(); const nodeRef = useRef(null); return ( ): UseArrangeAllValue => { const { getNodes, getEdges, setNodes, setEdges } = useReactFlow, Edge>(); - const viewport = useViewport(); + const zoom = useViewportZoom(); const { layout } = useLayout(); const { synchronizeLayoutData } = useSynchronizeLayoutData(); const { diagramDescription } = useDiagramDescription(); @@ -140,7 +141,7 @@ export const useArrangeAll = (reactFlowWrapper: React.MutableRefObject ({ - labels: computeLabels(node, viewport.zoom, reactFlowWrapper), + labels: computeLabels(node, zoom, reactFlowWrapper), ...node, })), edges, @@ -182,11 +183,7 @@ export const useArrangeAll = (reactFlowWrapper: React.MutableRefObject[] = nodes .filter((node) => !node.data.isBorderNode) .map((node) => {