Skip to content

Commit b2db0e6

Browse files
committed
🐛 Fix store observer bug
1 parent ee7a427 commit b2db0e6

File tree

7 files changed

+49
-34
lines changed

7 files changed

+49
-34
lines changed

.eslintrc.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@
7070
} ],
7171

7272
"block-scoped-var" : [ "error" ],
73-
74-
"padded-blocks" : [ "error", "never" ],
75-
"object-shorthand" : [ "error", "consistent" ]
73+
"padded-blocks" : [ "error", "never" ]
7674
}
7775
}

src/scripts/bootstrap.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from "react";
22
import { render } from "react-dom";
3+
import { Provider } from "mobx-react";
34
import App from "./components/App.react";
45

56
import "./shortcuts/FileShortcuts";
@@ -14,4 +15,13 @@ import "codemirror/addon/dialog/dialog";
1415
import "codemirror/keymap/sublime";
1516
import "markdown-it-asciimath/ASCIIMathTeXImg";
1617

17-
render( <App />, document.getElementById( "app" ) );
18+
const stores = {
19+
alertsStore : require( "./stores/AlertStore" ),
20+
editorStore : require( "./stores/EditorStore" )
21+
};
22+
23+
render( (
24+
<Provider {...stores}>
25+
<App />
26+
</Provider>
27+
), document.getElementById( "app" ) );

src/scripts/components/AppHeader.react.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
import ThemeIcon from "../../images/icons/menu/theme.svg";
22
import PreviewIcon from "../../images/icons/menu/preview.svg";
33
import React, { Component } from "react";
4-
import { observer } from "mobx-react";
4+
import { inject, observer } from "mobx-react";
55
import PropTypes from "prop-types";
66
import className from "classnames";
77
import ReactSVG from "react-svg";
88
import Controls from "./controls/Controls.react";
9-
import EditorStore from "../stores/EditorStore";
109

10+
@inject( [ "editorStore" ] )
1111
@observer
1212
class AppHeader extends Component {
1313
static propTypes = {
14+
editorStore : PropTypes.object,
1415
toggleTheme : PropTypes.func.isRequired,
1516
togglePreview : PropTypes.func.isRequired,
1617
isThemeToggled : PropTypes.bool.isRequired,
1718
isPreviewToggled : PropTypes.bool.isRequired
1819
}
1920

2021
revealFolder = () => {
21-
if ( EditorStore.path !== "" ) {
22-
require( "opn" )( EditorStore.directory );
22+
if ( this.props.editorStore.path ) {
23+
require( "opn" )( this.props.editorStore.directory );
2324
}
2425
}
2526

@@ -33,15 +34,15 @@ class AppHeader extends Component {
3334
} );
3435

3536
const titleClasses = className( "app-header-title", {
36-
"is-clickable" : EditorStore.path !== ""
37+
"is-clickable" : this.props.editorStore.path
3738
} );
3839

3940
return (
4041
<div className="app-header qilin-panel">
4142
<Controls />
4243

4344
<div className={titleClasses} onClick={this.revealFolder}>
44-
{EditorStore.filename}
45+
{this.props.editorStore.filename}
4546
</div>
4647

4748
<div className="app-header-buttons">

src/scripts/components/editor/EditorEditable.react.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import React, { Component } from "react";
2-
import { observer } from "mobx-react";
2+
import { inject, observer } from "mobx-react";
33
import CodeMirrorComponent from "react-codemirror";
4-
import EditorStore from "../../stores/EditorStore";
4+
import PropTypes from "prop-types";
55

6+
@inject( [ "editorStore" ] )
67
@observer
78
class EditorEditable extends Component {
9+
static propTypes = {
10+
editorStore : PropTypes.object
11+
}
12+
813
state = {
914
options : {
1015
mode : "gfm",
@@ -30,15 +35,15 @@ class EditorEditable extends Component {
3035
}
3136

3237
editorDidChange = value => {
33-
EditorStore.changeContent( value );
38+
this.props.editorStore.changeContent( value );
3439
}
3540

3641
render() {
3742
return (
3843
<div className="editor-editable qilin-panel">
3944
<CodeMirrorComponent
4045
options={this.state.options}
41-
value={EditorStore.content || ""}
46+
value={this.props.editorStore.content}
4247
onChange={this.editorDidChange}
4348
/>
4449
</div>

src/scripts/components/editor/EditorPreview.react.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import React, { Component } from "react";
2-
import { observer } from "mobx-react";
2+
import { inject, observer } from "mobx-react";
3+
import PropTypes from "prop-types";
34
import Markdown from "markdown-it";
4-
import EditorStore from "../../stores/EditorStore";
55

6+
@inject( [ "editorStore" ] )
67
@observer
78
class EditorPreview extends Component {
9+
static propTypes = {
10+
editorStore : PropTypes.object
11+
}
12+
813
componentWillMount() {
914
this.markdown = new Markdown( {
1015
html : true,
@@ -27,7 +32,7 @@ class EditorPreview extends Component {
2732
return (
2833
<div
2934
className="editor-preview qilin-panel typo"
30-
dangerouslySetInnerHTML={{ __html : this.markdown.render( EditorStore.content || "") }}
35+
dangerouslySetInnerHTML={{ __html : this.markdown.render( this.props.editorStore.content ) }}
3136
/>
3237
);
3338
}

src/scripts/stores/AlertStore.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { observable, action, computed } from "mobx";
1+
import { observable } from "mobx";
22

3-
class AlertStore {
4-
@observable info = [];
5-
@observable failure = [];
6-
@observable success = [];
7-
}
8-
9-
export default new AlertStore();
3+
export default {
4+
@observable info : [],
5+
@observable failure : [],
6+
@observable success : []
7+
};

src/scripts/stores/EditorStore.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
import path from "path";
22
import { observable, action, computed } from "mobx";
33

4-
class EditorStore {
5-
@observable path = "";
6-
@observable content = "";
4+
export default {
5+
@observable path : "",
6+
@observable content : "",
77

88
@action changePath( path ) {
99
this.path = path;
10-
}
10+
},
1111

1212
@action changeContent( content ) {
1313
this.content = content;
14-
}
14+
},
1515

1616
@computed get filename() {
1717
return this.path ? path.basename( this.path ) : "Untitled";
18-
}
18+
},
1919

2020
@computed get directory() {
2121
return path.dirname( this.path );
2222
}
23-
}
24-
25-
export default new EditorStore();
23+
};

0 commit comments

Comments
 (0)