-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathscratch.jsx
More file actions
76 lines (63 loc) · 2.33 KB
/
Copy pathscratch.jsx
File metadata and controls
76 lines (63 loc) · 2.33 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
import React from "react";
import { createRoot } from "react-dom/client";
import { compose } from "redux";
import process from "process";
import GUI, { AppStateHOC } from "@scratch/scratch-gui";
import ScratchIntegrationHOC from "./components/ScratchEditor/ScratchIntegrationHOC.jsx";
import ScratchStyles from "./assets/stylesheets/Scratch.scss";
const appTarget = document.getElementById("app");
document.getElementById("scratch-loading")?.remove();
GUI.setAppElement(appTarget);
const WrappedGui = compose(AppStateHOC, ScratchIntegrationHOC)(GUI);
if (process.env.NODE_ENV === "production" && typeof window === "object") {
// Warn before navigating away
window.onbeforeunload = () => true;
}
const searchParams = new URLSearchParams(window.location.search);
const projectId = searchParams.get("project_id");
const apiUrl = searchParams.get("api_url");
const defaultLocale = "en";
const locale = appTarget.dataset.locale || defaultLocale;
const handleUpdateProjectId = (projectId) => {
window.top.postMessage(
{ type: "scratch-gui-project-id-updated", projectId: projectId },
"*",
);
};
const handleRemixingStarted = () => {
window.top.postMessage({ type: "scratch-gui-remixing-started" }, "*");
};
const handleRemixingSucceeded = () => {
window.top.postMessage({ type: "scratch-gui-remixing-succeeded" }, "*");
};
const handleSavingStarted = () => {
window.top.postMessage({ type: "scratch-gui-saving-started" }, "*");
};
const handleSavingSucceeded = () => {
window.top.postMessage({ type: "scratch-gui-saving-succeeded" }, "*");
};
if (!projectId) {
console.error("project_id is required but not set");
} else if (!apiUrl) {
console.error("api_url is required but not set");
} else {
const root = createRoot(appTarget);
root.render(
<>
<style>{ScratchStyles}</style>
<WrappedGui
projectId={projectId}
locale={locale}
menuBarHidden={true}
projectHost={`${apiUrl}/api/scratch/projects`}
assetHost={`${apiUrl}/api/scratch/assets`}
basePath={`${process.env.ASSETS_URL}/scratch-gui/`}
onUpdateProjectId={handleUpdateProjectId}
onShowCreatingRemixAlert={handleRemixingStarted}
onShowRemixSuccessAlert={handleRemixingSucceeded}
onShowSavingAlert={handleSavingStarted}
onShowSaveSuccessAlert={handleSavingSucceeded}
/>
</>,
);
}