在Kotlin-js上样式化HTML元素

Styling HTML elements at Kotlin-js

使用Kotlinx-html时如何设置HTML元素的样式,我的应用程序正常运行,然后尝试使用AZA-Kotlin添加样式,但是一旦导入azadev.kotlin,它就会给我带来错误生成
我的完整代码是下面的:

Main.kt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import azadev.kotlin.css.Stylesheet
import azadev.kotlin.css.color
import azadev.kotlin.css.dimens.px
import azadev.kotlin.css.opacity
import azadev.kotlin.css.width

import kotlinx.html.*
import kotlinx.html.js.*
import kotlinx.html.dom.create
import kotlin.browser.*
import kotlinx.html.dom.append
import org.w3c.dom.HTMLButtonElement

fun main(args: Array<String>) {
    println("Hello JavaScript!")

    val myDiv = document.create.div("panel") {   // class ="panel"
        p {
            +"Here is"
            a("http://kotlinlang.org") { +"official Kotlin site" }
        }
    }

     val button = BUTTON()
     button!!.innerText ="Click me"
     button!!.onclick = { println("Button clicked!") }

    val btn = document.create.button {
       text("click me")
       onClickFunction = { _ -> window.alert("Kotlin!")   }
       Stylesheet {
           color = 0xffffff
           width = 10.px
           opacity = .8
           hover {
               color = 0xf2cacf
           }
      }
    }


    document.getElementById("container")!!.appendChild(myDiv)
    document.getElementById("container")!!.appendChild(btn)
    document.getElementById("container")!!.appendChild(button)

    document.getElementById("container")!!.append {
        div {
            +"added it"
        }
    }
}

fun BUTTON(): HTMLButtonElement {return document.create.button()}

我的gradle.build是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
group 'org.example'
version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.1.51'
    ext.kotlinx_html_version = '0.6.4'
    ext.aza_kotlin_css = '1.0'
    ext.web_dir = 'web'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'kotlin2js'

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    compile"org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
    compile"org.jetbrains.kotlinx:kotlinx-html-js:$kotlinx_html_version"
    compile"azadev.kotlin:aza-kotlin-css:$aza_kotlin_css"
}

compileKotlin2Js {
    kotlinOptions.outputFile ="${projectDir}/web/scripts/main.js"
    kotlinOptions.moduleKind ="umd"
    kotlinOptions.sourceMap = true
}

clean.doFirst() {
    delete("${web_dir}")
}

build.doLast() {
    // Copy kotlin.js and kotlin-meta.js from jar into web directory
    configurations.compile.each { File file ->
        copy {
            includeEmptyDirs = false

            from zipTree(file.absolutePath)
            into"${projectDir}/${web_dir}/lib"
            include { fileTreeElement ->
                def path = fileTreeElement.path
                path.endsWith(".js") && (path.startsWith("META-INF/resources/") || !path.startsWith("META-INF/"))
            }
        }
    }

    // Copy scripts to web directory
    copy {
        includeEmptyDirs = false
        from new File("build/classes/main")
        into"${web_dir}/lib"
    }

        // Copy resources to web directory
    copy {
        includeEmptyDirs = false
        from new File("src/main/kotlin/resources")
        into"${web_dir}"
    }
}

我的index.html是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
    <head>
        <meta charset="UTF-8">
        Sample Default

    </head>
    <body id="BODY">
    Kotlin 1.1 Example
   
    <input type="text" name="email" id="email"/>
    <script type="text/javascript" src="lib/kotlin.js">
    <script type="text/javascript" src="lib/kotlinx-html-js.js">
    <script type="text/javascript" src="scripts/main.js">
   </body>
</html>

我的应用程序结构:
enter

截止到今天,aza_kotlin_css和应用程序与我的配合出现了问题,如下所示[Mac新手的详细步骤:)]

/usr/bin/ruby -e"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

的形式安装Homebrew

brew install gradle

形式安装gradle

通过运行brew info gradle

找到安装路径

enter

enter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
buildscript {
    ext.kotlin_version = '1.2.21'
    ext.web_dir = 'web'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"   // for gradle build
    }
}
apply plugin: 'kotlin2js'

repositories {     jcenter()    }

dependencies {
    def kotlinx_html_version ="0.6.8"
    // for interacting with the DOM
    compile"org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
    // for DOM creation in the client sie
    compile"org.jetbrains.kotlinx:kotlinx-html-js:${kotlinx_html_version}"
    // for DOM creation in the server sie
    // compile"org.jetbrains.kotlinx:kotlinx-html-jvm:${kotlinx_html_version}"
}

sourceSets.main {
   kotlin.srcDirs += 'src/kotlin'
   resources.srcDirs += 'src/resources'
}

compileKotlin2Js.kotlinOptions {
   outputFile ="${projectDir}/web/scripts/main.js"
   moduleKind ="commonjs"  // can be other options, commonjs is the one that works with Nodejs
   sourceMap = true
}

clean.doFirst() {
    delete("${web_dir}")
}

build.doLast() {
    // Copy kotlin.js and kotlin-meta.js from jar into web directory
    configurations.compile.each { File file ->
        copy {
            includeEmptyDirs = false

            from zipTree(file.absolutePath)
            into"${projectDir}/${web_dir}/lib"
            include { fileTreeElement ->
                def path = fileTreeElement.path
                path.endsWith(".js") && (path.startsWith("META-INF/resources/")
                        || !path.startsWith("META-INF/"))
            }
        }
    }

    // Copy scripts to web directory
    copy {
        includeEmptyDirs = false
        from new File("build/classes/main")
        into"${web_dir}/lib"
    }

    // Copy resources to web directory
    copy {
        includeEmptyDirs = false
        from new File("src/resources")
        into"${web_dir}"
    }
}

使用以下内容创建了src/kotlin/Main.Kt文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import kotlinx.html.*
import kotlinx.html.js.*
import kotlinx.html.dom.create
import kotlin.browser.*
import kotlinx.html.dom.append
import org.w3c.dom.HTMLButtonElement

fun main(args: Array<String>) {
    println("Hello JavaScript!, do you know that fib(5) = ${fib(5)}")

    val myDiv = document.create.div("panel") {   // class ="panel"
        p {
            +"Here is"
            a("http://kotlinlang.org") { +"official Kotlin site" }
        }
    }

    val email = document.getElementById("email") as HTMLInputElement
    email.value ="[email protected]"

    val button = BUTTON()
    button!!.innerText ="Click me"
    button!!.onclick = { println("Button clicked!") }

    val btn = document.create.button {
        text("click me")
        onClickFunction = { _ -> window.alert("Kotlin!")   }
        style ="""
             color: 0xffffff;
             width: 10.px;
             opacity: .8;
             hover {
                color : 0xf2cacf
            }
           """
    }

   /*
    // OR use one of the bew to load the style from the .css file
      val btn = document.create.button(classes ="container left tree") {
          ... }
      //or
         val btn = document.create.button {
            classes = setOf("container","left","tree")
            classes +="siteHeader"  
          ... }
     */

    document.getElementById("container")!!.appendChild(myDiv)
    document.getElementById("container")!!.appendChild(btn)
    document.getElementById("container")!!.appendChild(button)

    document.getElementById("container")!!.append {
        div {
            +"added it"
        }
    }
}

fun BUTTON(): HTMLButtonElement {return document.create.button()}

使用以下内容创建了另一个文件src/kotlin/Fib.tk,该文件从Main.kt文件调用,并且所有内容都是single JavaScript文件:

1
2
3
4
5
6
fun fib(n: Int):Int {
    return when (n) {
        0,1 -> 1
        else -> fib(n - 1) + fib(n - 2)
    }
}

使用以下内容创建了src/resources/index.html文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<head>
    <meta charset="UTF-8">
    Sample Default
    <link rel="stylesheet" href="styles/main.css">
</head>
<body id="BODY">
    Kotlin 1.1 Example
   
    <input type="text" name="email" id="email"/>
    <script type="text/javascript" src="lib/kotlin.js">
    <script type="text/javascript" src="lib/kotlinx-html-js.js">
    <script type="text/javascript" src="scripts/main.js">
</body>
</html>

使用以下内容创建了src/resources/styles/main.css文件:

1
2
3
#panel {background-color: powderblue;}
h1   {color: blue;}
p    {color: red;}

使用gradle build构建项目,并且可以正常运行。

应用程序结构如下:

enter

1
2
3
4
5
6
7
8
9
10
11
12
val btn = document.create.button {
   text("click me")
   onClickFunction = { _ -> window.alert("Kotlin!")   }
    style ="""
             color: 0xffffff;
             width: 10.px;
             opacity: .8;
             hover {
                color : 0xf2cacf
            }
           """
}

另一个已知选项是使用css文件,并为元素附加所需的类,例如:

1
2
3
4
5
6
val btn = document.create.button(classes ="container left tree") { ... }
//or
val btn = document.create.button {
    classes = setOf("container","left","tree")
    classes +="siteHeader"  //
... }