The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { boundedArchive, prepareOffice } from "./officeArchive"; |
| 2 | import { PREVIEW_PAGE_ROWS } from "./largeTextProtocol"; |
| 3 | import type { OfficeWorkerRequest, OfficeWorkerResult } from "./officePreviewProtocol"; |
| 4 | import { PreviewError, previewFailure } from "./previewFailure"; |
| 5 | |
| 6 | let workbook: string[][][] = []; |
| 7 | function reply(result: OfficeWorkerResult) { |
| 8 | self.postMessage(result, { transfer: "bytes" in result ? [result.bytes] : [] }); |
| 9 | } |
| 10 | async function handle(input: OfficeWorkerRequest) { |
| 11 | if (input.type === "sheet-page") { |
| 12 | const rows = workbook[input.sheet]; |
| 13 | if (!rows) throw new PreviewError({ code: "sheet-unavailable" }); |
| 14 | reply({ |
| 15 | type: "sheet-page", |
| 16 | sheet: input.sheet, |
| 17 | index: input.index, |
| 18 | rows: rows.slice(input.index * PREVIEW_PAGE_ROWS, (input.index + 1) * PREVIEW_PAGE_ROWS), |
| 19 | }); |
| 20 | return; |
| 21 | } |
| 22 | if (input.kind === "docx" || input.kind === "xlsx" || input.kind === "pptx") { |
| 23 | reply({ type: input.kind, ...(await prepareOffice(input.bytes, input.kind)) }); |
| 24 | return; |
| 25 | } |
| 26 | const { openDocument, openEpub, openNotebook, openRtf } = |
| 27 | await import("./extendedDocumentPreview"); |
| 28 | if (input.kind === "ipynb" || input.kind === "rtf") { |
| 29 | reply(input.kind === "ipynb" ? openNotebook(input.bytes) : openRtf(input.bytes)); |
| 30 | return; |
| 31 | } |
| 32 | const { archive } = await boundedArchive(input); |
| 33 | const result = |
| 34 | input.kind === "epub" ? await openEpub(archive) : await openDocument(archive, input.kind); |
| 35 | if (result.type === "ods") { |
| 36 | workbook = result.sheets.map((sheet) => sheet.rows); |
| 37 | reply({ |
| 38 | ...result, |
| 39 | sheets: result.sheets.map((sheet) => ({ |
| 40 | ...sheet, |
| 41 | rows: sheet.rows.slice(0, PREVIEW_PAGE_ROWS), |
| 42 | })), |
| 43 | }); |
| 44 | } else reply(result); |
| 45 | } |
| 46 | self.addEventListener("message", (event: MessageEvent<OfficeWorkerRequest>) => { |
| 47 | void handle(event.data).catch((cause: unknown) => |
| 48 | reply({ type: "error", ...previewFailure(cause, "parse-failed") }), |
| 49 | ); |
| 50 | }); |