Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,19 @@ Most extensions currently in the marketplace have problems too deep to fix. Thes
## Support us

If you find this extension helpful, please consider supporting its development through GitHub Sponsors. Your support helps maintain and improve this extension. Click [here](https://github.com/sponsors/jrandolf) to become a sponsor.


## Updating PDF.js

* update `pdfjs_version.txt` to target version and hash
* from root folder of this repo run `tools/prepare_pdfjs.sh`, this will download PDF.js in given version and try to apply patches from the `patches` folder to it
* if the patches apply cleanly the command terminates and you are done
* if the patches fail to apply, for every conflict a `*.rej` file will be generated in the `assets/pdf.js` folder,
you need to resolve these manually and then delete the `.rej` files

If you want to keep current version of PDF.js but add some more patches, run with flag `--update-patches` which will stop after applying the current patches same as in the above case, leaving you to modify the `assets/pdf.js` however you see fit,
once done new patch will be created to match your edits.

After you prepared your patches you can run the same command again, the second time the patches should apply cleanly and the `assets/pdf.js` should have the updated content you created earlier.

The `prepare_pdfjs.sh` script does not create any commits in this repo, after you are happy with the patches you prepared be sure to commit everything manually.
6 changes: 5 additions & 1 deletion assets/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ PDFViewerApplicationOptions.set("disablePreferences", true);

void (async () => {
await window.PDFViewerApplication.initializedPromise;
window.PDFViewerApplication.open(config);
await window.PDFViewerApplication.open(config);
const [,hash] = config.url.split("#")
if(hash){
PDFViewerApplication.pdfLinkService.setHash(decodeURIComponent(hash))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part is basically what was suggested here for the other vscode PDF viewer - it allows opening into selected location inside PDF document by the #fragment of the input url

}
})();

window.addEventListener("message", async (event) => {
Expand Down
26 changes: 24 additions & 2 deletions assets/pdf.js/web/viewer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13347,7 +13347,8 @@ const PDFViewerApplication = {
const pdfRenderingQueue = new PDFRenderingQueue();
pdfRenderingQueue.onIdle = this._cleanup.bind(this);
this.pdfRenderingQueue = pdfRenderingQueue;
const pdfLinkService = new PDFLinkService({
const vscode = acquireVsCodeApi()
const pdfLinkService = new VSCodeLinkService(vscode, {
eventBus,
externalLinkTarget: AppOptions.get("externalLinkTarget"),
externalLinkRel: AppOptions.get("externalLinkRel"),
Expand Down Expand Up @@ -15362,4 +15363,25 @@ var __webpack_exports__PDFViewerApplicationConstants = __webpack_exports__.PDFVi
var __webpack_exports__PDFViewerApplicationOptions = __webpack_exports__.PDFViewerApplicationOptions;
export { __webpack_exports__PDFViewerApplication as PDFViewerApplication, __webpack_exports__PDFViewerApplicationConstants as PDFViewerApplicationConstants, __webpack_exports__PDFViewerApplicationOptions as PDFViewerApplicationOptions };

//# sourceMappingURL=viewer.mjs.map
//# sourceMappingURL=viewer.mjs.map


class VSCodeLinkService extends PDFLinkService {
#vscode
constructor(vscode, ...args){
super(...args)
this.#vscode = vscode
}

addLinkAttributes(link, url, newWindow = false){
if(typeof url === "string" && url.startsWith("https://file+.vscode-resource.vscode-cdn.net")){
link.onclick = () => {
this.#vscode.postMessage({
open: url
});
};
} else {
return super.addLinkAttributes(link, url, newWindow)
}
}
}
43 changes: 43 additions & 0 deletions patches/pdf.js.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
diff --git a/web/viewer.mjs b/web/viewer.mjs
index c3d794a..9df6c47 100644
--- a/web/viewer.mjs
+++ b/web/viewer.mjs
@@ -13347,7 +13347,8 @@ const PDFViewerApplication = {
const pdfRenderingQueue = new PDFRenderingQueue();
pdfRenderingQueue.onIdle = this._cleanup.bind(this);
this.pdfRenderingQueue = pdfRenderingQueue;
- const pdfLinkService = new PDFLinkService({
+ const vscode = acquireVsCodeApi()
+ const pdfLinkService = new VSCodeLinkService(vscode, {
eventBus,
externalLinkTarget: AppOptions.get("externalLinkTarget"),
externalLinkRel: AppOptions.get("externalLinkRel"),
@@ -15362,4 +15363,25 @@ var __webpack_exports__PDFViewerApplicationConstants = __webpack_exports__.PDFVi
var __webpack_exports__PDFViewerApplicationOptions = __webpack_exports__.PDFViewerApplicationOptions;
export { __webpack_exports__PDFViewerApplication as PDFViewerApplication, __webpack_exports__PDFViewerApplicationConstants as PDFViewerApplicationConstants, __webpack_exports__PDFViewerApplicationOptions as PDFViewerApplicationOptions };

-//# sourceMappingURL=viewer.mjs.map
\ No newline at end of file
+//# sourceMappingURL=viewer.mjs.map
+
+
+class VSCodeLinkService extends PDFLinkService {
+ #vscode
+ constructor(vscode, ...args){
+ super(...args)
+ this.#vscode = vscode
+ }
+
+ addLinkAttributes(link, url, newWindow = false){
+ if(typeof url === "string" && url.startsWith("https://file+.vscode-resource.vscode-cdn.net")){
+ link.onclick = () => {
+ this.#vscode.postMessage({
+ open: url
+ });
+ };
+ } else {
+ return super.addLinkAttributes(link, url, newWindow)
+ }
+ }
+}
\ No newline at end of file
12 changes: 12 additions & 0 deletions src/pdf_viewer_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
type Webview,
type WebviewPanel,
window,
commands,
} from "vscode";

import rawViewerHtml from "../assets/pdf.js/web/viewer.html";
Expand All @@ -45,6 +46,8 @@ const viewerHtml = rawViewerHtml
.replace(/* html */ `<script src="viewer.mjs" type="module"></script>`, "")
.replace(/* html */ `<link rel="stylesheet" href="viewer.css">`, "");

const vscodeWebviewUriPrefix = "https://file+.vscode-resource.vscode-cdn.net"

export class PDFViewerProvider implements CustomReadonlyEditorProvider {
public static readonly viewType = "pdf.view";

Expand Down Expand Up @@ -111,6 +114,14 @@ export class PDFViewerProvider implements CustomReadonlyEditorProvider {
document,
webviewPanel.webview
);

webviewPanel.webview.onDidReceiveMessage(msg => {
if("open" in msg){
const urlWithCdnScheme = msg.open as string
const [file, hash] = urlWithCdnScheme.substring(vscodeWebviewUriPrefix.length).split("#")
commands.executeCommand("vscode.open", Uri.file(file!).with({fragment: hash ?? ""}))
}
})
}

private getHtmlForWebview(document: PDFDocument, webview: Webview): string {
Expand All @@ -124,6 +135,7 @@ export class PDFViewerProvider implements CustomReadonlyEditorProvider {

const settings = {
url: `${webview.asWebviewUri(document.uri)}`,
docBaseUrl: `${webview.asWebviewUri(document.uri)}`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is required so pdf.js is able to create proper url in the annotations layer when resolving relative file links

In particular this is needed so the data.url is available here

};

return viewerHtml
Expand Down
4 changes: 4 additions & 0 deletions tools/download_pdfjs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ fi

# Unzip the file into the target directory
unzip pdfjs.zip -d assets/pdf.js
if [ "$?" != "0" ]; then
echo "Failed to unzip pdfjs.zip"
exit 1
fi

# Remove the zip file after extraction
rm pdfjs.zip
50 changes: 50 additions & 0 deletions tools/patching.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

# inspired by https://github.com/VSCodium/vscodium/blob/master/docs/howto-build.md#manual

repo_root=$PWD
patches=$repo_root/patches
pdfjs=$repo_root/assets/pdf.js

function prepare_patching {
pushd $pdfjs || { echo "'assets/pdf.js' dir not found"; exit 1; }

rm -rf $pdfjs/.git # use abs path to not accidentally nuke the main .git folder
git init --quiet
git add .
git commit -m start --quiet

popd
}

function clean_patching {
rm -rf $pdfjs/.git
}

function apply_patches {
pushd $pdfjs || { echo "'assets/pdf.js' dir not found"; exit 1; }

result=0
for p in $patches/*.patch; do
echo $p
if ! git apply --reject --recount --ignore-whitespace "${p}"; then
echo failed to apply patch "${p}"
result=1
fi
done

popd

return $result
}

function update_patches {
pushd $pdfjs || { echo "'assets/pdf.js' dir not found"; exit 1; }

git add .
mkdir -p $patches
git diff --cached > $patches/pdf.js.patch

popd
}

50 changes: 50 additions & 0 deletions tools/prepare_pdfjs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

while [[ $# -gt 0 ]]; do
case $1 in
--update-patches)
update_patches=true
shift
;;
*)
echo "Unexpected argument: $1"
exit 1
esac
done

. tools/download_pdfjs.sh

. tools/patching.sh

prepare_patching
trap "clean_patching" EXIT

apply_patches
apply_result=$?

if [[ $apply_result -ne 0 ]]; then
echo
read -p "Patches did not apply cleanly, fix all conflicts (look for .rej files), then hit any key to continue..." -n1 -s
echo

leftover_rej=init
while [[ $leftover_rej ]]; do
leftover_rej=$(find assets/pdf.js -name '*.rej')
if [[ $leftover_rej ]]; then
echo
echo Some .rej files remained, makes sure to remove these after resolving the conflicts:
echo $leftover_rej

read -p "hit any key to continue after you resolved and removed all .rej files..." -n1 -s
echo
fi
done

update_patches
elif [[ $update_patches ]]; then
echo
read -p "Do your changes inside 'assets/pdf.js', then hit any key to continue..." -n1 -s
echo

update_patches
fi