-
Notifications
You must be signed in to change notification settings - Fork 29
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
Add wporg/time block #414
Add wporg/time block #414
Conversation
4cf22a5
to
357ef4e
Compare
357ef4e
to
e6b167e
Compare
e6b167e
to
93160a3
Compare
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.
This is registering a block on the server, but the code in index.js
doesn't actually create a block. While register_block_type
works, it would be more correct to register the script a different way. You could try the method I used for showcase, which still uses the block.json
and the generated index.asset.php.
Overall, the references to this being a block are not accurate - the parent folder & block.json are fine because those are necessary for the build process (much like the "style" block we use in the themes). For clarity though it would be best to update those references (like in the docs).
It looks like we never set up php linting on this project, but I see a few issues intime/index.php
. You can run composer run lint mu-plugins/blocks/time
to see them.
mu-plugins/blocks/time/index.php
Outdated
*/ | ||
function time_converter_script() { | ||
?> | ||
<script type="text/javascript" id="time_converter_script"> |
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.
Could you add this on the frontend via a viewScript
?
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 did try that with no success, I'll have another go
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.
Added a viewScript
entry but it isn't automatically enqueued. I've used something similar to the method you used for showcase for this also
mu-plugins/blocks/time/src/index.js
Outdated
tagName: 'a', | ||
className: 'wporg-time', |
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.
This creates a link that doesn't do anything — could you maybe use the time
element instead? Use the PHP to add the datetime
attribute, and then use that to build up the element in JS?
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.
Done
mu-plugins/blocks/time/index.php
Outdated
$new_time_content = $dom->createElement( 'abbr', $time_content ); | ||
$new_time_content->setAttribute( 'class', 'wporg-time-date' ); | ||
$new_time_content->setAttribute( 'title', gmdate( 'c', $parsed_time ) ); |
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.
Do you know if there has been any feedback on the accessibility of this abbr
+ title
approach?
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 don't, I'll ask
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 asked, feedback was that a time
element would be better, and in particular the title
on the abbr
could be bad.
A quick search on make.w.org/core shows that they use the |
7e89b0d
to
75d1963
Compare
|
||
$metadata = get_metadata(); | ||
$time_converter_script_handle = register_block_script_handle( $metadata, 'viewScript', 0 ); | ||
wp_enqueue_script( $time_converter_script_handle, null, null, filemtime( __DIR__ . '/build/convert_times.js' ), true ); |
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've added all the parameters so that it loads in the footer. Not sure of there is a better way.
} | ||
|
||
$metadata = get_metadata(); | ||
$time_converter_script_handle = register_block_script_handle( $metadata, 'viewScript', 0 ); |
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.
Couldn't find a nice way to register this script in init
for better performance, and then only enqueue it here
After my MacOS upgrade to Ventura my php version went back to 8 and I had phpcs errors which blocked me seeing these in VSCode. Fixed now. |
$dom = new \DOMDocument(); | ||
|
||
// Ignore warnings about htlm5 tags. | ||
$dom->loadHTML( $content, LIBXML_NOERROR ); |
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.
Without this there can be many warnings displayed (nav, time, and many other html5 tags aren't supported).
Alternatively we could use regexp or a different dom parser...
Feedback received elsewhere: it would be best to avoid filtering Some possible solutions for this:
|
…enqueuing scripts" This reverts commit a5244fd.
90b0196
to
11329c5
Compare
Alternative solution using client side parsing in #415 |
Closing in favour of #415 |
Closes #413
This PR reimplements the existing [time] shortcode as a Gutenberg format.
The block uses the Format API so that the time can be applied to a string within a parent block, eg. a paragraph.
Notable changes from the existing shortcode implementation:
the_content
rather than the shortcode contentparse_time
) is separated from the dom manipulation (transform_time_blocks
)time_converter_script
no longer has a jQuery dependencyScreenshots
Testing
Time
using the option in the rich text toolbar