Improve workspace navigation, search, and tab actions
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
import { confirm } from "@tauri-apps/plugin-dialog";
|
||||
import { openUrl } from "@tauri-apps/plugin-opener";
|
||||
import { Documents } from "$lib/workspace/documents";
|
||||
import { tabsToClose, type TabCloseScope } from "$lib/workspace/tabActions";
|
||||
import type { ActionMenuItem } from "$lib/ui/actionMenu";
|
||||
import { requestText } from "$lib/workspace/dialogs.svelte";
|
||||
import TextDialog from "./TextDialog.svelte";
|
||||
import QuickOpen from "./QuickOpen.svelte";
|
||||
@@ -32,6 +34,7 @@
|
||||
workspaceRead,
|
||||
workspaceReadDataUrl,
|
||||
workspaceRename,
|
||||
workspaceReveal,
|
||||
workspaceWrite,
|
||||
workspaceWriteBytes,
|
||||
} from "$lib/workspace/api";
|
||||
@@ -68,6 +71,8 @@
|
||||
let saving = $state(false);
|
||||
let switching = false;
|
||||
let documents: Documents | null = null;
|
||||
let editorPane: EditorPane | undefined = $state();
|
||||
let closingTabs = false;
|
||||
const hasDirty = $derived(tabs.some(tab => tab.contents !== tab.saved));
|
||||
|
||||
function createDocuments() {
|
||||
@@ -194,10 +199,58 @@
|
||||
}
|
||||
|
||||
async function closeTab(relPath: string) {
|
||||
const tab = tabs.find(item => item.relPath === relPath);
|
||||
if (!tab) return;
|
||||
if (tab.contents !== tab.saved && !await confirm(`Discard unsaved edits in ${tab.name}?`, { title: "Close document", kind: "warning" })) return;
|
||||
documents?.close(relPath);
|
||||
await closeTabs(relPath, "one");
|
||||
}
|
||||
|
||||
async function closeTabs(path: string, scope: TabCloseScope) {
|
||||
const current = documents;
|
||||
if (!current || closingTabs || switching) return;
|
||||
closingTabs = true;
|
||||
try {
|
||||
const targets = tabsToClose(current.buffers, path, scope);
|
||||
for (const target of targets) {
|
||||
if (documents !== current || switching) break;
|
||||
const tab = current.buffers.find(item => item.id === target.id);
|
||||
if (!tab || (scope !== "one" && tab.pinned)) continue;
|
||||
const contents = tab.contents;
|
||||
if (contents !== tab.saved && !await confirm(`Discard unsaved edits in ${tab.name}?`, { title: "Close document", kind: "warning" })) break;
|
||||
if (documents !== current || switching || tab.contents !== contents) break;
|
||||
if (scope !== "one" && tab.pinned) continue;
|
||||
current.close(tab.relPath);
|
||||
}
|
||||
} catch (err) { error = String(err); }
|
||||
finally { closingTabs = false; }
|
||||
}
|
||||
|
||||
async function copyTabPath(path: string, system: boolean) {
|
||||
try {
|
||||
const text = system ? await workspaceAbsPath(path) : path;
|
||||
await navigator.clipboard.writeText(text);
|
||||
status = system ? "Copied system path" : "Copied workspace path";
|
||||
} catch (err) { error = String(err); }
|
||||
}
|
||||
|
||||
function tabFileActions(tab: OpenBuffer): ActionMenuItem[] {
|
||||
if (tab.kind === "empty") return [];
|
||||
const items: ActionMenuItem[] = [];
|
||||
if (tab.kind === "text" && isMarkdownPath(tab.relPath)) {
|
||||
items.push(
|
||||
{ label: reading ? "Editing view" : "Reading view", icon: "book", run: () => editorPane?.toggleReadingView() },
|
||||
{ label: isIde ? "Writing mode" : "Source mode", icon: "code", run: toggleChrome },
|
||||
);
|
||||
}
|
||||
items.push({ separator: true },
|
||||
{ label: "Rename…", icon: "rename", run: () => { void renameActive(); } },
|
||||
);
|
||||
if (tab.kind === "text") items.push({ label: "Save a copy…", icon: "copy", run: () => { void saveCopy(); } });
|
||||
items.push({ separator: true },
|
||||
{ label: "Copy path", icon: "copy", children: [
|
||||
{ label: "Workspace path", icon: "copy", run: () => { void copyTabPath(tab.relPath, false); } },
|
||||
{ label: "System path", icon: "copy", run: () => { void copyTabPath(tab.relPath, true); } },
|
||||
] },
|
||||
{ label: "Show in system explorer", icon: "folder", run: () => { void workspaceReveal(tab.relPath).catch(err => { error = String(err); }); } },
|
||||
);
|
||||
return items;
|
||||
}
|
||||
|
||||
async function openFile(entry: TreeEntry, line?: number, target: "current" | "new" = "new") {
|
||||
@@ -427,12 +480,13 @@
|
||||
if (!activePath || buffer?.kind === "empty") {
|
||||
return;
|
||||
}
|
||||
const current = activePath.split("/").pop() ?? activePath;
|
||||
const typed = await requestText("Rename to", current);
|
||||
if (typed == null) {
|
||||
const current = documents;
|
||||
const from = activePath;
|
||||
const typed = await requestText("Rename to", from.split("/").pop() ?? from);
|
||||
if (typed == null || documents !== current || switching) {
|
||||
return;
|
||||
}
|
||||
const plan = planRename(activePath, typed);
|
||||
const plan = planRename(from, typed);
|
||||
if (plan.action === "error") {
|
||||
error = plan.message;
|
||||
return;
|
||||
@@ -441,8 +495,9 @@
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await workspaceRename(activePath, plan.to);
|
||||
applyPathRename(activePath, plan.to);
|
||||
await workspaceRename(from, plan.to);
|
||||
current?.rename(from, plan.to);
|
||||
if (documents !== current) return;
|
||||
refreshToken += 1;
|
||||
status = `Renamed to ${plan.to}`;
|
||||
error = null;
|
||||
@@ -569,7 +624,9 @@
|
||||
</div>
|
||||
<div class="header-center" data-tauri-drag-region>
|
||||
{#if tabs.length > 0}
|
||||
<TabBar {tabs} {activePath} onSelect={selectTab} onClose={closeTab} />
|
||||
<TabBar {tabs} {activePath} onSelect={selectTab} onClose={closeTab}
|
||||
onCloseTabs={(path, scope) => void closeTabs(path, scope)} onPin={(path) => documents?.togglePin(path)}
|
||||
fileActions={tabFileActions} onMove={(path, index) => documents?.move(path, index)} />
|
||||
{/if}
|
||||
<IconButton icon="plus" label="New tab (Ctrl T)" onclick={newTab} />
|
||||
{#if desktop}<div class="window-drag-space" data-tauri-drag-region></div>{/if}
|
||||
@@ -650,6 +707,7 @@
|
||||
<details class="disk-review"><summary>View disk version</summary><pre>{buffer.diskContents}</pre></details>
|
||||
{/if}
|
||||
<EditorPane
|
||||
bind:this={editorPane}
|
||||
{tabs}
|
||||
hasWorkspace={!!workspace}
|
||||
onOpenWorkspace={() => void chooseWorkspace()}
|
||||
@@ -811,7 +869,7 @@
|
||||
}
|
||||
|
||||
.header-center :global(.tab.active) {
|
||||
background: var(--rs-bg-active);
|
||||
background: var(--rs-item-selection-bg);
|
||||
}
|
||||
|
||||
.ws {
|
||||
|
||||
Reference in New Issue
Block a user