mirror of
https://github.com/JoshiCodes/knock.git
synced 2026-12-04 15:39:42 +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:
@@ -122,6 +122,7 @@ class Workspace {
|
||||
for (const t of savedTabs) {
|
||||
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;
|
||||
this.persistOpen();
|
||||
}
|
||||
@@ -138,6 +139,48 @@ class Workspace {
|
||||
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) {
|
||||
const i = this.collections.findIndex((x) => x.id === c.id);
|
||||
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.persistOpen();
|
||||
this.persistDrafts();
|
||||
}
|
||||
|
||||
moveTab(from: number, to: number) {
|
||||
@@ -344,6 +388,7 @@ class Workspace {
|
||||
tab.preview = false;
|
||||
tab.diskChanged = false;
|
||||
this.persistOpen();
|
||||
this.persistDrafts();
|
||||
return true;
|
||||
} catch (e) {
|
||||
toast.error('Could not save', { description: errorText(e) });
|
||||
|
||||
Reference in New Issue
Block a user