@file-viewer/renderer-spreadsheet 2.3.1 → 2.3.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.en.md +1 -0
- package/README.md +1 -0
- package/THIRD_PARTY_NOTICES.md +5 -0
- package/dist/spreadsheet/imageSource.d.ts +6 -0
- package/dist/spreadsheet/imageSource.js +136 -0
- package/dist/spreadsheet.js +13 -0
- package/dist/worker/sheet.worker.js +2 -2
- package/package.json +6 -4
package/README.en.md
CHANGED
|
@@ -33,6 +33,7 @@ const options = {
|
|
|
33
33
|
- Defaults to `options.spreadsheet.worker: 'auto'`: small files keep the main-thread compatibility path, while files at or above `options.spreadsheet.workerAutoThreshold` (1MB by default) automatically try the static worker.
|
|
34
34
|
- Can force the static worker with `options.spreadsheet.worker: true`, disable auto worker with `worker: false`, and override custom static paths with `options.spreadsheet.workerUrl`.
|
|
35
35
|
- Supports multiple sheets, horizontally scrollable sheet tabs, merged cells, row and column sizing, borders, fills, alignment, text color, Office 365 and WPS in-cell images, workbook drawing images, double-click image previews, the global zoom provider, and optional header drag column resizing.
|
|
36
|
+
- TIFF drawings in XLSX files load the MIT-licensed UTIF.js decoder only when needed and are converted to PNG in the browser. Ordinary spreadsheets do not load the TIFF decoder.
|
|
36
37
|
- Uses virtual rendering for interaction. Host components usually hide full-document print for spreadsheets to avoid printing only the current viewport.
|
|
37
38
|
|
|
38
39
|
## Offline Assets
|
package/README.md
CHANGED
|
@@ -33,6 +33,7 @@ const options = {
|
|
|
33
33
|
- 默认 `options.spreadsheet.worker: 'auto'`:小文件走主线程兼容路径,大文件达到 `options.spreadsheet.workerAutoThreshold`(默认 1MB)后自动尝试静态 Worker。
|
|
34
34
|
- 可以通过 `options.spreadsheet.worker: true` 强制启用静态 Worker,通过 `worker: false` 关闭自动启用;静态路径特殊时配置 `options.spreadsheet.workerUrl`。
|
|
35
35
|
- 支持多 sheet、横向滚动标签栏、合并单元格、列宽/行高、边框、填充、对齐、文本颜色、Office 365 / WPS 单元格内嵌图片、workbook drawing 图片、图片双击大图预览、统一缩放 provider 和可选表头拖拽调整列宽。
|
|
36
|
+
- XLSX drawing 中的 TIFF 图片会按需加载 MIT 许可的 UTIF.js,在浏览器内转换为 PNG;普通表格不会加载 TIFF 解码器。
|
|
36
37
|
- 表格使用虚拟渲染,适合交互预览;完整打印按钮通常由上层能力判断隐藏,避免只打印当前视口。
|
|
37
38
|
|
|
38
39
|
## 离线资产
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
- `styled-exceljs` supplies Apache-2.0 licensed XLS/XLSX workbook, style, drawing, and chart parsing.
|
|
4
|
+
- `UTIF.js` is loaded only when an embedded TIFF drawing needs browser conversion. It is MIT licensed; File Viewer bounds encoded bytes, dimensions, and decoded pixels before conversion.
|
|
5
|
+
- `e-virt-table`, `JSZip`, `@xmldom/xmldom`, and `tinycolor2` remain scoped to the on-demand spreadsheet renderer.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { SheetImage } from './worker/type.js';
|
|
2
|
+
export interface SpreadsheetImageSourceResolver {
|
|
3
|
+
resolve(image: Pick<SheetImage, 'src' | 'contentType'>): Promise<string>;
|
|
4
|
+
dispose(): void;
|
|
5
|
+
}
|
|
6
|
+
export declare const createSpreadsheetImageSourceResolver: (documentRef: Document) => SpreadsheetImageSourceResolver;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
const MAX_TIFF_SOURCE_BYTES = 32 * 1024 * 1024;
|
|
2
|
+
const MAX_TIFF_DIMENSION = 16384;
|
|
3
|
+
const MAX_TIFF_PIXELS = 24000000;
|
|
4
|
+
const getBase64Payload = (source) => {
|
|
5
|
+
const match = /^data:([^;,]*);base64,([\s\S]*)$/i.exec(source);
|
|
6
|
+
if (!match) {
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
return { mime: match[1].toLowerCase(), payload: match[2] };
|
|
10
|
+
};
|
|
11
|
+
const decodeBase64Head = (payload, atob) => {
|
|
12
|
+
try {
|
|
13
|
+
return atob(payload.slice(0, 12));
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return '';
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const hasTiffHeader = (binary) => binary.length >= 4 && ((binary.charCodeAt(0) === 0x49 &&
|
|
20
|
+
binary.charCodeAt(1) === 0x49 &&
|
|
21
|
+
binary.charCodeAt(2) === 0x2a &&
|
|
22
|
+
binary.charCodeAt(3) === 0x00) ||
|
|
23
|
+
(binary.charCodeAt(0) === 0x4d &&
|
|
24
|
+
binary.charCodeAt(1) === 0x4d &&
|
|
25
|
+
binary.charCodeAt(2) === 0x00 &&
|
|
26
|
+
binary.charCodeAt(3) === 0x2a));
|
|
27
|
+
const decodeBase64 = (payload, atob) => {
|
|
28
|
+
const estimatedBytes = Math.floor(payload.length * 3 / 4);
|
|
29
|
+
if (estimatedBytes > MAX_TIFF_SOURCE_BYTES) {
|
|
30
|
+
throw new Error('TIFF source exceeds the embedded-image safety limit.');
|
|
31
|
+
}
|
|
32
|
+
const binary = atob(payload);
|
|
33
|
+
if (binary.length > MAX_TIFF_SOURCE_BYTES) {
|
|
34
|
+
throw new Error('TIFF source exceeds the embedded-image safety limit.');
|
|
35
|
+
}
|
|
36
|
+
const bytes = new Uint8Array(binary.length);
|
|
37
|
+
for (let index = 0; index < binary.length; index += 1) {
|
|
38
|
+
bytes[index] = binary.charCodeAt(index);
|
|
39
|
+
}
|
|
40
|
+
return bytes;
|
|
41
|
+
};
|
|
42
|
+
const getTiffDimension = (ifd, tag, fallback) => {
|
|
43
|
+
const tagged = ifd[tag];
|
|
44
|
+
const value = Array.isArray(tagged) ? tagged[0] : tagged;
|
|
45
|
+
const resolved = Number(value !== null && value !== void 0 ? value : ifd[fallback]);
|
|
46
|
+
return Number.isFinite(resolved) ? resolved : 0;
|
|
47
|
+
};
|
|
48
|
+
const assertSafeTiffDimensions = (width, height) => {
|
|
49
|
+
if (!Number.isInteger(width) ||
|
|
50
|
+
!Number.isInteger(height) ||
|
|
51
|
+
width <= 0 ||
|
|
52
|
+
height <= 0 ||
|
|
53
|
+
width > MAX_TIFF_DIMENSION ||
|
|
54
|
+
height > MAX_TIFF_DIMENSION ||
|
|
55
|
+
width * height > MAX_TIFF_PIXELS) {
|
|
56
|
+
throw new Error('TIFF dimensions exceed the embedded-image safety limit.');
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
const loadUtif = async () => {
|
|
60
|
+
const imported = await import('utif');
|
|
61
|
+
return imported.default || imported;
|
|
62
|
+
};
|
|
63
|
+
export const createSpreadsheetImageSourceResolver = (documentRef) => {
|
|
64
|
+
const view = documentRef.defaultView;
|
|
65
|
+
const decodeAtob = (view === null || view === void 0 ? void 0 : view.atob.bind(view)) || globalThis.atob.bind(globalThis);
|
|
66
|
+
const urlApi = (view === null || view === void 0 ? void 0 : view.URL) || URL;
|
|
67
|
+
const cache = new Map();
|
|
68
|
+
const objectUrls = new Set();
|
|
69
|
+
let disposed = false;
|
|
70
|
+
const decodeTiff = async (source, payload) => {
|
|
71
|
+
const bytes = decodeBase64(payload, decodeAtob);
|
|
72
|
+
const input = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
73
|
+
const utif = await loadUtif();
|
|
74
|
+
const ifd = utif.decode(input)[0];
|
|
75
|
+
if (!ifd) {
|
|
76
|
+
throw new Error('TIFF image does not contain a decodable page.');
|
|
77
|
+
}
|
|
78
|
+
const encodedWidth = getTiffDimension(ifd, 't256', 'width');
|
|
79
|
+
const encodedHeight = getTiffDimension(ifd, 't257', 'height');
|
|
80
|
+
assertSafeTiffDimensions(encodedWidth, encodedHeight);
|
|
81
|
+
utif.decodeImage(input, ifd);
|
|
82
|
+
const width = getTiffDimension(ifd, 't256', 'width');
|
|
83
|
+
const height = getTiffDimension(ifd, 't257', 'height');
|
|
84
|
+
assertSafeTiffDimensions(width, height);
|
|
85
|
+
const rgba = utif.toRGBA8(ifd);
|
|
86
|
+
if (rgba.byteLength !== width * height * 4) {
|
|
87
|
+
throw new Error('TIFF decoder returned an unexpected pixel buffer.');
|
|
88
|
+
}
|
|
89
|
+
const canvas = documentRef.createElement('canvas');
|
|
90
|
+
canvas.width = width;
|
|
91
|
+
canvas.height = height;
|
|
92
|
+
const context = canvas.getContext('2d');
|
|
93
|
+
if (!context) {
|
|
94
|
+
throw new Error('Canvas 2D is unavailable for TIFF conversion.');
|
|
95
|
+
}
|
|
96
|
+
const imageData = context.createImageData(width, height);
|
|
97
|
+
imageData.data.set(rgba);
|
|
98
|
+
context.putImageData(imageData, 0, 0);
|
|
99
|
+
const blob = await new Promise((resolve) => canvas.toBlob(resolve, 'image/png'));
|
|
100
|
+
if (!blob) {
|
|
101
|
+
throw new Error('Unable to encode the TIFF preview as PNG.');
|
|
102
|
+
}
|
|
103
|
+
const objectUrl = urlApi.createObjectURL(blob);
|
|
104
|
+
if (disposed) {
|
|
105
|
+
urlApi.revokeObjectURL(objectUrl);
|
|
106
|
+
return source;
|
|
107
|
+
}
|
|
108
|
+
objectUrls.add(objectUrl);
|
|
109
|
+
return objectUrl;
|
|
110
|
+
};
|
|
111
|
+
return {
|
|
112
|
+
resolve(image) {
|
|
113
|
+
const parsed = getBase64Payload(image.src);
|
|
114
|
+
if (!parsed) {
|
|
115
|
+
return Promise.resolve(image.src);
|
|
116
|
+
}
|
|
117
|
+
const declaredTiff = /image\/(?:tif|tiff)/i.test(image.contentType || parsed.mime);
|
|
118
|
+
const sniffedTiff = hasTiffHeader(decodeBase64Head(parsed.payload, decodeAtob));
|
|
119
|
+
if (!declaredTiff && !sniffedTiff) {
|
|
120
|
+
return Promise.resolve(image.src);
|
|
121
|
+
}
|
|
122
|
+
let pending = cache.get(image.src);
|
|
123
|
+
if (!pending) {
|
|
124
|
+
pending = decodeTiff(image.src, parsed.payload).catch(() => image.src);
|
|
125
|
+
cache.set(image.src, pending);
|
|
126
|
+
}
|
|
127
|
+
return pending;
|
|
128
|
+
},
|
|
129
|
+
dispose() {
|
|
130
|
+
disposed = true;
|
|
131
|
+
objectUrls.forEach(url => urlApi.revokeObjectURL(url));
|
|
132
|
+
objectUrls.clear();
|
|
133
|
+
cache.clear();
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
};
|
package/dist/spreadsheet.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createFileViewerWorkerController, createFileViewerTranslator, createFileViewerZoomChangeEmitter as createZoomChangeEmitter, registerFileViewerZoomProvider, resolveFileViewerColorScheme, resolveFileViewerFitScale, resolveFileViewerRuntimeAssetBaseUrl, resolveFileViewerSpreadsheetWorkerUrl, unregisterFileViewerZoomProvider, } from '@file-viewer/core';
|
|
2
2
|
import { renderSpreadsheetChart } from './spreadsheet/chartRenderer.js';
|
|
3
|
+
import { createSpreadsheetImageSourceResolver } from './spreadsheet/imageSource.js';
|
|
3
4
|
import { buildRows, clampWindowStart, collectWindowStarts, createEmptyVirtualState, DEFAULT_SHEET_DEFAULTS, displayCellKey, getDataKey, INDEX_COLUMN_KEY, markWindowState, ROW_STATE_FIELD, RowState, WINDOW_SIZE, } from './spreadsheet/state.js';
|
|
4
5
|
import { buildColumns, createTableConfig, detectIndexOffset, getDisplayColumns, getRowHeight, HEADER_HEIGHT, INDEX_COLUMN_WIDTH, normalizeCellStyle, normalizeRowHeight, RESIZABLE_COLUMN_MIN_WIDTH, RESIZABLE_ROW_MIN_HEIGHT, SPREADSHEET_MAX_ZOOM, SPREADSHEET_MIN_ZOOM, } from './spreadsheet/view.js';
|
|
5
6
|
const EXCEL_IMAGE_SCROLLBAR_GUARD = 18;
|
|
@@ -661,6 +662,7 @@ const renderFileViewerSpreadsheet = async (buffer, target, type, context) => {
|
|
|
661
662
|
let lastScrollY = 0;
|
|
662
663
|
let sheetSessionId = 0;
|
|
663
664
|
let disposed = false;
|
|
665
|
+
const imageSourceResolver = createSpreadsheetImageSourceResolver(documentRef);
|
|
664
666
|
let imageLightboxPreviousFocus = null;
|
|
665
667
|
let hasNotifiedFirstPaint = false;
|
|
666
668
|
let hasAppliedDefaultInitialFit = false;
|
|
@@ -698,6 +700,11 @@ const renderFileViewerSpreadsheet = async (buffer, target, type, context) => {
|
|
|
698
700
|
? documentRef.activeElement
|
|
699
701
|
: null;
|
|
700
702
|
lightboxImage.src = image.src;
|
|
703
|
+
void imageSourceResolver.resolve(image).then((source) => {
|
|
704
|
+
if (!disposed && imageLightbox.dataset.imageId === image.id) {
|
|
705
|
+
lightboxImage.src = source;
|
|
706
|
+
}
|
|
707
|
+
});
|
|
701
708
|
imageLightbox.dataset.imageId = image.id;
|
|
702
709
|
imageLightbox.dataset.open = 'true';
|
|
703
710
|
imageLightbox.setAttribute('aria-hidden', 'false');
|
|
@@ -852,6 +859,11 @@ const renderFileViewerSpreadsheet = async (buffer, target, type, context) => {
|
|
|
852
859
|
height: `${scalePx(image.height)}px`,
|
|
853
860
|
});
|
|
854
861
|
element.dataset.imageIndex = `${index}`;
|
|
862
|
+
void imageSourceResolver.resolve(image).then((source) => {
|
|
863
|
+
if (!disposed && element.isConnected && element.src !== source) {
|
|
864
|
+
element.src = source;
|
|
865
|
+
}
|
|
866
|
+
});
|
|
855
867
|
return element;
|
|
856
868
|
});
|
|
857
869
|
const chartElements = visibleCharts.map((chart) => {
|
|
@@ -1716,6 +1728,7 @@ const renderFileViewerSpreadsheet = async (buffer, target, type, context) => {
|
|
|
1716
1728
|
unmount() {
|
|
1717
1729
|
var _a, _b;
|
|
1718
1730
|
disposed = true;
|
|
1731
|
+
imageSourceResolver.dispose();
|
|
1719
1732
|
closeImageLightbox();
|
|
1720
1733
|
tableHostShell.removeEventListener('dblclick', handleImageDoubleClick, true);
|
|
1721
1734
|
imageLightbox.removeEventListener('click', handleImageLightboxClick);
|