Replaced webview with textflow in TootView
This makes toots look nicer, hyperlinks work and it has many more advanteges
This commit is contained in:
parent
7079bbe20d
commit
e09f52bf5c
1 changed files with 63 additions and 15 deletions
|
@ -1,12 +1,19 @@
|
|||
package eu.toldi.mastodon.view
|
||||
|
||||
import eu.toldi.mastodon.entities.Toot
|
||||
import javafx.application.Platform
|
||||
import javafx.scene.layout.BorderPane
|
||||
import javafx.scene.text.Text
|
||||
import eu.toldi.mastodon.helpers.BrowserHelper
|
||||
import javafx.scene.text.TextFlow
|
||||
import org.w3c.dom.Document
|
||||
import org.w3c.dom.Node
|
||||
import org.xml.sax.InputSource
|
||||
import tornadofx.*
|
||||
import java.io.StringReader
|
||||
import java.util.regex.Pattern
|
||||
import javax.xml.parsers.DocumentBuilderFactory
|
||||
|
||||
class TootView(val t:Toot) : View("My View") {
|
||||
|
||||
class TootView(val t: Toot) : View("My View") {
|
||||
lateinit var textFlow: TextFlow
|
||||
override val root = borderpane {
|
||||
top = hbox {
|
||||
setPrefSize(64.0, 64.0)
|
||||
|
@ -22,20 +29,61 @@ class TootView(val t:Toot) : View("My View") {
|
|||
}
|
||||
center {
|
||||
stackpane {
|
||||
val w = webview{
|
||||
engine.loadContent(t.content)
|
||||
engine.loadWorker.stateProperty().onChange {
|
||||
val wh = engine.executeScript("document.documentElement.scrollHeight").toString().toDouble()
|
||||
prefHeight = wh
|
||||
}
|
||||
}
|
||||
widthProperty().onChange {
|
||||
w.prefWidth = it
|
||||
val wh = w.engine.executeScript("document.documentElement.scrollHeight").toString().toDouble()
|
||||
w.prefHeight = wh
|
||||
textFlow = textflow {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
val br_pattern = Pattern.compile("(<br>|<br[ ]?/>)")
|
||||
init {
|
||||
parseContent()
|
||||
}
|
||||
|
||||
fun parseContent() {
|
||||
val factory = DocumentBuilderFactory.newDefaultInstance()
|
||||
val builder = factory.newDocumentBuilder()
|
||||
val html = "<html>${t.content}</html>"
|
||||
|
||||
var doc: Document = builder.parse(InputSource(StringReader(html.replace(br_pattern.toRegex(),"\n"))))
|
||||
|
||||
val nodes = doc.getElementsByTagName("html").item(0).childNodes
|
||||
for (i in 0..nodes.length) {
|
||||
val node = nodes.item(i) ?: continue
|
||||
parseNode(node)
|
||||
}
|
||||
}
|
||||
|
||||
fun parseNode(node: Node) {
|
||||
when {
|
||||
node.childNodes.length > 1 && node.nodeName != "a" -> {
|
||||
for (i in 0..node.childNodes.length) {
|
||||
val n = node.childNodes.item(i) ?: continue
|
||||
parseNode(n)
|
||||
}
|
||||
}
|
||||
node.nodeName == "p" -> {
|
||||
parseNode(node.childNodes.item(0))
|
||||
textFlow += text("\n\n")
|
||||
}
|
||||
|
||||
node.nodeName == "a" -> {
|
||||
textFlow += hyperlink(node.textContent) {
|
||||
setOnAction {
|
||||
BrowserHelper.openLink(node.attributes.getNamedItem("href").nodeValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
node.nodeName == "span" -> {
|
||||
textFlow += text(node.textContent)
|
||||
}
|
||||
node.nodeName == "br" -> {
|
||||
textFlow += text("\n")
|
||||
}
|
||||
else -> {
|
||||
textFlow += text(node.textContent)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue