This commit is contained in:
Timothy Jaeryang Baek committed 2026-11-07 20:43:49 +09:00
1 parent d4e8493cc5
commit 6ac83ce14e
4 files changed
+737 -24

No files matched your search

+37 -5
View File
@@ -86,14 +86,17 @@
directory: currentPath,
selectedFile,
expanded: [...expandedDirs],
preview: filePreviewRef?.captureView(),
preview: (commentPreviewRef ?? filePreviewRef)?.captureView(),
dirty: dirty || saving,
scrollTop: viewerRoot?.scrollTop ?? 0
};
const restoreView = async () => {
if (disposed || viewRestored || !initialView) return;
const view = initialView;
if (view.preview && selectedFile === view.selectedFile) {
if (view.preview?.kind === 'xlsx' && selectedFile === view.selectedFile) {
await tick();
await commentPreviewRef?.restoreView(view.preview);
} else if (view.preview && selectedFile === view.selectedFile) {
diskChanged = view.preview.dirty && fileContent !== view.preview.baseText;
await tick();
await filePreviewRef?.restoreView(
@@ -211,14 +214,14 @@
if (!confirmed) return false;
if (sending) {
try {
await filePreviewRef.save();
await (commentPreviewRef ?? filePreviewRef).save();
} catch (error) {
toast.error(String(error));
return false;
}
if (dirty) return false;
} else {
filePreviewRef?.discard();
await (commentPreviewRef ?? filePreviewRef)?.discard();
dirty = false;
}
return true;
@@ -644,6 +647,7 @@
let fileDocxData: ArrayBuffer | null = null;
let fileLoading = false;
let filePreviewRef: FilePreview;
let commentPreviewRef: CommentFilePreview;
let commentGeneration = 0;
let fileSearchTarget: FileSearchTarget | null = null;
let documentTargetPage: number | null = null;
@@ -2312,6 +2316,25 @@
</button>
</Tooltip>
{#if getFileExt(selectedFile) === 'xlsx' && selectedTerminal}
<Tooltip content={$i18n.t('Save')}>
<button
type="button"
class="shrink-0 flex h-5 w-5 items-center justify-center rounded transition-colors duration-100 text-gray-500 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 disabled:opacity-30 disabled:hover:bg-transparent"
on:click={() =>
commentPreviewRef?.save().catch((error) => toast.error(String(error)))}
disabled={saving || !dirty || !selectedFileWritable}
aria-label={$i18n.t('Save')}
>
{#if saving}
<Spinner className="size-3.5" />
{:else}
<Icon name="check" size={11} strokeWidth={1.7} />
{/if}
</button>
</Tooltip>
{/if}
{#if isTextFile}
{#if isHtml && showRaw}
<Tooltip content={$i18n.t('Save')}>
@@ -2488,7 +2511,8 @@
>
<button
class="underline"
on:click={() => filePreviewRef.save().catch((e) => toast.error(String(e)))}
on:click={() =>
(commentPreviewRef ?? filePreviewRef).save().catch((e) => toast.error(String(e)))}
>{$i18n.t('Save local changes')}</button
>
</div>{/if}
@@ -2524,6 +2548,14 @@
{#if supportsComments(selectedFile) && selectedTerminal}
{#key `${selectedTerminal.url}|${selectedTerminal.key}|${chatId}|${selectedFile}|${commentGeneration}`}
<CommentFilePreview
bind:this={commentPreviewRef}
bind:dirty
bind:saving
readOnly={!selectedFileWritable}
onSaved={(preview) => {
diskChanged = false;
onViewChange?.({ ...captureView(), preview, dirty: preview?.dirty ?? false });
}}
path={selectedFile}
baseUrl={selectedTerminal.url}
apiKey={selectedTerminal.key}
@@ -1,5 +1,5 @@
<script lang="ts">
import { getContext, onMount, onDestroy } from 'svelte';
import { getContext, onMount, onDestroy, tick } from 'svelte';
import type { WorkBook } from 'xlsx';
export let onReady = () => {};
export let currentSlide = 0;
@@ -13,7 +13,7 @@
};
$: (loading, previewError, reportReady());
import { downloadFileBlob, downloadFilePreview } from '$lib/apis/terminal';
import { downloadFileBlob, downloadFilePreview, uploadToTerminal } from '$lib/apis/terminal';
import { readOfficeComments } from '$lib/utils/comments/office';
import {
commentThreads,
@@ -21,7 +21,8 @@
type CommentResult,
type CommentState
} from '$lib/utils/comments/types';
import FilePreview from './FilePreview.svelte';
import ExcelPreview from '$lib/components/common/ExcelPreview.svelte';
import { ExcelWorkbookEditor } from '$lib/utils/excelToTable';
import Icon from './Icon.svelte';
import DocxPreview from '$lib/components/common/DocxPreview.svelte';
import PdfPagesPreview from '$lib/components/common/PdfPagesPreview.svelte';
@@ -33,6 +34,78 @@
export let apiKey: string;
export let chatId: string | null = null;
export let targetPage: number | null = null;
export let readOnly = true;
export let dirty = false;
export let saving = false;
export let onSaved: (preview: any) => void = () => {};
let editor: ExcelWorkbookEditor | null = null;
let excelPreview: ExcelPreview;
let loadingTask: Promise<void>;
let editingError = '';
export const captureView = () => {
return editor
? { ...editor.capture(), dirty, sheet, draft: excelPreview?.captureEdit() }
: null;
};
export async function restoreView(view: any) {
await loadingTask;
if (!alive || view?.kind !== 'xlsx') return;
const { readExcelWorkbook } = await import('$lib/utils/excelToTable');
workbook = await readExcelWorkbook(view.original);
editor = await ExcelWorkbookEditor.open(workbook, view.original);
editor.restore(view);
dirty = editor.dirty;
await setSheet(workbook.Sheets[view.sheet] ? view.sheet : workbook.SheetNames[0]);
await tick();
await excelPreview?.restoreEdit(view.draft);
}
export async function save() {
if (excelPreview && !excelPreview.flushEdit())
throw new Error('Finish or cancel the cell edit before saving.');
if (!editor || !editor.dirty) return;
if (readOnly || saving) throw new Error('File is not writable right now.');
saving = true;
try {
const bytes = await editor.bytes();
const current = await downloadFileBlob(baseUrl, apiKey, path, chatId ?? undefined);
if (!current)
throw new Error('Could not check the current file. Your changes are still in the editor.');
if (!editor.matches(await current.blob.arrayBuffer()))
throw new Error(
'The workbook changed on disk. Your edits are preserved. Reload the file before saving.'
);
// shortcut: the terminal upload API has no conditional write; it cannot prevent a concurrent write after this check.
const result = await uploadToTerminal(
baseUrl,
apiKey,
path.slice(0, path.lastIndexOf('/') + 1) || '/',
new File([bytes], path.split('/').pop()!, {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
}),
chatId ?? undefined
);
if (!result)
throw new Error('Failed to save workbook. Your changes are still in the editor.');
editor.markSaved(bytes);
original = bytes;
dirty = editor.dirty;
onSaved(captureView());
} finally {
saving = false;
}
}
export async function discard() {
excelPreview?.cancelEdit();
dirty = false;
if (!original || !editor) return;
const { readExcelWorkbook } = await import('$lib/utils/excelToTable');
const fresh = await readExcelWorkbook(original);
if (!alive) return;
workbook = fresh;
editor = await ExcelWorkbookEditor.open(fresh, original);
await setSheet(sheet);
}
let comments: FileComment[] = [];
let warnings: string[] = [];
let commentState: CommentState = 'loading';
@@ -209,6 +282,13 @@
const { readExcelWorkbook } = await import('$lib/utils/excelToTable');
if (!alive) return;
workbook = await readExcelWorkbook(bytes);
if (ext === 'xlsx' && !readOnly) {
try {
editor = await ExcelWorkbookEditor.open(workbook, bytes);
} catch (error) {
editingError = String(error).replace(/^Error: /, '');
}
}
await setSheet(workbook.Sheets[initialSheet] ? initialSheet : workbook.SheetNames[0]);
loading = false;
}
@@ -241,7 +321,7 @@
}
}
onMount(() => {
void load();
loadingTask = load();
});
onDestroy(() => {
alive = false;
@@ -313,12 +393,19 @@
{:else if ext === 'pptx' && slides.length}
<PptxPreview bind:currentSlide {slides} {targetPage} className="h-full" {comments} />
{:else if table !== null}
<FilePreview
selectedFile={path}
fileOfficeHtml={table}
excelSheetNames={workbook?.SheetNames ?? []}
selectedExcelSheet={sheet}
{#if editingError}<p role="status" class="px-2 py-1 text-xs text-gray-500">
{editingError}
</p>{/if}
<ExcelPreview
bind:this={excelPreview}
html={table}
sheetNames={workbook?.SheetNames ?? []}
selectedSheet={sheet}
onSheetChange={setSheet}
editor={readOnly ? null : editor}
{saving}
onDirtyChange={(value) => (dirty = value)}
onSave={save}
/>
{:else if previewError}<p class="p-4 text-sm text-gray-500 dark:text-gray-400">
{$i18n.t('Preview failed')}