From fbb402de90463f893d016e72329958c067e87ce8 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 3 Dec 2023 20:17:48 +0300 Subject: [PATCH] add builders for html field --- .../kscience/visionforge/html/VisionOfHtml.kt | 38 ++++++++++++++++++- .../visionforge/meta/VisionPropertyTest.kt | 2 - .../kscience/visionforge/inputRenderers.kt | 2 +- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/html/VisionOfHtml.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/html/VisionOfHtml.kt index 51458047..9cc223e5 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/html/VisionOfHtml.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/html/VisionOfHtml.kt @@ -25,8 +25,13 @@ public inline fun VisionOfPlainHtml.content(block: TagConsumer<*>.() -> Unit) { content = createHTML().apply(block).finalize() } +@Suppress("UnusedReceiverParameter") +public inline fun VisionOutput.html( + block: VisionOfPlainHtml.() -> Unit, +): VisionOfPlainHtml = VisionOfPlainHtml().apply(block) + @Serializable -public enum class InputFeedbackMode{ +public enum class InputFeedbackMode { /** * Fire feedback event on `onchange` event */ @@ -47,13 +52,18 @@ public enum class InputFeedbackMode{ @SerialName("html.input") public open class VisionOfHtmlInput( public val inputType: String, - public val feedbackMode: InputFeedbackMode = InputFeedbackMode.ONCHANGE + public val feedbackMode: InputFeedbackMode = InputFeedbackMode.ONCHANGE, ) : VisionOfHtml() { public var value: Value? by properties.value() public var disabled: Boolean by properties.boolean { false } public var fieldName: String? by properties.string() } +@Suppress("UnusedReceiverParameter") +public inline fun VisionOutput.htmlInput( + inputType: String, + block: VisionOfHtmlInput.() -> Unit = {}, +): VisionOfHtmlInput = VisionOfHtmlInput(inputType).apply(block) @Serializable @SerialName("html.text") @@ -61,18 +71,34 @@ public class VisionOfTextField : VisionOfHtmlInput(InputType.text.realValue) { public var text: String? by properties.string(key = VisionOfHtmlInput::value.name.asName()) } +@Suppress("UnusedReceiverParameter") +public inline fun VisionOutput.htmlTextField( + block: VisionOfTextField.() -> Unit = {}, +): VisionOfTextField = VisionOfTextField().apply(block) + + @Serializable @SerialName("html.checkbox") public class VisionOfCheckbox : VisionOfHtmlInput(InputType.checkBox.realValue) { public var checked: Boolean? by properties.boolean(key = VisionOfHtmlInput::value.name.asName()) } +@Suppress("UnusedReceiverParameter") +public inline fun VisionOutput.htmlCheckBox( + block: VisionOfCheckbox.() -> Unit = {}, +): VisionOfCheckbox = VisionOfCheckbox().apply(block) + @Serializable @SerialName("html.number") public class VisionOfNumberField : VisionOfHtmlInput(InputType.number.realValue) { public var number: Number? by properties.number(key = VisionOfHtmlInput::value.name.asName()) } +@Suppress("UnusedReceiverParameter") +public inline fun VisionOutput.htmlNumberField( + block: VisionOfNumberField.() -> Unit = {}, +): VisionOfNumberField = VisionOfNumberField().apply(block) + @Serializable @SerialName("html.range") public class VisionOfRangeField( @@ -83,3 +109,11 @@ public class VisionOfRangeField( public var number: Number? by properties.number(key = VisionOfHtmlInput::value.name.asName()) } +@Suppress("UnusedReceiverParameter") +public inline fun VisionOutput.htmlRangeField( + min: Double, + max: Double, + step: Double = 1.0, + block: VisionOfRangeField.() -> Unit = {}, +): VisionOfRangeField = VisionOfRangeField(min, max, step).apply(block) + diff --git a/visionforge-core/src/commonTest/kotlin/space/kscience/visionforge/meta/VisionPropertyTest.kt b/visionforge-core/src/commonTest/kotlin/space/kscience/visionforge/meta/VisionPropertyTest.kt index a6ea86a2..38abe34c 100644 --- a/visionforge-core/src/commonTest/kotlin/space/kscience/visionforge/meta/VisionPropertyTest.kt +++ b/visionforge-core/src/commonTest/kotlin/space/kscience/visionforge/meta/VisionPropertyTest.kt @@ -1,7 +1,6 @@ package space.kscience.visionforge.meta import kotlinx.coroutines.CompletableDeferred -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.take @@ -24,7 +23,6 @@ private class TestScheme : Scheme() { companion object : SchemeSpec(::TestScheme) } -@OptIn(ExperimentalCoroutinesApi::class) internal class VisionPropertyTest { private val manager = Global.request(VisionManager) diff --git a/visionforge-core/src/jsMain/kotlin/space/kscience/visionforge/inputRenderers.kt b/visionforge-core/src/jsMain/kotlin/space/kscience/visionforge/inputRenderers.kt index af54bd75..e1410752 100644 --- a/visionforge-core/src/jsMain/kotlin/space/kscience/visionforge/inputRenderers.kt +++ b/visionforge-core/src/jsMain/kotlin/space/kscience/visionforge/inputRenderers.kt @@ -194,7 +194,7 @@ internal fun FormData.toMeta(): Meta { } internal val formVisionRenderer: ElementVisionRenderer = - ElementVisionRenderer { visionName, vision, _ -> + ElementVisionRenderer { _, vision, _ -> val form = document.getElementById(vision.formId) as? HTMLFormElement ?: error("An element with id = '${vision.formId} is not a form")