Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playground improvements #169

Merged
merged 7 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/playground/assets/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ hr {
margin: 40px 0;
}

button {
margin: 20px 0;
padding: 6px 20px;
font-family: inherit;
background: none;
color: inherit;
border: 3px solid white;
border-radius: 6px;
}

button:hover {
cursor: pointer;
}

@media (max-width: 700px) {
header {
flex-direction: column;
Expand Down
29 changes: 21 additions & 8 deletions src/playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ezno Playground</title>
<!-- Vite doesn't rewrite them :( https://github.com/vitejs/vite/issues/7362 -->
<!-- Also maybe they should be in the parent directory? -->
<meta property="twitter:image" content="/ezno/playground/assets/banner.png">
<meta property="og:image" content="/ezno/playground/assets/banner.png">

<link rel="stylesheet" href="assets/index.css">
<meta property="twitter:image" content="assets/banner.png">
<meta property="og:image" content="assets/banner.png">
<link rel="icon" href="assets/ezno.svg">
</head>

<body>
<header>
<img src="assets/ezno.svg" alt="EZNO" height="40px">
<p>
Playground for the Ezno type checker. See what Ezno supports <a
href="https://github.com/kaleidawave/ezno/blob/main/checker/specification/specification.md">in its
specification</a>.
This is built using the <a href="https://www.npmjs.com/package/ezno">WASM JS build</a>, the native CLI tool
can be <a href="https://github.com/kaleidawave/ezno/releases">downloaded from the latest release</a>.
Playground for the Ezno type checker.
<a href="https://kaleidawave.github.io/posts/the-quest-continues/#there-is-a-new-web-based-playground">See
the announcement post for examples</a>
and see <a
href="https://github.com/kaleidawave/ezno/blob/main/checker/specification/specification.md">specification.md</a>
for what Ezno supports.
This playground uses the <a href="https://www.npmjs.com/package/ezno">WASM JS build of Ezno</a>. The native
CLI tool can be <a href="https://github.com/kaleidawave/ezno/releases">downloaded from the latest
release</a>.
</p>
</header>
<main>
Expand All @@ -30,14 +37,20 @@ <h3>Diagnostics</h3>
</ol>
</div>
<button id="share">Share</button>
<details>
<summary>More:</summary>
<div id="version"></div>
<div id="time"></div>
</details>
</main>
<footer>
<hr>
<p>
If you encounter any issues, please document them <a href="https://github.com/kaleidawave/ezno/">on the GH
issue tracker</a>. If diagnostics don't update then a crash has occurred (check browser logs for
reason). Editor built with <a
href="https://codemirror.net/">codemirror</a>
href="https://codemirror.net/">codemirror</a>. Saving/sharing system <a
href="https://www.val.town/v/kaleidawave/savedNamedPlaygrounds">on val.town</a>.
</p>
</footer>
<script type="module" src="./main.js"></script>
Expand Down
52 changes: 29 additions & 23 deletions src/playground/main.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { EditorView, keymap, hoverTooltip } from "@codemirror/view";
import { EditorState, Text } from "@codemirror/state";
import { linter } from "@codemirror/lint";
import { defaultKeymap, indentWithTab } from "@codemirror/commands";
import { defaultKeymap, indentWithTab, toggleLineComment } from "@codemirror/commands";
import { parser as jsParser } from "@lezer/javascript";
import { tags } from "@lezer/highlight";
import { HighlightStyle, syntaxHighlighting, LanguageSupport, LRLanguage } from "@codemirror/language";
import { init as init_ezno, check_with_options, get_version } from "ezno";
import lz from "lz-string";

const diagnosticsEntry = document.querySelector(".diagnostics");
const editorParent = document.querySelector("#editor");
const shareButton = document.querySelector("#share");

const timeOutput = document.querySelector("#time");

const myHighlightStyle = HighlightStyle.define([
{ tag: tags.keyword, color: '#C792EA' },
{ tag: tags.comment, color: '#8b949e' },
Expand All @@ -38,26 +39,26 @@ const theme = EditorView.theme({

const STORE = "https://kaleidawave-savednamedplaygrounds.web.val.run"


let text = "const x: 2 = 3;";
let initialText = text;

const { searchParams } = new URL(location);
const code = searchParams.get("code");
const id = searchParams.get("id");
const raw = searchParams.get("raw-example");

if (code) {
text = lz.decompressFromEncodedURIComponent(code);
setup()
} else if (id) {
if (id) {
fetch(STORE + `?id=${id}`, { method: "GET" }).then(res => res.json()).then((result) => {
if (result.content) {
text = result.content
initialText = text = result.content
setup()
} else if (result.error) {
alert(`Error getting code for id '${result.error}'`)
}
})
} else {
if (raw) {
initialText = raw;
}
setup()
}

Expand All @@ -72,7 +73,10 @@ async function setup() {
return linter((args) => {
text = args.state.doc.text.join("\n");
try {
const start = performance.now();
currentState = check_with_options(ROOT_PATH, (_) => text, { lsp_mode: true, store_type_mappings: true });
const elapsed = performance.now() - start;
timeOutput.innerText = `Parsed & checked in ${Math.trunc(elapsed)}ms`;

diagnosticsEntry.innerHTML = "";
for (const diagnostic of currentState.diagnostics) {
Expand All @@ -94,15 +98,15 @@ async function setup() {
}

function getHover() {
const cursor = hoverTooltip((view, pos, side) => {
const cursor = hoverTooltip((_view, pos, _side) => {
if (currentState) {
const type = currentState.get_type_at_position(ROOT_PATH, pos);
if (typeof type !== "undefined") {
return {
pos,
end: pos,
above: true,
create(view) {
create(_view) {
let dom = document.createElement("div")
dom.textContent = type
// TODO!
Expand Down Expand Up @@ -137,13 +141,13 @@ async function setup() {
return [cursor, cursorTooltipBaseTheme]
}

let editor = new EditorView({
const _editor = new EditorView({
state: EditorState.create({
doc: Text.of([text]),
extensions: [
keymap.of([...defaultKeymap, indentWithTab]),
keymap.of([...defaultKeymap, indentWithTab,]), //toggleLineComment
EditorState.tabSize.of(4),
new LanguageSupport(LRLanguage.define({ parser: jsParser.configure({ dialect: "ts" }) }), [getLinter()]),
new LanguageSupport(LRLanguage.define({ parser: jsParser.configure({ dialect: "ts" }), languageData: { commentTokens: { line: "// " } } }), [getLinter()]),
syntaxHighlighting(myHighlightStyle),
getHover(),
theme,
Expand All @@ -154,29 +158,31 @@ async function setup() {

console.log("Editor ready")
console.log(`Running ezno@${get_version()}`)
document.querySelector("#version").innerText = `Running ezno@${get_version()}`;

shareButton.addEventListener("click", () => {
const url = new URL(location);
const text = editor.state.doc.toString();
const lzCompressCode = lz.compressToEncodedURIComponent(text);
// TODO arbitrary length
console.debug(`lzCompressCode.length=${lzCompressCode.length}`);
if (lzCompressCode.length < 120) {
url.searchParams.set("code", lzCompressCode);
history.pushState({}, "", url);
} else {

if (text !== initialText) {
// This + `if` statement prevents spam
initialText = text;

fetch(STORE, {
method: "POST",
body: JSON.stringify({ content: text })
}).then(res => res.json()).then((result) => {
if (result.id) {
url.searchParams.set("id", result.id);
history.pushState({}, "", url);
navigator.clipboard.writeText(url.toString()).then(() => {
alert("Share URL copied to clipboard")
});
} else if (result.error) {
alert(`Error sharing code '${result.error}'`)
}
})
} else {
alert("Sharing existing code")
}
// TODO also copy to clipboard and popup
})
}
17 changes: 4 additions & 13 deletions src/playground/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions src/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"@codemirror/view": "^6.23.0",
"@lezer/highlight": "^1.2.0",
"@lezer/javascript": "^1.4.13",
"ezno": "=0.0.21",
"lz-string": "^1.5.0"
"ezno": "^0.0.22"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it'd be great if the playground mentioned the version it's using 👀

Copy link
Owner Author

@kaleidawave kaleidawave Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check main.js:162 😄

}
}
}