2017-09-09 19:39:10 -06:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2017-09-28 13:07:38 -04:00
|
|
|
import { Editor as Slate } from 'slate-react';
|
|
|
|
import Plain from 'slate-plain-serializer';
|
2017-08-31 21:25:44 -04:00
|
|
|
import { debounce } from 'lodash';
|
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-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-06-21 13:49:43 -04:00
|
|
|
this.state = {
|
2017-11-16 12:36:48 -05:00
|
|
|
value: Plain.deserialize(this.props.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) {
|
2017-11-16 12:36:48 -05:00
|
|
|
return !this.state.value.equals(nextState.value);
|
2017-07-27 08:46:53 -04:00
|
|
|
}
|
|
|
|
|
2017-10-02 11:23:47 -04:00
|
|
|
handleChange = change => {
|
2017-11-16 12:36:48 -05:00
|
|
|
if (!this.state.value.document.equals(change.value.document)) {
|
2017-09-28 11:02:59 -04:00
|
|
|
this.handleDocumentChange(change);
|
|
|
|
}
|
2017-11-16 12:36:48 -05:00
|
|
|
this.setState({ value: change.value });
|
2017-10-02 11:23:47 -04:00
|
|
|
};
|
2017-07-08 23:23:14 -04:00
|
|
|
|
2017-07-27 18:03:13 -04:00
|
|
|
/**
|
|
|
|
* When the document value changes, serialize from Slate's AST back to plain
|
2017-08-31 20:28:11 -04:00
|
|
|
* text (which is Markdown) and pass that up as the new value.
|
2017-07-27 18:03:13 -04:00
|
|
|
*/
|
2017-09-28 11:02:59 -04:00
|
|
|
handleDocumentChange = debounce(change => {
|
2017-11-16 12:36:48 -05:00
|
|
|
const value = Plain.serialize(change.value);
|
2017-09-26 15:52:50 -04:00
|
|
|
this.props.onChange(value);
|
|
|
|
}, 150);
|
2016-10-22 23:12:21 +03:00
|
|
|
|
2017-07-27 18:03:13 -04:00
|
|
|
/**
|
|
|
|
* If a paste contains plain text, deserialize it to Slate's AST and insert
|
|
|
|
* to the document. Selection logic (where to insert, whether to replace) is
|
|
|
|
* handled by Slate.
|
|
|
|
*/
|
2017-10-02 11:23:47 -04:00
|
|
|
handlePaste = (e, data, change) => {
|
2017-07-10 18:26:07 -04:00
|
|
|
if (data.text) {
|
2017-07-27 18:03:13 -04:00
|
|
|
const fragment = Plain.deserialize(data.text).document;
|
2017-10-02 11:23:47 -04:00
|
|
|
return change.insertFragment(fragment);
|
2017-07-10 18:26:07 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 (
|
2017-10-18 09:29:38 -07:00
|
|
|
<div className="nc-rawEditor-rawWrapper">
|
2017-07-08 22:09:47 -04:00
|
|
|
<Sticky
|
2017-10-18 09:29:38 -07:00
|
|
|
className="nc-visualEditor-editorControlBar"
|
|
|
|
classNameActive="nc-visualEditor-editorControlBarSticky"
|
2017-07-08 22:09:47 -04:00
|
|
|
fillContainerWidth
|
|
|
|
>
|
|
|
|
<Toolbar onToggleMode={this.handleToggleMode} disabled rawMode />
|
|
|
|
</Sticky>
|
2017-07-27 18:03:13 -04:00
|
|
|
<Slate
|
2017-10-18 09:29:38 -07:00
|
|
|
className="nc-rawEditor-rawEditor"
|
2017-11-16 12:36:48 -05:00
|
|
|
value={this.state.value}
|
2017-07-08 22:09:47 -04:00
|
|
|
onChange={this.handleChange}
|
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-08-31 20:28:11 -04:00
|
|
|
value: PropTypes.string,
|
2016-10-21 22:52:41 -07:00
|
|
|
};
|