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
@@ -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
 
@@ -424,7 +460,7 @@ export function createDesigner({
424
460
 
425
461
  panel.innerHTML = many > 1
426
462
  ? drawManyPanel(selectedItems())
427
- : (selected() ? drawPanel(selected()) : drawReportPanel(state.layout));
463
+ : (selected() ? drawPanel(selected(), state.layout) : drawReportPanel(state.layout));
428
464
  }
429
465
 
430
466
 
@@ -556,7 +592,7 @@ export function createDesigner({
556
592
  * nothing draws an outline round empty space.
557
593
  */
558
594
  function replaceLayout(next, { id: nextId, dirty }) {
559
- state.layout = next;
595
+ state.layout = openLayout(next);
560
596
  state.selection = [];
561
597
 
562
598
  /** another report is another design; its pages have not been run yet */
@@ -738,8 +774,9 @@ export function createDesigner({
738
774
  *
739
775
  * @param {number} clientX
740
776
  * @param {number} clientY
777
+ * @param {Element|null} [target] what was right-clicked, for which column
741
778
  */
742
- function openMenu(clientX, clientY) {
779
+ function openMenu(clientX, clientY, target = null) {
743
780
  closeMenu();
744
781
 
745
782
  main.insertAdjacentHTML('beforeend', drawMenu(menuActions({
@@ -747,10 +784,12 @@ export function createDesigner({
747
784
  canPaste: state.clipboard.length > 0,
748
785
  /** should canAddTable ever say no, a copy of a table is what it refuses */
749
786
  canDuplicate: canAddTable(state.layout)
750
- || !selectedItems().some(item => item.type === 'table')
787
+ || !selectedItems().some(item => item.type === 'table'),
788
+ ...aboutTarget(target)
751
789
  })));
752
790
 
753
791
  menu = main.querySelector('[data-role="menu"]');
792
+ menu.addEventListener('keydown', onMenuArrows);
754
793
 
755
794
  /**
756
795
  * Bound to the pill, not to the page area: a delegated listener out
@@ -799,7 +838,7 @@ export function createDesigner({
799
838
  setSelection(null);
800
839
  }
801
840
 
802
- openMenu(event.clientX, event.clientY);
841
+ openMenu(event.clientX, event.clientY, event.target);
803
842
  }
804
843
 
805
844
 
@@ -815,6 +854,92 @@ export function createDesigner({
815
854
  }
816
855
 
817
856
 
857
+ /**
858
+ * What a Format entry in the menu would format: the text item that was
859
+ * right-clicked, or the column of the table cell under the pointer. One
860
+ * item only - a format is one item's, and a choice made for six would be
861
+ * a different edit from the one the editor describes.
862
+ *
863
+ * @param {Element|null} target
864
+ * @returns {{label: string, index?: number}|null}
865
+ */
866
+ function formatTarget(target) {
867
+ if (state.selection.length !== 1) return null;
868
+
869
+ const item = selected();
870
+ if (item?.type === 'text') return { label: 'Format…' };
871
+ if (item?.type !== 'table') return null;
872
+
873
+ const index = columnUnder(target);
874
+ if (index === null) return null;
875
+
876
+ const column = item.columns[index];
877
+ return { label: `Format "${column.label || column.field}" column…`, index };
878
+ }
879
+
880
+
881
+ /**
882
+ * The table column under the pointer: a data cell or a heading, not a
883
+ * group band's full-width row - or null.
884
+ * @param {Element|null} target
885
+ * @returns {number|null}
886
+ */
887
+ function columnUnder(target) {
888
+ const cell = target?.closest?.('td, th');
889
+ if (!cell || cell.classList.contains('group-band-cell')) return null;
890
+
891
+ return selected()?.columns?.[cell.cellIndex] ? cell.cellIndex : null;
892
+ }
893
+
894
+
895
+ /**
896
+ * What the menu's entries about the item itself act on: Format and
897
+ * Highlight for a text item or the column under the pointer, and the
898
+ * table's rows for a table.
899
+ * @param {Element|null} target
900
+ * @returns {{format: object|null, highlight: object|null, rows: boolean}}
901
+ */
902
+ function aboutTarget(target) {
903
+ const format = formatTarget(target);
904
+ const item = state.selection.length === 1 ? selected() : null;
905
+
906
+ let highlight = null;
907
+ if (item?.type === 'text') highlight = { label: 'Highlight…' };
908
+ if (format && item?.type === 'table') {
909
+ const column = item.columns[format.index];
910
+ highlight = { label: `Highlight "${column.label || column.field}" column…`, index: format.index };
911
+ }
912
+
913
+ return { format, highlight, rows: item?.type === 'table' };
914
+ }
915
+
916
+
917
+ /**
918
+ * Up and down move through the menu, as in any other. Stopped here, on the
919
+ * menu: on their way to the document they would nudge the selection too.
920
+ */
921
+ function onMenuArrows(event) {
922
+ const keys = ['ArrowDown', 'ArrowUp', 'Home', 'End'];
923
+ if (!keys.includes(event.key)) return;
924
+
925
+ event.preventDefault();
926
+ event.stopPropagation();
927
+
928
+ const items = [...menu.querySelectorAll('button:not([disabled])')];
929
+ if (!items.length) return;
930
+
931
+ const at = items.indexOf(menu.ownerDocument.activeElement);
932
+ const next = {
933
+ ArrowDown: (at + 1) % items.length,
934
+ ArrowUp: (at - 1 + items.length) % items.length,
935
+ Home: 0,
936
+ End: items.length - 1
937
+ }[event.key];
938
+
939
+ items[next].focus();
940
+ }
941
+
942
+
818
943
  /** anything that is not the pill closes it */
819
944
  function onDocumentDown(event) {
820
945
  if (menu && !menu.contains(event.target)) closeMenu();
@@ -899,7 +1024,8 @@ export function createDesigner({
899
1024
  }
900
1025
 
901
1026
  case 'add-table': {
902
- const type = targetBand(state.layout, primary());
1027
+ /** never onto a group band, which is drawn inside a table */
1028
+ const type = targetBand(state.layout, primary(), { table: true });
903
1029
  if (!type) return;
904
1030
 
905
1031
  if (!canAddTable(state.layout)) return;
@@ -1021,6 +1147,115 @@ export function createDesigner({
1021
1147
  return;
1022
1148
  }
1023
1149
 
1150
+ case 'copy-grouping':
1151
+ if (item?.type !== 'table' || !data?.from) return;
1152
+
1153
+ if (copyGroupDesign(state.layout, item.id, data.from)) {
1154
+ /** whatever was selected on the bands just replaced is gone */
1155
+ state.selection = state.selection.filter(one => itemAt(one));
1156
+ restructured(`grouping:copy:${item.id}`);
1157
+ }
1158
+ return;
1159
+
1160
+ case 'edit-format': {
1161
+ /** a text item's own format, or one of a table's columns' */
1162
+ const isColumn = data?.index != null && item?.type === 'table';
1163
+ const owner = isColumn ? item.columns?.[Number(data.index)] : item;
1164
+ if (!owner || (!isColumn && item?.type !== 'text')) return;
1165
+
1166
+ const name = isColumn ? (owner.label || owner.field || 'column') : item.id;
1167
+ const where = primary();
1168
+
1169
+ askFormat(root, owner.format, state.layout.format, { title: `Format: ${name}` })
1170
+ .then(answer => {
1171
+ if (!answer) return;
1172
+
1173
+ /**
1174
+ * Still the same item, holding the same column - an undo
1175
+ * while the dialog was up swaps in a different layout.
1176
+ */
1177
+ if (itemAt(where) !== item) return;
1178
+ if (isColumn && !item.columns?.includes(owner)) return;
1179
+
1180
+ if (answer.format) owner.format = answer.format;
1181
+ else delete owner.format;
1182
+
1183
+ restructured(`format:${item.id}${isColumn ? `:${data.index}` : ''}`);
1184
+ });
1185
+ return;
1186
+ }
1187
+
1188
+ case 'edit-highlight':
1189
+ case 'edit-row-highlight': {
1190
+ const rows = action === 'edit-row-highlight';
1191
+ const isColumn = !rows && data?.index != null && item?.type === 'table';
1192
+ const owner = isColumn ? item.columns?.[Number(data.index)] : item;
1193
+
1194
+ if (!owner) return;
1195
+ if (rows ? item.type !== 'table' : (!isColumn && item.type !== 'text')) return;
1196
+
1197
+ const key = rows ? 'rowHighlight' : 'highlight';
1198
+ const name = rows ? `${item.id} rows`
1199
+ : isColumn ? (owner.label || owner.field || 'column') : item.id;
1200
+
1201
+ /** what a rule may test: a table's columns, or the data's fields */
1202
+ const fields = item.type === 'table'
1203
+ ? (item.columns ?? []).map(c => c.field).filter(Boolean)
1204
+ : availableFields(state.layout);
1205
+
1206
+ const where = primary();
1207
+
1208
+ askHighlight(root, owner[key], {
1209
+ title: `Highlight: ${name}`,
1210
+ fields: isColumn ? fields.filter(f => f !== owner.field) : fields,
1211
+ rows,
1212
+ format: isColumn || item.type === 'text' ? effectiveFormat(owner.format, state.layout.format) : null
1213
+ }).then(answer => {
1214
+ if (!answer) return;
1215
+ if (itemAt(where) !== item) return;
1216
+ if (isColumn && !item.columns?.includes(owner)) return;
1217
+
1218
+ if (answer.rules) owner[key] = answer.rules;
1219
+ else delete owner[key];
1220
+
1221
+ restructured(`highlight:${item.id}${isColumn ? `:${data.index}` : rows ? ':rows' : ''}`);
1222
+ });
1223
+ return;
1224
+ }
1225
+
1226
+ case 'format-india':
1227
+ state.layout.format = { ...(state.layout.format ?? {}), ...INDIAN_DEFAULTS };
1228
+ restructured('format:report:india');
1229
+ return;
1230
+
1231
+ case 'add-group-level':
1232
+ if (item?.type !== 'table') return;
1233
+
1234
+ if (addGroupLevel(item) !== -1) restructured(`grouping:add-level:${item.id}`);
1235
+ return;
1236
+
1237
+ case 'remove-group-level':
1238
+ case 'move-group-level-out': {
1239
+ if (item?.type !== 'table') return;
1240
+
1241
+ const level = Number(data?.level);
1242
+ const done = action === 'remove-group-level'
1243
+ ? removeGroupLevel(item, level)
1244
+ : moveGroupLevelOut(item, level);
1245
+
1246
+ if (done) {
1247
+ /**
1248
+ * A table's group band keys name levels by position, so
1249
+ * once levels move or go a selection on one of them may
1250
+ * point at another level's band - or at none.
1251
+ */
1252
+ state.selection = state.selection
1253
+ .filter(one => parseBandKey(one.band)?.table !== item.id);
1254
+ restructured(`grouping:${action}:${item.id}:${level}`);
1255
+ }
1256
+ return;
1257
+ }
1258
+
1024
1259
  case 'add-column':
1025
1260
  if (item?.type !== 'table') return;
1026
1261
  addColumn(item);
@@ -1419,7 +1654,8 @@ export function createDesigner({
1419
1654
 
1420
1655
  /** whether an item is still in the layout - undo can take it while a dialog is open */
1421
1656
  function itemStillThere(item) {
1422
- return (state.layout.bands ?? []).some(band => (band.items ?? []).includes(item));
1657
+ const bands = [...(state.layout.bands ?? []), ...groupBandsIn(state.layout).map(g => g.band)];
1658
+ return bands.some(band => (band.items ?? []).includes(item));
1423
1659
  }
1424
1660
 
1425
1661
 
@@ -1450,6 +1686,12 @@ export function createDesigner({
1450
1686
 
1451
1687
  const trimmed = String(raw).trim();
1452
1688
 
1689
+ /**
1690
+ * A table's group band is sized in pixels only: it is drawn inside the
1691
+ * table, once per group, where a share of the page means nothing.
1692
+ */
1693
+ if (parseBandKey(type) && /%$/.test(trimmed)) return;
1694
+
1453
1695
  if (trimmed === '') delete band.height;
1454
1696
  else if (/^-?[\d.]+\s*%$/.test(trimmed)) band.height = trimmed;
1455
1697
  else if (Number.isFinite(Number(trimmed))) band.height = Number(trimmed);
@@ -1461,6 +1703,34 @@ export function createDesigner({
1461
1703
  }
1462
1704
 
1463
1705
 
1706
+ /**
1707
+ * A group band's background, or none. Only a table's group bands take one:
1708
+ * they are drawn inside the table, where a shaded "Region North" row is
1709
+ * how a reader finds where each group starts.
1710
+ *
1711
+ * @param {string} type a group band key
1712
+ * @param {string|null} color a #rrggbb, or null for none
1713
+ */
1714
+ function setBandFill(type, color) {
1715
+ if (!parseBandKey(type)) return;
1716
+
1717
+ const band = findBand(state.layout, type);
1718
+ if (!band) return;
1719
+
1720
+ if (color) {
1721
+ if (band.background === color) return;
1722
+ band.background = color;
1723
+ } else {
1724
+ if (band.background == null) return;
1725
+ delete band.background;
1726
+ }
1727
+
1728
+ /** the rail is not rebuilt - a colour picker may still be open in it */
1729
+ draw();
1730
+ record(`band-fill:${type}`);
1731
+ }
1732
+
1733
+
1464
1734
  root.dataset.mode = state.mode;
1465
1735
 
1466
1736
  zoomPicker = createDropdown(bar.querySelector('[data-role="zoom-dropdown"]'));
@@ -1621,14 +1891,45 @@ export function createDesigner({
1621
1891
  */
1622
1892
  changed: (key, { rebuild = false } = {}) => {
1623
1893
  draw();
1624
- if (rebuild) drawRail();
1894
+ if (rebuild) redrawRailKeepingFocus();
1625
1895
  record(key);
1626
1896
  },
1627
1897
  act,
1628
1898
  toggleBand,
1629
- setBandHeight
1899
+ setBandHeight,
1900
+ setBandFill
1630
1901
  });
1631
1902
 
1903
+ /**
1904
+ * Rebuilds the rail without taking the focus out of the control being
1905
+ * typed in.
1906
+ *
1907
+ * *Group by* changes the rail's shape on the letter that turns grouping on
1908
+ * - the table's group band controls appear - and that letter is rarely the
1909
+ * last one. So the field that had the focus gets it back, caret and all,
1910
+ * and the rest of the word goes where it was being typed.
1911
+ */
1912
+ function redrawRailKeepingFocus() {
1913
+ const active = doc.activeElement;
1914
+ const key = panel.contains(active) ? active?.dataset?.field : null;
1915
+ const caret = key && typeof active.selectionStart === 'number'
1916
+ ? [active.selectionStart, active.selectionEnd]
1917
+ : null;
1918
+
1919
+ drawRail();
1920
+
1921
+ if (!key) return;
1922
+
1923
+ const again = panel.querySelector(`[data-field="${cssEscape(key)}"]:not([data-column])`);
1924
+ if (!again) return;
1925
+
1926
+ again.focus();
1927
+ if (caret && typeof again.setSelectionRange === 'function') {
1928
+ try { again.setSelectionRange(...caret); } catch { /* not a text control */ }
1929
+ }
1930
+ }
1931
+
1932
+
1632
1933
  /** the bar and the footer both carry actions; the rail wires its own */
1633
1934
  function onChromeClick(event) {
1634
1935
  const node = event.target.closest?.('[data-action]');