@file-viewer/renderer-word 3.1.2 → 3.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 -70
- package/README.md +3 -65
- package/dist/docxExport.d.ts +1 -0
- package/dist/docxExport.js +9 -0
- package/dist/docxFrames.d.ts +1 -0
- package/dist/docxFrames.js +49 -0
- package/dist/index.d.ts +1 -7
- package/dist/index.js +6 -21
- package/dist/sanitizeRtf.d.ts +0 -10
- package/dist/sanitizeRtf.js +0 -11
- package/dist/wordContainer.d.ts +2 -0
- package/dist/wordContainer.js +119 -0
- package/dist/wordDoc.d.ts +4 -3
- package/dist/wordDoc.js +37 -18
- package/dist/wordDocx.d.ts +1 -21
- package/dist/wordDocx.js +66 -272
- package/dist/wordMl.d.ts +0 -6
- package/dist/wordMl.js +43 -14
- package/dist/wordText.d.ts +2 -0
- package/dist/wordText.js +57 -0
- package/dist/wordViewport.d.ts +7 -0
- package/dist/wordViewport.js +15 -0
- package/package.json +38 -22
- package/dist/docxAnchors.d.ts +0 -8
- package/dist/docxAnchors.js +0 -60
package/dist/wordDocx.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { clampWordScale, isPositiveFinite, readWordFitViewport } from './wordViewport.js';
|
|
2
|
+
import { readDocxFlowPaperHeight } from './docxExport.js';
|
|
3
|
+
import { observeDocxFrames } from './docxFrames.js';
|
|
4
|
+
import { DEFAULT_FILE_VIEWER_DOCX_RUNTIME_VERSION, resolveFileViewerDocxWorkerJsZipUrl, resolveFileViewerDocxWorkerUrl, resolveFileViewerRuntimeAssetBaseUrl } from '@file-viewer/core/assets';
|
|
5
|
+
import { applyPrintPageSize, buildPrintPageStyle, createFileViewerTranslator, createFileViewerZoomChangeEmitter as createZoomChangeEmitter, formatCssPixels, getElementPrintPageSize, normalizeFileViewerTheme, replaceFileViewerCanvasWithImages, registerFileViewerZoomProvider, resolveFileViewerFitScale, unregisterFileViewerZoomProvider, waitForFileViewerNextPaint } from '@file-viewer/core';
|
|
5
6
|
const DOCX_DEFAULT_PAGE_SIZE = {
|
|
6
7
|
width: 794,
|
|
7
8
|
height: 1123
|
|
@@ -11,26 +12,6 @@ const DOCX_MIN_SCALE = 0.24;
|
|
|
11
12
|
const DOCX_MAX_SCALE = 3;
|
|
12
13
|
const DOCX_ZOOM_STEP = 0.15;
|
|
13
14
|
const ZIP_SIGNATURE_PK = 0x504b;
|
|
14
|
-
const WORDPROCESSINGML_NAMESPACE = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
|
|
15
|
-
const OFFICE_RELATIONSHIP_NAMESPACE = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';
|
|
16
|
-
const PACKAGE_RELATIONSHIP_NAMESPACE = 'http://schemas.openxmlformats.org/package/2006/relationships';
|
|
17
|
-
const VML_NAMESPACE = 'urn:schemas-microsoft-com:vml';
|
|
18
|
-
const DOCX_DOCUMENT_PART = 'word/document.xml';
|
|
19
|
-
const DOCX_DOCUMENT_RELATIONSHIPS_PART = 'word/_rels/document.xml.rels';
|
|
20
|
-
const DOCX_PAGE_BACKGROUND_CLASS = 'docx-page-background';
|
|
21
|
-
const DOCX_BACKGROUND_MIME_TYPES = {
|
|
22
|
-
bmp: 'image/bmp',
|
|
23
|
-
gif: 'image/gif',
|
|
24
|
-
jpeg: 'image/jpeg',
|
|
25
|
-
jpg: 'image/jpeg',
|
|
26
|
-
png: 'image/png',
|
|
27
|
-
svg: 'image/svg+xml',
|
|
28
|
-
tif: 'image/tiff',
|
|
29
|
-
tiff: 'image/tiff',
|
|
30
|
-
webp: 'image/webp'
|
|
31
|
-
};
|
|
32
|
-
// Modern bundlers expose the ESM named exports, while some legacy webpack
|
|
33
|
-
// configurations wrap the CommonJS browser API in `default`.
|
|
34
15
|
const resolveDocxLibrary = (module) => {
|
|
35
16
|
const library = typeof module.renderAsync === 'function' ? module : module.default;
|
|
36
17
|
if (!library || typeof library.renderAsync !== 'function') {
|
|
@@ -52,42 +33,6 @@ const loadLibrary = (() => {
|
|
|
52
33
|
return resolveDocxLibrary(await loader.load());
|
|
53
34
|
};
|
|
54
35
|
})();
|
|
55
|
-
export const isMissingDocxHeaderFooterRootError = (error) => {
|
|
56
|
-
if (!(error instanceof Error)) {
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
return /(?:undefined|null).*children|children.*(?:undefined|null)/i.test(error.message) &&
|
|
60
|
-
/renderHeaderFooter/i.test(error.stack || '');
|
|
61
|
-
};
|
|
62
|
-
/**
|
|
63
|
-
* Some malformed or partially generated DOCX files reference a header/footer
|
|
64
|
-
* part whose parsed root is missing. @file-viewer/docx 0.3.21 skips that part;
|
|
65
|
-
* this retry keeps older installed engines usable while the dependency update
|
|
66
|
-
* rolls through lockfiles and private registries.
|
|
67
|
-
*/
|
|
68
|
-
export const renderDocxWithHeaderFooterFallback = async (render, buffer, target, options) => {
|
|
69
|
-
try {
|
|
70
|
-
await render(buffer, target, undefined, options);
|
|
71
|
-
return false;
|
|
72
|
-
}
|
|
73
|
-
catch (error) {
|
|
74
|
-
if (!isMissingDocxHeaderFooterRootError(error)) {
|
|
75
|
-
throw error;
|
|
76
|
-
}
|
|
77
|
-
target.replaceChildren();
|
|
78
|
-
await render(buffer, target, undefined, {
|
|
79
|
-
...options,
|
|
80
|
-
renderHeaders: false,
|
|
81
|
-
renderFooters: false
|
|
82
|
-
});
|
|
83
|
-
return true;
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
/**
|
|
87
|
-
* DOCX / DOCM / DOTX / DOTM are OOXML packages, so a valid file must start
|
|
88
|
-
* with a ZIP signature. This catches common enterprise download failures where
|
|
89
|
-
* an object-storage XML error page is saved with a `.docx` extension.
|
|
90
|
-
*/
|
|
91
36
|
const assertValidDocxPackage = (buffer, context) => {
|
|
92
37
|
const signature = buffer.byteLength >= 4 ? new DataView(buffer).getUint16(0, false) : 0;
|
|
93
38
|
if (signature === ZIP_SIGNATURE_PK) {
|
|
@@ -98,11 +43,6 @@ const assertValidDocxPackage = (buffer, context) => {
|
|
|
98
43
|
const getTargetWindow = (target) => {
|
|
99
44
|
return target.ownerDocument.defaultView;
|
|
100
45
|
};
|
|
101
|
-
const createTargetXmlParser = (target) => {
|
|
102
|
-
var _a, _b;
|
|
103
|
-
const DOMParserCtor = (_b = (_a = getTargetWindow(target)) === null || _a === void 0 ? void 0 : _a.DOMParser) !== null && _b !== void 0 ? _b : globalThis.DOMParser;
|
|
104
|
-
return new DOMParserCtor();
|
|
105
|
-
};
|
|
106
46
|
const getTargetProtocol = (target) => {
|
|
107
47
|
var _a, _b, _c;
|
|
108
48
|
const candidates = [
|
|
@@ -115,108 +55,10 @@ const getTargetProtocol = (target) => {
|
|
|
115
55
|
return new URL(candidate).protocol;
|
|
116
56
|
}
|
|
117
57
|
catch {
|
|
118
|
-
// Ignore synthetic document URLs created by tests or embedded hosts.
|
|
119
58
|
}
|
|
120
59
|
}
|
|
121
60
|
return '';
|
|
122
61
|
};
|
|
123
|
-
const getElementsByLocalName = (root, namespace, localName) => {
|
|
124
|
-
const namespaced = Array.from(root.getElementsByTagNameNS(namespace, localName));
|
|
125
|
-
if (namespaced.length) {
|
|
126
|
-
return namespaced;
|
|
127
|
-
}
|
|
128
|
-
return Array.from(root.getElementsByTagName('*')).filter(element => element.localName === localName);
|
|
129
|
-
};
|
|
130
|
-
const parseDocxXml = (source, parser) => {
|
|
131
|
-
const xml = parser.parseFromString(source, 'application/xml');
|
|
132
|
-
return getElementsByLocalName(xml, 'http://www.mozilla.org/newlayout/xml/parsererror.xml', 'parsererror').length
|
|
133
|
-
? null
|
|
134
|
-
: xml;
|
|
135
|
-
};
|
|
136
|
-
const resolvePackagePartPath = (basePart, relationshipTarget) => {
|
|
137
|
-
const segments = relationshipTarget.startsWith('/') ? [] : basePart.split('/').slice(0, -1);
|
|
138
|
-
relationshipTarget
|
|
139
|
-
.replace(/^\/+/, '')
|
|
140
|
-
.split('/')
|
|
141
|
-
.forEach(segment => {
|
|
142
|
-
if (!segment || segment === '.') {
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
if (segment === '..') {
|
|
146
|
-
segments.pop();
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
segments.push(segment);
|
|
150
|
-
});
|
|
151
|
-
return segments.join('/');
|
|
152
|
-
};
|
|
153
|
-
const resolveDocxImageMimeType = (partName) => {
|
|
154
|
-
var _a;
|
|
155
|
-
const extension = ((_a = partName.split('.').pop()) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || '';
|
|
156
|
-
return DOCX_BACKGROUND_MIME_TYPES[extension];
|
|
157
|
-
};
|
|
158
|
-
/**
|
|
159
|
-
* WPS and Word can store a page background as a document-level VML fill. The
|
|
160
|
-
* DOCX engine intentionally ignores that legacy drawing node, so resolve only
|
|
161
|
-
* its package-local image relationship here and leave all body layout to it.
|
|
162
|
-
*/
|
|
163
|
-
export const resolveDocxPageBackgroundImage = async (buffer, createXmlParser = () => new DOMParser()) => {
|
|
164
|
-
try {
|
|
165
|
-
const archive = await JSZip.loadAsync(buffer);
|
|
166
|
-
const documentEntry = archive.file(DOCX_DOCUMENT_PART);
|
|
167
|
-
const relationshipsEntry = archive.file(DOCX_DOCUMENT_RELATIONSHIPS_PART);
|
|
168
|
-
if (!documentEntry || !relationshipsEntry) {
|
|
169
|
-
return undefined;
|
|
170
|
-
}
|
|
171
|
-
const parser = createXmlParser();
|
|
172
|
-
const documentXml = parseDocxXml(await documentEntry.async('string'), parser);
|
|
173
|
-
const relationshipsXml = parseDocxXml(await relationshipsEntry.async('string'), parser);
|
|
174
|
-
if (!documentXml || !relationshipsXml) {
|
|
175
|
-
return undefined;
|
|
176
|
-
}
|
|
177
|
-
const background = getElementsByLocalName(documentXml, WORDPROCESSINGML_NAMESPACE, 'background')[0];
|
|
178
|
-
const fill = background && getElementsByLocalName(background, VML_NAMESPACE, 'fill')[0];
|
|
179
|
-
const relationshipId = (fill === null || fill === void 0 ? void 0 : fill.getAttributeNS(OFFICE_RELATIONSHIP_NAMESPACE, 'id')) || (fill === null || fill === void 0 ? void 0 : fill.getAttribute('r:id'));
|
|
180
|
-
if (!relationshipId) {
|
|
181
|
-
return undefined;
|
|
182
|
-
}
|
|
183
|
-
const relationship = getElementsByLocalName(relationshipsXml, PACKAGE_RELATIONSHIP_NAMESPACE, 'Relationship').find(candidate => candidate.getAttribute('Id') === relationshipId);
|
|
184
|
-
const target = relationship === null || relationship === void 0 ? void 0 : relationship.getAttribute('Target');
|
|
185
|
-
if (!target || (relationship === null || relationship === void 0 ? void 0 : relationship.getAttribute('TargetMode')) === 'External') {
|
|
186
|
-
return undefined;
|
|
187
|
-
}
|
|
188
|
-
const partName = resolvePackagePartPath(DOCX_DOCUMENT_PART, target);
|
|
189
|
-
const mimeType = resolveDocxImageMimeType(partName);
|
|
190
|
-
const imageEntry = archive.file(partName) || archive.file(decodeURIComponent(partName));
|
|
191
|
-
if (!mimeType || !imageEntry) {
|
|
192
|
-
return undefined;
|
|
193
|
-
}
|
|
194
|
-
return `data:${mimeType};base64,${await imageEntry.async('base64')}`;
|
|
195
|
-
}
|
|
196
|
-
catch {
|
|
197
|
-
// A page background is optional and must never make an otherwise readable
|
|
198
|
-
// document fail. The DOCX engine remains responsible for package errors.
|
|
199
|
-
return undefined;
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
export const applyDocxPageBackgroundImage = (target, imageUrl) => {
|
|
203
|
-
if (!imageUrl) {
|
|
204
|
-
return 0;
|
|
205
|
-
}
|
|
206
|
-
let applied = 0;
|
|
207
|
-
target.querySelectorAll('section.docx').forEach(page => {
|
|
208
|
-
const existing = Array.from(page.children).find(child => child.classList.contains(DOCX_PAGE_BACKGROUND_CLASS));
|
|
209
|
-
const background = existing || target.ownerDocument.createElement('div');
|
|
210
|
-
background.className = DOCX_PAGE_BACKGROUND_CLASS;
|
|
211
|
-
background.setAttribute('aria-hidden', 'true');
|
|
212
|
-
background.style.backgroundImage = `url("${imageUrl}")`;
|
|
213
|
-
if (!existing) {
|
|
214
|
-
page.prepend(background);
|
|
215
|
-
}
|
|
216
|
-
applied += 1;
|
|
217
|
-
});
|
|
218
|
-
return applied;
|
|
219
|
-
};
|
|
220
62
|
const shouldUseDocxWorker = (target, docxOptions) => {
|
|
221
63
|
var _a, _b;
|
|
222
64
|
if ((docxOptions === null || docxOptions === void 0 ? void 0 : docxOptions.worker) === false) {
|
|
@@ -225,10 +67,6 @@ const shouldUseDocxWorker = (target, docxOptions) => {
|
|
|
225
67
|
if ((docxOptions === null || docxOptions === void 0 ? void 0 : docxOptions.worker) === true) {
|
|
226
68
|
return true;
|
|
227
69
|
}
|
|
228
|
-
// Auto mode is asset-aware. A browser can expose Worker even when a host has
|
|
229
|
-
// not copied the optional DOCX vendor files; probing that missing URL often
|
|
230
|
-
// returns an SPA HTML fallback and emits a SyntaxError before main-thread
|
|
231
|
-
// rendering recovers. Full packages inject workerUrl, so they still use it.
|
|
232
70
|
const WorkerCtor = (_b = (_a = getTargetWindow(target)) === null || _a === void 0 ? void 0 : _a.Worker) !== null && _b !== void 0 ? _b : globalThis.Worker;
|
|
233
71
|
return Boolean(WorkerCtor &&
|
|
234
72
|
(docxOptions === null || docxOptions === void 0 ? void 0 : docxOptions.workerUrl) &&
|
|
@@ -263,25 +101,6 @@ const appendDocxVendorAssetVersion = (url, explicitUrl) => {
|
|
|
263
101
|
}
|
|
264
102
|
return `${url}${url.includes('?') ? '&' : '?'}file-viewer-docx=${DEFAULT_FILE_VIEWER_DOCX_RUNTIME_VERSION}`;
|
|
265
103
|
};
|
|
266
|
-
export const applyDocxExternalLinkPolicy = (target, policy) => {
|
|
267
|
-
if (policy === 'allow') {
|
|
268
|
-
return 0;
|
|
269
|
-
}
|
|
270
|
-
let blocked = 0;
|
|
271
|
-
target.querySelectorAll('a[href]').forEach(anchor => {
|
|
272
|
-
const href = anchor.getAttribute('href');
|
|
273
|
-
if (!href || href.startsWith('#')) {
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
if (!anchor.hasAttribute('data-docx-external-href')) {
|
|
277
|
-
anchor.setAttribute('data-docx-external-href', href);
|
|
278
|
-
}
|
|
279
|
-
anchor.removeAttribute('href');
|
|
280
|
-
anchor.setAttribute('aria-disabled', 'true');
|
|
281
|
-
blocked += 1;
|
|
282
|
-
});
|
|
283
|
-
return blocked;
|
|
284
|
-
};
|
|
285
104
|
export const createDocxOptions = (target, context, notifyProgressiveRender) => {
|
|
286
105
|
var _a, _b, _c, _d, _e;
|
|
287
106
|
const docxOptions = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.docx;
|
|
@@ -298,21 +117,16 @@ export const createDocxOptions = (target, context, notifyProgressiveRender) => {
|
|
|
298
117
|
const externalResourcePolicy = (_c = docxOptions === null || docxOptions === void 0 ? void 0 : docxOptions.externalResourcePolicy) !== null && _c !== void 0 ? _c : 'block';
|
|
299
118
|
const options = {
|
|
300
119
|
useWorker,
|
|
120
|
+
exposeDisplayTargets: (docxOptions === null || docxOptions === void 0 ? void 0 : docxOptions.exposeDisplayTargets) === true,
|
|
301
121
|
reviewMode: (_d = docxOptions === null || docxOptions === void 0 ? void 0 : docxOptions.reviewMode) !== null && _d !== void 0 ? _d : 'all',
|
|
302
|
-
// Authored page breaks define separate anchor coordinate spaces even in
|
|
303
|
-
// flow mode. Only measured/fixed-height pagination remains opt-in.
|
|
304
122
|
breakPages: true,
|
|
305
123
|
fixedPageHeight: usePagedLayout,
|
|
306
124
|
ignoreLastRenderedPageBreak: (_e = docxOptions === null || docxOptions === void 0 ? void 0 : docxOptions.ignoreLastRenderedPageBreak) !== null && _e !== void 0 ? _e : !usePagedLayout,
|
|
307
125
|
externalLinkPolicy,
|
|
308
126
|
externalResourcePolicy,
|
|
309
127
|
darkMode,
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
applyDocxExternalLinkPolicy(target, externalLinkPolicy);
|
|
313
|
-
}
|
|
314
|
-
progress(event);
|
|
315
|
-
}
|
|
128
|
+
reviewPresentation: 'inline',
|
|
129
|
+
progress
|
|
316
130
|
};
|
|
317
131
|
if (useWorker) {
|
|
318
132
|
options.workerUrl = appendDocxVendorAssetVersion(resolveFileViewerDocxWorkerUrl(docxOptions, documentBaseUrl), !!(docxOptions === null || docxOptions === void 0 ? void 0 : docxOptions.workerUrl));
|
|
@@ -351,6 +165,9 @@ export const createDocxOptions = (target, context, notifyProgressiveRender) => {
|
|
|
351
165
|
if ((docxOptions === null || docxOptions === void 0 ? void 0 : docxOptions.hideWebHiddenContent) !== undefined) {
|
|
352
166
|
options.hideWebHiddenContent = docxOptions.hideWebHiddenContent;
|
|
353
167
|
}
|
|
168
|
+
if ((docxOptions === null || docxOptions === void 0 ? void 0 : docxOptions.renderAltChunks) !== undefined) {
|
|
169
|
+
options.renderAltChunks = docxOptions.renderAltChunks;
|
|
170
|
+
}
|
|
354
171
|
return options;
|
|
355
172
|
};
|
|
356
173
|
const isTargetHTMLElement = (value, target) => {
|
|
@@ -359,8 +176,6 @@ const isTargetHTMLElement = (value, target) => {
|
|
|
359
176
|
return HTMLElementCtor ? value instanceof HTMLElementCtor : value instanceof HTMLElement;
|
|
360
177
|
};
|
|
361
178
|
const DOCX_RESPONSIVE_CSS = `
|
|
362
|
-
/* This component has no review balloon rail, so keep all-markup deletions readable inline. */
|
|
363
|
-
.docx-fit-viewer [data-docx-review-enabled="true"][data-docx-review-mode="all"] del[data-docx-change-kind]{display:inline!important;width:auto!important;max-width:none!important;height:auto!important;overflow:visible!important;line-height:inherit!important;color:var(--docx-review-color,#c2410c);text-decoration:line-through;text-decoration-color:var(--docx-review-color,#c2410c)}
|
|
364
179
|
.docx-fit-viewer {
|
|
365
180
|
box-sizing: border-box;
|
|
366
181
|
height: 100%;
|
|
@@ -402,24 +217,13 @@ const DOCX_RESPONSIVE_CSS = `
|
|
|
402
217
|
top: 0;
|
|
403
218
|
left: 50%;
|
|
404
219
|
margin: 0 !important;
|
|
405
|
-
background: #ffffff !important;
|
|
406
220
|
box-shadow: 0 2px 14px rgba(25, 35, 48, 0.18);
|
|
407
221
|
box-sizing: border-box;
|
|
408
222
|
overflow: hidden;
|
|
409
223
|
transform-origin: top center;
|
|
410
224
|
}
|
|
411
|
-
.docx-fit-viewer .docx-page-background {
|
|
412
|
-
position: absolute;
|
|
413
|
-
inset: 0;
|
|
414
|
-
z-index: 0;
|
|
415
|
-
pointer-events: none;
|
|
416
|
-
background-position: center;
|
|
417
|
-
background-repeat: no-repeat;
|
|
418
|
-
background-size: 100% 100%;
|
|
419
|
-
}
|
|
420
225
|
.docx-fit-viewer[data-docx-dark-mode='true'] .docx-page-frame > section.docx,
|
|
421
226
|
.docx-fit-viewer[data-docx-dark-mode='true'] .docx-flow-frame > section.docx {
|
|
422
|
-
background: rgb(51, 51, 51) !important;
|
|
423
227
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.8);
|
|
424
228
|
outline: 1px solid rgba(255, 255, 255, 0.15);
|
|
425
229
|
outline-offset: -1px;
|
|
@@ -441,28 +245,12 @@ function installResponsiveStyle(target) {
|
|
|
441
245
|
target.prepend(style);
|
|
442
246
|
return style;
|
|
443
247
|
}
|
|
444
|
-
function wrapDocxSections(target, pagedLayout) {
|
|
445
|
-
const wrapper = target.querySelector('.docx-wrapper');
|
|
446
|
-
if (!wrapper) {
|
|
447
|
-
return [];
|
|
448
|
-
}
|
|
449
|
-
return Array.from(wrapper.children).flatMap(child => {
|
|
450
|
-
if (!isTargetHTMLElement(child, target) || !child.matches('section.docx')) {
|
|
451
|
-
return [];
|
|
452
|
-
}
|
|
453
|
-
const frame = target.ownerDocument.createElement('div');
|
|
454
|
-
frame.className = pagedLayout ? 'docx-page-frame' : 'docx-flow-frame';
|
|
455
|
-
child.before(frame);
|
|
456
|
-
frame.appendChild(child);
|
|
457
|
-
return [frame];
|
|
458
|
-
});
|
|
459
|
-
}
|
|
460
248
|
function makeDocxResponsive(target, context) {
|
|
461
249
|
var _a, _b;
|
|
462
250
|
target.classList.add('docx-fit-viewer');
|
|
463
251
|
const style = installResponsiveStyle(target);
|
|
464
252
|
const pagedLayout = ((_b = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.docx) === null || _b === void 0 ? void 0 : _b.visualPagination) === true;
|
|
465
|
-
|
|
253
|
+
let frames = [];
|
|
466
254
|
const view = getTargetWindow(target);
|
|
467
255
|
const ResizeObserverCtor = view === null || view === void 0 ? void 0 : view.ResizeObserver;
|
|
468
256
|
let resizeFrame = 0;
|
|
@@ -471,19 +259,18 @@ function makeDocxResponsive(target, context) {
|
|
|
471
259
|
let currentFitScale = 1;
|
|
472
260
|
const zoomEmitter = createZoomChangeEmitter();
|
|
473
261
|
const clampScale = (scale) => {
|
|
474
|
-
return
|
|
262
|
+
return clampWordScale(scale, DOCX_MIN_SCALE, DOCX_MAX_SCALE);
|
|
475
263
|
};
|
|
476
264
|
const applyResponsiveLayout = () => {
|
|
477
265
|
let firstScale = currentScale;
|
|
478
266
|
let firstFitScale = currentFitScale;
|
|
479
267
|
let measured = false;
|
|
480
|
-
frames.forEach(frame => {
|
|
268
|
+
frames.forEach((frame) => {
|
|
481
269
|
const page = frame.firstElementChild;
|
|
482
270
|
if (!isTargetHTMLElement(page, target)) {
|
|
483
271
|
return;
|
|
484
272
|
}
|
|
485
273
|
page.style.transform = 'translateX(-50%)';
|
|
486
|
-
correctDocxMixedAnchorOrigins(page);
|
|
487
274
|
const pageWidth = page.offsetWidth;
|
|
488
275
|
const contentHeight = pagedLayout
|
|
489
276
|
? page.offsetHeight
|
|
@@ -531,13 +318,17 @@ function makeDocxResponsive(target, context) {
|
|
|
531
318
|
maxScale: DOCX_MAX_SCALE
|
|
532
319
|
});
|
|
533
320
|
const setUserZoom = (nextZoom) => {
|
|
534
|
-
|
|
321
|
+
if (!isPositiveFinite(nextZoom))
|
|
322
|
+
return getZoomState();
|
|
323
|
+
userZoom = nextZoom;
|
|
535
324
|
view === null || view === void 0 ? void 0 : view.cancelAnimationFrame(resizeFrame);
|
|
536
325
|
applyResponsiveLayout();
|
|
537
326
|
return getZoomState();
|
|
538
327
|
};
|
|
539
328
|
const setAbsoluteScale = (scale) => {
|
|
540
|
-
|
|
329
|
+
if (!isPositiveFinite(scale))
|
|
330
|
+
return getZoomState();
|
|
331
|
+
return setUserZoom(clampScale(scale) / currentFitScale);
|
|
541
332
|
};
|
|
542
333
|
const readFitPageSize = () => {
|
|
543
334
|
for (const frame of frames) {
|
|
@@ -545,12 +336,13 @@ function makeDocxResponsive(target, context) {
|
|
|
545
336
|
if (!page) {
|
|
546
337
|
continue;
|
|
547
338
|
}
|
|
548
|
-
|
|
339
|
+
if (!page.offsetWidth || !page.offsetHeight)
|
|
340
|
+
continue;
|
|
549
341
|
return {
|
|
550
|
-
width: page.offsetWidth
|
|
342
|
+
width: page.offsetWidth,
|
|
551
343
|
height: isDocxFlowFrame(frame)
|
|
552
|
-
?
|
|
553
|
-
: page.offsetHeight
|
|
344
|
+
? Math.max(page.scrollHeight, page.offsetHeight)
|
|
345
|
+
: page.offsetHeight
|
|
554
346
|
};
|
|
555
347
|
}
|
|
556
348
|
return null;
|
|
@@ -558,7 +350,8 @@ function makeDocxResponsive(target, context) {
|
|
|
558
350
|
const fitDocx = (request) => {
|
|
559
351
|
var _a, _b;
|
|
560
352
|
const pageSize = readFitPageSize();
|
|
561
|
-
|
|
353
|
+
const viewport = readWordFitViewport(target, request);
|
|
354
|
+
if (!pageSize || !viewport) {
|
|
562
355
|
return {
|
|
563
356
|
applied: false,
|
|
564
357
|
mode: request.mode,
|
|
@@ -571,15 +364,15 @@ function makeDocxResponsive(target, context) {
|
|
|
571
364
|
const mode = request.mode === 'auto' ? 'width' : request.mode;
|
|
572
365
|
const scale = resolveFileViewerFitScale({
|
|
573
366
|
mode,
|
|
574
|
-
viewportWidth:
|
|
575
|
-
viewportHeight:
|
|
367
|
+
viewportWidth: viewport.width,
|
|
368
|
+
viewportHeight: viewport.height,
|
|
576
369
|
contentWidth: pageSize.width,
|
|
577
370
|
contentHeight: pageSize.height,
|
|
578
371
|
currentScale,
|
|
579
372
|
minScale: (_a = request.minScale) !== null && _a !== void 0 ? _a : DOCX_MIN_SCALE,
|
|
580
373
|
maxScale: (_b = request.maxScale) !== null && _b !== void 0 ? _b : DOCX_MAX_SCALE
|
|
581
374
|
});
|
|
582
|
-
if (!scale) {
|
|
375
|
+
if (!scale || !isPositiveFinite(scale)) {
|
|
583
376
|
return {
|
|
584
377
|
applied: false,
|
|
585
378
|
mode: request.mode,
|
|
@@ -611,16 +404,22 @@ function makeDocxResponsive(target, context) {
|
|
|
611
404
|
});
|
|
612
405
|
const observer = ResizeObserverCtor ? new ResizeObserverCtor(resize) : null;
|
|
613
406
|
observer === null || observer === void 0 ? void 0 : observer.observe(target);
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
407
|
+
const disposeFrames = observeDocxFrames(target, pagedLayout, nextFrames => {
|
|
408
|
+
observer === null || observer === void 0 ? void 0 : observer.disconnect();
|
|
409
|
+
observer === null || observer === void 0 ? void 0 : observer.observe(target);
|
|
410
|
+
frames = nextFrames;
|
|
411
|
+
frames.forEach(frame => {
|
|
412
|
+
const page = getDocxPageElement(frame);
|
|
413
|
+
if (page)
|
|
414
|
+
observer === null || observer === void 0 ? void 0 : observer.observe(page);
|
|
415
|
+
});
|
|
416
|
+
resize();
|
|
619
417
|
});
|
|
620
418
|
applyResponsiveLayout();
|
|
621
419
|
return () => {
|
|
622
420
|
view === null || view === void 0 ? void 0 : view.cancelAnimationFrame(resizeFrame);
|
|
623
421
|
observer === null || observer === void 0 ? void 0 : observer.disconnect();
|
|
422
|
+
disposeFrames();
|
|
624
423
|
unregisterFileViewerZoomProvider(target);
|
|
625
424
|
style.remove();
|
|
626
425
|
target.classList.remove('docx-fit-viewer');
|
|
@@ -649,7 +448,7 @@ function getDocxFramePrintSize(frame) {
|
|
|
649
448
|
}
|
|
650
449
|
return {
|
|
651
450
|
width: size.width,
|
|
652
|
-
height: Math.max(page.scrollHeight || 0, page.offsetHeight || 0, DOCX_DEFAULT_PAGE_SIZE.height)
|
|
451
|
+
height: Math.max(page.scrollHeight || 0, page.offsetHeight || 0, readDocxFlowPaperHeight(page, DOCX_DEFAULT_PAGE_SIZE.height))
|
|
653
452
|
};
|
|
654
453
|
}
|
|
655
454
|
function normalizeDocxPageForPrint(frame, pageSize) {
|
|
@@ -687,20 +486,22 @@ function normalizeDocxPageForPrint(frame, pageSize) {
|
|
|
687
486
|
page.style.boxShadow = 'none';
|
|
688
487
|
}
|
|
689
488
|
function buildDocxPrintStyle(target) {
|
|
690
|
-
const
|
|
691
|
-
const
|
|
692
|
-
const
|
|
489
|
+
const frames = Array.from(target.querySelectorAll('.docx-page-frame, .docx-flow-frame, .docx-canvas-sheet'));
|
|
490
|
+
const firstFrame = frames[0];
|
|
491
|
+
const pageSize = getDocxFramePrintSize(firstFrame);
|
|
492
|
+
const selector = isDocxCanvasSheet(firstFrame)
|
|
693
493
|
? '.viewer-export-content .docx-canvas-sheet'
|
|
694
|
-
: (firstFrame
|
|
494
|
+
: isDocxFlowFrame(firstFrame)
|
|
695
495
|
? '.viewer-export-content .docx-flow-frame'
|
|
696
496
|
: '.viewer-export-content .docx-page-frame';
|
|
697
497
|
return buildPrintPageStyle({
|
|
698
498
|
selector,
|
|
699
499
|
width: pageSize.width,
|
|
700
|
-
height: (firstFrame
|
|
701
|
-
? DOCX_DEFAULT_PAGE_SIZE.height
|
|
500
|
+
height: isDocxFlowFrame(firstFrame)
|
|
501
|
+
? readDocxFlowPaperHeight(getDocxPageElement(firstFrame), DOCX_DEFAULT_PAGE_SIZE.height)
|
|
702
502
|
: pageSize.height,
|
|
703
|
-
heightMode: (firstFrame
|
|
503
|
+
heightMode: isDocxFlowFrame(firstFrame) ? 'min' : 'fixed',
|
|
504
|
+
pages: frames.some(isDocxFlowFrame) ? undefined : frames.map(getDocxFramePrintSize)
|
|
704
505
|
});
|
|
705
506
|
}
|
|
706
507
|
async function prepareDocxCloneForExport(target) {
|
|
@@ -718,18 +519,25 @@ async function prepareDocxCloneForExport(target) {
|
|
|
718
519
|
const liveFrames = Array.from(target.querySelectorAll(selector));
|
|
719
520
|
const clone = target.cloneNode(true);
|
|
720
521
|
replaceFileViewerCanvasWithImages(target, clone);
|
|
721
|
-
const
|
|
722
|
-
printDocument
|
|
522
|
+
const engineWrapper = clone.querySelector('.docx-wrapper');
|
|
523
|
+
const printDocument = engineWrapper
|
|
524
|
+
? engineWrapper.cloneNode(false)
|
|
525
|
+
: target.ownerDocument.createElement('div');
|
|
526
|
+
printDocument.classList.add('docx-print-document');
|
|
527
|
+
printDocument.style.padding = '0';
|
|
528
|
+
printDocument.style.background = 'transparent';
|
|
723
529
|
const scopedStyles = Array.from(clone.querySelectorAll('style'))
|
|
724
|
-
.filter(style => { var _a; return !((_a = style.textContent) === null || _a === void 0 ? void 0 : _a.includes('.docx-fit-viewer')); })
|
|
725
|
-
.map(style => style.outerHTML)
|
|
530
|
+
.filter((style) => { var _a; return !((_a = style.textContent) === null || _a === void 0 ? void 0 : _a.includes('.docx-fit-viewer')); })
|
|
531
|
+
.map((style) => style.outerHTML)
|
|
726
532
|
.join('');
|
|
727
533
|
clone.querySelectorAll(selector).forEach((frame, index) => {
|
|
728
534
|
frame.dataset.viewerPrintPageIndex = String(index);
|
|
729
535
|
normalizeDocxPageForPrint(frame, getDocxFramePrintSize(liveFrames[index]));
|
|
730
536
|
printDocument.appendChild(frame.cloneNode(true));
|
|
731
537
|
});
|
|
732
|
-
return printDocument.childElementCount
|
|
538
|
+
return printDocument.childElementCount
|
|
539
|
+
? `${scopedStyles}${printDocument.outerHTML}`
|
|
540
|
+
: clone.innerHTML;
|
|
733
541
|
}
|
|
734
542
|
finally {
|
|
735
543
|
if (materializedForSnapshot && view) {
|
|
@@ -739,9 +547,6 @@ async function prepareDocxCloneForExport(target) {
|
|
|
739
547
|
}
|
|
740
548
|
}
|
|
741
549
|
}
|
|
742
|
-
/**
|
|
743
|
-
* 渲染docx文件
|
|
744
|
-
*/
|
|
745
550
|
export default async function (buffer, target, context) {
|
|
746
551
|
var _a, _b;
|
|
747
552
|
assertValidDocxPackage(buffer, context);
|
|
@@ -756,20 +561,10 @@ export default async function (buffer, target, context) {
|
|
|
756
561
|
(_a = context === null || context === void 0 ? void 0 : context.onProgressiveRender) === null || _a === void 0 ? void 0 : _a.call(context);
|
|
757
562
|
};
|
|
758
563
|
const docxOptions = createDocxOptions(target, context, notifyProgressiveRender);
|
|
759
|
-
const
|
|
760
|
-
loadLibrary(),
|
|
761
|
-
resolveDocxPageBackgroundImage(buffer, () => createTargetXmlParser(target))
|
|
762
|
-
]);
|
|
564
|
+
const { defaultOptions, renderAsync, disposeRenderedDocument } = await loadLibrary();
|
|
763
565
|
target.dataset.docxWorker = docxOptions.useWorker ? 'self' : 'false';
|
|
764
566
|
target.dataset.docxDarkMode = docxOptions.darkMode ? 'true' : 'false';
|
|
765
|
-
|
|
766
|
-
...defaultOptions,
|
|
767
|
-
...docxOptions
|
|
768
|
-
});
|
|
769
|
-
applyDocxExternalLinkPolicy(target, docxOptions.externalLinkPolicy);
|
|
770
|
-
target.dataset.docxHeaderFooterFallback = usedHeaderFooterFallback ? 'true' : 'false';
|
|
771
|
-
target.dataset.docxPageBackground =
|
|
772
|
-
applyDocxPageBackgroundImage(target, pageBackgroundImage) > 0 ? 'true' : 'false';
|
|
567
|
+
await renderAsync(buffer, target, undefined, { ...defaultOptions, ...docxOptions });
|
|
773
568
|
notifyProgressiveRender();
|
|
774
569
|
const disposeResponsive = makeDocxResponsive(target, context);
|
|
775
570
|
(_a = context === null || context === void 0 ? void 0 : context.registerExportAdapter) === null || _a === void 0 ? void 0 : _a.call(context, {
|
|
@@ -794,10 +589,9 @@ export default async function (buffer, target, context) {
|
|
|
794
589
|
(_a = context === null || context === void 0 ? void 0 : context.registerExportAdapter) === null || _a === void 0 ? void 0 : _a.call(context, null);
|
|
795
590
|
(_b = context === null || context === void 0 ? void 0 : context.registerThumbnailAdapter) === null || _b === void 0 ? void 0 : _b.call(context, null);
|
|
796
591
|
disposeResponsive();
|
|
592
|
+
disposeRenderedDocument(target);
|
|
797
593
|
delete target.dataset.docxWorker;
|
|
798
594
|
delete target.dataset.docxDarkMode;
|
|
799
|
-
delete target.dataset.docxHeaderFooterFallback;
|
|
800
|
-
delete target.dataset.docxPageBackground;
|
|
801
595
|
target.replaceChildren();
|
|
802
596
|
}
|
|
803
597
|
};
|
package/dist/wordMl.d.ts
CHANGED
|
@@ -1,8 +1,2 @@
|
|
|
1
1
|
export declare function decodeWordMlBytes(buffer: ArrayBuffer): string;
|
|
2
|
-
/**
|
|
3
|
-
* Adapt the single-file Word 2003 XML container to the existing OOXML renderer.
|
|
4
|
-
* This is structural conversion, not HTML insertion or remote conversion. Body,
|
|
5
|
-
* styles, table properties, section geometry and VML remain renderer-owned.
|
|
6
|
-
* Inline headers/footers become package parts; only embedded image data is read.
|
|
7
|
-
*/
|
|
8
2
|
export declare function convertWordMlToDocx(buffer: ArrayBuffer, target: HTMLElement): Promise<ArrayBuffer>;
|