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

@@ -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')}