Skip to content

Commit

Permalink
viewer: use esbuild to bundle pdf viewer and worker
Browse files Browse the repository at this point in the history
This improves maintainability:
1. no need to hardcode each script file in Android code
2. no need to symlink pdf.js and pdf.worker.js
3. adds sourcemaps in debug builds to help debug some pdf.js related
   issues
  • Loading branch information
octocorvus authored and thestinger committed Sep 16, 2023
1 parent ddce9b7 commit 627a566
Show file tree
Hide file tree
Showing 12 changed files with 711 additions and 26 deletions.
5 changes: 2 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@
]
},
"globals": {
"pdfjsLib": "readonly",
"channel": "readonly"
},
"ignorePatterns": [
"pdf.js",
"pdf.worker.js"
"/app/src/*/assets/viewer",
"build/"
]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ keystore.properties
/.idea
/releases
/node_modules
/app/src/*/assets/viewer
1 change: 0 additions & 1 deletion app/src/main/assets/pdf.js

This file was deleted.

1 change: 0 additions & 1 deletion app/src/main/assets/pdf.worker.js

This file was deleted.

8 changes: 4 additions & 4 deletions app/src/main/java/app/grapheneos/pdfviewer/PdfViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceReque
return new WebResourceResponse("application/pdf", null, mInputStream);
}

if ("/viewer.html".equals(path)) {
if ("/viewer/index.html".equals(path)) {
final WebResourceResponse response = fromAsset("text/html", path);
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Security-Policy", CONTENT_SECURITY_POLICY);
Expand All @@ -319,11 +319,11 @@ public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceReque
return response;
}

if ("/viewer.css".equals(path)) {
if ("/viewer/main.css".equals(path)) {
return fromAsset("text/css", path);
}

if ("/viewer.js".equals(path) || "/pdf.js".equals(path) || "/pdf.worker.js".equals(path)) {
if ("/viewer/js/index.js".equals(path) || "/viewer/js/worker.js".equals(path)) {
return fromAsset("application/javascript", path);
}

Expand Down Expand Up @@ -520,7 +520,7 @@ private void loadPdf() {
mDocumentState = 0;
showSystemUi();
invalidateOptionsMenu();
binding.webview.loadUrl("https://localhost/viewer.html");
binding.webview.loadUrl("https://localhost/viewer/index.html");
}

public void loadPdfWithPassword(final String password) {
Expand Down
Loading

0 comments on commit 627a566

Please sign in to comment.