report-studio 1.2.0 → 1.2.2
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/README.md +38 -2
- package/dist/{designer-qy59eEux.js → designer-DQlfKnTr.js} +928 -928
- package/dist/designer.js +1 -1
- package/dist/index.js +2 -2
- package/dist/react-designer.js +1 -1
- package/dist/react.js +2 -1
- package/dist/viewer-BSPNCNRU.js +2912 -0
- package/package.json +1 -1
- package/src/designer/index.html +1 -1
- package/src/designer/styles.css +31 -8
- package/src/engine/csv.js +12 -197
- package/src/engine/index.js +1 -0
- package/src/engine/rows.js +456 -0
- package/src/engine/xlsx-drawing.js +313 -0
- package/src/engine/xlsx-style.js +457 -0
- package/src/engine/xlsx.js +1200 -0
- package/src/engine/zip.js +138 -0
- package/src/index.html +1 -1
- package/src/index.js +5 -1
- package/src/preview/chrome.css +75 -12
- package/src/preview/chrome.js +69 -55
- package/src/preview/index.html +1 -1
- package/src/preview/viewer.js +42 -1
- package/src/react/index.js +3 -3
- package/src/shared/brand.css +3 -1
- package/src/shared/theme.css +15 -3
- package/types/index.d.ts +53 -0
- package/dist/viewer-xa17tyMJ.js +0 -1721
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* xlsx-drawing.js draws what a cell cannot hold: pictures, rules and boxes,
|
|
3
|
+
* as DrawingML - the floating layer Excel keeps above a sheet's cells.
|
|
4
|
+
*
|
|
5
|
+
* Every drawing is anchored to a cell and an offset into it, and given its size
|
|
6
|
+
* in EMUs, so it stays where it was put when a reader resizes a column above
|
|
7
|
+
* it. Positions arrive in sheet pixels; xlsx.js works those out from the page.
|
|
8
|
+
*
|
|
9
|
+
* The layer is always above the cells - DrawingML has no "behind" - which is
|
|
10
|
+
* why a box with text over it draws only its outline here and leaves its fill
|
|
11
|
+
* to the cells underneath. That choice is xlsx.js's; this file draws what it is
|
|
12
|
+
* told.
|
|
13
|
+
*
|
|
14
|
+
* Pictures are embedded, never linked. Only bytes in hand can be: a `data:`
|
|
15
|
+
* URI, or bytes the caller fetched (toXLSXAsync does). PNG, JPEG, GIF and BMP
|
|
16
|
+
* are what every Excel opens; WebP and AVIF are left out, as a frame with
|
|
17
|
+
* nothing in it is better than a file some readers refuse.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
const NS_XDR = 'http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing';
|
|
22
|
+
const NS_A = 'http://schemas.openxmlformats.org/drawingml/2006/main';
|
|
23
|
+
const NS_R = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';
|
|
24
|
+
|
|
25
|
+
/** English Metric Units: 914400 to the inch, 96 CSS pixels to the inch */
|
|
26
|
+
const EMU_PER_PX = 9525;
|
|
27
|
+
|
|
28
|
+
export const DRAWING_CONTENT_TYPE = 'application/vnd.openxmlformats-officedocument.drawing+xml';
|
|
29
|
+
export const REL_DRAWING = `${NS_R}/drawing`;
|
|
30
|
+
export const REL_IMAGE = `${NS_R}/image`;
|
|
31
|
+
|
|
32
|
+
/** the formats embedded, by extension */
|
|
33
|
+
export const IMAGE_TYPES = Object.freeze({
|
|
34
|
+
png: 'image/png',
|
|
35
|
+
jpeg: 'image/jpeg',
|
|
36
|
+
gif: 'image/gif',
|
|
37
|
+
bmp: 'image/bmp'
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const DATA_URI = /^data:image\/[a-z0-9.+-]+;base64,([a-z0-9+/=\s]*)$/i;
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
/** CSS pixels as EMUs, whole */
|
|
44
|
+
export function emu(px) {
|
|
45
|
+
return Math.max(0, Math.round((Number(px) || 0) * EMU_PER_PX));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
/* ---------------- pictures ---------------- */
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The bytes of a base64 `data:` URI, or null.
|
|
54
|
+
* @param {*} src
|
|
55
|
+
* @returns {Uint8Array|null}
|
|
56
|
+
*/
|
|
57
|
+
export function decodeDataUri(src) {
|
|
58
|
+
if (typeof src !== 'string') return null;
|
|
59
|
+
const match = src.trim().match(DATA_URI);
|
|
60
|
+
if (!match) return null;
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
const binary = atob(match[1].replace(/\s+/g, ''));
|
|
64
|
+
const bytes = new Uint8Array(binary.length);
|
|
65
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
66
|
+
return bytes;
|
|
67
|
+
} catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* What a picture is, from its first bytes rather than what it claims: its
|
|
75
|
+
* format and its natural size, which `contain` and `cover` need to fit it.
|
|
76
|
+
*
|
|
77
|
+
* @param {Uint8Array} bytes
|
|
78
|
+
* @returns {{ext: string, width: number|null, height: number|null}|null}
|
|
79
|
+
* null for a format that is not embedded
|
|
80
|
+
*/
|
|
81
|
+
export function sniffImage(bytes) {
|
|
82
|
+
if (!(bytes instanceof Uint8Array) || bytes.length < 10) return null;
|
|
83
|
+
|
|
84
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
85
|
+
const at = (i, ...sig) => sig.every((b, k) => bytes[i + k] === b);
|
|
86
|
+
|
|
87
|
+
if (at(0, 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A) && bytes.length >= 24) {
|
|
88
|
+
return { ext: 'png', width: view.getUint32(16), height: view.getUint32(20) };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (at(0, 0x47, 0x49, 0x46, 0x38)) {
|
|
92
|
+
return { ext: 'gif', width: view.getUint16(6, true), height: view.getUint16(8, true) };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (at(0, 0x42, 0x4D) && bytes.length >= 26) {
|
|
96
|
+
return { ext: 'bmp', width: Math.abs(view.getInt32(18, true)), height: Math.abs(view.getInt32(22, true)) };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (at(0, 0xFF, 0xD8)) return { ext: 'jpeg', ...jpegSize(bytes, view) };
|
|
100
|
+
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* A JPEG's size, from its first start-of-frame marker. The markers before it
|
|
107
|
+
* are skipped by their declared lengths; the ones that are not frames despite
|
|
108
|
+
* the number (DHT, JPG, DAC) are passed over.
|
|
109
|
+
*/
|
|
110
|
+
function jpegSize(bytes, view) {
|
|
111
|
+
let i = 2;
|
|
112
|
+
|
|
113
|
+
while (i + 9 < bytes.length) {
|
|
114
|
+
if (bytes[i] !== 0xFF) { i++; continue; }
|
|
115
|
+
|
|
116
|
+
const marker = bytes[i + 1];
|
|
117
|
+
if (marker === 0xD8 || marker === 0x01 || (marker >= 0xD0 && marker <= 0xD7)) { i += 2; continue; }
|
|
118
|
+
|
|
119
|
+
const length = view.getUint16(i + 2);
|
|
120
|
+
const frame = marker >= 0xC0 && marker <= 0xCF && ![0xC4, 0xC8, 0xCC].includes(marker);
|
|
121
|
+
|
|
122
|
+
if (frame) return { height: view.getUint16(i + 5), width: view.getUint16(i + 7) };
|
|
123
|
+
|
|
124
|
+
i += 2 + length;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return { width: null, height: null };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Where a picture sits in its box, as items.js's object-fit puts it.
|
|
133
|
+
*
|
|
134
|
+
* contain: scaled to fit and centred, so the box shows round it.
|
|
135
|
+
* cover: scaled to fill and centred, the overflow cropped - in Excel, a
|
|
136
|
+
* source rectangle in thousandths of a percent of each edge.
|
|
137
|
+
* fill: stretched to the box.
|
|
138
|
+
*
|
|
139
|
+
* A picture whose size could not be read is stretched, which is wrong only in
|
|
140
|
+
* proportion, never in place.
|
|
141
|
+
*
|
|
142
|
+
* @returns {{x: number, y: number, w: number, h: number, crop: object|null}}
|
|
143
|
+
* the picture's frame relative to the box, in pixels
|
|
144
|
+
*/
|
|
145
|
+
export function fitImage(fit, box, natural) {
|
|
146
|
+
const { width, height } = natural ?? {};
|
|
147
|
+
const whole = { x: 0, y: 0, w: box.w, h: box.h, crop: null };
|
|
148
|
+
|
|
149
|
+
if (!(width > 0 && height > 0) || fit === 'fill' || box.w <= 0 || box.h <= 0) return whole;
|
|
150
|
+
|
|
151
|
+
if (fit === 'cover') {
|
|
152
|
+
const scale = Math.max(box.w / width, box.h / height);
|
|
153
|
+
const crop = (shown, full) => Math.round(((1 - shown / full) / 2) * 100000);
|
|
154
|
+
const lr = crop(box.w, width * scale);
|
|
155
|
+
const tb = crop(box.h, height * scale);
|
|
156
|
+
|
|
157
|
+
return { ...whole, crop: lr || tb ? { l: lr, r: lr, t: tb, b: tb } : null };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const scale = Math.min(box.w / width, box.h / height);
|
|
161
|
+
const w = width * scale;
|
|
162
|
+
const h = height * scale;
|
|
163
|
+
|
|
164
|
+
return { x: (box.w - w) / 2, y: (box.h - h) / 2, w, h, crop: null };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
/* ---------------- the drawing part ---------------- */
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* A sheet's drawing part.
|
|
173
|
+
*
|
|
174
|
+
* @param {object[]} anchors each { col, colOff, row, rowOff, cx, cy, shape } -
|
|
175
|
+
* offsets and size in pixels, and a shape from picture(), box() or rule()
|
|
176
|
+
* @returns {string} xl/drawings/drawingN.xml
|
|
177
|
+
*/
|
|
178
|
+
export function drawingXml(anchors) {
|
|
179
|
+
const body = anchors.map((a, i) => {
|
|
180
|
+
const id = i + 2;
|
|
181
|
+
return '<xdr:oneCellAnchor>' +
|
|
182
|
+
`<xdr:from><xdr:col>${a.col}</xdr:col><xdr:colOff>${emu(a.colOff)}</xdr:colOff>` +
|
|
183
|
+
`<xdr:row>${a.row}</xdr:row><xdr:rowOff>${emu(a.rowOff)}</xdr:rowOff></xdr:from>` +
|
|
184
|
+
`<xdr:ext cx="${emu(a.cx)}" cy="${emu(a.cy)}"/>` +
|
|
185
|
+
a.shape(id, emu(a.cx), emu(a.cy)) +
|
|
186
|
+
'<xdr:clientData/>' +
|
|
187
|
+
'</xdr:oneCellAnchor>';
|
|
188
|
+
}).join('');
|
|
189
|
+
|
|
190
|
+
return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n' +
|
|
191
|
+
`<xdr:wsDr xmlns:xdr="${NS_XDR}" xmlns:a="${NS_A}" xmlns:r="${NS_R}">${body}</xdr:wsDr>`;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The drawing part's relationships: one per embedded picture.
|
|
197
|
+
* @param {{rId: string, file: string}[]} media
|
|
198
|
+
*/
|
|
199
|
+
export function drawingRels(media) {
|
|
200
|
+
return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n' +
|
|
201
|
+
'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' +
|
|
202
|
+
media.map(m => `<Relationship Id="${m.rId}" Type="${REL_IMAGE}" Target="../media/${m.file}"/>`).join('') +
|
|
203
|
+
'</Relationships>';
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* A picture.
|
|
209
|
+
* @param {{rId: string, alt?: string, crop?: object|null}} picture
|
|
210
|
+
*/
|
|
211
|
+
export function picture({ rId, alt = '', crop = null }) {
|
|
212
|
+
return (id, cx, cy) => '<xdr:pic>' +
|
|
213
|
+
`<xdr:nvPicPr><xdr:cNvPr id="${id}" name="Picture ${id - 1}" descr="${escapeAttr(alt)}"/>` +
|
|
214
|
+
'<xdr:cNvPicPr><a:picLocks noChangeAspect="1"/></xdr:cNvPicPr></xdr:nvPicPr>' +
|
|
215
|
+
'<xdr:blipFill>' +
|
|
216
|
+
`<a:blip r:embed="${rId}"/>` +
|
|
217
|
+
(crop ? `<a:srcRect l="${crop.l}" t="${crop.t}" r="${crop.r}" b="${crop.b}"/>` : '') +
|
|
218
|
+
'<a:stretch><a:fillRect/></a:stretch>' +
|
|
219
|
+
'</xdr:blipFill>' +
|
|
220
|
+
`<xdr:spPr>${xfrm(cx, cy)}<a:prstGeom prst="rect"><a:avLst/></a:prstGeom></xdr:spPr>` +
|
|
221
|
+
'</xdr:pic>';
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* A rectangle, rounded or not, with an outline, a fill, both or neither.
|
|
227
|
+
*
|
|
228
|
+
* @param {{fill?: string|null, outline?: object|null, radius?: number,
|
|
229
|
+
* w: number, h: number}} box fill is ARGB; outline is a pen from pen();
|
|
230
|
+
* radius and size in pixels
|
|
231
|
+
*/
|
|
232
|
+
export function box({ fill = null, outline = null, radius = 0, w, h }) {
|
|
233
|
+
const shortest = Math.min(w, h);
|
|
234
|
+
const adj = radius > 0 && shortest > 0
|
|
235
|
+
? Math.min(50000, Math.round((radius / shortest) * 100000))
|
|
236
|
+
: 0;
|
|
237
|
+
|
|
238
|
+
const geometry = adj > 0
|
|
239
|
+
? `<a:prstGeom prst="roundRect"><a:avLst><a:gd name="adj" fmla="val ${adj}"/></a:avLst></a:prstGeom>`
|
|
240
|
+
: '<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>';
|
|
241
|
+
|
|
242
|
+
return (id, cx, cy) => '<xdr:sp macro="" textlink="">' +
|
|
243
|
+
`<xdr:nvSpPr><xdr:cNvPr id="${id}" name="Box ${id - 1}"/><xdr:cNvSpPr/></xdr:nvSpPr>` +
|
|
244
|
+
`<xdr:spPr>${xfrm(cx, cy)}${geometry}` +
|
|
245
|
+
(fill ? solid(fill) : '<a:noFill/>') +
|
|
246
|
+
lineXml(outline) +
|
|
247
|
+
'</xdr:spPr>' +
|
|
248
|
+
'</xdr:sp>';
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* A straight rule, horizontal or vertical.
|
|
254
|
+
* @param {object} rulePen a pen from pen()
|
|
255
|
+
*/
|
|
256
|
+
export function rule(rulePen) {
|
|
257
|
+
return (id, cx, cy) => '<xdr:cxnSp macro="">' +
|
|
258
|
+
`<xdr:nvCxnSpPr><xdr:cNvPr id="${id}" name="Line ${id - 1}"/><xdr:cNvCxnSpPr/></xdr:nvCxnSpPr>` +
|
|
259
|
+
`<xdr:spPr>${xfrm(cx, cy)}<a:prstGeom prst="line"><a:avLst/></a:prstGeom>${lineXml(rulePen)}</xdr:spPr>` +
|
|
260
|
+
'</xdr:cxnSp>';
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* A pen: the CSS line styles as DrawingML draws them.
|
|
266
|
+
*
|
|
267
|
+
* @param {{style?: string, width?: number, color?: string|null}} spec color is ARGB
|
|
268
|
+
* @returns {object|null} null for no line
|
|
269
|
+
*/
|
|
270
|
+
export function pen({ style = 'solid', width = 1, color = null } = {}) {
|
|
271
|
+
if (style === 'none' || style === 'hidden') return null;
|
|
272
|
+
const w = Number(width);
|
|
273
|
+
if (!Number.isFinite(w) || w <= 0) return null;
|
|
274
|
+
|
|
275
|
+
return {
|
|
276
|
+
width: w,
|
|
277
|
+
color: color ?? 'FF000000',
|
|
278
|
+
dash: { dashed: 'dash', dotted: 'sysDot' }[style] ?? null,
|
|
279
|
+
double: style === 'double'
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
function lineXml(p) {
|
|
285
|
+
if (!p) return '<a:ln><a:noFill/></a:ln>';
|
|
286
|
+
|
|
287
|
+
return `<a:ln w="${emu(p.width)}"${p.double ? ' cmpd="dbl"' : ''}>` +
|
|
288
|
+
solid(p.color) +
|
|
289
|
+
(p.dash ? `<a:prstDash val="${p.dash}"/>` : '') +
|
|
290
|
+
'</a:ln>';
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
/** an ARGB colour as a solid DrawingML fill; DrawingML takes RGB without the alpha */
|
|
295
|
+
function solid(argbColor) {
|
|
296
|
+
return `<a:solidFill><a:srgbClr val="${argbColor.slice(-6)}"/></a:solidFill>`;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
/** the shape's own frame; the anchor is what places it, so the offset stays 0 */
|
|
301
|
+
function xfrm(cx, cy) {
|
|
302
|
+
return `<a:xfrm><a:off x="0" y="0"/><a:ext cx="${cx}" cy="${cy}"/></a:xfrm>`;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
function escapeAttr(text) {
|
|
307
|
+
return String(text ?? '')
|
|
308
|
+
.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, '')
|
|
309
|
+
.replace(/&/g, '&')
|
|
310
|
+
.replace(/</g, '<')
|
|
311
|
+
.replace(/>/g, '>')
|
|
312
|
+
.replace(/"/g, '"');
|
|
313
|
+
}
|