Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ import {
getSceneRequestFromSearch,
getSceneSearch,
getSemanticZoomDirection,
getZoomGesturePoint,
isContainerSceneNode,
isSceneEmpty,
isSceneNodeDrillable,
Expand Down Expand Up @@ -1671,11 +1672,9 @@ const LineageMapCanvas = ({
const isDrillable = (node: Node<SceneFlowNodeData>) =>
isSceneNodeDrillable(node.data.sceneNode);

if (event && 'clientX' in event) {
const point = reactFlowInstance.screenToFlowPosition({
x: event.clientX,
y: event.clientY,
});
const gesturePoint = getZoomGesturePoint(event);
if (gesturePoint) {
const point = reactFlowInstance.screenToFlowPosition(gesturePoint);
const underPointer = reactFlowInstance.getIntersectingNodes({
...point,
width: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
getSceneSearch,
getScreenFill,
getSemanticZoomDirection,
getZoomGesturePoint,
isSceneEmpty,
isSceneNodeDrillableWithChildren,
type SceneLayoutBox,
Expand Down Expand Up @@ -845,6 +846,41 @@ describe('getDeleteKeyAction', () => {
});
});

describe('zoom gesture point', () => {
const touchEvent = (
touches: { clientX: number; clientY: number }[],
changedTouches: { clientX: number; clientY: number }[] = []
) => ({ touches, changedTouches } as unknown as TouchEvent);

it('uses the pointer for a wheel or mouse event', () => {
expect(
getZoomGesturePoint({ clientX: 10, clientY: 20 } as MouseEvent)
).toEqual({ x: 10, y: 20 });
});

it('uses the midpoint of the fingers for a pinch', () => {
expect(
getZoomGesturePoint(
touchEvent([
{ clientX: 100, clientY: 40 },
{ clientX: 300, clientY: 80 },
])
)
).toEqual({ x: 200, y: 60 });
});

it('falls back to the lifted fingers once no touch remains', () => {
expect(
getZoomGesturePoint(touchEvent([], [{ clientX: 50, clientY: 70 }]))
).toEqual({ x: 50, y: 70 });
});

it('returns undefined without an event or touches', () => {
expect(getZoomGesturePoint(null)).toBeUndefined();
expect(getZoomGesturePoint(touchEvent([]))).toBeUndefined();
});
});

describe('box nearest a point', () => {
it('measures to box centres, not edges', () => {
const wide = { id: 'wide', x: 0, y: 0, width: 400, height: 40 };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

import type { TFunction } from 'i18next';
import { meanBy } from 'lodash';
import Qs from 'qs';
import type { Viewport } from 'reactflow';
import { EntityType } from '../../../enums/entity.enum';
Expand Down Expand Up @@ -970,6 +971,28 @@ export const getBoxNearestPoint = <T extends Point & Size>(
{ distance: Number.POSITIVE_INFINITY }
).box;

/**
* Screen point a zoom gesture aims at: the pointer for a wheel or mouse event,
* the midpoint of the fingers for a pinch, or undefined when there is none.
*/
export const getZoomGesturePoint = (
event?: MouseEvent | TouchEvent | null
): Point | undefined => {
if (!event) {
return undefined;
}
if ('clientX' in event) {
return { x: event.clientX, y: event.clientY };
}
const touches = Array.from(
event.touches.length ? event.touches : event.changedTouches
);

return touches.length
? { x: meanBy(touches, 'clientX'), y: meanBy(touches, 'clientY') }
: undefined;
};

/** Largest share of the screen's width or height that `size` covers at `zoom`. */
export const getScreenFill = (size: Size, zoom: number, screen: Size) =>
Math.max(
Expand Down
Loading