Skip to content

Commit 92d475b

Browse files
chore: refine pinch helper return handling
Co-authored-by: siarheihuzarevich <[email protected]>
1 parent 3270fbd commit 92d475b

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

projects/f-flow/src/f-zoom/f-zoom.directive.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,8 @@ export class FZoomDirective extends FZoomBase implements OnInit, AfterViewInit,
148148

149149
private _normalizeWheelStep(deltaY: number): number {
150150
const intensity = Math.abs(deltaY) / 100;
151-
const normalized = Math.max(NORMALIZED_MIN, Math.min(intensity, NORMALIZED_MAX));
152151

153-
return this.step * normalized;
152+
return this.step * this._normalizeIntensity(intensity);
154153
}
155154

156155
private _calculateDirection(deltaY: number): number {
@@ -168,7 +167,12 @@ export class FZoomDirective extends FZoomBase implements OnInit, AfterViewInit,
168167
return;
169168
}
170169

171-
this._pinchDistance = this._getTouchDistance(event.touches);
170+
const touchDistance = this._getTouchDistance(event.touches);
171+
if (touchDistance === null) {
172+
return;
173+
}
174+
175+
this._pinchDistance = touchDistance;
172176
};
173177

174178
private _onTouchMove = (event: TouchEvent) => {
@@ -183,6 +187,14 @@ export class FZoomDirective extends FZoomBase implements OnInit, AfterViewInit,
183187
}
184188

185189
const currentDistance = this._getTouchDistance(event.touches);
190+
const touchCenter = this._getTouchCenter(event.touches);
191+
192+
if (currentDistance === null || touchCenter === null) {
193+
this._resetPinch();
194+
195+
return;
196+
}
197+
186198
const delta = currentDistance - this._pinchDistance;
187199
if (Math.abs(delta) < PINCH_MOVEMENT_THRESHOLD) {
188200
return;
@@ -191,7 +203,7 @@ export class FZoomDirective extends FZoomBase implements OnInit, AfterViewInit,
191203
event.preventDefault();
192204

193205
this.setZoom(
194-
this._getTouchCenter(event.touches),
206+
touchCenter,
195207
this._normalizePinchStep(delta),
196208
delta > 0 ? EFZoomDirection.ZOOM_IN : EFZoomDirection.ZOOM_OUT,
197209
false,
@@ -230,10 +242,10 @@ export class FZoomDirective extends FZoomBase implements OnInit, AfterViewInit,
230242
);
231243
}
232244

233-
private _getTouchDistance(touches: TouchList): number {
245+
private _getTouchDistance(touches: TouchList): number | null {
234246
if (touches.length !== 2) {
235247
// Callers guard for pinch-ready touch events; return neutral distance if the check is bypassed.
236-
return 0;
248+
return null;
237249
}
238250

239251
const firstTouch = touches[0];
@@ -245,10 +257,10 @@ export class FZoomDirective extends FZoomBase implements OnInit, AfterViewInit,
245257
);
246258
}
247259

248-
private _getTouchCenter(touches: TouchList): IPoint {
260+
private _getTouchCenter(touches: TouchList): IPoint | null {
249261
if (touches.length !== 2) {
250262
// Callers guard for pinch-ready touch events; return neutral center if the check is bypassed.
251-
return PointExtensions.initialize();
263+
return null;
252264
}
253265

254266
const firstTouch = touches[0];
@@ -262,9 +274,8 @@ export class FZoomDirective extends FZoomBase implements OnInit, AfterViewInit,
262274

263275
private _normalizePinchStep(delta: number): number {
264276
const intensity = Math.abs(delta) / PINCH_NORMALIZATION_FACTOR;
265-
const normalized = Math.max(NORMALIZED_MIN, Math.min(intensity, NORMALIZED_MAX));
266277

267-
return this.step * normalized;
278+
return this.step * this._normalizeIntensity(intensity);
268279
}
269280

270281
private _resetPinch(): void {
@@ -275,6 +286,10 @@ export class FZoomDirective extends FZoomBase implements OnInit, AfterViewInit,
275286
return !!(target as HTMLElement | null)?.closest('[fLockedContext]');
276287
}
277288

289+
private _normalizeIntensity(intensity: number): number {
290+
return Math.max(NORMALIZED_MIN, Math.min(intensity, NORMALIZED_MAX));
291+
}
292+
278293
public zoomIn(position?: IPoint): void {
279294
this._onZoomToCenter(EFZoomDirection.ZOOM_IN, position);
280295
}

0 commit comments

Comments
 (0)