Fix update & replace options
This commit is contained in:
@@ -68,6 +68,7 @@ val javadocJar by tasks.registering(Jar::class) {
|
|||||||
publishing {
|
publishing {
|
||||||
repositories {
|
repositories {
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
|
/*
|
||||||
maven {
|
maven {
|
||||||
name = "releases"
|
name = "releases"
|
||||||
// change to point to your repo, e.g. http://my.org/repo
|
// change to point to your repo, e.g. http://my.org/repo
|
||||||
@@ -100,6 +101,7 @@ publishing {
|
|||||||
password = ossrhPassword
|
password = ossrhPassword
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure all publications
|
// Configure all publications
|
||||||
@@ -132,7 +134,9 @@ publishing {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
|
||||||
signing {
|
signing {
|
||||||
sign(publishing.publications)
|
sign(publishing.publications)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
@file:OptIn(ExperimentalStdlibApi::class)
|
|
||||||
|
|
||||||
package nl.astraeus.komp
|
package nl.astraeus.komp
|
||||||
|
|
||||||
import org.w3c.dom.Element
|
import org.w3c.dom.Element
|
||||||
@@ -129,6 +127,10 @@ internal fun Element.setKompEvent(name: String, event: (Event) -> Unit) {
|
|||||||
name
|
name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Komponent.updateMode.isUpdate) {
|
||||||
|
getKompEvents()[eventName] = event
|
||||||
|
}
|
||||||
|
|
||||||
this.addEventListener(eventName, event)
|
this.addEventListener(eventName, event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,14 @@ private fun ArrayList<ElementIndex>.currentElement(): Node? {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun ArrayList<ElementIndex>.currentPosition(): ElementIndex? {
|
||||||
|
return if (this.size < 2) {
|
||||||
|
null
|
||||||
|
} else {
|
||||||
|
this[this.size-2]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun ArrayList<ElementIndex>.nextElement() {
|
private fun ArrayList<ElementIndex>.nextElement() {
|
||||||
this.lastOrNull()?.let {
|
this.lastOrNull()?.let {
|
||||||
it.setAttr.clear()
|
it.setAttr.clear()
|
||||||
@@ -79,7 +87,7 @@ private fun Node.asElement() = this as? HTMLElement
|
|||||||
class HtmlBuilder(
|
class HtmlBuilder(
|
||||||
private val komponent: Komponent?,
|
private val komponent: Komponent?,
|
||||||
parent: Element,
|
parent: Element,
|
||||||
childIndex: Int = 0,
|
childIndex: Int = 0
|
||||||
) : HtmlConsumer {
|
) : HtmlConsumer {
|
||||||
private var currentPosition = arrayListOf<ElementIndex>()
|
private var currentPosition = arrayListOf<ElementIndex>()
|
||||||
private var inDebug = false
|
private var inDebug = false
|
||||||
@@ -135,6 +143,7 @@ class HtmlBuilder(
|
|||||||
|
|
||||||
override fun onTagStart(tag: Tag) {
|
override fun onTagStart(tag: Tag) {
|
||||||
logReplace { "onTagStart, [${tag.tagName}, ${tag.namespace}], currentPosition: $currentPosition" }
|
logReplace { "onTagStart, [${tag.tagName}, ${tag.namespace}], currentPosition: $currentPosition" }
|
||||||
|
|
||||||
currentNode = currentPosition.currentElement()
|
currentNode = currentPosition.currentElement()
|
||||||
|
|
||||||
if (currentNode == null) {
|
if (currentNode == null) {
|
||||||
@@ -169,8 +178,6 @@ class HtmlBuilder(
|
|||||||
currentPosition.replace(currentNode!!)
|
currentPosition.replace(currentNode!!)
|
||||||
}
|
}
|
||||||
|
|
||||||
check(currentNode == currentPosition.currentElement())
|
|
||||||
|
|
||||||
currentElement = currentNode as? Element ?: currentElement
|
currentElement = currentNode as? Element ?: currentElement
|
||||||
|
|
||||||
if (currentNode is Element) {
|
if (currentNode is Element) {
|
||||||
@@ -179,17 +186,17 @@ class HtmlBuilder(
|
|||||||
root = currentNode as Element
|
root = currentNode as Element
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Komponent.updateMode.isReplace) {
|
if (Komponent.updateMode.isUpdate) {
|
||||||
currentElement?.clearKompEvents()
|
currentElement?.clearKompEvents()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentPosition.lastOrNull()?.setAttr?.clear()
|
||||||
for (entry in tag.attributesEntries) {
|
for (entry in tag.attributesEntries) {
|
||||||
currentElement!!.setKompAttribute(entry.key, entry.value)
|
currentElement!!.setKompAttribute(entry.key, entry.value)
|
||||||
console.log("onTagStart - set attribute", entry.key)
|
|
||||||
currentPosition.lastOrNull()?.setAttr?.add(entry.key)
|
currentPosition.lastOrNull()?.setAttr?.add(entry.key)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tag.namespace != null) {
|
if (tag.namespace != null && Komponent.updateMode.isReplace) {
|
||||||
//logReplace"onTagStart, same node type")
|
//logReplace"onTagStart, same node type")
|
||||||
|
|
||||||
(currentNode as? Element)?.innerHTML = ""
|
(currentNode as? Element)?.innerHTML = ""
|
||||||
@@ -219,10 +226,9 @@ class HtmlBuilder(
|
|||||||
|
|
||||||
currentElement?.setKompAttribute(attribute, value)
|
currentElement?.setKompAttribute(attribute, value)
|
||||||
if (value == null || value.isEmpty()) {
|
if (value == null || value.isEmpty()) {
|
||||||
currentPosition.lastOrNull()?.setAttr?.remove(attribute)
|
currentPosition.currentPosition()?.setAttr?.remove(attribute)
|
||||||
} else {
|
} else {
|
||||||
console.log("onTagAttributeChange - set attribute", attribute)
|
currentPosition.currentPosition()?.setAttr?.add(attribute)
|
||||||
currentPosition.lastOrNull()?.setAttr?.add(attribute)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,15 +257,8 @@ class HtmlBuilder(
|
|||||||
checkTag(tag)
|
checkTag(tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
currentPosition.pop()
|
|
||||||
|
|
||||||
currentNode = currentPosition.currentElement()
|
|
||||||
currentElement = currentNode as? Element ?: currentElement
|
|
||||||
|
|
||||||
if (currentElement != null) {
|
if (currentElement != null) {
|
||||||
val setAttrs: Set<String> = currentPosition.lastOrNull()?.setAttr ?: setOf()
|
val setAttrs: Set<String> = currentPosition.currentPosition()?.setAttr ?: setOf()
|
||||||
|
|
||||||
console.log("onTagEnd - set attr:", setAttrs.joinToString(","))
|
|
||||||
|
|
||||||
// remove attributes that where not set
|
// remove attributes that where not set
|
||||||
val element = currentElement
|
val element = currentElement
|
||||||
@@ -273,7 +272,6 @@ class HtmlBuilder(
|
|||||||
!setAttrs.contains(attr) &&
|
!setAttrs.contains(attr) &&
|
||||||
attr != "style"
|
attr != "style"
|
||||||
) {
|
) {
|
||||||
console.log("remove attr", attr)
|
|
||||||
if (element is HTMLInputElement) {
|
if (element is HTMLInputElement) {
|
||||||
when (attr) {
|
when (attr) {
|
||||||
"checked" -> {
|
"checked" -> {
|
||||||
@@ -295,6 +293,11 @@ class HtmlBuilder(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentPosition.pop()
|
||||||
|
|
||||||
|
currentNode = currentPosition.currentElement()
|
||||||
|
currentElement = currentNode as? Element ?: currentElement
|
||||||
|
|
||||||
currentPosition.nextElement()
|
currentPosition.nextElement()
|
||||||
|
|
||||||
currentElement = currentElement?.parentElement as? HTMLElement
|
currentElement = currentElement?.parentElement as? HTMLElement
|
||||||
@@ -398,7 +401,6 @@ class HtmlBuilder(
|
|||||||
exceptionThrown = true
|
exceptionThrown = true
|
||||||
|
|
||||||
if (exception !is KomponentException) {
|
if (exception !is KomponentException) {
|
||||||
console.log("onTagError", tag, exception)
|
|
||||||
val position = mutableListOf<Element>()
|
val position = mutableListOf<Element>()
|
||||||
var ce = currentElement
|
var ce = currentElement
|
||||||
while (ce != null) {
|
while (ce != null) {
|
||||||
@@ -441,7 +443,7 @@ class HtmlBuilder(
|
|||||||
companion object {
|
companion object {
|
||||||
fun create(content: HtmlBuilder.() -> Unit): Element {
|
fun create(content: HtmlBuilder.() -> Unit): Element {
|
||||||
val container = document.createElement("div") as HTMLElement
|
val container = document.createElement("div") as HTMLElement
|
||||||
val consumer = HtmlBuilder(null, container, 0)
|
val consumer = HtmlBuilder(null, container)
|
||||||
content.invoke(consumer)
|
content.invoke(consumer)
|
||||||
return consumer.root ?: error("No root element found after render!")
|
return consumer.root ?: error("No root element found after render!")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -146,7 +146,6 @@ abstract class Komponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
val builder = HtmlBuilder(this, parent, childIndex)
|
val builder = HtmlBuilder(this, parent, childIndex)
|
||||||
builder.root = null
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
currentKomponent = this
|
currentKomponent = this
|
||||||
@@ -186,7 +185,7 @@ abstract class Komponent {
|
|||||||
var logRenderEvent = false
|
var logRenderEvent = false
|
||||||
var logReplaceEvent = false
|
var logReplaceEvent = false
|
||||||
var enableAssertions = false
|
var enableAssertions = false
|
||||||
var updateMode = UpdateMode.REPLACE
|
var updateMode = UpdateMode.UPDATE
|
||||||
var unsafeMode = UnsafeMode.UNSAFE_DISABLED
|
var unsafeMode = UnsafeMode.UNSAFE_DISABLED
|
||||||
|
|
||||||
fun create(parent: HTMLElement, component: Komponent) {
|
fun create(parent: HTMLElement, component: Komponent) {
|
||||||
|
|||||||
Reference in New Issue
Block a user