-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement image content entity
- Loading branch information
Showing
9 changed files
with
148 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
.../commonMain/kotlin/dev/d1s/beam/commons/contententity/ImageContentEntityTypeDefinition.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2023 Mikhail Titov | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.d1s.beam.commons.contententity | ||
|
||
public data object ImageContentEntityTypeDefinition : CommonContentEntityTypeDefinition(name = "image") { | ||
|
||
val url: ContentEntityParameterDefinition = urlParameter(required = true) | ||
|
||
val description: ContentEntityParameterDefinition = parameter("description", translatable = true) | ||
|
||
val width: ContentEntityParameterDefinition = widthParameter() | ||
|
||
val height: ContentEntityParameterDefinition = heightParameter() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...mmon/src/commonMain/kotlin/dev/d1s/beam/commons/validation/ImageContentEntityValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2023 Mikhail Titov | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.d1s.beam.commons.validation | ||
|
||
import dev.d1s.beam.commons.contententity.ContentEntity | ||
import dev.d1s.beam.commons.contententity.ImageContentEntityTypeDefinition | ||
import io.konform.validation.ValidationBuilder | ||
|
||
internal object ImageContentEntityValidator : | ||
ContentEntityValidator<ImageContentEntityTypeDefinition>(ImageContentEntityTypeDefinition) { | ||
|
||
override fun ValidationBuilder<ContentEntity>.validate() { | ||
val validator = this@ImageContentEntityValidator | ||
|
||
requireCorrectUrl(validator, definition.url) | ||
requireNotBlankText(validator, definition.description) | ||
requireCorrectWidth(validator, definition.width) | ||
requireCorrectHeight(validator, definition.height) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
beam-ui/src/jsMain/kotlin/dev/d1s/beam/ui/contententity/ImageContentEntityRenderer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2023 Mikhail Titov | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.d1s.beam.ui.contententity | ||
|
||
import dev.d1s.beam.commons.Block | ||
import dev.d1s.beam.commons.contententity.ContentEntities | ||
import dev.d1s.beam.commons.contententity.ContentEntity | ||
import dev.d1s.beam.commons.contententity.ImageContentEntityTypeDefinition | ||
import dev.d1s.beam.commons.contententity.get | ||
import io.kvision.html.div | ||
import io.kvision.html.image | ||
import io.kvision.panel.SimplePanel | ||
import io.kvision.utils.perc | ||
import io.kvision.utils.px | ||
import org.koin.core.component.KoinComponent | ||
|
||
class ImageContentEntityRenderer : ContentEntityRenderer, KoinComponent { | ||
|
||
override val definition = ImageContentEntityTypeDefinition | ||
|
||
override fun SimplePanel.render(sequence: ContentEntities, block: Block) { | ||
renderImageRow { | ||
renderImages(sequence) | ||
} | ||
} | ||
|
||
private fun SimplePanel.renderImageRow(block: SimplePanel.() -> Unit) { | ||
div(className = "row row-cols-auto g-3") { | ||
block() | ||
} | ||
} | ||
|
||
private fun SimplePanel.renderImages(sequence: ContentEntities) { | ||
sequence.forEach { entity -> | ||
renderImage(entity) | ||
} | ||
} | ||
|
||
private fun SimplePanel.renderImage(entity: ContentEntity) { | ||
val parameters = entity.parameters | ||
|
||
val src = parameters[definition.url] | ||
requireNotNull(src) | ||
|
||
val description = parameters[definition.description] | ||
|
||
val width = parameters[definition.width]?.toInt() | ||
val height = parameters[definition.height]?.toInt() | ||
|
||
image(src, description, responsive = true) { | ||
width?.let { | ||
this.width = it.perc | ||
} | ||
|
||
height?.let { | ||
this.height = it.px | ||
} | ||
} | ||
} | ||
} |