Skip to content

Build your personal knowledge base with Trilium Notes

fix(relation_map): ignore the click that ends a drag back to its start

Elian Doranecab2e581a7b
diff --git a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.spec.tsx b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.spec.tsx
index ed37633723..53c83120bc 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.spec.tsx
@@ -140,6 +140,26 @@ describe("relation map canvas clicks", () => {
expect(onOpenNote).toHaveBeenCalledTimes(2);
});
+ it("ignores a drag that comes back to where it started", () => {
+ const roundTrip = (target: HTMLElement) => {
+ target.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, clientX: 10, clientY: 10 }));
+ target.dispatchEvent(new PointerEvent("pointermove", { bubbles: true, clientX: 60, clientY: 10 }));
+ act(() => { target.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, clientX: 11, clientY: 10 })); });
+ };
+
+ const { canvas, title } = mount();
+ roundTrip(canvas);
+ roundTrip(title);
+ expect(onClickEmpty).not.toHaveBeenCalled();
+ expect(onSelectNote).not.toHaveBeenCalled();
+
+ roundTrip(mount(true).canvas);
+ expect(onPlace).not.toHaveBeenCalled();
+
+ click(canvas);
+ expect(onPlace).toHaveBeenCalledTimes(1);
+ });
+
it("closes the pane on empty canvas, but not on what stands over the map", () => {
const { wrapper, canvas, toolbar } = mount();
diff --git a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx
index 42366e39a9..cb3e40cd79 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx
@@ -588,21 +588,26 @@ export function useCanvasClicks({ placing, onPlace, onSelectNote, onClickEmpty,
onSelectNote(noteId: string): void;
onClickEmpty(): void;
onOpenNote(noteId: string, e: MouseEvent): void;
-}): Pick<HTMLAttributes<HTMLDivElement>, "onPointerDownCapture" | "onClickCapture" | "onAuxClickCapture"> {
+}): Pick<HTMLAttributes<HTMLDivElement>, "onPointerDownCapture" | "onPointerMoveCapture" | "onClickCapture" | "onAuxClickCapture"> {
const pressedAt = useRef<{ x: number; y: number }>(null);
-
- /** Whether the click targets the map canvas, with the pointer moved no more than
- * `CLICK_TOLERANCE` since the press. */
- const isPlainClickOnCanvas = (e: MouseEvent) => {
+ const movedRef = useRef(false);
+ const isAwayFromPress = (e: MouseEvent) => {
const pressed = pressedAt.current;
- return isOnCanvas(e)
- && !(pressed && Math.hypot(e.clientX - pressed.x, e.clientY - pressed.y) > CLICK_TOLERANCE);
+ return !!pressed && Math.hypot(e.clientX - pressed.x, e.clientY - pressed.y) > CLICK_TOLERANCE;
};
+
+ /** Whether the click targets the map canvas, with the pointer kept within `CLICK_TOLERANCE`
+ * of the press throughout. */
+ const isPlainClickOnCanvas = (e: MouseEvent) => isOnCanvas(e) && !movedRef.current && !isAwayFromPress(e);
const boxAt = (e: MouseEvent) => e.target instanceof Element ? e.target.closest<HTMLElement>(".note-box") : null;
return {
onPointerDownCapture(e) {
pressedAt.current = { x: e.clientX, y: e.clientY };
+ movedRef.current = false;
+ },
+ onPointerMoveCapture(e) {
+ if (isAwayFromPress(e)) movedRef.current = true;
},
onAuxClickCapture(e) {
const box = boxAt(e);