mirror of
https://github.com/JoshiCodes/knock.git
synced 2026-12-04 15:19:41 +01:00
Tab menu, copy as cURL, drafts survive restarts, copy response body
- Right click a tab: close, close others, close to the right, close all, duplicate, copy as cURL, show in sidebar - Ctrl+Shift+C copies the request as cURL with variables and auth filled in; secrets stay masked - Unsaved edits and scratch requests come back after a restart or crash - Copy button for the response body
This commit is contained in:
@@ -22,17 +22,24 @@
|
|||||||
import { openDocs } from '$lib/help';
|
import { openDocs } from '$lib/help';
|
||||||
import UpdateNotice from '$lib/views/UpdateNotice.svelte';
|
import UpdateNotice from '$lib/views/UpdateNotice.svelte';
|
||||||
import { updates } from '$lib/state/updates.svelte';
|
import { updates } from '$lib/state/updates.svelte';
|
||||||
|
import { copyAsCurl } from '$lib/copycurl';
|
||||||
|
|
||||||
let ready = $state(false);
|
let ready = $state(false);
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
ws.restore().finally(() => (ready = true));
|
ws.restore().finally(() => (ready = true));
|
||||||
updates.start();
|
updates.start();
|
||||||
|
// Keep unsaved drafts safe across restarts and crashes.
|
||||||
|
const draftTimer = setInterval(() => ready && ws.persistDrafts(), 4000);
|
||||||
|
const saveDrafts = () => ws.persistDrafts();
|
||||||
|
window.addEventListener('beforeunload', saveDrafts);
|
||||||
const un = listen<{ collection: string; paths: string[] }>('collection-changed', (e) => {
|
const un = listen<{ collection: string; paths: string[] }>('collection-changed', (e) => {
|
||||||
ws.onDiskChange(e.payload.collection, e.payload.paths);
|
ws.onDiskChange(e.payload.collection, e.payload.paths);
|
||||||
});
|
});
|
||||||
const unLink = onDeepLink();
|
const unLink = onDeepLink();
|
||||||
return () => {
|
return () => {
|
||||||
|
clearInterval(draftTimer);
|
||||||
|
window.removeEventListener('beforeunload', saveDrafts);
|
||||||
un.then((f) => f());
|
un.then((f) => f());
|
||||||
unLink.then((f) => f?.());
|
unLink.then((f) => f?.());
|
||||||
};
|
};
|
||||||
@@ -93,6 +100,9 @@
|
|||||||
} else if (k === 'p' || (k === 'f' && e.shiftKey)) {
|
} else if (k === 'p' || (k === 'f' && e.shiftKey)) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
ui.sidebarFilterRequest += 1;
|
ui.sidebarFilterRequest += 1;
|
||||||
|
} else if (k === 'c' && e.shiftKey) {
|
||||||
|
e.preventDefault();
|
||||||
|
copyAsCurl();
|
||||||
} else if (k === 'j' && e.shiftKey) {
|
} else if (k === 'j' && e.shiftKey) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
ui.toggleJsonModeRequest += 1;
|
ui.toggleJsonModeRequest += 1;
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import { invoke } from '@tauri-apps/api/core';
|
||||||
|
import { toast } from '@joshicodes/cock';
|
||||||
|
import { errorText } from './api';
|
||||||
|
import { plain } from './plain.svelte';
|
||||||
|
import { ws, type Tab } from './state/workspace.svelte';
|
||||||
|
import type { Request } from './types';
|
||||||
|
|
||||||
|
/** Copies the request as a cURL command with variables and auth filled in; secrets stay masked. */
|
||||||
|
export async function copyAsCurl(tab: Tab | undefined = ws.active) {
|
||||||
|
if (!tab?.draft) return;
|
||||||
|
try {
|
||||||
|
const code = await invoke<string>('code_snippet', {
|
||||||
|
args: { col: tab.col, request_id: tab.id, request: plain(tab.draft) as Request, env: ws.env(tab.col) },
|
||||||
|
lang: 'curl',
|
||||||
|
resolve: true
|
||||||
|
});
|
||||||
|
await navigator.clipboard.writeText(code);
|
||||||
|
toast.success('Copied as cURL', { description: 'Secret values are masked.' });
|
||||||
|
} catch (e) {
|
||||||
|
toast.error('Could not build the cURL command', { description: errorText(e) });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,8 @@ class Ui {
|
|||||||
sendRequest = $state(0);
|
sendRequest = $state(0);
|
||||||
/** Inline rename target in the sidebar: `${col}:${id}`. */
|
/** Inline rename target in the sidebar: `${col}:${id}`. */
|
||||||
renaming = $state<string | null>(null);
|
renaming = $state<string | null>(null);
|
||||||
|
/** Sidebar should expand to and select `${col}:${id}`. */
|
||||||
|
revealRequest = $state<string | null>(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ui = new Ui();
|
export const ui = new Ui();
|
||||||
|
|||||||
@@ -122,6 +122,7 @@ class Workspace {
|
|||||||
for (const t of savedTabs) {
|
for (const t of savedTabs) {
|
||||||
if (t.kind === 'request' && t.col && t.id) await this.openRequest(t.col, t.id, false);
|
if (t.kind === 'request' && t.col && t.id) await this.openRequest(t.col, t.id, false);
|
||||||
}
|
}
|
||||||
|
this.restoreDrafts();
|
||||||
if (active && this.tabs.some((t) => t.key === active)) this.activeKey = active;
|
if (active && this.tabs.some((t) => t.key === active)) this.activeKey = active;
|
||||||
this.persistOpen();
|
this.persistOpen();
|
||||||
}
|
}
|
||||||
@@ -138,6 +139,48 @@ class Workspace {
|
|||||||
store('knock.activeTab', this.activeKey);
|
store('knock.activeTab', this.activeKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unsaved edits and scratch requests survive a restart (or a crash):
|
||||||
|
* their drafts are kept in local storage until saved or closed.
|
||||||
|
*/
|
||||||
|
persistDrafts() {
|
||||||
|
const drafts = this.tabs
|
||||||
|
.filter((t) => t.kind === 'request' && t.draft && (this.isDirty(t) || !t.id) && !t.key.startsWith('history:'))
|
||||||
|
.map((t) => ({ key: t.key, col: t.col, id: t.id, draft: $state.snapshot(t.draft), saved: t.saved ?? '' }));
|
||||||
|
store('knock.drafts', drafts);
|
||||||
|
}
|
||||||
|
|
||||||
|
private restoreDrafts() {
|
||||||
|
const drafts: { key: string; col: string | null; id: string | null; draft: Request; saved: string }[] = load('knock.drafts', []);
|
||||||
|
for (const d of drafts) {
|
||||||
|
const open = this.tabs.find((t) => t.key === d.key);
|
||||||
|
if (open) {
|
||||||
|
open.draft = normalizeRequest(d.draft);
|
||||||
|
open.preview = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (d.id) continue; // file tab that was closed; its draft is gone with it
|
||||||
|
this.tabs.push({ key: d.key, kind: 'request', col: d.col, id: null, title: d.draft.name, preview: false, draft: normalizeRequest(d.draft), saved: d.saved });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
closeOthers(key: string, onlyRight = false) {
|
||||||
|
const i = this.tabs.findIndex((t) => t.key === key);
|
||||||
|
const victims = this.tabs.filter((t, j) => t.key !== key && (!onlyRight || j > i));
|
||||||
|
const dirty = victims.filter((t) => this.isDirty(t));
|
||||||
|
if (dirty.length && !confirm(`${dirty.length} tab(s) have unsaved changes. Close anyway?`)) return;
|
||||||
|
for (const t of victims) this.closeTab(t.key, true);
|
||||||
|
if (this.tabs.some((t) => t.key === key)) this.activate(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
duplicateTab(key: string) {
|
||||||
|
const t = this.tabs.find((x) => x.key === key);
|
||||||
|
if (!t?.draft) return;
|
||||||
|
const draft = normalizeRequest(JSON.parse(JSON.stringify($state.snapshot(t.draft))));
|
||||||
|
draft.name = `${draft.name} copy`;
|
||||||
|
this.insertTab({ key: `scratch:${crypto.randomUUID()}`, kind: 'request', col: t.col, id: null, title: draft.name, preview: false, draft, saved: '' });
|
||||||
|
}
|
||||||
|
|
||||||
upsert(c: CollectionView) {
|
upsert(c: CollectionView) {
|
||||||
const i = this.collections.findIndex((x) => x.id === c.id);
|
const i = this.collections.findIndex((x) => x.id === c.id);
|
||||||
if (i >= 0) this.collections[i] = c;
|
if (i >= 0) this.collections[i] = c;
|
||||||
@@ -299,6 +342,7 @@ class Workspace {
|
|||||||
this.activeKey = this.tabs[Math.min(i, this.tabs.length - 1)]?.key ?? null;
|
this.activeKey = this.tabs[Math.min(i, this.tabs.length - 1)]?.key ?? null;
|
||||||
}
|
}
|
||||||
this.persistOpen();
|
this.persistOpen();
|
||||||
|
this.persistDrafts();
|
||||||
}
|
}
|
||||||
|
|
||||||
moveTab(from: number, to: number) {
|
moveTab(from: number, to: number) {
|
||||||
@@ -344,6 +388,7 @@ class Workspace {
|
|||||||
tab.preview = false;
|
tab.preview = false;
|
||||||
tab.diskChanged = false;
|
tab.diskChanged = false;
|
||||||
this.persistOpen();
|
this.persistOpen();
|
||||||
|
this.persistDrafts();
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
toast.error('Could not save', { description: errorText(e) });
|
toast.error('Could not save', { description: errorText(e) });
|
||||||
|
|||||||
@@ -30,6 +30,28 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// "Show in sidebar" from a tab: open the folders above it and select it.
|
||||||
|
$effect(() => {
|
||||||
|
const target = ui.revealRequest;
|
||||||
|
if (!target) return;
|
||||||
|
const [col, ...rest] = target.split(':');
|
||||||
|
const id = rest.join(':');
|
||||||
|
const next = new Set(expanded);
|
||||||
|
next.delete(`closed:${col}`);
|
||||||
|
const segs = id.split('/');
|
||||||
|
for (let i = 1; i < segs.length; i++) next.add(`${col}:${segs.slice(0, i).join('/')}`);
|
||||||
|
expanded = next;
|
||||||
|
persistExpanded();
|
||||||
|
panel = 'tree';
|
||||||
|
filter = '';
|
||||||
|
selected = target;
|
||||||
|
ui.revealRequest = null;
|
||||||
|
queueMicrotask(() => {
|
||||||
|
const i = rows.findIndex((r) => r.key === target);
|
||||||
|
if (i >= 0) ensureVisible(i);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
function persistExpanded() {
|
function persistExpanded() {
|
||||||
localStorage.setItem('knock.expanded', JSON.stringify([...expanded]));
|
localStorage.setItem('knock.expanded', JSON.stringify([...expanded]));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,11 @@
|
|||||||
import Icon from '$lib/ui/Icon.svelte';
|
import Icon from '$lib/ui/Icon.svelte';
|
||||||
import { ws, type Tab } from '$lib/state/workspace.svelte';
|
import { ws, type Tab } from '$lib/state/workspace.svelte';
|
||||||
import { methodColor } from '$lib/request';
|
import { methodColor } from '$lib/request';
|
||||||
|
import { ContextMenu, MenuItem, MenuSeparator } from '@joshicodes/cock';
|
||||||
|
import { copyAsCurl } from '$lib/copycurl';
|
||||||
|
import { ui } from '$lib/state/ui.svelte';
|
||||||
|
|
||||||
|
let menuTab = $state<Tab | null>(null);
|
||||||
|
|
||||||
let dragFrom = $state<number | null>(null);
|
let dragFrom = $state<number | null>(null);
|
||||||
let dragOver = $state<number | null>(null);
|
let dragOver = $state<number | null>(null);
|
||||||
@@ -22,7 +27,8 @@
|
|||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="flex h-9 shrink-0 items-stretch border-b border-line bg-sidebar">
|
<ContextMenu label="Tab actions" class="flex h-9 shrink-0 items-stretch border-b border-line bg-sidebar">
|
||||||
|
{#snippet children()}
|
||||||
<div class="k-scroll flex min-w-0 flex-1 items-stretch overflow-y-hidden [scrollbar-width:none]" role="tablist">
|
<div class="k-scroll flex min-w-0 flex-1 items-stretch overflow-y-hidden [scrollbar-width:none]" role="tablist">
|
||||||
{#each ws.tabs as t, i (t.key)}
|
{#each ws.tabs as t, i (t.key)}
|
||||||
{@const active = t.key === ws.activeKey}
|
{@const active = t.key === ws.activeKey}
|
||||||
@@ -35,6 +41,7 @@
|
|||||||
class="group relative flex max-w-[220px] shrink-0 items-center gap-1.5 border-r border-line pl-3 pr-1.5 {active ? 'bg-panel text-strong' : 'text-fg-muted hover:bg-hover/40'} {dragOver === i ? 'shadow-[inset_2px_0_0_var(--knock-cyan)]' : ''}"
|
class="group relative flex max-w-[220px] shrink-0 items-center gap-1.5 border-r border-line pl-3 pr-1.5 {active ? 'bg-panel text-strong' : 'text-fg-muted hover:bg-hover/40'} {dragOver === i ? 'shadow-[inset_2px_0_0_var(--knock-cyan)]' : ''}"
|
||||||
onclick={() => ws.activate(t.key)}
|
onclick={() => ws.activate(t.key)}
|
||||||
ondblclick={() => ws.pin(t.key)}
|
ondblclick={() => ws.pin(t.key)}
|
||||||
|
oncontextmenu={() => (menuTab = t)}
|
||||||
onauxclick={(e) => {
|
onauxclick={(e) => {
|
||||||
if (e.button === 1) ws.closeTab(t.key);
|
if (e.button === 1) ws.closeTab(t.key);
|
||||||
}}
|
}}
|
||||||
@@ -84,4 +91,22 @@
|
|||||||
<button class="grid w-9 shrink-0 place-items-center text-fg-muted hover:bg-hover/40 hover:text-fg" title="New request (Ctrl+T)" onclick={() => ws.newScratch()}>
|
<button class="grid w-9 shrink-0 place-items-center text-fg-muted hover:bg-hover/40 hover:text-fg" title="New request (Ctrl+T)" onclick={() => ws.newScratch()}>
|
||||||
<Icon name="plus" />
|
<Icon name="plus" />
|
||||||
</button>
|
</button>
|
||||||
</div>
|
{/snippet}
|
||||||
|
{#snippet menu()}
|
||||||
|
{#if menuTab}
|
||||||
|
{@const t = menuTab}
|
||||||
|
<MenuItem onselect={() => ws.closeTab(t.key)} hint="Ctrl+W">Close</MenuItem>
|
||||||
|
<MenuItem onselect={() => ws.closeOthers(t.key)}>Close others</MenuItem>
|
||||||
|
<MenuItem onselect={() => ws.closeOthers(t.key, true)}>Close to the right</MenuItem>
|
||||||
|
<MenuItem onselect={() => ws.closeOthers('')}>Close all</MenuItem>
|
||||||
|
{#if t.kind === 'request'}
|
||||||
|
<MenuSeparator />
|
||||||
|
<MenuItem onselect={() => ws.duplicateTab(t.key)}>Duplicate</MenuItem>
|
||||||
|
<MenuItem onselect={() => copyAsCurl(t)} hint="Ctrl+Shift+C">Copy as cURL</MenuItem>
|
||||||
|
{#if t.id && t.col}
|
||||||
|
<MenuItem onselect={() => (ui.revealRequest = `${t.col}:${t.id}`)}>Show in sidebar</MenuItem>
|
||||||
|
{/if}
|
||||||
|
{/if}
|
||||||
|
{/if}
|
||||||
|
{/snippet}
|
||||||
|
</ContextMenu>
|
||||||
|
|||||||
@@ -36,6 +36,13 @@
|
|||||||
let assertsTotal = $derived(out?.asserts.length ?? 0);
|
let assertsTotal = $derived(out?.asserts.length ?? 0);
|
||||||
let authHint = $derived(resp && (resp.status === 401 || resp.status === 403) && out?.auth);
|
let authHint = $derived(resp && (resp.status === 401 || resp.status === 403) && out?.auth);
|
||||||
|
|
||||||
|
async function copyBody() {
|
||||||
|
await loadBody(tab);
|
||||||
|
const text = tab.result?.bodyText ?? '';
|
||||||
|
await navigator.clipboard.writeText(text);
|
||||||
|
toast.success('Body copied');
|
||||||
|
}
|
||||||
|
|
||||||
async function saveBody() {
|
async function saveBody() {
|
||||||
const id = tab.result?.result.body_id;
|
const id = tab.result?.result.body_id;
|
||||||
if (!id) return;
|
if (!id) return;
|
||||||
@@ -85,6 +92,7 @@
|
|||||||
{/if}
|
{/if}
|
||||||
<span class="flex-1"></span>
|
<span class="flex-1"></span>
|
||||||
{#if resp}
|
{#if resp}
|
||||||
|
<button class="grid size-7 place-items-center rounded text-fg-muted hover:bg-hover hover:text-fg" title="Copy body" onclick={copyBody}><Icon name="copy" /></button>
|
||||||
<button class="grid size-7 place-items-center rounded text-fg-muted hover:bg-hover hover:text-fg" title="Save body to file" onclick={saveBody}><Icon name="download" /></button>
|
<button class="grid size-7 place-items-center rounded text-fg-muted hover:bg-hover hover:text-fg" title="Save body to file" onclick={saveBody}><Icon name="download" /></button>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user