Files
fnx_web/svelte/src/filesystem/filemanager/FileManager.svelte
T

355 lines
10 KiB
Svelte
Raw Normal View History

<script lang="ts" module>
// Scroll offset per directory path. The file manager is destroyed when a file
// is opened, so the positions are kept here to survive the remount
const scroll_positions = new Map<string, number>()
</script>
<script lang="ts">
2026-10-02 15:10:34 +02:00
import { fs_trash } from "lib/FilesystemAPI.svelte"
import CreateDirectory from "./CreateDirectory.svelte"
import ListView from "./ListView.svelte"
import GalleryView from "./GalleryView.svelte"
import CompactView from "./CompactView.svelte"
import Button from "layout/Button.svelte";
import { formatDate } from "util/Formatting";
import { drop_target } from "lib/DropTarget"
2026-10-02 15:10:34 +02:00
import { drag_click, move_target, type FSNavigator } from "filesystem/FSNavigator";
2026-10-05 21:47:04 +02:00
import { pick_files, upload_files } from "filesystem/upload_widget/FSUploadWidget.svelte";
import EditWindow from "filesystem/edit_window/EditWindow.svelte";
import { FileAction, type FileActionHandler } from "./FileManagerLib";
2025-10-09 15:48:23 +02:00
import FileMenu from "./FileMenu.svelte";
2025-10-10 00:12:14 +02:00
import { loading_finish, loading_start } from "lib/Loading";
2026-10-02 18:19:42 +02:00
import { open_search } from "../SearchBar.svelte";
import { onMount } from "svelte";
import { toolbar_store } from "wrap/HeaderStore";
2020-11-17 23:39:27 +01:00
2025-10-13 16:05:50 +02:00
let {
nav = $bindable(),
edit_window = $bindable(),
directory_view = $bindable(localStorage.getItem("directory_view") || "list"),
2025-10-13 16:05:50 +02:00
children
}: {
nav: FSNavigator;
edit_window: EditWindow;
directory_view?: string;
children?: import('svelte').Snippet;
} = $props();
let large_icons = $state(localStorage.getItem("large_icons") === "true")
2026-10-02 15:10:34 +02:00
// In selection mode a click selects. Moving is the part of selection mode where
// the user browses to the directory which the selection has to go to
let moving = $state(false)
2025-10-13 16:05:50 +02:00
let creating_dir = $state(false)
let show_hidden = $state(false)
2026-06-10 23:53:03 +02:00
let file_menu!: FileMenu
$effect(() => {
window.scrollTo(0, scroll_positions.get($nav.base.path) ?? 0)
})
2022-02-21 21:55:09 +01:00
// Navigation functions
const file_event: FileActionHandler = (action: FileAction, index: number, orig: Event) => {
2026-10-02 15:10:34 +02:00
const node = nav.children[index]
if (action === FileAction.Drag) {
// Without write access it stays a normal drag of the link
if (nav.permissions.write) {
nav.drag_start(orig as DragEvent, node)
}
return
}
orig.preventDefault()
orig.stopPropagation()
2022-02-22 19:53:48 +01:00
switch (action) {
2025-04-30 13:21:11 +02:00
case FileAction.Click:
creating_dir = false
2020-11-17 23:39:27 +01:00
2026-10-02 15:10:34 +02:00
if (!nav.selecting) {
nav.navigate(node.path, true)
} else if (!moving) {
nav.select(node, (orig as MouseEvent).shiftKey, show_hidden)
} else if (node.type === "dir" && !nav.selection.has(node.path)) {
2025-04-30 13:21:11 +02:00
// If we are moving files we can only enter directories, and only if
// they're not selected. That last requirement prevents people from
// moving a directory into itself
2026-10-02 15:10:34 +02:00
nav.navigate(node.path, true)
2025-04-30 13:21:11 +02:00
}
break
case FileAction.Context:
// If this is a touch event we will select the item
if (navigator.maxTouchPoints && navigator.maxTouchPoints > 0) {
2026-10-02 15:10:34 +02:00
nav.select(node)
} else {
2026-10-02 15:10:34 +02:00
file_menu.open(node, orig.target, orig)
2023-11-16 16:33:35 +01:00
}
2025-04-30 13:21:11 +02:00
break
case FileAction.Share:
creating_dir = false
2026-10-02 15:10:34 +02:00
edit_window.edit(node, false, "share")
2025-04-30 13:21:11 +02:00
break
case FileAction.Branding:
2026-10-02 15:10:34 +02:00
edit_window.edit(node, false, "branding")
2025-04-30 13:21:11 +02:00
break
2025-10-09 15:48:23 +02:00
case FileAction.Menu:
2026-10-02 15:10:34 +02:00
file_menu.open(node, orig.target, orig)
2025-10-09 15:48:23 +02:00
break
}
}
2024-02-16 14:50:34 +01:00
2023-05-29 22:15:46 +02:00
const navigate_back = () => {
creating_dir = false
history.back()
}
2020-11-17 23:39:27 +01:00
// Deletion function
2023-11-16 16:33:35 +01:00
const delete_selected = async () => {
try {
2025-10-10 00:12:14 +02:00
loading_start()
2026-10-02 15:10:34 +02:00
await Promise.all([...nav.selection.values()].map(node => fs_trash(node.path)))
2023-11-16 16:33:35 +01:00
} catch (err) {
console.log(err)
alert("Delete failed: " + err.message + " ("+err.value+")")
} finally {
2026-10-02 15:10:34 +02:00
nav.set_selecting(false)
2025-10-10 00:12:14 +02:00
loading_finish()
2025-11-03 19:15:57 +01:00
await nav.reload()
2020-11-17 23:39:27 +01:00
}
2023-11-16 16:33:35 +01:00
}
2023-05-17 15:34:56 +02:00
const toggle_view = () => {
if (directory_view === "list") {
directory_view = "gallery"
2025-01-28 12:00:04 +01:00
} else if (directory_view === "gallery") {
directory_view = "compact"
2023-05-17 15:34:56 +02:00
} else {
directory_view = "list"
}
localStorage.setItem("directory_view", directory_view)
}
const toggle_large_icons = () => {
large_icons = !large_icons
localStorage.setItem("large_icons", JSON.stringify(large_icons))
}
2026-10-02 15:10:34 +02:00
const keydown = (e: KeyboardEvent) => {
if (
(document.activeElement as any).type !== undefined &&
(document.activeElement as any).type === "text"
) {
return // Prevent shortcuts from interfering with input fields
}
2026-10-02 15:10:34 +02:00
if (e.key === "a" && e.ctrlKey) {
// CTRL + A selects all files which are visible
for (const child of nav.children) {
if (show_hidden || !child.is_hidden()) {
nav.selection.set(child.path, child)
}
2025-01-28 12:00:04 +01:00
}
2026-10-02 15:10:34 +02:00
nav.set_selecting(true)
2025-01-28 12:00:04 +01:00
e.preventDefault()
2026-10-02 15:10:34 +02:00
} else if (e.key === "Escape" && (nav.selecting || creating_dir)) {
2025-01-28 12:00:04 +01:00
// When escape is pressed we return to viewing mode
2026-10-02 15:10:34 +02:00
nav.set_selecting(false)
creating_dir = false
2025-01-28 12:00:04 +01:00
e.preventDefault()
e.stopPropagation()
2023-11-17 13:14:16 +01:00
}
}
2026-10-02 15:10:34 +02:00
// Runs every time the navigator changes
2026-02-26 14:47:01 +01:00
$effect.pre(() => {
2026-10-02 15:10:34 +02:00
if (!$nav.selecting) {
moving = false
2023-11-16 16:33:35 +01:00
}
2026-10-02 15:10:34 +02:00
creating_dir = false
2026-02-26 14:47:01 +01:00
})
2023-11-17 13:14:16 +01:00
// The toolbar is shown in the bar at the top of the page, under the breadcrumbs.
// Other pages don't have it
onMount(() => {
toolbar_store.set(toolbar)
return () => toolbar_store.set(null)
})
2020-11-17 23:39:27 +01:00
</script>
<svelte:window
2026-10-02 15:10:34 +02:00
onkeydown={keydown}
onscroll={() => scroll_positions.set(nav.base.path, window.scrollY)}
/>
2023-11-17 13:14:16 +01:00
2026-10-02 15:10:34 +02:00
{#snippet nav_buttons()}
<button onclick={navigate_back} use:drag_click title="Back">
<i class="icon">arrow_back</i>
</button>
<button onclick={() => nav.navigate_up()} use:drag_click disabled={$nav.path.length <= 1} title="Up">
<i class="icon">north</i>
</button>
<button onclick={() => nav.reload()} use:drag_click title="Refresh directory listing">
<i class="icon">refresh</i>
</button>
{/snippet}
{#snippet toolbar()}
2026-10-02 15:10:34 +02:00
{#if !$nav.selecting}
<div class="toolbar">
<div class="toolbar_left">
2026-10-02 18:19:42 +02:00
<button onclick={() => open_search()} title="Press / to search">
<i class="icon">search</i>
</button>
2026-10-02 15:10:34 +02:00
{@render nav_buttons()}
{#if $nav.children.some(c => c.name === ".Trash" && c.type === "dir")}
<button
onclick={() => nav.navigate($nav.children.find(c => c.name === ".Trash").path, true)}
title="Open trash bin"
>
<i class="icon">delete</i>
2025-01-28 12:00:04 +01:00
</button>
{/if}
</div>
2024-02-13 13:13:44 +01:00
<div class="toolbar_middle">
<button onclick={() => toggle_view()} title="Switch between gallery, list and compact view">
<i class="icon" class:button_highlight={directory_view === "list"}>list</i>
<i class="icon" class:button_highlight={directory_view === "gallery"}>collections</i>
<i class="icon" class:button_highlight={directory_view === "compact"}>view_compact</i>
</button>
2023-11-16 16:33:35 +01:00
<button class="button_large_icons" onclick={() => toggle_large_icons()} title="Switch between large and small icons">
{#if large_icons}
<i class="icon">zoom_out</i>
{:else}
<i class="icon">zoom_in</i>
{/if}
</button>
2024-02-13 13:13:44 +01:00
<button onclick={() => {show_hidden = !show_hidden}} title="Toggle hidden files">
{#if show_hidden}
<i class="icon">visibility_off</i>
{:else}
<i class="icon">visibility</i>
{/if}
</button>
</div>
2025-01-28 12:00:04 +01:00
<div class="toolbar_right">
{#if $nav.permissions.write}
2026-10-05 21:47:04 +02:00
<button onclick={() => pick_files()} title="Upload files to this directory">
<i class="icon">cloud_upload</i>
</button>
2025-01-28 12:00:04 +01:00
<Button click={() => {creating_dir = !creating_dir}} highlight={creating_dir} icon="create_new_folder" title="Make folder"/>
2025-01-28 12:00:04 +01:00
2026-10-02 15:10:34 +02:00
<button onclick={() => nav.set_selecting(true)} title="Select and delete files">
<i class="icon">select_all</i>
</button>
{/if}
2023-11-16 16:33:35 +01:00
</div>
</div>
2026-10-02 15:10:34 +02:00
{:else}
<div class="toolbar toolbar_edit">
2026-10-02 15:10:34 +02:00
<div>
<Button click={() => nav.set_selecting(false)} icon="close"/>
{@render nav_buttons()}
</div>
<div class="toolbar_spacer">
2026-10-02 15:10:34 +02:00
{moving ? "Moving" : "Selected"} {$nav.selection.size} items
</div>
<div>
{#if moving}
<Button click={() => {creating_dir = !creating_dir}} highlight={creating_dir} icon="create_new_folder" title="Make folder"/>
<Button
click={() => nav.move_selection(nav.base.path)}
disabled={!$nav.can_move($nav.base.path)}
highlight
icon="done"
label="Move here"
/>
{:else}
<Button click={() => {moving = true}} icon="drive_file_move" label="Move"/>
<button onclick={delete_selected} class="button_red">
<i class="icon">delete</i>
Delete
</button>
{/if}
2020-11-17 23:39:27 +01:00
</div>
</div>
{/if}
2023-11-16 16:33:35 +01:00
{#if creating_dir}
<CreateDirectory nav={nav} />
{/if}
{/snippet}
2022-02-22 19:53:48 +01:00
<div
class="container"
2026-10-02 15:10:34 +02:00
use:move_target={{nav, node: () => nav.base}}
use:drop_target={{
2026-10-05 21:47:04 +02:00
upload: files => upload_files(files),
shadow: "var(--accent_color) 0 0 10px 2px inset",
}}
>
{#if $nav.base.abuse_type !== undefined}
2024-05-17 16:32:53 +02:00
<div class="highlight_red">
This directory has received an abuse report. It cannot be
shared.<br/>
Type of abuse: {$nav.base.abuse_type}<br/>
Report time: {formatDate($nav.base.abuse_report_time, true, true, true)}
2024-05-17 16:32:53 +02:00
</div>
{/if}
2025-10-13 16:05:50 +02:00
{@render children?.()}
2024-02-16 14:50:34 +01:00
2022-02-22 19:53:48 +01:00
{#if directory_view === "list"}
<ListView nav={nav} file_event={file_event} show_hidden={show_hidden} large_icons={large_icons}/>
2022-02-22 19:53:48 +01:00
{:else if directory_view === "gallery"}
<GalleryView nav={nav} file_event={file_event} show_hidden={show_hidden} large_icons={large_icons}/>
2025-01-28 12:00:04 +01:00
{:else if directory_view === "compact"}
<CompactView nav={nav} file_event={file_event} show_hidden={show_hidden} large_icons={large_icons}/>
2022-02-22 19:53:48 +01:00
{/if}
2020-11-17 23:39:27 +01:00
</div>
2025-10-13 16:05:50 +02:00
<FileMenu bind:this={file_menu} nav={nav} edit_window={edit_window} />
2025-10-09 15:48:23 +02:00
2020-11-17 23:39:27 +01:00
<style>
.container {
padding: 0;
/* Keeps the margins of the views inside the container. As a normal block
they would collapse through it and push it out of the file preview, which
gives that a scrollbar */
display: flow-root;
min-height: 100%; /* Used for drop target */
2020-11-17 23:39:27 +01:00
}
.toolbar {
2023-11-16 16:33:35 +01:00
display: flex;
flex: 1 1 auto;
2020-11-17 23:39:27 +01:00
flex-direction: row;
2025-01-28 12:00:04 +01:00
flex-wrap: wrap;
justify-content: space-between;
2023-11-16 16:33:35 +01:00
align-items: center;
border-top: 1px solid var(--separator);
2020-11-17 23:39:27 +01:00
}
2025-01-28 12:00:04 +01:00
.toolbar > * {
flex: 0 0 content;
display: flex;
flex-direction: row;
}
2023-11-16 16:33:35 +01:00
.toolbar_spacer {
flex: 1 1 auto;
text-align: center;
}
.toolbar_edit {
2026-10-01 02:06:00 +02:00
background-color: var(--green_tint);
2025-01-28 12:00:04 +01:00
}
/* Large icon mode only supported on wide screens. Hide the button on small screen */
@media (max-width: 500px) {
.button_large_icons {
display: none;
}
}
2020-11-17 23:39:27 +01:00
</style>