@rxdrag/website-lib-core 0.0.76 → 0.0.77

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": "@rxdrag/website-lib-core",
3
- "version": "0.0.76",
3
+ "version": "0.0.77",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./index.ts"
@@ -24,9 +24,9 @@
24
24
  "@types/react-dom": "^19.1.0",
25
25
  "eslint": "^7.32.0",
26
26
  "typescript": "^5",
27
+ "@rxdrag/tsconfig": "0.2.0",
27
28
  "@rxdrag/slate-preview": "1.2.61",
28
- "@rxdrag/eslint-config-custom": "0.2.12",
29
- "@rxdrag/tsconfig": "0.2.0"
29
+ "@rxdrag/eslint-config-custom": "0.2.12"
30
30
  },
31
31
  "dependencies": {
32
32
  "@iconify/utils": "^3.0.2",
@@ -36,8 +36,8 @@
36
36
  "lodash-es": "^4.17.21",
37
37
  "react": "^19.1.0",
38
38
  "react-dom": "^19.1.0",
39
- "@rxdrag/rxcms-models": "0.3.94",
40
- "@rxdrag/entify-lib": "0.0.23"
39
+ "@rxdrag/entify-lib": "0.0.23",
40
+ "@rxdrag/rxcms-models": "0.3.94"
41
41
  },
42
42
  "peerDependencies": {
43
43
  "astro": "^4.0.0 || ^5.0.0"
@@ -36,9 +36,8 @@ export const MainMedia = ({
36
36
  const mainAreaRef = useRef<HTMLDivElement>(null);
37
37
  const [isZoomed, setIsZoomed] = useState(false);
38
38
  const [position, setPosition] = useState({ x: 0, y: 0 });
39
- const [dragStart, setDragStart] = useState({ x: 0, y: 0 });
40
- const [isDragging, setIsDragging] = useState(false);
41
- const hasDraggedRef = useRef(false);
39
+ const hoverTimerRef = useRef<NodeJS.Timeout | null>(null);
40
+ const lastMousePosRef = useRef({ x: 0, y: 0 });
42
41
 
43
42
  const selectedMedia = value?.find((media) => media.id === selectedId);
44
43
  const isVideo = selectedMedia?.mediaType === MediaType.video;
@@ -47,50 +46,75 @@ export const MainMedia = ({
47
46
  useEffect(() => {
48
47
  setIsZoomed(false);
49
48
  setPosition({ x: 0, y: 0 });
49
+ if (hoverTimerRef.current) {
50
+ clearTimeout(hoverTimerRef.current);
51
+ }
50
52
  }, [selectedId]);
51
53
 
52
- const handleImageClick = () => {
53
- if (!enableZoom || isVideo) return;
54
- if (!hasDraggedRef.current) {
55
- setIsZoomed(!isZoomed);
56
- setPosition({ x: 0, y: 0 });
57
- }
58
- };
59
-
60
- const handleMouseDown = (e: React.MouseEvent) => {
61
- if (!enableZoom || isVideo) return;
62
- if (isZoomed) {
63
- setIsDragging(true);
64
- setDragStart({ x: e.clientX - position.x, y: e.clientY - position.y });
65
- hasDraggedRef.current = false;
66
- e.preventDefault();
54
+ // Clean up timer on unmount
55
+ useEffect(() => {
56
+ return () => {
57
+ if (hoverTimerRef.current) {
58
+ clearTimeout(hoverTimerRef.current);
59
+ }
60
+ };
61
+ }, []);
62
+
63
+ const updateZoomPosition = useCallback((clientX: number, clientY: number) => {
64
+ if (mainAreaRef.current) {
65
+ const { left, top, width, height } =
66
+ mainAreaRef.current.getBoundingClientRect();
67
+ const x = clientX - left;
68
+ const y = clientY - top;
69
+
70
+ // Calculate percentage position (0 to 1)
71
+ const ratioX = Math.max(0, Math.min(1, x / width));
72
+ const ratioY = Math.max(0, Math.min(1, y / height));
73
+
74
+ // Formula: Translate = Dimension * (0.5 - Ratio)
75
+ // This shifts the image opposite to mouse movement relative to center
76
+ const newX = width * (0.5 - ratioX);
77
+ const newY = height * (0.5 - ratioY);
78
+
79
+ setPosition({ x: newX, y: newY });
67
80
  }
68
- };
81
+ }, []);
69
82
 
70
- const handleMouseMove = useCallback(
83
+ const handleMouseEnter = useCallback(
71
84
  (e: React.MouseEvent) => {
72
- if (isDragging && isZoomed && mainAreaRef.current) {
73
- hasDraggedRef.current = true;
74
- const { width, height } = mainAreaRef.current.getBoundingClientRect();
75
- const scale = 2;
76
- const maxX = (width * (scale - 1)) / 2;
77
- const maxY = (height * (scale - 1)) / 2;
85
+ if (!enableZoom || isVideo) return;
86
+ lastMousePosRef.current = { x: e.clientX, y: e.clientY };
78
87
 
79
- let newX = e.clientX - dragStart.x;
80
- let newY = e.clientY - dragStart.y;
88
+ hoverTimerRef.current = setTimeout(() => {
89
+ setIsZoomed(true);
90
+ updateZoomPosition(
91
+ lastMousePosRef.current.x,
92
+ lastMousePosRef.current.y
93
+ );
94
+ }, 200);
95
+ },
96
+ [enableZoom, isVideo, updateZoomPosition]
97
+ );
81
98
 
82
- newX = Math.max(-maxX, Math.min(maxX, newX));
83
- newY = Math.max(-maxY, Math.min(maxY, newY));
99
+ const handleMouseMove = useCallback(
100
+ (e: React.MouseEvent) => {
101
+ if (!enableZoom || isVideo) return;
102
+ lastMousePosRef.current = { x: e.clientX, y: e.clientY };
84
103
 
85
- setPosition({ x: newX, y: newY });
104
+ if (isZoomed) {
105
+ updateZoomPosition(e.clientX, e.clientY);
86
106
  }
87
107
  },
88
- [isDragging, isZoomed, dragStart]
108
+ [enableZoom, isVideo, isZoomed, updateZoomPosition]
89
109
  );
90
110
 
91
- const handleMouseUp = () => {
92
- setIsDragging(false);
93
- };
111
+ const handleMouseLeave = useCallback(() => {
112
+ if (hoverTimerRef.current) {
113
+ clearTimeout(hoverTimerRef.current);
114
+ }
115
+ setIsZoomed(false);
116
+ setPosition({ x: 0, y: 0 });
117
+ }, []);
94
118
 
95
119
  return (
96
120
  <div
@@ -99,13 +123,11 @@ export const MainMedia = ({
99
123
  "relative group overflow-hidden rounded-xl bg-gray-100 dark:bg-gray-800 z-0",
100
124
  aspect,
101
125
  className,
102
- enableZoom && !isVideo ? (isZoomed ? "cursor-move" : "cursor-zoom-in") : ""
126
+ enableZoom && !isVideo && "cursor-crosshair"
103
127
  )}
104
- onClick={handleImageClick}
105
- onMouseDown={handleMouseDown}
128
+ onMouseEnter={handleMouseEnter}
106
129
  onMouseMove={handleMouseMove}
107
- onMouseUp={handleMouseUp}
108
- onMouseLeave={handleMouseUp}
130
+ onMouseLeave={handleMouseLeave}
109
131
  >
110
132
  {/* Navigation Arrows - Hide when zoomed to avoid misclick */}
111
133
  {!isZoomed && (
@@ -204,7 +226,7 @@ export const MainMedia = ({
204
226
  isZoomed ? 2 : 1
205
227
  })`
206
228
  : undefined,
207
- transition: isDragging ? "none" : "transform 0.3s ease-out",
229
+ transition: isZoomed ? "none" : "transform 0.3s ease-out",
208
230
  }}
209
231
  />
210
232
  )}