@file-viewer/renderer-spreadsheet 2.1.13 → 2.1.14

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 CHANGED
@@ -29,8 +29,8 @@ const options = {
29
29
  ## Capabilities
30
30
 
31
31
  - Supports `.xlsx`, `.xltx`, `.xlsm`, `.xlsb`, `.xls`, `.xlt`, `.xltm`, `.csv`, `.ods`, `.fods`, and `.numbers`.
32
- - Uses main-thread parsing by default to avoid worker stalls caused by local servers, mobile WebViews, MIME configuration, or strict CSP.
33
- - Can opt into the static worker path with `options.spreadsheet.worker: true` and `options.spreadsheet.workerUrl`.
32
+ - 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
+ - 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
34
  - 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.
35
35
  - Uses virtual rendering for interaction. Host components usually hide full-document print for spreadsheets to avoid printing only the current viewport.
36
36
 
package/README.md CHANGED
@@ -29,8 +29,8 @@ const options = {
29
29
  ## 能力边界
30
30
 
31
31
  - 支持 `.xlsx`、`.xltx`、`.xlsm`、`.xlsb`、`.xls`、`.xlt`、`.xltm`、`.csv`、`.ods`、`.fods`、`.numbers`。
32
- - 默认主线程解析,避免本地服务器、手机 WebView、MIME 或 CSP 导致 Worker 卡住。
33
- - 可以通过 `options.spreadsheet.worker: true` 与 `options.spreadsheet.workerUrl` 显式启用静态 Worker。
32
+ - 默认 `options.spreadsheet.worker: 'auto'`:小文件走主线程兼容路径,大文件达到 `options.spreadsheet.workerAutoThreshold`(默认 1MB)后自动尝试静态 Worker。
33
+ - 可以通过 `options.spreadsheet.worker: true` 强制启用静态 Worker,通过 `worker: false` 关闭自动启用;静态路径特殊时配置 `options.spreadsheet.workerUrl`。
34
34
  - 支持多 sheet、横向滚动标签栏、合并单元格、列宽/行高、边框、填充、对齐、文本颜色、图片覆盖层、统一缩放 provider 和可选表头拖拽调整列宽。
35
35
  - 表格使用虚拟渲染,适合交互预览;完整打印按钮通常由上层能力判断隐藏,避免只打印当前视口。
36
36
 
@@ -2,6 +2,7 @@ import { createFileViewerWorkerController, createFileViewerTranslator, createFil
2
2
  import { buildRows, clampWindowStart, collectWindowStarts, createEmptyVirtualState, DEFAULT_SHEET_DEFAULTS, displayCellKey, getDataKey, INDEX_COLUMN_KEY, markWindowState, ROW_STATE_FIELD, RowState, WINDOW_SIZE, } from './spreadsheet/state.js';
3
3
  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';
4
4
  const EXCEL_IMAGE_SCROLLBAR_GUARD = 18;
5
+ const DEFAULT_SPREADSHEET_WORKER_AUTO_THRESHOLD = 1 * 1024 * 1024;
5
6
  const spreadsheetStyle = `
6
7
  .excel-wrapper{position:relative;width:100%;height:100%;display:flex;flex-direction:column;background:#fff;color:#172033;font-family:Aptos,'Segoe UI','PingFang SC','Microsoft YaHei',sans-serif}
7
8
  .excel-wrapper *{box-sizing:border-box}
@@ -149,13 +150,142 @@ class MainThreadSpreadsheetWorker {
149
150
  }
150
151
  }
151
152
  }
153
+ class AutoFallbackSpreadsheetWorker {
154
+ constructor(primary, createFallback) {
155
+ this.createFallback = createFallback;
156
+ this.onmessage = null;
157
+ this.onerror = null;
158
+ this.destroyed = false;
159
+ this.usingFallback = false;
160
+ this.hasPrimaryMessage = false;
161
+ this.pendingMessages = [];
162
+ this.listeners = new Map();
163
+ this.handleMessage = (event) => {
164
+ var _a;
165
+ if (this.destroyed) {
166
+ return;
167
+ }
168
+ if (!this.usingFallback) {
169
+ this.hasPrimaryMessage = true;
170
+ this.pendingMessages.length = 0;
171
+ }
172
+ (_a = this.onmessage) === null || _a === void 0 ? void 0 : _a.call(this, event);
173
+ this.dispatch('message', event);
174
+ };
175
+ this.handleError = (event) => {
176
+ var _a;
177
+ if (this.destroyed) {
178
+ return;
179
+ }
180
+ if (!this.usingFallback && !this.hasPrimaryMessage) {
181
+ this.switchToFallback(event);
182
+ return;
183
+ }
184
+ (_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, event);
185
+ this.dispatch('error', event);
186
+ };
187
+ this.active = primary;
188
+ this.attach(primary);
189
+ }
190
+ addEventListener(type, listener) {
191
+ var _a;
192
+ if (!this.listeners.has(type)) {
193
+ this.listeners.set(type, new Set());
194
+ }
195
+ (_a = this.listeners.get(type)) === null || _a === void 0 ? void 0 : _a.add(listener);
196
+ }
197
+ removeEventListener(type, listener) {
198
+ var _a;
199
+ (_a = this.listeners.get(type)) === null || _a === void 0 ? void 0 : _a.delete(listener);
200
+ }
201
+ terminate() {
202
+ this.destroyed = true;
203
+ this.detach(this.active);
204
+ this.active.terminate();
205
+ this.pendingMessages.length = 0;
206
+ this.listeners.clear();
207
+ }
208
+ postMessage(message) {
209
+ if (this.destroyed) {
210
+ return;
211
+ }
212
+ if (!this.usingFallback && !this.hasPrimaryMessage) {
213
+ this.pendingMessages.push(message);
214
+ }
215
+ this.active.postMessage(message);
216
+ }
217
+ attach(worker) {
218
+ worker.addEventListener('message', this.handleMessage);
219
+ worker.addEventListener('error', this.handleError);
220
+ }
221
+ detach(worker) {
222
+ worker.removeEventListener('message', this.handleMessage);
223
+ worker.removeEventListener('error', this.handleError);
224
+ }
225
+ dispatch(type, event) {
226
+ var _a;
227
+ (_a = this.listeners.get(type)) === null || _a === void 0 ? void 0 : _a.forEach(listener => callListener(listener, event));
228
+ }
229
+ switchToFallback(event) {
230
+ var _a;
231
+ const messages = this.pendingMessages.splice(0);
232
+ this.detach(this.active);
233
+ this.active.terminate();
234
+ this.usingFallback = true;
235
+ this.hasPrimaryMessage = false;
236
+ try {
237
+ this.active = this.createFallback();
238
+ this.attach(this.active);
239
+ console.warn('[file-viewer] Spreadsheet Worker 自动模式启动失败,已回退到主线程解析。', event.message || event.type);
240
+ messages.forEach(message => this.active.postMessage(message));
241
+ }
242
+ catch (fallbackError) {
243
+ const targetGlobal = typeof window !== 'undefined' ? window : undefined;
244
+ const ErrorEventCtor = (targetGlobal === null || targetGlobal === void 0 ? void 0 : targetGlobal.ErrorEvent) ||
245
+ (typeof ErrorEvent !== 'undefined' ? ErrorEvent : undefined);
246
+ const message = fallbackError instanceof Error ? fallbackError.message : String(fallbackError);
247
+ const nextEvent = ErrorEventCtor
248
+ ? new ErrorEventCtor('error', { message, error: fallbackError })
249
+ : { type: 'error', message, error: fallbackError };
250
+ (_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, nextEvent);
251
+ this.dispatch('error', nextEvent);
252
+ }
253
+ }
254
+ }
152
255
  const createMainThreadSpreadsheetWorker = (target) => {
153
256
  return new MainThreadSpreadsheetWorker(getTargetWindow(target));
154
257
  };
155
- const createSpreadsheetWorkerFactory = (target, context) => {
258
+ const getSpreadsheetWorkerMode = (context) => {
259
+ var _a, _b, _c;
260
+ return (_c = (_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.worker) !== null && _c !== void 0 ? _c : 'auto';
261
+ };
262
+ const shouldUseSpreadsheetWorker = (byteLength, context) => {
263
+ var _a;
264
+ const spreadsheetOptions = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.spreadsheet;
265
+ const workerMode = getSpreadsheetWorkerMode(context);
266
+ if (workerMode === true) {
267
+ return true;
268
+ }
269
+ if (workerMode === false) {
270
+ return false;
271
+ }
272
+ const threshold = typeof (spreadsheetOptions === null || spreadsheetOptions === void 0 ? void 0 : spreadsheetOptions.workerAutoThreshold) === 'number' &&
273
+ Number.isFinite(spreadsheetOptions.workerAutoThreshold) &&
274
+ spreadsheetOptions.workerAutoThreshold >= 0
275
+ ? spreadsheetOptions.workerAutoThreshold
276
+ : DEFAULT_SPREADSHEET_WORKER_AUTO_THRESHOLD;
277
+ return byteLength >= threshold;
278
+ };
279
+ const wrapAutoSpreadsheetWorker = (worker, target, context) => {
280
+ if (getSpreadsheetWorkerMode(context) !== 'auto') {
281
+ return worker;
282
+ }
283
+ return new AutoFallbackSpreadsheetWorker(worker, () => createMainThreadSpreadsheetWorker(target));
284
+ };
285
+ const createSpreadsheetWorkerFactory = (target, bufferByteLength, context) => {
156
286
  return () => {
157
- var _a, _b, _c;
158
- if (((_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.worker) !== true) {
287
+ var _a;
288
+ if (!shouldUseSpreadsheetWorker(bufferByteLength, context)) {
159
289
  return createMainThreadSpreadsheetWorker(target);
160
290
  }
161
291
  const view = getTargetWindow(target);
@@ -164,13 +294,13 @@ const createSpreadsheetWorkerFactory = (target, context) => {
164
294
  if (!WorkerCtor) {
165
295
  return createMainThreadSpreadsheetWorker(target);
166
296
  }
167
- const workerUrl = resolveFileViewerSpreadsheetWorkerUrl((_c = context === null || context === void 0 ? void 0 : context.options) === null || _c === void 0 ? void 0 : _c.spreadsheet, getDocumentBaseUrl(target));
297
+ const workerUrl = resolveFileViewerSpreadsheetWorkerUrl((_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.spreadsheet, getDocumentBaseUrl(target));
168
298
  try {
169
- return new WorkerCtor(workerUrl, { type: 'module' });
299
+ return wrapAutoSpreadsheetWorker(new WorkerCtor(workerUrl, { type: 'module' }), target, context);
170
300
  }
171
301
  catch (moduleWorkerError) {
172
302
  try {
173
- return new WorkerCtor(workerUrl);
303
+ return wrapAutoSpreadsheetWorker(new WorkerCtor(workerUrl), target, context);
174
304
  }
175
305
  catch (classicWorkerError) {
176
306
  console.warn('[file-viewer] Spreadsheet Worker 无法创建,已回退到主线程解析。', classicWorkerError || moduleWorkerError);
@@ -361,7 +491,7 @@ const renderFileViewerSpreadsheet = async (buffer, target, _type, context) => {
361
491
  let hasNotifiedFirstPaint = false;
362
492
  const resizableColumns = ((_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.resizableColumns) === true;
363
493
  const resizableRows = ((_d = (_c = context === null || context === void 0 ? void 0 : context.options) === null || _c === void 0 ? void 0 : _c.spreadsheet) === null || _d === void 0 ? void 0 : _d.resizableRows) === true;
364
- const controller = createFileViewerWorkerController(createSpreadsheetWorkerFactory(target, context), { logErrors: false });
494
+ const controller = createFileViewerWorkerController(createSpreadsheetWorkerFactory(target, buffer.byteLength, context), { logErrors: false });
365
495
  const getActiveSheet = () => sheets.find(sheet => sheet.id === sheetIndex);
366
496
  const getSheetTabs = () => {
367
497
  const visible = sheets.filter(sheet => !sheet.hidden);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@file-viewer/renderer-spreadsheet",
3
- "version": "2.1.13",
3
+ "version": "2.1.14",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Standalone spreadsheet renderer plugin for Flyfish File Viewer powered by styled-exceljs and e-virt-table.",
@@ -68,7 +68,7 @@
68
68
  "LICENSE"
69
69
  ],
70
70
  "dependencies": {
71
- "@file-viewer/core": "^2.1.13",
71
+ "@file-viewer/core": "^2.1.14",
72
72
  "e-virt-table": "^1.3.26",
73
73
  "styled-exceljs": "0.21.1",
74
74
  "tinycolor2": "^1.6.0"