Skip to content

Commit

Permalink
Merge pull request #116 from liudongmiao/pdfjs-dist
Browse files Browse the repository at this point in the history
use release version of pdf.js
  • Loading branch information
PHPirates authored Sep 23, 2024
2 parents fa79e56 + d3aa676 commit 7d25164
Show file tree
Hide file tree
Showing 28 changed files with 122 additions and 5,987 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.firsttimeinforever.intellij.pdf.viewer
import com.firsttimeinforever.intellij.pdf.viewer.model.*
import com.firsttimeinforever.intellij.pdf.viewer.tex.SynctexPreciseLocation
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement

object IdeMessages {
@Serializable
Expand Down Expand Up @@ -48,7 +49,7 @@ object IdeMessages {
data class SynctexAvailability(val isAvailable: Boolean)

@Serializable
data class NavigateTo(val destination: String)
data class NavigateTo(val destination: JsonElement)

@Serializable
class ExitPresentationMode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.firsttimeinforever.intellij.pdf.viewer.model

import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonPrimitive

/**
* Represents a node in PDF outline.
Expand All @@ -15,13 +17,13 @@ data class PdfOutlineNode(
val name: String,
val children: List<PdfOutlineNode> = emptyList(),
val page: Int? = null,
val navigationReference: String = ""
val navigationReference: JsonElement = JsonPrimitive("")
) {
val isRoot: Boolean
get() = page == null && name == ROOT_NAME

companion object {
fun createRootNode(children: List<PdfOutlineNode> = emptyList(), navigationReference: String = ""): PdfOutlineNode {
fun createRootNode(children: List<PdfOutlineNode> = emptyList(), navigationReference: JsonElement = JsonPrimitive("")): PdfOutlineNode {
return PdfOutlineNode(ROOT_NAME, children, null, navigationReference)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.firsttimeinforever.intellij.pdf.viewer.settings.PdfViewerSettings
import com.firsttimeinforever.intellij.pdf.viewer.utility.PdfResourceLoader
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.ui.jcef.JBCefScrollbarsHelper
import com.intellij.util.Url
import com.intellij.util.Urls
import com.intellij.util.io.URLUtil
Expand Down Expand Up @@ -98,25 +99,31 @@ internal class PdfStaticServer : HttpRequestHandler() {

private fun sendInternalFile(path: String, context: ChannelHandlerContext, request: FullHttpRequest) {
val targetFile = makeInternalPath(path)
if (PdfViewerSettings.isDebugMode && targetFile.endsWith(".js.map")) {
logger.warn("Ignoring sourcemap $targetFile")
val url = PdfStaticServer::class.java.getResource(targetFile)
if (url == null) {
logger.debug("Cannot find internal file $targetFile")
HttpResponseStatus.NOT_FOUND.send(context.channel(), request)
return
}
val contentType = FileResponses.getContentType(targetFile)
logger.debug("Sending internal file: $targetFile with contentType: $contentType")
val resultBuffer = Unpooled.wrappedBuffer(PdfResourceLoader.loadFromRoot(targetFile))
var bytes = PdfResourceLoader.loadFromRoot(targetFile)
if (targetFile == "/web-view/fixes.css") {
bytes += JBCefScrollbarsHelper.buildScrollbarsStyle().toByteArray(Charsets.UTF_8)
}
val resultBuffer = Unpooled.wrappedBuffer(bytes)
val response = response(contentType, resultBuffer)
response.send(context.channel(), request)
}

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")
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)}"))
.toExternalForm()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.firsttimeinforever.intellij.pdf.viewer.model.PdfOutlineNode
import com.firsttimeinforever.intellij.pdf.viewer.ui.editor.PdfFileEditor
import com.intellij.ide.structureView.StructureViewTreeElement
import com.intellij.navigation.ItemPresentation
import kotlinx.serialization.json.JsonPrimitive
import javax.swing.Icon

abstract class PdfStructureViewElement(val editor: PdfFileEditor, val node: PdfOutlineNode): StructureViewTreeElement, ItemPresentation {
Expand All @@ -14,7 +15,7 @@ abstract class PdfStructureViewElement(val editor: PdfFileEditor, val node: PdfO
}

override fun canNavigate(): Boolean {
return node.navigationReference.isNotEmpty() &&
return node.navigationReference != JsonPrimitive("") &&
editor.viewComponent.controller?.canNavigate() == true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.intellij.openapi.util.registry.Registry
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.ui.jcef.JCEFHtmlPanel
import com.intellij.util.ui.UIUtil
import kotlinx.serialization.json.JsonElement
import java.awt.Color
import java.util.concurrent.atomic.AtomicBoolean

Expand Down Expand Up @@ -247,7 +248,7 @@ class PdfJcefPreviewController(val project: Project, val virtualFile: VirtualFil

fun canNavigate(): Boolean = outline != null

fun navigate(destinationReference: String) {
fun navigate(destinationReference: JsonElement) {
pipe.send(IdeMessages.NavigateTo(destinationReference))
}

Expand Down
3 changes: 0 additions & 3 deletions web-view/bootstrap/assets/treeitem-triangle-hidden.svg

This file was deleted.

3 changes: 0 additions & 3 deletions web-view/bootstrap/assets/treeitem-triangle.svg

This file was deleted.

75 changes: 32 additions & 43 deletions web-view/bootstrap/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import com.moowork.gradle.node.npm.NpmTask
import java.nio.file.Paths
import org.jetbrains.kotlin.de.undercouch.gradle.tasks.download.Download

plugins {
id("com.github.node-gradle.node") version "2.2.3"
id("de.undercouch.download") version "5.6.0"
}

repositories {
Expand All @@ -15,53 +14,43 @@ dependencies {
default(project(":web-view:viewer", configurations.viewerApplicationBundle.name))
}

tasks {
node {
download = true
version = project.properties["nodeVersion"].toString()
nodeModulesDir = projectDir
}
}
val pdfjsVersion = "4.6.82"

val copyApplicationBundle by tasks.registering(Copy::class) {
from(default)
into(File(projectDir, "application"))
val downloadZipFile by tasks.registering(Download::class) {
val destFile = layout.projectDirectory.asFile.resolve("dist/pdfjs-$pdfjsVersion-dist.zip")
src("https://github.com/mozilla/pdf.js/releases/download/v$pdfjsVersion/pdfjs-$pdfjsVersion-dist.zip")
dest(destFile)
tempAndMove(true)
onlyIf { !destFile.exists() || destFile.length() == 0L }
}

val ensureNodeModulesInstalled by tasks.registering {
dependsOn("nodeSetup")
dependsOn("npmSetup")
inputs.file(File(projectDir, "package.json")).withPathSensitivity(PathSensitivity.RELATIVE)
inputs.file(File(projectDir, "package-lock.json")).withPathSensitivity(PathSensitivity.RELATIVE)
if (!file(Paths.get(projectDir.toString(), "node_modules")).exists()) {
dependsOn("npm_ci")
val downloadAndUnzipFile by tasks.registering(Copy::class) {
dependsOn(downloadZipFile)
from(zipTree(downloadZipFile.get().dest)) {
exclude("*/*.pdf")
exclude("*/*.js.map")
exclude("*/*.mjs.map")
}
}

val collectOuterSources by tasks.registering {
val files = listOf(
"package.json",
"package-lock.json",
"postinstall.js",
"webpack.config.js",
"index.html"
)
for (file in files) {
inputs.file(File(projectDir, file)).withPathSensitivity(PathSensitivity.RELATIVE)
into(project.layout.buildDirectory)
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\">")
.replace("(<script src=\"viewer.m?js\"[^<>]*></script>)".toRegex(), "$1<script src=\"../viewer.js\"></script>")
viewerHtml.writeText(modifiedContent)
val tmpdir = file(layout.buildDirectory.get().asFile.resolve("tmp"))
if (tmpdir.exists()) {
tmpdir.deleteRecursively()
}
}
inputs.dir("src").withPathSensitivity(PathSensitivity.RELATIVE)
outputs.files(inputs.files)
}

val buildWebView by tasks.registering(NpmTask::class) {
dependsOn(ensureNodeModulesInstalled)
inputs.files(
collectOuterSources.map { it.outputs },
copyApplicationBundle.map { it.outputs }
)
setArgs(listOf("run", "build"))
outputs.dir(File(projectDir, "build"))
// outputs.cacheIf { true }
val buildWebView by tasks.registering(Copy::class) {
dependsOn(downloadAndUnzipFile)
from("src") {
include("*.css")
}
into(project.layout.buildDirectory)
}

artifacts {
Expand Down
Loading

0 comments on commit 7d25164

Please sign in to comment.