-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathEditor.js
More file actions
336 lines (303 loc) · 11.9 KB
/
Editor.js
File metadata and controls
336 lines (303 loc) · 11.9 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
import React, { Component } from "react";
import AceEditor from "react-ace";
import "brace/mode/javascript";
import "brace/theme/github";
import "brace/ext/searchbox";
import "brace/ext/language_tools";
import customCompleter from "./customCompleter.js";
import KeyboardShortcut from "./KeyboardShortcut.js";
import { browserType } from "../../utils/browserType";
import FontSize from "./FontSize.js";
import Reference from "../reference/Reference.js";
import SceneConfigMenu from "../structural/header/SceneConfigMenu.js";
import { save } from "../../actions/projectActions.js";
import sockets from "socket.io-client";
import "../../css/App.css";
import {
createTheme,
ThemeProvider,
} from "@material-ui/core";
/**
* Editor is a React Component that creates the Ace Editor in the DOM.
*/
class Editor extends Component {
constructor(props) {
super(props);
this.state = {
savedSettings: [],
logMenuOpen: false,
availProj: [],
sampleProj: [],
collectionOpen: false,
projectsOpen: false,
projectTab: "a",
snackOpen: false,
lastMsgTime: 0,
anchorEl: null,
navAwayModal: false,
needsNewId: false, // this explicitly tells us to make a new id
spinnerOpen: false,
coursesOpen: false,
tourOpen: false,
welcomeOpen: false,
updateCollection: false,
fetchCollection: false,
socket: sockets(),
googleUser: undefined
};
}
/**
* Called when the Edtior is unmounting (Being removed from the DOM)
*
* Editor will unmount when MYR enters ViewOnly mode, and we want to render
* whatever the code that's in the editor.
*/
componentWillUnmount() {
// Updates state in reducer before closing editor
const text = window.ace.edit("ace-editor").getSession().getValue();
this.props.refresh(text, this.props.user ? this.props.user.uid : "anon");
// Forces render cycle so user sees up to date view when viewonly loads
this.props.render(text);
}
/**
* Called when the Editor is mounted (component has been rendererd to the DOM)
*
* It sets custom completer of MYR API to editor,
* and add listener to check whether user have unsaved changes.
*/
componentDidMount() {
try {
// eslint-disable-next-line
this.refs.aceEditor.editor.completers = [customCompleter];
} catch (error) {
console.error("Unable to attach custom completers");
}
if (this.props.refExName) {
this.props.referenceExampleActions.fetchReferenceExample(this.props.refExName);
}
// Warn the issue before refreshing the page
window.addEventListener("beforeunload", (event) => {
let text;
try {
let editor = window.ace.edit("ace-editor");
text = editor.getSession().getValue();
} catch (err) {
console.error(err);
}
if (this.props.savedText !== text) {
event.preventDefault();
event.returnValue = "You may have unsaved scene changes!";
}
});
this.setState({"previousSettings":this.props.settings});
}
/**
* Called when the editor is loaded.
* It sets options to set the maximum error editor accepts and set the EMCAScript version to 6
*/
onLoad() {
window.ace.edit("ace-editor").session.$worker.send("setOptions", [{
"maxerr": 1000,
"esversion": 6
}]);
}
componentDidUpdate(){
if(JSON.stringify(this.state.previousSettings) !== JSON.stringify(this.props.settings) &&
this.props.user) {
this.props.userActions.updateUserSettings(this.props.user.uid,this.props.settings);
this.setState({"previousSettings":this.props.settings});
}
/*if (this.state.savedSettings.length === 0 && this.props.scene.id !== 0) {
this.setState({ savedSettings: this.buildSettingsArr() });
window.addEventListener("beforeunload", (event) => {
let finalSettings = this.buildSettingsArr();
if (!this.settingsEqual(finalSettings)) {
event.preventDefault();
event.returnValue = "You may have unsaved changes!";
}
});
} */
}
/**
* Flatten the sceneSettings from props (object) into an array for comparason
* @returns {array} Array of the scene settings
*/
buildSettingsArr = () => {
const sceneSettings = this.props.scene.settings;
return [sceneSettings.floorColor,
sceneSettings.showCoordHelper, sceneSettings.showFloor,
sceneSettings.skyColor, sceneSettings.viewOnly];
};
/**
* Compare two arrays of setting and determine whether is the settings are equal or not
* @param {array} newSettings Settings to compare
* @returns {boolean} If settings are equal or not
*/
settingsEqual = (newSettings) => {
for (let i = 0; i < newSettings.length; ++i) {
if (newSettings[i] !== this.state.savedSettings[i]) {
return false;
}
}
return true;
}
/**
* handeRender gets the information from Ace Editor and calls the action: render()
*/
handleRender = () => {
try {
let editor = window.ace.edit("ace-editor");
this.props.actions.render(editor.getSession().getValue(), this.props.user ? this.props.user.uid : "anon");
} catch (error) {
this.props.actions.render(this.props.text, this.props.user ? this.props.user.uid : "anon");
}
}
/**
* When the user clicks save it will upload the information to Firebase
*/
handleSave = (newCollectionID = undefined) => {
let editor, text;
if (!this.props.viewOnly) {
//If in editor mode, gets text directly from editor
editor = window.ace.edit("ace-editor");
text = editor.getSession().getValue();
} else {
//Otherwise, gets text from state (should be up to date since it is refreshed on editor unmount)
text = this.props.text;
}
if (this.props.user && this.props.user.uid && text) {
this.setState({ spinnerOpen: true });
let scene = document.querySelector("a-scene");
// Access the scene and screen shot, with perspective view in a lossy jpeg format
let img = scene.components.screenshot.getCanvas("perspective").toDataURL("image/jpeg", 0.1);
let newScene = {
name: (this.props.scene.name ? this.props.scene.name : "Untitled Scene"),
desc: this.props.scene.desc,
code: text,
uid: this.props.user.uid,
settings: {
...this.props.scene.settings,
collectionID: newCollectionID || this.props.scene.settings.collectionID
},
updateTime: Date.now(),
createTime: (this.props.scene.createTime ? this.props.scene.createTime : Date.now())
};
save(this.props.user.uid, newScene, img, this.props.projectId).then((projectId) => {
if (!projectId) {
console.error("Could not save the scene");
}
this.props.actions.updateSavedText(text);
// If we have a new projectId reload page with it
if (projectId !== this.props.projectId) {
this.setState({ spinnerOpen: false });
window.location.assign(`${window.origin}/scene/${projectId}`);
this.props.projectActions.asyncUserProj(this.props.user.uid);
}
if (!this.state.viewOnly) {
this.props.actions.refresh(text, this.props.user ? this.props.user.uid : "anon");
}
this.setState({ spinnerOpen: false, saveOpen: false });
this.state.socket.emit("save");
return true;
});
} else if (!text) {
alert("There is no code to save for this scene. Try adding some in the editor!");
} else {
// TODO: Don't use alert
alert("We were unable to save your project. Are you currently logged in?");
}
if (!this.state.viewOnly) {
this.props.actions.refresh(text, this.props.user ? this.props.user.uid : "anon");
}
this.setState({ savedSettings: this.buildSettingsArr() });
}
/**
* toggles the save drawer
*/
handleSaveToggle = () => this.setState({ saveOpen: !this.state.saveOpen });
/**
* forces save drawer closed
*/
handleSaveClose = () => this.setState({ saveOpen: false });
/**
* forces save drawer closed
*/
handleSaveOpen = () => this.setState({ saveOpen: true });
/**
* Creates the editor in the DOM
*/
render() {
const keyTheme = createTheme({
palette: {
primary: {
main: "#ffeb3b",
},
secondary: {
main: "#9c27b0",
}
},
});
const refTheme = createTheme({
palette: {
primary: {
main: "#a31545",
contrastText: "#FFFFFF"
},
secondary: {
main: "#4caf50",
contrastText: "#FFFFFF"
},
},
});
return (
<div id="leftConsole">
<AceEditor
editorProps={{
$blockScrolling: Infinity,
}}
height="86.5vh"
mode="javascript"
name="ace-editor"
// eslint-disable-next-line
ref="aceEditor"
theme="github"
fontSize = {this.props.settings.fontSize}
value={this.props.text}
width="100%"
wrapEnabled={true}
enableBasicAutocompletion={false}
enableLiveAutocompletion={true}
onLoad={this.onLoad}
/>
{ browserType() === "desktop" ? <div className="console-footer">
<ThemeProvider theme={keyTheme}>
<KeyboardShortcut/>
</ThemeProvider>
<ThemeProvider theme={keyTheme}>
<FontSize userActions={this.props.userActions} settings={this.props.settings}/>
</ThemeProvider>
<ThemeProvider theme={refTheme}>
<Reference
layoutType={this.props.layoutType}/>
</ThemeProvider>
<ThemeProvider>
<SceneConfigMenu
scene={this.props.scene}
sceneActions={this.props.sceneActions}
collectionActions={this.props.collectionActions}
user={this.props.user}
settings={this.props.settings}
userActions={this.props.userActions}
handleRender={this.handleRender}
handleSave={this.handleSave}
handleSaveClose={this.handleSaveClose}
layoutType={this.props.layoutType}
displayCollectionConfig={!this.props.collection}
/>
</ThemeProvider>
</div> : null }
</div>
);
}
}
export default Editor;