Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add valueToString to avoid ternaries when converting to strings
  • Loading branch information
youknowriad authored and ellatrix committed Apr 16, 2018
commit dd9e0992fe03ce9a5769a5d45545b228dbbcb2da
17 changes: 17 additions & 0 deletions blocks/rich-text/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ export function elementToString( value ) {
return renderToString( value );
}

/**
* Transforms a value in a given format into string.
*
* @param {Array|string?} value DOM Elements.
* @param {string} format Output format (string or element)
*
* @return {string} HTML output as string.
*/
export function valueToString( value, format ) {
switch ( format ) {
case 'string':
return value || '';
default:
return elementToString( value );
}
}

/**
* Strips out TinyMCE specific attributes and nodes from a WPElement
*
Expand Down
10 changes: 2 additions & 8 deletions blocks/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { pickAriaProps } from './aria';
import patterns from './patterns';
import { EVENTS } from './constants';
import { withBlockEditContext } from '../block-edit/context';
import { domToString, elementToString } from './format';
import { domToFormat, valueToString } from './format';

const { BACKSPACE, DELETE, ENTER } = keycodes;

Expand Down Expand Up @@ -679,13 +679,7 @@ export class RichText extends Component {

setContent( content ) {
const { format } = this.props;
switch ( format ) {
case 'string':
this.editor.setContent( content || '' );
break;
default:
this.editor.setContent( elementToString( content ) );
}
this.editor.setContent( valueToString( content, format ) );
}

getContent() {
Expand Down
11 changes: 3 additions & 8 deletions blocks/rich-text/tinymce.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { Component, Children, createElement } from '@wordpress/element';
import { Component, createElement } from '@wordpress/element';

/**
* Internal dependencies
*/
import { diffAriaProps, pickAriaProps } from './aria';
import { valueToString } from './format';

const IS_PLACEHOLDER_VISIBLE_ATTR_NAME = 'data-is-placeholder-visible';
export default class TinyMCE extends Component {
Expand Down Expand Up @@ -105,12 +106,6 @@ export default class TinyMCE extends Component {
// If a default value is provided, render it into the DOM even before
// TinyMCE finishes initializing. This avoids a short delay by allowing
// us to show and focus the content before it's truly ready to edit.
let extraProps = {};
if ( defaultValue && format === 'string' ) {
extraProps = { dangerouslySetInnerHTML: { __html: defaultValue } };
} else if ( defaultValue ) {
extraProps = { children: Children.toArray( defaultValue ) };
}

return createElement( tagName, {
...ariaProps,
Expand All @@ -120,7 +115,7 @@ export default class TinyMCE extends Component {
ref: ( node ) => this.editorNode = node,
style,
suppressContentEditableWarning: true,
...extraProps,
dangerouslySetInnerHTML: { __html: valueToString( defaultValue, format ) },
} );
}
}