diff --git a/src/main/kotlin/com/marvinelsen/willow/ui/cells/EntryCellFactory.kt b/src/main/kotlin/com/marvinelsen/willow/ui/cells/DictionaryEntryCellFactory.kt similarity index 98% rename from src/main/kotlin/com/marvinelsen/willow/ui/cells/EntryCellFactory.kt rename to src/main/kotlin/com/marvinelsen/willow/ui/cells/DictionaryEntryCellFactory.kt index 5634aaf..13f3170 100644 --- a/src/main/kotlin/com/marvinelsen/willow/ui/cells/EntryCellFactory.kt +++ b/src/main/kotlin/com/marvinelsen/willow/ui/cells/DictionaryEntryCellFactory.kt @@ -28,7 +28,7 @@ class DictionaryEntryCellFactory(private val resources: ResourceBundle, private } } -internal class EntryCell(private val resources: ResourceBundle, private val config: Config) : +private class EntryCell(private val resources: ResourceBundle, private val config: Config) : ListCell() { private val labelHeadword = Label().apply { styleClass.add("headword") diff --git a/src/main/kotlin/com/marvinelsen/willow/ui/cells/SentenceCellFactory.kt b/src/main/kotlin/com/marvinelsen/willow/ui/cells/SentenceCellFactory.kt index dc06116..c76bc79 100644 --- a/src/main/kotlin/com/marvinelsen/willow/ui/cells/SentenceCellFactory.kt +++ b/src/main/kotlin/com/marvinelsen/willow/ui/cells/SentenceCellFactory.kt @@ -50,7 +50,6 @@ private class SentenceCell(private val config: Config) : ListCell() ) ) - graphic = root } } diff --git a/src/main/kotlin/com/marvinelsen/willow/ui/controllers/DetailsController.kt b/src/main/kotlin/com/marvinelsen/willow/ui/controllers/DetailsController.kt index fbae74e..da8a223 100644 --- a/src/main/kotlin/com/marvinelsen/willow/ui/controllers/DetailsController.kt +++ b/src/main/kotlin/com/marvinelsen/willow/ui/controllers/DetailsController.kt @@ -19,6 +19,7 @@ import javafx.scene.control.TabPane import javafx.scene.input.ContextMenuEvent import javafx.scene.layout.FlowPane import javafx.scene.web.WebView +import kotlinx.html.DIV import kotlinx.html.body import kotlinx.html.div import kotlinx.html.h1 @@ -29,7 +30,7 @@ import kotlinx.html.span import kotlinx.html.stream.createHTML import java.util.ResourceBundle -@Suppress("UnusedPrivateMember") +@Suppress("UnusedPrivateMember", "TooManyFunctions") class DetailsController(private val model: Model, private val config: Config) { @FXML private lateinit var resources: ResourceBundle @@ -79,7 +80,6 @@ class DetailsController(private val model: Model, private val config: Config) { @FXML private lateinit var labelNoSentencesFound: Label - @FXML private fun initialize() { initializeLabelHeadword() @@ -227,101 +227,7 @@ class DetailsController(private val model: Model, private val config: Config) { model.selectedEntry.addListener { _, _, newEntry -> if (newEntry == null) return@addListener - webViewDefinition.engine.loadContent( - createHTML().html { - body { - if (newEntry.cedictDefinitions.isNotEmpty()) { - div(classes = "cedict-definition") { - h1 { - +"CC-CEDICT" - } - ol { - for (definition in newEntry.cedictDefinitions) { - li { - +definition.joinToString(separator = "; ") - } - } - } - } - } - if (newEntry.crossStraitsDefinitions.isNotEmpty()) { - div(classes = "cross-straits-definition") { - h1 { - +"Cross-Straits" - } - ol { - newEntry.crossStraitsDefinitions.forEach { definition -> - li { - span(classes = "definition") { - +definition.definition - } - if (definition.examples.isNotEmpty()) { - span(classes = "example") { - +definition.examples.joinToString( - prefix = "如:", - separator = "、", - postfix = "。" - ) { "「$it」" } - } - } - } - } - } - } - } - if (newEntry.moedictDefinitions.isNotEmpty()) { - div(classes = "moe-definition") { - h1 { - +"MOE" - } - newEntry.moedictDefinitions.groupBy { - it.type ?: "" - }.entries.forEach { (type, definitions) -> - if (type != "") { - span(classes = "type") { - +type - } - } - - ol { - definitions.forEach { definition -> - li { - span(classes = "definition") { - +definition.definition - } - - definition.examples.forEach { example -> - span(classes = "example") { - +example - } - } - - definition.quotes.forEach { quote -> - span(classes = "quote") { - +quote - } - } - - definition.synonyms?.let { - span(classes = "synonyms") { - +"似:${it.replace(",", "、")}" - } - } - - definition.antonyms?.let { - span(classes = "antonyms") { - +"反:${it.replace(",", "、")}" - } - } - } - } - } - } - } - } - } - } - ) + webViewDefinition.engine.loadContent(createDefinitionHtml(newEntry)) } } @@ -336,6 +242,7 @@ class DetailsController(private val model: Model, private val config: Config) { ) } + @Suppress("ReturnCount") private fun lazyUpdateTabContent(selectedTabId: String?) { when (selectedTabId) { "tabWords" -> { @@ -359,4 +266,105 @@ class DetailsController(private val model: Model, private val config: Config) { else -> {} } } + + private fun createDefinitionHtml(entry: DictionaryEntryFx) = createHTML().html { + body { + if (entry.cedictDefinitions.isNotEmpty()) { + div(classes = "cedict-definition") { + h1 { + +"CC-CEDICT" + } + cedictDefinition(entry) + } + } + + if (entry.crossStraitsDefinitions.isNotEmpty()) { + div(classes = "cross-straits-definition") { + h1 { + +"Cross-Straits" + } + crossStraitsDefinition(entry) + } + } + + if (entry.moedictDefinitions.isNotEmpty()) { + div(classes = "moe-definition") { + h1 { + +"MOE" + } + moeDefinition(entry) + } + } + } + } } + +private fun DIV.cedictDefinition(entry: DictionaryEntryFx) = ol { + for (definition in entry.cedictDefinitions) { + li { + +definition.joinToString(separator = "; ") + } + } +} + +private fun DIV.crossStraitsDefinition(entry: DictionaryEntryFx) = ol { + entry.crossStraitsDefinitions.forEach { definition -> + li { + span(classes = "definition") { + +definition.definition + } + if (definition.examples.isNotEmpty()) { + span(classes = "example") { + +definition.examples.joinToString( + prefix = "如:", + separator = "、", + postfix = "。" + ) { "「$it」" } + } + } + } + } +} + +private fun DIV.moeDefinition(entry: DictionaryEntryFx) = + entry.moedictDefinitions.groupBy { it.type ?: "" }.entries.forEach { (type, definitions) -> + if (type != "") { + span(classes = "type") { + +type + } + } + + ol { + definitions.forEach { definition -> + li { + span(classes = "definition") { + +definition.definition + } + + definition.examples.forEach { example -> + span(classes = "example") { + +example + } + } + + definition.quotes.forEach { quote -> + span(classes = "quote") { + +quote + } + } + + definition.synonyms?.let { + span(classes = "synonyms") { + +"似:${it.replace(",", "、")}" + } + } + + definition.antonyms?.let { + span(classes = "antonyms") { + +"反:${it.replace(",", "、")}" + } + } + } + } + } + }