import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import autobind from 'autobind-decorator'; import Modal from '../base/modal'; import ModalBody from '../base/modal-body'; import ModalHeader from '../base/modal-header'; import ModalFooter from '../base/modal-footer'; import CodeEditor from '../codemirror/code-editor'; import Dropdown from '../base/dropdown/dropdown'; import DropdownButton from '../base/dropdown/dropdown-button'; import DropdownItem from '../base/dropdown/dropdown-item'; import DropdownDivider from '../base/dropdown/dropdown-divider'; import MarkdownEditor from '../markdown-editor'; const MODES = { 'text/plain': 'Plain Text', 'application/json': 'JSON', 'application/xml': 'XML', 'application/edn': 'EDN', 'text/x-markdown': 'Markdown', 'text/html': 'HTML', }; @autobind class CodePromptModal extends PureComponent { constructor(props) { super(props); this.state = { title: 'Not Set', defaultValue: '', submitName: 'Not Set', placeholder: '', hint: '', mode: 'text/plain', hideMode: false, enableRender: false, }; } _setModalRef(n) { this.modal = n; } _handleChange(value) { this._onChange(value); } _handleChangeMode(mode) { this.setState({ mode }); this._onModeChange && this._onModeChange(mode); } hide() { this.modal.hide(); } show(options) { const { title, defaultValue, submitName, placeholder, hint, mode, hideMode, enableRender, onChange, onModeChange, } = options; this._onChange = onChange; this._onModeChange = onModeChange; const realMode = typeof mode === 'string' ? mode : 'text/plain'; this.setState({ title, defaultValue, submitName, placeholder, hint, enableRender, hideMode, mode: realMode || this.state.mode || 'text/plain', }); this.modal.show(); } render() { const { handleGetRenderContext, nunjucksPowerUserMode, isVariableUncovered, handleRender, editorKeyMap, editorIndentSize, editorFontSize, editorLineWrapping, } = this.props; const { submitName, title, placeholder, defaultValue, hint, mode, hideMode, enableRender, } = this.state; return ( {title} {mode === 'text/x-markdown' ? (
) : (
{isVariableUncovered && ( )} {!isVariableUncovered && ( )}
)}
{!hideMode ? ( {MODES[mode]} Editor Syntax {Object.keys(MODES).map(mode => ( {MODES[mode]} ))} ) : null}
{hint ? `* ${hint}` : ''}
); } } CodePromptModal.propTypes = { // Required editorFontSize: PropTypes.number.isRequired, editorIndentSize: PropTypes.number.isRequired, editorKeyMap: PropTypes.string.isRequired, editorLineWrapping: PropTypes.bool.isRequired, nunjucksPowerUserMode: PropTypes.bool.isRequired, isVariableUncovered: PropTypes.bool.isRequired, // Optional handleGetRenderContext: PropTypes.func, handleRender: PropTypes.func, }; export default CodePromptModal;