report-studio 0.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +363 -4
- package/dist/designer-qy59eEux.js +3416 -0
- package/dist/designer.js +2 -0
- package/dist/index.js +2 -4
- package/dist/react-designer.js +41 -0
- package/dist/react.js +58 -2
- package/dist/viewer-xa17tyMJ.js +1721 -0
- package/package.json +65 -18
- package/src/asset/images/icon.png +0 -0
- package/src/asset/images/logo.png +0 -0
- package/src/cli/index.js +282 -0
- package/src/cli/serve.js +135 -0
- package/src/designer/blank.js +53 -0
- package/src/designer/canvas.js +518 -0
- package/src/designer/designer.js +2054 -0
- package/src/designer/dialog.js +708 -0
- package/src/designer/fields.js +633 -0
- package/src/designer/geometry.js +154 -0
- package/src/designer/guides.js +370 -0
- package/src/designer/history.js +113 -0
- package/src/designer/icons.js +123 -0
- package/src/designer/index.html +28 -0
- package/src/designer/main.js +39 -0
- package/src/designer/menu.js +126 -0
- package/src/designer/panel.js +498 -0
- package/src/designer/preview.js +95 -0
- package/src/designer/rulers.js +231 -0
- package/src/designer/sample-data.js +411 -0
- package/src/designer/select.js +699 -0
- package/src/designer/structure.js +685 -0
- package/src/designer/styles.css +1654 -0
- package/src/designer/toast.js +62 -0
- package/src/designer/tokens.js +293 -0
- package/src/engine/csv.js +350 -0
- package/src/engine/group.js +367 -0
- package/src/engine/index.js +35 -0
- package/src/engine/measure.js +391 -0
- package/src/engine/paginate.js +865 -0
- package/src/engine/regions.js +175 -0
- package/src/engine/resolve.js +208 -0
- package/src/engine/search.js +129 -0
- package/src/engine/tables.js +87 -0
- package/src/engine/text-metrics.js +206 -0
- package/src/engine/validate.js +403 -0
- package/src/index.html +79 -0
- package/src/index.js +51 -0
- package/src/preview/chrome.css +505 -0
- package/src/preview/chrome.js +147 -0
- package/src/preview/index.html +28 -0
- package/src/preview/main.js +66 -0
- package/src/preview/styles.css +16 -0
- package/src/preview/viewer.js +763 -0
- package/src/react/designer.js +87 -0
- package/src/react/index.js +117 -0
- package/src/render/items.js +518 -0
- package/src/render/render.js +98 -0
- package/src/render/report.css +232 -0
- package/src/server/routes.js +331 -0
- package/src/shared/brand.css +29 -0
- package/src/shared/dropdown.css +136 -0
- package/src/shared/dropdown.js +166 -0
- package/src/shared/modal.css +125 -0
- package/src/shared/prefix.js +13 -0
- package/src/shared/store.js +145 -0
- package/src/shared/theme.css +72 -0
- package/src/shared/window.js +138 -0
- package/src/vite/index.js +53 -0
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* panel.js draws the properties rail and reports what was done to it.
|
|
3
|
+
*
|
|
4
|
+
* Like select.js, it decides nothing: which fields exist, what a value coerces
|
|
5
|
+
* to, and what adding a band or a column means are all fields.js and
|
|
6
|
+
* structure.js. This turns descriptors into controls and controls back into
|
|
7
|
+
* calls.
|
|
8
|
+
*
|
|
9
|
+
* The rail shows one of two things. With an item selected, that item's
|
|
10
|
+
* properties. With nothing selected, the report's own - name, page, margins and
|
|
11
|
+
* which bands exist - because those have to be editable somewhere and "nothing
|
|
12
|
+
* selected" is otherwise a wasted screen.
|
|
13
|
+
*
|
|
14
|
+
* It is redrawn when the *selection* changes and never while someone is typing
|
|
15
|
+
* in it: rebuilding an input under a cursor takes the focus and the caret with
|
|
16
|
+
* it, and typing "120" into a width becomes "1". Values that changed elsewhere,
|
|
17
|
+
* such as x and y during a drag, are synced onto the existing controls instead.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import {
|
|
21
|
+
fieldsFor, allFields, reportFields, allReportFields,
|
|
22
|
+
COLUMN_FIELDS, readField, writeField
|
|
23
|
+
} from './fields.js';
|
|
24
|
+
import { BAND_TYPES, BAND_NOTES, hasBand, findBand } from './structure.js';
|
|
25
|
+
import { esc, imageSrc } from '../render/items.js';
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The rail describes what a thing is; what you can do to it lives in the pill
|
|
30
|
+
* that opens on a right-click, beside the item rather than at the edge of the
|
|
31
|
+
* screen. See menu.js.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The rail for a selected item.
|
|
37
|
+
* @param {object} item
|
|
38
|
+
* @returns {string} markup
|
|
39
|
+
*/
|
|
40
|
+
export function drawPanel(item) {
|
|
41
|
+
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>
|
|
46
|
+
${sections(item, fieldsFor(item))}
|
|
47
|
+
${item.type === 'table' ? columns(item) : ''}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The rail with several items selected.
|
|
53
|
+
*
|
|
54
|
+
* No property controls: writing one width onto six items is a different edit
|
|
55
|
+
* from the one the controls describe, and offering a box that means "all of
|
|
56
|
+
* them" where it has always meant "this one" is how a designer loses somebody's
|
|
57
|
+
* layout. What a group genuinely shares is that it can be moved and deleted,
|
|
58
|
+
* so that is what it offers.
|
|
59
|
+
*
|
|
60
|
+
* @param {object[]} items
|
|
61
|
+
* @returns {string} markup
|
|
62
|
+
*/
|
|
63
|
+
export function drawManyPanel(items) {
|
|
64
|
+
return `
|
|
65
|
+
<div class="dz-panel-head">
|
|
66
|
+
<span class="dz-panel-type">${items.length} items</span>
|
|
67
|
+
</div>
|
|
68
|
+
|
|
69
|
+
<section class="dz-section">
|
|
70
|
+
<ul class="dz-selected">
|
|
71
|
+
${items.map(item => `
|
|
72
|
+
<li>
|
|
73
|
+
<span class="dz-selected-type">${esc(item.type)}</span>
|
|
74
|
+
<span class="dz-selected-id">${esc(item.id)}</span>
|
|
75
|
+
</li>`).join('')}
|
|
76
|
+
</ul>
|
|
77
|
+
|
|
78
|
+
<p class="dz-hint">
|
|
79
|
+
Drag to move them together, or use the arrow keys.
|
|
80
|
+
Select one on its own to change its properties.
|
|
81
|
+
</p>
|
|
82
|
+
</section>`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The rail with nothing selected: the report itself.
|
|
88
|
+
* @param {object} layout
|
|
89
|
+
* @returns {string} markup
|
|
90
|
+
*/
|
|
91
|
+
export function drawReportPanel(layout) {
|
|
92
|
+
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>
|
|
97
|
+
${sections(layout, reportFields(layout))}
|
|
98
|
+
${bands(layout)}`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
function sections(target, list) {
|
|
103
|
+
return list.map(section => `
|
|
104
|
+
<section class="dz-section">
|
|
105
|
+
<h3>${esc(section.title)}</h3>
|
|
106
|
+
${section.fields.map(field => control(target, field)).join('')}
|
|
107
|
+
</section>`).join('');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
function control(target, field, extra = '') {
|
|
112
|
+
const value = readField(target, field);
|
|
113
|
+
const id = `dz-f-${field.key.replace(/\./g, '-')}${extra ? `-${extra}` : ''}`;
|
|
114
|
+
const bind = extra
|
|
115
|
+
? `data-field="${esc(field.key)}" data-column="${esc(extra)}"`
|
|
116
|
+
: `data-field="${esc(field.key)}"`;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The value box gets a button that writes the braces for you. Typing them
|
|
120
|
+
* by hand is the one part of this format nobody should have to do twice -
|
|
121
|
+
* and a placeholder is only a placeholder if both braces are there.
|
|
122
|
+
*/
|
|
123
|
+
const label = field.type === 'textarea'
|
|
124
|
+
? `<span class="dz-field-top">
|
|
125
|
+
<label for="${id}">${esc(field.label)}</label>
|
|
126
|
+
<button type="button" class="dz-brace" data-action="insert-brace"
|
|
127
|
+
data-for="${esc(field.key)}"
|
|
128
|
+
title="Wrap the selected text in braces, or insert a pair"
|
|
129
|
+
aria-label="Insert a placeholder">{ }</button>
|
|
130
|
+
</span>`
|
|
131
|
+
: `<label for="${id}">${esc(field.label)}</label>`;
|
|
132
|
+
|
|
133
|
+
return `
|
|
134
|
+
<div class="dz-field dz-field-${field.type}">
|
|
135
|
+
${label}
|
|
136
|
+
${input(id, bind, field, value)}
|
|
137
|
+
${field.hint ? `<p class="dz-hint">${esc(field.hint)}</p>` : ''}
|
|
138
|
+
</div>`;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
function input(id, bind, field, value) {
|
|
143
|
+
switch (field.type) {
|
|
144
|
+
case 'textarea':
|
|
145
|
+
/**
|
|
146
|
+
* A textarea cannot style its own contents, so the placeholders are
|
|
147
|
+
* marked on a layer behind it and the box itself is given
|
|
148
|
+
* transparent text and a visible caret. The two are laid out
|
|
149
|
+
* identically - same font, padding and wrapping - so the marks sit
|
|
150
|
+
* exactly under the characters they belong to.
|
|
151
|
+
*/
|
|
152
|
+
return `
|
|
153
|
+
<div class="dz-value">
|
|
154
|
+
<div class="dz-value-marks" data-role="marks" aria-hidden="true"
|
|
155
|
+
>${markPlaceholders(value ?? '')}</div>
|
|
156
|
+
<textarea id="${id}" ${bind} rows="2"
|
|
157
|
+
spellcheck="false">${esc(value ?? '')}</textarea>
|
|
158
|
+
</div>`;
|
|
159
|
+
|
|
160
|
+
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>`;
|
|
164
|
+
|
|
165
|
+
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')}">`;
|
|
173
|
+
|
|
174
|
+
case 'toggle':
|
|
175
|
+
return `<input type="checkbox" id="${id}" ${bind}
|
|
176
|
+
${value ? 'checked' : ''}>`;
|
|
177
|
+
|
|
178
|
+
case 'image':
|
|
179
|
+
/**
|
|
180
|
+
* Buttons rather than a bound input: a picture arrives through a
|
|
181
|
+
* file dialog, which the designer opens, and the rail is rebuilt
|
|
182
|
+
* once it lands. No data-field, so syncPanel has nothing here to
|
|
183
|
+
* write a data: URI into.
|
|
184
|
+
*/
|
|
185
|
+
return `
|
|
186
|
+
<div class="dz-image" id="${id}">
|
|
187
|
+
${value
|
|
188
|
+
? `<img class="dz-image-thumb" src="${esc(imageSrc(value))}" alt="">`
|
|
189
|
+
: '<span class="dz-image-none">No image stored</span>'}
|
|
190
|
+
<div class="dz-image-actions">
|
|
191
|
+
<button type="button" class="dz-add-row"
|
|
192
|
+
data-action="upload-image">${value ? 'Replace' : 'Upload'}</button>
|
|
193
|
+
${value ? `<button type="button" class="dz-add-row"
|
|
194
|
+
data-action="clear-image">Remove</button>` : ''}
|
|
195
|
+
</div>
|
|
196
|
+
</div>`;
|
|
197
|
+
|
|
198
|
+
case 'text':
|
|
199
|
+
return `<input type="text" id="${id}" ${bind}
|
|
200
|
+
value="${esc(value ?? '')}" spellcheck="false">`;
|
|
201
|
+
|
|
202
|
+
default:
|
|
203
|
+
return `<input type="number" id="${id}" ${bind}
|
|
204
|
+
value="${esc(value ?? '')}"
|
|
205
|
+
${field.min != null ? `min="${field.min}"` : ''}
|
|
206
|
+
${field.max != null ? `max="${field.max}"` : ''} step="1">`;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
/**
|
|
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.
|
|
214
|
+
*/
|
|
215
|
+
function columns(table) {
|
|
216
|
+
const list = table.columns || [];
|
|
217
|
+
|
|
218
|
+
return `
|
|
219
|
+
<section class="dz-section dz-columns">
|
|
220
|
+
<h3>Columns</h3>
|
|
221
|
+
${list.map((column, index) => `
|
|
222
|
+
<div class="dz-column">
|
|
223
|
+
<div class="dz-column-head">
|
|
224
|
+
<span>${index + 1}</span>
|
|
225
|
+
${list.length > 1 ? `
|
|
226
|
+
<button type="button" class="dz-icon"
|
|
227
|
+
data-action="remove-column" data-index="${index}"
|
|
228
|
+
title="Remove this column"
|
|
229
|
+
aria-label="Remove column ${index + 1}">×</button>` : ''}
|
|
230
|
+
</div>
|
|
231
|
+
${COLUMN_FIELDS.map(field =>
|
|
232
|
+
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>
|
|
236
|
+
</section>`;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Which bands the report has.
|
|
242
|
+
*
|
|
243
|
+
* A checkbox each, in page order, rather than a list that can be reordered -
|
|
244
|
+
* 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.
|
|
246
|
+
*/
|
|
247
|
+
function bands(layout) {
|
|
248
|
+
return `
|
|
249
|
+
<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('')}
|
|
272
|
+
</section>`;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Wires one delegated listener set onto the rail. The rail element survives a
|
|
278
|
+
* redraw of its contents, so these are attached once and never rebound.
|
|
279
|
+
*
|
|
280
|
+
* @param {object} options
|
|
281
|
+
* @param {Element} options.panel
|
|
282
|
+
* @param {() => {target: object, fields: object[]}|null} options.getTarget what the
|
|
283
|
+
* plain fields are bound to - the selected item, or the layout
|
|
284
|
+
* @param {() => object|null} options.getItem the selected item, for column edits
|
|
285
|
+
* @param {(key: string, options?: {rebuild?: boolean}) => void} options.changed
|
|
286
|
+
* called when an edit lands, with a key naming the control - consecutive edits
|
|
287
|
+
* from the same one are one step. `rebuild` says the rail's controls have to
|
|
288
|
+
* be built again, because which ones there are has changed
|
|
289
|
+
* @param {(action: string, data: DOMStringMap) => void} options.act a button was pressed
|
|
290
|
+
* @param {(type: string, on: boolean) => void} options.toggleBand
|
|
291
|
+
* @param {(type: string, height: string) => void} options.setBandHeight
|
|
292
|
+
* @returns {() => void} detaches
|
|
293
|
+
*/
|
|
294
|
+
export function attachPanel({
|
|
295
|
+
panel, getTarget, getItem, changed, act, toggleBand, setBandHeight
|
|
296
|
+
}) {
|
|
297
|
+
/** keeps the marking layer under the text it belongs to */
|
|
298
|
+
function repaint(node) {
|
|
299
|
+
const box = node.closest?.('.dz-value');
|
|
300
|
+
const marks = box?.querySelector('[data-role="marks"]');
|
|
301
|
+
|
|
302
|
+
if (marks) marks.innerHTML = markPlaceholders(node.value);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
function onInput(event) {
|
|
307
|
+
const node = event.target.closest?.('[data-field], [data-band], [data-band-height]');
|
|
308
|
+
if (!node) return;
|
|
309
|
+
|
|
310
|
+
if (node.tagName === 'TEXTAREA') repaint(node);
|
|
311
|
+
|
|
312
|
+
if (node.dataset.band != null) {
|
|
313
|
+
toggleBand(node.dataset.band, node.checked);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (node.dataset.bandHeight != null) {
|
|
318
|
+
setBandHeight(node.dataset.bandHeight, node.value);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** a column control is bound to the column, not to the item */
|
|
323
|
+
if (node.dataset.column != null) {
|
|
324
|
+
const item = getItem();
|
|
325
|
+
const column = item?.columns?.[Number(node.dataset.column)];
|
|
326
|
+
if (!column) return;
|
|
327
|
+
|
|
328
|
+
const field = COLUMN_FIELDS.find(f => f.key === node.dataset.field);
|
|
329
|
+
if (field && writeField(column, field, node.value)) {
|
|
330
|
+
changed(`column:${node.dataset.column}:${field.key}`);
|
|
331
|
+
}
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const bound = getTarget();
|
|
336
|
+
if (!bound) return;
|
|
337
|
+
|
|
338
|
+
const field = bound.fields.find(f => f.key === node.dataset.field);
|
|
339
|
+
if (!field) return;
|
|
340
|
+
|
|
341
|
+
const raw = field.type === 'toggle' ? node.checked : node.value;
|
|
342
|
+
|
|
343
|
+
if (writeField(bound.target, field, raw)) {
|
|
344
|
+
/**
|
|
345
|
+
* Some fields change which fields there are - choosing a named
|
|
346
|
+
* sheet takes the width and height boxes away, and Custom brings
|
|
347
|
+
* them back. The caller is told, because it owns the rail.
|
|
348
|
+
*/
|
|
349
|
+
changed(`field:${field.key}`, { rebuild: Boolean(field.rebuilds) });
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function onClick(event) {
|
|
354
|
+
const node = event.target.closest?.('[data-action]');
|
|
355
|
+
if (!node) return;
|
|
356
|
+
|
|
357
|
+
event.preventDefault();
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Handled here rather than passed out: it needs the caret, which is a
|
|
361
|
+
* fact about this textarea and about nothing else in the designer.
|
|
362
|
+
*/
|
|
363
|
+
if (node.dataset.action === 'insert-brace') {
|
|
364
|
+
const box = node.closest('.dz-field')?.querySelector('textarea');
|
|
365
|
+
if (box) insertBraces(box);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
act(node.dataset.action, node.dataset);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* A long value scrolls inside the box, and the layer behind it must scroll
|
|
374
|
+
* by the same amount or the marks part company with the characters.
|
|
375
|
+
*/
|
|
376
|
+
function onScroll(event) {
|
|
377
|
+
const node = event.target;
|
|
378
|
+
if (node.tagName !== 'TEXTAREA') return;
|
|
379
|
+
|
|
380
|
+
const marks = node.closest('.dz-value')?.querySelector('[data-role="marks"]');
|
|
381
|
+
if (marks) marks.scrollTop = node.scrollTop;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
panel.addEventListener('scroll', onScroll, true);
|
|
385
|
+
panel.addEventListener('input', onInput);
|
|
386
|
+
/** a <select> and a colour picker report on change, not input */
|
|
387
|
+
panel.addEventListener('change', onInput);
|
|
388
|
+
panel.addEventListener('click', onClick);
|
|
389
|
+
|
|
390
|
+
return function detach() {
|
|
391
|
+
panel.removeEventListener('scroll', onScroll, true);
|
|
392
|
+
panel.removeEventListener('input', onInput);
|
|
393
|
+
panel.removeEventListener('change', onInput);
|
|
394
|
+
panel.removeEventListener('click', onClick);
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Refreshes the controls from their object without rebuilding them - for values
|
|
401
|
+
* changed somewhere else, such as x and y after a drag.
|
|
402
|
+
*
|
|
403
|
+
* The focused control is skipped: writing to an input while it has the caret
|
|
404
|
+
* moves the caret to the end, so a drag that happened to run while a field was
|
|
405
|
+
* focused would reorder the digits in it.
|
|
406
|
+
*
|
|
407
|
+
* @param {Element} panel
|
|
408
|
+
* @param {object|null} item the selected item, or null for the report rail
|
|
409
|
+
* @param {object} layout
|
|
410
|
+
*/
|
|
411
|
+
export function syncPanel(panel, item, layout) {
|
|
412
|
+
const target = item ?? layout;
|
|
413
|
+
if (!target) return;
|
|
414
|
+
|
|
415
|
+
const fields = item ? allFields(item) : allReportFields(layout);
|
|
416
|
+
|
|
417
|
+
for (const node of panel.querySelectorAll('[data-field]')) {
|
|
418
|
+
/** the panel's own document: a designer in its own window has another */
|
|
419
|
+
if (node === node.ownerDocument.activeElement) continue;
|
|
420
|
+
|
|
421
|
+
const isColumn = node.dataset.column != null;
|
|
422
|
+
const source = isColumn
|
|
423
|
+
? item?.columns?.[Number(node.dataset.column)]
|
|
424
|
+
: target;
|
|
425
|
+
|
|
426
|
+
if (!source) continue;
|
|
427
|
+
|
|
428
|
+
const list = isColumn ? COLUMN_FIELDS : fields;
|
|
429
|
+
const field = list.find(f => f.key === node.dataset.field);
|
|
430
|
+
if (!field) continue;
|
|
431
|
+
|
|
432
|
+
const value = readField(source, field);
|
|
433
|
+
|
|
434
|
+
if (field.type === 'toggle') {
|
|
435
|
+
node.checked = Boolean(value);
|
|
436
|
+
} else if (value != null) {
|
|
437
|
+
node.value = String(value);
|
|
438
|
+
|
|
439
|
+
if (node.tagName === 'TEXTAREA') {
|
|
440
|
+
const marks = node.closest('.dz-value')
|
|
441
|
+
?.querySelector('[data-role="marks"]');
|
|
442
|
+
|
|
443
|
+
if (marks) marks.innerHTML = markPlaceholders(node.value);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* The value, with every `{placeholder}` wrapped so it can be coloured.
|
|
452
|
+
*
|
|
453
|
+
* Escaped first and marked second: `esc` leaves braces alone, so the pattern
|
|
454
|
+
* still matches, and nothing the user typed can become markup on the way.
|
|
455
|
+
*
|
|
456
|
+
* @param {string} text
|
|
457
|
+
* @returns {string} markup for the layer behind the textarea
|
|
458
|
+
*/
|
|
459
|
+
export function markPlaceholders(text) {
|
|
460
|
+
const safe = esc(String(text ?? ''));
|
|
461
|
+
|
|
462
|
+
const marked = safe.replace(/\{[^{}]*\}/g,
|
|
463
|
+
match => `<mark class="dz-mark">${match}</mark>`);
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* A textarea keeps a line for a trailing newline and a div does not, so
|
|
467
|
+
* without this the layer comes up short and the marks drift off the text.
|
|
468
|
+
*/
|
|
469
|
+
return `${marked}\n`;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Wraps the selection in braces, or inserts an empty pair.
|
|
475
|
+
*
|
|
476
|
+
* With text selected it becomes `{that text}` - which is how someone turns a
|
|
477
|
+
* word they have already typed into a field without retyping it. With nothing
|
|
478
|
+
* selected the caret lands between the braces, ready for the field name.
|
|
479
|
+
*
|
|
480
|
+
* @param {HTMLTextAreaElement} field
|
|
481
|
+
*/
|
|
482
|
+
export function insertBraces(field) {
|
|
483
|
+
const start = field.selectionStart ?? field.value.length;
|
|
484
|
+
const end = field.selectionEnd ?? start;
|
|
485
|
+
|
|
486
|
+
const inner = field.value.slice(start, end);
|
|
487
|
+
const before = field.value.slice(0, start);
|
|
488
|
+
const after = field.value.slice(end);
|
|
489
|
+
|
|
490
|
+
field.value = `${before}{${inner}}${after}`;
|
|
491
|
+
|
|
492
|
+
const caret = start + 1 + inner.length;
|
|
493
|
+
field.focus();
|
|
494
|
+
field.setSelectionRange(caret, caret);
|
|
495
|
+
|
|
496
|
+
/** through the ordinary write path, so the layout and the layer both follow */
|
|
497
|
+
field.dispatchEvent(new Event('input', { bubbles: true }));
|
|
498
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* preview.js runs the report the designer is holding and draws the result.
|
|
3
|
+
*
|
|
4
|
+
* This is the moment the whole seam pays off (spec 2.2): the canvas has been
|
|
5
|
+
* drawing bands with items.js all along, and the preview draws pages with
|
|
6
|
+
* render.js, which draws its bands with items.js. So switching between them is
|
|
7
|
+
* a different *wrapper* around the same drawing - the design cannot look one
|
|
8
|
+
* way here and another way on paper, because there is only one way.
|
|
9
|
+
*
|
|
10
|
+
* What it adds over the canvas is everything the canvas cannot know: how many
|
|
11
|
+
* pages there are, where the breaks fall, what the aggregates come to, and
|
|
12
|
+
* whether a band that fitted at design time still fits once real rows arrive.
|
|
13
|
+
*
|
|
14
|
+
* It is a check, not the preview screen. Zoom, search, print and CSV live there
|
|
15
|
+
* (src/preview/), and the designer links to it rather than growing a second
|
|
16
|
+
* copy of a toolbar that already exists.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { buildPages } from '../engine/index.js';
|
|
20
|
+
import { render } from '../render/render.js';
|
|
21
|
+
import { ReportError } from '../engine/validate.js';
|
|
22
|
+
import { esc } from '../render/items.js';
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A ceiling, because real data has no reason to be small.
|
|
27
|
+
*
|
|
28
|
+
* Pagination itself is cheap and runs in full - the page *count* is one of the
|
|
29
|
+
* things worth previewing - but mounting five thousand pages of DOM to glance
|
|
30
|
+
* at a header is not. Spec 9 asks the real preview screen to mount only what is
|
|
31
|
+
* near the viewport; here the honest thing is to draw a bounded number and say
|
|
32
|
+
* so, rather than draw fewer and let it look like the report ends early.
|
|
33
|
+
*/
|
|
34
|
+
export const PAGE_LIMIT = 12;
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @param {object} layout
|
|
39
|
+
* @param {object} data
|
|
40
|
+
* @param {object} [options]
|
|
41
|
+
* @param {number} [options.limit]
|
|
42
|
+
* @returns {{markup: string, pageCount: number, error: string|null}}
|
|
43
|
+
*/
|
|
44
|
+
export function drawPreview(layout, data, { limit = PAGE_LIMIT } = {}) {
|
|
45
|
+
let paginated;
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
paginated = buildPages(layout, data ?? {});
|
|
49
|
+
} catch (error) {
|
|
50
|
+
return {
|
|
51
|
+
markup: problem(error),
|
|
52
|
+
pageCount: 0,
|
|
53
|
+
error: error?.message ?? String(error)
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const pageCount = paginated.pages.length;
|
|
58
|
+
const shown = pageCount > limit
|
|
59
|
+
? { ...paginated, pages: paginated.pages.slice(0, limit) }
|
|
60
|
+
: paginated;
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
markup: `
|
|
64
|
+
${render(shown)}
|
|
65
|
+
${pageCount > limit ? truncated(pageCount, limit) : ''}`,
|
|
66
|
+
pageCount,
|
|
67
|
+
error: null
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* A layout the engine refuses is reported in its own words. ReportError already
|
|
74
|
+
* names the band and the field (spec 9.1), so there is nothing to translate.
|
|
75
|
+
*/
|
|
76
|
+
function problem(error) {
|
|
77
|
+
const issues = error instanceof ReportError && error.issues?.length
|
|
78
|
+
? error.issues
|
|
79
|
+
: [error?.message ?? String(error)];
|
|
80
|
+
|
|
81
|
+
return `
|
|
82
|
+
<div class="dz-problems" role="alert">
|
|
83
|
+
<h2>This report will not run yet</h2>
|
|
84
|
+
<ul>${issues.map(i => `<li>${esc(i)}</li>`).join('')}</ul>
|
|
85
|
+
</div>`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
function truncated(pageCount, limit) {
|
|
90
|
+
return `
|
|
91
|
+
<p class="dz-truncated">
|
|
92
|
+
Showing the first ${limit} of ${esc(pageCount)} pages.
|
|
93
|
+
Open the preview screen to read them all.
|
|
94
|
+
</p>`;
|
|
95
|
+
}
|