-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathScratchIntegrationHOC.jsx
More file actions
145 lines (136 loc) · 4.15 KB
/
Copy pathScratchIntegrationHOC.jsx
File metadata and controls
145 lines (136 loc) · 4.15 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
import React from "react";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import { saveAs } from "file-saver";
import {
remixProject,
manualUpdateProject,
setStageSize,
} from "@RaspberryPiFoundation/scratch-gui";
import { allowedIframeHost } from "../../utils/iframeUtils";
import { postScratchGuiEvent } from "./events.js";
const ScratchIntegrationHOC = function (WrappedComponent) {
class ScratchIntegrationComponent extends React.Component {
constructor(props) {
super(props);
this.handleMessage = this.handleMessage.bind(this);
this.handleDownload = this.handleDownload.bind(this);
this.handleUpload = this.handleUpload.bind(this);
this.handleRemix = this.handleRemix.bind(this);
this.handleSave = this.handleSave.bind(this);
this.handleProjectChanged = this.handleProjectChanged.bind(this);
this.handleProjectRunStart = this.handleProjectRunStart.bind(this);
}
componentDidMount() {
window.addEventListener("message", this.handleMessage);
this.props.vm.on("PROJECT_CHANGED", this.handleProjectChanged);
this.props.vm.on("PROJECT_RUN_START", this.handleProjectRunStart);
this.props.setStageSize();
}
componentWillUnmount() {
window.removeEventListener("message", this.handleMessage);
this.props.vm.removeListener(
"PROJECT_CHANGED",
this.handleProjectChanged,
);
this.props.vm.removeListener(
"PROJECT_RUN_START",
this.handleProjectRunStart,
);
}
handleMessage(event) {
if (!allowedIframeHost(event.origin)) {
console.warn(
"iFrame received message from unknown origin:",
event.origin,
);
return;
}
if (event.data?.type === "webpackOk") {
return;
}
switch (event.data?.type) {
case "scratch-gui-download":
this.handleDownload(event);
break;
case "scratch-gui-upload":
this.handleUpload(event);
break;
case "scratch-gui-remix":
this.handleRemix(event);
break;
case "scratch-gui-save":
this.handleSave(event);
break;
case "scratch-gui-update-token":
// handled elsewhere
break;
default:
console.warn("Unknown message type:", event.data.type);
break;
}
}
handleDownload(event) {
const filename = event.data.filename;
this.props.saveProjectSb3().then((content) => {
saveAs(content, filename);
});
}
handleUpload(event) {
const file = event.data.file;
file
?.arrayBuffer()
?.then((arrayBuffer) => this.props.loadProject(arrayBuffer))
?.then(this.handleProjectChanged);
}
handleRemix() {
this.props.onClickRemix();
}
handleSave() {
this.props.onClickSave();
}
handleProjectChanged() {
postScratchGuiEvent("scratch-gui-project-changed");
}
handleProjectRunStart() {
postScratchGuiEvent("scratch-gui-project-run-started");
}
render() {
const {
loadProject,
localesOnly,
onClickRemix,
onClickSave,
saveProjectSb3,
setStageSize,
...componentProps
} = this.props;
return <WrappedComponent {...componentProps} />;
}
}
const mapStateToProps = (state) => ({
saveProjectSb3: state.scratchGui.vm.saveProjectSb3.bind(
state.scratchGui.vm,
),
loadProject: state.scratchGui.vm.loadProject.bind(state.scratchGui.vm),
vm: state.scratchGui.vm,
});
const mapDispatchToProps = (dispatch) => ({
onClickRemix: () => dispatch(remixProject()),
onClickSave: () => dispatch(manualUpdateProject()),
setStageSize: () => dispatch(setStageSize("small")),
});
ScratchIntegrationComponent.propTypes = {
saveProjectSb3: PropTypes.func,
loadProject: PropTypes.func,
onClickRemix: PropTypes.func,
onClickSave: PropTypes.func,
setStageSize: PropTypes.func,
vm: PropTypes.object,
};
return connect(
mapStateToProps,
mapDispatchToProps,
)(ScratchIntegrationComponent);
};
export { ScratchIntegrationHOC as default };