mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2026-04-12 02:57:22 +00:00
Merge pull request #729 from kekingcn/copilot/fix-pdf-preview-error
Restore mobile PDF preview compatibility for browsers without `Array.prototype.at`
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
* pdfjsVersion = 5.4.530
|
||||
* pdfjsBuild = 50cc4adac
|
||||
*/
|
||||
import "../web/compatibility.mjs";
|
||||
/******/ var __webpack_modules__ = ({
|
||||
|
||||
/***/ 34:
|
||||
|
||||
42
server/src/main/resources/static/pdfjs/web/compatibility.mjs
Normal file
42
server/src/main/resources/static/pdfjs/web/compatibility.mjs
Normal file
@@ -0,0 +1,42 @@
|
||||
const atTargets = [
|
||||
Array,
|
||||
String,
|
||||
Int8Array,
|
||||
Uint8Array,
|
||||
Uint8ClampedArray,
|
||||
Int16Array,
|
||||
Uint16Array,
|
||||
Int32Array,
|
||||
Uint32Array,
|
||||
Float32Array,
|
||||
Float64Array,
|
||||
globalThis.BigInt64Array,
|
||||
globalThis.BigUint64Array,
|
||||
];
|
||||
|
||||
function installAtPolyfill(target) {
|
||||
if (!target?.prototype || typeof target.prototype.at === "function") {
|
||||
return;
|
||||
}
|
||||
Object.defineProperty(target.prototype, "at", {
|
||||
value(index) {
|
||||
const length = this.length >>> 0;
|
||||
let relativeIndex = Number(index);
|
||||
if (Number.isNaN(relativeIndex)) {
|
||||
relativeIndex = 0;
|
||||
}
|
||||
relativeIndex = Math.trunc(relativeIndex);
|
||||
const resolvedIndex = relativeIndex >= 0 ? relativeIndex : length + relativeIndex;
|
||||
if (resolvedIndex < 0 || resolvedIndex >= length) {
|
||||
return undefined;
|
||||
}
|
||||
return this[resolvedIndex];
|
||||
},
|
||||
writable: true,
|
||||
configurable: true,
|
||||
});
|
||||
}
|
||||
|
||||
for (const target of atTargets) {
|
||||
installAtPolyfill(target);
|
||||
}
|
||||
@@ -29,6 +29,7 @@ See https://github.com/adobe-type-tools/cmap-resources
|
||||
|
||||
<!-- This snippet is used in production (included from viewer.html) -->
|
||||
<link rel="resource" type="application/l10n" href="locale/locale.json" />
|
||||
<script src="compatibility.mjs" type="module"></script>
|
||||
<script src="../build/pdf.mjs" type="module"></script>
|
||||
|
||||
<link rel="stylesheet" href="viewer.css" />
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package cn.keking;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class PdfViewerCompatibilityTests {
|
||||
|
||||
@Test
|
||||
void shouldLoadCompatibilityModuleBeforePdfJs() throws IOException {
|
||||
String viewerHtml = readResource("/static/pdfjs/web/viewer.html");
|
||||
|
||||
assertTrue(viewerHtml.contains("<script src=\"compatibility.mjs\" type=\"module\"></script>"));
|
||||
assertTrue(viewerHtml.indexOf("compatibility.mjs") < viewerHtml.indexOf("../build/pdf.mjs"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldLoadCompatibilityModuleInPdfWorker() throws IOException {
|
||||
String workerScript = readResource("/static/pdfjs/build/pdf.worker.mjs");
|
||||
|
||||
assertTrue(workerScript.contains("import \"../web/compatibility.mjs\";"));
|
||||
}
|
||||
|
||||
private String readResource(String resourcePath) throws IOException {
|
||||
try (InputStream inputStream = getClass().getResourceAsStream(resourcePath)) {
|
||||
assertNotNull(inputStream);
|
||||
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user