-
Notifications
You must be signed in to change notification settings - Fork 798
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
Gutenberg Block for Tweet Shortcode? #7597
Changes from 1 commit
728f07a
f3c43aa
b9da75c
1fbb0d4
6817940
2e98277
7b3729b
eb7499e
3d3cb22
9b2f478
9b22ae0
6fbc6c2
fb52f65
2b8a30a
8d9f1f4
0b5476f
baa38bc
ba90d71
32a0ea5
1f52720
ebe3953
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 |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
*/ | ||
|
||
add_shortcode( 'tweet', array( 'Jetpack_Tweet', 'jetpack_tweet_shortcode' ) ); | ||
add_action( 'enqueue_block_editor_assets', array( 'Jetpack_Tweet', 'enqueue_block_editor_assets' ) ); | ||
|
||
class Jetpack_Tweet { | ||
|
||
|
@@ -142,4 +143,61 @@ static public function jetpack_tweet_shortcode_script() { | |
} | ||
} | ||
|
||
static public function enqueue_block_editor_assets() { | ||
wp_enqueue_script( 'wp-blocks' ); | ||
wp_enqueue_script( 'wp-i18n' ); | ||
wp_enqueue_script( 'wp-element' ); | ||
wp_enqueue_script( 'shortcode' ); | ||
add_action( 'admin_print_footer_scripts', array( __CLASS__, 'admin_footer' ), 999 ); | ||
} | ||
|
||
static public function admin_footer() { | ||
?> | ||
<script> | ||
( function( wp ) { | ||
var blockStyle = { | ||
backgroundColor: '#900', | ||
color: '#fff', | ||
padding: '20px' | ||
}; | ||
|
||
wp.blocks.registerBlockType( 'jetpack/tweet', { | ||
title: wp.i18n.__( 'Tweet', 'jetpack' ), | ||
icon: 'twitter', | ||
category: 'layout', | ||
|
||
attributes : { | ||
tweet : wp.blocks.query.query( 'input[name=tweet]').value | ||
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. The idea with attribute matchers (or, perhaps soon to be called "sourced" values) is to avoid duplicating data between comment attributes and the actual saved markup of the block. For example, we don't need to serialize an image's URL into the comments blob if we know we can easily grab it from the So what we define in In your case, the saved content is a shortcode, from which you want to extract the I'm supposing you were expecting that defining this here would establish a link between the attribute value and the input from onInput: function( event ) {
props.setAttributes({
tweet: event.target.value
});
} 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 explanation. I wasn't expecting anything really, just playing around and seeing how things connect. Regarding extracting stuff out -- can the attribute matcher / sourced values be a custom function passed in that will return what we care about from the contents somehow? If so, I could just call 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. Currently this is possible by defining https://github.com/WordPress/gutenberg/blob/33542dd/blocks/api/parser.js#L28-L29 However, with increasing pressure to introduce server-capable attributes schemas / parsing, I'm not sure whether this can be relied upon. Some more discussion at: WordPress/gutenberg#1905 |
||
}, | ||
|
||
edit : function( props ) { | ||
return wp.element.createElement( | ||
'input', | ||
{ | ||
name : 'tweet', | ||
type : 'url', | ||
value : props.tweet | ||
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.
Instead, this would be: |
||
}, | ||
null | ||
); | ||
}, | ||
|
||
save : function( props ) { | ||
return wp.shortcode.string({ | ||
tag : 'tweet', | ||
attrs : { | ||
named : {}, | ||
numeric : [ | ||
props.tweet | ||
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. As above, this would be: |
||
] | ||
} | ||
}); | ||
} | ||
|
||
} ); | ||
} )( window.wp ); | ||
</script> | ||
<?php | ||
} | ||
|
||
} // class end |
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.
A text domain solution hasn't yet been implemented for
__
, so the second parameter has no effect 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.
Got it. I was just basing this off prior art in https://github.com/nylen/gutenberg-examples/blob/master/01-basic/block.js#L20
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.
Created an issue at nylen/gutenberg-examples#9