From 77c77c1886ae88c88a7263af65a945ba64d085db Mon Sep 17 00:00:00 2001 From: zetashift Date: Wed, 2 Nov 2022 23:19:02 +0100 Subject: [PATCH 01/17] Inital attempt at getting sbt to play with mdoc.js --- build.sbt | 2 +- docs/readme.md | 14 ++++++++++++++ prePR.sbt | 2 +- project/Build.scala | 19 +++++-------------- project/plugins.sbt | 11 ++++++----- 5 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 docs/readme.md diff --git a/build.sbt b/build.sbt index 4725f5357..b5d0bb885 100644 --- a/build.sbt +++ b/build.sbt @@ -13,4 +13,4 @@ val testsChrome = Build.testsChrome val testsFirefox = Build.testsFirefox val testsNodeJsdom = Build.testsNodeJsdom val example = Build.example -val readme = Build.readme +val docs = Build.docs diff --git a/docs/readme.md b/docs/readme.md new file mode 100644 index 000000000..965bc015a --- /dev/null +++ b/docs/readme.md @@ -0,0 +1,14 @@ +# My Project + +To install my project + +```scala +libraryDependencies += "com" % "lib" % "1.0.0" +``` + +```scala +val x = 1 +// x: Int = 1 +List(x, x) +// res0: List[Int] = List(1, 1) +``` diff --git a/prePR.sbt b/prePR.sbt index 100a174e9..03d67c7ec 100644 --- a/prePR.sbt +++ b/prePR.sbt @@ -41,7 +41,7 @@ ThisBuild / prePR_nonCross := Def.sequential( Def.taskDyn { if (scalaVersion.value.startsWith("2.12.")) - Def.task[Unit]((readme / Compile / compile).value) + Def.task[Unit]((docs / Compile / compile).value) else Def.task(()) }, diff --git a/project/Build.scala b/project/Build.scala index bab9374c2..b05bd4020 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -17,6 +17,7 @@ import scalafix.sbt.ScalafixPlugin.autoImport._ import scalatex.ScalatexReadme import Dependencies._ import Lib._ +import mdoc.MdocPlugin object Build { @@ -141,18 +142,8 @@ object Build { .enablePlugins(ScalaJSPlugin) .configure(commonSettings, crossScala, preventPublication) - lazy val readme = - ScalatexReadme( - projectId = "readme", - wd = file(""), - url = "https://github.com/scala-js/scala-js-dom/tree/main", - source = "Index", - autoResources = Seq("example-opt.js"), - ) - .configure(commonSettings, preventPublication) - .settings( - scalaVersion := Ver.scala212, - Compile / resources += (example / Compile / fullOptJS).value.data, - ) - + lazy val docs = project + .in(file("readme")) + .dependsOn(dom) + .enablePlugins(MdocPlugin) } diff --git a/project/plugins.sbt b/project/plugins.sbt index 20bb8dc35..f11f55fda 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,9 +1,10 @@ libraryDependencies += "org.scala-js" %% "scalajs-env-jsdom-nodejs" % "1.1.0" libraryDependencies += "org.scala-js" %% "scalajs-env-selenium" % "1.1.1" -addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.10.4") -addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.11.0") +addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.10.4") +addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.11.0") addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.11") -addSbtPlugin("com.lihaoyi" % "scalatex-sbt-plugin" % "0.3.11") -addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.7.1") -addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6") +addSbtPlugin("com.lihaoyi" % "scalatex-sbt-plugin" % "0.3.11") +addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.7.1") +addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6") +addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.3.6" ) \ No newline at end of file From 7b58cbe9d0219193a3b298b407abbc84e7b95fb1 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Thu, 3 Nov 2022 06:31:08 +0000 Subject: [PATCH 02/17] Fix mdoc configuration --- build.sbt | 1 + project/Build.scala | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/build.sbt b/build.sbt index b5d0bb885..507ef83ed 100644 --- a/build.sbt +++ b/build.sbt @@ -13,4 +13,5 @@ val testsChrome = Build.testsChrome val testsFirefox = Build.testsFirefox val testsNodeJsdom = Build.testsNodeJsdom val example = Build.example +val jsdocs = Build.jsdocs val docs = Build.docs diff --git a/project/Build.scala b/project/Build.scala index b05bd4020..d65017b1a 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -10,6 +10,8 @@ import org.scalajs.jsenv.selenium.SeleniumJSEnv import org.scalajs.sbtplugin.ScalaJSJUnitPlugin import org.scalajs.sbtplugin.ScalaJSPlugin import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._ +import mdoc.MdocPlugin +import mdoc.MdocPlugin.autoImport._ import sbtbuildinfo.BuildInfoPlugin import sbtbuildinfo.BuildInfoPlugin.autoImport._ import scalafix.sbt.ScalafixPlugin @@ -17,7 +19,6 @@ import scalafix.sbt.ScalafixPlugin.autoImport._ import scalatex.ScalatexReadme import Dependencies._ import Lib._ -import mdoc.MdocPlugin object Build { @@ -142,8 +143,17 @@ object Build { .enablePlugins(ScalaJSPlugin) .configure(commonSettings, crossScala, preventPublication) + lazy val jsdocs = project + .in(file("mdocs-js")) + .dependsOn(dom) + .enablePlugins(ScalaJSPlugin) + .configure(commonSettings, crossScala, preventPublication) + lazy val docs = project - .in(file("readme")) - .dependsOn(dom) - .enablePlugins(MdocPlugin) + .in(file("mdocs")) + .settings( + mdocJS := Some(jsdocs) + ) + .enablePlugins(MdocPlugin) + .configure(commonSettings, crossScala, preventPublication) } From 5cfbb6ed7c440179be7670c4b2ae3e2dd31b9570 Mon Sep 17 00:00:00 2001 From: zetashift Date: Wed, 2 Nov 2022 21:41:57 +0100 Subject: [PATCH 03/17] deriveBits should return an ArrayBuffer in a Promise --- api-reports/2_12.txt | 2 +- api-reports/2_13.txt | 2 +- dom/src/main/scala/org/scalajs/dom/SubtleCrypto.scala | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/api-reports/2_12.txt b/api-reports/2_12.txt index a96232668..9bc73d026 100644 --- a/api-reports/2_12.txt +++ b/api-reports/2_12.txt @@ -25158,7 +25158,7 @@ StyleSheetList[JC] def item(index: Int): StyleSheet StyleSheetList[JC] def length: Int StyleSheetList[JC] @scala.scalajs.js.annotation.JSBracketAccess def update(index: Int, v: StyleSheet): Unit SubtleCrypto[JT] def decrypt(algorithm: AlgorithmIdentifier, key: CryptoKey, data: BufferSource): js.Promise[js.Any] -SubtleCrypto[JT] def deriveBits(algorithm: AlgorithmIdentifier, baseKey: CryptoKey, length: Double): js.Promise[js.Any] +SubtleCrypto[JT] def deriveBits(algorithm: AlgorithmIdentifier, baseKey: CryptoKey, length: Double): js.Promise[ArrayBuffer] SubtleCrypto[JT] def deriveKey(algorithm: AlgorithmIdentifier, baseKey: CryptoKey, derivedKeyType: KeyAlgorithmIdentifier, extractable: Boolean, keyUsages: js.Array[KeyUsage]): js.Promise[js.Any] SubtleCrypto[JT] def digest(algorithm: HashAlgorithmIdentifier, data: BufferSource): js.Promise[js.Any] SubtleCrypto[JT] def encrypt(algorithm: AlgorithmIdentifier, key: CryptoKey, data: BufferSource): js.Promise[js.Any] diff --git a/api-reports/2_13.txt b/api-reports/2_13.txt index a96232668..9bc73d026 100644 --- a/api-reports/2_13.txt +++ b/api-reports/2_13.txt @@ -25158,7 +25158,7 @@ StyleSheetList[JC] def item(index: Int): StyleSheet StyleSheetList[JC] def length: Int StyleSheetList[JC] @scala.scalajs.js.annotation.JSBracketAccess def update(index: Int, v: StyleSheet): Unit SubtleCrypto[JT] def decrypt(algorithm: AlgorithmIdentifier, key: CryptoKey, data: BufferSource): js.Promise[js.Any] -SubtleCrypto[JT] def deriveBits(algorithm: AlgorithmIdentifier, baseKey: CryptoKey, length: Double): js.Promise[js.Any] +SubtleCrypto[JT] def deriveBits(algorithm: AlgorithmIdentifier, baseKey: CryptoKey, length: Double): js.Promise[ArrayBuffer] SubtleCrypto[JT] def deriveKey(algorithm: AlgorithmIdentifier, baseKey: CryptoKey, derivedKeyType: KeyAlgorithmIdentifier, extractable: Boolean, keyUsages: js.Array[KeyUsage]): js.Promise[js.Any] SubtleCrypto[JT] def digest(algorithm: HashAlgorithmIdentifier, data: BufferSource): js.Promise[js.Any] SubtleCrypto[JT] def encrypt(algorithm: AlgorithmIdentifier, key: CryptoKey, data: BufferSource): js.Promise[js.Any] diff --git a/dom/src/main/scala/org/scalajs/dom/SubtleCrypto.scala b/dom/src/main/scala/org/scalajs/dom/SubtleCrypto.scala index b4a224bb9..2b2349983 100644 --- a/dom/src/main/scala/org/scalajs/dom/SubtleCrypto.scala +++ b/dom/src/main/scala/org/scalajs/dom/SubtleCrypto.scala @@ -1,6 +1,7 @@ package org.scalajs.dom import scala.scalajs.js +import scala.scalajs.js.typedarray.ArrayBuffer /** [[http://www.w3.org/TR/WebCryptoAPI/#subtlecrypto-interface w3c ¶14 Subtle Crytpo interface]] * @@ -72,7 +73,8 @@ trait SubtleCrypto extends js.Object { * * Defined at [[http://www.w3.org/TR/WebCryptoAPI/#SubtleCrypto-method-deriveBits ¶14.3.8 The deriveBits method]] */ - def deriveBits(algorithm: AlgorithmIdentifier, baseKey: CryptoKey, length: Double): js.Promise[js.Any] = js.native + def deriveBits(algorithm: AlgorithmIdentifier, baseKey: CryptoKey, + length: Double): js.Promise[ArrayBuffer] = js.native /** Returns a Promise of a CryptoKey corresponding to the format, the algorithm, the raw key data, the usages and the * extractability given as parameters. From b2039fc6223e5d2354ae03958d881e463c394033 Mon Sep 17 00:00:00 2001 From: zetashift Date: Thu, 3 Nov 2022 22:26:57 +0100 Subject: [PATCH 04/17] mdoc now works, but interactive JS snippets crashes --- docs/readme.md | 67 ++++++++++++++++++++++++++++++++++++++++----- mdoc.properties | 2 ++ project/Build.scala | 6 +++- 3 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 mdoc.properties diff --git a/docs/readme.md b/docs/readme.md index 965bc015a..e92aac707 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -1,14 +1,67 @@ -# My Project +## scala-js-dom +--- +#### Statically typed DOM API for Scala.js -To install my project +Scala-js-dom provides a nice statically typed interface to the DOM such that it can be called from Scala code without resorting to `js.Dynamic`. +All javascript globals functions, singletons and classes are members of the `org.scalajs.dom`, +`org.scalajs.dom.html`, `org.scalajs.dom.svg`, etc. packages. + +For example: ```scala -libraryDependencies += "com" % "lib" % "1.0.0" +import org.scalajs.dom + +def main() = { + dom.window.alert("Hi from scala-js-dom!") +} ``` +## Usage + +Add the following to your sbt build definition: + ```scala -val x = 1 -// x: Int = 1 -List(x, x) -// res0: List[Int] = List(1, 1) +libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "@VERSION@" ``` +then enjoy the types available in org.scalajs.dom. scalajs-dom @VERSION@ is built and published for Scala.js 1.5+ with Scala 2.11, 2.12, 2.13, and 3.0+. + +To begin with, scala-js-dom organizes the full-list of DOM APIs into a number of buckets: + +- dom.html: HTML element APIs +- dom.svg: SVG element APIs +- dom.idb: IndexedDB APIs +- dom.css: CSS APIs +- dom: Miscellanious, unclassified APIs + +Most names have been shortened from names of the raw browser APIs, since the namespacing avoids collisions. By convention these types are imported qualified: e.g. as html.Canvas instead of directly as Canvas. There is also the dom.raw namespace which contains everything with their full, un-shortened name. + +## Examples + +### Appending a child to a Node + +```scala mdoc:js +import org.scalajs.dom._ + +def appendElement(div: html.Div): Unit = { + val child = document.createElement("div") + child.textContent = "I can add elements to DOM elements!" + div.appendChild(child) +} +``` + +### Add an EventListener for onmousemove + +```scala mdoc:js +import org.scalajs.dom._ + +def showOnMouseCoordinates(pre: html.Pre): Unit = { + pre.onmousemove = (ev: MouseEvent) => + pre.textContent = s""" + |ev.clientX: ${ev.clientX} + |ev.clientY: ${ev.clientY} + |ev.pageX: ${ev.pageX} + |ev.screenX: ${ev.screenX} + |ev.screenY: ${ev.screenY} + """.stripMargin +} +``` \ No newline at end of file diff --git a/mdoc.properties b/mdoc.properties new file mode 100644 index 000000000..aa22788e9 --- /dev/null +++ b/mdoc.properties @@ -0,0 +1,2 @@ +js-classpath=$(coursier fetch org.scala-js:scalajs-library_2.12:1.9.0 org.scala-js:scalajs-dom_sjs1_2.12:2.0.0 -p) +js-scalac-options=-Xplugin:$(coursier fetch --intransitive org.scala-js:scalajs-compiler_2.12.17:1.9.0) \ No newline at end of file diff --git a/project/Build.scala b/project/Build.scala index d65017b1a..c975d62fe 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -17,6 +17,7 @@ import sbtbuildinfo.BuildInfoPlugin.autoImport._ import scalafix.sbt.ScalafixPlugin import scalafix.sbt.ScalafixPlugin.autoImport._ import scalatex.ScalatexReadme +import sbtdynver.DynVerPlugin.autoImport.previousStableVersion import Dependencies._ import Lib._ @@ -152,7 +153,10 @@ object Build { lazy val docs = project .in(file("mdocs")) .settings( - mdocJS := Some(jsdocs) + mdocJS := Some(jsdocs), + mdocVariables := Map( + "VERSION" -> previousStableVersion.value.getOrElse("2.3.0") + ) ) .enablePlugins(MdocPlugin) .configure(commonSettings, crossScala, preventPublication) From f08914535239c17d6b45594557a014e96f989ef6 Mon Sep 17 00:00:00 2001 From: zetashift Date: Thu, 3 Nov 2022 22:39:37 +0100 Subject: [PATCH 05/17] Remove mdoc.properties, interactive JS works --- mdoc.properties | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 mdoc.properties diff --git a/mdoc.properties b/mdoc.properties deleted file mode 100644 index aa22788e9..000000000 --- a/mdoc.properties +++ /dev/null @@ -1,2 +0,0 @@ -js-classpath=$(coursier fetch org.scala-js:scalajs-library_2.12:1.9.0 org.scala-js:scalajs-dom_sjs1_2.12:2.0.0 -p) -js-scalac-options=-Xplugin:$(coursier fetch --intransitive org.scala-js:scalajs-compiler_2.12.17:1.9.0) \ No newline at end of file From ad8b15bf52bb7143ed89561d1f652805130df14e Mon Sep 17 00:00:00 2001 From: zetashift Date: Wed, 16 Nov 2022 19:19:53 +0100 Subject: [PATCH 06/17] Add a couple more examples and try to improve the Contributing section --- docs/readme.md | 73 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 11 deletions(-) diff --git a/docs/readme.md b/docs/readme.md index e92aac707..897a23365 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -1,9 +1,11 @@ ## scala-js-dom + --- + #### Statically typed DOM API for Scala.js Scala-js-dom provides a nice statically typed interface to the DOM such that it can be called from Scala code without resorting to `js.Dynamic`. -All javascript globals functions, singletons and classes are members of the `org.scalajs.dom`, +All javascript globals functions, singletons and classes are members of the `org.scalajs.dom`, `org.scalajs.dom.html`, `org.scalajs.dom.svg`, etc. packages. For example: @@ -11,9 +13,7 @@ For example: ```scala import org.scalajs.dom -def main() = { - dom.window.alert("Hi from scala-js-dom!") -} +def main() = dom.window.alert("Hi from scala-js-dom!") ``` ## Usage @@ -21,8 +21,9 @@ def main() = { Add the following to your sbt build definition: ```scala -libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "@VERSION@" +libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "@VERSION@" ``` + then enjoy the types available in org.scalajs.dom. scalajs-dom @VERSION@ is built and published for Scala.js 1.5+ with Scala 2.11, 2.12, 2.13, and 3.0+. To begin with, scala-js-dom organizes the full-list of DOM APIs into a number of buckets: @@ -31,13 +32,13 @@ To begin with, scala-js-dom organizes the full-list of DOM APIs into a number of - dom.svg: SVG element APIs - dom.idb: IndexedDB APIs - dom.css: CSS APIs -- dom: Miscellanious, unclassified APIs +- dom: Miscellanious, unclassified APIs -Most names have been shortened from names of the raw browser APIs, since the namespacing avoids collisions. By convention these types are imported qualified: e.g. as html.Canvas instead of directly as Canvas. There is also the dom.raw namespace which contains everything with their full, un-shortened name. +Most names have been shortened from names of the raw browser APIs, since the namespacing avoids collisions. By convention these types are imported qualified: e.g. as `html.Canvas` instead of directly as `Canvas`. There is also the `dom.raw` namespace which contains everything with their full, un-shortened name. ## Examples -### Appending a child to a Node +### Appending a child to a `Node` ```scala mdoc:js import org.scalajs.dom._ @@ -45,11 +46,11 @@ import org.scalajs.dom._ def appendElement(div: html.Div): Unit = { val child = document.createElement("div") child.textContent = "I can add elements to DOM elements!" - div.appendChild(child) + div.appendChild(child) } ``` -### Add an EventListener for onmousemove +### Add an EventListener for `onmousemove` ```scala mdoc:js import org.scalajs.dom._ @@ -64,4 +65,54 @@ def showOnMouseCoordinates(pre: html.Pre): Unit = { |ev.screenY: ${ev.screenY} """.stripMargin } -``` \ No newline at end of file +``` + +### Storing an item in `localStorage` + +```scala mdoc:js +def storeInputInLocalStorage(input: html.Input, box: html.Div) = { + val key = "myKey" + input.value = dom.window.localStorage.getItem(key) + + input.onkeyup = { (e: dom.Event) => + dom.window.localStorage.setItem( + key, input.value + ) + + output.textContent = s"Saved: ${input.value} to local storage!" + } +} +``` + +### Using `Canvas` to draw + +```scala mdoc:js + +type Context2D = dom.CanvasRenderingContext2D + +def drawCuteSmiley(canvas: html.Canvas) = { + val context = canvas.getContext("2d").asInstanceOf[Context2D] + + val size = 300 + canvas.width = size + canvas.height = size + + context.strokeStyle = "red" + context.lineWidth = 3 + context.beginPath() + context.moveTo(size/3, 0) + context.lineTo(size/3, size/3) + context.moveTo(size*2/3, 0) + context.lineTo(size*2/3, size/3) + context.moveTo(size, size/2) + context.arc(size/2, size/2, size/2, 0, 3.14) + + context.stroke() +} +``` + +## Contributing + +The DOM API is always evolving, and `scala-js-dom` tries to provide a thin-but-idiomatic Scala interface to modern browser APIs, without breaking the spec. + +If you see something that you think can be improved, feel free to send a pull request. See our [Contributing Guide](https://github.com/scala-js/scala-js-dom/blob/main/CONTRIBUTING.md) for a detailed overview for starting hacking on `scala-js-dom` and making a PR! From 5fb7ea2ba052dd2a91c3907bd756f2faa4948961 Mon Sep 17 00:00:00 2001 From: zetashift Date: Wed, 2 Nov 2022 19:37:26 +0100 Subject: [PATCH 07/17] Fixup Clipboard to use added ClipboardItem --- api-reports/2_12.txt | 15 ++++++++-- api-reports/2_13.txt | 15 ++++++++-- .../org/scalajs/dom/PresentationStyle.scala | 16 +++++++++++ .../org/scalajs/dom/PresentationStyle.scala | 15 ++++++++++ .../scala/org/scalajs/dom/Clipboard.scala | 4 +-- .../scala/org/scalajs/dom/ClipboardItem.scala | 28 +++++++++++++++++++ .../scalajs/dom/ClipboardItemOptions.scala | 13 +++++++++ .../scala/org/scalajs/dom/FrozenArray.scala | 23 +++++++++++++++ .../main/scala/org/scalajs/dom/package.scala | 2 ++ 9 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 dom/src/main/scala-2/org/scalajs/dom/PresentationStyle.scala create mode 100644 dom/src/main/scala-3/org/scalajs/dom/PresentationStyle.scala create mode 100644 dom/src/main/scala/org/scalajs/dom/ClipboardItem.scala create mode 100644 dom/src/main/scala/org/scalajs/dom/ClipboardItemOptions.scala create mode 100644 dom/src/main/scala/org/scalajs/dom/FrozenArray.scala diff --git a/api-reports/2_12.txt b/api-reports/2_12.txt index 9bc73d026..7968653c8 100644 --- a/api-reports/2_12.txt +++ b/api-reports/2_12.txt @@ -993,11 +993,11 @@ Clients[JT] def openWindow(url: String): js.Promise[WindowClient] Clipboard[JT] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit Clipboard[JT] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit Clipboard[JT] def dispatchEvent(evt: Event): Boolean -Clipboard[JT] def read(): js.Promise[DataTransfer] +Clipboard[JT] def read(): js.Promise[ClipboardItem] Clipboard[JT] def readText(): js.Promise[String] Clipboard[JT] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit Clipboard[JT] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit -Clipboard[JT] def write(data: DataTransfer): js.Promise[Unit] +Clipboard[JT] def write(data: ClipboardItem): js.Promise[Unit] Clipboard[JT] def writeText(newClipText: String): js.Promise[Unit] ClipboardEvent[JC] def bubbles: Boolean ClipboardEvent[JC] def cancelBubble: Boolean @@ -1019,6 +1019,10 @@ ClipboardEventInit[JT] var composed: js.UndefOr[Boolean] ClipboardEventInit[JT] var data: js.UndefOr[String] ClipboardEventInit[JT] var dataType: js.UndefOr[String] ClipboardEventInit[JT] var scoped: js.UndefOr[Boolean] +ClipboardItem[JC] def getType(`type`: String): js.Promise[Blob] +ClipboardItem[JC] def presentationStyle: PresentationStyle +ClipboardItem[JC] def types: FrozenArray[String] +ClipboardItemOptions[JT] def presentationStyle: js.UndefOr[PresentationStyle] CloseEvent[JT] def bubbles: Boolean CloseEvent[JT] def cancelBubble: Boolean CloseEvent[JT] def cancelable: Boolean @@ -2017,6 +2021,8 @@ FrameType[SO] val auxiliary: FrameType FrameType[SO] val nested: FrameType FrameType[SO] val none: FrameType FrameType[SO] val `top-level` = "top-level".asInstanceOf[FrameType] +FrozenArray[JT] @js.annotation.JSBracketAccess def apply(index: Int): T +FrozenArray[JT] val length: Int FullscreenOptions[JT] var navigationUI: js.UndefOr[String] GainNode[JT] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit GainNode[JT] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit @@ -16226,6 +16232,10 @@ PositionError[JT] def message: String PositionOptions[JC] var enableHighAccuracy: Boolean PositionOptions[JC] var maximumAge: Int PositionOptions[JC] var timeout: Int +PresentationStyle[JT] +PresentationStyle[SO] val attachment: PresentationStyle +PresentationStyle[SO] val inline: PresentationStyle +PresentationStyle[SO] val unspecified: PresentationStyle ProcessingInstruction[JC] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit ProcessingInstruction[JC] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit ProcessingInstruction[JC] def appendChild(newChild: Node): Node @@ -26946,6 +26956,7 @@ package[SO] type BufferSource = ArrayBufferView | ArrayBuffer package[SO] type ByteString = String package[SO] type ClientRect = DOMRect (@deprecated in 2.0.0) package[SO] type ClientRectList = DOMRectList (@deprecated in 2.0.0) +package[SO] type ClipboardItemData = js.Promise[String | Blob] package[SO] type HashAlgorithmIdentifier = HashAlgorithm | String package[SO] type HeadersInit = Headers | Sequence[Sequence[ByteString]] | OpenEndedDictionary[ByteString] package[SO] type IDBKey = Any diff --git a/api-reports/2_13.txt b/api-reports/2_13.txt index 9bc73d026..7968653c8 100644 --- a/api-reports/2_13.txt +++ b/api-reports/2_13.txt @@ -993,11 +993,11 @@ Clients[JT] def openWindow(url: String): js.Promise[WindowClient] Clipboard[JT] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit Clipboard[JT] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit Clipboard[JT] def dispatchEvent(evt: Event): Boolean -Clipboard[JT] def read(): js.Promise[DataTransfer] +Clipboard[JT] def read(): js.Promise[ClipboardItem] Clipboard[JT] def readText(): js.Promise[String] Clipboard[JT] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit Clipboard[JT] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit -Clipboard[JT] def write(data: DataTransfer): js.Promise[Unit] +Clipboard[JT] def write(data: ClipboardItem): js.Promise[Unit] Clipboard[JT] def writeText(newClipText: String): js.Promise[Unit] ClipboardEvent[JC] def bubbles: Boolean ClipboardEvent[JC] def cancelBubble: Boolean @@ -1019,6 +1019,10 @@ ClipboardEventInit[JT] var composed: js.UndefOr[Boolean] ClipboardEventInit[JT] var data: js.UndefOr[String] ClipboardEventInit[JT] var dataType: js.UndefOr[String] ClipboardEventInit[JT] var scoped: js.UndefOr[Boolean] +ClipboardItem[JC] def getType(`type`: String): js.Promise[Blob] +ClipboardItem[JC] def presentationStyle: PresentationStyle +ClipboardItem[JC] def types: FrozenArray[String] +ClipboardItemOptions[JT] def presentationStyle: js.UndefOr[PresentationStyle] CloseEvent[JT] def bubbles: Boolean CloseEvent[JT] def cancelBubble: Boolean CloseEvent[JT] def cancelable: Boolean @@ -2017,6 +2021,8 @@ FrameType[SO] val auxiliary: FrameType FrameType[SO] val nested: FrameType FrameType[SO] val none: FrameType FrameType[SO] val `top-level` = "top-level".asInstanceOf[FrameType] +FrozenArray[JT] @js.annotation.JSBracketAccess def apply(index: Int): T +FrozenArray[JT] val length: Int FullscreenOptions[JT] var navigationUI: js.UndefOr[String] GainNode[JT] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit GainNode[JT] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit @@ -16226,6 +16232,10 @@ PositionError[JT] def message: String PositionOptions[JC] var enableHighAccuracy: Boolean PositionOptions[JC] var maximumAge: Int PositionOptions[JC] var timeout: Int +PresentationStyle[JT] +PresentationStyle[SO] val attachment: PresentationStyle +PresentationStyle[SO] val inline: PresentationStyle +PresentationStyle[SO] val unspecified: PresentationStyle ProcessingInstruction[JC] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit ProcessingInstruction[JC] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit ProcessingInstruction[JC] def appendChild(newChild: Node): Node @@ -26946,6 +26956,7 @@ package[SO] type BufferSource = ArrayBufferView | ArrayBuffer package[SO] type ByteString = String package[SO] type ClientRect = DOMRect (@deprecated in 2.0.0) package[SO] type ClientRectList = DOMRectList (@deprecated in 2.0.0) +package[SO] type ClipboardItemData = js.Promise[String | Blob] package[SO] type HashAlgorithmIdentifier = HashAlgorithm | String package[SO] type HeadersInit = Headers | Sequence[Sequence[ByteString]] | OpenEndedDictionary[ByteString] package[SO] type IDBKey = Any diff --git a/dom/src/main/scala-2/org/scalajs/dom/PresentationStyle.scala b/dom/src/main/scala-2/org/scalajs/dom/PresentationStyle.scala new file mode 100644 index 000000000..0d1c69f69 --- /dev/null +++ b/dom/src/main/scala-2/org/scalajs/dom/PresentationStyle.scala @@ -0,0 +1,16 @@ +package org.scalajs.dom + +import scala.scalajs.js + +/** + * Helps distinguish whether apps "pasting" a clipboard item should insert the contents of an appropriate representation inline at the point of paste or if it should be treated as an attachment. + * See [[https://w3c.github.io/clipboard-apis/#enumdef-presentationstyle PresentationStyle enum]] + */ +@js.native +sealed trait PresentationStyle extends js.Any + +object PresentationStyle{ + val unspecified: PresentationStyle = "unspecified".asInstanceOf[PresentationStyle] + val inline: PresentationStyle = "inline".asInstanceOf[PresentationStyle] + val attachment: PresentationStyle = "attachment".asInstanceOf[PresentationStyle] +} diff --git a/dom/src/main/scala-3/org/scalajs/dom/PresentationStyle.scala b/dom/src/main/scala-3/org/scalajs/dom/PresentationStyle.scala new file mode 100644 index 000000000..1c09ad1ab --- /dev/null +++ b/dom/src/main/scala-3/org/scalajs/dom/PresentationStyle.scala @@ -0,0 +1,15 @@ +package org.scalajs.dom + +import scala.scalajs.js + +/** + * Helps distinguish whether apps "pasting" a clipboard item should insert the contents of an appropriate representation inline at the point of paste or if it should be treated as an attachment. + * See [[https://w3c.github.io/clipboard-apis/#enumdef-presentationstyle PresentationStyle enum]] + */ +opaque type PresentationStyle <: String = String + +object PresentationStyle { + val unspecified: PresentationStyle = "unspecified" + val inline: PresentationStyle = "inline" + val attachment: PresentationStyle = "attachment" +} \ No newline at end of file diff --git a/dom/src/main/scala/org/scalajs/dom/Clipboard.scala b/dom/src/main/scala/org/scalajs/dom/Clipboard.scala index e07c4ed6d..105a30a48 100644 --- a/dom/src/main/scala/org/scalajs/dom/Clipboard.scala +++ b/dom/src/main/scala/org/scalajs/dom/Clipboard.scala @@ -25,7 +25,7 @@ trait Clipboard extends EventTarget { * * To read from the clipboard, you must first have the "clipboard-read" permission. */ - def read(): js.Promise[DataTransfer] = js.native + def read(): js.Promise[ClipboardItem] = js.native /** The readText() method returns a Promise which resolves with a copy of the textual contents of the system * clipboard. @@ -38,7 +38,7 @@ trait Clipboard extends EventTarget { * Before you can write to the clipboard, you need to use the Permissions API to get the "clipboard-write" * permission. */ - def write(data: DataTransfer): js.Promise[Unit] = js.native + def write(data: ClipboardItem): js.Promise[Unit] = js.native /** The writeText() method writes the specified text string to the system clipboard. */ def writeText(newClipText: String): js.Promise[Unit] = js.native diff --git a/dom/src/main/scala/org/scalajs/dom/ClipboardItem.scala b/dom/src/main/scala/org/scalajs/dom/ClipboardItem.scala new file mode 100644 index 000000000..bf7203759 --- /dev/null +++ b/dom/src/main/scala/org/scalajs/dom/ClipboardItem.scala @@ -0,0 +1,28 @@ +/** All documentation for facades is thanks to Mozilla Contributors at https://developer.mozilla.org/en-US/docs/Web/API + * and available under the Creative Commons Attribution-ShareAlike v2.5 or later. + * http://creativecommons.org/licenses/by-sa/2.5/ + * + * Everything else is under the MIT License http://opensource.org/licenses/MIT + */ +package org.scalajs.dom + +import scala.scalajs.js +import scala.scalajs.js.annotation._ + +/** A clipboard item is conceptually data that the user has expressed a desire to make shareable by invoking a "cut" or + * "copy" command + */ +@js.native +@JSGlobal +class ClipboardItem(items: js.Dictionary[ClipboardItemData], options: ClipboardItemOptions = js.native) + extends js.Object { + def presentationStyle: PresentationStyle = js.native + + /** Returns an Array of MIME types available within the ClipboardItem. */ + def types: FrozenArray[String] = js.native + + /** Returns a Promise that resolves with a Blob of the requested MIME type, or an error if the MIME type is not found. + */ + def getType(`type`: String): js.Promise[Blob] = js.native + +} diff --git a/dom/src/main/scala/org/scalajs/dom/ClipboardItemOptions.scala b/dom/src/main/scala/org/scalajs/dom/ClipboardItemOptions.scala new file mode 100644 index 000000000..04921d2a9 --- /dev/null +++ b/dom/src/main/scala/org/scalajs/dom/ClipboardItemOptions.scala @@ -0,0 +1,13 @@ +/** All documentation for facades is thanks to Mozilla Contributors at https://developer.mozilla.org/en-US/docs/Web/API + * and available under the Creative Commons Attribution-ShareAlike v2.5 or later. + * http://creativecommons.org/licenses/by-sa/2.5/ + * + * Everything else is under the MIT License http://opensource.org/licenses/MIT + */ +package org.scalajs.dom + +import scala.scalajs.js + +trait ClipboardItemOptions extends js.Object { + def presentationStyle: js.UndefOr[PresentationStyle] = js.undefined +} diff --git a/dom/src/main/scala/org/scalajs/dom/FrozenArray.scala b/dom/src/main/scala/org/scalajs/dom/FrozenArray.scala new file mode 100644 index 000000000..eb3848e15 --- /dev/null +++ b/dom/src/main/scala/org/scalajs/dom/FrozenArray.scala @@ -0,0 +1,23 @@ +/** All documentation for facades is thanks to Mozilla Contributors at https://developer.mozilla.org/en-US/docs/Web/API + * and available under the Creative Commons Attribution-ShareAlike v2.5 or later. + * http://creativecommons.org/licenses/by-sa/2.5/ + * + * Everything else is under the MIT License http://opensource.org/licenses/MIT + */ +package org.scalajs.dom + +import scala.scalajs.js + +/** A frozen array type is a parameterized type whose values are references to objects that hold a fixed length array of + * unmodifiable values. The values in the array are of type T. + * + * Since FrozenArray values are references, they are unlike sequence types, which are lists of values that are + * passed by value. + */ +@js.native +trait FrozenArray[+T] extends js.Iterable[T] { + val length: Int = js.native + + @js.annotation.JSBracketAccess + def apply(index: Int): T = js.native +} diff --git a/dom/src/main/scala/org/scalajs/dom/package.scala b/dom/src/main/scala/org/scalajs/dom/package.scala index a17cd5c33..95bf272d9 100644 --- a/dom/src/main/scala/org/scalajs/dom/package.scala +++ b/dom/src/main/scala/org/scalajs/dom/package.scala @@ -114,4 +114,6 @@ package object dom { val webcrypto: Crypto = js.native type BlobPart = BufferSource | Blob | String + + type ClipboardItemData = js.Promise[String | Blob] } From 116eff5562ad71dc6072fcca0954446e7effc3df Mon Sep 17 00:00:00 2001 From: zetashift Date: Wed, 2 Nov 2022 19:57:21 +0100 Subject: [PATCH 08/17] Clipboard read and write methods should use arrays of ClipboardItem --- api-reports/2_12.txt | 4 ++-- api-reports/2_13.txt | 4 ++-- dom/src/main/scala/org/scalajs/dom/Clipboard.scala | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/api-reports/2_12.txt b/api-reports/2_12.txt index 7968653c8..3f80f8603 100644 --- a/api-reports/2_12.txt +++ b/api-reports/2_12.txt @@ -993,11 +993,11 @@ Clients[JT] def openWindow(url: String): js.Promise[WindowClient] Clipboard[JT] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit Clipboard[JT] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit Clipboard[JT] def dispatchEvent(evt: Event): Boolean -Clipboard[JT] def read(): js.Promise[ClipboardItem] +Clipboard[JT] def read(): js.Promise[js.Array[ClipboardItem]] Clipboard[JT] def readText(): js.Promise[String] Clipboard[JT] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit Clipboard[JT] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit -Clipboard[JT] def write(data: ClipboardItem): js.Promise[Unit] +Clipboard[JT] def write(data: js.Array[ClipboardItem]): js.Promise[Unit] Clipboard[JT] def writeText(newClipText: String): js.Promise[Unit] ClipboardEvent[JC] def bubbles: Boolean ClipboardEvent[JC] def cancelBubble: Boolean diff --git a/api-reports/2_13.txt b/api-reports/2_13.txt index 7968653c8..3f80f8603 100644 --- a/api-reports/2_13.txt +++ b/api-reports/2_13.txt @@ -993,11 +993,11 @@ Clients[JT] def openWindow(url: String): js.Promise[WindowClient] Clipboard[JT] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit Clipboard[JT] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit Clipboard[JT] def dispatchEvent(evt: Event): Boolean -Clipboard[JT] def read(): js.Promise[ClipboardItem] +Clipboard[JT] def read(): js.Promise[js.Array[ClipboardItem]] Clipboard[JT] def readText(): js.Promise[String] Clipboard[JT] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit Clipboard[JT] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit -Clipboard[JT] def write(data: ClipboardItem): js.Promise[Unit] +Clipboard[JT] def write(data: js.Array[ClipboardItem]): js.Promise[Unit] Clipboard[JT] def writeText(newClipText: String): js.Promise[Unit] ClipboardEvent[JC] def bubbles: Boolean ClipboardEvent[JC] def cancelBubble: Boolean diff --git a/dom/src/main/scala/org/scalajs/dom/Clipboard.scala b/dom/src/main/scala/org/scalajs/dom/Clipboard.scala index 105a30a48..bdb6b0e92 100644 --- a/dom/src/main/scala/org/scalajs/dom/Clipboard.scala +++ b/dom/src/main/scala/org/scalajs/dom/Clipboard.scala @@ -25,7 +25,7 @@ trait Clipboard extends EventTarget { * * To read from the clipboard, you must first have the "clipboard-read" permission. */ - def read(): js.Promise[ClipboardItem] = js.native + def read(): js.Promise[js.Array[ClipboardItem]] = js.native /** The readText() method returns a Promise which resolves with a copy of the textual contents of the system * clipboard. @@ -38,7 +38,7 @@ trait Clipboard extends EventTarget { * Before you can write to the clipboard, you need to use the Permissions API to get the "clipboard-write" * permission. */ - def write(data: ClipboardItem): js.Promise[Unit] = js.native + def write(data: js.Array[ClipboardItem]): js.Promise[Unit] = js.native /** The writeText() method writes the specified text string to the system clipboard. */ def writeText(newClipText: String): js.Promise[Unit] = js.native From 879948c600dfa5f09166acc3ff614e58d50bdfda Mon Sep 17 00:00:00 2001 From: Scala Steward Date: Sat, 5 Nov 2022 00:38:01 +0000 Subject: [PATCH 09/17] Update sbt to 1.7.3 --- project/build.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/build.properties b/project/build.properties index 563a014da..6a9f03889 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.7.2 +sbt.version=1.7.3 From 7887bd5c89d989d9d2e1a96e1c2d272d418656a0 Mon Sep 17 00:00:00 2001 From: Scala Steward Date: Sat, 5 Nov 2022 00:38:11 +0000 Subject: [PATCH 10/17] Update scalafmt-core to 3.6.1 --- .scalafmt.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.scalafmt.conf b/.scalafmt.conf index dd74f0989..632748292 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -1,4 +1,4 @@ -version = 3.6.0 +version = 3.6.1 runner.dialect = scala213source3 project.git = true style = Scala.js From ecd3b91d33ee90a8f2cd876d19cf4f950c4e728e Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Sat, 5 Nov 2022 09:50:15 -0700 Subject: [PATCH 11/17] Remove stale todo --- dom/src/main/scala/org/scalajs/dom/Body.scala | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dom/src/main/scala/org/scalajs/dom/Body.scala b/dom/src/main/scala/org/scalajs/dom/Body.scala index 0b9cf0453..3e472ea70 100644 --- a/dom/src/main/scala/org/scalajs/dom/Body.scala +++ b/dom/src/main/scala/org/scalajs/dom/Body.scala @@ -22,9 +22,7 @@ trait Body extends js.Object { /** Takes a Response stream and reads it to completion. It returns a promise that resolves with a FormData object. */ def formData(): js.Promise[FormData] = js.native - /** Takes a Response stream and reads it to completion. It returns a promise that resolves with a JSON object. //todo: - * define the JSON type, and return a Promise[JSON] as per spec - */ + /** Takes a Response stream and reads it to completion. It returns a promise that resolves with a JSON object. */ def json(): js.Promise[js.Any] = js.native /** Takes a Response stream and reads it to completion. It returns a promise that resolves with a USVString (text). */ From 52d46044a2d499c8edcb1e456b2dac30bc82c549 Mon Sep 17 00:00:00 2001 From: Scala Steward Date: Sat, 12 Nov 2022 22:25:44 +0000 Subject: [PATCH 12/17] Update sbt to 1.8.0 --- project/build.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/build.properties b/project/build.properties index 6a9f03889..8b9a0b0ab 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.7.3 +sbt.version=1.8.0 From 56f96051b28e1f4f6ae1a55dcdf0d13327249dfa Mon Sep 17 00:00:00 2001 From: zetashift Date: Sun, 19 Feb 2023 23:36:22 +0100 Subject: [PATCH 13/17] Fixup plugins.sbt --- project/plugins.sbt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/project/plugins.sbt b/project/plugins.sbt index f11f55fda..ca29f8d5d 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -6,5 +6,7 @@ addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.11.0") addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.11") addSbtPlugin("com.lihaoyi" % "scalatex-sbt-plugin" % "0.3.11") addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.7.1") -addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6") -addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.3.6" ) \ No newline at end of file +addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.3.6" ) +addSbtPlugin("com.lihaoyi" % "scalatex-sbt-plugin" % "0.3.11") +addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.7.1") +addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.0") From 67df4b9a581f97916226a515de30efc8c0dc9904 Mon Sep 17 00:00:00 2001 From: zetashift Date: Wed, 16 Nov 2022 19:29:45 +0100 Subject: [PATCH 14/17] Add last 2 examples from the old readme --- docs/readme.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/docs/readme.md b/docs/readme.md index 897a23365..58d2ae30b 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -111,6 +111,58 @@ def drawCuteSmiley(canvas: html.Canvas) = { } ``` +### Using `Fetch` to make API calls in the browser + +```scala mdoc:js +def fetchBoredApi(element: html.Pre) = { + val url = + "https://www.boredapi.com/api/activity" + + val responseText = for { + response <- dom.fetch(url) + text <- response.text() + } yield { + text + } + + for (text <- responseText) + pre.textContent = text +} +``` + +### Using Websockets + +```scala mdoc:js +def echoWebSocket(input: html.Input, pre: html.Pre) = { + val echo = "wss://echo.websocket.org" + val socket = new dom.WebSocket(echo) + + socket.onmessage = { + (e: dom.MessageEvent) => + pre.textContent += + e.data.toString + } + + socket.onopen = { (e: dom.Event) => + in.onkeyup = { (e: dom.Event) => + socket.send(input.value) + } + } +} +``` + +### Styling an HTML element + +```scala mdoc:js +def changeColor(div: html.Div) = { + val colors = Seq("red", "green", "blue") + + val index = util.Random.nextInt(colors.length) + + div.style.color = colors(index) +} +``` + ## Contributing The DOM API is always evolving, and `scala-js-dom` tries to provide a thin-but-idiomatic Scala interface to modern browser APIs, without breaking the spec. From 5e3d47b7b5ccfff7aa2e5fdd9423defe8cdeff4f Mon Sep 17 00:00:00 2001 From: zetashift Date: Sun, 19 Feb 2023 23:36:53 +0100 Subject: [PATCH 15/17] Fixup plugins.sbt --- docs/readme.md | 17 +++++++++++++++++ project/Build.scala | 1 + 2 files changed, 18 insertions(+) diff --git a/docs/readme.md b/docs/readme.md index 58d2ae30b..c1f9fa51c 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -1,3 +1,4 @@ +<<<<<<< HEAD ## scala-js-dom --- @@ -168,3 +169,19 @@ def changeColor(div: html.Div) = { The DOM API is always evolving, and `scala-js-dom` tries to provide a thin-but-idiomatic Scala interface to modern browser APIs, without breaking the spec. If you see something that you think can be improved, feel free to send a pull request. See our [Contributing Guide](https://github.com/scala-js/scala-js-dom/blob/main/CONTRIBUTING.md) for a detailed overview for starting hacking on `scala-js-dom` and making a PR! +======= +# My Project + +To install my project + +```scala +libraryDependencies += "com" % "lib" % "1.0.0" +``` + +```scala +val x = 1 +// x: Int = 1 +List(x, x) +// res0: List[Int] = List(1, 1) +``` +>>>>>>> 78916a1 (Update plugins.sbt) diff --git a/project/Build.scala b/project/Build.scala index c975d62fe..3f43b42d1 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -20,6 +20,7 @@ import scalatex.ScalatexReadme import sbtdynver.DynVerPlugin.autoImport.previousStableVersion import Dependencies._ import Lib._ +import mdoc.MdocPlugin object Build { From 0c0a3a983d210486391797eebf2f5436486f36ac Mon Sep 17 00:00:00 2001 From: zetashift Date: Sun, 19 Feb 2023 20:50:00 +0100 Subject: [PATCH 16/17] README code blocks update --- docs/readme.md | 72 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/docs/readme.md b/docs/readme.md index c1f9fa51c..8b2a1b9fc 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -39,11 +39,17 @@ Most names have been shortened from names of the raw browser APIs, since the nam ## Examples -### Appending a child to a `Node` +You can start using the bindings using the following import: -```scala mdoc:js +```scala mdoc:js:shared import org.scalajs.dom._ +``` + + +### Appending a child to a `Node` + +```scala mdoc:js:shared def appendElement(div: html.Div): Unit = { val child = document.createElement("div") child.textContent = "I can add elements to DOM elements!" @@ -51,11 +57,14 @@ def appendElement(div: html.Div): Unit = { } ``` +```scala mdoc:js:invisible + pre.textContent = s""" @@ -73,14 +82,14 @@ def showOnMouseCoordinates(pre: html.Pre): Unit = { ```scala mdoc:js def storeInputInLocalStorage(input: html.Input, box: html.Div) = { val key = "myKey" - input.value = dom.window.localStorage.getItem(key) + input.value = window.localStorage.getItem(key) - input.onkeyup = { (e: dom.Event) => - dom.window.localStorage.setItem( + input.onkeyup = { (e: Event) => + window.localStorage.setItem( key, input.value ) - output.textContent = s"Saved: ${input.value} to local storage!" + box.textContent = s"Saved: ${input.value} to local storage!" } } ``` @@ -88,8 +97,7 @@ def storeInputInLocalStorage(input: html.Input, box: html.Div) = { ### Using `Canvas` to draw ```scala mdoc:js - -type Context2D = dom.CanvasRenderingContext2D +type Context2D = CanvasRenderingContext2D def drawCuteSmiley(canvas: html.Canvas) = { val context = canvas.getContext("2d").asInstanceOf[Context2D] @@ -115,41 +123,47 @@ def drawCuteSmiley(canvas: html.Canvas) = { ### Using `Fetch` to make API calls in the browser ```scala mdoc:js +import scala.concurrent.ExecutionContext.Implicits.global + def fetchBoredApi(element: html.Pre) = { val url = "https://www.boredapi.com/api/activity" val responseText = for { - response <- dom.fetch(url) - text <- response.text() + response <- fetch(url).toFuture + text <- response.text().toFuture } yield { text } for (text <- responseText) - pre.textContent = text + element.textContent = text } ``` + + ### Using Websockets -```scala mdoc:js -def echoWebSocket(input: html.Input, pre: html.Pre) = { - val echo = "wss://echo.websocket.org" - val socket = new dom.WebSocket(echo) - - socket.onmessage = { - (e: dom.MessageEvent) => - pre.textContent += - e.data.toString - } - socket.onopen = { (e: dom.Event) => - in.onkeyup = { (e: dom.Event) => - socket.send(input.value) - } - } -} +```scala mdoc:js +// TODO: currently crashes with an error +// def echoWebSocket(input: html.Input, pre: html.Pre) = { +// val echo = "wss://echo.websocket.org" +// val socket = new WebSocket(echo) + +// socket.onmessage = { +// (e: MessageEvent) => +// pre.textContent += +// e.data.toString +// } + +// socket.onopen = { (e: Event) => +// input.onkeyup = { (e: Event) => +// socket.send(input.value) +// } +// } +// } ``` ### Styling an HTML element From f679c6ae9bb9316bebbbe3eea9efdefc07c97d28 Mon Sep 17 00:00:00 2001 From: zetashift Date: Sun, 19 Feb 2023 23:23:36 +0100 Subject: [PATCH 17/17] Add first runnable example --- docs/readme.md | 28 +++++----------------------- project/Build.scala | 1 - 2 files changed, 5 insertions(+), 24 deletions(-) diff --git a/docs/readme.md b/docs/readme.md index 8b2a1b9fc..166c4eb9a 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -1,4 +1,3 @@ -<<<<<<< HEAD ## scala-js-dom --- @@ -45,10 +44,8 @@ You can start using the bindings using the following import: import org.scalajs.dom._ ``` - ### Appending a child to a `Node` - ```scala mdoc:js:shared def appendElement(div: html.Div): Unit = { val child = document.createElement("div") @@ -58,8 +55,12 @@ def appendElement(div: html.Div): Unit = { ``` ```scala mdoc:js:invisible - + --- +document.getElementById("demo1").addEventListener("click", (ev: Event) => { + appendElement(document.getElementById("outer-container").asInstanceOf[html.Div]) +}) ``` ### Add an EventListener for `onmousemove` @@ -141,11 +142,8 @@ def fetchBoredApi(element: html.Pre) = { } ``` - - ### Using Websockets - ```scala mdoc:js // TODO: currently crashes with an error // def echoWebSocket(input: html.Input, pre: html.Pre) = { @@ -183,19 +181,3 @@ def changeColor(div: html.Div) = { The DOM API is always evolving, and `scala-js-dom` tries to provide a thin-but-idiomatic Scala interface to modern browser APIs, without breaking the spec. If you see something that you think can be improved, feel free to send a pull request. See our [Contributing Guide](https://github.com/scala-js/scala-js-dom/blob/main/CONTRIBUTING.md) for a detailed overview for starting hacking on `scala-js-dom` and making a PR! -======= -# My Project - -To install my project - -```scala -libraryDependencies += "com" % "lib" % "1.0.0" -``` - -```scala -val x = 1 -// x: Int = 1 -List(x, x) -// res0: List[Int] = List(1, 1) -``` ->>>>>>> 78916a1 (Update plugins.sbt) diff --git a/project/Build.scala b/project/Build.scala index 3f43b42d1..c975d62fe 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -20,7 +20,6 @@ import scalatex.ScalatexReadme import sbtdynver.DynVerPlugin.autoImport.previousStableVersion import Dependencies._ import Lib._ -import mdoc.MdocPlugin object Build {