report-studio 1.2.3 → 1.3.1

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 (53) hide show
  1. package/README.md +254 -3
  2. package/dist/designer-_sWqzOj8.js +5052 -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-4I7C1Rnd.js +4083 -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/choice.js +368 -0
  12. package/src/designer/designer.js +342 -12
  13. package/src/designer/dialog.js +634 -6
  14. package/src/designer/fields.js +345 -36
  15. package/src/designer/geometry.js +77 -4
  16. package/src/designer/grouping.js +556 -0
  17. package/src/designer/icons.js +22 -0
  18. package/src/designer/menu.js +50 -6
  19. package/src/designer/panel.js +872 -69
  20. package/src/designer/rulers.js +14 -6
  21. package/src/designer/sample-data.js +112 -31
  22. package/src/designer/select.js +24 -3
  23. package/src/designer/structure.js +126 -12
  24. package/src/designer/styles.css +2017 -732
  25. package/src/designer/tokens.js +38 -11
  26. package/src/engine/calculate.js +487 -0
  27. package/src/engine/csv.js +17 -9
  28. package/src/engine/format.js +483 -0
  29. package/src/engine/group.js +350 -70
  30. package/src/engine/highlight.js +224 -0
  31. package/src/engine/index.js +8 -1
  32. package/src/engine/measure.js +53 -26
  33. package/src/engine/paginate.js +189 -92
  34. package/src/engine/resolve.js +117 -10
  35. package/src/engine/rows.js +71 -8
  36. package/src/engine/search.js +12 -4
  37. package/src/engine/tables.js +309 -11
  38. package/src/engine/validate.js +304 -8
  39. package/src/engine/xlsx-style.js +50 -4
  40. package/src/engine/xlsx.js +91 -21
  41. package/src/index.js +18 -0
  42. package/src/preview/chrome.css +302 -84
  43. package/src/preview/chrome.js +70 -58
  44. package/src/preview/viewer.js +137 -85
  45. package/src/render/items.js +61 -10
  46. package/src/render/report.css +0 -5
  47. package/src/shared/dropdown.css +78 -45
  48. package/src/shared/dropdown.js +105 -5
  49. package/src/shared/modal.css +78 -13
  50. package/types/index.d.ts +104 -9
  51. package/types/layout.d.ts +164 -2
  52. package/dist/designer-DQlfKnTr.js +0 -3416
  53. package/dist/viewer-BSPNCNRU.js +0 -2912
@@ -7,7 +7,12 @@
7
7
  */
8
8
 
9
9
 
10
- import { tablesOf, hasOwnGrouping } from './tables.js';
10
+ import { tablesOf, hasOwnGrouping, levelsOf, MAX_GROUP_LEVELS, GROUP_SORTS } from './tables.js';
11
+ import {
12
+ FORMAT_TYPES, GROUPINGS, NEGATIVES, ZEROS, SYMBOL_POSITIONS, MAX_DECIMALS
13
+ } from './format.js';
14
+ import { HIGHLIGHT_OPS, HIGHLIGHT_STYLE_KEYS } from './highlight.js';
15
+ import { parseExpression } from './calculate.js';
11
16
  import { designHeight } from './measure.js';
12
17
 
13
18
 
@@ -74,6 +79,8 @@ export function validateLayout(json) {
74
79
  issues.push('layout.groupBy must be a field name or null');
75
80
  }
76
81
 
82
+ validateFormat(json.format, 'layout.format', issues, false);
83
+
77
84
  const seenTypes = new Set();
78
85
 
79
86
  json.bands.forEach((band, i) => {
@@ -114,12 +121,18 @@ export function validateLayout(json) {
114
121
  /**
115
122
  * What the tables of a report may be, together.
116
123
  *
117
- * Any number of them, each bound to its own dataset - but two things they may
118
- * not do, because the engine has no honest way to print them:
124
+ * Any number of them, each bound to its own dataset, and any number of them
125
+ * grouped - each through a `grouping` of its own, which carries the bands its
126
+ * groups are drawn with. What they may not do, because the engine has no
127
+ * honest way to print it:
128
+ *
129
+ * Two tables grouped the older way. `groupBy` on a table borrows the report's
130
+ * groupHeader and groupFooter, and there is one of each - so both tables
131
+ * would draw their groups with the same heading. A second grouped table says
132
+ * so with a `grouping` of its own.
119
133
  *
120
- * Two grouped tables. groupHeader and groupFooter are the report's bands, so
121
- * both tables would draw their groups with the same heading - and a subtotal
122
- * in it would be summed over whichever table happened to be drawn.
134
+ * One table grouped both ways. Which of the two it meant is not something to
135
+ * guess at.
123
136
  *
124
137
  * Two tables side by side. Each table splits across pages on its own, so the
125
138
  * one on the left would run to page three before the one on the right began,
@@ -142,14 +155,17 @@ function validateTables(json, bands, issues) {
142
155
  if (table.dataset != null && typeof table.dataset !== 'string') {
143
156
  issues.push(`table "${table.id}".dataset must be a dataset name or null`);
144
157
  }
158
+
159
+ validateGrouping(table, issues);
145
160
  }
146
161
 
147
162
  const grouped = tables.filter(hasOwnGrouping);
148
163
 
149
164
  if (grouped.length > 1) {
150
165
  issues.push(
151
- `${grouped.length} tables are grouped (${grouped.map(t => `"${t.id}"`).join(', ')}); ` +
152
- `a report groups one, because groupHeader and groupFooter are shared`);
166
+ `${grouped.length} tables are grouped by groupBy (${grouped.map(t => `"${t.id}"`).join(', ')}); ` +
167
+ `only one can use the report's groupHeader and groupFooter - give the others ` +
168
+ `a grouping of their own`);
153
169
  }
154
170
 
155
171
  if (grouped.length > 0 && json.groupBy == null
@@ -159,6 +175,16 @@ function validateTables(json, bands, issues) {
159
175
  `no groupHeader or groupFooter band to show it`);
160
176
  }
161
177
 
178
+ /** layout.groupBy means the first table, unless a table names its own groupBy */
179
+ const first = tables[0];
180
+
181
+ if (json.groupBy != null && grouped.length === 0
182
+ && first && levelsOf(first).length > 0) {
183
+ issues.push(
184
+ `layout.groupBy groups the first table, "${first.id}", which already has a ` +
185
+ `grouping of its own; remove one of the two`);
186
+ }
187
+
162
188
  for (const band of bands) {
163
189
  if (!Array.isArray(band?.items)) continue;
164
190
 
@@ -182,6 +208,122 @@ function validateTables(json, bands, issues) {
182
208
  }
183
209
 
184
210
 
211
+ /**
212
+ * A table's own grouping: a list of levels, each a field and the bands its
213
+ * groups are drawn with.
214
+ *
215
+ * "grouping": [{ "by": "region", "header": { "height": 30, "items": [] }, "footer": null }]
216
+ *
217
+ * Levels are outermost first; each after the first groups within the groups of
218
+ * the one before. Grouping twice by the same field is refused - every group of
219
+ * the inner level would be the whole of its outer group, drawn twice over.
220
+ *
221
+ * @param {object} table
222
+ * @param {string[]} issues
223
+ */
224
+ function validateGrouping(table, issues) {
225
+ if (table.grouping == null) return;
226
+
227
+ const where = `table "${table.id}".grouping`;
228
+
229
+ if (!Array.isArray(table.grouping)) {
230
+ issues.push(`${where} must be a list of group levels, or null`);
231
+ return;
232
+ }
233
+
234
+ if (table.grouping.length > 0 && hasOwnGrouping(table)) {
235
+ issues.push(
236
+ `table "${table.id}" is grouped both by groupBy and by grouping; remove groupBy`);
237
+ }
238
+
239
+ if (table.grouping.length > MAX_GROUP_LEVELS) {
240
+ issues.push(
241
+ `${where} has ${table.grouping.length} levels; a table may be grouped at ` +
242
+ `${MAX_GROUP_LEVELS} at most`);
243
+ }
244
+
245
+ /** the level each field was first grouped by, to name a repeat against */
246
+ const seen = new Map();
247
+
248
+ table.grouping.forEach((level, n) => {
249
+ const at = `${where}[${n}]`;
250
+
251
+ if (level == null || typeof level !== 'object' || Array.isArray(level)) {
252
+ issues.push(`${at} must be an object with by, header and footer`);
253
+ return;
254
+ }
255
+
256
+ if (typeof level.by !== 'string' || level.by === '') {
257
+ issues.push(`${at}.by must be the name of the field to group by`);
258
+ } else if (seen.has(level.by)) {
259
+ issues.push(
260
+ `${at} groups by "${level.by}" again, as ${where}[${seen.get(level.by)}] ` +
261
+ `already does; group each level by a different field`);
262
+ } else {
263
+ seen.set(level.by, n);
264
+ }
265
+
266
+ if (level.sort != null && !GROUP_SORTS.includes(level.sort)) {
267
+ issues.push(
268
+ `${at}.sort must be ${GROUP_SORTS.map(s => `"${s}"`).join(' or ')}, or null ` +
269
+ `to keep the groups in the order they are first seen`);
270
+ }
271
+
272
+ if (level.newPage != null && typeof level.newPage !== 'boolean') {
273
+ issues.push(`${at}.newPage must be true or false`);
274
+ }
275
+
276
+ validateGroupBand(level.header, `${at}.header`, issues);
277
+ validateGroupBand(level.footer, `${at}.footer`, issues);
278
+
279
+ if (level.header == null && level.footer == null) {
280
+ issues.push(`${at} has neither a header nor a footer to show its groups`);
281
+ }
282
+ });
283
+ }
284
+
285
+
286
+ /**
287
+ * One of a table's group bands: a band in all but its place in the file, so
288
+ * its items are checked the same way. It may not hold a table - it is drawn
289
+ * once for every group, inside the table it belongs to.
290
+ *
291
+ * @param {*} band
292
+ * @param {string} where
293
+ * @param {string[]} issues
294
+ */
295
+ function validateGroupBand(band, where, issues) {
296
+ if (band == null) return;
297
+
298
+ if (typeof band !== 'object' || Array.isArray(band)) {
299
+ issues.push(`${where} must be a band with items, or null`);
300
+ return;
301
+ }
302
+
303
+ if (band.height != null && !(typeof band.height === 'number' && band.height >= 0)) {
304
+ issues.push(`${where}.height must be a number of pixels`);
305
+ }
306
+
307
+ if (band.background != null && (typeof band.background !== 'string' || band.background === '')) {
308
+ issues.push(`${where}.background must be a colour, or null`);
309
+ }
310
+
311
+ if (!Array.isArray(band.items)) {
312
+ issues.push(`${where}.items must be an array`);
313
+ return;
314
+ }
315
+
316
+ band.items.forEach((item, j) => {
317
+ if (item?.type === 'table') {
318
+ issues.push(`${where}.items[${j}] is a table; a group band cannot hold one`);
319
+ return;
320
+ }
321
+
322
+ validateItem(item, `${where}.items[${j}]`, issues);
323
+ });
324
+ }
325
+
326
+
185
327
  function validatePage(page, issues) {
186
328
  if (page == null || typeof page !== 'object') {
187
329
  issues.push('layout.page must be an object with width, height and margin');
@@ -209,6 +351,45 @@ function validatePage(page, issues) {
209
351
  if (printable <= 0) {
210
352
  issues.push(`layout.page margins leave no printable height (${printable}px)`);
211
353
  }
354
+
355
+ validateMarginSwitches(page, issues);
356
+ }
357
+
358
+
359
+ /**
360
+ * `marginOff` holds the margins switched off in the designer, with the value
361
+ * each comes back as; `marginLinked` is the lock that makes the four one
362
+ * number. The engine never reads either - a side that is off prints from
363
+ * `margin`, as 0 - so the one thing worth catching is a file that says a side
364
+ * is off while still printing a margin there.
365
+ */
366
+ function validateMarginSwitches(page, issues) {
367
+ if (page.marginLinked != null && typeof page.marginLinked !== 'boolean') {
368
+ issues.push('layout.page.marginLinked must be true or false');
369
+ }
370
+
371
+ if (page.marginOff == null) return;
372
+
373
+ if (typeof page.marginOff !== 'object' || Array.isArray(page.marginOff)) {
374
+ issues.push('layout.page.marginOff must be an object of the margins switched off');
375
+ return;
376
+ }
377
+
378
+ for (const [side, value] of Object.entries(page.marginOff)) {
379
+ if (!['top', 'right', 'bottom', 'left'].includes(side)) {
380
+ issues.push(`layout.page.marginOff.${side} is not a side - use top, right, bottom or left`);
381
+ continue;
382
+ }
383
+
384
+ if (!(typeof value === 'number' && value >= 0)) {
385
+ issues.push(`layout.page.marginOff.${side} must be a number of pixels`);
386
+ }
387
+
388
+ if (page.margin[side] !== 0) {
389
+ issues.push(`layout.page.margin.${side} must be 0 while that margin is switched off ` +
390
+ '(it is listed in layout.page.marginOff)');
391
+ }
392
+ }
212
393
  }
213
394
 
214
395
 
@@ -231,6 +412,8 @@ function validateItem(item, where, issues) {
231
412
  if (typeof item.value !== 'string') {
232
413
  issues.push(`${where}.value must be a string`);
233
414
  }
415
+ validateFormat(item.format, `${where}.format`, issues, true);
416
+ validateHighlight(item.highlight, `${where}.highlight`, issues, false);
234
417
  return;
235
418
  }
236
419
 
@@ -275,7 +458,120 @@ function validateItem(item, where, issues) {
275
458
  if (col == null || typeof col.field !== 'string' || col.field === '') {
276
459
  issues.push(`${where}.columns[${k}].field must be a non-empty string`);
277
460
  }
461
+ validateFormat(col?.format, `${where}.columns[${k}].format`, issues, true);
462
+
463
+ if (col?.expression != null && col.expression !== '') {
464
+ const parsed = typeof col.expression === 'string'
465
+ ? parseExpression(col.expression)
466
+ : { error: 'must be text' };
467
+ if (parsed.error) issues.push(`${where}.columns[${k}].expression ${parsed.error}`);
468
+ }
469
+ validateHighlight(col?.highlight, `${where}.columns[${k}].highlight`, issues, false);
278
470
  });
471
+
472
+ validateHighlight(item.rowHighlight, `${where}.rowHighlight`, issues, true);
473
+ }
474
+
475
+
476
+ /**
477
+ * A list of highlight rules: each a condition and the style it gives.
478
+ *
479
+ * @param {*} rules
480
+ * @param {string} where
481
+ * @param {string[]} issues
482
+ * @param {boolean} needsField whether every rule must name a field - a row's
483
+ * rules do, a row having no one value of its own to test
484
+ */
485
+ function validateHighlight(rules, where, issues, needsField) {
486
+ if (rules == null) return;
487
+
488
+ if (!Array.isArray(rules)) {
489
+ issues.push(`${where} must be a list of rules, or null`);
490
+ return;
491
+ }
492
+
493
+ rules.forEach((rule, n) => {
494
+ const at = `${where}[${n}]`;
495
+ const when = rule?.when;
496
+
497
+ if (when == null || typeof when !== 'object') {
498
+ issues.push(`${at}.when must say what to test`);
499
+ return;
500
+ }
501
+
502
+ if (!HIGHLIGHT_OPS.includes(when.op)) {
503
+ issues.push(`${at}.when.op "${when.op}" is not one of: ${HIGHLIGHT_OPS.join(', ')}`);
504
+ }
505
+
506
+ if (when.field != null && (typeof when.field !== 'string' || when.field === '')) {
507
+ issues.push(`${at}.when.field must be the name of a field`);
508
+ } else if (needsField && when.field == null) {
509
+ issues.push(`${at}.when.field must name the field of the row to test`);
510
+ }
511
+
512
+ if (when.op === 'between' && (when.value == null || when.to == null)) {
513
+ issues.push(`${at}.when needs both value and to for between`);
514
+ }
515
+
516
+ const style = rule.style;
517
+ if (style == null || typeof style !== 'object' || Array.isArray(style)) {
518
+ issues.push(`${at}.style must be an object`);
519
+ return;
520
+ }
521
+
522
+ for (const key of Object.keys(style)) {
523
+ if (!HIGHLIGHT_STYLE_KEYS.includes(key)) {
524
+ issues.push(`${at}.style.${key} is not one of: ${HIGHLIGHT_STYLE_KEYS.join(', ')}`);
525
+ }
526
+ }
527
+ });
528
+ }
529
+
530
+
531
+ /**
532
+ * A number or date format - a text item's, a column's, or the report's own
533
+ * defaults, which have every key but `type`. Every key is optional; what is
534
+ * there has to be one of the things format.js knows.
535
+ *
536
+ * @param {*} format
537
+ * @param {string} where
538
+ * @param {string[]} issues
539
+ * @param {boolean} typed whether it may say a `type` - the report's defaults may not
540
+ */
541
+ function validateFormat(format, where, issues, typed) {
542
+ if (format == null) return;
543
+
544
+ if (typeof format !== 'object' || Array.isArray(format)) {
545
+ issues.push(`${where} must be an object, or null`);
546
+ return;
547
+ }
548
+
549
+ const oneOf = (key, list) => {
550
+ if (format[key] != null && !list.includes(format[key])) {
551
+ issues.push(`${where}.${key} "${format[key]}" is not one of: ${list.join(', ')}`);
552
+ }
553
+ };
554
+
555
+ if (typed) oneOf('type', FORMAT_TYPES);
556
+ else if (format.type != null) {
557
+ issues.push(`${where}.type is an item's or a column's to say, not the report's`);
558
+ }
559
+
560
+ oneOf('grouping', GROUPINGS);
561
+ oneOf('negative', NEGATIVES);
562
+ oneOf('zero', ZEROS);
563
+ oneOf('symbolPosition', SYMBOL_POSITIONS);
564
+
565
+ if (format.decimals != null && !(Number.isInteger(format.decimals)
566
+ && format.decimals >= 0 && format.decimals <= MAX_DECIMALS)) {
567
+ issues.push(`${where}.decimals must be a whole number from 0 to ${MAX_DECIMALS}`);
568
+ }
569
+
570
+ for (const key of ['symbol', 'pattern']) {
571
+ if (format[key] != null && typeof format[key] !== 'string') {
572
+ issues.push(`${where}.${key} must be text`);
573
+ }
574
+ }
279
575
  }
280
576
 
281
577
 
@@ -238,15 +238,41 @@ export function excelBorder({ style = 'solid', width = 1, color = null } = {}) {
238
238
  export function cellFormat(design) {
239
239
  if (!design) return null;
240
240
 
241
- if (design.kind === 'text') return textFormat(design.style ?? {});
241
+ if (design.kind === 'text') return lit(textFormat(design.style ?? {}), design.highlight);
242
242
  if (design.kind === 'head' || design.kind === 'cell') {
243
- return tableFormat(design.kind === 'head', design.column ?? {}, design.table ?? {});
243
+ return lit(tableFormat(design.kind === 'head', design.column ?? {}, design.table ?? {}),
244
+ design.highlight);
244
245
  }
245
246
 
246
247
  return null;
247
248
  }
248
249
 
249
250
 
251
+ /**
252
+ * A format with a highlight rule's style laid over it: the text colour, the
253
+ * fill, bold and italic, each only where the rule set it - the same four the
254
+ * page draws, so a red figure on the page is a red figure in the sheet.
255
+ *
256
+ * @param {object} format from textFormat or tableFormat
257
+ * @param {object|null|undefined} highlight from highlight.js
258
+ * @returns {object}
259
+ */
260
+ function lit(format, highlight) {
261
+ if (!highlight) return format;
262
+
263
+ const font = { ...format.font };
264
+ if (highlight.color) font.color = argb(highlight.color) ?? font.color;
265
+ if (highlight.fontWeight) font.bold = isBold(highlight.fontWeight);
266
+ if (highlight.fontStyle) font.italic = highlight.fontStyle === 'italic' || highlight.fontStyle === 'oblique';
267
+
268
+ return {
269
+ ...format,
270
+ font,
271
+ fill: highlight.background ? (argb(highlight.background) ?? format.fill) : format.fill
272
+ };
273
+ }
274
+
275
+
250
276
  /**
251
277
  * A text item: its own font, colour and alignment. Text sits at the top of its
252
278
  * box on the page and never wraps here - a cell's neighbours are empty more
@@ -343,6 +369,20 @@ export function createStyles() {
343
369
  const borders = ['<border><left/><right/><top/><bottom/><diagonal/></border>'];
344
370
  const xfs = ['<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>'];
345
371
 
372
+ /** custom number format codes, by code; Excel numbers its own from 164 up */
373
+ const numFmts = new Map();
374
+
375
+ /**
376
+ * The id of a custom number format, such as "#,##0.00" or "dd/mm/yyyy",
377
+ * adding it the first time it is asked for.
378
+ * @param {string} code
379
+ * @returns {number}
380
+ */
381
+ function numFmt(code) {
382
+ if (!numFmts.has(code)) numFmts.set(code, 164 + numFmts.size);
383
+ return numFmts.get(code);
384
+ }
385
+
346
386
  const index = new Map([
347
387
  ...fonts.map((x, i) => [x, i]),
348
388
  ...fills.map((x, i) => [x, i]),
@@ -401,7 +441,8 @@ export function createStyles() {
401
441
 
402
442
  /**
403
443
  * @param {object|null} format from cellFormat, or { font: { bold } } and the like
404
- * @param {number} [numFmtId=0] a built-in number format, such as 14 for a date
444
+ * @param {number} [numFmtId=0] a number format's id: built in, such as 14 for a
445
+ * date, or one numFmt handed out
405
446
  * @returns {number} the cell format's index
406
447
  */
407
448
  function xf(format, numFmtId = 0) {
@@ -434,6 +475,11 @@ export function createStyles() {
434
475
  function xml() {
435
476
  return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n' +
436
477
  '<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">' +
478
+ (numFmts.size
479
+ ? `<numFmts count="${numFmts.size}">${[...numFmts]
480
+ .map(([code, id]) => `<numFmt numFmtId="${id}" formatCode="${escapeAttr(code)}"/>`)
481
+ .join('')}</numFmts>`
482
+ : '') +
437
483
  `<fonts count="${fonts.length}">${fonts.join('')}</fonts>` +
438
484
  `<fills count="${fills.length}">${fills.join('')}</fills>` +
439
485
  `<borders count="${borders.length}">${borders.join('')}</borders>` +
@@ -443,7 +489,7 @@ export function createStyles() {
443
489
  '</styleSheet>';
444
490
  }
445
491
 
446
- return { xf, xml, get size() { return xfs.length; } };
492
+ return { xf, numFmt, xml, get size() { return xfs.length; } };
447
493
  }
448
494
 
449
495