From cf2ee11074d992938e0efb3097419a30b55d455a Mon Sep 17 00:00:00 2001 From: cyril lopez Date: Thu, 22 Mar 2018 13:23:26 +0100 Subject: [PATCH] Add disabled style to InputSelect Disable wysiwyg controls when in preview mode --- .../lib/src/components/InputSelect/index.js | 1 + .../src/components/InputSelect/styles.scss | 4 ++ .../lib/src/components/Wysiwyg/components.js | 42 +++++++------------ .../lib/src/components/Wysiwyg/index.js | 41 +++++++++++------- .../components/WysiwygInlineControls/index.js | 15 +++++-- .../WysiwygInlineControls/styles.scss | 4 ++ 6 files changed, 61 insertions(+), 46 deletions(-) diff --git a/packages/strapi-helper-plugin/lib/src/components/InputSelect/index.js b/packages/strapi-helper-plugin/lib/src/components/InputSelect/index.js index 96f38935b6..586ccf4085 100644 --- a/packages/strapi-helper-plugin/lib/src/components/InputSelect/index.js +++ b/packages/strapi-helper-plugin/lib/src/components/InputSelect/index.js @@ -24,6 +24,7 @@ function InputSelect(props) { 'form-control', !props.deactivateErrorHighlight && props.error && 'is-invalid', !isEmpty(props.className) && props.className, + props.disabled && styles.inputSelectDisabled, )} disabled={props.disabled} id={props.name} diff --git a/packages/strapi-helper-plugin/lib/src/components/InputSelect/styles.scss b/packages/strapi-helper-plugin/lib/src/components/InputSelect/styles.scss index 0b9b5ebe3d..e970ffa1a3 100644 --- a/packages/strapi-helper-plugin/lib/src/components/InputSelect/styles.scss +++ b/packages/strapi-helper-plugin/lib/src/components/InputSelect/styles.scss @@ -15,3 +15,7 @@ -webkit-appearance: none; box-shadow: 0px 1px 1px rgba(104, 118, 142, 0.05); } + +.inputSelectDisabled { + cursor: not-allowed; +} diff --git a/packages/strapi-helper-plugin/lib/src/components/Wysiwyg/components.js b/packages/strapi-helper-plugin/lib/src/components/Wysiwyg/components.js index 3406cfafd6..bdd451b696 100644 --- a/packages/strapi-helper-plugin/lib/src/components/Wysiwyg/components.js +++ b/packages/strapi-helper-plugin/lib/src/components/Wysiwyg/components.js @@ -5,30 +5,20 @@ * */ -import React from "react"; -import { FormattedMessage } from "react-intl"; -import PropTypes from "prop-types"; -import styles from "./styles.scss"; +import React from 'react'; +import { FormattedMessage } from 'react-intl'; +import PropTypes from 'prop-types'; +import styles from './styles.scss'; const Image = props => { - const { alt, height, src, width } = props.contentState - .getEntity(props.entityKey) - .getData(); + const { alt, height, src, width } = props.contentState.getEntity(props.entityKey).getData(); - return ( - {alt} - ); + return {alt}; }; Image.propTypes = { contentState: PropTypes.object.isRequired, - entityKey: PropTypes.string.isRequired + entityKey: PropTypes.string.isRequired, }; const Link = props => { @@ -42,26 +32,22 @@ const Link = props => { }; Link.defaultProps = { - children: "" + children: '', }; Link.propTypes = { children: PropTypes.node, contentState: PropTypes.object.isRequired, - entityKey: PropTypes.string.isRequired + entityKey: PropTypes.string.isRequired, }; const ToggleMode = props => { const label = props.isPreviewMode - ? "components.Wysiwyg.ToggleMode.markdown" - : "components.Wysiwyg.ToggleMode.preview"; + ? 'components.Wysiwyg.ToggleMode.markdown' + : 'components.Wysiwyg.ToggleMode.preview'; return ( - ); @@ -69,12 +55,12 @@ const ToggleMode = props => { ToggleMode.defaultProps = { isPreviewMode: false, - onClick: () => {} + onClick: () => {}, }; ToggleMode.propTypes = { isPreviewMode: PropTypes.bool, - onClick: PropTypes.func + onClick: PropTypes.func, }; export { Image, Link, ToggleMode }; 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 ee5c63681d..1f3a160229 100644 --- a/packages/strapi-helper-plugin/lib/src/components/Wysiwyg/index.js +++ b/packages/strapi-helper-plugin/lib/src/components/Wysiwyg/index.js @@ -110,19 +110,19 @@ class Wysiwyg extends React.Component { const textWithEntity = Modifier.replaceText( this.getEditorState().getCurrentContent(), this.getSelection(), - defaultContent, + defaultContent ); const { anchorOffset, focusOffset } = getDefaultSelectionOffsets( defaultContent, startReplacer, endReplacer, - cursorPosition, + cursorPosition ); const updatedSelection = this.getSelection().merge({ anchorOffset, focusOffset }); const newEditorState = EditorState.push( this.getEditorState(), textWithEntity, - 'insert-character', + 'insert-character' ); // Don't handle selection @@ -133,7 +133,7 @@ class Wysiwyg extends React.Component { }, () => { this.focus(); - }, + } ); } @@ -184,7 +184,7 @@ class Wysiwyg extends React.Component { const { anchorOffset, focusOffset } = getDefaultSelectionOffsets( defaultContent, startReplacer, - endReplacer, + endReplacer ); let newEditorState = this.createNewEditorState(newContentState, defaultContent); const updatedSelection = @@ -214,7 +214,7 @@ class Wysiwyg extends React.Component { const textWithEntity = Modifier.replaceText( this.getEditorState().getCurrentContent(), this.getSelection(), - text, + text ); newEditorState = EditorState.push(this.getEditorState(), textWithEntity, 'insert-characters'); } @@ -229,7 +229,7 @@ class Wysiwyg extends React.Component { createNewContentStateFromBlock = ( newBlock, - contentState = this.getEditorState().getCurrentContent(), + contentState = this.getEditorState().getCurrentContent() ) => ContentState.createFromBlockArray(this.createNewBlockMap(newBlock, contentState).toArray()) .set('selectionBefore', contentState.getSelectionBefore()) @@ -304,6 +304,11 @@ class Wysiwyg extends React.Component { handleDrop = e => { e.preventDefault(); + + if (this.state.isPreviewMode) { + return this.setState({ isDraging: false }); + } + const { dataTransfer: { files } } = e; const formData = new FormData(); formData.append('files', files[0]); @@ -319,7 +324,14 @@ class Wysiwyg extends React.Component { const newContentState = this.createNewContentStateFromBlock(newBlock); const newEditorState = EditorState.push(editorState, newContentState); - this.setState({ editorState: EditorState.moveFocusToEnd(newEditorState) }); + this.setState({ editorState: newEditorState }); + this.props.onChange({ + target: { + value: newEditorState.getCurrentContent().getPlainText(), + name: this.props.name, + type: 'textarea', + }, + }); }) .catch(err => { console.log('error', err.response); @@ -361,15 +373,14 @@ class Wysiwyg extends React.Component { const textWithEntity = Modifier.replaceText( this.getEditorState().getCurrentContent(), this.getSelection(), - ' ', + ' ' ); const newEditorState = EditorState.push( this.getEditorState(), textWithEntity, - 'insert-characters', + 'insert-characters' ); - // return this.setState({ editorState: EditorState.moveFocusToEnd(newEditorState) }); return this.setState({ editorState: newEditorState }); } }; @@ -405,7 +416,7 @@ class Wysiwyg extends React.Component { if (blocksFromHTML.contentBlocks) { const contentState = ContentState.createFromBlockArray( blocksFromHTML.contentBlocks, - blocksFromHTML.entityMap, + blocksFromHTML.entityMap ); return EditorState.createWithContent(contentState, decorator); } @@ -422,7 +433,7 @@ class Wysiwyg extends React.Component { }, () => { this.focus(); - }, + } ); }; @@ -440,7 +451,7 @@ class Wysiwyg extends React.Component { styles.editorWrapper, this.state.isFocused && styles.editorFocus, !this.props.deactivateErrorHighlight && this.props.error && styles.editorError, - !isEmpty(this.props.className) && this.props.className, + !isEmpty(this.props.className) && this.props.className )} onDragEnter={this.handleDragEnter} onDragOver={this.handleDragOver} @@ -456,6 +467,7 @@ class Wysiwyg extends React.Component {