Skip to content
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

Merged
merged 6 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Contributor

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).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1b1b3db.

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([
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When passing mode as an object, react-ace's own proptype fails: Failed prop type: Invalid prop mode of type object supplied to ReactAce, expected string. The code editor's render function should be updated to pass mode as undefined if it is custom.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8b6523e.

PropTypes.string,
PropTypes.object
]),
};

EuiCodeEditor.defaultProps = {
Expand Down