-
Notifications
You must be signed in to change notification settings - Fork 152
/
index.jsx
183 lines (157 loc) · 4.89 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import React, {Component} from 'react';
import CodeMirror from 'codemirror';
import { inject, observer } from 'mobx-react'
import { dispatch } from 'store'
import './addons';
import dispatchCommand from '../../commands/dispatchCommand'
import _ from 'lodash'
import * as TabActions from 'commons/Tab/actions';
function initializeEditor (editorContainer, theme) {
// @todo: add other setting item from config
const editorDOM = document.createElement('div')
Object.assign(editorDOM.style, { width: '100%', height: '100%' })
editorContainer.appendChild(editorDOM)
const editor = CodeMirror(editorDOM, {
theme,
autofocus: true,
lineNumbers: true,
matchBrackets: true,
autoCloseBrackets: true,
})
// 1. resize
editor.setSize('100%', '100%')
// 2. prevent default codemirror dragover handler, so the drag-to-split feature can work
// but the default handler that open a file on drop is actually pretty neat,
// should make our drag feature compatible with it later
editor.on('dragover', (a, e) => e.preventDefault())
editor.isFocused = editor.hasFocus // little hack to make codemirror work with legacy interface
return editor
}
// Ref: codemirror/mode/meta.js
function getMode (tab) {
return CodeMirror.findModeByMIME(tab.contentType) || CodeMirror.findModeByFileName(tab.path.split('/').pop())
}
const debounced = _.debounce(func => func(), 1000)
@inject(state => ({
themeName: state.SettingState.settings.theme.syntax_theme.value,
}))
@observer
class CodeMirrorEditor extends Component {
static defaultProps = {
theme: 'default',
height: '100%',
width: '100%',
};
constructor (props) {
super(props)
this.state = {}
}
componentDidMount () {
const { themeName, tab } = this.props;
let editorInitialized = false
// todo add other setting item from config
if (tab.editor) {
this.editor = tab.editor
this.editorContainer.appendChild(this.editor.getWrapperElement())
} else {
this.editor = tab.editor = initializeEditor(this.editorContainer, themeName)
editorInitialized = true
}
const editor = this.editor
// @fixme:
// related counterparts:
// 1. IdeEnvironment.js
// 2. commandBindings/file.js
window.ide.editors[tab.id] = editor
if (editorInitialized && tab.path && tab.content) {
const body = tab.content.body
const modeInfo = getMode(tab)
if (body) editor.setValue(body)
if (modeInfo) {
let mode = modeInfo.mode
if (mode === 'null') {
editor.setOption('mode', mode)
} else {
require([`codemirror/mode/${mode}/${mode}.js`], () => editor.setOption('mode', mode))
}
}
}
editor.focus()
editor.on('change', this.onChange)
editor.on('focus', this.onFocus)
}
componentWillUnmount () {
this.editor.off('change', this.onChange)
this.editor.off('focus', this.onFocus)
}
onChange = (e) => {
if (!this.isChanging) this.isChanging = true
const { tab } = this.props;
dispatch(TabActions.updateTab({
id: tab.id,
flags: { modified: true },
content: { body: this.editor.getValue() }
}))
if (tab.path) debounced(() => {
dispatchCommand('file:save')
this.isChanging = false
})
}
onFocus = () => {
dispatch(TabActions.activateTab(this.props.tab.id))
}
componentWillReceiveProps ({ tab, themeName }) {
if (tab.flags.modified || !this.editor || !tab.content) return
if (tab.content.body !== this.editor.getValue()) {
this.editor.setValue(tab.content.body)
}
const nextTheme = themeName
const theme = this.props.themeName
if (theme !== nextTheme) this.editor.setOption('theme', nextTheme)
}
render () {
const {width, height} = this.props
const name = this.state.name
const divStyle = { width, height }
return (
<div ref={c => this.editorContainer = c}
id={name}
style={divStyle}
/>
)
}
}
@inject(state => ({
themeName: state.SettingState.settings.theme.syntax_theme.value,
}))
@observer
class TablessCodeMirrorEditor extends Component {
constructor (props) {
super(props)
this.state = {}
}
componentDidMount() {
const { themeName, width, height } = this.props
this.editor = initializeEditor(this.editorContainer, themeName)
this.editor.focus()
this.editor.on('change', this.onChange)
}
onChange = (e) => {
dispatch(TabActions.createTabInGroup(this.props.tabGroupId, {
flags: { modified: true },
content: this.editor.getValue()
}))
}
componentWillReceiveProps ({ themeName }) {
const nextTheme = themeName
const theme = this.props.themeName
if (theme !== nextTheme) this.editor.setOption('theme', nextTheme)
}
render () {
return (
<div ref={ c => this.editorContainer = c } style={{ height: '100%', width: '100%' }} />
)
}
}
export default CodeMirrorEditor
export { TablessCodeMirrorEditor }