-
Notifications
You must be signed in to change notification settings - Fork 537
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added usePannign hook, fixed useRenderer lint issue
- Loading branch information
1 parent
0393e7e
commit 553fa0b
Showing
3 changed files
with
78 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]> | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
import Hammer from "hammerjs"; | ||
import { useEffect } from "react"; | ||
|
||
import { PlotCoordinator } from "@lichtblick/suite-base/panels/Plot/PlotCoordinator"; | ||
|
||
const usePanning = ( | ||
canvasDiv: HTMLDivElement | ReactNull, | ||
coordinator: PlotCoordinator | undefined, | ||
draggingRef: React.MutableRefObject<boolean>, | ||
): void => { | ||
useEffect(() => { | ||
if (!canvasDiv || !coordinator) { | ||
return; | ||
} | ||
|
||
const hammerManager = new Hammer.Manager(canvasDiv); | ||
const threshold = 10; | ||
hammerManager.add(new Hammer.Pan({ threshold })); | ||
|
||
hammerManager.on("panstart", (event) => { | ||
draggingRef.current = true; | ||
const boundingRect = event.target.getBoundingClientRect(); | ||
coordinator.addInteractionEvent({ | ||
type: "panstart", | ||
cancelable: false, | ||
deltaY: event.deltaY, | ||
deltaX: event.deltaX, | ||
center: { | ||
x: event.center.x, | ||
y: event.center.y, | ||
}, | ||
boundingClientRect: boundingRect.toJSON(), | ||
}); | ||
}); | ||
|
||
hammerManager.on("panmove", (event) => { | ||
const boundingRect = event.target.getBoundingClientRect(); | ||
coordinator.addInteractionEvent({ | ||
type: "panmove", | ||
cancelable: false, | ||
deltaY: event.deltaY, | ||
deltaX: event.deltaX, | ||
boundingClientRect: boundingRect.toJSON(), | ||
}); | ||
}); | ||
|
||
hammerManager.on("panend", (event) => { | ||
const boundingRect = event.target.getBoundingClientRect(); | ||
coordinator.addInteractionEvent({ | ||
type: "panend", | ||
cancelable: false, | ||
deltaY: event.deltaY, | ||
deltaX: event.deltaX, | ||
boundingClientRect: boundingRect.toJSON(), | ||
}); | ||
|
||
// We need to do this a little bit later so that the onClick handler still sees | ||
// draggingRef.current===true and can skip the seek. | ||
setTimeout(() => { | ||
draggingRef.current = false; | ||
}, 0); | ||
}); | ||
|
||
return () => { | ||
hammerManager.destroy(); | ||
}; | ||
}, [canvasDiv, coordinator, draggingRef]); | ||
}; | ||
|
||
export default usePanning; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters