Skip to content

Commit

Permalink
Rich Text: Consolidate isEmpty conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed May 2, 2018
1 parent 1076746 commit 26be694
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
17 changes: 0 additions & 17 deletions blocks/rich-text/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
24 changes: 16 additions & 8 deletions blocks/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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: `<img src="${ createBlobURL( blob ) }">`,
mode: 'BLOCKS',
Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 = (
Expand Down

0 comments on commit 26be694

Please sign in to comment.