@open-file-viewer/core 0.1.19 → 0.1.20
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.cjs +100 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +100 -9
- package/dist/index.js.map +1 -1
- package/dist/style.css +98 -59
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -4637,7 +4637,6 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4637
4637
|
options.viewport.append(viewer);
|
|
4638
4638
|
const showDocumentFallback = (error) => {
|
|
4639
4639
|
viewer.remove();
|
|
4640
|
-
options.viewport.classList.add("ofv-center");
|
|
4641
4640
|
const fileLike = {
|
|
4642
4641
|
source: options.fileUrl,
|
|
4643
4642
|
name: options.fileName,
|
|
@@ -4651,6 +4650,9 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4651
4650
|
message: options.encryptedMessage || "\u8BF7\u4E0B\u8F7D\u540E\u4F7F\u7528\u5BC6\u7801\u6253\u5F00\uFF0C\u6216\u4E0A\u4F20\u89E3\u5BC6\u540E\u7684 PDF \u6587\u4EF6\u3002",
|
|
4652
4651
|
action: options.encryptedAction || "\u4E0B\u8F7D PDF"
|
|
4653
4652
|
}) : createPdfFallback(options.fileName, options.fileUrl, normalizePdfError(error), options.fallbackTitle);
|
|
4653
|
+
if (!fallback.classList.contains("ofv-pdf-web-fallback")) {
|
|
4654
|
+
options.viewport.classList.add("ofv-center");
|
|
4655
|
+
}
|
|
4654
4656
|
options.viewport.append(fallback);
|
|
4655
4657
|
};
|
|
4656
4658
|
let documentTask;
|
|
@@ -5056,6 +5058,9 @@ function createPageStatus(className, text) {
|
|
|
5056
5058
|
return status;
|
|
5057
5059
|
}
|
|
5058
5060
|
function createPdfFallback(fileName, url, message, titleText = "PDF \u9884\u89C8\u5931\u8D25") {
|
|
5061
|
+
if (isEmbeddableRemoteUrl(url)) {
|
|
5062
|
+
return createPdfWebFallback(fileName, url);
|
|
5063
|
+
}
|
|
5059
5064
|
const fallback = document.createElement("div");
|
|
5060
5065
|
fallback.className = "ofv-fallback";
|
|
5061
5066
|
const title = document.createElement("strong");
|
|
@@ -5069,6 +5074,21 @@ function createPdfFallback(fileName, url, message, titleText = "PDF \u9884\u89C8
|
|
|
5069
5074
|
fallback.append(title, meta, download);
|
|
5070
5075
|
return fallback;
|
|
5071
5076
|
}
|
|
5077
|
+
function createPdfWebFallback(fileName, url) {
|
|
5078
|
+
const fallback = document.createElement("div");
|
|
5079
|
+
fallback.className = "ofv-pdf-web-fallback";
|
|
5080
|
+
const iframe = document.createElement("iframe");
|
|
5081
|
+
iframe.className = "ofv-pdf-web-fallback-frame";
|
|
5082
|
+
iframe.src = url;
|
|
5083
|
+
iframe.title = `${fileName} HTML preview`;
|
|
5084
|
+
iframe.referrerPolicy = "no-referrer";
|
|
5085
|
+
iframe.setAttribute("sandbox", "allow-forms allow-popups allow-presentation allow-same-origin");
|
|
5086
|
+
fallback.append(iframe);
|
|
5087
|
+
return fallback;
|
|
5088
|
+
}
|
|
5089
|
+
function isEmbeddableRemoteUrl(url) {
|
|
5090
|
+
return /^https?:\/\//i.test(url);
|
|
5091
|
+
}
|
|
5072
5092
|
function normalizePdfError(error) {
|
|
5073
5093
|
const message = error instanceof Error ? error.message : String(error || "");
|
|
5074
5094
|
const name = typeof error === "object" && error !== null && "name" in error ? String(error.name) : "";
|
|
@@ -5952,14 +5972,17 @@ function renderLegacyWordDocument(panel, document2) {
|
|
|
5952
5972
|
}
|
|
5953
5973
|
meta.hidden = true;
|
|
5954
5974
|
page.append(meta);
|
|
5955
|
-
appendBlocksToPage(page, pages[0] || [], document2.layout);
|
|
5975
|
+
let nextLineNumber = appendBlocksToPage(page, pages[0] || [], document2.layout, document2.layout.headerBrand === "oasis" ? 2 : 1);
|
|
5976
|
+
if (document2.layout.headerBrand === "oasis") {
|
|
5977
|
+
nextLineNumber -= 1;
|
|
5978
|
+
}
|
|
5956
5979
|
appendWarnings(page, document2);
|
|
5957
5980
|
article.append(page);
|
|
5958
5981
|
for (const pageBlocks of pages.slice(1)) {
|
|
5959
5982
|
const nextPage = window.document.createElement("section");
|
|
5960
5983
|
nextPage.className = "ofv-msdoc-page";
|
|
5961
5984
|
appendPageChrome(nextPage, document2, article.children.length + 1, pageCount);
|
|
5962
|
-
appendBlocksToPage(nextPage, pageBlocks, document2.layout);
|
|
5985
|
+
nextLineNumber = appendBlocksToPage(nextPage, pageBlocks, document2.layout, nextLineNumber);
|
|
5963
5986
|
article.append(nextPage);
|
|
5964
5987
|
}
|
|
5965
5988
|
panel.append(article);
|
|
@@ -6154,26 +6177,31 @@ function appendPageChrome(page, document2, pageNumber, pageCount) {
|
|
|
6154
6177
|
page.classList.add("ofv-msdoc-line-numbered");
|
|
6155
6178
|
}
|
|
6156
6179
|
page.setAttribute("aria-label", document2.title || "Word \u6587\u6863");
|
|
6157
|
-
if (document2.layout.headerBrand === "oasis") {
|
|
6180
|
+
if (document2.layout.headerBrand === "oasis" && pageNumber === 1) {
|
|
6158
6181
|
page.append(createOasisHeader(document2.assets.find((asset) => asset.id === document2.layout.headerImageId)));
|
|
6159
6182
|
}
|
|
6160
6183
|
if (document2.layout.footer) {
|
|
6161
6184
|
page.append(createPageFooter(document2.layout.footer, pageNumber, pageCount));
|
|
6162
6185
|
}
|
|
6163
6186
|
}
|
|
6164
|
-
function appendBlocksToPage(page, blocks, layout) {
|
|
6165
|
-
let lineNumber =
|
|
6187
|
+
function appendBlocksToPage(page, blocks, layout, startLineNumber = 1) {
|
|
6188
|
+
let lineNumber = startLineNumber;
|
|
6166
6189
|
for (const block of blocks) {
|
|
6167
6190
|
if (block.type === "pageBreak") {
|
|
6168
6191
|
continue;
|
|
6169
6192
|
}
|
|
6170
6193
|
const element = renderWordBlock(block);
|
|
6194
|
+
if (layout.headerBrand === "oasis" && block.type === "heading") {
|
|
6195
|
+
const prefix = getOasisHeadingPrefix(block.text);
|
|
6196
|
+
if (prefix) element.prepend(`${prefix} `);
|
|
6197
|
+
}
|
|
6171
6198
|
if (layout.lineNumbers && element instanceof HTMLElement && !element.classList.contains("ofv-msdoc-page-header")) {
|
|
6172
6199
|
element.dataset.line = String(lineNumber);
|
|
6173
6200
|
lineNumber += estimatedLineCount(block);
|
|
6174
6201
|
}
|
|
6175
6202
|
page.append(element);
|
|
6176
6203
|
}
|
|
6204
|
+
return lineNumber;
|
|
6177
6205
|
}
|
|
6178
6206
|
function appendWarnings(page, document2) {
|
|
6179
6207
|
if (document2.warnings.length === 0) {
|
|
@@ -6286,15 +6314,37 @@ function renderWordBlock(block) {
|
|
|
6286
6314
|
return table;
|
|
6287
6315
|
}
|
|
6288
6316
|
if (block.type === "toc") {
|
|
6317
|
+
if (/^(?:\d+\s+){2,}\d+$/.test(block.title) && /^\d+$/.test(block.page || "")) {
|
|
6318
|
+
const ruler = window.document.createElement("p");
|
|
6319
|
+
ruler.className = "ofv-msdoc-code ofv-msdoc-code-ruler";
|
|
6320
|
+
for (const value of `${block.title} ${block.page}`.split(/\s+/)) {
|
|
6321
|
+
const mark = window.document.createElement("span");
|
|
6322
|
+
mark.textContent = value;
|
|
6323
|
+
ruler.append(mark);
|
|
6324
|
+
}
|
|
6325
|
+
return ruler;
|
|
6326
|
+
}
|
|
6289
6327
|
const paragraph2 = window.document.createElement("p");
|
|
6290
6328
|
paragraph2.className = `ofv-msdoc-toc ofv-msdoc-toc-level-${block.level}`;
|
|
6291
6329
|
const title = window.document.createElement("span");
|
|
6292
|
-
title.
|
|
6330
|
+
title.className = "ofv-msdoc-toc-title";
|
|
6331
|
+
const numberedTitle = block.title.match(/^(\d+(?:\.\d+)*)\s+(.+)$/);
|
|
6332
|
+
if (numberedTitle) {
|
|
6333
|
+
const number = window.document.createElement("span");
|
|
6334
|
+
number.className = "ofv-msdoc-toc-number";
|
|
6335
|
+
number.textContent = numberedTitle[1];
|
|
6336
|
+
title.append(number, numberedTitle[2]);
|
|
6337
|
+
} else {
|
|
6338
|
+
title.textContent = block.title;
|
|
6339
|
+
}
|
|
6293
6340
|
paragraph2.append(title);
|
|
6294
6341
|
if (block.page) {
|
|
6342
|
+
const leader = window.document.createElement("span");
|
|
6343
|
+
leader.className = "ofv-msdoc-toc-leader";
|
|
6295
6344
|
const page = window.document.createElement("span");
|
|
6345
|
+
page.className = "ofv-msdoc-toc-page";
|
|
6296
6346
|
page.textContent = block.page;
|
|
6297
|
-
paragraph2.append(page);
|
|
6347
|
+
paragraph2.append(leader, page);
|
|
6298
6348
|
}
|
|
6299
6349
|
return paragraph2;
|
|
6300
6350
|
}
|
|
@@ -6305,6 +6355,24 @@ function renderWordBlock(block) {
|
|
|
6305
6355
|
appendInlineRuns(paragraph, block.text, block.type === "code");
|
|
6306
6356
|
return paragraph;
|
|
6307
6357
|
}
|
|
6358
|
+
function getOasisHeadingPrefix(text) {
|
|
6359
|
+
const headings = {
|
|
6360
|
+
Introduction: "1",
|
|
6361
|
+
Terminology: "1.1",
|
|
6362
|
+
"Word Styles": "2",
|
|
6363
|
+
"Overall Style": "2.1",
|
|
6364
|
+
"Title Page": "2.2",
|
|
6365
|
+
Headings: "2.3",
|
|
6366
|
+
Paragraphs: "2.4",
|
|
6367
|
+
Lists: "2.5",
|
|
6368
|
+
Tables: "2.6",
|
|
6369
|
+
"Code Examples": "2.7",
|
|
6370
|
+
"Character Styles": "2.8",
|
|
6371
|
+
References: "3",
|
|
6372
|
+
Normative: "3.1"
|
|
6373
|
+
};
|
|
6374
|
+
return headings[text];
|
|
6375
|
+
}
|
|
6308
6376
|
function appendInlineRuns(element, text, preserveTabs = false) {
|
|
6309
6377
|
if (preserveTabs) {
|
|
6310
6378
|
appendInlineText(element, text, true);
|
|
@@ -6976,7 +7044,9 @@ function findValueAfterLabel(paragraphs, label) {
|
|
|
6976
7044
|
return paragraphs.slice(index + 1).find((paragraph) => paragraph !== WORD_PAGE_BREAK && paragraph.length > 0);
|
|
6977
7045
|
}
|
|
6978
7046
|
function paginateWordBlocks(blocks, layout) {
|
|
6979
|
-
|
|
7047
|
+
if (layout.headerBrand === "oasis") {
|
|
7048
|
+
return paginateOasisBlocks(blocks);
|
|
7049
|
+
}
|
|
6980
7050
|
const pages = [];
|
|
6981
7051
|
let current = [];
|
|
6982
7052
|
let usedLines = 0;
|
|
@@ -6989,6 +7059,7 @@ function paginateWordBlocks(blocks, layout) {
|
|
|
6989
7059
|
usedLines = 0;
|
|
6990
7060
|
continue;
|
|
6991
7061
|
}
|
|
7062
|
+
const maxLines = layout.lineNumbers ? 33 : 46;
|
|
6992
7063
|
const lines = estimatedLineCount(block);
|
|
6993
7064
|
const shouldBreak = current.length > 0 && (usedLines + lines > maxLines || block.type === "heading" && usedLines > Math.floor(maxLines * 0.72));
|
|
6994
7065
|
if (shouldBreak) {
|
|
@@ -7004,6 +7075,26 @@ function paginateWordBlocks(blocks, layout) {
|
|
|
7004
7075
|
}
|
|
7005
7076
|
return pages;
|
|
7006
7077
|
}
|
|
7078
|
+
function paginateOasisBlocks(blocks) {
|
|
7079
|
+
const pages = [];
|
|
7080
|
+
let current = [];
|
|
7081
|
+
const flush = () => {
|
|
7082
|
+
if (current.length > 0) pages.push(current);
|
|
7083
|
+
current = [];
|
|
7084
|
+
};
|
|
7085
|
+
for (const block of blocks) {
|
|
7086
|
+
if (block.type === "pageBreak") {
|
|
7087
|
+
flush();
|
|
7088
|
+
continue;
|
|
7089
|
+
}
|
|
7090
|
+
const startsStyledPage = block.type === "heading" && block.level === 1;
|
|
7091
|
+
const startsCodeExamplesPage = block.type === "paragraph" && /^For bibliography lists,/i.test(block.text);
|
|
7092
|
+
if (current.length > 0 && (startsStyledPage || startsCodeExamplesPage)) flush();
|
|
7093
|
+
current.push(block);
|
|
7094
|
+
}
|
|
7095
|
+
flush();
|
|
7096
|
+
return pages.length > 0 ? pages : [[]];
|
|
7097
|
+
}
|
|
7007
7098
|
function estimatedLineCount(block) {
|
|
7008
7099
|
if (block.type === "pageBreak") {
|
|
7009
7100
|
return 0;
|