@canmingir/link 1.2.42 → 1.2.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canmingir/link",
3
- "version": "1.2.42",
3
+ "version": "1.2.43",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./index.js",
@@ -1,8 +1,8 @@
1
+ import React, { useCallback, useEffect, useRef, useState } from "react";
2
+
1
3
  import { Box } from "@mui/material";
2
4
  import { useSelection } from "../selection/SelectionContext";
3
5
 
4
- import React, { useCallback, useEffect, useRef, useState } from "react";
5
-
6
6
  const DraggableNode = ({
7
7
  children,
8
8
  registerRef,
@@ -13,7 +13,7 @@ const DraggableNode = ({
13
13
  onConnect,
14
14
  }) => {
15
15
  const [offset, setOffset] = useState(() =>
16
- initialPosition ? { ...initialPosition } : { x: 0, y: 0 }
16
+ initialPosition ? { ...initialPosition } : { x: 0, y: 0 },
17
17
  );
18
18
 
19
19
  useEffect(() => {
@@ -27,6 +27,7 @@ const DraggableNode = ({
27
27
  const localRef = useRef(null);
28
28
  const lastDeltaRef = useRef({ x: 0, y: 0 });
29
29
  const onDragRef = useRef(onDrag);
30
+ const didDragRef = useRef(false);
30
31
 
31
32
  const {
32
33
  isSelected,
@@ -58,14 +59,32 @@ const DraggableNode = ({
58
59
  localRef.current = el;
59
60
  registerRef?.(el);
60
61
  },
61
- [registerRef]
62
+ [registerRef],
62
63
  );
63
64
 
65
+ useEffect(() => {
66
+ const el = localRef.current;
67
+ if (!el) return;
68
+
69
+ const onClickCapture = (e) => {
70
+ if (didDragRef.current) {
71
+ e.stopPropagation();
72
+ e.preventDefault();
73
+ didDragRef.current = false;
74
+ }
75
+ };
76
+
77
+ el.addEventListener("click", onClickCapture, true);
78
+ return () => el.removeEventListener("click", onClickCapture, true);
79
+ }, []);
80
+
64
81
  const handleMouseDown = useCallback(
65
82
  (e) => {
66
83
  if (e.button !== 0) return;
67
84
  e.stopPropagation();
68
85
 
86
+ didDragRef.current = false;
87
+
69
88
  if (onConnect && e.altKey) {
70
89
  e.preventDefault();
71
90
  onConnect(nodeId, [...selectedIds]);
@@ -91,6 +110,10 @@ const DraggableNode = ({
91
110
  const dx = ev.clientX - startX;
92
111
  const dy = ev.clientY - startY;
93
112
 
113
+ if (!didDragRef.current && (Math.abs(dx) > 3 || Math.abs(dy) > 3)) {
114
+ didDragRef.current = true;
115
+ }
116
+
94
117
  const deltaDx = dx - lastDeltaRef.current.x;
95
118
  const deltaDy = dy - lastDeltaRef.current.y;
96
119
  lastDeltaRef.current = { x: dx, y: dy };
@@ -125,7 +148,7 @@ const DraggableNode = ({
125
148
  clearSelection,
126
149
  moveSelectedNodes,
127
150
  onConnect,
128
- ]
151
+ ],
129
152
  );
130
153
 
131
154
  return (