From 26be694b9624ea41fbf6f237d3d0a5281cd3a536 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Wed, 2 May 2018 09:50:10 -0400 Subject: [PATCH] Rich Text: Consolidate isEmpty conditions --- blocks/rich-text/format.js | 17 ----------------- blocks/rich-text/index.js | 24 ++++++++++++++++-------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/blocks/rich-text/format.js b/blocks/rich-text/format.js index 595bff937aa8d..221966b81fa7e 100644 --- a/blocks/rich-text/format.js +++ b/blocks/rich-text/format.js @@ -108,20 +108,3 @@ export function domToFormat( value, format, editor ) { return domToElement( value ); } } - -/** - * Checks whether the value is empty or not - * - * @param {Array|string} value Value. - * @param {string} format Format (string or element) - * - * @return {boolean} Is value empty. - */ -export function isEmpty( value, format ) { - switch ( format ) { - case 'string': - return value === ''; - default: - return ! value.length; - } -} diff --git a/blocks/rich-text/index.js b/blocks/rich-text/index.js index 74a7e3c975ff8..7a463ded84dee 100644 --- a/blocks/rich-text/index.js +++ b/blocks/rich-text/index.js @@ -43,7 +43,7 @@ import { pickAriaProps } from './aria'; import patterns from './patterns'; import { EVENTS } from './constants'; import { withBlockEditContext } from '../block-edit/context'; -import { domToFormat, valueToString, isEmpty } from './format'; +import { domToFormat, valueToString } from './format'; const { BACKSPACE, DELETE, ENTER } = keycodes; @@ -283,8 +283,7 @@ export class RichText extends Component { // Note: a pasted file may have the URL as plain text. if ( item && ! HTML ) { const blob = item.getAsFile ? item.getAsFile() : item; - const rootNode = this.editor.getBody(); - const isEmptyEditor = this.editor.dom.isEmpty( rootNode ); + const isEmptyEditor = this.isEmpty(); const content = rawHandler( { HTML: ``, mode: 'BLOCKS', @@ -347,8 +346,7 @@ export class RichText extends Component { } } - const rootNode = this.editor.getBody(); - const isEmptyEditor = this.editor.dom.isEmpty( rootNode ); + const isEmptyEditor = this.isEmpty(); let mode = 'INLINE'; @@ -377,7 +375,7 @@ export class RichText extends Component { return; } - if ( isEmpty && this.props.onReplace ) { + if ( isEmptyEditor && this.props.onReplace ) { this.props.onReplace( content ); } else { this.splitContent( content ); @@ -447,7 +445,7 @@ export class RichText extends Component { this.props.onMerge( forward ); } - if ( this.props.onRemove && dom.isEmpty( rootNode ) ) { + if ( this.props.onRemove && this.isEmpty() ) { this.props.onRemove( forward ); } @@ -732,6 +730,16 @@ export class RichText extends Component { } } + /** + * Returns true if the field is currently empty, or false otherwise. + * + * @return {boolean} Whether field is empty. + */ + isEmpty() { + const { value } = this.props; + return ! value || ! value.length; + } + isFormatActive( format ) { return this.state.formats[ format ] && this.state.formats[ format ].isActive; } @@ -811,7 +819,7 @@ export class RichText extends Component { // changes, we unmount and destroy the previous TinyMCE element, then // mount and initialize a new child element in its place. const key = [ 'editor', Tagname ].join(); - const isPlaceholderVisible = placeholder && ( ! isSelected || keepPlaceholderOnFocus ) && isEmpty( value, format ); + const isPlaceholderVisible = placeholder && ( ! isSelected || keepPlaceholderOnFocus ) && this.isEmpty(); const classes = classnames( wrapperClassName, 'blocks-rich-text' ); const formatToolbar = (