Skip to content

Build your personal knowledge base with Trilium Notes

fix(relation_map): follow only the pointer that started a drag

Elian Doran799b112da2e3
diff --git a/apps/client/src/widgets/type_widgets/relation_map/drags.spec.tsx b/apps/client/src/widgets/type_widgets/relation_map/drags.spec.tsx
index 1f5777b45d..c5f31be76b 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/drags.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/drags.spec.tsx
@@ -53,8 +53,8 @@ describe("relation map pointer drags", () => {
/** The map is zoomed to 2x. */
const getScale = () => 2;
- function pointer(type: string, clientX: number, clientY: number) {
- return new PointerEvent(type, { clientX, clientY, button: 0, isPrimary: true });
+ function pointer(type: string, clientX: number, clientY: number, pointerId = 1) {
+ return new PointerEvent(type, { clientX, clientY, button: 0, isPrimary: pointerId === 1, pointerId });
}
/** Releases the pointer, and waits for the name to be asked and the relation created. */
@@ -65,8 +65,8 @@ describe("relation map pointer drags", () => {
});
}
- function dispatch(type: string, clientX: number, clientY: number) {
- act(() => { window.dispatchEvent(pointer(type, clientX, clientY)); });
+ function dispatch(type: string, clientX: number, clientY: number, pointerId = 1) {
+ act(() => { window.dispatchEvent(pointer(type, clientX, clientY, pointerId)); });
}
it("moves a box once the pointer leaves the click tolerance, and saves it on release", () => {
@@ -83,6 +83,26 @@ describe("relation map pointer drags", () => {
expect(state.dragged).toBeNull();
});
+ it("follows only the pointer that started the gesture", async () => {
+ act(() => startDrag?.(pointer("pointerdown", 100, 100), { noteId: "box", x: 10, y: 20 }));
+ dispatch("pointermove", 300, 300, 2);
+ dispatch("pointerup", 300, 300, 2);
+ expect(state.dragged).toBeNull();
+ expect(moveNote).not.toHaveBeenCalled();
+
+ dispatch("pointermove", 140, 160);
+ dispatch("pointerup", 140, 160);
+ expect(moveNote).toHaveBeenCalledWith("box", 30, 50);
+
+ act(() => startDrawing?.(pointer("pointerdown", 100, 100), "source"));
+ dispatch("pointermove", 150, 100, 2);
+ expect(state.pending).toMatchObject({ pointer: { x: 50, y: 50 } });
+ dispatch("pointercancel", 150, 100, 2);
+ expect(state.pending).not.toBeNull();
+ dispatch("pointercancel", 150, 100);
+ expect(state.pending).toBeNull();
+ });
+
it("puts a box back when the drag is canceled", () => {
act(() => startDrag?.(pointer("pointerdown", 100, 100), { noteId: "box", x: 10, y: 20 }));
dispatch("pointermove", 140, 160);
diff --git a/apps/client/src/widgets/type_widgets/relation_map/drags.ts b/apps/client/src/widgets/type_widgets/relation_map/drags.ts
index 1bd8d52529..8c4b5db445 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/drags.ts
+++ b/apps/client/src/widgets/type_widgets/relation_map/drags.ts
@@ -28,7 +28,7 @@ export function useBoxDragging({ containerRef, mapApiRef, getScale }: {
const zoom = getScale();
let position: MapDataNoteEntry | null = null;
- track({
+ track(e.pointerId, {
onMove(moveEvent) {
const dx = moveEvent.clientX - e.clientX;
const dy = moveEvent.clientY - e.clientY;
@@ -77,7 +77,7 @@ export function useRelationDrawing({ containerRef, mapApiRef, getScale, askRelat
});
setPending(pendingAt(e));
- track({
+ track(e.pointerId, {
onMove: (moveEvent) => setPending(pendingAt(moveEvent)),
async onEnd(endEvent) {
const dropped = pendingAt(endEvent);
@@ -104,30 +104,35 @@ export function useRelationDrawing({ containerRef, mapApiRef, getScale, askRelat
}
/**
- * Follows the pointer on the window until it is released or the gesture is canceled, so the gesture
- * continues outside the box it started on. Stops following on unmount.
+ * Follows the pointer `pointerId` on the window until it is released or the gesture is canceled,
+ * so the gesture continues outside the box it started on, and ignores any other pointer, such as a
+ * second finger. Stops following on unmount.
*/
function usePointerTracking() {
const stopRef = useRef<() => void>(null);
useEffect(() => () => stopRef.current?.(), []);
- return useCallback(({ onMove, onEnd }: {
+ return useCallback((pointerId: number, { onMove, onEnd }: {
onMove(e: PointerEvent): void;
onEnd(e: PointerEvent): void;
}) => {
stopRef.current?.();
+ const move = (e: PointerEvent) => {
+ if (e.pointerId === pointerId) onMove(e);
+ };
const end = (e: PointerEvent) => {
+ if (e.pointerId !== pointerId) return;
stop();
onEnd(e);
};
const stop = () => {
- window.removeEventListener("pointermove", onMove);
+ window.removeEventListener("pointermove", move);
window.removeEventListener("pointerup", end);
window.removeEventListener("pointercancel", end);
stopRef.current = null;
};
- window.addEventListener("pointermove", onMove);
+ window.addEventListener("pointermove", move);
window.addEventListener("pointerup", end);
window.addEventListener("pointercancel", end);
stopRef.current = stop;