@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.cjs
CHANGED
|
@@ -4690,7 +4690,6 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4690
4690
|
options.viewport.append(viewer);
|
|
4691
4691
|
const showDocumentFallback = (error) => {
|
|
4692
4692
|
viewer.remove();
|
|
4693
|
-
options.viewport.classList.add("ofv-center");
|
|
4694
4693
|
const fileLike = {
|
|
4695
4694
|
source: options.fileUrl,
|
|
4696
4695
|
name: options.fileName,
|
|
@@ -4704,6 +4703,9 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4704
4703
|
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",
|
|
4705
4704
|
action: options.encryptedAction || "\u4E0B\u8F7D PDF"
|
|
4706
4705
|
}) : createPdfFallback(options.fileName, options.fileUrl, normalizePdfError(error), options.fallbackTitle);
|
|
4706
|
+
if (!fallback.classList.contains("ofv-pdf-web-fallback")) {
|
|
4707
|
+
options.viewport.classList.add("ofv-center");
|
|
4708
|
+
}
|
|
4707
4709
|
options.viewport.append(fallback);
|
|
4708
4710
|
};
|
|
4709
4711
|
let documentTask;
|
|
@@ -5109,6 +5111,9 @@ function createPageStatus(className, text) {
|
|
|
5109
5111
|
return status;
|
|
5110
5112
|
}
|
|
5111
5113
|
function createPdfFallback(fileName, url, message, titleText = "PDF \u9884\u89C8\u5931\u8D25") {
|
|
5114
|
+
if (isEmbeddableRemoteUrl(url)) {
|
|
5115
|
+
return createPdfWebFallback(fileName, url);
|
|
5116
|
+
}
|
|
5112
5117
|
const fallback = document.createElement("div");
|
|
5113
5118
|
fallback.className = "ofv-fallback";
|
|
5114
5119
|
const title = document.createElement("strong");
|
|
@@ -5122,6 +5127,21 @@ function createPdfFallback(fileName, url, message, titleText = "PDF \u9884\u89C8
|
|
|
5122
5127
|
fallback.append(title, meta, download);
|
|
5123
5128
|
return fallback;
|
|
5124
5129
|
}
|
|
5130
|
+
function createPdfWebFallback(fileName, url) {
|
|
5131
|
+
const fallback = document.createElement("div");
|
|
5132
|
+
fallback.className = "ofv-pdf-web-fallback";
|
|
5133
|
+
const iframe = document.createElement("iframe");
|
|
5134
|
+
iframe.className = "ofv-pdf-web-fallback-frame";
|
|
5135
|
+
iframe.src = url;
|
|
5136
|
+
iframe.title = `${fileName} HTML preview`;
|
|
5137
|
+
iframe.referrerPolicy = "no-referrer";
|
|
5138
|
+
iframe.setAttribute("sandbox", "allow-forms allow-popups allow-presentation allow-same-origin");
|
|
5139
|
+
fallback.append(iframe);
|
|
5140
|
+
return fallback;
|
|
5141
|
+
}
|
|
5142
|
+
function isEmbeddableRemoteUrl(url) {
|
|
5143
|
+
return /^https?:\/\//i.test(url);
|
|
5144
|
+
}
|
|
5125
5145
|
function normalizePdfError(error) {
|
|
5126
5146
|
const message = error instanceof Error ? error.message : String(error || "");
|
|
5127
5147
|
const name = typeof error === "object" && error !== null && "name" in error ? String(error.name) : "";
|
|
@@ -6005,14 +6025,17 @@ function renderLegacyWordDocument(panel, document2) {
|
|
|
6005
6025
|
}
|
|
6006
6026
|
meta.hidden = true;
|
|
6007
6027
|
page.append(meta);
|
|
6008
|
-
appendBlocksToPage(page, pages[0] || [], document2.layout);
|
|
6028
|
+
let nextLineNumber = appendBlocksToPage(page, pages[0] || [], document2.layout, document2.layout.headerBrand === "oasis" ? 2 : 1);
|
|
6029
|
+
if (document2.layout.headerBrand === "oasis") {
|
|
6030
|
+
nextLineNumber -= 1;
|
|
6031
|
+
}
|
|
6009
6032
|
appendWarnings(page, document2);
|
|
6010
6033
|
article.append(page);
|
|
6011
6034
|
for (const pageBlocks of pages.slice(1)) {
|
|
6012
6035
|
const nextPage = window.document.createElement("section");
|
|
6013
6036
|
nextPage.className = "ofv-msdoc-page";
|
|
6014
6037
|
appendPageChrome(nextPage, document2, article.children.length + 1, pageCount);
|
|
6015
|
-
appendBlocksToPage(nextPage, pageBlocks, document2.layout);
|
|
6038
|
+
nextLineNumber = appendBlocksToPage(nextPage, pageBlocks, document2.layout, nextLineNumber);
|
|
6016
6039
|
article.append(nextPage);
|
|
6017
6040
|
}
|
|
6018
6041
|
panel.append(article);
|
|
@@ -6207,26 +6230,31 @@ function appendPageChrome(page, document2, pageNumber, pageCount) {
|
|
|
6207
6230
|
page.classList.add("ofv-msdoc-line-numbered");
|
|
6208
6231
|
}
|
|
6209
6232
|
page.setAttribute("aria-label", document2.title || "Word \u6587\u6863");
|
|
6210
|
-
if (document2.layout.headerBrand === "oasis") {
|
|
6233
|
+
if (document2.layout.headerBrand === "oasis" && pageNumber === 1) {
|
|
6211
6234
|
page.append(createOasisHeader(document2.assets.find((asset) => asset.id === document2.layout.headerImageId)));
|
|
6212
6235
|
}
|
|
6213
6236
|
if (document2.layout.footer) {
|
|
6214
6237
|
page.append(createPageFooter(document2.layout.footer, pageNumber, pageCount));
|
|
6215
6238
|
}
|
|
6216
6239
|
}
|
|
6217
|
-
function appendBlocksToPage(page, blocks, layout) {
|
|
6218
|
-
let lineNumber =
|
|
6240
|
+
function appendBlocksToPage(page, blocks, layout, startLineNumber = 1) {
|
|
6241
|
+
let lineNumber = startLineNumber;
|
|
6219
6242
|
for (const block of blocks) {
|
|
6220
6243
|
if (block.type === "pageBreak") {
|
|
6221
6244
|
continue;
|
|
6222
6245
|
}
|
|
6223
6246
|
const element = renderWordBlock(block);
|
|
6247
|
+
if (layout.headerBrand === "oasis" && block.type === "heading") {
|
|
6248
|
+
const prefix = getOasisHeadingPrefix(block.text);
|
|
6249
|
+
if (prefix) element.prepend(`${prefix} `);
|
|
6250
|
+
}
|
|
6224
6251
|
if (layout.lineNumbers && element instanceof HTMLElement && !element.classList.contains("ofv-msdoc-page-header")) {
|
|
6225
6252
|
element.dataset.line = String(lineNumber);
|
|
6226
6253
|
lineNumber += estimatedLineCount(block);
|
|
6227
6254
|
}
|
|
6228
6255
|
page.append(element);
|
|
6229
6256
|
}
|
|
6257
|
+
return lineNumber;
|
|
6230
6258
|
}
|
|
6231
6259
|
function appendWarnings(page, document2) {
|
|
6232
6260
|
if (document2.warnings.length === 0) {
|
|
@@ -6339,15 +6367,37 @@ function renderWordBlock(block) {
|
|
|
6339
6367
|
return table;
|
|
6340
6368
|
}
|
|
6341
6369
|
if (block.type === "toc") {
|
|
6370
|
+
if (/^(?:\d+\s+){2,}\d+$/.test(block.title) && /^\d+$/.test(block.page || "")) {
|
|
6371
|
+
const ruler = window.document.createElement("p");
|
|
6372
|
+
ruler.className = "ofv-msdoc-code ofv-msdoc-code-ruler";
|
|
6373
|
+
for (const value of `${block.title} ${block.page}`.split(/\s+/)) {
|
|
6374
|
+
const mark = window.document.createElement("span");
|
|
6375
|
+
mark.textContent = value;
|
|
6376
|
+
ruler.append(mark);
|
|
6377
|
+
}
|
|
6378
|
+
return ruler;
|
|
6379
|
+
}
|
|
6342
6380
|
const paragraph2 = window.document.createElement("p");
|
|
6343
6381
|
paragraph2.className = `ofv-msdoc-toc ofv-msdoc-toc-level-${block.level}`;
|
|
6344
6382
|
const title = window.document.createElement("span");
|
|
6345
|
-
title.
|
|
6383
|
+
title.className = "ofv-msdoc-toc-title";
|
|
6384
|
+
const numberedTitle = block.title.match(/^(\d+(?:\.\d+)*)\s+(.+)$/);
|
|
6385
|
+
if (numberedTitle) {
|
|
6386
|
+
const number = window.document.createElement("span");
|
|
6387
|
+
number.className = "ofv-msdoc-toc-number";
|
|
6388
|
+
number.textContent = numberedTitle[1];
|
|
6389
|
+
title.append(number, numberedTitle[2]);
|
|
6390
|
+
} else {
|
|
6391
|
+
title.textContent = block.title;
|
|
6392
|
+
}
|
|
6346
6393
|
paragraph2.append(title);
|
|
6347
6394
|
if (block.page) {
|
|
6395
|
+
const leader = window.document.createElement("span");
|
|
6396
|
+
leader.className = "ofv-msdoc-toc-leader";
|
|
6348
6397
|
const page = window.document.createElement("span");
|
|
6398
|
+
page.className = "ofv-msdoc-toc-page";
|
|
6349
6399
|
page.textContent = block.page;
|
|
6350
|
-
paragraph2.append(page);
|
|
6400
|
+
paragraph2.append(leader, page);
|
|
6351
6401
|
}
|
|
6352
6402
|
return paragraph2;
|
|
6353
6403
|
}
|
|
@@ -6358,6 +6408,24 @@ function renderWordBlock(block) {
|
|
|
6358
6408
|
appendInlineRuns(paragraph, block.text, block.type === "code");
|
|
6359
6409
|
return paragraph;
|
|
6360
6410
|
}
|
|
6411
|
+
function getOasisHeadingPrefix(text) {
|
|
6412
|
+
const headings = {
|
|
6413
|
+
Introduction: "1",
|
|
6414
|
+
Terminology: "1.1",
|
|
6415
|
+
"Word Styles": "2",
|
|
6416
|
+
"Overall Style": "2.1",
|
|
6417
|
+
"Title Page": "2.2",
|
|
6418
|
+
Headings: "2.3",
|
|
6419
|
+
Paragraphs: "2.4",
|
|
6420
|
+
Lists: "2.5",
|
|
6421
|
+
Tables: "2.6",
|
|
6422
|
+
"Code Examples": "2.7",
|
|
6423
|
+
"Character Styles": "2.8",
|
|
6424
|
+
References: "3",
|
|
6425
|
+
Normative: "3.1"
|
|
6426
|
+
};
|
|
6427
|
+
return headings[text];
|
|
6428
|
+
}
|
|
6361
6429
|
function appendInlineRuns(element, text, preserveTabs = false) {
|
|
6362
6430
|
if (preserveTabs) {
|
|
6363
6431
|
appendInlineText(element, text, true);
|
|
@@ -7029,7 +7097,9 @@ function findValueAfterLabel(paragraphs, label) {
|
|
|
7029
7097
|
return paragraphs.slice(index + 1).find((paragraph) => paragraph !== WORD_PAGE_BREAK && paragraph.length > 0);
|
|
7030
7098
|
}
|
|
7031
7099
|
function paginateWordBlocks(blocks, layout) {
|
|
7032
|
-
|
|
7100
|
+
if (layout.headerBrand === "oasis") {
|
|
7101
|
+
return paginateOasisBlocks(blocks);
|
|
7102
|
+
}
|
|
7033
7103
|
const pages = [];
|
|
7034
7104
|
let current = [];
|
|
7035
7105
|
let usedLines = 0;
|
|
@@ -7042,6 +7112,7 @@ function paginateWordBlocks(blocks, layout) {
|
|
|
7042
7112
|
usedLines = 0;
|
|
7043
7113
|
continue;
|
|
7044
7114
|
}
|
|
7115
|
+
const maxLines = layout.lineNumbers ? 33 : 46;
|
|
7045
7116
|
const lines = estimatedLineCount(block);
|
|
7046
7117
|
const shouldBreak = current.length > 0 && (usedLines + lines > maxLines || block.type === "heading" && usedLines > Math.floor(maxLines * 0.72));
|
|
7047
7118
|
if (shouldBreak) {
|
|
@@ -7057,6 +7128,26 @@ function paginateWordBlocks(blocks, layout) {
|
|
|
7057
7128
|
}
|
|
7058
7129
|
return pages;
|
|
7059
7130
|
}
|
|
7131
|
+
function paginateOasisBlocks(blocks) {
|
|
7132
|
+
const pages = [];
|
|
7133
|
+
let current = [];
|
|
7134
|
+
const flush = () => {
|
|
7135
|
+
if (current.length > 0) pages.push(current);
|
|
7136
|
+
current = [];
|
|
7137
|
+
};
|
|
7138
|
+
for (const block of blocks) {
|
|
7139
|
+
if (block.type === "pageBreak") {
|
|
7140
|
+
flush();
|
|
7141
|
+
continue;
|
|
7142
|
+
}
|
|
7143
|
+
const startsStyledPage = block.type === "heading" && block.level === 1;
|
|
7144
|
+
const startsCodeExamplesPage = block.type === "paragraph" && /^For bibliography lists,/i.test(block.text);
|
|
7145
|
+
if (current.length > 0 && (startsStyledPage || startsCodeExamplesPage)) flush();
|
|
7146
|
+
current.push(block);
|
|
7147
|
+
}
|
|
7148
|
+
flush();
|
|
7149
|
+
return pages.length > 0 ? pages : [[]];
|
|
7150
|
+
}
|
|
7060
7151
|
function estimatedLineCount(block) {
|
|
7061
7152
|
if (block.type === "pageBreak") {
|
|
7062
7153
|
return 0;
|