@file-viewer/renderer-spreadsheet 2.4.0 → 3.0.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.
- package/dist/spreadsheet/chartRenderer.js +216 -41
- package/dist/spreadsheet/chartSampling.d.ts +2 -0
- package/dist/spreadsheet/chartSampling.js +32 -0
- package/dist/spreadsheet/state.d.ts +2 -1
- package/dist/spreadsheet/view.d.ts +4 -5
- package/dist/spreadsheet/view.js +136 -5
- package/dist/spreadsheet/worker/sheetjs/SheetJsModel.d.ts +2 -6
- package/dist/spreadsheet/worker/sheetjs/SheetJsModel.js +20 -5
- package/dist/spreadsheet/worker/sheetjs/chartParser.js +51 -3
- package/dist/spreadsheet/worker/sheetjs/parser.js +82 -4
- package/dist/spreadsheet/worker/sheetjs/richText.d.ts +4 -0
- package/dist/spreadsheet/worker/sheetjs/richText.js +135 -0
- package/dist/spreadsheet/worker/type.d.ts +27 -1
- package/dist/worker/sheet.worker.js +36 -36
- package/file-viewer.capability.json +22 -0
- package/package.json +11 -6
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { extremaPointIndexes } from './chartSampling.js';
|
|
1
2
|
const SVG_NAMESPACE = 'http://www.w3.org/2000/svg';
|
|
2
3
|
const DEFAULT_COLORS = ['#4472c4', '#ed7d31', '#70ad47', '#ffc000', '#5b9bd5', '#a5a5a5'];
|
|
4
|
+
const MAX_LINE_POINTS_PER_PIXEL = 2;
|
|
3
5
|
const svgElement = (documentRef, name, attributes = {}) => {
|
|
4
6
|
const element = documentRef.createElementNS(SVG_NAMESPACE, name);
|
|
5
7
|
Object.entries(attributes).forEach(([key, value]) => element.setAttribute(key, `${value}`));
|
|
@@ -24,16 +26,148 @@ const svgText = (documentRef, value, x, y, options = {}) => {
|
|
|
24
26
|
const seriesColor = (series, index) => {
|
|
25
27
|
return series.color || DEFAULT_COLORS[index % DEFAULT_COLORS.length];
|
|
26
28
|
};
|
|
29
|
+
const seriesStrokeWidth = (series) => {
|
|
30
|
+
return Math.min(8, Math.max(0.5, series.lineWidth || 2.5));
|
|
31
|
+
};
|
|
32
|
+
const seriesDashArray = (series) => {
|
|
33
|
+
switch (series.lineDash) {
|
|
34
|
+
case 'dash':
|
|
35
|
+
case 'sysDash':
|
|
36
|
+
return '8 4';
|
|
37
|
+
case 'dashDot':
|
|
38
|
+
case 'sysDashDot':
|
|
39
|
+
return '8 4 2 4';
|
|
40
|
+
case 'dot':
|
|
41
|
+
case 'sysDot':
|
|
42
|
+
return '2 3';
|
|
43
|
+
case 'lgDash':
|
|
44
|
+
return '12 5';
|
|
45
|
+
case 'lgDashDot':
|
|
46
|
+
return '12 5 2 5';
|
|
47
|
+
case 'lgDashDotDot':
|
|
48
|
+
return '12 5 2 5 2 5';
|
|
49
|
+
default:
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const showsSeriesLine = (chart, series) => {
|
|
54
|
+
var _a;
|
|
55
|
+
if (series.lineVisible === false) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
return chart.type !== 'scatter' || ((_a = chart.scatterStyle) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== 'marker';
|
|
59
|
+
};
|
|
60
|
+
const showsSeriesMarker = (chart, series) => {
|
|
61
|
+
if (series.marker) {
|
|
62
|
+
return series.marker.symbol.toLowerCase() !== 'none';
|
|
63
|
+
}
|
|
64
|
+
if (chart.type !== 'scatter') {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
const scatterStyle = (chart.scatterStyle || 'lineMarker').toLowerCase();
|
|
68
|
+
return scatterStyle === 'marker' || scatterStyle.endsWith('marker');
|
|
69
|
+
};
|
|
70
|
+
const categoryLabelIndexes = (count, plotWidth) => {
|
|
71
|
+
const limit = Math.max(2, Math.floor(plotWidth / 28));
|
|
72
|
+
if (count <= limit) {
|
|
73
|
+
return Array.from({ length: count }, (_, index) => index);
|
|
74
|
+
}
|
|
75
|
+
return Array.from(new Set(Array.from({ length: limit }, (_, index) => Math.round((index * (count - 1)) / (limit - 1)))));
|
|
76
|
+
};
|
|
77
|
+
const drawSeriesMarker = (documentRef, svg, series, color, x, y) => {
|
|
78
|
+
var _a, _b;
|
|
79
|
+
const symbol = ((_a = series.marker) === null || _a === void 0 ? void 0 : _a.symbol.toLowerCase()) || 'circle';
|
|
80
|
+
const radius = Math.min(10, Math.max(1.5, ((((_b = series.marker) === null || _b === void 0 ? void 0 : _b.size) || 5) * 4) / 6));
|
|
81
|
+
const common = {
|
|
82
|
+
class: 'excel-chart-marker',
|
|
83
|
+
'data-marker-symbol': symbol
|
|
84
|
+
};
|
|
85
|
+
switch (symbol) {
|
|
86
|
+
case 'square':
|
|
87
|
+
svg.appendChild(svgElement(documentRef, 'rect', {
|
|
88
|
+
...common,
|
|
89
|
+
x: x - radius,
|
|
90
|
+
y: y - radius,
|
|
91
|
+
width: radius * 2,
|
|
92
|
+
height: radius * 2,
|
|
93
|
+
fill: color
|
|
94
|
+
}));
|
|
95
|
+
break;
|
|
96
|
+
case 'diamond':
|
|
97
|
+
svg.appendChild(svgElement(documentRef, 'polygon', {
|
|
98
|
+
...common,
|
|
99
|
+
points: `${x},${y - radius} ${x + radius},${y} ${x},${y + radius} ${x - radius},${y}`,
|
|
100
|
+
fill: color
|
|
101
|
+
}));
|
|
102
|
+
break;
|
|
103
|
+
case 'triangle':
|
|
104
|
+
svg.appendChild(svgElement(documentRef, 'polygon', {
|
|
105
|
+
...common,
|
|
106
|
+
points: `${x},${y - radius} ${x + radius},${y + radius} ${x - radius},${y + radius}`,
|
|
107
|
+
fill: color
|
|
108
|
+
}));
|
|
109
|
+
break;
|
|
110
|
+
case 'dash':
|
|
111
|
+
svg.appendChild(svgElement(documentRef, 'line', {
|
|
112
|
+
...common,
|
|
113
|
+
x1: x - radius,
|
|
114
|
+
y1: y,
|
|
115
|
+
x2: x + radius,
|
|
116
|
+
y2: y,
|
|
117
|
+
stroke: color,
|
|
118
|
+
'stroke-width': 2
|
|
119
|
+
}));
|
|
120
|
+
break;
|
|
121
|
+
case 'plus':
|
|
122
|
+
svg.appendChild(svgElement(documentRef, 'path', {
|
|
123
|
+
...common,
|
|
124
|
+
d: `M${x - radius},${y} L${x + radius},${y} M${x},${y - radius} L${x},${y + radius}`,
|
|
125
|
+
fill: 'none',
|
|
126
|
+
stroke: color,
|
|
127
|
+
'stroke-width': 1.5
|
|
128
|
+
}));
|
|
129
|
+
break;
|
|
130
|
+
case 'x':
|
|
131
|
+
svg.appendChild(svgElement(documentRef, 'path', {
|
|
132
|
+
...common,
|
|
133
|
+
d: `M${x - radius},${y - radius} L${x + radius},${y + radius} M${x + radius},${y - radius} L${x - radius},${y + radius}`,
|
|
134
|
+
fill: 'none',
|
|
135
|
+
stroke: color,
|
|
136
|
+
'stroke-width': 1.5
|
|
137
|
+
}));
|
|
138
|
+
break;
|
|
139
|
+
case 'dot':
|
|
140
|
+
case 'circle':
|
|
141
|
+
case 'auto':
|
|
142
|
+
default:
|
|
143
|
+
svg.appendChild(svgElement(documentRef, 'circle', {
|
|
144
|
+
...common,
|
|
145
|
+
cx: x,
|
|
146
|
+
cy: y,
|
|
147
|
+
r: symbol === 'dot' ? Math.max(1, radius / 2) : radius,
|
|
148
|
+
fill: color
|
|
149
|
+
}));
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
27
153
|
const categoryLabels = (chart) => {
|
|
28
154
|
var _a;
|
|
29
155
|
const labels = ((_a = chart.series.find((series) => series.categories.length)) === null || _a === void 0 ? void 0 : _a.categories) || [];
|
|
30
|
-
const valueCount =
|
|
156
|
+
const valueCount = chart.series.reduce((count, series) => Math.max(count, series.values.length), 0);
|
|
31
157
|
return labels.length ? labels : Array.from({ length: valueCount }, (_, index) => `${index + 1}`);
|
|
32
158
|
};
|
|
33
159
|
const valueDomain = (chart) => {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
160
|
+
let min = 0;
|
|
161
|
+
let max = 0;
|
|
162
|
+
chart.series.forEach((series) => {
|
|
163
|
+
series.values.forEach((value) => {
|
|
164
|
+
if (!Number.isFinite(value)) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
min = Math.min(min, value);
|
|
168
|
+
max = Math.max(max, value);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
37
171
|
if (min === max) {
|
|
38
172
|
return { min: 0, max: Math.max(1, max), span: Math.max(1, max) };
|
|
39
173
|
}
|
|
@@ -75,25 +209,36 @@ const drawValueGrid = (documentRef, svg, plot, domain) => {
|
|
|
75
209
|
}));
|
|
76
210
|
}
|
|
77
211
|
};
|
|
78
|
-
const drawCategoryLabels = (documentRef, svg, chart, plot) => {
|
|
212
|
+
const drawCategoryLabels = (documentRef, svg, chart, plot, pointAxis = false) => {
|
|
79
213
|
const categories = categoryLabels(chart);
|
|
80
214
|
if (!categories.length) {
|
|
81
215
|
return;
|
|
82
216
|
}
|
|
217
|
+
const categorySeries = chart.series.find((series) => series.categories.length);
|
|
218
|
+
const sourcePointCount = Math.max((categorySeries === null || categorySeries === void 0 ? void 0 : categorySeries.sourcePointCount) || categories.length, categories.length);
|
|
83
219
|
const step = plot.width / categories.length;
|
|
84
|
-
categories.forEach((
|
|
220
|
+
categoryLabelIndexes(categories.length, plot.width).forEach((index) => {
|
|
221
|
+
var _a, _b;
|
|
222
|
+
const category = categories[index];
|
|
85
223
|
const maxLength = categories.length > 8 ? 8 : 14;
|
|
86
224
|
const label = category.length > maxLength ? `${category.slice(0, maxLength - 1)}…` : category;
|
|
87
|
-
|
|
225
|
+
const sourceIndex = (_b = (_a = categorySeries === null || categorySeries === void 0 ? void 0 : categorySeries.sourcePointIndexes) === null || _a === void 0 ? void 0 : _a[index]) !== null && _b !== void 0 ? _b : index;
|
|
226
|
+
const x = pointAxis && categories.length > 1
|
|
227
|
+
? plot.left + (sourceIndex / Math.max(sourcePointCount - 1, 1)) * plot.width
|
|
228
|
+
: plot.left + (index + 0.5) * step;
|
|
229
|
+
const labelElement = svgText(documentRef, label, x, plot.bottom + 17, {
|
|
88
230
|
anchor: 'middle',
|
|
89
231
|
size: 10
|
|
90
|
-
})
|
|
232
|
+
});
|
|
233
|
+
labelElement.classList.add('excel-chart-category-label');
|
|
234
|
+
labelElement.dataset.categoryIndex = `${sourceIndex}`;
|
|
235
|
+
svg.appendChild(labelElement);
|
|
91
236
|
});
|
|
92
237
|
};
|
|
93
238
|
const drawColumnChart = (documentRef, svg, chart, plot) => {
|
|
94
239
|
const domain = valueDomain(chart);
|
|
95
240
|
const categories = categoryLabels(chart);
|
|
96
|
-
const categoryCount = Math.max(categories.length,
|
|
241
|
+
const categoryCount = Math.max(categories.length, chart.series.reduce((count, series) => Math.max(count, series.values.length), 0), 1);
|
|
97
242
|
const groupWidth = plot.width / categoryCount;
|
|
98
243
|
const seriesCount = Math.max(chart.series.length, 1);
|
|
99
244
|
const slotWidth = (groupWidth * 0.76) / seriesCount;
|
|
@@ -123,7 +268,7 @@ const drawColumnChart = (documentRef, svg, chart, plot) => {
|
|
|
123
268
|
const drawHorizontalBarChart = (documentRef, svg, chart, plot) => {
|
|
124
269
|
const domain = valueDomain(chart);
|
|
125
270
|
const categories = categoryLabels(chart);
|
|
126
|
-
const categoryCount = Math.max(categories.length,
|
|
271
|
+
const categoryCount = Math.max(categories.length, chart.series.reduce((count, series) => Math.max(count, series.values.length), 0), 1);
|
|
127
272
|
const groupHeight = plot.height / categoryCount;
|
|
128
273
|
const seriesCount = Math.max(chart.series.length, 1);
|
|
129
274
|
const slotHeight = (groupHeight * 0.76) / seriesCount;
|
|
@@ -163,14 +308,20 @@ const drawHorizontalBarChart = (documentRef, svg, chart, plot) => {
|
|
|
163
308
|
};
|
|
164
309
|
const drawLineChart = (documentRef, svg, chart, plot, fillArea) => {
|
|
165
310
|
const domain = valueDomain(chart);
|
|
166
|
-
const count = Math.max(
|
|
311
|
+
const count = Math.max(chart.series.reduce((pointCount, series) => Math.max(pointCount, series.sourcePointCount || series.values.length), 0), 1);
|
|
167
312
|
const step = count > 1 ? plot.width / (count - 1) : plot.width;
|
|
168
313
|
drawValueGrid(documentRef, svg, plot, domain);
|
|
169
314
|
chart.series.forEach((series, seriesIndex) => {
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
315
|
+
const pointIndexes = extremaPointIndexes(series.values, Math.max(64, Math.floor(plot.width * MAX_LINE_POINTS_PER_PIXEL)));
|
|
316
|
+
const points = pointIndexes.map((index) => {
|
|
317
|
+
var _a, _b;
|
|
318
|
+
const value = series.values[index];
|
|
319
|
+
const sourceIndex = (_b = (_a = series.sourcePointIndexes) === null || _a === void 0 ? void 0 : _a[index]) !== null && _b !== void 0 ? _b : index;
|
|
320
|
+
return {
|
|
321
|
+
x: plot.left + (count > 1 ? sourceIndex * step : plot.width / 2),
|
|
322
|
+
y: plot.bottom - ((value - domain.min) / domain.span) * plot.height
|
|
323
|
+
};
|
|
324
|
+
});
|
|
174
325
|
if (!points.length) {
|
|
175
326
|
return;
|
|
176
327
|
}
|
|
@@ -184,26 +335,28 @@ const drawLineChart = (documentRef, svg, chart, plot, fillArea) => {
|
|
|
184
335
|
opacity: 0.2
|
|
185
336
|
}));
|
|
186
337
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
stroke: '
|
|
202
|
-
'stroke-width': 1
|
|
338
|
+
const color = seriesColor(series, seriesIndex);
|
|
339
|
+
const dashArray = seriesDashArray(series);
|
|
340
|
+
if (showsSeriesLine(chart, series)) {
|
|
341
|
+
svg.appendChild(svgElement(documentRef, 'path', {
|
|
342
|
+
class: 'excel-chart-series-line',
|
|
343
|
+
'data-source-point-count': series.sourcePointCount || series.values.length,
|
|
344
|
+
'data-transferred-point-count': series.values.length,
|
|
345
|
+
'data-rendered-point-count': points.length,
|
|
346
|
+
d: pathData,
|
|
347
|
+
fill: 'none',
|
|
348
|
+
stroke: color,
|
|
349
|
+
'stroke-width': seriesStrokeWidth(series),
|
|
350
|
+
...(dashArray ? { 'stroke-dasharray': dashArray } : {}),
|
|
351
|
+
'stroke-linejoin': 'round',
|
|
352
|
+
'stroke-linecap': 'round'
|
|
203
353
|
}));
|
|
204
|
-
}
|
|
354
|
+
}
|
|
355
|
+
if (showsSeriesMarker(chart, series)) {
|
|
356
|
+
points.forEach((point) => drawSeriesMarker(documentRef, svg, series, color, point.x, point.y));
|
|
357
|
+
}
|
|
205
358
|
});
|
|
206
|
-
drawCategoryLabels(documentRef, svg, chart, plot);
|
|
359
|
+
drawCategoryLabels(documentRef, svg, chart, plot, true);
|
|
207
360
|
};
|
|
208
361
|
const polarPoint = (cx, cy, radius, angle) => {
|
|
209
362
|
const radians = ((angle - 90) * Math.PI) / 180;
|
|
@@ -256,15 +409,36 @@ const drawLegend = (documentRef, svg, chart) => {
|
|
|
256
409
|
chart.series.slice(0, 8).forEach((series, index) => {
|
|
257
410
|
const x = right ? 510 : 76 + (index % 4) * 136;
|
|
258
411
|
const y = right ? 72 + index * 28 : 342 - Math.floor(index / 4) * 20;
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
412
|
+
const color = seriesColor(series, index);
|
|
413
|
+
const lineLike = chart.type === 'line' || chart.type === 'scatter' || chart.type === 'radar';
|
|
414
|
+
if (lineLike) {
|
|
415
|
+
if (showsSeriesLine(chart, series)) {
|
|
416
|
+
const dashArray = seriesDashArray(series);
|
|
417
|
+
svg.appendChild(svgElement(documentRef, 'line', {
|
|
418
|
+
x1: x,
|
|
419
|
+
y1: y - 5,
|
|
420
|
+
x2: x + 12,
|
|
421
|
+
y2: y - 5,
|
|
422
|
+
stroke: color,
|
|
423
|
+
'stroke-width': seriesStrokeWidth(series),
|
|
424
|
+
...(dashArray ? { 'stroke-dasharray': dashArray } : {})
|
|
425
|
+
}));
|
|
426
|
+
}
|
|
427
|
+
if (showsSeriesMarker(chart, series)) {
|
|
428
|
+
drawSeriesMarker(documentRef, svg, series, color, x + 6, y - 5);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
svg.appendChild(svgElement(documentRef, 'rect', {
|
|
433
|
+
x,
|
|
434
|
+
y: y - 10,
|
|
435
|
+
width: 10,
|
|
436
|
+
height: 10,
|
|
437
|
+
fill: color
|
|
438
|
+
}));
|
|
439
|
+
}
|
|
266
440
|
const label = series.name.length > 16 ? `${series.name.slice(0, 15)}…` : series.name;
|
|
267
|
-
svg.appendChild(svgText(documentRef, label, x +
|
|
441
|
+
svg.appendChild(svgText(documentRef, label, x + 18, y, { size: 10 }));
|
|
268
442
|
});
|
|
269
443
|
};
|
|
270
444
|
export const renderSpreadsheetChart = (documentRef, chart) => {
|
|
@@ -273,6 +447,7 @@ export const renderSpreadsheetChart = (documentRef, chart) => {
|
|
|
273
447
|
container.dataset.chartId = chart.id;
|
|
274
448
|
container.dataset.chartType = chart.type;
|
|
275
449
|
container.dataset.seriesNames = JSON.stringify(chart.series.map((series) => series.name));
|
|
450
|
+
container.dataset.seriesMarkers = JSON.stringify(chart.series.map((series) => { var _a; return ((_a = series.marker) === null || _a === void 0 ? void 0 : _a.symbol) || null; }));
|
|
276
451
|
container.setAttribute('role', 'img');
|
|
277
452
|
container.setAttribute('aria-label', chart.title || chart.id || 'Spreadsheet chart');
|
|
278
453
|
const svg = svgElement(documentRef, 'svg', {
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export const MAX_TRANSFERRED_LINE_CHART_POINTS = 4096;
|
|
2
|
+
export const extremaPointIndexes = (values, maxPoints) => {
|
|
3
|
+
const pointLimit = Math.max(2, Math.floor(maxPoints));
|
|
4
|
+
if (values.length <= pointLimit) {
|
|
5
|
+
return values.map((_, index) => index);
|
|
6
|
+
}
|
|
7
|
+
const bucketCount = Math.max(1, Math.floor((pointLimit - 2) / 2));
|
|
8
|
+
const bucketSize = (values.length - 2) / bucketCount;
|
|
9
|
+
const result = [0];
|
|
10
|
+
for (let bucket = 0; bucket < bucketCount; bucket += 1) {
|
|
11
|
+
const start = Math.max(1, Math.floor(1 + bucket * bucketSize));
|
|
12
|
+
const end = Math.min(values.length - 1, Math.ceil(1 + (bucket + 1) * bucketSize));
|
|
13
|
+
let minIndex = start;
|
|
14
|
+
let maxIndex = start;
|
|
15
|
+
for (let index = start + 1; index < end; index += 1) {
|
|
16
|
+
if (values[index] < values[minIndex]) {
|
|
17
|
+
minIndex = index;
|
|
18
|
+
}
|
|
19
|
+
if (values[index] > values[maxIndex]) {
|
|
20
|
+
maxIndex = index;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (minIndex <= maxIndex) {
|
|
24
|
+
result.push(minIndex, maxIndex);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
result.push(maxIndex, minIndex);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
result.push(values.length - 1);
|
|
31
|
+
return Array.from(new Set(result));
|
|
32
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Align, Column, VerticalAlign } from 'e-virt-table';
|
|
2
|
-
import type { CellMerge } from './worker/type.js';
|
|
2
|
+
import type { CellMerge, SheetRichTextRun } from './worker/type.js';
|
|
3
3
|
export declare const WINDOW_SIZE = 500;
|
|
4
4
|
export declare const PREFETCH_AHEAD = 3;
|
|
5
5
|
export declare const PREFETCH_BEHIND = 1;
|
|
@@ -29,6 +29,7 @@ export interface CellBorderCache {
|
|
|
29
29
|
color: string;
|
|
30
30
|
}
|
|
31
31
|
export interface CellStyleCache {
|
|
32
|
+
richText?: SheetRichTextRun[];
|
|
32
33
|
backgroundColor?: string;
|
|
33
34
|
color?: string;
|
|
34
35
|
font?: string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Column, ConfigType } from 'e-virt-table';
|
|
2
|
-
import type { SheetModel } from './worker/type.js';
|
|
2
|
+
import type { SheetCellMetadata, SheetModel } from './worker/type.js';
|
|
3
3
|
import { type CellStyleCache, type SheetDefaults, type VirtualSheetState } from './state.js';
|
|
4
4
|
export declare const INDEX_COLUMN_WIDTH = 68;
|
|
5
5
|
export declare const SPREADSHEET_MIN_ZOOM = 0.25;
|
|
@@ -23,6 +23,7 @@ interface SpreadsheetCopyParams {
|
|
|
23
23
|
xArr: number[];
|
|
24
24
|
yArr: number[];
|
|
25
25
|
}
|
|
26
|
+
export declare const buildFont: (style: Record<string, any>) => string | undefined;
|
|
26
27
|
export declare const getRowHeight: (heights: number | number[] | undefined, index: number, fallback: number) => number;
|
|
27
28
|
export declare const normalizeRowHeight: (height: number | undefined, fallback: number) => number;
|
|
28
29
|
export declare const detectIndexOffset: (ws: SheetModel) => 0 | 1;
|
|
@@ -31,9 +32,7 @@ export declare const buildColumns: (ws: SheetModel) => {
|
|
|
31
32
|
dataKeys: string[];
|
|
32
33
|
};
|
|
33
34
|
export declare const getDisplayColumns: (columns: Column[], zoomScale?: number) => Column[];
|
|
34
|
-
export declare const
|
|
35
|
-
|
|
36
|
-
style: any;
|
|
37
|
-
} | undefined) => CellStyleCache | undefined;
|
|
35
|
+
export declare const createTextLayer: (documentRef: Document, value: unknown, style: CellStyleCache, padding: number) => HTMLSpanElement;
|
|
36
|
+
export declare const normalizeCellStyle: (meta: SheetCellMetadata | undefined) => CellStyleCache | undefined;
|
|
38
37
|
export declare const createTableConfig: ({ hostHeight, darkMode, resizableColumns, resizableRows, copySelection, sheetDefaults, virtualState, zoomScale }: TableConfigOptions) => ConfigType;
|
|
39
38
|
export {};
|
package/dist/spreadsheet/view.js
CHANGED
|
@@ -62,6 +62,16 @@ const scaleBorder = (border, zoomScale) => {
|
|
|
62
62
|
width: Math.max(0.5, border.width * normalizeZoomScale(zoomScale))
|
|
63
63
|
};
|
|
64
64
|
};
|
|
65
|
+
const scaleRichText = (runs, zoomScale) => {
|
|
66
|
+
if (!(runs === null || runs === void 0 ? void 0 : runs.length)) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
const normalizedScale = normalizeZoomScale(zoomScale);
|
|
70
|
+
return runs.map(run => ({
|
|
71
|
+
...run,
|
|
72
|
+
...(run.fontSize ? { fontSize: run.fontSize * normalizedScale } : {})
|
|
73
|
+
}));
|
|
74
|
+
};
|
|
65
75
|
const scaleCellStyle = (style, zoomScale) => {
|
|
66
76
|
if (!style) {
|
|
67
77
|
return undefined;
|
|
@@ -69,6 +79,7 @@ const scaleCellStyle = (style, zoomScale) => {
|
|
|
69
79
|
return {
|
|
70
80
|
...style,
|
|
71
81
|
font: scaleFont(style.font, zoomScale),
|
|
82
|
+
richText: scaleRichText(style.richText, zoomScale),
|
|
72
83
|
borderTop: scaleBorder(style.borderTop, zoomScale),
|
|
73
84
|
borderRight: scaleBorder(style.borderRight, zoomScale),
|
|
74
85
|
borderBottom: scaleBorder(style.borderBottom, zoomScale),
|
|
@@ -96,7 +107,41 @@ const cloneColumns = (columns) => {
|
|
|
96
107
|
});
|
|
97
108
|
});
|
|
98
109
|
};
|
|
99
|
-
const
|
|
110
|
+
const CSS_GENERIC_FONT_FAMILIES = new Set([
|
|
111
|
+
'caption',
|
|
112
|
+
'cursive',
|
|
113
|
+
'emoji',
|
|
114
|
+
'fangsong',
|
|
115
|
+
'fantasy',
|
|
116
|
+
'icon',
|
|
117
|
+
'math',
|
|
118
|
+
'menu',
|
|
119
|
+
'message-box',
|
|
120
|
+
'monospace',
|
|
121
|
+
'sans-serif',
|
|
122
|
+
'serif',
|
|
123
|
+
'small-caption',
|
|
124
|
+
'status-bar',
|
|
125
|
+
'system-ui',
|
|
126
|
+
'ui-monospace',
|
|
127
|
+
'ui-rounded',
|
|
128
|
+
'ui-sans-serif',
|
|
129
|
+
'ui-serif'
|
|
130
|
+
]);
|
|
131
|
+
const normalizeFontFamily = (value) => {
|
|
132
|
+
const family = typeof value === 'string' ? value.trim() : '';
|
|
133
|
+
if (!family) {
|
|
134
|
+
return TABLE_FONT_FAMILY;
|
|
135
|
+
}
|
|
136
|
+
if (family.includes(',') || CSS_GENERIC_FONT_FAMILIES.has(family.toLowerCase())) {
|
|
137
|
+
return family;
|
|
138
|
+
}
|
|
139
|
+
if ((family.startsWith('"') && family.endsWith('"')) || (family.startsWith("'") && family.endsWith("'"))) {
|
|
140
|
+
return family;
|
|
141
|
+
}
|
|
142
|
+
return `"${family.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
|
|
143
|
+
};
|
|
144
|
+
export const buildFont = (style) => {
|
|
100
145
|
if (typeof style.font === 'string' && style.font.trim()) {
|
|
101
146
|
return style.font;
|
|
102
147
|
}
|
|
@@ -125,7 +170,7 @@ const buildFont = (style) => {
|
|
|
125
170
|
style.fontStyle || 'normal',
|
|
126
171
|
normalizedWeight,
|
|
127
172
|
`${Number.parseInt(style.fontSize, 10) || TABLE_FONT_SIZE}px`,
|
|
128
|
-
style.fontFamily
|
|
173
|
+
normalizeFontFamily(style.fontFamily)
|
|
129
174
|
].join(' ');
|
|
130
175
|
};
|
|
131
176
|
const alignFromClassName = (className) => {
|
|
@@ -357,6 +402,10 @@ const hasTextLayout = (style) => {
|
|
|
357
402
|
return !!(style.horizontalAlign || style.verticalAlign || style.wrapText || style.shrinkToFit);
|
|
358
403
|
};
|
|
359
404
|
const shouldRenderTextInOverlay = (style, column) => {
|
|
405
|
+
var _a;
|
|
406
|
+
if ((_a = style === null || style === void 0 ? void 0 : style.richText) === null || _a === void 0 ? void 0 : _a.length) {
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
360
409
|
if (!hasTextLayout(style)) {
|
|
361
410
|
return false;
|
|
362
411
|
}
|
|
@@ -626,9 +675,9 @@ const createBorderLine = (documentRef, side, border) => {
|
|
|
626
675
|
}
|
|
627
676
|
return line;
|
|
628
677
|
};
|
|
629
|
-
const createTextLayer = (documentRef, value, style, padding) => {
|
|
678
|
+
export const createTextLayer = (documentRef, value, style, padding) => {
|
|
679
|
+
var _a;
|
|
630
680
|
const text = documentRef.createElement('span');
|
|
631
|
-
text.textContent = getTextValue(value);
|
|
632
681
|
Object.assign(text.style, {
|
|
633
682
|
position: 'relative',
|
|
634
683
|
zIndex: '1',
|
|
@@ -650,6 +699,52 @@ const createTextLayer = (documentRef, value, style, padding) => {
|
|
|
650
699
|
transform: style.shrinkToFit ? 'scale(0.92)' : 'none',
|
|
651
700
|
transformOrigin: `${style.horizontalAlign || 'left'} center`
|
|
652
701
|
});
|
|
702
|
+
if ((_a = style.richText) === null || _a === void 0 ? void 0 : _a.length) {
|
|
703
|
+
const content = documentRef.createElement('span');
|
|
704
|
+
content.dataset.fileViewerRichText = 'true';
|
|
705
|
+
Object.assign(content.style, {
|
|
706
|
+
display: 'inline-block',
|
|
707
|
+
maxWidth: '100%',
|
|
708
|
+
minWidth: '0',
|
|
709
|
+
whiteSpace: 'inherit',
|
|
710
|
+
wordBreak: 'inherit'
|
|
711
|
+
});
|
|
712
|
+
style.richText.forEach((run) => {
|
|
713
|
+
const segment = documentRef.createElement('span');
|
|
714
|
+
segment.textContent = run.text;
|
|
715
|
+
segment.dataset.fileViewerRichTextRun = 'true';
|
|
716
|
+
if (run.fontFamily) {
|
|
717
|
+
segment.style.fontFamily = run.fontFamily;
|
|
718
|
+
}
|
|
719
|
+
if (run.fontSize) {
|
|
720
|
+
segment.style.fontSize = `${run.fontSize}px`;
|
|
721
|
+
}
|
|
722
|
+
if (run.color) {
|
|
723
|
+
segment.style.color = run.color;
|
|
724
|
+
}
|
|
725
|
+
if (run.bold) {
|
|
726
|
+
segment.style.fontWeight = 'bold';
|
|
727
|
+
}
|
|
728
|
+
if (run.italic) {
|
|
729
|
+
segment.style.fontStyle = 'italic';
|
|
730
|
+
}
|
|
731
|
+
const decorations = [
|
|
732
|
+
run.underline ? 'underline' : '',
|
|
733
|
+
run.strike ? 'line-through' : ''
|
|
734
|
+
].filter(Boolean);
|
|
735
|
+
if (decorations.length) {
|
|
736
|
+
segment.style.textDecoration = decorations.join(' ');
|
|
737
|
+
}
|
|
738
|
+
if (run.verticalAlign) {
|
|
739
|
+
segment.style.verticalAlign = run.verticalAlign;
|
|
740
|
+
}
|
|
741
|
+
content.appendChild(segment);
|
|
742
|
+
});
|
|
743
|
+
text.appendChild(content);
|
|
744
|
+
}
|
|
745
|
+
else {
|
|
746
|
+
text.textContent = getTextValue(value);
|
|
747
|
+
}
|
|
653
748
|
return text;
|
|
654
749
|
};
|
|
655
750
|
const applyOverflowLayout = (text, overflowLayout) => {
|
|
@@ -695,12 +790,48 @@ const renderCellOverlay = (cellEl, borderStyle, textStyle, textValue, renderText
|
|
|
695
790
|
}
|
|
696
791
|
cellEl.appendChild(fragment);
|
|
697
792
|
};
|
|
793
|
+
const normalizeRichTextRuns = (value) => {
|
|
794
|
+
if (!Array.isArray(value)) {
|
|
795
|
+
return undefined;
|
|
796
|
+
}
|
|
797
|
+
const runs = value.flatMap((candidate) => {
|
|
798
|
+
if (!candidate || typeof candidate !== 'object') {
|
|
799
|
+
return [];
|
|
800
|
+
}
|
|
801
|
+
const run = candidate;
|
|
802
|
+
if (typeof run.text !== 'string' || !run.text) {
|
|
803
|
+
return [];
|
|
804
|
+
}
|
|
805
|
+
const fontSize = typeof run.fontSize === 'number' && Number.isFinite(run.fontSize) && run.fontSize > 0
|
|
806
|
+
? run.fontSize
|
|
807
|
+
: undefined;
|
|
808
|
+
return [{
|
|
809
|
+
text: run.text,
|
|
810
|
+
...(typeof run.fontFamily === 'string' && run.fontFamily ? { fontFamily: run.fontFamily } : {}),
|
|
811
|
+
...(fontSize ? { fontSize } : {}),
|
|
812
|
+
...(typeof run.color === 'string' && run.color ? { color: run.color } : {}),
|
|
813
|
+
...(run.bold ? { bold: true } : {}),
|
|
814
|
+
...(run.italic ? { italic: true } : {}),
|
|
815
|
+
...(run.underline ? { underline: true } : {}),
|
|
816
|
+
...(run.strike ? { strike: true } : {}),
|
|
817
|
+
...(run.verticalAlign === 'sub' || run.verticalAlign === 'super'
|
|
818
|
+
? { verticalAlign: run.verticalAlign }
|
|
819
|
+
: {})
|
|
820
|
+
}];
|
|
821
|
+
});
|
|
822
|
+
return runs.length ? runs : undefined;
|
|
823
|
+
};
|
|
698
824
|
export const normalizeCellStyle = (meta) => {
|
|
825
|
+
var _a;
|
|
699
826
|
if (!meta) {
|
|
700
827
|
return undefined;
|
|
701
828
|
}
|
|
702
829
|
const style = meta.style || {};
|
|
703
830
|
const nextStyle = {};
|
|
831
|
+
const richText = normalizeRichTextRuns(meta.richText);
|
|
832
|
+
if (richText) {
|
|
833
|
+
nextStyle.richText = richText;
|
|
834
|
+
}
|
|
704
835
|
if (style.backgroundColor) {
|
|
705
836
|
nextStyle.backgroundColor = style.backgroundColor;
|
|
706
837
|
}
|
|
@@ -747,7 +878,7 @@ export const normalizeCellStyle = (meta) => {
|
|
|
747
878
|
if (hasClassName(meta.className, 'htShrink')) {
|
|
748
879
|
nextStyle.shrinkToFit = true;
|
|
749
880
|
}
|
|
750
|
-
if (!nextStyle.backgroundColor && !nextStyle.color && !nextStyle.font && !hasBorder(nextStyle) && !hasTextLayout(nextStyle)) {
|
|
881
|
+
if (!((_a = nextStyle.richText) === null || _a === void 0 ? void 0 : _a.length) && !nextStyle.backgroundColor && !nextStyle.color && !nextStyle.font && !hasBorder(nextStyle) && !hasTextLayout(nextStyle)) {
|
|
751
882
|
return undefined;
|
|
752
883
|
}
|
|
753
884
|
return nextStyle;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Range, WorkSheet } from 'styled-exceljs';
|
|
2
|
-
import type { CellMerge, SheetChartDefinition, SheetCellImage, SheetColumn, SheetModel, SheetStructure, SheetWindow } from '../type.js';
|
|
2
|
+
import type { CellMerge, SheetChartDefinition, SheetCellImage, SheetCellMetadata, SheetColumn, SheetModel, SheetStructure, SheetWindow } from '../type.js';
|
|
3
3
|
export declare const createAutoFitSampleRanges: (totalRowCount: number, totalColCount: number) => Range[];
|
|
4
4
|
export declare const defaults: {
|
|
5
5
|
rowHeight: number;
|
|
@@ -16,10 +16,6 @@ interface SheetSliceOptions {
|
|
|
16
16
|
interface SheetObjectOptions {
|
|
17
17
|
includeLayout?: boolean;
|
|
18
18
|
}
|
|
19
|
-
type SheetCellMeta = {
|
|
20
|
-
className?: string;
|
|
21
|
-
style: Record<string, string>;
|
|
22
|
-
};
|
|
23
19
|
export default class SheetJsModel implements SheetModel {
|
|
24
20
|
private readonly _ws;
|
|
25
21
|
private readonly _startRow;
|
|
@@ -48,7 +44,7 @@ export default class SheetJsModel implements SheetModel {
|
|
|
48
44
|
colWidth: number;
|
|
49
45
|
};
|
|
50
46
|
get data(): string[][];
|
|
51
|
-
get cell(): Record<string,
|
|
47
|
+
get cell(): Record<string, SheetCellMetadata>;
|
|
52
48
|
get merge(): CellMerge[];
|
|
53
49
|
get columns(): SheetColumn[];
|
|
54
50
|
get structure(): SheetStructure;
|