Update the browser, editor, settings, and installer
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
type DropTarget,
|
||||
} from "$lib/workspace/treeDrop";
|
||||
import { planRename } from "$lib/workspace/renamePath";
|
||||
import { isImagePath } from "$lib/workspace/media";
|
||||
import { selectTreePath, treeSelectionEdges, visibleTreePaths, type SelectionModifiers, type TreeSelection } from "$lib/workspace/treeSelection";
|
||||
import ContextMenu from "./ContextMenu.svelte";
|
||||
import type { MenuItem } from "./menuTypes";
|
||||
@@ -49,6 +50,7 @@
|
||||
keysActive?: boolean;
|
||||
showInternal?: boolean;
|
||||
onSaveTemplate?: (entry: TreeEntry) => void;
|
||||
onManageTemplates?: () => void;
|
||||
};
|
||||
|
||||
let {
|
||||
@@ -63,6 +65,7 @@
|
||||
keysActive = true,
|
||||
showInternal = false,
|
||||
onSaveTemplate,
|
||||
onManageTemplates,
|
||||
}: Props = $props();
|
||||
|
||||
let expanded = $state<Set<string>>(new Set([""]));
|
||||
@@ -86,11 +89,17 @@
|
||||
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 };
|
||||
type MoveBatch = { action?: "rename"; 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[]>([]);
|
||||
$effect(() => {
|
||||
void workspaceRoot; void refreshToken;
|
||||
let cancelled = false;
|
||||
void noteTemplates().then(value => { if (!cancelled) templates = value; });
|
||||
return () => { cancelled = true; };
|
||||
});
|
||||
onMount(() => {
|
||||
const read = () => { void noteTemplates().then(value => templates = value); };
|
||||
read(); window.addEventListener("runestone-templates-changed", read);
|
||||
@@ -383,7 +392,7 @@
|
||||
|
||||
async function commitRename() {
|
||||
const from = renameRel;
|
||||
if (!from) {
|
||||
if (!from || moving) {
|
||||
return;
|
||||
}
|
||||
const plan = planRename(from, renameDraft);
|
||||
@@ -395,15 +404,25 @@
|
||||
error = plan.message;
|
||||
return;
|
||||
}
|
||||
const operationRoot = workspaceRoot;
|
||||
const before = structuredClone($state.snapshot(marks));
|
||||
moving = true;
|
||||
try {
|
||||
await workspaceRename(from, plan.to);
|
||||
await workspaceRename(from, plan.to, operationRoot);
|
||||
if (workspaceRoot !== operationRoot) return;
|
||||
const after = moveMarks(marks, from, plan.to);
|
||||
undoMoves = [...undoMoves, { action: "rename", moves: [{ from, to: plan.to }], before, after: structuredClone(after) }];
|
||||
redoMoves = [];
|
||||
renameTreePaths(from, plan.to);
|
||||
onEntryRenamed?.(from, plan.to);
|
||||
await persistMarks(after);
|
||||
await load(parentRel(from));
|
||||
selectOnly(plan.to);
|
||||
onEntryRenamed?.(from, plan.to);
|
||||
await focusTreePath(plan.to);
|
||||
onStatus?.(`Renamed to ${plan.to}`);
|
||||
} catch (err) {
|
||||
error = err instanceof Error ? err.message : String(err);
|
||||
}
|
||||
} finally { moving = false; }
|
||||
}
|
||||
|
||||
function onWindowKey(event: KeyboardEvent) {
|
||||
@@ -492,6 +511,16 @@
|
||||
expanded = new Set([...expanded].filter(path => path !== rel && !path.startsWith(`${rel}/`)));
|
||||
}
|
||||
|
||||
function renameTreePaths(from: string, to: string) {
|
||||
const rename = (path: string) => path === from || path.startsWith(`${from}/`) ? to + path.slice(from.length) : path;
|
||||
expanded = new Set([...expanded].map(rename));
|
||||
childrenMap = Object.fromEntries(Object.entries(childrenMap).map(([parent, entries]) => [rename(parent), entries.map(entry => {
|
||||
const rel_path = rename(entry.rel_path);
|
||||
return rel_path === entry.rel_path ? entry : { ...entry, rel_path, name: rel_path.split("/").pop()! };
|
||||
})]));
|
||||
selection = { paths: new Set([...selection.paths].map(rename)), anchor: selection.anchor === null ? null : rename(selection.anchor) };
|
||||
}
|
||||
|
||||
async function handleDrop(target: TreeEntry, entries: TreeEntry[], place: DropPlace) {
|
||||
if (moving) return;
|
||||
const operationRoot = workspaceRoot;
|
||||
@@ -573,11 +602,12 @@
|
||||
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("");
|
||||
for (const move of applied) renameTreePaths(move.from, move.to);
|
||||
const folders = new Set([...Object.keys(childrenMap), ...applied.flatMap(move => [parentRel(move.from), parentRel(move.to)])]);
|
||||
for (const folder of folders) await load(folder);
|
||||
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.");
|
||||
onStatus?.(redo ? "File change restored." : "File change undone.");
|
||||
} catch (reason) {
|
||||
const rollbackErrors: string[] = [];
|
||||
for (const move of applied.reverse()) {
|
||||
@@ -659,9 +689,10 @@
|
||||
icon: "file-plus",
|
||||
run: () => { void createIn(parent, "file"); },
|
||||
},
|
||||
...(templates.length ? [{ kind: "group" as const, label: "New note", icon: "file-plus" as const, items: [
|
||||
...(templates.length || onManageTemplates ? [{ kind: "group" as const, label: "New note", icon: "file-plus" as const, run: () => { void fromTemplate(); }, 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); } })),
|
||||
...(onManageTemplates ? [{ kind: "sep" as const }, { kind: "action" as const, label: "Manage templates…", icon: "copy" as const, run: onManageTemplates }] : []),
|
||||
] }] : [{ 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 => ({
|
||||
@@ -689,10 +720,9 @@
|
||||
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); } },
|
||||
{ kind: "action", label: undoMoves.at(-1)?.action === "rename" ? "Undo rename" : "Undo file move", icon: "undo", disabled: !undoMoves.length || moving, run: () => { void replayMoves(); } },
|
||||
{ kind: "action", label: redoMoves.at(-1)?.action === "rename" ? "Redo rename" : "Redo file move", icon: "redo", disabled: !redoMoves.length || moving, run: () => { void replayMoves(true); } },
|
||||
];
|
||||
}
|
||||
|
||||
@@ -896,7 +926,7 @@
|
||||
<div class="drag-ghost" style:left="{dragPoint.x + 12}px" style:top="{dragPoint.y + 12}px">
|
||||
<span>{dragEntries.length > 1 ? `${dragEntries.length} items` : dragRel.split("/").pop()}</span>
|
||||
{#if imageLanding}
|
||||
<span class="hint">Insert image</span>
|
||||
<span class="hint">{isImagePath(dragRel ?? "") ? "Insert image" : "Insert link"}</span>
|
||||
{:else if landing && dropTarget}
|
||||
<span class="hint">{dropHint(landing.name, dropTarget.place)}</span>
|
||||
{/if}
|
||||
|
||||
Reference in New Issue
Block a user