Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

sheet-protocol.ts

93 lines4.3 KB

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