-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSkulptRunner.jsx
More file actions
598 lines (543 loc) · 19.2 KB
/
SkulptRunner.jsx
File metadata and controls
598 lines (543 loc) · 19.2 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/* eslint-disable react-hooks/exhaustive-deps */
import "../../../../../assets/stylesheets/PythonRunner.scss";
import React, { useContext, useEffect, useRef, useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import { useTranslation } from "react-i18next";
import { Tab, Tabs, TabList, TabPanel } from "react-tabs";
import Sk from "skulpt";
import { useMediaQuery } from "react-responsive";
import classNames from "classnames";
import {
setError,
setErrorDetails,
codeRunHandled,
stopDraw,
setSenseHatEnabled,
triggerDraw,
setLoadedRunner,
} from "../../../../../redux/EditorSlice";
import ErrorMessage from "../../../ErrorMessage/ErrorMessage";
import ApiCallHandler from "../../../../../utils/apiCallHandler";
import store from "../../../../../redux/stores/WebComponentStore";
import VisualOutputPane from "../VisualOutputPane";
import OutputViewToggle from "../OutputViewToggle";
import { SettingsContext } from "../../../../../utils/settings";
import RunnerControls from "../../../../RunButton/RunnerControls";
import { MOBILE_MEDIA_QUERY } from "../../../../../utils/mediaQueryBreakpoints";
import { getPythonImports } from "../../../../../utils/getPythonImports";
const externalLibraries = {
"./pygal/__init__.js": {
path: `${process.env.ASSETS_URL}/shims/pygal/pygal.js`,
dependencies: [
"https://cdnjs.cloudflare.com/ajax/libs/highcharts/6.0.2/highcharts.js",
"https://cdnjs.cloudflare.com/ajax/libs/highcharts/6.0.2/js/highcharts-more.js",
],
},
"./py5/__init__.js": {
path: `${process.env.ASSETS_URL}/shims/processing/py5/py5-shim.js`,
dependencies: [`${process.env.ASSETS_URL}/libraries/processing/p5/p5.js`],
},
"./py5_imported/__init__.js": {
path: `${process.env.ASSETS_URL}/shims/processing/py5_imported_mode/py5_imported.js`,
},
"./py5_imported_mode.py": {
path: `${process.env.ASSETS_URL}/shims/processing/py5_imported_mode/py5_imported_mode.py`,
},
"./p5/__init__.js": {
path: `${process.env.ASSETS_URL}/shims/processing/p5/p5-shim.js`,
dependencies: [`${process.env.ASSETS_URL}/libraries/processing/p5/p5.js`],
},
"./_internal_sense_hat/__init__.js": {
path: `${process.env.ASSETS_URL}/shims/sense_hat/_internal_sense_hat.js`,
},
"./sense_hat.py": {
path: `${process.env.ASSETS_URL}/shims/sense_hat/sense_hat_blob.py`,
},
};
const VISUAL_LIBRARIES = [
"pygal",
"py5",
"py5_imported",
"p5",
"sense_hat",
"turtle",
];
const SkulptRunner = ({ active, outputPanels = ["text", "visual"] }) => {
const loadedRunner = useSelector((state) => state.editor.loadedRunner);
const projectCode = useSelector((state) => state.editor.project.components);
const mainComponent = projectCode?.find(
(component) => component.name === "main" && component.extension === "py",
);
const projectIdentifier = useSelector(
(state) => state.editor.project.identifier,
);
const user = useSelector((state) => state.auth.user);
const isSplitView = useSelector((state) => state.editor.isSplitView);
const isEmbedded = useSelector((state) => state.editor.isEmbedded);
const isOutputOnly = useSelector((state) => state.editor.isOutputOnly);
const codeRunTriggered = useSelector(
(state) => state.editor.codeRunTriggered,
);
const codeRunStopped = useSelector((state) => state.editor.codeRunStopped);
const drawTriggered = useSelector((state) => state.editor.drawTriggered);
const senseHatAlwaysEnabled = useSelector(
(state) => state.editor.senseHatAlwaysEnabled,
);
const reactAppApiEndpoint = useSelector((s) => s.editor.reactAppApiEndpoint);
const output = useRef();
const dispatch = useDispatch();
const { t } = useTranslation();
const settings = useContext(SettingsContext);
const isMobile = useMediaQuery({ query: MOBILE_MEDIA_QUERY });
const project = useSelector((state) => state.editor.project);
const testForVisualImports = (project) => {
for (const component of project.components || []) {
if (component.extension === "py") {
try {
const imports = getPythonImports(component.content, t);
const hasVisualImports = imports.some((name) =>
VISUAL_LIBRARIES.includes(name),
);
if (hasVisualImports) {
return true;
}
} catch (error) {
console.error("Error occurred while getting imports:", error);
}
}
}
return false;
};
const [codeHasVisualOutput, setCodeHasVisualOutput] = useState(
!!senseHatAlwaysEnabled || testForVisualImports(project),
);
const [showVisualOutput, setShowVisualOutput] = useState(codeHasVisualOutput);
const getInput = () => {
const pageInput = document.getElementById("input");
const webComponentInput = document.querySelector("editor-wc")
? document.querySelector("editor-wc").shadowRoot.getElementById("input")
: null;
return pageInput || webComponentInput;
};
useEffect(() => {
if (!codeRunTriggered) {
setCodeHasVisualOutput(
!!senseHatAlwaysEnabled || testForVisualImports(project),
);
}
}, [project, codeRunTriggered, senseHatAlwaysEnabled, t]);
useEffect(() => {
if (active && loadedRunner !== "skulpt") {
dispatch(setLoadedRunner("skulpt"));
}
}, [active]);
useEffect(() => {
if (active) {
if (codeRunTriggered) {
runCode();
}
}
}, [codeRunTriggered, active]);
useEffect(() => {
if (codeRunTriggered && !senseHatAlwaysEnabled) {
setShowVisualOutput(!!codeHasVisualOutput);
}
}, [codeRunTriggered, codeHasVisualOutput]);
useEffect(() => {
if (codeRunStopped && active && getInput()) {
const input = getInput();
input.removeAttribute("id");
input.removeAttribute("contentEditable");
dispatch(setError(t("output.errors.interrupted")));
dispatch(codeRunHandled());
}
}, [codeRunStopped]);
useEffect(() => {
if (!codeRunTriggered && !drawTriggered && active) {
if (getInput()) {
const input = getInput();
input.removeAttribute("id");
input.removeAttribute("contentEditable");
}
}
}, [drawTriggered, codeRunTriggered]);
const outf = (text) => {
if (text !== "") {
const node = output.current;
if (node) {
const div = document.createElement("span");
div.classList.add("pythonrunner-console-output-line");
div.innerHTML = new Option(text).innerHTML;
node.appendChild(div);
node.scrollTop = node.scrollHeight;
}
}
};
const builtinRead = (library) => {
if (library === "./_internal_sense_hat/__init__.js") {
dispatch(setSenseHatEnabled(true));
}
if (library === "./p5/__init__.js" || library === "./py5/__init__.js") {
dispatch(triggerDraw());
}
// TODO: Handle pre-importing py5_imported when refactored py5 shim imported
let localProjectFiles = projectCode
.filter((component) => component.name !== "main")
.map((component) => `./${component.name}.py`);
if (localProjectFiles.includes(library)) {
let filename = library.slice(2, -3);
let component = projectCode.find((x) => x.name === filename);
if (component) {
return component.content;
}
}
if (
Sk.builtinFiles !== undefined &&
Sk.builtinFiles["files"][library] !== undefined
) {
return Sk.builtinFiles["files"][library];
}
if (externalLibraries[library]) {
var externalLibraryInfo = externalLibraries[library];
return (
externalLibraries[library].code ||
Sk.misceval.promiseToSuspension(
fetch(externalLibraryInfo.path)
.then((response) => response.text())
.then((code) => {
if (!code) {
throw new Sk.builtin.ImportError(
"Failed to load remote module",
);
}
externalLibraries[library].code = code;
var promise;
function mapUrlToPromise(path) {
// If the script is already in the DOM don't add it again.
const existingScriptElement = document.querySelector(
`script[src="${path}"]`,
);
if (!existingScriptElement) {
return new Promise(function (resolve, _reject) {
let scriptElement = document.createElement("script");
scriptElement.type = "text/javascript";
scriptElement.src = path;
scriptElement.async = true;
scriptElement.crossOrigin = "";
scriptElement.onload = function () {
resolve(true);
};
document.body.appendChild(scriptElement);
});
}
}
if (externalLibraryInfo.loadDepsSynchronously) {
promise = (externalLibraryInfo.dependencies || []).reduce(
(p, url) => {
return p.then(() => mapUrlToPromise(url));
},
Promise.resolve(),
); // initial
} else {
promise = Promise.all(
(externalLibraryInfo.dependencies || []).map(mapUrlToPromise),
);
}
return promise
.then(function () {
return code;
})
.catch(function () {
throw new Sk.builtin.ImportError(
"Failed to load dependencies required",
);
});
}),
)
);
}
throw new Error("File not found: '" + library + "'");
};
const inputSpan = () => {
const span = document.createElement("span");
span.setAttribute("id", "input");
span.setAttribute("spellCheck", "false");
span.setAttribute("class", "pythonrunner-input");
span.setAttribute("contentEditable", "true");
return span;
};
const inf = function () {
if (Sk.sense_hat) {
Sk.sense_hat.mz_criteria.noInputEvents = false;
}
const outputPane = output.current;
outputPane.appendChild(inputSpan());
const input = getInput();
input.focus();
return new Promise(function (resolve, reject) {
input.addEventListener("keydown", function removeInput(e) {
if (e.key === "Enter") {
input.removeEventListener(e.type, removeInput);
// resolve the promise with the value of the input field
const answer = input.innerText;
input.removeAttribute("id");
input.removeAttribute("contentEditable");
input.innerText = answer + "\n";
document.addEventListener("keyup", function storeInput(e) {
if (e.key === "Enter") {
document.removeEventListener(e.type, storeInput);
resolve(answer);
}
});
}
});
});
};
const handleError = (err) => {
let errorDetails = {};
let errorMessage;
let explanation;
if (err.message === t("output.errors.interrupted")) {
errorMessage = err.message;
errorDetails = {
type: "Interrupted",
message: errorMessage,
};
} else {
const errorDescription = (err.tp$str && err.tp$str().v)
.replace(/\[(.*?)\]/, "")
.replace(/\.$/, "");
const errorType = err.tp$name || err.constructor.name;
// If this is an error in the sense_hat.py shim, remove the first line of
// the traceback as this will be the line in the shim which we don't want
// to show to users, so that the error message will instead point to the
// line in the user's code which caused the error.
if (
err.traceback.length > 1 &&
err.traceback[0].filename === "./sense_hat.py"
) {
err.traceback.shift();
}
const lineNumber = err.traceback[0].lineno;
const fileName = err.traceback[0].filename.replace(/^\.\//, "");
if (errorType === "ImportError" && window.crossOriginIsolated) {
const articleLink = `https://help.editor.raspberrypi.org/hc/en-us/articles/30841379339924-What-Python-libraries-are-available-in-the-Code-Editor`;
const moduleName = errorDescription.replace(/No module named /, "");
explanation = `You should check your code for typos. If you are using p5, py5, sense_hat or turtle, ${moduleName} might not work - read this <a href=${articleLink}>article</a> for more information.`;
}
let userId;
if (user?.profile) {
userId = user.profile?.user;
}
const { createError } = ApiCallHandler({ reactAppApiEndpoint });
errorMessage = `${errorType}: ${errorDescription} on line ${lineNumber} of ${fileName}${
explanation ? `. ${explanation}` : ""
}`;
createError(projectIdentifier, userId, {
errorType,
errorMessage,
});
errorDetails = {
type: errorType,
line: lineNumber,
file: fileName,
description: errorDescription,
message: errorMessage,
};
}
dispatch(setError(errorMessage));
dispatch(setErrorDetails(errorDetails));
dispatch(stopDraw());
if (getInput()) {
const input = getInput();
input.removeAttribute("id");
input.removeAttribute("contentEditable");
}
};
const runCode = () => {
// clear previous output
dispatch(setError(""));
dispatch(setErrorDetails({}));
if (output.current) {
output.current.innerHTML = "";
}
dispatch(setSenseHatEnabled(false));
var prog = mainComponent?.content || "";
if (prog.includes(`# ${t("input.comment.py5")}`)) {
prog = prog.replace(
`# ${t("input.comment.py5")}`,
"from py5_imported_mode import *",
);
if (!prog.match(/(\nrun_sketch)/)) {
prog = prog.concat("\nrun_sketch()");
}
}
Sk.configure({
inputfun: inf,
output: outf,
read: builtinRead,
debugging: true,
inputTakesPrompt: true,
uncaughtException: handleError,
});
var myPromise = Sk.misceval
.asyncToPromise(() => Sk.importMainWithBody("main", false, prog, true), {
"*": () => {
if (store.getState().editor.codeRunStopped) {
throw new Error(t("output.errors.interrupted"));
}
},
})
.catch((err) => {
handleError(err);
})
.finally(() => {
dispatch(codeRunHandled());
});
myPromise.then(function (_mod) {});
};
function shiftFocusToInput(e) {
if (document.getSelection().toString().length > 0) {
return;
}
const inputBox = getInput();
if (inputBox && e.target !== inputBox) {
const input = getInput();
const selection = window.getSelection();
selection.removeAllRanges();
if (input.innerText && input.innerText.length > 0) {
const range = document.createRange();
range.setStart(input, 1);
range.collapse(true);
selection.addRange(range);
}
input.focus();
}
}
const singleOutputPanel = outputPanels.length === 1;
const showVisualOutputPanel = outputPanels.includes("visual");
const showTextOutputPanel = outputPanels.includes("text");
const outputPanelClasses = (panelType) => {
return classNames("output-panel", `output-panel--${panelType}`, {
"output-panel--single": singleOutputPanel,
});
};
const [tabbedViewSelectedIndex, setTabbedViewSelectedIndex] = useState(
showVisualOutput ? 0 : 1,
);
useEffect(() => {
if (showVisualOutput) {
setTabbedViewSelectedIndex(0);
} else {
setTabbedViewSelectedIndex(1);
}
}, [showVisualOutput]);
return (
<div
className={classNames("pythonrunner-container", "skulptrunner", {
"skulptrunner--active": active,
})}
>
{isSplitView || singleOutputPanel ? (
<>
{showVisualOutputPanel && (
<div
className={outputPanelClasses("visual")}
style={{ display: showVisualOutput ? undefined : "none" }}
>
<Tabs forceRenderTabPanel={true}>
<div
className={classNames("react-tabs__tab-container", {
"react-tabs__tab-container--hidden": singleOutputPanel,
})}
>
<TabList>
<Tab key={0}>
<span className="react-tabs__tab-text">
{t("output.visualOutput")}
</span>
</Tab>
</TabList>
{!isEmbedded && showVisualOutput && <OutputViewToggle />}
{!isEmbedded && isMobile && <RunnerControls skinny />}
</div>
<TabPanel key={0}>
<VisualOutputPane />
</TabPanel>
</Tabs>
</div>
)}
{showTextOutputPanel && (
<div className={outputPanelClasses("text")}>
<Tabs forceRenderTabPanel={true}>
<div
className={classNames("react-tabs__tab-container", {
"react-tabs__tab-container--hidden": singleOutputPanel,
})}
>
<TabList>
<Tab key={0}>
<span className="react-tabs__tab-text">
{t("output.textOutput")}
</span>
</Tab>
</TabList>
{!showVisualOutput && !isEmbedded && isMobile && (
<RunnerControls skinny />
)}
</div>
<ErrorMessage />
<TabPanel key={0}>
<pre
className={`pythonrunner-console pythonrunner-console--${settings.fontSize}`}
onClick={shiftFocusToInput}
ref={output}
></pre>
</TabPanel>
</Tabs>
</div>
)}
</>
) : (
<Tabs
forceRenderTabPanel={true}
defaultIndex={showVisualOutput ? 0 : 1}
selectedIndex={tabbedViewSelectedIndex}
onSelect={setTabbedViewSelectedIndex}
>
<div className="react-tabs__tab-container">
<TabList>
<Tab
key={0}
style={{ display: showVisualOutput ? undefined : "none" }}
>
<span className="react-tabs__tab-text">
{t("output.visualOutput")}
</span>
</Tab>
<Tab key={1}>
<span className="react-tabs__tab-text">
{t("output.textOutput")}
</span>
</Tab>
</TabList>
{!isEmbedded && showVisualOutput && <OutputViewToggle />}
{!isEmbedded && isMobile && <RunnerControls skinny />}
</div>
{!isOutputOnly && <ErrorMessage />}
<TabPanel key={0}>
<VisualOutputPane />
</TabPanel>
<TabPanel key={1}>
<pre
className={`pythonrunner-console pythonrunner-console--${settings.fontSize}`}
onClick={shiftFocusToInput}
ref={output}
></pre>
</TabPanel>
</Tabs>
)}
</div>
);
};
export default SkulptRunner;