Skip to content

Commit 6a1978b

Browse files
committed
Add Laminar library of text counter application
1 parent 6ac763b commit 6a1978b

File tree

13 files changed

+640
-0
lines changed

13 files changed

+640
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# Ignore generated Metals files
22
.metals
3+
**/metals.sbt
34
# Ignore Scala.js generated output files
45
**/target
56
**/.scala-build
67
**/.bsp
8+
**/.bloop
79
# Ignore generated Mac files
810
**/.DS_Store
11+
# Ignore generated Node.js generated output files
12+
**/node_modules
File renamed without changes.

laminar/.scalafmt.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
version = "3.7.15"
2+
runner.dialect = scala213

laminar/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Text Counter Application in Laminar
2+
3+
This is a text counter application using Laminar. It is based on Scala.js and uses Scala code to build HTML code.

laminar/build.sbt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
enablePlugins(ScalaJSPlugin)
2+
name := "Text Counter Application using Laminar"
3+
scalaVersion := "2.13.12"
4+
scalaJSUseMainModuleInitializer := true
5+
libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "2.4.0"
6+
libraryDependencies += "com.raquo" %%% "laminar" % "16.0.0"

laminar/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<head>
2+
<title>Text Counter Application using Laminar</title>
3+
</head>
4+
<body>
5+
<script
6+
type="module"
7+
src="target/scala-2.13/text-counter-application-using-laminar-fastopt/main.js"
8+
></script>
9+
</body>

laminar/project/build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.9.9

laminar/project/plugins.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.15.0")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.9.9
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import org.scalajs.dom.document
2+
import org.scalajs.dom
3+
import com.raquo.laminar.api.L._
4+
case class FormState(
5+
textContent: String = ""
6+
)
7+
object TextCounterApp {
8+
private val stateVarForm = Var(FormState())
9+
private val submitter = Observer[FormState] { state =>
10+
val textContent = document
11+
.getElementById("textContent")
12+
.asInstanceOf[dom.html.TextArea]
13+
.value
14+
val characters = textContent.length
15+
val words = textContent.trim.split("\\s+").length
16+
val lines = textContent.split("\\n").length
17+
document.getElementById("textOutput").innerHTML =
18+
s"Characters: $characters\nWords: $words\nLines: $lines"
19+
}
20+
def main(args: Array[String]): Unit = {
21+
def textCounterContent: HtmlElement = {
22+
div(
23+
idAttr := "app",
24+
h1("Text Counter Application using Laminar"),
25+
div(
26+
textArea(idAttr := "textContent", rows := 10, cols := 80),
27+
button(
28+
"Update Text Counter Output",
29+
onClick.preventDefault.mapTo(stateVarForm.now()) --> submitter
30+
),
31+
div(idAttr := "textOutput")
32+
)
33+
)
34+
}
35+
renderOnDomContentLoaded(document.body, textCounterContent)
36+
}
37+
}

0 commit comments

Comments
 (0)