report-studio 1.2.3 → 1.3.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 (46) hide show
  1. package/README.md +254 -3
  2. package/dist/designer-Dj330bWd.js +4690 -0
  3. package/dist/designer.js +1 -1
  4. package/dist/index.js +2 -2
  5. package/dist/react-designer.js +1 -1
  6. package/dist/react.js +1 -1
  7. package/dist/viewer-BewnkhfP.js +4028 -0
  8. package/package.json +2 -2
  9. package/src/designer/blank.js +0 -1
  10. package/src/designer/canvas.js +172 -54
  11. package/src/designer/designer.js +313 -12
  12. package/src/designer/dialog.js +609 -5
  13. package/src/designer/fields.js +128 -11
  14. package/src/designer/geometry.js +77 -4
  15. package/src/designer/grouping.js +556 -0
  16. package/src/designer/icons.js +9 -0
  17. package/src/designer/menu.js +50 -6
  18. package/src/designer/panel.js +478 -38
  19. package/src/designer/rulers.js +14 -6
  20. package/src/designer/sample-data.js +112 -31
  21. package/src/designer/select.js +3 -2
  22. package/src/designer/structure.js +126 -12
  23. package/src/designer/styles.css +661 -27
  24. package/src/designer/tokens.js +38 -11
  25. package/src/engine/calculate.js +487 -0
  26. package/src/engine/csv.js +17 -9
  27. package/src/engine/format.js +483 -0
  28. package/src/engine/group.js +350 -70
  29. package/src/engine/highlight.js +224 -0
  30. package/src/engine/index.js +8 -1
  31. package/src/engine/measure.js +53 -26
  32. package/src/engine/paginate.js +189 -92
  33. package/src/engine/resolve.js +117 -10
  34. package/src/engine/rows.js +71 -8
  35. package/src/engine/search.js +12 -4
  36. package/src/engine/tables.js +309 -11
  37. package/src/engine/validate.js +265 -8
  38. package/src/engine/xlsx-style.js +50 -4
  39. package/src/engine/xlsx.js +91 -21
  40. package/src/index.js +18 -0
  41. package/src/render/items.js +61 -10
  42. package/src/render/report.css +0 -5
  43. package/types/index.d.ts +96 -8
  44. package/types/layout.d.ts +156 -2
  45. package/dist/designer-DQlfKnTr.js +0 -3416
  46. package/dist/viewer-BSPNCNRU.js +0 -2912
@@ -23,13 +23,15 @@
23
23
  * and how tall it is, in pixels from the top of the printable area. CSV ignores
24
24
  * all of it. A descriptor is one of
25
25
  *
26
- * { kind: 'text', style, template, x, y, w, h } a text item, placed on the page
26
+ * { kind: 'text', style, template, format, raw, x, y, w, h }
27
+ * a text item, placed on the page
27
28
  * { kind: 'head', column, table } a table's column label
28
29
  * { kind: 'cell', column, table } a table's data cell
29
30
  *
30
31
  * where `table` is { style, wrap } - the table's own style block, and whether
31
32
  * its cells wrap - and `template` is the text item's value before placeholders
32
- * were filled. They are the page objects' own values, unresolved: what the
33
+ * were filled. `format` is the text item's, and `raw` the one value it printed
34
+ * when it is a single placeholder; a column's format is on the column. They are the page objects' own values, unresolved: what the
33
35
  * stylesheet does when they are absent is the exporter's to know.
34
36
  *
35
37
  * reportLayout adds what reportRows leaves out: the graphics - images, lines
@@ -39,6 +41,10 @@
39
41
  */
40
42
 
41
43
 
44
+ import { levelsOf, fragmentBandsOf } from './tables.js';
45
+ import { cellHighlight } from './highlight.js';
46
+
47
+
42
48
  /** the items that are drawn rather than written */
43
49
  const GRAPHICS = ['image', 'line', 'box'];
44
50
 
@@ -125,9 +131,11 @@ export function tableRows(pages, only = null) {
125
131
  }
126
132
 
127
133
  for (const fragment of (item.groups ?? [])) {
134
+ const path = Array.isArray(fragment.path) ? fragment.path : [fragment.key];
135
+
128
136
  fragment.rows.forEach((row, n) => {
129
137
  rows.push({
130
- group: fragment.key, row,
138
+ group: fragment.key, groups: path, row,
131
139
  height: rowHeight(item, fragment.rowHeights, n)
132
140
  });
133
141
  });
@@ -138,7 +146,49 @@ export function tableRows(pages, only = null) {
138
146
 
139
147
  if (columns === null) return null;
140
148
 
141
- return { id, columns, table: tableDesign(first), headerHeight: headerHeight(first), rows };
149
+ return {
150
+ id, columns, table: tableDesign(first), headerHeight: headerHeight(first), rows,
151
+ groupBy: levelsOf(first).map(level => level?.by ?? null)
152
+ };
153
+ }
154
+
155
+
156
+ /**
157
+ * The group columns an export puts before a grouped table's own, one per level
158
+ * it is grouped at - region, then city - so the groups survive a sort.
159
+ *
160
+ * Named by `label`: a list names each level in turn; a single name is the
161
+ * column of a table grouped at one level, as it has always been. A nested
162
+ * table's levels left unnamed are called after the column showing the field
163
+ * they group by, or the field itself.
164
+ *
165
+ * @param {{columns: object[], groupBy?: string[], rows: {group: *, groups?: Array}[]}} table
166
+ * from tableRows
167
+ * @param {string|string[]} [label='Group']
168
+ * @returns {{labels: string[], cells: (row: object) => Array}} no labels when
169
+ * the table is not grouped
170
+ */
171
+ export function groupColumns(table, label = 'Group') {
172
+ const pathOf = (row) => row.groups ?? (row.group == null ? [] : [row.group]);
173
+ const depth = Math.max(0, ...table.rows.map(row => pathOf(row).length));
174
+
175
+ const named = (n) => {
176
+ const by = table.groupBy?.[n];
177
+ const column = table.columns.find(c => by != null && c.field === by);
178
+ return column ? columnLabel(column) : (by ?? `Group ${n + 1}`);
179
+ };
180
+
181
+ const labels = Array.from({ length: depth }, (_, n) => {
182
+ if (Array.isArray(label)) return label[n] ?? named(n);
183
+ return depth === 1 ? label : named(n);
184
+ });
185
+
186
+ const cells = (row) => {
187
+ const path = pathOf(row);
188
+ return Array.from({ length: depth }, (_, n) => path[n] ?? null);
189
+ };
190
+
191
+ return { labels, cells };
142
192
  }
143
193
 
144
194
 
@@ -173,7 +223,12 @@ export function reportTables(pages) {
173
223
 
174
224
  /** what the descriptors of a table's cells share */
175
225
  function tableDesign(table) {
176
- return { style: table.style ?? {}, wrap: table.wrap !== false };
226
+ return {
227
+ style: table.style ?? {},
228
+ wrap: table.wrap !== false,
229
+ /** carried for the table sheet, which styles each row by them */
230
+ rowHighlight: table.rowHighlight ?? null
231
+ };
177
232
  }
178
233
 
179
234
  /** what items.js draws a header row at: measured if a label wrapped, else declared */
@@ -257,6 +312,10 @@ function collectBand(band, out, context) {
257
312
  kind: 'text',
258
313
  style: item.style ?? {},
259
314
  template: item.value,
315
+ /** a share is held as a fraction and shown as a percentage, whatever the item's format */
316
+ format: item.rawFormat ?? item.format,
317
+ raw: item.raw,
318
+ highlight: item.highlighted ?? null,
260
319
  x: Number(item.x) || 0,
261
320
  y: origin + (Number(item.y) || 0),
262
321
  w: Number(item.w) || 0,
@@ -428,7 +487,9 @@ function collectTable(table, out, context) {
428
487
  out.push({
429
488
  cells: table.columns.map(c => row?.[c.field]),
430
489
  header: false,
431
- design: table.columns.map(column => ({ kind: 'cell', column, table: shared })),
490
+ design: table.columns.map(column => ({
491
+ kind: 'cell', column, table: shared, highlight: cellHighlight(table, column, row)
492
+ })),
432
493
  page, top: cursor, height
433
494
  });
434
495
  cursor += Number(height) || 0;
@@ -446,11 +507,13 @@ function collectTable(table, out, context) {
446
507
  }
447
508
 
448
509
  for (const fragment of table.groups) {
510
+ const { headers, footers } = fragmentBandsOf(fragment);
511
+
449
512
  if (fragment.showHeader && table.showHeader) labels();
450
- if (fragment.headerBand) groupBand(fragment.headerBand);
513
+ headers.forEach(band => groupBand(band));
451
514
 
452
515
  fragment.rows.forEach((row, n) => cells(row, rowHeight(table, fragment.rowHeights, n)));
453
516
 
454
- if (fragment.showFooter && fragment.footerBand) groupBand(fragment.footerBand);
517
+ footers.forEach(band => groupBand(band));
455
518
  }
456
519
  }
@@ -7,6 +7,9 @@
7
7
  * vanilla preview and a future React Preview share one implementation.
8
8
  */
9
9
 
10
+ import { fragmentBandsOf } from './tables.js';
11
+ import { formatValue } from './format.js';
12
+
10
13
 
11
14
  /**
12
15
  * Every occurrence of {query} in a built report, in page order.
@@ -80,10 +83,14 @@ function collectTable(item, pageNO, bandType, needle, caseSensitive, matches) {
80
83
 
81
84
  /** a grouped table carries its rows inside per-group fragments */
82
85
  for (const fragment of (item.groups ?? [])) {
83
- collectBand(fragment.headerBand, pageNO, 'groupHeader', needle, caseSensitive, matches);
86
+ const { headers, footers } = fragmentBandsOf(fragment);
87
+
88
+ for (const band of headers) {
89
+ collectBand(band, pageNO, 'groupHeader', needle, caseSensitive, matches);
90
+ }
84
91
  collectRows(fragment.rows, columns, item.id, pageNO, bandType, needle, caseSensitive, matches);
85
- if (fragment.showFooter) {
86
- collectBand(fragment.footerBand, pageNO, 'groupFooter', needle, caseSensitive, matches);
92
+ for (const band of footers) {
93
+ collectBand(band, pageNO, 'groupFooter', needle, caseSensitive, matches);
87
94
  }
88
95
  }
89
96
  }
@@ -92,7 +99,8 @@ function collectTable(item, pageNO, bandType, needle, caseSensitive, matches) {
92
99
  function collectRows(rows, columns, itemId, pageNO, bandType, needle, caseSensitive, matches) {
93
100
  for (const row of (rows ?? [])) {
94
101
  for (const column of columns) {
95
- push(matches, row?.[column.field], {
102
+ /** what the page shows, so ₹1,00,000 is found as it is read */
103
+ push(matches, formatValue(row?.[column.field], column.format), {
96
104
  pageNO, bandType, itemId, field: column.field
97
105
  }, needle, caseSensitive);
98
106
  }
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * tables.js answers the questions every stage asks about a report's tables:
3
- * which there are, which dataset each reads, and which one is grouped.
3
+ * which there are, which dataset each reads, and which are grouped and how.
4
4
  *
5
5
  * A report used to have one table, so each stage found "the" table on its own -
6
6
  * and three copies of "the first table in the first band that has one" agreed
@@ -49,7 +49,34 @@ export function datasetOf(table, layout) {
49
49
 
50
50
 
51
51
  /**
52
- * Whether a table asks to be grouped by a field of its own.
52
+ * The two band types a group is drawn with. In a layout file they are either a
53
+ * table's own (`table.grouping[n].header` / `.footer`) or, in the older form,
54
+ * the report's own bands of these types.
55
+ */
56
+ export const GROUP_BAND_TYPES = ['groupHeader', 'groupFooter'];
57
+
58
+
59
+ /**
60
+ * The most levels a table may be grouped at.
61
+ *
62
+ * Every level adds a header and a footer that a page has to find room for
63
+ * beside a row, and a report grouped eight deep has stopped being a table and
64
+ * become an outline. validate.js refuses more; the designer stops offering.
65
+ */
66
+ export const MAX_GROUP_LEVELS = 8;
67
+
68
+
69
+ /** the orders a level's groups may be put in, besides the order they are first seen */
70
+ export const GROUP_SORTS = ['asc', 'desc'];
71
+
72
+
73
+ /**
74
+ * Whether a table asks to be grouped through the older `groupBy` field.
75
+ *
76
+ * Kept under its old name because that is what it has always answered. A table
77
+ * grouped the current way, through `grouping`, is not one of these - isGrouped
78
+ * answers "grouped either way".
79
+ *
53
80
  * @param {object} table
54
81
  * @returns {boolean}
55
82
  */
@@ -59,21 +86,160 @@ export function hasOwnGrouping(table) {
59
86
 
60
87
 
61
88
  /**
62
- * The grouped table, and the field it is grouped by.
89
+ * A table's grouping levels, outermost first, as the layout states them.
63
90
  *
64
- * A table that names its own `groupBy` is the one. Failing that, a report-wide
65
- * `layout.groupBy` - which is what every layout written before a report could
66
- * hold two tables says - means the first table, since that was the only one it
67
- * could have meant.
91
+ * Each level after the first splits the groups of the one before it: grouped
92
+ * by region and then by city, "Oslo" under North and "Oslo" under South are
93
+ * two groups, each inside its own region.
94
+ *
95
+ * @param {object} table
96
+ * @returns {object[]} `{ by, header, footer }` entries, never null
97
+ */
98
+ export function levelsOf(table) {
99
+ return Array.isArray(table?.grouping) ? table.grouping : [];
100
+ }
101
+
102
+
103
+ /**
104
+ * Whether a table is grouped, whichever way the layout says so.
105
+ * @param {object} table
106
+ * @returns {boolean}
107
+ */
108
+ export function isGrouped(table) {
109
+ return levelsOf(table).length > 0 || hasOwnGrouping(table);
110
+ }
111
+
112
+
113
+ /**
114
+ * The group bands one level of a table is drawn with - the outermost level's
115
+ * when no level is named. A table that is not grouped that deep, or a level
116
+ * without one of them, gets null for that band.
68
117
  *
69
- * One at most: groupHeader and groupFooter are the report's bands, not a
70
- * table's, so two grouped tables would have to share one heading design.
71
- * validate.js refuses a second; this takes the first.
118
+ * Reads a normalised layout - one that has been through normalizeGrouping, as
119
+ * every layout past group.js has.
120
+ *
121
+ * @param {object} table
122
+ * @param {number} [level=0] 0 is the outermost level
123
+ * @returns {{header: object|null, footer: object|null}}
124
+ */
125
+ export function groupBandsOf(table, level = 0) {
126
+ const found = levelsOf(table)[level];
127
+
128
+ return {
129
+ header: found?.header ?? null,
130
+ footer: found?.footer ?? null
131
+ };
132
+ }
133
+
134
+
135
+ /**
136
+ * What one fragment of a group draws around its rows - the rule measure.js
137
+ * sizes a grouped table by, paginate.js splits it by, and the renderer draws
138
+ * it by, kept here so the three cannot disagree.
139
+ *
140
+ * `leaf` is one of the innermost groups group.js makes, with `opens` and
141
+ * `closes` (both 0 for a table grouped at one level). A fragment is all of
142
+ * that group's rows on one page, or as many as fit.
143
+ *
144
+ * column labels on the first fragment of the table on a page, and wherever
145
+ * an outermost group starts. A one-level table so prints
146
+ * them over every group, as it always has; a nested one
147
+ * does not repeat them for every city in a region.
148
+ * group headers from the level the group opens inwards - and every level's
149
+ * on the first fragment on a page, so a page that carries
150
+ * on a region still says which region it is.
151
+ * group footers on the fragment that ends the group only, from the
152
+ * innermost level out to the one it closes.
153
+ *
154
+ * @param {object} table the table, past normalizeGrouping
155
+ * @param {object} leaf the group the fragment is of
156
+ * @param {boolean} firstOnPage whether nothing of this table is above it on the page
157
+ * @param {boolean} ends whether the fragment holds the group's last row
158
+ * @returns {{columnHeader: boolean, headers: {level: number, band: object}[],
159
+ * footers: {level: number, band: object}[]}} headers outermost first,
160
+ * footers innermost first, each as it is drawn
161
+ */
162
+ export function groupFrame(table, leaf, firstOnPage, ends) {
163
+ const levels = levelsOf(table);
164
+ const opens = leaf?.opens ?? 0;
165
+ const closes = leaf?.closes ?? 0;
166
+
167
+ const headers = [];
168
+ for (let level = firstOnPage ? 0 : opens; level < levels.length; level++) {
169
+ const band = levels[level]?.header;
170
+ if (band) headers.push({ level, band });
171
+ }
172
+
173
+ const footers = [];
174
+ if (ends) {
175
+ for (let level = levels.length - 1; level >= closes; level--) {
176
+ const band = levels[level]?.footer;
177
+ if (band) footers.push({ level, band });
178
+ }
179
+ }
180
+
181
+ return { columnHeader: firstOnPage || opens === 0, headers, footers };
182
+ }
183
+
184
+
185
+ /**
186
+ * Whether a group has to start on a page of its own: it starts a group at a
187
+ * level that says `newPage`, and it is not the table's first group - which
188
+ * starts wherever the table does, as anything else in a band would.
189
+ *
190
+ * @param {object} table the table, past normalizeGrouping
191
+ * @param {object} leaf one of the innermost groups group.js makes
192
+ * @param {number} n its place in the table's `groups`
193
+ * @returns {boolean}
194
+ */
195
+ export function startsPage(table, leaf, n) {
196
+ if (n === 0) return false;
197
+
198
+ const levels = levelsOf(table);
199
+ for (let level = leaf?.opens ?? 0; level < levels.length; level++) {
200
+ if (levels[level]?.newPage === true) return true;
201
+ }
202
+
203
+ return false;
204
+ }
205
+
206
+
207
+ /**
208
+ * The group bands a built fragment draws, in drawing order: headers outermost
209
+ * first, footers innermost first.
210
+ *
211
+ * Reads `headerBands` / `footerBands`, and falls back to the single
212
+ * `headerBand` / `footerBand` a fragment built some other way may carry - the
213
+ * designer's canvas draws one-level fragments of its own.
214
+ *
215
+ * @param {object} fragment one entry of a built table's `groups`
216
+ * @returns {{headers: object[], footers: object[]}}
217
+ */
218
+ export function fragmentBandsOf(fragment) {
219
+ const headers = Array.isArray(fragment?.headerBands)
220
+ ? fragment.headerBands
221
+ : [fragment?.headerBand];
222
+
223
+ const footers = Array.isArray(fragment?.footerBands)
224
+ ? fragment.footerBands
225
+ : [fragment?.showFooter ? fragment?.footerBand : null];
226
+
227
+ return { headers: headers.filter(Boolean), footers: footers.filter(Boolean) };
228
+ }
229
+
230
+
231
+ /**
232
+ * The table the older form of grouping means, and its field.
233
+ *
234
+ * A table that names its own `groupBy` is the one. Failing that, a report-wide
235
+ * `layout.groupBy` means the first table, since when that was written it was
236
+ * the only table there could be. Either way it is drawn with the report's own
237
+ * groupHeader and groupFooter bands, which is why there is at most one.
72
238
  *
73
239
  * @param {object} layout
74
240
  * @returns {{table: object, by: string}|null}
75
241
  */
76
- export function groupingOf(layout) {
242
+ function legacyGroupingOf(layout) {
77
243
  const tables = tablesOf(layout);
78
244
 
79
245
  const own = tables.find(hasOwnGrouping);
@@ -85,3 +251,135 @@ export function groupingOf(layout) {
85
251
 
86
252
  return null;
87
253
  }
254
+
255
+
256
+ /** the report's own band of a type, which only the older form of grouping uses */
257
+ function reportBand(layout, type) {
258
+ return (Array.isArray(layout?.bands) ? layout.bands : [])
259
+ .find(band => band?.type === type) ?? null;
260
+ }
261
+
262
+
263
+ /**
264
+ * Every grouped table, in table order, with the field it is grouped by and the
265
+ * bands its groups are drawn with - its outermost level's. A table grouped
266
+ * within its groups as well has the rest in `levels`, which lists every level,
267
+ * outermost first.
268
+ *
269
+ * Reads either form. A table's own `grouping` says all three itself; a table
270
+ * grouped the older way borrows the report's groupHeader and groupFooter.
271
+ * Where a table says both, its own `grouping` is the one taken - validate.js
272
+ * calls that a mistake, and this reads whatever it is given as far as it goes.
273
+ *
274
+ * @param {object} layout
275
+ * @returns {{table: object, by: string, header: object|null, footer: object|null,
276
+ * levels: {by: string, header: object|null, footer: object|null}[]}[]}
277
+ */
278
+ export function groupingsOf(layout) {
279
+ const legacy = legacyGroupingOf(layout);
280
+ const found = [];
281
+
282
+ for (const table of tablesOf(layout)) {
283
+ /** each level whole - its `sort` and `newPage` with it - bands made explicit */
284
+ const levels = levelsOf(table)
285
+ .filter(level => level != null && typeof level === 'object')
286
+ .map(level => ({
287
+ ...level,
288
+ header: level.header ?? null,
289
+ footer: level.footer ?? null
290
+ }));
291
+
292
+ if (levels.length > 0) {
293
+ found.push({ table, ...levels[0], levels });
294
+ } else if (legacy?.table === table) {
295
+ const only = {
296
+ by: legacy.by,
297
+ header: reportBand(layout, 'groupHeader'),
298
+ footer: reportBand(layout, 'groupFooter')
299
+ };
300
+ found.push({ table, ...only, levels: [only] });
301
+ }
302
+ }
303
+
304
+ return found;
305
+ }
306
+
307
+
308
+ /**
309
+ * The first grouped table, and the field it is grouped by - or null.
310
+ *
311
+ * What this answered when a report could group only one table, and still the
312
+ * question a caller asks when it only wants to know whether anything is.
313
+ * groupingsOf lists them all.
314
+ *
315
+ * @param {object} layout
316
+ * @returns {{table: object, by: string, header: object|null, footer: object|null}|null}
317
+ */
318
+ export function groupingOf(layout) {
319
+ return groupingsOf(layout)[0] ?? null;
320
+ }
321
+
322
+
323
+ /**
324
+ * A copy of a layout with its grouping said one way only: on each table.
325
+ *
326
+ * The older form - `layout.groupBy` or `table.groupBy`, drawn with the report's
327
+ * own groupHeader and groupFooter bands - becomes that table's `grouping`, with
328
+ * those bands moved into it. Every stage after this one then has a single shape
329
+ * to read, and a report that groups three tables is no different to them from
330
+ * one that groups one.
331
+ *
332
+ * What goes away: `layout.groupBy`, every `table.groupBy`, and the report's
333
+ * group bands, which only ever meant "how the grouped table's groups look" and
334
+ * now sit on that table. A group band the older form left unused is dropped
335
+ * too; nothing ever drew it.
336
+ *
337
+ * Each group band is stamped with its type, so the stages that tell a group
338
+ * band from any other by its type go on telling it.
339
+ *
340
+ * Idempotent: a layout already in this shape comes back as an equal copy.
341
+ *
342
+ * @param {object} layout
343
+ * @returns {object} a new layout; the one given is not touched
344
+ */
345
+ export function normalizeGrouping(layout) {
346
+ const out = structuredClone(layout);
347
+ if (out == null || typeof out !== 'object') return out;
348
+
349
+ for (const grouping of groupingsOf(out)) {
350
+ if (levelsOf(grouping.table).length === 0) {
351
+ grouping.table.grouping = [{
352
+ by: grouping.by,
353
+ header: grouping.header,
354
+ footer: grouping.footer
355
+ }];
356
+ }
357
+ }
358
+
359
+ for (const table of tablesOf(out)) {
360
+ delete table.groupBy;
361
+
362
+ for (const level of levelsOf(table)) {
363
+ if (level == null || typeof level !== 'object') continue;
364
+
365
+ level.header = typed(level.header, 'groupHeader');
366
+ level.footer = typed(level.footer, 'groupFooter');
367
+ }
368
+ }
369
+
370
+ delete out.groupBy;
371
+
372
+ if (Array.isArray(out.bands)) {
373
+ out.bands = out.bands.filter(band => !GROUP_BAND_TYPES.includes(band?.type));
374
+ }
375
+
376
+ return out;
377
+ }
378
+
379
+
380
+ /** a group band carrying its type, or null for a level that has none */
381
+ function typed(band, type) {
382
+ if (band == null || typeof band !== 'object') return null;
383
+ band.type = type;
384
+ return band;
385
+ }