Skip to content

Commit

Permalink
[2728] Prevents unnecessary rerenders in DiagramPanel
Browse files Browse the repository at this point in the history
Bug: #2728
Signed-off-by: Michaël Charfadi <michael.charfadi@obeosoft.com>
  • Loading branch information
mcharfadi committed Dec 1, 2023
1 parent 461d4a0 commit 25ef685
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ The only way to export diagram as SVG image is to use the ReactFlow toolbar loca
- https://github.com/eclipse-sirius/sirius-web/issues/2687[#2687] [diagram] Fix an issue where ConnectionHandles where rerendering too much
- https://github.com/eclipse-sirius/sirius-web/issues/2698[#2698] [diagram] Fix an issue that prevents _imageNode_ to be resizable.
- https://github.com/eclipse-sirius/sirius-web/issues/2684[#2684] [diagram] Fix an issue where the edges' arrows where not exported during the SVG export.
- https://github.com/eclipse-sirius/sirius-web/issues/2728[#2728] [diagram] Prevents unnecessary rerenders in DiagramPanel

=== New Features

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import TonalityIcon from '@material-ui/icons/Tonality';
import VisibilityOffIcon from '@material-ui/icons/VisibilityOff';
import ZoomInIcon from '@material-ui/icons/ZoomIn';
import ZoomOutIcon from '@material-ui/icons/ZoomOut';
import { useState } from 'react';
import { Node, Panel, useNodes, useReactFlow } from 'reactflow';
import { memo, useState } from 'react';
import { Panel, useReactFlow } from 'reactflow';
import { EdgeData, NodeData } from '../DiagramRenderer.types';
import { ShareDiagramDialog } from '../ShareDiagramDialog';
import { useFadeDiagramElements } from '../fade/useFadeDiagramElements';
Expand All @@ -34,17 +34,20 @@ import { useHideDiagramElements } from '../hide/useHideDiagramElements';
import { DiagramPanelProps, DiagramPanelState } from './DiagramPanel.types';
import { useExportToImage } from './useExportToImage';

export const DiagramPanel = ({ snapToGrid, onSnapToGrid }: DiagramPanelProps) => {
export const DiagramPanel = memo(({ snapToGrid, onSnapToGrid }: DiagramPanelProps) => {
const [state, setState] = useState<DiagramPanelState>({
dialogOpen: null,
});

const nodes = useNodes<NodeData>();
const reactFlow = useReactFlow<NodeData, EdgeData>();
const { getEdges, getNodes } = reactFlow;

const getAllElementsIds = () => [...getNodes().map((elem) => elem.id), ...getEdges().map((elem) => elem.id)];
const getSelectedNodes = () => getNodes().filter((node) => node.selected);

const { fullscreen, onFullscreen } = useFullscreen();

const reactFlow = useReactFlow<NodeData, EdgeData>();
const selectedNodes: Node<NodeData>[] = nodes.filter((node) => node.selected);
const handleFitToScreen = () => reactFlow.fitView({ duration: 200, nodes: selectedNodes });
const handleFitToScreen = () => reactFlow.fitView({ duration: 200, nodes: getSelectedNodes() });
const handleZoomIn = () => reactFlow.zoomIn({ duration: 200 });
const handleZoomOut = () => reactFlow.zoomOut({ duration: 200 });
const handleShare = () => setState((prevState) => ({ ...prevState, dialogOpen: 'Share' }));
Expand All @@ -58,10 +61,6 @@ export const DiagramPanel = ({ snapToGrid, onSnapToGrid }: DiagramPanelProps) =>

const { exportToImage } = useExportToImage();

const getAllElementsIds = () => {
return [...reactFlow.getNodes().map((elem) => elem.id), ...reactFlow.getEdges().map((elem) => elem.id)];
};

return (
<>
<Panel position="top-left">
Expand Down Expand Up @@ -125,4 +124,4 @@ export const DiagramPanel = ({ snapToGrid, onSnapToGrid }: DiagramPanelProps) =>
{state.dialogOpen === 'Share' ? <ShareDiagramDialog onClose={handleCloseDialog} /> : null}
</>
);
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ import { UseSnapToGridValue } from './useSnapToGrid.types';
export const useSnapToGrid = (): UseSnapToGridValue => {
const [state, setState] = useState<boolean>(false);

const onSnapToGrid = (snapToGrid: boolean) => setState(snapToGrid);

return {
snapToGrid: state,
onSnapToGrid,
onSnapToGrid: setState,
};
};

0 comments on commit 25ef685

Please sign in to comment.