-
-
Notifications
You must be signed in to change notification settings - Fork 5
Add support for opening relative links to other PDFs #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ import { | |
| type Webview, | ||
| type WebviewPanel, | ||
| window, | ||
| commands, | ||
| } from "vscode"; | ||
|
|
||
| import rawViewerHtml from "../assets/pdf.js/web/viewer.html"; | ||
|
|
@@ -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"; | ||
|
|
||
|
|
@@ -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 { | ||
|
|
@@ -124,6 +135,7 @@ export class PDFViewerProvider implements CustomReadonlyEditorProvider { | |
|
|
||
| const settings = { | ||
| url: `${webview.asWebviewUri(document.uri)}`, | ||
| docBaseUrl: `${webview.asWebviewUri(document.uri)}`, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| }; | ||
|
|
||
| return viewerHtml | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
#fragmentof the input url