@file-viewer/renderer-spreadsheet 2.1.29 → 2.2.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/README.en.md +3 -2
- package/README.md +3 -2
- package/dist/spreadsheet/view.d.ts +3 -2
- package/dist/spreadsheet/view.js +27 -30
- package/dist/spreadsheet/worker/sheetjs/index.d.ts +1 -0
- package/dist/spreadsheet/worker/sheetjs/index.js +1 -0
- package/dist/spreadsheet/worker/sheetjs/parser.d.ts +2 -1
- package/dist/spreadsheet/worker/sheetjs/parser.js +12 -4
- package/dist/spreadsheet/worker/sheetjs/textEncoding.d.ts +22 -0
- package/dist/spreadsheet/worker/sheetjs/textEncoding.js +153 -0
- package/dist/spreadsheet.d.ts +1 -1
- package/dist/spreadsheet.js +35 -19
- package/dist/worker/sheet.worker.js +91 -0
- package/package.json +6 -3
package/README.en.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @file-viewer/renderer-spreadsheet
|
|
2
2
|
|
|
3
|
-
Standalone spreadsheet renderer package for Flyfish File Viewer. It is powered by `styled-exceljs` and `e-virt-table`, previews XLSX, XLSM, XLSB, XLS, CSV, ODS, FODS, Numbers, and compatible spreadsheet files in the browser, and resolves the optional worker URL through the File Viewer asset manifest for offline enterprise deployments.
|
|
3
|
+
Standalone spreadsheet renderer package for Flyfish File Viewer. It is powered by `styled-exceljs` and `e-virt-table`, previews XLSX, XLSM, XLSB, XLS, CSV, TSV, ODS, FODS, Numbers, and compatible spreadsheet files in the browser, and resolves the optional worker URL through the File Viewer asset manifest for offline enterprise deployments.
|
|
4
4
|
|
|
5
5
|
## Usage
|
|
6
6
|
|
|
@@ -28,7 +28,8 @@ const options = {
|
|
|
28
28
|
|
|
29
29
|
## Capabilities
|
|
30
30
|
|
|
31
|
-
- Supports `.xlsx`, `.xltx`, `.xlsm`, `.xlsb`, `.xls`, `.xlt`, `.xltm`, `.csv`, `.ods`, `.fods`, and `.numbers`.
|
|
31
|
+
- Supports `.xlsx`, `.xltx`, `.xlsm`, `.xlsb`, `.xls`, `.xlt`, `.xltm`, `.csv`, `.tsv`, `.ods`, `.fods`, and `.numbers`.
|
|
32
|
+
- CSV / TSV automatically detects UTF-8 (with or without BOM) and GBK / GB18030. For known inputs, set `options.spreadsheet.textEncoding` to `utf-8`, `gbk`, or `gb18030`.
|
|
32
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.
|
|
33
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`.
|
|
34
35
|
- Supports multiple sheets, horizontally scrollable sheet tabs, merged cells, row and column sizing, borders, fills, alignment, text color, workbook images, the global zoom provider, and optional header drag column resizing.
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @file-viewer/renderer-spreadsheet
|
|
2
2
|
|
|
3
|
-
Flyfish File Viewer 的独立表格 renderer 包。它基于 `styled-exceljs` 与 `e-virt-table` 提供 XLSX、XLSM、XLSB、XLS、CSV、ODS、FODS、Numbers 等表格文件的浏览器端预览,并通过 File Viewer 统一的 asset manifest 解析可选 Worker 路径,适合企业内网和离线部署。
|
|
3
|
+
Flyfish File Viewer 的独立表格 renderer 包。它基于 `styled-exceljs` 与 `e-virt-table` 提供 XLSX、XLSM、XLSB、XLS、CSV、TSV、ODS、FODS、Numbers 等表格文件的浏览器端预览,并通过 File Viewer 统一的 asset manifest 解析可选 Worker 路径,适合企业内网和离线部署。
|
|
4
4
|
|
|
5
5
|
## 用法
|
|
6
6
|
|
|
@@ -28,7 +28,8 @@ const options = {
|
|
|
28
28
|
|
|
29
29
|
## 能力边界
|
|
30
30
|
|
|
31
|
-
- 支持 `.xlsx`、`.xltx`、`.xlsm`、`.xlsb`、`.xls`、`.xlt`、`.xltm`、`.csv`、`.ods`、`.fods`、`.numbers`。
|
|
31
|
+
- 支持 `.xlsx`、`.xltx`、`.xlsm`、`.xlsb`、`.xls`、`.xlt`、`.xltm`、`.csv`、`.tsv`、`.ods`、`.fods`、`.numbers`。
|
|
32
|
+
- CSV / TSV 默认自动识别 UTF-8(含 BOM)与 GBK / GB18030;遇到来源编码已知的文件时,可通过 `options.spreadsheet.textEncoding` 固定为 `utf-8`、`gbk` 或 `gb18030`。
|
|
32
33
|
- 默认 `options.spreadsheet.worker: 'auto'`:小文件走主线程兼容路径,大文件达到 `options.spreadsheet.workerAutoThreshold`(默认 1MB)后自动尝试静态 Worker。
|
|
33
34
|
- 可以通过 `options.spreadsheet.worker: true` 强制启用静态 Worker,通过 `worker: false` 关闭自动启用;静态路径特殊时配置 `options.spreadsheet.workerUrl`。
|
|
34
35
|
- 支持多 sheet、横向滚动标签栏、合并单元格、列宽/行高、边框、填充、对齐、文本颜色、图片覆盖层、统一缩放 provider 和可选表头拖拽调整列宽。
|
|
@@ -7,6 +7,7 @@ export declare const RESIZABLE_COLUMN_MIN_WIDTH = 40;
|
|
|
7
7
|
export declare const RESIZABLE_ROW_MIN_HEIGHT = 18;
|
|
8
8
|
interface TableConfigOptions {
|
|
9
9
|
hostHeight: number;
|
|
10
|
+
darkMode?: boolean;
|
|
10
11
|
resizableColumns?: boolean;
|
|
11
12
|
resizableRows?: boolean;
|
|
12
13
|
copySelection?: (params: SpreadsheetCopyParams) => void;
|
|
@@ -22,7 +23,7 @@ interface SpreadsheetCopyParams {
|
|
|
22
23
|
}
|
|
23
24
|
export declare const getRowHeight: (heights: number | number[] | undefined, index: number, fallback: number) => number;
|
|
24
25
|
export declare const normalizeRowHeight: (height: number | undefined, fallback: number) => number;
|
|
25
|
-
export declare const detectIndexOffset: (ws: SheetModel) =>
|
|
26
|
+
export declare const detectIndexOffset: (ws: SheetModel) => 0 | 1;
|
|
26
27
|
export declare const buildColumns: (ws: SheetModel) => {
|
|
27
28
|
columns: Column[];
|
|
28
29
|
dataKeys: string[];
|
|
@@ -32,5 +33,5 @@ export declare const normalizeCellStyle: (meta: {
|
|
|
32
33
|
className?: string;
|
|
33
34
|
style: any;
|
|
34
35
|
} | undefined) => CellStyleCache | undefined;
|
|
35
|
-
export declare const createTableConfig: ({ hostHeight, resizableColumns, resizableRows, copySelection, sheetDefaults, virtualState, zoomScale }: TableConfigOptions) => ConfigType;
|
|
36
|
+
export declare const createTableConfig: ({ hostHeight, darkMode, resizableColumns, resizableRows, copySelection, sheetDefaults, virtualState, zoomScale }: TableConfigOptions) => ConfigType;
|
|
36
37
|
export {};
|
package/dist/spreadsheet/view.js
CHANGED
|
@@ -13,10 +13,15 @@ export const RESIZABLE_ROW_MIN_HEIGHT = 18;
|
|
|
13
13
|
const EXCEL_HEADER_BG = '#f3f3f3';
|
|
14
14
|
const EXCEL_HEADER_TEXT = '#5f6368';
|
|
15
15
|
const EXCEL_GRID = '#d7dbe0';
|
|
16
|
+
const EXCEL_DARK_HEADER_BG = '#1f2937';
|
|
17
|
+
const EXCEL_DARK_HEADER_TEXT = '#cbd5e1';
|
|
18
|
+
const EXCEL_DARK_GRID = '#374151';
|
|
19
|
+
const EXCEL_DARK_BODY_BG = '#111827';
|
|
20
|
+
const EXCEL_DARK_BODY_TEXT = '#e5e7eb';
|
|
16
21
|
const EXCEL_GREEN = '#21a366';
|
|
17
22
|
const EXCEL_GREEN_SOFT = 'rgba(33, 163, 102, 0.1)';
|
|
18
23
|
const DATA_AREA_MIN_COL_INDEX = 1;
|
|
19
|
-
const
|
|
24
|
+
const _BORDER_SIDE_KEYS = ['Top', 'Right', 'Bottom', 'Left'];
|
|
20
25
|
const DEFAULT_BORDER_COLOR = '#000000';
|
|
21
26
|
const BORDER_STYLE_PRIORITY = {
|
|
22
27
|
double: 8,
|
|
@@ -27,16 +32,6 @@ const BORDER_STYLE_PRIORITY = {
|
|
|
27
32
|
const HEADER_FONT = `bold 12px ${TABLE_FONT_FAMILY}`;
|
|
28
33
|
const BODY_FONT = `${TABLE_FONT_SIZE}px ${TABLE_FONT_FAMILY}`;
|
|
29
34
|
const MAX_OVERFLOW_SCAN_COLS = 32;
|
|
30
|
-
const INDEX_CELL_STYLE = {
|
|
31
|
-
backgroundColor: EXCEL_HEADER_BG,
|
|
32
|
-
color: EXCEL_HEADER_TEXT,
|
|
33
|
-
font: HEADER_FONT
|
|
34
|
-
};
|
|
35
|
-
const LOADING_CELL_STYLE = {
|
|
36
|
-
backgroundColor: '#f4f7f9',
|
|
37
|
-
color: '#73808d',
|
|
38
|
-
font: `italic ${TABLE_FONT_SIZE}px ${TABLE_FONT_FAMILY}`
|
|
39
|
-
};
|
|
40
35
|
let measureCanvas;
|
|
41
36
|
const textWidthCache = new Map();
|
|
42
37
|
const normalizeZoomScale = (scale = 1) => {
|
|
@@ -78,13 +73,15 @@ const scaleCellStyle = (style, zoomScale) => {
|
|
|
78
73
|
};
|
|
79
74
|
const getHeaderFont = (zoomScale) => scaleFont(HEADER_FONT, zoomScale) || HEADER_FONT;
|
|
80
75
|
const getBodyFont = (zoomScale) => scaleFont(BODY_FONT, zoomScale) || BODY_FONT;
|
|
81
|
-
const getIndexCellStyle = (zoomScale) => ({
|
|
82
|
-
|
|
76
|
+
const getIndexCellStyle = (zoomScale, darkMode = false) => ({
|
|
77
|
+
backgroundColor: darkMode ? EXCEL_DARK_HEADER_BG : EXCEL_HEADER_BG,
|
|
78
|
+
color: darkMode ? EXCEL_DARK_HEADER_TEXT : EXCEL_HEADER_TEXT,
|
|
83
79
|
font: getHeaderFont(zoomScale)
|
|
84
80
|
});
|
|
85
|
-
const getLoadingCellStyle = (zoomScale) => ({
|
|
86
|
-
|
|
87
|
-
|
|
81
|
+
const getLoadingCellStyle = (zoomScale, darkMode = false) => ({
|
|
82
|
+
backgroundColor: darkMode ? '#18212f' : '#f4f7f9',
|
|
83
|
+
color: darkMode ? '#94a3b8' : '#73808d',
|
|
84
|
+
font: scaleFont(`italic ${TABLE_FONT_SIZE}px ${TABLE_FONT_FAMILY}`, zoomScale)
|
|
88
85
|
});
|
|
89
86
|
const cloneColumns = (columns) => {
|
|
90
87
|
return columns.map(column => {
|
|
@@ -751,14 +748,14 @@ export const normalizeCellStyle = (meta) => {
|
|
|
751
748
|
}
|
|
752
749
|
return nextStyle;
|
|
753
750
|
};
|
|
754
|
-
export const createTableConfig = ({ hostHeight, resizableColumns = false, resizableRows = false, copySelection, sheetDefaults, virtualState, zoomScale = 1 }) => {
|
|
751
|
+
export const createTableConfig = ({ hostHeight, darkMode = false, resizableColumns = false, resizableRows = false, copySelection, sheetDefaults, virtualState, zoomScale = 1 }) => {
|
|
755
752
|
const normalizedScale = normalizeZoomScale(zoomScale);
|
|
756
753
|
const scaledPadding = scaleNumber(CELL_PADDING, normalizedScale);
|
|
757
754
|
const scaledHeaderHeight = scaleNumber(HEADER_HEIGHT, normalizedScale);
|
|
758
755
|
const scaledCellHeight = scaleNumber(normalizeRowHeight(sheetDefaults.rowHeight, sheetDefaults.rowHeight), normalizedScale);
|
|
759
756
|
const scaledCellWidth = scaleNumber(sheetDefaults.colWidth, normalizedScale);
|
|
760
|
-
const headerStyle = getIndexCellStyle(normalizedScale);
|
|
761
|
-
const loadingStyle = getLoadingCellStyle(normalizedScale);
|
|
757
|
+
const headerStyle = getIndexCellStyle(normalizedScale, darkMode);
|
|
758
|
+
const loadingStyle = getLoadingCellStyle(normalizedScale, darkMode);
|
|
762
759
|
const spanMethod = ({ rowIndex, colIndex, column }) => {
|
|
763
760
|
if (colIndex === 0) {
|
|
764
761
|
return;
|
|
@@ -836,17 +833,17 @@ export const createTableConfig = ({ hostHeight, resizableColumns = false, resiza
|
|
|
836
833
|
HEADER_FONT: getHeaderFont(normalizedScale),
|
|
837
834
|
BODY_FONT: getBodyFont(normalizedScale),
|
|
838
835
|
BORDER_RADIUS: 0,
|
|
839
|
-
BORDER_COLOR: EXCEL_GRID,
|
|
840
|
-
HEADER_BG_COLOR: EXCEL_HEADER_BG,
|
|
841
|
-
BODY_BG_COLOR: '#ffffff',
|
|
842
|
-
HEADER_TEXT_COLOR: EXCEL_HEADER_TEXT,
|
|
843
|
-
BODY_TEXT_COLOR: '#202124',
|
|
844
|
-
READONLY_COLOR: '#ffffff',
|
|
845
|
-
READONLY_TEXT_COLOR: '#202124',
|
|
846
|
-
EDIT_BG_COLOR: '#ffffff',
|
|
847
|
-
PLACEHOLDER_COLOR: '#8a94a3',
|
|
848
|
-
SCROLLER_COLOR: '#c1c7d0',
|
|
849
|
-
SCROLLER_FOCUS_COLOR: '#9aa0a6',
|
|
836
|
+
BORDER_COLOR: darkMode ? EXCEL_DARK_GRID : EXCEL_GRID,
|
|
837
|
+
HEADER_BG_COLOR: darkMode ? EXCEL_DARK_HEADER_BG : EXCEL_HEADER_BG,
|
|
838
|
+
BODY_BG_COLOR: darkMode ? EXCEL_DARK_BODY_BG : '#ffffff',
|
|
839
|
+
HEADER_TEXT_COLOR: darkMode ? EXCEL_DARK_HEADER_TEXT : EXCEL_HEADER_TEXT,
|
|
840
|
+
BODY_TEXT_COLOR: darkMode ? EXCEL_DARK_BODY_TEXT : '#202124',
|
|
841
|
+
READONLY_COLOR: darkMode ? EXCEL_DARK_BODY_BG : '#ffffff',
|
|
842
|
+
READONLY_TEXT_COLOR: darkMode ? EXCEL_DARK_BODY_TEXT : '#202124',
|
|
843
|
+
EDIT_BG_COLOR: darkMode ? '#18212f' : '#ffffff',
|
|
844
|
+
PLACEHOLDER_COLOR: darkMode ? '#94a3b8' : '#8a94a3',
|
|
845
|
+
SCROLLER_COLOR: darkMode ? '#475569' : '#c1c7d0',
|
|
846
|
+
SCROLLER_FOCUS_COLOR: darkMode ? '#64748b' : '#9aa0a6',
|
|
850
847
|
SELECT_ROW_COL_BG_COLOR: EXCEL_GREEN_SOFT,
|
|
851
848
|
SELECT_AREA_COLOR: 'rgba(33, 163, 102, 0.14)',
|
|
852
849
|
SELECT_BORDER_COLOR: EXCEL_GREEN,
|
|
@@ -1 +1,2 @@
|
|
|
1
1
|
export { createSpreadsheetParserContext, handleSpreadsheetWorkerRequest } from './parser.js';
|
|
2
|
+
export { decodeSpreadsheetText, isTextSpreadsheetSource, isValidUtf8, prepareSpreadsheetReadInput, type DecodedSpreadsheetText, type PreparedSpreadsheetReadInput, type SpreadsheetTextEncoding, type SpreadsheetTextSource, } from './textEncoding.js';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { WorkBook } from 'styled-exceljs';
|
|
2
|
+
import { type SpreadsheetTextSource } from './textEncoding.js';
|
|
2
3
|
import type { SheetChartDefinition, SheetDefinition } from '../type.js';
|
|
3
4
|
export interface SpreadsheetParserContext {
|
|
4
5
|
workbook: WorkBook | null;
|
|
@@ -14,6 +15,6 @@ export interface SpreadsheetWorkerResponse {
|
|
|
14
15
|
payload?: Record<string, any>;
|
|
15
16
|
}
|
|
16
17
|
export declare const createSpreadsheetParserContext: () => SpreadsheetParserContext;
|
|
17
|
-
export declare const parseSpreadsheetWorkbook: (context: SpreadsheetParserContext, data: ArrayBuffer) => Promise<SpreadsheetWorkerResponse[]>;
|
|
18
|
+
export declare const parseSpreadsheetWorkbook: (context: SpreadsheetParserContext, data: ArrayBuffer, source?: SpreadsheetTextSource) => Promise<SpreadsheetWorkerResponse[]>;
|
|
18
19
|
export declare const parseSpreadsheetSheet: (context: SpreadsheetParserContext, payload?: Record<string, any>) => SpreadsheetWorkerResponse[];
|
|
19
20
|
export declare const handleSpreadsheetWorkerRequest: (context: SpreadsheetParserContext, request: SpreadsheetWorkerRequest) => SpreadsheetWorkerResponse[] | Promise<SpreadsheetWorkerResponse[]>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { read, utils } from 'styled-exceljs';
|
|
2
2
|
import SheetJsModel from './SheetJsModel.js';
|
|
3
3
|
import { parseSpreadsheetCharts } from './chartParser.js';
|
|
4
|
+
import { prepareSpreadsheetReadInput, } from './textEncoding.js';
|
|
4
5
|
const readOptions = {
|
|
5
6
|
type: 'array',
|
|
6
7
|
dense: true,
|
|
@@ -87,9 +88,12 @@ const parseSheets = (context) => {
|
|
|
87
88
|
}, []);
|
|
88
89
|
return [{ type: 'sheets', payload: { sheets: context.sheets } }];
|
|
89
90
|
};
|
|
90
|
-
export const parseSpreadsheetWorkbook = async (context, data) => {
|
|
91
|
+
export const parseSpreadsheetWorkbook = async (context, data, source = {}) => {
|
|
91
92
|
try {
|
|
92
|
-
|
|
93
|
+
const input = prepareSpreadsheetReadInput(data, source);
|
|
94
|
+
context.workbook = input.kind === 'text'
|
|
95
|
+
? read(input.data, { ...readOptions, type: 'string' })
|
|
96
|
+
: read(input.data, readOptions);
|
|
93
97
|
const signature = data.byteLength >= 2 ? new DataView(data).getUint16(0, false) : 0;
|
|
94
98
|
if (signature === 0x504b) {
|
|
95
99
|
try {
|
|
@@ -149,10 +153,14 @@ export const parseSpreadsheetSheet = (context, payload = {}) => {
|
|
|
149
153
|
}
|
|
150
154
|
};
|
|
151
155
|
export const handleSpreadsheetWorkerRequest = (context, request) => {
|
|
152
|
-
var _a;
|
|
156
|
+
var _a, _b, _c, _d;
|
|
153
157
|
switch (request.type) {
|
|
154
158
|
case 'parseWorkbook':
|
|
155
|
-
return parseSpreadsheetWorkbook(context, (_a = request.payload) === null || _a === void 0 ? void 0 : _a.workbook
|
|
159
|
+
return parseSpreadsheetWorkbook(context, (_a = request.payload) === null || _a === void 0 ? void 0 : _a.workbook, {
|
|
160
|
+
fileType: (_b = request.payload) === null || _b === void 0 ? void 0 : _b.fileType,
|
|
161
|
+
filename: (_c = request.payload) === null || _c === void 0 ? void 0 : _c.filename,
|
|
162
|
+
textEncoding: (_d = request.payload) === null || _d === void 0 ? void 0 : _d.textEncoding,
|
|
163
|
+
});
|
|
156
164
|
case 'parseSheet':
|
|
157
165
|
return parseSpreadsheetSheet(context, request.payload);
|
|
158
166
|
default:
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type SpreadsheetTextEncoding = 'auto' | 'utf-8' | 'gbk' | 'gb18030';
|
|
2
|
+
export interface SpreadsheetTextSource {
|
|
3
|
+
fileType?: string;
|
|
4
|
+
filename?: string;
|
|
5
|
+
textEncoding?: SpreadsheetTextEncoding;
|
|
6
|
+
}
|
|
7
|
+
export interface DecodedSpreadsheetText {
|
|
8
|
+
text: string;
|
|
9
|
+
encoding: 'utf-8' | 'gb18030';
|
|
10
|
+
}
|
|
11
|
+
export type PreparedSpreadsheetReadInput = {
|
|
12
|
+
kind: 'binary';
|
|
13
|
+
data: ArrayBuffer;
|
|
14
|
+
} | {
|
|
15
|
+
kind: 'text';
|
|
16
|
+
data: string;
|
|
17
|
+
encoding: DecodedSpreadsheetText['encoding'];
|
|
18
|
+
};
|
|
19
|
+
export declare const isTextSpreadsheetSource: ({ fileType, filename, }: Pick<SpreadsheetTextSource, "fileType" | "filename">) => boolean;
|
|
20
|
+
export declare const isValidUtf8: (bytes: Uint8Array) => boolean;
|
|
21
|
+
export declare const decodeSpreadsheetText: (data: ArrayBuffer, encoding?: SpreadsheetTextEncoding) => DecodedSpreadsheetText;
|
|
22
|
+
export declare const prepareSpreadsheetReadInput: (data: ArrayBuffer, source?: SpreadsheetTextSource) => PreparedSpreadsheetReadInput;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
const TEXT_SPREADSHEET_EXTENSIONS = new Set(['csv', 'tsv']);
|
|
2
|
+
const TEXT_SPREADSHEET_MIME_TYPES = new Set([
|
|
3
|
+
'text/csv',
|
|
4
|
+
'text/tab-separated-values',
|
|
5
|
+
]);
|
|
6
|
+
const normalizeFileType = (value) => {
|
|
7
|
+
return String(value || '')
|
|
8
|
+
.trim()
|
|
9
|
+
.toLowerCase()
|
|
10
|
+
.replace(/^\./, '')
|
|
11
|
+
.split(/[?#;]/, 1)[0];
|
|
12
|
+
};
|
|
13
|
+
const getFilenameExtension = (filename) => {
|
|
14
|
+
const clean = String(filename || '').trim().toLowerCase().split(/[?#]/, 1)[0];
|
|
15
|
+
const slash = Math.max(clean.lastIndexOf('/'), clean.lastIndexOf('\\'));
|
|
16
|
+
const dot = clean.lastIndexOf('.');
|
|
17
|
+
return dot > slash ? clean.slice(dot + 1) : '';
|
|
18
|
+
};
|
|
19
|
+
export const isTextSpreadsheetSource = ({ fileType, filename, }) => {
|
|
20
|
+
const normalizedType = normalizeFileType(fileType);
|
|
21
|
+
if (normalizedType) {
|
|
22
|
+
return TEXT_SPREADSHEET_EXTENSIONS.has(normalizedType) ||
|
|
23
|
+
TEXT_SPREADSHEET_MIME_TYPES.has(normalizedType);
|
|
24
|
+
}
|
|
25
|
+
return TEXT_SPREADSHEET_EXTENSIONS.has(getFilenameExtension(filename));
|
|
26
|
+
};
|
|
27
|
+
const decodeBytes = (bytes, encoding) => {
|
|
28
|
+
if (typeof TextDecoder === 'undefined') {
|
|
29
|
+
throw new Error('Spreadsheet text decoding requires the browser TextDecoder API.');
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
return new TextDecoder(encoding).decode(bytes);
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (encoding === 'gb18030' && error instanceof RangeError) {
|
|
36
|
+
throw new Error('This browser does not provide GB18030 text decoding. Use a current browser or set spreadsheet.textEncoding to "utf-8".');
|
|
37
|
+
}
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const hasUtf8Bom = (bytes) => {
|
|
42
|
+
return bytes.length >= 3 &&
|
|
43
|
+
bytes[0] === 0xef &&
|
|
44
|
+
bytes[1] === 0xbb &&
|
|
45
|
+
bytes[2] === 0xbf;
|
|
46
|
+
};
|
|
47
|
+
const isContinuationByte = (value) => {
|
|
48
|
+
return value !== undefined && value >= 0x80 && value <= 0xbf;
|
|
49
|
+
};
|
|
50
|
+
// Validate bytes directly instead of relying on TextDecoder({ fatal: true }).
|
|
51
|
+
// Some older WebViews accept TextDecoder but ignore the fatal option, which
|
|
52
|
+
// would turn GBK into replacement characters before the GB18030 fallback runs.
|
|
53
|
+
export const isValidUtf8 = (bytes) => {
|
|
54
|
+
for (let index = 0; index < bytes.length;) {
|
|
55
|
+
const first = bytes[index];
|
|
56
|
+
if (first === undefined) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if (first <= 0x7f) {
|
|
60
|
+
index += 1;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const second = bytes[index + 1];
|
|
64
|
+
const third = bytes[index + 2];
|
|
65
|
+
const fourth = bytes[index + 3];
|
|
66
|
+
if (first >= 0xc2 && first <= 0xdf && isContinuationByte(second)) {
|
|
67
|
+
index += 2;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (first === 0xe0 &&
|
|
71
|
+
second !== undefined && second >= 0xa0 && second <= 0xbf &&
|
|
72
|
+
isContinuationByte(third)) {
|
|
73
|
+
index += 3;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (((first >= 0xe1 && first <= 0xec) || (first >= 0xee && first <= 0xef)) &&
|
|
77
|
+
isContinuationByte(second) &&
|
|
78
|
+
isContinuationByte(third)) {
|
|
79
|
+
index += 3;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (first === 0xed &&
|
|
83
|
+
second !== undefined && second >= 0x80 && second <= 0x9f &&
|
|
84
|
+
isContinuationByte(third)) {
|
|
85
|
+
index += 3;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (first === 0xf0 &&
|
|
89
|
+
second !== undefined && second >= 0x90 && second <= 0xbf &&
|
|
90
|
+
isContinuationByte(third) &&
|
|
91
|
+
isContinuationByte(fourth)) {
|
|
92
|
+
index += 4;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (first >= 0xf1 && first <= 0xf3 &&
|
|
96
|
+
isContinuationByte(second) &&
|
|
97
|
+
isContinuationByte(third) &&
|
|
98
|
+
isContinuationByte(fourth)) {
|
|
99
|
+
index += 4;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (first === 0xf4 &&
|
|
103
|
+
second !== undefined && second >= 0x80 && second <= 0x8f &&
|
|
104
|
+
isContinuationByte(third) &&
|
|
105
|
+
isContinuationByte(fourth)) {
|
|
106
|
+
index += 4;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
return true;
|
|
112
|
+
};
|
|
113
|
+
const normalizeTextEncoding = (encoding) => {
|
|
114
|
+
const normalized = String(encoding || 'auto').trim().toLowerCase().replace('_', '-');
|
|
115
|
+
if (normalized === 'utf8' || normalized === 'utf-8') {
|
|
116
|
+
return 'utf-8';
|
|
117
|
+
}
|
|
118
|
+
if (normalized === 'gbk' || normalized === 'cp936' || normalized === 'gb2312') {
|
|
119
|
+
return 'gbk';
|
|
120
|
+
}
|
|
121
|
+
if (normalized === 'gb18030') {
|
|
122
|
+
return 'gb18030';
|
|
123
|
+
}
|
|
124
|
+
return 'auto';
|
|
125
|
+
};
|
|
126
|
+
export const decodeSpreadsheetText = (data, encoding = 'auto') => {
|
|
127
|
+
const bytes = new Uint8Array(data);
|
|
128
|
+
const normalizedEncoding = normalizeTextEncoding(encoding);
|
|
129
|
+
if (normalizedEncoding === 'utf-8') {
|
|
130
|
+
const content = hasUtf8Bom(bytes) ? bytes.subarray(3) : bytes;
|
|
131
|
+
return { text: decodeBytes(content, 'utf-8'), encoding: 'utf-8' };
|
|
132
|
+
}
|
|
133
|
+
if (normalizedEncoding === 'gbk' || normalizedEncoding === 'gb18030') {
|
|
134
|
+
return { text: decodeBytes(bytes, 'gb18030'), encoding: 'gb18030' };
|
|
135
|
+
}
|
|
136
|
+
if (hasUtf8Bom(bytes)) {
|
|
137
|
+
return { text: decodeBytes(bytes.subarray(3), 'utf-8'), encoding: 'utf-8' };
|
|
138
|
+
}
|
|
139
|
+
return isValidUtf8(bytes)
|
|
140
|
+
? { text: decodeBytes(bytes, 'utf-8'), encoding: 'utf-8' }
|
|
141
|
+
: { text: decodeBytes(bytes, 'gb18030'), encoding: 'gb18030' };
|
|
142
|
+
};
|
|
143
|
+
export const prepareSpreadsheetReadInput = (data, source = {}) => {
|
|
144
|
+
if (!isTextSpreadsheetSource(source)) {
|
|
145
|
+
return { kind: 'binary', data };
|
|
146
|
+
}
|
|
147
|
+
const decoded = decodeSpreadsheetText(data, source.textEncoding);
|
|
148
|
+
return {
|
|
149
|
+
kind: 'text',
|
|
150
|
+
data: decoded.text,
|
|
151
|
+
encoding: decoded.encoding,
|
|
152
|
+
};
|
|
153
|
+
};
|
package/dist/spreadsheet.d.ts
CHANGED
|
@@ -33,5 +33,5 @@ type EVirtTableConstructor = new (container: HTMLElement, options: {
|
|
|
33
33
|
export declare const resolveEVirtTableConstructor: (module: unknown) => EVirtTableConstructor;
|
|
34
34
|
export declare const resolveEVirtTableStyleText: (documentRef: Document) => string;
|
|
35
35
|
export declare const scopeEVirtTableStyleText: (cssText: string, shadow: boolean) => string;
|
|
36
|
-
declare const renderFileViewerSpreadsheet: (buffer: ArrayBuffer, target: HTMLDivElement,
|
|
36
|
+
declare const renderFileViewerSpreadsheet: (buffer: ArrayBuffer, target: HTMLDivElement, type?: string, context?: FileRenderContext) => Promise<AppWrapper>;
|
|
37
37
|
export default renderFileViewerSpreadsheet;
|
package/dist/spreadsheet.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createFileViewerWorkerController, createFileViewerTranslator, createFileViewerZoomChangeEmitter as createZoomChangeEmitter, registerFileViewerZoomProvider, resolveFileViewerSpreadsheetWorkerUrl, unregisterFileViewerZoomProvider, } from '@file-viewer/core';
|
|
1
|
+
import { createFileViewerWorkerController, createFileViewerTranslator, createFileViewerZoomChangeEmitter as createZoomChangeEmitter, registerFileViewerZoomProvider, resolveFileViewerColorScheme, resolveFileViewerRuntimeAssetBaseUrl, resolveFileViewerSpreadsheetWorkerUrl, unregisterFileViewerZoomProvider, } from '@file-viewer/core';
|
|
2
2
|
import { renderSpreadsheetChart } from './spreadsheet/chartRenderer.js';
|
|
3
3
|
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
4
|
import { buildColumns, createTableConfig, detectIndexOffset, getDisplayColumns, getRowHeight, HEADER_HEIGHT, INDEX_COLUMN_WIDTH, normalizeCellStyle, normalizeRowHeight, RESIZABLE_COLUMN_MIN_WIDTH, RESIZABLE_ROW_MIN_HEIGHT, } from './spreadsheet/view.js';
|
|
@@ -11,7 +11,7 @@ const E_VIRT_TABLE_STYLE_MARKERS = [
|
|
|
11
11
|
'.e-virt-table-context-menu',
|
|
12
12
|
];
|
|
13
13
|
const spreadsheetStyle = `
|
|
14
|
-
.excel-wrapper{position:relative;width:100%;height:100%;display:flex;flex-direction:column;background
|
|
14
|
+
.excel-wrapper{position:relative;width:100%;height:100%;display:flex;flex-direction:column;background:var(--file-viewer-render-surface-background,#fff);color:#172033;font-family:Aptos,'Segoe UI','PingFang SC','Microsoft YaHei',sans-serif}
|
|
15
15
|
.excel-wrapper *{box-sizing:border-box}
|
|
16
16
|
.excel-wrapper .table-wrapper{position:relative;width:100%;flex:1;min-height:0;background:#fff;overflow:hidden}
|
|
17
17
|
.excel-wrapper .table-host{position:absolute;inset:0}
|
|
@@ -43,12 +43,18 @@ const spreadsheetStyle = `
|
|
|
43
43
|
.excel-wrapper .sheet-tab.active{border-color:rgba(33,163,102,.28);background:rgba(33,163,102,.12);color:#137347}
|
|
44
44
|
.excel-wrapper .summary{flex:0 0 auto;max-width:42%;overflow:hidden;color:#64748b;font-size:12px;font-weight:700;white-space:nowrap;text-overflow:ellipsis}
|
|
45
45
|
.excel-wrapper .hidden{display:none!important}
|
|
46
|
-
[data-
|
|
47
|
-
[data-
|
|
48
|
-
[data-
|
|
49
|
-
[data-
|
|
50
|
-
[data-
|
|
51
|
-
|
|
46
|
+
.excel-wrapper[data-spreadsheet-theme='dark']{color-scheme:dark;background:var(--file-viewer-render-surface-background,#0f172a);color:#e5e7eb}
|
|
47
|
+
.excel-wrapper[data-spreadsheet-theme='dark'] .table-wrapper{background:#111827}
|
|
48
|
+
.excel-wrapper[data-spreadsheet-theme='dark'] .toolbar{background:#111827;border-color:rgba(148,163,184,.22)}
|
|
49
|
+
.excel-wrapper[data-spreadsheet-theme='dark'] .sheet-tab{color:#cbd5e1}
|
|
50
|
+
.excel-wrapper[data-spreadsheet-theme='dark'] .sheet-tab:hover{background:#1f2937}
|
|
51
|
+
.excel-wrapper[data-spreadsheet-theme='dark'] .sheet-tab.active{border-color:rgba(52,211,153,.38);background:rgba(33,163,102,.2);color:#6ee7b7}
|
|
52
|
+
.excel-wrapper[data-spreadsheet-theme='dark'] .summary,.excel-wrapper[data-spreadsheet-theme='dark'] .sheet-loading-summary{color:#94a3b8}
|
|
53
|
+
.excel-wrapper[data-spreadsheet-theme='dark'] .loading{background:rgba(2,6,23,.9)}
|
|
54
|
+
.excel-wrapper[data-spreadsheet-theme='dark'] .loading-card{border-color:rgba(52,211,153,.2);background:rgba(17,24,39,.96);box-shadow:0 24px 58px rgba(0,0,0,.42)}
|
|
55
|
+
.excel-wrapper[data-spreadsheet-theme='dark'] .loading-copy strong{color:#f0fdf4}
|
|
56
|
+
.excel-wrapper[data-spreadsheet-theme='dark'] .loading-copy p{color:#94a3b8}
|
|
57
|
+
.excel-wrapper[data-spreadsheet-theme='dark'] .error{border-color:rgba(251,146,60,.28);background:#2a1710;color:#fdba74;box-shadow:0 20px 48px rgba(0,0,0,.36)}
|
|
52
58
|
@keyframes sheet-loading-spin{to{transform:rotate(360deg)}}
|
|
53
59
|
@keyframes sheet-loading-pulse{0%,100%{opacity:.55;transform:scale(.9)}50%{opacity:1;transform:scale(1)}}
|
|
54
60
|
@media (max-width:720px){.excel-wrapper .toolbar{align-items:stretch;flex-direction:column}.excel-wrapper .btn-group{flex:0 0 auto}.excel-wrapper .summary{max-width:none;white-space:normal}.excel-wrapper .sheet-loading{left:12px;right:12px;bottom:58px;justify-content:center}.excel-wrapper .loading-card{margin:18px;flex-direction:column;text-align:center}}
|
|
@@ -145,9 +151,7 @@ const getTargetWindow = (target) => {
|
|
|
145
151
|
return target.ownerDocument.defaultView;
|
|
146
152
|
};
|
|
147
153
|
const getDocumentBaseUrl = (target) => {
|
|
148
|
-
return target.ownerDocument
|
|
149
|
-
target.ownerDocument.URL ||
|
|
150
|
-
'file:///';
|
|
154
|
+
return resolveFileViewerRuntimeAssetBaseUrl(target.ownerDocument);
|
|
151
155
|
};
|
|
152
156
|
const callListener = (listener, event) => {
|
|
153
157
|
if (!listener) {
|
|
@@ -496,15 +500,18 @@ const writeSpreadsheetClipboard = async (documentRef, text) => {
|
|
|
496
500
|
}
|
|
497
501
|
return copyTextWithTextareaFallback(documentRef, text);
|
|
498
502
|
};
|
|
499
|
-
const renderFileViewerSpreadsheet = async (buffer, target,
|
|
500
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
503
|
+
const renderFileViewerSpreadsheet = async (buffer, target, type, context) => {
|
|
504
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
501
505
|
const documentRef = target.ownerDocument;
|
|
502
506
|
const loadedEVirtTable = await loadEVirtTable(documentRef);
|
|
503
507
|
const EVirtTable = loadedEVirtTable.constructor;
|
|
504
508
|
const t = createFileViewerTranslator(context === null || context === void 0 ? void 0 : context.options);
|
|
505
509
|
const zoomEmitter = createZoomChangeEmitter();
|
|
510
|
+
const systemDark = (_c = (_b = (_a = documentRef.defaultView) === null || _a === void 0 ? void 0 : _a.matchMedia) === null || _b === void 0 ? void 0 : _b.call(_a, '(prefers-color-scheme: dark)').matches) !== null && _c !== void 0 ? _c : false;
|
|
511
|
+
const darkMode = resolveFileViewerColorScheme((_d = context === null || context === void 0 ? void 0 : context.options) === null || _d === void 0 ? void 0 : _d.theme, systemDark) === 'dark';
|
|
506
512
|
const root = documentRef.createElement('div');
|
|
507
513
|
root.className = 'excel-wrapper';
|
|
514
|
+
root.dataset.spreadsheetTheme = darkMode ? 'dark' : 'light';
|
|
508
515
|
root.dataset.viewerZoomProvider = 'xlsx';
|
|
509
516
|
const loading = documentRef.createElement('div');
|
|
510
517
|
loading.className = 'loading';
|
|
@@ -598,8 +605,8 @@ const renderFileViewerSpreadsheet = async (buffer, target, _type, context) => {
|
|
|
598
605
|
let sheetSessionId = 0;
|
|
599
606
|
let disposed = false;
|
|
600
607
|
let hasNotifiedFirstPaint = false;
|
|
601
|
-
const resizableColumns = ((
|
|
602
|
-
const resizableRows = ((
|
|
608
|
+
const resizableColumns = ((_f = (_e = context === null || context === void 0 ? void 0 : context.options) === null || _e === void 0 ? void 0 : _e.spreadsheet) === null || _f === void 0 ? void 0 : _f.resizableColumns) === true;
|
|
609
|
+
const resizableRows = ((_h = (_g = context === null || context === void 0 ? void 0 : context.options) === null || _g === void 0 ? void 0 : _g.spreadsheet) === null || _h === void 0 ? void 0 : _h.resizableRows) === true;
|
|
603
610
|
const controller = createFileViewerWorkerController(createSpreadsheetWorkerFactory(target, buffer.byteLength, context), { logErrors: false });
|
|
604
611
|
const getActiveSheet = () => sheets.find(sheet => sheet.id === sheetIndex);
|
|
605
612
|
const getSheetTabs = () => {
|
|
@@ -849,6 +856,7 @@ const renderFileViewerSpreadsheet = async (buffer, target, _type, context) => {
|
|
|
849
856
|
const buildTableView = () => ({
|
|
850
857
|
config: createTableConfig({
|
|
851
858
|
hostHeight: getHostHeight(),
|
|
859
|
+
darkMode,
|
|
852
860
|
resizableColumns,
|
|
853
861
|
resizableRows,
|
|
854
862
|
copySelection: copySpreadsheetSelection,
|
|
@@ -905,6 +913,7 @@ const renderFileViewerSpreadsheet = async (buffer, target, _type, context) => {
|
|
|
905
913
|
columns: [],
|
|
906
914
|
config: createTableConfig({
|
|
907
915
|
hostHeight: getHostHeight(),
|
|
916
|
+
darkMode,
|
|
908
917
|
resizableColumns,
|
|
909
918
|
resizableRows,
|
|
910
919
|
copySelection: copySpreadsheetSelection,
|
|
@@ -924,6 +933,7 @@ const renderFileViewerSpreadsheet = async (buffer, target, _type, context) => {
|
|
|
924
933
|
const view = {
|
|
925
934
|
config: createTableConfig({
|
|
926
935
|
hostHeight: getHostHeight(),
|
|
936
|
+
darkMode,
|
|
927
937
|
resizableColumns,
|
|
928
938
|
resizableRows,
|
|
929
939
|
copySelection: copySpreadsheetSelection,
|
|
@@ -1373,7 +1383,13 @@ const renderFileViewerSpreadsheet = async (buffer, target, _type, context) => {
|
|
|
1373
1383
|
scrollActiveSheetIntoView();
|
|
1374
1384
|
}
|
|
1375
1385
|
const emitParseWorkbook = () => {
|
|
1376
|
-
|
|
1386
|
+
var _a, _b;
|
|
1387
|
+
emitWorker('parseWorkbook', {
|
|
1388
|
+
workbook: buffer,
|
|
1389
|
+
fileType: type,
|
|
1390
|
+
filename: context === null || context === void 0 ? void 0 : context.filename,
|
|
1391
|
+
textEncoding: (_b = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.spreadsheet) === null || _b === void 0 ? void 0 : _b.textEncoding,
|
|
1392
|
+
});
|
|
1377
1393
|
};
|
|
1378
1394
|
controller.onWorkerEvent('sheets', ({ sheets: list }) => {
|
|
1379
1395
|
sheets = list;
|
|
@@ -1422,11 +1438,11 @@ const renderFileViewerSpreadsheet = async (buffer, target, _type, context) => {
|
|
|
1422
1438
|
errorMessage = event.message || t('spreadsheet.error.workerFailed');
|
|
1423
1439
|
renderChrome();
|
|
1424
1440
|
});
|
|
1425
|
-
(
|
|
1441
|
+
(_j = context === null || context === void 0 ? void 0 : context.registerExportAdapter) === null || _j === void 0 ? void 0 : _j.call(context, {
|
|
1426
1442
|
print: false,
|
|
1427
1443
|
exportHtml: false,
|
|
1428
1444
|
});
|
|
1429
|
-
(
|
|
1445
|
+
(_k = context === null || context === void 0 ? void 0 : context.registerThumbnailAdapter) === null || _k === void 0 ? void 0 : _k.call(context, {
|
|
1430
1446
|
beforeCapture: async ({ signal }) => {
|
|
1431
1447
|
while (!hasInitialWindow && !errorMessage && !disposed) {
|
|
1432
1448
|
if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
|
|
@@ -1447,7 +1463,7 @@ const renderFileViewerSpreadsheet = async (buffer, target, _type, context) => {
|
|
|
1447
1463
|
getTarget: () => tableWrapper,
|
|
1448
1464
|
});
|
|
1449
1465
|
ensureTable();
|
|
1450
|
-
const ResizeObserverCtor = ((
|
|
1466
|
+
const ResizeObserverCtor = ((_l = getTargetWindow(target)) === null || _l === void 0 ? void 0 : _l.ResizeObserver) ||
|
|
1451
1467
|
(typeof ResizeObserver !== 'undefined' ? ResizeObserver : undefined);
|
|
1452
1468
|
if (ResizeObserverCtor) {
|
|
1453
1469
|
resizeObserver = new ResizeObserverCtor(() => {
|