2017-02-28 06:26:24 +00:00
|
|
|
import React, {PureComponent, PropTypes} from 'react';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2017-03-09 06:23:23 +00:00
|
|
|
import CodeEditor from './code-editor';
|
2017-03-08 05:52:17 +00:00
|
|
|
import Input from '../base/debounced-input';
|
2017-03-01 21:15:56 +00:00
|
|
|
|
|
|
|
const MODE_INPUT = 'input';
|
|
|
|
const MODE_EDITOR = 'editor';
|
|
|
|
const TYPE_TEXT = 'text';
|
|
|
|
const NUNJUCKS_REGEX = /({%|%}|{{|}})/;
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 06:26:24 +00:00
|
|
|
class OneLineEditor extends PureComponent {
|
2017-02-27 21:00:13 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
2017-03-01 21:15:56 +00:00
|
|
|
|
2017-03-08 00:56:51 +00:00
|
|
|
let mode;
|
2017-03-09 06:23:23 +00:00
|
|
|
if (props.forceInput) {
|
|
|
|
mode = MODE_INPUT;
|
|
|
|
} else if (props.forceEditor) {
|
2017-03-01 21:15:56 +00:00
|
|
|
mode = MODE_EDITOR;
|
|
|
|
} else if (this._mayContainNunjucks(props.defaultValue)) {
|
|
|
|
mode = MODE_EDITOR;
|
2017-03-08 00:56:51 +00:00
|
|
|
} else {
|
|
|
|
mode = MODE_INPUT;
|
2017-03-01 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 00:31:23 +00:00
|
|
|
this.state = {
|
|
|
|
mode
|
|
|
|
};
|
2017-02-27 21:00:13 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
focus (setToEnd = false) {
|
2017-03-01 21:15:56 +00:00
|
|
|
if (this.state.mode === MODE_EDITOR) {
|
2017-03-09 06:23:23 +00:00
|
|
|
if (this._editor && !this._editor.hasFocus()) {
|
|
|
|
setToEnd ? this._editor.focusEnd() : this._editor.focus();
|
2017-03-01 21:15:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-09 06:23:23 +00:00
|
|
|
if (this._input && !this._input.hasFocus()) {
|
|
|
|
setToEnd ? this._input.focusEnd() : this._input.focus();
|
|
|
|
}
|
2017-03-01 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 21:10:35 +00:00
|
|
|
focusEnd () {
|
2017-03-09 06:23:23 +00:00
|
|
|
this.focus(true);
|
2017-03-03 21:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
selectAll () {
|
|
|
|
if (this.state.mode === MODE_EDITOR) {
|
|
|
|
this._editor.selectAll();
|
|
|
|
} else {
|
|
|
|
this._input.select();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 21:15:56 +00:00
|
|
|
getValue () {
|
|
|
|
if (this.state.mode === MODE_EDITOR) {
|
|
|
|
return this._editor.getValue();
|
|
|
|
} else {
|
|
|
|
return this._input.getValue();
|
2017-02-28 06:26:24 +00:00
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 00:31:23 +00:00
|
|
|
_handleInputDragEnter () {
|
|
|
|
this._convertToEditorPreserveFocus();
|
|
|
|
}
|
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
_handleInputMouseEnter () {
|
2017-03-23 22:10:42 +00:00
|
|
|
// Convert to editor when user hovers mouse over input
|
|
|
|
/*
|
|
|
|
* NOTE: we're doing it in a timeout because we don't want to convert if the
|
|
|
|
* mouse goes in an out right away.
|
|
|
|
*/
|
|
|
|
this._mouseEnterTimeout = setTimeout(this._convertToEditorPreserveFocus, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleInputMouseLeave () {
|
|
|
|
clearTimeout(this._mouseEnterTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleEditorMouseLeave () {
|
|
|
|
this._convertToInputIfNotFocused();
|
2017-03-09 06:23:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleEditorFocus (e) {
|
2017-03-09 06:23:23 +00:00
|
|
|
const focusedFromTabEvent = !!e.sourceCapabilities;
|
|
|
|
|
|
|
|
if (focusedFromTabEvent) {
|
|
|
|
this._editor.focusEnd();
|
|
|
|
}
|
|
|
|
|
2017-03-09 07:38:48 +00:00
|
|
|
// Set focused state
|
|
|
|
this._editor.setAttribute('data-focused', 'on');
|
|
|
|
|
2017-03-01 21:15:56 +00:00
|
|
|
this.props.onFocus && this.props.onFocus(e);
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2017-03-01 21:15:56 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleInputFocus (e) {
|
2017-03-09 06:23:23 +00:00
|
|
|
// If we're focusing the whole thing, blur the input. This happens when
|
|
|
|
// the user tabs to the field.
|
|
|
|
const start = this._input.getSelectionStart();
|
|
|
|
const end = this._input.getSelectionEnd();
|
|
|
|
const focusedFromTabEvent = start === 0 && end === e.target.value.length;
|
2017-03-01 21:15:56 +00:00
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
if (focusedFromTabEvent) {
|
|
|
|
this._input.focusEnd();
|
|
|
|
|
|
|
|
// Also convert to editor if we tabbed to it. Just in case the user
|
|
|
|
// needs an editor
|
|
|
|
this._convertToEditorPreserveFocus();
|
2017-03-01 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 07:38:48 +00:00
|
|
|
// Set focused state
|
|
|
|
this._input.setAttribute('data-focused', 'on');
|
|
|
|
|
2017-03-01 21:15:56 +00:00
|
|
|
// Also call the regular callback
|
|
|
|
this.props.onFocus && this.props.onFocus(e);
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2017-03-01 21:15:56 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleInputChange (value) {
|
2017-03-09 06:23:23 +00:00
|
|
|
this._convertToEditorPreserveFocus();
|
2017-02-27 21:00:13 +00:00
|
|
|
this.props.onChange && this.props.onChange(value);
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleInputKeyDown (e) {
|
2017-03-01 21:15:56 +00:00
|
|
|
if (this.props.onKeyDown) {
|
|
|
|
this.props.onKeyDown(e, e.target.value);
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2017-03-01 21:15:56 +00:00
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
_handleInputBlur () {
|
2017-03-09 07:38:48 +00:00
|
|
|
// Set focused state
|
|
|
|
this._input.removeAttribute('data-focused');
|
|
|
|
|
2017-03-03 21:10:35 +00:00
|
|
|
this.props.onBlur && this.props.onBlur();
|
2017-03-09 06:23:23 +00:00
|
|
|
}
|
2017-03-03 21:10:35 +00:00
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
_handleEditorBlur () {
|
|
|
|
// Editor was already removed from the DOM, so do nothing
|
|
|
|
if (!this._editor) {
|
2017-03-01 21:15:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-09 07:38:48 +00:00
|
|
|
// Set focused state
|
|
|
|
this._editor.removeAttribute('data-focused');
|
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
this._editor.clearSelection();
|
|
|
|
|
|
|
|
if (!this.props.forceEditor) {
|
2017-03-16 17:51:56 +00:00
|
|
|
// Convert back to input sometime in the future.
|
|
|
|
// NOTE: this was originally added because the input would disappear if
|
|
|
|
// the user tabbed away very shortly after typing, but it's actually a pretty
|
|
|
|
// good feature.
|
|
|
|
setTimeout(() => {
|
|
|
|
this._convertToInputIfNotFocused();
|
|
|
|
}, 2000);
|
2017-03-09 06:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.props.onBlur && this.props.onBlur();
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2017-03-01 21:15:56 +00:00
|
|
|
|
2017-03-12 00:31:23 +00:00
|
|
|
_handleKeyDown (e) {
|
2017-03-01 00:10:23 +00:00
|
|
|
// submit form if needed
|
|
|
|
if (e.keyCode === 13) {
|
|
|
|
let node = e.target;
|
|
|
|
for (let i = 0; i < 20 && node; i++) {
|
|
|
|
if (node.tagName === 'FORM') {
|
2017-03-03 20:09:08 +00:00
|
|
|
node.dispatchEvent(new window.Event('submit'));
|
2017-03-01 00:10:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
node = node.parentNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 21:15:56 +00:00
|
|
|
this.props.onKeyDown && this.props.onKeyDown(e, this.getValue());
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2017-03-01 00:10:23 +00:00
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
_convertToEditorPreserveFocus () {
|
|
|
|
if (this.state.mode !== MODE_INPUT || this.props.forceInput) {
|
2017-03-08 00:56:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
if (this._input.hasFocus()) {
|
|
|
|
const start = this._input.getSelectionStart();
|
|
|
|
const end = this._input.getSelectionEnd();
|
2017-03-08 00:56:51 +00:00
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
// Wait for the editor to swap and restore cursor position
|
|
|
|
const check = () => {
|
|
|
|
if (this._editor) {
|
|
|
|
this._editor.focus();
|
|
|
|
this._editor.setSelection(start, end);
|
|
|
|
} else {
|
|
|
|
setTimeout(check, 40);
|
|
|
|
}
|
|
|
|
};
|
2017-03-08 00:56:51 +00:00
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
// Tell the component to show the editor
|
|
|
|
setTimeout(check);
|
2017-03-08 00:56:51 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
this.setState({mode: MODE_EDITOR});
|
2017-03-08 00:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_convertToInputIfNotFocused () {
|
|
|
|
if (this.state.mode === MODE_INPUT || this.props.forceEditor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-16 17:51:56 +00:00
|
|
|
if (!this._editor || this._editor.hasFocus()) {
|
2017-03-08 00:56:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._mayContainNunjucks(this.getValue())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({mode: MODE_INPUT});
|
|
|
|
}
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_setEditorRef (n) {
|
|
|
|
this._editor = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setInputRef (n) {
|
|
|
|
this._input = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
_mayContainNunjucks (text) {
|
|
|
|
return !!text.match(NUNJUCKS_REGEX);
|
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
|
|
|
|
render () {
|
2017-03-01 21:15:56 +00:00
|
|
|
const {
|
2017-03-23 22:10:42 +00:00
|
|
|
id,
|
2017-03-01 21:15:56 +00:00
|
|
|
defaultValue,
|
|
|
|
className,
|
|
|
|
onChange,
|
|
|
|
placeholder,
|
|
|
|
render,
|
2017-04-04 23:06:43 +00:00
|
|
|
onPaste,
|
2017-03-12 00:31:23 +00:00
|
|
|
getRenderContext,
|
|
|
|
getAutocompleteConstants,
|
2017-03-31 21:59:12 +00:00
|
|
|
mode: syntaxMode,
|
2017-03-03 20:09:08 +00:00
|
|
|
type: originalType
|
2017-03-01 21:15:56 +00:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const {mode} = this.state;
|
|
|
|
|
|
|
|
const type = originalType || TYPE_TEXT;
|
|
|
|
const showEditor = type === TYPE_TEXT && mode === MODE_EDITOR;
|
|
|
|
|
|
|
|
if (showEditor) {
|
|
|
|
return (
|
2017-03-09 06:23:23 +00:00
|
|
|
<CodeEditor
|
2017-03-01 21:15:56 +00:00
|
|
|
ref={this._setEditorRef}
|
|
|
|
defaultTabBehavior
|
|
|
|
hideLineNumbers
|
|
|
|
hideScrollbars
|
|
|
|
noMatchBrackets
|
|
|
|
noStyleActiveLine
|
|
|
|
noLint
|
|
|
|
singleLine
|
|
|
|
tabIndex={0}
|
2017-03-23 22:10:42 +00:00
|
|
|
id={id}
|
2017-03-31 21:59:12 +00:00
|
|
|
mode={syntaxMode}
|
2017-03-01 21:15:56 +00:00
|
|
|
placeholder={placeholder}
|
2017-04-04 23:06:43 +00:00
|
|
|
onPaste={onPaste}
|
2017-03-01 21:15:56 +00:00
|
|
|
onBlur={this._handleEditorBlur}
|
2017-03-12 00:31:23 +00:00
|
|
|
onKeyDown={this._handleKeyDown}
|
2017-03-01 21:15:56 +00:00
|
|
|
onFocus={this._handleEditorFocus}
|
2017-03-09 06:23:23 +00:00
|
|
|
onMouseLeave={this._handleEditorMouseLeave}
|
2017-03-03 01:44:07 +00:00
|
|
|
onChange={onChange}
|
2017-03-01 21:15:56 +00:00
|
|
|
render={render}
|
2017-03-12 00:31:23 +00:00
|
|
|
getRenderContext={getRenderContext}
|
|
|
|
getAutocompleteConstants={getAutocompleteConstants}
|
2017-03-09 07:38:48 +00:00
|
|
|
className="editor--single-line"
|
2017-03-01 21:15:56 +00:00
|
|
|
defaultValue={defaultValue}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<Input
|
|
|
|
ref={this._setInputRef}
|
2017-03-23 22:10:42 +00:00
|
|
|
id={id}
|
2017-03-01 21:15:56 +00:00
|
|
|
type={type}
|
2017-03-09 07:38:48 +00:00
|
|
|
className={className}
|
2017-03-08 00:56:51 +00:00
|
|
|
style={{
|
2017-03-09 06:23:23 +00:00
|
|
|
// background: 'rgba(255, 0, 0, 0.05)', // For debugging
|
2017-03-08 00:56:51 +00:00
|
|
|
width: '100%'
|
|
|
|
}}
|
2017-03-01 21:15:56 +00:00
|
|
|
placeholder={placeholder}
|
|
|
|
defaultValue={defaultValue}
|
2017-03-09 06:23:23 +00:00
|
|
|
onBlur={this._handleInputBlur}
|
2017-03-01 21:15:56 +00:00
|
|
|
onChange={this._handleInputChange}
|
2017-03-09 06:23:23 +00:00
|
|
|
onMouseEnter={this._handleInputMouseEnter}
|
2017-03-23 22:10:42 +00:00
|
|
|
onMouseLeave={this._handleInputMouseLeave}
|
2017-03-12 00:31:23 +00:00
|
|
|
onDragEnter={this._handleInputDragEnter}
|
2017-04-04 23:06:43 +00:00
|
|
|
onPaste={onPaste}
|
2017-03-01 21:15:56 +00:00
|
|
|
onFocus={this._handleInputFocus}
|
|
|
|
onKeyDown={this._handleInputKeyDown}
|
|
|
|
/>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2017-03-01 21:15:56 +00:00
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
OneLineEditor.propTypes = {
|
2017-02-27 21:00:13 +00:00
|
|
|
defaultValue: PropTypes.string.isRequired,
|
2017-03-01 21:15:56 +00:00
|
|
|
|
|
|
|
// Optional
|
2017-03-23 22:10:42 +00:00
|
|
|
id: PropTypes.string,
|
2017-03-01 21:15:56 +00:00
|
|
|
type: PropTypes.string,
|
2017-03-31 21:59:12 +00:00
|
|
|
mode: PropTypes.string,
|
2017-03-01 21:15:56 +00:00
|
|
|
onBlur: PropTypes.func,
|
|
|
|
onKeyDown: PropTypes.func,
|
|
|
|
onFocus: PropTypes.func,
|
|
|
|
onChange: PropTypes.func,
|
2017-04-04 23:06:43 +00:00
|
|
|
onPaste: PropTypes.func,
|
2017-03-01 21:15:56 +00:00
|
|
|
render: PropTypes.func,
|
2017-03-12 00:31:23 +00:00
|
|
|
getRenderContext: PropTypes.func,
|
|
|
|
getAutocompleteConstants: PropTypes.func,
|
2017-03-01 21:15:56 +00:00
|
|
|
placeholder: PropTypes.string,
|
2017-03-09 06:23:23 +00:00
|
|
|
className: PropTypes.string,
|
|
|
|
forceEditor: PropTypes.bool,
|
2017-03-23 22:10:42 +00:00
|
|
|
forceInput: PropTypes.bool
|
2017-03-09 06:23:23 +00:00
|
|
|
};
|
2017-02-27 21:00:13 +00:00
|
|
|
|
|
|
|
export default OneLineEditor;
|