Skip to content

Commit 5bed8f1

Browse files
2 parents 5dac360 + b67c4ed commit 5bed8f1

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ Looking for a way to set it up using webpack? Checkout `example` directory for a
7070
|defaultValue | ''| Default value of the editor|
7171
|onLoad| | Function onLoad|
7272
|onBeforeLoad| | function that trigger before editor setup|
73-
|onChange| | function that occurs on document change it has 1 argument value. see the example above|
73+
|onChange| | function that occurs on document change it has 2 arguments the value and the event. see the example above|
7474
|onCopy| | function that trigger by editor `copy` event, and pass text as argument|
7575
|onPaste| | function that trigger by editor `paste` event, and pass text as argument|
76+
|onSelectionChange| | function that trigger by editor `selectionChange` event, and passes a [Selection](https://ace.c9.io/#nav=api&api=selection) as it's first argument and the event as the second|
7677
|onFocus| | function that trigger by editor `focus` event|
7778
|onBlur| | function that trigger by editor `blur` event|
7879
|onScroll| | function that trigger by editor `scroll` event|

example/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ class App extends Component {
6464
value: newValue
6565
})
6666
}
67+
68+
onSelectionChange(newValue, event) {
69+
console.log('select-change', newValue);
70+
console.log('select-change-event', event);
71+
}
6772
setTheme(e) {
6873
this.setState({
6974
theme: e.target.value
@@ -212,6 +217,7 @@ class App extends Component {
212217
name="blah2"
213218
onLoad={this.onLoad}
214219
onChange={this.onChange}
220+
onSelectionChange={this.onSelectionChange}
215221
fontSize={this.state.fontSize}
216222
height="100%"
217223
showPrintMargin={this.state.showPrintMargin}

src/ace.jsx

+12-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default class ReactAce extends Component {
2525
'onBlur',
2626
'onCopy',
2727
'onPaste',
28+
'onSelectionChange',
2829
'onScroll',
2930
'handleOptions',
3031
'updateRef',
@@ -80,6 +81,7 @@ export default class ReactAce extends Component {
8081
this.editor.on('copy', this.onCopy);
8182
this.editor.on('paste', this.onPaste);
8283
this.editor.on('change', this.onChange);
84+
this.editor.getSession().selection.on('changeSelection', this.onSelectionChange);
8385
this.editor.session.on('changeScrollTop', this.onScroll);
8486
this.handleOptions(this.props);
8587
this.editor.getSession().setAnnotations(annotations || []);
@@ -203,10 +205,17 @@ export default class ReactAce extends Component {
203205
this.editor = null;
204206
}
205207

206-
onChange() {
208+
onChange(event) {
207209
if (this.props.onChange && !this.silent) {
208210
const value = this.editor.getValue();
209-
this.props.onChange(value);
211+
this.props.onChange(value, event);
212+
}
213+
}
214+
215+
onSelectionChange(event) {
216+
if (this.props.onSelectionChange) {
217+
const value = this.editor.getSelection();
218+
this.props.onSelectionChange(value, event);
210219
}
211220
}
212221

@@ -308,6 +317,7 @@ ReactAce.propTypes = {
308317
value: PropTypes.string,
309318
defaultValue: PropTypes.string,
310319
onLoad: PropTypes.func,
320+
onSelectionChange: PropTypes.func,
311321
onBeforeLoad: PropTypes.func,
312322
minLines: PropTypes.number,
313323
maxLines: PropTypes.number,

tests/src/ace.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ describe('Ace Component', () => {
125125

126126
expect(onChangeCallback.callCount).to.equal(1);
127127
expect(onChangeCallback.getCall(0).args[0]).to.equal(expectText);
128+
expect(onChangeCallback.getCall(0).args[1].action).to.eq('insert')
128129
});
129130

130131
it('should call the onCopy method', () => {
@@ -170,6 +171,19 @@ describe('Ace Component', () => {
170171
expect(onFocusCallback.callCount).to.equal(1);
171172
});
172173

174+
it('should call the onSelectionChange method callback', () => {
175+
const onSelectionChangeCallback = sinon.spy();
176+
const wrapper = mount(<AceEditor onSelectionChange={onSelectionChangeCallback}/>, mountOptions);
177+
178+
// Check is not previously called
179+
expect(onSelectionChangeCallback.callCount).to.equal(0);
180+
181+
// Trigger the focus event
182+
wrapper.instance().editor.getSession().selection.selectAll()
183+
184+
expect(onSelectionChangeCallback.callCount).to.equal(1);
185+
});
186+
173187
it('should call the onBlur method callback', () => {
174188
const onBlurCallback = sinon.spy();
175189
const wrapper = mount(<AceEditor onBlur={onBlurCallback}/>, mountOptions);

types.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ export interface AceEditorProps {
149149
scrollMargin?: number[]
150150
onLoad?: (editor: EditorProps) => void
151151
onBeforeLoad?: (ace: any) => void
152-
onChange?: (value: string) => void
152+
onChange?: (value: string, event?: any) => void
153+
onSelection?: (selectedText: string, event?: any) => void
153154
onCopy?: (value: string) => void
154155
onPaste?: (value: string) => void
155156
onFocus?: () => void

0 commit comments

Comments
 (0)