Update editor, workspace, Git services, appearance, and project guides
This commit is contained in:
+185
-31
@@ -1,5 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { untrack } from "svelte";
|
||||
import { onMount, tick, untrack } from "svelte";
|
||||
import { createNote, noteTemplates, type NoteTemplate } from "$lib/workspace/templates";
|
||||
import { confirm } from "@tauri-apps/plugin-dialog";
|
||||
import { requestText } from "$lib/workspace/dialogs.svelte";
|
||||
import type { TreeEntry } from "$lib/workspace/types";
|
||||
@@ -11,8 +12,11 @@
|
||||
workspaceList,
|
||||
workspaceRename,
|
||||
workspaceReveal,
|
||||
workspaceOpenDefault,
|
||||
workspaceOpenTerminal,
|
||||
} from "$lib/workspace/api";
|
||||
import { loadMarks, saveMarks, type Marks } from "$lib/workspace/marks";
|
||||
import { moveMarks, replayMoveMarks } from "$lib/workspace/treeMoveHistory";
|
||||
import { actionEntries, selectionRoots, setSelectedMarks, runTreeActions } from "$lib/workspace/treeActions";
|
||||
import {
|
||||
dropHint,
|
||||
@@ -24,7 +28,7 @@
|
||||
type DropPlace,
|
||||
type DropTarget,
|
||||
} from "$lib/workspace/treeDrop";
|
||||
import { planRename, planDocumentTitleRename } from "$lib/workspace/renamePath";
|
||||
import { planRename } from "$lib/workspace/renamePath";
|
||||
import { selectTreePath, treeSelectionEdges, visibleTreePaths, type SelectionModifiers, type TreeSelection } from "$lib/workspace/treeSelection";
|
||||
import ContextMenu from "./ContextMenu.svelte";
|
||||
import type { MenuItem } from "./menuTypes";
|
||||
@@ -43,6 +47,8 @@
|
||||
onEntryRenamed?: (from: string, to: string) => void;
|
||||
onCreated?: (entry: TreeEntry) => void;
|
||||
keysActive?: boolean;
|
||||
showInternal?: boolean;
|
||||
onSaveTemplate?: (entry: TreeEntry) => void;
|
||||
};
|
||||
|
||||
let {
|
||||
@@ -55,6 +61,8 @@
|
||||
onEntryRenamed,
|
||||
onCreated,
|
||||
keysActive = true,
|
||||
showInternal = false,
|
||||
onSaveTemplate,
|
||||
}: Props = $props();
|
||||
|
||||
let expanded = $state<Set<string>>(new Set([""]));
|
||||
@@ -73,10 +81,21 @@
|
||||
const selectionEdges = $derived(treeSelectionEdges(visiblePaths, selection.paths));
|
||||
let renameRel = $state<string | null>(null);
|
||||
let renameDraft = $state("");
|
||||
let renameDocument = false;
|
||||
let skipClick = false;
|
||||
let imageLanding = $state(false);
|
||||
let cancelDrag: (() => void) | undefined;
|
||||
let treeElement: HTMLDivElement;
|
||||
let clipboard = $state<{ root: string; paths: string[]; cut: boolean } | null>(null);
|
||||
type MoveBatch = { moves: { from: string; to: string }[]; before: Marks; after: Marks };
|
||||
let undoMoves = $state<MoveBatch[]>([]);
|
||||
let redoMoves = $state<MoveBatch[]>([]);
|
||||
let moving = $state(false);
|
||||
let templates = $state<NoteTemplate[]>([]);
|
||||
onMount(() => {
|
||||
const read = () => { void noteTemplates().then(value => templates = value); };
|
||||
read(); window.addEventListener("runestone-templates-changed", read);
|
||||
return () => window.removeEventListener("runestone-templates-changed", read);
|
||||
});
|
||||
$effect(() => () => cancelDrag?.());
|
||||
|
||||
const pins = $derived(new Set(marks.pins));
|
||||
@@ -117,7 +136,7 @@
|
||||
async function load(rel: string) {
|
||||
loading = true;
|
||||
try {
|
||||
const entries = await workspaceList(rel);
|
||||
const entries = await workspaceList(rel, showInternal);
|
||||
childrenMap = { ...childrenMap, [rel]: sortEntries(rel, entries) };
|
||||
void inspectPluginDocuments(entries);
|
||||
error = null;
|
||||
@@ -130,6 +149,7 @@
|
||||
|
||||
$effect(() => {
|
||||
void workspaceRoot;
|
||||
undoMoves = []; redoMoves = []; clipboard = null;
|
||||
expanded = new Set([""]);
|
||||
selection = { paths: new Set(), anchor: null };
|
||||
childrenMap = {};
|
||||
@@ -142,6 +162,7 @@
|
||||
|
||||
$effect(() => {
|
||||
void refreshToken;
|
||||
void showInternal;
|
||||
void $pluginDocumentTypes;
|
||||
const keys = untrack(() => Object.keys(childrenMap));
|
||||
for (const key of keys) {
|
||||
@@ -352,8 +373,7 @@
|
||||
function startRename(entry: TreeEntry) {
|
||||
selectOnly(entry.rel_path);
|
||||
renameRel = entry.rel_path;
|
||||
renameDocument = entry.kind === "file" && (!!$pluginDocuments[entry.rel_path] || /\.(md|markdown|mdown)$/i.test(entry.name));
|
||||
renameDraft = renameDocument ? entry.name.replace(/\.[^.]+$/, "") : entry.name;
|
||||
renameDraft = entry.name;
|
||||
}
|
||||
|
||||
function cancelRename() {
|
||||
@@ -366,7 +386,7 @@
|
||||
if (!from) {
|
||||
return;
|
||||
}
|
||||
const plan = renameDocument ? planDocumentTitleRename(from, renameDraft) : planRename(from, renameDraft);
|
||||
const plan = planRename(from, renameDraft);
|
||||
cancelRename();
|
||||
if (plan.action === "noop") {
|
||||
return;
|
||||
@@ -390,6 +410,26 @@
|
||||
if (!keysActive) {
|
||||
return;
|
||||
}
|
||||
if (event.target instanceof Element && treeElement?.contains(event.target)
|
||||
&& !event.target.closest('input, textarea, [contenteditable="true"]')
|
||||
&& (event.ctrlKey || event.metaKey) && !event.altKey) {
|
||||
const key = event.key.toLowerCase();
|
||||
if (key === "z" || key === "y") {
|
||||
event.preventDefault(); event.stopPropagation();
|
||||
void replayMoves(key === "y" || event.shiftKey);
|
||||
return;
|
||||
}
|
||||
const entries = selectionRoots([...selection.paths].map(findEntry).filter((entry): entry is TreeEntry => !!entry));
|
||||
if ((key === "c" || key === "x") && entries.length) {
|
||||
event.preventDefault(); clipboard = { root: workspaceRoot, paths: entries.map(entry => entry.rel_path), cut: key === "x" }; return;
|
||||
}
|
||||
if (key === "v") {
|
||||
event.preventDefault();
|
||||
const selected = entries[0];
|
||||
void pasteEntries(selected?.kind === "folder" ? selected.rel_path : selected ? parentRel(selected.rel_path) : "");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (event.key !== "F2" || event.ctrlKey || event.metaKey || event.altKey) {
|
||||
return;
|
||||
}
|
||||
@@ -409,22 +449,23 @@
|
||||
startRename(entry);
|
||||
}
|
||||
|
||||
async function createIn(parent: string, kind: "folder" | "note") {
|
||||
async function createIn(parent: string, kind: "folder" | "note" | "file") {
|
||||
let name: string;
|
||||
if (kind === "note") {
|
||||
name = "untitled.md";
|
||||
} else {
|
||||
const typed = await requestText("Folder name", "folder");
|
||||
const typed = await requestText(kind === "folder" ? "Folder name" : "File name, including any extension", kind === "folder" ? "folder" : "Untitled");
|
||||
if (!typed) {
|
||||
return;
|
||||
}
|
||||
if (typed.includes("/") || typed.includes("\\")) {
|
||||
error = "Name cannot contain a path separator";
|
||||
const plan = planRename("", typed);
|
||||
if (plan.action === "error") {
|
||||
error = plan.message;
|
||||
return;
|
||||
}
|
||||
name = typed;
|
||||
name = typed.trim();
|
||||
}
|
||||
const finalName = uniqueName(namesIn(parent), name);
|
||||
const finalName = kind === "file" ? name : uniqueName(namesIn(parent), name);
|
||||
const rel = joinRel(parent, finalName);
|
||||
try {
|
||||
await workspaceCreate(rel, kind === "folder" ? "folder" : "file");
|
||||
@@ -438,7 +479,7 @@
|
||||
}
|
||||
await load(parent);
|
||||
onStatus?.(`Created ${rel}`);
|
||||
if (kind === "note") {
|
||||
if (kind !== "folder") {
|
||||
onCreated?.({ name: finalName, rel_path: rel, kind: "file" });
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -452,16 +493,22 @@
|
||||
}
|
||||
|
||||
async function handleDrop(target: TreeEntry, entries: TreeEntry[], place: DropPlace) {
|
||||
if (moving) return;
|
||||
const operationRoot = workspaceRoot;
|
||||
const before = structuredClone($state.snapshot(marks));
|
||||
const moves: MoveBatch["moves"] = [];
|
||||
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)) };
|
||||
childrenMap = { ...childrenMap, [destination]: sortEntries(destination, await workspaceList(destination, showInternal)) };
|
||||
} catch (err) {
|
||||
error = err instanceof Error ? err.message : String(err);
|
||||
return;
|
||||
}
|
||||
const paths = new Map(entries.map(entry => [entry.rel_path, entry.rel_path]));
|
||||
moving = true;
|
||||
const result = await runTreeActions(place === "after" ? [...entries].reverse() : entries, async entry => {
|
||||
if (workspaceRoot !== operationRoot) throw new Error("The workspace changed. File moves stopped.");
|
||||
const plan = planDrop(entry.rel_path, target, place, namesIn);
|
||||
if (plan.action === "noop") return;
|
||||
if (plan.action === "reorder") {
|
||||
@@ -471,14 +518,17 @@
|
||||
await load(plan.parent);
|
||||
return;
|
||||
}
|
||||
await workspaceRename(plan.from, plan.to);
|
||||
await workspaceRename(plan.from, plan.to, operationRoot);
|
||||
if (workspaceRoot !== operationRoot) throw new Error("The workspace changed. File moves stopped.");
|
||||
moves.push({ from: plan.from, to: plan.to });
|
||||
paths.set(plan.from, plan.to);
|
||||
forgetFolder(plan.from);
|
||||
onEntryRenamed?.(plan.from, plan.to);
|
||||
const order = { ...marks.order };
|
||||
const next = moveMarks(marks, plan.from, plan.to);
|
||||
const order = { ...next.order };
|
||||
order[plan.fromParent] = plan.orderFrom;
|
||||
order[plan.toParent] = plan.orderTo;
|
||||
await persistMarks({ ...marks, order });
|
||||
await persistMarks({ ...next, order });
|
||||
await load(plan.fromParent);
|
||||
if (plan.toParent !== plan.fromParent) {
|
||||
await load(plan.toParent);
|
||||
@@ -488,6 +538,82 @@
|
||||
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`);
|
||||
if (moves.length || JSON.stringify(before.order) !== JSON.stringify(marks.order)) {
|
||||
undoMoves = [...undoMoves, { moves, before, after: structuredClone($state.snapshot(marks)) }];
|
||||
redoMoves = [];
|
||||
}
|
||||
moving = false;
|
||||
await focusTreePath(paths.values().next().value);
|
||||
}
|
||||
|
||||
async function focusTreePath(path?: string) {
|
||||
await tick();
|
||||
const target = [...treeElement.querySelectorAll<HTMLElement>("[data-tree-rel]")].find(node => node.dataset.treeRel === path);
|
||||
(target ?? treeElement).focus({ preventScroll: true });
|
||||
}
|
||||
|
||||
async function replayMoves(redo = false) {
|
||||
if (moving) return;
|
||||
const stack = redo ? redoMoves : undoMoves;
|
||||
const batch = stack.at(-1);
|
||||
if (!batch) return;
|
||||
const operationRoot = workspaceRoot;
|
||||
moving = true;
|
||||
const applied: { from: string; to: string }[] = [];
|
||||
try {
|
||||
for (const move of redo ? batch.moves : [...batch.moves].reverse()) {
|
||||
if (workspaceRoot !== operationRoot) throw new Error("The workspace changed. File moves stopped.");
|
||||
const from = redo ? move.from : move.to, to = redo ? move.to : move.from;
|
||||
await workspaceRename(from, to, operationRoot);
|
||||
if (workspaceRoot !== operationRoot) throw new Error("The workspace changed. File moves stopped.");
|
||||
applied.push({ from, to });
|
||||
onEntryRenamed?.(from, to);
|
||||
}
|
||||
if (workspaceRoot !== operationRoot) throw new Error("The workspace changed. File moves stopped.");
|
||||
await persistMarks(replayMoveMarks(marks, batch.before, batch.after, batch.moves, redo));
|
||||
if (redo) { redoMoves = redoMoves.slice(0, -1); undoMoves = [...undoMoves, batch]; }
|
||||
else { undoMoves = undoMoves.slice(0, -1); redoMoves = [...redoMoves, batch]; }
|
||||
childrenMap = {}; expanded = new Set([""]);
|
||||
await load("");
|
||||
selection = { paths: new Set(applied.map(move => move.to)), anchor: applied.at(-1)?.to ?? null };
|
||||
await focusTreePath(applied.at(-1)?.to);
|
||||
onStatus?.(redo ? "File move restored." : "File move undone.");
|
||||
} catch (reason) {
|
||||
const rollbackErrors: string[] = [];
|
||||
for (const move of applied.reverse()) {
|
||||
if (workspaceRoot !== operationRoot) { rollbackErrors.push("The original workspace is unavailable."); break; }
|
||||
try { await workspaceRename(move.to, move.from, operationRoot); onEntryRenamed?.(move.to, move.from); }
|
||||
catch (failure) { rollbackErrors.push(String(failure)); }
|
||||
}
|
||||
if (rollbackErrors.length) { undoMoves = []; redoMoves = []; }
|
||||
error = [String(reason), ...rollbackErrors].join("\n");
|
||||
} finally { moving = false; }
|
||||
}
|
||||
|
||||
async function pasteEntries(parent: string) {
|
||||
if (!clipboard || clipboard.root !== workspaceRoot || moving) return;
|
||||
const source = clipboard;
|
||||
const operationRoot = workspaceRoot;
|
||||
const before = structuredClone($state.snapshot(marks));
|
||||
const moves: MoveBatch["moves"] = [];
|
||||
moving = true;
|
||||
const result = await runTreeActions(source.paths.map(rel_path => ({ rel_path, name: rel_path.split("/").pop()!, kind: "file" as const })), async entry => {
|
||||
if (workspaceRoot !== operationRoot) throw new Error("The workspace changed. Paste stopped.");
|
||||
if (source.cut) {
|
||||
const to = joinRel(parent, entry.name);
|
||||
if (to === entry.rel_path) return;
|
||||
await workspaceRename(entry.rel_path, to, operationRoot);
|
||||
if (workspaceRoot !== operationRoot) throw new Error("The workspace changed. Paste stopped.");
|
||||
moves.push({ from: entry.rel_path, to });
|
||||
onEntryRenamed?.(entry.rel_path, to);
|
||||
await persistMarks(moveMarks(marks, entry.rel_path, to));
|
||||
} else await workspaceCopy(entry.rel_path, parent, operationRoot);
|
||||
});
|
||||
if (moves.length) { undoMoves = [...undoMoves, { moves, before, after: structuredClone($state.snapshot(marks)) }]; redoMoves = []; }
|
||||
if (source.cut) clipboard = result.failures.length ? { ...source, paths: source.paths.filter(path => !moves.some(move => move.from === path)) } : null;
|
||||
for (const folder of new Set([parent, ...source.paths.map(parentRel)])) await load(folder);
|
||||
error = result.failures.length ? result.failures.join("\n") : null;
|
||||
moving = false;
|
||||
}
|
||||
|
||||
async function copyEntries(entries: TreeEntry[]) {
|
||||
@@ -519,23 +645,25 @@
|
||||
}
|
||||
|
||||
function paneMenuItems(parent: string): MenuItem[] {
|
||||
const fromTemplate = async (template?: NoteTemplate) => {
|
||||
try {
|
||||
const path = await createNote(parent, template);
|
||||
await load(parent);
|
||||
onCreated?.({ name: path.split("/").pop()!, rel_path: path, kind: "file" });
|
||||
} catch (reason) { error = String(reason); }
|
||||
};
|
||||
return [
|
||||
{
|
||||
kind: "action",
|
||||
label: "Create folder",
|
||||
icon: "folder-plus",
|
||||
run: () => {
|
||||
void createIn(parent, "folder");
|
||||
},
|
||||
},
|
||||
{
|
||||
kind: "action",
|
||||
label: "Create new note",
|
||||
label: "New file…",
|
||||
icon: "file-plus",
|
||||
run: () => {
|
||||
void createIn(parent, "note");
|
||||
},
|
||||
run: () => { void createIn(parent, "file"); },
|
||||
},
|
||||
...(templates.length ? [{ kind: "group" as const, label: "New note", icon: "file-plus" as const, items: [
|
||||
{ kind: "action" as const, label: "Blank note", icon: "file-plus" as const, run: () => { void fromTemplate(); } },
|
||||
...templates.map(template => ({ kind: "action" as const, label: template.name, icon: "file-plus" as const, run: () => { void fromTemplate(template); } })),
|
||||
] }] : [{ kind: "action" as const, label: "New note", icon: "file-plus" as const, run: () => { void fromTemplate(); } }]),
|
||||
{ kind: "action", label: "New folder", icon: "folder-plus", run: () => { void createIn(parent, "folder"); } },
|
||||
...$pluginDocumentTypes.filter(item => item.type.create).map(({ owner, type }): MenuItem => ({
|
||||
kind: "action", label: type.create!.label, icon: type.icon === "kanban" ? "kanban" : "file-plus",
|
||||
run: () => { void (async () => {
|
||||
@@ -557,11 +685,24 @@
|
||||
];
|
||||
}
|
||||
|
||||
function folderActions(parent: string): MenuItem[] {
|
||||
return [
|
||||
{ kind: "action", label: "Open in terminal", icon: "terminal", run: () => { void workspaceOpenTerminal(parent).catch(reason => error = String(reason)); } },
|
||||
{ kind: "action", label: "Paste", icon: "paste", disabled: !clipboard || clipboard.root !== workspaceRoot || moving, run: () => { void pasteEntries(parent); } },
|
||||
{ kind: "action", label: "Copy path", icon: "copy", run: () => { void workspaceAbsPath(parent).then(path => copyText(path, "path")).catch(reason => error = String(reason)); } },
|
||||
{ kind: "sep" },
|
||||
{ kind: "action", label: "Undo file move", icon: "undo", disabled: !undoMoves.length || moving, run: () => { void replayMoves(); } },
|
||||
{ kind: "action", label: "Redo file move", icon: "redo", disabled: !redoMoves.length || moving, run: () => { void replayMoves(true); } },
|
||||
];
|
||||
}
|
||||
|
||||
function menuItems(entry: TreeEntry | null): MenuItem[] {
|
||||
if (!entry) {
|
||||
return [
|
||||
...paneMenuItems(""),
|
||||
{ kind: "sep" },
|
||||
...folderActions(""),
|
||||
{ kind: "sep" },
|
||||
{
|
||||
kind: "action",
|
||||
label: "Show in Explorer",
|
||||
@@ -584,6 +725,17 @@
|
||||
: [];
|
||||
return [
|
||||
...createItems,
|
||||
...(!multiple && entry.kind === "file" && /\.md$/i.test(entry.name) && onSaveTemplate ? [
|
||||
{ kind: "action" as const, label: "Save as template", icon: "copy" as const, run: () => onSaveTemplate?.(entry) },
|
||||
] : []),
|
||||
...(!multiple && entry.kind === "file" ? [
|
||||
{ kind: "action" as const, label: "Open in new tab", icon: "file-plus" as const, run: () => onOpenFile(entry, true) },
|
||||
{ kind: "action" as const, label: "Open in default app", icon: "external" as const, run: () => { void workspaceOpenDefault(entry.rel_path).catch(reason => error = String(reason)); } },
|
||||
] : []),
|
||||
...folderActions(entry.kind === "folder" ? entry.rel_path : parentRel(entry.rel_path)),
|
||||
{ kind: "sep" },
|
||||
{ kind: "action", label: "Cut", icon: "cut", run: () => { clipboard = { root: workspaceRoot, paths: selectionRoots(entries).map(item => item.rel_path), cut: true }; } },
|
||||
{ kind: "action", label: "Copy", icon: "copy", run: () => { clipboard = { root: workspaceRoot, paths: selectionRoots(entries).map(item => item.rel_path), cut: false }; } },
|
||||
{
|
||||
kind: "action",
|
||||
label: multiple ? `Make copies (${entries.length})` : "Make a copy",
|
||||
@@ -671,7 +823,7 @@
|
||||
|
||||
<div class="wrap">
|
||||
<div class="ribbon tree-tools">
|
||||
<IconButton icon="file-plus" label="New file" onclick={() => void createIn("", "note")} />
|
||||
<IconButton icon="file-plus" label="New file" onclick={() => void createIn("", "file")} />
|
||||
<IconButton icon="folder-plus" label="New folder" onclick={() => void createIn("", "folder")} />
|
||||
<IconButton
|
||||
icon="sort"
|
||||
@@ -691,7 +843,9 @@
|
||||
/>
|
||||
</div>
|
||||
<div
|
||||
bind:this={treeElement}
|
||||
class="tree"
|
||||
tabindex="-1"
|
||||
role="navigation"
|
||||
aria-label="Workspace files"
|
||||
oncontextmenu={(event) => {
|
||||
|
||||
Reference in New Issue
Block a user