forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial.js
More file actions
199 lines (199 loc) · 5.97 KB
/
tutorial.js
File metadata and controls
199 lines (199 loc) · 5.97 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
var STORAGE_KEY = "SAVED_TUTORIAL";
var ENDPOINT_KEY = "SAVED_ENDPOINT";
var existing = localStorage.getItem(STORAGE_KEY);
var editor = monaco.editor.create(document.getElementById("container"), {
value: existing ||
"# My Tutorial\n\n## Step 1\n\nHere is some text.\n\n## Step 2\n\nCongratulations, you did it!\n ",
language: "markdown"
});
var targets = [
{
name: "Minecraft",
id: "minecraft",
endpoints: [
{
name: "nether",
url: "https://minecraft.makecode.com/beta?ipc=1&inGame=1&nether=1&controller=1"
},
{
name: "beta",
url: "https://minecraft.makecode.com/beta?ipc=1&inGame=1&controller=1"
},
{
name: "released",
url: "https://minecraft.makecode.com?ipc=1&inGame=1&controller=1"
}
]
}, {
name: "Arcade",
id: "arcade",
endpoints: [
{
name: "beta",
url: "https://arcade.makecode.com/beta?controller=1"
},
{
name: "released",
url: "https://arcade.makecode.com?controller=1"
}
]
}, {
name: "Adafruit",
id: "adafruit",
endpoints: [
{
name: "beta",
url: "https://makecode.adafruit.com/beta?controller=1"
},
{
name: "released",
url: "https://makecode.adafruit.com?controller=1"
}
]
}, {
name: "Micro:bit",
id: "microbit",
endpoints: [
{
name: "beta",
url: "https://makecode.microbit.org/beta?controller=1"
} /*,
{
name: "released",
url: "https://makecode.microbit.org?controller=1"
}*/
]
}/* not supported
, {
name: "LEGO EV3",
id: "ev3",
endpoints: [
{
name: "beta",
url: "https://makecode.mindstorms.com/beta?controller=1"
},
{
name: "released",
url: "https://makecode.mindstorms.com?controller=1"
}
]
} */
];
var selectedEndpoint;
var selectedId;
editor.onDidChangeModelContent(debounce(function () {
localStorage.setItem(STORAGE_KEY, editor.getValue());
}, 500));
var iframe = document.createElement("iframe");
document.getElementById("makecode-editor").appendChild(iframe);
loadIframe(localStorage.getItem(ENDPOINT_KEY));
initDropdown();
document.getElementById("run-button").addEventListener("click", function () {
var md = editor.getValue();
sendMessage("importtutorial", md);
});
window.addEventListener("message", receiveMessage, false);
var pendingMsgs = {};
function sendMessage(action, md) {
console.log('send ' + action);
var msg = {
type: "pxteditor",
id: Math.random().toString(),
action: action
};
if (action == 'importtutorial') {
msg.markdown = md;
msg.response = true;
}
else if (action == 'renderblocks') {
msg.response = true;
msg.ts = 'while(true){ }';
}
else if (action == 'toggletrace') {
msg.intervalSpeed = 1000;
}
else if (action == 'settracestate') {
msg.enabled = true;
}
if (msg.response)
pendingMsgs[msg.id] = msg;
iframe.contentWindow.postMessage(msg, "*");
}
function receiveMessage(ev) {
var msg = ev.data;
console.log('received...');
console.log(msg);
if (msg.resp)
console.log(JSON.stringify(msg.resp, null, 2));
if (msg.type == "pxthost") {
if (msg.action == "workspacesync") {
// no project
msg.projects = [];
iframe.contentWindow.postMessage(msg, "*");
return;
}
else if (msg.action == "workspacesave") {
console.log(JSON.stringify(msg.project, null, 2));
}
}
if (msg.type == "pxteditor") {
var req = pendingMsgs[msg.id];
if (req.action == "renderblocks") {
var img = document.createElement("img");
img.src = msg.resp;
}
}
delete pendingMsgs[msg.id];
}
function debounce(func, wait, immediate) {
var timeout;
return function () {
var context = this;
var args = arguments;
var later = function () {
timeout = null;
if (!immediate)
func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow)
func.apply(context, args);
return timeout;
};
}
function initDropdown() {
var s = document.getElementById("endpoint-select");
for (var _i = 0, targets_1 = targets; _i < targets_1.length; _i++) {
var target = targets_1[_i];
for (var _a = 0, _b = target.endpoints; _a < _b.length; _a++) {
var endpoint = _b[_a];
var opt = document.createElement("option");
opt.value = target.name + "-" + endpoint.name;
opt.innerText = target.name + " " + endpoint.name;
s.appendChild(opt);
}
}
s.addEventListener("change", function (ev) {
loadIframe(ev.target.value);
});
}
function loadIframe(selected) {
if (selected === selectedEndpoint)
return;
for (var _i = 0, targets_2 = targets; _i < targets_2.length; _i++) {
var target = targets_2[_i];
for (var _a = 0, _b = target.endpoints; _a < _b.length; _a++) {
var endpoint = _b[_a];
if (!selected || selected === target.name + "-" + endpoint.name) {
iframe.setAttribute("src", endpoint.url);
selectedEndpoint = target.name + "-" + endpoint.name;
selectedId = target.id;
return;
}
}
}
// Load first target
loadIframe(null);
}