Skip to content

Commit

Permalink
Custom HTML Block: apply editor-styles to preview mode. (#13080)
Browse files Browse the repository at this point in the history
  • Loading branch information
torounit authored and youknowriad committed Mar 6, 2019
1 parent 7d0a418 commit cd11278
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 57 deletions.
79 changes: 79 additions & 0 deletions packages/block-library/src/html/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { BlockControls, PlainText, transformStyles } from '@wordpress/editor';
import { Disabled, SandBox } from '@wordpress/components';
import { withSelect } from '@wordpress/data';

class HTMLEdit extends Component {
constructor() {
super( ...arguments );
this.state = {
isPreview: false,
styles: [],
};
this.switchToHTML = this.switchToHTML.bind( this );
this.switchToPreview = this.switchToPreview.bind( this );
}

componentDidMount() {
const { styles } = this.props;
this.setState( { styles: transformStyles( styles ) } );
}

switchToPreview() {
this.setState( { isPreview: true } );
}

switchToHTML() {
this.setState( { isPreview: false } );
}

render() {
const { attributes, setAttributes } = this.props;
const { isPreview, styles } = this.state;

return (
<div className="wp-block-html">
<BlockControls>
<div className="components-toolbar">
<button
className={ `components-tab-button ${ ! isPreview ? 'is-active' : '' }` }
onClick={ this.switchToHTML }
>
<span>HTML</span>
</button>
<button
className={ `components-tab-button ${ isPreview ? 'is-active' : '' }` }
onClick={ this.switchToPreview }
>
<span>{ __( 'Preview' ) }</span>
</button>
</div>
</BlockControls>
<Disabled.Consumer>
{ ( isDisabled ) => (
( isPreview || isDisabled ) ? (
<SandBox html={ attributes.content } styles={ styles } />
) : (
<PlainText
value={ attributes.content }
onChange={ ( content ) => setAttributes( { content } ) }
placeholder={ __( 'Write HTML…' ) }
aria-label={ __( 'HTML' ) }
/>
)
) }
</Disabled.Consumer>
</div>
);
}
}
export default withSelect( ( select ) => {
const { getEditorSettings } = select( 'core/editor' );
return {
styles: getEditorSettings().styles,
};
} )( HTMLEdit );
46 changes: 7 additions & 39 deletions packages/block-library/src/html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
*/
import { RawHTML } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Disabled, SandBox, SVG, Path } from '@wordpress/components';
import { SVG, Path } from '@wordpress/components';
import { getPhrasingContentSchema } from '@wordpress/blocks';
import { BlockControls, PlainText } from '@wordpress/editor';
import { withState } from '@wordpress/compose';

/**
* Internal dependencies
*/
import edit from './edit';

export const name = 'core/html';

Expand Down Expand Up @@ -56,42 +59,7 @@ export const settings = {
],
},

edit: withState( {
isPreview: false,
} )( ( { attributes, setAttributes, setState, isPreview } ) => (
<div className="wp-block-html">
<BlockControls>
<div className="components-toolbar">
<button
className={ `components-tab-button ${ ! isPreview ? 'is-active' : '' }` }
onClick={ () => setState( { isPreview: false } ) }
>
<span>HTML</span>
</button>
<button
className={ `components-tab-button ${ isPreview ? 'is-active' : '' }` }
onClick={ () => setState( { isPreview: true } ) }
>
<span>{ __( 'Preview' ) }</span>
</button>
</div>
</BlockControls>
<Disabled.Consumer>
{ ( isDisabled ) => (
( isPreview || isDisabled ) ? (
<SandBox html={ attributes.content } />
) : (
<PlainText
value={ attributes.content }
onChange={ ( content ) => setAttributes( { content } ) }
placeholder={ __( 'Write HTML…' ) }
aria-label={ __( 'HTML' ) }
/>
)
) }
</Disabled.Consumer>
</div>
) ),
edit: edit,

save( { attributes } ) {
return <RawHTML>{ attributes.content }</RawHTML>;
Expand Down
15 changes: 10 additions & 5 deletions packages/components/src/sandbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ class Sandbox extends Component {
// the iframe root and interfere with our mechanism for
// determining the unconstrained page bounds.
function removeViewportStyles( ruleOrNode ) {
[ 'width', 'height', 'minHeight', 'maxHeight' ].forEach( function( style ) {
if ( /^\\d+(vmin|vmax|vh|vw)$/.test( ruleOrNode.style[ style ] ) ) {
ruleOrNode.style[ style ] = '';
}
} );
if( ruleOrNode.style ) {
[ 'width', 'height', 'minHeight', 'maxHeight' ].forEach( function( style ) {
if ( /^\\d+(vmin|vmax|vh|vw)$/.test( ruleOrNode.style[ style ] ) ) {
ruleOrNode.style[ style ] = '';
}
} );
}
}
Array.prototype.forEach.call( document.querySelectorAll( '[style]' ), removeViewportStyles );
Expand Down Expand Up @@ -165,6 +167,9 @@ class Sandbox extends Component {
<head>
<title>{ this.props.title }</title>
<style dangerouslySetInnerHTML={ { __html: style } } />
{ ( this.props.styles && this.props.styles.map(
( rules, i ) => <style key={ i } dangerouslySetInnerHTML={ { __html: rules } } />
) ) }
</head>
<body data-resizable-iframe-connected="data-resizable-iframe-connected" className={ this.props.type }>
<div dangerouslySetInnerHTML={ { __html: this.props.html } } />
Expand Down
14 changes: 4 additions & 10 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { flow, map } from 'lodash';
/**
* WordPress Dependencies
*/
import { compose } from '@wordpress/compose';
import { createElement, Component } from '@wordpress/element';
import { DropZoneProvider, SlotFillProvider } from '@wordpress/components';
import { withDispatch } from '@wordpress/data';
Expand All @@ -15,7 +14,7 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { traverse, wrap, urlRewrite } from '../../editor-styles';
import transformStyles from '../../editor-styles';

class EditorProvider extends Component {
constructor( props ) {
Expand Down Expand Up @@ -51,14 +50,9 @@ class EditorProvider extends Component {
return;
}

map( this.props.settings.styles, ( { css, baseURL } ) => {
const transforms = [
wrap( '.editor-styles-wrapper' ),
];
if ( baseURL ) {
transforms.push( urlRewrite( baseURL ) );
}
const updatedCSS = traverse( css, compose( transforms ) );
const updatedStyles = transformStyles( this.props.settings.styles, '.editor-styles-wrapper' );

map( updatedStyles, ( updatedCSS ) => {
if ( updatedCSS ) {
const node = document.createElement( 'style' );
node.innerHTML = updatedCSS;
Expand Down
45 changes: 42 additions & 3 deletions packages/editor/src/editor-styles/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
export { default as traverse } from './traverse';
export { default as urlRewrite } from './transforms/url-rewrite';
export { default as wrap } from './transforms/wrap';
/**
* External dependencies
*/
import { map } from 'lodash';

/**
* WordPress dependencies
*/
import { compose } from '@wordpress/compose';

/**
* External dependencies
*/
import traverse from './traverse';
import urlRewrite from './transforms/url-rewrite';
import wrap from './transforms/wrap';

/**
* Convert css rules.
*
* @param {Array} styles CSS rules.
* @param {string} wrapperClassName Wrapper Class Name.
* @return {Array} converted rules.
*/
const transformStyles = ( styles, wrapperClassName = '' ) => {
return map( styles, ( { css, baseURL } ) => {
const transforms = [];
if ( wrapperClassName ) {
transforms.push( wrap( wrapperClassName ) );
}
if ( baseURL ) {
transforms.push( urlRewrite( baseURL ) );
}
if ( transforms.length ) {
return traverse( css, compose( transforms ) );
}

return css;
} );
};

export default transformStyles;
1 change: 1 addition & 0 deletions packages/editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ import './hooks';

export * from './components';
export * from './utils';
export { default as transformStyles } from './editor-styles';

0 comments on commit cd11278

Please sign in to comment.