static-cms/src/components/Widgets/MarkdownControl.js

63 lines
1.8 KiB
JavaScript
Raw Normal View History

import React, { PropTypes } from 'react';
import registry from '../../lib/registry';
2016-08-11 18:21:32 -03:00
import RawEditor from './MarkdownControlElements/RawEditor';
import VisualEditor from './MarkdownControlElements/VisualEditor';
2016-08-18 10:51:38 -03:00
import { processEditorPlugins } from './richText';
import { StickyContainer } from '../UI/Sticky/Sticky';
2016-08-03 10:30:42 -03:00
const MODE_STORAGE_KEY = 'cms.md-mode';
export default class MarkdownControl extends React.Component {
2016-10-03 14:33:48 +02:00
static propTypes = {
onChange: PropTypes.func.isRequired,
2017-01-10 22:23:22 -02:00
onAddAsset: PropTypes.func.isRequired,
onRemoveAsset: PropTypes.func.isRequired,
2017-01-10 22:23:22 -02:00
getAsset: PropTypes.func.isRequired,
2016-10-03 14:33:48 +02:00
value: PropTypes.node,
};
2016-08-11 18:21:32 -03:00
constructor(props) {
super(props);
this.state = { mode: localStorage.getItem(MODE_STORAGE_KEY) || 'visual' };
}
2016-08-18 10:51:38 -03:00
componentWillMount() {
processEditorPlugins(registry.getEditorComponents());
2016-08-18 10:51:38 -03:00
}
handleMode = (mode) => {
this.setState({ mode });
localStorage.setItem(MODE_STORAGE_KEY, mode);
2016-10-03 14:25:27 +02:00
};
2016-08-11 18:21:32 -03:00
render() {
2017-01-10 22:23:22 -02:00
const { onChange, onAddAsset, onRemoveAsset, getAsset, value } = this.props;
const { mode } = this.state;
const visualEditor = (
<div className="cms-editor-visual">
<VisualEditor
onChange={onChange}
onAddAsset={onAddAsset}
onRemoveAsset={onRemoveAsset}
onMode={this.handleMode}
getAsset={getAsset}
value={value}
/>
</div>
);
const rawEditor = (
<div className="cms-editor-raw">
<RawEditor
onChange={onChange}
2017-01-10 22:23:22 -02:00
onAddAsset={onAddAsset}
onRemoveAsset={onRemoveAsset}
onMode={this.handleMode}
2017-01-10 22:23:22 -02:00
getAsset={getAsset}
value={value}
/>
</div>
);
return <StickyContainer>{ mode === 'visual' ? visualEditor : rawEditor }</StickyContainer>;
2016-08-11 18:21:32 -03:00
}
2016-05-30 16:55:32 -07:00
}