Skip to content

Commit

Permalink
feat: implement embed content entity
Browse files Browse the repository at this point in the history
  • Loading branch information
d1snin committed Sep 6, 2023
1 parent 631a528 commit 16915ff
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ private val definitions = listOf(
TextContentEntityTypeDefinition,
ButtonLinkContentEntityTypeDefinition,
SpaceContentEntityTypeDefinition,
ImageContentEntityTypeDefinition
ImageContentEntityTypeDefinition,
EmbedContentEntityTypeDefinition
)

public fun definition(name: ContentEntityTypeName): ContentEntityTypeDefinition? =
Expand Down
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 EmbedContentEntityTypeDefinition : CommonContentEntityTypeDefinition(name = "embed") {

val url: ContentEntityParameterDefinition = urlParameter(required = true)

val document: ContentEntityParameterDefinition = parameter("document", translatable = true)

val width: ContentEntityParameterDefinition = widthParameter()

val height: ContentEntityParameterDefinition = heightParameter()
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ internal abstract class ContentEntityValidator<in D : ContentEntityTypeDefinitio
TextContentEntityValidator,
ButtonLinkContentEntityValidator,
SpaceContentEntityValidator,
ImageContentEntityValidator
ImageContentEntityValidator,
EmbedContentEntityValidator
)
}
}
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.EmbedContentEntityTypeDefinition
import io.konform.validation.ValidationBuilder

internal object EmbedContentEntityValidator :
ContentEntityValidator<EmbedContentEntityTypeDefinition>(EmbedContentEntityTypeDefinition) {

override fun ValidationBuilder<ContentEntity>.validate() {
val validator = this@EmbedContentEntityValidator

requireCorrectUrl(validator, definition.url)
requireNotBlankText(validator, definition.document)
requireCorrectWidth(validator, definition.width)
requireCorrectHeight(validator, definition.height)
}
}
5 changes: 5 additions & 0 deletions beam-ui/src/jsMain/kotlin/dev/d1s/beam/ui/Module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ object Qualifier {
val ButtonLinkContentEntityRenderer = named("button-link-content-entity-renderer")
val SpaceContentEntityRenderer = named("space-content-entity-renderer")
val ImageContentEntityRenderer = named("image-content-entity-renderer")
val EmbedContentEntityRenderer = named("embed-content-entity-renderer")

val NotFoundSpaceFailureCardContent = named("not-found-space-failure-card-content")
val EmptySpaceFailureCardContent = named("empty-space-failure-card-content")
Expand Down Expand Up @@ -177,6 +178,10 @@ private fun Module.contentEntityRenderers() {
singleOf<ContentEntityRenderer>(::ImageContentEntityRenderer) {
qualifier = Qualifier.ImageContentEntityRenderer
}

singleOf<ContentEntityRenderer>(::EmbedContentEntityRenderer) {
qualifier = Qualifier.EmbedContentEntityRenderer
}
}

private fun Module.spaceFailureCardContents() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.ContentEntity
import dev.d1s.beam.commons.contententity.EmbedContentEntityTypeDefinition
import dev.d1s.beam.commons.contententity.get
import io.kvision.core.Border
import io.kvision.core.BorderStyle
import io.kvision.core.Position
import io.kvision.html.div
import io.kvision.html.iframe
import io.kvision.panel.SimplePanel
import io.kvision.utils.perc
import io.kvision.utils.px
import org.koin.core.component.KoinComponent

class EmbedContentEntityRenderer : SingleContentEntityRenderer, KoinComponent {

override val definition = EmbedContentEntityTypeDefinition

override fun SimplePanel.render(entity: ContentEntity, block: Block) {
div(className = "d-flex w-100") {
renderIframe(entity)
separateContentEntity(entity, block)
}
}

private fun SimplePanel.renderIframe(entity: ContentEntity) {
val parameters = entity.parameters

val url = parameters[definition.url]
requireNotNull(url)

val document = parameters[definition.document]

val width = parameters[definition.width]?.toInt()
val height = parameters[definition.height]?.toInt()

val calculatedWidth = (width ?: 100).perc

div(className = "rounded overflow-hidden") {
this.width = calculatedWidth

height?.let {
this.height = it.px
}

iframe(
src = url
) {
document?.let {
srcdoc = it
}

position = Position.RELATIVE

this.width = calculatedWidth
this.height = this@div.height

border = Border(style = BorderStyle.NONE)

setAttribute("allowfullscreen", "true")
}
}
}
}

0 comments on commit 16915ff

Please sign in to comment.