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
@@ -17,7 +17,7 @@ import { allFields, allReportFields, fieldsFor, reportFields } from './fields.js
17
17
  import { blankLayout, isEmptyLayout } from './blank.js';
18
18
  import {
19
19
  drawSheet, selectionBox, selectionBoxes, bandUnder, bandBox, drawGuides,
20
- guideLines, guideNubs
20
+ guideLines, guideNubs, designZones, zoneStyle
21
21
  } from './canvas.js';
22
22
  import { attachEditing } from './select.js';
23
23
  import {
@@ -32,7 +32,11 @@ import {
32
32
  } from '../preview/viewer.js';
33
33
  import { buildPages } from '../engine/index.js';
34
34
  import { drawProblems, sameIssues } from './toast.js';
35
- import { askColumns, askReport, askName, askData, askToken, askDiscard } from './dialog.js';
35
+ import {
36
+ askColumns, askReport, askName, askData, askToken, askDiscard, askFormat, askHighlight
37
+ } from './dialog.js';
38
+ import { INDIAN_DEFAULTS, effectiveFormat } from '../engine/format.js';
39
+ import { availableFields } from './tokens.js';
36
40
  import { sampleData, completeData } from './sample-data.js';
37
41
  import { drawPreview } from './preview.js';
38
42
  import { createHistory } from './history.js';
@@ -42,6 +46,10 @@ import {
42
46
  moveItemToBand, createText, createTable, createLine, createBox, createImage, addColumn,
43
47
  removeColumn, targetBand, canAddTable
44
48
  } from './structure.js';
49
+ import {
50
+ openLayout, copyGroupDesign, groupBandsIn, parseBandKey,
51
+ addGroupLevel, removeGroupLevel, moveGroupLevelOut
52
+ } from './grouping.js';
45
53
 
46
54
 
47
55
  /** the raster formats the renderer will draw from a data: URI; see imageSrc */
@@ -198,7 +206,12 @@ export function createDesigner({
198
206
  */
199
207
  rulers: false,
200
208
 
201
- layout: isEmptyLayout(layout) ? blankLayout() : layout,
209
+ /**
210
+ * A layout grouped the older way has its grouping moved onto its
211
+ * table here, once - every control in the designer edits a table's
212
+ * own grouping. Any other layout is the host's own object, untouched.
213
+ */
214
+ layout: isEmptyLayout(layout) ? blankLayout() : openLayout(layout),
202
215
  /**
203
216
  * The selected items, oldest first, as `{ band, id }` - the band type
204
217
  * and the item id, because ids are only unique within a band
@@ -396,6 +409,29 @@ export function createDesigner({
396
409
  /** a table's height is its rows', so it is never written here */
397
410
  if (item.type !== 'table') drawn.style.height = `${item.h}px`;
398
411
  }
412
+
413
+ if (boxes.some(({ item }) => item.type === 'table')) patchTableZones();
414
+ }
415
+
416
+
417
+ /**
418
+ * A grouped table's group bands are drawn inside it, as zones of their own
419
+ * on top of it - so a table being dragged or resized takes them along, and
420
+ * their names in the gutter with them.
421
+ */
422
+ function patchTableZones() {
423
+ const margin = state.layout.page.margin;
424
+
425
+ for (const z of designZones(state.layout)) {
426
+ if (!z.table) continue;
427
+
428
+ const key = cssEscape(z.type);
429
+ const zone = canvas.querySelector(`.dz-zone[data-band-type="${key}"]`);
430
+ if (zone) zone.setAttribute('style', zoneStyle(z, margin));
431
+
432
+ const tag = canvas.querySelector(`[data-band-tag="${key}"]`);
433
+ if (tag) tag.style.top = `${z.top + margin.top}px`;
434
+ }
399
435
  }
400
436
 
401
437
 
@@ -416,15 +452,35 @@ export function createDesigner({
416
452
  }
417
453
 
418
454
 
455
+ /**
456
+ * How far down the rail was, for each kind of thing it has shown - the
457
+ * report, a box, a text item, several at once.
458
+ *
459
+ * Without it, the place someone had scrolled the report's rail to went
460
+ * whenever an item was selected: a box's rail fits without scrolling, so
461
+ * the offset collapsed to 0, and deselecting brought the report back at
462
+ * its top. Kept per kind rather than per item, so stepping from one text
463
+ * item to the next stays on the font being changed.
464
+ */
465
+ const railScroll = new Map();
466
+ let railShowing = null;
467
+
419
468
  function drawRail() {
420
469
  /** a rendered report has no item to describe and no report to restructure */
421
470
  if (state.mode === 'preview') return;
422
471
 
423
472
  const many = state.selection.length;
473
+ const item = selected();
474
+ const showing = many > 1 ? 'many' : (item ? `item:${item.type}` : 'report');
475
+
476
+ if (railShowing) railScroll.set(railShowing, panel.scrollTop);
424
477
 
425
478
  panel.innerHTML = many > 1
426
479
  ? drawManyPanel(selectedItems())
427
- : (selected() ? drawPanel(selected()) : drawReportPanel(state.layout));
480
+ : (item ? drawPanel(item, state.layout) : drawReportPanel(state.layout));
481
+
482
+ panel.scrollTop = railScroll.get(showing) ?? 0;
483
+ railShowing = showing;
428
484
  }
429
485
 
430
486
 
@@ -556,7 +612,7 @@ export function createDesigner({
556
612
  * nothing draws an outline round empty space.
557
613
  */
558
614
  function replaceLayout(next, { id: nextId, dirty }) {
559
- state.layout = next;
615
+ state.layout = openLayout(next);
560
616
  state.selection = [];
561
617
 
562
618
  /** another report is another design; its pages have not been run yet */
@@ -738,8 +794,9 @@ export function createDesigner({
738
794
  *
739
795
  * @param {number} clientX
740
796
  * @param {number} clientY
797
+ * @param {Element|null} [target] what was right-clicked, for which column
741
798
  */
742
- function openMenu(clientX, clientY) {
799
+ function openMenu(clientX, clientY, target = null) {
743
800
  closeMenu();
744
801
 
745
802
  main.insertAdjacentHTML('beforeend', drawMenu(menuActions({
@@ -747,10 +804,12 @@ export function createDesigner({
747
804
  canPaste: state.clipboard.length > 0,
748
805
  /** should canAddTable ever say no, a copy of a table is what it refuses */
749
806
  canDuplicate: canAddTable(state.layout)
750
- || !selectedItems().some(item => item.type === 'table')
807
+ || !selectedItems().some(item => item.type === 'table'),
808
+ ...aboutTarget(target)
751
809
  })));
752
810
 
753
811
  menu = main.querySelector('[data-role="menu"]');
812
+ menu.addEventListener('keydown', onMenuArrows);
754
813
 
755
814
  /**
756
815
  * Bound to the pill, not to the page area: a delegated listener out
@@ -799,7 +858,7 @@ export function createDesigner({
799
858
  setSelection(null);
800
859
  }
801
860
 
802
- openMenu(event.clientX, event.clientY);
861
+ openMenu(event.clientX, event.clientY, event.target);
803
862
  }
804
863
 
805
864
 
@@ -815,6 +874,92 @@ export function createDesigner({
815
874
  }
816
875
 
817
876
 
877
+ /**
878
+ * What a Format entry in the menu would format: the text item that was
879
+ * right-clicked, or the column of the table cell under the pointer. One
880
+ * item only - a format is one item's, and a choice made for six would be
881
+ * a different edit from the one the editor describes.
882
+ *
883
+ * @param {Element|null} target
884
+ * @returns {{label: string, index?: number}|null}
885
+ */
886
+ function formatTarget(target) {
887
+ if (state.selection.length !== 1) return null;
888
+
889
+ const item = selected();
890
+ if (item?.type === 'text') return { label: 'Format…' };
891
+ if (item?.type !== 'table') return null;
892
+
893
+ const index = columnUnder(target);
894
+ if (index === null) return null;
895
+
896
+ const column = item.columns[index];
897
+ return { label: `Format "${column.label || column.field}" column…`, index };
898
+ }
899
+
900
+
901
+ /**
902
+ * The table column under the pointer: a data cell or a heading, not a
903
+ * group band's full-width row - or null.
904
+ * @param {Element|null} target
905
+ * @returns {number|null}
906
+ */
907
+ function columnUnder(target) {
908
+ const cell = target?.closest?.('td, th');
909
+ if (!cell || cell.classList.contains('group-band-cell')) return null;
910
+
911
+ return selected()?.columns?.[cell.cellIndex] ? cell.cellIndex : null;
912
+ }
913
+
914
+
915
+ /**
916
+ * What the menu's entries about the item itself act on: Format and
917
+ * Highlight for a text item or the column under the pointer, and the
918
+ * table's rows for a table.
919
+ * @param {Element|null} target
920
+ * @returns {{format: object|null, highlight: object|null, rows: boolean}}
921
+ */
922
+ function aboutTarget(target) {
923
+ const format = formatTarget(target);
924
+ const item = state.selection.length === 1 ? selected() : null;
925
+
926
+ let highlight = null;
927
+ if (item?.type === 'text') highlight = { label: 'Highlight…' };
928
+ if (format && item?.type === 'table') {
929
+ const column = item.columns[format.index];
930
+ highlight = { label: `Highlight "${column.label || column.field}" column…`, index: format.index };
931
+ }
932
+
933
+ return { format, highlight, rows: item?.type === 'table' };
934
+ }
935
+
936
+
937
+ /**
938
+ * Up and down move through the menu, as in any other. Stopped here, on the
939
+ * menu: on their way to the document they would nudge the selection too.
940
+ */
941
+ function onMenuArrows(event) {
942
+ const keys = ['ArrowDown', 'ArrowUp', 'Home', 'End'];
943
+ if (!keys.includes(event.key)) return;
944
+
945
+ event.preventDefault();
946
+ event.stopPropagation();
947
+
948
+ const items = [...menu.querySelectorAll('button:not([disabled])')];
949
+ if (!items.length) return;
950
+
951
+ const at = items.indexOf(menu.ownerDocument.activeElement);
952
+ const next = {
953
+ ArrowDown: (at + 1) % items.length,
954
+ ArrowUp: (at - 1 + items.length) % items.length,
955
+ Home: 0,
956
+ End: items.length - 1
957
+ }[event.key];
958
+
959
+ items[next].focus();
960
+ }
961
+
962
+
818
963
  /** anything that is not the pill closes it */
819
964
  function onDocumentDown(event) {
820
965
  if (menu && !menu.contains(event.target)) closeMenu();
@@ -899,7 +1044,8 @@ export function createDesigner({
899
1044
  }
900
1045
 
901
1046
  case 'add-table': {
902
- const type = targetBand(state.layout, primary());
1047
+ /** never onto a group band, which is drawn inside a table */
1048
+ const type = targetBand(state.layout, primary(), { table: true });
903
1049
  if (!type) return;
904
1050
 
905
1051
  if (!canAddTable(state.layout)) return;
@@ -1021,6 +1167,115 @@ export function createDesigner({
1021
1167
  return;
1022
1168
  }
1023
1169
 
1170
+ case 'copy-grouping':
1171
+ if (item?.type !== 'table' || !data?.from) return;
1172
+
1173
+ if (copyGroupDesign(state.layout, item.id, data.from)) {
1174
+ /** whatever was selected on the bands just replaced is gone */
1175
+ state.selection = state.selection.filter(one => itemAt(one));
1176
+ restructured(`grouping:copy:${item.id}`);
1177
+ }
1178
+ return;
1179
+
1180
+ case 'edit-format': {
1181
+ /** a text item's own format, or one of a table's columns' */
1182
+ const isColumn = data?.index != null && item?.type === 'table';
1183
+ const owner = isColumn ? item.columns?.[Number(data.index)] : item;
1184
+ if (!owner || (!isColumn && item?.type !== 'text')) return;
1185
+
1186
+ const name = isColumn ? (owner.label || owner.field || 'column') : item.id;
1187
+ const where = primary();
1188
+
1189
+ askFormat(root, owner.format, state.layout.format, { title: `Format: ${name}` })
1190
+ .then(answer => {
1191
+ if (!answer) return;
1192
+
1193
+ /**
1194
+ * Still the same item, holding the same column - an undo
1195
+ * while the dialog was up swaps in a different layout.
1196
+ */
1197
+ if (itemAt(where) !== item) return;
1198
+ if (isColumn && !item.columns?.includes(owner)) return;
1199
+
1200
+ if (answer.format) owner.format = answer.format;
1201
+ else delete owner.format;
1202
+
1203
+ restructured(`format:${item.id}${isColumn ? `:${data.index}` : ''}`);
1204
+ });
1205
+ return;
1206
+ }
1207
+
1208
+ case 'edit-highlight':
1209
+ case 'edit-row-highlight': {
1210
+ const rows = action === 'edit-row-highlight';
1211
+ const isColumn = !rows && data?.index != null && item?.type === 'table';
1212
+ const owner = isColumn ? item.columns?.[Number(data.index)] : item;
1213
+
1214
+ if (!owner) return;
1215
+ if (rows ? item.type !== 'table' : (!isColumn && item.type !== 'text')) return;
1216
+
1217
+ const key = rows ? 'rowHighlight' : 'highlight';
1218
+ const name = rows ? `${item.id} rows`
1219
+ : isColumn ? (owner.label || owner.field || 'column') : item.id;
1220
+
1221
+ /** what a rule may test: a table's columns, or the data's fields */
1222
+ const fields = item.type === 'table'
1223
+ ? (item.columns ?? []).map(c => c.field).filter(Boolean)
1224
+ : availableFields(state.layout);
1225
+
1226
+ const where = primary();
1227
+
1228
+ askHighlight(root, owner[key], {
1229
+ title: `Highlight: ${name}`,
1230
+ fields: isColumn ? fields.filter(f => f !== owner.field) : fields,
1231
+ rows,
1232
+ format: isColumn || item.type === 'text' ? effectiveFormat(owner.format, state.layout.format) : null
1233
+ }).then(answer => {
1234
+ if (!answer) return;
1235
+ if (itemAt(where) !== item) return;
1236
+ if (isColumn && !item.columns?.includes(owner)) return;
1237
+
1238
+ if (answer.rules) owner[key] = answer.rules;
1239
+ else delete owner[key];
1240
+
1241
+ restructured(`highlight:${item.id}${isColumn ? `:${data.index}` : rows ? ':rows' : ''}`);
1242
+ });
1243
+ return;
1244
+ }
1245
+
1246
+ case 'format-india':
1247
+ state.layout.format = { ...(state.layout.format ?? {}), ...INDIAN_DEFAULTS };
1248
+ restructured('format:report:india');
1249
+ return;
1250
+
1251
+ case 'add-group-level':
1252
+ if (item?.type !== 'table') return;
1253
+
1254
+ if (addGroupLevel(item) !== -1) restructured(`grouping:add-level:${item.id}`);
1255
+ return;
1256
+
1257
+ case 'remove-group-level':
1258
+ case 'move-group-level-out': {
1259
+ if (item?.type !== 'table') return;
1260
+
1261
+ const level = Number(data?.level);
1262
+ const done = action === 'remove-group-level'
1263
+ ? removeGroupLevel(item, level)
1264
+ : moveGroupLevelOut(item, level);
1265
+
1266
+ if (done) {
1267
+ /**
1268
+ * A table's group band keys name levels by position, so
1269
+ * once levels move or go a selection on one of them may
1270
+ * point at another level's band - or at none.
1271
+ */
1272
+ state.selection = state.selection
1273
+ .filter(one => parseBandKey(one.band)?.table !== item.id);
1274
+ restructured(`grouping:${action}:${item.id}:${level}`);
1275
+ }
1276
+ return;
1277
+ }
1278
+
1024
1279
  case 'add-column':
1025
1280
  if (item?.type !== 'table') return;
1026
1281
  addColumn(item);
@@ -1419,7 +1674,8 @@ export function createDesigner({
1419
1674
 
1420
1675
  /** whether an item is still in the layout - undo can take it while a dialog is open */
1421
1676
  function itemStillThere(item) {
1422
- return (state.layout.bands ?? []).some(band => (band.items ?? []).includes(item));
1677
+ const bands = [...(state.layout.bands ?? []), ...groupBandsIn(state.layout).map(g => g.band)];
1678
+ return bands.some(band => (band.items ?? []).includes(item));
1423
1679
  }
1424
1680
 
1425
1681
 
@@ -1450,6 +1706,12 @@ export function createDesigner({
1450
1706
 
1451
1707
  const trimmed = String(raw).trim();
1452
1708
 
1709
+ /**
1710
+ * A table's group band is sized in pixels only: it is drawn inside the
1711
+ * table, once per group, where a share of the page means nothing.
1712
+ */
1713
+ if (parseBandKey(type) && /%$/.test(trimmed)) return;
1714
+
1453
1715
  if (trimmed === '') delete band.height;
1454
1716
  else if (/^-?[\d.]+\s*%$/.test(trimmed)) band.height = trimmed;
1455
1717
  else if (Number.isFinite(Number(trimmed))) band.height = Number(trimmed);
@@ -1461,6 +1723,34 @@ export function createDesigner({
1461
1723
  }
1462
1724
 
1463
1725
 
1726
+ /**
1727
+ * A group band's background, or none. Only a table's group bands take one:
1728
+ * they are drawn inside the table, where a shaded "Region North" row is
1729
+ * how a reader finds where each group starts.
1730
+ *
1731
+ * @param {string} type a group band key
1732
+ * @param {string|null} color a #rrggbb, or null for none
1733
+ */
1734
+ function setBandFill(type, color) {
1735
+ if (!parseBandKey(type)) return;
1736
+
1737
+ const band = findBand(state.layout, type);
1738
+ if (!band) return;
1739
+
1740
+ if (color) {
1741
+ if (band.background === color) return;
1742
+ band.background = color;
1743
+ } else {
1744
+ if (band.background == null) return;
1745
+ delete band.background;
1746
+ }
1747
+
1748
+ /** the rail is not rebuilt - a colour picker may still be open in it */
1749
+ draw();
1750
+ record(`band-fill:${type}`);
1751
+ }
1752
+
1753
+
1464
1754
  root.dataset.mode = state.mode;
1465
1755
 
1466
1756
  zoomPicker = createDropdown(bar.querySelector('[data-role="zoom-dropdown"]'));
@@ -1621,14 +1911,54 @@ export function createDesigner({
1621
1911
  */
1622
1912
  changed: (key, { rebuild = false } = {}) => {
1623
1913
  draw();
1624
- if (rebuild) drawRail();
1914
+
1915
+ /**
1916
+ * An edit can change more than the box it was typed in - with the
1917
+ * margins' Same value on, a number typed into one side is all four
1918
+ * - so the rest of the rail is brought up to date. syncPanel leaves
1919
+ * the focused box alone, so the caret stays where it is.
1920
+ */
1921
+ if (rebuild) redrawRailKeepingFocus();
1922
+ else syncPanel(panel, selected(), state.layout);
1923
+
1625
1924
  record(key);
1626
1925
  },
1627
1926
  act,
1628
1927
  toggleBand,
1629
- setBandHeight
1928
+ setBandHeight,
1929
+ setBandFill
1630
1930
  });
1631
1931
 
1932
+ /**
1933
+ * Rebuilds the rail without taking the focus out of the control being
1934
+ * typed in.
1935
+ *
1936
+ * *Group by* changes the rail's shape on the letter that turns grouping on
1937
+ * - the table's group band controls appear - and that letter is rarely the
1938
+ * last one. So the field that had the focus gets it back, caret and all,
1939
+ * and the rest of the word goes where it was being typed.
1940
+ */
1941
+ function redrawRailKeepingFocus() {
1942
+ const active = doc.activeElement;
1943
+ const key = panel.contains(active) ? active?.dataset?.field : null;
1944
+ const caret = key && typeof active.selectionStart === 'number'
1945
+ ? [active.selectionStart, active.selectionEnd]
1946
+ : null;
1947
+
1948
+ drawRail();
1949
+
1950
+ if (!key) return;
1951
+
1952
+ const again = panel.querySelector(`[data-field="${cssEscape(key)}"]:not([data-column])`);
1953
+ if (!again) return;
1954
+
1955
+ again.focus();
1956
+ if (caret && typeof again.setSelectionRange === 'function') {
1957
+ try { again.setSelectionRange(...caret); } catch { /* not a text control */ }
1958
+ }
1959
+ }
1960
+
1961
+
1632
1962
  /** the bar and the footer both carry actions; the rail wires its own */
1633
1963
  function onChromeClick(event) {
1634
1964
  const node = event.target.closest?.('[data-action]');