-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathCodemirror.js
134 lines (131 loc) · 4.02 KB
/
Codemirror.js
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
const React = require('react');
const ReactDOM = require('react-dom');
const PropTypes = require('prop-types');
const className = require('classnames');
const debounce = require('lodash.debounce');
const isEqual = require('lodash.isequal');
const createReactClass = require('create-react-class');
function normalizeLineEndings (str) {
if (!str) return str;
return str.replace(/\r\n|\r/g, '\n');
}
const CodeMirror = createReactClass({
propTypes: {
autoFocus: PropTypes.bool,
className: PropTypes.any,
codeMirrorInstance: PropTypes.func,
defaultValue: PropTypes.string,
name: PropTypes.string,
onChange: PropTypes.func,
onCursorActivity: PropTypes.func,
onFocusChange: PropTypes.func,
onScroll: PropTypes.func,
options: PropTypes.object,
path: PropTypes.string,
value: PropTypes.string,
preserveScrollPosition: PropTypes.bool,
},
getDefaultProps () {
return {
preserveScrollPosition: false,
};
},
getCodeMirrorInstance () {
return this.props.codeMirrorInstance || require('codemirror');
},
getInitialState () {
return {
isFocused: false,
};
},
componentWillMount () {
this.componentWillReceiveProps = debounce(this.componentWillReceiveProps, 0);
if (this.props.path) {
console.error('Warning: react-codemirror: the `path` prop has been changed to `name`');
}
},
componentDidMount () {
const codeMirrorInstance = this.getCodeMirrorInstance();
this.codeMirror = codeMirrorInstance.fromTextArea(this.textareaNode, this.props.options);
this.codeMirror.on('change', this.codemirrorValueChanged);
this.codeMirror.on('cursorActivity', this.cursorActivity);
this.codeMirror.on('focus', this.focusChanged.bind(this, true));
this.codeMirror.on('blur', this.focusChanged.bind(this, false));
this.codeMirror.on('scroll', this.scrollChanged);
this.codeMirror.setValue(this.props.defaultValue || this.props.value || '');
},
componentWillUnmount () {
// is there a lighter-weight way to remove the cm instance?
if (this.codeMirror) {
this.codeMirror.toTextArea();
}
},
componentWillReceiveProps: function (nextProps) {
if (this.codeMirror && nextProps.value !== undefined && nextProps.value !== this.props.value && normalizeLineEndings(this.codeMirror.getValue()) !== normalizeLineEndings(nextProps.value)) {
if (this.props.preserveScrollPosition) {
var prevScrollPosition = this.codeMirror.getScrollInfo();
this.codeMirror.setValue(nextProps.value);
this.codeMirror.scrollTo(prevScrollPosition.left, prevScrollPosition.top);
} else {
this.codeMirror.setValue(nextProps.value);
}
}
if (typeof nextProps.options === 'object') {
for (let optionName in nextProps.options) {
if (nextProps.options.hasOwnProperty(optionName)) {
this.setOptionIfChanged(optionName, nextProps.options[optionName]);
}
}
}
},
setOptionIfChanged (optionName, newValue) {
const oldValue = this.codeMirror.getOption(optionName);
if (!isEqual(oldValue, newValue)) {
this.codeMirror.setOption(optionName, newValue);
}
},
getCodeMirror () {
return this.codeMirror;
},
focus () {
if (this.codeMirror) {
this.codeMirror.focus();
}
},
focusChanged (focused) {
this.setState({
isFocused: focused,
});
this.props.onFocusChange && this.props.onFocusChange(focused);
},
cursorActivity (cm) {
this.props.onCursorActivity && this.props.onCursorActivity(cm);
},
scrollChanged (cm) {
this.props.onScroll && this.props.onScroll(cm.getScrollInfo());
},
codemirrorValueChanged (doc, change) {
if (this.props.onChange && change.origin !== 'setValue') {
this.props.onChange(doc.getValue(), change);
}
},
render () {
const editorClassName = className(
'ReactCodeMirror',
this.state.isFocused ? 'ReactCodeMirror--focused' : null,
this.props.className
);
return (
<div className={editorClassName}>
<textarea
ref={ref => this.textareaNode = ref}
name={this.props.name || this.props.path}
defaultValue={this.props.value}
autoComplete="off"
autoFocus={this.props.autoFocus}
/>
</div>
);
},
});
module.exports = CodeMirror;