@open-file-viewer/core 0.1.25 → 0.1.26

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/index.js CHANGED
@@ -6346,6 +6346,7 @@ function decodeXml(value) {
6346
6346
  // src/plugins/office.ts
6347
6347
  import JSZip3 from "jszip";
6348
6348
  import DOMPurify2 from "dompurify";
6349
+ import * as docxPreview from "docx-preview";
6349
6350
 
6350
6351
  // src/plugins/msdoc.ts
6351
6352
  var CFB_SIGNATURE = [208, 207, 17, 224, 161, 177, 26, 225];
@@ -7782,6 +7783,7 @@ var presentationExtensions = /* @__PURE__ */ new Set(["pptx", "pptm", "ppt", "pp
7782
7783
  var packagedOfficeCandidates = /* @__PURE__ */ new Set(["wps", "et", "dps", "numbers", "key"]);
7783
7784
  var SHEET_WINDOW_ROWS = 200;
7784
7785
  var SHEET_WINDOW_COLUMNS = 80;
7786
+ var DEFAULT_DOCX_RENDER_TIMEOUT_MS = 15e3;
7785
7787
  var DEFAULT_PPTX_RENDER_TIMEOUT_MS = 12e3;
7786
7788
  var PPTX_REL_NS = "http://schemas.openxmlformats.org/package/2006/relationships";
7787
7789
  var officeMimeTypes = /* @__PURE__ */ new Set([
@@ -8090,23 +8092,28 @@ async function renderDocx(panel, arrayBuffer, fit) {
8090
8092
  document.head.append(styleContainer);
8091
8093
  let disposeFit;
8092
8094
  try {
8093
- const docxPreview = await import("docx-preview");
8094
- await docxPreview.renderAsync(arrayBuffer, content, styleContainer, {
8095
- className: "ofv-docx",
8096
- inWrapper: true,
8097
- breakPages: true,
8098
- ignoreWidth: false,
8099
- ignoreHeight: false,
8100
- ignoreFonts: false,
8101
- renderHeaders: true,
8102
- renderFooters: true,
8103
- renderFootnotes: true,
8104
- renderEndnotes: true,
8105
- renderComments: true,
8106
- renderAltChunks: true,
8107
- experimental: true,
8108
- useBase64URL: true
8109
- });
8095
+ await withTimeout(
8096
+ (async () => {
8097
+ await docxPreview.renderAsync(arrayBuffer, content, styleContainer, {
8098
+ className: "ofv-docx",
8099
+ inWrapper: true,
8100
+ breakPages: true,
8101
+ ignoreWidth: false,
8102
+ ignoreHeight: false,
8103
+ ignoreFonts: false,
8104
+ renderHeaders: true,
8105
+ renderFooters: true,
8106
+ renderFootnotes: true,
8107
+ renderEndnotes: true,
8108
+ renderComments: true,
8109
+ renderAltChunks: true,
8110
+ experimental: true,
8111
+ useBase64URL: true
8112
+ });
8113
+ })(),
8114
+ docxRenderTimeoutMs(),
8115
+ "DOCX rendering"
8116
+ );
8110
8117
  await normalizeDocxLayout(content, arrayBuffer);
8111
8118
  const shouldUseTextboxFallback = await docxPreviewLooksBlank(content, arrayBuffer) || await docxPreviewMissesRichTextboxContent(content, arrayBuffer) || await docxShouldPreferTextboxLayoutFallback(arrayBuffer);
8112
8119
  if (shouldUseTextboxFallback) {
@@ -8129,13 +8136,18 @@ async function renderDocx(panel, arrayBuffer, fit) {
8129
8136
  } catch (error) {
8130
8137
  disposeFit?.();
8131
8138
  styleContainer.remove();
8132
- content.replaceChildren();
8133
- await renderDocxContentFallback(content, arrayBuffer);
8134
- panel.append(content);
8139
+ const fallbackContent = document.createElement("div");
8140
+ fallbackContent.className = "ofv-docx-document";
8141
+ await renderDocxContentFallback(fallbackContent, arrayBuffer);
8142
+ panel.append(fallbackContent);
8135
8143
  console.warn("DOCX layout preview failed, fell back to Mammoth:", error);
8136
8144
  }
8137
8145
  return () => void 0;
8138
8146
  }
8147
+ function docxRenderTimeoutMs() {
8148
+ const override = globalThis.__OFV_DOCX_RENDER_TIMEOUT_MS__;
8149
+ return typeof override === "number" && override > 0 ? override : DEFAULT_DOCX_RENDER_TIMEOUT_MS;
8150
+ }
8139
8151
  async function docxPreviewLooksBlank(container, arrayBuffer) {
8140
8152
  if (container.querySelector("img, svg, canvas, table")) {
8141
8153
  return false;
@@ -8228,12 +8240,13 @@ async function renderDocxContentFallback(container, arrayBuffer, options = {}) {
8228
8240
  return;
8229
8241
  }
8230
8242
  try {
8231
- await renderDocxWithMammoth(container, arrayBuffer);
8232
- const renderedText = normalizePreviewText(container.querySelector(".ofv-document")?.textContent || "");
8243
+ const mammothContent = document.createElement("div");
8244
+ await withTimeout(renderDocxWithMammoth(mammothContent, arrayBuffer), docxRenderTimeoutMs(), "DOCX fallback rendering");
8245
+ const renderedText = normalizePreviewText(mammothContent.querySelector(".ofv-document")?.textContent || "");
8233
8246
  if (renderedText.length >= 24) {
8247
+ container.append(...Array.from(mammothContent.childNodes));
8234
8248
  return;
8235
8249
  }
8236
- container.querySelector(".ofv-document")?.remove();
8237
8250
  await renderDocxTextFallback(container, arrayBuffer);
8238
8251
  } catch (fallbackError) {
8239
8252
  await renderDocxTextFallback(container, arrayBuffer);
@@ -10518,13 +10531,13 @@ function renderPptxTextFallback(container, insight) {
10518
10531
  container.append(article);
10519
10532
  }
10520
10533
  }
10521
- async function withTimeout(promise, timeoutMs) {
10534
+ async function withTimeout(promise, timeoutMs, label = "PPTX rendering") {
10522
10535
  let timeoutId;
10523
10536
  try {
10524
10537
  return await Promise.race([
10525
10538
  promise,
10526
10539
  new Promise((_resolve, reject) => {
10527
- timeoutId = window.setTimeout(() => reject(new Error(`PPTX rendering timed out after ${timeoutMs}ms.`)), timeoutMs);
10540
+ timeoutId = window.setTimeout(() => reject(new Error(`${label} timed out after ${timeoutMs}ms.`)), timeoutMs);
10528
10541
  })
10529
10542
  ]);
10530
10543
  } finally {