for #790, when saving a sketch, pull from codemirror window (#847)

* for #790, when saving a sketch, pull from codemirror window

* fix lint errors
This commit is contained in:
Cassie Tarakajian 2019-02-25 16:45:20 -05:00 committed by GitHub
parent 61f20d1d4c
commit 2cc0d578fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 25 deletions

View file

@ -157,7 +157,7 @@ class Nav extends React.PureComponent {
<button
onClick={() => {
if (this.props.user.authenticated) {
this.props.saveProject();
this.props.saveProject(this.props.cmController.getContent());
} else {
this.props.showErrorModal('forceAuthentication');
}
@ -585,7 +585,8 @@ Nav.propTypes = {
tidyCode: PropTypes.func,
showFind: PropTypes.func,
findNext: PropTypes.func,
findPrev: PropTypes.func
findPrev: PropTypes.func,
getContent: PropTypes.func
}),
startSketch: PropTypes.func.isRequired,
stopSketch: PropTypes.func.isRequired,

View file

@ -66,7 +66,7 @@ export function clearPersistedState() {
};
}
export function saveProject(autosave = false) {
export function saveProject(selectedFile = null, autosave = false) {
return (dispatch, getState) => {
const state = getState();
if (state.user.id && state.project.owner && state.project.owner.id !== state.user.id) {
@ -74,6 +74,11 @@ export function saveProject(autosave = false) {
}
const formParams = Object.assign({}, state.project);
formParams.files = [...state.files];
if (selectedFile) {
console.log('selected file being updated');
const fileToUpdate = formParams.files.find(file => file.id === selectedFile.id);
fileToUpdate.content = selectedFile.content;
}
if (state.project.id) {
return axios.put(`${ROOT_URL}/projects/${state.project.id}`, formParams, { withCredentials: true })
.then((response) => {
@ -156,7 +161,7 @@ export function saveProject(autosave = false) {
export function autosaveProject() {
return (dispatch, getState) => {
saveProject(true)(dispatch, getState);
saveProject(null, true)(dispatch, getState);
};
}

View file

@ -69,6 +69,7 @@ class Editor extends React.Component {
this.showFind = this.showFind.bind(this);
this.findNext = this.findNext.bind(this);
this.findPrev = this.findPrev.bind(this);
this.getContent = this.getContent.bind(this);
}
componentDidMount() {
@ -144,7 +145,8 @@ class Editor extends React.Component {
tidyCode: this.tidyCode,
showFind: this.showFind,
findNext: this.findNext,
findPrev: this.findPrev
findPrev: this.findPrev,
getContent: this.getContent
});
}
@ -230,13 +232,24 @@ class Editor extends React.Component {
return mode;
}
initializeDocuments(files) {
this._docs = {};
files.forEach((file) => {
if (file.name !== 'root') {
this._docs[file.id] = CodeMirror.Doc(file.content, this.getFileMode(file.name)); // eslint-disable-line
getContent() {
const content = this._cm.getValue();
const updatedFile = Object.assign({}, this.props.file, { content });
return updatedFile;
}
});
findPrev() {
this._cm.focus();
this._cm.execCommand('findPrev');
}
findNext() {
this._cm.focus();
this._cm.execCommand('findNext');
}
showFind() {
this._cm.execCommand('findPersistent');
}
tidyCode() {
@ -255,18 +268,13 @@ class Editor extends React.Component {
}
}
showFind() {
this._cm.execCommand('findPersistent');
initializeDocuments(files) {
this._docs = {};
files.forEach((file) => {
if (file.name !== 'root') {
this._docs[file.id] = CodeMirror.Doc(file.content, this.getFileMode(file.name)); // eslint-disable-line
}
findNext() {
this._cm.focus();
this._cm.execCommand('findNext');
}
findPrev() {
this._cm.focus();
this._cm.execCommand('findPrev');
});
}
toggleEditorOptions() {

View file

@ -157,7 +157,7 @@ class IDEView extends React.Component {
e.preventDefault();
e.stopPropagation();
if (this.isUserOwner() || (this.props.user.authenticated && !this.props.project.owner)) {
this.props.saveProject();
this.props.saveProject(this.cmController.getContent());
} else if (this.props.user.authenticated) {
this.props.cloneProject();
} else {