Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
securingsincity committed May 15, 2017
2 parents 5dac360 + b67c4ed commit 5bed8f1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ Looking for a way to set it up using webpack? Checkout `example` directory for a
|defaultValue | ''| Default value of the editor|
|onLoad| | Function onLoad|
|onBeforeLoad| | function that trigger before editor setup|
|onChange| | function that occurs on document change it has 1 argument value. see the example above|
|onChange| | function that occurs on document change it has 2 arguments the value and the event. see the example above|
|onCopy| | function that trigger by editor `copy` event, and pass text as argument|
|onPaste| | function that trigger by editor `paste` event, and pass text as argument|
|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|
|onFocus| | function that trigger by editor `focus` event|
|onBlur| | function that trigger by editor `blur` event|
|onScroll| | function that trigger by editor `scroll` event|
Expand Down
6 changes: 6 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class App extends Component {
value: newValue
})
}

onSelectionChange(newValue, event) {
console.log('select-change', newValue);
console.log('select-change-event', event);
}
setTheme(e) {
this.setState({
theme: e.target.value
Expand Down Expand Up @@ -212,6 +217,7 @@ class App extends Component {
name="blah2"
onLoad={this.onLoad}
onChange={this.onChange}
onSelectionChange={this.onSelectionChange}
fontSize={this.state.fontSize}
height="100%"
showPrintMargin={this.state.showPrintMargin}
Expand Down
14 changes: 12 additions & 2 deletions src/ace.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default class ReactAce extends Component {
'onBlur',
'onCopy',
'onPaste',
'onSelectionChange',
'onScroll',
'handleOptions',
'updateRef',
Expand Down Expand Up @@ -80,6 +81,7 @@ export default class ReactAce extends Component {
this.editor.on('copy', this.onCopy);
this.editor.on('paste', this.onPaste);
this.editor.on('change', this.onChange);
this.editor.getSession().selection.on('changeSelection', this.onSelectionChange);
this.editor.session.on('changeScrollTop', this.onScroll);
this.handleOptions(this.props);
this.editor.getSession().setAnnotations(annotations || []);
Expand Down Expand Up @@ -203,10 +205,17 @@ export default class ReactAce extends Component {
this.editor = null;
}

onChange() {
onChange(event) {
if (this.props.onChange && !this.silent) {
const value = this.editor.getValue();
this.props.onChange(value);
this.props.onChange(value, event);
}
}

onSelectionChange(event) {
if (this.props.onSelectionChange) {
const value = this.editor.getSelection();
this.props.onSelectionChange(value, event);
}
}

Expand Down Expand Up @@ -308,6 +317,7 @@ ReactAce.propTypes = {
value: PropTypes.string,
defaultValue: PropTypes.string,
onLoad: PropTypes.func,
onSelectionChange: PropTypes.func,
onBeforeLoad: PropTypes.func,
minLines: PropTypes.number,
maxLines: PropTypes.number,
Expand Down
14 changes: 14 additions & 0 deletions tests/src/ace.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ describe('Ace Component', () => {

expect(onChangeCallback.callCount).to.equal(1);
expect(onChangeCallback.getCall(0).args[0]).to.equal(expectText);
expect(onChangeCallback.getCall(0).args[1].action).to.eq('insert')
});

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

it('should call the onSelectionChange method callback', () => {
const onSelectionChangeCallback = sinon.spy();
const wrapper = mount(<AceEditor onSelectionChange={onSelectionChangeCallback}/>, mountOptions);

// Check is not previously called
expect(onSelectionChangeCallback.callCount).to.equal(0);

// Trigger the focus event
wrapper.instance().editor.getSession().selection.selectAll()

expect(onSelectionChangeCallback.callCount).to.equal(1);
});

it('should call the onBlur method callback', () => {
const onBlurCallback = sinon.spy();
const wrapper = mount(<AceEditor onBlur={onBlurCallback}/>, mountOptions);
Expand Down
3 changes: 2 additions & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ export interface AceEditorProps {
scrollMargin?: number[]
onLoad?: (editor: EditorProps) => void
onBeforeLoad?: (ace: any) => void
onChange?: (value: string) => void
onChange?: (value: string, event?: any) => void
onSelection?: (selectedText: string, event?: any) => void
onCopy?: (value: string) => void
onPaste?: (value: string) => void
onFocus?: () => void
Expand Down

0 comments on commit 5bed8f1

Please sign in to comment.