report-studio 1.2.2 → 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
@@ -11,6 +11,10 @@
11
11
  * Nothing here invents a field the engine does not read.
12
12
  */
13
13
 
14
+ import { levelsOf } from '../engine/tables.js';
15
+ import { setGroupBy } from './grouping.js';
16
+ import { formatValue, DATE_PATTERNS, CURRENCY_SYMBOLS } from '../engine/format.js';
17
+
14
18
 
15
19
  /**
16
20
  * Spec 10 left the font list open, and closes it here: web-safe families only.
@@ -107,7 +111,13 @@ export const COLUMN_FIELDS = [
107
111
  { key: 'field', label: 'Field', type: 'text' },
108
112
  { key: 'label', label: 'Label', type: 'text' },
109
113
  { key: 'width', label: 'Width', type: 'number', min: 1 },
110
- { key: 'align', label: 'Align', type: 'choice', options: ALIGNMENTS }
114
+ { key: 'align', label: 'Align', type: 'choice', options: ALIGNMENTS },
115
+ {
116
+ key: 'expression', label: 'Calculation', type: 'text',
117
+ hint: 'Blank shows the field as it is. A calculation - {qty} * {price}, ' +
118
+ 'round({amount} * 0.18, 2), percentage({amount}) - is worked out for ' +
119
+ "each row and saved under this column's field"
120
+ }
111
121
  ];
112
122
 
113
123
 
@@ -219,11 +229,7 @@ export function fieldsFor(item) {
219
229
  hint: "The key in the data object whose rows this table " +
220
230
  "draws. Blank reads the report's dataset"
221
231
  },
222
- {
223
- key: 'groupBy', label: 'Group by', type: 'text', nullable: true,
224
- hint: 'A field to group these rows by, or blank. One table ' +
225
- 'in a report can be grouped'
226
- }
232
+ GROUP_BY
227
233
  ]
228
234
  },
229
235
  {
@@ -490,6 +496,11 @@ function pageFields(layout) {
490
496
  * @returns {object[]} `{ title, fields }` sections
491
497
  */
492
498
  export function reportFields(layout) {
499
+ /**
500
+ * No *Group by* here any more. Grouping is a table's own - each grouped
501
+ * table carries its field and the bands its groups are drawn with - so it
502
+ * is set on the table, where there is no question which one it means.
503
+ */
493
504
  if (!layout) return [];
494
505
 
495
506
  return [
@@ -501,11 +512,6 @@ export function reportFields(layout) {
501
512
  key: 'dataset', label: 'Dataset', type: 'text', nullable: true,
502
513
  hint: 'The key in the data object that drives the rows, ' +
503
514
  'for every table that does not name its own'
504
- },
505
- {
506
- key: 'groupBy', label: 'Group by', type: 'text', nullable: true,
507
- hint: 'A field name, or blank for no grouping. Groups the ' +
508
- 'first table, unless a table sets its own'
509
515
  }
510
516
  ]
511
517
  },
@@ -521,11 +527,72 @@ export function reportFields(layout) {
521
527
  num('page.margin.bottom', 'Bottom', { min: 0 }),
522
528
  num('page.margin.left', 'Left', { min: 0 })
523
529
  ]
530
+ },
531
+ {
532
+ /**
533
+ * What every number and date format in the report falls back on.
534
+ * A text item or a column says it is money or a date; how money
535
+ * and dates are written is said once, here.
536
+ */
537
+ title: 'Numbers and dates',
538
+ fields: [
539
+ {
540
+ key: 'format.grouping', label: 'Digit groups', type: 'choice',
541
+ options: [
542
+ { value: 'international', label: '1,234,567' },
543
+ { value: 'indian', label: '12,34,567 (Indian)' },
544
+ { value: 'none', label: '1234567' }
545
+ ]
546
+ },
547
+ {
548
+ key: 'format.symbol', label: 'Currency', type: 'choice',
549
+ options: CURRENCY_SYMBOLS, custom: true,
550
+ hint: 'The symbol currency formats print; Custom… for one not listed'
551
+ },
552
+ {
553
+ key: 'format.symbolPosition', label: 'Symbol goes', type: 'choice',
554
+ options: [
555
+ { value: 'before', label: 'Before ₹100' },
556
+ { value: 'after', label: 'After 100 ₹' }
557
+ ]
558
+ },
559
+ {
560
+ key: 'format.pattern', label: 'Dates', type: 'choice',
561
+ options: datePatternChoices(layout.format?.pattern),
562
+ hint: 'How a date prints unless its own format says otherwise'
563
+ }
564
+ ],
565
+ actions: [{
566
+ action: 'format-india', label: 'Use Indian: ₹, 1,00,000, dd/MM/yyyy',
567
+ title: 'Indian digit groups, the rupee and day/month/year dates'
568
+ }]
524
569
  }
525
570
  ];
526
571
  }
527
572
 
528
573
 
574
+ /**
575
+ * The date layouts a choice offers, each shown as the date it prints - ISO
576
+ * first, being the default. A pattern the report already has that is not one
577
+ * of them is offered too, so the choice can show it.
578
+ * @param {string|undefined} current
579
+ * @returns {{value: string, label: string}[]}
580
+ */
581
+ export function datePatternChoices(current) {
582
+ const patterns = ['yyyy-MM-dd', ...DATE_PATTERNS.filter(p => p !== 'yyyy-MM-dd')];
583
+ if (typeof current === 'string' && current && !patterns.includes(current)) patterns.push(current);
584
+
585
+ return patterns.map(pattern => ({
586
+ value: pattern,
587
+ label: formatValue(SAMPLE_DATE, { type: 'date', pattern })
588
+ }));
589
+ }
590
+
591
+
592
+ /** the day the format choices are shown with */
593
+ const SAMPLE_DATE = '2026-06-17';
594
+
595
+
529
596
  export function allReportFields(layout) {
530
597
  return reportFields(layout).flatMap(section => section.fields);
531
598
  }
@@ -537,6 +604,25 @@ export function allFields(item) {
537
604
  }
538
605
 
539
606
 
607
+ /**
608
+ * A table's *Group by*: the field its own grouping splits the rows by.
609
+ *
610
+ * Derived rather than a path, because there is more to turning grouping on
611
+ * than one value - a table that was not grouped gets a header to show its
612
+ * groups in, and clearing the box takes the grouping away. setGroupBy is where
613
+ * that is decided. It rebuilds the rail when it turns grouping on or off,
614
+ * since that is when the table's group band controls come and go.
615
+ */
616
+ const GROUP_BY = {
617
+ key: 'groupBy', label: 'Group by', type: 'text', nullable: true,
618
+ hint: 'A field to group these rows by, or blank. Each grouped table has ' +
619
+ 'its own group header and footer',
620
+ read: (table) => levelsOf(table)[0]?.by ?? table.groupBy ?? null,
621
+ write: (table, value) => setGroupBy(table, value),
622
+ rebuilds: (table) => levelsOf(table).length > 0
623
+ };
624
+
625
+
540
626
  function pathOf(key) {
541
627
  return key.split('.');
542
628
  }
@@ -567,6 +653,37 @@ export function readField(item, field) {
567
653
  }
568
654
 
569
655
 
656
+ /**
657
+ * Takes a value off an item, so it is as if it had never been set - which for
658
+ * a colour is "none": a box with no fill, text in the stylesheet's colour.
659
+ *
660
+ * Removed rather than written as null, because absent is what an item that was
661
+ * never given one says, and the engine and the renderer read the two alike.
662
+ *
663
+ * @param {object} item mutated in place
664
+ * @param {object} field
665
+ * @returns {boolean} true when there was a value to take off
666
+ */
667
+ export function clearField(item, field) {
668
+ if (field.read || field.write) return false;
669
+
670
+ const path = pathOf(field.key);
671
+ const last = path.pop();
672
+
673
+ let node = item;
674
+ for (const step of path) {
675
+ if (node?.[step] == null || typeof node[step] !== 'object') return false;
676
+ node = node[step];
677
+ }
678
+
679
+ /** a null standing in the file is already none */
680
+ if (node[last] == null) return false;
681
+
682
+ delete node[last];
683
+ return true;
684
+ }
685
+
686
+
570
687
  /**
571
688
  * Puts a typed value onto an item, and says whether anything changed.
572
689
  *
@@ -9,7 +9,8 @@
9
9
  */
10
10
 
11
11
 
12
- import { DESIGN_ROWS, designHeight } from '../engine/measure.js';
12
+ import { DESIGN_ROWS, designHeight as printedDesignHeight } from '../engine/measure.js';
13
+ import { levelsOf } from '../engine/tables.js';
13
14
 
14
15
 
15
16
  /** spec 5.2: positions snap to a 10px grid */
@@ -51,12 +52,84 @@ export function snap(value) {
51
52
  * equivalent, and it is the same figure the canvas draws with, so the selection
52
53
  * outline lands exactly on the table rather than near it.
53
54
  *
54
- * Re-exported from the engine rather than written twice: it is also the height
55
+ * Built on the engine's figure rather than written twice: that is the height
55
56
  * paginate.js treats as the table's designed size, and the canvas and the
56
- * printed page have to agree about that or nothing below a table lands where it
57
+ * printed page have to agree about it or nothing below a table lands where it
57
58
  * was put.
59
+ *
60
+ * A grouped table is drawn as one group - its column headings, its group
61
+ * header, the sample rows, its group footer - because that is the unit it
62
+ * prints in, so its box on the canvas is taller by its two group bands. The
63
+ * engine's figure leaves them out, and a table grows from that figure at print
64
+ * time, so what sits below the table still clears it there.
65
+ *
66
+ * @param {object} item
67
+ * @returns {number} pixels
68
+ */
69
+ export function designHeight(item) {
70
+ const height = printedDesignHeight(item);
71
+ if (item?.type !== 'table') return height;
72
+
73
+ return height + levelsOf(item).reduce((sum, level) =>
74
+ sum + stripHeight(level?.header) + stripHeight(level?.footer), 0);
75
+ }
76
+
77
+
78
+ /**
79
+ * Where every group band of a grouped table sits inside its box, in the order a
80
+ * group draws them: below the column headings each level's header, outermost
81
+ * first; then the sample rows; then each level's footer, innermost first.
82
+ *
83
+ * @param {object} table
84
+ * @returns {{kind: string, level: number, band: object, y: number, h: number}[]}
85
+ * offsets from the table's own top; only the bands the table has
86
+ */
87
+ export function levelStrips(table) {
88
+ const levels = levelsOf(table);
89
+
90
+ const rowHeight = table?.rowHeight ?? 0;
91
+ let y = table?.showHeader ? (table.headerHeight ?? rowHeight) : 0;
92
+
93
+ const strips = [];
94
+ const add = (kind, level, band) => {
95
+ if (!band) return;
96
+ const h = stripHeight(band);
97
+ strips.push({ kind, level, band, y, h });
98
+ y += h;
99
+ };
100
+
101
+ levels.forEach((level, n) => add('groupHeader', n, level?.header));
102
+ y += DESIGN_ROWS * rowHeight;
103
+ for (let n = levels.length - 1; n >= 0; n--) add('groupFooter', n, levels[n]?.footer);
104
+
105
+ return strips;
106
+ }
107
+
108
+
109
+ /**
110
+ * Where a grouped table's outermost group bands sit inside its box - what a
111
+ * table grouped at one level has. levelStrips has every level's.
112
+ *
113
+ * @param {object} table
114
+ * @returns {{groupHeader: {y: number, h: number}|null,
115
+ * groupFooter: {y: number, h: number}|null}}
116
+ * offsets from the table's own top; null for a band the table does not have
58
117
  */
59
- export { designHeight };
118
+ export function groupStrips(table) {
119
+ const outer = (kind) => {
120
+ const strip = levelStrips(table).find(s => s.kind === kind && s.level === 0);
121
+ return strip ? { y: strip.y, h: strip.h } : null;
122
+ };
123
+
124
+ return { groupHeader: outer('groupHeader'), groupFooter: outer('groupFooter') };
125
+ }
126
+
127
+
128
+ /** a group band's height, in pixels - only a number counts, as in validate.js */
129
+ export function stripHeight(band) {
130
+ const height = Number(band?.height);
131
+ return band && Number.isFinite(height) && height > 0 ? height : 0;
132
+ }
60
133
 
61
134
 
62
135
  /** an item's box in band-relative coordinates */