Skip to content
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

use release version of pdf.js #116

Merged
merged 14 commits into from
Sep 23, 2024
Merged

use release version of pdf.js #116

merged 14 commits into from
Sep 23, 2024

Conversation

liudongmiao
Copy link
Contributor

As discussing in #114, there are several options to include pdf.js.
In #114, we choose compile from pdf.js's source code.

However, we don't need to.

This PR simplify the processes, just download and unzip, the inject intellij-pdf-viewer's js and css, make it much easy.

@@ -112,11 +112,11 @@ internal class PdfStaticServer : HttpRequestHandler() {
fun getPreviewUrl(file: VirtualFile, withReloadSalt: Boolean = false): String {
val salt = if (withReloadSalt) Random.nextInt() else 0
vfsMap[file.url] = file
val url = parseEncodedPath("$serverUrl/index.html")
val url = parseEncodedPath("$serverUrl/web/viewer.html")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

entry point of mozilla's pdf.js

val server = BuiltInServerManager.getInstance()
return server.addAuthToken(url)
// `file` would be read via `urlDecoder.path()`, which calls `decodeComponent`
.addParameters(mapOf("__reloadSalt" to "$salt", "file" to URLUtil.encodeURIComponent("get-file/${file.url}")))
.addParameters(mapOf("__reloadSalt" to "$salt", "file" to "/$uuid/get-file/${URLUtil.encodeURIComponent(file.url)}"))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

as we use web/viewer.html, so change the file to absolute path.

doLast {
val viewerHtml = layout.buildDirectory.get().asFile.resolve("web/viewer.html")
val modifiedContent = viewerHtml.readText()
.replace("(<link rel=\"stylesheet\" href=\"viewer.css\">)".toRegex(), "$1<link rel=\"stylesheet\" href=\"../fixes.css\">")
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 is the magic 1, add our css after original viewer.css

val viewerHtml = layout.buildDirectory.get().asFile.resolve("web/viewer.html")
val modifiedContent = viewerHtml.readText()
.replace("(<link rel=\"stylesheet\" href=\"viewer.css\">)".toRegex(), "$1<link rel=\"stylesheet\" href=\"../fixes.css\">")
.replace("(<script src=\"viewer.m?js\"[^<>]*></script>)".toRegex(), "$1<script src=\"../viewer.js\"></script>")
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 is the magic 2, add our js after original viewer.js


@ExperimentalSerializationApi
fun main() {
ApplicationFactory.init()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

point to inject our javascript.

1. navigateTo is removed since v2.9.359
   ref: mozilla/pdf.js@92ec10b

2. the type of `dest` in outline is `{string | Array<any> | null}`
   we use `JsonElement` to indicate
@liudongmiao
Copy link
Contributor Author

This version won't use pdf_viewer.css, it may break theme values.
However, there are viewerCssTheme and pageColorsBackground, pageColorsForeground.
pageColorsXXX is available since pdf.js v2.14+, in commit mozilla/pdf.js@c8afd6c


@Serializable
data class InternalOutline(
val dest: String,
val dest: JsonElement,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the dest in outline is {string | Array | null}

Copy link
Collaborator

@PHPirates PHPirates left a comment

Choose a reason for hiding this comment

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

Great! This solution is a lot cleaner. I'll create a release with this included.

Copy link
Collaborator

@PHPirates PHPirates left a comment

Choose a reason for hiding this comment

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

This version won't use pdf_viewer.css, it may break theme values. However, there are viewerCssTheme and pageColorsBackground, pageColorsForeground. pageColorsXXX is available since pdf.js v2.14+, in commit mozilla/pdf.js@c8afd6c

Wait, as you say, something is not working with the theme. The background color of the sidebar is not correct anymore:
before:
image

now:
image

@liudongmiao
Copy link
Contributor Author

@PHPirates it's nothing with pdf_viewer.css. However, it's other css changes in pdf viewer.

val pageViewerRule = pdfViewerCss?.cssRules?.asList()?.filterIsInstance<CSSStyleRule>()
?.find { it.selectorText == ".pdfViewer .page" }
pageViewerRule?.style?.removeProperty("-o-border-image")
pageViewerRule?.style?.removeProperty("border-image")
val thumbnailRule = viewerCss.cssRules.asList().filterIsInstance<CSSStyleRule>().find { it.selectorText == ".thumbnailImage" }
thumbnailRule?.style?.removeProperty("box-shadow")

There is no border-image in .pdfViewer .page since v3.3.122 , see mozilla/pdf.js@685b586

There is no box-shadow in .thumbnailImage since v3.6.172, see mozilla/pdf.js@1b6a83d

I'll use the this new way with the old pdf.js, may be a downgrade until we solve the css.

@liudongmiao
Copy link
Contributor Author

I don't see much difference. However, there is scrollbar in new version.

up: pdfjs-dist, with pdf.js 4.6
below: market version
light

dark

pdf.js sidebar triangles switched to svg since 2.6.347 in commit
mozilla/pdf.js@c0b671d

However, previous viewer.css use a png, so it need fix
Now, we use pdf.js version, which use svg already
@liudongmiao
Copy link
Contributor Author

@PHPirates I have added severa commits, please help to review.

And, I cannot reproduct your sidebar's screenshot.

And, for the sidebar on macos, now, it shows always. However, I think it's acceptable, and Markdown plugin show the scrollbar always too. And, the scrollbar looks like the same as markdown plugin now.

@liudongmiao
Copy link
Contributor Author

Wait, as you say, something is not working with the theme. The background color of the sidebar is not correct anymore:

@PHPirates Just found the differences. In pdf.js v2.7 and v0.16.1, the background color of sidebar is rgba(0, 0, 0, 0.1)

#sidebarContent {
top: 32px;
bottom: 0;
overflow: auto;
-webkit-overflow-scrolling: touch;
position: absolute;
width: 100%;
background-color: rgba(0, 0, 0, 0.1);
}

https://github.com/mozilla/pdf.js/blob/v2.7.350/web/viewer.css#L258-L266
However, upstream change the background-color in mozilla/pdf.js@685b586
then remove the background-color (then inherit background color from body) in mozilla/pdf.js@8ab5476
both is in pdf.js v3.3.122

However, intellij-pdf-viewer want to change sidebar background color to the same as body background color.

setProperty(Internals.StyleVariables.bodyBackgroundColor, viewTheme.background)
setProperty(Internals.StyleVariables.sidebarBackgroundColor, viewTheme.background)

However, I don't know why your screenshot don't update the background. Please use DevTools to find it.
You can change the registry ide.browser.jcef.contextMenu.devTools.enabled to true, then open devtools.
ref: https://plugins.jetbrains.com/docs/intellij/jcef.html#debugging

@PHPirates
Copy link
Collaborator

Thanks for the explanation! Sorry must have been something wrong on my side, after a gradle clean everything works as expected. Also changing the background color in the PDF Viewer settings work fine (foreground --main-color doesn't look like it changes anything, but that was already in 0.16.1 so is not caused by the pdf.js update).

The scrollbar looks good to me, much better than it was!

upstream change the background-color of narrow sidebar to --sidebar-narrow-bg-color
in commit mozilla/pdf.js@7c52c01
since v2.8.335
There is no `border-image` in `.pdfViewer .page` since v3.3.122,
see mozilla/pdf.js@685b586

There is no `box-shadow` in `.thumbnailImage` since v3.6.172,
see mozilla/pdf.js@1b6a83d

#116 (comment)
@liudongmiao
Copy link
Contributor Author

@PHPirates I self-review again, and it could be merged into master now.

For the events in

062acd1#diff-85699ae3492958e0a83d3d768422d1124006a9213eaa31c775de20f22a4755a5L223-L243

it's introduced in a2d6bdd, the commit message is to add mouse shortcuts for zoom in/out, however, it's core function was removed as in another commit ad34116

@PHPirates
Copy link
Collaborator

Great, thanks for checking!

@PHPirates PHPirates merged commit 7d25164 into FirstTimeInForever:master Sep 23, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants