Skip to content

Commit

Permalink
refactor(std): replace dot,mag from own to math
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Minaev committed Dec 5, 2024
1 parent 117eba0 commit 0ad7ccf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
29 changes: 16 additions & 13 deletions packages/framework/block-std/src/event/control/pointer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IS_IPAD } from '@blocksuite/global/env';
import { dotProduct, magnitude } from '@blocksuite/global/utils';
import { Vec } from '@blocksuite/global/utils';

import type { UIEventDispatcher } from '../dispatcher.js';

Expand Down Expand Up @@ -486,13 +486,16 @@ class PinchController extends DualDragControllerBase {
override _handleMove(event: PointerEvent, state: MultiPointerEventState) {
if (event.pointerType !== 'touch') return;

const deltaFirstPointerVec = state.pointers[0].delta;
const deltaSecondPointerVec = state.pointers[1].delta;
const deltaFirstPointer = state.pointers[0].delta;
const deltaSecondPointer = state.pointers[1].delta;

const deltaFirstPointerValue = magnitude(deltaFirstPointerVec);
const deltaSecondPointerValue = magnitude(deltaSecondPointerVec);
const deltaFirstPointerVec = Vec.toVec(deltaFirstPointer);
const deltaSecondPointerVec = Vec.toVec(deltaSecondPointer);

const deltaDotProduct = dotProduct(
const deltaFirstPointerValue = Vec.len(deltaFirstPointerVec);
const deltaSecondPointerValue = Vec.len(deltaSecondPointerVec);

const deltaDotProduct = Vec.dpr(
deltaFirstPointerVec,
deltaSecondPointerVec
);
Expand All @@ -501,7 +504,7 @@ class PinchController extends DualDragControllerBase {

// the changes of distance between two pointers is not far enough
if (
!isFarEnough(deltaFirstPointerVec, deltaSecondPointerVec) ||
!isFarEnough(deltaFirstPointer, deltaSecondPointer) ||
deltaDotProduct > 0 ||
deltaFirstPointerValue < deltaValueThreshold ||
deltaSecondPointerValue < deltaValueThreshold
Expand All @@ -516,17 +519,17 @@ class PanController extends DualDragControllerBase {
override _handleMove(event: PointerEvent, state: MultiPointerEventState) {
if (event.pointerType !== 'touch') return;

const deltaFirstPointerVec = state.pointers[0].delta;
const deltaSecondPointerVec = state.pointers[1].delta;
const deltaFirstPointer = state.pointers[0].delta;
const deltaSecondPointer = state.pointers[1].delta;

const deltaDotProduct = dotProduct(
deltaFirstPointerVec,
deltaSecondPointerVec
const deltaDotProduct = Vec.dpr(
Vec.toVec(deltaFirstPointer),
Vec.toVec(deltaSecondPointer)
);

// the center move distance is not far enough
if (
!isFarEnough(deltaFirstPointerVec, deltaSecondPointerVec) &&
!isFarEnough(deltaFirstPointer, deltaSecondPointer) &&
deltaDotProduct < 0
)
return;
Expand Down
9 changes: 0 additions & 9 deletions packages/framework/global/src/utils/math.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { IPoint } from '../utils.js';
import type { Bound, IBound } from './model/bound.js';

import { PointLocation } from './model/point-location.js';
Expand Down Expand Up @@ -537,11 +536,3 @@ export function getCenterAreaBounds(bounds: IBound, ratio: number) {
rotate,
};
}

export function dotProduct(vectorA: IPoint, vectorB: IPoint) {
return vectorA.x * vectorB.x + vectorA.y * vectorB.y;
}

export function magnitude(vector: IPoint) {
return Math.sqrt(vector.x * vector.x + vector.y * vector.y);
}

0 comments on commit 0ad7ccf

Please sign in to comment.