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 all 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
74 changes: 74 additions & 0 deletions blocks/library/shortcode/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* WordPress dependencies
*/
import { withInstanceId, Dashicon } from '@wordpress/components';
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import ShortcodePreview from './preview';
import BlockControls from '../../block-controls';
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, isSelected } = this.props;
const inputId = `blocks-shortcode-input-${ instanceId }`;
const shortcodeContent = ( attributes.text || '' ).trim();

const controls = isSelected && (
<BlockControls key="controls">
<div className="components-toolbar">
<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={ () => shortcodeContent.length && this.setState( { preview: true } ) } >
<span>{ __( 'Preview' ) }</span>
</button>
</div>
</BlockControls>
);

if ( preview ) {
return [
controls,
<ShortcodePreview key="preview"
shortcode={ shortcodeContent }
postId={ postId }
/>,
];
}

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>,
];
}
}

export default withInstanceId( Shortcode );
25 changes: 2 additions & 23 deletions blocks/library/shortcode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
*/
import { RawHTML } from '@wordpress/element';
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 @@ -60,27 +59,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="shortcode" />
{ __( 'Shortcode' ) }
</label>
<PlainText
className="input-control"
id={ inputId }
value={ attributes.text }
placeholder={ __( 'Write shortcode here…' ) }
onChange={ ( text ) => setAttributes( { text } ) }
/>
</div>
);
}
),
edit: Shortcode,

save( { attributes } ) {
return <RawHTML>{ attributes.text }</RawHTML>;
Expand Down
53 changes: 53 additions & 0 deletions blocks/library/shortcode/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';

/**
* WordPress dependencies
*/
import { withAPIData, Spinner, SandBox } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { compose } from '@wordpress/element';

function ShortcodePreview( { response } ) {
if ( response.isLoading || ! response.data ) {
return (
<div key="loading" className="wp-block-embed is-loading">
<Spinner />
<p>{ __( 'Loading...' ) }</p>
</div>
);
}

const html = response.data.head_scripts_styles + ' ' + response.data.html + ' ' + response.data.footer_scripts_styles;
return (
<figure className="wp-block-embed" key="embed">
<SandBox
html={ html }
title="Preview"
type={ response.data.type }
/>
</figure>
);
}

const applyConnect = connect(
( state ) => {
return {
postId: state.currentPost.id,
};
},
);

const applyWithAPIData = withAPIData( ( props ) => {
const { shortcode, postId } = props;
return {
response: `/gutenberg/v1/shortcodes?shortcode=${ encodeURIComponent( shortcode ) }&postId=${ postId }`,
};
} );

export default compose( [
applyConnect,
applyWithAPIData,
] )( ShortcodePreview );
1 change: 0 additions & 1 deletion blocks/library/shortcode/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { blockEditRender } from 'blocks/test/helpers';
describe( 'core/shortcode', () => {
test( 'block edit matches snapshot', () => {
const wrapper = blockEditRender( name, settings );

expect( wrapper ).toMatchSnapshot();
} );
} );
9 changes: 8 additions & 1 deletion components/sandbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,18 @@ 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 @@ -157,8 +163,9 @@ 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