Upgrade to Kotlin 1.9.22, , kotlinx-html 0.11.0, version 1.2.1

This commit is contained in:
2024-01-24 14:53:44 +01:00
parent 5cc4826e65
commit 06a1e9956e
6 changed files with 164 additions and 98 deletions

View File

@@ -1,12 +1,15 @@
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
plugins { plugins {
kotlin("multiplatform") version "1.9.0" kotlin("multiplatform") version "1.9.22"
`maven-publish` id("maven-publish")
signing id("signing")
id("org.jetbrains.dokka") version "1.5.31" id("org.jetbrains.dokka") version "1.5.31"
} }
group = "nl.astraeus" group = "nl.astraeus"
version = "1.1.1" version = "1.2.1"
repositories { repositories {
mavenCentral() mavenCentral()
@@ -22,17 +25,35 @@ kotlin {
} }
} }
} }
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
//moduleName = project.name
browser()
mavenPublication {
groupId = group as String
pom { name = "${project.name}-wasm-js" }
}
}
@OptIn(ExperimentalKotlinGradlePluginApi::class)
applyDefaultHierarchyTemplate {
common {
group("jsCommon") {
withJs()
// TODO: switch to `withWasmJs()` after upgrade to Kotlin 2.0
withWasm()
}
}
}
sourceSets { sourceSets {
val commonMain by getting { val commonMain by getting {
dependencies { dependencies {
api("org.jetbrains.kotlinx:kotlinx-html:0.9.1") api("org.jetbrains.kotlinx:kotlinx-html:0.11.0")
}
}
val jsMain by getting {
dependencies {
} }
} }
val jsMain by getting
val jsTest by getting { val jsTest by getting {
dependencies { dependencies {
implementation(kotlin("test-js")) implementation(kotlin("test-js"))
@@ -150,3 +171,14 @@ tasks.named<Task>("publishKotlinMultiplatformPublicationToReleasesRepository") {
dependsOn(tasks.named<Task>("signJsPublication")) dependsOn(tasks.named<Task>("signJsPublication"))
} }
tasks.named<Task>("publishKotlinMultiplatformPublicationToMavenLocal") {
dependsOn(tasks.named<Task>("signWasmJsPublication"))
}
tasks.named<Task>("publishWasmJsPublicationToMavenLocalRepository") {
dependsOn(tasks.named<Task>("signKotlinMultiplatformPublication"))
}
tasks.named<Task>("publishWasmJsPublicationToMavenLocalRepository") {
dependsOn(tasks.named<Task>("signJsPublication"))
}

View File

@@ -1,5 +1,5 @@
#Wed Mar 04 13:29:12 CET 2020 #Wed Mar 04 13:29:12 CET 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-all.zip
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

View File

@@ -0,0 +1,64 @@
package nl.astraeus.komp
import org.w3c.dom.Node
import org.w3c.dom.get
data class ElementIndex(
val parent: Node,
var childIndex: Int,
var setAttr: MutableSet<String> = mutableSetOf()
) {
override fun toString(): String {
return "${parent.nodeName}[$childIndex]"
}
}
fun ArrayList<ElementIndex>.currentParent(): Node {
this.lastOrNull()?.let {
return it.parent
}
throw IllegalStateException("currentParent should never be null!")
}
fun ArrayList<ElementIndex>.currentElement(): Node? {
this.lastOrNull()?.let {
return it.parent.childNodes[it.childIndex]
}
return null
}
fun ArrayList<ElementIndex>.currentPosition(): ElementIndex? {
return if (this.size < 2) {
null
} else {
this[this.size - 2]
}
}
fun ArrayList<ElementIndex>.nextElement() {
this.lastOrNull()?.let {
it.setAttr.clear()
it.childIndex++
}
}
fun ArrayList<ElementIndex>.pop() {
this.removeLast()
}
fun ArrayList<ElementIndex>.push(element: Node) {
this.add(ElementIndex(element, 0))
}
fun ArrayList<ElementIndex>.replace(new: Node) {
if (this.currentElement() != null) {
this.currentElement()?.parentElement?.replaceChild(
new,
this.currentElement()!!
)
} else {
this.last().parent.appendChild(new)
}
}

View File

@@ -7,7 +7,6 @@ import kotlinx.html.FlowOrMetaDataOrPhrasingContent
import kotlinx.html.Tag import kotlinx.html.Tag
import kotlinx.html.TagConsumer import kotlinx.html.TagConsumer
import kotlinx.html.Unsafe import kotlinx.html.Unsafe
import org.w3c.dom.events.Event
import org.w3c.dom.Element import org.w3c.dom.Element
import org.w3c.dom.HTMLElement import org.w3c.dom.HTMLElement
import org.w3c.dom.HTMLInputElement import org.w3c.dom.HTMLInputElement
@@ -27,62 +26,6 @@ interface HtmlConsumer : TagConsumer<Element> {
fun FlowOrMetaDataOrPhrasingContent.currentElement(): Element = fun FlowOrMetaDataOrPhrasingContent.currentElement(): Element =
currentElement ?: error("No current element defined!") currentElement ?: error("No current element defined!")
private data class ElementIndex(
val parent: Node,
var childIndex: Int,
var setAttr: MutableSet<String> = mutableSetOf()
)
private fun ArrayList<ElementIndex>.currentParent(): Node {
this.lastOrNull()?.let {
return it.parent
}
throw IllegalStateException("currentParent should never be null!")
}
private fun ArrayList<ElementIndex>.currentElement(): Node? {
this.lastOrNull()?.let {
return it.parent.childNodes[it.childIndex]
}
return null
}
private fun ArrayList<ElementIndex>.currentPosition(): ElementIndex? {
return if (this.size < 2) {
null
} else {
this[this.size - 2]
}
}
private fun ArrayList<ElementIndex>.nextElement() {
this.lastOrNull()?.let {
it.setAttr.clear()
it.childIndex++
}
}
private fun ArrayList<ElementIndex>.pop() {
this.removeLast()
}
private fun ArrayList<ElementIndex>.push(element: Node) {
this.add(ElementIndex(element, 0))
}
private fun ArrayList<ElementIndex>.replace(new: Node) {
if (this.currentElement() != null) {
this.currentElement()?.parentElement?.replaceChild(
new,
this.currentElement()!!
)
} else {
this.last().parent.appendChild(new)
}
}
private fun Node.asElement() = this as? HTMLElement private fun Node.asElement() = this as? HTMLElement
class HtmlBuilder( class HtmlBuilder(
@@ -94,6 +37,7 @@ class HtmlBuilder(
private var inDebug = false private var inDebug = false
private var exceptionThrown = false private var exceptionThrown = false
private var currentNode: Node? = null private var currentNode: Node? = null
private var firstTag: Boolean = true
var root: Element? = null var root: Element? = null
init { init {
@@ -112,6 +56,13 @@ class HtmlBuilder(
) )
} }
} else { } else {
// current element should become parent
/*
val ce = komponent.element
if (ce != null) {
append(ce as Element)
}
*/
komponent.create( komponent.create(
currentPosition.last().parent as Element, currentPosition.last().parent as Element,
currentPosition.last().childIndex currentPosition.last().childIndex
@@ -146,7 +97,7 @@ class HtmlBuilder(
override fun onTagStart(tag: Tag) { override fun onTagStart(tag: Tag) {
logReplace { logReplace {
"onTagStart, [${tag.tagName}, ${tag.namespace}], currentPosition: $currentPosition" "onTagStart, [${tag.tagName}, ${tag.namespace ?: ""}], currentPosition: $currentPosition"
} }
currentNode = currentPosition.currentElement() currentNode = currentPosition.currentElement()
@@ -159,7 +110,7 @@ class HtmlBuilder(
document.createElement(tag.tagName) document.createElement(tag.tagName)
} }
//logReplace"onTagStart, currentElement1.1: $currentNode") logReplace { "onTagStart, currentElement1.1: $currentNode" }
currentPosition.currentParent().appendChild(currentNode!!) currentPosition.currentParent().appendChild(currentNode!!)
} else if ( } else if (
!currentNode?.asElement()?.tagName.equals(tag.tagName, true) || !currentNode?.asElement()?.tagName.equals(tag.tagName, true) ||
@@ -187,17 +138,18 @@ class HtmlBuilder(
currentElement = currentNode as? Element ?: currentElement currentElement = currentNode as? Element ?: currentElement
if (currentNode is Element) { if (currentNode is Element) {
if (root == null) { if (firstTag) {
//logReplace"Setting root: $currentNode") logReplace { "Setting root: $currentNode" }
root = currentNode as Element root = currentNode as Element
firstTag = false
} }
currentElement?.clearKompEvents() currentElement?.clearKompEvents()
// if currentElement = checkbox make sure it's cleared
(currentElement as? HTMLInputElement)?.checked = false (currentElement as? HTMLInputElement)?.checked = false
currentPosition.lastOrNull()?.setAttr?.clear() currentPosition.lastOrNull()?.setAttr?.clear()
// if currentElement = checkbox make sure it's cleared
for (entry in tag.attributesEntries) { for (entry in tag.attributesEntries) {
currentElement!!.setKompAttribute(entry.key, entry.value) currentElement!!.setKompAttribute(entry.key, entry.value)
currentPosition.lastOrNull()?.setAttr?.add(entry.key) currentPosition.lastOrNull()?.setAttr?.add(entry.key)
@@ -207,14 +159,14 @@ class HtmlBuilder(
currentPosition.push(currentNode!!) currentPosition.push(currentNode!!)
} }
private fun checkTag(tag: Tag) { private fun checkTag(source: String, tag: Tag) {
check(currentElement != null) { check(currentElement != null) {
js("debugger") js("debugger;")
"No current tag" "No current tag ($source)"
} }
check(currentElement?.tagName.equals(tag.tagName, ignoreCase = true)) { check(currentElement?.tagName.equals(tag.tagName, ignoreCase = true)) {
js("debugger") js("debugger;")
"Wrong current tag" "Wrong current tag ($source), got: ${tag.tagName} expected ${currentElement?.tagName}"
} }
} }
@@ -226,7 +178,7 @@ class HtmlBuilder(
logReplace { "onTagAttributeChange, ${tag.tagName} [$attribute, $value]" } logReplace { "onTagAttributeChange, ${tag.tagName} [$attribute, $value]" }
if (Komponent.enableAssertions) { if (Komponent.enableAssertions) {
checkTag(tag) checkTag("onTagAttributeChange", tag)
} }
currentElement?.setKompAttribute(attribute, value) currentElement?.setKompAttribute(attribute, value)
@@ -245,13 +197,17 @@ class HtmlBuilder(
logReplace { "onTagEvent, ${tag.tagName} [$event, $value]" } logReplace { "onTagEvent, ${tag.tagName} [$event, $value]" }
if (Komponent.enableAssertions) { if (Komponent.enableAssertions) {
checkTag(tag) checkTag("onTagEvent", tag)
} }
currentElement?.setKompEvent(event.lowercase(), value.asDynamic()) currentElement?.setKompEvent(event.lowercase(), value.asDynamic())
} }
override fun onTagEnd(tag: Tag) { override fun onTagEnd(tag: Tag) {
logReplace {
"onTagEnd, [${tag.tagName}, ${tag.namespace}], currentPosition: $currentPosition"
}
if (exceptionThrown) { if (exceptionThrown) {
return return
} }
@@ -263,7 +219,7 @@ class HtmlBuilder(
} }
if (Komponent.enableAssertions) { if (Komponent.enableAssertions) {
checkTag(tag) checkTag("onTagEnd", tag)
} }
if (currentElement != null) { if (currentElement != null) {

View File

@@ -111,8 +111,25 @@ abstract class Komponent {
* *
* HTMLBuilder.render() is called 1st time the component is rendered, after that this * HTMLBuilder.render() is called 1st time the component is rendered, after that this
* method will be called * method will be called
*
* @deprecated
*/ */
open fun update() { @Deprecated(
"Deprecated to avoid confusing with requestUpdate, use renderUpdate instead",
ReplaceWith("renderUpdate"),
level = DeprecationLevel.WARNING
)
protected fun update() {
refresh()
}
/**
* This function can be overwritten if you know how to update the Komponent yourself
*
* HTMLBuilder.render() is called 1st time the component is rendered, after that this
* method will be called
*/
protected fun renderUpdate() {
refresh() refresh()
} }
@@ -228,7 +245,7 @@ abstract class Komponent {
if (next.memoizeChanged()) { if (next.memoizeChanged()) {
next.onBeforeUpdate() next.onBeforeUpdate()
next.update() next.renderUpdate()
next.updateMemoizeHash() next.updateMemoizeHash()
next.onAfterUpdate() next.onAfterUpdate()
} else if (logRenderEvent) { } else if (logRenderEvent) {

View File

@@ -1,6 +1,7 @@
package nl.astraeus.komp package nl.astraeus.komp
import kotlinx.browser.document import kotlinx.browser.document
import kotlinx.html.DIV
import kotlinx.html.InputType import kotlinx.html.InputType
import kotlinx.html.classes import kotlinx.html.classes
import kotlinx.html.div import kotlinx.html.div
@@ -125,7 +126,7 @@ class IncludeKomponent(
} }
class ReplaceKomponent : Komponent() { class ReplaceKomponent : Komponent() {
val includeKomponent = IncludeKomponent() val includeKomponent = IncludeKomponent("Other text")
var includeSpan = true var includeSpan = true
override fun generateMemoizeHash(): Int = includeSpan.hashCode() * 7 + includeKomponent.generateMemoizeHash() override fun generateMemoizeHash(): Int = includeSpan.hashCode() * 7 + includeKomponent.generateMemoizeHash()
@@ -136,20 +137,8 @@ class ReplaceKomponent : Komponent() {
div { div {
if (includeSpan) { if (includeSpan) {
span { for (index in 0 ..< 3) {
i("fas fa-eye") { extracted(index)
+"span1"
}
}
span {
i("fas fa-eye") {
+"span2"
}
}
span {
i("fas fa-eye") {
+"span3"
}
} }
} }
@@ -157,6 +146,14 @@ class ReplaceKomponent : Komponent() {
} }
} }
} }
private fun HtmlBuilder.extracted(index: Int) {
span {
i("fas fa-eye") {
+ ("span" + (index+1))
}
}
}
} }
class TestUpdate { class TestUpdate {