From 787c841a51b6a1779507dc4021ce97510c4bd653 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 21 Jul 2021 16:31:17 +0300 Subject: [PATCH] [WIP] new DF experiment --- build.gradle.kts | 4 +- settings.gradle.kts | 1 + .../space/kscience/visionforge/VisionBase.kt | 8 ++-- .../kscience/visionforge/VisionChange.kt | 2 +- .../visionforge/VisionPropertyContainer.kt | 7 +-- .../kscience/visionforge/schemeDesctiptors.kt | 5 ++- visionforge-markdown/build.gradle.kts | 44 +++++++++++++++++++ .../visionforge/markup/VisionOfMarkup.kt | 40 +++++++++++++++++ .../kscience/visionforge/markup/markdown.kt | 25 +++++++++++ .../visionforge/markup/MarkupPlugin.kt | 38 ++++++++++++++++ .../visionforge/plotly/VisionOfPlotly.kt | 1 - .../visionforge/three/server/VisionServer.kt | 2 +- .../kscience/visionforge/solid/Extruded.kt | 4 +- 13 files changed, 163 insertions(+), 18 deletions(-) create mode 100644 visionforge-markdown/build.gradle.kts create mode 100644 visionforge-markdown/src/commonMain/kotlin/space/kscience/visionforge/markup/VisionOfMarkup.kt create mode 100644 visionforge-markdown/src/commonMain/kotlin/space/kscience/visionforge/markup/markdown.kt create mode 100644 visionforge-markdown/src/jsMain/kotlin/space/kscience/visionforge/markup/MarkupPlugin.kt diff --git a/build.gradle.kts b/build.gradle.kts index 4ab2a7d5..c7fbc2d8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { // kotlin("js") version(kotlinVersion) apply false } -val dataforgeVersion by extra("0.4.3") +val dataforgeVersion by extra("0.5.0-dev-2") val fxVersion by extra("11") allprojects { @@ -20,7 +20,7 @@ allprojects { } group = "space.kscience" - version = "0.2.0-dev-22" + version = "0.2.0-dev-23" } subprojects { diff --git a/settings.gradle.kts b/settings.gradle.kts index 6311841e..70f3606a 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -33,6 +33,7 @@ include( ":visionforge-gdml", ":visionforge-server", ":visionforge-plotly", + ":visionforge-markdown", ":demo:solid-showcase", ":demo:gdml", ":demo:muon-monitor", diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionBase.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionBase.kt index 78b75124..4dcffbca 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionBase.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionBase.kt @@ -30,13 +30,13 @@ internal data class PropertyListener( @SerialName("vision") public open class VisionBase( override @Transient var parent: VisionGroup? = null, - protected var properties: Config? = null + protected var properties: MutableItemProvider? = null ) : Vision { @Synchronized - protected fun getOrCreateProperties(): Config { + protected fun getOrCreateProperties(): MutableItemProvider { if (properties == null) { - val newProperties = Config() + val newProperties = MetaBuilder() properties = newProperties } return properties!! @@ -46,7 +46,7 @@ public open class VisionBase( * A fast accessor method to get own property (no inheritance or styles */ override fun getOwnProperty(name: Name): MetaItem? = if (name == Name.EMPTY) { - properties?.asMetaItem() + properties?.rootItem } else { properties?.getItem(name) } diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionChange.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionChange.kt index 06ccb7bc..aa50001a 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionChange.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionChange.kt @@ -21,7 +21,7 @@ public class VisionChangeBuilder : VisionContainerBuilder { private var reset: Boolean = false private var vision: Vision? = null - private val propertyChange = Config() + private val propertyChange = MetaBuilder() private val children: HashMap = HashMap() public fun isEmpty(): Boolean = propertyChange.isEmpty() && propertyChange.isEmpty() && children.isEmpty() diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionPropertyContainer.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionPropertyContainer.kt index 112295c9..3710175c 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionPropertyContainer.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionPropertyContainer.kt @@ -1,9 +1,6 @@ package space.kscience.visionforge -import space.kscience.dataforge.meta.Config -import space.kscience.dataforge.meta.MetaItem -import space.kscience.dataforge.meta.get -import space.kscience.dataforge.meta.set +import space.kscience.dataforge.meta.* import space.kscience.dataforge.names.Name /** @@ -20,7 +17,7 @@ public interface VisionPropertyContainer { public fun setProperty(name: Name, item: MetaItem?, notify: Boolean = true) } -public open class SimpleVisionPropertyContainer(protected val config: Config): VisionPropertyContainer{ +public open class SimpleVisionPropertyContainer(protected val config: ObservableMeta): VisionPropertyContainer{ override fun getProperty( name: Name, inherit: Boolean, diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/schemeDesctiptors.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/schemeDesctiptors.kt index 8f342b52..4187843f 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/schemeDesctiptors.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/schemeDesctiptors.kt @@ -2,10 +2,11 @@ package space.kscience.visionforge import space.kscience.dataforge.meta.Scheme import space.kscience.dataforge.meta.SchemeSpec +import space.kscience.dataforge.meta.asObservable import space.kscience.dataforge.meta.descriptors.NodeDescriptor import space.kscience.dataforge.meta.descriptors.NodeDescriptorBuilder import space.kscience.dataforge.meta.descriptors.ValueDescriptorBuilder -import space.kscience.dataforge.meta.toConfig +import space.kscience.dataforge.meta.toMutableMeta import space.kscience.dataforge.values.ValueType import kotlin.reflect.KProperty1 import kotlin.reflect.typeOf @@ -53,7 +54,7 @@ public inline fun NodeDescriptorBuilder.value( } public fun NodeDescriptor.copy(block: NodeDescriptorBuilder.() -> Unit = {}): NodeDescriptor { - return NodeDescriptorBuilder(toMeta().toConfig()).apply(block) + return NodeDescriptorBuilder(toMeta().toMutableMeta().asObservable()).apply(block) } public inline fun NodeDescriptorBuilder.scheme( diff --git a/visionforge-markdown/build.gradle.kts b/visionforge-markdown/build.gradle.kts new file mode 100644 index 00000000..210e4ee8 --- /dev/null +++ b/visionforge-markdown/build.gradle.kts @@ -0,0 +1,44 @@ +plugins { + id("ru.mipt.npm.gradle.mpp") +} + +val markdownVersion = "0.2.4" + +kscience { + useSerialization() +} + +kotlin { + js { + //binaries.library() + binaries.executable() + browser { + webpackTask { + outputFileName = "js/visionforge-markdown.js" + } + } + } + + jvm { + val processResourcesTaskName = + compilations[org.jetbrains.kotlin.gradle.plugin.KotlinCompilation.MAIN_COMPILATION_NAME] + .processResourcesTaskName + } + + + val jsBrowserDistribution by tasks.getting + + tasks.getByName("jvmProcessResources") { + dependsOn(jsBrowserDistribution) + from(jsBrowserDistribution) + } + + sourceSets { + commonMain { + dependencies { + api(project(":visionforge-core")) + api("org.jetbrains:markdown:$markdownVersion") + } + } + } +} \ No newline at end of file diff --git a/visionforge-markdown/src/commonMain/kotlin/space/kscience/visionforge/markup/VisionOfMarkup.kt b/visionforge-markdown/src/commonMain/kotlin/space/kscience/visionforge/markup/VisionOfMarkup.kt new file mode 100644 index 00000000..689ed221 --- /dev/null +++ b/visionforge-markdown/src/commonMain/kotlin/space/kscience/visionforge/markup/VisionOfMarkup.kt @@ -0,0 +1,40 @@ +package space.kscience.visionforge.markup + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import kotlinx.serialization.modules.SerializersModule +import kotlinx.serialization.modules.polymorphic +import kotlinx.serialization.modules.subclass +import space.kscience.dataforge.meta.string +import space.kscience.dataforge.names.Name +import space.kscience.dataforge.names.toName +import space.kscience.visionforge.Vision +import space.kscience.visionforge.VisionBase +import space.kscience.visionforge.setProperty + +@Serializable +@SerialName("vision.markup") +public class VisionOfMarkup( + public val format: String = COMMONMARK_FORMAT +) : VisionBase() { + + //TODO add templates + + public var content: String? + get() = getOwnProperty(CONTENT_PROPERTY_KEY).string + set(value) { + setProperty(CONTENT_PROPERTY_KEY, value) + } + + public companion object { + public val CONTENT_PROPERTY_KEY: Name = "content".toName() + public const val COMMONMARK_FORMAT: String = "markdown.commonmark" + public const val GFM_FORMAT: String = "markdown.gfm" + } +} + +internal val markupSerializersModule = SerializersModule { + polymorphic(Vision::class) { + subclass(VisionOfMarkup.serializer()) + } +} \ No newline at end of file diff --git a/visionforge-markdown/src/commonMain/kotlin/space/kscience/visionforge/markup/markdown.kt b/visionforge-markdown/src/commonMain/kotlin/space/kscience/visionforge/markup/markdown.kt new file mode 100644 index 00000000..0ac353e4 --- /dev/null +++ b/visionforge-markdown/src/commonMain/kotlin/space/kscience/visionforge/markup/markdown.kt @@ -0,0 +1,25 @@ +package space.kscience.visionforge.markup + +import kotlinx.html.TagConsumer +import kotlinx.html.div +import kotlinx.html.unsafe +import org.intellij.markdown.flavours.MarkdownFlavourDescriptor +import org.intellij.markdown.flavours.commonmark.CommonMarkFlavourDescriptor +import org.intellij.markdown.html.HtmlGenerator +import org.intellij.markdown.parser.MarkdownParser + +/** + * Render markdown inside kotlinx-html tag + */ +public fun TagConsumer.markdown( + flavour: MarkdownFlavourDescriptor = CommonMarkFlavourDescriptor(), + block: () -> String +): T { + val src = block() + val parsedTree = MarkdownParser(flavour).buildMarkdownTreeFromString(src) + return div("visionforge-markdown") { + unsafe { + +HtmlGenerator(src, parsedTree, flavour).generateHtml() + } + } +} \ No newline at end of file diff --git a/visionforge-markdown/src/jsMain/kotlin/space/kscience/visionforge/markup/MarkupPlugin.kt b/visionforge-markdown/src/jsMain/kotlin/space/kscience/visionforge/markup/MarkupPlugin.kt new file mode 100644 index 00000000..30b8b848 --- /dev/null +++ b/visionforge-markdown/src/jsMain/kotlin/space/kscience/visionforge/markup/MarkupPlugin.kt @@ -0,0 +1,38 @@ +package space.kscience.visionforge.markup + +import kotlinx.browser.document +import kotlinx.serialization.modules.SerializersModule +import org.w3c.dom.Element +import space.kscience.dataforge.context.Context +import space.kscience.dataforge.context.PluginFactory +import space.kscience.dataforge.context.PluginTag +import space.kscience.dataforge.meta.Meta +import space.kscience.visionforge.ElementVisionRenderer +import space.kscience.visionforge.Vision +import space.kscience.visionforge.VisionClient +import space.kscience.visionforge.VisionPlugin +import kotlin.reflect.KClass + +public class MarkupPlugin : VisionPlugin(), ElementVisionRenderer { + public val visionClient: VisionClient by require(VisionClient) + override val tag: PluginTag get() = Companion.tag + override val visionSerializersModule: SerializersModule get() = markupSerializersModule + + override fun rateVision(vision: Vision): Int = when (vision) { + is VisionOfMarkup -> ElementVisionRenderer.DEFAULT_RATING + else -> ElementVisionRenderer.ZERO_RATING + } + + override fun render(element: Element, vision: Vision, meta: Meta) { + require(vision is VisionOfMarkup) { "The vision is not a markup vision" } + val div = document.createElement("div") + element.append(div) + TODO() + } + + public companion object : PluginFactory { + override val tag: PluginTag = PluginTag("vision.markup", PluginTag.DATAFORGE_GROUP) + override val type: KClass = MarkupPlugin::class + override fun invoke(meta: Meta, context: Context): MarkupPlugin = MarkupPlugin() + } +} \ No newline at end of file diff --git a/visionforge-plotly/src/commonMain/kotlin/space/kscience/visionforge/plotly/VisionOfPlotly.kt b/visionforge-plotly/src/commonMain/kotlin/space/kscience/visionforge/plotly/VisionOfPlotly.kt index 773ff4bf..8603971e 100644 --- a/visionforge-plotly/src/commonMain/kotlin/space/kscience/visionforge/plotly/VisionOfPlotly.kt +++ b/visionforge-plotly/src/commonMain/kotlin/space/kscience/visionforge/plotly/VisionOfPlotly.kt @@ -2,7 +2,6 @@ package space.kscience.visionforge.plotly import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import space.kscience.dataforge.meta.Config import space.kscience.dataforge.misc.DFExperimental import space.kscience.plotly.Plot import space.kscience.plotly.Plotly diff --git a/visionforge-server/src/main/kotlin/space/kscience/visionforge/three/server/VisionServer.kt b/visionforge-server/src/main/kotlin/space/kscience/visionforge/three/server/VisionServer.kt index 2c9ed631..fabb0675 100644 --- a/visionforge-server/src/main/kotlin/space/kscience/visionforge/three/server/VisionServer.kt +++ b/visionforge-server/src/main/kotlin/space/kscience/visionforge/three/server/VisionServer.kt @@ -54,7 +54,7 @@ public class VisionServer internal constructor( private val application: Application, private val rootRoute: String, ) : Configurable, CoroutineScope by application { - override val config: Config = Config() + override val config: ObservableMeta = ObservableMeta() public var updateInterval: Long by config.long(300, key = UPDATE_INTERVAL_KEY) public var cacheFragments: Boolean by config.boolean(true) public var dataEmbed: Boolean by config.boolean(true, "data.embed".toName()) diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Extruded.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Extruded.kt index 40e03512..d94062d8 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Extruded.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Extruded.kt @@ -2,7 +2,7 @@ package space.kscience.visionforge.solid import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import space.kscience.dataforge.meta.Config +import space.kscience.dataforge.meta.ObservableMeta import space.kscience.visionforge.* import kotlin.math.PI import kotlin.math.cos @@ -95,7 +95,7 @@ public class Extruded( public class ExtrudeBuilder( public var shape: List = emptyList(), public var layers: ArrayList = ArrayList(), - config: Config = Config() + config: ObservableMeta = ObservableMeta() ) : SimpleVisionPropertyContainer(config) { public fun shape(block: Shape2DBuilder.() -> Unit) { this.shape = Shape2DBuilder().apply(block).build()