visionforge/demo/js-playground/src/main/kotlin/markupComponent.kt

56 lines
1.8 KiB
Kotlin
Raw Normal View History

2021-08-17 21:57:09 +03:00
import kotlinx.css.*
import kotlinx.css.properties.border
import kotlinx.dom.clear
import kotlinx.html.dom.append
import org.intellij.markdown.flavours.commonmark.CommonMarkFlavourDescriptor
import org.intellij.markdown.flavours.gfm.GFMFlavourDescriptor
import org.w3c.dom.Element
import org.w3c.dom.HTMLElement
2021-11-24 12:28:41 +03:00
import react.Props
2021-12-29 20:00:03 +03:00
import react.fc
2021-08-17 21:57:09 +03:00
import react.useEffect
import react.useRef
import space.kscience.visionforge.markup.VisionOfMarkup
import space.kscience.visionforge.markup.markdown
import space.kscience.visionforge.useProperty
import styled.css
import styled.styledDiv
2021-11-24 12:28:41 +03:00
external interface MarkupProps : Props {
2021-08-17 21:57:09 +03:00
var markup: VisionOfMarkup?
}
2021-12-29 20:00:03 +03:00
val Markup = fc<MarkupProps>("Markup") { props ->
2021-08-17 21:57:09 +03:00
val elementRef = useRef<Element>(null)
useEffect(props.markup, elementRef) {
val element = elementRef.current as? HTMLElement ?: error("Markup element not found")
props.markup?.let { vision ->
val flavour = when (vision.format) {
VisionOfMarkup.COMMONMARK_FORMAT -> CommonMarkFlavourDescriptor()
VisionOfMarkup.GFM_FORMAT -> GFMFlavourDescriptor()
//TODO add new formats via plugins
else -> error("Format ${vision.format} not recognized")
}
2021-08-18 23:02:17 +03:00
vision.useProperty(VisionOfMarkup::content) { content: String? ->
2021-08-17 21:57:09 +03:00
element.clear()
element.append {
markdown(flavour) { content ?: "" }
}
}
}
}
styledDiv {
css {
width = 100.pct
height = 100.pct
border(2.pt, BorderStyle.solid, Color.blue)
padding(left = 8.pt)
2021-08-17 21:57:09 +03:00
backgroundColor = Color.white
flex(1.0)
zIndex = 10000
}
ref = elementRef
}
}