This commit is contained in:
Reudy 2026-09-26 19:13:02 +02:00
commit 0ad405c26e
78 changed files with 9127 additions and 0 deletions

View file

@ -0,0 +1,231 @@
<script lang="ts">
import FilePlusIcon from '@lucide/svelte/icons/file-plus';
import FolderPlusIcon from '@lucide/svelte/icons/folder-plus';
import FileInputIcon from '@lucide/svelte/icons/file-input';
import PencilIcon from '@lucide/svelte/icons/pencil';
import TrashIcon from '@lucide/svelte/icons/trash-2';
import DownloadIcon from '@lucide/svelte/icons/download';
import { vault, baseName, dirName, displayName, joinPath, sanitizeName, EXT } from '$lib/storage/vault.svelte';
import { workspace } from '$lib/state/workspace.svelte';
import { menu, type MenuItem } from '$lib/state/menu.svelte';
import { downloadCanvas } from '$lib/components/download';
import FileTreeNode, { type Node } from './FileTreeNode.svelte';
const OPEN_KEY = 'papure:openFolders';
let open = $state<Set<string>>(loadOpen());
let renaming = $state<string | null>(null);
let importInput: HTMLInputElement;
let importDir = '';
function loadOpen() {
try {
return new Set<string>(JSON.parse(localStorage.getItem(OPEN_KEY) ?? '[]'));
} catch {
return new Set<string>();
}
}
function toggleFolder(path: string) {
const next = new Set(open);
if (next.has(path)) next.delete(path);
else next.add(path);
open = next;
try {
localStorage.setItem(OPEN_KEY, JSON.stringify([...next]));
} catch {
/* ignore */
}
}
function ensureOpen(dir: string) {
if (!dir) return;
const parts = dir.split('/');
const next = new Set(open);
for (let i = 1; i <= parts.length; i++) next.add(parts.slice(0, i).join('/'));
open = next;
}
const root = $derived.by(() => {
const nodes = new Map<string, Node>();
const top: Node = { path: '', name: '', kind: 'folder', children: [] };
const folderNode = (path: string): Node => {
if (!path) return top;
let n = nodes.get(path);
if (!n) {
n = { path, name: baseName(path), kind: 'folder', children: [] };
nodes.set(path, n);
folderNode(dirName(path)).children.push(n);
}
return n;
};
for (const f of vault.folders) folderNode(f);
for (const f of vault.canvases) {
folderNode(dirName(f.path)).children.push({ path: f.path, name: displayName(f.path), kind: 'canvas', children: [] });
}
const sort = (n: Node) => {
n.children.sort((a, b) => (a.kind === b.kind ? a.name.localeCompare(b.name, undefined, { numeric: true }) : a.kind === 'folder' ? -1 : 1));
n.children.forEach(sort);
};
sort(top);
return top;
});
async function newCanvas(dir = '') {
const path = await vault.createCanvas(dir);
ensureOpen(dir);
await workspace.open(path);
renaming = path;
}
async function newFolder(dir = '') {
const path = await vault.createFolder(dir);
ensureOpen(dir);
renaming = path;
}
function importHere(dir = '') {
importDir = dir;
importInput.click();
}
async function onImport(e: Event) {
const input = e.currentTarget as HTMLInputElement;
const files = [...(input.files ?? [])];
input.value = '';
for (const file of files) {
try {
const path = await vault.importPdfAsCanvas(importDir, file);
ensureOpen(importDir);
await workspace.open(path);
} catch (err) {
console.error(err);
workspace.toast(`Could not import ${file.name}`, 'error');
}
}
}
async function commitRename(path: string, kind: Node['kind'], name: string) {
if (renaming !== path) return; // Enter already committed; this is the blur
renaming = null;
const clean = sanitizeName(name);
if (!clean) return;
const target = joinPath(dirName(path), kind === 'canvas' ? clean + EXT : clean);
if (target === path) return;
if (vault.exists(target)) return workspace.toast(`"${clean}" already exists`, 'error');
await move(path, target);
}
async function move(from: string, to: string) {
await workspace.flush();
await vault.rename(from, to);
await workspace.renamed(from, to);
}
async function onDrop(target: string, source: string) {
if (!source || source === target || target.startsWith(source + '/') || dirName(source) === target) return;
const to = joinPath(target, baseName(source));
if (vault.exists(to)) return workspace.toast(`"${baseName(source)}" already exists there`, 'error');
ensureOpen(target);
await move(source, to);
}
async function remove(n: Node) {
const what = n.kind === 'folder' ? `folder "${n.name}" and everything in it` : `"${n.name}"`;
if (!confirm(`Delete ${what}?`)) return;
const openPath = workspace.doc?.path;
if (openPath && (openPath === n.path || openPath.startsWith(n.path + '/'))) workspace.close();
await vault.remove(n.path);
}
function contextMenu(e: MouseEvent, n: Node | null) {
e.preventDefault();
const dir = !n ? '' : n.kind === 'folder' ? n.path : dirName(n.path);
const items: MenuItem[] = [
{ label: 'New canvas', icon: FilePlusIcon, action: () => newCanvas(dir) },
{ label: 'New folder', icon: FolderPlusIcon, action: () => newFolder(dir) },
{ label: 'Import PDF…', icon: FileInputIcon, action: () => importHere(dir) }
];
if (n) {
items.push('separator', { label: 'Rename', icon: PencilIcon, action: () => (renaming = n.path) });
if (n.kind === 'canvas') items.push({ label: 'Download PDF', icon: DownloadIcon, action: () => downloadCanvas(n.path) });
items.push('separator', { label: 'Delete', icon: TrashIcon, danger: true, action: () => remove(n) });
}
menu.show(e.clientX, e.clientY, items);
}
</script>
<div class="head">
<span class="title">Files</span>
<div class="actions">
<button class="icon-btn" title="New canvas" onclick={() => newCanvas()}><FilePlusIcon size={16} strokeWidth={1.75} /></button>
<button class="icon-btn" title="New folder" onclick={() => newFolder()}><FolderPlusIcon size={16} strokeWidth={1.75} /></button>
<button class="icon-btn" title="Import PDF as canvas" onclick={() => importHere()}><FileInputIcon size={16} strokeWidth={1.75} /></button>
</div>
</div>
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div
class="tree"
oncontextmenu={(e) => contextMenu(e, null)}
ondragover={(e) => e.preventDefault()}
ondrop={(e) => {
e.preventDefault();
onDrop('', e.dataTransfer?.getData('text/x-papure-path') ?? '');
}}
>
{#each root.children as node (node.path)}
<FileTreeNode
{node}
depth={0}
{open}
{renaming}
activePath={workspace.doc?.path ?? null}
onToggle={toggleFolder}
onOpen={(p) => workspace.open(p)}
onContext={contextMenu}
onRename={commitRename}
onCancelRename={() => (renaming = null)}
{onDrop}
/>
{:else}
<p class="empty">No canvases yet.<br />Right-click or use the buttons above.</p>
{/each}
</div>
<input bind:this={importInput} type="file" accept="application/pdf,.pdf" multiple hidden onchange={onImport} />
<style>
.head {
display: flex;
align-items: center;
justify-content: space-between;
padding: 8px 8px 6px 14px;
}
.title {
font-size: 11px;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.06em;
color: var(--faint);
}
.actions {
display: flex;
gap: 2px;
}
.actions .icon-btn {
width: 26px;
height: 26px;
}
.tree {
flex: 1;
overflow: auto;
padding: 2px 6px 16px;
}
.empty {
color: var(--faint);
text-align: center;
margin-top: 32px;
font-size: 12px;
line-height: 1.6;
}
</style>