report-studio 0.0.1 → 1.0.0
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/LICENSE +21 -0
- package/README.md +238 -4
- package/dist/designer.js +3209 -0
- package/dist/index.js +2 -4
- package/dist/viewer-D-c0lKzQ.js +1576 -0
- package/package.json +66 -33
- package/src/asset/images/icon.png +0 -0
- package/src/asset/images/logo.png +0 -0
- package/src/cli/index.js +282 -0
- package/src/cli/serve.js +135 -0
- package/src/designer/blank.js +53 -0
- package/src/designer/canvas.js +518 -0
- package/src/designer/designer.js +1900 -0
- package/src/designer/dialog.js +708 -0
- package/src/designer/fields.js +584 -0
- package/src/designer/geometry.js +154 -0
- package/src/designer/guides.js +370 -0
- package/src/designer/history.js +113 -0
- package/src/designer/icons.js +118 -0
- package/src/designer/index.html +28 -0
- package/src/designer/main.js +39 -0
- package/src/designer/menu.js +126 -0
- package/src/designer/panel.js +478 -0
- package/src/designer/preview.js +95 -0
- package/src/designer/rulers.js +231 -0
- package/src/designer/sample-data.js +240 -0
- package/src/designer/select.js +699 -0
- package/src/designer/structure.js +604 -0
- package/src/designer/styles.css +1616 -0
- package/src/designer/toast.js +62 -0
- package/src/designer/tokens.js +284 -0
- package/src/engine/csv.js +315 -0
- package/src/engine/group.js +304 -0
- package/src/engine/index.js +34 -0
- package/src/engine/measure.js +390 -0
- package/src/engine/paginate.js +861 -0
- package/src/engine/regions.js +175 -0
- package/src/engine/resolve.js +171 -0
- package/src/engine/search.js +129 -0
- package/src/engine/text-metrics.js +206 -0
- package/src/engine/validate.js +328 -0
- package/src/index.html +79 -0
- package/src/index.js +50 -0
- package/src/preview/chrome.css +505 -0
- package/src/preview/chrome.js +147 -0
- package/src/preview/index.html +28 -0
- package/src/preview/main.js +66 -0
- package/src/preview/styles.css +16 -0
- package/src/preview/viewer.js +745 -0
- package/src/render/items.js +425 -0
- package/src/render/render.js +98 -0
- package/src/render/report.css +202 -0
- package/src/server/routes.js +331 -0
- package/src/shared/brand.css +29 -0
- package/src/shared/dropdown.css +136 -0
- package/src/shared/dropdown.js +166 -0
- package/src/shared/modal.css +125 -0
- package/src/shared/prefix.js +13 -0
- package/src/shared/store.js +145 -0
- package/src/shared/theme.css +72 -0
- package/src/shared/window.js +138 -0
- package/src/vite/index.js +53 -0
- package/dist/react.js +0 -4
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* toast.js is the corner the designer says things from.
|
|
3
|
+
*
|
|
4
|
+
* The validator's findings used to be a banner above the page, which pushed the
|
|
5
|
+
* design down the canvas every time a report was mid-edit - and setting groupBy
|
|
6
|
+
* before switching the group band on is exactly the sort of half-finished state
|
|
7
|
+
* a designer is in most of the time. So they moved to the top right, out of the
|
|
8
|
+
* way of the thing being designed.
|
|
9
|
+
*
|
|
10
|
+
* A validation finding is a condition, not an event: it is true until the layout
|
|
11
|
+
* is fixed. So this toast does not time out - it is dismissed, and comes back
|
|
12
|
+
* when what is wrong changes. sameIssues is what decides that, and it is here
|
|
13
|
+
* rather than in designer.js because it is the whole of the rule.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { esc } from '../render/items.js';
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Whether a dismissal still covers what the validator is now saying.
|
|
21
|
+
*
|
|
22
|
+
* Order matters as much as content: validate.js reports fields in a fixed
|
|
23
|
+
* order, so a change in order is a change in what is wrong.
|
|
24
|
+
*
|
|
25
|
+
* @param {string[]|null} issues
|
|
26
|
+
* @param {string[]|null} dismissed
|
|
27
|
+
* @returns {boolean}
|
|
28
|
+
*/
|
|
29
|
+
export function sameIssues(issues, dismissed) {
|
|
30
|
+
if (!Array.isArray(issues) || !Array.isArray(dismissed)) return false;
|
|
31
|
+
if (issues.length !== dismissed.length) return false;
|
|
32
|
+
|
|
33
|
+
return issues.every((line, i) => line === dismissed[i]);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The problems toast.
|
|
39
|
+
*
|
|
40
|
+
* role="alert" rather than a live region on the container: the container is
|
|
41
|
+
* always in the document, and a screen reader should hear this when it arrives
|
|
42
|
+
* rather than be told a region exists.
|
|
43
|
+
*
|
|
44
|
+
* @param {string[]} issues what validateLayout returned
|
|
45
|
+
* @returns {string} markup
|
|
46
|
+
*/
|
|
47
|
+
export function drawProblems(issues) {
|
|
48
|
+
const many = issues.length > 1;
|
|
49
|
+
|
|
50
|
+
return `
|
|
51
|
+
<div class="dz-toast dz-problems" role="alert" data-role="problems">
|
|
52
|
+
<div class="dz-toast-text">
|
|
53
|
+
<strong class="dz-toast-title">This report will not render yet</strong>
|
|
54
|
+
<ul>${issues.map(one => `<li>${esc(one)}</li>`).join('')}</ul>
|
|
55
|
+
</div>
|
|
56
|
+
<button type="button" class="dz-toast-close"
|
|
57
|
+
data-action="dismiss-problems"
|
|
58
|
+
title="Dismiss${many ? ` ${issues.length} problems` : ''}"
|
|
59
|
+
aria-label="Dismiss${many ? ` ${issues.length} problems` : ''}"
|
|
60
|
+
>×</button>
|
|
61
|
+
</div>`;
|
|
62
|
+
}
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tokens.js is the list of things a report can print that are not in the data:
|
|
3
|
+
* the page number, the date, and the aggregates over a group.
|
|
4
|
+
*
|
|
5
|
+
* They exist because the placeholder syntax in spec 3.4 is written down in a
|
|
6
|
+
* document and nowhere else. Someone designing a footer has to know that the
|
|
7
|
+
* page count is `{totalPages}` and not `{pages}`, that a row count takes empty
|
|
8
|
+
* brackets, and that `{sum(amount)}` needs the field name inside them. Getting
|
|
9
|
+
* any of those slightly wrong produces an empty string and a console warning,
|
|
10
|
+
* which is a poor way to find out.
|
|
11
|
+
*
|
|
12
|
+
* The rules about *where* each one resolves come from the engine's own
|
|
13
|
+
* behaviour. An aggregate now resolves in every band (group.js); what changes
|
|
14
|
+
* with the band is the rows it is worked out over - one group's in a group
|
|
15
|
+
* band, the whole report's anywhere else. That is worth saying before it is
|
|
16
|
+
* inserted rather than being discovered from a number that is not the one that
|
|
17
|
+
* was expected.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { datasets } from './sample-data.js';
|
|
21
|
+
import { BAND_TYPES, hasBand, findBand } from './structure.js';
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
/** the placeholder that stands in for the field until one is chosen */
|
|
25
|
+
const FIELD = 'field';
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Every insertable token.
|
|
30
|
+
*
|
|
31
|
+
* `bands` is where it resolves, and an empty list means anywhere. `field` marks
|
|
32
|
+
* the ones that take a field name inside the brackets.
|
|
33
|
+
*/
|
|
34
|
+
export const TOKENS = [
|
|
35
|
+
{
|
|
36
|
+
id: 'page',
|
|
37
|
+
prefer: ['pageFooter', 'reportFooter', 'pageHeader'],
|
|
38
|
+
label: 'Page number',
|
|
39
|
+
value: '{page}',
|
|
40
|
+
note: 'The page this is printed on'
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
id: 'page-of',
|
|
44
|
+
prefer: ['pageFooter', 'reportFooter', 'pageHeader'],
|
|
45
|
+
label: 'Page X of Y',
|
|
46
|
+
value: 'Page {page} of {totalPages}',
|
|
47
|
+
note: 'The usual footer line, written out'
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
id: 'total-pages',
|
|
51
|
+
prefer: ['pageFooter', 'reportFooter', 'pageHeader'],
|
|
52
|
+
label: 'Total pages',
|
|
53
|
+
value: '{totalPages}',
|
|
54
|
+
note: 'How many pages the report came to'
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
id: 'today',
|
|
58
|
+
prefer: ['pageHeader', 'reportHeader', 'pageFooter'],
|
|
59
|
+
label: 'Today',
|
|
60
|
+
value: '{today}',
|
|
61
|
+
note: 'The date the report was run'
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
id: 'count',
|
|
65
|
+
prefer: ['groupFooter', 'reportFooter'],
|
|
66
|
+
label: 'Row count',
|
|
67
|
+
value: '{count()}',
|
|
68
|
+
note: 'How many rows are in scope',
|
|
69
|
+
aggregate: true
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
id: 'sum',
|
|
73
|
+
prefer: ['groupFooter', 'reportFooter'],
|
|
74
|
+
label: 'Sum',
|
|
75
|
+
value: `{sum(${FIELD})}`,
|
|
76
|
+
note: 'Added up over the rows in scope',
|
|
77
|
+
field: true,
|
|
78
|
+
aggregate: true
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
id: 'avg',
|
|
82
|
+
prefer: ['groupFooter', 'reportFooter'],
|
|
83
|
+
label: 'Average',
|
|
84
|
+
value: `{avg(${FIELD})}`,
|
|
85
|
+
note: 'The mean of the rows in scope',
|
|
86
|
+
field: true,
|
|
87
|
+
aggregate: true
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
id: 'min',
|
|
91
|
+
prefer: ['groupFooter', 'reportFooter'],
|
|
92
|
+
label: 'Smallest',
|
|
93
|
+
value: `{min(${FIELD})}`,
|
|
94
|
+
field: true,
|
|
95
|
+
aggregate: true
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
id: 'max',
|
|
99
|
+
prefer: ['groupFooter', 'reportFooter'],
|
|
100
|
+
label: 'Largest',
|
|
101
|
+
value: `{max(${FIELD})}`,
|
|
102
|
+
field: true,
|
|
103
|
+
aggregate: true
|
|
104
|
+
}
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
export function findToken(id) {
|
|
109
|
+
return TOKENS.find(t => t.id === id) ?? null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
/** whether a token has to be told which field to work over */
|
|
114
|
+
export function needsField(token) {
|
|
115
|
+
return Boolean(token?.field);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
/** the two group bands, whose aggregates are one group's rather than the report's */
|
|
120
|
+
const GROUP_SCOPED = ['groupHeader', 'groupFooter'];
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The rows an aggregate would be worked out over if it went in this band.
|
|
125
|
+
*
|
|
126
|
+
* The reason the dialog says anything at all: `{sum(price)}` is a different
|
|
127
|
+
* number in a group footer and in a report footer, and both are correct. Which
|
|
128
|
+
* one someone is asking for is not something they can be left to find out from
|
|
129
|
+
* the printed report.
|
|
130
|
+
*
|
|
131
|
+
* @param {object} token
|
|
132
|
+
* @param {string} bandType
|
|
133
|
+
* @returns {'group'|'report'|null} null for a token that is not an aggregate
|
|
134
|
+
*/
|
|
135
|
+
export function aggregateScope(token, bandType) {
|
|
136
|
+
if (!token?.aggregate) return null;
|
|
137
|
+
|
|
138
|
+
return GROUP_SCOPED.includes(bandType) ? 'group' : 'report';
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Where a token prints something.
|
|
144
|
+
*
|
|
145
|
+
* @returns {boolean} true when a token with no band restriction is asked about
|
|
146
|
+
* any band at all - `{page}` is valid everywhere (spec 3.4)
|
|
147
|
+
*/
|
|
148
|
+
export function resolvesIn(token, bandType) {
|
|
149
|
+
if (!token) return false;
|
|
150
|
+
if (!token.bands?.length) return true;
|
|
151
|
+
|
|
152
|
+
return token.bands.includes(bandType);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The token's text, with the field filled in.
|
|
158
|
+
*
|
|
159
|
+
* @param {object} token
|
|
160
|
+
* @param {string} [field]
|
|
161
|
+
* @returns {string}
|
|
162
|
+
*/
|
|
163
|
+
export function tokenValue(token, field) {
|
|
164
|
+
if (!token) return '';
|
|
165
|
+
if (!needsField(token)) return token.value;
|
|
166
|
+
|
|
167
|
+
return token.value.replace(FIELD, String(field || FIELD));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Every field name the report already knows about, for the field picker.
|
|
173
|
+
*
|
|
174
|
+
* Read off the layout rather than typed: the columns are already there, and
|
|
175
|
+
* offering a list beats hoping someone spells `amount` the same way twice.
|
|
176
|
+
*
|
|
177
|
+
* @param {object} layout
|
|
178
|
+
* @returns {string[]} sorted, de-duplicated
|
|
179
|
+
*/
|
|
180
|
+
export function availableFields(layout) {
|
|
181
|
+
const found = new Set();
|
|
182
|
+
|
|
183
|
+
for (const [, fields] of datasets(layout)) {
|
|
184
|
+
for (const field of fields) found.add(field);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (layout?.groupBy) found.add(layout.groupBy);
|
|
188
|
+
|
|
189
|
+
return [...found].sort();
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Which bands a token may sensibly be put on, given the report as it stands.
|
|
195
|
+
*
|
|
196
|
+
* Only bands the report actually has - offering to insert into a band that does
|
|
197
|
+
* not exist would be offering to create one as a side effect of inserting a
|
|
198
|
+
* page number.
|
|
199
|
+
*
|
|
200
|
+
* @param {object} layout
|
|
201
|
+
* @param {object} token
|
|
202
|
+
* @returns {string[]} band types, in page order
|
|
203
|
+
*/
|
|
204
|
+
export function bandChoices(layout, token) {
|
|
205
|
+
const present = BAND_TYPES.filter(type => hasBand(layout, type));
|
|
206
|
+
|
|
207
|
+
if (!token?.bands?.length) return present;
|
|
208
|
+
|
|
209
|
+
const allowed = present.filter(type => token.bands.includes(type));
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* A report with no footer band gets the full list rather than an empty one,
|
|
213
|
+
* so the dialog can say what is wrong instead of offering nothing and
|
|
214
|
+
* leaving the reason to be guessed.
|
|
215
|
+
*/
|
|
216
|
+
return allowed.length ? allowed : present;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Where a token should go unless told otherwise.
|
|
222
|
+
*
|
|
223
|
+
* An aggregate belongs in a footer and a page number belongs in the page
|
|
224
|
+
* footer, so those are offered first - not because anywhere else is forbidden,
|
|
225
|
+
* but because it is where they are almost always wanted.
|
|
226
|
+
*
|
|
227
|
+
* @param {object} layout
|
|
228
|
+
* @param {object} token
|
|
229
|
+
* @param {string|null} [current] the band the selection is on
|
|
230
|
+
* @returns {string|null}
|
|
231
|
+
*/
|
|
232
|
+
export function defaultBand(layout, token, current = null) {
|
|
233
|
+
const choices = bandChoices(layout, token);
|
|
234
|
+
if (!choices.length) return null;
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* The token's own home wins, and the band being worked on is only a
|
|
238
|
+
* fallback.
|
|
239
|
+
*
|
|
240
|
+
* It used to be the other way round, and it was wrong: `{page}` resolves in
|
|
241
|
+
* any band, so having a table selected in the detail band made the detail
|
|
242
|
+
* band "valid", and a page number inserted while looking at the rows landed
|
|
243
|
+
* among them. Where a page number goes is not a matter of what happened to
|
|
244
|
+
* be selected at the time.
|
|
245
|
+
*/
|
|
246
|
+
for (const type of (token?.prefer ?? [])) {
|
|
247
|
+
if (choices.includes(type)) return type;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (current && choices.includes(current) && resolvesIn(token, current)) {
|
|
251
|
+
return current;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return choices[0];
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* A text item carrying the token.
|
|
260
|
+
*
|
|
261
|
+
* Right-aligned when it prints a number, because that is where a total belongs
|
|
262
|
+
* and moving every one of them by hand afterwards is the sort of thing a
|
|
263
|
+
* designer should have done for you.
|
|
264
|
+
*
|
|
265
|
+
* @param {object} token
|
|
266
|
+
* @param {string} [field]
|
|
267
|
+
* @returns {object} the parts to merge onto a new text item
|
|
268
|
+
*/
|
|
269
|
+
export function tokenItem(token, field) {
|
|
270
|
+
const numeric = needsField(token) || token?.id === 'count'
|
|
271
|
+
|| token?.id === 'page' || token?.id === 'total-pages';
|
|
272
|
+
|
|
273
|
+
return {
|
|
274
|
+
value: tokenValue(token, field),
|
|
275
|
+
w: token?.id === 'page-of' ? 200 : 120,
|
|
276
|
+
align: numeric ? 'right' : 'left'
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
/** the band an insert would land on, for the dialog to name */
|
|
282
|
+
export function bandLabel(layout, type) {
|
|
283
|
+
return findBand(layout, type) ? type : null;
|
|
284
|
+
}
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* csv.js writes a report out as CSV, in two shapes.
|
|
3
|
+
*
|
|
4
|
+
* toCSV is the data rectangle: the table's rows and nothing else, one header
|
|
5
|
+
* row and uniform columns, which is what a spreadsheet needs to sort, filter
|
|
6
|
+
* and pivot. Rows split across a page break come back in one piece.
|
|
7
|
+
*
|
|
8
|
+
* toReportCSV is the report as it reads on paper: every text line and every
|
|
9
|
+
* table row, in the order the page draws them. Lines and boxes have no text to
|
|
10
|
+
* give, so they are the two item types that cannot come along.
|
|
11
|
+
*
|
|
12
|
+
* Spec 5.1: "extract table rows from the page list, download as a file", and
|
|
13
|
+
* spec 2.2 lists CSV export as a page-list consumer. So both read the built
|
|
14
|
+
* pages - the same thing the renderer draws - and never the layout or the raw
|
|
15
|
+
* data.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
/** cells starting with these are executed as formulas by Excel and Sheets */
|
|
20
|
+
const FORMULA_LEAD = /^[=+\-@\t\r]/;
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The report's table as CSV text.
|
|
25
|
+
*
|
|
26
|
+
* @param {object[]} pages the `pages` array from buildPages
|
|
27
|
+
* @param {object} [options]
|
|
28
|
+
* @param {string} [options.delimiter=','] use ';' for locales where Excel expects it
|
|
29
|
+
* @param {string} [options.newline='\r\n'] RFC 4180 line ending
|
|
30
|
+
* @param {boolean} [options.header=true] emit the column labels as the first row
|
|
31
|
+
* @param {boolean} [options.groupColumn=true] prepend the group key on a grouped report
|
|
32
|
+
* @param {string} [options.groupLabel='Group']
|
|
33
|
+
* @param {boolean} [options.neutraliseFormulas=true] see below
|
|
34
|
+
* @returns {string} CSV text, empty when the report holds no table
|
|
35
|
+
*/
|
|
36
|
+
export function toCSV(pages, options = {}) {
|
|
37
|
+
const {
|
|
38
|
+
delimiter = ',',
|
|
39
|
+
newline = '\r\n',
|
|
40
|
+
header = true,
|
|
41
|
+
groupColumn = true,
|
|
42
|
+
groupLabel = 'Group',
|
|
43
|
+
neutraliseFormulas = true
|
|
44
|
+
} = options;
|
|
45
|
+
|
|
46
|
+
const table = collect(pages);
|
|
47
|
+
if (!table) return '';
|
|
48
|
+
|
|
49
|
+
const grouped = groupColumn && table.rows.some(r => r.group !== null);
|
|
50
|
+
const labels = table.columns.map(c => c.label ?? c.field);
|
|
51
|
+
|
|
52
|
+
const lines = [];
|
|
53
|
+
|
|
54
|
+
if (header) {
|
|
55
|
+
lines.push(encodeRow(grouped ? [groupLabel, ...labels] : labels,
|
|
56
|
+
delimiter, neutraliseFormulas));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
for (const { group, row } of table.rows) {
|
|
60
|
+
const cells = table.columns.map(c => row?.[c.field]);
|
|
61
|
+
lines.push(encodeRow(grouped ? [group, ...cells] : cells,
|
|
62
|
+
delimiter, neutraliseFormulas));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return lines.join(newline);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The whole report as CSV, in the order it is read.
|
|
71
|
+
*
|
|
72
|
+
* One CSV row per line of the report: text items that share a line become the
|
|
73
|
+
* cells of one row, and a table contributes its column labels and then its
|
|
74
|
+
* rows, where the table sits. So the file re-reads as the report does, at the
|
|
75
|
+
* cost of the rectangle toCSV keeps - the rows are ragged, because the report
|
|
76
|
+
* is.
|
|
77
|
+
*
|
|
78
|
+
* Boxes and lines are left out. They are drawn rather than written, and a CSV
|
|
79
|
+
* has nowhere to put a rule or a filled panel.
|
|
80
|
+
*
|
|
81
|
+
* Nothing is gathered across pages the way toCSV gathers table rows: a page
|
|
82
|
+
* header drawn on all three pages is written three times, because that is what
|
|
83
|
+
* the report has. The order is the paper's, not the layout array's - bands are
|
|
84
|
+
* anchored to a zone, so `top` is what says which is read first.
|
|
85
|
+
*
|
|
86
|
+
* @param {object[]} pages the `pages` array from buildPages
|
|
87
|
+
* @param {object} [options]
|
|
88
|
+
* @param {string} [options.delimiter=',']
|
|
89
|
+
* @param {string} [options.newline='\r\n'] RFC 4180 line ending
|
|
90
|
+
* @param {boolean} [options.neutraliseFormulas=true] as toCSV
|
|
91
|
+
* @returns {string} CSV text, empty when the report writes nothing
|
|
92
|
+
*/
|
|
93
|
+
export function toReportCSV(pages, options = {}) {
|
|
94
|
+
const {
|
|
95
|
+
delimiter = ',',
|
|
96
|
+
newline = '\r\n',
|
|
97
|
+
neutraliseFormulas = true
|
|
98
|
+
} = options;
|
|
99
|
+
|
|
100
|
+
const out = [];
|
|
101
|
+
|
|
102
|
+
for (const page of (pages ?? [])) {
|
|
103
|
+
for (const band of readingOrder(page?.bands)) collectBand(band, out);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (out.length === 0) return '';
|
|
107
|
+
|
|
108
|
+
return out
|
|
109
|
+
.map(cells => encodeRow(cells, delimiter, neutraliseFormulas))
|
|
110
|
+
.join(newline);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* A page's bands, top of the paper first.
|
|
116
|
+
*
|
|
117
|
+
* The array they arrive in is the order they were built, which is not the order
|
|
118
|
+
* they are read: a page footer is anchored to the bottom edge and is often
|
|
119
|
+
* second in the list. `top` is the position the renderer places them at, so it
|
|
120
|
+
* is the only thing that knows.
|
|
121
|
+
*/
|
|
122
|
+
function readingOrder(bands) {
|
|
123
|
+
return [...(bands ?? [])].sort((a, b) => (a?.top ?? 0) - (b?.top ?? 0));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Adds a band's lines to the output.
|
|
129
|
+
*
|
|
130
|
+
* Items are sorted down the band and then across it, and neighbours that share
|
|
131
|
+
* a line are written as one row - so a caption and the figure beside it arrive
|
|
132
|
+
* as two cells rather than as two rows, which is how the band was designed to
|
|
133
|
+
* be read.
|
|
134
|
+
*
|
|
135
|
+
* @param {object} band
|
|
136
|
+
* @param {Array[]} out gathered rows, appended to
|
|
137
|
+
*/
|
|
138
|
+
function collectBand(band, out) {
|
|
139
|
+
const drawn = (band?.items ?? [])
|
|
140
|
+
.filter(item => item?.type === 'text' || item?.type === 'table')
|
|
141
|
+
.sort((a, b) => (a.y ?? 0) - (b.y ?? 0) || (a.x ?? 0) - (b.x ?? 0));
|
|
142
|
+
|
|
143
|
+
/** the run of text items being gathered, and the item that anchors its line */
|
|
144
|
+
let line = null;
|
|
145
|
+
const flush = () => {
|
|
146
|
+
if (line) out.push(line.cells);
|
|
147
|
+
line = null;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
for (const item of drawn) {
|
|
151
|
+
if (item.type === 'table') {
|
|
152
|
+
flush();
|
|
153
|
+
collectTable(item, out);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (line && sameLine(line.anchor, item)) {
|
|
158
|
+
line.cells.push(item.text);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
flush();
|
|
163
|
+
line = { anchor: item, cells: [item.text] };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
flush();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Whether two items are on the same line of the band.
|
|
172
|
+
*
|
|
173
|
+
* By overlap rather than by an equal y: a heading and the date beside it are
|
|
174
|
+
* set in different sizes, so they are placed a few pixels apart and still read
|
|
175
|
+
* as one line. Half the shorter item is the threshold - enough that a genuine
|
|
176
|
+
* second line, which clears the first, is never folded into it.
|
|
177
|
+
*
|
|
178
|
+
* Measured against the item that opened the line rather than against the line
|
|
179
|
+
* as it grows, so one tall item cannot go on absorbing everything under it.
|
|
180
|
+
*/
|
|
181
|
+
function sameLine(anchor, item) {
|
|
182
|
+
const span = (i) => {
|
|
183
|
+
const top = i.y ?? 0;
|
|
184
|
+
return [top, top + (i.measuredHeight ?? i.h ?? 0)];
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const [aTop, aBottom] = span(anchor);
|
|
188
|
+
const [bTop, bBottom] = span(item);
|
|
189
|
+
const overlap = Math.min(aBottom, bBottom) - Math.max(aTop, bTop);
|
|
190
|
+
|
|
191
|
+
return overlap >= Math.min(aBottom - aTop, bBottom - bTop) / 2;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Adds a table's rows to the output, in the order items.js draws them.
|
|
197
|
+
*
|
|
198
|
+
* The grouped shape repeats the column labels for every group that starts one,
|
|
199
|
+
* and wraps each fragment's rows in the group's own header and footer bands -
|
|
200
|
+
* which are bands, so they are written by the same code as any other.
|
|
201
|
+
*
|
|
202
|
+
* @param {object} table
|
|
203
|
+
* @param {Array[]} out
|
|
204
|
+
*/
|
|
205
|
+
function collectTable(table, out) {
|
|
206
|
+
const labels = () => table.columns.map(c => c.label ?? c.field);
|
|
207
|
+
const cells = (row) => table.columns.map(c => row?.[c.field]);
|
|
208
|
+
|
|
209
|
+
if (!table.groups) {
|
|
210
|
+
if (table.showHeader) out.push(labels());
|
|
211
|
+
for (const row of (table.row ?? [])) out.push(cells(row));
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
for (const fragment of table.groups) {
|
|
216
|
+
if (fragment.showHeader && table.showHeader) out.push(labels());
|
|
217
|
+
if (fragment.headerBand) collectBand(fragment.headerBand, out);
|
|
218
|
+
|
|
219
|
+
for (const row of fragment.rows) out.push(cells(row));
|
|
220
|
+
|
|
221
|
+
if (fragment.showFooter && fragment.footerBand) {
|
|
222
|
+
collectBand(fragment.footerBand, out);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* A filename for the download, derived from the layout's name.
|
|
230
|
+
* @param {object} paginated the buildPages result
|
|
231
|
+
* @param {string} [extension='csv']
|
|
232
|
+
* @returns {string}
|
|
233
|
+
*/
|
|
234
|
+
export function reportFilename(paginated, extension = 'csv') {
|
|
235
|
+
const name = String(paginated?.name ?? 'report')
|
|
236
|
+
.replace(/[\\/:*?"<>|]+/g, '-')
|
|
237
|
+
.replace(/\s+/g, ' ')
|
|
238
|
+
.trim() || 'report';
|
|
239
|
+
|
|
240
|
+
return `${name}.${extension}`;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Walks the page list for the first table, gathering its rows across every page
|
|
246
|
+
* and every group fragment. One table per report today; if that ever changes,
|
|
247
|
+
* this is the single place that has to decide what a multi-table export means.
|
|
248
|
+
* @param {object[]} pages
|
|
249
|
+
* @returns {{columns: object[], rows: {group: string|null, row: object}[]}|null}
|
|
250
|
+
*/
|
|
251
|
+
function collect(pages) {
|
|
252
|
+
let columns = null;
|
|
253
|
+
let id = null;
|
|
254
|
+
const rows = [];
|
|
255
|
+
|
|
256
|
+
for (const page of (pages ?? [])) {
|
|
257
|
+
for (const band of (page.bands ?? [])) {
|
|
258
|
+
for (const item of (band.items ?? [])) {
|
|
259
|
+
if (item.type !== 'table') continue;
|
|
260
|
+
if (id !== null && item.id !== id) continue;
|
|
261
|
+
|
|
262
|
+
if (columns === null) {
|
|
263
|
+
id = item.id;
|
|
264
|
+
columns = item.columns ?? [];
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (item.row) {
|
|
268
|
+
for (const row of item.row) rows.push({ group: null, row });
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
for (const fragment of (item.groups ?? [])) {
|
|
273
|
+
for (const row of fragment.rows) {
|
|
274
|
+
rows.push({ group: fragment.key, row });
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return columns === null ? null : { columns, rows };
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
function encodeRow(cells, delimiter, neutraliseFormulas) {
|
|
286
|
+
return cells.map(cell => encodeCell(cell, delimiter, neutraliseFormulas)).join(delimiter);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* RFC 4180 quoting: wrap when the value holds the delimiter, a quote, or a line
|
|
292
|
+
* break, and double any quote inside.
|
|
293
|
+
*
|
|
294
|
+
* A leading =, +, - or @ is prefixed with a tab as well. A spreadsheet treats
|
|
295
|
+
* such a cell as a formula, which turns an exported report into an execution
|
|
296
|
+
* vector the moment someone opens it. The tab keeps the text readable and is
|
|
297
|
+
* dropped on import; pass neutraliseFormulas:false for a byte-exact export.
|
|
298
|
+
*/
|
|
299
|
+
function encodeCell(value, delimiter, neutraliseFormulas) {
|
|
300
|
+
if (value === null || value === undefined) return '';
|
|
301
|
+
|
|
302
|
+
let text = String(value);
|
|
303
|
+
|
|
304
|
+
if (neutraliseFormulas && FORMULA_LEAD.test(text)) {
|
|
305
|
+
text = `\t${text}`;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const mustQuote =
|
|
309
|
+
text.includes(delimiter) ||
|
|
310
|
+
text.includes('"') ||
|
|
311
|
+
text.includes('\n') ||
|
|
312
|
+
text.includes('\r');
|
|
313
|
+
|
|
314
|
+
return mustQuote ? `"${text.replace(/"/g, '""')}"` : text;
|
|
315
|
+
}
|