-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathweb-component.html
More file actions
156 lines (139 loc) · 4.76 KB
/
Copy pathweb-component.html
File metadata and controls
156 lines (139 loc) · 4.76 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
<!DOCTYPE html>
<html>
<style>
#root {
min-block-size: 100dvh;
-ms-overflow-style: none; /* Remove scrollbar for IE and Edge */
scrollbar-width: none; /* Remove scroll bar for Firefox */
}
::-webkit-scrollbar {
display: none;
}
body {
display: flex;
flex-direction: column;
block-size: 100dvh;
margin: 0;
}
#editor-component {
flex: 1 1 auto;
display: flex;
min-block-size: 0;
}
.sample-projects-bar {
flex: 0 0 auto;
display: flex;
align-items: center;
gap: 12px;
padding: 10px 12px;
border-bottom: 1px solid rgba(0, 0, 0, 0.12);
background: #fff;
color: #111;
font-size: 14px;
font-family: sans-serif;
line-height: 1.4;
}
.sample-projects-bar a {
color: #005fb8;
text-decoration: underline;
}
.editor-wc {
flex: 1 1 auto;
display: flex;
}
::part(editor-root) {
display: block;
flex: 1 1 auto;
max-block-size: 100dvh;
}
</style>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" href="data:," />
<title>Editor Web component</title>
</head>
<body>
<div class="sample-projects-bar" id="sample-projects-bar">
<span>Load Sample Projects:</span>
<a href="#" data-project="blank-html-starter">blank-html-starter</a>
<a href="#" data-project="cool-html">cool-html</a>
<a href="#" data-project="blank-python-starter">blank-python-starter</a>
<a href="#" data-project="cool-python">cool-python</a>
<a href="#" data-project="python-plotly">python-plotly</a>
<a href="#" data-project="cool-scratch">cool-scratch</a>
</div>
<div id="editor-component"></div>
<p id="results"></p>
<p id="project-identifier"></p>
<button id="run-button">Run code</button>
</body>
<script>
document.addEventListener("DOMContentLoaded", (e) => {
const queryParams = new URLSearchParams(window.location.search);
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 sampleBar = document.getElementById("sample-projects-bar");
const editorContainer = document.getElementById("editor-component");
const createWebComponent = (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(
"sidebar_options",
JSON.stringify([
"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);
queryParams.forEach((value, key) => {
newWebComp.setAttribute(key, value);
});
if (initialProject) {
newWebComp.setAttribute("initial_project", initialProject);
}
return newWebComp;
};
webComp = createWebComponent();
editorContainer.prepend(webComp);
const loadSampleProjectByName = async (projectName) => {
const url = new URL(`projects/${projectName}.json`, window.location.href);
const res = await fetch(url, { cache: "no-store" });
const project = await res.text();
webComp.parentNode.removeChild(webComp);
webComp = createWebComponent(project);
editorContainer.prepend(webComp);
};
sampleBar.addEventListener("click", (event) => {
const link = event.target.closest("a[data-project]");
if (!link || !sampleBar.contains(link)) return;
event.preventDefault();
loadSampleProjectByName(link.dataset.project);
});
document.getElementById('run-button').onclick = (event) => {
webComp.rerunCode();
};
});
</script>
</html>