-
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
Closed
niranjan-uma-shankar
wants to merge
48
commits into
WordPress:master
from
niranjan-uma-shankar:master
Closed
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
8cec7fc
eslint fixes and reordering for better readibility and conforming to …
9fc96f8
Minor changes to the title and code comments to change language to sh…
00abaca
Post ID is now retrieved from redux store, instead of from GET params
3646f61
Injects custom css and js that might be needed by iframe
2681b28
Checks shortcode content type and fetches custom css/js (if any)
d7971cb
JS and CSS fixes to enable rendering of shortcode previews
3e5c6b4
Merge remote-tracking branch 'upstream/master'
80800ed
fixes warnings
63549b4
fixes warnings
9997daf
fixes warnings
4095bca
fixes warnings
aa57071
fixes warnings
0e71158
Bug fix : [playlist] shortcode now renders in the preview
4871b73
Fixes jest Snapshot which caused the tests to fail
7f00de6
Merge remote-tracking branch 'upstream/master'
a6d250a
Enhancement: Implements caching of results
niranjan-uma-shankar 3a55c5a
lint fixes
niranjan-uma-shankar 7fdad34
Fix: Don't save in transients if result is empty
niranjan-uma-shankar a486b44
Fix: Handles better empty shortcode output scenario
niranjan-uma-shankar c9f11b5
Minor fix: Translatable function for a string was missing, fixed it
niranjan-uma-shankar 5bf4ea3
Added some null checks
niranjan-uma-shankar 861dd1c
Unit tests for the Shortcode preview rendering REST API.
niranjan-uma-shankar bc9a96a
Added some error messages
niranjan-uma-shankar fc05936
Refactored code & breaking down to components
niranjan-uma-shankar d2143a5
Updated tests to reflect new refactor changes
niranjan-uma-shankar 6c31a5f
Code refactor
niranjan-uma-shankar f6d525f
Adding a comment
niranjan-uma-shankar 6aacd6e
Added encodeURIcomponent() to encode the shortcode content on the fro…
niranjan-uma-shankar 54f2fb3
Refactored code
niranjan-uma-shankar e4bbdb6
Removed these lines - they were added as a fix for an earlier observe…
niranjan-uma-shankar 63cfafd
Merge remote-tracking branch 'upstream/master'
niranjan-uma-shankar 2ce2fef
Drop the focus/setFocus props from block edit functions (#4872)
niranjan-uma-shankar 3797ce8
Drop the focus/setFocus props from block edit functions (#4872)
niranjan-uma-shankar 65efa0b
[embed] shortcode previews are rendered with oembed endpoint
niranjan-uma-shankar 1259bc9
Moved connect HOC to ShortcodePreview component, so that jest tests c…
niranjan-uma-shankar 3b506ed
Avoiding the selector, since it creates an issue with bundle size due…
niranjan-uma-shankar 9230d75
rebase master
niranjan-uma-shankar f837d0a
Variable name changes
niranjan-uma-shankar 8237a66
Refactoring changes
niranjan-uma-shankar 0ae8100
Modifications to the tests in line with the REST API changes
niranjan-uma-shankar d8ed5b2
Merge remote-tracking branch 'upstream/master'
niranjan-uma-shankar 49b2e85
Undo logging changes
niranjan-uma-shankar 4595fb4
Updated schema, and fixed lint
niranjan-uma-shankar 777b304
Reverting API changes to do_shortcode/run_shortcode method
niranjan-uma-shankar 80ae62a
Merge
niranjan-uma-shankar d81a308
Merge
niranjan-uma-shankar 12d83a0
Merge remote-tracking branch 'upstream/master'
niranjan-uma-shankar 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
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 ); |
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,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 ); |
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 |
---|---|---|
|
@@ -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%; | ||
} | ||
|
||
body > div > * { | ||
margin-top: 0 !important; /* has to have !important to override inline styles */ | ||
margin-bottom: 0 !important; | ||
|
@@ -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 } } /> | ||
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> | ||
); | ||
|
Oops, something went wrong.
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.
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.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.
@Lewiscowles1986 Regarding changes done to Sandbox component - have you verified if it doesn't affect the
embed
block? This is because theembed
block uses this component too. Check here.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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
tbf I changed
height: 100%;
tomin-height: 100%;
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.
@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 comment
The reason will be displayed to describe this comment to others. Learn more.
Works in embed block still 😉
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.