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.
Files changed (63) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +238 -4
  3. package/dist/designer.js +3209 -0
  4. package/dist/index.js +2 -4
  5. package/dist/viewer-D-c0lKzQ.js +1576 -0
  6. package/package.json +66 -33
  7. package/src/asset/images/icon.png +0 -0
  8. package/src/asset/images/logo.png +0 -0
  9. package/src/cli/index.js +282 -0
  10. package/src/cli/serve.js +135 -0
  11. package/src/designer/blank.js +53 -0
  12. package/src/designer/canvas.js +518 -0
  13. package/src/designer/designer.js +1900 -0
  14. package/src/designer/dialog.js +708 -0
  15. package/src/designer/fields.js +584 -0
  16. package/src/designer/geometry.js +154 -0
  17. package/src/designer/guides.js +370 -0
  18. package/src/designer/history.js +113 -0
  19. package/src/designer/icons.js +118 -0
  20. package/src/designer/index.html +28 -0
  21. package/src/designer/main.js +39 -0
  22. package/src/designer/menu.js +126 -0
  23. package/src/designer/panel.js +478 -0
  24. package/src/designer/preview.js +95 -0
  25. package/src/designer/rulers.js +231 -0
  26. package/src/designer/sample-data.js +240 -0
  27. package/src/designer/select.js +699 -0
  28. package/src/designer/structure.js +604 -0
  29. package/src/designer/styles.css +1616 -0
  30. package/src/designer/toast.js +62 -0
  31. package/src/designer/tokens.js +284 -0
  32. package/src/engine/csv.js +315 -0
  33. package/src/engine/group.js +304 -0
  34. package/src/engine/index.js +34 -0
  35. package/src/engine/measure.js +390 -0
  36. package/src/engine/paginate.js +861 -0
  37. package/src/engine/regions.js +175 -0
  38. package/src/engine/resolve.js +171 -0
  39. package/src/engine/search.js +129 -0
  40. package/src/engine/text-metrics.js +206 -0
  41. package/src/engine/validate.js +328 -0
  42. package/src/index.html +79 -0
  43. package/src/index.js +50 -0
  44. package/src/preview/chrome.css +505 -0
  45. package/src/preview/chrome.js +147 -0
  46. package/src/preview/index.html +28 -0
  47. package/src/preview/main.js +66 -0
  48. package/src/preview/styles.css +16 -0
  49. package/src/preview/viewer.js +745 -0
  50. package/src/render/items.js +425 -0
  51. package/src/render/render.js +98 -0
  52. package/src/render/report.css +202 -0
  53. package/src/server/routes.js +331 -0
  54. package/src/shared/brand.css +29 -0
  55. package/src/shared/dropdown.css +136 -0
  56. package/src/shared/dropdown.js +166 -0
  57. package/src/shared/modal.css +125 -0
  58. package/src/shared/prefix.js +13 -0
  59. package/src/shared/store.js +145 -0
  60. package/src/shared/theme.css +72 -0
  61. package/src/shared/window.js +138 -0
  62. package/src/vite/index.js +53 -0
  63. package/dist/react.js +0 -4
@@ -0,0 +1,304 @@
1
+ /**
2
+ * group.js partitions the dataset and resolves aggregate placeholders.
3
+ *
4
+ * An aggregate has to be worked out over some set of rows, and there are only
5
+ * two sets a band can mean:
6
+ *
7
+ * groupHeader / groupFooter - that group's rows, and only that group's.
8
+ * They are templates rather than bands: one copy is drawn per group, so
9
+ * each copy has a different answer. Filled in per fragment by paginate.js,
10
+ * where which group is being drawn is known.
11
+ *
12
+ * every other band - the whole dataset. Filled in here, once.
13
+ *
14
+ * It used to be reportFooter and nothing else, which made a total under the
15
+ * rows - the place a total is most often wanted, and the one a designer reaches
16
+ * for first - print the raw `{sum(amount)}` back at you. There is no scope a
17
+ * detail or a page footer could mean *other* than the whole report, so there
18
+ * was never a reason to refuse them one.
19
+ */
20
+
21
+ import { ReportError } from './validate.js';
22
+
23
+
24
+ const REGEX = /\{([^{}]+)\}/g;
25
+
26
+
27
+ export function group(resolvedJson, data) {
28
+ let json = structuredClone(resolvedJson);
29
+ const hasGroup = json.groupBy != null;
30
+ if (hasGroup) {
31
+ return withGroup(json, data);
32
+ } else {
33
+ return withoutGroup(json, data);
34
+ }
35
+ }
36
+
37
+
38
+ function withGroup(json, data) {
39
+ const bands = json.bands;
40
+ const tableBand = findTableBand(bands);
41
+
42
+ if (!tableBand) {
43
+ throw new ReportError(
44
+ `group: layout.groupBy is "${json.groupBy}" but no band contains a table to group`
45
+ );
46
+ }
47
+
48
+ const dataset = datasetFor(tableBand, json, data);
49
+ const groupedData = groupedRows(dataset, json.groupBy);
50
+ const groupedAggregates = groupedAggregate(bands, groupedData);
51
+
52
+ tableBand.groups =
53
+ Object.entries(groupedData).map(
54
+ ([key, rows]) => ({
55
+ key,
56
+ rows,
57
+ aggregates: groupedAggregates[key]
58
+ })
59
+ );
60
+
61
+ aggregate(bands, dataset);
62
+ return json;
63
+ }
64
+
65
+
66
+ function withoutGroup(json, data) {
67
+ const bands = json.bands;
68
+ const tableBand = findTableBand(bands);
69
+
70
+ /** a report of nothing but text bands is legal - there is simply nothing to bind */
71
+ const dataset = tableBand ? datasetFor(tableBand, json, data) : [];
72
+
73
+ if (tableBand) tableBand.row = dataset;
74
+
75
+ aggregate(bands, dataset);
76
+ return json;
77
+ }
78
+
79
+
80
+ /**
81
+ * The rows a table is bound to. A missing dataset is a layout/data mismatch
82
+ * worth naming rather than a silent empty table.
83
+ * @param {object} tableBand the table item
84
+ * @param {object} json
85
+ * @param {object} data
86
+ * @returns {object[]}
87
+ */
88
+ function datasetFor(tableBand, json, data) {
89
+ const key = tableBand.dataset ?? json.dataset;
90
+ const dataset = data[key];
91
+
92
+ if (dataset === undefined) {
93
+ throw new ReportError(
94
+ `group: table "${tableBand.id}" is bound to dataset "${key}", which is not present in the data`
95
+ );
96
+ }
97
+
98
+ if (!Array.isArray(dataset)) {
99
+ throw new ReportError(
100
+ `group: dataset "${key}" must be an array of rows, got ${typeof dataset}`
101
+ );
102
+ }
103
+
104
+ return dataset;
105
+ }
106
+
107
+
108
+ function findTableBand(bands) {
109
+ for (const band of bands) {
110
+ for (const item of band.items) {
111
+ if (item.type == "table") {
112
+ return item;
113
+ }
114
+ }
115
+ }
116
+ }
117
+
118
+
119
+ /**
120
+ * The bands whose aggregates belong to one group rather than to the report.
121
+ *
122
+ * They are left alone here on purpose. Filling them in with the report's totals
123
+ * would not merely be the wrong number - it would consume the placeholder, and
124
+ * paginate.js would find nothing left to put the group's own answer into.
125
+ */
126
+ const GROUP_SCOPED = ['groupHeader', 'groupFooter'];
127
+
128
+
129
+ /**
130
+ * Report-wide aggregates: every band except the two that mean a group.
131
+ * @param {object[]} bands
132
+ * @param {object[]} dataset
133
+ */
134
+ function aggregate(bands, dataset) {
135
+ for (const band of bands) {
136
+ if (GROUP_SCOPED.includes(band.type)) continue;
137
+
138
+ for (const item of band.items) {
139
+ if (item.type != "text") continue;
140
+
141
+ /**
142
+ * Build on `text`, never on `value`. resolve.js has already put the
143
+ * data placeholders in and deliberately left the aggregate tokens
144
+ * standing; re-resolving from `value` here would discard its work
145
+ * and leave "{authorizer}" printed literally on the report.
146
+ */
147
+ item.text = fillAggregates(item.text ?? item.value, dataset);
148
+ }
149
+ }
150
+ }
151
+
152
+
153
+ /**
154
+ * Replaces every aggregate placeholder in a value while keeping the literal
155
+ * text around it, so "Items: {count()}" stays "Items: 48" rather than "48".
156
+ * Non-aggregate placeholders were already handled by resolve.js and are left
157
+ * as they are.
158
+ * @param {string} value
159
+ * @param {object[]} dataset
160
+ * @returns {string}
161
+ */
162
+ function fillAggregates(value, dataset) {
163
+ return value.replace(REGEX, (match, key) => {
164
+ const expr = parseAggregate(key.trim());
165
+ if (!expr) return match;
166
+
167
+ const result = compute(expr, dataset);
168
+ return result == null ? '' : String(result);
169
+ });
170
+ }
171
+
172
+
173
+ function parseAggregate(expr) {
174
+ const match = expr.match(/^(\w+)\((.*?)\)$/);
175
+ if (!match) return null;
176
+ return {
177
+ key: match[1],
178
+ field: match[2].trim()
179
+ }
180
+ }
181
+
182
+
183
+ /**
184
+ * Per-group aggregates, keyed on both dimensions that matter: the group key and
185
+ * the field the expression targets. Dropping either makes groups overwrite each
186
+ * other, or {sum(price)} collide with {sum(qty)}.
187
+ *
188
+ * Both group bands are read, not just the footer. "Region A - 12 orders" is a
189
+ * heading, and a count that worked in the footer and printed blank in the
190
+ * header is the same fault this file exists to have stopped having.
191
+ *
192
+ * Shape: aggregates[groupKey][field] = { count, sum, avg, min, max }
193
+ * @param {object[]} bands
194
+ * @param {object} dataset grouped rows, keyed by group key
195
+ * @returns {object}
196
+ */
197
+ function groupedAggregate(bands, dataset) {
198
+ const aggregates = {};
199
+
200
+ /** every group gets a slot, even one no groupFooter expression touches */
201
+ for (const key of Object.keys(dataset)) {
202
+ aggregates[key] = {};
203
+ }
204
+
205
+ for (const band of bands) {
206
+ if (!GROUP_SCOPED.includes(band.type)) continue;
207
+
208
+ for (const item of band.items) {
209
+ if (item.type != "text") continue;
210
+
211
+ for (const expr of parseAll(item.value)) {
212
+ for (const [key, rows] of Object.entries(dataset)) {
213
+ if (!aggregates[key][expr.field]) {
214
+ aggregates[key][expr.field] = {
215
+ count: null,
216
+ sum: null,
217
+ avg: null,
218
+ min: null,
219
+ max: null
220
+ };
221
+ }
222
+
223
+ aggregates[key][expr.field][expr.key] = compute(expr, rows);
224
+ }
225
+ }
226
+ }
227
+ }
228
+
229
+ return aggregates;
230
+ }
231
+
232
+
233
+ /** every aggregate expression in a value, not just the first */
234
+ function parseAll(value) {
235
+ const found = [];
236
+ for (const [, key] of value.matchAll(REGEX)) {
237
+ const expr = parseAggregate(key.trim());
238
+ if (expr) found.push(expr);
239
+ }
240
+ return found;
241
+ }
242
+
243
+
244
+ function groupedRows(dataset, groupBy) {
245
+ const grouped = {};
246
+ for (const row of dataset) {
247
+ const key = row[groupBy];
248
+ if (!grouped[key]) {
249
+ grouped[key] = [];
250
+ }
251
+ grouped[key].push(row);
252
+ }
253
+ return grouped;
254
+ }
255
+
256
+
257
+ /**
258
+ * Runs one aggregate over one set of rows.
259
+ * Non-numeric cells are skipped rather than counted as zero - a blank price
260
+ * should not drag an average down, and Math.max of nothing is -Infinity, which
261
+ * is not a number anyone wants printed on a report.
262
+ * @param {object} expr {key, field}
263
+ * @param {object[]} rows
264
+ * @returns {number|null} null where the answer is undefined for these rows
265
+ */
266
+ function compute(expr, rows) {
267
+ if (expr.key === 'count') return rows.length;
268
+
269
+ const values = rows
270
+ .map(row => row?.[expr.field])
271
+ /** Number('') is 0, so blanks have to be dropped before coercion */
272
+ .filter(v => v !== null && v !== undefined && v !== '')
273
+ .map(Number)
274
+ .filter(n => Number.isFinite(n));
275
+
276
+ switch (expr.key) {
277
+ case 'sum':
278
+ /** the sum of no rows is zero; the sum of no numbers is not */
279
+ return values.length ? round(values.reduce((a, b) => a + b, 0)) : null;
280
+ case 'avg':
281
+ return values.length
282
+ ? round(values.reduce((a, b) => a + b, 0) / values.length)
283
+ : null;
284
+ case 'max':
285
+ return values.length ? Math.max(...values) : null;
286
+ case 'min':
287
+ return values.length ? Math.min(...values) : null;
288
+ default:
289
+ console.warn(`Unknown aggregate "${expr.key}()"; it has no value.`);
290
+ return null;
291
+ }
292
+ }
293
+
294
+
295
+ /**
296
+ * Floating point sums produce 0.1 + 0.2 = 0.30000000000000004, which has no
297
+ * place on a printed report. Twelve significant digits is well past any real
298
+ * currency or quantity and short of the artefacts.
299
+ * @param {number} n
300
+ * @returns {number}
301
+ */
302
+ function round(n) {
303
+ return Number(n.toPrecision(12));
304
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * The engine's public surface.
3
+ *
4
+ * Spec 4: buildPages(layout, data) -> PageList, in four ordered stages, each a
5
+ * pure function over plain objects. Nothing here imports React, and nothing
6
+ * here touches the DOM beyond the offscreen canvas used for text measurement.
7
+ */
8
+
9
+ import { resolve } from './resolve.js';
10
+ import { group } from './group.js';
11
+ import { measure } from './measure.js';
12
+ import { paginate } from './paginate.js';
13
+
14
+ export { resolve } from './resolve.js';
15
+ export { group } from './group.js';
16
+ export { measure } from './measure.js';
17
+ export { paginate } from './paginate.js';
18
+ export { validateLayout, ReportError } from './validate.js';
19
+ export { search, searchPages } from './search.js';
20
+ export { pageRegions, bandZoneHeight, resolveHeight, DEFAULT_BAND_HEIGHTS } from './regions.js';
21
+ export { toCSV, toReportCSV, reportFilename } from './csv.js';
22
+ export { measureText, wrapLines, textHeight, LINE_HEIGHT_RATIO } from './text-metrics.js';
23
+
24
+
25
+ /**
26
+ * Turns a layout file plus a data payload into a page list.
27
+ * @param {object} layout the report layout file
28
+ * @param {object} data the host application's data, keyed by dataset name
29
+ * @returns {object} the layout, with a `pages` array of positioned bands
30
+ * @throws {ReportError} when the layout or data cannot be used
31
+ */
32
+ export function buildPages(layout, data) {
33
+ return paginate(measure(group(resolve(layout, data), data)));
34
+ }