-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathFileItem.jsx
More file actions
284 lines (251 loc) · 8.51 KB
/
FileItem.jsx
File metadata and controls
284 lines (251 loc) · 8.51 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import { useEffect, useRef, useState } from "react";
import { FaRegFile, FaRegFolderOpen } from "react-icons/fa6";
import { useFileIcons } from "../../hooks/useFileIcons";
import CreateFolderAction from "../Actions/CreateFolder/CreateFolder.action";
import RenameAction from "../Actions/Rename/Rename.action";
import { getDataSize } from "../../utils/getDataSize";
import { formatDate } from "../../utils/formatDate";
import { getActionByKey } from "../../utils/getActionByKey";
import { useFileNavigation } from "../../contexts/FileNavigationContext";
import { useSelection } from "../../contexts/SelectionContext";
import { useClipBoard } from "../../contexts/ClipboardContext";
import { useLayout } from "../../contexts/LayoutContext";
import Checkbox from "../../components/Checkbox/Checkbox";
const dragIconSize = 50;
const FileItem = ({
index,
file,
actions,
onCreateFolder,
enableFilePreview,
filesViewRef,
selectedFileIndexes,
triggerAction,
handleContextMenu,
setLastSelectedFile,
}) => {
const [fileSelected, setFileSelected] = useState(false);
const [lastClickTime, setLastClickTime] = useState(0);
const [checkboxClassName, setCheckboxClassName] = useState("hidden");
const [dropZoneClass, setDropZoneClass] = useState("");
const [tooltipPosition, setTooltipPosition] = useState(null);
const { activeLayout } = useLayout();
const iconSize = activeLayout === "grid" ? 48 : 20;
const fileIcons = useFileIcons(iconSize);
const { setCurrentPath, currentPathFiles } = useFileNavigation();
const { setSelectedFiles } = useSelection();
const { clipBoard, handleCutCopy, setClipBoard, handlePasting } = useClipBoard();
const dragIconRef = useRef(null);
const dragIcons = useFileIcons(dragIconSize);
const isFileMoving =
clipBoard?.isMoving &&
clipBoard.files.find((f) => f.name === file.name && f.path === file.path);
const handleFileAccess = () => {
const openAction = getActionByKey(actions, "open");
openAction.onClick(file);
if (file.isDirectory) {
setCurrentPath(file.path);
setSelectedFiles([]);
} else {
enableFilePreview && triggerAction.show("previewFile");
}
};
const handleFileRangeSelection = (shiftKey, ctrlKey) => {
if (selectedFileIndexes.length > 0 && shiftKey) {
let reverseSelection = false;
let startRange = selectedFileIndexes[0];
let endRange = index;
// Reverse Selection
if (startRange >= endRange) {
const temp = startRange;
startRange = endRange;
endRange = temp;
reverseSelection = true;
}
const filesRange = currentPathFiles.slice(startRange, endRange + 1);
setSelectedFiles(reverseSelection ? filesRange.reverse() : filesRange);
} else if (selectedFileIndexes.length > 0 && ctrlKey) {
// Remove file from selected files if it already exists on CTRL + Click, otherwise push it in selectedFiles
setSelectedFiles((prev) => {
const filteredFiles = prev.filter((f) => f.path !== file.path);
if (prev.length === filteredFiles.length) {
return [...prev, file];
}
return filteredFiles;
});
} else {
setSelectedFiles([file]);
}
};
const handleFileSelection = (e) => {
e.stopPropagation();
if (file.isEditing) return;
handleFileRangeSelection(e.shiftKey, e.ctrlKey);
const currentTime = new Date().getTime();
if (currentTime - lastClickTime < 300) {
handleFileAccess();
return;
}
setLastClickTime(currentTime);
};
const handleOnKeyDown = (e) => {
if (e.key === "Enter") {
e.stopPropagation();
setSelectedFiles([file]);
handleFileAccess();
}
};
const handleItemContextMenu = (e) => {
e.stopPropagation();
e.preventDefault();
if (file.isEditing) return;
if (!fileSelected) {
setSelectedFiles([file]);
}
setLastSelectedFile(file);
handleContextMenu(e, true);
};
// Selection Checkbox Functions
const handleMouseOver = () => {
setCheckboxClassName("visible");
};
const handleMouseLeave = () => {
!fileSelected && setCheckboxClassName("hidden");
};
const handleCheckboxChange = (e) => {
if (e.target.checked) {
setSelectedFiles((prev) => [...prev, file]);
} else {
setSelectedFiles((prev) => prev.filter((f) => f.name !== file.name && f.path !== file.path));
}
setFileSelected(e.target.checked);
};
//
const handleDragStart = (e) => {
e.dataTransfer.setDragImage(dragIconRef.current, 30, 50);
e.dataTransfer.effectAllowed = "copy";
handleCutCopy(true);
};
const handleDragEnd = () => setClipBoard(null);
const handleDragEnterOver = (e) => {
e.preventDefault();
if (fileSelected || !file.isDirectory) {
e.dataTransfer.dropEffect = "none";
} else {
setTooltipPosition({ x: e.clientX, y: e.clientY + 12 });
e.dataTransfer.dropEffect = "copy";
setDropZoneClass("file-drop-zone");
}
};
const handleDragLeave = (e) => {
// To stay in dragging state for the child elements of the target drop-zone
if (!e.currentTarget.contains(e.relatedTarget)) {
setDropZoneClass((prev) => (prev ? "" : prev));
setTooltipPosition(null);
}
};
const handleDrop = (e) => {
e.preventDefault();
if (fileSelected || !file.isDirectory) return;
handlePasting(file);
setDropZoneClass((prev) => (prev ? "" : prev));
setTooltipPosition(null);
};
useEffect(() => {
setFileSelected(selectedFileIndexes.includes(index));
setCheckboxClassName(selectedFileIndexes.includes(index) ? "visible" : "hidden");
}, [selectedFileIndexes]);
return (
<div
className={`file-item-container ${dropZoneClass} ${
fileSelected || !!file.isEditing ? "file-selected" : ""
} ${isFileMoving ? "file-moving" : ""}`}
tabIndex={0}
title={file.name}
onClick={handleFileSelection}
onKeyDown={handleOnKeyDown}
onContextMenu={handleItemContextMenu}
onMouseOver={handleMouseOver}
onMouseLeave={handleMouseLeave}
draggable={fileSelected}
onDragStart={handleDragStart}
onDragEnd={handleDragEnd}
onDragEnter={handleDragEnterOver}
onDragOver={handleDragEnterOver}
onDragLeave={handleDragLeave}
onDrop={handleDrop}
>
<div className="file-item">
{!file.isEditing && (
<Checkbox
name={file.name}
id={file.name}
checked={fileSelected}
className={`selection-checkbox ${checkboxClassName}`}
onChange={handleCheckboxChange}
onClick={(e) => e.stopPropagation()}
/>
)}
{file.isDirectory ? (
<FaRegFolderOpen size={iconSize} />
) : (
<>
{fileIcons[file.name?.split(".").pop()?.toLowerCase()] ?? <FaRegFile size={iconSize} />}
</>
)}
{file.isEditing ? (
<div className={`rename-file-container ${activeLayout}`}>
{triggerAction.actionType === "createFolder" ? (
<CreateFolderAction
filesViewRef={filesViewRef}
file={file}
onCreateFolder={onCreateFolder}
triggerAction={triggerAction}
/>
) : (
<RenameAction
filesViewRef={filesViewRef}
file={file}
onRename={getActionByKey(actions, "rename").onClick}
triggerAction={triggerAction}
/>
)}
</div>
) : (
<span className="text-truncate file-name">{file.name}</span>
)}
</div>
{activeLayout === "list" && (
<>
<div className="modified-date">{formatDate(file.updatedAt)}</div>
<div className="size">{file?.size > 0 ? getDataSize(file?.size) : ""}</div>
</>
)}
{/* Drag Icon & Tooltip Setup */}
{tooltipPosition && (
<div
style={{
top: `${tooltipPosition.y}px`,
left: `${tooltipPosition.x}px`,
}}
className="drag-move-tooltip"
>
Move to <span className="drop-zone-file-name">{file.name}</span>
</div>
)}
<div ref={dragIconRef} className="drag-icon">
{file.isDirectory ? (
<FaRegFolderOpen size={dragIconSize} />
) : (
<>
{dragIcons[file.name?.split(".").pop()?.toLowerCase()] ?? (
<FaRegFile size={dragIconSize} />
)}
</>
)}
</div>
{/* Drag Icon & Tooltip Setup */}
</div>
);
};
export default FileItem;