Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rich Text: Determine emptiness by value #5091

Merged
merged 3 commits into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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