-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9281 from keithjgrant/8905-codemirror-replacement
AceEditor - codemirror replacement Reviewed-by: John Hill <johill@redhat.com> https://github.com/unlikelyzero
- Loading branch information
Showing
67 changed files
with
442 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
import React, { useEffect, useRef, useCallback } from 'react'; | ||
import { oneOf, bool, number, string, func } from 'prop-types'; | ||
import ReactAce from 'react-ace'; | ||
import 'ace-builds/src-noconflict/mode-json'; | ||
import 'ace-builds/src-noconflict/mode-javascript'; | ||
import 'ace-builds/src-noconflict/mode-yaml'; | ||
import 'ace-builds/src-noconflict/mode-django'; | ||
import 'ace-builds/src-noconflict/theme-github'; | ||
import { withI18n } from '@lingui/react'; | ||
import { t } from '@lingui/macro'; | ||
import styled from 'styled-components'; | ||
import debounce from '../../util/debounce'; | ||
|
||
const LINE_HEIGHT = 24; | ||
const PADDING = 12; | ||
|
||
const FocusWrapper = styled.div` | ||
&& + .keyboard-help-text { | ||
opacity: 0; | ||
transition: opacity 0.1s linear; | ||
} | ||
&:focus-within + .keyboard-help-text { | ||
opacity: 1; | ||
} | ||
& .ace_hidden-cursors .ace_cursor { | ||
opacity: 0; | ||
} | ||
`; | ||
|
||
const AceEditor = styled(ReactAce)` | ||
font-family: var(--pf-global--FontFamily--monospace); | ||
max-height: 90vh; | ||
& .ace_gutter, | ||
& .ace_scroller { | ||
padding-top: 4px; | ||
padding-bottom: 4px; | ||
} | ||
& .ace_mobile-menu { | ||
display: none; | ||
} | ||
${props => | ||
props.hasErrors && | ||
` | ||
&& { | ||
--pf-c-form-control--PaddingRight: var(--pf-c-form-control--invalid--PaddingRight); | ||
--pf-c-form-control--BorderBottomColor: var(--pf-c-form-control--invalid--BorderBottomColor); | ||
padding-right: 24px; | ||
padding-bottom: var(--pf-c-form-control--invalid--PaddingBottom); | ||
background: var(--pf-c-form-control--invalid--Background); | ||
border-bottom-width: var(--pf-c-form-control--invalid--BorderBottomWidth); | ||
}`} | ||
${props => | ||
props.setOptions.readOnly && | ||
` | ||
&& .ace_cursor { | ||
opacity: 0; | ||
} | ||
`} | ||
`; | ||
AceEditor.displayName = 'AceEditor'; | ||
|
||
function CodeEditor({ | ||
id, | ||
value, | ||
onChange, | ||
mode, | ||
readOnly, | ||
hasErrors, | ||
rows, | ||
fullHeight, | ||
className, | ||
i18n, | ||
}) { | ||
const wrapper = useRef(null); | ||
const editor = useRef(null); | ||
|
||
useEffect( | ||
function removeTextareaTabIndex() { | ||
const editorInput = editor.current.refEditor?.querySelector('textarea'); | ||
if (editorInput && !readOnly) { | ||
editorInput.tabIndex = -1; | ||
} | ||
}, | ||
[readOnly] | ||
); | ||
|
||
const listen = useCallback(event => { | ||
if (wrapper.current === document.activeElement && event.key === 'Enter') { | ||
const editorInput = editor.current.refEditor?.querySelector('textarea'); | ||
if (!editorInput) { | ||
return; | ||
} | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
editorInput.focus(); | ||
} | ||
}, []); | ||
|
||
useEffect(function addKeyEventListeners() { | ||
const wrapperEl = wrapper.current; | ||
wrapperEl.addEventListener('keydown', listen); | ||
|
||
return () => { | ||
wrapperEl.removeEventListener('keydown', listen); | ||
}; | ||
}); | ||
|
||
const aceModes = { | ||
javascript: 'json', | ||
yaml: 'yaml', | ||
jinja2: 'django', | ||
}; | ||
|
||
const numRows = fullHeight ? value.split('\n').length : rows; | ||
|
||
return ( | ||
<> | ||
<FocusWrapper ref={wrapper} tabIndex={readOnly ? -1 : 0}> | ||
<AceEditor | ||
mode={aceModes[mode] || 'text'} | ||
className={`pf-c-form-control ${className}`} | ||
theme="github" | ||
onChange={debounce(onChange, 250)} | ||
value={value} | ||
name={id || 'code-editor'} | ||
editorProps={{ $blockScrolling: true }} | ||
fontSize={16} | ||
width="100%" | ||
height={`${numRows * LINE_HEIGHT + PADDING}px`} | ||
hasErrors={hasErrors} | ||
setOptions={{ | ||
readOnly, | ||
highlightActiveLine: !readOnly, | ||
highlightGutterLine: !readOnly, | ||
useWorker: false, | ||
showPrintMargin: false, | ||
}} | ||
commands={[ | ||
{ | ||
name: 'escape', | ||
bindKey: { win: 'Esc', mac: 'Esc' }, | ||
exec: () => { | ||
wrapper.current.focus(); | ||
}, | ||
}, | ||
{ | ||
name: 'tab escape', | ||
bindKey: { win: 'Shift-Tab', mac: 'Shift-Tab' }, | ||
exec: () => { | ||
wrapper.current.focus(); | ||
}, | ||
}, | ||
]} | ||
ref={editor} | ||
/> | ||
</FocusWrapper> | ||
{!readOnly && ( | ||
<div | ||
className="pf-c-form__helper-text keyboard-help-text" | ||
aria-live="polite" | ||
> | ||
{i18n._(t`Press Enter to edit. Press ESC to stop editing.`)} | ||
</div> | ||
)} | ||
</> | ||
); | ||
} | ||
CodeEditor.propTypes = { | ||
value: string.isRequired, | ||
onChange: func, | ||
mode: oneOf(['javascript', 'yaml', 'jinja2']).isRequired, | ||
readOnly: bool, | ||
hasErrors: bool, | ||
fullHeight: bool, | ||
rows: number, | ||
className: string, | ||
}; | ||
CodeEditor.defaultProps = { | ||
readOnly: false, | ||
onChange: () => {}, | ||
rows: 6, | ||
fullHeight: false, | ||
hasErrors: false, | ||
className: '', | ||
}; | ||
|
||
export default withI18n()(CodeEditor); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react'; | ||
import { mountWithContexts } from '../../../testUtils/enzymeHelpers'; | ||
import CodeEditor from './CodeEditor'; | ||
import debounce from '../../util/debounce'; | ||
|
||
jest.mock('../../util/debounce'); | ||
|
||
describe('CodeEditor', () => { | ||
beforeEach(() => { | ||
document.body.createTextRange = jest.fn(); | ||
}); | ||
|
||
it('should pass value and mode through to ace editor', () => { | ||
const onChange = jest.fn(); | ||
const wrapper = mountWithContexts( | ||
<CodeEditor value={'---\nfoo: bar'} onChange={onChange} mode="yaml" /> | ||
); | ||
const aceEditor = wrapper.find('AceEditor'); | ||
expect(aceEditor.prop('mode')).toEqual('yaml'); | ||
expect(aceEditor.prop('setOptions').readOnly).toEqual(false); | ||
expect(aceEditor.prop('value')).toEqual('---\nfoo: bar'); | ||
}); | ||
|
||
it('should trigger onChange prop', () => { | ||
debounce.mockImplementation(fn => fn); | ||
const onChange = jest.fn(); | ||
const wrapper = mountWithContexts( | ||
<CodeEditor value="---" onChange={onChange} mode="yaml" /> | ||
); | ||
const aceEditor = wrapper.find('AceEditor'); | ||
aceEditor.prop('onChange')('newvalue'); | ||
expect(onChange).toHaveBeenCalledWith('newvalue'); | ||
}); | ||
|
||
it('should render in read only mode', () => { | ||
const onChange = jest.fn(); | ||
const wrapper = mountWithContexts( | ||
<CodeEditor value="---" onChange={onChange} mode="yaml" readOnly /> | ||
); | ||
const aceEditor = wrapper.find('AceEditor'); | ||
expect(aceEditor.prop('setOptions').readOnly).toEqual(true); | ||
expect(aceEditor.prop('value')).toEqual('---'); | ||
}); | ||
}); |
Oops, something went wrong.