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
@@ -0,0 +1,224 @@
1
+ /**
2
+ * highlight.js decides how a value is styled by the rules it is given -
3
+ * Crystal's Highlighting Expert: a list of conditions, each with a style, the
4
+ * first that holds being the one used.
5
+ *
6
+ * highlight: [
7
+ * { when: { op: '<', value: 0 }, style: { color: '#b3261e' } },
8
+ * { when: { field: 'status', op: '=', value: 'Overdue' }, style: { background: '#fdecec' } }
9
+ * ]
10
+ *
11
+ * Rules sit in three places, each answered here:
12
+ *
13
+ * a table column's `highlight` that column's cells; a rule tests the cell's
14
+ * value, or another field of its row
15
+ * a table's `rowHighlight` whole rows; a rule tests a field of the row
16
+ * a text item's `highlight` the item; a rule tests the one value it
17
+ * prints, or a field of the data
18
+ *
19
+ * A style changes the text colour, the background, bold and italic - and only
20
+ * what it names, so a rule that turns a figure red leaves the font alone.
21
+ *
22
+ * Values are compared as what they are: numbers as numbers ("1200" included),
23
+ * dates by their day, anything else as text without regard to case. Nothing
24
+ * here touches the document; render/, measure.js and the workbook all ask the
25
+ * same function, so the page, its row heights and the sheet cannot disagree.
26
+ */
27
+
28
+ import { toNumber, dateParts } from './format.js';
29
+
30
+
31
+ /** what a rule can test for, in the order the designer offers them */
32
+ export const HIGHLIGHT_OPS = [
33
+ '=', '!=', '<', '<=', '>', '>=', 'between', 'contains', 'startsWith', 'empty', 'notEmpty'
34
+ ];
35
+
36
+ /** the operators that take no value to compare with */
37
+ export const UNARY_OPS = ['empty', 'notEmpty'];
38
+
39
+ /** what a rule's style may set */
40
+ export const HIGHLIGHT_STYLE_KEYS = ['color', 'background', 'fontWeight', 'fontStyle'];
41
+
42
+
43
+ /**
44
+ * The style the first rule that holds gives a value, or null when none does.
45
+ *
46
+ * @param {object[]|null|undefined} rules
47
+ * @param {*} value what is being drawn - a cell's value, a text item's
48
+ * @param {object|null} [row] what `field` is looked up in - the cell's row, or
49
+ * the data a text item was filled from
50
+ * @returns {object|null} only the keys the rule's style sets
51
+ */
52
+ export function highlightFor(rules, value, row = null) {
53
+ if (!Array.isArray(rules)) return null;
54
+
55
+ for (const rule of rules) {
56
+ const when = rule?.when;
57
+ if (!when || !HIGHLIGHT_OPS.includes(when.op)) continue;
58
+
59
+ const subject = typeof when.field === 'string' && when.field !== ''
60
+ ? lookup(row, when.field)
61
+ : value;
62
+
63
+ if (holds(subject, when)) return cleanStyle(rule.style);
64
+ }
65
+
66
+ return null;
67
+ }
68
+
69
+
70
+ /**
71
+ * How one table cell is styled by the rules over it: the table's row rules,
72
+ * then its column's own on top - a red figure in a shaded row is both.
73
+ *
74
+ * @param {object} table
75
+ * @param {object} column
76
+ * @param {object} row
77
+ * @returns {object|null}
78
+ */
79
+ export function cellHighlight(table, column, row) {
80
+ const forRow = highlightFor(table?.rowHighlight, null, row);
81
+ const forCell = highlightFor(column?.highlight, row?.[column?.field], row);
82
+
83
+ if (!forRow && !forCell) return null;
84
+ return { ...(forRow ?? {}), ...(forCell ?? {}) };
85
+ }
86
+
87
+
88
+ /**
89
+ * One line a person reads to know what the rules are - the rail's summary.
90
+ * @param {object[]|null|undefined} rules
91
+ * @returns {string}
92
+ */
93
+ export function describeHighlight(rules) {
94
+ const count = Array.isArray(rules) ? rules.length : 0;
95
+ if (count === 0) return 'None';
96
+ return count === 1 ? '1 rule' : `${count} rules`;
97
+ }
98
+
99
+
100
+ /**
101
+ * A rule's condition in words: "is less than 0", "status equals Overdue".
102
+ * @param {object} when
103
+ * @param {(value: *) => string} [show] how an operand is written
104
+ * @returns {string}
105
+ */
106
+ export function describeCondition(when, show = String) {
107
+ const words = {
108
+ '=': 'equals', '!=': 'is not', '<': 'is less than', '<=': 'is at most',
109
+ '>': 'is more than', '>=': 'is at least', between: 'is between',
110
+ contains: 'contains', startsWith: 'starts with', empty: 'is empty',
111
+ notEmpty: 'is not empty'
112
+ }[when?.op] ?? when?.op;
113
+
114
+ const subject = when?.field ? `${when.field} ` : '';
115
+ if (UNARY_OPS.includes(when?.op)) return `${subject}${words}`;
116
+ if (when?.op === 'between') return `${subject}${words} ${show(when.value)} and ${show(when.to)}`;
117
+
118
+ return `${subject}${words} ${show(when?.value)}`;
119
+ }
120
+
121
+
122
+ /* ---------------- comparing ---------------- */
123
+
124
+
125
+ function holds(subject, when) {
126
+ const { op } = when;
127
+
128
+ if (op === 'empty') return isEmpty(subject);
129
+ if (op === 'notEmpty') return !isEmpty(subject);
130
+
131
+ /** nothing is less than, more than or equal to a blank - it is blank */
132
+ if (isEmpty(subject)) return op === '!=' && !isEmpty(when.value);
133
+
134
+ if (op === 'contains') return text(subject).includes(text(when.value));
135
+ if (op === 'startsWith') return text(subject).startsWith(text(when.value));
136
+
137
+ if (op === 'between') {
138
+ const low = compare(subject, when.value);
139
+ const high = compare(subject, when.to);
140
+ return low !== null && high !== null && low >= 0 && high <= 0;
141
+ }
142
+
143
+ const order = compare(subject, when.value);
144
+ if (order === null) return op === '!=';
145
+
146
+ switch (op) {
147
+ case '=': return order === 0;
148
+ case '!=': return order !== 0;
149
+ case '<': return order < 0;
150
+ case '<=': return order <= 0;
151
+ case '>': return order > 0;
152
+ case '>=': return order >= 0;
153
+ default: return false;
154
+ }
155
+ }
156
+
157
+
158
+ /**
159
+ * Which way two values order - negative, zero or positive - compared as the
160
+ * kind of thing they both are; null when there is nothing to compare with.
161
+ *
162
+ * Two values of different kinds do not order at all: "Pending" is neither
163
+ * more nor less than 1000, and saying it was - by the alphabet, where letters
164
+ * come after digits - would shade every word in a column of figures. They can
165
+ * still be equal or not, as text: "007" is "007".
166
+ */
167
+ function compare(a, b) {
168
+ if (b === undefined || b === null || b === '') return null;
169
+
170
+ const x = toNumber(a);
171
+ const y = toNumber(b);
172
+ if (x !== null && y !== null) return Math.sign(x - y);
173
+
174
+ const p = dateParts(a);
175
+ const q = dateParts(b);
176
+ if (p && q) return Math.sign(dayValue(p) - dayValue(q));
177
+
178
+ if (kind(a) !== kind(b)) return null;
179
+
180
+ return Math.sign(text(a).localeCompare(text(b)));
181
+ }
182
+
183
+
184
+ /** what a value is, for deciding whether two can be ordered */
185
+ function kind(value) {
186
+ if (toNumber(value) !== null) return 'number';
187
+ if (dateParts(value)) return 'date';
188
+ return 'text';
189
+ }
190
+
191
+
192
+ /** a date as one number that orders the way the dates do */
193
+ function dayValue(p) {
194
+ return ((((p.year * 100 + p.month) * 100 + p.day) * 100 + p.hour) * 100 + p.minute) * 100 + p.second;
195
+ }
196
+
197
+
198
+ function isEmpty(value) {
199
+ return value === null || value === undefined || String(value).trim() === '';
200
+ }
201
+
202
+
203
+ function text(value) {
204
+ return String(value ?? '').trim().toLowerCase();
205
+ }
206
+
207
+
208
+ /** a dotted path - `customer.balance` - into the row or the data */
209
+ function lookup(row, path) {
210
+ return path.split('.').reduce((node, part) => node?.[part], row ?? undefined);
211
+ }
212
+
213
+
214
+ /** only the keys a style may set, and only those it sets */
215
+ function cleanStyle(style) {
216
+ const out = {};
217
+
218
+ for (const key of HIGHLIGHT_STYLE_KEYS) {
219
+ const value = style?.[key];
220
+ if (typeof value === 'string' && value !== '') out[key] = value;
221
+ }
222
+
223
+ return out;
224
+ }
@@ -20,8 +20,15 @@ export { search, searchPages } from './search.js';
20
20
  export { pageRegions, bandZoneHeight, resolveHeight, DEFAULT_BAND_HEIGHTS } from './regions.js';
21
21
  export { toCSV, toReportCSV, reportFilename, reportTables } from './csv.js';
22
22
  export { toXLSX, toXLSXAsync, reportImageSources, XLSX_MIME } from './xlsx.js';
23
- export { tablesOf, datasetOf, groupingOf } from './tables.js';
23
+ export {
24
+ tablesOf, datasetOf, groupingOf, groupingsOf, groupBandsOf, levelsOf, isGrouped, normalizeGrouping,
25
+ fragmentBandsOf, MAX_GROUP_LEVELS
26
+ } from './tables.js';
24
27
  export { measureText, wrapLines, textHeight, LINE_HEIGHT_RATIO } from './text-metrics.js';
28
+ export {
29
+ formatValue, describeFormat, excelNumberFormat, DEFAULT_FORMAT, INDIAN_DEFAULTS
30
+ } from './format.js';
31
+ export { highlightFor, cellHighlight, describeHighlight } from './highlight.js';
25
32
 
26
33
 
27
34
  /**
@@ -8,6 +8,9 @@
8
8
  */
9
9
 
10
10
  import { textHeight } from './text-metrics.js';
11
+ import { levelsOf, groupFrame } from './tables.js';
12
+ import { formatValue } from './format.js';
13
+ import { cellHighlight } from './highlight.js';
11
14
 
12
15
 
13
16
  /**
@@ -151,10 +154,17 @@ export function rowHeightsFor(item, rows) {
151
154
  const width = widths[index];
152
155
  if (!(width > 0)) continue;
153
156
 
154
- const value = row?.[column.field];
155
- if (value === null || value === undefined || value === '') continue;
157
+ /** measured as printed: ₹1,00,000.00 wraps where 100000 would not */
158
+ const value = formatValue(row?.[column.field], column.format);
159
+ if (value === '') continue;
156
160
 
157
- tallest = Math.max(tallest, textHeight(String(value), width, font));
161
+ /** a bold highlight is wider, and may wrap where the plain text did not */
162
+ const lit = cellHighlight(item, column, row);
163
+ const drawn = lit?.fontWeight || lit?.fontStyle
164
+ ? { ...font, fontWeight: lit.fontWeight ?? font.fontWeight, fontStyle: lit.fontStyle ?? font.fontStyle }
165
+ : font;
166
+
167
+ tallest = Math.max(tallest, textHeight(String(value), width, drawn));
158
168
  }
159
169
 
160
170
  return tallest;
@@ -289,7 +299,8 @@ function measureItem(item, json) {
289
299
  */
290
300
  function measureTextItem(item) {
291
301
  const declared = item.h ?? 0;
292
- const wrapped = textHeight(item.text ?? item.value ?? '', item.w ?? 0, item.style);
302
+ const wrapped = textHeight(item.text ?? item.value ?? '', item.w ?? 0,
303
+ item.highlighted ? { ...(item.style ?? {}), ...item.highlighted } : item.style);
293
304
  return Math.max(declared, wrapped);
294
305
  }
295
306
 
@@ -344,20 +355,25 @@ function measureTableItem(item, json) {
344
355
  return headerHgt + total(item.rowHeights);
345
356
  }
346
357
 
347
- const groups = item.groups ?? [];
348
- const grpHeaderHgt = bandHeight(json, 'groupHeader');
349
- const grpFooterHgt = bandHeight(json, 'groupFooter');
358
+ /** every level's bands, so each carries the measuredHeight paginate.js reads */
359
+ for (const level of levelsOf(item)) {
360
+ groupBandHeight(level?.header, json);
361
+ groupBandHeight(level?.footer, json);
362
+ }
350
363
 
351
364
  let height = 0;
352
365
 
353
- for (const group of groups) {
366
+ /** the table whole, on one page: only its first group is first on the page */
367
+ item.groups.forEach((group, n) => {
354
368
  group.rowHeights = rowHeightsFor(item, group.rows);
355
369
 
356
- height += headerHgt
357
- + grpHeaderHgt
370
+ const frame = groupFrame(item, group, n === 0, true);
371
+
372
+ height += (frame.columnHeader ? headerHgt : 0)
373
+ + bandsHeight(frame.headers)
358
374
  + total(group.rowHeights)
359
- + grpFooterHgt;
360
- }
375
+ + bandsHeight(frame.footers);
376
+ });
361
377
 
362
378
  return height;
363
379
  }
@@ -370,22 +386,33 @@ export function total(list) {
370
386
 
371
387
 
372
388
  /**
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
389
+ * What a list of group bands from groupFrame is worth, by the heights measured
390
+ * onto them.
391
+ * @param {{band: object}[]} framed
378
392
  * @returns {number}
379
393
  */
380
- function bandHeight(json, type) {
381
- const band = json.bands.find(b => b.type === type);
382
- if (!band?.items) return 0;
394
+ export function bandsHeight(framed) {
395
+ return total(framed.map(({ band }) => band?.measuredHeight ?? 0));
396
+ }
383
397
 
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
398
 
390
- return Math.max(band.height ?? 0, contentHeight);
399
+ /**
400
+ * Height of a template band that never flows on its own - one of a grouped
401
+ * table's group bands. It is measured here rather than read raw so a group band
402
+ * whose text wraps is still accounted for.
403
+ *
404
+ * Measured with measureBand, the same as every other band, and the answer left
405
+ * on the band as measuredHeight. paginate.js reserves room for a group by that
406
+ * figure, so this has to be the same figure: a separate count of text items
407
+ * only, as there was, said a group band holding a box taller than its text was
408
+ * shorter here than it was there.
409
+ *
410
+ * @param {object|null} band
411
+ * @param {object} json
412
+ * @returns {number}
413
+ */
414
+ function groupBandHeight(band, json) {
415
+ if (!Array.isArray(band?.items)) return 0;
416
+
417
+ return measureBand(band, json).measuredHeight;
391
418
  }