import React from 'react'; import PropTypes from 'prop-types'; import RawEditor from './RawEditor'; import VisualEditor from './VisualEditor'; const MODE_STORAGE_KEY = 'cms.md-mode'; let editorControl; let _getEditorComponents = () => []; export const getEditorControl = () => editorControl; export const getEditorComponents = () => _getEditorComponents(); export default class MarkdownControl extends React.Component { static propTypes = { onChange: PropTypes.func.isRequired, onAddAsset: PropTypes.func.isRequired, getAsset: PropTypes.func.isRequired, classNameWrapper: PropTypes.string.isRequired, editorControl: PropTypes.func.isRequired, value: PropTypes.string, }; static defaultProps = { value: '', }; constructor(props) { super(props); editorControl = props.editorControl; _getEditorComponents = props.getEditorComponents; this.state = { mode: localStorage.getItem(MODE_STORAGE_KEY) || 'visual' }; } handleMode = (mode) => { this.setState({ mode }); localStorage.setItem(MODE_STORAGE_KEY, mode); }; processRef = ref => this.ref = ref; render() { const { onChange, onAddAsset, getAsset, value, classNameWrapper, field, getEditorComponents, } = this.props; const { mode } = this.state; const visualEditor = (
); const rawEditor = (
); return mode === 'visual' ? visualEditor : rawEditor; } }