Add drag and drop to file manager

This commit is contained in:
Pixeldrain-Server committed 2026-10-02 15:10:34 +02:00
1 parent 9a7ed0a347
commit f5b9f543c6
14 files changed
+432 -466

No files matched your search

+134 -188
View File
@@ -1,10 +1,13 @@
<script lang="ts">
import { formatDataVolume, formatDate } from "util/Formatting";
import { fs_check_response, fs_encode_path, fs_get_trash, fs_node_icon, fs_path_url, type TrashEntry } from "lib/FilesystemAPI.svelte";
import { loading_finish, loading_run, loading_start, loading_store } from "lib/Loading";
import { formatDataVolume } from "util/Formatting";
import { fs_check_response, fs_get_trash, fs_path_url, fs_rename, fs_trash, type FSNode } from "lib/FilesystemAPI.svelte";
import { loading_run } from "lib/Loading";
import { onMount } from "svelte";
import { breadcrumbs_store } from "wrap/HeaderStore";
import { toolbar_store } from "wrap/HeaderStore";
import { FSNavigator } from "filesystem/FSNavigator";
import Button from "layout/Button.svelte";
import ListView from "./ListView.svelte";
import { FileAction, type FileActionHandler } from "./FileManagerLib";
let {
nav,
@@ -14,214 +17,157 @@ let {
root_path?: string
} = $props();
let entries: TrashEntry[] = $state([]);
let error: string = $state("");
// The trash entries are not the children of the .Trash directory, so they get a
// navigator of their own for the ListView to list and sort
const trash = new FSNavigator(false)
const load = async () => {
loading_run(async () => {
try {
entries = await fs_get_trash(root_path)
error = "";
} catch (err: any) {
error = err?.message ?? JSON.stringify(err);
let size = $derived($trash.children.reduce(
(acc, node) => acc + (node.type === "dir" ? node.recursive_size : node.file_size), 0
))
const load = () => loading_run(async () => {
try {
trash.children = await fs_get_trash(root_path)
} catch (err: any) {
alert("Failed to load the trash bin: " + (err.message ?? err))
}
trash.selecting = false
trash.selection.clear()
trash.sort_children("")
})
const file_event: FileActionHandler = (action, index, e) => {
if (action === FileAction.Drag) {
return
} else if (action === FileAction.Context) {
// On a touch screen a long press starts selecting, like in the file
// manager. A right click is left to the browser's own menu
if (!navigator.maxTouchPoints) {
return
}
})
};
trash.selecting = true
}
e.preventDefault()
e.stopPropagation()
const delete_entry = async (entry: TrashEntry) => {
if (!confirm(`Permanently delete "${entry.node.name}"?`)) return;
try {
loading_start();
await fs_check_response(
await fetch(fs_path_url(entry.node.path) + "?trash", { method: "DELETE" })
);
entries = entries.filter(e => e.node.id !== entry.node.id);
} catch (err: any) {
alert(err?.message ?? JSON.stringify(err));
} finally {
loading_finish();
if (trash.selecting) {
trash.select(trash.children[index], (e as MouseEvent).shiftKey)
} else {
nav.navigate(trash.children[index].path, true)
}
};
}
const restore_entry = async (entry: TrashEntry) => {
if (!entry.original_path) {
alert("Original path is not known, cannot restore this file.");
return;
const selected = () => [...trash.selection.values()]
// Runs an action on all the selected entries and reloads the listing
const run_selected = async (action: (node: FSNode) => Promise<any>) => {
const results = await loading_run(() => Promise.allSettled(selected().map(action)))
const failed = results.filter(r => r.status === "rejected") as PromiseRejectedResult[]
if (failed.length !== 0) {
alert(
failed.length + " of " + results.length + " failed: " +
(failed[0].reason.message ?? failed[0].reason)
)
}
try {
loading_start();
const form = new FormData();
form.append("action", "rename");
form.append("target", "/me" + entry.original_path);
await fs_check_response(
await fetch(fs_path_url(entry.node.path), { method: "POST", body: form })
);
entries = entries.filter(e => e.node.id !== entry.node.id);
} catch (err: any) {
alert(err?.message ?? JSON.stringify(err));
} finally {
loading_finish();
await load()
}
const restore = () => run_selected(async node => {
if (!node.original_path) {
throw new Error("the original location of " + node.name + " is not known")
}
};
return fs_rename(node.path, root_path + node.original_path)
})
const delete_selected = () => {
if (confirm(`Permanently delete ${selected().length} of the items in the trash?`)) {
run_selected(node => fs_trash(node.path))
}
}
const empty_trash = async () => {
if (!confirm("Permanently delete all items in the trash?")) return;
try {
loading_start();
await fs_check_response(
await fetch(fs_path_url(root_path) + "?empty_trash", { method: "DELETE" })
);
entries = [];
} catch (err: any) {
alert(err?.message ?? JSON.stringify(err));
} finally {
loading_finish();
if (!confirm("Permanently delete all items in the trash?")) {
return
}
};
try {
await loading_run(async () => fs_check_response(
await fetch(fs_path_url(root_path) + "?empty_trash", { method: "DELETE" })
))
} catch (err: any) {
alert(err.message ?? err)
}
await load()
}
onMount(() => {
breadcrumbs_store.set(breadcrumbs)
toolbar_store.set(toolbar)
load()
});
return () => toolbar_store.set(null)
})
</script>
{#snippet breadcrumbs()}
<i class="icon">delete</i>
<div>
Trash bin
{#if $loading_store === 0 && entries.length === 0}
(empty)
{:else if entries.length !== 0}
({formatDataVolume(entries.reduce(
(acc, entry) => acc + entry.node.file_size, 0
), 3)})
{/if}
</div>
<div class="separator"></div>
<button onclick={() => window.history.back()} title="Back">
<i class="icon">arrow_back</i>
</button>
<button onclick={() => nav.navigate_up()} disabled={$nav.path.length <= 1} title="Up">
<i class="icon">north</i>
</button>
<button onclick={() => nav.reload()} title="Refresh directory listing">
<i class="icon">refresh</i>
</button>
{#if entries.length !== 0}
<button class="button button_red" onclick={empty_trash}>
{#snippet toolbar()}
{#if $trash.selecting}
<div class="toolbar toolbar_edit">
<Button icon="close" click={() => trash.set_selecting(false)}/>
<div class="toolbar_spacer">Selecting files</div>
<Button click={restore} icon="restore_from_trash" label="Restore"/>
<button onclick={delete_selected} class="button_red">
<i class="icon">delete_forever</i>
<span>Delete all</span>
Delete
</button>
{/if}
</div>
{:else}
<div class="toolbar">
<div>
<button onclick={() => history.back()} title="Back">
<i class="icon">arrow_back</i>
</button>
<button onclick={() => nav.navigate_up()} title="Up">
<i class="icon">north</i>
</button>
<button onclick={load} title="Refresh trash bin">
<i class="icon">refresh</i>
</button>
</div>
<div>Trash bin ({formatDataVolume(size, 3)})</div>
<div>
{#if $trash.children.length !== 0}
<button onclick={() => trash.set_selecting(true)} title="Select files to restore or delete">
<i class="icon">select_all</i>
</button>
<button onclick={empty_trash} class="button_red">
<i class="icon">delete_forever</i>
Delete all
</button>
{/if}
</div>
</div>
{/if}
{/snippet}
{#if error}
<p class="error">{error}</p>
{:else if entries.length > 0}
<table class="trash_table">
<thead>
<tr>
<td></td>
<td>Name</td>
<td class="hide_small">Original location</td>
<td class="hide_small">Deleted at</td>
<td class="hide_small">Size</td>
<td></td>
</tr>
</thead>
<tbody>
{#each entries as entry (entry.node.id)}
<tr class="entry">
<td class="icon_cell">
<img
src={fs_node_icon(entry.node, 32, 32)}
class="node_icon"
alt="icon"
/>
</td>
<td class="name_cell">
<a class="entry_link" href={"/d" + fs_encode_path(entry.node.path)}>
{entry.node.name}
</a>
</td>
<td class="hide_small">
{entry.original_path ? entry.original_path.slice(0, -entry.node.name.length) : "—"}
</td>
<td class="hide_small">
{entry.deletion_date ? formatDate(entry.deletion_date) : "—"}
</td>
<td class="hide_small">
{#if entry.node.type === "file"}
{formatDataVolume(entry.node.file_size, 3)}
{/if}
</td>
<td>
<div class="row_actions">
<button title="Restore to original location" onclick={() => restore_entry(entry)}>
<i class="icon">restore_from_trash</i>
</button>
<button class="button button_red" title="Delete permanently" onclick={() => delete_entry(entry)}>
<i class="icon">delete_forever</i>
</button>
</div>
</td>
</tr>
{/each}
</tbody>
</table>
{/if}
<ListView nav={trash} file_event={file_event} show_hidden hide_edit hide_branding trash/>
<style>
.separator {
flex: 1 1 auto;
}
.error {
color: var(--red_color);
}
.trash_table {
width: 100%;
border-collapse: collapse;
background: var(--body_background);
border-radius: 8px;
}
.trash_table td {
padding: 0;
}
.entry {
cursor: default;
/* Same as the toolbar of the file manager */
.toolbar {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
border-top: 1px solid var(--separator);
}
.entry:hover {
background: var(--input_hover_background);
}
.icon_cell {
width: 2.5em;
}
.node_icon {
display: block;
width: 2em;
height: 2em;
object-fit: contain;
}
.name_cell {
word-break: break-all;
}
.entry_link {
color: var(--body_text_color);
text-decoration: none;
}
.entry_link:hover {
text-decoration: underline;
}
.row_actions {
.toolbar > * {
flex: 0 0 content;
display: flex;
gap: 0.25em;
flex-direction: row;
}
@media (max-width: 600px) {
.hide_small {
display: none;
}
.toolbar_spacer {
flex: 1 1 auto;
text-align: center;
}
.toolbar_edit {
background-color: var(--green_tint);
}
</style>