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,390 @@
1
+ /**
2
+ * measure.js computes the rendered height of every band and every item in it.
3
+ *
4
+ * Spec 4.3:
5
+ * text - canvas measureText, greedy wrap
6
+ * table - headerHeight + rows x rowHeight
7
+ * band - the fixed `height` field, or the computed height where it holds a table
8
+ */
9
+
10
+ import { textHeight } from './text-metrics.js';
11
+
12
+
13
+ /**
14
+ * Rows a table is drawn with before there is any data.
15
+ *
16
+ * A table declares no height (spec 3.3): it is its header plus however many
17
+ * rows the data turns out to have. The designer still has to draw it as *some*
18
+ * size, and whatever it draws is what the person arranging the band lines
19
+ * everything else up against.
20
+ *
21
+ * So this is not a designer detail that happens to live here. It is the
22
+ * baseline the engine measures a table's growth from - the difference between
23
+ * this height and the real one is exactly how far what sits below the table has
24
+ * to float down the page.
25
+ */
26
+ export const DESIGN_ROWS = 3;
27
+
28
+
29
+ /**
30
+ * The padding report.css draws inside a cell, on each side.
31
+ *
32
+ * Here because it is the difference between the column's width and the width
33
+ * the text actually gets, and a wrap measured against the wrong one is a row
34
+ * that comes out a line short of what prints.
35
+ */
36
+ export const CELL_PADDING = 8;
37
+
38
+
39
+ /**
40
+ * The font a table's cells are drawn in.
41
+ *
42
+ * report.css declares the same default, so what is measured here is what the
43
+ * browser draws. A table that names its own font is measured in that one.
44
+ *
45
+ * @param {object} item a table
46
+ * @returns {object} a style block for text-metrics
47
+ */
48
+ function cellFont(item) {
49
+ return {
50
+ fontSize: item.style?.fontSize ?? 12,
51
+ fontFamily: item.style?.fontFamily,
52
+ fontWeight: 'normal',
53
+ fontStyle: 'normal'
54
+ };
55
+ }
56
+
57
+
58
+ /**
59
+ * How wide each column is actually drawn.
60
+ *
61
+ * Not what the column says. report.css lays a table out with
62
+ * `table-layout: fixed`, and when the columns add up to less than the table is
63
+ * wide the browser hands the difference out in proportion - so a 300px column
64
+ * in a 714px table whose columns total 450 is drawn at 476. Measuring a wrap
65
+ * against the declared width instead put a row two lines tall that prints on
66
+ * one, which is a page count that is wrong in the safe direction and still
67
+ * wrong.
68
+ *
69
+ * @param {object} item a table
70
+ * @returns {number[]}
71
+ */
72
+ function columnWidths(item) {
73
+ const declared = (item.columns ?? []).map(c => c.width ?? 0);
74
+ const sum = declared.reduce((a, b) => a + b, 0);
75
+ const table = item.w ?? sum;
76
+
77
+ const scale = sum > 0 && table > sum ? table / sum : 1;
78
+
79
+ return declared.map(w => w * scale);
80
+ }
81
+
82
+
83
+ /**
84
+ * The grid line drawn inside each cell, which eats into the text's width the
85
+ * same way the padding does - box-sizing: border-box, so that it can, which is
86
+ * what keeps a table exactly as tall as it was measured.
87
+ *
88
+ * @param {object} item a table
89
+ * @returns {number} pixels
90
+ */
91
+ function ruleWidth(item) {
92
+ if (item.style?.borderStyle === 'none') return 0;
93
+
94
+ const declared = Number(item.style?.borderWidth);
95
+
96
+ return Number.isFinite(declared) && declared > 0 ? declared : 1;
97
+ }
98
+
99
+
100
+ /**
101
+ * What each column leaves for its text, once the padding and the grid have had
102
+ * theirs. The last column carries the table's right-hand rule as well as its
103
+ * left one, so it gets one less pixel than the rest.
104
+ *
105
+ * @param {object} item a table
106
+ * @returns {number[]}
107
+ */
108
+ export function textWidths(item) {
109
+ const widths = columnWidths(item);
110
+ const rule = ruleWidth(item);
111
+ const last = widths.length - 1;
112
+
113
+ return widths.map((w, index) =>
114
+ w - CELL_PADDING * 2 - rule * (index === last ? 2 : 1));
115
+ }
116
+
117
+
118
+ /**
119
+ * How tall each of these rows has to be.
120
+ *
121
+ * A cell whose text is wider than its column wraps, and a row is as tall as its
122
+ * tallest cell - never shorter than the height the table declares, which stays
123
+ * the floor rather than becoming the whole answer.
124
+ *
125
+ * The declared height used to be the whole answer, and a cell that did not fit
126
+ * was cut off with an ellipsis. That is a strange thing for a report to do with
127
+ * somebody's data: an address that runs long is not less true for it.
128
+ *
129
+ * A table can still ask for the old behaviour with `wrap: false`, which is the
130
+ * right answer for a column of figures that must stay one line however wide the
131
+ * numbers get.
132
+ *
133
+ * @param {object} item a table
134
+ * @param {object[]} rows
135
+ * @returns {number[]} one height per row, in order
136
+ */
137
+ export function rowHeightsFor(item, rows) {
138
+ const declared = item.rowHeight ?? 0;
139
+ const list = rows ?? [];
140
+
141
+ if (item.wrap === false) return list.map(() => declared);
142
+
143
+ const font = cellFont(item);
144
+ const columns = item.columns ?? [];
145
+ const widths = textWidths(item);
146
+
147
+ return list.map(row => {
148
+ let tallest = declared;
149
+
150
+ for (const [index, column] of columns.entries()) {
151
+ const width = widths[index];
152
+ if (!(width > 0)) continue;
153
+
154
+ const value = row?.[column.field];
155
+ if (value === null || value === undefined || value === '') continue;
156
+
157
+ tallest = Math.max(tallest, textHeight(String(value), width, font));
158
+ }
159
+
160
+ return tallest;
161
+ });
162
+ }
163
+
164
+
165
+ /**
166
+ * How tall the header row has to be.
167
+ *
168
+ * A column label wraps like any other cell, and one that did so used to be cut
169
+ * off - which is the same fault as a truncated cell, in the one row of a table
170
+ * nobody can scroll past.
171
+ *
172
+ * @param {object} item a table
173
+ * @returns {number} 0 for a table that draws no header
174
+ */
175
+ export function headerHeightFor(item) {
176
+ if (!item.showHeader) return 0;
177
+
178
+ const declared = item.headerHeight ?? item.rowHeight ?? 0;
179
+ if (item.wrap === false) return declared;
180
+
181
+ /**
182
+ * The header is one row whose cells happen to be the column labels, so it
183
+ * is measured as one - each label in its own column, which is why the
184
+ * fields are renamed rather than every column being pointed at one key.
185
+ */
186
+ const columns = (item.columns ?? []).map((column, index) =>
187
+ ({ ...column, field: `label${index}` }));
188
+
189
+ const row = Object.fromEntries((item.columns ?? []).map((column, index) =>
190
+ [`label${index}`, column.label]));
191
+
192
+ const [measured] = rowHeightsFor(
193
+ { ...item, rowHeight: declared, columns }, [row]);
194
+
195
+ return measured ?? declared;
196
+ }
197
+
198
+
199
+ /**
200
+ * The height a table is drawn at while it is being designed.
201
+ *
202
+ * Anything that is not a table has a height of its own and keeps it.
203
+ *
204
+ * @param {object} item
205
+ * @returns {number} pixels
206
+ */
207
+ export function designHeight(item) {
208
+ if (item.type !== 'table') return item.h ?? 0;
209
+
210
+ const rowHeight = item.rowHeight ?? 0;
211
+ const header = item.showHeader ? (item.headerHeight ?? rowHeight) : 0;
212
+
213
+ return header + DESIGN_ROWS * rowHeight;
214
+ }
215
+
216
+
217
+ export function measure(json) {
218
+ const measureJson = structuredClone(json);
219
+ const grouped = measureJson.groupBy != null;
220
+
221
+ for (const band of measureJson.bands) {
222
+ if (!band.items) continue;
223
+ measureBand(band, grouped, measureJson);
224
+ }
225
+
226
+ return measureJson;
227
+ }
228
+
229
+
230
+ /**
231
+ * Items are absolutely positioned inside their band, so the band's content
232
+ * height is the lowest edge any item reaches - max(y + height). That is order
233
+ * independent, and handles items side by side on the same row as well as items
234
+ * separated by a gap, neither of which a running total can express.
235
+ * @param {object} band
236
+ * @param {boolean} grouped
237
+ * @param {object} json
238
+ * @returns {object} the same band, measured
239
+ */
240
+ function measureBand(band, grouped, json) {
241
+ let contentHeight = 0;
242
+
243
+ for (const item of band.items) {
244
+ item.measuredHeight = measureItem(item, grouped, json);
245
+ contentHeight = Math.max(contentHeight, (item.y ?? 0) + item.measuredHeight);
246
+ }
247
+
248
+ /**
249
+ * A declared band height is the design intent, so it wins - but it can only
250
+ * grow to fit content, never clip it. A band holding a table has no useful
251
+ * declared height; the row count decides.
252
+ */
253
+ band.contentHeight = contentHeight;
254
+ band.measuredHeight = holdsTable(band)
255
+ ? contentHeight
256
+ : Math.max(band.height ?? 0, contentHeight);
257
+
258
+ return band;
259
+ }
260
+
261
+
262
+ function holdsTable(band) {
263
+ return band.items.some(item => item.type === 'table');
264
+ }
265
+
266
+
267
+ function measureItem(item, grouped, json) {
268
+ switch (item.type) {
269
+ case 'text':
270
+ return measureTextItem(item);
271
+ case 'table':
272
+ return measureTableItem(item, grouped, json);
273
+ case 'line':
274
+ return measureLineItem(item);
275
+ case 'box':
276
+ return item.h ?? 0;
277
+ default:
278
+ console.warn(`Unknown item type "${item.type}" on item "${item.id}"; treating as zero height.`);
279
+ return 0;
280
+ }
281
+ }
282
+
283
+
284
+ /**
285
+ * A declared `h` is the design intent and is honoured, but text that wraps past
286
+ * it grows the item rather than being clipped. Placeholders are already
287
+ * resolved at this point, so what is measured is what will be drawn.
288
+ * @param {object} item
289
+ * @returns {number}
290
+ */
291
+ function measureTextItem(item) {
292
+ const declared = item.h ?? 0;
293
+ const wrapped = textHeight(item.text ?? item.value ?? '', item.w ?? 0, item.style);
294
+ return Math.max(declared, wrapped);
295
+ }
296
+
297
+
298
+ /**
299
+ * The box a line occupies, which is its declared `h` - the rule is drawn inside
300
+ * that box rather than being it, so that a hairline is still something the
301
+ * designer can get hold of.
302
+ *
303
+ * A rule thicker than its box grows the box, for the same reason wrapped text
304
+ * grows a text item: what is drawn is never quietly clipped.
305
+ *
306
+ * @param {object} item
307
+ * @returns {number}
308
+ */
309
+ function measureLineItem(item) {
310
+ const declared = item.h ?? 0;
311
+
312
+ /** only a horizontal rule's thickness is a height; a vertical one's is a width */
313
+ if (item.orientation === 'vertical') return declared;
314
+
315
+ return Math.max(declared, item.style?.thickness ?? 0);
316
+ }
317
+
318
+
319
+ /**
320
+ * headerHeight + rows x rowHeight. When the report is grouped, each group also
321
+ * carries its own header and footer band, and a table header that repeats per
322
+ * group - all of which occupy real space and were previously unaccounted for.
323
+ * @param {object} item
324
+ * @param {boolean} grouped
325
+ * @param {object} json
326
+ * @returns {number}
327
+ */
328
+ function measureTableItem(item, grouped, json) {
329
+ const headerHgt = headerHeightFor(item);
330
+
331
+ /** stamped on so the renderer draws the row at the height that was budgeted */
332
+ item.headerMeasured = headerHgt;
333
+
334
+ /**
335
+ * The heights are written onto the table as well as summed, because
336
+ * pagination has to slice rows by them and the renderer has to draw each
337
+ * row at its own. Measuring the same wrap three times in three files is how
338
+ * the three of them come to disagree.
339
+ */
340
+ if (!grouped) {
341
+ item.rowHeights = rowHeightsFor(item, item.row);
342
+
343
+ return headerHgt + total(item.rowHeights);
344
+ }
345
+
346
+ const groups = item.groups ?? [];
347
+ const grpHeaderHgt = bandHeight(json, 'groupHeader');
348
+ const grpFooterHgt = bandHeight(json, 'groupFooter');
349
+
350
+ let height = 0;
351
+
352
+ for (const group of groups) {
353
+ group.rowHeights = rowHeightsFor(item, group.rows);
354
+
355
+ height += headerHgt
356
+ + grpHeaderHgt
357
+ + total(group.rowHeights)
358
+ + grpFooterHgt;
359
+ }
360
+
361
+ return height;
362
+ }
363
+
364
+
365
+ /** @param {number[]} list @returns {number} */
366
+ export function total(list) {
367
+ return (list ?? []).reduce((sum, n) => sum + n, 0);
368
+ }
369
+
370
+
371
+ /**
372
+ * Height of a template band that never flows on its own (groupHeader /
373
+ * groupFooter). It is measured here rather than read raw so a group band whose
374
+ * text wraps is still accounted for.
375
+ * @param {object} json
376
+ * @param {string} type
377
+ * @returns {number}
378
+ */
379
+ function bandHeight(json, type) {
380
+ const band = json.bands.find(b => b.type === type);
381
+ if (!band?.items) return 0;
382
+
383
+ let contentHeight = 0;
384
+ for (const item of band.items) {
385
+ const height = item.type === 'text' ? measureTextItem(item) : 0;
386
+ contentHeight = Math.max(contentHeight, (item.y ?? 0) + height);
387
+ }
388
+
389
+ return Math.max(band.height ?? 0, contentHeight);
390
+ }