Skip to content

Commit

Permalink
EuiCodeEditor: make it take a custom mode (#935)
Browse files Browse the repository at this point in the history
* Allow mode prop to take object for custom mode

* Adding mode to PropTypes

* Adding note for mode prop type

* Adding to CHANGELOG

* Don't pass down mode prop to AceEditor if a custom mode is in use

Otherwise, AceEditor will complain about wanting a string mode prop.

* Implementing componentDidUpdate check
  • Loading branch information
ycombinator authored Jun 20, 2018
1 parent 4f1ccb0 commit fae538f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Added `getPopoverScreenCoordinates` service function for positioining popover/tooltip content, updated `EuiToolTip` to use it ([#924](https://github.com/elastic/eui/pull/924))
- Allow `mode` prop in `EuiCodeEditor` to take custom mode object ([#935](https://github.com/elastic/eui/pull/935))

## [`0.0.54`](https://github.com/elastic/eui/tree/v0.0.54)

Expand Down
14 changes: 14 additions & 0 deletions src-docs/src/views/code_editor/code_editor_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import ReadOnly from './read_only';
const readOnlySource = require('!!raw-loader!./read_only');
const readOnlyrHtml = renderToHtml(ReadOnly);

import CustomMode from './custom_mode';
const customModeSource = require('!!raw-loader!./custom_mode');
const customModeHtml = renderToHtml(CustomMode);

export const CodeEditorExample = {
title: 'Code Editor',
sections: [{
Expand Down Expand Up @@ -54,5 +58,15 @@ export const CodeEditorExample = {
code: readOnlyrHtml,
}],
demo: <ReadOnly />,
}, {
title: 'Custom mode',
source: [{
type: GuideSectionTypes.JS,
code: customModeSource,
}, {
type: GuideSectionTypes.HTML,
code: customModeHtml,
}],
demo: <CustomMode />,
}],
};
30 changes: 30 additions & 0 deletions src-docs/src/views/code_editor/custom_mode.js
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' }}
/>
);
}
}
32 changes: 32 additions & 0 deletions src/components/code_editor/code_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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([
PropTypes.string,
PropTypes.object
]),
};

EuiCodeEditor.defaultProps = {
Expand Down

0 comments on commit fae538f

Please sign in to comment.