Skip to content

Commit

Permalink
refactor: zoomRectToFit
Browse files Browse the repository at this point in the history
  • Loading branch information
honorsin authored and F-star committed Sep 25, 2024
1 parent f91ef9d commit dfe3db8
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions packages/core/src/zoom_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ export class ZoomManager {
});
}
private zoomRectToFit(rect: IRect, maxZoom?: number) {
const padding = this.editor.setting.get('zoomToFixPadding');
const viewport = this.editor.viewportManager.getViewport();
const { setting, viewportManager } = this.editor;
const padding = setting.get('zoomToFixPadding');
const viewport = viewportManager.getViewport();

// TODO: consider padding from ruler
// const rulerWidth = this.editor.setting.get('enableRuler')
Expand All @@ -127,37 +128,26 @@ export class ZoomManager {
// const leftPadding = rulerWidth + padding;
// const topPadding = rulerWidth + padding;

let vh = viewport.height - padding * 2;
if (vh <= 0) {
vh = viewport.height;
}
let vw = viewport.width - padding * 2;
if (vw <= 0) {
vw = viewport.width;
}
const calculateDimension = (dimension: number): number => {
const adjustedDimension = dimension - padding * 2;
return adjustedDimension > 0 ? adjustedDimension : dimension;
};

const vh = calculateDimension(viewport.height);
const vw = calculateDimension(viewport.width);

let newZoom: number;
const viewportRatio = vw / vh;
const bboxRatio = rect.width / rect.height;
if (viewportRatio > bboxRatio) {
// basic height
newZoom = vh / rect.height;
} else {
newZoom = vw / rect.width;
}

if (maxZoom && newZoom > maxZoom) {
newZoom = maxZoom;
}
let newZoom =
viewportRatio > bboxRatio ? vh / rect.height : vw / rect.width;
newZoom = maxZoom ? Math.min(newZoom, maxZoom) : newZoom;

const newViewportX = rect.x - (viewport.width / newZoom - rect.width) / 2;
const newViewportY = rect.y - (viewport.height / newZoom - rect.height) / 2;

this.setZoom(newZoom);
this.editor.viewportManager.setViewport({
x: newViewportX,
y: newViewportY,
});
viewportManager.setViewport({ x: newViewportX, y: newViewportY });
}
zoomToSelection() {
const selectedBoundingRect = this.editor.selectedElements.getBoundingRect();
Expand Down

0 comments on commit dfe3db8

Please sign in to comment.