Skip to content

Commit

Permalink
[Fix] Center diagram properly when moving from diagram manager to dia…
Browse files Browse the repository at this point in the history
…gram
  • Loading branch information
bindeali committed Dec 28, 2023
1 parent b836950 commit fc989fb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/function/FunctionDiagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ export function changeDiagrams(diagram?: string) {
}
}
setRepresentation(Diagrams[diagram].representation, diagram, false);
if (Diagrams[diagram].origin.x === 0 && Diagrams[diagram].origin.y === 0) {
centerDiagram();
} else {
paper.scale(Diagrams[diagram].scale, Diagrams[diagram].scale);
paper.translate(Diagrams[diagram].origin.x, Diagrams[diagram].origin.y);
}
StoreSettings.update((s) => {
s.selectedDiagram = diagram!;
});
Expand Down Expand Up @@ -82,9 +76,15 @@ export function centerDiagram(
x += elem.getBBox().x;
y += elem.getBBox().y;
}
const computedSize = p.getComputedSize();
if (computedSize.width === 0 && computedSize.height === 0) {
computedSize.width = document.getElementById("mainView")?.offsetWidth ?? 0;
computedSize.height =
document.getElementById("mainView")?.offsetHeight ?? 0;
}
p.translate(
-((x / g.getElements().length) * scale) + p.getComputedSize().width / 2,
-((y / g.getElements().length) * scale) + p.getComputedSize().height / 2
-((x / g.getElements().length) * scale) + computedSize.width / 2,
-((y / g.getElements().length) * scale) + computedSize.height / 2
);
if (g === graph) updateDiagramPosition(AppSettings.selectedDiagram);
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/DiagramCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from "../config/Variables";
import { CellColors } from "../config/visual/CellColors";
import {
centerDiagram,
resetDiagramSelection,
updateDiagramPosition,
zoomDiagram,
Expand Down Expand Up @@ -130,6 +131,24 @@ export default class DiagramCanvas extends React.Component<Props> {
},
});

if (graph.getElements().length > 0) {
if (
Diagrams[AppSettings.selectedDiagram].origin.x === 0 &&
Diagrams[AppSettings.selectedDiagram].origin.y === 0
) {
centerDiagram();
} else {
paper.scale(
Diagrams[AppSettings.selectedDiagram].scale,
Diagrams[AppSettings.selectedDiagram].scale
);
paper.translate(
Diagrams[AppSettings.selectedDiagram].origin.x,
Diagrams[AppSettings.selectedDiagram].origin.y
);
}
}

/**
* This handles all the various mouse events on the canvas and the elements within.
* For more information on JointJS events visit
Expand Down
2 changes: 1 addition & 1 deletion src/main/MainView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const MainView: React.FC<Props> = (props: Props) => {
timer = setTimeout(getStyle, 200);
});
return (
<div className={"mainView"} style={getStyle()}>
<div className={"mainView"} id={"mainView"} style={getStyle()}>
{mode === MainViewMode.CANVAS && (
<DiagramCanvas
projectLanguage={props.projectLanguage}
Expand Down

0 comments on commit fc989fb

Please sign in to comment.