Skip to content

Commit

Permalink
Add Laminar library of text counter application
Browse files Browse the repository at this point in the history
  • Loading branch information
lulunac27a committed Mar 6, 2024
1 parent 6ac763b commit 6a1978b
Show file tree
Hide file tree
Showing 13 changed files with 640 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Ignore generated Metals files
.metals
**/metals.sbt
# Ignore Scala.js generated output files
**/target
**/.scala-build
**/.bsp
**/.bloop
# Ignore generated Mac files
**/.DS_Store
# Ignore generated Node.js generated output files
**/node_modules
File renamed without changes.
2 changes: 2 additions & 0 deletions laminar/.scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version = "3.7.15"
runner.dialect = scala213
3 changes: 3 additions & 0 deletions laminar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Text Counter Application in Laminar

This is a text counter application using Laminar. It is based on Scala.js and uses Scala code to build HTML code.
6 changes: 6 additions & 0 deletions laminar/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
enablePlugins(ScalaJSPlugin)
name := "Text Counter Application using Laminar"
scalaVersion := "2.13.12"
scalaJSUseMainModuleInitializer := true
libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "2.4.0"
libraryDependencies += "com.raquo" %%% "laminar" % "16.0.0"
9 changes: 9 additions & 0 deletions laminar/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<head>
<title>Text Counter Application using Laminar</title>
</head>
<body>
<script
type="module"
src="target/scala-2.13/text-counter-application-using-laminar-fastopt/main.js"
></script>
</body>
1 change: 1 addition & 0 deletions laminar/project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.9.9
1 change: 1 addition & 0 deletions laminar/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.15.0")
1 change: 1 addition & 0 deletions laminar/project/project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.9.9
37 changes: 37 additions & 0 deletions laminar/src/main/scala/textcounter/Model.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import org.scalajs.dom.document
import org.scalajs.dom
import com.raquo.laminar.api.L._
case class FormState(
textContent: String = ""
)
object TextCounterApp {
private val stateVarForm = Var(FormState())
private val submitter = Observer[FormState] { state =>
val textContent = document
.getElementById("textContent")
.asInstanceOf[dom.html.TextArea]
.value
val characters = textContent.length
val words = textContent.trim.split("\\s+").length
val lines = textContent.split("\\n").length
document.getElementById("textOutput").innerHTML =
s"Characters: $characters\nWords: $words\nLines: $lines"
}
def main(args: Array[String]): Unit = {
def textCounterContent: HtmlElement = {
div(
idAttr := "app",
h1("Text Counter Application using Laminar"),
div(
textArea(idAttr := "textContent", rows := 10, cols := 80),
button(
"Update Text Counter Output",
onClick.preventDefault.mapTo(stateVarForm.now()) --> submitter
),
div(idAttr := "textOutput")
)
)
}
renderOnDomContentLoaded(document.body, textCounterContent)
}
}
Loading

0 comments on commit 6a1978b

Please sign in to comment.