Skip to content

Commit 2cc0d57

Browse files
authored
for #790, when saving a sketch, pull from codemirror window (#847)
* for #790, when saving a sketch, pull from codemirror window * fix lint errors
1 parent 61f20d1 commit 2cc0d57

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

client/components/Nav.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class Nav extends React.PureComponent {
157157
<button
158158
onClick={() => {
159159
if (this.props.user.authenticated) {
160-
this.props.saveProject();
160+
this.props.saveProject(this.props.cmController.getContent());
161161
} else {
162162
this.props.showErrorModal('forceAuthentication');
163163
}
@@ -585,7 +585,8 @@ Nav.propTypes = {
585585
tidyCode: PropTypes.func,
586586
showFind: PropTypes.func,
587587
findNext: PropTypes.func,
588-
findPrev: PropTypes.func
588+
findPrev: PropTypes.func,
589+
getContent: PropTypes.func
589590
}),
590591
startSketch: PropTypes.func.isRequired,
591592
stopSketch: PropTypes.func.isRequired,

client/modules/IDE/actions/project.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,19 @@ export function clearPersistedState() {
6666
};
6767
}
6868

69-
export function saveProject(autosave = false) {
69+
export function saveProject(selectedFile = null, autosave = false) {
7070
return (dispatch, getState) => {
7171
const state = getState();
7272
if (state.user.id && state.project.owner && state.project.owner.id !== state.user.id) {
7373
return Promise.reject();
7474
}
7575
const formParams = Object.assign({}, state.project);
7676
formParams.files = [...state.files];
77+
if (selectedFile) {
78+
console.log('selected file being updated');
79+
const fileToUpdate = formParams.files.find(file => file.id === selectedFile.id);
80+
fileToUpdate.content = selectedFile.content;
81+
}
7782
if (state.project.id) {
7883
return axios.put(`${ROOT_URL}/projects/${state.project.id}`, formParams, { withCredentials: true })
7984
.then((response) => {
@@ -156,7 +161,7 @@ export function saveProject(autosave = false) {
156161

157162
export function autosaveProject() {
158163
return (dispatch, getState) => {
159-
saveProject(true)(dispatch, getState);
164+
saveProject(null, true)(dispatch, getState);
160165
};
161166
}
162167

client/modules/IDE/components/Editor.jsx

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class Editor extends React.Component {
6969
this.showFind = this.showFind.bind(this);
7070
this.findNext = this.findNext.bind(this);
7171
this.findPrev = this.findPrev.bind(this);
72+
this.getContent = this.getContent.bind(this);
7273
}
7374

7475
componentDidMount() {
@@ -144,7 +145,8 @@ class Editor extends React.Component {
144145
tidyCode: this.tidyCode,
145146
showFind: this.showFind,
146147
findNext: this.findNext,
147-
findPrev: this.findPrev
148+
findPrev: this.findPrev,
149+
getContent: this.getContent
148150
});
149151
}
150152

@@ -230,13 +232,24 @@ class Editor extends React.Component {
230232
return mode;
231233
}
232234

233-
initializeDocuments(files) {
234-
this._docs = {};
235-
files.forEach((file) => {
236-
if (file.name !== 'root') {
237-
this._docs[file.id] = CodeMirror.Doc(file.content, this.getFileMode(file.name)); // eslint-disable-line
238-
}
239-
});
235+
getContent() {
236+
const content = this._cm.getValue();
237+
const updatedFile = Object.assign({}, this.props.file, { content });
238+
return updatedFile;
239+
}
240+
241+
findPrev() {
242+
this._cm.focus();
243+
this._cm.execCommand('findPrev');
244+
}
245+
246+
findNext() {
247+
this._cm.focus();
248+
this._cm.execCommand('findNext');
249+
}
250+
251+
showFind() {
252+
this._cm.execCommand('findPersistent');
240253
}
241254

242255
tidyCode() {
@@ -255,18 +268,13 @@ class Editor extends React.Component {
255268
}
256269
}
257270

258-
showFind() {
259-
this._cm.execCommand('findPersistent');
260-
}
261-
262-
findNext() {
263-
this._cm.focus();
264-
this._cm.execCommand('findNext');
265-
}
266-
267-
findPrev() {
268-
this._cm.focus();
269-
this._cm.execCommand('findPrev');
271+
initializeDocuments(files) {
272+
this._docs = {};
273+
files.forEach((file) => {
274+
if (file.name !== 'root') {
275+
this._docs[file.id] = CodeMirror.Doc(file.content, this.getFileMode(file.name)); // eslint-disable-line
276+
}
277+
});
270278
}
271279

272280
toggleEditorOptions() {

client/modules/IDE/pages/IDEView.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class IDEView extends React.Component {
157157
e.preventDefault();
158158
e.stopPropagation();
159159
if (this.isUserOwner() || (this.props.user.authenticated && !this.props.project.owner)) {
160-
this.props.saveProject();
160+
this.props.saveProject(this.cmController.getContent());
161161
} else if (this.props.user.authenticated) {
162162
this.props.cloneProject();
163163
} else {

0 commit comments

Comments
 (0)