Skip to content

Commit

Permalink
fix #15 png handling (#23)
Browse files Browse the repository at this point in the history
* wip

* updated docs and save png works

* rewrote logic to avoid code duplication

* removed unused import

* refactored

* Update src/main/kotlin/de/docs_as_co/intellij/plugin/drawio/editor/DrawioWebView.kt

Co-authored-by: Henning Dieterichs <[email protected]>

Co-authored-by: Henning Dieterichs <[email protected]>
  • Loading branch information
rdmueller and hediet authored Nov 3, 2020
1 parent 1b26d49 commit bea765d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ gradle-app.setting
# Once you use this, check in the file
# More information: https://www.jetbrains.com/​help/idea/required-plugin.html
!/.idea/externalDependencies.xml
out
10 changes: 10 additions & 0 deletions src/docs/arc42/chapters/03_system_scope_and_context.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ let's use the https://github.com/RicardoNiepel/C4-PlantUML[C4 PlantUML syntax] h
=== Technical Context

TODO

==== draw.io interface

The draw.io interface is messages based and described in the support article https://desk.draw.io/support/solutions/articles/16000042544-embed-mode[Embed Mode].

Example can be found at

* Introduction/Example: https://github.com/jgraph/drawio-html5
* GitHub Example: https://github.com/jgraph/drawio-github
* https://support.draw.io/display/DOB/2016/05/09/Simple+draw.io+embedding+walk-through[Simple draw.io embedding walk-through]
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.intellij.openapi.vfs.VirtualFile
import com.intellij.testFramework.runNonUndoableWriteAction
import com.jetbrains.rd.util.lifetime.LifetimeDefinition
import java.beans.PropertyChangeListener
import java.util.*
import javax.swing.JComponent


Expand All @@ -25,29 +26,42 @@ class DiagramsEditor(private val project: Project, private val file: VirtualFile
init {

view.initializedPromise.then {
view.loadXmlLike(file.inputStream.reader().readText())
if (file.name.endsWith(".png")) {
val payload = file.inputStream.readBytes()
view.loadPng(payload)
} else {
val payload = file.inputStream.reader().readText()
view.loadXmlLike(payload)
}
}

view.xmlContent.advise(lifetime) { xml ->
if (xml !== null) {
val isSVGFile = file.name.endsWith(".svg")
val isPNGFile = file.name.endsWith(".png")
if ( isSVGFile ) {
//ignore the xml payload and ask for an exported svg
view.exportSvg().then{ svg : String ->
saveFile (svg)
view.exportSvg().then{ data : String ->
saveFile (data.toByteArray(charset("utf-8")))
}
} else if ( isPNGFile ) {
//ignore the xml payload and ask for an exported svg
view.exportPng().then { data: ByteArray ->
saveFile(data)
}
} else {
saveFile(xml)
saveFile(xml.toByteArray(charset("utf-8")))
}
}
}
}

private fun saveFile(data : String) {
private fun saveFile(data : ByteArray) {
ApplicationManager.getApplication().invokeLater {
ApplicationManager.getApplication().runWriteAction {
file.getOutputStream(this).apply {
writer().apply {
//svg and png are returned base64 encoded
write(data)
flush()
}
Expand All @@ -56,7 +70,6 @@ class DiagramsEditor(private val project: Project, private val file: VirtualFile
}
}
}

}
override fun getComponent(): JComponent {
return view.component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DiagramsEditorProvider : FileEditorProvider, DumbAware {
return false;
}
//check for the right file extension
val extensions = arrayOf(".drawio", ".drawio.svg", ".dio", ".dio.svg")
val extensions = arrayOf(".drawio", ".drawio.svg", ".drawio.png", ".dio", ".dio.svg", ".dio.png")
if (extensions.any { ext -> file.name.endsWith(ext)}) {
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,30 @@ class DrawioWebView(lifetime: Lifetime) : BaseDrawioWebView(lifetime) {
_xmlContent.set(null) // xmlLike is not xml
send(OutgoingMessage.Event.Load(xmlLike, 1))
}

fun loadPng(payload: ByteArray) {
_xmlContent.set(null) // xmlLike is not xml
val xmlLike = "data:image/png;base64," + Base64.getEncoder().encodeToString(payload)
send(OutgoingMessage.Event.Load(xmlLike, 1))
}


fun exportSvg() : Promise<String> {
val result = AsyncPromise<String>()
send(OutgoingMessage.Request.Export(OutgoingMessage.Request.Export.XMLSVG)).then { response ->
val data = (response as IncomingMessage.Response.Export).data
result.setResult(data)
}
return result
}
fun exportPng() : Promise<ByteArray> {
val result = AsyncPromise<ByteArray>()
send(OutgoingMessage.Request.Export(OutgoingMessage.Request.Export.XMLPNG)).then { response ->
val data = (response as IncomingMessage.Response.Export).data
val payload = data.split(",")[1]
val decodedBytes = Base64.getDecoder().decode(payload)
val svg = String(decodedBytes)
result.setResult(svg)
result.setResult(decodedBytes)
}
return result
}
}
}

0 comments on commit bea765d

Please sign in to comment.