diff --git a/packages/strapi-helper-plugin/lib/src/components/Wysiwyg/constants.js b/packages/strapi-helper-plugin/lib/src/components/Wysiwyg/constants.js index 40a0b1ee13..deb5d61090 100644 --- a/packages/strapi-helper-plugin/lib/src/components/Wysiwyg/constants.js +++ b/packages/strapi-helper-plugin/lib/src/components/Wysiwyg/constants.js @@ -25,7 +25,7 @@ export const CONTROLS = [ export const NEW_CONTROLS = [ [ - {label: 'B', style: 'BOLD', handler: 'addEntity', text: '__innerText__' }, + {label: 'B', style: 'BOLD', handler: 'addEntity', text: '**innerText**' }, {label: 'I', style: 'ITALIC', className: 'styleButtonItalic', handler: 'addEntity', text: '*innerText*' }, {label: 'U', style: 'UNDERLINE', handler: 'addEntity', text: 'innerText' }, {label: 'UL', style: 'unordered-list-item', className: 'styleButtonUL', hideLabel: true, handler: 'addEntity', text: '- innerText' }, 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 71a645e470..b891e0f070 100644 --- a/packages/strapi-helper-plugin/lib/src/components/Wysiwyg/index.js +++ b/packages/strapi-helper-plugin/lib/src/components/Wysiwyg/index.js @@ -9,15 +9,17 @@ import { // CompositeDecorator, ContentState, convertFromHTML, + convertFromRaw, // convertToRaw, Editor, EditorState, getDefaultKeyBinding, Modifier, RichUtils, + SelectionState, } from 'draft-js'; import PropTypes from 'prop-types'; -import { cloneDeep, isEmpty, replace } from 'lodash'; +import { cloneDeep, isEmpty, replace, trimStart, trimEnd } from 'lodash'; import cn from 'classnames'; import { FormattedMessage } from 'react-intl'; @@ -74,26 +76,38 @@ class Wysiwyg extends React.Component { addEntity = (text, style) => { const editorState = this.state.editorState; const currentContent = editorState.getCurrentContent(); - // Get the selected text const selection = editorState.getSelection(); const anchorKey = selection.getAnchorKey(); const currentContentBlock = currentContent.getBlockForKey(anchorKey); + // Range of the text we want to replace const start = selection.getStartOffset(); const end = selection.getEndOffset(); const selectedText = currentContentBlock.getText().slice(start, end); const innerText = selectedText === '' ? getInnerText(style) : replace(text, 'innerText', selectedText); - // Replace it with the value + + // TODO const value to trim + const innerTextLength = trimStart(innerText, '*-`').length; + const focusOffset = start === end ? trimEnd(innerText, '*`').length : start + trimEnd(innerText, '*`').length; + const anchorOffset = start + innerText.length - trimStart(innerText, '*-`').length; + const updateSelection = selection.merge({ + anchorOffset, + focusOffset, + }); const textWithEntity = Modifier.replaceText(currentContent, selection, innerText); + const newEditorState = EditorState.push(editorState, textWithEntity, 'insert-characters'); this.setState({ - editorState: EditorState.push(editorState, textWithEntity, 'insert-characters'), + editorState: EditorState.forceSelection(newEditorState, updateSelection), headerValue: '', }, () => { this.focus(); }); } + getStartOffset = (text) => text.split('innerText')[0].length; + + handleChangeSelect = ({ target }) => { this.setState({ headerValue: target.value }); const splitData = target.value.split('.'); @@ -121,6 +135,7 @@ class Wysiwyg extends React.Component { } return; } + return getDefaultKeyBinding(e); }