Skip to content

Commit defc60b

Browse files
committed
⚡ 🐛 improve reliability of Tab/Editor/File models, resolve some issues
1 parent 01b1e89 commit defc60b

File tree

4 files changed

+86
-47
lines changed

4 files changed

+86
-47
lines changed

app/components/Editor/components/CodeMirrorEditor/index.jsx

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,22 @@ class CodeMirrorEditor extends Component {
6262

6363
componentDidMount () {
6464
const { themeName, editor, file } = this.props
65-
let cmInitialized = false
65+
66+
let isCmFreshlyInit = false
6667
// todo add other setting item from config
6768
if (editor.cm) {
6869
this.cm = editor.cm
6970
this.cmContainer.appendChild(this.cm.getWrapperElement())
7071
} else {
7172
this.cm = editor.cm = initializeEditor(this.cmContainer, themeName)
72-
cmInitialized = true
73+
isCmFreshlyInit = true
7374
}
74-
const cm = this.cm
7575

76-
if (cmInitialized && file) {
77-
if (file.content) cm.setValue(file.content)
78-
const modeInfo = getMode(file)
76+
const cm = this.cm
77+
if (isCmFreshlyInit) {
78+
cm.setValue(editor.content)
79+
let modeInfo
80+
if (file) modeInfo = getMode(file)
7981
if (modeInfo) {
8082
let mode = modeInfo.mode
8183
if (mode === 'null') {
@@ -131,15 +133,15 @@ class CodeMirrorEditor extends Component {
131133
onChange = (e) => {
132134
if (!this.isChanging) this.isChanging = true
133135
const { tab, file } = this.props
134-
FileStore.updateFile({
135-
id: file.id,
136-
content: this.cm.getValue(),
137-
})
138136
TabStore.updateTab({
139137
id: tab.id,
140138
flags: { modified: true },
141139
})
142140
if (file) debounced(() => {
141+
FileStore.updateFile({
142+
id: file.id,
143+
content: this.cm.getValue(),
144+
})
143145
dispatchCommand('file:save')
144146
this.isChanging = false
145147
})
@@ -196,13 +198,20 @@ class TablessCodeMirrorEditor extends Component {
196198
this.cm.on('change', this.onChange)
197199
}
198200

201+
componentWillUnmount () {
202+
this.cm.off('change', this.onChange)
203+
}
204+
199205
onChange = (e) => {
200206
TabStore.createTab({
207+
flags: { modified: true },
201208
tabGroup: {
202209
id: this.props.tabGroupId,
203210
},
204-
flags: { modified: true },
205-
content: this.cm.getValue()
211+
editor: {
212+
content: this.cm.getValue(),
213+
cm: this.cm,
214+
},
206215
})
207216
}
208217

app/components/Editor/state.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import uniqueId from 'lodash/uniqueId'
2+
import is from 'utils/is'
23
import { extendObservable, observable, computed, action } from 'mobx'
4+
import CodeMirror from 'codemirror'
35
import FileStore from 'commons/File/store'
46

57
const state = observable({
@@ -9,12 +11,8 @@ const state = observable({
911
class Editor {
1012
constructor (props={}) {
1113
this.id = uniqueId('editor_')
12-
if (props.filePath) this.filePath = props.filePath
13-
if (!this.file && props.content) {
14-
this._content = props.content
15-
}
16-
if (props.gitBlame) this.gitBlame = props.gitBlame
1714
state.entities.set(this.id, this)
15+
this.update(props)
1816
}
1917

2018
@observable tabId = ''
@@ -30,10 +28,17 @@ class Editor {
3028
@computed get content () {
3129
return this.file ? this.file.content : this._content
3230
}
31+
set content (v) { return this._content = v }
3332

3433
@action update (props={}) {
35-
if (props.filePath) this.filePath = props.filePath
36-
if (props.gitBlame) this.gitBlame = props.gitBlame
34+
if (is.string(props.tabId)) this.tabId = props.tabId
35+
if (is.string(props.filePath)) this.filePath = props.filePath
36+
if (is.pojo(props.gitBlame)) this.gitBlame = props.gitBlame
37+
// file
38+
if (!this.file && props.content) {
39+
this._content = props.content
40+
}
41+
if (props.cm instanceof CodeMirror) this.cm = props.cm
3742
}
3843
}
3944

app/components/Tab/actions.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@ import store from 'mobxStore'
55

66
export const TAB_CREATE = 'TAB_CREATE'
77
export const createTab = registerAction(TAB_CREATE,
8-
(tabConfig) => {
9-
const tab = new Tab(tabConfig)
10-
if (!tab.tabGroup) {
11-
const activeTabGroup = state.activeTabGroup
12-
activeTabGroup.addTab(tab)
13-
}
8+
(tabProps) => {
9+
const tab = new Tab(tabProps)
1410
},
1511
)
1612

@@ -51,15 +47,15 @@ export const removeGroup = registerAction('tab:remove_tab_group',
5147
)
5248

5349
export const updateTab = registerAction('tab:update',
54-
(tabConfig={}) => {
55-
const tabId = tabConfig.id
50+
(tabProps={}) => {
51+
const tabId = tabProps.id
5652
const tab = state.tabs.get(tabId)
57-
if (tab) tab.update(tabConfig)
53+
if (tab) tab.update(tabProps)
5854
}
5955
)
6056

6157
export const TAB_UPDATE_BY_PATH = 'TAB_UPDATE_BY_PATH'
62-
export const updateTabByPath = registerAction(TAB_UPDATE_BY_PATH, (tabConfig = {}) => tabConfig)
58+
export const updateTabByPath = registerAction(TAB_UPDATE_BY_PATH, (tabProps = {}) => tabProps)
6359

6460
export const TAB_UPDATE_FLAGS = 'TAB_UPDATE_FLAGS'
6561
export const updateTabFlags = registerAction('tab:update_flags', (tabId, flag, value=true) => {

app/components/Tab/state.js

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,77 @@
1+
import uniqueId from 'lodash/uniqueId'
2+
import is from 'utils/is'
13
import { extendObservable, observable, computed, action } from 'mobx'
24
import { TabStateScope } from 'commons/Tab'
35
import PaneState from 'components/Pane/state'
46
import EditorState, { Editor } from 'components/Editor/state'
57
import FileState, { FileNode } from 'commons/File/state'
68

7-
const { Tab: BaseTab, TabGroup: BaseTabGroup, entities: state } = TabStateScope()
9+
const { Tab: BaseTab, TabGroup: BaseTabGroup, state } = TabStateScope()
810

911
class Tab extends BaseTab {
1012
constructor (props={}) {
11-
super(props)
12-
let editor
13-
if (props.editor) {
14-
editor = new Editor(props.editor)
15-
editor.tabId = this.id
13+
super()
14+
this.id = is.undefined(props.id) ? uniqueId('tab_') : props.id
15+
state.tabs.set(this.id, this)
16+
console.log('the tab', this);
17+
this.update(props)
18+
}
19+
20+
@action update (props={}) {
21+
if (is.string(props.title)) this.title = props.title
22+
if (is.pojo(props.flags)) this.flags = props.flags
23+
24+
// tabGroup
25+
let tabGroup
26+
if (props.tabGroup && props.tabGroup.id) {
27+
tabGroup = state.tabGroups.get(props.tabGroup.id)
28+
}
29+
if (!tabGroup) tabGroup = state.activeTabGroup
30+
if (this.tabGroup !== tabGroup) { tabGroup && tabGroup.addTab(this) }
31+
32+
// editor
33+
if (!props.editor) props.editor = {}
34+
props.editor.tabId = this.id
35+
if (this.editor) {
36+
this.editor.update(props.editor)
37+
} else {
38+
new Editor(props.editor)
1639
}
40+
1741
}
1842

1943
@observable flags = {}
2044

45+
@computed get title () {
46+
if (this.file) {
47+
return this.file.name
48+
} else {
49+
return this._title
50+
}
51+
}
52+
53+
set title (v) { return this._title = v }
54+
2155
@computed get editor () {
2256
return EditorState.entities.values().find(editor => editor.tabId === this.id)
2357
}
58+
set editor (editor) {
59+
if (editor) editor.tabId = this.id
60+
return editor
61+
}
2462

2563
@computed get file () {
2664
const editor = this.editor
2765
return editor ? editor.file : null
2866
}
29-
30-
@action update (props={}) {
31-
if (props.title) this.title = props.title
32-
if (props.flags) this.flags = props.flags
33-
if (this.editor) {
34-
this.editor.update(props.editor)
35-
} else if (props.editor) {
36-
editor = new Editor(props.editor)
37-
editor.tabId = this.id
38-
}
39-
}
4067
}
4168

4269
class TabGroup extends BaseTabGroup {
4370
static Tab = Tab;
4471
constructor (props={}) {
45-
super(props)
72+
super()
73+
this.id = is.undefined(props.id) ? uniqueId('tab_group_') : props.id
74+
state.tabGroups.set(this.id, this)
4675
extendObservable(this, props)
4776
}
4877

0 commit comments

Comments
 (0)