Open History to see the last commit
1// Adapted from opencode (MIT). Copyright (c) 2025 opencode. See LICENSE.opencode.
2import type { SheetInfo } from "@betteroffice/xlsx";
3import type { CellAddr, CellRange, GridMeta, HyperlinkRegion } from "@betteroffice/xlsx/headless";
4import type { WorkbookView } from "../officePreviewProtocol";
5import type { Span } from "./sheet-cells";
6import type { Method } from "./worker-rpc";
7
8// What the Excel preview's window and its workbook worker say to each other. The window keeps the selection, the
9// headers and the scroll area; the engine, its recalculation, the display lists and the cells' painting live in the
10// worker.
11
12/** A visible sheet: its index in the workbook and its tab name. */
13export type SheetTab = { readonly index: number; readonly name: string };
14
15/** What a sheet's grid needs from the workbook before its first frame. */
16export type SheetLayout = {
17 readonly info: SheetInfo;
18 /** The used range; undefined for an empty sheet. */
19 readonly used: CellRange | undefined;
20 readonly merges: readonly CellRange[];
21 readonly hidden: { readonly rows: readonly Span[]; readonly columns: readonly Span[] };
22 /** How far the charts reach, in the engine's scroll coordinates. */
23 readonly chartWidth: number;
24 readonly chartHeight: number;
25};
26
27/** A viewport, in CSS pixels and the engine's scroll coordinates: the frozen panes, then the cells past them from `x`, `y`. */
28export type SheetViewport = {
29 readonly x: number;
30 readonly y: number;
31 readonly width: number;
32 readonly height: number;
33};
34
35/** The cells of one viewport. */
36export type SheetFrameRequest = {
37 readonly sheet: number;
38 readonly x: number;
39 readonly y: number;
40 readonly width: number;
41 readonly height: number;
42 /** Device pixels per CSS pixel. */
43 readonly ratio: number;
44 /** Where the cells start inside the bitmap's first device pixel, so a frame lands where a direct paint would. */
45 readonly offset: { readonly x: number; readonly y: number };
46 /**
47 * A larger viewport around this one to draw instead, so the frame still covers the screen after a scroll that
48 * outruns the next frame. The worker draws the requested viewport alone where the engine refuses the larger one.
49 */
50 readonly wider: SheetViewport | undefined;
51};
52
53/** A painted frame: the bitmap and the little geometry the window needs to draw headers, select and follow links. */
54export type SheetFrame = {
55 /** The cells, `offset` device pixels in from its top-left corner. */
56 readonly bitmap: ImageBitmap;
57 /** The viewport the bitmap shows: the requested one, or a wider one with the margin. */
58 readonly viewport: SheetViewport;
59 readonly grid: GridMeta | undefined;
60 readonly hyperlinks: readonly HyperlinkRegion[];
61 /** The frozen panes' size in the frame. */
62 readonly frozen: { readonly width: number; readonly height: number };
63 /** Where the sheet's last cell ends, past the frozen panes; undefined when the engine cannot tell. */
64 readonly end: { readonly x: number; readonly y: number } | undefined;
65};
66
67/** The workbook worker's methods. Every method but `open` needs the workbook `open` opened. */
68export type SheetMethods = {
69 /**
70 * Opens the file's bytes and the sheet to show first: the active one, or the first visible one when the workbook was
71 * saved on a hidden sheet. Undefined when no sheet is visible.
72 */
73 readonly open: Method<
74 { readonly bytes: ArrayBuffer; readonly view: WorkbookView },
75 { readonly tabs: readonly SheetTab[]; readonly layout: SheetLayout } | undefined
76 >;
77 /** Makes a sheet the active one; rejects, leaving the sheet before it active, when it cannot open. */
78 readonly show: Method<{ readonly sheet: number }, SheetLayout>;
79 /** Paints a frame; undefined when even its smallest tiles cannot be drawn. */
80 readonly frame: Method<SheetFrameRequest, SheetFrame | undefined>;
81 /** Where a cell starts and where the cell past it starts, to scroll it into view. */
82 readonly position: Method<
83 { readonly sheet: number; readonly cell: CellAddr },
84 | { readonly start: { x: number; y: number }; readonly next: { x: number; y: number } }
85 | undefined
86 >;
87 /** The cells' text as they show, row by row, for screen readers. */
88 readonly cells: Method<
89 { readonly sheet: number; readonly range: CellRange },
90 readonly (readonly string[])[]
91 >;
92 /** The cells as they show, as tab-separated text to paste; undefined when they cannot be read. */
93 readonly copy: Method<{ readonly sheet: number; readonly range: CellRange }, string | undefined>;
94};