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

Enhance the shortcode block to support previewing of shortcodes #4710

Closed
Closed
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
c3fd280
Work in progress fixes for ticket #1054
Jan 24, 2018
8cec7fc
eslint fixes and reordering for better readibility and conforming to …
Jan 25, 2018
9fc96f8
Minor changes to the title and code comments to change language to sh…
Jan 25, 2018
00abaca
Post ID is now retrieved from redux store, instead of from GET params
Jan 25, 2018
3646f61
Injects custom css and js that might be needed by iframe
Jan 27, 2018
2681b28
Checks shortcode content type and fetches custom css/js (if any)
Jan 27, 2018
d7971cb
JS and CSS fixes to enable rendering of shortcode previews
Jan 27, 2018
3e5c6b4
Merge remote-tracking branch 'upstream/master'
Jan 27, 2018
80800ed
fixes warnings
Jan 27, 2018
63549b4
fixes warnings
Jan 27, 2018
9997daf
fixes warnings
Jan 27, 2018
4095bca
fixes warnings
Jan 27, 2018
aa57071
fixes warnings
Jan 27, 2018
0e71158
Bug fix : [playlist] shortcode now renders in the preview
Jan 28, 2018
4871b73
Fixes jest Snapshot which caused the tests to fail
Jan 30, 2018
7f00de6
Merge remote-tracking branch 'upstream/master'
Feb 2, 2018
a6d250a
Enhancement: Implements caching of results
niranjan-uma-shankar Feb 2, 2018
3a55c5a
lint fixes
niranjan-uma-shankar Feb 2, 2018
7fdad34
Fix: Don't save in transients if result is empty
niranjan-uma-shankar Feb 2, 2018
a486b44
Fix: Handles better empty shortcode output scenario
niranjan-uma-shankar Feb 2, 2018
c9f11b5
Minor fix: Translatable function for a string was missing, fixed it
niranjan-uma-shankar Feb 3, 2018
5bf4ea3
Added some null checks
niranjan-uma-shankar Feb 3, 2018
861dd1c
Unit tests for the Shortcode preview rendering REST API.
niranjan-uma-shankar Feb 3, 2018
bc9a96a
Added some error messages
niranjan-uma-shankar Feb 6, 2018
fc05936
Refactored code & breaking down to components
niranjan-uma-shankar Feb 6, 2018
d2143a5
Updated tests to reflect new refactor changes
niranjan-uma-shankar Feb 6, 2018
6c31a5f
Code refactor
niranjan-uma-shankar Feb 6, 2018
f6d525f
Adding a comment
niranjan-uma-shankar Feb 6, 2018
6aacd6e
Added encodeURIcomponent() to encode the shortcode content on the fro…
niranjan-uma-shankar Feb 12, 2018
54f2fb3
Refactored code
niranjan-uma-shankar Feb 15, 2018
e4bbdb6
Removed these lines - they were added as a fix for an earlier observe…
niranjan-uma-shankar Feb 15, 2018
63cfafd
Merge remote-tracking branch 'upstream/master'
niranjan-uma-shankar Feb 15, 2018
2ce2fef
Drop the focus/setFocus props from block edit functions (#4872)
niranjan-uma-shankar Feb 15, 2018
3797ce8
Drop the focus/setFocus props from block edit functions (#4872)
niranjan-uma-shankar Feb 15, 2018
65efa0b
[embed] shortcode previews are rendered with oembed endpoint
niranjan-uma-shankar Feb 17, 2018
1259bc9
Moved connect HOC to ShortcodePreview component, so that jest tests c…
niranjan-uma-shankar Feb 19, 2018
3b506ed
Avoiding the selector, since it creates an issue with bundle size due…
niranjan-uma-shankar Feb 24, 2018
9230d75
rebase master
niranjan-uma-shankar Feb 24, 2018
f837d0a
Variable name changes
niranjan-uma-shankar Feb 25, 2018
8237a66
Refactoring changes
niranjan-uma-shankar Feb 25, 2018
0ae8100
Modifications to the tests in line with the REST API changes
niranjan-uma-shankar Feb 25, 2018
d8ed5b2
Merge remote-tracking branch 'upstream/master'
niranjan-uma-shankar Feb 25, 2018
49b2e85
Undo logging changes
niranjan-uma-shankar Feb 25, 2018
4595fb4
Updated schema, and fixed lint
niranjan-uma-shankar Feb 26, 2018
777b304
Reverting API changes to do_shortcode/run_shortcode method
niranjan-uma-shankar Mar 6, 2018
80ae62a
Merge
niranjan-uma-shankar Apr 15, 2018
d81a308
Merge
niranjan-uma-shankar Apr 15, 2018
12d83a0
Merge remote-tracking branch 'upstream/master'
niranjan-uma-shankar Apr 15, 2018
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
102 changes: 102 additions & 0 deletions blocks/library/shortcode/block.js
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;
Copy link
Contributor

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 by isSelected and setFocus has been removed.

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.

const inputId = `blocks-shortcode-input-${ instanceId }`;

const controls = focus && (
<BlockControls key="controls">
<div 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.

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 Tabs component.

Choose a reason for hiding this comment

The 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 ) {
Copy link
Member

@gziolo gziolo Feb 13, 2018

Choose a reason for hiding this comment

The 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

Copy link
Author

@niranjan-uma-shankar niranjan-uma-shankar Feb 13, 2018

Choose a reason for hiding this comment

The 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() : '';
Copy link
Member

Choose a reason for hiding this comment

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

This can be expressed also as:

const shortcodeContent = ( attributes.text || '' ).trim();

Choose a reason for hiding this comment

The 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' ) }
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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 <ShortcodePreview /> component.

Choose a reason for hiding this comment

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

Agree! I will implement this change.

</div>,
];
}

return [
controls,
<ShortcodePreview key="preview"
Copy link
Member

Choose a reason for hiding this comment

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

This is the only place where postId is necessary. Can we wrap ShortcodePreview with the connect HOC only? This will make sure you don't need to tweak unit tests, as well as it assures you don't need to pass down props to the child component.

Choose a reason for hiding this comment

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

Great suggestion @gziolo. I will move the connect HOC to ShortcodePreview so that we can avoid having to tweak the unit tests.

Choose a reason for hiding this comment

The 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 withAPIData HOC in ShortcodePreview , so it is essential that it is passed down as a prop. Unless I'm missing out on a way by which the withAPIData can still access the postId from connect HOC.

Copy link
Member

Choose a reason for hiding this comment

The 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 applyWithAPIData is what you already have in ShortcodePreview and applyConnect needs to be moved to this file. There is no need to pass down postId from the parent component.

Copy link
Member

Choose a reason for hiding this comment

The 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 );
24 changes: 2 additions & 22 deletions blocks/library/shortcode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { withInstanceId, Dashicon } from '@wordpress/components';

/**
* Internal dependencies
*/
import './editor.scss';
import PlainText from '../../plain-text';
import Shortcode from './block';

export const name = 'core/shortcode';

Expand Down Expand Up @@ -58,26 +57,7 @@ export const settings = {
html: false,
},

edit: withInstanceId(
( { attributes, setAttributes, instanceId } ) => {
const inputId = `blocks-shortcode-input-${ instanceId }`;

return (
<div className="wp-block-shortcode">
<label htmlFor={ inputId }>
<Dashicon icon="editor-code" />
{ __( 'Shortcode' ) }
</label>
<PlainText
id={ inputId }
value={ attributes.text }
placeholder={ __( 'Write shortcode here…' ) }
onChange={ ( text ) => setAttributes( { text } ) }
/>
</div>
);
}
),
edit: Shortcode,

save( { attributes } ) {
return attributes.text;
Expand Down
37 changes: 37 additions & 0 deletions blocks/library/shortcode/preview.js
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 );
4 changes: 2 additions & 2 deletions blocks/library/shortcode/test/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`core/shortcode block edit matches snapshot 1`] = `
class="wp-block-shortcode"
>
<label
for="blocks-shortcode-input-0"
for="blocks-shortcode-input-undefined"
>
<svg
aria-hidden="true"
Expand All @@ -25,7 +25,7 @@ exports[`core/shortcode block edit matches snapshot 1`] = `
</label>
<textarea
class="blocks-plain-text"
id="blocks-shortcode-input-0"
id="blocks-shortcode-input-undefined"
placeholder="Write shortcode here…"
rows="1"
/>
Expand Down
3 changes: 2 additions & 1 deletion blocks/library/shortcode/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
* Internal dependencies
*/
import { name, settings } from '../';
import { Shortcode } from '../block';
import { blockEditRender } from 'blocks/test/helpers';

settings.edit = Shortcode;
Copy link
Contributor

@youknowriad youknowriad Feb 13, 2018

Choose a reason for hiding this comment

The 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)

Copy link
Member

@gziolo gziolo Feb 13, 2018

Choose a reason for hiding this comment

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

I think it is to avoid having edit wrapped with connect:

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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 state.currentPost.id to avoid messing with the bundles.

Hopefully, we'd agree at some point at merging Editor and Blocks modules to fix all these issues.

Choose a reason for hiding this comment

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

As rightly mentioned by @gziolo, this change was done because the jest tests avoid having a connected component. I've described the issue in detail in this comment .

describe( 'core/shortcode', () => {
test( 'block edit matches snapshot', () => {
const wrapper = blockEditRender( name, settings );

expect( wrapper ).toMatchSnapshot();
} );
} );
15 changes: 14 additions & 1 deletion components/sandbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Choose a reason for hiding this comment

The 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,
Expand Down Expand Up @@ -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%;
Copy link
Contributor

Choose a reason for hiding this comment

The 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 {"type":"html"}. It works for videos & audio & I've not had any issues.

Copy link
Author

@niranjan-uma-shankar niranjan-uma-shankar Feb 24, 2018

Choose a reason for hiding this comment

The 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 embed block? This is because the embed block uses this component too. Check here.

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

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

tbf I changed height: 100%; to min-height: 100%;

Choose a reason for hiding this comment

The 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.

screen shot 2018-02-24 at 11 54 34 pm

Copy link
Contributor

@Lewiscowles1986 Lewiscowles1986 Feb 24, 2018

Choose a reason for hiding this comment

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

Works in embed block still 😉
screenshot capture - 2018-02-24 - 20-24-54
screenshot capture - 2018-02-24 - 20-24-45

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
gutenberg.zip

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;
Expand All @@ -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 } } />
Copy link
Contributor

Choose a reason for hiding this comment

The 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>
);
Expand Down
Loading