-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Enhance the shortcode block to support previewing of shortcodes #4710
Changes from 29 commits
c3fd280
8cec7fc
9fc96f8
00abaca
3646f61
2681b28
d7971cb
3e5c6b4
80800ed
63549b4
9997daf
4095bca
aa57071
0e71158
4871b73
7f00de6
a6d250a
3a55c5a
7fdad34
a486b44
c9f11b5
5bf4ea3
861dd1c
bc9a96a
fc05936
d2143a5
6c31a5f
f6d525f
6aacd6e
54f2fb3
e4bbdb6
63cfafd
2ce2fef
3797ce8
65efa0b
1259bc9
3b506ed
9230d75
f837d0a
8237a66
0ae8100
d8ed5b2
49b2e85
4595fb4
777b304
80ae62a
d81a308
12d83a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { withInstanceId, Dashicon } from '@wordpress/components'; | ||
import { Component, compose } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ShortcodePreview from './preview'; | ||
import BlockControls from '../../block-controls'; | ||
import { getCurrentPostId } from '../../../editor/store/selectors'; | ||
import PlainText from '../../plain-text'; | ||
|
||
export class Shortcode extends Component { | ||
constructor() { | ||
super(); | ||
this.state = { | ||
preview: false, | ||
}; | ||
} | ||
|
||
render() { | ||
const { preview } = this.state; | ||
const { instanceId, postId, setAttributes, attributes, focus, setFocus } = this.props; | ||
const inputId = `blocks-shortcode-input-${ instanceId }`; | ||
|
||
const controls = focus && ( | ||
<BlockControls key="controls"> | ||
<div className="components-toolbar"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks very similar to the Tabs in the HTML block, maybe not in this PR but it would be nice to extract those to a reusable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree! |
||
<button | ||
className={ `components-tab-button ${ ! preview ? 'is-active' : '' }` } | ||
onClick={ () => this.setState( { preview: false } ) }> | ||
<span>{ __( 'Shortcode' ) }</span> | ||
</button> | ||
<button | ||
className={ `components-tab-button ${ preview ? 'is-active' : '' }` } | ||
onClick={ () => this.setState( { preview: true } ) }> | ||
<span>{ __( 'Preview' ) }</span> | ||
</button> | ||
</div> | ||
</BlockControls> | ||
); | ||
|
||
if ( ! preview ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we flip this check and put code responsible for the preview in here? if ( preview ) {
return [
controls,
<ShortcodePreview key="preview"
shortcode={ attributes.text }
setFocus={ setFocus }
/>,
];
}
// code continues with the default behavior There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, flipping it is possible. It is cleaner to have the positive condition appear first, than the negative condition, and I was on two minds if I should have the positive condition appear first. However, I went with keeping the negative condition (ie ! preview) first since that shows the UI that appears first when one loads the editor. Happy to change it per your suggestion. |
||
return [ | ||
controls, | ||
<div className="wp-block-shortcode" key="placeholder"> | ||
<label htmlFor={ inputId }> | ||
<Dashicon icon="editor-code" /> | ||
{ __( 'Shortcode' ) } | ||
</label> | ||
<PlainText | ||
id={ inputId } | ||
value={ attributes.text } | ||
placeholder={ __( 'Write shortcode here…' ) } | ||
onChange={ ( text ) => setAttributes( { text } ) } | ||
/> | ||
</div>, | ||
]; | ||
} | ||
|
||
const shortcodeContent = ( !! attributes.text ) ? attributes.text.trim() : ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be expressed also as: const shortcodeContent = ( attributes.text || '' ).trim(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion! Will note this change when I process the next commit. |
||
|
||
if ( ! shortcodeContent.length ) { | ||
return [ | ||
controls, | ||
<div key="empty" className="wp-block-embed is-loading"> | ||
{ __( 'Enter something to preview' ) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid this, I'm wondering if we should disable the preview button entirely when the shortcode is empty. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this suggestion. In case this code would need to stay, I would move it to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree! I will implement this change. |
||
</div>, | ||
]; | ||
} | ||
|
||
return [ | ||
controls, | ||
<ShortcodePreview key="preview" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only place where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great suggestion @gziolo. I will move the connect HOC to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gziolo , this might actually not work out. The postID is actually needed by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should be able to do it using the following code: export default compose( [
applyConnect,
applyWithAPIData,
] )( ShortcodePreview ); where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see it works, awesome 👍 |
||
shortcode={ shortcodeContent } | ||
postId={ postId } | ||
setFocus={ setFocus } | ||
/>, | ||
]; | ||
} | ||
} | ||
|
||
const applyConnect = connect( | ||
( state ) => { | ||
return { | ||
postId: getCurrentPostId( state ), | ||
}; | ||
}, | ||
); | ||
|
||
export default compose( [ | ||
applyConnect, | ||
withInstanceId, | ||
] )( Shortcode ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { withAPIData, Spinner, SandBox } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
function ShortcodePreview( { response, setFocus } ) { | ||
if ( response.isLoading || ! response.data ) { | ||
return ( | ||
<div key="loading" className="wp-block-embed is-loading"> | ||
<Spinner /> | ||
<p>{ __( 'Loading...' ) }</p> | ||
</div> | ||
); | ||
} | ||
|
||
const html = response.data.html + ' ' + response.data.js + ' ' + response.data.style; | ||
return ( | ||
<figure className="wp-block-embed" key="embed"> | ||
<SandBox | ||
html={ html } | ||
title="Preview" | ||
type={ response.data.type } | ||
onFocus={ setFocus } | ||
/> | ||
</figure> | ||
); | ||
} | ||
|
||
const applyWithAPIData = withAPIData( ( props ) => { | ||
const { shortcode, postId } = props; | ||
return { | ||
response: `/gutenberg/v1/shortcodes?shortcode=${ encodeURIComponent( shortcode ) }&postId=${ postId }`, | ||
}; | ||
} ); | ||
|
||
export default applyWithAPIData( ShortcodePreview ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,13 @@ | |
* Internal dependencies | ||
*/ | ||
import { name, settings } from '../'; | ||
import { Shortcode } from '../block'; | ||
import { blockEditRender } from 'blocks/test/helpers'; | ||
|
||
settings.edit = Shortcode; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change seems weird to me but I'm not familiar with these tests enough to understand whether it's legit or not? Can you clarify the intention? (cc @gziolo) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is to avoid having const applyConnect = connect(
( state ) => {
return {
postId: getCurrentPostId( state ),
};
},
); I think it can be coded differently to avoid this kind of workarounds. It seems like most of the blocks shouldn't depend on the external data when they load for the first time. Shortcode isn't an exception here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, I missed this, we should definitely avoid using the selector directly here since it's on another module, maybe we could use the reusable block temporary trick for now Hopefully, we'd agree at some point at merging Editor and Blocks modules to fix all these issues. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
describe( 'core/shortcode', () => { | ||
test( 'block edit matches snapshot', () => { | ||
const wrapper = blockEditRender( name, settings ); | ||
|
||
expect( wrapper ).toMatchSnapshot(); | ||
} ); | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,12 @@ export default class Sandbox extends Component { | |
|
||
function sendResize() { | ||
var clientBoundingRect = document.body.getBoundingClientRect(); | ||
if ( 0 === clientBoundingRect.height ) { | ||
clientBoundingRect.height = document.getElementById( 'content' ).clientHeight; | ||
} | ||
if ( 0 === clientBoundingRect.width ) { | ||
clientBoundingRect.width = document.getElementById( 'content' ).clientWidth; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain a bit why we need these changes? Also, does it have an impact on embed blocks? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In an earlier iteration, I faced an issue with [audio] shortcodes getting zero height (for some weird reason) hence I enforced a height and width, both of which should technically never be zero. This does not have an effect on embed blocks, since all it does is to only ensure that the bounding rectangle's height and width is never zero, and that it takes the values of the main content div's height and width. Having said this, in the current iteration I was unable to replicate the zero height issue, so maybe it makes sense to remove these lines of code altogether. |
||
window.parent.postMessage( { | ||
action: 'resize', | ||
width: clientBoundingRect.width, | ||
|
@@ -141,12 +147,18 @@ export default class Sandbox extends Component { | |
body { | ||
margin: 0; | ||
} | ||
|
||
body.html { | ||
width: 100%; | ||
} | ||
|
||
body.video, | ||
body.video > div, | ||
body.video > div > iframe { | ||
width: 100%; | ||
height: 100%; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it going to harm anyone to apply height: 100% in all cases? I'm using it on my branch based upon your work where everything has There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Lewiscowles1986 Regarding changes done to Sandbox component - have you verified if it doesn't affect the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested it with oEmbeds by typing a youtube URL into the shortcode block. Seemed to work There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tbf I changed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Lewiscowles1986 I was referring to the embed block - not embeds in the shortcode block. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have found a problem with the embed block though. It seems intended to only render the block once without an editor like shortcode block has I can share my commit once I push to github, it's mostly your code. |
||
} | ||
|
||
body > div > * { | ||
margin-top: 0 !important; /* has to have !important to override inline styles */ | ||
margin-bottom: 0 !important; | ||
|
@@ -162,8 +174,9 @@ export default class Sandbox extends Component { | |
<style dangerouslySetInnerHTML={ { __html: style } } /> | ||
</head> | ||
<body data-resizable-iframe-connected="data-resizable-iframe-connected" className={ this.props.type }> | ||
<div dangerouslySetInnerHTML={ { __html: this.props.html } } /> | ||
<div id="content" dangerouslySetInnerHTML={ { __html: this.props.html } } /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need a separate div for the html, js and styles can't we just concat everything when passing the html prop? |
||
<script type="text/javascript" dangerouslySetInnerHTML={ { __html: observeAndResizeJS } } /> | ||
|
||
</body> | ||
</html> | ||
); | ||
|
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.
If you rebase your branch, you'll notice that
focus
should be replaced byisSelected
andsetFocus
has been removed.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.
Thanks for bringing this to my attention. I will rebase against master and make the necessary changes.