From 3d535db304bfc6fa09e655f737de8a36c0482868 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 1 Mar 2026 02:29:37 -0600 Subject: [PATCH] refac --- src/lib/components/chat/FileNav.svelte | 12 +++ .../chat/FileNav/FilePreview.svelte | 96 ++++++++++++++++++- src/lib/components/icons/PenAlt.svelte | 18 ++++ 3 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 src/lib/components/icons/PenAlt.svelte diff --git a/src/lib/components/chat/FileNav.svelte b/src/lib/components/chat/FileNav.svelte index 795da2f4da..144beccf4d 100644 --- a/src/lib/components/chat/FileNav.svelte +++ b/src/lib/components/chat/FileNav.svelte @@ -441,6 +441,18 @@ {filePdfData} {fileContent} onDownload={() => downloadFile(selectedFile)} + onSave={async (content) => { + const terminal = selectedTerminal; + if (!terminal || !selectedFile) return; + const fileName = selectedFile.split('/').pop() ?? 'file'; + const dir = selectedFile.substring(0, selectedFile.lastIndexOf('/') + 1) || '/'; + const file = new File([content], fileName, { type: 'text/plain' }); + const result = await uploadToTerminal(terminal.url, terminal.key, dir, file); + toast[result ? 'success' : 'error']( + $i18n.t(result ? 'File saved' : 'Failed to save file') + ); + if (result) fileContent = content; + }} /> {:else} {#if uploading} diff --git a/src/lib/components/chat/FileNav/FilePreview.svelte b/src/lib/components/chat/FileNav/FilePreview.svelte index 8f7af32b59..5489aa3bc2 100644 --- a/src/lib/components/chat/FileNav/FilePreview.svelte +++ b/src/lib/components/chat/FileNav/FilePreview.svelte @@ -7,6 +7,7 @@ import PDFViewer from '../../common/PDFViewer.svelte'; import Tooltip from '../../common/Tooltip.svelte'; import Reset from '../../icons/Reset.svelte'; + import PenAlt from '../../icons/PenAlt.svelte'; const i18n = getContext('i18n'); @@ -17,6 +18,41 @@ export let fileContent: string | null = null; export let onDownload: () => void = () => {}; + export let onSave: ((content: string) => Promise) | null = null; + + let editing = false; + let editContent = ''; + let saving = false; + + // Reset edit state when switching files + $: selectedFile, resetEdit(); + + const resetEdit = () => { + editing = false; + editContent = ''; + saving = false; + }; + + const startEdit = () => { + editContent = fileContent ?? ''; + editing = true; + showRaw = true; + }; + + const saveEdit = async () => { + if (!onSave) return; + saving = true; + await onSave(editContent); + saving = false; + editing = false; + }; + + const cancelEdit = () => { + editing = false; + editContent = ''; + }; + + $: isTextFile = fileContent !== null && fileImageUrl === null && filePdfData === null; const MD_EXTS = new Set(['md', 'markdown', 'mdx']); const CSV_EXTS = new Set(['csv', 'tsv']); @@ -127,7 +163,10 @@ {/if} + {#if isTextFile && onSave} + {#if editing} + + + + + + + {:else} + + + + {/if} + {/if}