-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathScratchContainer.jsx
More file actions
155 lines (136 loc) · 4.71 KB
/
Copy pathScratchContainer.jsx
File metadata and controls
155 lines (136 loc) · 4.71 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
import React, { useEffect, useRef, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { ClickScrollPlugin, OverlayScrollbars } from "overlayscrollbars";
import { OverlayScrollbarsComponent } from "overlayscrollbars-react";
import { applyScratchProjectIdentifierUpdate } from "../../../redux/EditorSlice";
import { runStartedEvent } from "../../../events/WebComponentCustomEvents";
import {
subscribeToScratchProjectIdentifierUpdates,
postMessageToScratchIframe,
getScratchAllowedOrigin,
} from "../../../utils/scratchIframe";
const SCRATCH_MIN_WIDTH = 1024;
const SCRATCH_SCROLLBAR_OPTIONS = {
overflow: {
x: "scroll",
y: "hidden",
},
scrollbars: {
theme: "os-theme-scratch",
visibility: "auto",
clickScroll: "instant",
},
};
OverlayScrollbars.plugin(ClickScrollPlugin);
export default function ScratchContainer() {
const dispatch = useDispatch();
const projectIdentifier = useSelector(
(state) => state.editor.project.identifier,
);
const scratchIframeProjectIdentifier = useSelector(
(state) => state.editor.scratchIframeProjectIdentifier,
);
const scratchApiEndpoint = useSelector(
(state) => state.editor.scratchApiEndpoint,
);
const accessToken = useSelector((state) => state.auth?.user?.access_token);
const [initialAccessToken] = useState(accessToken);
const iframeProjectIdentifier =
scratchIframeProjectIdentifier || projectIdentifier;
const lastScratchTokenStateRef = useRef({
nonce: null,
hadAccessToken: false,
});
useEffect(() => {
return subscribeToScratchProjectIdentifierUpdates(
(nextProjectIdentifier) => {
dispatch(
applyScratchProjectIdentifierUpdate({
projectIdentifier: nextProjectIdentifier,
}),
);
},
);
}, [dispatch]);
useEffect(() => {
if (accessToken === initialAccessToken) return;
postMessageToScratchIframe({
type: "scratch-gui-update-token",
accessToken: accessToken,
});
}, [accessToken, initialAccessToken]);
useEffect(() => {
const allowedOrigin = getScratchAllowedOrigin();
const handleScratchRunStarted = (event) => {
if (event.origin !== allowedOrigin) return;
if (event.data?.type !== "scratch-gui-project-run-started") return;
document.dispatchEvent(runStartedEvent({}));
};
window.addEventListener("message", handleScratchRunStarted);
return () => {
window.removeEventListener("message", handleScratchRunStarted);
};
}, []);
useEffect(() => {
const allowedOrigin = getScratchAllowedOrigin();
const authKey = localStorage.getItem("authKey");
const requiresAuth = Boolean(
authKey && authKey !== "undefined" && authKey !== "null",
);
const handleScratchMessage = (event) => {
if (event.origin !== allowedOrigin) return;
if (event.data?.type !== "scratch-gui-ready") return;
if (!event.data?.nonce) return;
const hasAccessToken = Boolean(accessToken);
const previousHandshake = lastScratchTokenStateRef.current;
const isSameNonce = previousHandshake.nonce === event.data.nonce;
const shouldSkipDuplicateNonce =
isSameNonce && (previousHandshake.hadAccessToken || !hasAccessToken);
if (shouldSkipDuplicateNonce) return;
lastScratchTokenStateRef.current = {
nonce: event.data.nonce,
hadAccessToken: hasAccessToken,
};
postMessageToScratchIframe({
type: "scratch-gui-set-token",
nonce: event.data.nonce,
accessToken: accessToken || null,
requiresAuth,
});
};
window.addEventListener("message", handleScratchMessage);
return () => {
window.removeEventListener("message", handleScratchMessage);
};
}, [accessToken]);
const queryParams = new URLSearchParams();
queryParams.set("project_id", iframeProjectIdentifier);
queryParams.set("api_url", scratchApiEndpoint);
queryParams.set("scratchMetadata", "1");
queryParams.set("parent_origin", window.location.origin);
const iframeSrcUrl = `${
process.env.ASSETS_URL
}/scratch.html?${queryParams.toString()}`;
return (
<div className="scratch-container" data-testid="scratch-container">
<OverlayScrollbarsComponent
className="scratch-container__viewport"
data-testid="scratch-container-viewport"
options={SCRATCH_SCROLLBAR_OPTIONS}
>
<iframe
className="scratch-container__iframe"
src={iframeSrcUrl}
title={"Scratch"}
allow="camera; microphone"
style={{
width: "100%",
minWidth: `${SCRATCH_MIN_WIDTH}px`,
border: 0,
display: "block",
}}
/>
</OverlayScrollbarsComponent>
</div>
);
}