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,518 @@
1
+ /**
2
+ * items.js draws the things that live inside a band: text boxes, tables,
3
+ * rules, boxes and images.
4
+ *
5
+ * Split out of render.js because the designer canvas needs exactly this and
6
+ * nothing more. The preview wraps these in anchored, paginated bands; the
7
+ * designer wraps them in band zones on a single unpaginated page. Spec 5.2 asks
8
+ * for one drawing path so that what is designed looks like what is printed -
9
+ * two paths drift, and the drift is always found by a user rather than a test.
10
+ *
11
+ * Every element carries data-item-id and data-item-type. The preview ignores
12
+ * them; the designer uses them to take a pointer event back to a layout item
13
+ * without maintaining a parallel element map that can fall out of step.
14
+ */
15
+
16
+
17
+ /** items are placed against the band box, which the caller has made relative */
18
+ export const position = (p) => `position:absolute;top:${px(p?.y)}px;left:${px(p?.x)}px;`;
19
+
20
+
21
+ /**
22
+ * A number on its way into a style attribute.
23
+ *
24
+ * A layout is JSON, and JSON says nothing about what type a field holds: a `w`
25
+ * that arrived as a string writes `width:undefinedpx` at best and closes the
26
+ * attribute at worst. Anything that is not a finite number is the fallback.
27
+ *
28
+ * @param {*} value
29
+ * @param {number} [fallback]
30
+ * @returns {number}
31
+ */
32
+ export function px(value, fallback = 0) {
33
+ const n = Number(value);
34
+ return Number.isFinite(n) ? n : fallback;
35
+ }
36
+
37
+
38
+ /**
39
+ * A style *value* on its way into a `style="..."` attribute.
40
+ *
41
+ * `esc` is for text between tags; this is for the other half. Item text comes
42
+ * from the host's data and is escaped on the way in, but a colour, a font
43
+ * family or a background comes from the layout - and a layout is a file, which
44
+ * an application may well let its own users author. Written raw, a colour of
45
+ * `red" onmouseover="alert(1)` closed the attribute and put a live event
46
+ * handler on the element.
47
+ *
48
+ * Removed rather than entity-escaped: `"` inside a style attribute is a
49
+ * quote again by the time CSS parses it, so escaping would move the hole
50
+ * rather than close it. None of these characters appear in a colour, a length
51
+ * or a font stack, so nothing legitimate is lost - and `;` and `{}` go too, so
52
+ * a value cannot append a declaration of its own to the ones around it.
53
+ *
54
+ * Brackets stay, because `rgb(...)` and `linear-gradient(...)` are ordinary
55
+ * things to write in a layout. `url(` alone does not: it is the one CSS
56
+ * function that fetches, and a report that quietly calls home when it is
57
+ * opened is not a report anybody asked for.
58
+ *
59
+ * @param {*} value
60
+ * @param {string} [fallback] what an empty or absent value becomes
61
+ * @returns {string}
62
+ */
63
+ export function css(value, fallback = '') {
64
+ if (value == null) return fallback;
65
+
66
+ const clean = String(value)
67
+ .replace(/[<>"'`&;{}\\]/g, '')
68
+ .replace(/url\s*\(/gi, '')
69
+ .replace(/\s+/g, ' ')
70
+ .trim();
71
+
72
+ return clean === '' ? fallback : clean;
73
+ }
74
+
75
+
76
+ /**
77
+ * Everything that reaches the page came from user data, so it is escaped on the
78
+ * way into the markup. Without this a value like "<script>" is executed rather
79
+ * than printed.
80
+ * @param {*} value
81
+ * @returns {string}
82
+ */
83
+ export function esc(value) {
84
+ if (value == null) return '';
85
+ return String(value)
86
+ .replace(/&/g, '&amp;')
87
+ .replace(/</g, '&lt;')
88
+ .replace(/>/g, '&gt;')
89
+ .replace(/"/g, '&quot;')
90
+ .replace(/'/g, '&#39;');
91
+ }
92
+
93
+
94
+ /**
95
+ * Draws every item in a band, in layout order.
96
+ * @param {object[]} list the band's items
97
+ * @returns {string} markup
98
+ */
99
+ export function items(list) {
100
+ return (list || []).map(i => {
101
+ switch (i.type) {
102
+ case "text":
103
+ return text(i);
104
+ case "table":
105
+ return table(i)
106
+ case "line":
107
+ return line(i)
108
+ case "box":
109
+ return box(i)
110
+ case "image":
111
+ return image(i)
112
+ default:
113
+ console.warn(`Unknown item type "${i?.type}"; it is not drawn.`);
114
+ return '';
115
+ }
116
+ }).join("");
117
+ }
118
+
119
+
120
+ function text(i) {
121
+ /**
122
+ * `?? {}` the way box and line have always had it. A text item with no
123
+ * style block is a layout validateLayout accepts - it asks for a style to
124
+ * be an object *if there is one* - and reading straight through it threw a
125
+ * TypeError out of the renderer for a layout the validator had just called
126
+ * fine. Written by hand or generated by an API rather than drawn, but the
127
+ * two halves of the package have to agree about what they accept.
128
+ */
129
+ const style = i.style ?? {};
130
+
131
+ return `
132
+ <p id="${esc(i.id)}" style="
133
+ width:${px(i.w)}px;
134
+ height:${px(i.h)}px;
135
+ ${position(i)}
136
+ font-size:${px(style.fontSize, 12)}px;
137
+ font-family:${css(style.fontFamily, 'inherit')};
138
+ font-weight:${css(style.fontWeight, 'normal')};
139
+ font-style:${css(style.fontStyle, 'normal')};
140
+ text-align:${css(style.align, 'left')};
141
+ color:${css(style.color, 'inherit')};
142
+ " data-item-id="${esc(i.id)}" data-item-type="text">${esc(i.text)}</p>
143
+ `
144
+ }
145
+
146
+
147
+ /**
148
+ * A rectangle: a rule bent round four sides, with an optional fill.
149
+ *
150
+ * `box-sizing: border-box` in the stylesheet, so `w` and `h` are the outside of
151
+ * the box however thick its border - which is what the designer draws round it
152
+ * and what the engine paginated against. A border that added to them would put
153
+ * the two a few pixels apart at every thickness.
154
+ *
155
+ * Drawn before the items above it, because paginate orders a band by `y` and a
156
+ * backdrop is placed at the top of what it sits behind.
157
+ */
158
+ function box(i) {
159
+ const style = i.style ?? {};
160
+ const width = Math.max(0, Number(style.borderWidth ?? 1)) || 1;
161
+
162
+ const pen = RULE_STYLES.includes(style.borderStyle) ? style.borderStyle : 'solid';
163
+ const border = `${width}px ${pen} ${css(style.borderColor, '#000000')}`;
164
+
165
+ const radius = Math.max(0, Number(style.radius ?? 0)) || 0;
166
+
167
+ const background = css(style.background);
168
+
169
+ return `
170
+ <div id="${esc(i.id)}" class="box" style="
171
+ width:${px(i.w)}px;
172
+ height:${px(i.h)}px;
173
+ ${position(i)}
174
+ border:${border};
175
+ ${radius ? `border-radius:${radius}px;` : ''}
176
+ ${background ? `background:${background};` : ''}
177
+ " data-item-id="${esc(i.id)}" data-item-type="box"></div>
178
+ `
179
+ }
180
+
181
+
182
+ /** how a picture may be fitted to its box; anything else is `contain` */
183
+ const IMAGE_FITS = ['contain', 'cover', 'fill'];
184
+
185
+ /**
186
+ * The raster formats a `data:` URI may carry.
187
+ *
188
+ * SVG is left out on purpose. As an <img> it cannot run script, but it is also
189
+ * the one image format that is a document, and a layout or a payload is a file
190
+ * an application may let its own users write - "save image as" and "open in
191
+ * new tab" both turn it back into a page that can.
192
+ */
193
+ const DATA_IMAGE = /^data:image\/(png|jpe?g|gif|webp|avif|bmp);base64,[a-z0-9+/=\s]*$/i;
194
+
195
+ /** the schemes an image may be fetched from, besides a data: URI and a relative path */
196
+ const IMAGE_SCHEMES = ['http:', 'https:', 'blob:'];
197
+
198
+
199
+ /**
200
+ * An image URL, or '' when it is not one a report should load.
201
+ *
202
+ * The picture's address comes from the layout or from the host's data, and it
203
+ * ends up in a `src` attribute - so `javascript:` and its relatives are refused
204
+ * here, once, for every route to the page. `esc` still runs over what is
205
+ * returned; this decides *whether* to load it, not how to write it down.
206
+ *
207
+ * Whitespace and control characters are stripped before the scheme is read,
208
+ * because a browser does the same: "java\tscript:" is `javascript:` to it.
209
+ *
210
+ * @param {*} value
211
+ * @returns {string}
212
+ */
213
+ export function imageSrc(value) {
214
+ if (typeof value !== 'string') return '';
215
+
216
+ const url = value.trim();
217
+ if (url === '') return '';
218
+
219
+ /** read for its scheme only - a space in the middle of a path is the path's */
220
+ const bare = url.replace(/[\u0000-\u0020\u007f]+/g, '');
221
+
222
+ if (/^data:/i.test(bare)) return DATA_IMAGE.test(bare) ? bare : '';
223
+
224
+ /** a scheme is letters up to the first colon, before any / ? or # */
225
+ const scheme = bare.match(/^([a-z][a-z0-9+.-]*:)/i)?.[1]?.toLowerCase();
226
+
227
+ if (scheme == null) return url;
228
+
229
+ return IMAGE_SCHEMES.includes(scheme) ? url : '';
230
+ }
231
+
232
+
233
+ /**
234
+ * A picture, scaled into a fixed box.
235
+ *
236
+ * The box is the item and the <img> fills it, so `w` and `h` mean what they
237
+ * mean for every other item and `fit` decides how the picture sits inside -
238
+ * letterboxed, cropped, or stretched. The engine paginated against `h`, and an
239
+ * image that grew to its natural size would undo that.
240
+ *
241
+ * Three states, told apart by `resolvedSrc`, which only the engine writes:
242
+ * a picture - drawn
243
+ * engine, no picture - an empty frame; the report prints nothing there
244
+ * designer canvas - a placeholder naming the field it will be read from,
245
+ * since a frame with nothing in it cannot be found again
246
+ */
247
+ function image(i) {
248
+ const built = Object.prototype.hasOwnProperty.call(i, 'resolvedSrc');
249
+ const src = imageSrc(built ? i.resolvedSrc : i.src);
250
+ const fit = IMAGE_FITS.includes(i.fit) ? i.fit : 'contain';
251
+
252
+ let inner = '';
253
+
254
+ if (src) {
255
+ inner = `<img src="${esc(src)}" alt="${esc(i.alt ?? '')}" ` +
256
+ `style="object-fit:${fit}" draggable="false">`;
257
+ } else if (!built) {
258
+ const field = typeof i.field === 'string' ? i.field.trim() : '';
259
+ inner = `<span class="image-empty">${field ? esc(`{${field}}`) : 'Image'}</span>`;
260
+ }
261
+
262
+ return `
263
+ <div id="${esc(i.id)}" class="image" style="
264
+ width:${px(i.w)}px;
265
+ height:${px(i.h)}px;
266
+ ${position(i)}
267
+ " data-item-id="${esc(i.id)}" data-item-type="image">${inner}</div>
268
+ `
269
+ }
270
+
271
+
272
+ /**
273
+ * The pen a line is drawn with, as one CSS border shorthand.
274
+ *
275
+ * `lineStyle` is whitelisted rather than passed through, the way tableRule
276
+ * already whitelists the grid's: the designer canvas draws straight from the
277
+ * layout without asking the validator, so a value it does not recognise should
278
+ * fall back to a rule rather than land in a style attribute.
279
+ */
280
+ function stroke(style) {
281
+ const thickness = Math.max(0, Number(style?.thickness ?? 1)) || 1;
282
+ const pen = LINE_STYLES.includes(style?.lineStyle) ? style.lineStyle : 'solid';
283
+
284
+ return `${thickness}px ${pen} ${css(style?.color, '#000000')}`;
285
+ }
286
+
287
+
288
+ /**
289
+ * A rule, drawn down the middle of the item's box.
290
+ *
291
+ * The box is not the rule: a 1px hairline that *was* its box would be a 1px
292
+ * target in the designer, and an item nobody can click is an item nobody can
293
+ * edit. So the box is whatever `h` (or `w`, running vertically) says, and the
294
+ * rule sits centred inside it - which is also where an eye expects a rule that
295
+ * separates one thing from another to fall.
296
+ *
297
+ * The border goes on the inner span rather than the box, so that `w` and `h`
298
+ * keep meaning what they mean for every other item.
299
+ */
300
+ function line(i) {
301
+ const vertical = i.orientation === 'vertical';
302
+ const pen = stroke(i.style);
303
+
304
+ return `
305
+ <div id="${esc(i.id)}" class="line${vertical ? ' is-vertical' : ''}" style="
306
+ width:${px(i.w)}px;
307
+ height:${px(i.h)}px;
308
+ ${position(i)}
309
+ " data-item-id="${esc(i.id)}" data-item-type="line"><span class="line-mark"
310
+ style="${vertical ? `border-left:${pen}` : `border-top:${pen}`}"></span></div>
311
+ `
312
+ }
313
+
314
+
315
+ /** what a rule may be drawn in; anything else is not a rule */
316
+ const LINE_STYLES = ['solid', 'dashed', 'dotted', 'double'];
317
+
318
+ /** a table's grid may also be drawn in nothing at all */
319
+ const RULE_STYLES = [...LINE_STYLES, 'none'];
320
+
321
+
322
+ /**
323
+ * The widest rule a table can be drawn with and still measure what it declares.
324
+ *
325
+ * .cell is box-sizing: border-box with a fixed height, so a rule is drawn
326
+ * inside the row rather than added to it - until the two rules of a cell are
327
+ * together taller than the row, at which point the cell has to grow and the
328
+ * table stops being headerHeight + rows x rowHeight. Half the shallowest row
329
+ * is the point where that starts, so that is the limit.
330
+ *
331
+ * @param {object} item the table
332
+ * @returns {number} pixels
333
+ */
334
+ function maxRuleWidth(item) {
335
+ const rowHeight = item.rowHeight ?? 0;
336
+ const headerHeight = item.showHeader ? (item.headerHeight ?? rowHeight) : rowHeight;
337
+
338
+ const shallowest = Math.min(rowHeight || Infinity, headerHeight || Infinity);
339
+
340
+ return Number.isFinite(shallowest) ? Math.max(1, Math.floor(shallowest / 2)) : 1;
341
+ }
342
+
343
+
344
+ /**
345
+ * A table's grid, as custom properties on the table itself.
346
+ *
347
+ * Set here and read by .cell rather than written onto every cell: a five
348
+ * hundred row table has fifteen hundred cells, and the same border string
349
+ * repeated that many times is markup nobody needs to send. Custom properties
350
+ * inherit, so one declaration on the table reaches all of them.
351
+ *
352
+ * Only what the table actually declares is emitted, so a table that says
353
+ * nothing keeps taking its colour from the stylesheet's own variable.
354
+ *
355
+ * @param {object} item the table
356
+ * @returns {string} css declarations, or empty
357
+ */
358
+ function tableRule(item) {
359
+ const style = item.style ?? {};
360
+ const out = [];
361
+
362
+ /**
363
+ * Whitelisted rather than passed through: the designer canvas draws
364
+ * straight from the layout without asking the validator, and a border style
365
+ * it does not recognise should fall back to the stylesheet rather than land
366
+ * in a style attribute.
367
+ */
368
+ if (RULE_STYLES.includes(style.borderStyle)) {
369
+ out.push(`--rs-rule-style:${style.borderStyle}`);
370
+ }
371
+
372
+ if (style.borderColor) out.push(`--rs-rule-color:${css(style.borderColor)}`);
373
+
374
+ /**
375
+ * The header's own colours, set here for the same reason the grid is: they
376
+ * belong to every `th` in the table, and a table that says nothing about
377
+ * them keeps taking the stylesheet's.
378
+ */
379
+ if (style.headerBackground) {
380
+ out.push(`--rs-head-bg:${css(style.headerBackground)}`);
381
+ }
382
+
383
+ if (style.headerColor) out.push(`--rs-head-color:${css(style.headerColor)}`);
384
+
385
+ /**
386
+ * The cell font, which used to go nowhere at all: the rail offered a size
387
+ * and a colour, report.css hard-coded 12px, and the two never met. It
388
+ * matters more than it looks - the engine wraps a cell against this font,
389
+ * so a table drawn in one and measured in another paginates to a row count
390
+ * it does not print.
391
+ */
392
+ if (style.fontFamily) out.push(`--rs-cell-family:${css(style.fontFamily)}`);
393
+
394
+ const size = Number(style.fontSize);
395
+ if (Number.isFinite(size) && size > 0) out.push(`--rs-cell-size:${size}px`);
396
+
397
+ if (style.color) out.push(`--rs-cell-color:${css(style.color)}`);
398
+
399
+ /** one line and an ellipsis, for a column of figures that must not wrap */
400
+ if (item.wrap === false) out.push('--rs-cell-wrap:nowrap');
401
+
402
+ /**
403
+ * Clamped rather than trusted: the rail will not offer a width past this,
404
+ * but a hand-written layout can, and a rule that outgrows its row would
405
+ * quietly undo the arithmetic pagination was done with.
406
+ */
407
+ const width = Number(style.borderWidth);
408
+
409
+ if (Number.isFinite(width) && width > 0) {
410
+ out.push(`--rs-rule-width:${Math.min(width, maxRuleWidth(item))}px`);
411
+ }
412
+
413
+ return out.length ? `${out.join(';')};` : '';
414
+ }
415
+
416
+
417
+ function table(i) {
418
+ return `
419
+ <table id="${esc(i.id)}" style="width:${px(i.w)}px;height:${px(i.measuredHeight)}px;${position(i)}${tableRule(i)}" data-item-id="${esc(i.id)}" data-item-type="table">
420
+ ${i.groups ? groupedBody(i) : flatBody(i)}
421
+ </table>
422
+ `
423
+ }
424
+
425
+
426
+ /**
427
+ * A cell's content, in a box of exactly the row's height.
428
+ *
429
+ * `height` on a `<tr>` is a minimum, not a height: a cell whose padding and
430
+ * line box need more than that makes the row taller, and the table then outgrows
431
+ * the `headerHeight + rows x rowHeight` the engine paginated against. A few
432
+ * pixels a row is invisible until the fortieth row has pushed the detail band
433
+ * over the page footer. So the padding lives on a box of a known height inside
434
+ * the cell, and the cell itself adds nothing.
435
+ *
436
+ * @param {number} height the row height the engine budgeted
437
+ * @param {string} align the column's text alignment
438
+ * @param {*} value what the cell shows
439
+ * @returns {string} markup
440
+ */
441
+ function cell(height, align, value) {
442
+ return `<div class="cell" style="height:${px(height)}px;text-align:${css(align, 'left')}">` +
443
+ `${esc(value)}</div>`;
444
+ }
445
+
446
+
447
+ /** the column header row, shared by both table shapes */
448
+ function columnHeader(i) {
449
+ /** what measure.js budgeted, which is taller than declared if a label wrapped */
450
+ const height = i.headerMeasured ?? i.headerHeight ?? i.rowHeight;
451
+
452
+ return `<thead>
453
+ <tr style="height:${px(height)}px">
454
+ ${i.columns.map(c => `<th style="width:${px(c.width)}px;">${cell(height, c.align, c.label)}</th>`).join("")}
455
+ </tr>
456
+ </thead>`;
457
+ }
458
+
459
+
460
+ /**
461
+ * @param {object} i the table
462
+ * @param {object[]} rows
463
+ * @param {number[]} [heights] one per row, from measure.js; absent on the
464
+ * designer canvas, where nothing has been measured and every row is its
465
+ * declared height
466
+ */
467
+ function dataRows(i, rows, heights) {
468
+ return (rows || []).map((r, n) => {
469
+ const height = heights?.[n] ?? i.rowHeight;
470
+
471
+ return `<tr style="height:${px(height)}px">
472
+ ${i.columns.map(c => `<td style="width:${px(c.width)}px;">${cell(height, c.align, r[c.field])}</td>`).join("")}
473
+ </tr>`;
474
+ }).join("");
475
+ }
476
+
477
+
478
+ /**
479
+ * A group band is a designed band with absolutely positioned items, so it is
480
+ * dropped into a full-width cell that recreates the band box inside the table.
481
+ */
482
+ function groupBandRow(band, colSpan, cls) {
483
+ if (!band) return '';
484
+ const height = band.measuredHeight > 0 ? band.measuredHeight : (band.height ?? 0);
485
+ return `<tr class="${cls}">
486
+ <td colspan="${colSpan}" class="group-band-cell">
487
+ <div class="band" style="position:relative;height:${px(height)}px">${items(band.items)}</div>
488
+ </td>
489
+ </tr>`;
490
+ }
491
+
492
+
493
+ function flatBody(i) {
494
+ return `
495
+ ${i.showHeader ? columnHeader(i) : ""}
496
+ <tbody>
497
+ ${dataRows(i, i.row, i.rowHeights)}
498
+ </tbody>
499
+ `;
500
+ }
501
+
502
+
503
+ /**
504
+ * One tbody per group fragment, so a group that survives a page break keeps its
505
+ * header on the next page and only prints its footer on the fragment that ends it.
506
+ */
507
+ function groupedBody(i) {
508
+ const colSpan = i.columns.length;
509
+
510
+ return i.groups.map(g => `
511
+ ${g.showHeader && i.showHeader ? columnHeader(i) : ""}
512
+ <tbody>
513
+ ${groupBandRow(g.headerBand, colSpan, "group-header")}
514
+ ${dataRows(i, g.rows, g.rowHeights)}
515
+ ${g.showFooter ? groupBandRow(g.footerBand, colSpan, "group-footer") : ""}
516
+ </tbody>
517
+ `).join("");
518
+ }
@@ -0,0 +1,98 @@
1
+ /**
2
+ * render.js takes the pages json and turns it into the preview's markup.
3
+ *
4
+ * It owns page and band composition only - where a band sits on a paginated
5
+ * page, and how the page box is drawn. Drawing the items inside a band lives in
6
+ * items.js, because the designer canvas needs that half and none of this one.
7
+ */
8
+
9
+ import { items, esc, px } from './items.js';
10
+
11
+
12
+ /**
13
+ * The page margins, as padding.
14
+ *
15
+ * Every length goes through px() on the way into a style attribute. The page
16
+ * block is the layout's, buildPages does not run the validator over it, and a
17
+ * margin that arrived as a string writes `padding-top:undefinedpx` - or worse,
18
+ * closes the attribute.
19
+ */
20
+ const padding = (m) => `padding-top:${px(m?.top)}px;padding-left:${px(m?.left)}px;` +
21
+ `padding-bottom:${px(m?.bottom)}px;padding-right:${px(m?.right)}px;`
22
+
23
+ /** display order */
24
+ const order = ['pageHeader', 'reportHeader', 'detail', 'reportFooter', 'pageFooter'];
25
+
26
+
27
+ export function render(json) {
28
+ const pageConf = json.page;
29
+ let html = `
30
+ <main id="main-page">
31
+ ${json.pages.map(p =>
32
+ `
33
+ <section id="page-${esc(p.pageNO)}" class="page" style="height:${px(pageConf.height)}px; width:${px(pageConf.width)}px; ${padding(pageConf.margin)};position:relative">
34
+ ${page(p, pageConf)}
35
+ </section>
36
+ `
37
+ ).join('')}
38
+ </main>
39
+ `;
40
+ return html;
41
+ }
42
+
43
+
44
+ function page(page, pageConf) {
45
+ /**
46
+ * copy before sorting - the page list is the seam every other consumer
47
+ * reads, and render has no business reordering it in place.
48
+ * A band type render does not know about sorts to the end, not the front.
49
+ */
50
+ const rank = (type) => {
51
+ const i = order.indexOf(type);
52
+ return i === -1 ? order.length : i;
53
+ };
54
+
55
+ const sortedPage = [...page.bands].sort(
56
+ ({ type: a }, { type: b }) => rank(a) - rank(b)
57
+ );
58
+
59
+ let pageHtmlStr = [];
60
+
61
+
62
+ for (const band of sortedPage) {
63
+ pageHtmlStr.push(bandConversion(band, page.pageNO, pageConf))
64
+ }
65
+
66
+ return pageHtmlStr.join('');
67
+ }
68
+
69
+
70
+ /**
71
+ * Bands are anchored, not stacked: the engine gave each one the top of its zone,
72
+ * so a page whose detail ran short still prints its footer on the bottom edge.
73
+ * Without a `top` - an older page list, or a band the engine did not place - it
74
+ * falls back to flowing, which is what this used to do for everything.
75
+ */
76
+ function bandConversion(comp, pageNO, pageConf) {
77
+ const anchored = Number.isFinite(comp.top);
78
+ const margin = pageConf?.margin ?? { top: 0, left: 0, right: 0 };
79
+
80
+ /**
81
+ * An absolutely positioned child anchors to its ancestor's *padding* box,
82
+ * and the page draws its margins as padding - so the margins have to be
83
+ * added back here, or every band would sit inside them.
84
+ */
85
+ const box = anchored
86
+ ? `position:absolute;top:${px(comp.top) + px(margin.top)}px;` +
87
+ `left:${px(margin.left)}px;right:${px(margin.right)}px;`
88
+ : `position:relative;`;
89
+
90
+ /** the zone is the height; a band that outgrew it keeps its measured height */
91
+ const height = Math.max(comp.zoneHeight ?? 0, comp.measuredHeight ?? 0);
92
+
93
+ return `
94
+ <div class="band" id="${esc(comp.type)}-${esc(pageNO)}" style="${box}${height > 0 ? `height:${px(height)}px` : ""}" data-band-type="${esc(comp.type)}">
95
+ ${items(comp.items)}
96
+ </div>
97
+ `
98
+ }