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
@@ -19,32 +19,246 @@
19
19
 
20
20
  import {
21
21
  fieldsFor, allFields, reportFields, allReportFields,
22
- COLUMN_FIELDS, readField, writeField
22
+ COLUMN_FIELDS, readField, writeField, clearField
23
23
  } from './fields.js';
24
- import { BAND_TYPES, BAND_NOTES, hasBand, findBand } from './structure.js';
24
+ import { BAND_NOTES, hasBand, findBand, railBands } from './structure.js';
25
+ import {
26
+ parseBandKey, bandLabel, groupBandKey, groupedTables, setGroupBy, setGroupOption, GROUP_KINDS
27
+ } from './grouping.js';
28
+ import { levelsOf, MAX_GROUP_LEVELS } from '../engine/tables.js';
29
+ import { describeFormat } from '../engine/format.js';
30
+ import { describeHighlight } from '../engine/highlight.js';
25
31
  import { esc, imageSrc } from '../render/items.js';
32
+ import { icon } from './icons.js';
33
+ import { choiceBox, showChoice, refreshChoices, attachChoices } from './choice.js';
26
34
 
27
35
 
28
36
  /**
29
37
  * The rail describes what a thing is; what you can do to it lives in the pill
30
38
  * that opens on a right-click, beside the item rather than at the edge of the
31
39
  * screen. See menu.js.
40
+ *
41
+ * It is laid out the way a design tool's inspector is: sections divided by a
42
+ * rule rather than boxed, a two-column grid that pairs X with Y and W with H,
43
+ * borderless boxes that only outline themselves on hover, and the label inside
44
+ * the box wherever a letter says enough. Every number's label can be dragged
45
+ * sideways to change it, as in Figma.
46
+ */
47
+
48
+
49
+ /**
50
+ * The row across the top of the rail: what is selected, by its glyph and id.
51
+ * @param {string} type shown under the id, and what the tests read
52
+ * @param {string} [name]
53
+ * @param {string} [glyph] markup for the icon
54
+ * @returns {string} markup
32
55
  */
56
+ function panelHead(type, name, glyph = '') {
57
+ return `
58
+ <div class="dz-panel-head">
59
+ ${glyph ? `<span class="dz-panel-icon">${glyph}</span>` : ''}
60
+ ${name != null ? `<span class="dz-panel-id">${esc(name)}</span>` : ''}
61
+ <span class="dz-panel-type">${esc(type)}</span>
62
+ </div>`;
63
+ }
64
+
65
+
66
+ /**
67
+ * A section's title, with any buttons that act on the section as a whole on
68
+ * the right of it - where the + that adds a row sits in a design tool.
69
+ * @param {string} title
70
+ * @param {string} [actions] markup
71
+ * @returns {string} markup
72
+ */
73
+ function sectionHead(title, actions = '') {
74
+ return `
75
+ <div class="dz-section-head">
76
+ <h3>${esc(title)}</h3>
77
+ ${actions ? `<span class="dz-section-tools">${actions}</span>` : ''}
78
+ </div>`;
79
+ }
80
+
81
+
82
+ /** a + drawn rather than typed, so it sits on the same grid as the glyphs */
83
+ const PLUS = `<svg viewBox="0 0 16 16" width="14" height="14" fill="none"
84
+ stroke="currentColor" stroke-width="1.5" stroke-linecap="round"
85
+ aria-hidden="true" focusable="false"><path d="M8 3.5v9M3.5 8h9"/></svg>`;
33
86
 
34
87
 
35
88
  /**
36
89
  * The rail for a selected item.
37
90
  * @param {object} item
91
+ * @param {object} [layout] the report it is in, which a grouped table's own
92
+ * group band controls need - the other grouped tables, to copy a design from
38
93
  * @returns {string} markup
39
94
  */
40
- export function drawPanel(item) {
95
+ export function drawPanel(item, layout = null) {
41
96
  return `
42
- <div class="dz-panel-head">
43
- <span class="dz-panel-type">${esc(item.type)}</span>
44
- <span class="dz-panel-id">${esc(item.id)}</span>
45
- </div>
97
+ ${panelHead(item.type, item.id, icon(item.type))}
46
98
  ${sections(item, fieldsFor(item))}
47
- ${item.type === 'table' ? columns(item) : ''}`;
99
+ ${item.type === 'text' ? `
100
+ <section class="dz-section">
101
+ ${sectionHead('Format')}
102
+ <div class="dz-field dz-field-format">
103
+ <label class="dz-label">Values</label>
104
+ ${formatButton(item, layout)}
105
+ </div>
106
+ <div class="dz-field dz-field-format">
107
+ <label class="dz-label">Highlight</label>
108
+ ${highlightButton(item.highlight, 'edit-highlight')}
109
+ </div>
110
+ </section>` : ''}
111
+ ${item.type === 'table' ? `
112
+ <section class="dz-section">
113
+ ${sectionHead('Row highlight')}
114
+ <div class="dz-field dz-field-format">
115
+ <label class="dz-label">Rows</label>
116
+ ${highlightButton(item.rowHighlight, 'edit-row-highlight')}
117
+ </div>
118
+ </section>` : ''}
119
+ ${item.type === 'table' ? grouping(item, layout) : ''}
120
+ ${item.type === 'table' ? columns(item, layout) : ''}`;
121
+ }
122
+
123
+
124
+ /**
125
+ * A grouped table's own group bands, switched on and off and sized here as
126
+ * well as in the report's band list - this is where somebody who has just set
127
+ * *Group by* is looking.
128
+ *
129
+ * And, when another table is grouped too, a way to give this one the same
130
+ * header and footer: each grouping is a table's own, so two that should look
131
+ * alike are made alike by copying.
132
+ *
133
+ * @param {object} table
134
+ * @param {object|null} layout
135
+ * @returns {string} markup, empty for a table that is not grouped
136
+ */
137
+ function grouping(table, layout) {
138
+ const levels = levelsOf(table);
139
+ if (!levels.length) return '';
140
+
141
+ /** the table alone is enough to find its own bands */
142
+ const scope = layout ?? { bands: [{ type: 'detail', items: [table] }] };
143
+
144
+ const others = layout
145
+ ? groupedTables(layout).filter(g => g.table !== table.id)
146
+ : [];
147
+
148
+ return `
149
+ <section class="dz-section dz-bands dz-grouping">
150
+ ${sectionHead('Grouping')}
151
+ ${levels.map((level, n) => groupLevel(scope, table, level, n, levels.length)).join('')}
152
+ ${levels.length < MAX_GROUP_LEVELS ? `
153
+ <button type="button" class="dz-add-row" data-action="add-group-level"
154
+ title="Split each group of this table again, by another field">
155
+ Group within each group</button>` : ''}
156
+ ${others.map(g => `
157
+ <button type="button" class="dz-add-row" data-action="copy-grouping"
158
+ data-from="${esc(g.table)}"
159
+ title="Replace this table's group header and footer with copies of ${esc(g.table)}'s">
160
+ Copy group design from ${esc(g.table)}</button>`).join('')}
161
+ </section>`;
162
+ }
163
+
164
+
165
+ /**
166
+ * One level of a table's grouping: its band switches and, for a table grouped
167
+ * at more than one level, which field it groups by and the controls that move
168
+ * and remove it.
169
+ *
170
+ * The outermost level's field is the table's *Group by*, up with the table's
171
+ * other properties, so a table grouped once shows nothing here but its bands -
172
+ * as it always has.
173
+ *
174
+ * @param {object} scope a layout holding the table
175
+ * @param {object} table
176
+ * @param {object} level
177
+ * @param {number} n
178
+ * @param {number} depth how many levels the table has
179
+ * @returns {string} markup
180
+ */
181
+ function groupLevel(scope, table, level, n, depth) {
182
+ const by = typeof level?.by === 'string' ? level.by : '';
183
+ const bands = levelBands(scope, table, by, n);
184
+
185
+ if (depth === 1) return `${levelOptions(level, n)}${bands}`;
186
+
187
+ const id = `dz-f-group-level-${n}`;
188
+
189
+ return `
190
+ <div class="dz-group-level" data-level="${n}">
191
+ <div class="dz-group-level-head">
192
+ <span class="dz-group-level-name">Level ${n + 1}${by ? ` · <b>${esc(by)}</b>` : ''}</span>
193
+ <span class="dz-group-level-actions">
194
+ ${n > 0 ? `
195
+ <button type="button" class="dz-icon" data-action="move-group-level-out"
196
+ data-level="${n}"
197
+ title="Group by this before the level above it"
198
+ aria-label="Move level ${n + 1} out">&uarr;</button>` : ''}
199
+ <button type="button" class="dz-icon" data-action="remove-group-level"
200
+ data-level="${n}"
201
+ title="Stop grouping by this, and remove its header and footer"
202
+ aria-label="Remove level ${n + 1}">&times;</button>
203
+ </span>
204
+ </div>
205
+ ${n > 0 ? `
206
+ <div class="dz-field dz-field-text">
207
+ <label class="dz-label" for="${id}">Then group by</label>
208
+ <input type="text" id="${id}" data-group-level="${n}"
209
+ value="${esc(by)}" spellcheck="false">
210
+ <p class="dz-hint">A field to group each group above by</p>
211
+ </div>` : ''}
212
+ ${levelOptions(level, n)}
213
+ ${bands}
214
+ </div>`;
215
+ }
216
+
217
+
218
+ /**
219
+ * A level's header and footer switches, named for what they are inside the
220
+ * table's own section - "Header, before each region" - rather than by the key
221
+ * the report's band list needs.
222
+ */
223
+ function levelBands(scope, table, by, n) {
224
+ const what = by || 'group';
225
+
226
+ return GROUP_KINDS.map(kind => bandRow(scope, groupBandKey(kind, table.id, n),
227
+ kind === 'groupHeader'
228
+ ? { label: 'Group header', note: `Before each ${what}` }
229
+ : { label: 'Group footer', note: `After each ${what}` })).join('');
230
+ }
231
+
232
+
233
+ /** the orders a level's groups can be put in, as the rail offers them */
234
+ const SORT_CHOICES = [
235
+ { value: '', label: 'As they come' },
236
+ { value: 'asc', label: 'A to Z, 0 to 9' },
237
+ { value: 'desc', label: 'Z to A, 9 to 0' }
238
+ ];
239
+
240
+
241
+ /**
242
+ * How one level's groups are ordered, and whether each starts a page.
243
+ * @param {object} level
244
+ * @param {number} n
245
+ * @returns {string} markup
246
+ */
247
+ function levelOptions(level, n) {
248
+ const sort = typeof level?.sort === 'string' ? level.sort : '';
249
+ const id = `dz-f-group-${n}`;
250
+
251
+ return `
252
+ <div class="dz-field dz-field-choice">
253
+ <label class="dz-label" id="${id}-sort-label" for="${id}-sort">Order groups</label>
254
+ ${choiceBox(`${id}-sort`, `data-group-option="sort" data-level="${n}"`, SORT_CHOICES, sort)}
255
+ </div>
256
+ <div class="dz-field dz-field-toggle">
257
+ <label for="${id}-page">New page for each group</label>
258
+ <input type="checkbox" id="${id}-page" data-group-option="newPage" data-level="${n}"
259
+ ${level?.newPage === true ? 'checked' : ''}>
260
+ <p class="dz-hint">Every group but the first starts at the top of a page</p>
261
+ </div>`;
48
262
  }
49
263
 
50
264
 
@@ -62,9 +276,7 @@ export function drawPanel(item) {
62
276
  */
63
277
  export function drawManyPanel(items) {
64
278
  return `
65
- <div class="dz-panel-head">
66
- <span class="dz-panel-type">${items.length} items</span>
67
- </div>
279
+ ${panelHead(`${items.length} items`)}
68
280
 
69
281
  <section class="dz-section">
70
282
  <ul class="dz-selected">
@@ -90,10 +302,7 @@ export function drawManyPanel(items) {
90
302
  */
91
303
  export function drawReportPanel(layout) {
92
304
  return `
93
- <div class="dz-panel-head">
94
- <span class="dz-panel-type">report</span>
95
- <span class="dz-panel-id">${esc(layout.name ?? 'Untitled')}</span>
96
- </div>
305
+ ${panelHead('report', layout.name ?? 'Untitled', icon('open'))}
97
306
  ${sections(layout, reportFields(layout))}
98
307
  ${bands(layout)}`;
99
308
  }
@@ -102,12 +311,62 @@ export function drawReportPanel(layout) {
102
311
  function sections(target, list) {
103
312
  return list.map(section => `
104
313
  <section class="dz-section">
105
- <h3>${esc(section.title)}</h3>
314
+ ${sectionHead(section.title)}
106
315
  ${section.fields.map(field => control(target, field)).join('')}
316
+ ${(section.actions ?? []).map(a => `
317
+ <button type="button" class="dz-add-row dz-section-action"
318
+ data-action="${esc(a.action)}" title="${esc(a.title ?? '')}">
319
+ ${esc(a.label)}</button>`).join('')}
107
320
  </section>`).join('');
108
321
  }
109
322
 
110
323
 
324
+ /**
325
+ * How a text item's values print: one line saying so, and the button that
326
+ * opens the Format Editor. The editor is a dialog rather than controls here -
327
+ * a currency alone is five choices, and the rail has only just got its room
328
+ * back.
329
+ *
330
+ * @param {object} owner a text item, or a column
331
+ * @param {object|null} layout for the report's defaults the summary shows through
332
+ * @param {string} [index] the column's place in its table, for a column
333
+ * @returns {string} markup
334
+ */
335
+ function formatButton(owner, layout, index) {
336
+ const summary = describeFormat(owner?.format, layout?.format);
337
+
338
+ return `
339
+ <button type="button" class="dz-format" data-action="edit-format"
340
+ ${index != null ? `data-index="${esc(index)}"` : ''}
341
+ title="${esc(summary)} - click to change how values print">
342
+ <span class="dz-format-summary">${esc(summary)}</span>
343
+ <span class="dz-format-edit">Edit</span>
344
+ </button>`;
345
+ }
346
+
347
+
348
+ /**
349
+ * The same summary-and-Edit button for highlight rules: how many there are,
350
+ * and the editor they are changed in.
351
+ *
352
+ * @param {object[]|null|undefined} rules
353
+ * @param {string} action the designer action that opens the editor
354
+ * @param {string} [index] a column's place, for a column's rules
355
+ * @returns {string} markup
356
+ */
357
+ function highlightButton(rules, action, index) {
358
+ const summary = describeHighlight(rules);
359
+
360
+ return `
361
+ <button type="button" class="dz-format" data-action="${esc(action)}"
362
+ ${index != null ? `data-index="${esc(index)}"` : ''}
363
+ title="${esc(summary)} - click to style values by what they are">
364
+ <span class="dz-format-summary">${esc(summary)}</span>
365
+ <span class="dz-format-edit">Edit</span>
366
+ </button>`;
367
+ }
368
+
369
+
111
370
  function control(target, field, extra = '') {
112
371
  const value = readField(target, field);
113
372
  const id = `dz-f-${field.key.replace(/\./g, '-')}${extra ? `-${extra}` : ''}`;
@@ -120,20 +379,43 @@ function control(target, field, extra = '') {
120
379
  * by hand is the one part of this format nobody should have to do twice -
121
380
  * and a placeholder is only a placeholder if both braces are there.
122
381
  */
382
+ /**
383
+ * A number with a short label carries it inside the box, and the label
384
+ * proper is kept for screen readers and the tests that read it. Any
385
+ * number's visible label - inside or above - is the handle that scrubs it.
386
+ */
387
+ const inside = field.type === 'number' && field.short;
388
+ const scrub = field.type === 'number' ? ' dz-scrub' : '';
389
+
123
390
  const label = field.type === 'textarea'
124
391
  ? `<span class="dz-field-top">
125
- <label for="${id}">${esc(field.label)}</label>
392
+ <label class="dz-label" for="${id}">${esc(field.label)}</label>
126
393
  <button type="button" class="dz-brace" data-action="insert-brace"
127
394
  data-for="${esc(field.key)}"
128
395
  title="Wrap the selected text in braces, or insert a pair"
129
396
  aria-label="Insert a placeholder">{&nbsp;}</button>
130
397
  </span>`
131
- : `<label for="${id}">${esc(field.label)}</label>`;
398
+ /**
399
+ * A row of glyph buttons is named by its radiogroup instead: a label
400
+ * for its first button would choose Left whenever "Align" was clicked.
401
+ */
402
+ : `<label class="dz-label${inside ? ' dz-sr' : scrub}" id="${id}-label"${field.segmented ? '' : ` for="${id}"`}
403
+ ${scrub ? 'title="Drag sideways to change"' : ''}>${esc(field.label)}</label>`;
404
+
405
+ const half = field.half || field.type === 'number' || field.type === 'color';
406
+
407
+ const box = inside
408
+ ? `<span class="dz-input">
409
+ <span class="dz-input-prefix dz-scrub" aria-hidden="true"
410
+ title="${esc(field.label)} - drag sideways to change">${esc(field.short)}</span>
411
+ ${input(id, bind, field, value)}
412
+ </span>`
413
+ : input(id, bind, field, value);
132
414
 
133
415
  return `
134
- <div class="dz-field dz-field-${field.type}">
416
+ <div class="dz-field dz-field-${field.type}${half ? ' is-half' : ''}">
135
417
  ${label}
136
- ${input(id, bind, field, value)}
418
+ ${box}
137
419
  ${field.hint ? `<p class="dz-hint">${esc(field.hint)}</p>` : ''}
138
420
  </div>`;
139
421
  }
@@ -158,18 +440,14 @@ function input(id, bind, field, value) {
158
440
  </div>`;
159
441
 
160
442
  case 'choice':
161
- return `<select id="${id}" ${bind}>${field.options.map(o =>
162
- `<option value="${esc(o.value)}"${o.value === value ? ' selected' : ''}
163
- >${esc(o.label)}</option>`).join('')}</select>`;
443
+ if (field.segmented) return segmented(id, bind, field, value);
444
+
445
+ return field.custom
446
+ ? customChoice(id, bind, field, value)
447
+ : choiceBox(id, bind, field.options, value);
164
448
 
165
449
  case 'color':
166
- /**
167
- * A colour input cannot hold null, and several style fields are
168
- * legitimately absent - so the swatch falls back to black and the
169
- * value is only written once someone actually picks one.
170
- */
171
- return `<input type="color" id="${id}" ${bind}
172
- value="${esc(value ?? '#000000')}">`;
450
+ return colorInput(id, bind, field, value);
173
451
 
174
452
  case 'toggle':
175
453
  return `<input type="checkbox" id="${id}" ${bind}
@@ -199,41 +477,268 @@ function input(id, bind, field, value) {
199
477
  return `<input type="text" id="${id}" ${bind}
200
478
  value="${esc(value ?? '')}" spellcheck="false">`;
201
479
 
480
+ /**
481
+ * A number can be shown and not be editable - a margin switched off
482
+ * shows what it will come back as. Disabled rather than hidden, so the
483
+ * rows beside it in the rail's grid stay where they are.
484
+ */
202
485
  default:
203
486
  return `<input type="number" id="${id}" ${bind}
204
487
  value="${esc(value ?? '')}"
205
488
  ${field.min != null ? `min="${field.min}"` : ''}
206
- ${field.max != null ? `max="${field.max}"` : ''} step="1">`;
489
+ ${field.max != null ? `max="${field.max}"` : ''} step="1"
490
+ ${field.disabled ? 'disabled' : ''}>`;
207
491
  }
208
492
  }
209
493
 
210
494
 
211
495
  /**
212
- * A table's columns, one card each. Four controls on one row would be 60px wide
213
- * apiece in a rail this narrow, so they stack and the card carries the number.
496
+ * A group band's background: the same swatch a colour field has - "none" with
497
+ * a slash through it until one is picked, and a button to take it off again.
214
498
  */
215
- function columns(table) {
216
- const list = table.columns || [];
499
+ function bandFill(type, band, label) {
500
+ const color = typeof band?.background === 'string' && band.background ? band.background : null;
501
+
502
+ return `
503
+ <span class="dz-swatch dz-band-fill${color ? '' : ' is-none'}"
504
+ title="${esc(label)} background">
505
+ <span class="dz-swatch-well">
506
+ <input type="color" data-band-fill="${esc(type)}" value="${esc(color ?? '#ffffff')}"
507
+ aria-label="${esc(label)} background">
508
+ </span>
509
+ <button type="button" class="dz-icon dz-swatch-clear" data-action="clear-band-fill"
510
+ data-fill-for="${esc(type)}" title="No background"
511
+ aria-label="Remove ${esc(label)} background">&times;</button>
512
+ </span>`;
513
+ }
514
+
515
+
516
+ /** what a choice's Custom… option says, which no real value can */
517
+ const CUSTOM_CHOICE = '__custom__';
518
+
519
+
520
+ /**
521
+ * A choice of the usual values, and Custom… for any other - the currency
522
+ * symbol, where ₹ and $ cover most reports and CHF still has to be possible.
523
+ *
524
+ * The select and the box are both bound to the field. The select writes what
525
+ * it is set to, except Custom…, which only shows the box; the box writes what
526
+ * is typed into it. A value that is none of the options shows as Custom…,
527
+ * with the box holding it.
528
+ */
529
+ function customChoice(id, bind, field, value) {
530
+ const listed = field.options.some(o => o.value === (value ?? ''));
531
+ const selected = listed ? (value ?? '') : CUSTOM_CHOICE;
532
+
533
+ return `
534
+ <span class="dz-choice-custom">
535
+ ${choiceBox(id, bind, [...field.options, { value: CUSTOM_CHOICE, label: 'Custom…' }], selected)}
536
+ <input type="text" ${bind} data-custom="true" value="${esc(listed ? '' : value ?? '')}"
537
+ spellcheck="false" maxlength="6" aria-label="${esc(field.label)}, custom"
538
+ placeholder="such as CHF"${listed ? ' hidden' : ''}>
539
+ </span>`;
540
+ }
541
+
542
+
543
+ /**
544
+ * A short choice as a row of glyph buttons - text alignment, which way a line
545
+ * runs, which way up the page is - as a design tool draws them.
546
+ *
547
+ * Radio buttons under the glyphs rather than buttons that act: a radio is
548
+ * already a choice of one, reports through the same change event a select
549
+ * does, and brings the arrow keys with it. The first carries the id, so the
550
+ * field's label still names the group.
551
+ *
552
+ * @returns {string} markup
553
+ */
554
+ function segmented(id, bind, field, value) {
555
+ const name = id;
217
556
 
218
557
  return `
558
+ <span class="dz-segmented" role="radiogroup" aria-label="${esc(field.label)}">
559
+ ${field.options.map((o, n) => `
560
+ <label class="dz-seg" title="${esc(o.label)}">
561
+ <input type="radio" ${n === 0 ? `id="${id}"` : ''} name="${esc(name)}" ${bind}
562
+ value="${esc(o.value)}"${o.value === value ? ' checked' : ''}
563
+ aria-label="${esc(o.label)}">
564
+ <span class="dz-seg-glyph">${icon(o.icon) || esc(o.label)}</span>
565
+ </label>`).join('')}
566
+ </span>`;
567
+ }
568
+
569
+
570
+ /**
571
+ * A colour, or none.
572
+ *
573
+ * A colour input cannot hold null, and several style fields are legitimately
574
+ * absent - a box with no fill, text in the stylesheet's colour. Drawn as the
575
+ * input's black, an absent colour looked like a chosen one. So the input sits
576
+ * under a "none" swatch while there is no colour, and still opens the picker
577
+ * when clicked; the value is only written once someone actually picks one.
578
+ * A chosen colour gets a button that takes it off again.
579
+ *
580
+ * Whether it is none is a class on the wrapper, which the input and syncPanel
581
+ * keep up to date - rebuilding the rail while the picker is open would close it.
582
+ */
583
+ function colorInput(id, bind, field, value) {
584
+ const none = value == null || value === '';
585
+
586
+ /**
587
+ * Beside the chip, the colour as hex - which is also a box it can be typed
588
+ * into, as a fill is in a design tool. It is not bound to the field itself:
589
+ * half a hex code is not a colour, so it writes through the picker once
590
+ * what is typed is one. See onHex.
591
+ */
592
+ return `
593
+ <span class="dz-swatch dz-input${none ? ' is-none' : ''}">
594
+ <span class="dz-swatch-well">
595
+ <input type="color" id="${id}" ${bind} value="${esc(none ? '#000000' : value)}">
596
+ </span>
597
+ <input type="text" class="dz-hex" data-hex="${esc(field.key)}"
598
+ value="${esc(none ? '' : hexText(value))}" placeholder="None"
599
+ maxlength="7" spellcheck="false" autocomplete="off"
600
+ aria-label="${esc(field.label)}, as hex">
601
+ <button type="button" class="dz-icon dz-swatch-clear" data-action="clear-color"
602
+ data-for="${esc(field.key)}"
603
+ title="No colour" aria-label="Remove ${esc(field.label)} colour">&times;</button>
604
+ </span>`;
605
+ }
606
+
607
+
608
+ /** a colour as the hex box shows it: no hash, upper case */
609
+ function hexText(value) {
610
+ const text = String(value ?? '');
611
+ return text.startsWith('#') ? text.slice(1).toUpperCase() : text;
612
+ }
613
+
614
+
615
+ /**
616
+ * What was typed into a hex box, as the picker's #rrggbb - or null while it is
617
+ * not a colour yet. Three digits only count once the box is left, since "ff9"
618
+ * is as likely to be the start of "ff9c4b" as it is #ffff99.
619
+ *
620
+ * @param {string} text
621
+ * @param {boolean} final whether the box has been left or Enter pressed
622
+ * @returns {string|null}
623
+ */
624
+ export function parseHex(text, final = false) {
625
+ const bare = String(text ?? '').trim().replace(/^#/, '').toLowerCase();
626
+
627
+ if (/^[0-9a-f]{6}$/.test(bare)) return `#${bare}`;
628
+ if (final && /^[0-9a-f]{3}$/.test(bare)) return `#${[...bare].map(c => c + c).join('')}`;
629
+
630
+ return null;
631
+ }
632
+
633
+
634
+ /** a column's closed row, kept saying what the column now is */
635
+ function summarise(node, column) {
636
+ if (!node) return;
637
+
638
+ const name = node.querySelector('.dz-column-name');
639
+ const width = node.querySelector('.dz-column-width');
640
+
641
+ if (name) name.textContent = column.label || column.field || '';
642
+ if (width) width.textContent = String(column.width ?? '');
643
+ }
644
+
645
+
646
+ /** shows a colour control as none, or as its colour */
647
+ function markSwatch(node, none) {
648
+ node.closest?.('.dz-swatch')?.classList.toggle('is-none', none);
649
+ }
650
+
651
+
652
+ /**
653
+ * Puts the colour a control now holds into its hex box - unless that box is
654
+ * the one being typed in, which would move the caret under the typist.
655
+ */
656
+ function showHex(node, value) {
657
+ const hex = node.closest?.('.dz-swatch')?.querySelector('.dz-hex');
658
+ if (!hex || hex === hex.ownerDocument.activeElement) return;
659
+
660
+ hex.value = value == null || value === '' ? '' : hexText(value);
661
+ }
662
+
663
+
664
+ /**
665
+ * Which columns are open, by the column object itself.
666
+ *
667
+ * A table of ten columns, all open, is a rail ten screens long - so a column
668
+ * is a row that opens, as a layer is. The rail is rebuilt often (a column
669
+ * added, the selection changed and changed back), and a column somebody had
670
+ * open should still be open when it is drawn again, so the state is kept here
671
+ * rather than in the markup.
672
+ *
673
+ * Weak, and keyed by object, so nothing has to be cleaned up: an undo hands
674
+ * the designer a fresh copy of the layout, whose columns simply start closed.
675
+ */
676
+ const openColumns = new WeakMap();
677
+
678
+ /** tables whose columns have been drawn at least once */
679
+ const drawnTables = new WeakSet();
680
+
681
+
682
+ /**
683
+ * Whether a column is drawn open. One that has been opened or closed keeps
684
+ * that. One never seen before is closed - unless its table has been drawn
685
+ * already, in which case it has just been added, and is opened to be filled in.
686
+ */
687
+ function isOpen(table, column) {
688
+ if (openColumns.has(column)) return openColumns.get(column);
689
+
690
+ const open = drawnTables.has(table);
691
+ openColumns.set(column, open);
692
+ return open;
693
+ }
694
+
695
+
696
+ /**
697
+ * A table's columns, a row each that opens onto the rail's two-column grid -
698
+ * field and label side by side, then width and alignment. Closed, the row
699
+ * still says which column it is, what it shows and how wide it is. Adding one
700
+ * is the + on the section's title, where a design tool puts it.
701
+ */
702
+ function columns(table, layout) {
703
+ const list = table.columns || [];
704
+
705
+ const add = `
706
+ <button type="button" class="dz-icon" data-action="add-column"
707
+ title="Add column" aria-label="Add column">${PLUS}</button>`;
708
+
709
+ const markup = `
219
710
  <section class="dz-section dz-columns">
220
- <h3>Columns</h3>
711
+ ${sectionHead('Columns', add)}
221
712
  ${list.map((column, index) => `
222
- <div class="dz-column">
223
- <div class="dz-column-head">
224
- <span>${index + 1}</span>
713
+ <details class="dz-column" data-index="${index}"${isOpen(table, column) ? ' open' : ''}>
714
+ <summary class="dz-column-head">
715
+ <span class="dz-column-caret" aria-hidden="true"></span>
716
+ <span class="dz-column-number">${index + 1}</span>
717
+ <span class="dz-column-name">${esc(column.label || column.field || '')}</span>
718
+ <span class="dz-column-width">${esc(column.width ?? '')}</span>
225
719
  ${list.length > 1 ? `
226
720
  <button type="button" class="dz-icon"
227
721
  data-action="remove-column" data-index="${index}"
228
722
  title="Remove this column"
229
723
  aria-label="Remove column ${index + 1}">&times;</button>` : ''}
230
- </div>
724
+ </summary>
725
+ <div class="dz-column-body">
231
726
  ${COLUMN_FIELDS.map(field =>
232
727
  control(column, field, String(index))).join('')}
233
- </div>`).join('')}
234
- <button type="button" class="dz-add-row" data-action="add-column">
235
- Add column</button>
728
+ <div class="dz-field dz-field-format is-half">
729
+ <label class="dz-label">Format</label>
730
+ ${formatButton(column, layout, String(index))}
731
+ </div>
732
+ <div class="dz-field dz-field-format is-half">
733
+ <label class="dz-label">Highlight</label>
734
+ ${highlightButton(column.highlight, 'edit-highlight', String(index))}
735
+ </div>
736
+ </div>
737
+ </details>`).join('')}
236
738
  </section>`;
739
+
740
+ drawnTables.add(table);
741
+ return markup;
237
742
  }
238
743
 
239
744
 
@@ -242,37 +747,73 @@ function columns(table) {
242
747
  *
243
748
  * A checkbox each, in page order, rather than a list that can be reordered -
244
749
  * validate.js allows one band of each type and regions.js decides where each
245
- * sits, so there is no order here for anyone to choose.
750
+ * sits, so there is no order here for anyone to choose. Each grouped table's
751
+ * own header and footer are listed with the report's, named for their table.
246
752
  */
247
753
  function bands(layout) {
248
754
  return `
249
755
  <section class="dz-section dz-bands">
250
- <h3>Bands</h3>
251
- ${BAND_TYPES.map(type => {
252
- const band = findBand(layout, type);
253
- const on = hasBand(layout, type);
254
-
255
- return `
256
- <div class="dz-band-row${on ? ' is-on' : ''}">
257
- <label class="dz-band-name">
258
- <input type="checkbox" data-band="${esc(type)}" ${on ? 'checked' : ''}>
259
- <span>${esc(type)}</span>
260
- </label>
261
- ${on && type !== 'detail'
262
- ? `<input type="text" class="dz-band-height"
263
- data-band-height="${esc(type)}"
264
- value="${esc(band.height ?? '')}"
265
- placeholder="auto" spellcheck="false"
266
- aria-label="${esc(type)} height">`
267
- : `<span class="dz-band-height-none">${
268
- type === 'detail' ? 'what is left' : ''}</span>`}
269
- <p class="dz-hint">${esc(BAND_NOTES[type] ?? '')}</p>
270
- </div>`;
271
- }).join('')}
756
+ ${sectionHead('Bands')}
757
+ ${railBands(layout).map(type => bandRow(layout, type)).join('')}
272
758
  </section>`;
273
759
  }
274
760
 
275
761
 
762
+ /** what a band row says under its name */
763
+ function bandNote(type) {
764
+ const parsed = parseBandKey(type);
765
+ if (!parsed) return BAND_NOTES[type] ?? '';
766
+
767
+ const whose = parsed.level > 0
768
+ ? `level ${parsed.level + 1} group of ${parsed.table}`
769
+ : `group of ${parsed.table}`;
770
+
771
+ return parsed.kind === 'groupHeader' ? `Before each ${whose}` : `After each ${whose}`;
772
+ }
773
+
774
+
775
+ /**
776
+ * One band's switch and height, by key.
777
+ *
778
+ * The report's band list names a table's group band in full - whose it is and
779
+ * which level - because it sits among every other band. Inside the table's own
780
+ * grouping section both are already said by where it is, so that section
781
+ * passes a shorter name and note of its own.
782
+ *
783
+ * @param {object} layout
784
+ * @param {string} type a band type, or a table's group band key
785
+ * @param {{label?: string, note?: string}} [names]
786
+ * @returns {string} markup
787
+ */
788
+ function bandRow(layout, type, names = {}) {
789
+ const band = findBand(layout, type);
790
+ const on = hasBand(layout, type);
791
+ const label = names.label ?? bandLabel(type);
792
+ const note = names.note ?? bandNote(type);
793
+
794
+ /** a table's group band, switched on, can be shaded */
795
+ const fillable = on && parseBandKey(type) != null;
796
+
797
+ return `
798
+ <div class="dz-band-row${on ? ' is-on' : ''}${fillable ? ' has-fill' : ''}">
799
+ <label class="dz-band-name">
800
+ <input type="checkbox" data-band="${esc(type)}" ${on ? 'checked' : ''}>
801
+ <span>${esc(label)}</span>
802
+ </label>
803
+ ${fillable ? bandFill(type, band, label) : ''}
804
+ ${on && type !== 'detail'
805
+ ? `<input type="text" class="dz-band-height"
806
+ data-band-height="${esc(type)}"
807
+ value="${esc(band.height ?? '')}"
808
+ placeholder="auto" spellcheck="false"
809
+ aria-label="${esc(label)} height">`
810
+ : `<span class="dz-band-height-none">${
811
+ type === 'detail' ? 'what is left' : ''}</span>`}
812
+ ${note ? `<p class="dz-hint">${esc(note)}</p>` : ''}
813
+ </div>`;
814
+ }
815
+
816
+
276
817
  /**
277
818
  * Wires one delegated listener set onto the rail. The rail element survives a
278
819
  * redraw of its contents, so these are attached once and never rebound.
@@ -289,10 +830,12 @@ function bands(layout) {
289
830
  * @param {(action: string, data: DOMStringMap) => void} options.act a button was pressed
290
831
  * @param {(type: string, on: boolean) => void} options.toggleBand
291
832
  * @param {(type: string, height: string) => void} options.setBandHeight
833
+ * @param {(type: string, color: string|null) => void} [options.setBandFill] a
834
+ * group band's background, null for none
292
835
  * @returns {() => void} detaches
293
836
  */
294
837
  export function attachPanel({
295
- panel, getTarget, getItem, changed, act, toggleBand, setBandHeight
838
+ panel, getTarget, getItem, changed, act, toggleBand, setBandHeight, setBandFill = () => { }
296
839
  }) {
297
840
  /** keeps the marking layer under the text it belongs to */
298
841
  function repaint(node) {
@@ -303,12 +846,65 @@ export function attachPanel({
303
846
  }
304
847
 
305
848
 
849
+ /**
850
+ * A hex box writes through the picker beside it, once what is typed is a
851
+ * colour - so a colour typed and a colour picked are the same edit, and
852
+ * the field, the swatch and the history all hear about it one way.
853
+ */
854
+ function onHex(node, final) {
855
+ const hex = parseHex(node.value, final);
856
+ if (!hex) return;
857
+
858
+ const picker = node.closest('.dz-swatch')?.querySelector('input[type="color"]');
859
+ if (!picker) return;
860
+
861
+ const none = node.closest('.dz-swatch').classList.contains('is-none');
862
+ if (picker.value === hex && !none) return;
863
+
864
+ picker.value = hex;
865
+ picker.dispatchEvent(new Event('input', { bubbles: true }));
866
+ }
867
+
868
+
306
869
  function onInput(event) {
307
- const node = event.target.closest?.('[data-field], [data-band], [data-band-height]');
870
+ if (event.target.dataset?.hex != null) {
871
+ onHex(event.target, event.type === 'change');
872
+ return;
873
+ }
874
+
875
+ const node = event.target.closest?.(
876
+ '[data-field], [data-band], [data-band-height], [data-band-fill], [data-group-level], [data-group-option]');
308
877
  if (!node) return;
309
878
 
310
879
  if (node.tagName === 'TEXTAREA') repaint(node);
311
880
 
881
+ /** a choice's button shows what its select holds, however it was set */
882
+ if (node.tagName === 'SELECT') showChoice(node);
883
+
884
+ /** how one level's groups are ordered and placed, on the selected table */
885
+ if (node.dataset.groupOption != null) {
886
+ const item = getItem();
887
+ const level = Number(node.dataset.level);
888
+ const option = node.dataset.groupOption;
889
+ const value = node.type === 'checkbox' ? node.checked : node.value;
890
+
891
+ if (item?.type === 'table' && setGroupOption(item, level, option, value)) {
892
+ changed(`group-option:${level}:${option}`);
893
+ }
894
+ return;
895
+ }
896
+
897
+ /** an inner level's field, on the selected table */
898
+ if (node.dataset.groupLevel != null) {
899
+ const item = getItem();
900
+ const level = Number(node.dataset.groupLevel);
901
+
902
+ if (item?.type === 'table' && setGroupBy(item, node.value.trim(), level)) {
903
+ changed(`group-level:${level}`);
904
+ }
905
+ return;
906
+ }
907
+
312
908
  if (node.dataset.band != null) {
313
909
  toggleBand(node.dataset.band, node.checked);
314
910
  return;
@@ -319,6 +915,13 @@ export function attachPanel({
319
915
  return;
320
916
  }
321
917
 
918
+ /** a colour picked is no longer none */
919
+ if (node.dataset.bandFill != null) {
920
+ markSwatch(node, false);
921
+ setBandFill(node.dataset.bandFill, node.value);
922
+ return;
923
+ }
924
+
322
925
  /** a column control is bound to the column, not to the item */
323
926
  if (node.dataset.column != null) {
324
927
  const item = getItem();
@@ -327,6 +930,7 @@ export function attachPanel({
327
930
 
328
931
  const field = COLUMN_FIELDS.find(f => f.key === node.dataset.field);
329
932
  if (field && writeField(column, field, node.value)) {
933
+ summarise(node.closest('.dz-column'), column);
330
934
  changed(`column:${node.dataset.column}:${field.key}`);
331
935
  }
332
936
  return;
@@ -338,15 +942,43 @@ export function attachPanel({
338
942
  const field = bound.fields.find(f => f.key === node.dataset.field);
339
943
  if (!field) return;
340
944
 
945
+ if (node.tagName === 'SELECT' && node.value === CUSTOM_CHOICE) {
946
+ const box = node.closest('.dz-choice-custom')?.querySelector('[data-custom]');
947
+ if (box) {
948
+ box.hidden = false;
949
+ box.focus();
950
+ }
951
+ return;
952
+ }
953
+
341
954
  const raw = field.type === 'toggle' ? node.checked : node.value;
342
955
 
956
+ /** a colour picked is no longer none, even one picked as black */
957
+ if (field.type === 'color') {
958
+ markSwatch(node, false);
959
+ showHex(node, node.value);
960
+ }
961
+
962
+ /**
963
+ * A field may say which reading of its object decides the rail's
964
+ * shape, rather than that every edit to it does - *Group by* changes
965
+ * the controls only when it turns grouping on or off, not on every
966
+ * letter of the field name.
967
+ */
968
+ const shape = typeof field.rebuilds === 'function' ? field.rebuilds : null;
969
+ const before = shape ? shape(bound.target) : null;
970
+
343
971
  if (writeField(bound.target, field, raw)) {
344
972
  /**
345
973
  * Some fields change which fields there are - choosing a named
346
974
  * sheet takes the width and height boxes away, and Custom brings
347
975
  * them back. The caller is told, because it owns the rail.
348
976
  */
349
- changed(`field:${field.key}`, { rebuild: Boolean(field.rebuilds) });
977
+ const rebuild = shape
978
+ ? shape(bound.target) !== before
979
+ : Boolean(field.rebuilds);
980
+
981
+ changed(`field:${field.key}`, { rebuild });
350
982
  }
351
983
  }
352
984
 
@@ -366,9 +998,134 @@ export function attachPanel({
366
998
  return;
367
999
  }
368
1000
 
1001
+ /** a group band's background, back to none */
1002
+ if (node.dataset.action === 'clear-band-fill') {
1003
+ markSwatch(node, true);
1004
+ setBandFill(node.dataset.fillFor, null);
1005
+ return;
1006
+ }
1007
+
1008
+ /** handled here too: it is bound to a field, as the colour beside it is */
1009
+ if (node.dataset.action === 'clear-color') {
1010
+ const bound = getTarget();
1011
+ const field = bound?.fields.find(f => f.key === node.dataset.for);
1012
+
1013
+ if (field && clearField(bound.target, field)) {
1014
+ markSwatch(node, true);
1015
+ showHex(node, null);
1016
+ changed(`field:${field.key}`);
1017
+ }
1018
+ return;
1019
+ }
1020
+
369
1021
  act(node.dataset.action, node.dataset);
370
1022
  }
371
1023
 
1024
+ /**
1025
+ * A column opened or closed stays that way through the next rebuild.
1026
+ * `toggle` does not bubble, so this listens on the way down.
1027
+ */
1028
+ function onToggle(event) {
1029
+ const node = event.target;
1030
+ if (!node.classList?.contains('dz-column')) return;
1031
+
1032
+ const column = getItem()?.columns?.[Number(node.dataset.index)];
1033
+ if (column) openColumns.set(column, node.open);
1034
+ }
1035
+
1036
+ /**
1037
+ * A hex box left holding something that is not a colour goes back to the
1038
+ * colour the field has - as a number box does to its last good value.
1039
+ */
1040
+ function onLeave(event) {
1041
+ const node = event.target;
1042
+ if (node.dataset?.hex == null) return;
1043
+
1044
+ const swatch = node.closest('.dz-swatch');
1045
+ const picker = swatch?.querySelector('input[type="color"]');
1046
+ if (!picker) return;
1047
+
1048
+ node.value = swatch.classList.contains('is-none') ? '' : hexText(picker.value);
1049
+ }
1050
+
1051
+ /** Enter in a hex box settles it, three-digit shorthand included */
1052
+ function onKey(event) {
1053
+ if (event.key !== 'Enter' || event.target.dataset?.hex == null) return;
1054
+
1055
+ event.preventDefault();
1056
+ onHex(event.target, true);
1057
+ event.target.blur();
1058
+ }
1059
+
1060
+ /**
1061
+ * Scrubbing: drag a number's label sideways to change it, a unit for every
1062
+ * two pixels and ten with Shift held - Figma's gesture.
1063
+ *
1064
+ * Each step is written into the box and announced as ordinary input, so
1065
+ * the write path, the clamping and the history's merging of one control's
1066
+ * edits into one step all apply without a second route in. A press that
1067
+ * never moves is a click, and puts the caret in the box as a label does.
1068
+ */
1069
+ function onPress(event) {
1070
+ const handle = event.target.closest?.('.dz-scrub');
1071
+ if (!handle || event.button !== 0) return;
1072
+
1073
+ const box = handle.closest('.dz-field')?.querySelector('input[type="number"]');
1074
+ if (!box || box.disabled) return;
1075
+
1076
+ event.preventDefault();
1077
+
1078
+ const startX = event.clientX;
1079
+ const start = Number(box.value) || 0;
1080
+ const min = box.min !== '' ? Number(box.min) : -Infinity;
1081
+ const max = box.max !== '' ? Number(box.max) : Infinity;
1082
+ let moved = false;
1083
+
1084
+ handle.setPointerCapture?.(event.pointerId);
1085
+
1086
+ function move(e) {
1087
+ const dx = e.clientX - startX;
1088
+ if (!moved && Math.abs(dx) < 3) return;
1089
+
1090
+ if (!moved) {
1091
+ moved = true;
1092
+ panel.classList.add('is-scrubbing');
1093
+ }
1094
+
1095
+ const step = e.shiftKey ? 10 : 1;
1096
+ const next = Math.min(max, Math.max(min, start + Math.round(dx / 2) * step));
1097
+
1098
+ if (String(next) === box.value) return;
1099
+
1100
+ box.value = String(next);
1101
+ box.dispatchEvent(new Event('input', { bubbles: true }));
1102
+ }
1103
+
1104
+ function end() {
1105
+ handle.removeEventListener('pointermove', move);
1106
+ handle.removeEventListener('pointerup', end);
1107
+ handle.removeEventListener('pointercancel', end);
1108
+ panel.classList.remove('is-scrubbing');
1109
+
1110
+ if (!moved) {
1111
+ box.focus();
1112
+ box.select?.();
1113
+ return;
1114
+ }
1115
+
1116
+ /**
1117
+ * The label is a <label>, whose click would put the caret in the
1118
+ * box at the end of a drag. One click is swallowed, and only the
1119
+ * one that ends this drag.
1120
+ */
1121
+ handle.addEventListener('click', (e) => e.preventDefault(), { once: true, capture: true });
1122
+ }
1123
+
1124
+ handle.addEventListener('pointermove', move);
1125
+ handle.addEventListener('pointerup', end);
1126
+ handle.addEventListener('pointercancel', end);
1127
+ }
1128
+
372
1129
  /**
373
1130
  * A long value scrolls inside the box, and the layer behind it must scroll
374
1131
  * by the same amount or the marks part company with the characters.
@@ -386,12 +1143,24 @@ export function attachPanel({
386
1143
  /** a <select> and a colour picker report on change, not input */
387
1144
  panel.addEventListener('change', onInput);
388
1145
  panel.addEventListener('click', onClick);
1146
+ panel.addEventListener('focusout', onLeave);
1147
+ panel.addEventListener('keydown', onKey);
1148
+ panel.addEventListener('pointerdown', onPress);
1149
+ panel.addEventListener('toggle', onToggle, true);
1150
+
1151
+ /** the choices open the shared list rather than the operating system's */
1152
+ const detachChoices = attachChoices(panel);
389
1153
 
390
1154
  return function detach() {
1155
+ detachChoices();
1156
+ panel.removeEventListener('toggle', onToggle, true);
391
1157
  panel.removeEventListener('scroll', onScroll, true);
392
1158
  panel.removeEventListener('input', onInput);
393
1159
  panel.removeEventListener('change', onInput);
394
1160
  panel.removeEventListener('click', onClick);
1161
+ panel.removeEventListener('focusout', onLeave);
1162
+ panel.removeEventListener('keydown', onKey);
1163
+ panel.removeEventListener('pointerdown', onPress);
395
1164
  };
396
1165
  }
397
1166
 
@@ -412,6 +1181,12 @@ export function syncPanel(panel, item, layout) {
412
1181
  const target = item ?? layout;
413
1182
  if (!target) return;
414
1183
 
1184
+ /** a closed column's row says its name and width, which an undo can change */
1185
+ for (const node of panel.querySelectorAll('.dz-column')) {
1186
+ const column = item?.columns?.[Number(node.dataset.index)];
1187
+ if (column) summarise(node, column);
1188
+ }
1189
+
415
1190
  const fields = item ? allFields(item) : allReportFields(layout);
416
1191
 
417
1192
  for (const node of panel.querySelectorAll('[data-field]')) {
@@ -431,6 +1206,31 @@ export function syncPanel(panel, item, layout) {
431
1206
 
432
1207
  const value = readField(source, field);
433
1208
 
1209
+ /** an undo can take a colour off, or put one back */
1210
+ if (field.type === 'color') {
1211
+ markSwatch(node, value == null || value === '');
1212
+ showHex(node, value);
1213
+ }
1214
+
1215
+ /** a row of glyph buttons: each one only says whether it is the one */
1216
+ if (node.type === 'radio') {
1217
+ node.checked = node.value === String(value ?? '');
1218
+ continue;
1219
+ }
1220
+
1221
+ /** a choice with Custom…: the select says which, the box holds any other */
1222
+ if (field.custom) {
1223
+ const listed = field.options.some(o => o.value === (value ?? ''));
1224
+
1225
+ if (node.dataset.custom) {
1226
+ node.value = listed ? '' : String(value ?? '');
1227
+ node.hidden = listed;
1228
+ } else {
1229
+ node.value = listed ? String(value ?? '') : CUSTOM_CHOICE;
1230
+ }
1231
+ continue;
1232
+ }
1233
+
434
1234
  if (field.type === 'toggle') {
435
1235
  node.checked = Boolean(value);
436
1236
  } else if (value != null) {
@@ -444,6 +1244,9 @@ export function syncPanel(panel, item, layout) {
444
1244
  }
445
1245
  }
446
1246
  }
1247
+
1248
+ /** every choice's button says what its select now holds */
1249
+ refreshChoices(panel);
447
1250
  }
448
1251
 
449
1252