Skip to content

Commit

Permalink
Add isReadOnly prop to EuiCodeEditor. (#169)
Browse files Browse the repository at this point in the history
* Add isReadOnly prop to EuiCodeEditor.
* Place cursor at beginning of code editor if it's read-only.
* Surface prompt for isReadOnly mode. Only show cursor when the editor is focused.
  • Loading branch information
cjcenizal authored Nov 30, 2017
1 parent 92af5db commit 4cd31d9
Show file tree
Hide file tree
Showing 7 changed files with 337 additions and 77 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Added icons to match Kibana's app directory [(#162)](https://github.com/elastic/eui/pull/162)
- Convert icons from SVG to React component during the build and stop using
sprites [(#160)](https://github.com/elastic/eui/pull/160)
- Add `isReadOnly`, `setOptions`, and `cursorStart` props to `<EuiCodeEditor>` ([#169](https://github.com/elastic/eui/pull/169))

**Bug fixes**

Expand Down
18 changes: 18 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 @@ -16,6 +16,10 @@ import CodeEditor from './code_editor';
const codeEditorSource = require('!!raw-loader!./code_editor');
const codeEditorHtml = renderToHtml(CodeEditor);

import ReadOnly from './read_only';
const readOnlySource = require('!!raw-loader!./read_only');
const readOnlyrHtml = renderToHtml(ReadOnly);

export default props => (
<GuidePage title={props.route.name}>
<GuideSection
Expand Down Expand Up @@ -44,6 +48,20 @@ export default props => (
<CodeEditor />
}
/>

<GuideSection
title="Read-only"
source={[{
type: GuideSectionTypes.JS,
code: readOnlySource,
}, {
type: GuideSectionTypes.HTML,
code: readOnlyrHtml,
}]}
demo={
<ReadOnly />
}
/>
</GuidePage>
);

27 changes: 27 additions & 0 deletions src-docs/src/views/code_editor/read_only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React, { Component } from 'react';

import 'brace/mode/less';
import 'brace/theme/github';

import {
EuiCodeEditor,
} from '../../../../src/components';

export default class extends Component {
state = {
value: '<p>This code is read only</p>'
};

render() {
return (
<EuiCodeEditor
mode="less"
theme="github"
width="100%"
value={this.state.value}
setOptions={{ fontSize: '14px' }}
isReadOnly
/>
);
}
}
162 changes: 144 additions & 18 deletions src/components/code_editor/__snapshots__/code_editor.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`EuiCodeEditor hint element should be disabled when the ui ace box gains focus 1`] = `
exports[`EuiCodeEditor behavior hint element should be disabled when the ui ace box gains focus 1`] = `
<div
class="euiCodeEditorKeyboardHint euiCodeEditorKeyboardHint-isInactive"
class="euiCodeEditorKeyboardHint"
data-test-subj="codeEditorHint"
id="42"
role="button"
tabindex="0"
>
<p
class="euiText euiVerticalRhythmSmall"
class="euiText"
>
Press Enter to start editing.
Press Enter to start
editing
.
</p>
<p
class="euiText euiVerticalRhythmSmall"
class="euiText"
>
When you’re done, press Escape to stop editing.
When you’re done, press Escape to stop
editing
.
</p>
</div>
`;

exports[`EuiCodeEditor hint element should be enabled when the ui ace box loses focus 1`] = `
exports[`EuiCodeEditor behavior hint element should be enabled when the ui ace box loses focus 1`] = `
<div
class="euiCodeEditorKeyboardHint"
data-test-subj="codeEditorHint"
Expand All @@ -30,19 +34,23 @@ exports[`EuiCodeEditor hint element should be enabled when the ui ace box loses
tabindex="0"
>
<p
class="euiText euiVerticalRhythmSmall"
class="euiText"
>
Press Enter to start editing.
Press Enter to start
editing
.
</p>
<p
class="euiText euiVerticalRhythmSmall"
class="euiText"
>
When you’re done, press Escape to stop editing.
When you’re done, press Escape to stop
editing
.
</p>
</div>
`;

exports[`EuiCodeEditor hint element should be tabable 1`] = `
exports[`EuiCodeEditor behavior hint element should be tabable 1`] = `
<div
class="euiCodeEditorKeyboardHint"
data-test-subj="codeEditorHint"
Expand All @@ -51,14 +59,18 @@ exports[`EuiCodeEditor hint element should be tabable 1`] = `
tabindex="0"
>
<p
class="euiText euiVerticalRhythmSmall"
class="euiText"
>
Press Enter to start editing.
Press Enter to start
editing
.
</p>
<p
class="euiText euiVerticalRhythmSmall"
class="euiText"
>
When you’re done, press Escape to stop editing.
When you’re done, press Escape to stop
editing
.
</p>
</div>
`;
Expand All @@ -75,12 +87,12 @@ exports[`EuiCodeEditor is rendered 1`] = `
tabindex="0"
>
<p
class="euiText euiVerticalRhythmSmall"
class="euiText"
>
Press Enter to start editing.
</p>
<p
class="euiText euiVerticalRhythmSmall"
class="euiText"
>
When you’re done, press Escape to stop editing.
</p>
Expand Down Expand Up @@ -175,3 +187,117 @@ exports[`EuiCodeEditor is rendered 1`] = `
</div>
</div>
`;

exports[`EuiCodeEditor props isReadOnly renders alternate hint text 1`] = `
<div
class="euiCodeEditorWrapper"
>
<div
class="euiCodeEditorKeyboardHint"
data-test-subj="codeEditorHint"
id="42"
role="button"
tabindex="0"
>
<p
class="euiText"
>
Press Enter to start interacting with the code.
</p>
<p
class="euiText"
>
When you’re done, press Escape to stop interacting with the code.
</p>
</div>
<div
class=" ace_editor ace-tm"
id="brace-editor"
style="width: 500px; height: 500px;"
>
<textarea
autocapitalize="off"
autocorrect="off"
class="ace_text-input"
spellcheck="false"
style="opacity: 0;"
tabindex="-1"
wrap="off"
/>
<div
class="ace_gutter"
>
<div
class="ace_layer ace_gutter-layer ace_folding-enabled"
/>
<div
class="ace_gutter-active-line"
style="display: none;"
/>
</div>
<div
class="ace_scroller"
>
<div
class="ace_content"
>
<div
class="ace_layer ace_print-margin-layer"
>
<div
class="ace_print-margin"
style="left: 4px; visibility: visible;"
/>
</div>
<div
class="ace_layer ace_marker-layer"
/>
<div
class="ace_layer ace_text-layer"
style="padding: 0px 4px;"
/>
<div
class="ace_layer ace_marker-layer"
/>
<div
class="ace_layer ace_cursor-layer ace_hidden-cursors"
>
<div
class="ace_cursor"
/>
</div>
</div>
</div>
<div
class="ace_scrollbar ace_scrollbar-v"
style="display: none; width: 20px;"
>
<div
class="ace_scrollbar-inner"
style="width: 20px;"
/>
</div>
<div
class="ace_scrollbar ace_scrollbar-h"
style="display: none; height: 20px;"
>
<div
class="ace_scrollbar-inner"
style="height: 20px;"
/>
</div>
<div
style="height: auto; width: auto; top: 0px; left: 0px; visibility: hidden; position: absolute; white-space: pre; overflow: hidden;"
>
<div
style="height: auto; width: auto; top: 0px; left: 0px; visibility: hidden; position: absolute; white-space: pre; overflow: visible;"
/>
<div
style="height: auto; width: auto; top: 0px; left: 0px; visibility: hidden; position: absolute; white-space: pre; overflow: visible;"
>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
</div>
</div>
</div>
</div>
`;
10 changes: 10 additions & 0 deletions src/components/code_editor/_code_editor.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
.euiCodeEditorWrapper {
position: relative;

.ace_hidden-cursors {
opacity: 0;
}

&.euiCodeEditorWrapper-isEditing {
.ace_hidden-cursors {
opacity: 1;
}
}
}

.euiCodeEditorKeyboardHint {
Expand Down
Loading

0 comments on commit 4cd31d9

Please sign in to comment.