-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathweb-component.html
More file actions
240 lines (211 loc) · 7.32 KB
/
Copy pathweb-component.html
File metadata and controls
240 lines (211 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" href="data:," />
<title>Editor Web component</title>
<script type="module" src="/src/web-component.jsx"></script>
<style>
/* Lexend is used by the projects-ui sample theme */
@import url("https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100..900;1,100..900&family=Lexend:wght@100..900&display=swap");
* {
box-sizing: border-box;
}
body {
margin: 0;
block-size: 100dvh;
font-family: "Roboto", sans-serif;
}
main {
block-size: 100%;
display: grid;
grid-template-rows: auto 1fr auto;
}
#editor-component {
display: flex;
min-block-size: 0;
}
.editor-wc {
flex: 1;
display: flex;
}
::part(editor-root) {
flex: 1;
block-size: 100%;
}
.sample-projects-bar {
flex: 0 0 auto;
display: flex;
flex-wrap: wrap;
gap: 0.5rem 1rem;
padding: 1rem;
border-block-end: 1px solid gainsboro;
}
.sample-projects-bar a {
color: #005fb8;
text-decoration: underline;
}
.debug {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 0.5rem;
padding: 1rem;
border-block-start: 1px solid gainsboro;
overflow-x: auto;
> * {
margin: 0;
}
p:empty {
display: none;
}
}
</style>
</head>
<body>
<main>
<div class="sample-projects-bar" id="sample-projects-bar">
<span>Load Sample Projects:</span>
<a href="?project=blank-html-starter">blank-html-starter</a>
<a href="?project=cool-html">cool-html</a>
<a href="?project=draw-a-cat">draw-a-cat</a>
<a href="?project=blank-python-starter">blank-python-starter</a>
<a href="?project=cool-python">cool-python</a>
<a href="?project=python-plotly">python-plotly</a>
<a href="?project=cool-scratch">cool-scratch</a>
<a href="?project=astro-pi">astro-pi</a>
<a href="?project=projects-ui">projects-ui</a>
</div>
<div id="editor-component"></div>
<div class="debug">
<button id="run-button">Run code</button>
<p id="results"></p>
<p id="project-identifier"></p>
</div>
</main>
<script>
const loadSampleProjectByName = async (projectName) => {
const url = new URL(
`projects/${projectName}.json`,
window.location.href,
);
const res = await fetch(url, { cache: "no-store" });
return await res.text();
};
const loadThemeByName = async (projectName) => {
const url = new URL(
`projects/themes/${projectName}.css`,
window.location.href,
);
const res = await fetch(url, { cache: "no-store" });
if (res.status === 404) {
return null;
}
if (!res.ok) {
throw new Error(
`Failed to load theme "${projectName}": ${res.status} ${res.statusText}`,
);
}
// Guard against hosts that answer a missing file with a 200 HTML page —
// that markup would otherwise be injected into the shadow DOM verbatim.
const contentType = res.headers.get("content-type") || "";
if (!contentType.includes("text/css")) {
throw new Error(
`Theme "${projectName}" was served as "${contentType}", expected text/css`,
);
}
return await res.text();
};
document.addEventListener("DOMContentLoaded", async (e) => {
const queryParams = new URLSearchParams(window.location.search);
const projectName = queryParams.get("project");
const [initialProject, themeText] = await Promise.all([
projectName ? loadSampleProjectByName(projectName) : null,
projectName ? loadThemeByName(projectName) : null,
]);
let webComp;
// subscribe to the 'codeChanged' custom event which is pushed by the project react component
document.addEventListener("editor-codeChanged", function (e) {
const code = webComp.editorCode;
});
document.addEventListener("editor-runCompleted", (e) => {
document.getElementById("results").innerText = JSON.stringify(
e.detail,
);
});
document.addEventListener("editor-projectIdentifierChanged", (e) => {
document.getElementById("project-identifier").innerText = e.detail;
});
const editorContainer = document.getElementById("editor-component");
const createWebComponent = (initialProject = null) => {
const initialProjectObject = initialProject
? JSON.parse(initialProject)
: null;
const newWebComp = document.createElement("editor-wc");
newWebComp.setAttribute("with_projectbar", "true");
newWebComp.setAttribute("with_sidebar", "true");
newWebComp.setAttribute("friendly_errors_enabled", "true");
newWebComp.setAttribute("editable_instructions", "true");
// see "Offline support" section in README for more details
newWebComp.setAttribute("offline_enabled", "true");
newWebComp.setAttribute(
"sidebar_options",
JSON.stringify([
"projects",
"instructions",
"file",
"images",
"download",
"settings",
"info",
]),
);
newWebComp.setAttribute("code", "");
newWebComp.setAttribute("class", "editor-wc");
newWebComp.setAttribute("host_styles", JSON.stringify([]));
const scratchApiEndpoint = new URL(
".",
window.location.href,
).href.replace(/\/$/, "");
newWebComp.setAttribute("scratch_api_endpoint", scratchApiEndpoint);
if (themeText) {
newWebComp.setAttribute("host_styles", JSON.stringify([themeText]));
}
if (projectName === "projects-ui") {
newWebComp.setAttribute("editable_instructions", "false");
newWebComp.setAttribute(
"sidebar_options",
JSON.stringify([
"instructions",
"file",
"images",
"download",
"settings",
"info",
]),
);
}
// Applied last so an explicit query param always wins over the
// defaults above.
queryParams.forEach((value, key) => {
newWebComp.setAttribute(key, value);
});
if (initialProject) {
newWebComp.setAttribute(
"sense_hat_always_enabled",
initialProjectObject.sense_hat_always_enabled || false,
);
newWebComp.setAttribute("initial_project", initialProject);
}
return newWebComp;
};
webComp = createWebComponent(initialProject);
editorContainer.prepend(webComp);
document.getElementById("run-button").onclick = (event) => {
webComp.rerunCode();
};
});
</script>
</body>
</html>