report-studio 0.0.1 → 1.1.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 (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +363 -4
  3. package/dist/designer-qy59eEux.js +3416 -0
  4. package/dist/designer.js +2 -0
  5. package/dist/index.js +2 -4
  6. package/dist/react-designer.js +41 -0
  7. package/dist/react.js +58 -2
  8. package/dist/viewer-xa17tyMJ.js +1721 -0
  9. package/package.json +65 -18
  10. package/src/asset/images/icon.png +0 -0
  11. package/src/asset/images/logo.png +0 -0
  12. package/src/cli/index.js +282 -0
  13. package/src/cli/serve.js +135 -0
  14. package/src/designer/blank.js +53 -0
  15. package/src/designer/canvas.js +518 -0
  16. package/src/designer/designer.js +2054 -0
  17. package/src/designer/dialog.js +708 -0
  18. package/src/designer/fields.js +633 -0
  19. package/src/designer/geometry.js +154 -0
  20. package/src/designer/guides.js +370 -0
  21. package/src/designer/history.js +113 -0
  22. package/src/designer/icons.js +123 -0
  23. package/src/designer/index.html +28 -0
  24. package/src/designer/main.js +39 -0
  25. package/src/designer/menu.js +126 -0
  26. package/src/designer/panel.js +498 -0
  27. package/src/designer/preview.js +95 -0
  28. package/src/designer/rulers.js +231 -0
  29. package/src/designer/sample-data.js +411 -0
  30. package/src/designer/select.js +699 -0
  31. package/src/designer/structure.js +685 -0
  32. package/src/designer/styles.css +1654 -0
  33. package/src/designer/toast.js +62 -0
  34. package/src/designer/tokens.js +293 -0
  35. package/src/engine/csv.js +350 -0
  36. package/src/engine/group.js +367 -0
  37. package/src/engine/index.js +35 -0
  38. package/src/engine/measure.js +391 -0
  39. package/src/engine/paginate.js +865 -0
  40. package/src/engine/regions.js +175 -0
  41. package/src/engine/resolve.js +208 -0
  42. package/src/engine/search.js +129 -0
  43. package/src/engine/tables.js +87 -0
  44. package/src/engine/text-metrics.js +206 -0
  45. package/src/engine/validate.js +403 -0
  46. package/src/index.html +79 -0
  47. package/src/index.js +51 -0
  48. package/src/preview/chrome.css +505 -0
  49. package/src/preview/chrome.js +147 -0
  50. package/src/preview/index.html +28 -0
  51. package/src/preview/main.js +66 -0
  52. package/src/preview/styles.css +16 -0
  53. package/src/preview/viewer.js +763 -0
  54. package/src/react/designer.js +87 -0
  55. package/src/react/index.js +117 -0
  56. package/src/render/items.js +518 -0
  57. package/src/render/render.js +98 -0
  58. package/src/render/report.css +232 -0
  59. package/src/server/routes.js +331 -0
  60. package/src/shared/brand.css +29 -0
  61. package/src/shared/dropdown.css +136 -0
  62. package/src/shared/dropdown.js +166 -0
  63. package/src/shared/modal.css +125 -0
  64. package/src/shared/prefix.js +13 -0
  65. package/src/shared/store.js +145 -0
  66. package/src/shared/theme.css +72 -0
  67. package/src/shared/window.js +138 -0
  68. package/src/vite/index.js +53 -0
@@ -0,0 +1,391 @@
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
+ for (const band of measureJson.bands) {
220
+ if (!band.items) continue;
221
+ measureBand(band, measureJson);
222
+ }
223
+
224
+ return measureJson;
225
+ }
226
+
227
+
228
+ /**
229
+ * Items are absolutely positioned inside their band, so the band's content
230
+ * height is the lowest edge any item reaches - max(y + height). That is order
231
+ * independent, and handles items side by side on the same row as well as items
232
+ * separated by a gap, neither of which a running total can express.
233
+ * @param {object} band
234
+ * @param {object} json
235
+ * @returns {object} the same band, measured
236
+ */
237
+ function measureBand(band, json) {
238
+ let contentHeight = 0;
239
+
240
+ for (const item of band.items) {
241
+ item.measuredHeight = measureItem(item, json);
242
+ contentHeight = Math.max(contentHeight, (item.y ?? 0) + item.measuredHeight);
243
+ }
244
+
245
+ /**
246
+ * A declared band height is the design intent, so it wins - but it can only
247
+ * grow to fit content, never clip it. A band holding a table has no useful
248
+ * declared height; the row count decides.
249
+ */
250
+ band.contentHeight = contentHeight;
251
+ band.measuredHeight = holdsTable(band)
252
+ ? contentHeight
253
+ : Math.max(band.height ?? 0, contentHeight);
254
+
255
+ return band;
256
+ }
257
+
258
+
259
+ function holdsTable(band) {
260
+ return band.items.some(item => item.type === 'table');
261
+ }
262
+
263
+
264
+ function measureItem(item, json) {
265
+ switch (item.type) {
266
+ case 'text':
267
+ return measureTextItem(item);
268
+ case 'table':
269
+ return measureTableItem(item, json);
270
+ case 'line':
271
+ return measureLineItem(item);
272
+ case 'box':
273
+ case 'image':
274
+ /** a picture is scaled into its box, never the box grown round it */
275
+ return item.h ?? 0;
276
+ default:
277
+ console.warn(`Unknown item type "${item.type}" on item "${item.id}"; treating as zero height.`);
278
+ return 0;
279
+ }
280
+ }
281
+
282
+
283
+ /**
284
+ * A declared `h` is the design intent and is honoured, but text that wraps past
285
+ * it grows the item rather than being clipped. Placeholders are already
286
+ * resolved at this point, so what is measured is what will be drawn.
287
+ * @param {object} item
288
+ * @returns {number}
289
+ */
290
+ function measureTextItem(item) {
291
+ const declared = item.h ?? 0;
292
+ const wrapped = textHeight(item.text ?? item.value ?? '', item.w ?? 0, item.style);
293
+ return Math.max(declared, wrapped);
294
+ }
295
+
296
+
297
+ /**
298
+ * The box a line occupies, which is its declared `h` - the rule is drawn inside
299
+ * that box rather than being it, so that a hairline is still something the
300
+ * designer can get hold of.
301
+ *
302
+ * A rule thicker than its box grows the box, for the same reason wrapped text
303
+ * grows a text item: what is drawn is never quietly clipped.
304
+ *
305
+ * @param {object} item
306
+ * @returns {number}
307
+ */
308
+ function measureLineItem(item) {
309
+ const declared = item.h ?? 0;
310
+
311
+ /** only a horizontal rule's thickness is a height; a vertical one's is a width */
312
+ if (item.orientation === 'vertical') return declared;
313
+
314
+ return Math.max(declared, item.style?.thickness ?? 0);
315
+ }
316
+
317
+
318
+ /**
319
+ * headerHeight + rows x rowHeight. When the table is grouped, each group also
320
+ * carries its own header and footer band, and a table header that repeats per
321
+ * group - all of which occupy real space and were previously unaccounted for.
322
+ *
323
+ * Grouped is the table's own fact - group.js gave it `groups` - and not the
324
+ * report's: of two tables in a grouped report, only one is split into groups.
325
+ * @param {object} item
326
+ * @param {object} json
327
+ * @returns {number}
328
+ */
329
+ function measureTableItem(item, json) {
330
+ const headerHgt = headerHeightFor(item);
331
+
332
+ /** stamped on so the renderer draws the row at the height that was budgeted */
333
+ item.headerMeasured = headerHgt;
334
+
335
+ /**
336
+ * The heights are written onto the table as well as summed, because
337
+ * pagination has to slice rows by them and the renderer has to draw each
338
+ * row at its own. Measuring the same wrap three times in three files is how
339
+ * the three of them come to disagree.
340
+ */
341
+ if (!Array.isArray(item.groups)) {
342
+ item.rowHeights = rowHeightsFor(item, item.row);
343
+
344
+ return headerHgt + total(item.rowHeights);
345
+ }
346
+
347
+ const groups = item.groups ?? [];
348
+ const grpHeaderHgt = bandHeight(json, 'groupHeader');
349
+ const grpFooterHgt = bandHeight(json, 'groupFooter');
350
+
351
+ let height = 0;
352
+
353
+ for (const group of groups) {
354
+ group.rowHeights = rowHeightsFor(item, group.rows);
355
+
356
+ height += headerHgt
357
+ + grpHeaderHgt
358
+ + total(group.rowHeights)
359
+ + grpFooterHgt;
360
+ }
361
+
362
+ return height;
363
+ }
364
+
365
+
366
+ /** @param {number[]} list @returns {number} */
367
+ export function total(list) {
368
+ return (list ?? []).reduce((sum, n) => sum + n, 0);
369
+ }
370
+
371
+
372
+ /**
373
+ * Height of a template band that never flows on its own (groupHeader /
374
+ * groupFooter). It is measured here rather than read raw so a group band whose
375
+ * text wraps is still accounted for.
376
+ * @param {object} json
377
+ * @param {string} type
378
+ * @returns {number}
379
+ */
380
+ function bandHeight(json, type) {
381
+ const band = json.bands.find(b => b.type === type);
382
+ if (!band?.items) return 0;
383
+
384
+ let contentHeight = 0;
385
+ for (const item of band.items) {
386
+ const height = item.type === 'text' ? measureTextItem(item) : 0;
387
+ contentHeight = Math.max(contentHeight, (item.y ?? 0) + height);
388
+ }
389
+
390
+ return Math.max(band.height ?? 0, contentHeight);
391
+ }