-
Notifications
You must be signed in to change notification settings - Fork 843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EuiCodeEditor: make it take a custom mode #935
Changes from all commits
36e842a
27ff3d0
a35db56
eb87680
8b6523e
1b1b3db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React, { Component } from 'react'; | ||
import 'brace/mode/text'; | ||
|
||
import { | ||
EuiCodeEditor, | ||
} from '../../../../src/components'; | ||
|
||
const TextMode = window.ace.acequire('ace/mode/text').Mode; | ||
class MyCustomAceMode extends TextMode { | ||
// Your custom mode definition goes here. | ||
// See https://github.com/ajaxorg/ace/wiki/Creating-or-Extending-an-Edit-Mode | ||
} | ||
|
||
export default class extends Component { | ||
state = { | ||
value: '' | ||
}; | ||
|
||
render() { | ||
return ( | ||
<EuiCodeEditor | ||
mode={new MyCustomAceMode()} | ||
theme="github" | ||
width="100%" | ||
value={this.state.value} | ||
setOptions={{ fontSize: '14px' }} | ||
/> | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,26 @@ export class EuiCodeEditor extends Component { | |
}); | ||
} | ||
|
||
isCustomMode() { | ||
return typeof this.props.mode === 'object'; | ||
} | ||
|
||
setCustomMode() { | ||
this.aceEditor.editor.getSession().setMode(this.props.mode); | ||
} | ||
|
||
componentDidMount() { | ||
if (this.isCustomMode()) { | ||
this.setCustomMode(); | ||
} | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
if ((this.props.mode !== prevProps.mode) && this.isCustomMode()) { | ||
this.setCustomMode(); | ||
} | ||
} | ||
|
||
render() { | ||
const { | ||
width, | ||
|
@@ -136,6 +156,10 @@ export class EuiCodeEditor extends Component { | |
</div> | ||
); | ||
|
||
if (this.isCustomMode()) { | ||
delete rest.mode; // Otherwise, the AceEditor component will complain about wanting a string value for the mode prop. | ||
} | ||
|
||
return ( | ||
<div | ||
className={classes} | ||
|
@@ -165,6 +189,14 @@ EuiCodeEditor.propTypes = { | |
isReadOnly: PropTypes.bool, | ||
setOptions: PropTypes.object, | ||
cursorStart: PropTypes.number, | ||
|
||
/** | ||
* Use string for a built-in mode or object for a custom mode | ||
*/ | ||
mode: PropTypes.oneOfType([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When passing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 8b6523e. |
||
PropTypes.string, | ||
PropTypes.object | ||
]), | ||
}; | ||
|
||
EuiCodeEditor.defaultProps = { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should also add
componentDidUpdate
and compare old props.mode with new props.mode, updating the session if the old & new modes differ and if the new mode is a custom object (switching to a string is handled internally by react-ace).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 1b1b3db.