ScratchDesktopGUIHOC.jsx
11.5 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
import {ipcRenderer} from 'electron';
import {dialog} from '@electron/remote';
import * as remote from '@electron/remote/renderer';
import bindAll from 'lodash.bindall';
import omit from 'lodash.omit';
import PropTypes from 'prop-types';
import React from 'react';
import {connect} from 'react-redux';
import GUIComponent from 'openblock-gui/src/components/gui/gui.jsx';
import {FormattedMessage} from 'react-intl';
import {
LoadingStates,
onFetchedProjectData,
onLoadedProject,
defaultProjectId,
requestNewProject,
requestProjectUpload,
setProjectId
} from 'openblock-gui/src/reducers/project-state';
import {
openLoadingProject,
closeLoadingProject,
openTelemetryModal,
openUpdateModal
} from 'openblock-gui/src/reducers/modals';
import {setUpdate} from 'openblock-gui/src/reducers/update';
import analytics, {initialAnalytics} from 'openblock-gui/src/lib/analytics';
import MessageBoxType from 'openblock-gui/src/lib/message-box.js';
import ElectronStorageHelper from '../common/ElectronStorageHelper';
import showPrivacyPolicy from './showPrivacyPolicy';
/**
* Higher-order component to add desktop logic to the GUI.
* @param {Component} WrappedComponent - a GUI-like component to wrap.
* @returns {Component} - a component similar to GUI with desktop-specific logic added.
*/
const ScratchDesktopGUIHOC = function (WrappedComponent) {
class ScratchDesktopGUIComponent extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'handleProjectTelemetryEvent',
'handleSetTitleFromSave',
'handleShowMessageBox',
'handleStorageInit',
'handleUpdateProjectTitle'
]);
this.props.onLoadingStarted();
ipcRenderer.invoke('get-initial-project-data').then(initialProjectData => {
const hasInitialProject = initialProjectData && (initialProjectData.length > 0);
this.props.onHasInitialProject(hasInitialProject, this.props.loadingState);
if (!hasInitialProject) {
this.props.onLoadingCompleted();
ipcRenderer.send('loading-completed');
return;
}
this.props.vm.loadProject(initialProjectData).then(
() => {
this.props.onLoadingCompleted();
ipcRenderer.send('loading-completed');
this.props.onLoadedProject(this.props.loadingState, true);
},
e => {
this.props.onLoadingCompleted();
ipcRenderer.send('loading-completed');
this.props.onLoadedProject(this.props.loadingState, false);
dialog.showMessageBox(remote.getCurrentWindow(), {
type: 'error',
title: 'Failed to load project',
message: 'Invalid or corrupt project file.',
detail: e.message
});
// this effectively sets the default project ID
// TODO: maybe setting the default project ID should be implicit in `requestNewProject`
this.props.onHasInitialProject(false, this.props.loadingState);
// restart as if we didn't have an initial project to load
this.props.onRequestNewProject();
}
);
});
ipcRenderer.send('set-locale', this.props.locale);
}
componentDidMount () {
ipcRenderer.on('setTitleFromSave', this.handleSetTitleFromSave);
ipcRenderer.on('setUpdate', (event, args) => {
this.props.onSetUpdate(args);
});
ipcRenderer.on('setUserId', (event, args) => {
initialAnalytics(args);
// Register "base" page view
analytics.send({hitType: 'pageview', page: '/community/electron'});
});
ipcRenderer.on('setPlatform', (event, args) => {
this.platform = args;
});
}
componentWillUnmount () {
ipcRenderer.removeListener('setTitleFromSave', this.handleSetTitleFromSave);
}
handleClickAbout () {
ipcRenderer.send('open-about-window');
}
handleClickLicense () {
ipcRenderer.send('open-license-window');
}
handleClickCheckUpdate () {
ipcRenderer.send('reqeustCheckUpdate');
}
handleClickUpdate () {
ipcRenderer.send('reqeustUpdate');
}
handleAbortUpdate () {
ipcRenderer.send('abortUpdate');
}
handleClickClearCache () {
ipcRenderer.send('clearCache');
}
handleClickInstallDriver () {
ipcRenderer.send('installDriver');
}
handleProjectTelemetryEvent (event, metadata) {
ipcRenderer.send(event, metadata);
}
handleSetTitleFromSave (event, args) {
this.handleUpdateProjectTitle(args.title);
}
handleStorageInit (storageInstance) {
storageInstance.addHelper(new ElectronStorageHelper(storageInstance));
}
handleUpdateProjectTitle (newTitle) {
this.setState({projectTitle: newTitle});
}
handleShowMessageBox (type, message) {
/**
* To avoid the electron bug: the input-box lose focus after call alert or confirm on windows platform.
* https://github.com/electron/electron/issues/19977
*/
if (this.platform === 'win32') {
let options;
if (type === MessageBoxType.confirm) {
options = {
type: 'warning',
buttons: ['Ok', 'Cancel'],
message: message
};
} else if (type === MessageBoxType.alert) {
options = {
type: 'error',
message: message
};
}
const result = dialog.showMessageBoxSync(remote.getCurrentWindow(), options);
if (result === 0) {
return true;
}
return false;
}
if (type === 'confirm') {
return confirm(message); // eslint-disable-line no-alert
}
return alert(message); // eslint-disable-line no-alert
}
render () {
const childProps = omit(this.props, Object.keys(ScratchDesktopGUIComponent.propTypes));
return (<WrappedComponent
canEditTitle
canModifyCloudData={false}
canSave={false}
isScratchDesktop
onClickAbout={[
{
title: (<FormattedMessage
defaultMessage="About"
description="Menu bar item for about"
id="gui.desktopMenuBar.about"
/>),
onClick: () => this.handleClickAbout()
},
{
title: (<FormattedMessage
defaultMessage="License"
description="Menu bar item for license"
id="gui.desktopMenuBar.license"
/>),
onClick: () => this.handleClickLicense()
},
{
title: (<FormattedMessage
defaultMessage="Privacy policy"
description="Menu bar item for privacy policy"
id="gui.menuBar.privacyPolicy"
/>),
onClick: () => showPrivacyPolicy()
},
{
title: (<FormattedMessage
defaultMessage="Data settings"
description="Menu bar item for data settings"
id="gui.menuBar.dataSettings"
/>),
onClick: () => this.props.onTelemetrySettingsClicked()
}
]}
onClickLogo={this.handleClickLogo}
onClickCheckUpdate={this.handleClickCheckUpdate}
onClickUpdate={this.handleClickUpdate}
onAbortUpdate={this.handleAbortUpdate}
onClickInstallDriver={this.handleClickInstallDriver}
onClickClearCache={this.handleClickClearCache}
onProjectTelemetryEvent={this.handleProjectTelemetryEvent}
onShowMessageBox={this.handleShowMessageBox}
onShowPrivacyPolicy={showPrivacyPolicy}
onStorageInit={this.handleStorageInit}
onUpdateProjectTitle={this.handleUpdateProjectTitle}
// allow passed-in props to override any of the above
{...childProps}
/>);
}
}
ScratchDesktopGUIComponent.propTypes = {
loadingState: PropTypes.oneOf(LoadingStates),
locale: PropTypes.string.isRequired,
onFetchedInitialProjectData: PropTypes.func,
onHasInitialProject: PropTypes.func,
onLoadedProject: PropTypes.func,
onLoadingCompleted: PropTypes.func,
onLoadingStarted: PropTypes.func,
onRequestNewProject: PropTypes.func,
onTelemetrySettingsClicked: PropTypes.func,
onSetUpdate: PropTypes.func,
// using PropTypes.instanceOf(VM) here will cause prop type warnings due to VM mismatch
vm: GUIComponent.WrappedComponent.propTypes.vm
};
const mapStateToProps = state => {
const loadingState = state.scratchGui.projectState.loadingState;
return {
loadingState: loadingState,
locale: state.locales.locale,
vm: state.scratchGui.vm
};
};
const mapDispatchToProps = dispatch => ({
onLoadingStarted: () => dispatch(openLoadingProject()),
onLoadingCompleted: () => dispatch(closeLoadingProject()),
onHasInitialProject: (hasInitialProject, loadingState) => {
if (hasInitialProject) {
// emulate sb-file-uploader
return dispatch(requestProjectUpload(loadingState));
}
// `createProject()` might seem more appropriate but it's not a valid state transition here
// setting the default project ID is a valid transition from NOT_LOADED and acts like "create new"
return dispatch(setProjectId(defaultProjectId));
},
onFetchedInitialProjectData: (projectData, loadingState) =>
dispatch(onFetchedProjectData(projectData, loadingState)),
onLoadedProject: (loadingState, loadSuccess) => {
const canSaveToServer = false;
return dispatch(onLoadedProject(loadingState, canSaveToServer, loadSuccess));
},
onRequestNewProject: () => dispatch(requestNewProject(false)),
onSetUpdate: arg => {
dispatch(setUpdate(arg));
dispatch(openUpdateModal());
},
onTelemetrySettingsClicked: () => dispatch(openTelemetryModal())
});
return connect(mapStateToProps, mapDispatchToProps)(ScratchDesktopGUIComponent);
};
export default ScratchDesktopGUIHOC;