Skip to content

Commit

Permalink
feat(preview panel): render specification
Browse files Browse the repository at this point in the history
  • Loading branch information
Pakisan committed Sep 7, 2024
1 parent d340014 commit 9de3112
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.intellij.json.JsonFileType
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.editor.Document
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
import io.netty.handler.codec.http.FullHttpRequest
import org.jetbrains.yaml.YAMLFileType
import java.io.File
import java.net.URL

/**
* @author Pavel Bodiachevskii
Expand All @@ -28,6 +30,20 @@ class AsyncAPISpecificationHtmlRenderer {
private val specificationTemplateCssUrl = "default.min.css"
private val specificationTemplateJsUrl = "index.js"

fun render(specificationVirtualFile: VirtualFile, document: Document?): String {
if (specificationVirtualFile.fileType !is YAMLFileType && specificationVirtualFile.fileType !is JsonFileType) {
return "specification: ${specificationVirtualFile.name} not in json or yaml format."
}

val isJson = specificationVirtualFile.fileType is JsonFileType
val specification = replaceLocalReferences(document?.text ?: "", specificationVirtualFile, isJson)

val specificationTemplate = this.javaClass.getResource(specificationTemplateUrl)
specificationTemplate ?: return "specification template not found."

return composePage(specificationTemplate, specification)
}

fun render(request: FullHttpRequest, specificationUrl: String?): String {
specificationUrl ?: return "specification: not found."

Expand All @@ -53,21 +69,7 @@ class AsyncAPISpecificationHtmlRenderer {
supplier = { specificationVirtualFile }
)

return specificationTemplate.readText(Charsets.UTF_8)
.replace(
"schema: {},",
"schema: $specification,"
)
.replace(
"<link rel=\"stylesheet\" href=\"\">",
"<link rel=\"stylesheet\" href=\"${urlProvider.resource(specificationTemplateCssUrl)}\">"
).replace(
"<script src=\"\"></script>",
"<script src=\"${urlProvider.resource(specificationTemplateJsUrl)}\"></script>"
).replace(
"<!-- WebSocket -->",
webSocket?.toString() ?: ""
)
return composePage(specificationTemplate, specification, webSocket)
}

fun replaceLocalReferences(specification: String, specificationFile: VirtualFile, isJson: Boolean): String {
Expand Down Expand Up @@ -100,4 +102,32 @@ class AsyncAPISpecificationHtmlRenderer {
return urlProvider.reference(referencedFile.path, specificationComponentReference)
}

private fun composePage(
specificationTemplate: URL,
specification: String,
webSocket: CharSequence? = null
): String {
val page = specificationTemplate.readText(Charsets.UTF_8)
.replace(
"schema: {},",
"schema: $specification,"
)
.replace(
"<link rel=\"stylesheet\" href=\"\">",
"<link rel=\"stylesheet\" href=\"${urlProvider.resource(specificationTemplateCssUrl)}\">"
).replace(
"<script src=\"\"></script>",
"<script src=\"${urlProvider.resource(specificationTemplateJsUrl)}\"></script>"
)

return if (webSocket != null) {
page.replace(
"<!-- WebSocket -->",
webSocket.toString()
)
} else {
page
}
}

}
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
package com.asyncapi.plugin.idea.extensions.editor.preview

import com.asyncapi.plugin.idea._core.AsyncAPISpecificationHtmlRenderer
import com.asyncapi.plugin.idea.extensions.editor.ui.AsyncAPIJCEFHtmlPanel
import com.intellij.codeHighlighting.BackgroundEditorHighlighter
import com.intellij.ide.structureView.StructureViewBuilder
import com.intellij.openapi.components.service
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.event.DocumentEvent
import com.intellij.openapi.editor.event.DocumentListener
import com.intellij.openapi.fileEditor.FileEditor
import com.intellij.openapi.fileEditor.FileEditorLocation
import com.intellij.openapi.fileEditor.FileEditorState
import com.intellij.openapi.fileEditor.FileEditorStateLevel
import com.intellij.openapi.fileEditor.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.UserDataHolderBase
import com.intellij.openapi.vfs.VirtualFile
import java.awt.LayoutManager
import java.beans.PropertyChangeListener
import javax.swing.JComponent
import javax.swing.JPanel
import javax.swing.OverlayLayout

class AsyncAPIPreviewEditor(
private val file: VirtualFile,
private val document: Document?,
private val project: Project
) :
Expand All @@ -31,14 +32,15 @@ class AsyncAPIPreviewEditor(

private val asyncAPIPreviewEditorComponent: JComponent
private val htmlPanel: AsyncAPIJCEFHtmlPanel = AsyncAPIJCEFHtmlPanel(editor)
private val specificationHtmlRenderer = service<AsyncAPISpecificationHtmlRenderer>()

init {
asyncAPIPreviewEditorComponent = createComponent()

val documentListenerHandler = object : DocumentListener {
override fun documentChanged(event: DocumentEvent) {
if (asyncAPIPreviewEditorComponent.isVisible && asyncAPIPreviewEditorComponent.isDisplayable) {
htmlPanel.setHtml("${document?.text}")
htmlPanel.setHtml(specificationHtmlRenderer.render(file, document))
}
}
}
Expand All @@ -50,7 +52,7 @@ class AsyncAPIPreviewEditor(
val overlay: LayoutManager = OverlayLayout(previewEditor)
previewEditor.setLayout(overlay)

htmlPanel.setHtml("${document?.text}")
htmlPanel.setHtml(specificationHtmlRenderer.render(file, document))
previewEditor.add(htmlPanel.component)

return previewEditor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AsyncAPIPreviewEditorProvider: WeighedFileEditorProvider() {
}

override fun createEditor(project: Project, file: VirtualFile): FileEditor {
return AsyncAPIPreviewEditor(FileDocumentManager.getInstance().getDocument(file), project)
return AsyncAPIPreviewEditor(file, FileDocumentManager.getInstance().getDocument(file), project)
}

override fun getEditorTypeId(): String {
Expand Down

0 comments on commit 9de3112

Please sign in to comment.