Skip to content

Commit

Permalink
[4206] Improve performance of diagram viewport change
Browse files Browse the repository at this point in the history
Bug: #4206
Signed-off-by: Denis Nikiforov <[email protected]>
  • Loading branch information
AresEkb committed Dec 14, 2024
1 parent cdcdfa2 commit ed8d2f1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<SVGCircleElement>(null);

return (
Expand All @@ -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<SVGCircleElement>(null);
return (
<Draggable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
* Obeo - initial API and implementation
*******************************************************************************/
import { useMultiToast } from '@eclipse-sirius/sirius-components-core';
import { Edge, Node, useReactFlow, useViewport } from '@xyflow/react';
import { Edge, Node, useReactFlow } from '@xyflow/react';
import { LayoutOptions } from 'elkjs/lib/elk-api';
import ELK, { ElkLabel, ElkNode } from 'elkjs/lib/elk.bundled';
import { useContext } from 'react';
import { DiagramContext } from '../../contexts/DiagramContext';
import { DiagramContextValue } from '../../contexts/DiagramContext.types';
import { useDiagramDescription } from '../../contexts/useDiagramDescription';
import { useViewportZoom } from '../core/useViewportZoom';
import { EdgeData, NodeData } from '../DiagramRenderer.types';
import { ListNodeData } from '../node/ListNode.types';
import { DiagramNodeType } from '../node/NodeTypes.types';
Expand Down Expand Up @@ -119,7 +120,7 @@ const computeLabels = (

export const useArrangeAll = (reactFlowWrapper: React.MutableRefObject<HTMLDivElement | null>): UseArrangeAllValue => {
const { getNodes, getEdges, setNodes, setEdges } = useReactFlow<Node<NodeData>, Edge<EdgeData>>();
const viewport = useViewport();
const zoom = useViewportZoom();
const { layout } = useLayout();
const { synchronizeLayoutData } = useSynchronizeLayoutData();
const { diagramDescription } = useDiagramDescription();
Expand All @@ -140,7 +141,7 @@ export const useArrangeAll = (reactFlowWrapper: React.MutableRefObject<HTMLDivEl
id: parentNodeId,
layoutOptions: options,
children: nodes.map((node) => ({
labels: computeLabels(node, viewport.zoom, reactFlowWrapper),
labels: computeLabels(node, zoom, reactFlowWrapper),
...node,
})),
edges,
Expand Down Expand Up @@ -182,11 +183,7 @@ export const useArrangeAll = (reactFlowWrapper: React.MutableRefObject<HTMLDivEl
layoutedAllNodes = [...layoutedAllNodes, ...nodes.reverse()];
continue;
}
const headerVerticalFootprint: number = computeHeaderVerticalFootprint(
parentNode,
viewport.zoom,
reactFlowWrapper
);
const headerVerticalFootprint: number = computeHeaderVerticalFootprint(parentNode, zoom, reactFlowWrapper);
const subGroupNodes: Node<NodeData>[] = nodes
.filter((node) => !node.data.isBorderNode)
.map((node) => {
Expand Down

0 comments on commit ed8d2f1

Please sign in to comment.