Compare commits
14 Commits
6b5a9bbe57
...
1813e5b33f
| Author | SHA1 | Date | |
|---|---|---|---|
| 1813e5b33f | |||
| dbd47015b7 | |||
| 3c41535870 | |||
| 1b2dd7f43a | |||
| ad42f33142 | |||
| 06a1e9956e | |||
| 5cc4826e65 | |||
| a1f1f3bb38 | |||
| 4954382f96 | |||
| 419886bed0 | |||
| ccc07a3545 | |||
| 981bceacfb | |||
| 1b93c54cf4 | |||
| 5f7fde44c6 |
@@ -1,19 +1,22 @@
|
||||
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
|
||||
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
|
||||
|
||||
plugins {
|
||||
kotlin("multiplatform") version "1.7.20"
|
||||
`maven-publish`
|
||||
signing
|
||||
kotlin("multiplatform") version "2.0.0"
|
||||
id("maven-publish")
|
||||
id("signing")
|
||||
id("org.jetbrains.dokka") version "1.5.31"
|
||||
}
|
||||
|
||||
group = "nl.astraeus"
|
||||
version = "1.0.8-SNAPSHOT"
|
||||
version = "1.2.3-SNAPSHOT"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
kotlin {
|
||||
js(BOTH) {
|
||||
js {
|
||||
browser {
|
||||
testTask {
|
||||
useKarma {
|
||||
@@ -22,20 +25,37 @@ 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 {
|
||||
val commonMain by getting {
|
||||
dependencies {
|
||||
implementation(kotlin("stdlib-common"))
|
||||
|
||||
api("org.jetbrains.kotlinx:kotlinx-html-js:0.7.3")
|
||||
}
|
||||
}
|
||||
val jsMain by getting {
|
||||
dependencies {
|
||||
implementation(kotlin("stdlib-js"))
|
||||
api("org.jetbrains.kotlinx:kotlinx-html:0.11.0")
|
||||
}
|
||||
}
|
||||
val jsMain by getting
|
||||
val jsTest by getting {
|
||||
dependencies {
|
||||
implementation(kotlin("test-js"))
|
||||
@@ -71,25 +91,25 @@ publishing {
|
||||
maven {
|
||||
name = "releases"
|
||||
// change to point to your repo, e.g. http://my.org/repo
|
||||
setUrl("https://nexus.astraeus.nl/nexus/content/repositories/releases")
|
||||
setUrl("https://reposilite.astraeus.nl/releases")
|
||||
credentials {
|
||||
val nexusUsername: String? by project
|
||||
val nexusPassword: String? by project
|
||||
val reposiliteUsername: String? by project
|
||||
val reposilitePassword: String? by project
|
||||
|
||||
username = nexusUsername
|
||||
password = nexusPassword
|
||||
username = reposiliteUsername
|
||||
password = reposilitePassword
|
||||
}
|
||||
}
|
||||
maven {
|
||||
name = "snapshots"
|
||||
// change to point to your repo, e.g. http://my.org/repo
|
||||
setUrl("https://nexus.astraeus.nl/nexus/content/repositories/snapshots")
|
||||
setUrl("https://reposilite.astraeus.nl/snapshots")
|
||||
credentials {
|
||||
val nexusUsername: String? by project
|
||||
val nexusPassword: String? by project
|
||||
val reposiliteUsername: String? by project
|
||||
val reposilitePassword: String? by project
|
||||
|
||||
username = nexusUsername
|
||||
password = nexusPassword
|
||||
username = reposiliteUsername
|
||||
password = reposilitePassword
|
||||
}
|
||||
}
|
||||
maven {
|
||||
@@ -100,6 +120,18 @@ publishing {
|
||||
password = ossrhPassword
|
||||
}
|
||||
}
|
||||
maven {
|
||||
name = "gitea"
|
||||
setUrl("https://gitea.astraeus.nl/api/packages/rnentjes/maven")
|
||||
|
||||
credentials() {
|
||||
val giteaUsername: kotlin.String? by project
|
||||
val giteaPassword: kotlin.String? by project
|
||||
|
||||
username = giteaUsername
|
||||
password = giteaPassword
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Configure all publications
|
||||
@@ -136,3 +168,23 @@ publishing {
|
||||
signing {
|
||||
sign(publishing.publications)
|
||||
}
|
||||
|
||||
tasks.named<Task>("signJsPublication") {
|
||||
dependsOn(tasks.named<Task>("publishKotlinMultiplatformPublicationToMavenLocal"))
|
||||
}
|
||||
|
||||
tasks.named<Task>("publishJsPublicationToReleasesRepository") {
|
||||
dependsOn(tasks.named<Task>("signKotlinMultiplatformPublication"))
|
||||
}
|
||||
|
||||
tasks.named<Task>("publishKotlinMultiplatformPublicationToMavenLocalRepository") {
|
||||
dependsOn(tasks.named<Task>("signJsPublication"))
|
||||
}
|
||||
|
||||
tasks.named<Task>("publishKotlinMultiplatformPublicationToReleasesRepository") {
|
||||
dependsOn(tasks.named<Task>("signJsPublication"))
|
||||
}
|
||||
|
||||
tasks.named<Task>("publishKotlinMultiplatformPublicationToSonatypeRepository") {
|
||||
dependsOn(tasks.named<Task>("signJsPublication"))
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ fun greet() = "world"
|
||||
|
||||
Replace the code in the file with the following for a simple click app:
|
||||
|
||||
```koltin
|
||||
```kotlin
|
||||
import kotlinx.browser.document
|
||||
import kotlinx.html.button
|
||||
import kotlinx.html.div
|
||||
@@ -144,4 +144,4 @@ the data changes, that would look like this:
|
||||
|
||||
In that case you can remove the requestUpdate call from the onClickFunction.
|
||||
|
||||
You can find a working repository of this example here: [example]()
|
||||
You can find a working repository of this example here: [kotlin-komponent-start](https://github.com/rnentjes/kotlin-komponent-start)
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
* [Home](home.md)
|
||||
* [Getting started](getting-started.md)
|
||||
* [How it works](how-it-works.md)
|
||||
|
||||
|
||||
@@ -15,8 +15,8 @@ This way there will not be double updates of the same komponent.
|
||||
The render call will be invoked and every html builder function (div, span etc.) will call the
|
||||
different HtmlBuilder functions like onTagStart, onTagAttributeChange etc.
|
||||
|
||||
In these functions the HtmlBuilder will compare the dom against the call being made and it will update the DOM
|
||||
if needed.
|
||||
In these functions the HtmlBuilder will compare the dom against the call being made, and it will update the DOM
|
||||
as needed.
|
||||
|
||||
|
||||
|
||||
|
||||
|
Before Width: | Height: | Size: 184 KiB After Width: | Height: | Size: 184 KiB |
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,5 +1,5 @@
|
||||
#Wed Mar 04 13:29:12 CET 2020
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-all.zip
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStorePath=wrapper/dists
|
||||
|
||||
@@ -9,3 +9,5 @@ See the komp-todo repository for a basic example here: [komp-todo](https://githu
|
||||
For a more complete example take a look at the simple-password-manager repository: [simple-password-manager](https://github.com/rnentjes/simple-password-manager)
|
||||
|
||||
Available on maven central: "nl.astraeus:kotlin-komponent-js:1.0.0"
|
||||
|
||||
Some getting started documentation can be found [here](docs/getting-started.md)
|
||||
@@ -1,8 +1,9 @@
|
||||
package nl.astraeus.komp
|
||||
|
||||
import org.w3c.dom.events.Event
|
||||
import org.w3c.dom.Element
|
||||
import org.w3c.dom.HTMLInputElement
|
||||
import org.w3c.dom.events.Event
|
||||
import org.w3c.dom.events.EventListener
|
||||
import org.w3c.dom.get
|
||||
|
||||
private fun Int.asSpaces(): String {
|
||||
|
||||
64
src/jsMain/kotlin/nl/astraeus/komp/ElementIndex.kt
Normal file
64
src/jsMain/kotlin/nl/astraeus/komp/ElementIndex.kt
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,6 @@ import org.w3c.dom.HTMLInputElement
|
||||
import org.w3c.dom.HTMLSpanElement
|
||||
import org.w3c.dom.Node
|
||||
import org.w3c.dom.asList
|
||||
import org.w3c.dom.events.Event
|
||||
import org.w3c.dom.get
|
||||
|
||||
private var currentElement: Element? = null
|
||||
@@ -27,62 +26,6 @@ interface HtmlConsumer : TagConsumer<Element> {
|
||||
fun FlowOrMetaDataOrPhrasingContent.currentElement(): Element =
|
||||
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
|
||||
|
||||
class HtmlBuilder(
|
||||
@@ -94,6 +37,7 @@ class HtmlBuilder(
|
||||
private var inDebug = false
|
||||
private var exceptionThrown = false
|
||||
private var currentNode: Node? = null
|
||||
private var firstTag: Boolean = true
|
||||
var root: Element? = null
|
||||
|
||||
init {
|
||||
@@ -112,6 +56,13 @@ class HtmlBuilder(
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// current element should become parent
|
||||
/*
|
||||
val ce = komponent.element
|
||||
if (ce != null) {
|
||||
append(ce as Element)
|
||||
}
|
||||
*/
|
||||
komponent.create(
|
||||
currentPosition.last().parent as Element,
|
||||
currentPosition.last().childIndex
|
||||
@@ -146,7 +97,7 @@ class HtmlBuilder(
|
||||
|
||||
override fun onTagStart(tag: Tag) {
|
||||
logReplace {
|
||||
"onTagStart, [${tag.tagName}, ${tag.namespace}], currentPosition: $currentPosition"
|
||||
"onTagStart, [${tag.tagName}, ${tag.namespace ?: ""}], currentPosition: $currentPosition"
|
||||
}
|
||||
|
||||
currentNode = currentPosition.currentElement()
|
||||
@@ -159,7 +110,7 @@ class HtmlBuilder(
|
||||
document.createElement(tag.tagName)
|
||||
}
|
||||
|
||||
//logReplace"onTagStart, currentElement1.1: $currentNode")
|
||||
logReplace { "onTagStart, currentElement1.1: $currentNode" }
|
||||
currentPosition.currentParent().appendChild(currentNode!!)
|
||||
} else if (
|
||||
!currentNode?.asElement()?.tagName.equals(tag.tagName, true) ||
|
||||
@@ -187,17 +138,18 @@ class HtmlBuilder(
|
||||
currentElement = currentNode as? Element ?: currentElement
|
||||
|
||||
if (currentNode is Element) {
|
||||
if (root == null) {
|
||||
//logReplace"Setting root: $currentNode")
|
||||
if (firstTag) {
|
||||
logReplace { "Setting root: $currentNode" }
|
||||
root = currentNode as Element
|
||||
firstTag = false
|
||||
}
|
||||
|
||||
currentElement?.clearKompEvents()
|
||||
|
||||
// if currentElement = checkbox make sure it's cleared
|
||||
(currentElement as? HTMLInputElement)?.checked = false
|
||||
|
||||
currentPosition.lastOrNull()?.setAttr?.clear()
|
||||
|
||||
// if currentElement = checkbox make sure it's cleared
|
||||
for (entry in tag.attributesEntries) {
|
||||
currentElement!!.setKompAttribute(entry.key, entry.value)
|
||||
currentPosition.lastOrNull()?.setAttr?.add(entry.key)
|
||||
@@ -207,14 +159,14 @@ class HtmlBuilder(
|
||||
currentPosition.push(currentNode!!)
|
||||
}
|
||||
|
||||
private fun checkTag(tag: Tag) {
|
||||
private fun checkTag(source: String, tag: Tag) {
|
||||
check(currentElement != null) {
|
||||
js("debugger")
|
||||
"No current tag"
|
||||
js("debugger;")
|
||||
"No current tag ($source)"
|
||||
}
|
||||
check(currentElement?.tagName.equals(tag.tagName, ignoreCase = true)) {
|
||||
js("debugger")
|
||||
"Wrong current tag"
|
||||
js("debugger;")
|
||||
"Wrong current tag ($source), got: ${tag.tagName} expected ${currentElement?.tagName}"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,7 +178,7 @@ class HtmlBuilder(
|
||||
logReplace { "onTagAttributeChange, ${tag.tagName} [$attribute, $value]" }
|
||||
|
||||
if (Komponent.enableAssertions) {
|
||||
checkTag(tag)
|
||||
checkTag("onTagAttributeChange", tag)
|
||||
}
|
||||
|
||||
currentElement?.setKompAttribute(attribute, value)
|
||||
@@ -240,18 +192,22 @@ class HtmlBuilder(
|
||||
override fun onTagEvent(
|
||||
tag: Tag,
|
||||
event: String,
|
||||
value: (Event) -> Unit
|
||||
value: (kotlinx.html.org.w3c.dom.events.Event) -> Unit
|
||||
) {
|
||||
logReplace { "onTagEvent, ${tag.tagName} [$event, $value]" }
|
||||
|
||||
if (Komponent.enableAssertions) {
|
||||
checkTag(tag)
|
||||
checkTag("onTagEvent", tag)
|
||||
}
|
||||
|
||||
currentElement?.setKompEvent(event.toLowerCase(), value)
|
||||
currentElement?.setKompEvent(event.lowercase(), value.asDynamic())
|
||||
}
|
||||
|
||||
override fun onTagEnd(tag: Tag) {
|
||||
logReplace {
|
||||
"onTagEnd, [${tag.tagName}, ${tag.namespace}], currentPosition: $currentPosition"
|
||||
}
|
||||
|
||||
if (exceptionThrown) {
|
||||
return
|
||||
}
|
||||
@@ -263,7 +219,7 @@ class HtmlBuilder(
|
||||
}
|
||||
|
||||
if (Komponent.enableAssertions) {
|
||||
checkTag(tag)
|
||||
checkTag("onTagEnd", tag)
|
||||
}
|
||||
|
||||
if (currentElement != null) {
|
||||
@@ -395,7 +351,7 @@ class HtmlBuilder(
|
||||
currentPosition.nextElement()
|
||||
}
|
||||
|
||||
override fun onTagError(tag: Tag, exception: Throwable) {
|
||||
fun onTagError(tag: Tag, exception: Throwable) {
|
||||
exceptionThrown = true
|
||||
|
||||
if (exception !is KomponentException) {
|
||||
|
||||
@@ -111,8 +111,25 @@ abstract class Komponent {
|
||||
*
|
||||
* HTMLBuilder.render() is called 1st time the component is rendered, after that this
|
||||
* 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
|
||||
*/
|
||||
open fun renderUpdate() {
|
||||
refresh()
|
||||
}
|
||||
|
||||
@@ -228,7 +245,7 @@ abstract class Komponent {
|
||||
|
||||
if (next.memoizeChanged()) {
|
||||
next.onBeforeUpdate()
|
||||
next.update()
|
||||
next.renderUpdate()
|
||||
next.updateMemoizeHash()
|
||||
next.onAfterUpdate()
|
||||
} else if (logRenderEvent) {
|
||||
|
||||
@@ -31,7 +31,7 @@ class MutableCollectionStateDelegate<T>(
|
||||
}
|
||||
|
||||
// todo: return iterator wrapper to update at changes?
|
||||
// override fun iterator(): MutableIterator<T> = collection.iterator()
|
||||
//override fun iterator(): MutableIterator<T> = collection.iterator()
|
||||
|
||||
override fun remove(element: T): Boolean {
|
||||
komponent.requestUpdate()
|
||||
|
||||
@@ -14,8 +14,8 @@ interface Delegate<T> {
|
||||
property: KProperty<*>,
|
||||
value: T
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
open class StateDelegate<T>(
|
||||
val komponent: Komponent,
|
||||
@@ -51,6 +51,6 @@ open class StateDelegate<T>(
|
||||
inline fun <reified T> Komponent.state(
|
||||
initialValue: T
|
||||
): Delegate<T> = StateDelegate(
|
||||
this,
|
||||
initialValue
|
||||
)
|
||||
this,
|
||||
initialValue
|
||||
)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package nl.astraeus.komp
|
||||
|
||||
import kotlinx.browser.document
|
||||
import kotlinx.html.DIV
|
||||
import kotlinx.html.InputType
|
||||
import kotlinx.html.classes
|
||||
import kotlinx.html.div
|
||||
@@ -125,7 +126,7 @@ class IncludeKomponent(
|
||||
}
|
||||
|
||||
class ReplaceKomponent : Komponent() {
|
||||
val includeKomponent = IncludeKomponent()
|
||||
val includeKomponent = IncludeKomponent("Other text")
|
||||
var includeSpan = true
|
||||
|
||||
override fun generateMemoizeHash(): Int = includeSpan.hashCode() * 7 + includeKomponent.generateMemoizeHash()
|
||||
@@ -136,20 +137,8 @@ class ReplaceKomponent : Komponent() {
|
||||
|
||||
div {
|
||||
if (includeSpan) {
|
||||
span {
|
||||
i("fas fa-eye") {
|
||||
+"span1"
|
||||
}
|
||||
}
|
||||
span {
|
||||
i("fas fa-eye") {
|
||||
+"span2"
|
||||
}
|
||||
}
|
||||
span {
|
||||
i("fas fa-eye") {
|
||||
+"span3"
|
||||
}
|
||||
for (index in 0 ..< 3) {
|
||||
extracted(index)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,6 +146,14 @@ class ReplaceKomponent : Komponent() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun HtmlBuilder.extracted(index: Int) {
|
||||
span {
|
||||
i("fas fa-eye") {
|
||||
+ ("span" + (index+1))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TestUpdate {
|
||||
|
||||
Reference in New Issue
Block a user