const React = require('react');
const _ = require('lodash');
const cx = require('classnames');
const request = require("superagent");
const Nav = require('naturalcrit/nav/nav.jsx');
const Navbar = require('../../navbar/navbar.jsx');
const ReportIssue = require('../../navbar/issue.navitem.jsx');
const PrintLink = require('../../navbar/print.navitem.jsx');
const Account = require('../../navbar/account.navitem.jsx');
//const RecentlyEdited = require('../../navbar/recent.navitem.jsx').edited;
const SplitPane = require('naturalcrit/splitPane/splitPane.jsx');
const Editor = require('../../editor/editor.jsx');
const BrewRenderer = require('../../brewRenderer/brewRenderer.jsx');
const Markdown = require('naturalcrit/markdown.js');
const SAVE_TIMEOUT = 3000;
const EditPage = React.createClass({
getDefaultProps: function() {
return {
brew : {
text : '',
shareId : null,
editId : null,
createdAt : null,
updatedAt : null,
title : '',
description : '',
tags : '',
published : false,
authors : [],
systems : []
}
};
},
getInitialState: function() {
return {
brew : this.props.brew,
isSaving : false,
isPending : false,
errors : null,
htmlErrors : [],
lastUpdated : this.props.brew.updatedAt
};
},
savedBrew : null,
componentDidMount: function(){
this.debounceSave = _.debounce(this.save, SAVE_TIMEOUT);
window.onbeforeunload = ()=>{
if(this.state.isSaving || this.state.isPending){
return 'You have unsaved changes!';
}
};
this.setState({
htmlErrors : Markdown.validate(this.state.brew.text)
})
document.addEventListener('keydown', this.handleControlKeys);
},
componentWillUnmount: function() {
window.onbeforeunload = function(){};
document.removeEventListener('keydown', this.handleControlKeys);
},
handleControlKeys : function(e){
if(!(e.ctrlKey || e.metaKey)) return;
const S_KEY = 83;
const P_KEY = 80;
if(e.keyCode == S_KEY) this.save();
if(e.keyCode == P_KEY) window.open(`/print/${this.props.brew.shareId}?dialog=true`, '_blank').focus();
if(e.keyCode == P_KEY || e.keyCode == S_KEY){
e.stopPropagation();
e.preventDefault();
}
},
handleSplitMove : function(){
this.refs.editor.update();
},
handleMetadataChange : function(metadata){
this.setState({
brew : _.merge({}, this.state.brew, metadata),
isPending : true,
}, ()=>{
console.log(this.hasChanges());
(this.hasChanges() ? this.debounceSave() : this.debounceSave.cancel());
});
},
handleTextChange : function(text){
//If there are errors, run the validator on everychange to give quick feedback
var htmlErrors = this.state.htmlErrors;
if(htmlErrors.length) htmlErrors = Markdown.validate(text);
this.setState({
brew : _.merge({}, this.state.brew, {text : text}),
isPending : true,
htmlErrors : htmlErrors
});
(this.hasChanges() ? this.debounceSave() : this.debounceSave.cancel());
},
hasChanges : function(){
if(this.savedBrew){
return !_.isEqual(this.state.brew, this.savedBrew)
}else{
return !_.isEqual(this.state.brew, this.props.brew)
}
return false;
},
save : function(){
this.debounceSave.cancel();
this.setState({
isSaving : true,
errors : null,
htmlErrors : Markdown.validate(this.state.brew.text)
});
request
.put('/api/update/' + this.props.brew.editId)
.send(this.state.brew)
.end((err, res) => {
if(err){
this.setState({
errors : err,
})
}else{
this.savedBrew = res.body;
this.setState({
isPending : false,
isSaving : false,
lastUpdated : res.body.updatedAt
})
}
})
},
renderSaveButton : function(){
if(this.state.errors){
var errMsg = '';
try{
errMsg += this.state.errors.toString() + '\n\n';
errMsg += '```\n' + JSON.stringify(this.state.errors.response.error, null, ' ') + '\n```';
}catch(e){}
return
Report the issue
here
.