-
Notifications
You must be signed in to change notification settings - Fork 1
/
customDrag.js
109 lines (95 loc) · 2.63 KB
/
customDrag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import React from "react";
import { useState, useEffect, useRef } from "react";
import { Resizable } from "re-resizable";
import Draggable from "react-draggable";
export const DraggableObject = ({ imageUrl, text }) => {
const [backgroundStyle, setBackgroundStyle] = useState({
backgroundSize: "contain",
backgroundRepeat: "no-repeat",
});
const pressTimeRef = useRef(0);
const isDraggedRef = useRef(false);
const [isDeleted, setIsDeleted] = useState(false);
const [isSelected, setIsSelected] = useState(false);
const [isDragging, setIsDragging] = useState(false);
const handleToggleSelect = () => {
setIsSelected(!isSelected);
};
const handleDelete = () => {
setIsDeleted(true);
onDelete(); // Notify the parent component about the deletion
};
useEffect(() => {
let timer;
if (isSelected) {
timer = setTimeout(() => {
setIsSelected(false);
}, 1000);
}
return () => clearTimeout(timer);
}, [isSelected]);
useEffect(() => {
setBackgroundStyle((prevStyle) => ({
...prevStyle,
backgroundImage: `url(${imageUrl})`,
}));
}, [imageUrl]);
const handlePress = () => {
pressTimeRef.current = Date.now();
isDraggedRef.current = false;
};
const handleRelease = () => {
const pressTime = pressTimeRef.current;
const releaseTime = Date.now();
const pressReleaseDelta = releaseTime - pressTime;
console.log(pressReleaseDelta);
if (pressReleaseDelta < 300 || !isDraggedRef.current) {
setIsSelected(!isSelected);
onSelect();
}
};
const handleDrag = () => {
isDraggedRef.current = true;
};
console.log("Import Image", imageUrl);
imageUrl = text ? "" : imageUrl;
if (isDeleted) {
return <div></div>; // If the object is deleted, don't render anything
}
return (
<Draggable
bounds="parent"
onMouseDown={handlePress}
onMouseUp={handleRelease}
onTouchStart={handlePress}
onTouchEnd={handleRelease}
cancel=".delete-button"
onDrag={handleDrag}
>
<Resizable
defaultSize={{
width: 220,
height: 220,
}}
style={backgroundStyle}
lockAspectRatio={true}
onClick={handleToggleSelect}
className={`deletable-object`}
>
{text}
{isSelected && !isDraggedRef.current && (
<button className="delete-button" onClick={handleDelete}>
x
</button>
)}
</Resizable>
</Draggable>
);
};
export const EmojiDraggable = ({ emoji }) => {
return (
<Draggable bounds={"parent"}>
<div className="emoji">{emoji}</div>
</Draggable>
);
};