diff --git a/demo/jupyter-playground/src/main/kotlin/hep/dataforge/playground/VisionForgePlayGroundForJupyter.kt b/demo/jupyter-playground/src/main/kotlin/hep/dataforge/playground/VisionForgePlayGroundForJupyter.kt index cd485580..c1228ce1 100644 --- a/demo/jupyter-playground/src/main/kotlin/hep/dataforge/playground/VisionForgePlayGroundForJupyter.kt +++ b/demo/jupyter-playground/src/main/kotlin/hep/dataforge/playground/VisionForgePlayGroundForJupyter.kt @@ -29,9 +29,10 @@ import space.kscience.gdml.Gdml @DFExperimental internal class VisionForgePlayGroundForJupyter : JupyterIntegration() { - val jsBundle = ResourceFallbacksBundle(listOf(ResourceLocation("js/visionforge-playground.js", - ResourcePathType.CLASSPATH_PATH))) - val jsResource = LibraryResource(name = "VisionForge", type = ResourceType.JS, bundles = listOf(jsBundle)) + private val jsBundle = ResourceFallbacksBundle(listOf( + ResourceLocation("js/visionforge-playground.js", ResourcePathType.CLASSPATH_PATH)) + ) + private val jsResource = LibraryResource(name = "VisionForge", type = ResourceType.JS, bundles = listOf(jsBundle)) private var counter = 0 @@ -43,7 +44,7 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() { } script { type = "text/javascript" - unsafe { +"window.renderVisionsAt(\"$id\");" } + unsafe { +"renderVisionsAt(\"$id\");" } } } @@ -54,17 +55,20 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() { VisionForge.withPlotly().withSolids() } - import("space.kscience.gdml.*", + import( + "space.kscience.gdml.*", "kscience.plotly.*", "kscience.plotly.models.*", "kotlinx.html.*", - "hep.dataforge.vision.solid.*") + "hep.dataforge.vision.solid.*" + ) + import("hep.dataforge.vision.VisionForge") + render { gdmlModel -> val fragment = VisionManager.fragment { vision(gdmlModel.toVision()) } - HTML(produceHtmlVisionString(fragment)) } diff --git a/demo/playground/src/jsMain/kotlin/playgroundMain.kt b/demo/playground/src/jsMain/kotlin/playgroundMain.kt index 26397ad4..de1b2fb4 100644 --- a/demo/playground/src/jsMain/kotlin/playgroundMain.kt +++ b/demo/playground/src/jsMain/kotlin/playgroundMain.kt @@ -1,14 +1,10 @@ //import hep.dataforge.vision.plotly.withPlotly -import hep.dataforge.vision.plotly.withPlotly -import hep.dataforge.vision.renderVisionsAt +import hep.dataforge.vision.plotly.usePlotly import hep.dataforge.vision.renderVisionsInWindow -import hep.dataforge.vision.solid.three.withThreeJs -import kotlinx.browser.window +import hep.dataforge.vision.solid.three.useThreeJs fun main() { - withPlotly() - withThreeJs() + usePlotly() + useThreeJs() renderVisionsInWindow() - window.asDynamic()["renderVisionsInWindow"] = ::renderVisionsInWindow - window.asDynamic()["renderVisionsAt"] = ::renderVisionsAt } \ No newline at end of file diff --git a/demo/playground/src/jvmMain/kotlin/gdmlCubes.kt b/demo/playground/src/jvmMain/kotlin/gdmlCubes.kt index a3d27b5f..29dff01a 100644 --- a/demo/playground/src/jvmMain/kotlin/gdmlCubes.kt +++ b/demo/playground/src/jvmMain/kotlin/gdmlCubes.kt @@ -61,8 +61,6 @@ internal val cubes = Gdml { @DFExperimental fun main() { - println(cubes.encodeToString()) - val content = VisionManager.fragment { vision("canvas") { cubes.toVision() diff --git a/demo/playground/src/jvmMain/kotlin/generateSchema.kt b/demo/playground/src/jvmMain/kotlin/generateSchema.kt index e3411516..25f82ea1 100644 --- a/demo/playground/src/jvmMain/kotlin/generateSchema.kt +++ b/demo/playground/src/jvmMain/kotlin/generateSchema.kt @@ -3,8 +3,10 @@ package hep.dataforge.vision.examples import com.github.ricky12awesome.jss.encodeToSchema import hep.dataforge.vision.solid.SolidGroup import hep.dataforge.vision.solid.SolidManager +import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.json.Json +@ExperimentalSerializationApi fun main() { val schema = Json { serializersModule = SolidManager.serializersModuleForSolids @@ -16,213 +18,4 @@ fun main() { encodeDefaults = true }.encodeToSchema(SolidGroup.serializer(), generateDefinitions = false) println(schema) -} - - -// -//import hep.dataforge.meta.JSON_PRETTY -//import kotlinx.serialization.* -//import kotlinx.serialization.descriptors.PrimitiveKind -//import kotlinx.serialization.descriptors.SerialDescriptor -//import kotlinx.serialization.descriptors.StructureKind -//import kotlinx.serialization.descriptors.UnionKind -//import kotlinx.serialization.json.* -//import kotlinx.serialization.modules.SerialModule -//import kotlin.reflect.KClass -// -//private fun SerialDescriptor.getJsonType() = when (this.kind) { -// StructureKind.LIST -> "array" -// PrimitiveKind.BYTE, PrimitiveKind.SHORT, PrimitiveKind.INT, PrimitiveKind.LONG, -// PrimitiveKind.FLOAT, PrimitiveKind.DOUBLE -> "number" -// PrimitiveKind.STRING, PrimitiveKind.CHAR, UnionKind.ENUM_KIND -> "string" -// PrimitiveKind.BOOLEAN -> "boolean" -// else -> "object" -//} -// -//private fun SerialDescriptor.isVisualObject() = serialName.startsWith("solid")||serialName.startsWith("group.solid") -// -//private const val definitionNode = "\$defs" -// -//private fun SerialModule.enumerate(type: KClass<*>): Sequence { -// val list = ArrayList() -// fun send(descriptor: SerialDescriptor) = list.add(descriptor) -// -// val enumerator = object : SerialModuleCollector { -// override fun contextual(kClass: KClass, serializer: KSerializer) { -// if (kClass == type) { -// send(serializer.descriptor) -// } -// } -// -// override fun polymorphic( -// baseClass: KClass, -// actualClass: KClass, -// actualSerializer: KSerializer -// ) { -// if (baseClass == type) { -// send(actualSerializer.descriptor) -// } -// } -// -// } -// dumpTo(enumerator) -// return list.asSequence() -//} -// -///** -// * Creates an [JsonObject] which contains Json Schema of given [descriptor]. -// * -// * Schema can contain following fields: -// * `description`, `type` for all descriptors; -// * `properties` and `required` for objects; -// * `enum` for enums; -// * `items` for arrays. -// * -// * User can modify this schema to add additional validation keywords -// * (as per [https://json-schema.org/latest/json-schema-validation.html]) -// * if they want. -// */ -//private fun jsonSchema(descriptor: SerialDescriptor, context: SerialModule): JsonObject { -// -// if (descriptor.serialName in arrayOf( -// "hep.dataforge.vision.solid.Point3D", -// "hep.dataforge.vision.solid.Point3D?", -// "hep.dataforge.vision.solid.Point2D", -// "hep.dataforge.vision.solid.Point2D?", -// "hep.dataforge.meta.Meta", -// "hep.dataforge.meta.Meta?" -// ) -// ) return json { -// "\$ref" to "#/$definitionNode/${descriptor.serialName.replace("?", "")}" -// } -// -// -// val properties: MutableMap = mutableMapOf() -// val requiredProperties: MutableSet = mutableSetOf() -// val isEnum = descriptor.kind == UnionKind.ENUM_KIND -// val isPolymorphic = descriptor.kind is PolymorphicKind -// -// -// if (!isEnum && !isPolymorphic) descriptor.elementDescriptors().forEachIndexed { index, child -> -// val elementName = descriptor.getElementName(index) -// -// val elementSchema = when (elementName) { -// "properties" -> buildJsonObject { -// put("\$ref", "#/$definitionNode/hep.dataforge.meta.Meta") -// } -// "first", "second" -> buildJsonObject { -// put("\$ref", "#/$definitionNode/children") -// } -// "styleSheet" -> buildJsonObject { -// put("type", "object") -// put("additionalProperties", buildJsonObject { -// put("\$ref", "#/$definitionNode/hep.dataforge.meta.Meta") -// }) -// } -// in arrayOf("children", "prototypes") -> buildJsonObject { -// put("type", "object") -// put("additionalProperties", buildJsonObject { -// put("\$ref", "#/$definitionNode/children") -// }) -// } -// else -> jsonSchema(child, context) -// } -// properties[elementName] = elementSchema -// -// if (!descriptor.isElementOptional(index)) requiredProperties.add(elementName) -// } -// -// val jsonType = descriptor.getJsonType() -// val objectData: MutableMap = mutableMapOf( -// "description" to JsonLiteral(descriptor.serialName), -// "type" to JsonLiteral(jsonType) -// ) -// if (isEnum) { -// val allElementNames = (0 until descriptor.elementsCount).map(descriptor::getElementName) -// objectData += "enum" to JsonArray(allElementNames.map(::JsonLiteral)) -// } -// when (jsonType) { -// "object" -> { -// if(descriptor.isVisualObject()) { -// properties["type"] = json { -// "const" to descriptor.serialName -// } -// } -// objectData["properties"] = JsonObject(properties) -// val required = requiredProperties.map { JsonLiteral(it) } -// if (required.isNotEmpty()) { -// objectData["required"] = JsonArray(required) -// } -// } -// "array" -> objectData["items"] = properties.values.let { -// check(it.size == 1) { "Array descriptor has returned inconsistent number of elements: expected 1, found ${it.size}" } -// it.first() -// } -// else -> { /* no-op */ -// } -// } -// return JsonObject(objectData) -//} -// -//fun main() { -// val context = SolidManager.serialModule -// val definitions = json { -// "children" to json { -// "anyOf" to jsonArray { -// context.enumerate(Solid::class).forEach { -// if (it.serialName == "hep.dataforge.vis.spatial.SolidGroup") { -// +json { -// "\$ref" to "#/$definitionNode/${it.serialName}" -// } -// } else { -// +jsonSchema(it, context) -// } -// } -// } -// } -// "hep.dataforge.meta.Meta" to json { -// "type" to "object" -// } -// "hep.dataforge.vision.solid.Point3D" to json { -// "type" to "object" -// "properties" to json { -// "x" to json { -// "type" to "number" -// } -// "y" to json { -// "type" to "number" -// } -// "z" to json { -// "type" to "number" -// } -// } -// } -// "hep.dataforge.vision.solid.Point2D" to json { -// "type" to "object" -// "properties" to json { -// "x" to json { -// "type" to "number" -// } -// "y" to json { -// "type" to "number" -// } -// } -// } -// "hep.dataforge.vision.solid.SolidGroup" to jsonSchema( -// SolidGroup.serializer().descriptor, -// context -// ) -// -// } -// -// println( -// JSON_PRETTY.stringify( -// JsonObjectSerializer, -// json { -// "\$defs" to definitions -// "\$ref" to "#/$definitionNode/hep.dataforge.vision.solid.SolidGroup" -// } -// ) -// ) -//} -// +} \ No newline at end of file diff --git a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionForge.kt b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionForge.kt index 2058c3cf..b9c98d93 100644 --- a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionForge.kt +++ b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionForge.kt @@ -2,4 +2,7 @@ package hep.dataforge.vision import hep.dataforge.context.Context +/** + * A drop-in replacement for [Global] for VisionForge activities + */ public expect val VisionForge: Context \ No newline at end of file diff --git a/visionforge-core/src/jsMain/kotlin/hep/dataforge/vision/VisionForge.kt b/visionforge-core/src/jsMain/kotlin/hep/dataforge/vision/VisionForge.kt index fc3cd8a6..9f478b34 100644 --- a/visionforge-core/src/jsMain/kotlin/hep/dataforge/vision/VisionForge.kt +++ b/visionforge-core/src/jsMain/kotlin/hep/dataforge/vision/VisionForge.kt @@ -24,6 +24,9 @@ public fun renderVisionsInWindow() { } } +/** + * Render all visions in an element with a given [id] + */ @JsExport public fun renderVisionsAt(id: String) { val element = document.getElementById(id) diff --git a/visionforge-plotly/src/jsMain/kotlin/hep/dataforge/vision/plotly/plotlyJs.kt b/visionforge-plotly/src/jsMain/kotlin/hep/dataforge/vision/plotly/plotlyJs.kt index 601c255f..27f13860 100644 --- a/visionforge-plotly/src/jsMain/kotlin/hep/dataforge/vision/plotly/plotlyJs.kt +++ b/visionforge-plotly/src/jsMain/kotlin/hep/dataforge/vision/plotly/plotlyJs.kt @@ -1,6 +1,8 @@ package hep.dataforge.vision.plotly -import hep.dataforge.context.* +import hep.dataforge.context.Context +import hep.dataforge.context.PluginFactory +import hep.dataforge.context.PluginTag import hep.dataforge.meta.Meta import hep.dataforge.names.Name import hep.dataforge.names.asName @@ -51,6 +53,6 @@ public actual class PlotlyPlugin : VisionPlugin(), ElementVisionRenderer { * Ensure that [PlotlyPlugin] is loaded in the global [VisionForge] context */ @JsExport -public fun withPlotly() { +public fun usePlotly() { VisionForge.plugins.fetch(PlotlyPlugin) } \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/hep/dataforge/vision/solid/three/ThreePlugin.kt b/visionforge-threejs/src/main/kotlin/hep/dataforge/vision/solid/three/ThreePlugin.kt index 246e90dd..b00a6cac 100644 --- a/visionforge-threejs/src/main/kotlin/hep/dataforge/vision/solid/three/ThreePlugin.kt +++ b/visionforge-threejs/src/main/kotlin/hep/dataforge/vision/solid/three/ThreePlugin.kt @@ -156,7 +156,7 @@ public class ThreePlugin : AbstractPlugin(), ElementVisionRenderer { * Ensure that [ThreePlugin] is loaded in the global [VisionForge] context */ @JsExport -public fun withThreeJs() { +public fun useThreeJs() { VisionForge.plugins.fetch(ThreePlugin) } diff --git a/visionforge-threejs/visionforge-threejs-server/src/jsMain/kotlin/hep/dataforge/vision/three/server/jsMain.kt b/visionforge-threejs/visionforge-threejs-server/src/jsMain/kotlin/hep/dataforge/vision/three/server/jsMain.kt index 268e1a8e..069eb073 100644 --- a/visionforge-threejs/visionforge-threejs-server/src/jsMain/kotlin/hep/dataforge/vision/three/server/jsMain.kt +++ b/visionforge-threejs/visionforge-threejs-server/src/jsMain/kotlin/hep/dataforge/vision/three/server/jsMain.kt @@ -1,9 +1,9 @@ package hep.dataforge.vision.three.server import hep.dataforge.vision.renderVisionsInWindow -import hep.dataforge.vision.solid.three.withThreeJs +import hep.dataforge.vision.solid.three.useThreeJs public fun main() { - withThreeJs() + useThreeJs() renderVisionsInWindow() } \ No newline at end of file