2016-08-11 17:06:01 -03:00
|
|
|
import React, { PropTypes } from 'react';
|
2017-07-08 23:23:14 -04:00
|
|
|
import { Editor as SlateEditor, Plain as SlatePlain } from 'slate';
|
2017-07-18 19:14:40 -04:00
|
|
|
import { markdownToRemark, remarkToMarkdown } from '../../unified';
|
2017-04-18 12:30:38 -04:00
|
|
|
import Toolbar from '../Toolbar/Toolbar';
|
2017-06-23 14:42:40 -04:00
|
|
|
import { Sticky } from '../../../../UI/Sticky/Sticky';
|
2016-10-22 04:37:22 -07:00
|
|
|
import styles from './index.css';
|
2016-10-21 23:16:19 -07:00
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
export default class RawEditor extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-07-18 19:14:40 -04:00
|
|
|
const value = remarkToMarkdown(this.props.value);
|
2017-06-21 13:49:43 -04:00
|
|
|
this.state = {
|
2017-07-08 23:23:14 -04:00
|
|
|
editorState: SlatePlain.deserialize(value || ''),
|
2016-10-21 22:52:41 -07:00
|
|
|
};
|
|
|
|
}
|
2016-12-27 23:18:37 -08:00
|
|
|
|
2017-07-27 08:46:53 -04:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
if (this.state.editorState.equals(nextState.editorState)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-08 23:23:14 -04:00
|
|
|
handleChange = editorState => {
|
|
|
|
this.setState({ editorState });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDocumentChange = (doc, editorState) => {
|
|
|
|
const value = SlatePlain.serialize(editorState);
|
2017-07-18 19:14:40 -04:00
|
|
|
const html = markdownToRemark(value);
|
2017-06-21 13:49:43 -04:00
|
|
|
this.props.onChange(html);
|
2016-10-22 23:12:21 +03:00
|
|
|
};
|
|
|
|
|
2017-07-10 18:26:07 -04:00
|
|
|
handlePaste = (e, data, state) => {
|
|
|
|
if (data.text) {
|
|
|
|
const fragment = SlatePlain.deserialize(data.text).document;
|
|
|
|
return state.transform().insertFragment(fragment).apply();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-08 22:09:47 -04:00
|
|
|
handleToggleMode = () => {
|
2016-11-01 16:55:21 -07:00
|
|
|
this.props.onMode('visual');
|
|
|
|
};
|
2016-10-26 18:48:47 +02:00
|
|
|
|
2016-08-11 17:06:01 -03:00
|
|
|
render() {
|
2017-07-08 22:09:47 -04:00
|
|
|
return (
|
|
|
|
<div className={styles.root}>
|
|
|
|
<Sticky
|
|
|
|
className={styles.editorControlBar}
|
|
|
|
classNameActive={styles.editorControlBarSticky}
|
|
|
|
fillContainerWidth
|
|
|
|
>
|
|
|
|
<Toolbar onToggleMode={this.handleToggleMode} disabled rawMode />
|
|
|
|
</Sticky>
|
2017-07-08 23:23:14 -04:00
|
|
|
<SlateEditor
|
|
|
|
className={styles.SlateEditor}
|
|
|
|
state={this.state.editorState}
|
2017-07-08 22:09:47 -04:00
|
|
|
onChange={this.handleChange}
|
2017-07-08 23:23:14 -04:00
|
|
|
onDocumentChange={this.handleDocumentChange}
|
2017-07-10 18:26:07 -04:00
|
|
|
onPaste={this.handlePaste}
|
2017-03-16 20:45:46 -04:00
|
|
|
/>
|
2017-07-08 22:09:47 -04:00
|
|
|
</div>
|
|
|
|
);
|
2016-08-11 17:06:01 -03:00
|
|
|
}
|
|
|
|
}
|
2016-10-21 22:52:41 -07:00
|
|
|
|
|
|
|
RawEditor.propTypes = {
|
|
|
|
onChange: PropTypes.func.isRequired,
|
2016-11-01 16:55:21 -07:00
|
|
|
onMode: PropTypes.func.isRequired,
|
2017-07-18 19:14:40 -04:00
|
|
|
value: PropTypes.object,
|
2016-10-21 22:52:41 -07:00
|
|
|
};
|