Merge pull request #5480 from xyflow/fix-connection-pinch
Fix connection pinch
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
'@xyflow/react': patch
|
||||||
|
'@xyflow/svelte': patch
|
||||||
|
'@xyflow/system': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Prevent multi-touch events while making a new connection
|
||||||
@@ -25,6 +25,7 @@ type ZoomPaneProps = Omit<
|
|||||||
const selector = (s: ReactFlowState) => ({
|
const selector = (s: ReactFlowState) => ({
|
||||||
userSelectionActive: s.userSelectionActive,
|
userSelectionActive: s.userSelectionActive,
|
||||||
lib: s.lib,
|
lib: s.lib,
|
||||||
|
connectionInProgress: s.connection.inProgress,
|
||||||
});
|
});
|
||||||
|
|
||||||
export function ZoomPane({
|
export function ZoomPane({
|
||||||
@@ -51,7 +52,7 @@ export function ZoomPane({
|
|||||||
}: ZoomPaneProps) {
|
}: ZoomPaneProps) {
|
||||||
const store = useStoreApi();
|
const store = useStoreApi();
|
||||||
const zoomPane = useRef<HTMLDivElement>(null);
|
const zoomPane = useRef<HTMLDivElement>(null);
|
||||||
const { userSelectionActive, lib } = useStore(selector, shallow);
|
const { userSelectionActive, lib, connectionInProgress } = useStore(selector, shallow);
|
||||||
const zoomActivationKeyPressed = useKeyPress(zoomActivationKeyCode);
|
const zoomActivationKeyPressed = useKeyPress(zoomActivationKeyCode);
|
||||||
const panZoom = useRef<PanZoomInstance>();
|
const panZoom = useRef<PanZoomInstance>();
|
||||||
|
|
||||||
@@ -126,6 +127,7 @@ export function ZoomPane({
|
|||||||
noWheelClassName,
|
noWheelClassName,
|
||||||
lib,
|
lib,
|
||||||
onTransformChange,
|
onTransformChange,
|
||||||
|
connectionInProgress,
|
||||||
});
|
});
|
||||||
}, [
|
}, [
|
||||||
onPaneContextMenu,
|
onPaneContextMenu,
|
||||||
@@ -143,6 +145,7 @@ export function ZoomPane({
|
|||||||
noWheelClassName,
|
noWheelClassName,
|
||||||
lib,
|
lib,
|
||||||
onTransformChange,
|
onTransformChange,
|
||||||
|
connectionInProgress,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ type ZoomParams = {
|
|||||||
paneClickDistance: number;
|
paneClickDistance: number;
|
||||||
onTransformChange: (transform: Transform) => void;
|
onTransformChange: (transform: Transform) => void;
|
||||||
onDraggingChange: (dragging: boolean) => void;
|
onDraggingChange: (dragging: boolean) => void;
|
||||||
|
connectionInProgress: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function zoom(domNode: Element, params: ZoomParams) {
|
export default function zoom(domNode: Element, params: ZoomParams) {
|
||||||
|
|||||||
@@ -70,7 +70,8 @@
|
|||||||
paneClickDistance,
|
paneClickDistance,
|
||||||
onTransformChange: (transform: Transform) => {
|
onTransformChange: (transform: Transform) => {
|
||||||
store.viewport = { x: transform[0], y: transform[1], zoom: transform[2] };
|
store.viewport = { x: transform[0], y: transform[1], zoom: transform[2] };
|
||||||
}
|
},
|
||||||
|
connectionInProgress: store.connection.inProgress
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{@render children()}
|
{@render children()}
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ export type PanZoomUpdateOptions = {
|
|||||||
zoomActivationKeyPressed: boolean;
|
zoomActivationKeyPressed: boolean;
|
||||||
lib: string;
|
lib: string;
|
||||||
onTransformChange: OnTransformChange;
|
onTransformChange: OnTransformChange;
|
||||||
|
connectionInProgress: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type PanZoomInstance = {
|
export type PanZoomInstance = {
|
||||||
|
|||||||
@@ -207,6 +207,11 @@ function onPointerDown(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onPointerUp(event: MouseEvent | TouchEvent) {
|
function onPointerUp(event: MouseEvent | TouchEvent) {
|
||||||
|
// Prevent multi-touch aborting connection
|
||||||
|
if ('touches' in event && event.touches.length > 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (connectionStarted) {
|
if (connectionStarted) {
|
||||||
if ((closestHandle || resultHandleDomNode) && connection && isValid) {
|
if ((closestHandle || resultHandleDomNode) && connection && isValid) {
|
||||||
onConnect?.(connection);
|
onConnect?.(connection);
|
||||||
|
|||||||
@@ -108,6 +108,7 @@ export function XYPanZoom({
|
|||||||
zoomActivationKeyPressed,
|
zoomActivationKeyPressed,
|
||||||
lib,
|
lib,
|
||||||
onTransformChange,
|
onTransformChange,
|
||||||
|
connectionInProgress,
|
||||||
}: PanZoomUpdateOptions) {
|
}: PanZoomUpdateOptions) {
|
||||||
if (userSelectionActive && !zoomPanValues.isZoomingOrPanning) {
|
if (userSelectionActive && !zoomPanValues.isZoomingOrPanning) {
|
||||||
destroy();
|
destroy();
|
||||||
@@ -178,6 +179,7 @@ export function XYPanZoom({
|
|||||||
noPanClassName,
|
noPanClassName,
|
||||||
noWheelClassName,
|
noWheelClassName,
|
||||||
lib,
|
lib,
|
||||||
|
connectionInProgress,
|
||||||
});
|
});
|
||||||
d3ZoomInstance.filter(filter);
|
d3ZoomInstance.filter(filter);
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ export type FilterParams = {
|
|||||||
noWheelClassName: string;
|
noWheelClassName: string;
|
||||||
noPanClassName: string;
|
noPanClassName: string;
|
||||||
lib: string;
|
lib: string;
|
||||||
|
connectionInProgress: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function createFilter({
|
export function createFilter({
|
||||||
@@ -25,10 +26,12 @@ export function createFilter({
|
|||||||
noWheelClassName,
|
noWheelClassName,
|
||||||
noPanClassName,
|
noPanClassName,
|
||||||
lib,
|
lib,
|
||||||
|
connectionInProgress,
|
||||||
}: FilterParams) {
|
}: FilterParams) {
|
||||||
return (event: any): boolean => {
|
return (event: any): boolean => {
|
||||||
const zoomScroll = zoomActivationKeyPressed || zoomOnScroll;
|
const zoomScroll = zoomActivationKeyPressed || zoomOnScroll;
|
||||||
const pinchZoom = zoomOnPinch && event.ctrlKey;
|
const pinchZoom = zoomOnPinch && event.ctrlKey;
|
||||||
|
const isWheelEvent = event.type === 'wheel';
|
||||||
|
|
||||||
if (
|
if (
|
||||||
event.button === 1 &&
|
event.button === 1 &&
|
||||||
@@ -48,20 +51,25 @@ export function createFilter({
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// we want to disable pinch-zooming while making a connection
|
||||||
|
if (connectionInProgress && !isWheelEvent) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// if the target element is inside an element with the nowheel class, we prevent zooming
|
// if the target element is inside an element with the nowheel class, we prevent zooming
|
||||||
if (isWrappedWithClass(event, noWheelClassName) && event.type === 'wheel') {
|
if (isWrappedWithClass(event, noWheelClassName) && isWheelEvent) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the target element is inside an element with the nopan class, we prevent panning
|
// if the target element is inside an element with the nopan class, we prevent panning
|
||||||
if (
|
if (
|
||||||
isWrappedWithClass(event, noPanClassName) &&
|
isWrappedWithClass(event, noPanClassName) &&
|
||||||
(event.type !== 'wheel' || (panOnScroll && event.type === 'wheel' && !zoomActivationKeyPressed))
|
(!isWheelEvent || (panOnScroll && isWheelEvent && !zoomActivationKeyPressed))
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!zoomOnPinch && event.ctrlKey && event.type === 'wheel') {
|
if (!zoomOnPinch && event.ctrlKey && isWheelEvent) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +79,7 @@ export function createFilter({
|
|||||||
}
|
}
|
||||||
|
|
||||||
// when there is no scroll handling enabled, we prevent all wheel events
|
// when there is no scroll handling enabled, we prevent all wheel events
|
||||||
if (!zoomScroll && !panOnScroll && !pinchZoom && event.type === 'wheel') {
|
if (!zoomScroll && !panOnScroll && !pinchZoom && isWheelEvent) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,6 +98,6 @@ export function createFilter({
|
|||||||
(Array.isArray(panOnDrag) && panOnDrag.includes(event.button)) || !event.button || event.button <= 1;
|
(Array.isArray(panOnDrag) && panOnDrag.includes(event.button)) || !event.button || event.button <= 1;
|
||||||
|
|
||||||
// default filter for d3-zoom
|
// default filter for d3-zoom
|
||||||
return (!event.ctrlKey || event.type === 'wheel') && buttonAllowed;
|
return (!event.ctrlKey || isWheelEvent) && buttonAllowed;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user