Skip to content

Commit

Permalink
Merge pull request #167 from xuwei-k/share-query-param
Browse files Browse the repository at this point in the history
add `share` button
  • Loading branch information
xuwei-k authored Jul 20, 2024
2 parents 5e95299 + f894a6a commit 01207f8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
19 changes: 19 additions & 0 deletions localServer/src/test/scala/scalameta_ast/IntegrationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.microsoft.playwright.options.AriaRole
import org.scalatest.BeforeAndAfterAll
import org.scalatest.freespec.AnyFreeSpec
import unfiltered.jetty.Server
import java.nio.charset.StandardCharsets
import java.util.Base64
import scala.io.Source
import scala.jdk.CollectionConverters._
import scala.util.Using
Expand Down Expand Up @@ -627,4 +629,21 @@ abstract class IntegrationTest(
wildcardImport(page).check()
assert(output(page).textContent() == fromResource("path-filter/syntactic.txt"))
}

"query" in withBrowser { page =>
val src = "val x: Y"
val base64 = Base64.getEncoder.encodeToString(src.getBytes(StandardCharsets.UTF_8))
page.navigate(s"http://127.0.0.1:${port()}?source=${base64}")
assert(inputElem(page).inputValue() == src)
assert(
output(page).textContent() == List(
"""Decl.Val(""",
""" Nil,""",
""" List(Pat.Var(Term.Name("x"))),""",
""" Type.Name("Y")""",
""")""",
""
).mkString("\n")
)
}
}
60 changes: 53 additions & 7 deletions sources/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,44 @@ import hljs from "https://unpkg.com/@highlightjs/[email protected]/es/highlight.
import scala from "https://unpkg.com/@highlightjs/[email protected]/es/languages/scala.min.js";
hljs.registerLanguage("scala", scala);

function fromBase64(base64) {
const binString = atob(base64);
const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0));
return new TextDecoder().decode(bytes);
}

function toBase64(text) {
const bytes = new TextEncoder().encode(text);
const binString = Array.from(bytes, (byte) =>
String.fromCodePoint(byte),
).join("");
return btoa(binString);
}

const getFromStorageOr = (key, defaultValue, fun) => {
const saved = localStorage.getItem(key);
if (saved === null) {
return defaultValue;
} else {
if (fun == null) {
return saved;
const p = new URLSearchParams(location.search);
const fromQuery = p.get(key);
if (fromQuery == null) {
const saved = localStorage.getItem(key);
if (saved === null) {
return defaultValue;
} else {
return fun(saved);
if (fun == null) {
return saved;
} else {
return fun(saved);
}
}
} else {
try {
if (fun == null) {
return fromBase64(fromQuery);
} else {
return fun(fromBase64(fromQuery));
}
} catch (e) {
console.log(e);
return defaultValue;
}
}
};
Expand Down Expand Up @@ -369,6 +398,23 @@ const App = () => {
clear local storage
</button>
</div>
<div class="col">
<button
class="btn btn-primary"
id="share"
onclick=${() =>
navigator.clipboard.writeText(
window.location.origin +
window.location.pathname +
"?" +
[["source", inputScala]]
.map(([k, v]) => k + "=" + toBase64(v))
.join("&"),
)}
>
share
</button>
</div>
</div>
</div>
<div class="col-2">
Expand Down

0 comments on commit 01207f8

Please sign in to comment.