Skip to content

Commit 98e5a37

Browse files
authored
Merge pull request #113 from Coding/hackape/mobxify-settings
mobxify settings
2 parents 4223d33 + da0fddf commit 98e5a37

File tree

12 files changed

+404
-381
lines changed

12 files changed

+404
-381
lines changed

app/components/CodeMirrorEditor/index.jsx

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/*@flow weak*/
21
import React, {Component} from 'react';
32
import CodeMirror from 'codemirror';
4-
import {connect} from 'react-redux';
3+
import { inject, observer } from 'mobx-react'
4+
import { dispatch } from 'store'
55
import './addons';
66
import dispatchCommand from '../../commands/dispatchCommand'
77
import _ from 'lodash'
@@ -39,10 +39,10 @@ function getMode (tab) {
3939

4040
const debounced = _.debounce(func => func(), 1000)
4141

42-
@connect(state => ({
43-
setting: state.SettingState.views.tabs.EDITOR,
44-
themeSetting: state.SettingState.views.tabs.THEME,
42+
@inject(state => ({
43+
themeName: state.SettingState.settings.theme.syntax_theme.value,
4544
}))
45+
@observer
4646
class CodeMirrorEditor extends Component {
4747
static defaultProps = {
4848
theme: 'default',
@@ -56,15 +56,14 @@ class CodeMirrorEditor extends Component {
5656
}
5757

5858
componentDidMount () {
59-
const { themeSetting, tab } = this.props;
60-
const themeConfig = themeSetting.items[1].value
59+
const { themeName, tab } = this.props;
6160
let editorInitialized = false
6261
// todo add other setting item from config
6362
if (tab.editor) {
6463
this.editor = tab.editor
6564
this.editorContainer.appendChild(this.editor.getWrapperElement())
6665
} else {
67-
this.editor = tab.editor = initializeEditor(this.editorContainer, themeConfig)
66+
this.editor = tab.editor = initializeEditor(this.editorContainer, themeName)
6867
editorInitialized = true
6968
}
7069
const editor = this.editor
@@ -100,7 +99,7 @@ class CodeMirrorEditor extends Component {
10099

101100
onChange = (e) => {
102101
if (!this.isChanging) this.isChanging = true
103-
const {tab, dispatch} = this.props;
102+
const { tab } = this.props;
104103
dispatch(TabActions.updateTab({
105104
id: tab.id,
106105
flags: { modified: true },
@@ -113,17 +112,17 @@ class CodeMirrorEditor extends Component {
113112
}
114113

115114
onFocus = () => {
116-
this.props.dispatch(TabActions.activateTab(this.props.tab.id))
115+
dispatch(TabActions.activateTab(this.props.tab.id))
117116
}
118117

119-
componentWillReceiveProps ({ tab, themeSetting }) {
118+
componentWillReceiveProps ({ tab, themeName }) {
120119
if (tab.flags.modified || !this.editor || !tab.content) return
121120
if (tab.content.body !== this.editor.getValue()) {
122121
this.editor.setValue(tab.content.body)
123122
}
124123

125-
const nextTheme = themeSetting.items[1].value
126-
const theme = this.props.themeSetting.items[1].value
124+
const nextTheme = themeName
125+
const theme = this.props.themeName
127126
if (theme !== nextTheme) this.editor.setOption('theme', nextTheme)
128127
}
129128

@@ -142,35 +141,34 @@ class CodeMirrorEditor extends Component {
142141
}
143142

144143

145-
@connect(state => ({
146-
setting: state.SettingState.views.tabs.EDITOR,
147-
themeSetting: state.SettingState.views.tabs.THEME,
144+
@inject(state => ({
145+
themeName: state.SettingState.settings.theme.syntax_theme.value,
148146
}))
147+
@observer
149148
class TablessCodeMirrorEditor extends Component {
150149
constructor (props) {
151150
super(props)
152151
this.state = {}
153152
}
154153

155154
componentDidMount() {
156-
const { themeSetting, width, height } = this.props
157-
const theme = themeSetting.items[1].value
155+
const { themeName, width, height } = this.props
158156

159-
this.editor = initializeEditor(this.editorContainer, theme)
157+
this.editor = initializeEditor(this.editorContainer, themeName)
160158
this.editor.focus()
161159
this.editor.on('change', this.onChange)
162160
}
163161

164162
onChange = (e) => {
165-
this.props.dispatch(TabActions.createTabInGroup(this.props.tabGroupId, {
163+
dispatch(TabActions.createTabInGroup(this.props.tabGroupId, {
166164
flags: { modified: true },
167165
content: this.editor.getValue()
168166
}))
169167
}
170168

171-
componentWillReceiveProps ({ themeSetting }) {
172-
const nextTheme = themeSetting.items[1].value
173-
const theme = this.props.themeSetting.items[1].value
169+
componentWillReceiveProps ({ themeName }) {
170+
const nextTheme = themeName
171+
const theme = this.props.themeName
174172
if (theme !== nextTheme) this.editor.setOption('theme', nextTheme)
175173
}
176174

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import React, { Component } from 'react'
2+
import { inject, observer } from 'mobx-react'
3+
import { runInAction } from 'mobx'
4+
import cx from 'classnames'
5+
import _ from 'lodash'
6+
7+
@observer
8+
class SettingForm extends Component {
9+
constructor (props) {
10+
super(props)
11+
}
12+
13+
updateSettingItemBind = settingItem => {
14+
let update
15+
if (this.props.setting.requireConfirm) {
16+
update = value => settingItem.tempValue = value
17+
} else {
18+
update = value => settingItem.value = value
19+
}
20+
return (e) => {
21+
const value = (() => {
22+
switch (e.target.type) {
23+
case 'checkbox':
24+
return e.target.checked
25+
case 'number':
26+
return Number(e.target.value)
27+
case 'text':
28+
case 'select-one':
29+
default:
30+
return e.target.value
31+
}
32+
})()
33+
update(value)
34+
}
35+
}
36+
37+
render () {
38+
const { setting } = this.props
39+
return <div>
40+
{setting.items.map(settingItem =>
41+
<FormInputGroup
42+
key={settingItem.key}
43+
settingItem={settingItem}
44+
updateSettingItem={this.updateSettingItemBind(settingItem)}
45+
/>
46+
)}
47+
</div>
48+
}
49+
}
50+
51+
const FormInputGroup = observer(({ settingItem, updateSettingItem }) => {
52+
if (settingItem.options && _.isArray(settingItem.options)) {
53+
return (
54+
<div className="form-group">
55+
<label>{settingItem.name}</label>
56+
<select className="form-control"
57+
onChange={updateSettingItem}
58+
value={settingItem.tempValue === undefined ? settingItem.value : settingItem.tempValue }
59+
>
60+
{settingItem.options.map(option =>
61+
_.isObject(option) ?
62+
<option key={option.value} value={option.value}>{option.name}</option>
63+
: <option key={option} value={option}>{option}</option>
64+
)}
65+
</select>
66+
</div>)
67+
} else if (_.isBoolean(settingItem.value)) {
68+
return (
69+
<div className="form-group">
70+
<div className="checkbox">
71+
<label>
72+
<input type="checkbox"
73+
onChange={updateSettingItem} checked={settingItem.tempValue === undefined ? settingItem.value : settingItem.tempValue }
74+
/>
75+
<strong>{settingItem.name}</strong>
76+
</label>
77+
78+
</div>
79+
</div>)
80+
} else {
81+
return (
82+
<div className="form-group">
83+
<label>{settingItem.name}</label>
84+
<input className="form-control"
85+
type={_.isNumber(settingItem.value) ? 'number' : 'text'}
86+
min="1"
87+
onChange={updateSettingItem}
88+
value={settingItem.tempValue === undefined ? settingItem.value : settingItem.tempValue }
89+
/>
90+
</div>)
91+
}
92+
})
93+
94+
export default SettingForm

app/components/Setting/actions.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)