2018-03-05 14:30:28 +01:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* Wysiwyg
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
import React from 'react';
|
2018-03-08 12:56:44 +01:00
|
|
|
import {
|
|
|
|
|
ContentState,
|
|
|
|
|
EditorState,
|
|
|
|
|
getDefaultKeyBinding,
|
2018-03-14 22:47:56 +01:00
|
|
|
genKey,
|
2018-03-08 18:55:49 +01:00
|
|
|
Modifier,
|
2018-03-08 12:56:44 +01:00
|
|
|
RichUtils,
|
2018-03-20 13:27:42 +01:00
|
|
|
SelectionState,
|
2018-03-08 12:56:44 +01:00
|
|
|
} from 'draft-js';
|
2018-03-05 14:30:28 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2018-03-20 16:12:05 +01:00
|
|
|
import { isEmpty, isNaN, replace, words } from 'lodash';
|
2018-03-05 14:30:28 +01:00
|
|
|
import cn from 'classnames';
|
2018-03-06 18:20:05 +01:00
|
|
|
import Controls from 'components/WysiwygInlineControls';
|
2018-03-14 22:20:39 +01:00
|
|
|
import Drop from 'components/WysiwygDropUpload';
|
2018-03-12 13:26:07 +01:00
|
|
|
import WysiwygBottomControls from 'components/WysiwygBottomControls';
|
2018-03-12 14:02:34 +01:00
|
|
|
import WysiwygEditor from 'components/WysiwygEditor';
|
2018-03-14 22:47:56 +01:00
|
|
|
import request from 'utils/request';
|
2018-03-28 16:00:08 +02:00
|
|
|
import CustomSelect from './customSelect';
|
|
|
|
|
import PreviewControl from './previewControl';
|
|
|
|
|
import PreviewWysiwyg from './previewWysiwyg';
|
|
|
|
|
import ToggleMode from './toggleMode';
|
2018-03-22 20:33:34 +01:00
|
|
|
import { CONTROLS } from './constants';
|
2018-03-28 16:17:27 +02:00
|
|
|
import {
|
|
|
|
|
getBlockContent,
|
|
|
|
|
getBlockStyle,
|
|
|
|
|
getDefaultSelectionOffsets,
|
|
|
|
|
getKeyCommandData,
|
|
|
|
|
getOffSets,
|
|
|
|
|
} from './helpers';
|
2018-03-28 17:29:55 +02:00
|
|
|
import {
|
|
|
|
|
createNewBlock,
|
|
|
|
|
getNextBlocksList,
|
|
|
|
|
getSelectedBlocksList,
|
|
|
|
|
updateSelection,
|
|
|
|
|
} from './utils';
|
2018-03-13 13:12:06 +01:00
|
|
|
import styles from './styles.scss';
|
2018-03-06 18:20:05 +01:00
|
|
|
|
2018-03-08 12:56:44 +01:00
|
|
|
/* eslint-disable react/jsx-handler-names */
|
2018-03-15 19:10:02 +01:00
|
|
|
/* eslint-disable react/sort-comp */
|
2018-03-05 14:30:28 +01:00
|
|
|
class Wysiwyg extends React.Component {
|
2018-03-06 18:20:05 +01:00
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
2018-03-08 15:50:28 +01:00
|
|
|
this.state = {
|
|
|
|
|
editorState: EditorState.createEmpty(),
|
|
|
|
|
isFocused: false,
|
|
|
|
|
initialValue: '',
|
2018-03-14 22:20:39 +01:00
|
|
|
isDraging: false,
|
2018-03-16 11:45:21 +01:00
|
|
|
isPreviewMode: false,
|
2018-03-08 15:50:28 +01:00
|
|
|
headerValue: '',
|
2018-03-22 20:33:34 +01:00
|
|
|
isFullscreen: false,
|
2018-03-08 15:50:28 +01:00
|
|
|
};
|
2018-03-06 18:20:05 +01:00
|
|
|
this.focus = () => {
|
|
|
|
|
this.setState({ isFocused: true });
|
2018-03-09 12:45:37 +01:00
|
|
|
return this.domEditor.focus();
|
2018-03-09 16:41:24 +01:00
|
|
|
};
|
2018-03-19 12:30:04 +01:00
|
|
|
this.blur = () => {
|
|
|
|
|
this.setState({ isFocused: false });
|
|
|
|
|
return this.domEditor.blur();
|
|
|
|
|
};
|
2018-03-06 18:20:05 +01:00
|
|
|
}
|
2018-03-05 14:30:28 +01:00
|
|
|
|
2018-03-22 20:33:34 +01:00
|
|
|
getChildContext = () => ({
|
|
|
|
|
handleChangeSelect: this.handleChangeSelect,
|
|
|
|
|
headerValue: this.state.headerValue,
|
|
|
|
|
html: this.props.value,
|
|
|
|
|
isFocused: this.state.isFocused,
|
|
|
|
|
isPreviewMode: this.state.isPreviewMode,
|
|
|
|
|
isFullscreen: this.state.isFullscreen,
|
|
|
|
|
placeholder: this.props.placeholder,
|
|
|
|
|
});
|
|
|
|
|
|
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-06 21:32:48 +01:00
|
|
|
|
|
|
|
|
// Handle reset props
|
|
|
|
|
if (nextProps.value === this.state.initialValue && this.state.hasInitialValue) {
|
|
|
|
|
this.setInitialValue(nextProps);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 09:59:42 +01:00
|
|
|
/**
|
|
|
|
|
* Init the editor with data from
|
|
|
|
|
* @param {[type]} props [description]
|
|
|
|
|
*/
|
2018-03-22 12:01:11 +01:00
|
|
|
setInitialValue = props => {
|
2018-03-14 09:59:42 +01:00
|
|
|
const contentState = ContentState.createFromText(props.value);
|
2018-03-20 16:12:05 +01:00
|
|
|
const editorState = EditorState.createWithContent(contentState);
|
2018-03-22 12:01:11 +01:00
|
|
|
this.setState({
|
|
|
|
|
editorState: EditorState.moveFocusToEnd(editorState),
|
|
|
|
|
hasInitialValue: true,
|
|
|
|
|
initialValue: props.value,
|
|
|
|
|
});
|
|
|
|
|
};
|
2018-03-14 09:59:42 +01:00
|
|
|
|
2018-03-20 15:34:27 +01:00
|
|
|
addContent = (content, style) => {
|
2018-03-15 19:10:02 +01:00
|
|
|
const selectedText = this.getSelectedText();
|
2018-03-20 15:17:33 +01:00
|
|
|
const { innerContent, endReplacer, startReplacer } = getBlockContent(style);
|
2018-03-22 12:01:11 +01:00
|
|
|
const defaultContent =
|
|
|
|
|
selectedText === ''
|
|
|
|
|
? replace(content, 'textToReplace', innerContent)
|
|
|
|
|
: replace(content, 'textToReplace', selectedText);
|
2018-03-20 16:12:05 +01:00
|
|
|
const cursorPosition = getOffSets(this.getSelection()).start;
|
2018-03-28 17:29:55 +02:00
|
|
|
const textWithEntity = this.modifyBlockContent(defaultContent);
|
2018-03-22 12:01:11 +01:00
|
|
|
const { anchorOffset, focusOffset } = getDefaultSelectionOffsets(
|
|
|
|
|
defaultContent,
|
|
|
|
|
startReplacer,
|
|
|
|
|
endReplacer,
|
2018-03-22 20:33:34 +01:00
|
|
|
cursorPosition,
|
2018-03-22 12:01:11 +01:00
|
|
|
);
|
2018-03-20 16:12:05 +01:00
|
|
|
const updatedSelection = this.getSelection().merge({ anchorOffset, focusOffset });
|
2018-03-22 12:01:11 +01:00
|
|
|
const newEditorState = EditorState.push(
|
|
|
|
|
this.getEditorState(),
|
|
|
|
|
textWithEntity,
|
2018-03-22 20:33:34 +01:00
|
|
|
'insert-character',
|
2018-03-22 12:01:11 +01:00
|
|
|
);
|
2018-03-15 20:21:21 +01:00
|
|
|
|
|
|
|
|
// Don't handle selection
|
|
|
|
|
if (selectedText !== '') {
|
2018-03-22 12:01:11 +01:00
|
|
|
return this.setState(
|
|
|
|
|
{
|
|
|
|
|
editorState: EditorState.moveFocusToEnd(newEditorState),
|
|
|
|
|
},
|
|
|
|
|
() => {
|
|
|
|
|
this.focus();
|
2018-03-22 20:33:34 +01:00
|
|
|
},
|
2018-03-22 12:01:11 +01:00
|
|
|
);
|
2018-03-15 20:21:21 +01:00
|
|
|
}
|
2018-03-20 15:34:27 +01:00
|
|
|
|
2018-03-22 12:01:11 +01:00
|
|
|
return this.setState({
|
|
|
|
|
editorState: EditorState.forceSelection(newEditorState, updatedSelection),
|
|
|
|
|
});
|
|
|
|
|
};
|
2018-03-15 19:10:02 +01:00
|
|
|
|
|
|
|
|
addOlBlock = () => {
|
2018-03-28 17:29:55 +02:00
|
|
|
const selectedBlocksList = getSelectedBlocksList(this.getEditorState());
|
2018-03-27 18:59:24 +02:00
|
|
|
let newEditorState = this.getEditorState();
|
2018-03-15 19:10:02 +01:00
|
|
|
|
2018-03-27 18:59:24 +02:00
|
|
|
if (getOffSets(this.getSelection()).start !== 0) {
|
2018-03-28 17:29:55 +02:00
|
|
|
const nextBlocks = getNextBlocksList(newEditorState, this.getSelection().getStartKey());
|
2018-03-27 18:59:24 +02:00
|
|
|
|
|
|
|
|
nextBlocks.map((block, index) => {
|
|
|
|
|
const previousContent =
|
|
|
|
|
index === 0
|
|
|
|
|
? this.getEditorState()
|
|
|
|
|
.getCurrentContent()
|
|
|
|
|
.getBlockForKey(this.getCurrentAnchorKey())
|
|
|
|
|
: newEditorState.getCurrentContent().getBlockBefore(block.getKey());
|
|
|
|
|
const number = previousContent ? parseInt(previousContent.getText().split('.')[0], 10) : 0;
|
|
|
|
|
const liNumber = isNaN(number) ? 1 : number + 1;
|
|
|
|
|
const nextBlockText = index === 0 ? `${liNumber}.` : nextBlocks.get(index - 1).getText();
|
2018-03-28 17:29:55 +02:00
|
|
|
const newBlock = createNewBlock(nextBlockText, 'block-list', block.getKey());
|
2018-03-27 18:59:24 +02:00
|
|
|
const newContentState = this.createNewContentStateFromBlock(
|
|
|
|
|
newBlock,
|
|
|
|
|
newEditorState.getCurrentContent(),
|
|
|
|
|
);
|
|
|
|
|
newEditorState = EditorState.push(newEditorState, newContentState);
|
|
|
|
|
});
|
2018-03-28 17:29:55 +02:00
|
|
|
const updatedSelection = updateSelection(this.getSelection(), nextBlocks, 2);
|
2018-03-27 18:59:24 +02:00
|
|
|
|
|
|
|
|
return this.setState({
|
|
|
|
|
editorState: EditorState.acceptSelection(newEditorState, updatedSelection),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
selectedBlocksList.map((block, i) => {
|
|
|
|
|
const selectedText = block.getText();
|
|
|
|
|
const li = selectedText === '' ? `${i + 1}.` : `${i + 1}. ${selectedText}`;
|
2018-03-28 17:29:55 +02:00
|
|
|
const newBlock = createNewBlock(li, 'block-list', block.getKey());
|
2018-03-27 18:59:24 +02:00
|
|
|
const newContentState = this.createNewContentStateFromBlock(
|
|
|
|
|
newBlock,
|
|
|
|
|
newEditorState.getCurrentContent(),
|
|
|
|
|
);
|
|
|
|
|
newEditorState = EditorState.push(newEditorState, newContentState);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return this.setState({ editorState: EditorState.moveFocusToEnd(newEditorState) });
|
2018-03-22 12:01:11 +01:00
|
|
|
};
|
2018-03-15 19:10:02 +01:00
|
|
|
|
|
|
|
|
addUlBlock = () => {
|
2018-03-28 17:29:55 +02:00
|
|
|
const selectedBlocksList = getSelectedBlocksList(this.getEditorState());
|
2018-03-27 18:02:47 +02:00
|
|
|
let newEditorState = this.getEditorState();
|
2018-03-20 14:49:49 +01:00
|
|
|
|
2018-03-27 18:02:47 +02:00
|
|
|
if (getOffSets(this.getSelection()).start !== 0) {
|
2018-03-28 17:29:55 +02:00
|
|
|
const nextBlocks = getNextBlocksList(newEditorState, this.getSelection().getStartKey());
|
2018-03-27 18:02:47 +02:00
|
|
|
|
|
|
|
|
nextBlocks.map((block, index) => {
|
|
|
|
|
const nextBlockText = index === 0 ? '-' : nextBlocks.get(index - 1).getText();
|
2018-03-28 17:29:55 +02:00
|
|
|
const newBlock = createNewBlock(nextBlockText, 'block-list', block.getKey());
|
2018-03-27 18:59:24 +02:00
|
|
|
const newContentState = this.createNewContentStateFromBlock(
|
|
|
|
|
newBlock,
|
|
|
|
|
newEditorState.getCurrentContent(),
|
|
|
|
|
);
|
2018-03-27 18:02:47 +02:00
|
|
|
newEditorState = EditorState.push(newEditorState, newContentState);
|
|
|
|
|
});
|
2018-03-28 17:29:55 +02:00
|
|
|
const updatedSelection = updateSelection(this.getSelection(), nextBlocks, 1);
|
2018-03-27 18:02:47 +02:00
|
|
|
|
2018-03-27 18:59:24 +02:00
|
|
|
return this.setState({
|
|
|
|
|
editorState: EditorState.acceptSelection(newEditorState, updatedSelection),
|
|
|
|
|
});
|
2018-03-27 18:02:47 +02:00
|
|
|
}
|
|
|
|
|
|
2018-03-27 18:59:24 +02:00
|
|
|
selectedBlocksList.map(block => {
|
2018-03-27 18:02:47 +02:00
|
|
|
const selectedText = block.getText();
|
|
|
|
|
const li = selectedText === '' ? '-' : `- ${selectedText}`;
|
2018-03-28 17:29:55 +02:00
|
|
|
const newBlock = createNewBlock(li, 'block-list', block.getKey());
|
2018-03-27 18:59:24 +02:00
|
|
|
const newContentState = this.createNewContentStateFromBlock(
|
|
|
|
|
newBlock,
|
|
|
|
|
newEditorState.getCurrentContent(),
|
|
|
|
|
);
|
2018-03-27 18:02:47 +02:00
|
|
|
newEditorState = EditorState.push(newEditorState, newContentState);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return this.setState({ editorState: EditorState.moveFocusToEnd(newEditorState) });
|
2018-03-22 12:01:11 +01:00
|
|
|
};
|
2018-03-20 14:49:49 +01:00
|
|
|
|
2018-03-22 12:01:11 +01:00
|
|
|
addBlock = text => {
|
2018-03-20 14:49:49 +01:00
|
|
|
const nextBlockKey = this.getNextBlockKey(this.getCurrentAnchorKey()) || genKey();
|
2018-03-28 17:29:55 +02:00
|
|
|
const newBlock = createNewBlock(text, 'block-list', nextBlockKey);
|
2018-03-15 19:10:02 +01:00
|
|
|
const newContentState = this.createNewContentStateFromBlock(newBlock);
|
2018-03-20 14:49:49 +01:00
|
|
|
const newEditorState = this.createNewEditorState(newContentState, text);
|
2018-03-15 19:10:02 +01:00
|
|
|
|
|
|
|
|
return this.setState({ editorState: EditorState.moveFocusToEnd(newEditorState) });
|
2018-03-22 12:01:11 +01:00
|
|
|
};
|
2018-03-15 19:10:02 +01:00
|
|
|
|
2018-03-20 15:34:27 +01:00
|
|
|
addSimpleBlockWithSelection = (content, style) => {
|
|
|
|
|
const selectedText = this.getSelectedText();
|
|
|
|
|
const { innerContent, endReplacer, startReplacer } = getBlockContent(style);
|
2018-03-22 12:01:11 +01:00
|
|
|
const defaultContent =
|
|
|
|
|
selectedText === ''
|
|
|
|
|
? replace(content, 'textToReplace', innerContent)
|
|
|
|
|
: replace(content, 'textToReplace', selectedText);
|
2018-03-28 17:29:55 +02:00
|
|
|
const newBlock = createNewBlock(defaultContent);
|
2018-03-20 15:34:27 +01:00
|
|
|
const newContentState = this.createNewContentStateFromBlock(newBlock);
|
2018-03-22 12:01:11 +01:00
|
|
|
const { anchorOffset, focusOffset } = getDefaultSelectionOffsets(
|
|
|
|
|
defaultContent,
|
|
|
|
|
startReplacer,
|
2018-03-22 20:33:34 +01:00
|
|
|
endReplacer,
|
2018-03-22 12:01:11 +01:00
|
|
|
);
|
2018-03-20 15:34:27 +01:00
|
|
|
let newEditorState = this.createNewEditorState(newContentState, defaultContent);
|
2018-03-22 12:01:11 +01:00
|
|
|
const updatedSelection =
|
|
|
|
|
getOffSets(this.getSelection()).start === 0
|
|
|
|
|
? this.getSelection().merge({ anchorOffset, focusOffset })
|
|
|
|
|
: new SelectionState({
|
|
|
|
|
anchorKey: newBlock.getKey(),
|
|
|
|
|
anchorOffset,
|
|
|
|
|
focusOffset,
|
|
|
|
|
focusKey: newBlock.getKey(),
|
|
|
|
|
isBackward: false,
|
|
|
|
|
});
|
2018-03-20 15:34:27 +01:00
|
|
|
|
|
|
|
|
newEditorState = EditorState.acceptSelection(newEditorState, updatedSelection);
|
|
|
|
|
|
2018-03-22 12:01:11 +01:00
|
|
|
return this.setState({
|
|
|
|
|
editorState: EditorState.forceSelection(newEditorState, newEditorState.getSelection()),
|
|
|
|
|
});
|
|
|
|
|
};
|
2018-03-20 15:34:27 +01:00
|
|
|
|
2018-03-15 19:10:02 +01:00
|
|
|
createNewEditorState = (newContentState, text) => {
|
|
|
|
|
let newEditorState;
|
|
|
|
|
|
|
|
|
|
if (getOffSets(this.getSelection()).start !== 0) {
|
2018-03-22 12:01:11 +01:00
|
|
|
newEditorState = EditorState.push(this.getEditorState(), newContentState);
|
2018-03-15 19:10:02 +01:00
|
|
|
} else {
|
2018-03-28 17:29:55 +02:00
|
|
|
const textWithEntity = this.modifyBlockContent(text);
|
2018-03-15 19:10:02 +01:00
|
|
|
newEditorState = EditorState.push(this.getEditorState(), textWithEntity, 'insert-characters');
|
|
|
|
|
}
|
|
|
|
|
return newEditorState;
|
2018-03-22 12:01:11 +01:00
|
|
|
};
|
2018-03-15 19:10:02 +01:00
|
|
|
|
2018-03-22 12:01:11 +01:00
|
|
|
createNewBlockMap = (newBlock, contentState) =>
|
|
|
|
|
contentState.getBlockMap().set(newBlock.key, newBlock);
|
2018-03-15 19:10:02 +01:00
|
|
|
|
2018-03-22 12:01:11 +01:00
|
|
|
createNewContentStateFromBlock = (
|
|
|
|
|
newBlock,
|
2018-03-22 20:33:34 +01:00
|
|
|
contentState = this.getEditorState().getCurrentContent(),
|
2018-03-22 12:01:11 +01:00
|
|
|
) =>
|
|
|
|
|
ContentState.createFromBlockArray(this.createNewBlockMap(newBlock, contentState).toArray())
|
2018-03-15 19:10:02 +01:00
|
|
|
.set('selectionBefore', contentState.getSelectionBefore())
|
2018-03-22 12:01:11 +01:00
|
|
|
.set('selectionAfter', contentState.getSelectionAfter());
|
2018-03-15 19:10:02 +01:00
|
|
|
|
2018-03-15 18:58:40 +01:00
|
|
|
getCharactersNumber = (editorState = this.getEditorState()) => {
|
|
|
|
|
const plainText = editorState.getCurrentContent().getPlainText();
|
|
|
|
|
const spacesNumber = plainText.split(' ').length;
|
|
|
|
|
|
|
|
|
|
return words(plainText).join('').length + spacesNumber - 1;
|
2018-03-22 12:01:11 +01:00
|
|
|
};
|
2018-03-15 18:58:40 +01:00
|
|
|
|
2018-03-15 11:31:07 +01:00
|
|
|
getEditorState = () => this.state.editorState;
|
|
|
|
|
|
|
|
|
|
getSelection = () => this.getEditorState().getSelection();
|
|
|
|
|
|
2018-03-19 18:06:48 +01:00
|
|
|
getCurrentAnchorKey = () => this.getSelection().getAnchorKey();
|
|
|
|
|
|
2018-03-22 12:01:11 +01:00
|
|
|
getCurrentContentBlock = () =>
|
|
|
|
|
this.getEditorState()
|
|
|
|
|
.getCurrentContent()
|
|
|
|
|
.getBlockForKey(this.getSelection().getAnchorKey());
|
2018-03-15 11:31:07 +01:00
|
|
|
|
2018-03-27 18:02:47 +02:00
|
|
|
getNextBlockKey = (currentBlockKey, editorState = this.getEditorState()) =>
|
2018-03-27 18:59:24 +02:00
|
|
|
editorState.getCurrentContent().getKeyAfter(currentBlockKey);
|
2018-03-19 18:06:48 +01:00
|
|
|
|
2018-03-22 12:01:11 +01:00
|
|
|
getSelectedText = ({ start, end } = getOffSets(this.getSelection())) =>
|
|
|
|
|
this.getCurrentContentBlock()
|
|
|
|
|
.getText()
|
|
|
|
|
.slice(start, end);
|
2018-03-15 11:31:07 +01:00
|
|
|
|
2018-03-20 16:53:11 +01:00
|
|
|
handleBlur = () => {
|
|
|
|
|
const target = {
|
|
|
|
|
name: this.props.name,
|
2018-03-28 17:29:55 +02:00
|
|
|
type: 'textarea',
|
2018-03-22 12:01:11 +01:00
|
|
|
value: this.getEditorState()
|
|
|
|
|
.getCurrentContent()
|
|
|
|
|
.getPlainText(),
|
2018-03-20 16:53:11 +01:00
|
|
|
};
|
|
|
|
|
this.props.onBlur({ target });
|
|
|
|
|
this.blur();
|
2018-03-22 12:01:11 +01:00
|
|
|
};
|
2018-03-20 16:53:11 +01:00
|
|
|
|
2018-03-14 09:59:42 +01:00
|
|
|
handleChangeSelect = ({ target }) => {
|
|
|
|
|
this.setState({ headerValue: target.value });
|
2018-03-15 18:10:19 +01:00
|
|
|
const selectedText = this.getSelectedText();
|
|
|
|
|
const title = selectedText === '' ? `${target.value} ` : `${target.value} ${selectedText}`;
|
2018-03-20 14:49:49 +01:00
|
|
|
this.addBlock(title);
|
2018-03-15 18:10:19 +01:00
|
|
|
|
2018-03-20 14:49:49 +01:00
|
|
|
return this.setState({ headerValue: '' });
|
2018-03-22 12:01:11 +01:00
|
|
|
};
|
2018-03-14 09:59:42 +01:00
|
|
|
|
2018-03-16 11:45:21 +01:00
|
|
|
handleClickPreview = () => this.setState({ isPreviewMode: !this.state.isPreviewMode });
|
|
|
|
|
|
2018-03-22 12:01:11 +01:00
|
|
|
handleDragEnter = e => {
|
2018-03-14 22:47:56 +01:00
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
|
|
if (!this.state.isDraging) {
|
|
|
|
|
this.setState({ isDraging: true });
|
|
|
|
|
}
|
2018-03-22 12:01:11 +01:00
|
|
|
};
|
2018-03-14 22:20:39 +01:00
|
|
|
|
|
|
|
|
handleDragLeave = () => this.setState({ isDraging: false });
|
|
|
|
|
|
2018-03-22 12:01:11 +01:00
|
|
|
handleDragOver = e => {
|
2018-03-14 22:20:39 +01:00
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
2018-03-22 12:01:11 +01:00
|
|
|
};
|
2018-03-14 22:20:39 +01:00
|
|
|
|
2018-03-22 12:01:11 +01:00
|
|
|
handleDrop = e => {
|
2018-03-14 22:20:39 +01:00
|
|
|
e.preventDefault();
|
2018-03-22 13:23:26 +01:00
|
|
|
|
|
|
|
|
if (this.state.isPreviewMode) {
|
|
|
|
|
return this.setState({ isDraging: false });
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 12:01:11 +01:00
|
|
|
const { dataTransfer: { files } } = e;
|
2018-03-14 22:47:56 +01:00
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append('files', files[0]);
|
|
|
|
|
const headers = {
|
|
|
|
|
'X-Forwarded-Host': 'strapi',
|
|
|
|
|
};
|
2018-03-14 22:20:39 +01:00
|
|
|
|
2018-03-22 12:01:11 +01:00
|
|
|
return request('/upload', { method: 'POST', headers, body: formData }, false, false)
|
2018-03-14 22:47:56 +01:00
|
|
|
.then(response => {
|
2018-03-28 16:17:27 +02:00
|
|
|
const newContentState = this.createNewContentStateFromBlock(
|
2018-03-28 17:29:55 +02:00
|
|
|
createNewBlock(``),
|
2018-03-28 16:17:27 +02:00
|
|
|
);
|
|
|
|
|
const newEditorState = EditorState.push(this.getEditorState(), newContentState);
|
2018-03-22 13:23:26 +01:00
|
|
|
this.setState({ editorState: newEditorState });
|
2018-03-28 16:17:27 +02:00
|
|
|
this.sendData(newEditorState);
|
2018-03-14 22:47:56 +01:00
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
console.log('error', err.response);
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
this.setState({ isDraging: false });
|
|
|
|
|
});
|
2018-03-22 12:01:11 +01:00
|
|
|
};
|
2018-03-14 22:20:39 +01:00
|
|
|
|
2018-03-27 11:34:38 +02:00
|
|
|
handleKeyCommand = (command, editorState) => {
|
|
|
|
|
const newState = RichUtils.handleKeyCommand(editorState, command);
|
|
|
|
|
|
2018-03-26 16:20:58 +02:00
|
|
|
if (command === 'bold' || command === 'italic' || command === 'underline') {
|
2018-03-28 16:17:27 +02:00
|
|
|
const { content, style } = getKeyCommandData(command);
|
2018-03-26 16:20:58 +02:00
|
|
|
this.addContent(content, style);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 11:34:38 +02:00
|
|
|
if (newState && command !== 'backspace') {
|
2018-03-15 16:54:28 +01:00
|
|
|
this.onChange(newState);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-03-27 11:34:38 +02:00
|
|
|
|
2018-03-15 16:54:28 +01:00
|
|
|
return false;
|
2018-03-22 12:01:11 +01:00
|
|
|
};
|
2018-03-15 18:10:19 +01:00
|
|
|
|
2018-03-27 11:51:57 +02:00
|
|
|
handleReturn = (e, editorState) => {
|
|
|
|
|
const selection = editorState.getSelection();
|
|
|
|
|
const currentBlock = editorState.getCurrentContent().getBlockForKey(selection.getStartKey());
|
|
|
|
|
|
|
|
|
|
if (currentBlock.getText().split('')[0] === '-') {
|
|
|
|
|
this.addUlBlock();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
currentBlock.getText().split('.').length > 1 &&
|
|
|
|
|
!isNaN(parseInt(currentBlock.getText().split('.')[0], 10))
|
|
|
|
|
) {
|
|
|
|
|
this.addOlBlock();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2018-03-27 18:59:24 +02:00
|
|
|
};
|
2018-03-27 11:51:57 +02:00
|
|
|
|
2018-03-22 12:01:11 +01:00
|
|
|
mapKeyToEditorCommand = e => {
|
2018-03-14 09:59:42 +01:00
|
|
|
if (e.keyCode === 9 /* TAB */) {
|
2018-03-22 12:01:11 +01:00
|
|
|
const newEditorState = RichUtils.onTab(e, this.state.editorState, 4 /* maxDepth */);
|
2018-03-14 09:59:42 +01:00
|
|
|
if (newEditorState !== this.state.editorState) {
|
|
|
|
|
this.onChange(newEditorState);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return getDefaultKeyBinding(e);
|
2018-03-22 12:01:11 +01:00
|
|
|
};
|
2018-03-14 09:59:42 +01:00
|
|
|
|
2018-03-28 17:29:55 +02:00
|
|
|
modifyBlockContent = (text, contentState = this.getEditorState().getCurrentContent()) =>
|
|
|
|
|
Modifier.replaceText(contentState, this.getSelection(), text);
|
|
|
|
|
|
2018-03-22 12:01:11 +01:00
|
|
|
onChange = editorState => {
|
2018-03-19 12:30:04 +01:00
|
|
|
this.setState({ editorState });
|
2018-03-28 16:17:27 +02:00
|
|
|
this.sendData(editorState);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sendData = editorState =>
|
2018-03-22 12:01:11 +01:00
|
|
|
this.props.onChange({
|
|
|
|
|
target: {
|
|
|
|
|
value: editorState.getCurrentContent().getPlainText(),
|
|
|
|
|
name: this.props.name,
|
|
|
|
|
type: 'textarea',
|
|
|
|
|
},
|
|
|
|
|
});
|
2018-03-15 20:21:21 +01:00
|
|
|
|
2018-03-22 12:01:11 +01:00
|
|
|
toggleFullScreen = e => {
|
2018-03-12 14:02:34 +01:00
|
|
|
e.preventDefault();
|
2018-03-22 20:33:34 +01:00
|
|
|
this.setState({
|
|
|
|
|
isFullscreen: !this.state.isFullscreen,
|
|
|
|
|
isPreviewMode: false,
|
|
|
|
|
});
|
2018-03-22 12:01:11 +01:00
|
|
|
};
|
2018-03-12 14:02:34 +01:00
|
|
|
|
2018-03-08 12:56:44 +01:00
|
|
|
componentDidCatch(error, info) {
|
|
|
|
|
console.log('err', error);
|
|
|
|
|
console.log('info', info);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 20:33:34 +01:00
|
|
|
renderDrop = () => (
|
|
|
|
|
<Drop
|
|
|
|
|
onDrop={this.handleDrop}
|
|
|
|
|
onDragOver={this.handleDragOver}
|
|
|
|
|
onDragLeave={this.handleDragLeave}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
|
2018-03-05 14:30:28 +01:00
|
|
|
render() {
|
2018-03-22 20:33:34 +01:00
|
|
|
const { editorState, isFocused, isPreviewMode, isFullscreen } = this.state;
|
|
|
|
|
const editorStyle = isFullscreen ? { marginTop: '0' } : this.props.style;
|
2018-03-28 16:17:27 +02:00
|
|
|
|
2018-03-05 14:30:28 +01:00
|
|
|
return (
|
2018-03-23 16:48:45 +01:00
|
|
|
<div className={cn(isFullscreen && styles.fullscreenOverlay)}>
|
2018-03-22 18:19:49 +01:00
|
|
|
{/* FIRST EDITOR WITH CONTROLS} */}
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
styles.editorWrapper,
|
|
|
|
|
this.state.isFocused && styles.editorFocus,
|
|
|
|
|
!this.props.deactivateErrorHighlight && this.props.error && styles.editorError,
|
|
|
|
|
!isEmpty(this.props.className) && this.props.className,
|
2018-03-22 20:33:34 +01:00
|
|
|
isFullscreen && isFocused && styles.fullscreenFocus,
|
2018-03-22 18:19:49 +01:00
|
|
|
)}
|
2018-03-22 20:33:34 +01:00
|
|
|
onClick={e => {
|
|
|
|
|
if (isFullscreen) {
|
2018-03-22 18:19:49 +01:00
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
onDragEnter={this.handleDragEnter}
|
|
|
|
|
onDragOver={this.handleDragOver}
|
|
|
|
|
style={editorStyle}
|
|
|
|
|
>
|
2018-03-22 20:33:34 +01:00
|
|
|
{this.state.isDraging && this.renderDrop()}
|
2018-03-22 18:19:49 +01:00
|
|
|
<div className={styles.controlsContainer}>
|
2018-03-22 20:33:34 +01:00
|
|
|
<CustomSelect />
|
2018-03-22 18:19:49 +01:00
|
|
|
{CONTROLS.map((value, key) => (
|
|
|
|
|
<Controls
|
|
|
|
|
key={key}
|
|
|
|
|
buttons={value}
|
|
|
|
|
disabled={isPreviewMode}
|
|
|
|
|
editorState={editorState}
|
|
|
|
|
handlers={{
|
|
|
|
|
addContent: this.addContent,
|
|
|
|
|
addOlBlock: this.addOlBlock,
|
|
|
|
|
addSimpleBlockWithSelection: this.addSimpleBlockWithSelection,
|
|
|
|
|
addUlBlock: this.addUlBlock,
|
|
|
|
|
}}
|
|
|
|
|
onToggle={this.toggleInlineStyle}
|
|
|
|
|
onToggleBlock={this.toggleBlockType}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
2018-03-23 16:40:01 +01:00
|
|
|
{!isFullscreen ? (
|
2018-03-22 20:33:34 +01:00
|
|
|
<ToggleMode isPreviewMode={isPreviewMode} onClick={this.handleClickPreview} />
|
2018-03-27 18:59:24 +02:00
|
|
|
) : (
|
|
|
|
|
<div style={{ marginRight: '10px' }} />
|
|
|
|
|
)}
|
2018-03-08 18:55:49 +01:00
|
|
|
</div>
|
2018-03-22 18:19:49 +01:00
|
|
|
{/* WYSIWYG PREVIEW NOT FULLSCREEN */}
|
|
|
|
|
{isPreviewMode ? (
|
2018-03-22 20:33:34 +01:00
|
|
|
<PreviewWysiwyg />
|
2018-03-22 18:19:49 +01:00
|
|
|
) : (
|
2018-03-22 20:33:34 +01:00
|
|
|
<div
|
|
|
|
|
className={cn(styles.editor, isFullscreen && styles.editorFullScreen)}
|
|
|
|
|
onClick={this.focus}
|
|
|
|
|
>
|
2018-03-22 18:19:49 +01:00
|
|
|
<WysiwygEditor
|
|
|
|
|
blockStyleFn={getBlockStyle}
|
|
|
|
|
editorState={editorState}
|
|
|
|
|
handleKeyCommand={this.handleKeyCommand}
|
2018-03-27 11:34:38 +02:00
|
|
|
handleReturn={this.handleReturn}
|
2018-03-22 18:19:49 +01:00
|
|
|
keyBindingFn={this.mapKeyToEditorCommand}
|
|
|
|
|
onBlur={this.handleBlur}
|
|
|
|
|
onChange={this.onChange}
|
|
|
|
|
placeholder={this.props.placeholder}
|
|
|
|
|
setRef={editor => (this.domEditor = editor)}
|
2018-03-27 11:51:57 +02:00
|
|
|
stripPastedStyles
|
2018-03-22 18:19:49 +01:00
|
|
|
/>
|
|
|
|
|
<input className={styles.editorInput} value="" tabIndex="-1" />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2018-03-22 20:33:34 +01:00
|
|
|
{!isFullscreen && (
|
2018-03-22 18:19:49 +01:00
|
|
|
<WysiwygBottomControls
|
|
|
|
|
charactersNumber={this.getCharactersNumber()}
|
|
|
|
|
onClick={this.toggleFullScreen}
|
2018-03-08 12:56:44 +01:00
|
|
|
/>
|
2018-03-22 18:19:49 +01:00
|
|
|
)}
|
2018-03-08 12:56:44 +01:00
|
|
|
</div>
|
2018-03-22 18:19:49 +01:00
|
|
|
{/* PREVIEW WYSIWYG FULLSCREEN */}
|
2018-03-22 20:33:34 +01:00
|
|
|
{isFullscreen && (
|
2018-03-22 18:19:49 +01:00
|
|
|
<div
|
|
|
|
|
className={cn(styles.editorWrapper, isFocused && styles.fullscreenPreviewFocused)}
|
2018-03-22 20:33:34 +01:00
|
|
|
onClick={e => {
|
2018-03-22 18:19:49 +01:00
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
}}
|
2018-03-22 20:33:34 +01:00
|
|
|
style={{ marginTop: '0' }}
|
2018-03-22 18:19:49 +01:00
|
|
|
>
|
2018-03-22 20:33:34 +01:00
|
|
|
<PreviewControl
|
|
|
|
|
onClick={this.toggleFullScreen}
|
|
|
|
|
characters={this.getCharactersNumber()}
|
|
|
|
|
/>
|
|
|
|
|
<PreviewWysiwyg />
|
2018-03-16 13:48:32 +01:00
|
|
|
</div>
|
2018-03-22 12:01:11 +01:00
|
|
|
)}
|
2018-03-06 18:20:05 +01:00
|
|
|
</div>
|
2018-03-05 14:30:28 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 20:33:34 +01:00
|
|
|
Wysiwyg.childContextTypes = {
|
|
|
|
|
handleChangeSelect: PropTypes.func,
|
|
|
|
|
headerValue: PropTypes.string,
|
|
|
|
|
html: PropTypes.string,
|
|
|
|
|
isFocused: PropTypes.bool,
|
|
|
|
|
isFullscreen: PropTypes.bool,
|
|
|
|
|
isPreviewMode: PropTypes.bool,
|
|
|
|
|
placeholder: PropTypes.string,
|
|
|
|
|
previewHTML: PropTypes.func,
|
|
|
|
|
};
|
|
|
|
|
|
2018-03-05 14:30:28 +01:00
|
|
|
Wysiwyg.defaultProps = {
|
2018-03-05 19:23:06 +01:00
|
|
|
autoFocus: false,
|
2018-03-20 16:53:11 +01:00
|
|
|
className: '',
|
|
|
|
|
deactivateErrorHighlight: false,
|
|
|
|
|
error: false,
|
|
|
|
|
onBlur: () => {},
|
2018-03-08 12:56:44 +01:00
|
|
|
onChange: () => {},
|
2018-03-06 18:20:05 +01:00
|
|
|
placeholder: '',
|
2018-03-20 16:53:11 +01:00
|
|
|
style: {},
|
2018-03-08 12:56:44 +01:00
|
|
|
value: '',
|
2018-03-05 14:30:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Wysiwyg.propTypes = {
|
|
|
|
|
autoFocus: PropTypes.bool,
|
2018-03-20 16:53:11 +01:00
|
|
|
className: PropTypes.string,
|
|
|
|
|
deactivateErrorHighlight: PropTypes.bool,
|
|
|
|
|
error: PropTypes.bool,
|
2018-03-08 12:56:44 +01:00
|
|
|
name: PropTypes.string.isRequired,
|
2018-03-20 16:53:11 +01:00
|
|
|
onBlur: PropTypes.func,
|
2018-03-08 12:56:44 +01:00
|
|
|
onChange: PropTypes.func,
|
2018-03-05 14:30:28 +01:00
|
|
|
placeholder: PropTypes.string,
|
2018-03-20 16:53:11 +01:00
|
|
|
style: PropTypes.object,
|
2018-03-08 12:56:44 +01:00
|
|
|
value: PropTypes.string,
|
2018-03-05 14:30:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Wysiwyg;
|