report-studio 1.3.0 → 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.
@@ -44,10 +44,15 @@ const STYLES = [
44
44
  { label: 'Italic', value: 'italic' }
45
45
  ];
46
46
 
47
+ /**
48
+ * A choice with an `icon` on every option can be drawn as a row of glyph
49
+ * buttons rather than a dropdown, as a design tool draws text alignment - the
50
+ * field asks for that with `segmented`. The label is still each button's name.
51
+ */
47
52
  const ALIGNMENTS = [
48
- { label: 'Left', value: 'left' },
49
- { label: 'Centre', value: 'center' },
50
- { label: 'Right', value: 'right' }
53
+ { label: 'Left', value: 'left', icon: 'alignLeft' },
54
+ { label: 'Centre', value: 'center', icon: 'alignCenter' },
55
+ { label: 'Right', value: 'right', icon: 'alignRight' }
51
56
  ];
52
57
 
53
58
  const IMAGE_FITS = [
@@ -57,8 +62,8 @@ const IMAGE_FITS = [
57
62
  ];
58
63
 
59
64
  const ORIENTATIONS = [
60
- { label: 'Horizontal', value: 'horizontal' },
61
- { label: 'Vertical', value: 'vertical' }
65
+ { label: 'Horizontal', value: 'horizontal', icon: 'horizontal' },
66
+ { label: 'Vertical', value: 'vertical', icon: 'vertical' }
62
67
  ];
63
68
 
64
69
  /**
@@ -80,6 +85,16 @@ const LINE_STYLES = [
80
85
  const RULE_STYLES = [...LINE_STYLES, { label: 'None', value: 'none' }];
81
86
 
82
87
 
88
+ /**
89
+ * Two hints for the rail's layout, which say nothing about the value:
90
+ *
91
+ * short a letter drawn inside the box in place of a label above it, as X,
92
+ * Y, W and H are in every design tool. The full label is still there
93
+ * for a screen reader and a tooltip
94
+ * half the control takes one of the rail's two columns rather than both.
95
+ * Numbers and colours always do; a choice or a text box asks, when
96
+ * what it holds is short enough to read at half the width
97
+ */
83
98
  const num = (key, label, extra = {}) =>
84
99
  ({ key, label, type: 'number', ...extra });
85
100
 
@@ -108,10 +123,10 @@ function maxRuleWidth(item) {
108
123
  * readField and writeField need, since they walk a path on whatever they get.
109
124
  */
110
125
  export const COLUMN_FIELDS = [
111
- { key: 'field', label: 'Field', type: 'text' },
112
- { key: 'label', label: 'Label', type: 'text' },
126
+ { key: 'field', label: 'Field', type: 'text', half: true },
127
+ { key: 'label', label: 'Label', type: 'text', half: true },
113
128
  { key: 'width', label: 'Width', type: 'number', min: 1 },
114
- { key: 'align', label: 'Align', type: 'choice', options: ALIGNMENTS },
129
+ { key: 'align', label: 'Align', type: 'choice', options: ALIGNMENTS, half: true, segmented: true },
115
130
  {
116
131
  key: 'expression', label: 'Calculation', type: 'text',
117
132
  hint: 'Blank shows the field as it is. A calculation - {qty} * {price}, ' +
@@ -137,10 +152,10 @@ export function fieldsFor(item) {
137
152
  const box = {
138
153
  title: 'Position and size',
139
154
  fields: [
140
- num('x', 'X'),
141
- num('y', 'Y'),
142
- num('w', 'Width', { min: 1 }),
143
- ...(item.type === 'table' ? [] : [num('h', 'Height', { min: 1 })])
155
+ num('x', 'X', { short: 'X' }),
156
+ num('y', 'Y', { short: 'Y' }),
157
+ num('w', 'Width', { min: 1, short: 'W' }),
158
+ ...(item.type === 'table' ? [] : [num('h', 'Height', { min: 1, short: 'H' })])
144
159
  ]
145
160
  };
146
161
 
@@ -152,7 +167,7 @@ export function fieldsFor(item) {
152
167
  fields: [
153
168
  {
154
169
  key: 'style.borderStyle', label: 'Style', type: 'choice',
155
- options: RULE_STYLES
170
+ options: RULE_STYLES, half: true
156
171
  },
157
172
  num('style.borderWidth', 'Width', { min: 1 }),
158
173
  { key: 'style.borderColor', label: 'Colour', type: 'color' },
@@ -205,12 +220,12 @@ export function fieldsFor(item) {
205
220
  fields: [
206
221
  {
207
222
  key: 'orientation', label: 'Runs', type: 'choice',
208
- options: ORIENTATIONS
223
+ options: ORIENTATIONS, half: true, segmented: true
209
224
  },
210
225
  num('style.thickness', 'Thickness', { min: 1 }),
211
226
  {
212
227
  key: 'style.lineStyle', label: 'Style', type: 'choice',
213
- options: LINE_STYLES
228
+ options: LINE_STYLES, half: true
214
229
  },
215
230
  { key: 'style.color', label: 'Colour', type: 'color' }
216
231
  ]
@@ -271,7 +286,7 @@ export function fieldsFor(item) {
271
286
  },
272
287
  {
273
288
  key: 'style.borderStyle', label: 'Borders', type: 'choice',
274
- options: RULE_STYLES
289
+ options: RULE_STYLES, half: true
275
290
  },
276
291
  num('style.borderWidth', 'Border width', {
277
292
  min: 1,
@@ -303,9 +318,9 @@ export function fieldsFor(item) {
303
318
  fields: [
304
319
  { key: 'style.fontFamily', label: 'Font', type: 'choice', options: FONT_STACKS },
305
320
  num('style.fontSize', 'Size', { min: 1 }),
306
- { key: 'style.fontWeight', label: 'Weight', type: 'choice', options: WEIGHTS },
307
- { key: 'style.fontStyle', label: 'Style', type: 'choice', options: STYLES },
308
- { key: 'style.align', label: 'Align', type: 'choice', options: ALIGNMENTS },
321
+ { key: 'style.fontWeight', label: 'Weight', type: 'choice', options: WEIGHTS, half: true },
322
+ { key: 'style.fontStyle', label: 'Style', type: 'choice', options: STYLES, half: true },
323
+ { key: 'style.align', label: 'Align', type: 'choice', options: ALIGNMENTS, half: true, segmented: true },
309
324
  { key: 'style.color', label: 'Colour', type: 'color' }
310
325
  ]
311
326
  }
@@ -470,9 +485,11 @@ function pageFields(layout) {
470
485
  label: 'Orientation',
471
486
  type: 'choice',
472
487
  rebuilds: true,
488
+ half: true,
489
+ segmented: true,
473
490
  options: [
474
- { value: 'portrait', label: 'Portrait' },
475
- { value: 'landscape', label: 'Landscape' }
491
+ { value: 'portrait', label: 'Portrait', icon: 'portrait' },
492
+ { value: 'landscape', label: 'Landscape', icon: 'landscape' }
476
493
  ],
477
494
  read: (target) => orientationOf(target.page),
478
495
  write: (target, value) => setOrientation(target, value)
@@ -485,6 +502,186 @@ function pageFields(layout) {
485
502
  }
486
503
 
487
504
 
505
+ /* ---------------- margins ---------------- */
506
+
507
+ export const MARGIN_SIDES = ['top', 'right', 'bottom', 'left'];
508
+
509
+ const SIDE_LABELS = { top: 'Top', right: 'Right', bottom: 'Bottom', left: 'Left' };
510
+
511
+
512
+ /**
513
+ * A margin is switched off by printing it as 0 and keeping what it was beside
514
+ * it, in `page.marginOff` - `{ left: 40 }` for a left margin switched off at
515
+ * 40px. Switching it back on puts the 40 back.
516
+ *
517
+ * `page.margin` therefore always holds the margins that print, and the engine,
518
+ * the renderer and the canvas - which all read it directly - never have to
519
+ * know a margin can be off. Only the rail and the validator do.
520
+ *
521
+ * `page.marginLinked` is the lock a design tool has on padding: while it is
522
+ * set, a number typed into any side is written to all four.
523
+ *
524
+ * Both keys are left off the file entirely when they say nothing, as `paper`
525
+ * is: a report with every margin on and no lock looks as it always did.
526
+ */
527
+
528
+
529
+ /** @returns {boolean} whether the side prints its margin */
530
+ export function marginIsOn(page, side) {
531
+ return !(page?.marginOff && side in page.marginOff);
532
+ }
533
+
534
+
535
+ /** the side's number - what it prints, or what it will print once back on */
536
+ function marginValue(page, side) {
537
+ return marginIsOn(page, side) ? page?.margin?.[side] : page.marginOff[side];
538
+ }
539
+
540
+
541
+ /** an empty `marginOff` says nothing, so it is not written */
542
+ function tidyMarginOff(page) {
543
+ if (page.marginOff && !Object.keys(page.marginOff).length) delete page.marginOff;
544
+ }
545
+
546
+
547
+ /**
548
+ * Switches one side's margin off or back on.
549
+ *
550
+ * @param {object} page mutated in place
551
+ * @param {string} side
552
+ * @param {boolean} on
553
+ * @returns {boolean} whether anything changed
554
+ */
555
+ export function setMarginOn(page, side, on) {
556
+ if (marginIsOn(page, side) === on) return false;
557
+
558
+ if (on) {
559
+ page.margin[side] = page.marginOff[side];
560
+ delete page.marginOff[side];
561
+ tidyMarginOff(page);
562
+ } else {
563
+ page.marginOff = { ...page.marginOff, [side]: page.margin[side] };
564
+ page.margin[side] = 0;
565
+ }
566
+
567
+ return true;
568
+ }
569
+
570
+
571
+ /**
572
+ * The section's own switch: off takes every margin away, on puts every one
573
+ * back. It reads as on while any side is, so one click on a page with some
574
+ * margins switched off clears the rest, and the next brings them all back.
575
+ *
576
+ * @param {object} page mutated in place
577
+ * @param {boolean} on
578
+ * @returns {boolean} whether anything changed
579
+ */
580
+ export function setMarginsOn(page, on) {
581
+ let changed = false;
582
+ for (const side of MARGIN_SIDES) changed = setMarginOn(page, side, on) || changed;
583
+ return changed;
584
+ }
585
+
586
+
587
+ /**
588
+ * Sets a side's number. A side that is off keeps the number for when it is
589
+ * switched on, and still prints nothing. With the lock on, every side takes it.
590
+ *
591
+ * @param {object} page mutated in place
592
+ * @param {string} side
593
+ * @param {number} value
594
+ * @returns {boolean} whether anything changed
595
+ */
596
+ export function setMarginValue(page, side, value) {
597
+ const sides = page.marginLinked ? MARGIN_SIDES : [side];
598
+ let changed = false;
599
+
600
+ for (const one of sides) {
601
+ const store = marginIsOn(page, one) ? page.margin : page.marginOff;
602
+ if (store[one] === value) continue;
603
+
604
+ store[one] = value;
605
+ changed = true;
606
+ }
607
+
608
+ return changed;
609
+ }
610
+
611
+
612
+ /**
613
+ * Turns the lock on or off. Locking makes the four sides one number, so they
614
+ * all take the first side that is printing one - the top, usually - or the
615
+ * top's own if none is.
616
+ *
617
+ * @param {object} page mutated in place
618
+ * @param {boolean} linked
619
+ * @returns {boolean} whether anything changed
620
+ */
621
+ export function setMarginLinked(page, linked) {
622
+ if (Boolean(page.marginLinked) === linked) return false;
623
+
624
+ if (!linked) {
625
+ delete page.marginLinked;
626
+ return true;
627
+ }
628
+
629
+ page.marginLinked = true;
630
+
631
+ const lead = MARGIN_SIDES.find(side => marginIsOn(page, side)) ?? 'top';
632
+ setMarginValue(page, lead, marginValue(page, lead));
633
+
634
+ return true;
635
+ }
636
+
637
+
638
+ /**
639
+ * The Margins section: a switch for all of them and the lock across the top,
640
+ * then a row per side - its switch on the left, its number on the right. A
641
+ * side that is off shows the number it will come back with, greyed and not
642
+ * editable; the switches rebuild the rail so the boxes follow them.
643
+ *
644
+ * The number boxes keep the paths they always had, so anything that finds a
645
+ * margin's box by `page.margin.top` still does.
646
+ *
647
+ * @param {object} layout
648
+ * @returns {object[]}
649
+ */
650
+ function marginFields(layout) {
651
+ const all = {
652
+ key: 'page.marginsOn', label: 'All sides', type: 'toggle', half: true,
653
+ rebuilds: true,
654
+ read: (target) => MARGIN_SIDES.some(side => marginIsOn(target.page, side)),
655
+ write: (target, on) => setMarginsOn(target.page, on)
656
+ };
657
+
658
+ const linked = {
659
+ key: 'page.marginLinked', label: 'Same value', type: 'toggle', half: true,
660
+ rebuilds: true,
661
+ read: (target) => Boolean(target.page?.marginLinked),
662
+ write: (target, on) => setMarginLinked(target.page, on)
663
+ };
664
+
665
+ const rows = MARGIN_SIDES.flatMap(side => [
666
+ {
667
+ key: `page.marginOn.${side}`, label: SIDE_LABELS[side], type: 'toggle', half: true,
668
+ rebuilds: true,
669
+ read: (target) => marginIsOn(target.page, side),
670
+ write: (target, on) => setMarginOn(target.page, side, on)
671
+ },
672
+ num(`page.margin.${side}`, `${SIDE_LABELS[side]} margin`, {
673
+ min: 0,
674
+ short: SIDE_LABELS[side][0],
675
+ disabled: !marginIsOn(layout.page, side),
676
+ read: (target) => marginValue(target.page, side),
677
+ write: (target, value) => setMarginValue(target.page, side, value)
678
+ })
679
+ ]);
680
+
681
+ return [all, linked, ...rows];
682
+ }
683
+
684
+
488
685
  /**
489
686
  * The report's own properties - what the rail shows when no item is selected.
490
687
  *
@@ -521,12 +718,7 @@ export function reportFields(layout) {
521
718
  },
522
719
  {
523
720
  title: 'Margins',
524
- fields: [
525
- num('page.margin.top', 'Top', { min: 0 }),
526
- num('page.margin.right', 'Right', { min: 0 }),
527
- num('page.margin.bottom', 'Bottom', { min: 0 }),
528
- num('page.margin.left', 'Left', { min: 0 })
529
- ]
721
+ fields: marginFields(layout)
530
722
  },
531
723
  {
532
724
  /**
@@ -117,6 +117,19 @@ export const ICONS = {
117
117
  <path d="M2.25 13.75h11.5"/>`),
118
118
 
119
119
  /** a disk: the shutter above, the label below */
120
+ /** text alignment: three rules, ragged on the side the text is not set to */
121
+ alignLeft: wrap(`<path d="M2.75 4h10.5M2.75 8h6.5M2.75 12h8.5"/>`),
122
+ alignCenter: wrap(`<path d="M2.75 4h10.5M4.75 8h6.5M3.75 12h8.5"/>`),
123
+ alignRight: wrap(`<path d="M2.75 4h10.5M6.75 8h6.5M4.75 12h8.5"/>`),
124
+
125
+ /** which way a line runs */
126
+ horizontal: wrap(`<path d="M2.5 8h11"/>`),
127
+ vertical: wrap(`<path d="M8 2.5v11"/>`),
128
+
129
+ /** a sheet stood up, and the same sheet on its side */
130
+ portrait: wrap(`<rect x="4.25" y="2.25" width="7.5" height="11.5" rx="1"/>`),
131
+ landscape: wrap(`<rect x="2.25" y="4.25" width="11.5" height="7.5" rx="1"/>`),
132
+
120
133
  save: wrap(`<path d="M2.75 3.25h8.5l2.5 2.5v7a.5.5 0 0 1-.5.5h-11
121
134
  a.5.5 0 0 1-.5-.5v-9a.5.5 0 0 1 .5-.5z"/>
122
135
  <path d="M5 3.25v3.5h5.5v-3.5M5 13.25V9.5h6v3.75"/>`)