Allow creation of HtmlElement with HtmlBuilder
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
group 'nl.astraeus'
|
group 'nl.astraeus'
|
||||||
version '0.1.14-SNAPSHOT'
|
version '0.1.15-SNAPSHOT'
|
||||||
|
|
||||||
apply plugin: 'kotlin2js'
|
apply plugin: 'kotlin2js'
|
||||||
apply plugin: 'kotlin-dce-js'
|
apply plugin: 'kotlin-dce-js'
|
||||||
|
|||||||
2
komp.iml
2
komp.iml
@@ -1,5 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<module external.linked.project.id="komp" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="nl.astraeus" external.system.module.version="0.1.14-SNAPSHOT" type="JAVA_MODULE" version="4">
|
<module external.linked.project.id="komp" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="nl.astraeus" external.system.module.version="0.1.15-SNAPSHOT" type="JAVA_MODULE" version="4">
|
||||||
<component name="FacetManager">
|
<component name="FacetManager">
|
||||||
<facet type="kotlin-language" name="Kotlin">
|
<facet type="kotlin-language" name="Kotlin">
|
||||||
<configuration version="3" platform="JavaScript " allPlatforms="JS []" useProjectSettings="false">
|
<configuration version="3" platform="JavaScript " allPlatforms="JS []" useProjectSettings="false">
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import org.w3c.dom.Node
|
|||||||
import org.w3c.dom.asList
|
import org.w3c.dom.asList
|
||||||
import org.w3c.dom.css.CSSStyleDeclaration
|
import org.w3c.dom.css.CSSStyleDeclaration
|
||||||
import org.w3c.dom.events.Event
|
import org.w3c.dom.events.Event
|
||||||
|
import kotlin.browser.document
|
||||||
|
|
||||||
@Suppress("NOTHING_TO_INLINE")
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
private inline fun HTMLElement.setEvent(name: String, noinline callback : (Event) -> Unit) : Unit {
|
private inline fun HTMLElement.setEvent(name: String, noinline callback : (Event) -> Unit) : Unit {
|
||||||
@@ -139,7 +140,7 @@ class HtmlBuilder(
|
|||||||
// stupid hack as browsers doesn't support createEntityReference
|
// stupid hack as browsers doesn't support createEntityReference
|
||||||
val s = document.createElement("span") as HTMLElement
|
val s = document.createElement("span") as HTMLElement
|
||||||
s.innerHTML = entity.text
|
s.innerHTML = entity.text
|
||||||
path.last().appendChild(s.childNodes.asList().filter { it.nodeType == Node.TEXT_NODE }.first())
|
path.last().appendChild(s.childNodes.asList().first { it.nodeType == Node.TEXT_NODE })
|
||||||
|
|
||||||
// other solution would be
|
// other solution would be
|
||||||
// pathLast().innerHTML += entity.text
|
// pathLast().innerHTML += entity.text
|
||||||
@@ -170,4 +171,11 @@ class HtmlBuilder(
|
|||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
private fun HTMLElement.asR(): HTMLElement = this.asDynamic()
|
private fun HTMLElement.asR(): HTMLElement = this.asDynamic()
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun create(content: HtmlBuilder.() -> Unit) : HTMLElement {
|
||||||
|
val consumer = HtmlBuilder(DummyKomponent(), document)
|
||||||
|
content.invoke(consumer)
|
||||||
|
return consumer.finalize()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package nl.astraeus.komp
|
package nl.astraeus.komp
|
||||||
|
|
||||||
import kotlinx.html.Tag
|
import kotlinx.html.Tag
|
||||||
|
import kotlinx.html.div
|
||||||
import org.w3c.dom.HTMLDivElement
|
import org.w3c.dom.HTMLDivElement
|
||||||
import org.w3c.dom.HTMLElement
|
import org.w3c.dom.HTMLElement
|
||||||
import org.w3c.dom.Node
|
import org.w3c.dom.Node
|
||||||
@@ -20,6 +21,14 @@ fun Tag.include(component: Komponent) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class DummyKomponent: Komponent() {
|
||||||
|
override fun HtmlBuilder.render() {
|
||||||
|
div {
|
||||||
|
+ "dummy"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
abstract class Komponent {
|
abstract class Komponent {
|
||||||
var element: Node? = null
|
var element: Node? = null
|
||||||
val declaredStyles: MutableMap<String, CSSStyleDeclaration> = HashMap()
|
val declaredStyles: MutableMap<String, CSSStyleDeclaration> = HashMap()
|
||||||
|
|||||||
Reference in New Issue
Block a user