Improve workspace navigation, search, and tab actions

This commit is contained in:
Knotty Wyvern
2026-09-29 07:48:24 -07:00
parent bdc15e952d
commit 43106ed9c7
34 changed files with 1798 additions and 100 deletions
+93 -59
View File
@@ -12,7 +12,8 @@
workspaceRename,
workspaceReveal,
} from "$lib/workspace/api";
import { loadMarks, saveMarks, togglePath, type Marks } from "$lib/workspace/marks";
import { loadMarks, saveMarks, type Marks } from "$lib/workspace/marks";
import { actionEntries, selectionRoots, setSelectedMarks, runTreeActions } from "$lib/workspace/treeActions";
import {
dropHint,
dropPlaceAt,
@@ -63,6 +64,7 @@
let menu = $state<{ x: number; y: number; entry: TreeEntry | null } | null>(null);
let sortMode = $state<"custom" | "name" | "name-desc">("custom");
let dragRel = $state<string | null>(null);
let dragEntries = $state<TreeEntry[]>([]);
let dropTarget = $state<DropTarget | null>(null);
let dragPoint = $state<{ x: number; y: number } | null>(null);
let selection = $state<TreeSelection>({ paths: new Set(), anchor: null });
@@ -212,7 +214,7 @@
return null;
}
const rel = hit.dataset.treeRel;
if (!rel || rel === dragRel) {
if (!rel || dragEntries.some(entry => rel === entry.rel_path || rel.startsWith(`${entry.rel_path}/`))) {
return null;
}
const kind = hit.dataset.treeKind === "folder" ? "folder" : "file";
@@ -228,6 +230,7 @@
const startX = event.clientX;
const startY = event.clientY;
let started = false;
let singleFile = false;
cancelDrag?.();
const onMove = (moveEvent: PointerEvent) => {
if (!started) {
@@ -235,11 +238,13 @@
return;
}
started = true;
selectOnly(entry.rel_path);
if (!selection.paths.has(entry.rel_path)) selectOnly(entry.rel_path);
dragEntries = selectionRoots(actionEntries(entry, selection.paths, visiblePaths, findEntry));
singleFile = selection.paths.size === 1 && entry.kind === "file";
dragRel = entry.rel_path;
}
dragPoint = { x: moveEvent.clientX, y: moveEvent.clientY };
imageLanding = entry.kind === "file" && highlightImageDrop(entry.rel_path, dragPoint);
imageLanding = singleFile && highlightImageDrop(entry.rel_path, dragPoint);
dropTarget = readDropAt(moveEvent.clientX, moveEvent.clientY);
};
const clean = () => {
@@ -248,7 +253,7 @@
window.removeEventListener("pointercancel", clean);
window.removeEventListener("keydown", onKey, true);
highlightImageDrop();
dragRel = null; dropTarget = null; dragPoint = null; imageLanding = false;
dragRel = null; dragEntries = []; dropTarget = null; dragPoint = null; imageLanding = false;
cancelDrag = undefined;
};
const onKey = (key: KeyboardEvent) => { if (key.key === "Escape") { key.preventDefault(); clean(); } };
@@ -257,12 +262,12 @@
skipClick = true;
setTimeout(() => { skipClick = false; }, 0);
}
if (started && entry.kind === "file" && dropWorkspaceImage(entry.rel_path, { x: up.clientX, y: up.clientY })) { clean(); return; }
if (started && singleFile && dropWorkspaceImage(entry.rel_path, { x: up.clientX, y: up.clientY })) { clean(); return; }
if (started && dragRel && dropTarget) {
const target = findEntry(dropTarget.rel);
if (target) {
skipClick = true;
void handleDrop(target, dragRel, dropTarget.place);
void handleDrop(target, [...dragEntries], dropTarget.place);
}
}
clean();
@@ -436,21 +441,35 @@
}
}
async function handleDrop(target: TreeEntry, draggedRel: string, place: DropPlace) {
const plan = planDrop(draggedRel, target, place, namesIn);
if (plan.action === "noop") {
function forgetFolder(rel: string) {
childrenMap = Object.fromEntries(Object.entries(childrenMap).filter(([path]) => path !== rel && !path.startsWith(`${rel}/`)));
expanded = new Set([...expanded].filter(path => path !== rel && !path.startsWith(`${rel}/`)));
}
async function handleDrop(target: TreeEntry, entries: TreeEntry[], place: DropPlace) {
const destination = place === "inside" ? target.rel_path : parentRel(target.rel_path);
// Load collapsed folders before checking for name conflicts.
try {
childrenMap = { ...childrenMap, [destination]: sortEntries(destination, await workspaceList(destination)) };
} catch (err) {
error = err instanceof Error ? err.message : String(err);
return;
}
try {
const paths = new Map(entries.map(entry => [entry.rel_path, entry.rel_path]));
const result = await runTreeActions(place === "after" ? [...entries].reverse() : entries, async entry => {
const plan = planDrop(entry.rel_path, target, place, namesIn);
if (plan.action === "noop") return;
if (plan.action === "reorder") {
const order = { ...marks.order };
order[plan.parent] = plan.names;
await persistMarks({ ...marks, order });
await load(plan.parent);
onStatus?.(`Moved ${draggedRel}`);
return;
}
await workspaceRename(plan.from, plan.to);
paths.set(plan.from, plan.to);
forgetFolder(plan.from);
onEntryRenamed?.(plan.from, plan.to);
const order = { ...marks.order };
order[plan.fromParent] = plan.orderFrom;
order[plan.toParent] = plan.orderTo;
@@ -459,9 +478,36 @@
if (plan.toParent !== plan.fromParent) {
await load(plan.toParent);
}
selectOnly(plan.to);
onEntryRenamed?.(plan.from, plan.to);
onStatus?.(`Moved to ${plan.to}`);
});
if (place === "inside") expanded = new Set([...expanded, destination]);
selection = { paths: new Set(paths.values()), anchor: paths.values().next().value ?? null };
error = result.failures.length ? result.failures.join("\n") : null;
onStatus?.(`Moved ${result.completed.length} of ${entries.length} items`);
}
async function copyEntries(entries: TreeEntry[]) {
const roots = selectionRoots(entries);
const result = await runTreeActions(roots, entry => workspaceCopy(entry.rel_path));
for (const parent of new Set(roots.map(entry => parentRel(entry.rel_path)))) await load(parent);
error = result.failures.length ? result.failures.join("\n") : null;
onStatus?.(`Copied ${result.completed.length} of ${roots.length} items`);
}
async function deleteEntries(entries: TreeEntry[]) {
const roots = selectionRoots(entries);
const message = entries.length === 1
? `Permanently delete ${entries[0].name}${entries[0].kind === "folder" ? " and everything inside it" : ""}?`
: `Permanently delete ${entries.length} selected items${entries.some(entry => entry.kind === "folder") ? " and all contents of selected folders" : ""}?\n\n${roots.map(entry => entry.rel_path).join("\n")}`;
try {
if (!await confirm(message, { title: "Delete from disk", kind: "warning" })) return;
const result = await runTreeActions(roots, async entry => {
await workspaceDelete(entry.rel_path);
forgetFolder(entry.rel_path);
onEntryGone?.(entry.rel_path);
});
for (const parent of new Set(roots.map(entry => parentRel(entry.rel_path)))) await load(parent);
error = result.failures.length ? result.failures.join("\n") : null;
onStatus?.(`Deleted ${result.completed.length} of ${roots.length} items`);
} catch (err) {
error = err instanceof Error ? err.message : String(err);
}
@@ -472,6 +518,7 @@
{
kind: "action",
label: "Create folder",
icon: "folder-plus",
run: () => {
void createIn(parent, "folder");
},
@@ -479,6 +526,7 @@
{
kind: "action",
label: "Create new note",
icon: "file-plus",
run: () => {
void createIn(parent, "note");
},
@@ -490,55 +538,51 @@
if (!entry) {
return paneMenuItems("");
}
const pinned = pins.has(entry.rel_path);
const bookmarked = bookmarks.has(entry.rel_path);
const entries = actionEntries(entry, selection.paths, visiblePaths, findEntry);
const multiple = entries.length > 1;
const pinned = entries.every(item => pins.has(item.rel_path));
const bookmarked = entries.every(item => bookmarks.has(item.rel_path));
const createItems =
entry.kind === "folder"
!multiple && entry.kind === "folder"
? [...paneMenuItems(entry.rel_path), { kind: "sep" as const }]
: [];
return [
...createItems,
{
kind: "action",
label: "Make a copy",
run: () => {
void (async () => {
try {
const copy = await workspaceCopy(entry.rel_path);
await load(parentRel(entry.rel_path));
onStatus?.(`Copied to ${copy.rel_path}`);
} catch (err) {
error = err instanceof Error ? err.message : String(err);
}
})();
},
label: multiple ? `Make copies (${entries.length})` : "Make a copy",
icon: "files",
run: () => { void copyEntries(entries); },
},
{
kind: "action",
label: bookmarked ? "Remove bookmark" : "Bookmark",
label: bookmarked ? (multiple ? "Remove bookmarks" : "Remove bookmark") : "Bookmark",
icon: "bookmark",
run: () => {
void persistMarks({
...marks,
bookmarks: togglePath(marks.bookmarks, entry.rel_path),
bookmarks: setSelectedMarks(marks.bookmarks, entries, !bookmarked),
});
},
},
{ kind: "sep" },
{
kind: "action",
label: "Copy workspace path",
label: multiple ? "Copy workspace paths" : "Copy workspace path",
icon: "files",
run: () => {
void copyText(entry.rel_path, "workspace path");
void copyText(entries.map(item => item.rel_path).join("\n"), multiple ? "workspace paths" : "workspace path");
},
},
{
kind: "action",
label: "Copy system path",
label: multiple ? "Copy system paths" : "Copy system path",
icon: "files",
run: () => {
void (async () => {
try {
const abs = await workspaceAbsPath(entry.rel_path);
await copyText(abs, "system path");
const paths = await Promise.all(entries.map(item => workspaceAbsPath(item.rel_path)));
await copyText(paths.join("\n"), multiple ? "system paths" : "system path");
} catch (err) {
error = err instanceof Error ? err.message : String(err);
}
@@ -548,13 +592,11 @@
{
kind: "action",
label: "Show in Explorer",
icon: "folder-open",
run: () => {
void (async () => {
try {
await workspaceReveal(entry.rel_path);
} catch (err) {
error = err instanceof Error ? err.message : String(err);
}
const result = await runTreeActions(entries, item => workspaceReveal(item.rel_path));
error = result.failures.length ? result.failures.join("\n") : null;
})();
},
},
@@ -565,33 +607,24 @@
run: () => {
void persistMarks({
...marks,
pins: togglePath(marks.pins, entry.rel_path),
pins: setSelectedMarks(marks.pins, entries, !pinned),
});
},
},
{
kind: "action",
label: "Rename",
icon: "pen",
disabled: multiple,
run: () => {
startRename(entry);
},
},
{
kind: "action",
label: "Delete",
run: () => {
void (async () => {
try {
if (!await confirm(`Permanently delete ${entry.name}${entry.kind === "folder" ? " and everything inside it" : ""}?`, { title: "Delete from disk", kind: "warning" })) return;
await workspaceDelete(entry.rel_path);
await load(parentRel(entry.rel_path));
onEntryGone?.(entry.rel_path);
onStatus?.(`Deleted ${entry.rel_path}`);
} catch (err) {
error = err instanceof Error ? err.message : String(err);
}
})();
},
label: multiple ? `Delete (${entries.length})` : "Delete",
icon: "trash",
run: () => { void deleteEntries(entries); },
},
];
}
@@ -647,8 +680,8 @@
{bookmarks}
{onRowClick}
onContextMenu={(target, x, y) => {
if (!selection.paths.has(target.rel_path)) selectOnly(target.rel_path);
menu = { x, y, entry: target };
selectOnly(target.rel_path);
}}
{dropTarget}
{dragRel}
@@ -670,7 +703,7 @@
{#if dragRel && dragPoint}
{@const landing = dropTarget ? findEntry(dropTarget.rel) : null}
<div class="drag-ghost" style:left="{dragPoint.x + 12}px" style:top="{dragPoint.y + 12}px">
<span>{dragRel.split("/").pop()}</span>
<span>{dragEntries.length > 1 ? `${dragEntries.length} items` : dragRel.split("/").pop()}</span>
{#if imageLanding}
<span class="hint">Insert image</span>
{:else if landing && dropTarget}
@@ -715,6 +748,7 @@
padding: 4px 8px;
background: var(--rs-bg-elevated);
border: 1px solid var(--rs-accent);
border-radius: var(--rs-control-radius);
color: var(--rs-text-strong);
font-size: 12px;
}