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,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* text-metrics.js measures text the way the browser will actually draw it.
|
|
3
|
+
*
|
|
4
|
+
* Spec 4.3: text height comes from canvas 2D measureText, wrapping is greedy,
|
|
5
|
+
* and measurements are cached by text + font because the same strings recur
|
|
6
|
+
* constantly across a report.
|
|
7
|
+
*
|
|
8
|
+
* Outside a browser (tests, SSR) there is no canvas, so a documented
|
|
9
|
+
* approximation stands in. The browser path is the authoritative one.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
/** CSS line-height multiplier - preview/styles.css draws text at this ratio */
|
|
14
|
+
export const LINE_HEIGHT_RATIO = 1.3;
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
/** measureText results, keyed by `${font}\u0000${text}` */
|
|
18
|
+
const cache = new Map();
|
|
19
|
+
|
|
20
|
+
/** guards against a cache that grows without bound on very large reports */
|
|
21
|
+
const CACHE_LIMIT = 10000;
|
|
22
|
+
|
|
23
|
+
let ctx;
|
|
24
|
+
let ctxResolved = false;
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* One offscreen 2D context for the life of the module, or null when there is
|
|
29
|
+
* no browser to ask.
|
|
30
|
+
* @returns {CanvasRenderingContext2D|null}
|
|
31
|
+
*/
|
|
32
|
+
function context() {
|
|
33
|
+
if (ctxResolved) return ctx;
|
|
34
|
+
ctxResolved = true;
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
if (typeof OffscreenCanvas !== 'undefined') {
|
|
38
|
+
ctx = new OffscreenCanvas(1, 1).getContext('2d');
|
|
39
|
+
} else if (typeof document !== 'undefined') {
|
|
40
|
+
ctx = document.createElement('canvas').getContext('2d');
|
|
41
|
+
} else {
|
|
42
|
+
ctx = null;
|
|
43
|
+
}
|
|
44
|
+
} catch {
|
|
45
|
+
ctx = null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return ctx;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Builds the CSS font shorthand measureText expects.
|
|
54
|
+
* @param {object} style an item's style block
|
|
55
|
+
* @returns {string}
|
|
56
|
+
*/
|
|
57
|
+
export function fontString(style = {}) {
|
|
58
|
+
const fontStyle = style.fontStyle || 'normal';
|
|
59
|
+
const fontWeight = style.fontWeight || 'normal';
|
|
60
|
+
const fontSize = style.fontSize ?? 12;
|
|
61
|
+
const fontFamily = style.fontFamily || 'Helvetica, Arial, sans-serif';
|
|
62
|
+
return `${fontStyle} ${fontWeight} ${fontSize}px ${fontFamily}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Width of a single line, in pixels.
|
|
68
|
+
* @param {string} text
|
|
69
|
+
* @param {object} style
|
|
70
|
+
* @returns {number}
|
|
71
|
+
*/
|
|
72
|
+
export function measureText(text, style) {
|
|
73
|
+
const str = String(text ?? '');
|
|
74
|
+
if (str === '') return 0;
|
|
75
|
+
|
|
76
|
+
const font = fontString(style);
|
|
77
|
+
const key = `${font}\u0000${str}`;
|
|
78
|
+
|
|
79
|
+
const hit = cache.get(key);
|
|
80
|
+
if (hit !== undefined) return hit;
|
|
81
|
+
|
|
82
|
+
const c = context();
|
|
83
|
+
let width;
|
|
84
|
+
|
|
85
|
+
if (c) {
|
|
86
|
+
c.font = font;
|
|
87
|
+
width = c.measureText(str).width;
|
|
88
|
+
} else {
|
|
89
|
+
width = approximateWidth(str, style);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (cache.size >= CACHE_LIMIT) cache.clear();
|
|
93
|
+
cache.set(key, width);
|
|
94
|
+
|
|
95
|
+
return width;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Fallback used only where no canvas exists. Character widths are expressed as
|
|
101
|
+
* a fraction of the font size, coarse-grained by character class. It is close
|
|
102
|
+
* enough to keep pagination sane in tests; it is not a substitute for the real
|
|
103
|
+
* measurement a browser gives.
|
|
104
|
+
* @param {string} str
|
|
105
|
+
* @param {object} style
|
|
106
|
+
* @returns {number}
|
|
107
|
+
*/
|
|
108
|
+
function approximateWidth(str, style = {}) {
|
|
109
|
+
const size = style.fontSize ?? 12;
|
|
110
|
+
const bold = String(style.fontWeight) === 'bold' || Number(style.fontWeight) >= 600;
|
|
111
|
+
const mono = /mono|courier|consolas/i.test(style.fontFamily || '');
|
|
112
|
+
|
|
113
|
+
let units = 0;
|
|
114
|
+
|
|
115
|
+
for (const ch of str) {
|
|
116
|
+
if (mono) { units += 0.6; continue; }
|
|
117
|
+
if (ch === ' ') units += 0.26;
|
|
118
|
+
else if (/[iljI.,:;'`|!\[\]()]/.test(ch)) units += 0.28;
|
|
119
|
+
else if (/[ftr]/.test(ch)) units += 0.34;
|
|
120
|
+
else if (/[A-Z]/.test(ch)) units += 0.68;
|
|
121
|
+
else if (/[mwMW@]/.test(ch)) units += 0.88;
|
|
122
|
+
else if (/[0-9]/.test(ch)) units += 0.56;
|
|
123
|
+
else units += 0.52;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return units * size * (bold ? 1.06 : 1);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Greedy word wrap: add words until the line would exceed {width}, then break.
|
|
132
|
+
* A single word longer than the line is broken character by character rather
|
|
133
|
+
* than allowed to overflow.
|
|
134
|
+
* @param {string} text
|
|
135
|
+
* @param {number} width available width in pixels
|
|
136
|
+
* @param {object} style
|
|
137
|
+
* @returns {string[]} the laid-out lines
|
|
138
|
+
*/
|
|
139
|
+
export function wrapLines(text, width, style) {
|
|
140
|
+
const source = String(text ?? '');
|
|
141
|
+
|
|
142
|
+
/** an author's explicit newlines are hard breaks */
|
|
143
|
+
const paragraphs = source.split('\n');
|
|
144
|
+
|
|
145
|
+
if (!(width > 0)) return paragraphs;
|
|
146
|
+
|
|
147
|
+
const lines = [];
|
|
148
|
+
|
|
149
|
+
for (const paragraph of paragraphs) {
|
|
150
|
+
const words = paragraph.split(/(\s+)/).filter(w => w !== '');
|
|
151
|
+
|
|
152
|
+
if (words.length === 0) {
|
|
153
|
+
lines.push('');
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
let line = '';
|
|
158
|
+
|
|
159
|
+
for (const word of words) {
|
|
160
|
+
const candidate = line + word;
|
|
161
|
+
|
|
162
|
+
if (line !== '' && measureText(candidate, style) > width) {
|
|
163
|
+
lines.push(line.trimEnd());
|
|
164
|
+
line = word.trimStart();
|
|
165
|
+
} else {
|
|
166
|
+
line = candidate;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* whatever now sits on the line may be a single word wider than the
|
|
171
|
+
* item. Break it rather than let it overflow the box silently.
|
|
172
|
+
*/
|
|
173
|
+
while (measureText(line, style) > width && line.trim().length > 1) {
|
|
174
|
+
let cut = line.length;
|
|
175
|
+
while (cut > 1 && measureText(line.slice(0, cut), style) > width) cut--;
|
|
176
|
+
if (cut >= line.length) break;
|
|
177
|
+
lines.push(line.slice(0, cut));
|
|
178
|
+
line = line.slice(cut);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
lines.push(line.trimEnd());
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return lines;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Rendered height of a text item: however many lines it wraps to, at the
|
|
191
|
+
* line height the preview draws with.
|
|
192
|
+
* @param {string} text
|
|
193
|
+
* @param {number} width available width in pixels
|
|
194
|
+
* @param {object} style
|
|
195
|
+
* @returns {number}
|
|
196
|
+
*/
|
|
197
|
+
export function textHeight(text, width, style) {
|
|
198
|
+
const lineHeight = Math.ceil((style?.fontSize ?? 12) * LINE_HEIGHT_RATIO);
|
|
199
|
+
return wrapLines(text, width, style).length * lineHeight;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
/** exposed for tests - measurements are cached for the life of the module */
|
|
204
|
+
export function clearMetricsCache() {
|
|
205
|
+
cache.clear();
|
|
206
|
+
}
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* validate.js checks a layout file before the engine walks it.
|
|
3
|
+
*
|
|
4
|
+
* Spec 6.1 chose a hand-written validator over a schema library specifically to
|
|
5
|
+
* get errors that name the band and item at fault. A malformed layout should
|
|
6
|
+
* say so here, not surface as a TypeError three stages downstream.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
import { tablesOf, hasOwnGrouping } from './tables.js';
|
|
11
|
+
import { designHeight } from './measure.js';
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
const BAND_TYPES = [
|
|
15
|
+
'reportHeader',
|
|
16
|
+
'pageHeader',
|
|
17
|
+
'groupHeader',
|
|
18
|
+
'detail',
|
|
19
|
+
'groupFooter',
|
|
20
|
+
'reportFooter',
|
|
21
|
+
'pageFooter'
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
const ITEM_TYPES = ['text', 'table', 'line', 'box', 'image'];
|
|
25
|
+
|
|
26
|
+
/** spec 3.3: a line runs along one axis; which one is the item's to say */
|
|
27
|
+
const LINE_ORIENTATIONS = ['horizontal', 'vertical'];
|
|
28
|
+
|
|
29
|
+
/** the four CSS border styles that read as a rule rather than a box */
|
|
30
|
+
const LINE_STYLES = ['solid', 'dashed', 'dotted', 'double'];
|
|
31
|
+
|
|
32
|
+
/** a table's grid may also be turned off, which a line cannot be */
|
|
33
|
+
const RULE_STYLES = [...LINE_STYLES, 'none'];
|
|
34
|
+
|
|
35
|
+
/** how a picture is fitted to its box: the three object-fit values that mean something here */
|
|
36
|
+
export const IMAGE_FITS = ['contain', 'cover', 'fill'];
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
/** Thrown for a layout the engine cannot work with. */
|
|
40
|
+
export class ReportError extends Error {
|
|
41
|
+
constructor(message, issues = []) {
|
|
42
|
+
super(issues.length ? `${message}\n - ${issues.join('\n - ')}` : message);
|
|
43
|
+
this.name = 'ReportError';
|
|
44
|
+
this.issues = issues;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Collects everything wrong with a layout, rather than stopping at the first
|
|
51
|
+
* problem - a designer fixing five typos wants all five named at once.
|
|
52
|
+
* @param {*} json
|
|
53
|
+
* @returns {string[]} human-readable issues, empty when the layout is usable
|
|
54
|
+
*/
|
|
55
|
+
export function validateLayout(json) {
|
|
56
|
+
const issues = [];
|
|
57
|
+
|
|
58
|
+
if (json == null || typeof json !== 'object' || Array.isArray(json)) {
|
|
59
|
+
return ['layout must be an object'];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
validatePage(json.page, issues);
|
|
63
|
+
|
|
64
|
+
if (!Array.isArray(json.bands)) {
|
|
65
|
+
issues.push('layout.bands must be an array');
|
|
66
|
+
return issues;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (json.bands.length === 0) {
|
|
70
|
+
issues.push('layout.bands is empty - there is nothing to render');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (json.groupBy != null && typeof json.groupBy !== 'string') {
|
|
74
|
+
issues.push('layout.groupBy must be a field name or null');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const seenTypes = new Set();
|
|
78
|
+
|
|
79
|
+
json.bands.forEach((band, i) => {
|
|
80
|
+
const where = `bands[${i}]`;
|
|
81
|
+
|
|
82
|
+
if (band == null || typeof band !== 'object') {
|
|
83
|
+
issues.push(`${where} must be an object`);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (!BAND_TYPES.includes(band.type)) {
|
|
88
|
+
issues.push(`${where}.type "${band.type}" is not one of: ${BAND_TYPES.join(', ')}`);
|
|
89
|
+
} else if (seenTypes.has(band.type)) {
|
|
90
|
+
issues.push(`${where}.type "${band.type}" appears more than once; the engine places one band of each type`);
|
|
91
|
+
} else {
|
|
92
|
+
seenTypes.add(band.type);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (!Array.isArray(band.items)) {
|
|
96
|
+
issues.push(`${where}.items must be an array`);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
band.items.forEach((item, j) =>
|
|
101
|
+
validateItem(item, `${where}.items[${j}]`, issues));
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
if (json.groupBy != null && !seenTypes.has('groupHeader') && !seenTypes.has('groupFooter')) {
|
|
105
|
+
issues.push('layout.groupBy is set but there is no groupHeader or groupFooter band to show it');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
validateTables(json, json.bands, issues);
|
|
109
|
+
|
|
110
|
+
return issues;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* What the tables of a report may be, together.
|
|
116
|
+
*
|
|
117
|
+
* Any number of them, each bound to its own dataset - but two things they may
|
|
118
|
+
* not do, because the engine has no honest way to print them:
|
|
119
|
+
*
|
|
120
|
+
* Two grouped tables. groupHeader and groupFooter are the report's bands, so
|
|
121
|
+
* both tables would draw their groups with the same heading - and a subtotal
|
|
122
|
+
* in it would be summed over whichever table happened to be drawn.
|
|
123
|
+
*
|
|
124
|
+
* Two tables side by side. Each table splits across pages on its own, so the
|
|
125
|
+
* one on the left would run to page three before the one on the right began,
|
|
126
|
+
* and began on page three. Tables in one band are stacked: each starts below
|
|
127
|
+
* where the one above it was designed to end, and the engine moves it down
|
|
128
|
+
* by however far the rows above really ran.
|
|
129
|
+
*
|
|
130
|
+
* @param {object} json the layout
|
|
131
|
+
* @param {object[]} bands
|
|
132
|
+
* @param {string[]} issues
|
|
133
|
+
*/
|
|
134
|
+
function validateTables(json, bands, issues) {
|
|
135
|
+
const tables = tablesOf(json);
|
|
136
|
+
|
|
137
|
+
for (const table of tables) {
|
|
138
|
+
if (table.groupBy != null && typeof table.groupBy !== 'string') {
|
|
139
|
+
issues.push(`table "${table.id}".groupBy must be a field name or null`);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (table.dataset != null && typeof table.dataset !== 'string') {
|
|
143
|
+
issues.push(`table "${table.id}".dataset must be a dataset name or null`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const grouped = tables.filter(hasOwnGrouping);
|
|
148
|
+
|
|
149
|
+
if (grouped.length > 1) {
|
|
150
|
+
issues.push(
|
|
151
|
+
`${grouped.length} tables are grouped (${grouped.map(t => `"${t.id}"`).join(', ')}); ` +
|
|
152
|
+
`a report groups one, because groupHeader and groupFooter are shared`);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (grouped.length > 0 && json.groupBy == null
|
|
156
|
+
&& !bands.some(b => b?.type === 'groupHeader' || b?.type === 'groupFooter')) {
|
|
157
|
+
issues.push(
|
|
158
|
+
`table "${grouped[0].id}" is grouped by "${grouped[0].groupBy}" but there is ` +
|
|
159
|
+
`no groupHeader or groupFooter band to show it`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
for (const band of bands) {
|
|
163
|
+
if (!Array.isArray(band?.items)) continue;
|
|
164
|
+
|
|
165
|
+
const stack = band.items
|
|
166
|
+
.filter(item => item?.type === 'table')
|
|
167
|
+
.map(item => ({ item, top: Number(item.y) || 0, bottom: (Number(item.y) || 0) + designHeight(item) }))
|
|
168
|
+
.sort((a, b) => a.top - b.top);
|
|
169
|
+
|
|
170
|
+
for (let i = 1; i < stack.length; i++) {
|
|
171
|
+
const above = stack[i - 1];
|
|
172
|
+
const below = stack[i];
|
|
173
|
+
|
|
174
|
+
if (below.top < above.bottom) {
|
|
175
|
+
issues.push(
|
|
176
|
+
`tables "${above.item.id}" and "${below.item.id}" overlap in the ${band.type} band; ` +
|
|
177
|
+
`move "${below.item.id}" to y ${Math.ceil(above.bottom)} or lower - tables are ` +
|
|
178
|
+
`stacked, one under another, not placed side by side`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
function validatePage(page, issues) {
|
|
186
|
+
if (page == null || typeof page !== 'object') {
|
|
187
|
+
issues.push('layout.page must be an object with width, height and margin');
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
for (const key of ['width', 'height']) {
|
|
192
|
+
if (!(typeof page[key] === 'number' && page[key] > 0)) {
|
|
193
|
+
issues.push(`layout.page.${key} must be a positive number`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (page.margin == null || typeof page.margin !== 'object') {
|
|
198
|
+
issues.push('layout.page.margin must be an object with top, right, bottom and left');
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
for (const side of ['top', 'right', 'bottom', 'left']) {
|
|
203
|
+
if (!(typeof page.margin[side] === 'number' && page.margin[side] >= 0)) {
|
|
204
|
+
issues.push(`layout.page.margin.${side} must be a number of pixels`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const printable = page.height - (page.margin.top + page.margin.bottom);
|
|
209
|
+
if (printable <= 0) {
|
|
210
|
+
issues.push(`layout.page margins leave no printable height (${printable}px)`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
function validateItem(item, where, issues) {
|
|
216
|
+
if (item == null || typeof item !== 'object') {
|
|
217
|
+
issues.push(`${where} must be an object`);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (!ITEM_TYPES.includes(item.type)) {
|
|
222
|
+
issues.push(`${where}.type "${item.type}" is not one of: ${ITEM_TYPES.join(', ')}`);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (typeof item.id !== 'string' || item.id === '') {
|
|
227
|
+
issues.push(`${where}.id must be a non-empty string`);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (item.type === 'text') {
|
|
231
|
+
if (typeof item.value !== 'string') {
|
|
232
|
+
issues.push(`${where}.value must be a string`);
|
|
233
|
+
}
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (item.type === 'line') {
|
|
238
|
+
validateLine(item, where, issues);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (item.type === 'box') {
|
|
243
|
+
validateBox(item, where, issues);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (item.type === 'image') {
|
|
248
|
+
validateImage(item, where, issues);
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** table */
|
|
253
|
+
if (!(typeof item.rowHeight === 'number' && item.rowHeight > 0)) {
|
|
254
|
+
issues.push(`${where}.rowHeight must be a positive number`);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (item.style?.borderStyle != null
|
|
258
|
+
&& !RULE_STYLES.includes(item.style.borderStyle)) {
|
|
259
|
+
issues.push(
|
|
260
|
+
`${where}.style.borderStyle "${item.style.borderStyle}" is not one of: ` +
|
|
261
|
+
RULE_STYLES.join(', '));
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (item.style?.borderWidth != null
|
|
265
|
+
&& !(typeof item.style.borderWidth === 'number' && item.style.borderWidth > 0)) {
|
|
266
|
+
issues.push(`${where}.style.borderWidth must be a positive number of pixels`);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (!Array.isArray(item.columns) || item.columns.length === 0) {
|
|
270
|
+
issues.push(`${where}.columns must be a non-empty array`);
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
item.columns.forEach((col, k) => {
|
|
275
|
+
if (col == null || typeof col.field !== 'string' || col.field === '') {
|
|
276
|
+
issues.push(`${where}.columns[${k}].field must be a non-empty string`);
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* A box carries no content either - it is a rule bent round four sides - so what
|
|
284
|
+
* there is to check is the pen and the corner. Everything is optional: a bare
|
|
285
|
+
* `{type: "box"}` is a hairline rectangle, which is what a box usually is.
|
|
286
|
+
*/
|
|
287
|
+
function validateBox(item, where, issues) {
|
|
288
|
+
const style = item.style;
|
|
289
|
+
if (style == null) return;
|
|
290
|
+
|
|
291
|
+
if (typeof style !== 'object' || Array.isArray(style)) {
|
|
292
|
+
issues.push(`${where}.style must be an object`);
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (style.borderStyle != null && !RULE_STYLES.includes(style.borderStyle)) {
|
|
297
|
+
issues.push(
|
|
298
|
+
`${where}.style.borderStyle "${style.borderStyle}" is not one of: ` +
|
|
299
|
+
RULE_STYLES.join(', '));
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
for (const key of ['borderWidth', 'radius']) {
|
|
303
|
+
const value = style[key];
|
|
304
|
+
|
|
305
|
+
/** a radius of nothing is a square corner, so zero is allowed there */
|
|
306
|
+
const floor = key === 'radius' ? 0 : 1;
|
|
307
|
+
|
|
308
|
+
if (value != null && !(typeof value === 'number' && value >= floor)) {
|
|
309
|
+
issues.push(
|
|
310
|
+
`${where}.style.${key} must be a number of pixels` +
|
|
311
|
+
(floor ? ' greater than zero' : ' of zero or more'));
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* An image is a fixed box with a picture in it: `src` is one stored in the
|
|
319
|
+
* layout, `field` is a path into the data whose value is the picture's URL.
|
|
320
|
+
*
|
|
321
|
+
* Neither is required. A freshly drawn image has no picture yet, and an empty
|
|
322
|
+
* frame is a layout the designer has to be able to save - it prints nothing,
|
|
323
|
+
* the way a text box with an empty value does. Whether a URL is one the page
|
|
324
|
+
* will actually load is the renderer's question, not this one: a layout can be
|
|
325
|
+
* valid and its data still point somewhere a report should not go.
|
|
326
|
+
*/
|
|
327
|
+
function validateImage(item, where, issues) {
|
|
328
|
+
if (item.src != null && typeof item.src !== 'string') {
|
|
329
|
+
issues.push(`${where}.src must be a string - a URL or a data: URI`);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (item.field != null && typeof item.field !== 'string') {
|
|
333
|
+
issues.push(`${where}.field must be a data path such as "company.logo"`);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (item.fit != null && !IMAGE_FITS.includes(item.fit)) {
|
|
337
|
+
issues.push(`${where}.fit "${item.fit}" is not one of: ${IMAGE_FITS.join(', ')}`);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (item.alt != null && typeof item.alt !== 'string') {
|
|
341
|
+
issues.push(`${where}.alt must be a string`);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* A line carries no content, so what there is to get wrong is its direction and
|
|
348
|
+
* its pen. Both are optional in the file - a bare `{type: "line"}` draws a
|
|
349
|
+
* hairline rule, which is what a line usually is - and only a value that is
|
|
350
|
+
* present and wrong is reported.
|
|
351
|
+
*/
|
|
352
|
+
function validateLine(item, where, issues) {
|
|
353
|
+
if (item.orientation != null
|
|
354
|
+
&& !LINE_ORIENTATIONS.includes(item.orientation)) {
|
|
355
|
+
issues.push(
|
|
356
|
+
`${where}.orientation "${item.orientation}" is not one of: ` +
|
|
357
|
+
LINE_ORIENTATIONS.join(', '));
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const style = item.style;
|
|
361
|
+
if (style == null) return;
|
|
362
|
+
|
|
363
|
+
if (typeof style !== 'object' || Array.isArray(style)) {
|
|
364
|
+
issues.push(`${where}.style must be an object`);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if (style.thickness != null
|
|
369
|
+
&& !(typeof style.thickness === 'number' && style.thickness > 0)) {
|
|
370
|
+
issues.push(`${where}.style.thickness must be a positive number of pixels`);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (style.lineStyle != null && !LINE_STYLES.includes(style.lineStyle)) {
|
|
374
|
+
issues.push(
|
|
375
|
+
`${where}.style.lineStyle "${style.lineStyle}" is not one of: ` +
|
|
376
|
+
LINE_STYLES.join(', '));
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Throws unless the layout is usable.
|
|
383
|
+
* @param {*} json
|
|
384
|
+
* @param {string} stage the engine stage asking, so the message says who refused
|
|
385
|
+
*/
|
|
386
|
+
export function assertLayout(json, stage) {
|
|
387
|
+
const issues = validateLayout(json);
|
|
388
|
+
if (issues.length) {
|
|
389
|
+
throw new ReportError(`${stage}: the layout file is not usable`, issues);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* The data payload is the host application's, so it gets the same treatment.
|
|
396
|
+
* @param {*} data
|
|
397
|
+
* @param {string} stage
|
|
398
|
+
*/
|
|
399
|
+
export function assertData(data, stage) {
|
|
400
|
+
if (data == null || typeof data !== 'object' || Array.isArray(data)) {
|
|
401
|
+
throw new ReportError(`${stage}: data must be an object keyed by dataset name`);
|
|
402
|
+
}
|
|
403
|
+
}
|
package/src/index.html
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
|
|
4
|
+
<head>
|
|
5
|
+
<meta charset="UTF-8">
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
7
|
+
<title>Report Studio</title>
|
|
8
|
+
<link rel="icon" type="image/png" href="./asset/images/icon.png">
|
|
9
|
+
<link rel="stylesheet" href="./shared/theme.css">
|
|
10
|
+
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
11
|
+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
12
|
+
<link
|
|
13
|
+
href="https://fonts.googleapis.com/css2?family=Courier+Prime:ital,wght@0,400;0,700&family=Instrument+Sans:ital,wght@0,400..700&display=swap"
|
|
14
|
+
rel="stylesheet">
|
|
15
|
+
<style>
|
|
16
|
+
body {
|
|
17
|
+
min-height: 100vh;
|
|
18
|
+
display: grid;
|
|
19
|
+
place-content: center;
|
|
20
|
+
gap: 20px;
|
|
21
|
+
background: var(--backdrop);
|
|
22
|
+
font-family: "Instrument Sans", system-ui, sans-serif;
|
|
23
|
+
color: var(--ink);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/* the wordmark stands in for the heading; the text stays for readers */
|
|
27
|
+
h1 {
|
|
28
|
+
height: 26px;
|
|
29
|
+
width: calc(26px * var(--brand-logo-ratio));
|
|
30
|
+
|
|
31
|
+
background-image: var(--brand-logo);
|
|
32
|
+
background-repeat: no-repeat;
|
|
33
|
+
background-position: left center;
|
|
34
|
+
background-size: contain;
|
|
35
|
+
|
|
36
|
+
text-indent: -9999px;
|
|
37
|
+
overflow: hidden;
|
|
38
|
+
white-space: nowrap;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
p {
|
|
42
|
+
color: var(--ink-soft);
|
|
43
|
+
font-size: 13px;
|
|
44
|
+
max-width: 40ch;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
nav {
|
|
48
|
+
display: flex;
|
|
49
|
+
gap: 12px;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
a {
|
|
53
|
+
padding: 10px 18px;
|
|
54
|
+
background: var(--surface);
|
|
55
|
+
border: 1px solid var(--border);
|
|
56
|
+
border-radius: 6px;
|
|
57
|
+
color: var(--ink);
|
|
58
|
+
text-decoration: none;
|
|
59
|
+
font-size: 14px;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
a:hover {
|
|
63
|
+
border-color: var(--accent);
|
|
64
|
+
background: var(--accent-soft);
|
|
65
|
+
}
|
|
66
|
+
</style>
|
|
67
|
+
</head>
|
|
68
|
+
|
|
69
|
+
<body>
|
|
70
|
+
<h1>Report Studio</h1>
|
|
71
|
+
<p>The development playground. Both screens run straight from
|
|
72
|
+
<code>src/</code> with no build step.</p>
|
|
73
|
+
<nav>
|
|
74
|
+
<a href="./designer/">Designer</a>
|
|
75
|
+
<a href="./preview/">Preview</a>
|
|
76
|
+
</nav>
|
|
77
|
+
</body>
|
|
78
|
+
|
|
79
|
+
</html>
|