@svgrid/grid 1.2.21 → 1.2.23
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.
- package/dist/GridFooter.svelte +34 -20
- package/dist/GridFooter.svelte.d.ts +8 -0
- package/dist/SvAutoComplete.svelte +100 -0
- package/dist/SvAutoComplete.svelte.d.ts +16 -0
- package/dist/SvButton.svelte +97 -0
- package/dist/SvButton.svelte.d.ts +23 -0
- package/dist/SvCalendar.svelte +669 -0
- package/dist/SvCalendar.svelte.d.ts +70 -0
- package/dist/SvCheckBox.svelte +81 -0
- package/dist/SvCheckBox.svelte.d.ts +19 -0
- package/dist/SvColorInput.svelte +187 -0
- package/dist/SvColorInput.svelte.d.ts +15 -0
- package/dist/SvComboBox.svelte +130 -0
- package/dist/SvComboBox.svelte.d.ts +15 -0
- package/dist/SvCountryInput.svelte +123 -0
- package/dist/SvCountryInput.svelte.d.ts +14 -0
- package/dist/SvDateTimePicker.svelte +322 -0
- package/dist/SvDateTimePicker.svelte.d.ts +37 -0
- package/dist/SvDropDownList.svelte +143 -0
- package/dist/SvDropDownList.svelte.d.ts +15 -0
- package/dist/SvForm.svelte +147 -0
- package/dist/SvForm.svelte.d.ts +29 -0
- package/dist/SvGauge.svelte +99 -0
- package/dist/SvGauge.svelte.d.ts +31 -0
- package/dist/SvGrid.controller.svelte.d.ts +16 -2
- package/dist/SvGrid.controller.svelte.js +63 -8
- package/dist/SvGrid.css +84 -31
- package/dist/SvGrid.helpers.d.ts +4 -0
- package/dist/SvGrid.helpers.js +40 -12
- package/dist/SvGrid.svelte +84 -5
- package/dist/SvGrid.types.d.ts +36 -0
- package/dist/SvGridDropdown.svelte +16 -30
- package/dist/SvGridDropdown.svelte.d.ts +4 -0
- package/dist/SvListBox.svelte +122 -0
- package/dist/SvListBox.svelte.d.ts +19 -0
- package/dist/SvMaskedInput.svelte +0 -0
- package/dist/SvMaskedInput.svelte.d.ts +15 -0
- package/dist/SvNumberInput.svelte +0 -0
- package/dist/SvNumberInput.svelte.d.ts +22 -0
- package/dist/SvPasswordInput.svelte +110 -0
- package/dist/SvPasswordInput.svelte.d.ts +22 -0
- package/dist/SvPhoneInput.svelte +0 -0
- package/dist/SvPhoneInput.svelte.d.ts +19 -0
- package/dist/SvRadioGroup.svelte +108 -0
- package/dist/SvRadioGroup.svelte.d.ts +23 -0
- package/dist/SvRating.svelte +111 -0
- package/dist/SvRating.svelte.d.ts +20 -0
- package/dist/SvRepeatButton.svelte +89 -0
- package/dist/SvRepeatButton.svelte.d.ts +21 -0
- package/dist/SvSlider.svelte +182 -0
- package/dist/SvSlider.svelte.d.ts +25 -0
- package/dist/SvSwitchButton.svelte +94 -0
- package/dist/SvSwitchButton.svelte.d.ts +19 -0
- package/dist/SvTabs.svelte +122 -0
- package/dist/SvTabs.svelte.d.ts +26 -0
- package/dist/SvTagsInput.svelte +81 -0
- package/dist/SvTagsInput.svelte.d.ts +19 -0
- package/dist/SvTimePicker.svelte +359 -0
- package/dist/SvTimePicker.svelte.d.ts +27 -0
- package/dist/SvToggleButton.svelte +57 -0
- package/dist/SvToggleButton.svelte.d.ts +16 -0
- package/dist/SvTree.svelte +210 -0
- package/dist/SvTree.svelte.d.ts +29 -0
- package/dist/build-api.js +78 -1
- package/dist/cdn/svgrid.js +10022 -5904
- package/dist/cdn/svgrid.svelte-external.js +8531 -4429
- package/dist/cell-render.d.ts +4 -0
- package/dist/cell-render.js +35 -1
- package/dist/clipboard.js +19 -1
- package/dist/collaboration.d.ts +2 -0
- package/dist/collaboration.js +4 -0
- package/dist/core.d.ts +22 -1
- package/dist/countries.d.ts +15 -0
- package/dist/countries.js +61 -0
- package/dist/datetime/date-core.d.ts +88 -0
- package/dist/datetime/date-core.js +165 -0
- package/dist/datetime/date-format.d.ts +39 -0
- package/dist/datetime/date-format.js +371 -0
- package/dist/datetime/date-restrict.d.ts +23 -0
- package/dist/datetime/date-restrict.js +50 -0
- package/dist/datetime/date-selection.d.ts +25 -0
- package/dist/datetime/date-selection.js +94 -0
- package/dist/datetime/mask.d.ts +21 -0
- package/dist/datetime/mask.js +90 -0
- package/dist/editing.d.ts +1 -1
- package/dist/editing.js +7 -0
- package/dist/editors/cell-editors.d.ts +2 -2
- package/dist/editors/cell-editors.js +3 -1
- package/dist/export-format.d.ts +178 -0
- package/dist/export-format.js +419 -0
- package/dist/hyperformula-adapter.d.ts +4 -0
- package/dist/hyperformula-adapter.js +15 -6
- package/dist/index.d.ts +36 -0
- package/dist/index.js +41 -0
- package/dist/list-option.d.ts +10 -0
- package/dist/list-option.js +7 -0
- package/dist/menus.js +18 -2
- package/dist/popover.d.ts +48 -0
- package/dist/popover.js +65 -0
- package/dist/row-resize.d.ts +4 -4
- package/dist/row-resize.js +8 -5
- package/dist/selection.d.ts +6 -0
- package/dist/selection.js +31 -5
- package/dist/server-data-source.d.ts +43 -0
- package/dist/server-data-source.js +100 -0
- package/dist/sparkline.js +8 -4
- package/dist/svgrid-wrapper.types.d.ts +26 -1
- package/package.json +6 -1
- package/src/GridFooter.svelte +34 -20
- package/src/SvAutoComplete.svelte +100 -0
- package/src/SvButton.svelte +97 -0
- package/src/SvCalendar.svelte +669 -0
- package/src/SvCalendar.test.ts +211 -0
- package/src/SvCheckBox.svelte +81 -0
- package/src/SvColorInput.svelte +187 -0
- package/src/SvComboBox.svelte +130 -0
- package/src/SvCountryInput.svelte +123 -0
- package/src/SvDateTimePicker.svelte +322 -0
- package/src/SvDateTimePicker.test.ts +136 -0
- package/src/SvDropDownList.svelte +143 -0
- package/src/SvForm.svelte +147 -0
- package/src/SvGauge.svelte +99 -0
- package/src/SvGrid.controller.svelte.ts +74 -8
- package/src/SvGrid.css +84 -31
- package/src/SvGrid.helpers.ts +41 -12
- package/src/SvGrid.svelte +84 -5
- package/src/SvGrid.types.ts +33 -0
- package/src/SvGridDropdown.svelte +16 -30
- package/src/SvListBox.svelte +122 -0
- package/src/SvMaskedInput.svelte +0 -0
- package/src/SvNumberInput.svelte +0 -0
- package/src/SvPasswordInput.svelte +110 -0
- package/src/SvPhoneInput.svelte +0 -0
- package/src/SvRadioGroup.svelte +108 -0
- package/src/SvRating.svelte +111 -0
- package/src/SvRepeatButton.svelte +89 -0
- package/src/SvSlider.svelte +182 -0
- package/src/SvSwitchButton.svelte +94 -0
- package/src/SvTabs.svelte +122 -0
- package/src/SvTagsInput.svelte +81 -0
- package/src/SvTimePicker.svelte +359 -0
- package/src/SvTimePicker.test.ts +98 -0
- package/src/SvToggleButton.svelte +57 -0
- package/src/SvTree.svelte +210 -0
- package/src/build-api.ts +115 -0
- package/src/cell-render.test.ts +38 -0
- package/src/cell-render.ts +43 -0
- package/src/clipboard.test.ts +18 -0
- package/src/clipboard.ts +24 -1
- package/src/collaboration.test.ts +30 -0
- package/src/collaboration.ts +6 -0
- package/src/core.ts +27 -3
- package/src/countries.ts +71 -0
- package/src/datetime/date-core.test.ts +217 -0
- package/src/datetime/date-core.ts +204 -0
- package/src/datetime/date-format.test.ts +121 -0
- package/src/datetime/date-format.ts +317 -0
- package/src/datetime/date-restrict.ts +60 -0
- package/src/datetime/date-selection.test.ts +129 -0
- package/src/datetime/date-selection.ts +137 -0
- package/src/datetime/mask.test.ts +36 -0
- package/src/datetime/mask.ts +84 -0
- package/src/editing.test.ts +22 -0
- package/src/editing.ts +7 -1
- package/src/editors/cell-editors.ts +7 -1
- package/src/export-data-api.test.ts +126 -0
- package/src/export-format.test.ts +107 -0
- package/src/export-format.ts +598 -0
- package/src/hyperformula-adapter.test.ts +35 -1
- package/src/hyperformula-adapter.ts +18 -6
- package/src/index.ts +71 -0
- package/src/list-option.ts +15 -0
- package/src/menus.ts +21 -2
- package/src/popover.ts +79 -0
- package/src/row-resize.test.ts +25 -0
- package/src/row-resize.ts +8 -5
- package/src/selection.test.ts +36 -4
- package/src/selection.ts +30 -9
- package/src/server-data-source.test.ts +201 -0
- package/src/server-data-source.ts +148 -0
- package/src/sparkline.test.ts +9 -0
- package/src/sparkline.ts +9 -4
- package/src/svgrid-wrapper.types.ts +28 -1
- package/src/svgrid.behavior.test.ts +27 -0
- package/src/svgrid.new-features.wrapper.test.ts +27 -1
- package/src/ui-buttons.test.ts +105 -0
- package/src/ui-composite.test.ts +89 -0
- package/src/ui-inputs.test.ts +92 -0
- package/src/ui-range.test.ts +61 -0
- package/src/ui-selection.test.ts +106 -0
package/dist/editing.js
CHANGED
|
@@ -299,6 +299,13 @@ export function createEditing(ctx) {
|
|
|
299
299
|
* (no preventDefault) whenever the async API is unavailable.
|
|
300
300
|
*/
|
|
301
301
|
function onGridPaste(event) {
|
|
302
|
+
// A cell is being edited: the focused editor input handles its own paste
|
|
303
|
+
// natively. Never hijack it for a range paste - doing so (on Firefox /
|
|
304
|
+
// insecure contexts, where the async Clipboard API is unavailable) would
|
|
305
|
+
// preventDefault the editor's paste and write to the data cell instead,
|
|
306
|
+
// so the edit "wouldn't update".
|
|
307
|
+
if (ctx.editingCell)
|
|
308
|
+
return;
|
|
302
309
|
// When the async API is available the keydown handler already pasted via
|
|
303
310
|
// pasteFromClipboard(); ignore the (suppressed) native event so we don't
|
|
304
311
|
// paste twice.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type CellEditorType = 'text' | 'number' | 'date' | 'datetime' | 'time' | 'password' | 'checkbox' | 'list' | 'chips' | 'select' | 'rich-select' | 'autocomplete' | 'textarea' | 'color' | 'rating';
|
|
1
|
+
export type CellEditorType = 'text' | 'number' | 'date' | 'datetime' | 'time' | 'date-native' | 'datetime-native' | 'time-native' | 'password' | 'checkbox' | 'list' | 'chips' | 'select' | 'rich-select' | 'autocomplete' | 'textarea' | 'color' | 'rating';
|
|
2
2
|
/** Normalized option used by list/chips editors. The optional `color`
|
|
3
3
|
* paints the chip - both in the in-cell readonly chips render and in
|
|
4
4
|
* the dropdown trigger when `renderChipsInTrigger` is on. Any CSS
|
|
@@ -20,4 +20,4 @@ export declare function normalizeEditorOptions(options: ReadonlyArray<string | n
|
|
|
20
20
|
label?: string;
|
|
21
21
|
color?: string;
|
|
22
22
|
}> | undefined): CellEditorOption[];
|
|
23
|
-
export declare function parseEditorValue(
|
|
23
|
+
export declare function parseEditorValue(rawType: CellEditorType, value: unknown, opts?: ParseEditorValueOptions): any;
|
|
@@ -14,7 +14,9 @@ export function normalizeEditorOptions(options) {
|
|
|
14
14
|
};
|
|
15
15
|
});
|
|
16
16
|
}
|
|
17
|
-
export function parseEditorValue(
|
|
17
|
+
export function parseEditorValue(rawType, value, opts) {
|
|
18
|
+
// `date-native` etc. parse identically to their rich counterparts.
|
|
19
|
+
const type = rawType.replace(/-native$/, '');
|
|
18
20
|
if (type === 'number') {
|
|
19
21
|
const parsed = Number(value);
|
|
20
22
|
return Number.isFinite(parsed) ? parsed : null;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure, render-free value formatting for data export. The grid's on-screen
|
|
3
|
+
* `formatCellValue` (see cell-render.ts) is a closure bound to a `Row<TData>`
|
|
4
|
+
* and the live table, so it can't be reused directly by an exporter that only
|
|
5
|
+
* has raw values + a column's `format` config. These helpers replicate the
|
|
6
|
+
* numeric / date / percent branches of that formatter as standalone functions:
|
|
7
|
+
*
|
|
8
|
+
* - `formatValueForExport(value, format)` returns the display STRING a user
|
|
9
|
+
* sees on screen, so CSV / TSV / HTML / PDF exports are faithful to the
|
|
10
|
+
* grid ("what you see is what you export") instead of dumping raw values.
|
|
11
|
+
* - `toExcelNumFmt(format)` maps a `CellFormatConfig` to an Excel number
|
|
12
|
+
* format code, so an xlsx cell can stay a real number / date while still
|
|
13
|
+
* displaying formatted (currency symbol, thousands, date pattern).
|
|
14
|
+
*
|
|
15
|
+
* Custom per-row `formatter` callbacks are intentionally NOT handled here -
|
|
16
|
+
* they may close over row/table state. Columns that need custom export text
|
|
17
|
+
* should provide an `exportValue(row)` hook at the export layer instead.
|
|
18
|
+
*/
|
|
19
|
+
import type { CellFormatConfig } from './core';
|
|
20
|
+
export { computeColumnStat, contrastText, formatsNeedingStats, resolveCellFormat, type ColumnStat, type ConditionalFormat, type ResolvedCellFormat, } from './conditional-formatting';
|
|
21
|
+
/** Best-effort coercion of a cell value to a Date. Accepts Date, epoch ms
|
|
22
|
+
* numbers, and parseable date strings. Returns null when it isn't a date. */
|
|
23
|
+
export declare function coerceExportDate(value: unknown): Date | null;
|
|
24
|
+
/**
|
|
25
|
+
* Format a raw cell value to its on-screen display string using the column's
|
|
26
|
+
* `format` config. Mirrors the grid's own numeric / currency / percent / date
|
|
27
|
+
* branches. Values with no (or an unrecognised) format fall back to
|
|
28
|
+
* `String(value)`, and `null` / `undefined` become an empty string.
|
|
29
|
+
*/
|
|
30
|
+
export declare function formatValueForExport(value: unknown, format: CellFormatConfig | undefined): string;
|
|
31
|
+
/**
|
|
32
|
+
* Map a `CellFormatConfig` to an Excel/OOXML number-format code, so an xlsx
|
|
33
|
+
* writer can keep the cell numeric/date while displaying it formatted.
|
|
34
|
+
* Returns `undefined` when the format has no natural Excel representation
|
|
35
|
+
* (or the caller should just write a string).
|
|
36
|
+
*
|
|
37
|
+
* These are deliberately conservative, widely-recognised codes:
|
|
38
|
+
* number → `#,##0[.00…]` (grouping + N decimals)
|
|
39
|
+
* currency → `"$"#,##0.00` (symbol + grouping + 2 decimals)
|
|
40
|
+
* percent → `0[.00…]%` (Excel multiplies by 100, so this pairs with a
|
|
41
|
+
* 0..1 fraction; see `valueForExcel`)
|
|
42
|
+
* date → `yyyy-mm-dd`, datetime → `yyyy-mm-dd hh:mm`
|
|
43
|
+
*/
|
|
44
|
+
export declare function toExcelNumFmt(format: CellFormatConfig | undefined): string | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Turn a grid's data rows + columns into the header-first `{ field: value }`
|
|
47
|
+
* record shape the serializers consume, applying each column's `format` so
|
|
48
|
+
* the export matches what's on screen (unless `rawValues`). Optionally
|
|
49
|
+
* restricts + reorders columns to a `columns` field subset.
|
|
50
|
+
*/
|
|
51
|
+
export declare function projectGridRows(dataRows: ReadonlyArray<Record<string, unknown>>, columns: ReadonlyArray<GridExportColumn>, opts?: {
|
|
52
|
+
columns?: ReadonlyArray<string>;
|
|
53
|
+
rawValues?: boolean;
|
|
54
|
+
}): {
|
|
55
|
+
records: Array<Record<string, unknown>>;
|
|
56
|
+
fields: string[];
|
|
57
|
+
align: Record<string, 'left' | 'center' | 'right'>;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* The value to write into a native xlsx cell that carries an Excel numFmt.
|
|
61
|
+
* Numbers stay numbers; percent values are divided to the 0..1 fraction Excel
|
|
62
|
+
* expects (unless the config already stores 0..1); dates become real `Date`s.
|
|
63
|
+
* Returns `{ ok: false }` when the value can't be represented natively and the
|
|
64
|
+
* caller should fall back to the formatted string.
|
|
65
|
+
*/
|
|
66
|
+
export declare function valueForExcel(value: unknown, format: CellFormatConfig | undefined): {
|
|
67
|
+
ok: true;
|
|
68
|
+
value: number | Date;
|
|
69
|
+
} | {
|
|
70
|
+
ok: false;
|
|
71
|
+
};
|
|
72
|
+
export type GridExportScope = 'displayed' | 'selected' | 'all';
|
|
73
|
+
/** The slice of a grid column an exporter needs. Accepts `api.getColumns()`. */
|
|
74
|
+
export type GridExportColumn = {
|
|
75
|
+
field: string;
|
|
76
|
+
header: string;
|
|
77
|
+
format?: CellFormatConfig | undefined;
|
|
78
|
+
align?: 'left' | 'center' | 'right';
|
|
79
|
+
};
|
|
80
|
+
export type GridExportOptions = {
|
|
81
|
+
/** Base filename (no extension). Default 'grid'. */
|
|
82
|
+
filename?: string;
|
|
83
|
+
/** Which rows to export. Default 'displayed' (current view). */
|
|
84
|
+
rows?: GridExportScope;
|
|
85
|
+
/** Restrict to these fields, in this order. Default: all visible columns. */
|
|
86
|
+
columns?: string[];
|
|
87
|
+
/** Export raw values instead of the on-screen formatted display. Default false. */
|
|
88
|
+
rawValues?: boolean;
|
|
89
|
+
/** Prepend a UTF-8 BOM (csv/tsv) so Excel detects UTF-8. Default true. */
|
|
90
|
+
bom?: boolean;
|
|
91
|
+
/** Cancel a large export. */
|
|
92
|
+
signal?: AbortSignal;
|
|
93
|
+
/** Progress for large exports. */
|
|
94
|
+
onProgress?: (progress: SerializeProgress) => void;
|
|
95
|
+
/** When false, skip the browser download and just return the text. Default true. */
|
|
96
|
+
download?: boolean;
|
|
97
|
+
};
|
|
98
|
+
export type GridClipboardFormat = 'csv' | 'tsv' | 'markdown';
|
|
99
|
+
export type GridClipboardOptions = {
|
|
100
|
+
/** Clipboard payload format. Default 'tsv' (pastes straight into Excel). */
|
|
101
|
+
format?: GridClipboardFormat;
|
|
102
|
+
rows?: GridExportScope;
|
|
103
|
+
columns?: string[];
|
|
104
|
+
rawValues?: boolean;
|
|
105
|
+
};
|
|
106
|
+
export type SerializeProgress = {
|
|
107
|
+
phase: 'serialize';
|
|
108
|
+
/** 0..1 completion of the row walk. */
|
|
109
|
+
ratio: number;
|
|
110
|
+
row: number;
|
|
111
|
+
total: number;
|
|
112
|
+
};
|
|
113
|
+
export type CsvOptions = {
|
|
114
|
+
/** Field delimiter. Default ',' (','→CSV, '\t'→TSV). */
|
|
115
|
+
delimiter?: string;
|
|
116
|
+
/** Line ending between records. Default '\r\n' (Excel-friendly). */
|
|
117
|
+
eol?: string;
|
|
118
|
+
/** Prepend a UTF-8 BOM so Excel detects UTF-8. Default true for csv/tsv. */
|
|
119
|
+
bom?: boolean;
|
|
120
|
+
};
|
|
121
|
+
export type SerializeOptions = {
|
|
122
|
+
onProgress?: (p: SerializeProgress) => void;
|
|
123
|
+
signal?: AbortSignal;
|
|
124
|
+
csv?: CsvOptions;
|
|
125
|
+
/** Rows processed between event-loop yields. Default 5000. */
|
|
126
|
+
chunkRows?: number;
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Serialize projected rows to a delimited string (CSV / TSV).
|
|
130
|
+
* `rows[0]` is treated as the header row.
|
|
131
|
+
*/
|
|
132
|
+
export declare function serializeDelimited(rows: ReadonlyArray<Record<string, unknown>>, fields: ReadonlyArray<string>, opts?: SerializeOptions): Promise<string>;
|
|
133
|
+
export type ExportCellVisual = {
|
|
134
|
+
fill?: string;
|
|
135
|
+
color?: string;
|
|
136
|
+
bold?: boolean;
|
|
137
|
+
icon?: string;
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Serialize projected rows to a standalone HTML `<table>` document.
|
|
141
|
+
* `rows[0]` is the header row (rendered as `<th>`); the rest are `<td>`.
|
|
142
|
+
*/
|
|
143
|
+
export declare function serializeHtml(rows: ReadonlyArray<Record<string, unknown>>, fields: ReadonlyArray<string>, opts?: SerializeOptions & {
|
|
144
|
+
title?: string;
|
|
145
|
+
align?: Record<string, 'left' | 'center' | 'right'>;
|
|
146
|
+
/** Per-data-cell conditional-format visual (`rowIdx` is 0-based over data). */
|
|
147
|
+
cellStyle?: (rowIdx: number, colIdx: number) => ExportCellVisual | undefined;
|
|
148
|
+
/** Per-data-cell hyperlink URL. */
|
|
149
|
+
cellLink?: (rowIdx: number, colIdx: number) => string | undefined;
|
|
150
|
+
}): Promise<string>;
|
|
151
|
+
/**
|
|
152
|
+
* Serialize projected rows to a JSON array of `{ field: value }` objects.
|
|
153
|
+
* `rows[0]` (the header row) is dropped - JSON keys are the field names.
|
|
154
|
+
*/
|
|
155
|
+
export declare function serializeJson(rows: ReadonlyArray<Record<string, unknown>>, fields: ReadonlyArray<string>, opts?: SerializeOptions): Promise<string>;
|
|
156
|
+
/**
|
|
157
|
+
* Serialize projected rows to a GitHub-flavored Markdown table. `rows[0]` is
|
|
158
|
+
* the header row; `align` sets the `:---`, `:--:`, `---:` markers per column.
|
|
159
|
+
*/
|
|
160
|
+
export declare function serializeMarkdown(rows: ReadonlyArray<Record<string, unknown>>, fields: ReadonlyArray<string>, opts?: SerializeOptions & {
|
|
161
|
+
align?: Record<string, 'left' | 'center' | 'right'>;
|
|
162
|
+
}): Promise<string>;
|
|
163
|
+
/**
|
|
164
|
+
* Serialize projected rows to a simple XML document:
|
|
165
|
+
* `<rows><row><field>value</field>…</row>…</rows>`. `rows[0]` (header) is
|
|
166
|
+
* dropped - element names come from the field names.
|
|
167
|
+
*/
|
|
168
|
+
export declare function serializeXml(rows: ReadonlyArray<Record<string, unknown>>, fields: ReadonlyArray<string>, opts?: SerializeOptions & {
|
|
169
|
+
rootTag?: string;
|
|
170
|
+
rowTag?: string;
|
|
171
|
+
}): Promise<string>;
|
|
172
|
+
/** Trigger a browser download of an already-built Blob. No-op guard for SSR. */
|
|
173
|
+
export declare function downloadBlobFile(blob: Blob, filename: string): void;
|
|
174
|
+
/** Trigger a browser download of a text blob. No-op guard for SSR. */
|
|
175
|
+
export declare function downloadTextFile(text: string, filename: string, mime: string): void;
|
|
176
|
+
/** Write text to the system clipboard. Throws when the API is unavailable
|
|
177
|
+
* (insecure context / SSR). */
|
|
178
|
+
export declare function copyTextToClipboard(text: string): Promise<void>;
|
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
import { formatNumericWithConfig, getDateFormatter, resolveDatePattern, } from './cell-formatting';
|
|
2
|
+
// Re-export the pure conditional-formatting engine here too, so the
|
|
3
|
+
// Svelte-free `@svgrid/grid/format` subpath carries it for @svgrid/enterprise's
|
|
4
|
+
// export code (which must not pull in SvGrid.svelte).
|
|
5
|
+
export { computeColumnStat, contrastText, formatsNeedingStats, resolveCellFormat, } from './conditional-formatting';
|
|
6
|
+
/** Best-effort coercion of a cell value to a Date. Accepts Date, epoch ms
|
|
7
|
+
* numbers, and parseable date strings. Returns null when it isn't a date. */
|
|
8
|
+
export function coerceExportDate(value) {
|
|
9
|
+
if (value instanceof Date)
|
|
10
|
+
return Number.isNaN(value.getTime()) ? null : value;
|
|
11
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
12
|
+
const d = new Date(value);
|
|
13
|
+
return Number.isNaN(d.getTime()) ? null : d;
|
|
14
|
+
}
|
|
15
|
+
if (typeof value === 'string' && value.trim() !== '') {
|
|
16
|
+
const d = new Date(value);
|
|
17
|
+
return Number.isNaN(d.getTime()) ? null : d;
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Format a raw cell value to its on-screen display string using the column's
|
|
23
|
+
* `format` config. Mirrors the grid's own numeric / currency / percent / date
|
|
24
|
+
* branches. Values with no (or an unrecognised) format fall back to
|
|
25
|
+
* `String(value)`, and `null` / `undefined` become an empty string.
|
|
26
|
+
*/
|
|
27
|
+
export function formatValueForExport(value, format) {
|
|
28
|
+
if (value == null)
|
|
29
|
+
return '';
|
|
30
|
+
if (!format)
|
|
31
|
+
return String(value);
|
|
32
|
+
if (format.type === 'number' || format.type === 'currency' || format.type === 'percent') {
|
|
33
|
+
return formatNumericWithConfig(value, {
|
|
34
|
+
type: format.type,
|
|
35
|
+
locales: format.locales,
|
|
36
|
+
currency: format.type === 'currency' ? (format.currency ?? 'USD') : undefined,
|
|
37
|
+
valueIsPercentPoints: format.type === 'percent' ? format.valueIsPercentPoints : undefined,
|
|
38
|
+
options: format.options,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
if (format.type === 'date' || format.type === 'datetime') {
|
|
42
|
+
const parsed = coerceExportDate(value);
|
|
43
|
+
if (parsed) {
|
|
44
|
+
const preset = resolveDatePattern(format.pattern, format.type);
|
|
45
|
+
const merged = preset || format.options
|
|
46
|
+
? { ...preset, ...format.options }
|
|
47
|
+
: format.type === 'date'
|
|
48
|
+
? { year: 'numeric', month: '2-digit', day: '2-digit' }
|
|
49
|
+
: {
|
|
50
|
+
year: 'numeric',
|
|
51
|
+
month: '2-digit',
|
|
52
|
+
day: '2-digit',
|
|
53
|
+
hour: '2-digit',
|
|
54
|
+
minute: '2-digit',
|
|
55
|
+
};
|
|
56
|
+
return getDateFormatter(format.locales, merged).format(parsed);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return String(value);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Map a `CellFormatConfig` to an Excel/OOXML number-format code, so an xlsx
|
|
63
|
+
* writer can keep the cell numeric/date while displaying it formatted.
|
|
64
|
+
* Returns `undefined` when the format has no natural Excel representation
|
|
65
|
+
* (or the caller should just write a string).
|
|
66
|
+
*
|
|
67
|
+
* These are deliberately conservative, widely-recognised codes:
|
|
68
|
+
* number → `#,##0[.00…]` (grouping + N decimals)
|
|
69
|
+
* currency → `"$"#,##0.00` (symbol + grouping + 2 decimals)
|
|
70
|
+
* percent → `0[.00…]%` (Excel multiplies by 100, so this pairs with a
|
|
71
|
+
* 0..1 fraction; see `valueForExcel`)
|
|
72
|
+
* date → `yyyy-mm-dd`, datetime → `yyyy-mm-dd hh:mm`
|
|
73
|
+
*/
|
|
74
|
+
export function toExcelNumFmt(format) {
|
|
75
|
+
if (!format)
|
|
76
|
+
return undefined;
|
|
77
|
+
const decimals = () => {
|
|
78
|
+
// `options` is a union across number/date formats; only the numeric ones
|
|
79
|
+
// carry fraction-digit hints, so read them through a narrow shape.
|
|
80
|
+
const o = format.options;
|
|
81
|
+
if (o && typeof o.maximumFractionDigits === 'number')
|
|
82
|
+
return o.maximumFractionDigits;
|
|
83
|
+
if (o && typeof o.minimumFractionDigits === 'number')
|
|
84
|
+
return o.minimumFractionDigits;
|
|
85
|
+
return format.type === 'currency' ? 2 : format.type === 'number' ? 0 : 2;
|
|
86
|
+
};
|
|
87
|
+
const dp = (n) => (n > 0 ? '.' + '0'.repeat(n) : '');
|
|
88
|
+
switch (format.type) {
|
|
89
|
+
case 'number':
|
|
90
|
+
return `#,##0${dp(decimals())}`;
|
|
91
|
+
case 'currency': {
|
|
92
|
+
const sym = currencySymbol(format.currency ?? 'USD');
|
|
93
|
+
return `"${sym}"#,##0${dp(decimals())}`;
|
|
94
|
+
}
|
|
95
|
+
case 'percent':
|
|
96
|
+
return `0${dp(decimals())}%`;
|
|
97
|
+
case 'date':
|
|
98
|
+
return 'yyyy-mm-dd';
|
|
99
|
+
case 'datetime':
|
|
100
|
+
return 'yyyy-mm-dd hh:mm';
|
|
101
|
+
default:
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Turn a grid's data rows + columns into the header-first `{ field: value }`
|
|
107
|
+
* record shape the serializers consume, applying each column's `format` so
|
|
108
|
+
* the export matches what's on screen (unless `rawValues`). Optionally
|
|
109
|
+
* restricts + reorders columns to a `columns` field subset.
|
|
110
|
+
*/
|
|
111
|
+
export function projectGridRows(dataRows, columns, opts = {}) {
|
|
112
|
+
const chosen = opts.columns && opts.columns.length
|
|
113
|
+
? opts.columns
|
|
114
|
+
.map((f) => columns.find((c) => c.field === f))
|
|
115
|
+
.filter((c) => !!c)
|
|
116
|
+
: columns.filter((c) => c.field);
|
|
117
|
+
const fields = chosen.map((c) => c.field);
|
|
118
|
+
const align = {};
|
|
119
|
+
for (const c of chosen)
|
|
120
|
+
if (c.align)
|
|
121
|
+
align[c.field] = c.align;
|
|
122
|
+
const header = {};
|
|
123
|
+
for (const c of chosen)
|
|
124
|
+
header[c.field] = c.header;
|
|
125
|
+
const records = [header];
|
|
126
|
+
for (const row of dataRows) {
|
|
127
|
+
const rec = {};
|
|
128
|
+
for (const c of chosen) {
|
|
129
|
+
const raw = row[c.field];
|
|
130
|
+
rec[c.field] = opts.rawValues ? raw : formatValueForExport(raw, c.format);
|
|
131
|
+
}
|
|
132
|
+
records.push(rec);
|
|
133
|
+
}
|
|
134
|
+
return { records, fields, align };
|
|
135
|
+
}
|
|
136
|
+
/** A tiny currency-code → symbol map for the Excel numFmt code. Falls back
|
|
137
|
+
* to the ISO code itself (e.g. "CHF") when we don't have a glyph. */
|
|
138
|
+
function currencySymbol(code) {
|
|
139
|
+
const map = {
|
|
140
|
+
USD: '$',
|
|
141
|
+
EUR: '€',
|
|
142
|
+
GBP: '£',
|
|
143
|
+
JPY: '¥',
|
|
144
|
+
CNY: '¥',
|
|
145
|
+
INR: '₹',
|
|
146
|
+
KRW: '₩',
|
|
147
|
+
RUB: '₽',
|
|
148
|
+
BRL: 'R$',
|
|
149
|
+
};
|
|
150
|
+
return map[code.toUpperCase()] ?? code.toUpperCase();
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* The value to write into a native xlsx cell that carries an Excel numFmt.
|
|
154
|
+
* Numbers stay numbers; percent values are divided to the 0..1 fraction Excel
|
|
155
|
+
* expects (unless the config already stores 0..1); dates become real `Date`s.
|
|
156
|
+
* Returns `{ ok: false }` when the value can't be represented natively and the
|
|
157
|
+
* caller should fall back to the formatted string.
|
|
158
|
+
*/
|
|
159
|
+
export function valueForExcel(value, format) {
|
|
160
|
+
if (value == null || !format)
|
|
161
|
+
return { ok: false };
|
|
162
|
+
if (format.type === 'number' || format.type === 'currency') {
|
|
163
|
+
const n = typeof value === 'number' ? value : Number(value);
|
|
164
|
+
return Number.isFinite(n) ? { ok: true, value: n } : { ok: false };
|
|
165
|
+
}
|
|
166
|
+
if (format.type === 'percent') {
|
|
167
|
+
const n = typeof value === 'number' ? value : Number(value);
|
|
168
|
+
if (!Number.isFinite(n))
|
|
169
|
+
return { ok: false };
|
|
170
|
+
// Excel's percent numFmt multiplies by 100, so it wants the fraction.
|
|
171
|
+
return { ok: true, value: format.valueIsPercentPoints === true ? n / 100 : n };
|
|
172
|
+
}
|
|
173
|
+
if (format.type === 'date' || format.type === 'datetime') {
|
|
174
|
+
const d = coerceExportDate(value);
|
|
175
|
+
return d ? { ok: true, value: d } : { ok: false };
|
|
176
|
+
}
|
|
177
|
+
return { ok: false };
|
|
178
|
+
}
|
|
179
|
+
const EXPORT_BOM = '';
|
|
180
|
+
function throwIfAborted(signal) {
|
|
181
|
+
if (signal?.aborted) {
|
|
182
|
+
throw new DOMException('Export aborted', 'AbortError');
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/** Let the event loop breathe (paint a progress bar, honour an abort). */
|
|
186
|
+
const yieldToLoop = () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
187
|
+
/** RFC-4180 quoting: wrap in double quotes and double any embedded quote
|
|
188
|
+
* when the value contains the delimiter, a quote, or a newline. */
|
|
189
|
+
function csvCell(value, delimiter) {
|
|
190
|
+
const s = value == null
|
|
191
|
+
? ''
|
|
192
|
+
: value instanceof Date
|
|
193
|
+
? value.toISOString()
|
|
194
|
+
: String(value);
|
|
195
|
+
if (s === '')
|
|
196
|
+
return s;
|
|
197
|
+
if (s.includes(delimiter) || s.includes('"') || s.includes('\n') || s.includes('\r')) {
|
|
198
|
+
return `"${s.replace(/"/g, '""')}"`;
|
|
199
|
+
}
|
|
200
|
+
return s;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Serialize projected rows to a delimited string (CSV / TSV).
|
|
204
|
+
* `rows[0]` is treated as the header row.
|
|
205
|
+
*/
|
|
206
|
+
export async function serializeDelimited(rows, fields, opts = {}) {
|
|
207
|
+
const delimiter = opts.csv?.delimiter ?? ',';
|
|
208
|
+
const eol = opts.csv?.eol ?? '\r\n';
|
|
209
|
+
const useBom = opts.csv?.bom ?? true;
|
|
210
|
+
const chunk = Math.max(1, opts.chunkRows ?? 5000);
|
|
211
|
+
const total = rows.length;
|
|
212
|
+
const parts = [];
|
|
213
|
+
for (let i = 0; i < rows.length; i++) {
|
|
214
|
+
throwIfAborted(opts.signal);
|
|
215
|
+
const row = rows[i];
|
|
216
|
+
parts.push(fields.map((f) => csvCell(row[f], delimiter)).join(delimiter));
|
|
217
|
+
if (i > 0 && i % chunk === 0) {
|
|
218
|
+
opts.onProgress?.({ phase: 'serialize', ratio: i / total, row: i, total });
|
|
219
|
+
await yieldToLoop();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
opts.onProgress?.({ phase: 'serialize', ratio: 1, row: total, total });
|
|
223
|
+
const body = parts.join(eol);
|
|
224
|
+
return (useBom ? EXPORT_BOM : '') + body;
|
|
225
|
+
}
|
|
226
|
+
const HTML_ESCAPE = {
|
|
227
|
+
'&': '&',
|
|
228
|
+
'<': '<',
|
|
229
|
+
'>': '>',
|
|
230
|
+
'"': '"',
|
|
231
|
+
"'": ''',
|
|
232
|
+
};
|
|
233
|
+
function htmlCell(value) {
|
|
234
|
+
const s = value == null ? '' : value instanceof Date ? value.toISOString() : String(value);
|
|
235
|
+
return s.replace(/[&<>"']/g, (c) => HTML_ESCAPE[c]);
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Serialize projected rows to a standalone HTML `<table>` document.
|
|
239
|
+
* `rows[0]` is the header row (rendered as `<th>`); the rest are `<td>`.
|
|
240
|
+
*/
|
|
241
|
+
export async function serializeHtml(rows, fields, opts = {}) {
|
|
242
|
+
const chunk = Math.max(1, opts.chunkRows ?? 5000);
|
|
243
|
+
const total = rows.length;
|
|
244
|
+
const align = opts.align ?? {};
|
|
245
|
+
const cellStyle = opts.cellStyle;
|
|
246
|
+
const cellLink = opts.cellLink;
|
|
247
|
+
const title = opts.title ?? 'Grid export';
|
|
248
|
+
const head = rows[0];
|
|
249
|
+
const th = head
|
|
250
|
+
? fields.map((f) => `<th>${htmlCell(head[f])}</th>`).join('')
|
|
251
|
+
: '';
|
|
252
|
+
const bodyParts = [];
|
|
253
|
+
for (let i = 1; i < rows.length; i++) {
|
|
254
|
+
throwIfAborted(opts.signal);
|
|
255
|
+
const row = rows[i];
|
|
256
|
+
const tds = fields
|
|
257
|
+
.map((f, ci) => {
|
|
258
|
+
const a = align[f];
|
|
259
|
+
const styles = [];
|
|
260
|
+
if (a && a !== 'left')
|
|
261
|
+
styles.push(`text-align:${a}`);
|
|
262
|
+
const cf = cellStyle?.(i - 1, ci);
|
|
263
|
+
if (cf?.fill)
|
|
264
|
+
styles.push(`background:${cf.fill}`);
|
|
265
|
+
if (cf?.color)
|
|
266
|
+
styles.push(`color:${cf.color}`);
|
|
267
|
+
if (cf?.bold)
|
|
268
|
+
styles.push('font-weight:700');
|
|
269
|
+
const style = styles.length ? ` style="${styles.join(';')}"` : '';
|
|
270
|
+
let text = cf?.icon ? `${cf.icon} ${htmlCell(row[f])}` : htmlCell(row[f]);
|
|
271
|
+
const href = cellLink?.(i - 1, ci);
|
|
272
|
+
if (href)
|
|
273
|
+
text = `<a href="${htmlCell(href)}">${text}</a>`;
|
|
274
|
+
return `<td${style}>${text}</td>`;
|
|
275
|
+
})
|
|
276
|
+
.join('');
|
|
277
|
+
bodyParts.push(`<tr>${tds}</tr>`);
|
|
278
|
+
if (i > 0 && i % chunk === 0) {
|
|
279
|
+
opts.onProgress?.({ phase: 'serialize', ratio: i / total, row: i, total });
|
|
280
|
+
await yieldToLoop();
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
opts.onProgress?.({ phase: 'serialize', ratio: 1, row: total, total });
|
|
284
|
+
return (`<!doctype html><html><head><meta charset="utf-8"><title>${htmlCell(title)}</title>` +
|
|
285
|
+
`<style>table{border-collapse:collapse;font-family:system-ui,sans-serif;font-size:13px}` +
|
|
286
|
+
`th,td{border:1px solid #d0d7de;padding:6px 10px}th{background:#f6f8fa;text-align:left}</style>` +
|
|
287
|
+
`</head><body><table><thead><tr>${th}</tr></thead><tbody>${bodyParts.join('')}</tbody></table></body></html>`);
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Serialize projected rows to a JSON array of `{ field: value }` objects.
|
|
291
|
+
* `rows[0]` (the header row) is dropped - JSON keys are the field names.
|
|
292
|
+
*/
|
|
293
|
+
export async function serializeJson(rows, fields, opts = {}) {
|
|
294
|
+
const chunk = Math.max(1, opts.chunkRows ?? 5000);
|
|
295
|
+
const total = rows.length - 1;
|
|
296
|
+
const out = [];
|
|
297
|
+
for (let i = 1; i < rows.length; i++) {
|
|
298
|
+
throwIfAborted(opts.signal);
|
|
299
|
+
const src = rows[i];
|
|
300
|
+
const obj = {};
|
|
301
|
+
for (const f of fields)
|
|
302
|
+
obj[f] = src[f];
|
|
303
|
+
out.push(obj);
|
|
304
|
+
if (i > 0 && i % chunk === 0) {
|
|
305
|
+
opts.onProgress?.({ phase: 'serialize', ratio: i / total, row: i, total });
|
|
306
|
+
await yieldToLoop();
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
opts.onProgress?.({ phase: 'serialize', ratio: 1, row: total, total });
|
|
310
|
+
return JSON.stringify(out, null, 2);
|
|
311
|
+
}
|
|
312
|
+
/** Escape a Markdown table cell: pipes and newlines would break the row. */
|
|
313
|
+
function mdCell(value) {
|
|
314
|
+
const s = value == null ? '' : value instanceof Date ? value.toISOString() : String(value);
|
|
315
|
+
return s.replace(/\\/g, '\\\\').replace(/\|/g, '\\|').replace(/\r?\n/g, '<br>');
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Serialize projected rows to a GitHub-flavored Markdown table. `rows[0]` is
|
|
319
|
+
* the header row; `align` sets the `:---`, `:--:`, `---:` markers per column.
|
|
320
|
+
*/
|
|
321
|
+
export async function serializeMarkdown(rows, fields, opts = {}) {
|
|
322
|
+
const chunk = Math.max(1, opts.chunkRows ?? 5000);
|
|
323
|
+
const total = rows.length;
|
|
324
|
+
const align = opts.align ?? {};
|
|
325
|
+
const head = rows[0];
|
|
326
|
+
const headerLine = `| ${fields.map((f) => mdCell(head?.[f])).join(' | ')} |`;
|
|
327
|
+
const sepLine = `| ${fields
|
|
328
|
+
.map((f) => {
|
|
329
|
+
const a = align[f];
|
|
330
|
+
return a === 'right' ? '---:' : a === 'center' ? ':--:' : '---';
|
|
331
|
+
})
|
|
332
|
+
.join(' | ')} |`;
|
|
333
|
+
const bodyLines = [];
|
|
334
|
+
for (let i = 1; i < rows.length; i++) {
|
|
335
|
+
throwIfAborted(opts.signal);
|
|
336
|
+
const src = rows[i];
|
|
337
|
+
bodyLines.push(`| ${fields.map((f) => mdCell(src[f])).join(' | ')} |`);
|
|
338
|
+
if (i > 0 && i % chunk === 0) {
|
|
339
|
+
opts.onProgress?.({ phase: 'serialize', ratio: i / total, row: i, total });
|
|
340
|
+
await yieldToLoop();
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
opts.onProgress?.({ phase: 'serialize', ratio: 1, row: total, total });
|
|
344
|
+
return [headerLine, sepLine, ...bodyLines].join('\n') + '\n';
|
|
345
|
+
}
|
|
346
|
+
const XML_ESCAPE = {
|
|
347
|
+
'&': '&',
|
|
348
|
+
'<': '<',
|
|
349
|
+
'>': '>',
|
|
350
|
+
'"': '"',
|
|
351
|
+
"'": ''',
|
|
352
|
+
};
|
|
353
|
+
function xmlText(value) {
|
|
354
|
+
const s = value == null ? '' : value instanceof Date ? value.toISOString() : String(value);
|
|
355
|
+
return s.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, '').replace(/[&<>"']/g, (c) => XML_ESCAPE[c]);
|
|
356
|
+
}
|
|
357
|
+
/** Coerce a field name into a valid XML element name. */
|
|
358
|
+
function xmlTag(field) {
|
|
359
|
+
const t = field.replace(/[^A-Za-z0-9_.-]/g, '_');
|
|
360
|
+
return /^[A-Za-z_]/.test(t) ? t : `_${t}`;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Serialize projected rows to a simple XML document:
|
|
364
|
+
* `<rows><row><field>value</field>…</row>…</rows>`. `rows[0]` (header) is
|
|
365
|
+
* dropped - element names come from the field names.
|
|
366
|
+
*/
|
|
367
|
+
export async function serializeXml(rows, fields, opts = {}) {
|
|
368
|
+
const chunk = Math.max(1, opts.chunkRows ?? 5000);
|
|
369
|
+
const total = rows.length - 1;
|
|
370
|
+
const root = opts.rootTag ?? 'rows';
|
|
371
|
+
const rowTag = opts.rowTag ?? 'row';
|
|
372
|
+
const tags = fields.map(xmlTag);
|
|
373
|
+
const parts = [`<?xml version="1.0" encoding="UTF-8"?>`, `<${root}>`];
|
|
374
|
+
for (let i = 1; i < rows.length; i++) {
|
|
375
|
+
throwIfAborted(opts.signal);
|
|
376
|
+
const src = rows[i];
|
|
377
|
+
const cells = fields
|
|
378
|
+
.map((f, ci) => `<${tags[ci]}>${xmlText(src[f])}</${tags[ci]}>`)
|
|
379
|
+
.join('');
|
|
380
|
+
parts.push(` <${rowTag}>${cells}</${rowTag}>`);
|
|
381
|
+
if (i > 0 && i % chunk === 0) {
|
|
382
|
+
opts.onProgress?.({ phase: 'serialize', ratio: i / total, row: i, total });
|
|
383
|
+
await yieldToLoop();
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
parts.push(`</${root}>`);
|
|
387
|
+
opts.onProgress?.({ phase: 'serialize', ratio: 1, row: total, total });
|
|
388
|
+
return parts.join('\n');
|
|
389
|
+
}
|
|
390
|
+
// ----- Delivery: download + clipboard (browser-guarded) --------------------
|
|
391
|
+
/** Trigger a browser download of an already-built Blob. No-op guard for SSR. */
|
|
392
|
+
export function downloadBlobFile(blob, filename) {
|
|
393
|
+
if (typeof document === 'undefined' || typeof URL === 'undefined') {
|
|
394
|
+
throw new Error('@svgrid/grid: export requires a browser environment');
|
|
395
|
+
}
|
|
396
|
+
const url = URL.createObjectURL(blob);
|
|
397
|
+
const a = document.createElement('a');
|
|
398
|
+
a.href = url;
|
|
399
|
+
a.download = filename;
|
|
400
|
+
document.body.appendChild(a);
|
|
401
|
+
a.click();
|
|
402
|
+
a.remove();
|
|
403
|
+
setTimeout(() => URL.revokeObjectURL(url), 0);
|
|
404
|
+
}
|
|
405
|
+
/** Trigger a browser download of a text blob. No-op guard for SSR. */
|
|
406
|
+
export function downloadTextFile(text, filename, mime) {
|
|
407
|
+
if (typeof document === 'undefined' || typeof URL === 'undefined') {
|
|
408
|
+
throw new Error('@svgrid/grid: text export requires a browser environment');
|
|
409
|
+
}
|
|
410
|
+
downloadBlobFile(new Blob([text], { type: mime }), filename);
|
|
411
|
+
}
|
|
412
|
+
/** Write text to the system clipboard. Throws when the API is unavailable
|
|
413
|
+
* (insecure context / SSR). */
|
|
414
|
+
export async function copyTextToClipboard(text) {
|
|
415
|
+
if (typeof navigator === 'undefined' || !navigator.clipboard) {
|
|
416
|
+
throw new Error('@svgrid/grid: clipboard API is unavailable in this context');
|
|
417
|
+
}
|
|
418
|
+
await navigator.clipboard.writeText(text);
|
|
419
|
+
}
|
|
@@ -43,6 +43,10 @@ export type HyperFormulaInstance = {
|
|
|
43
43
|
row: number;
|
|
44
44
|
col: number;
|
|
45
45
|
}): unknown;
|
|
46
|
+
/** Bulk-load an entire sheet in one call (one recalc instead of one per
|
|
47
|
+
* cell). Optional so minimal / mock instances still satisfy the type;
|
|
48
|
+
* the adapter falls back to per-cell `setCellContents` when it's absent. */
|
|
49
|
+
setSheetContent?(sheetId: number, values: unknown[][]): unknown;
|
|
46
50
|
destroy(): void;
|
|
47
51
|
rebuildAndRecalculate(): void;
|
|
48
52
|
};
|
|
@@ -31,12 +31,21 @@ export function createHyperFormulaSheet(config) {
|
|
|
31
31
|
const colByField = new Map();
|
|
32
32
|
fields.forEach((f, i) => colByField.set(f, i));
|
|
33
33
|
// 1. Seed HF with every cell. HF expects raw strings for formulas
|
|
34
|
-
// (`'=A1+B1'`) and primitives for everything else.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
// (`'=A1+B1'`) and primitives for everything else. Prefer a single bulk
|
|
35
|
+
// `setSheetContent` write - it recalculates ONCE instead of once per
|
|
36
|
+
// setCellContents, ~3x faster to mount a large sheet (e.g. 200x26). Fall
|
|
37
|
+
// back to per-cell for instances that don't expose it.
|
|
38
|
+
if (typeof hf.setSheetContent === 'function') {
|
|
39
|
+
const grid = rows.map((row) => fields.map((f) => row[f]));
|
|
40
|
+
hf.setSheetContent(sheetId, grid);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
for (let r = 0; r < rows.length; r += 1) {
|
|
44
|
+
const row = rows[r];
|
|
45
|
+
for (let c = 0; c < fields.length; c += 1) {
|
|
46
|
+
const field = fields[c];
|
|
47
|
+
hf.setCellContents({ sheet: sheetId, row: r, col: c }, row[field]);
|
|
48
|
+
}
|
|
40
49
|
}
|
|
41
50
|
}
|
|
42
51
|
/** Pull computed values out of HF into a fresh row array. Preserves
|