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

Add HTML block with preview. #1391

Merged
merged 7 commits into from
Jun 23, 2017
Merged
Show file tree
Hide file tree
Changes from 6 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
54 changes: 54 additions & 0 deletions blocks/library/code/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,57 @@ div[data-type="core/code"] {
width: 100%;
}
}

// We should extract this to a separate components-toolbar
.components-tab-button {
display: inline-flex;
align-items: flex-end;
margin: 0;
padding: 3px;
background: none;
outline: none;
color: $dark-gray-500;
cursor: pointer;
position: relative;
height: $icon-button-size;
font-family: $default-font;
font-size: $default-font-size;
font-weight: 500;
border: 0;

&.is-active,
&.is-active:hover {
color: $white;
}

&:disabled {
cursor: default;
}

&> span {
border: 1px solid transparent;
padding: 0 6px;
box-sizing: content-box;
height: 28px;
line-height: 28px;
}

&:hover > span,
&:focus > span {
color: $dark-gray-500;
}

&:not(:disabled) {
&.is-active > span,
&:hover > span,
&:focus > span {
border: 1px solid $dark-gray-500;
}
}

&.is-active > span,
&.is-active:hover > span {
background-color: $dark-gray-500;
color: $white;
}
}
87 changes: 87 additions & 0 deletions blocks/library/html/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* External dependencies
*/
import TextareaAutosize from 'react-autosize-textarea';

/**
* WordPress dependencies
*/
import { __ } from 'i18n';
import { Component } from 'element';

/**
* Internal dependencies
*/
import './style.scss';
import { registerBlockType, query } from '../../api';
import BlockControls from '../../block-controls';

const { children } = query;

registerBlockType( 'core/html', {
title: __( 'Custom HTML' ),

icon: 'editor-code',

category: 'formatting',

attributes: {
content: children(),
},

edit: class extends Component {
constructor() {
super( ...arguments );
this.preview = this.preview.bind( this );
this.edit = this.edit.bind( this );
this.state = {
preview: false,
};
}

preview() {
this.setState( { preview: true } );
}

edit() {
this.setState( { preview: false } );
}

render() {
const { preview } = this.state;
const { attributes, setAttributes, focus } = this.props;

return (
<div>
{ focus &&
<BlockControls key="controls">
<ul className="components-toolbar">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use the regular Toolbar component for this? I guess we just need to make the icon optional and drop the fixed width?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It forces icon at the moment. And we'll probably just create a segmented-control out of this in any case.

Copy link
Contributor

@youknowriad youknowriad Jun 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as part of #1256 I updated the Toolbar component to allow this. Edit: I updated the styling only. but seems easy to drop the icon requirement

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be done separately of course

<li>
<button className={ `components-tab-button ${ ! preview ? 'is-active' : '' }` } onClick={ this.edit }>
<span>HTML</span>
</button>
</li>
<li>
<button className={ `components-tab-button ${ preview ? 'is-active' : '' }` } onClick={ this.preview }>
<span>{ __( 'Preview' ) }</span>
</button>
</li>
</ul>
</BlockControls>
}
{ preview
? <div dangerouslySetInnerHTML={ { __html: attributes.content } } />
: <TextareaAutosize
value={ attributes.content }
onChange={ ( event ) => setAttributes( { content: event.target.value } ) }
/>
}
</div>
);
}
},

save( { attributes } ) {
return attributes.content;
},
} );
14 changes: 14 additions & 0 deletions blocks/library/html/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
div[data-type="core/html"] {
textarea {
box-shadow: none;
font-family: $editor-html-font;
font-size: $text-editor-font-size;
color: $dark-gray-800;
border: 1px solid $light-gray-500;
border-radius: 4px;
padding: .8em 1.6em;
margin: 0;
overflow-x: auto;
width: 100%;
}
}
1 change: 1 addition & 0 deletions blocks/library/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import './pullquote';
import './table';
import './preformatted';
import './code';
import './html';
import './freeform';
import './latest-posts';
import './cover-image';
4 changes: 4 additions & 0 deletions blocks/test/fixtures/core-html.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- wp:core/html -->
<h1>Some HTML code</h1>
<marquee>This text will scroll from right to left</marquee>
<!-- /wp:core/html -->
19 changes: 19 additions & 0 deletions blocks/test/fixtures/core-html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[
{
"uid": "_uid_0",
"name": "core/html",
"attributes": {
"content": [
{
"children": "Some HTML code"
"type": "h1"
}
"\n"
{
"children": "This text will scroll from right to left"
"type": "marquee"
}
]
}
}
]
4 changes: 4 additions & 0 deletions blocks/test/fixtures/core-html.serialized.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- wp:core/html -->
<h1>Some HTML code</h1>
<marquee>This text will scroll from right to left</marquee>
<!-- /wp:core/html -->