bobbingwide / oik-bob-bing-wide

More lazy smart shortcodes
https://www.oik-plugins.com/oik-plugins/oik-bob-bing-wide-more-lazy-smart-shortcodes/
GNU General Public License v2.0
2 stars 0 forks source link

Update blocks to build using wp-scripts and register with block.json - starting with oik-bw/wp #42

Closed bobbingwide closed 2 years ago

bobbingwide commented 3 years ago

I want to be able to style the output of the "oik-bbw/wp" block using the global styles solution.

The blocks in oik-bob-bing-wide are currently:

I believe that in order to be able to allow the blocks to support typography, colours and so on, that I need to change them:

Requirements

Proposed solution

I know that the sb-post-edit-block allows this customisation, so I'll aim to build the blocks using the same method.

If the solution doesn't work then I'll be mystified, but at least I'll be better prepared for the future.

bobbingwide commented 3 years ago

Before starting this work I decided to commit the changes I'd made. I rebuilt the code for a production build and was surprised to find that the WordPress block now supported setting the font size. It worked in the editor but the SSR logic couldn't handle the fontSize attribute.

<!-- wp:oik-bbw/wp {"fontSize":"extra-small"} /-->

Before I make the other changes, I'm going update the server logic to support the additional attributes that could be expected were an attempt be made to override the block's original logic. This will get me one step further with the Written theme, where the font size needs to be smaller when the block is used in the sidebar.

bobbingwide commented 3 years ago

I updated the server logic. It took a bit of time finding the right values for block.json and even though I added the supports section I still needed to add each of the attributes being passed by the different controls. If an attribute wasn't defined the block would fail with Error loading block: Invalid parameter(s): attributes eg

{"code":"rest_invalid_param","message":"Invalid parameter(s): attributes","data":{"status":400,"params":{"attributes":"fontSize is not a valid property of Object."},"details":{"attributes":{"code":"rest_additional_properties_forbidden","message":"fontSize is not a valid property of Object.","data":null}}}}

So far I've had to add className, backgroundColor, style and now fontSize. Is it never ending?

bobbingwide commented 3 years ago

The block's now behaving like the sb-post-edit-block behaved when it didn't have any toolbar icons. image You can't select it easily.

bobbingwide commented 3 years ago

And style's an object, not a string.

{"code":"rest_invalid_param","message":"Invalid parameter(s): attributes","data":{"status":400,"params":{"attributes":"[style] is not of type string."},"details":{"attributes":{"code":"rest_invalid_type","message":"[style] is not of type string.","data":{"param":"[style]"}}}}}

bobbingwide commented 3 years ago

Another problem that I had was with the CSS associated with the fontSize attribute.

The values were being set at the block level.

<div style="background-color: #dbddc0; font-size: 13px;" class="has-background is-style-fancy wp-block-oik-bbw-wp">
<p>WordPress version: 5.8-beta4</p>
<p>Gutenberg version: 11.1.0.20210702</p>
</div>

The fontSize defined against the <div> was being overridden by the default font size for the paragraph, which wasn't what I wanted.

My initial solution was to change the tags used to <span>s with a separating <br />.

<div style="background-color: #dbddc0; font-size: 13px;" class="has-background is-style-fancy wp-block-oik-bbw-wp">
<span class="label">WordPress version: </span>
<span class="value">5.8-beta4</span>
<br>
<span class="label">Gutenberg version: </span>
<span class="value">11.1.0.20210702</span>
</div>
bobbingwide commented 3 years ago

I've implemented block.json for oik-bbw/wp in the blocks/oik-wp folder, alongside the source files.

This may cause problems in the future:

  1. Will the files be included in the plugin's .zip file?
  2. Will the files be accessible to i18n routines?

Notes:

bobbingwide commented 3 years ago

The oik-bob-bing-wide plugin was originally built using webpack.

When used in WordPress 5.8 the Widget block editor produces a "doing it wrong" message due to the enqueueing of wp-editor. It seems the best solution is to rework the code to build it using wp-scripts which will enable use of

import ServerSideRender from '@wordpress/server-side-render';

See bobbingwide/bobbingwide#30 (comment)

Requirements

Proposed solution

Process for updating node_modules

  1. Remove the existing node_modules folder
  2. npm install
  3. npm install @wordpress/scripts --save-dev

See https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/

bobbingwide commented 3 years ago

While refactoring the blocks I came across the following message for a number of the blocks.

The block "oik-bbw/search" must have a title.

This was when I'd changed the JavaScript to register the block using just the block name and not the metadata from block.json but when the block wasn't being registered on the server.

The solution is to register the block from block.json on the server.

bobbingwide commented 3 years ago

While updating the oik-bbw/dashicon block I found a method that enabled me to use the Typography controls to set the font size. I also found it fairly easy to set the foreground colour, but not the background colour. The background colour applies to the whole <div> not just the <span> for the dashicon. image

I also discovered, by reading the documentation, that in the save method you need to call useBlockProps.save().

save: props => {
    const blockProps = useBlockProps.save();
            return(
        <div {...blockProps} >
                 <Dashicon icon={props.attributes.dashicon} />
        </div>

            );

I may need to use this technique for some other blocks. See https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/

bobbingwide commented 3 years ago

2. Will the files be accessible to i18n routines?

As determined when updating oik for Internationalization and Localization, the block.json files in the src folder can be processed by makepot with the following changes to the build command in package.json.

From:

"makepot": "wp i18n make-pot . languages/oik-bob-bing-wide.pot --exclude=node_modules,vendor,src",

To:

"makepot": "wp i18n make-pot . languages/oik-bob-bing-wide.pot --ignore-domain --domain=oik-bob-bing-wide --exclude=node_modules,vendor,src/*.js,tests",

Other changes are needed in the code to:

bobbingwide commented 3 years ago

After updating the makepot command it failed with an out of memory fatal error.

> oik-bob-bing-wide@2.0.0 makepot C:\apache\htdocs\wordpress\wp-content\plugins\oik-bob-bing-wide
> wp i18n make-pot . languages/oik-bob-bing-wide.pot --ignore-domain --domain=oik-bob-bing-wide --exclude=node_modules,vendor,src/*.js,tests

Plugin file detected.

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 20480 bytes) in phar://C:/d_drive/dos/wp-cli.phar/vendor/mck89/peast/lib/Peast/Syntax/CommentsRegistry.php on line 172

Increasing the memory did not resolve the problem. It took longer to fail, in a different line of code

Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 4096 bytes) in phar://C:/d_drive/dos/wp-cli.phar/vendor/mck89/peast/lib/Peast/Syntax/ParserAbstract.php on line 235

Explanation

The problem occurred when parsing the files in shortcodes/jquery: vis.js and vis-public.js Excluding these enabled the process to run to completion, with 256M memory

> oik-bob-bing-wide@2.0.0 makepot C:\apache\htdocs\wordpress\wp-content\plugins\oik-bob-bing-wide
> wp i18n make-pot . languages/oik-bob-bing-wide.pot --domain=oik-bob-bing-wide --exclude=node_modules,vendor,src/*.js,tests,shortcodes/jquery

Plugin file detected.
Success: POT file successfully generated!
bobbingwide commented 2 years ago

wp-scripts is now v22.1.0. While refactoring the Dashicon block in issue #46, to be delivered in oik-bob-bing-wide v2.2.1, I've finally discovered how to build each block as a separately deliverable unit.

I want to revisit the method I'd used to deliver multiple internationalized blocks in build/index.js and deliver each block in its own package. The terminology used for this in wp-scripts is entry point.

The official documentation at https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/#build states

The fallback entry point is src/index.js (other supported extensions: .jsx, .ts, and .tsx) in case there is no block.json file found. The output generated will be written to build/index.js.

The documentation shows a custom build where the entry points and custom --output-path are specified on the wp-scripts command line. I haven't worked out how to use this. Instead, looking at the code generated by Ryan Welcher's https://github.com/ryanwelcher/create-block-multple-blocks-template I learnt what I needed to write in webpack.config.js.

This has been a long journey!

Requirements

Proposed solution

Each block's block.json file will need updating to define editorScript, editorStyle and style, where required, replacing index with the block name, excluding the prefix. eg for oik-bbw/csv it's csv.

bobbingwide commented 2 years ago

Delivered in v2.2.1. Closing therefore.