@molecule/app-spreadsheet-grid-angular 0.0.0-stage → 0.0.1-stage

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,675 @@
1
+ /**
2
+ * SpreadsheetGrid component (React parity).
3
+ *
4
+ * @module
5
+ */
6
+ import { NgTemplateOutlet } from '@angular/common';
7
+ import { afterNextRender, Component, ElementRef, inject, Injector, signal, } from '@angular/core';
8
+ import { useTranslation } from '@molecule/app-angular';
9
+ import { copyTextWithFallback } from '@molecule/app-clipboard';
10
+ import { getClassMap } from '@molecule/app-ui';
11
+ import { computeLayout } from './layout.js';
12
+ import { cellRef, formatCellValue, normalizeSelection, parseClipboardTsv, serializeSelectionTsv, } from './utilities.js';
13
+ import * as i0 from "@angular/core";
14
+ const _c0 = a0 => ({ $implicit: a0 });
15
+ const _forTrack0 = ($index, $item) => $item.key;
16
+ const _forTrack1 = ($index, $item) => $item.ref;
17
+ function SpreadsheetGrid_ng_template_0_Conditional_1_Template(rf, ctx) { if (rf & 1) {
18
+ const _r4 = i0.ɵɵgetCurrentView();
19
+ i0.ɵɵelementStart(0, "input", 10);
20
+ i0.ɵɵlistener("input", function SpreadsheetGrid_ng_template_0_Conditional_1_Template_input_input_0_listener($event) { i0.ɵɵrestoreView(_r4); const ctx_r2 = i0.ɵɵnextContext(2); return i0.ɵɵresetView(ctx_r2.onDraftInput($event)); })("blur", function SpreadsheetGrid_ng_template_0_Conditional_1_Template_input_blur_0_listener() { i0.ɵɵrestoreView(_r4); const ctx_r2 = i0.ɵɵnextContext(2); return i0.ɵɵresetView(ctx_r2.commitEdit()); })("keydown", function SpreadsheetGrid_ng_template_0_Conditional_1_Template_input_keydown_0_listener($event) { i0.ɵɵrestoreView(_r4); const ctx_r2 = i0.ɵɵnextContext(2); return i0.ɵɵresetView(ctx_r2.onEditorKeydown($event)); });
21
+ i0.ɵɵelementEnd();
22
+ } if (rf & 2) {
23
+ const cell_r2 = i0.ɵɵnextContext().$implicit;
24
+ const ctx_r2 = i0.ɵɵnextContext();
25
+ i0.ɵɵclassMap(ctx_r2.plan.editorClasses);
26
+ i0.ɵɵattribute("data-mol-id", "spreadsheet-grid-editor-" + cell_r2.ref)("value", ctx.draft)("aria-label", ctx_r2.cellEditorLabel(cell_r2.ref));
27
+ } }
28
+ function SpreadsheetGrid_ng_template_0_Conditional_2_Conditional_1_Template(rf, ctx) { if (rf & 1) {
29
+ i0.ɵɵelementContainer(0, 7);
30
+ } if (rf & 2) {
31
+ const cell_r2 = i0.ɵɵnextContext(2).$implicit;
32
+ const ctx_r2 = i0.ɵɵnextContext();
33
+ i0.ɵɵproperty("ngTemplateOutlet", ctx)("ngTemplateOutletContext", ctx_r2.cellContext(cell_r2));
34
+ } }
35
+ function SpreadsheetGrid_ng_template_0_Conditional_2_Conditional_2_Template(rf, ctx) { if (rf & 1) {
36
+ i0.ɵɵelementContainerStart(0);
37
+ i0.ɵɵtext(1);
38
+ i0.ɵɵelementContainerEnd();
39
+ } if (rf & 2) {
40
+ const cell_r2 = i0.ɵɵnextContext(2).$implicit;
41
+ i0.ɵɵadvance();
42
+ i0.ɵɵtextInterpolate(cell_r2.text);
43
+ } }
44
+ function SpreadsheetGrid_ng_template_0_Conditional_2_Template(rf, ctx) { if (rf & 1) {
45
+ i0.ɵɵelementStart(0, "div");
46
+ i0.ɵɵconditionalCreate(1, SpreadsheetGrid_ng_template_0_Conditional_2_Conditional_1_Template, 1, 2, "ng-container", 7)(2, SpreadsheetGrid_ng_template_0_Conditional_2_Conditional_2_Template, 2, 1, "ng-container");
47
+ i0.ɵɵelementEnd();
48
+ } if (rf & 2) {
49
+ let tmp_5_0;
50
+ const ctx_r2 = i0.ɵɵnextContext(2);
51
+ i0.ɵɵclassMap(ctx_r2.plan.contentClasses);
52
+ i0.ɵɵadvance();
53
+ i0.ɵɵconditional((tmp_5_0 = ctx_r2.renderCellTemplate()) ? 1 : 2, tmp_5_0);
54
+ } }
55
+ function SpreadsheetGrid_ng_template_0_Template(rf, ctx) { if (rf & 1) {
56
+ const _r1 = i0.ɵɵgetCurrentView();
57
+ i0.ɵɵelementStart(0, "div", 8);
58
+ i0.ɵɵlistener("mousedown", function SpreadsheetGrid_ng_template_0_Template_div_mousedown_0_listener($event) { const cell_r2 = i0.ɵɵrestoreView(_r1).$implicit; const ctx_r2 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r2.onCellMouseDown(cell_r2, $event)); })("mouseenter", function SpreadsheetGrid_ng_template_0_Template_div_mouseenter_0_listener($event) { const cell_r2 = i0.ɵɵrestoreView(_r1).$implicit; const ctx_r2 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r2.onCellMouseEnter(cell_r2, $event)); })("dblclick", function SpreadsheetGrid_ng_template_0_Template_div_dblclick_0_listener() { const cell_r2 = i0.ɵɵrestoreView(_r1).$implicit; const ctx_r2 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r2.beginEdit(cell_r2.row, cell_r2.col)); });
59
+ i0.ɵɵconditionalCreate(1, SpreadsheetGrid_ng_template_0_Conditional_1_Template, 1, 5, "input", 9)(2, SpreadsheetGrid_ng_template_0_Conditional_2_Template, 3, 3, "div", 9);
60
+ i0.ɵɵelementEnd();
61
+ } if (rf & 2) {
62
+ let tmp_8_0;
63
+ const cell_r2 = ctx.$implicit;
64
+ const ctx_r2 = i0.ɵɵnextContext();
65
+ i0.ɵɵstyleMap(cell_r2.style);
66
+ i0.ɵɵclassMap(cell_r2.classes);
67
+ i0.ɵɵattribute("data-mol-id", "spreadsheet-grid-cell-" + cell_r2.ref)("tabindex", cell_r2.anchor ? 0 : -1)("aria-selected", cell_r2.selected ? "true" : null);
68
+ i0.ɵɵadvance();
69
+ i0.ɵɵconditional((tmp_8_0 = ctx_r2.editorAt(cell_r2.row, cell_r2.col)) ? 1 : 2, tmp_8_0);
70
+ } }
71
+ function SpreadsheetGrid_For_5_Template(rf, ctx) { if (rf & 1) {
72
+ i0.ɵɵelementStart(0, "div", 11);
73
+ i0.ɵɵtext(1);
74
+ i0.ɵɵelementEnd();
75
+ } if (rf & 2) {
76
+ const header_r5 = ctx.$implicit;
77
+ i0.ɵɵstyleMap(header_r5.style);
78
+ i0.ɵɵclassMap(header_r5.classes);
79
+ i0.ɵɵattribute("aria-colindex", header_r5.index + 1);
80
+ i0.ɵɵadvance();
81
+ i0.ɵɵtextInterpolate(header_r5.text);
82
+ } }
83
+ function SpreadsheetGrid_For_8_Template(rf, ctx) { if (rf & 1) {
84
+ i0.ɵɵelementStart(0, "div", 11);
85
+ i0.ɵɵtext(1);
86
+ i0.ɵɵelementEnd();
87
+ } if (rf & 2) {
88
+ const header_r6 = ctx.$implicit;
89
+ i0.ɵɵstyleMap(header_r6.style);
90
+ i0.ɵɵclassMap(header_r6.classes);
91
+ i0.ɵɵattribute("aria-colindex", header_r6.index + 1);
92
+ i0.ɵɵadvance();
93
+ i0.ɵɵtextInterpolate(header_r6.text);
94
+ } }
95
+ function SpreadsheetGrid_For_11_Template(rf, ctx) { if (rf & 1) {
96
+ i0.ɵɵelementStart(0, "div");
97
+ i0.ɵɵtext(1);
98
+ i0.ɵɵelementEnd();
99
+ } if (rf & 2) {
100
+ const label_r7 = ctx.$implicit;
101
+ i0.ɵɵstyleMap(label_r7.style);
102
+ i0.ɵɵclassMap(label_r7.classes);
103
+ i0.ɵɵadvance();
104
+ i0.ɵɵtextInterpolate(label_r7.text);
105
+ } }
106
+ function SpreadsheetGrid_For_14_Template(rf, ctx) { if (rf & 1) {
107
+ i0.ɵɵelementStart(0, "div");
108
+ i0.ɵɵtext(1);
109
+ i0.ɵɵelementEnd();
110
+ } if (rf & 2) {
111
+ const label_r8 = ctx.$implicit;
112
+ i0.ɵɵstyleMap(label_r8.style);
113
+ i0.ɵɵclassMap(label_r8.classes);
114
+ i0.ɵɵadvance();
115
+ i0.ɵɵtextInterpolate(label_r8.text);
116
+ } }
117
+ function SpreadsheetGrid_Conditional_15_For_3_Template(rf, ctx) { if (rf & 1) {
118
+ i0.ɵɵelementContainer(0, 7);
119
+ } if (rf & 2) {
120
+ const cell_r9 = ctx.$implicit;
121
+ i0.ɵɵnextContext(2);
122
+ const cellTpl_r10 = i0.ɵɵreference(1);
123
+ i0.ɵɵproperty("ngTemplateOutlet", cellTpl_r10)("ngTemplateOutletContext", i0.ɵɵpureFunction1(2, _c0, cell_r9));
124
+ } }
125
+ function SpreadsheetGrid_Conditional_15_Template(rf, ctx) { if (rf & 1) {
126
+ i0.ɵɵelementStart(0, "div")(1, "div");
127
+ i0.ɵɵrepeaterCreate(2, SpreadsheetGrid_Conditional_15_For_3_Template, 1, 4, "ng-container", 7, _forTrack1);
128
+ i0.ɵɵelementEnd()();
129
+ } if (rf & 2) {
130
+ const ctx_r2 = i0.ɵɵnextContext();
131
+ i0.ɵɵstyleMap(ctx_r2.plan.frozenRowsRegionStyle);
132
+ i0.ɵɵadvance();
133
+ i0.ɵɵstyleMap(ctx_r2.plan.frozenRowsInnerStyle);
134
+ i0.ɵɵadvance();
135
+ i0.ɵɵrepeater(ctx_r2.plan.frozenRowsCells);
136
+ } }
137
+ function SpreadsheetGrid_Conditional_16_For_3_Template(rf, ctx) { if (rf & 1) {
138
+ i0.ɵɵelementContainer(0, 7);
139
+ } if (rf & 2) {
140
+ const cell_r11 = ctx.$implicit;
141
+ i0.ɵɵnextContext(2);
142
+ const cellTpl_r10 = i0.ɵɵreference(1);
143
+ i0.ɵɵproperty("ngTemplateOutlet", cellTpl_r10)("ngTemplateOutletContext", i0.ɵɵpureFunction1(2, _c0, cell_r11));
144
+ } }
145
+ function SpreadsheetGrid_Conditional_16_Template(rf, ctx) { if (rf & 1) {
146
+ i0.ɵɵelementStart(0, "div")(1, "div");
147
+ i0.ɵɵrepeaterCreate(2, SpreadsheetGrid_Conditional_16_For_3_Template, 1, 4, "ng-container", 7, _forTrack1);
148
+ i0.ɵɵelementEnd()();
149
+ } if (rf & 2) {
150
+ const ctx_r2 = i0.ɵɵnextContext();
151
+ i0.ɵɵstyleMap(ctx_r2.plan.frozenColsRegionStyle);
152
+ i0.ɵɵadvance();
153
+ i0.ɵɵstyleMap(ctx_r2.plan.frozenColsInnerStyle);
154
+ i0.ɵɵadvance();
155
+ i0.ɵɵrepeater(ctx_r2.plan.frozenColsCells);
156
+ } }
157
+ function SpreadsheetGrid_Conditional_17_For_2_Template(rf, ctx) { if (rf & 1) {
158
+ i0.ɵɵelementContainer(0, 7);
159
+ } if (rf & 2) {
160
+ const cell_r12 = ctx.$implicit;
161
+ i0.ɵɵnextContext(2);
162
+ const cellTpl_r10 = i0.ɵɵreference(1);
163
+ i0.ɵɵproperty("ngTemplateOutlet", cellTpl_r10)("ngTemplateOutletContext", i0.ɵɵpureFunction1(2, _c0, cell_r12));
164
+ } }
165
+ function SpreadsheetGrid_Conditional_17_Template(rf, ctx) { if (rf & 1) {
166
+ i0.ɵɵelementStart(0, "div");
167
+ i0.ɵɵrepeaterCreate(1, SpreadsheetGrid_Conditional_17_For_2_Template, 1, 4, "ng-container", 7, _forTrack1);
168
+ i0.ɵɵelementEnd();
169
+ } if (rf & 2) {
170
+ const ctx_r2 = i0.ɵɵnextContext();
171
+ i0.ɵɵstyleMap(ctx_r2.plan.frozenCornerStyle);
172
+ i0.ɵɵadvance();
173
+ i0.ɵɵrepeater(ctx_r2.plan.frozenCornerCells);
174
+ } }
175
+ function SpreadsheetGrid_For_21_Template(rf, ctx) { if (rf & 1) {
176
+ i0.ɵɵelementContainer(0, 7);
177
+ } if (rf & 2) {
178
+ const cell_r13 = ctx.$implicit;
179
+ i0.ɵɵnextContext();
180
+ const cellTpl_r10 = i0.ɵɵreference(1);
181
+ i0.ɵɵproperty("ngTemplateOutlet", cellTpl_r10)("ngTemplateOutletContext", i0.ɵɵpureFunction1(2, _c0, cell_r13));
182
+ } }
183
+ /**
184
+ * Whether a node-valued input is an Angular template (an `ng-template`) rather
185
+ * than a plain value. Checked structurally — `TemplateRef` is a type-only
186
+ * import in the emitted metadata, so `instanceof` is not dependable across
187
+ * bundles.
188
+ *
189
+ * @param value - The input value.
190
+ * @returns True when it can be rendered with `ngTemplateOutlet`.
191
+ */
192
+ const isTemplate = (value) => typeof value === 'object' &&
193
+ value !== null &&
194
+ typeof value.createEmbeddedView === 'function';
195
+ /**
196
+ * SpreadsheetGrid component.
197
+ *
198
+ * High-performance virtualized spreadsheet cell grid: the host IS React's root
199
+ * `<div role="grid">` (`div[mol-spreadsheet-grid]`), so no wrapper element
200
+ * exists. Renders an `rows × columns` grid backed by a sparse
201
+ * `Map<cellRef, value>`, with frozen rows/columns, range selection (click +
202
+ * drag, Shift+click), copy/paste through the system clipboard as TSV, and
203
+ * in-cell editing (double-click → input; Enter commits, Escape cancels).
204
+ * Only the cells inside the visible viewport (plus a small overscan margin)
205
+ * are rendered.
206
+ *
207
+ * React's callbacks are inputs (they keep their React names and their argument
208
+ * lists); React's `renderCell` render prop is an `ng-template`.
209
+ */
210
+ export class SpreadsheetGrid {
211
+ /** Total number of rows in the grid. */
212
+ rows;
213
+ /** Total number of columns in the grid. */
214
+ columns;
215
+ /** Sparse cell-data map. Cells not present render as empty. */
216
+ cells;
217
+ /** Called when the user commits an edit (React's `onCellChange`). */
218
+ onCellChange;
219
+ /** Current selection. */
220
+ selection;
221
+ /** Called when the user changes the selection (React's `onSelectionChange`). */
222
+ onSelectionChange;
223
+ /** Number of rows frozen at the top (default `0` at read time). */
224
+ frozenRows;
225
+ /** Number of columns frozen on the left (default `0` at read time). */
226
+ frozenCols;
227
+ /** Per-cell width in pixels (default `96` at read time). */
228
+ cellWidth;
229
+ /** Per-cell height in pixels (default `28` at read time). */
230
+ cellHeight;
231
+ /** Width of the row-number gutter (default `48` at read time). */
232
+ gutterWidth;
233
+ /** Height of the column-letter header (default `28` at read time). */
234
+ headerHeight;
235
+ /** Visible viewport width in pixels (default `720` at read time). */
236
+ viewportWidth;
237
+ /** Visible viewport height in pixels (default `360` at read time). */
238
+ viewportHeight;
239
+ /** `data-mol-id` for AI-agent selectors. */
240
+ dataMolId;
241
+ /** Optional cell renderer (`ng-template`): `$implicit`/`a0` value, `a1` ref. */
242
+ renderCell;
243
+ /** The grid's horizontal scroll offset. */
244
+ scrollLeft = signal(0, /* @ts-ignore */
245
+ ...(ngDevMode ? [{ debugName: "scrollLeft" }] : /* istanbul ignore next */ []));
246
+ /** The grid's vertical scroll offset. */
247
+ scrollTop = signal(0, /* @ts-ignore */
248
+ ...(ngDevMode ? [{ debugName: "scrollTop" }] : /* istanbul ignore next */ []));
249
+ /** The open in-cell editor. */
250
+ editing = signal(null, /* @ts-ignore */
251
+ ...(ngDevMode ? [{ debugName: "editing" }] : /* istanbul ignore next */ []));
252
+ /** The component host (the root `<div role="grid">`). */
253
+ host = inject(ElementRef);
254
+ /** The injector `afterNextRender` needs outside a constructor. */
255
+ injector = inject(Injector);
256
+ /**
257
+ * Reactive translation, read in the injection context. Its `t` reads the
258
+ * locale, so the view re-renders the labels on a locale switch.
259
+ */
260
+ i18n = useTranslation();
261
+ /** Where the current drag started, while a mouse button is held. */
262
+ dragAnchor = null;
263
+ /** The plan computed for the current change-detection pass. */
264
+ currentPlan = null;
265
+ /**
266
+ * The layout plan for the current change-detection pass (recomputed in
267
+ * {@link ngDoCheck}, before the template and host bindings read it).
268
+ *
269
+ * @returns The layout plan.
270
+ */
271
+ get plan() {
272
+ if (!this.currentPlan)
273
+ this.currentPlan = computeLayout(this.geometry(), this.cm());
274
+ return this.currentPlan;
275
+ }
276
+ /**
277
+ * Recomputes the plan, then re-applies React's controlled-input value: the
278
+ * live value follows the draft after a re-render (the binding sets the
279
+ * attribute; only a user edit moves the property).
280
+ */
281
+ ngDoCheck() {
282
+ this.currentPlan = computeLayout(this.geometry(), this.cm());
283
+ const open = this.editing();
284
+ const field = this.host.nativeElement.querySelector('input');
285
+ if (open && field instanceof HTMLInputElement && field.value !== open.draft)
286
+ field.value = open.draft;
287
+ }
288
+ /**
289
+ * The current class map.
290
+ *
291
+ * @returns The UIClassMap.
292
+ */
293
+ cm() {
294
+ return getClassMap();
295
+ }
296
+ /**
297
+ * The root's accessible label (React's `t('spreadsheetGrid.label', …)`).
298
+ *
299
+ * @returns The translated label.
300
+ */
301
+ label() {
302
+ return this.i18n.t('spreadsheetGrid.label', undefined, { defaultValue: 'Spreadsheet' });
303
+ }
304
+ /**
305
+ * The editor field's accessible label (React's `t('spreadsheetGrid.cellEditor', { ref }, …)`).
306
+ *
307
+ * @param ref - The cell's A1 reference.
308
+ * @returns The translated label.
309
+ */
310
+ cellEditorLabel(ref) {
311
+ return this.i18n.t('spreadsheetGrid.cellEditor', { ref }, {
312
+ defaultValue: 'Edit cell {{ref}}',
313
+ });
314
+ }
315
+ /**
316
+ * The editor state of one cell, when that cell is the one being edited.
317
+ *
318
+ * @param row - 0-indexed row.
319
+ * @param col - 0-indexed column.
320
+ * @returns The editor state, or null.
321
+ */
322
+ editorAt(row, col) {
323
+ const current = this.editing();
324
+ return current && current.row === row && current.col === col ? current : null;
325
+ }
326
+ /**
327
+ * The `renderCell` template, when the caller supplied one.
328
+ *
329
+ * @returns The template, or null.
330
+ */
331
+ renderCellTemplate() {
332
+ return isTemplate(this.renderCell) ? this.renderCell : null;
333
+ }
334
+ /**
335
+ * The context a `renderCell` template receives (the value as `$implicit`/`a0`,
336
+ * the A1 reference as `a1`).
337
+ *
338
+ * @param cell - The cell.
339
+ * @returns The template context.
340
+ */
341
+ cellContext(cell) {
342
+ return { $implicit: cell.value, a0: cell.value, a1: cell.ref, a2: undefined };
343
+ }
344
+ /**
345
+ * The current geometry from the inputs and the grid's own state.
346
+ *
347
+ * @returns The geometry.
348
+ */
349
+ geometry() {
350
+ return {
351
+ rows: this.rows,
352
+ columns: this.columns,
353
+ cells: this.cells,
354
+ selection: this.selection,
355
+ frozenRows: this.frozenRows ?? 0,
356
+ frozenCols: this.frozenCols ?? 0,
357
+ cellWidth: this.cellWidth ?? 96,
358
+ cellHeight: this.cellHeight ?? 28,
359
+ gutterWidth: this.gutterWidth ?? 48,
360
+ headerHeight: this.headerHeight ?? 28,
361
+ viewportWidth: this.viewportWidth ?? 720,
362
+ viewportHeight: this.viewportHeight ?? 360,
363
+ scrollLeft: this.scrollLeft(),
364
+ scrollTop: this.scrollTop(),
365
+ editing: this.editing(),
366
+ };
367
+ }
368
+ /**
369
+ * Opens the in-cell editor on one cell, seeding the draft from its value.
370
+ *
371
+ * @param row - 0-indexed row.
372
+ * @param col - 0-indexed column.
373
+ */
374
+ beginEdit(row, col) {
375
+ this.editing.set({
376
+ row,
377
+ col,
378
+ draft: formatCellValue(this.cells.get(cellRef(row, col))),
379
+ });
380
+ // React focuses and selects the field in an effect after the editor
381
+ // mounts; there is no field before this render, so focus after the next one.
382
+ afterNextRender(() => {
383
+ const field = this.host.nativeElement.querySelector('input');
384
+ if (field instanceof HTMLInputElement) {
385
+ field.focus();
386
+ field.select();
387
+ }
388
+ }, { injector: this.injector });
389
+ }
390
+ /** Commits the open edit: empty clears the cell, number-like text becomes a number. */
391
+ commitEdit() {
392
+ const current = this.editing();
393
+ if (!current)
394
+ return;
395
+ const ref = cellRef(current.row, current.col);
396
+ const trimmed = current.draft;
397
+ let next;
398
+ if (trimmed === '')
399
+ next = null;
400
+ else if (/^-?\d+(\.\d+)?$/.test(trimmed))
401
+ next = Number(trimmed);
402
+ else
403
+ next = trimmed;
404
+ this.onCellChange?.(ref, next);
405
+ this.editing.set(null);
406
+ }
407
+ /** Closes the editor without committing. */
408
+ cancelEdit() {
409
+ this.editing.set(null);
410
+ }
411
+ /**
412
+ * React's cell `onMouseDown`: commits an open edit, then starts or extends
413
+ * the selection.
414
+ *
415
+ * @param cell - The cell.
416
+ * @param event - The mouse event.
417
+ */
418
+ onCellMouseDown(cell, event) {
419
+ if (this.editing())
420
+ this.commitEdit();
421
+ const selection = this.selection;
422
+ if (event.shiftKey) {
423
+ this.onSelectionChange?.({ r1: selection.r1, c1: selection.c1, r2: cell.row, c2: cell.col });
424
+ }
425
+ else {
426
+ this.dragAnchor = { row: cell.row, col: cell.col };
427
+ this.onSelectionChange?.({ r1: cell.row, c1: cell.col, r2: cell.row, c2: cell.col });
428
+ }
429
+ }
430
+ /**
431
+ * React's cell `onMouseEnter`: extends the selection while the primary
432
+ * button is held.
433
+ *
434
+ * @param cell - The cell.
435
+ * @param event - The mouse event.
436
+ */
437
+ onCellMouseEnter(cell, event) {
438
+ if (event.buttons !== 1)
439
+ return;
440
+ const anchor = this.dragAnchor;
441
+ if (!anchor)
442
+ return;
443
+ this.onSelectionChange?.({ r1: anchor.row, c1: anchor.col, r2: cell.row, c2: cell.col });
444
+ }
445
+ /** Clears the drag anchor on mouse up. */
446
+ onMouseUp() {
447
+ this.dragAnchor = null;
448
+ }
449
+ /**
450
+ * React's container `onKeyDown`: copy, and Edit on Enter / F2.
451
+ *
452
+ * @param event - The keyboard event.
453
+ */
454
+ onKeydown(event) {
455
+ const isMod = event.ctrlKey || event.metaKey;
456
+ if (this.editing())
457
+ return;
458
+ if (isMod && event.key.toLowerCase() === 'c') {
459
+ event.preventDefault();
460
+ void this.handleCopy();
461
+ return;
462
+ }
463
+ if (isMod && event.key.toLowerCase() === 'v')
464
+ return;
465
+ if (event.key === 'Enter' || event.key === 'F2') {
466
+ event.preventDefault();
467
+ this.beginEdit(this.selection.r1, this.selection.c1);
468
+ }
469
+ }
470
+ /**
471
+ * The editor field's key handling: Enter commits, Escape cancels. The grid's
472
+ * own key handler runs only while no editor is open (React reads the state
473
+ * the render before the event began, so a key that commits here never
474
+ * reaches it), so the event stays on the field.
475
+ *
476
+ * @param event - The keyboard event.
477
+ */
478
+ onEditorKeydown(event) {
479
+ event.stopPropagation();
480
+ if (event.key === 'Enter') {
481
+ event.preventDefault();
482
+ this.commitEdit();
483
+ }
484
+ else if (event.key === 'Escape') {
485
+ event.preventDefault();
486
+ this.cancelEdit();
487
+ }
488
+ }
489
+ /**
490
+ * React's editor `onChange`: each typed value becomes the draft.
491
+ *
492
+ * @param event - The native input event.
493
+ */
494
+ onDraftInput(event) {
495
+ const current = this.editing();
496
+ if (!current)
497
+ return;
498
+ this.editing.set({ ...current, draft: event.target.value });
499
+ }
500
+ /**
501
+ * React's container `onPaste`: intercepts the native paste event.
502
+ *
503
+ * @param event - The clipboard event.
504
+ */
505
+ onPaste(event) {
506
+ const text = event.clipboardData ? event.clipboardData.getData('text/plain') : '';
507
+ if (!text)
508
+ return;
509
+ event.preventDefault();
510
+ this.handlePaste(text);
511
+ }
512
+ /**
513
+ * The scroller's `scroll` handler: refreshes the rendered window.
514
+ *
515
+ * @param event - The scroll event.
516
+ */
517
+ onScroll(event) {
518
+ const element = event.currentTarget;
519
+ this.scrollLeft.set(element.scrollLeft);
520
+ this.scrollTop.set(element.scrollTop);
521
+ }
522
+ /**
523
+ * Serializes the selection to TSV and copies it through the clipboard bond.
524
+ *
525
+ * @returns The TSV text.
526
+ */
527
+ async handleCopy() {
528
+ const tsv = serializeSelectionTsv(this.cells, this.selection);
529
+ await copyTextWithFallback(tsv);
530
+ return tsv;
531
+ }
532
+ /**
533
+ * Writes pasted TSV into cells starting at the selection anchor.
534
+ *
535
+ * @param text - Clipboard text (typically TSV).
536
+ */
537
+ handlePaste(text) {
538
+ const grid = parseClipboardTsv(text);
539
+ if (grid.length === 0)
540
+ return;
541
+ const n = normalizeSelection(this.selection);
542
+ for (let r = 0; r < grid.length; r += 1) {
543
+ const row = grid[r];
544
+ for (let c = 0; c < row.length; c += 1) {
545
+ const targetRow = n.r1 + r;
546
+ const targetCol = n.c1 + c;
547
+ if (targetRow >= this.rows || targetCol >= this.columns)
548
+ continue;
549
+ const raw = row[c];
550
+ let next;
551
+ if (raw === '')
552
+ next = null;
553
+ else if (/^-?\d+(\.\d+)?$/.test(raw))
554
+ next = Number(raw);
555
+ else
556
+ next = raw;
557
+ this.onCellChange?.(cellRef(targetRow, targetCol), next);
558
+ }
559
+ }
560
+ }
561
+ static ɵfac = function SpreadsheetGrid_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || SpreadsheetGrid)(); };
562
+ static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: SpreadsheetGrid, selectors: [["div", "mol-spreadsheet-grid", ""]], hostAttrs: ["role", "grid"], hostVars: 9, hostBindings: function SpreadsheetGrid_HostBindings(rf, ctx) { if (rf & 1) {
563
+ i0.ɵɵlistener("keydown", function SpreadsheetGrid_keydown_HostBindingHandler($event) { return ctx.onKeydown($event); })("paste", function SpreadsheetGrid_paste_HostBindingHandler($event) { return ctx.onPaste($event); })("mouseup", function SpreadsheetGrid_mouseup_HostBindingHandler() { return ctx.onMouseUp(); });
564
+ } if (rf & 2) {
565
+ i0.ɵɵattribute("tabindex", 0)("data-mol-id", ctx.dataMolId ?? "spreadsheet-grid")("aria-rowcount", ctx.rows)("aria-colcount", ctx.columns)("aria-label", ctx.label());
566
+ i0.ɵɵstyleMap(ctx.plan.rootStyle);
567
+ i0.ɵɵclassMap(ctx.plan.rootClasses);
568
+ } }, inputs: { rows: "rows", columns: "columns", cells: "cells", onCellChange: "onCellChange", selection: "selection", onSelectionChange: "onSelectionChange", frozenRows: "frozenRows", frozenCols: "frozenCols", cellWidth: "cellWidth", cellHeight: "cellHeight", gutterWidth: "gutterWidth", headerHeight: "headerHeight", viewportWidth: "viewportWidth", viewportHeight: "viewportHeight", dataMolId: "dataMolId", renderCell: "renderCell" }, decls: 24, vars: 28, consts: [["cellTpl", ""], ["aria-hidden", "true"], ["role", "row", "aria-rowindex", "1"], ["role", "columnheader", 3, "class", "style"], [3, "class", "style"], [3, "style"], ["data-mol-id", "spreadsheet-grid-scroll", 3, "scroll"], [3, "ngTemplateOutlet", "ngTemplateOutletContext"], ["role", "gridcell", 3, "mousedown", "mouseenter", "dblclick"], [3, "class"], [3, "input", "blur", "keydown"], ["role", "columnheader"]], template: function SpreadsheetGrid_Template(rf, ctx) { if (rf & 1) {
569
+ i0.ɵɵtemplate(0, SpreadsheetGrid_ng_template_0_Template, 3, 8, "ng-template", null, 0, i0.ɵɵtemplateRefExtractor);
570
+ i0.ɵɵelement(2, "div", 1);
571
+ i0.ɵɵelementStart(3, "div", 2);
572
+ i0.ɵɵrepeaterCreate(4, SpreadsheetGrid_For_5_Template, 2, 6, "div", 3, _forTrack0);
573
+ i0.ɵɵelementStart(6, "div");
574
+ i0.ɵɵrepeaterCreate(7, SpreadsheetGrid_For_8_Template, 2, 6, "div", 3, _forTrack0);
575
+ i0.ɵɵelementEnd()();
576
+ i0.ɵɵelementStart(9, "div", 1);
577
+ i0.ɵɵrepeaterCreate(10, SpreadsheetGrid_For_11_Template, 2, 5, "div", 4, _forTrack0);
578
+ i0.ɵɵelementStart(12, "div");
579
+ i0.ɵɵrepeaterCreate(13, SpreadsheetGrid_For_14_Template, 2, 5, "div", 4, _forTrack0);
580
+ i0.ɵɵelementEnd()();
581
+ i0.ɵɵconditionalCreate(15, SpreadsheetGrid_Conditional_15_Template, 4, 4, "div", 5);
582
+ i0.ɵɵconditionalCreate(16, SpreadsheetGrid_Conditional_16_Template, 4, 4, "div", 5);
583
+ i0.ɵɵconditionalCreate(17, SpreadsheetGrid_Conditional_17_Template, 3, 2, "div", 5);
584
+ i0.ɵɵelementStart(18, "div", 6);
585
+ i0.ɵɵlistener("scroll", function SpreadsheetGrid_Template_div_scroll_18_listener($event) { return ctx.onScroll($event); });
586
+ i0.ɵɵelementStart(19, "div");
587
+ i0.ɵɵrepeaterCreate(20, SpreadsheetGrid_For_21_Template, 1, 4, "ng-container", 7, _forTrack1);
588
+ i0.ɵɵelementEnd()();
589
+ i0.ɵɵelementStart(22, "span", 1);
590
+ i0.ɵɵtext(23);
591
+ i0.ɵɵelementEnd();
592
+ } if (rf & 2) {
593
+ i0.ɵɵadvance(2);
594
+ i0.ɵɵstyleMap(ctx.plan.cornerStyle);
595
+ i0.ɵɵclassMap(ctx.plan.cornerClasses);
596
+ i0.ɵɵadvance();
597
+ i0.ɵɵstyleMap(ctx.plan.headerLaneStyle);
598
+ i0.ɵɵclassMap(ctx.plan.headerLaneClasses);
599
+ i0.ɵɵadvance();
600
+ i0.ɵɵrepeater(ctx.plan.frozenHeaderCells);
601
+ i0.ɵɵadvance(2);
602
+ i0.ɵɵstyleMap(ctx.plan.headerScrollerStyle);
603
+ i0.ɵɵadvance();
604
+ i0.ɵɵrepeater(ctx.plan.scrollingHeaderCells);
605
+ i0.ɵɵadvance(2);
606
+ i0.ɵɵstyleMap(ctx.plan.gutterLaneStyle);
607
+ i0.ɵɵclassMap(ctx.plan.gutterLaneClasses);
608
+ i0.ɵɵadvance();
609
+ i0.ɵɵrepeater(ctx.plan.frozenGutterCells);
610
+ i0.ɵɵadvance(2);
611
+ i0.ɵɵstyleMap(ctx.plan.gutterScrollerStyle);
612
+ i0.ɵɵadvance();
613
+ i0.ɵɵrepeater(ctx.plan.scrollingGutterCells);
614
+ i0.ɵɵadvance(2);
615
+ i0.ɵɵconditional(ctx.plan.showFrozenRows ? 15 : -1);
616
+ i0.ɵɵadvance();
617
+ i0.ɵɵconditional(ctx.plan.showFrozenCols ? 16 : -1);
618
+ i0.ɵɵadvance();
619
+ i0.ɵɵconditional(ctx.plan.showFrozenCorner ? 17 : -1);
620
+ i0.ɵɵadvance();
621
+ i0.ɵɵstyleMap(ctx.plan.scrollRegionStyle);
622
+ i0.ɵɵadvance();
623
+ i0.ɵɵstyleMap(ctx.plan.scrollSpacerStyle);
624
+ i0.ɵɵadvance();
625
+ i0.ɵɵrepeater(ctx.plan.mainCells);
626
+ i0.ɵɵadvance(2);
627
+ i0.ɵɵstyleMap(ctx.plan.totalStyle);
628
+ i0.ɵɵclassMap(ctx.plan.totalClasses);
629
+ i0.ɵɵadvance();
630
+ i0.ɵɵtextInterpolate(ctx.plan.totalText);
631
+ } }, dependencies: [NgTemplateOutlet], encapsulation: 2 });
632
+ }
633
+ (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(SpreadsheetGrid, [{
634
+ type: Component,
635
+ args: [{
636
+ selector: 'div[mol-spreadsheet-grid]',
637
+ standalone: true,
638
+ imports: [NgTemplateOutlet],
639
+ inputs: [
640
+ 'rows',
641
+ 'columns',
642
+ 'cells',
643
+ 'onCellChange',
644
+ 'selection',
645
+ 'onSelectionChange',
646
+ 'frozenRows',
647
+ 'frozenCols',
648
+ 'cellWidth',
649
+ 'cellHeight',
650
+ 'gutterWidth',
651
+ 'headerHeight',
652
+ 'viewportWidth',
653
+ 'viewportHeight',
654
+ 'dataMolId',
655
+ 'renderCell',
656
+ ],
657
+ host: {
658
+ role: 'grid',
659
+ '[attr.tabindex]': '0',
660
+ '[attr.data-mol-id]': 'dataMolId ?? "spreadsheet-grid"',
661
+ '[attr.aria-rowcount]': 'rows',
662
+ '[attr.aria-colcount]': 'columns',
663
+ '[attr.aria-label]': 'label()',
664
+ '[class]': 'plan.rootClasses',
665
+ '[style]': 'plan.rootStyle',
666
+ '(keydown)': 'onKeydown($event)',
667
+ '(paste)': 'onPaste($event)',
668
+ '(mouseup)': 'onMouseUp()',
669
+ },
670
+ // prettier-ignore
671
+ template: `<ng-template #cellTpl let-cell><div role="gridcell" [attr.data-mol-id]="'spreadsheet-grid-cell-' + cell.ref" [attr.tabindex]="cell.anchor ? 0 : -1" [attr.aria-selected]="cell.selected ? 'true' : null" [class]="cell.classes" [style]="cell.style" (mousedown)="onCellMouseDown(cell, $event)" (mouseenter)="onCellMouseEnter(cell, $event)" (dblclick)="beginEdit(cell.row, cell.col)">@if (editorAt(cell.row, cell.col); as open) {<input [attr.data-mol-id]="'spreadsheet-grid-editor-' + cell.ref" [class]="plan.editorClasses" [attr.value]="open.draft" (input)="onDraftInput($event)" (blur)="commitEdit()" (keydown)="onEditorKeydown($event)" [attr.aria-label]="cellEditorLabel(cell.ref)" />} @else {<div [class]="plan.contentClasses">@if (renderCellTemplate(); as cellRenderer) {<ng-container [ngTemplateOutlet]="cellRenderer" [ngTemplateOutletContext]="cellContext(cell)"></ng-container>} @else {<ng-container>{{ cell.text }}</ng-container>}</div>}</div></ng-template><div aria-hidden="true" [class]="plan.cornerClasses" [style]="plan.cornerStyle"></div><div role="row" aria-rowindex="1" [class]="plan.headerLaneClasses" [style]="plan.headerLaneStyle">@for (header of plan.frozenHeaderCells; track header.key) {<div role="columnheader" [attr.aria-colindex]="header.index + 1" [class]="header.classes" [style]="header.style">{{ header.text }}</div>}<div [style]="plan.headerScrollerStyle">@for (header of plan.scrollingHeaderCells; track header.key) {<div role="columnheader" [attr.aria-colindex]="header.index + 1" [class]="header.classes" [style]="header.style">{{ header.text }}</div>}</div></div><div aria-hidden="true" [class]="plan.gutterLaneClasses" [style]="plan.gutterLaneStyle">@for (label of plan.frozenGutterCells; track label.key) {<div [class]="label.classes" [style]="label.style">{{ label.text }}</div>}<div [style]="plan.gutterScrollerStyle">@for (label of plan.scrollingGutterCells; track label.key) {<div [class]="label.classes" [style]="label.style">{{ label.text }}</div>}</div></div>@if (plan.showFrozenRows) {<div [style]="plan.frozenRowsRegionStyle"><div [style]="plan.frozenRowsInnerStyle">@for (cell of plan.frozenRowsCells; track cell.ref) {<ng-container [ngTemplateOutlet]="cellTpl" [ngTemplateOutletContext]="{ $implicit: cell }"></ng-container>}</div></div>}@if (plan.showFrozenCols) {<div [style]="plan.frozenColsRegionStyle"><div [style]="plan.frozenColsInnerStyle">@for (cell of plan.frozenColsCells; track cell.ref) {<ng-container [ngTemplateOutlet]="cellTpl" [ngTemplateOutletContext]="{ $implicit: cell }"></ng-container>}</div></div>}@if (plan.showFrozenCorner) {<div [style]="plan.frozenCornerStyle">@for (cell of plan.frozenCornerCells; track cell.ref) {<ng-container [ngTemplateOutlet]="cellTpl" [ngTemplateOutletContext]="{ $implicit: cell }"></ng-container>}</div>}<div data-mol-id="spreadsheet-grid-scroll" [style]="plan.scrollRegionStyle" (scroll)="onScroll($event)"><div [style]="plan.scrollSpacerStyle">@for (cell of plan.mainCells; track cell.ref) {<ng-container [ngTemplateOutlet]="cellTpl" [ngTemplateOutletContext]="{ $implicit: cell }"></ng-container>}</div></div><span [class]="plan.totalClasses" [style]="plan.totalStyle" aria-hidden="true">{{ plan.totalText }}</span>`,
672
+ }]
673
+ }], null, null); })();
674
+ (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassDebugInfo(SpreadsheetGrid, { className: "SpreadsheetGrid", filePath: "spreadsheet-grid.component.ts", lineNumber: 111 }); })();
675
+ //# sourceMappingURL=spreadsheet-grid.component.js.map