@file-viewer/renderer-word 3.3.3 → 3.3.5

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.
@@ -36,12 +36,12 @@ const parseOdf = async (buffer, type, t) => {
36
36
  const zip = await JSZip.loadAsync(buffer);
37
37
  const content = await ((_a = zip.file('content.xml')) === null || _a === void 0 ? void 0 : _a.async('text'));
38
38
  if (!content) {
39
- throw new Error(t('word.error.missingOdfContent'));
39
+ throw t.error('word.error.missingOdfContent');
40
40
  }
41
41
  const doc = new DOMParser().parseFromString(content, 'application/xml');
42
42
  const parseError = doc.querySelector('parsererror');
43
43
  if (parseError) {
44
- throw new Error(t('word.error.odfXmlParseFailed'));
44
+ throw t.error('word.error.odfXmlParseFailed');
45
45
  }
46
46
  if (type === 'odp') {
47
47
  const slides = Array.from(doc.getElementsByTagName('draw:page'));
@@ -115,7 +115,7 @@ const renderRtf = async (buffer, target, context) => {
115
115
  };
116
116
  export default async function renderOpenDocument(buffer, target, type, context) {
117
117
  var _a;
118
- const t = createFileViewerTranslator(context === null || context === void 0 ? void 0 : context.options);
118
+ const t = createFileViewerTranslator((context === null || context === void 0 ? void 0 : context.i18n) || (context === null || context === void 0 ? void 0 : context.options));
119
119
  const normalizedType = (type || 'odt').toLowerCase();
120
120
  if (normalizedType === 'rtf') {
121
121
  return renderRtf(buffer, target, context);
package/dist/wordDoc.d.ts CHANGED
@@ -1,6 +1,13 @@
1
1
  import { type FileRenderContext, type FileViewerRenderedInstance as AppWrapper } from '@file-viewer/core';
2
+ type WordLayoutPages = Array<{
3
+ width: number;
4
+ height: number;
5
+ html: string;
6
+ }>;
2
7
  export default function render(buffer: ArrayBuffer, target: HTMLDivElement, context?: FileRenderContext): Promise<AppWrapper>;
3
8
  export declare function mountWordDocument(rendered: {
4
9
  html: string;
5
10
  css: string;
11
+ layoutPages?: WordLayoutPages;
6
12
  }, target: HTMLDivElement, context?: FileRenderContext): AppWrapper;
13
+ export {};
package/dist/wordDoc.js CHANGED
@@ -169,11 +169,18 @@ const getTargetWindow = (target) => {
169
169
  function injectPageBreakMarkers(html) {
170
170
  return html.replace(/<(p|table|section)([^>]*?)style="([^"]*?\bbreak-before\s*:\s*page;?[^"]*?)"([^>]*)>/gi, (match) => `${PAGE_BREAK_MARKER}${match}`);
171
171
  }
172
- function wrapAsWordPages(html) {
172
+ function wrapAsWordPages(html, layoutPages) {
173
+ if ((layoutPages === null || layoutPages === void 0 ? void 0 : layoutPages.length) && layoutPages.every(page => isPositiveFinite(page.width) && isPositiveFinite(page.height))) {
174
+ return `<div class="msdoc-stage">${layoutPages.map(page => `<section class="msdoc-page" data-msdoc-page-width="${page.width}" data-msdoc-page-height="${page.height}"><div class="msdoc-root" style="width:${page.width}px;min-height:${page.height}px;padding:0">${page.html}</div></section>`).join('')}</div>`;
175
+ }
173
176
  const normalizedHtml = injectPageBreakMarkers(html);
174
177
  const pages = normalizedHtml.split(PAGE_BREAK_MARKER);
175
178
  return `<div class="msdoc-stage">${pages.map(page => (`<section class="msdoc-page"><div class="msdoc-root">${page || EMPTY_PAGE_HTML}</div></section>`)).join('')}</div>`;
176
179
  }
180
+ function authoredPageSize(page) {
181
+ const width = Number(page.dataset.msdocPageWidth), height = Number(page.dataset.msdocPageHeight);
182
+ return isPositiveFinite(width) && isPositiveFinite(height) ? { width, height } : MSDOC_PAGE_SIZE;
183
+ }
177
184
  function prepareMsDocCloneForExport(target) {
178
185
  const clone = target.cloneNode(true);
179
186
  clone.classList.remove('msdoc-zoom-viewer');
@@ -185,10 +192,11 @@ function prepareMsDocCloneForExport(target) {
185
192
  node.style.transform = 'none';
186
193
  });
187
194
  clone.querySelectorAll('.msdoc-page').forEach((page, index) => {
195
+ const pageSize = authoredPageSize(page);
188
196
  page.dataset.viewerPrintPageIndex = String(index);
189
- applyPrintPageSize(page, MSDOC_PAGE_SIZE, { heightMode: 'min' });
197
+ applyPrintPageSize(page, pageSize, { heightMode: 'min' });
190
198
  page.style.position = 'relative';
191
- page.style.width = formatCssPixels(MSDOC_PAGE_SIZE.width);
199
+ page.style.width = formatCssPixels(pageSize.width);
192
200
  page.style.maxWidth = 'none';
193
201
  page.style.margin = '0 auto 18px';
194
202
  const root = page.querySelector('.msdoc-root');
@@ -198,9 +206,9 @@ function prepareMsDocCloneForExport(target) {
198
206
  root.style.position = 'relative';
199
207
  root.style.top = 'auto';
200
208
  root.style.left = 'auto';
201
- root.style.width = formatCssPixels(MSDOC_PAGE_SIZE.width);
209
+ root.style.width = formatCssPixels(pageSize.width);
202
210
  root.style.maxWidth = 'none';
203
- root.style.minHeight = formatCssPixels(MSDOC_PAGE_SIZE.height);
211
+ root.style.minHeight = formatCssPixels(pageSize.height);
204
212
  root.style.height = 'auto';
205
213
  root.style.transform = 'none';
206
214
  root.style.transformOrigin = 'top left';
@@ -210,11 +218,13 @@ function prepareMsDocCloneForExport(target) {
210
218
  });
211
219
  return clone.innerHTML;
212
220
  }
213
- function buildMsDocPrintStyle() {
221
+ function buildMsDocPrintStyle(target) {
222
+ const page = target.querySelector('.msdoc-page');
223
+ const pageSize = page ? authoredPageSize(page) : MSDOC_PAGE_SIZE;
214
224
  return buildPrintPageStyle({
215
225
  selector: '.viewer-export-content .msdoc-page',
216
- width: MSDOC_PAGE_SIZE.width,
217
- height: MSDOC_PAGE_SIZE.height,
226
+ width: pageSize.width,
227
+ height: pageSize.height,
218
228
  heightMode: 'min'
219
229
  });
220
230
  }
@@ -247,8 +257,9 @@ function makeMsDocResponsive(target) {
247
257
  if (!root) {
248
258
  return;
249
259
  }
250
- const rootWidth = root.offsetWidth || MSDOC_PAGE_SIZE.width;
251
- const rootHeight = Math.max(root.scrollHeight, root.offsetHeight, MSDOC_PAGE_SIZE.height);
260
+ const pageSize = authoredPageSize(page);
261
+ const rootWidth = root.offsetWidth || pageSize.width;
262
+ const rootHeight = Math.max(root.scrollHeight, root.offsetHeight, pageSize.height);
252
263
  const availableWidth = Math.max(target.clientWidth - 48, 120);
253
264
  const fitScale = Math.min(1, Math.max(MSDOC_MIN_SCALE, availableWidth / rootWidth));
254
265
  const scale = clampScale(fitScale * userZoom);
@@ -404,13 +415,13 @@ export function mountWordDocument(rendered, target, context) {
404
415
  style.dataset.msdoc = '';
405
416
  style.textContent = `${rendered.css}\n${WORD_PAGE_CSS}`;
406
417
  const content = target.ownerDocument.createElement('div');
407
- content.append(sanitizeMsDocHtml(wrapAsWordPages(rendered.html), targetWindow));
418
+ content.append(sanitizeMsDocHtml(wrapAsWordPages(rendered.html, rendered.layoutPages), targetWindow));
408
419
  target.replaceChildren(style, ...Array.from(content.childNodes));
409
420
  const disposeResponsive = makeMsDocResponsive(target);
410
421
  (_a = context === null || context === void 0 ? void 0 : context.registerExportAdapter) === null || _a === void 0 ? void 0 : _a.call(context, {
411
422
  includeDocumentStyles: false,
412
423
  getPrintMaskPages: () => Array.from(target.querySelectorAll('.msdoc-page')),
413
- printStyle: buildMsDocPrintStyle,
424
+ printStyle: () => buildMsDocPrintStyle(target),
414
425
  toHtml: () => prepareMsDocCloneForExport(target)
415
426
  });
416
427
  (_b = context === null || context === void 0 ? void 0 : context.registerThumbnailAdapter) === null || _b === void 0 ? void 0 : _b.call(context, {
package/dist/wordDocx.js CHANGED
@@ -38,7 +38,7 @@ const assertValidDocxPackage = (buffer, context) => {
38
38
  if (signature === ZIP_SIGNATURE_PK) {
39
39
  return;
40
40
  }
41
- throw new Error(createFileViewerTranslator(context === null || context === void 0 ? void 0 : context.options)('word.error.invalidDocx'));
41
+ throw new Error(createFileViewerTranslator((context === null || context === void 0 ? void 0 : context.i18n) || (context === null || context === void 0 ? void 0 : context.options))('word.error.invalidDocx'));
42
42
  };
43
43
  const getTargetWindow = (target) => {
44
44
  return target.ownerDocument.defaultView;
@@ -288,7 +288,7 @@ function makeDocxResponsive(target, context) {
288
288
  measured = true;
289
289
  }
290
290
  page.style.transform = `translateX(-50%) scale(${scale})`;
291
- frame.style.width = `${Math.ceil(Math.max(pageWidth * scale, target.clientWidth - 28, 120))}px`;
291
+ frame.style.width = `${pageWidth * scale}px`;
292
292
  frame.style.maxWidth = 'none';
293
293
  frame.style.height = `${Math.ceil(contentHeight * scale)}px`;
294
294
  });
@@ -536,8 +536,13 @@ async function prepareDocxCloneForExport(target) {
536
536
  normalizeDocxPageForPrint(frame, getDocxFramePrintSize(liveFrames[index]));
537
537
  printDocument.appendChild(frame.cloneNode(true));
538
538
  });
539
+ const screenPaperStyle = `<style>@media screen {
540
+ .docx-print-document > :is(.docx-page-frame, .docx-flow-frame) > section.docx {
541
+ background-color: var(--file-viewer-docx-page-background, #fff);
542
+ }
543
+ }</style>`;
539
544
  return printDocument.childElementCount
540
- ? `${scopedStyles}${printDocument.outerHTML}`
545
+ ? `${scopedStyles}${screenPaperStyle}${printDocument.outerHTML}`
541
546
  : clone.innerHTML;
542
547
  }
543
548
  finally {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@file-viewer/renderer-word",
3
- "version": "3.3.3",
3
+ "version": "3.3.5",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Standalone DOCX, DOC, and OpenDocument renderer for File Viewer; RTF parsing is an explicit opt-in capability.",
@@ -81,9 +81,9 @@
81
81
  "README.zh-CN.md"
82
82
  ],
83
83
  "dependencies": {
84
- "@file-viewer/core": "3.3.3",
85
- "@file-viewer/doc": "3.3.3",
86
- "@file-viewer/docx": "0.3.39",
84
+ "@file-viewer/core": "3.3.5",
85
+ "@file-viewer/doc": "3.3.5",
86
+ "@file-viewer/docx": "0.3.42",
87
87
  "jszip": "3.10.2"
88
88
  },
89
89
  "fileViewer": {