-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathHome.razor
More file actions
535 lines (473 loc) · 21 KB
/
Copy pathHome.razor
File metadata and controls
535 lines (473 loc) · 21 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
@page "/"
@inject IVirtualFileSystem VFS
@inject VFSStateService StateService
@inject ToastService Toast
@inject RecentFilesService RecentFiles
@inject RecycleBinService RecycleBin
@inject NavigationManager Navigation
@rendermode InteractiveServer
@implements IDisposable
<PageTitle>My Files - CloudDrive</PageTitle>
<div class="h-full flex flex-col bg-white">
@* Action Bar *@
<ActionBar OnNewFolder="ShowNewFolderDialog" OnUpload="ShowUploadDialog" />
@* Content Area *@
<div class="flex-1 overflow-auto">
@if (!HasItems)
{
<EmptyState Title="This folder is empty"
Description="Create a folder or upload files to get started">
<Action>
<div class="flex gap-2 justify-center">
<Button Variant="secondary" OnClick="ShowNewFolderDialog"
Icon="M12 10.5v6m3-3H9m4.06-7.19-2.12-2.12a1.5 1.5 0 0 0-1.061-.44H4.5A2.25 2.25 0 0 0 2.25 6v12a2.25 2.25 0 0 0 2.25 2.25h15A2.25 2.25 0 0 0 21.75 18V9a2.25 2.25 0 0 0-2.25-2.25h-5.379a1.5 1.5 0 0 1-1.06-.44Z">
New Folder
</Button>
<Button Variant="primary" OnClick="CreateSampleData"
Icon="M9.813 15.904 9 18.75l-.813-2.846a4.5 4.5 0 0 0-3.09-3.09L2.25 12l2.846-.813a4.5 4.5 0 0 0 3.09-3.09L9 5.25l.813 2.846a4.5 4.5 0 0 0 3.09 3.09L15.75 12l-2.846.813a4.5 4.5 0 0 0-3.09 3.09ZM18.259 8.715 18 9.75l-.259-1.035a3.375 3.375 0 0 0-2.455-2.456L14.25 6l1.036-.259a3.375 3.375 0 0 0 2.455-2.456L18 2.25l.259 1.035a3.375 3.375 0 0 0 2.456 2.456L21.75 6l-1.035.259a3.375 3.375 0 0 0-2.456 2.456ZM16.894 20.567 16.5 21.75l-.394-1.183a2.25 2.25 0 0 0-1.423-1.423L13.5 18.75l1.183-.394a2.25 2.25 0 0 0 1.423-1.423l.394-1.183.394 1.183a2.25 2.25 0 0 0 1.423 1.423l1.183.394-1.183.394a2.25 2.25 0 0 0-1.423 1.423Z">
Create Sample Data
</Button>
</div>
</Action>
</EmptyState>
}
else if (StateService.ViewMode == ViewMode.Grid)
{
<FileGrid>
@foreach (var item in GetSortedItems())
{
<FileCard Path="@item.Path"
Name="@item.Name"
IsDirectory="@item.IsDirectory"
ModifiedText="@item.ModifiedText"
OnSelect="SelectItem"
OnOpen="OpenItem"
OnContextMenu="ShowContextMenu" />
}
</FileGrid>
}
else
{
<FileList>
@foreach (var item in GetSortedItems())
{
<FileRow Path="@item.Path"
Name="@item.Name"
IsDirectory="@item.IsDirectory"
ModifiedText="@item.ModifiedText"
SizeText="@item.SizeText"
OnSelect="SelectItem"
OnOpen="OpenItem"
OnContextMenu="ShowContextMenu" />
}
</FileList>
}
</div>
</div>
@* New Folder Dialog *@
<Modal @bind-IsOpen="_showNewFolderDialog" Title="New Folder">
<ChildContent>
<Input @bind-Value="_newFolderName"
Label="Folder name"
Placeholder="Enter folder name"
OnKeyDown="HandleNewFolderKeyDown" />
</ChildContent>
<Footer>
<Button Variant="ghost" OnClick="() => _showNewFolderDialog = false">Cancel</Button>
<Button Variant="primary" OnClick="CreateNewFolder" Disabled="@string.IsNullOrWhiteSpace(_newFolderName)">Create</Button>
</Footer>
</Modal>
@* Upload (Create File) Dialog *@
<Modal @bind-IsOpen="_showUploadDialog" Title="Create New File">
<ChildContent>
<div class="space-y-4">
<Input @bind-Value="_newFileName"
Label="File name"
Placeholder="e.g., document.txt" />
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Content</label>
<textarea @bind="_newFileContent"
class="input min-h-[120px]"
placeholder="Enter file content (optional)"></textarea>
</div>
</div>
</ChildContent>
<Footer>
<Button Variant="ghost" OnClick="() => _showUploadDialog = false">Cancel</Button>
<Button Variant="primary" OnClick="CreateNewFile" Disabled="@string.IsNullOrWhiteSpace(_newFileName)">Create</Button>
</Footer>
</Modal>
@* Context Menu *@
@if (_showContextMenu)
{
<div class="context-menu" style="left: @(_contextMenuX)px; top: @(_contextMenuY)px;">
@if (_contextMenuIsDirectory)
{
<button class="context-menu-item" @onclick="OpenContextItem">
<svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 9.776c.112-.017.227-.026.344-.026h15.812c.117 0 .232.009.344.026m-16.5 0a2.25 2.25 0 0 0-1.883 2.542l.857 6a2.25 2.25 0 0 0 2.227 1.932H19.05a2.25 2.25 0 0 0 2.227-1.932l.857-6a2.25 2.25 0 0 0-1.883-2.542m-16.5 0V6A2.25 2.25 0 0 1 6 3.75h3.879a1.5 1.5 0 0 1 1.06.44l2.122 2.12a1.5 1.5 0 0 0 1.06.44H18A2.25 2.25 0 0 1 20.25 9v.776" />
</svg>
Open
</button>
}
else
{
<button class="context-menu-item" @onclick="OpenContextItem">
<svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L10.582 16.07a4.5 4.5 0 0 1-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 0 1 1.13-1.897l8.932-8.931Zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0 1 15.75 21H5.25A2.25 2.25 0 0 1 3 18.75V8.25A2.25 2.25 0 0 1 5.25 6H10" />
</svg>
Edit
</button>
}
<button class="context-menu-item" @onclick="RenameContextItem">
<svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L6.832 19.82a4.5 4.5 0 0 1-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 0 1 1.13-1.897L16.863 4.487Zm0 0L19.5 7.125" />
</svg>
Rename
</button>
<button class="context-menu-item" @onclick="ToggleStarContextItem">
<svg class="w-4 h-4" fill="@(StateService.IsStarred(_contextMenuPath) ? "currentColor" : "none")" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M11.48 3.499a.562.562 0 0 1 1.04 0l2.125 5.111a.563.563 0 0 0 .475.345l5.518.442c.499.04.701.663.321.988l-4.204 3.602a.563.563 0 0 0-.182.557l1.285 5.385a.562.562 0 0 1-.84.61l-4.725-2.885a.562.562 0 0 0-.586 0L6.982 20.54a.562.562 0 0 1-.84-.61l1.285-5.386a.562.562 0 0 0-.182-.557l-4.204-3.602a.562.562 0 0 1 .321-.988l5.518-.442a.563.563 0 0 0 .475-.345L11.48 3.5Z" />
</svg>
@(StateService.IsStarred(_contextMenuPath) ? "Remove star" : "Add star")
</button>
<div class="context-menu-separator"></div>
<button class="context-menu-item-danger" @onclick="DeleteContextItem">
<svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
</svg>
Delete
</button>
</div>
}
@* Rename Dialog *@
<Modal @bind-IsOpen="_showRenameDialog" Title="Rename">
<ChildContent>
<Input @bind-Value="_renameNewName"
Label="New name"
Placeholder="Enter new name"
OnKeyDown="HandleRenameKeyDown" />
</ChildContent>
<Footer>
<Button Variant="ghost" OnClick="() => _showRenameDialog = false">Cancel</Button>
<Button Variant="primary" OnClick="PerformRename" Disabled="@string.IsNullOrWhiteSpace(_renameNewName)">Rename</Button>
</Footer>
</Modal>
@code {
// Dialogs
private bool _showNewFolderDialog;
private string _newFolderName = "";
private bool _showUploadDialog;
private string _newFileName = "";
private string _newFileContent = "";
private bool _showRenameDialog;
private string _renameNewName = "";
private string _renameOldPath = "";
private bool _renameIsDirectory;
// Context Menu
private bool _showContextMenu;
private double _contextMenuX;
private double _contextMenuY;
private string _contextMenuPath = "";
private bool _contextMenuIsDirectory;
private bool HasItems => GetCurrentItems().Any();
// The current directory is reflected in the URL as ?path=docs/src so the
// location is deep-linkable (refresh / share / back-forward restore it).
[SupplyParameterFromQuery(Name = "path")]
public string? PathQuery { get; set; }
protected override void OnInitialized()
{
StateService.OnDataChanged += RequestRender;
StateService.OnCurrentPathChanged += OnCurrentPathChanged;
StateService.OnSortChanged += RequestRender;
StateService.OnViewModeChanged += RequestRender;
}
// Runs on first render and whenever the query string changes (deep link,
// refresh, browser back/forward): make the current folder match the URL.
protected override void OnParametersSet()
{
var desired = VFSStateService.FromQueryPath(PathQuery);
if (StateService.CurrentPath != desired)
StateService.NavigateTo(desired);
}
// When navigation happens through state (breadcrumb, opening a folder,
// import, etc.) push the matching URL so the address bar stays in sync.
private void OnCurrentPathChanged()
{
var target = VFSStateService.ToFolderUrl(StateService.CurrentPath);
var current = "/" + Navigation.ToBaseRelativePath(Navigation.Uri);
if (!string.Equals(current, target, StringComparison.Ordinal))
Navigation.NavigateTo(target);
InvokeAsync(StateHasChanged);
}
// Service events may fire on non-UI threads; marshal to the Dispatcher.
private void RequestRender() => InvokeAsync(StateHasChanged);
private IEnumerable<FileItem> GetCurrentItems()
{
var currentPath = StateService.CurrentPath;
// Get directories and files at current path
var directories = VFS.Directories
.Where(d => IsDirectChildOf(d.Path.Value, currentPath))
.Select(d => new FileItem
{
Path = d.Path.Value,
Name = d.Name,
IsDirectory = true,
ModifiedDate = d.LastWriteTime,
Size = 0
});
var files = VFS.Files
.Where(f => IsDirectChildOf(f.Path.Value, currentPath))
.Select(f => new FileItem
{
Path = f.Path.Value,
Name = f.Name,
IsDirectory = false,
ModifiedDate = f.LastWriteTime,
Size = f.Content?.Length ?? 0
});
return directories.Concat(files);
}
private bool IsDirectChildOf(string path, string parentPath)
{
// Normalize both paths to a consistent format
static string NormalizePath(string p)
{
p = p.TrimEnd('/');
// Normalize vfs:// and vfs:/ and vfs: to a single format
if (p == "vfs:" || p == "vfs:/" || p == "vfs://")
return "vfs://";
// Ensure path starts with vfs://
if (p.StartsWith("vfs:") && !p.StartsWith("vfs://"))
p = "vfs://" + p.Substring(4).TrimStart('/');
return p;
}
var normalizedPath = NormalizePath(path);
var normalizedParent = NormalizePath(parentPath);
// For root parent, check if path is a direct child (no additional slashes after vfs://)
if (normalizedParent == "vfs://")
{
// Remove the vfs:// prefix and check that there's exactly one segment
var pathWithoutPrefix = normalizedPath.Substring(6); // Remove "vfs://"
// Direct child has no slashes (it's a top-level item)
return !string.IsNullOrEmpty(pathWithoutPrefix) && !pathWithoutPrefix.Contains('/');
}
// For non-root parent, check if path starts with parent and has exactly one more segment
if (!normalizedPath.StartsWith(normalizedParent + "/"))
return false;
var remainder = normalizedPath.Substring(normalizedParent.Length + 1);
return !string.IsNullOrEmpty(remainder) && !remainder.Contains('/');
}
private IEnumerable<FileItem> GetSortedItems()
{
var items = GetCurrentItems();
// Sort directories first, then files
var directories = items.Where(i => i.IsDirectory);
var files = items.Where(i => !i.IsDirectory);
directories = SortItems(directories);
files = SortItems(files);
return directories.Concat(files);
}
private IEnumerable<FileItem> SortItems(IEnumerable<FileItem> items)
{
items = StateService.SortBy switch
{
SortOption.Name => StateService.SortAscending
? items.OrderBy(i => i.Name)
: items.OrderByDescending(i => i.Name),
SortOption.DateModified => StateService.SortAscending
? items.OrderBy(i => i.ModifiedDate)
: items.OrderByDescending(i => i.ModifiedDate),
SortOption.Size => StateService.SortAscending
? items.OrderBy(i => i.Size)
: items.OrderByDescending(i => i.Size),
_ => items.OrderBy(i => i.Name)
};
return items;
}
private void SelectItem(string path)
{
StateService.SelectItem(path);
HideContextMenu();
}
private void OpenItem(string path)
{
HideContextMenu();
if (VFS.DirectoryExists(path))
{
StateService.NavigateTo(path);
RecentFiles.RecordAccess(path, true);
}
else if (VFS.FileExists(path))
{
RecentFiles.RecordAccess(path, false);
Navigation.NavigateTo($"/file?path={Uri.EscapeDataString(path)}");
}
}
private void ShowContextMenu((string Path, double X, double Y) args)
{
_contextMenuPath = args.Path;
_contextMenuX = args.X;
_contextMenuY = args.Y;
_contextMenuIsDirectory = VFS.DirectoryExists(args.Path);
_showContextMenu = true;
StateService.SelectItem(args.Path);
}
private void HideContextMenu()
{
_showContextMenu = false;
}
private void OpenContextItem()
{
OpenItem(_contextMenuPath);
}
private void RenameContextItem()
{
HideContextMenu();
_renameOldPath = _contextMenuPath;
_renameIsDirectory = _contextMenuIsDirectory;
_renameNewName = System.IO.Path.GetFileName(_contextMenuPath.TrimEnd('/')) ?? "";
_showRenameDialog = true;
}
private void ToggleStarContextItem()
{
StateService.ToggleStar(_contextMenuPath);
Toast.ShowSuccess(StateService.IsStarred(_contextMenuPath) ? "Added to starred" : "Removed from starred");
HideContextMenu();
}
private void DeleteContextItem()
{
RecycleBin.MoveToRecycleBin(VFS, _contextMenuPath);
StateService.ClearSelection();
StateService.NotifyDataChanged();
Toast.ShowSuccess("Moved to Recycle Bin");
HideContextMenu();
}
private void ShowNewFolderDialog()
{
_newFolderName = "";
_showNewFolderDialog = true;
}
private void ShowUploadDialog()
{
_newFileName = "";
_newFileContent = "";
_showUploadDialog = true;
}
private void CreateNewFolder()
{
if (string.IsNullOrWhiteSpace(_newFolderName)) return;
try
{
var newPath = BuildChildPath(StateService.CurrentPath, _newFolderName);
VFS.CreateDirectory(newPath);
StateService.NotifyDataChanged();
Toast.ShowSuccess($"Created folder '{_newFolderName}'");
_showNewFolderDialog = false;
}
catch (Exception ex)
{
Toast.ShowError($"Failed to create folder: {ex.Message}");
}
}
private void CreateNewFile()
{
if (string.IsNullOrWhiteSpace(_newFileName)) return;
try
{
var newPath = BuildChildPath(StateService.CurrentPath, _newFileName);
VFS.CreateFile(newPath, _newFileContent);
StateService.NotifyDataChanged();
Toast.ShowSuccess($"Created file '{_newFileName}'");
_showUploadDialog = false;
}
catch (Exception ex)
{
Toast.ShowError($"Failed to create file: {ex.Message}");
}
}
private static string BuildChildPath(string parentPath, string childName)
{
// Handle root path specially to preserve "vfs://" prefix
if (parentPath is "vfs://" or "vfs:" or "vfs:/")
return $"vfs://{childName}";
return $"{parentPath.TrimEnd('/')}/{childName}";
}
private void PerformRename()
{
if (string.IsNullOrWhiteSpace(_renameNewName)) return;
try
{
if (_renameIsDirectory)
{
VFS.RenameDirectory(_renameOldPath, _renameNewName);
}
else
{
VFS.RenameFile(_renameOldPath, _renameNewName);
}
StateService.ClearSelection();
StateService.NotifyDataChanged();
Toast.ShowSuccess($"Renamed to '{_renameNewName}'");
_showRenameDialog = false;
}
catch (Exception ex)
{
Toast.ShowError($"Failed to rename: {ex.Message}");
}
}
private void HandleNewFolderKeyDown(KeyboardEventArgs e)
{
if (e.Key == "Enter")
CreateNewFolder();
}
private void HandleRenameKeyDown(KeyboardEventArgs e)
{
if (e.Key == "Enter")
PerformRename();
}
private void CreateSampleData()
{
try
{
VFS.CreateDirectory("/docs")
.CreateDirectory("/src")
.CreateDirectory("/tests")
.CreateDirectory("/images")
.CreateFile("/docs/readme.md", "# Welcome to CloudDrive\n\nThis is a virtual file system demonstration.\n\n## Features\n- File management\n- Folder navigation\n- Search functionality\n- Undo/Redo support")
.CreateFile("/docs/changelog.md", "# Changelog\n\n## v1.0.0\n- Initial release\n- Full VFS support")
.CreateFile("/src/main.cs", "using System;\n\nnamespace CloudDrive\n{\n class Program\n {\n static void Main(string[] args)\n {\n Console.WriteLine(\"Hello CloudDrive!\");\n }\n }\n}")
.CreateFile("/src/app.config.json", "{\n \"appName\": \"CloudDrive\",\n \"version\": \"1.0.0\",\n \"features\": {\n \"darkMode\": true,\n \"autoSave\": true\n }\n}")
.CreateFile("/tests/test.cs", "using Xunit;\n\npublic class CloudDriveTests\n{\n [Fact]\n public void FileCreation_ShouldWork()\n {\n Assert.True(true);\n }\n}");
StateService.NotifyDataChanged();
Toast.ShowSuccess("Sample data created successfully!");
}
catch (Exception ex)
{
Toast.ShowError($"Failed to create sample data: {ex.Message}");
}
}
public void Dispose()
{
StateService.OnDataChanged -= RequestRender;
StateService.OnCurrentPathChanged -= OnCurrentPathChanged;
StateService.OnSortChanged -= RequestRender;
StateService.OnViewModeChanged -= RequestRender;
}
private class FileItem
{
public string Path { get; init; } = "";
public string Name { get; init; } = "";
public bool IsDirectory { get; init; }
public DateTimeOffset ModifiedDate { get; init; }
public int Size { get; init; }
public string ModifiedText => ModifiedDate.DateTime.ToString("MMM d, yyyy");
public string SizeText => IsDirectory ? "-" : FormatSize(Size);
private static string FormatSize(int bytes)
{
if (bytes < 1024) return $"{bytes} B";
if (bytes < 1024 * 1024) return $"{bytes / 1024.0:F1} KB";
return $"{bytes / (1024.0 * 1024.0):F1} MB";
}
}
}