Skip to content

Commit

Permalink
Rich Text: Determine emptiness by value (#5091)
Browse files Browse the repository at this point in the history
* Rich Text: Determine emptiness by value

* Rich Text: Filter both sets of child nodes in split

* Rich Text: Consolidate isEmpty conditions
  • Loading branch information
aduth authored May 2, 2018
1 parent 63cf515 commit 5b2e500
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 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;
}
}
30 changes: 18 additions & 12 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 @@ -105,7 +105,7 @@ export function getFormatProperties( formatName, parents ) {
const DEFAULT_FORMATS = [ 'bold', 'italic', 'strikethrough', 'link' ];

export class RichText extends Component {
constructor( { value } ) {
constructor() {
super( ...arguments );

this.onInit = this.onInit.bind( this );
Expand All @@ -127,7 +127,6 @@ export class RichText extends Component {
selectedNodeId: 0,
};

this.isEmpty = ! value || ! value.length;
this.containerRef = createRef();
}

Expand Down Expand Up @@ -284,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 @@ -348,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 @@ -378,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 All @@ -392,7 +389,6 @@ export class RichText extends Component {

onChange() {
this.savedContent = this.getContent();
this.isEmpty = isEmpty( this.savedContent, this.props.format );
this.props.onChange( this.savedContent );
}

Expand Down Expand Up @@ -449,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 @@ -583,7 +579,7 @@ export class RichText extends Component {
const afterFragment = afterRange.extractContents();

const { format } = this.props;
const before = domToFormat( beforeFragment.childNodes, format, this.editor );
const before = domToFormat( filterEmptyNodes( beforeFragment.childNodes ), format, this.editor );
const after = domToFormat( filterEmptyNodes( afterFragment.childNodes ), format, this.editor );

this.restoreContentAndSplit( before, after, blocks );
Expand Down Expand Up @@ -734,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 @@ -813,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 ) && this.isEmpty;
const isPlaceholderVisible = placeholder && ( ! isSelected || keepPlaceholderOnFocus ) && this.isEmpty();
const classes = classnames( wrapperClassName, 'blocks-rich-text' );

const formatToolbar = (
Expand Down

0 comments on commit 5b2e500

Please sign in to comment.