diff --git a/src/components/MarkdownEditor/MarkdownEditor.js b/src/components/MarkdownEditor/MarkdownEditor.js
index f18f5ec98..ad2df39c0 100644
--- a/src/components/MarkdownEditor/MarkdownEditor.js
+++ b/src/components/MarkdownEditor/MarkdownEditor.js
@@ -17,13 +17,13 @@ import { client } from 'utils/ApiClient';
@observer
class MarkdownEditor extends React.Component {
static propTypes = {
- text: React.PropTypes.string.isRequired,
+ text: React.PropTypes.string,
onChange: React.PropTypes.func.isRequired,
replaceText: React.PropTypes.func.isRequired,
// This is actually not used but it triggers
// re-render to help with CodeMirror focus issues
- preview: React.PropTypes.bool.isRequired,
+ preview: React.PropTypes.bool,
}
getEditorInstance = () => {
diff --git a/src/scenes/DocumentEdit/DocumentEdit.js b/src/scenes/DocumentEdit/DocumentEdit.js
index ceacce94b..4a6b9559b 100644
--- a/src/scenes/DocumentEdit/DocumentEdit.js
+++ b/src/scenes/DocumentEdit/DocumentEdit.js
@@ -1,8 +1,10 @@
import React, { Component } from 'react';
import { observer } from 'mobx-react';
-import { browserHistory } from 'react-router';
+import { browserHistory, withRouter } from 'react-router';
-import store from './DocumentEditStore';
+import DocumentEditStore, {
+ DOCUMENT_EDIT_SETTINGS,
+} from './DocumentEditStore';
import Switch from 'components/Switch';
import Layout, { Title, HeaderAction } from 'components/Layout';
@@ -14,26 +16,43 @@ import DropdownMenu, { MenuItem } from 'components/DropdownMenu';
import EditorLoader from './components/EditorLoader';
import SaveAction from './components/SaveAction';
-import styles from './DocumentEdit.scss';
-import classNames from 'classnames/bind';
-const cx = classNames.bind(styles);
+const DISREGARD_CHANGES = `You have unsaved changes.
+Are you sure you want to disgard them?`;
+@withRouter
@observer
class DocumentEdit extends Component {
+ static store;
+
+ static propTypes = {
+ route: React.PropTypes.object.isRequired,
+ router: React.PropTypes.object.isRequired,
+ params: React.PropTypes.object,
+ }
+
+ state = {
+ scrollTop: 0,
+ }
+
+ constructor(props) {
+ super(props);
+ this.store = new DocumentEditStore(
+ JSON.parse(localStorage[DOCUMENT_EDIT_SETTINGS] || '{}')
+ );
+ }
+
componentDidMount = () => {
- // This is a bit hacky, should find a better way
- store.reset();
if (this.props.route.newDocument) {
- store.atlasId = this.props.params.id;
- store.newDocument = true;
+ this.store.atlasId = this.props.params.id;
+ this.store.newDocument = true;
} else if (this.props.route.newChildDocument) {
- store.documentId = this.props.params.id;
- store.newChildDocument = true;
- store.fetchDocument();
+ this.store.documentId = this.props.params.id;
+ this.store.newChildDocument = true;
+ this.store.fetchDocument();
} else {
- store.documentId = this.props.params.id;
- store.newDocument = false;
- store.fetchDocument();
+ this.store.documentId = this.props.params.id;
+ this.store.newDocument = false;
+ this.store.fetchDocument();
}
// Load editor async
@@ -41,6 +60,14 @@ class DocumentEdit extends Component {
.then(({ Editor }) => {
this.setState({ Editor });
});
+
+ // Set onLeave hook
+ this.props.router.setRouteLeaveHook(this.props.route, () => {
+ if (this.store.hasPendingChanges) {
+ return confirm(DISREGARD_CHANGES);
+ }
+ return;
+ });
}
onSave = () => {
@@ -48,10 +75,10 @@ class DocumentEdit extends Component {
// alert("Please add a title before saving (hint: Write a markdown header)");
// return
// }
- if (store.newDocument || store.newChildDocument) {
- store.saveDocument();
+ if (this.store.newDocument || this.store.newChildDocument) {
+ this.store.saveDocument();
} else {
- store.updateDocument();
+ this.store.updateDocument();
}
}
@@ -59,43 +86,37 @@ class DocumentEdit extends Component {
browserHistory.goBack();
}
- state = {
- scrollTop: 0,
- }
-
onScroll = (scrollTop) => {
this.setState({
- scrollTop: scrollTop,
- })
- }
-
- onPreviewToggle = () => {
- store.togglePreview();
+ scrollTop,
+ });
}
render() {
+ console.log("DocumentEdit#render", this.store.preview);
+
let title = (
- { store.title }
+ { this.store.title }
);
- let titleText = store.title;
+ let titleText = this.store.title;
const actions = (
-