diff --git a/packages/strapi-helper-plugin/lib/src/components/Wysiwyg/index.js b/packages/strapi-helper-plugin/lib/src/components/Wysiwyg/index.js
index f1d54a6dce..78349eab81 100644
--- a/packages/strapi-helper-plugin/lib/src/components/Wysiwyg/index.js
+++ b/packages/strapi-helper-plugin/lib/src/components/Wysiwyg/index.js
@@ -5,16 +5,39 @@
*/
import React from 'react';
-import { ContentState, convertFromHTML, Editor, EditorState } from 'draft-js';
+import { ContentState, convertFromHTML, Editor, EditorState, getDefaultKeyBinding, RichUtils, convertToRaw } from 'draft-js';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import { isEmpty } from 'lodash';
import cn from 'classnames';
+import Controls from 'components/WysiwygInlineControls';
import styles from './styles.scss';
+const styleMap = {
+ CODE: {
+ backgroundColor: 'rgba(0, 0, 0, 0.05)',
+ fontFamily: '"Inconsolata", "Menlo", "Consolas", monospace',
+ fontSize: 16,
+ padding: 2,
+ },
+};
+function getBlockStyle(block) {
+ switch (block.getType()) {
+ case 'blockquote': return 'RichEditor-blockquote';
+ default: return null;
+ }
+}
+
class Wysiwyg extends React.Component {
- state = { editorState: EditorState.createEmpty(), isFocused: false, hasInitialValue: false };
+ constructor(props) {
+ super(props);
+ this.state = { editorState: EditorState.createEmpty(), isFocused: false };
+ this.focus = () => {
+ this.setState({ isFocused: true });
+ return this.refs.editor.focus();
+ }
+ }
componentDidMount() {
if (this.props.autoFocus) {
@@ -32,40 +55,60 @@ class Wysiwyg extends React.Component {
}
}
- focus = () => {
- this.setState({ isFocused: true });
- this.domEditor.focus();
+ handleKeyCommand(command, editorState) {
+ const newState = RichUtils.handleKeyCommand(editorState, command);
+ if (newState) {
+ this.onChange(newState);
+ return true;
+ }
+ return false;
}
- handleBlur = (editorState) => {
- this.setState({ isFocused: false });
- this.domEditor.blur();
- }
-
- /**
- * Determine if the state is empty
- * @return {Boolean}
- */
- isContentStateEmpty = () => !this.state.editorState.getCurrentContent().hasText();
-
onChange = (editorState) => {
- const target = {
- name: this.props.name,
- type: 'text',
+ this.setState({ editorState });
+ this.props.onChange({ target: {
value: editorState.getCurrentContent().getPlainText(),
- };
-
- this.props.onChange({ target });
- this.setState({editorState});
+ name: this.props.name,
+ type: 'textarea',
+ }});
}
- setDomEditorRef = ref => this.domEditor = ref;
+ mapKeyToEditorCommand = (e) => {
+ if (e.keyCode === 9 /* TAB */) {
+ const newEditorState = RichUtils.onTab(
+ e,
+ this.state.editorState,
+ 4, /* maxDepth */
+ );
+ if (newEditorState !== this.state.editorState) {
+ this.onChange(newEditorState);
+ }
+ return;
+ }
+ return getDefaultKeyBinding(e);
+ }
+
+ toggleBlockType = (blockType) => {
+ this.onChange(
+ RichUtils.toggleBlockType(
+ this.state.editorState,
+ blockType
+ )
+ );
+ }
+
+ toggleInlineStyle = (inlineStyle) => {
+ this.onChange(
+ RichUtils.toggleInlineStyle(
+ this.state.editorState,
+ inlineStyle
+ )
+ );
+ }
setInitialValue = (props) => {
- let editorState;
- const blocksFromHTML = convertFromHTML(props.value);
- const contentState = ContentState.createFromBlockArray(blocksFromHTML);
- editorState = EditorState.createWithContent(contentState);
+ const contentState = ContentState.createFromText(props.value);
+ let editorState = EditorState.createWithContent(contentState);
// Get the cursor at the end
editorState = EditorState.moveFocusToEnd(editorState);
@@ -73,40 +116,47 @@ class Wysiwyg extends React.Component {
this.setState({ editorState, hasInitialValue: true });
}
+ previewHTML = () => {
+ const blocksFromHTML = convertFromHTML(this.props.value);
+ const contentState = ContentState.createFromBlockArray(blocksFromHTML);
+ const editorState = EditorState.createWithContent(contentState);
+ this.setState({ editorState });
+ }
+
render() {
- const {
- placeholder,
- } = this.props;
const { editorState } = this.state;
return (
-