Skip to content

Commit d5926c1

Browse files
committed
[frontend] add playground back as an internal page
1 parent 7a205c6 commit d5926c1

File tree

5 files changed

+109
-1
lines changed

5 files changed

+109
-1
lines changed

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@mercuryworkshop/epoxy-transport": "^2.1.27",
1919
"@mercuryworkshop/scramjet": "workspace:scramjet",
2020
"dreamland": "workspace:dreamland",
21+
"monaco-editor": "^0.52.2",
2122
"vite-plugin-static-copy": "^3.1.0"
2223
}
2324
}

frontend/public/sw.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
/// <reference types="@mercuryworkshop/scramjet" />
12
importScripts("/scram/scramjet.all.js");
2-
33
const { ScramjetServiceWorker } = $scramjetLoadWorker();
44
const scramjet = new ScramjetServiceWorker();
55

@@ -15,3 +15,47 @@ async function handleRequest(event) {
1515
self.addEventListener("fetch", (event) => {
1616
event.respondWith(handleRequest(event));
1717
});
18+
19+
let playgroundData;
20+
self.addEventListener("message", ({ data }) => {
21+
if (data.type === "playgroundData") {
22+
playgroundData = data;
23+
}
24+
});
25+
26+
scramjet.addEventListener("request", (e) => {
27+
if (playgroundData && e.url.href.startsWith(playgroundData.origin)) {
28+
const headers = {};
29+
const origin = playgroundData.origin;
30+
if (e.url.href === origin + "/") {
31+
headers["content-type"] = "text/html";
32+
e.response = new Response(playgroundData.html, {
33+
headers,
34+
});
35+
} else if (e.url.href === origin + "/style.css") {
36+
headers["content-type"] = "text/css";
37+
e.response = new Response(playgroundData.css, {
38+
headers,
39+
});
40+
} else if (e.url.href === origin + "/script.js") {
41+
headers["content-type"] = "application/javascript";
42+
e.response = new Response(playgroundData.js, {
43+
headers,
44+
});
45+
} else {
46+
e.response = new Response("empty response", {
47+
headers,
48+
});
49+
}
50+
e.response.rawHeaders = headers;
51+
e.response.rawResponse = {
52+
body: e.response.body,
53+
headers: headers,
54+
status: e.response.status,
55+
statusText: e.response.statusText,
56+
};
57+
e.response.finalURL = e.url.toString();
58+
} else {
59+
return;
60+
}
61+
});

frontend/src/Tab.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
injectHistoryEmulation,
88
} from "./history";
99
import { NewTab } from "./pages/NewTab";
10+
import { Playground } from "./pages/Playground";
1011

1112
let id = 0;
1213
export class Tab extends StatefulClass {
@@ -65,6 +66,11 @@ export class Tab extends StatefulClass {
6566
case "newtab":
6667
this.title = "New Tab";
6768
this.internalpage = <NewTab tab={this} />;
69+
break;
70+
case "playground":
71+
this.title = "Scramjet Playground";
72+
this.internalpage = <Playground tab={this} />;
73+
break;
6874
}
6975
} else {
7076
this.internalpage = null;

frontend/src/pages/Playground.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { Component } from "dreamland/core";
2+
import type { Tab } from "../Tab";
3+
4+
import { editor } from "monaco-editor";
5+
6+
const Editor: Component<
7+
{
8+
language: string;
9+
},
10+
{
11+
editor: editor.IStandaloneCodeEditor;
12+
}
13+
> = function (cx) {
14+
cx.mount = () => {
15+
this.editor = editor.create(cx.root, {
16+
language: this.language,
17+
automaticLayout: true,
18+
});
19+
};
20+
21+
return <div></div>;
22+
};
23+
Editor.css = `
24+
:scope {
25+
width: 100%;
26+
height: 100%;
27+
}
28+
29+
`;
30+
31+
export const Playground: Component<
32+
{
33+
tab: Tab;
34+
},
35+
{}
36+
> = function (cx) {
37+
return (
38+
<div>
39+
<h1>Scramjet Playground</h1>
40+
<Editor language="html"></Editor>
41+
</div>
42+
);
43+
};
44+
Playground.css = `
45+
:scope {
46+
width: 100%;
47+
height: 100%;
48+
}
49+
`;

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)