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
+ * canvas.js draws the design-time page.
3
+ *
4
+ * The preview draws a *paginated* report: many pages, bands placed by the
5
+ * engine, real data. The designer draws one page, unpaginated, with no data -
6
+ * so it cannot reuse render.js. What it does reuse is everything below the
7
+ * band: items.js draws the text boxes and tables, and report.css styles them,
8
+ * so what is designed looks like what is printed (spec 5.2).
9
+ *
10
+ * Band geometry is not guessed either - pageRegions() is the same function the
11
+ * engine uses at print time, called with isFirstPage and isLastPage both true
12
+ * so the canvas shows every band at once. Move a band's height here and the
13
+ * printed page moves with it, because it is one calculation, not two.
14
+ *
15
+ * +-------------------------+
16
+ * | pageHeader | anchored zones, from pageRegions()
17
+ * | reportHeader |
18
+ * | - - - - - - - - - - - |
19
+ * | groupHeader | the repeating unit, stacked inside
20
+ * | detail | whatever the detail region has left
21
+ * | groupFooter |
22
+ * | - - - - - - - - - - - |
23
+ * | reportFooter |
24
+ * | pageFooter | pinned to the bottom edge
25
+ * +-------------------------+
26
+ */
27
+
28
+ import { pageRegions, bandZoneHeight } from '../engine/regions.js';
29
+ import { items, esc } from '../render/items.js';
30
+ import {
31
+ SAMPLE_ROWS, designHeight, itemBox, resizableAxes, handlesFor
32
+ } from './geometry.js';
33
+ import { ticksFor, guidesOf } from './rulers.js';
34
+
35
+
36
+ /** zones that own a strip of the page, drawn top to bottom */
37
+ const PAGE_ZONES = ['pageHeader', 'reportHeader', 'reportFooter', 'pageFooter'];
38
+
39
+
40
+ /**
41
+ * A table at design time has no data, so it is given its own column names as
42
+ * cell values. Blank rows would be a table-shaped hole; the field names say
43
+ * which column is which without having to read the header twice.
44
+ */
45
+ function sampleRows(columns) {
46
+ return Array.from({ length: SAMPLE_ROWS }, () =>
47
+ Object.fromEntries((columns || []).map(c => [c.field, c.field])));
48
+ }
49
+
50
+
51
+ /**
52
+ * items.js expects what the engine produces - resolved text, real rows. This
53
+ * supplies the design-time equivalent: the placeholder string shown verbatim,
54
+ * because that is the thing being edited, and sample rows for a table.
55
+ */
56
+ function designItem(item) {
57
+ if (item.type === 'text') {
58
+ return { ...item, text: item.value };
59
+ }
60
+
61
+ if (item.type === 'table') {
62
+ return {
63
+ ...item,
64
+ row: sampleRows(item.columns),
65
+ measuredHeight: designHeight(item)
66
+ };
67
+ }
68
+
69
+ return item;
70
+ }
71
+
72
+
73
+ /**
74
+ * Where every band sits on the design page, in printable-area coordinates.
75
+ *
76
+ * groupHeader and groupFooter own no zone of their own - at print time they are
77
+ * drawn inside the detail table, once per group. Here they are stacked at the
78
+ * top and bottom of the detail region, which is where they land on the page and
79
+ * makes the repeating unit legible.
80
+ *
81
+ * @param {object} layout
82
+ * @returns {object[]} `{ type, top, height, band }`, in draw order
83
+ */
84
+ export function designZones(layout) {
85
+ const { page } = layout;
86
+ const contentHeight = page.height - page.margin.top - page.margin.bottom;
87
+
88
+ const byType = Object.fromEntries(
89
+ (layout.bands || []).map(b => [b.type, b]));
90
+
91
+ const regions = pageRegions({
92
+ bands: byType,
93
+ contentHeight,
94
+ isFirstPage: true,
95
+ isLastPage: true
96
+ });
97
+
98
+ const zones = [];
99
+
100
+ for (const type of PAGE_ZONES) {
101
+ const region = regions[type];
102
+ if (region && byType[type]) {
103
+ zones.push({ type, ...region, band: byType[type] });
104
+ }
105
+ }
106
+
107
+ const detail = regions.detail;
108
+ const groupHeader = byType.groupHeader;
109
+ const groupFooter = byType.groupFooter;
110
+
111
+ const headHeight = groupHeader ? bandZoneHeight(groupHeader, contentHeight) : 0;
112
+ const footHeight = groupFooter ? bandZoneHeight(groupFooter, contentHeight) : 0;
113
+
114
+ if (groupHeader) {
115
+ zones.push({
116
+ type: 'groupHeader', top: detail.top,
117
+ height: headHeight, band: groupHeader
118
+ });
119
+ }
120
+
121
+ if (byType.detail) {
122
+ zones.push({
123
+ type: 'detail',
124
+ top: detail.top + headHeight,
125
+ height: Math.max(detail.height - headHeight - footHeight, 0),
126
+ band: byType.detail
127
+ });
128
+ }
129
+
130
+ if (groupFooter) {
131
+ zones.push({
132
+ type: 'groupFooter',
133
+ top: detail.top + detail.height - footHeight,
134
+ height: footHeight, band: groupFooter
135
+ });
136
+ }
137
+
138
+ return zones;
139
+ }
140
+
141
+
142
+ /**
143
+ * An absolutely positioned child anchors to its ancestor's *padding* box, and
144
+ * the page draws its margins as padding - so the margins are added back here,
145
+ * exactly as render.js does it, or every band would sit inside them.
146
+ */
147
+ function zone(z, margin) {
148
+ /**
149
+ * Top to bottom, which is the order paginate.js places a band's items in
150
+ * and therefore the order they are painted in the report. Drawing them in
151
+ * file order instead put a box added last over the things it was drawn to
152
+ * frame - on the canvas only, so the design and the print disagreed.
153
+ */
154
+ const drawn = [...(z.band.items || [])]
155
+ .sort((a, b) => (a.y ?? 0) - (b.y ?? 0))
156
+ .map(designItem);
157
+
158
+ return `
159
+ <div class="band dz-zone" data-band-type="${esc(z.type)}"
160
+ style="position:absolute;top:${z.top + margin.top}px;left:${margin.left}px;right:${margin.right}px;height:${z.height}px">
161
+ ${items(drawn)}
162
+ </div>`;
163
+ }
164
+
165
+
166
+ /**
167
+ * The two rulers and the corner between them.
168
+ *
169
+ * They measure the printable area rather than the paper, so the number under an
170
+ * item is the `x` the properties rail shows for it. Anything else would be a
171
+ * second coordinate system for someone to convert between, and the margins are
172
+ * already drawn - the ruler starting where they end says how wide they are
173
+ * without a number for that too.
174
+ *
175
+ * @param {object} layout
176
+ * @param {{w: number, h: number}} paper the printable area
177
+ * @returns {string} markup
178
+ */
179
+ function rulerFrame(layout, paper) {
180
+ const guides = guidesOf(layout);
181
+
182
+ const marks = (length, axis) => ticksFor(length).map(tick => `
183
+ <span class="dz-tick${tick.major ? ' is-major' : ''}"
184
+ style="${axis === 'x' ? 'left' : 'top'}:${tick.at}px"
185
+ >${tick.major ? `<i>${tick.at}</i>` : ''}</span>`).join('');
186
+
187
+ /**
188
+ * The guides get a container of their own inside each ruler, so a drag can
189
+ * replace them without touching the ticks - which never change, and there
190
+ * are two hundred of them.
191
+ */
192
+ const nubs = (axis) => `
193
+ <span class="dz-ruler-guides" data-role="ruler-guides"
194
+ data-axis="${axis}">${guideNubs(layout, axis)}</span>`;
195
+
196
+ return `
197
+ <div class="dz-ruler-corner" data-role="ruler-corner"></div>
198
+
199
+ <div class="dz-ruler dz-ruler-x" data-role="ruler" data-axis="x"
200
+ style="width:${paper.w}px" aria-hidden="true">
201
+ ${marks(paper.w, 'x')}${nubs('x')}
202
+ </div>
203
+
204
+ <div class="dz-ruler dz-ruler-y" data-role="ruler" data-axis="y"
205
+ style="height:${paper.h}px" aria-hidden="true">
206
+ ${marks(paper.h, 'y')}${nubs('y')}
207
+ </div>`;
208
+ }
209
+
210
+
211
+ /**
212
+ * Where each guide meets its ruler, so one dragged off the page is still
213
+ * findable - and so the ruler says what it is carrying.
214
+ *
215
+ * @param {object} layout
216
+ * @param {'x'|'y'} axis
217
+ * @returns {string} markup
218
+ */
219
+ export function guideNubs(layout, axis) {
220
+ return guidesOf(layout)[axis].map(at => `
221
+ <span class="dz-ruler-guide"
222
+ style="${axis === 'x' ? 'left' : 'top'}:${at}px"></span>`).join('');
223
+ }
224
+
225
+
226
+ /**
227
+ * The guides themselves, drawn over the page.
228
+ *
229
+ * In the page's coordinates rather than the printable area's, because that is
230
+ * the box they are drawn in - the same conversion every other overlay on this
231
+ * sheet makes, and for the same reason.
232
+ *
233
+ * @param {object} layout
234
+ * @returns {string} markup, the contents of the .dz-rules layer
235
+ */
236
+ export function guideLines(layout) {
237
+ const margin = layout.page.margin;
238
+ const guides = guidesOf(layout);
239
+
240
+ const across = guides.x.map(at => `
241
+ <span class="dz-rule dz-rule-v" data-role="guide" data-axis="x"
242
+ data-at="${at}" style="left:${at + margin.left}px"></span>`);
243
+
244
+ const down = guides.y.map(at => `
245
+ <span class="dz-rule dz-rule-h" data-role="guide" data-axis="y"
246
+ data-at="${at}" style="top:${at + margin.top}px"></span>`);
247
+
248
+ return [...across, ...down].join('');
249
+ }
250
+
251
+
252
+ /** the band name, in the gutter beside the page rather than over the design */
253
+ function tag(z, margin) {
254
+ return `<span class="dz-tag" data-band-tag="${esc(z.type)}"
255
+ style="top:${z.top + margin.top}px">${esc(z.type)}</span>`;
256
+ }
257
+
258
+
259
+
260
+ /**
261
+ * The selected item's box in page coordinates - margins included, so it can be
262
+ * dropped straight into the page box beside the zones.
263
+ *
264
+ * @param {object} layout
265
+ * @param {{band: string, id: string}|null} selection
266
+ * @returns {object|null} `{ item, box }`, or null when nothing is selected
267
+ */
268
+ export function selectionBox(layout, selection) {
269
+ if (!selection) return null;
270
+
271
+ const margin = layout.page.margin;
272
+ const zone = designZones(layout).find(z => z.type === selection.band);
273
+ if (!zone) return null;
274
+
275
+ const item = (zone.band.items || []).find(i => i.id === selection.id);
276
+ if (!item) return null;
277
+
278
+ const box = itemBox(item);
279
+
280
+ return {
281
+ item,
282
+ box: {
283
+ ...box,
284
+ x: box.x + margin.left,
285
+ y: box.y + zone.top + margin.top
286
+ }
287
+ };
288
+ }
289
+
290
+
291
+ /**
292
+ * Spec 5.2: the canvas is the preview's drawing plus selection chrome on top.
293
+ * The outline is a sibling of the bands rather than something added inside the
294
+ * item, so items.js keeps producing exactly what the preview produces - the
295
+ * moment selection has to be drawn *into* an item, the two paths have parted.
296
+ */
297
+ function overlay(found, { withHandles = true, primary = true } = {}) {
298
+ const { box, item } = found;
299
+
300
+ /**
301
+ * Handles only on a lone selection. Dragging the south edge of six items at
302
+ * once has no single answer - do they all become that tall, or does the
303
+ * group scale? - and a handle that has to ask is worse than one that is not
304
+ * offered.
305
+ */
306
+ const handles = withHandles
307
+ ? handlesFor(resizableAxes(item))
308
+ .map(h => `<span class="dz-handle dz-handle-${h}" data-handle="${h}"></span>`)
309
+ .join('')
310
+ : '';
311
+
312
+ return `
313
+ <div class="dz-select${primary ? '' : ' is-secondary'}" data-role="select"
314
+ data-select-id="${esc(item.id)}"
315
+ style="top:${box.y}px;left:${box.x}px;width:${box.w}px;height:${box.h}px">
316
+ ${handles}
317
+ </div>`;
318
+ }
319
+
320
+
321
+ /**
322
+ * Every selected item's box, in the order they were selected.
323
+ *
324
+ * @param {object} layout
325
+ * @param {object[]} selections
326
+ * @returns {object[]} `{ item, box }`, skipping any that have since gone
327
+ */
328
+ export function selectionBoxes(layout, selections) {
329
+ return (selections || [])
330
+ .map(one => selectionBox(layout, one))
331
+ .filter(Boolean);
332
+ }
333
+
334
+ /**
335
+ * The whole sheet: a gutter of band names, and the page itself.
336
+ * @param {object} layout a validated layout
337
+ * @param {object|object[]|null} [selection] the item or items to outline
338
+ * @param {object} [options]
339
+ * @param {boolean} [options.rulers] draw the rulers and the guides on them
340
+ * @returns {string} markup
341
+ */
342
+ export function drawSheet(layout, selection = null, { rulers = false } = {}) {
343
+ const { page } = layout;
344
+ const margin = page.margin;
345
+ const zones = designZones(layout);
346
+
347
+ /** one or many: the canvas draws an outline per selected item either way */
348
+ const list = selection == null ? [] : [].concat(selection);
349
+ const found = selectionBoxes(layout, list);
350
+
351
+ const pageBox =
352
+ `width:${page.width}px;height:${page.height}px;` +
353
+ `padding-top:${margin.top}px;padding-right:${margin.right}px;` +
354
+ `padding-bottom:${margin.bottom}px;padding-left:${margin.left}px;`;
355
+
356
+ /** the printable area, which is what the rulers measure and guides sit in */
357
+ const paper = {
358
+ w: page.width - margin.left - margin.right,
359
+ h: page.height - margin.top - margin.bottom
360
+ };
361
+
362
+ return `
363
+ <div class="dz-sheet${rulers ? ' has-rulers' : ''}"
364
+ style="--page-height:${page.height}px;--dz-margin-left:${margin.left}px;
365
+ --dz-margin-top:${margin.top}px">
366
+ ${rulers ? rulerFrame(layout, paper) : ''}
367
+ <div class="dz-gutter">
368
+ ${zones.map(z => tag(z, margin)).join('')}
369
+ </div>
370
+ <div class="page dz-page" style="${pageBox}">
371
+ <div class="dz-rules" data-role="rules"
372
+ >${rulers ? guideLines(layout) : ''}</div>
373
+ ${zones.map(z => zone(z, margin)).join('')}
374
+ ${found.map((one, i) => overlay(one, {
375
+ withHandles: found.length === 1,
376
+ primary: i === found.length - 1
377
+ })).join('')}
378
+ <div class="dz-guides" data-role="guides"></div>
379
+ </div>
380
+ </div>`;
381
+ }
382
+
383
+
384
+ /**
385
+ * A band's box: where it starts on the page, and how big it is in its own
386
+ * coordinates.
387
+ *
388
+ * Two coordinate systems in one object because the two callers want different
389
+ * halves of it - the guides are worked out in band coordinates and drawn in
390
+ * page ones, and the conversion between them is exactly `left` and `top`.
391
+ *
392
+ * @param {object} layout
393
+ * @param {string} bandType
394
+ * @returns {{left: number, top: number, w: number, h: number}|null}
395
+ */
396
+ export function bandBox(layout, bandType) {
397
+ const zone = designZones(layout).find(z => z.type === bandType);
398
+ if (!zone) return null;
399
+
400
+ const margin = layout.page.margin;
401
+
402
+ return {
403
+ left: margin.left,
404
+ top: zone.top + margin.top,
405
+ w: layout.page.width - margin.left - margin.right,
406
+ h: zone.height,
407
+
408
+ /**
409
+ * Where the band starts inside the printable area, which is what the
410
+ * rulers measure and therefore what a guide's position means. `originX`
411
+ * is zero and stays in the object anyway: a caller converting one axis
412
+ * and not the other is the bug this is here to make impossible.
413
+ */
414
+ originX: 0,
415
+ originY: zone.top
416
+ };
417
+ }
418
+
419
+
420
+ /**
421
+ * The guides, as markup for the layer drawSheet leaves empty.
422
+ *
423
+ * Everything arrives in band coordinates and is drawn in page ones, so the
424
+ * band's own origin is added to all of it here - the one place that knows both.
425
+ *
426
+ * @param {object} band the bandBox the guides were worked out against
427
+ * @param {object[]} lines
428
+ * @param {object[]} gaps
429
+ * @returns {string} markup
430
+ */
431
+ export function drawGuides(band, lines = [], gaps = []) {
432
+ if (!band) return '';
433
+
434
+ const across = (n) => n + band.left;
435
+ const down = (n) => n + band.top;
436
+
437
+ const drawnLines = lines.map(line => line.axis === 'x'
438
+ ? `<span class="dz-guide dz-guide-v${line.centre ? ' is-centre' : ''}"
439
+ style="left:${across(line.at)}px;top:${down(line.from)}px;
440
+ height:${Math.max(line.to - line.from, 0)}px"></span>`
441
+ : `<span class="dz-guide dz-guide-h${line.centre ? ' is-centre' : ''}"
442
+ style="top:${down(line.at)}px;left:${across(line.from)}px;
443
+ width:${Math.max(line.to - line.from, 0)}px"></span>`);
444
+
445
+ const drawnGaps = gaps.map(gap => {
446
+ const length = Math.max(gap.to - gap.from, 0);
447
+ const label = `<i>${Math.round(gap.distance)}</i>`;
448
+ const band_ = gap.toBand ? ' is-band' : '';
449
+
450
+ return gap.axis === 'y'
451
+ ? `<span class="dz-gap dz-gap-v${band_}"
452
+ style="left:${across(gap.at)}px;top:${down(gap.from)}px;
453
+ height:${length}px">${label}</span>`
454
+ : `<span class="dz-gap dz-gap-h${band_}"
455
+ style="top:${down(gap.at)}px;left:${across(gap.from)}px;
456
+ width:${length}px">${label}</span>`;
457
+ });
458
+
459
+ return [...drawnLines, ...drawnGaps].join('');
460
+ }
461
+
462
+
463
+ /**
464
+ * The printable-area y of a point inside a band.
465
+ *
466
+ * Bands hold items in their own coordinates (spec 3.3), so comparing one item's
467
+ * position with another band's means lifting it out of its band first.
468
+ *
469
+ * @param {object} layout
470
+ * @param {string} bandType
471
+ * @param {number} y a band-relative offset
472
+ * @returns {number|null} null when the report has no such band
473
+ */
474
+ export function absoluteTop(layout, bandType, y) {
475
+ const zone = designZones(layout).find(z => z.type === bandType);
476
+ return zone ? zone.top + y : null;
477
+ }
478
+
479
+
480
+ /**
481
+ * Which band a printable-area y falls in.
482
+ *
483
+ * The zones tile the page - whatever a missing band would have taken is
484
+ * absorbed by the detail (see regions.js) - so any point on the sheet is in
485
+ * exactly one of them. A point off the top or bottom is clamped to the nearest
486
+ * rather than returned as nothing, because dragging an item slightly past the
487
+ * edge is a gesture towards that band, not a gesture at nothing.
488
+ *
489
+ * @param {object} layout
490
+ * @param {number} top
491
+ * @returns {string|null} a band type, or null for a report with no bands
492
+ */
493
+ export function bandAtOffset(layout, top) {
494
+ const zones = designZones(layout);
495
+ if (!zones.length) return null;
496
+
497
+ const ordered = [...zones].sort((a, b) => a.top - b.top);
498
+
499
+ for (const zone of ordered) {
500
+ if (top >= zone.top && top < zone.top + zone.height) return zone.type;
501
+ }
502
+
503
+ return top < ordered[0].top ? ordered[0].type : ordered.at(-1).type;
504
+ }
505
+
506
+
507
+ /**
508
+ * Where an item would land if it were dropped where it currently sits.
509
+ *
510
+ * @param {object} layout
511
+ * @param {string} bandType the band it is in now
512
+ * @param {object} item
513
+ * @returns {string|null} the band type under it
514
+ */
515
+ export function bandUnder(layout, bandType, item) {
516
+ const top = absoluteTop(layout, bandType, item?.y ?? 0);
517
+ return top === null ? null : bandAtOffset(layout, top);
518
+ }