Skip to content

Commit 75f3b82

Browse files
Merge branch 'jaszhix-master'
2 parents 8c5af22 + c6355c7 commit 75f3b82

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Looking for a way to set it up using webpack? Checkout `example` directory for a
7171
|onFocus| function that trigger by editor `focus` event|
7272
|onBlur| function that trigger by editor `blur` event|
7373
|editorProps| Object of properties to apply directly to the Ace editor instance|
74+
|setOptions| Object of [options](https://github.com/ajaxorg/ace/wiki/Configuring-Ace) to apply directly to the Ace editor instance|
7475
|keyboardHandler| String corresponding to the keybinding mode to set (such as vim)|
7576
|commands| Array of new commands to add to the editor
7677

example/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ <h2>Mode: java, theme: github</h2>
1111
<h2>Mode: javascript, theme: monokai</h2>
1212
<div id="example2"></div>
1313
<button onclick="reloadProps();">Reload NEW! Props</button>
14+
<h2>Mode: javascript, theme: monokai (using setOptions property)</h2>
15+
<div id="example3"></div>
16+
<button onclick="reloadProps2();">Reload NEW! Props</button>
1417
<script src="/static/bundle.js"></script>
1518
</body>
1619
</html>

example/index.js

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { render } from 'react-dom'
2+
import { render } from 'react-dom';
33
import AceEditor from 'react-ace';
44
import brace from 'brace';
55

@@ -9,6 +9,7 @@ import 'brace/mode/javascript';
99
import 'brace/theme/github';
1010
import 'brace/theme/monokai';
1111
import 'brace/theme/solarized_light';
12+
import 'brace/ext/language_tools';
1213

1314

1415
function onLoad(editor) {
@@ -62,3 +63,52 @@ global.reloadProps = function() {
6263
document.getElementById('example2')
6364
);
6465
};
66+
67+
// Render the third editor using setOptions prop
68+
69+
const defaultValue2 =
70+
`function onLoad(editor) {
71+
if (true) {
72+
console.log(\"i\'ve loaded\");
73+
}
74+
}`;
75+
76+
render(
77+
<AceEditor
78+
mode="javascript"
79+
theme="monokai"
80+
name="blah3"
81+
onLoad={onLoad}
82+
height="6em"
83+
setOptions={{
84+
enableBasicAutocompletion: false,
85+
enableLiveAutocompletion: false,
86+
tabSize: 4,
87+
fontSize: 14,
88+
showGutter: true
89+
}}
90+
value={defaultValue2}
91+
/>,
92+
document.getElementById('example3')
93+
);
94+
95+
global.reloadProps2 = function() {
96+
render(
97+
<AceEditor
98+
mode="javascript"
99+
theme="monokai"
100+
name="blah3"
101+
onLoad={onLoad}
102+
height="6em"
103+
setOptions={{
104+
enableBasicAutocompletion: true,
105+
enableLiveAutocompletion: true,
106+
tabSize: 2,
107+
fontSize: 16,
108+
showGutter: false
109+
}}
110+
value={defaultValue2}
111+
/>,
112+
document.getElementById('example3')
113+
);
114+
};

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-ace",
3-
"version": "3.2.0",
3+
"version": "3.3.0",
44
"description": "A react component for Ace Editor",
55
"main": "lib/ace.js",
66
"scripts": {
@@ -45,7 +45,8 @@
4545
"brace"
4646
],
4747
"dependencies": {
48-
"brace": "^0.8.0"
48+
"brace": "^0.8.0",
49+
"lodash.isequal": "^4.1.1"
4950
},
5051
"peerDependencies": {
5152
"react": "^0.13.0 || ^0.14.0 || ^15.0.1"

src/ace.jsx

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ace from 'brace';
22
import React, { Component, PropTypes } from 'react';
3+
import isEqual from 'lodash.isequal';
34

45
const editorOptions = [
56
'minLines',
@@ -21,6 +22,7 @@ export default class ReactAce extends Component {
2122
'onBlur',
2223
'onCopy',
2324
'onPaste',
25+
'handleOptions',
2426
]
2527
.forEach(method => {
2628
this[method] = this[method].bind(this);
@@ -67,6 +69,7 @@ export default class ReactAce extends Component {
6769
this.editor.on('copy', this.onCopy);
6870
this.editor.on('paste', this.onPaste);
6971
this.editor.on('change', this.onChange);
72+
this.handleOptions(this.props);
7073

7174
for (let i = 0; i < editorOptions.length; i++) {
7275
const option = editorOptions[i];
@@ -116,6 +119,9 @@ export default class ReactAce extends Component {
116119
if (nextProps.showGutter !== oldProps.showGutter) {
117120
this.editor.renderer.setShowGutter(nextProps.showGutter);
118121
}
122+
if (!isEqual(nextProps.setOptions, oldProps.setOptions)) {
123+
this.handleOptions(nextProps);
124+
}
119125
if (this.editor.getValue() !== nextProps.value) {
120126
// editor.setValue is a synchronous function call, change event is emitted before setValue return.
121127
this.silent = true;
@@ -160,6 +166,13 @@ export default class ReactAce extends Component {
160166
}
161167
}
162168

169+
handleOptions(props) {
170+
const setOptions = Object.keys(props.setOptions);
171+
for (let y = 0; y < setOptions.length; y++) {
172+
this.editor.setOption(setOptions[y], props.setOptions[setOptions[y]]);
173+
}
174+
}
175+
163176
render() {
164177
const { name, className, width, height } = this.props;
165178
const divStyle = { width, height };
@@ -199,6 +212,7 @@ ReactAce.propTypes = {
199212
showPrintMargin: PropTypes.bool,
200213
cursorStart: PropTypes.number,
201214
editorProps: PropTypes.object,
215+
setOptions: PropTypes.object,
202216
keyboardHandler: PropTypes.string,
203217
wrapEnabled: PropTypes.bool,
204218
enableBasicAutocompletion: PropTypes.oneOfType([
@@ -232,6 +246,7 @@ ReactAce.defaultProps = {
232246
tabSize: 4,
233247
cursorStart: 1,
234248
editorProps: {},
249+
setOptions: {},
235250
wrapEnabled: false,
236251
enableBasicAutocompletion: false,
237252
enableLiveAutocompletion: false,

0 commit comments

Comments
 (0)