@betteroffice/xlsx 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,458 @@
1
+ /**
2
+ * TS mirror of the Rust display list.
3
+ *
4
+ * Hand-mirrored from crates/xlsx-render/src/display_list.rs — keep in sync.
5
+ * The renderer front-half (Rust) emits a target-agnostic list of draw commands;
6
+ * a backend (Canvas2D in the browser, tiny-skia on servers) executes them. These
7
+ * types are plain data with no DOM or framework dependency so the same list
8
+ * flows across the wasm boundary as JSON and into either backend unchanged.
9
+ */
10
+ /**
11
+ * Axis-aligned rectangle in device-independent pixels.
12
+ */
13
+ interface Rect {
14
+ x: number;
15
+ y: number;
16
+ w: number;
17
+ h: number;
18
+ }
19
+ /**
20
+ * Horizontal alignment for a text command, matching the spreadsheet cell
21
+ * alignment values the renderer resolves before emitting.
22
+ */
23
+ type TextAlign = 'left' | 'center' | 'right';
24
+ /**
25
+ * Fill a solid rectangle — cell backgrounds, selection bands, header fills.
26
+ */
27
+ interface FillRectCmd {
28
+ op: 'fillRect';
29
+ x: number;
30
+ y: number;
31
+ w: number;
32
+ h: number;
33
+ /** css color string (`#rrggbb`, `rgba(...)`), resolved from theme + tint in Rust. */
34
+ color: string;
35
+ }
36
+ /**
37
+ * Stroke a straight line — gridlines, cell borders, frozen-pane dividers.
38
+ */
39
+ interface LineCmd {
40
+ op: 'line';
41
+ x1: number;
42
+ y1: number;
43
+ x2: number;
44
+ y2: number;
45
+ width: number;
46
+ color: string;
47
+ /**
48
+ * Stroke pattern. Absent/`undefined` is solid; `'dashed'`/`'dotted'` apply a
49
+ * dash pattern; `'double'` draws two thin parallel passes offset by ±. Skip-
50
+ * serialized at its default in Rust, so a solid line omits the field.
51
+ */
52
+ style?: 'dashed' | 'dotted' | 'double';
53
+ }
54
+ /**
55
+ * Paint a single-line text run. Spreadsheet cell text is single-line and
56
+ * clipped to the cell box, so `clip` is the cell rect and `align` places the
57
+ * run within it. Text metrics are owned upstream in Rust.
58
+ */
59
+ interface TextCmd {
60
+ op: 'text';
61
+ x: number;
62
+ y: number;
63
+ text: string;
64
+ fontSize: number;
65
+ /** resolved font color (`#rrggbb`); a number-format color prefix wins upstream. */
66
+ color: string;
67
+ /** clip rectangle; the backend saves/clips/restores around the fill. */
68
+ clip?: Rect;
69
+ align?: TextAlign;
70
+ /**
71
+ * Font style facets resolved from the cell's style. All skip-serialized at
72
+ * `false` in Rust, so an unstyled run omits them; treat absent as `false`.
73
+ */
74
+ bold?: boolean;
75
+ italic?: boolean;
76
+ underline?: boolean;
77
+ strike?: boolean;
78
+ /** css/font family from the style font; the backend falls back to its default. */
79
+ fontFamily?: string;
80
+ }
81
+ /**
82
+ * One draw command; discriminated on `op`.
83
+ */
84
+ type DrawCmd = FillRectCmd | LineCmd | TextCmd;
85
+ /**
86
+ * Grid metadata for the frame: which sheet cells the visible tracks map to and
87
+ * their viewport-local pixel boundaries. `rowOffsets[i]`/`colOffsets[i]` is the
88
+ * leading edge of the i-th visible row/col in device-independent px from the
89
+ * frame origin; both arrays have length `visible count + 1`, the last entry
90
+ * being one-past-end (the trailing edge of the last visible track). The pure
91
+ * hit-test and a11y seams read this to place clicks and mirror the grid without
92
+ * re-deriving geometry.
93
+ *
94
+ * Hand-mirrored from crates/xlsx-render/src/display_list.rs — keep in sync.
95
+ */
96
+ interface GridMeta {
97
+ startRow: number;
98
+ startCol: number;
99
+ rowOffsets: number[];
100
+ colOffsets: number[];
101
+ }
102
+ /**
103
+ * A full frame to paint: logical size plus the ordered command stream. `grid`
104
+ * is optional so a synthetic or pre-grid-metadata frame still type-checks; the
105
+ * hit-test and a11y builders treat its absence as "no addressable cells".
106
+ */
107
+ interface DisplayList {
108
+ width: number;
109
+ height: number;
110
+ commands: DrawCmd[];
111
+ grid?: GridMeta;
112
+ }
113
+
114
+ /**
115
+ * Pure viewport types. No DOM — this is the compute seam the a11y grid, the
116
+ * canvas painter, and a future Rust port all read from.
117
+ */
118
+ /**
119
+ * Cumulative pixel offsets for a track axis (columns or rows). `offsets[i]` is
120
+ * the leading edge of track `i`; `offsets.length === trackCount + 1`, so the
121
+ * last element is the total extent. Frozen tracks are the first `frozen` of
122
+ * these — they are pinned and always visible.
123
+ */
124
+ type TrackOffsets = number[];
125
+ /**
126
+ * Scroll + geometry state driving virtualization. `scrollX/scrollY` are in
127
+ * content pixels of the scrollable (non-frozen) region; `width/height` is the
128
+ * visible viewport in css pixels; `dpr` scales to device pixels at paint time.
129
+ */
130
+ interface ViewportState {
131
+ scrollX: number;
132
+ scrollY: number;
133
+ width: number;
134
+ height: number;
135
+ dpr: number;
136
+ frozenRows: number;
137
+ frozenCols: number;
138
+ }
139
+ /**
140
+ * An inclusive track index range. `last < first` (e.g. `{ first: 0, last: -1 }`)
141
+ * denotes an empty range.
142
+ */
143
+ interface VisibleRange {
144
+ first: number;
145
+ last: number;
146
+ }
147
+ /**
148
+ * The visible track window on both axes, frozen tracks excluded (they always
149
+ * render). Feed to the renderer to build the display list for a frame.
150
+ */
151
+ interface VisibleCells {
152
+ cols: VisibleRange;
153
+ rows: VisibleRange;
154
+ }
155
+
156
+ /**
157
+ * Pure viewport math: visible-range computation against cumulative track
158
+ * offsets, and scroll clamping. No DOM, no side effects — every function is a
159
+ * deterministic transform of plain numbers so it is golden-testable and can be
160
+ * mirrored in Rust.
161
+ */
162
+
163
+ /**
164
+ * Number of tracks described by an offsets array.
165
+ */
166
+ declare function trackCount(offsets: TrackOffsets): number;
167
+ /**
168
+ * Total pixel extent of a track axis (position of the trailing edge).
169
+ */
170
+ declare function totalExtent(offsets: TrackOffsets): number;
171
+ /**
172
+ * The inclusive range of scrollable (non-frozen) tracks intersecting the
173
+ * viewport. `frozen` tracks at the head are pinned and excluded here — the
174
+ * scrollable window starts after them and is shortened by their pinned width.
175
+ */
176
+ declare function visibleRange(offsets: TrackOffsets, frozen: number, scroll: number, viewportExtent: number): VisibleRange;
177
+ /**
178
+ * Visible column and row ranges for a frame, frozen tracks excluded.
179
+ */
180
+ declare function visibleCells(state: ViewportState, colOffsets: TrackOffsets, rowOffsets: TrackOffsets): VisibleCells;
181
+ /**
182
+ * Clamp a viewport's scroll offsets into the valid range for the given content.
183
+ * Returns the same state when already in range, a corrected copy otherwise.
184
+ */
185
+ declare function clampScroll(state: ViewportState, colOffsets: TrackOffsets, rowOffsets: TrackOffsets): ViewportState;
186
+ /**
187
+ * Build a uniform offsets array of `count` tracks each `size` px wide.
188
+ */
189
+ declare function uniformOffsets(count: number, size: number): TrackOffsets;
190
+
191
+ /**
192
+ * Pure selection types. Plain data only — no DOM, no framework — so the model
193
+ * is golden-testable and mirrorable in Rust. The chrome layer interprets the
194
+ * declarative {@link SelectionAction}s the reducer emits.
195
+ */
196
+ /**
197
+ * A single cell coordinate. `row`/`col` are zero-based sheet indices, not px.
198
+ */
199
+ interface CellAddr {
200
+ row: number;
201
+ col: number;
202
+ }
203
+ /**
204
+ * An active selection as an anchor/focus pair. `anchor` is the fixed corner
205
+ * (where the selection started); `focus` is the moving corner. A collapsed
206
+ * selection has `anchor` equal to `focus`.
207
+ */
208
+ interface Selection {
209
+ anchor: CellAddr;
210
+ focus: CellAddr;
211
+ }
212
+ /**
213
+ * A normalized, inclusive rectangle of cells. `top <= bottom`, `left <= right`.
214
+ * Derived from a {@link Selection} by {@link normalizeRange}; the natural shape
215
+ * for hit-testing, overlay rects, and range reads.
216
+ */
217
+ interface CellRange {
218
+ top: number;
219
+ left: number;
220
+ bottom: number;
221
+ right: number;
222
+ }
223
+ /**
224
+ * A cardinal move direction for {@link moveFocus} and arrow-key handling.
225
+ */
226
+ type Direction = 'up' | 'down' | 'left' | 'right';
227
+ /**
228
+ * Grid bounds a move must stay inside: the used range is `[0, rows)` × `[0,
229
+ * cols)`, and `rowsPerPage` is the viewport height in whole rows for
230
+ * PageUp/PageDown steps.
231
+ */
232
+ interface SelectionLimits {
233
+ rows: number;
234
+ cols: number;
235
+ rowsPerPage: number;
236
+ }
237
+ /**
238
+ * Options for {@link moveFocus}: whether to extend (keep the anchor) or collapse
239
+ * to a single cell, how many cells to step, and the grid bounds to clamp into.
240
+ */
241
+ interface MoveOpts {
242
+ extend?: boolean;
243
+ step?: number;
244
+ limits: SelectionLimits;
245
+ }
246
+ /**
247
+ * A keyboard event reduced to the fields the reducer reads. `metaKey`/`ctrlKey`
248
+ * are both accepted so a caller need not normalize mac cmd vs win/linux ctrl.
249
+ */
250
+ interface KeyInput {
251
+ key: string;
252
+ shiftKey?: boolean;
253
+ metaKey?: boolean;
254
+ ctrlKey?: boolean;
255
+ altKey?: boolean;
256
+ }
257
+ /**
258
+ * The declarative outcome of a keystroke. The chrome interprets it: `move`
259
+ * replaces the selection, `startEdit` opens the in-cell editor (seeding it with
260
+ * `initialInput` when a printable key triggered it), `clear` deletes the
261
+ * selected cells' contents, and `none` means the key was not handled here.
262
+ */
263
+ type SelectionAction = {
264
+ type: 'move';
265
+ selection: Selection;
266
+ } | {
267
+ type: 'startEdit';
268
+ initialInput?: string;
269
+ } | {
270
+ type: 'clear';
271
+ } | {
272
+ type: 'none';
273
+ };
274
+
275
+ /**
276
+ * Selection model: pure transforms over {@link Selection} data. Every function
277
+ * returns a fresh value and never mutates its input, so state lives with the
278
+ * chrome and this layer stays a deterministic reducer core.
279
+ */
280
+
281
+ /**
282
+ * A collapsed selection at a single cell.
283
+ */
284
+ declare function selectionAt(addr: CellAddr): Selection;
285
+ /**
286
+ * The normalized inclusive rectangle spanned by a selection's anchor and focus.
287
+ */
288
+ declare function normalizeRange(sel: Selection): CellRange;
289
+ /**
290
+ * Whether a cell falls inside a selection's normalized range.
291
+ */
292
+ declare function rangeContains(sel: Selection, addr: CellAddr): boolean;
293
+ /**
294
+ * Number of cells covered by a selection.
295
+ */
296
+ declare function cellCount(sel: Selection): number;
297
+ /**
298
+ * Extend the selection to a new focus, holding the anchor fixed (shift-click /
299
+ * shift-arrow semantics). The address is clamped into the grid when limits pass.
300
+ */
301
+ declare function extendTo(sel: Selection, addr: CellAddr, limits?: SelectionLimits): Selection;
302
+ /**
303
+ * Move the focus one (or `step`) cells in a direction. When `extend` is set the
304
+ * anchor holds and the range grows; otherwise the whole selection collapses onto
305
+ * the new focus. The result is clamped to the grid bounds in `opts.limits`.
306
+ */
307
+ declare function moveFocus(sel: Selection, direction: Direction, opts: MoveOpts): Selection;
308
+
309
+ /**
310
+ * Keyboard reducer: maps a keystroke over the current selection to a
311
+ * declarative {@link SelectionAction}. Pure — it neither mutates state nor
312
+ * touches the DOM; the chrome owns the keydown listener and interprets the
313
+ * action. Excel navigation semantics, with two deliberate choices documented at
314
+ * the F2/Enter handlers below.
315
+ */
316
+
317
+ /**
318
+ * Reduce a keystroke to a selection action against the current selection and
319
+ * grid limits. Returns `{ type: 'none' }` for keys this layer does not own so
320
+ * the chrome can fall through to its own handling.
321
+ */
322
+ declare function selectionKeyReducer(state: Selection, key: KeyInput, limits: SelectionLimits): SelectionAction;
323
+
324
+ /**
325
+ * Pure hit-testing over a frame's {@link GridMeta}: map a viewport-local point
326
+ * to a cell, and map cells/ranges back to their pixel rects for overlay
327
+ * placement. Binary search over the boundary offsets; DOM-free so click
328
+ * handling and the selection overlay share one geometry source with the painter.
329
+ */
330
+
331
+ /**
332
+ * The cell under a viewport-local point, or `null` when the point is outside the
333
+ * visible grid (or the frame carries no grid metadata).
334
+ */
335
+ declare function cellAtPoint(grid: GridMeta | undefined, x: number, y: number): CellAddr | null;
336
+ /**
337
+ * The pixel rect of a single cell in viewport-local coordinates, or `null` when
338
+ * the cell is not within the visible window.
339
+ */
340
+ declare function cellRect(grid: GridMeta | undefined, row: number, col: number): Rect | null;
341
+ /**
342
+ * The pixel rect covering the visible part of a cell range, clipped to the
343
+ * window. `null` when the range lies entirely outside the visible grid — so the
344
+ * selection overlay is hidden rather than mispainted when scrolled away.
345
+ */
346
+ declare function rangeRect(grid: GridMeta | undefined, range: CellRange): Rect | null;
347
+
348
+ /**
349
+ * Pure TSV codec for clipboard interchange, in the Excel dialect (tab-separated
350
+ * fields, CRLF rows, `"`-quoted fields with doubled embedded quotes). No DOM —
351
+ * the chrome hands the system clipboard the string this produces and feeds paste
352
+ * text back through {@link fromTsv}.
353
+ *
354
+ * Security (CLAUDE.md formula-injection contract): a foreign spreadsheet treats
355
+ * a pasted cell as a formula when its text leads with `=`, `+`, `-`, `@`, or a
356
+ * control char that can break the field. We defang such *text* with a leading
357
+ * single quote so it pastes literally — but we export our own real formulas
358
+ * (`isFormula`) verbatim, because faithfully round-tripping a `=SUM(...)` the
359
+ * user actually wrote is the point, and it is our value, not attacker text.
360
+ */
361
+ /**
362
+ * One source cell as the wasm range read returns it: the raw entered `input`
363
+ * (a `=...` string when `isFormula`) plus whether the model holds it as a
364
+ * formula.
365
+ */
366
+ interface CellInput {
367
+ input: string;
368
+ isFormula: boolean;
369
+ }
370
+ /**
371
+ * Serialize a row-major grid of cells to a TSV string for the system clipboard,
372
+ * defanging formula-looking text (see the module security note) while exporting
373
+ * genuine formulas as-is.
374
+ */
375
+ declare function toTsv(cells: CellInput[][]): string;
376
+ /**
377
+ * Parse clipboard TSV back into a row-major grid of raw strings. Handles the
378
+ * Excel dialect: `"`-quoted fields with doubled quotes and embedded tabs/
379
+ * newlines, CRLF or LF rows, and a single trailing newline (which does not add
380
+ * an empty row). The formula-injection guard quote is left in place — it is part
381
+ * of the literal text a defanged cell round-trips to.
382
+ */
383
+ declare function fromTsv(text: string): string[][];
384
+
385
+ /**
386
+ * Pure a11y types: a serializable mirror of the visible grid the chrome renders
387
+ * as an offscreen `role="grid"` for screen readers. Plain data only — the
388
+ * builder takes label templates as a param rather than importing i18n, so this
389
+ * seam stays dependency-free.
390
+ */
391
+ /**
392
+ * Label templates the builder interpolates. Field names mirror the `a11y` keys
393
+ * in the framework adapter so a caller can pass localized strings through.
394
+ * Placeholders: `{address}`, `{value}`, `{row}`, `{column}`.
395
+ */
396
+ interface A11yStrings {
397
+ gridLabel: string;
398
+ cellLabel: string;
399
+ cellLabelSelected: string;
400
+ emptyCellLabel: string;
401
+ emptyCellLabelSelected: string;
402
+ rowHeaderLabel: string;
403
+ columnHeaderLabel: string;
404
+ }
405
+ /**
406
+ * One mirrored cell: its address, display text, selection state, and the fully
407
+ * interpolated aria label (e.g. `"B2, 42, selected"`).
408
+ */
409
+ interface A11yCell {
410
+ row: number;
411
+ col: number;
412
+ address: string;
413
+ text: string;
414
+ selected: boolean;
415
+ label: string;
416
+ }
417
+ /**
418
+ * A column header: the letter (`"A"`) and its aria label (`"Column A"`).
419
+ */
420
+ interface A11yColumnHeader {
421
+ col: number;
422
+ text: string;
423
+ label: string;
424
+ }
425
+ /**
426
+ * One mirrored row: its 1-based header label and the cells left-to-right.
427
+ */
428
+ interface A11yRow {
429
+ row: number;
430
+ header: string;
431
+ cells: A11yCell[];
432
+ }
433
+ /**
434
+ * The full offscreen grid mirror for the current frame.
435
+ */
436
+ interface A11yGrid {
437
+ label: string;
438
+ sheetName: string;
439
+ columnHeaders: A11yColumnHeader[];
440
+ rows: A11yRow[];
441
+ }
442
+
443
+ /**
444
+ * Pure a11y grid builder: turns a display list plus the current selection into a
445
+ * serializable {@link A11yGrid} the chrome renders offscreen for screen readers.
446
+ * Cell text is recovered from the frame's clipped text commands, keyed by cell
447
+ * box origin; geometry and addresses come from the frame's grid metadata. No
448
+ * DOM, no i18n import — label templates arrive as a param.
449
+ */
450
+
451
+ /**
452
+ * Build the offscreen grid mirror for a frame. Returns an empty grid (no rows)
453
+ * when the frame carries no grid metadata, so the chrome can render nothing
454
+ * rather than guess geometry.
455
+ */
456
+ declare function buildA11yGrid(displayList: DisplayList, selection: Selection | null, sheetName: string, t: A11yStrings): A11yGrid;
457
+
458
+ export { type A11yCell, type A11yColumnHeader, type A11yGrid, type A11yRow, type A11yStrings, type CellAddr, type CellInput, type CellRange, type Direction, type DisplayList, type DrawCmd, type FillRectCmd, type GridMeta, type KeyInput, type LineCmd, type MoveOpts, type Rect, type Selection, type SelectionAction, type SelectionLimits, type TextAlign, type TextCmd, type TrackOffsets, type ViewportState, type VisibleCells, type VisibleRange, buildA11yGrid, cellAtPoint, cellCount, cellRect, clampScroll, extendTo, fromTsv, moveFocus, normalizeRange, rangeContains, rangeRect, selectionAt, selectionKeyReducer, toTsv, totalExtent, trackCount, uniformOffsets, visibleCells, visibleRange };