Skip to content

Commit

Permalink
unify zoomTo
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasvo committed Dec 3, 2024
1 parent adf1a54 commit 05f0e32
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 149 deletions.
51 changes: 11 additions & 40 deletions lib/commonjs/ReactNativeZoomableView.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/commonjs/ReactNativeZoomableView.js.map

Large diffs are not rendered by default.

51 changes: 11 additions & 40 deletions lib/module/ReactNativeZoomableView.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/module/ReactNativeZoomableView.js.map

Large diffs are not rendered by default.

27 changes: 7 additions & 20 deletions lib/typescript/ReactNativeZoomableView.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,28 +197,15 @@ declare class ReactNativeZoomableView extends Component<ReactNativeZoomableViewP
*/
_getNextZoomStep(): number | undefined;
/**
* Zooms to a specific location in our view
* Zooms to a specific level and/or location in our view
*
* @param x
* @param y
* @param newZoomLevel
*
* @private
*/
_zoomToLocation(x: number, y: number, newZoomLevel: number): void;
/**
* Zooms to a specificied zoom level.
* Returns a promise if everything was updated and a boolean, whether it could be updated or if it exceeded the min/max zoom limits.
*
* @param {number} newZoomLevel
*
* @return {bool}
*/
zoomTo(newZoomLevel: number): boolean;
/**
* Sets zoom relative to the zoomableview
* @param coords relative coords compared to the zoom subject
*/
zoomToRelCoords(x: number, y: number, newZoomLevel: number): boolean;
zoomTo(newZoomLevel: number, coords?: {
x: number;
y: number;
}): true | undefined;
/**
* Zooms in or out by a specified change level
* Use a positive number for `zoomLevelChange` to zoom in
Expand All @@ -230,7 +217,7 @@ declare class ReactNativeZoomableView extends Component<ReactNativeZoomableViewP
*
* @return {bool}
*/
zoomBy(zoomLevelChange: number): boolean;
zoomBy(zoomLevelChange: number): true | undefined;
/**
* Moves the zoomed view to a specified position
* Returns a promise when finished
Expand Down
55 changes: 8 additions & 47 deletions src/ReactNativeZoomableView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1040,11 +1040,7 @@ class ReactNativeZoomableView extends Component<
zoomPositionCoordinates.y = 0;
}

this._zoomToLocation(
zoomPositionCoordinates.x,
zoomPositionCoordinates.y,
nextZoomStep
);
this.zoomTo(nextZoomStep, zoomPositionCoordinates);

onDoubleTapAfter?.(
e,
Expand Down Expand Up @@ -1079,16 +1075,15 @@ class ReactNativeZoomableView extends Component<
}

/**
* Zooms to a specific location in our view
* Zooms to a specific level and/or location in our view
*
* @param x
* @param y
* @param newZoomLevel
*
* @private
* @param coords relative coords compared to the zoom subject
*/
_zoomToLocation(x: number, y: number, newZoomLevel: number) {
zoomTo(newZoomLevel: number, coords = { x: 0, y: 0 }) {
if (!this.props.zoomEnabled) return;
if (this.props.maxZoom && newZoomLevel > this.props.maxZoom) return;
if (this.props.minZoom && newZoomLevel < this.props.minZoom) return;

this.props.onZoomBefore?.(null, null, this._getZoomableViewEventObject());

Expand All @@ -1106,14 +1101,14 @@ class ReactNativeZoomableView extends Component<
this.state.originalWidth,
prevScale,
newScale,
x
coords.x
),
y: calcNewScaledOffsetForZoomCentering(
this.offsetY,
this.state.originalHeight,
prevScale,
newScale,
y
coords.y
),
});
prevScale = newScale;
Expand All @@ -1124,40 +1119,6 @@ class ReactNativeZoomableView extends Component<
// == Zoom Animation Ends ==

this.props.onZoomAfter?.(null, null, this._getZoomableViewEventObject());
}

/**
* Zooms to a specificied zoom level.
* Returns a promise if everything was updated and a boolean, whether it could be updated or if it exceeded the min/max zoom limits.
*
* @param {number} newZoomLevel
*
* @return {bool}
*/
zoomTo(newZoomLevel: number) {
if (
// if we would go out of our min/max limits -> abort
(this.props.maxZoom && newZoomLevel > this.props.maxZoom) ||
(this.props.minZoom && newZoomLevel < this.props.minZoom)
)
return false;

this._zoomToLocation(0, 0, newZoomLevel);
return true;
}

/**
* Sets zoom relative to the zoomableview
*/
zoomToRelCoords(x: number, y: number, newZoomLevel: number) {
if (
// if we would go out of our min/max limits -> abort
(this.props.maxZoom && newZoomLevel > this.props.maxZoom) ||
(this.props.minZoom && newZoomLevel < this.props.minZoom)
)
return false;
// Just exposing _zoomToLocation
this._zoomToLocation(x, y, newZoomLevel);
return true;
}

Expand Down

0 comments on commit 05f0e32

Please sign in to comment.