2018-06-25 17:42:50 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
2017-08-10 01:56:27 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-06-01 22:58:09 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
|
|
|
import classnames from 'classnames';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { Tab, TabList, TabPanel, Tabs } from 'react-tabs';
|
2017-06-01 22:58:09 +00:00
|
|
|
import Button from './base/button';
|
|
|
|
import CodeEditor from './codemirror/code-editor';
|
2017-06-16 20:29:22 +00:00
|
|
|
import MarkdownPreview from './markdown-preview';
|
2017-06-01 22:58:09 +00:00
|
|
|
|
|
|
|
@autobind
|
|
|
|
class MarkdownEditor extends PureComponent {
|
2018-06-25 17:42:50 +00:00
|
|
|
constructor(props) {
|
2017-06-01 22:58:09 +00:00
|
|
|
super(props);
|
|
|
|
this.state = {
|
2018-12-12 17:36:11 +00:00
|
|
|
markdown: props.defaultValue,
|
2017-06-01 22:58:09 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_handleChange(markdown) {
|
2017-06-01 22:58:09 +00:00
|
|
|
this.props.onChange(markdown);
|
2018-06-25 17:42:50 +00:00
|
|
|
this.setState({ markdown });
|
2017-06-01 22:58:09 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_setEditorRef(n) {
|
2017-06-16 20:29:22 +00:00
|
|
|
this._editor = n;
|
2017-06-01 22:58:09 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
focusEnd() {
|
2017-06-16 20:29:22 +00:00
|
|
|
this._editor && this._editor.focusEnd();
|
2017-06-01 22:58:09 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
focus() {
|
2017-06-16 22:21:41 +00:00
|
|
|
this._editor && this._editor.focus();
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
render() {
|
2017-06-01 22:58:09 +00:00
|
|
|
const {
|
|
|
|
fontSize,
|
|
|
|
lineWrapping,
|
|
|
|
indentSize,
|
|
|
|
keyMap,
|
2017-06-09 21:42:19 +00:00
|
|
|
mode,
|
2017-06-01 22:58:09 +00:00
|
|
|
placeholder,
|
|
|
|
defaultPreviewMode,
|
|
|
|
className,
|
2017-06-09 21:42:19 +00:00
|
|
|
tall,
|
2017-06-01 22:58:09 +00:00
|
|
|
handleRender,
|
2017-10-13 08:22:13 +00:00
|
|
|
handleGetRenderContext,
|
2018-11-30 05:50:30 +00:00
|
|
|
nunjucksPowerUserMode,
|
2018-12-12 17:36:11 +00:00
|
|
|
isVariableUncovered,
|
2017-06-01 22:58:09 +00:00
|
|
|
} = this.props;
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
const { markdown } = this.state;
|
2017-06-01 22:58:09 +00:00
|
|
|
|
2018-10-17 16:42:33 +00:00
|
|
|
const classes = classnames('react-tabs', 'markdown-editor', 'outlined', className, {
|
2018-12-12 17:36:11 +00:00
|
|
|
'markdown-editor--dynamic-height': !tall,
|
2018-10-17 16:42:33 +00:00
|
|
|
});
|
2017-06-09 21:42:19 +00:00
|
|
|
|
2017-06-01 22:58:09 +00:00
|
|
|
return (
|
2017-08-10 01:56:27 +00:00
|
|
|
<Tabs className={classes} defaultIndex={defaultPreviewMode ? 1 : 0}>
|
2017-06-01 22:58:09 +00:00
|
|
|
<TabList>
|
2018-12-12 15:35:44 +00:00
|
|
|
<Tab tabIndex="-1">
|
2018-05-23 04:28:25 +00:00
|
|
|
<Button value="Write">Write</Button>
|
2017-06-01 22:58:09 +00:00
|
|
|
</Tab>
|
2018-12-12 15:35:44 +00:00
|
|
|
<Tab tabIndex="-1">
|
2018-05-23 04:28:25 +00:00
|
|
|
<Button value="Preview">Preview</Button>
|
2017-06-01 22:58:09 +00:00
|
|
|
</Tab>
|
|
|
|
</TabList>
|
2017-08-10 01:56:27 +00:00
|
|
|
<TabPanel className="react-tabs__tab-panel markdown-editor__edit">
|
2017-06-01 22:58:09 +00:00
|
|
|
<div className="form-control form-control--outlined">
|
2018-11-30 05:50:30 +00:00
|
|
|
{isVariableUncovered && (
|
|
|
|
<CodeEditor
|
|
|
|
ref={this._setEditorRef}
|
|
|
|
hideGutters
|
|
|
|
hideLineNumbers
|
|
|
|
dynamicHeight={!tall}
|
|
|
|
manualPrettify
|
|
|
|
noStyleActiveLine
|
|
|
|
mode={mode || 'text/x-markdown'}
|
|
|
|
placeholder={placeholder}
|
|
|
|
debounceMillis={300}
|
|
|
|
keyMap={keyMap}
|
|
|
|
fontSize={fontSize}
|
|
|
|
lineWrapping={lineWrapping}
|
|
|
|
indentSize={indentSize}
|
|
|
|
defaultValue={markdown}
|
|
|
|
render={handleRender}
|
|
|
|
getRenderContext={handleGetRenderContext}
|
|
|
|
nunjucksPowerUserMode={nunjucksPowerUserMode}
|
|
|
|
isVariableUncovered={isVariableUncovered}
|
|
|
|
onChange={this._handleChange}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{!isVariableUncovered && (
|
|
|
|
<CodeEditor
|
|
|
|
ref={this._setEditorRef}
|
|
|
|
hideGutters
|
|
|
|
hideLineNumbers
|
|
|
|
dynamicHeight={!tall}
|
|
|
|
manualPrettify
|
|
|
|
noStyleActiveLine
|
|
|
|
mode={mode || 'text/x-markdown'}
|
|
|
|
placeholder={placeholder}
|
|
|
|
debounceMillis={300}
|
|
|
|
keyMap={keyMap}
|
|
|
|
fontSize={fontSize}
|
|
|
|
lineWrapping={lineWrapping}
|
|
|
|
indentSize={indentSize}
|
|
|
|
defaultValue={markdown}
|
|
|
|
render={handleRender}
|
|
|
|
getRenderContext={handleGetRenderContext}
|
|
|
|
nunjucksPowerUserMode={nunjucksPowerUserMode}
|
|
|
|
isVariableUncovered={isVariableUncovered}
|
|
|
|
onChange={this._handleChange}
|
|
|
|
/>
|
|
|
|
)}
|
2017-06-01 22:58:09 +00:00
|
|
|
</div>
|
2018-10-17 16:42:33 +00:00
|
|
|
<div className="txt-sm italic faint">Styling with Markdown is supported</div>
|
2017-06-01 22:58:09 +00:00
|
|
|
</TabPanel>
|
2017-08-10 01:56:27 +00:00
|
|
|
<TabPanel className="react-tabs__tab-panel markdown-editor__preview">
|
2018-06-25 17:42:50 +00:00
|
|
|
<MarkdownPreview markdown={markdown} handleRender={handleRender} />
|
2017-06-01 22:58:09 +00:00
|
|
|
</TabPanel>
|
|
|
|
</Tabs>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkdownEditor.propTypes = {
|
|
|
|
// Required
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
defaultValue: PropTypes.string.isRequired,
|
|
|
|
fontSize: PropTypes.number.isRequired,
|
|
|
|
indentSize: PropTypes.number.isRequired,
|
|
|
|
keyMap: PropTypes.string.isRequired,
|
|
|
|
lineWrapping: PropTypes.bool.isRequired,
|
|
|
|
handleRender: PropTypes.func.isRequired,
|
|
|
|
handleGetRenderContext: PropTypes.func.isRequired,
|
2017-10-13 08:22:13 +00:00
|
|
|
nunjucksPowerUserMode: PropTypes.bool.isRequired,
|
2018-11-30 05:50:30 +00:00
|
|
|
isVariableUncovered: PropTypes.bool.isRequired,
|
2017-06-01 22:58:09 +00:00
|
|
|
|
|
|
|
// Optional
|
|
|
|
placeholder: PropTypes.string,
|
|
|
|
defaultPreviewMode: PropTypes.bool,
|
2017-06-09 21:42:19 +00:00
|
|
|
className: PropTypes.string,
|
|
|
|
mode: PropTypes.string,
|
2018-12-12 17:36:11 +00:00
|
|
|
tall: PropTypes.bool,
|
2017-06-01 22:58:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default MarkdownEditor;
|