Skip to content

Commit

Permalink
TinyMCE per block: Adding a preview button to display the generated H…
Browse files Browse the repository at this point in the history
…TML (#176)
  • Loading branch information
youknowriad authored Mar 3, 2017
1 parent a4dd691 commit b9e5350
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
27 changes: 17 additions & 10 deletions tinymce-per-block/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ class App extends Component {
const type = forceType ? forceType : this.state.activeRenderer;
switch ( type ) {
case 'block':
const html = serializers.block.serialize(
nextContent.map( block => {
const blockDefinition = getBlock( block.blockType );
return blockDefinition.serialize( block );
} )
);
this.setState( {
content: {
block: nextContent,
html: serializers.block.serialize(
nextContent.map( block => {
const blockDefinition = getBlock( block.blockType );
return blockDefinition.serialize( block );
} )
)
raw: html,
html
}
} );
return;
Expand All @@ -50,16 +52,17 @@ class App extends Component {
return parsedBlock ? parsedBlock : getBlock( 'text' ).parse( rawBlock );
} )
.map( block => Object.assign( { uid: uniqueId() }, block ) ),
html: nextContent
html: nextContent,
raw: nextContent
}
} );
return;
}
}

toggleRenderer = () => {
selectRenderer = ( renderer ) => () => {
this.setState( {
activeRenderer: this.state.activeRenderer === 'block' ? 'html' : 'block'
activeRenderer: renderer
} );
}

Expand All @@ -72,7 +75,11 @@ class App extends Component {
const Renderer = renderers[ activeRenderer ];
return (
<div className="renderers">
<button className="toggle-renderer" onClick={ this.toggleRenderer }>Html/Block</button>
<div className="toggle-renderer">
<button onClick={ this.selectRenderer( 'block' ) }>Block</button>
<button onClick={ this.selectRenderer( 'html' ) }>Html</button>
<button onClick={ this.selectRenderer( 'raw' ) }>Preview</button>
</div>
<div className={ `renderer-${ activeRenderer }` }>
<Renderer content={ content[ activeRenderer ] } onChange={ this.updateParsedContent } />
</div>
Expand Down
1 change: 1 addition & 0 deletions tinymce-per-block/src/renderers/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as block } from './block';
export { default as html } from './html';
export { default as raw } from './raw';
10 changes: 10 additions & 0 deletions tinymce-per-block/src/renderers/raw/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* External dependencies
*/
import { createElement } from 'wp-elements';

export default function RawRenderer( { content } ) {
return (
<div dangerouslySetInnerHTML={ { __html: content } } />
);
}

0 comments on commit b9e5350

Please sign in to comment.