Skip to content

Commit

Permalink
feat: Support auto align to bounds on resize (#485)
Browse files Browse the repository at this point in the history
  • Loading branch information
nuintun authored Jul 3, 2024
1 parent 9525dbb commit 800beb1
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions src/core/instance.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
handleDoubleClick,
isDoubleClickAllowed,
} from "./double-click/double-click.logic";
import { handleAlignToScaleBounds } from "./zoom/zoom.logic";

type StartCoordsType = { x: number; y: number } | null;

Expand All @@ -59,7 +60,7 @@ export class ZoomPanPinch {

public transformState: ReactZoomPanPinchState;
public setup: LibrarySetup;
public observer?: ResizeObserver;
public observer: ResizeObserver;

Check failure on line 63 in src/core/instance.core.ts

View workflow job for this annotation

GitHub Actions / build

Property 'observer' has no initializer and is not definitely assigned in the constructor.

Check failure on line 63 in src/core/instance.core.ts

View workflow job for this annotation

GitHub Actions / Run tests

Property 'observer' has no initializer and is not definitely assigned in the constructor.
public onChangeCallbacks: Set<(ctx: ReactZoomPanPinchRef) => void> =
new Set();
public onInitCallbacks: Set<(ctx: ReactZoomPanPinchRef) => void> = new Set();
Expand Down Expand Up @@ -157,7 +158,7 @@ export class ZoomPanPinch {
document.removeEventListener("mouseleave", this.clearPanning, passive);

handleCancelAnimation(this);
this.observer?.disconnect();
this.observer.disconnect();
};

handleInitializeWrapperEvents = (wrapper: HTMLDivElement): void => {
Expand All @@ -172,34 +173,40 @@ export class ZoomPanPinch {
};

handleInitialize = (contentComponent: HTMLDivElement): void => {
let isCentered = false;

const { centerOnInit } = this.setup;
this.applyTransformation();
this.onInitCallbacks.forEach((callback) => callback(getContext(this)));

if (centerOnInit) {
this.setCenter();
this.observer = new ResizeObserver(() => {
const currentWidth = contentComponent.offsetWidth;
const currentHeight = contentComponent.offsetHeight;

if (currentWidth > 0 || currentHeight > 0) {
this.onInitCallbacks.forEach((callback) =>
callback(getContext(this)),
);
this.setCenter();

this.observer?.disconnect();
this.applyTransformation();
this.onInitCallbacks.forEach((callback) => {
callback(getContext(this));
});

this.observer = new ResizeObserver((entries) => {
for(const entry of entries) {
if(entry.target === contentComponent) {
if(centerOnInit && !isCentered) {
const currentWidth = contentComponent.offsetWidth;
const currentHeight = contentComponent.offsetHeight;

if (currentWidth > 0 || currentHeight > 0) {
isCentered = true;

this.setCenter();
}
} else {
const { pinchMidpoint } = this;

handleAlignToScaleBounds(this, pinchMidpoint?.x, pinchMidpoint?.y);
}

break;
}
});

// if nothing about the contentComponent has changed after 5 seconds, disconnect the observer
setTimeout(() => {
this.observer?.disconnect();
}, 5000);
}
});

// Start observing the target node for configured mutations
this.observer.observe(contentComponent);
}
// Start observing the target node for configured mutations
this.observer.observe(contentComponent);
};

/// ///////
Expand Down

0 comments on commit 800beb1

Please sign in to comment.