2018-03-05 14:30:28 +01:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* Wysiwyg
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from 'react';
|
2018-03-05 19:23:06 +01:00
|
|
|
import { ContentState, convertFromHTML, Editor, EditorState } from 'draft-js';
|
2018-03-05 14:30:28 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
2018-03-05 19:23:06 +01:00
|
|
|
import { isEmpty } from 'lodash';
|
2018-03-05 14:30:28 +01:00
|
|
|
import cn from 'classnames';
|
|
|
|
|
|
|
|
|
|
import styles from './styles.scss';
|
|
|
|
|
|
|
|
|
|
class Wysiwyg extends React.Component {
|
2018-03-05 19:23:06 +01:00
|
|
|
state = { editorState: EditorState.createEmpty(), isFocused: false, hasInitialValue: false };
|
2018-03-05 14:30:28 +01:00
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
if (this.props.autoFocus) {
|
|
|
|
|
this.focus();
|
|
|
|
|
}
|
2018-03-05 19:23:06 +01:00
|
|
|
|
|
|
|
|
if (!isEmpty(this.props.value)) {
|
|
|
|
|
this.setInitialValue(this.props);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
|
if (nextProps.value !== this.props.value && !this.state.hasInitialValue) {
|
|
|
|
|
this.setInitialValue(nextProps);
|
|
|
|
|
}
|
2018-03-05 14:30:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
focus = () => {
|
|
|
|
|
this.setState({ isFocused: true });
|
|
|
|
|
this.domEditor.focus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleBlur = (editorState) => {
|
|
|
|
|
this.setState({ isFocused: false });
|
2018-03-05 19:23:06 +01:00
|
|
|
this.domEditor.blur();
|
2018-03-05 14:30:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the state is empty
|
2018-03-05 19:23:06 +01:00
|
|
|
* @return {Boolean}
|
2018-03-05 14:30:28 +01:00
|
|
|
*/
|
|
|
|
|
isContentStateEmpty = () => !this.state.editorState.getCurrentContent().hasText();
|
|
|
|
|
|
2018-03-05 19:23:06 +01:00
|
|
|
onChange = (editorState) => {
|
|
|
|
|
const target = {
|
|
|
|
|
name: this.props.name,
|
|
|
|
|
type: 'text',
|
|
|
|
|
value: editorState.getCurrentContent().getPlainText(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.props.onChange({ target });
|
|
|
|
|
this.setState({editorState});
|
|
|
|
|
}
|
2018-03-05 14:30:28 +01:00
|
|
|
|
|
|
|
|
setDomEditorRef = ref => this.domEditor = ref;
|
|
|
|
|
|
2018-03-05 19:23:06 +01:00
|
|
|
setInitialValue = (props) => {
|
|
|
|
|
let editorState;
|
|
|
|
|
const blocksFromHTML = convertFromHTML(props.value);
|
|
|
|
|
const contentState = ContentState.createFromBlockArray(blocksFromHTML);
|
|
|
|
|
editorState = EditorState.createWithContent(contentState);
|
|
|
|
|
|
|
|
|
|
// Get the cursor at the end
|
|
|
|
|
editorState = EditorState.moveFocusToEnd(editorState);
|
|
|
|
|
|
|
|
|
|
this.setState({ editorState, hasInitialValue: true });
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 14:30:28 +01:00
|
|
|
render() {
|
|
|
|
|
const {
|
|
|
|
|
placeholder,
|
|
|
|
|
} = this.props;
|
|
|
|
|
const { editorState } = this.state;
|
|
|
|
|
|
|
|
|
|
return (
|
2018-03-05 19:23:06 +01:00
|
|
|
<React.Fragment>
|
|
|
|
|
<div className={cn(styles.editorWrapper, this.state.isFocused && styles.editorFocus)} onClick={this.focus}>
|
|
|
|
|
<FormattedMessage id={placeholder} defaultMessage={placeholder}>
|
|
|
|
|
{(message) => (
|
|
|
|
|
<Editor
|
|
|
|
|
editorState={editorState}
|
|
|
|
|
onChange={this.onChange}
|
|
|
|
|
onBlur={this.handleBlur}
|
|
|
|
|
ref={this.setDomEditorRef}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</FormattedMessage>
|
|
|
|
|
</div>
|
|
|
|
|
<input
|
|
|
|
|
className={styles.editorInput}
|
|
|
|
|
type="button"
|
|
|
|
|
value=""
|
|
|
|
|
tabIndex="-1"
|
|
|
|
|
/>
|
|
|
|
|
</React.Fragment>
|
2018-03-05 14:30:28 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Wysiwyg.defaultProps = {
|
2018-03-05 19:23:06 +01:00
|
|
|
autoFocus: false,
|
2018-03-05 14:30:28 +01:00
|
|
|
placeholder: 'app.utils.placeholder.defaultMessage',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Wysiwyg.propTypes = {
|
|
|
|
|
autoFocus: PropTypes.bool,
|
|
|
|
|
placeholder: PropTypes.string,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Wysiwyg;
|