-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e0f1d14
Add HTML block with preview.
mtias 6df0eb4
Polish the controls a bit.
jasmussen b533453
Save raw content.
mtias 9d4914e
Apply is-active class to buttons.
mtias 98472d1
Add fixtures.
mtias 5809480
Copy update and add translations.
mtias e791466
Fix JSON in test
ellatrix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> | ||
<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; | ||
}, | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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%; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 requirementThere was a problem hiding this comment.
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