Browse Source

Make DomElementWrapper private (#1749)

pull/1636/merge
Shagen Ogandzhanian 3 years ago committed by GitHub
parent
commit
4bf01cf5e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/StyleBuilder.kt
  2. 68
      web/core/src/jsMain/kotlin/org/jetbrains/compose/web/elements/Base.kt
  3. 19
      web/internal-web-core-runtime/src/jsMain/kotlin/org/jetbrains/compose/web/internal/runtime/DomApplier.kt

2
web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/StyleBuilder.kt

@ -7,9 +7,7 @@
package org.jetbrains.compose.web.css package org.jetbrains.compose.web.css
import org.jetbrains.compose.web.internal.runtime.DomElementWrapper
import org.jetbrains.compose.web.internal.runtime.ComposeWebInternalApi import org.jetbrains.compose.web.internal.runtime.ComposeWebInternalApi
import org.w3c.dom.css.CSSStyleDeclaration
import kotlin.properties.ReadOnlyProperty import kotlin.properties.ReadOnlyProperty
/** /**

68
web/core/src/jsMain/kotlin/org/jetbrains/compose/web/elements/Base.kt

@ -4,8 +4,9 @@ import androidx.compose.runtime.*
import org.jetbrains.compose.web.attributes.AttrsBuilder import org.jetbrains.compose.web.attributes.AttrsBuilder
import org.jetbrains.compose.web.ExperimentalComposeWebApi import org.jetbrains.compose.web.ExperimentalComposeWebApi
import org.jetbrains.compose.web.css.StyleHolder import org.jetbrains.compose.web.css.StyleHolder
import org.jetbrains.compose.web.internal.runtime.DomElementWrapper
import org.jetbrains.compose.web.internal.runtime.ComposeWebInternalApi import org.jetbrains.compose.web.internal.runtime.ComposeWebInternalApi
import org.jetbrains.compose.web.internal.runtime.DomNodeWrapper
import org.jetbrains.compose.web.internal.runtime.NamedEventListener
import org.w3c.dom.Element import org.w3c.dom.Element
import org.w3c.dom.HTMLElement import org.w3c.dom.HTMLElement
import org.w3c.dom.css.ElementCSSInlineStyle import org.w3c.dom.css.ElementCSSInlineStyle
@ -35,44 +36,57 @@ private inline fun <TScope, T> ComposeDomNode(
currentComposer.endNode() currentComposer.endNode()
} }
@ComposeWebInternalApi
private class DomElementWrapper(override val node: Element): DomNodeWrapper(node) {
private var currentListeners = emptyList<NamedEventListener>()
@OptIn(ComposeWebInternalApi::class) fun updateEventListeners(list: List<NamedEventListener>) {
private fun DomElementWrapper.updateProperties(applicators: List<Pair<(Element, Any) -> Unit, Any>>) { currentListeners.forEach {
node.removeAttribute("class") node.removeEventListener(it.name, it)
}
applicators.forEach { (applicator, item) -> currentListeners = list
applicator(node, item)
currentListeners.forEach {
node.addEventListener(it.name, it)
}
} }
}
@OptIn(ComposeWebInternalApi::class) fun updateProperties(applicators: List<Pair<(Element, Any) -> Unit, Any>>) {
private fun DomElementWrapper.updateStyleDeclarations(styleApplier: StyleHolder) { node.removeAttribute("class")
when (node) {
is HTMLElement, is SVGElement -> { applicators.forEach { (applicator, item) ->
node.removeAttribute("style") applicator(node, item)
}
}
val style = node.unsafeCast<ElementCSSInlineStyle>().style fun updateStyleDeclarations(styleApplier: StyleHolder) {
when (node) {
is HTMLElement, is SVGElement -> {
node.removeAttribute("style")
styleApplier.properties.forEach { (name, value) -> val style = node.unsafeCast<ElementCSSInlineStyle>().style
style.setProperty(name, value.toString())
}
styleApplier.variables.forEach { (name, value) -> styleApplier.properties.forEach { (name, value) ->
style.setProperty(name, value.toString()) style.setProperty(name, value.toString())
}
styleApplier.variables.forEach { (name, value) ->
style.setProperty(name, value.toString())
}
} }
} }
} }
}
@OptIn(ComposeWebInternalApi::class) fun updateAttrs(attrs: Map<String, String>) {
fun DomElementWrapper.updateAttrs(attrs: Map<String, String>) { node.getAttributeNames().forEach { name ->
node.getAttributeNames().forEach { name -> if (name == "style") return@forEach
if (name == "style") return@forEach node.removeAttribute(name)
node.removeAttribute(name) }
}
attrs.forEach { attrs.forEach {
node.setAttribute(it.key, it.value) node.setAttribute(it.key, it.value)
}
} }
} }

19
web/internal-web-core-runtime/src/jsMain/kotlin/org/jetbrains/compose/web/internal/runtime/DomApplier.kt

@ -71,21 +71,4 @@ open class DomNodeWrapper(open val node: Node) {
node.insertBefore(child, node.childNodes[toIndex]!!) node.insertBefore(child, node.childNodes[toIndex]!!)
} }
} }
} }
@ComposeWebInternalApi
class DomElementWrapper(override val node: Element): DomNodeWrapper(node) {
private var currentListeners = emptyList<NamedEventListener>()
fun updateEventListeners(list: List<NamedEventListener>) {
currentListeners.forEach {
node.removeEventListener(it.name, it)
}
currentListeners = list
currentListeners.forEach {
node.addEventListener(it.name, it)
}
}
}
Loading…
Cancel
Save