Skip to content

Commit 648d9e6

Browse files
authored
Merge pull request #982 from Kit/block-api-v2
Blocks: Upgrade to apiVersion 2
2 parents c708de2 + 9fb089e commit 648d9e6

File tree

9 files changed

+84
-45
lines changed

9 files changed

+84
-45
lines changed

includes/blocks/broadcasts/block.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://schemas.wp.org/trunk/block.json",
3-
"apiVersion": 1,
3+
"apiVersion": 2,
44
"name": "convertkit/broadcasts",
55
"title": "Kit Broadcasts",
66
"category": "kit",

includes/blocks/form-builder-field-custom/block.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://schemas.wp.org/trunk/block.json",
3-
"apiVersion": 1,
3+
"apiVersion": 2,
44
"name": "convertkit/form-builder-field-custom",
55
"title": "Kit Form Builder: Custom Field",
66
"category": "kit",

includes/blocks/form-builder-field-email/block.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://schemas.wp.org/trunk/block.json",
3-
"apiVersion": 1,
3+
"apiVersion": 2,
44
"name": "convertkit/form-builder-field-email",
55
"title": "Kit Form Builder: Email Field",
66
"category": "kit",

includes/blocks/form-builder-field-name/block.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://schemas.wp.org/trunk/block.json",
3-
"apiVersion": 1,
3+
"apiVersion": 2,
44
"name": "convertkit/form-builder-field-name",
55
"title": "Kit Form Builder: Name Field",
66
"category": "kit",

includes/blocks/form-builder/block.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://schemas.wp.org/trunk/block.json",
3-
"apiVersion": 1,
3+
"apiVersion": 2,
44
"name": "convertkit/form-builder",
55
"title": "Kit Form Builder",
66
"category": "kit",

includes/blocks/form/block.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://schemas.wp.org/trunk/block.json",
3-
"apiVersion": 1,
3+
"apiVersion": 2,
44
"name": "convertkit/form",
55
"title": "Kit Form",
66
"category": "kit",

includes/blocks/formtrigger/block.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://schemas.wp.org/trunk/block.json",
3-
"apiVersion": 1,
3+
"apiVersion": 2,
44
"name": "convertkit/formtrigger",
55
"title": "Kit Form Trigger",
66
"category": "kit",

includes/blocks/product/block.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://schemas.wp.org/trunk/block.json",
3-
"apiVersion": 1,
3+
"apiVersion": 2,
44
"name": "convertkit/product",
55
"title": "Kit Product",
66
"category": "kit",

resources/backend/js/gutenberg.js

Lines changed: 76 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ function convertKitGutenbergRegisterBlock(block) {
4141
// Define some constants for the various items we'll use.
4242
const el = element.createElement;
4343
const { registerBlockType } = blocks;
44-
const { InspectorControls, InnerBlocks } = editor;
45-
const { Fragment, useState } = element;
44+
const { InspectorControls, InnerBlocks, useBlockProps } = editor;
45+
const { useState } = element;
4646
const {
4747
Button,
4848
Icon,
@@ -364,14 +364,21 @@ function convertKitGutenbergRegisterBlock(block) {
364364
* @param {Object} props Block properties.
365365
* @return {Object} Block settings sidebar elements.
366366
*/
367-
const editBlock = function (props) {
367+
const EditBlock = function (props) {
368+
const blockProps = useBlockProps();
369+
370+
// Refresh button disabled state on DisplayNoticeWithLink.
371+
// This must be here to avoid React error on hook order change when the user e.g.
372+
// connects their Kit account from within the block itself.
373+
const [buttonDisabled, setButtonDisabled] = useState(false);
374+
368375
// If requesting an example of how this block looks (which is requested
369376
// when the user adds a new block and hovers over this block's icon),
370377
// show the preview image.
371378
if (props.attributes.is_gutenberg_example === true) {
372-
return (
373-
Fragment,
374-
{},
379+
return el(
380+
'div',
381+
blockProps,
375382
el('img', {
376383
src: block.gutenberg_example_image,
377384
})
@@ -381,7 +388,12 @@ function convertKitGutenbergRegisterBlock(block) {
381388
// If no access token has been defined in the Plugin, or no resources exist in Kit
382389
// for this block, show a message in the block to tell the user what to do.
383390
if (!block.has_access_token || !block.has_resources) {
384-
return DisplayNoticeWithLink(props);
391+
return DisplayNoticeWithLink(
392+
props,
393+
blockProps,
394+
buttonDisabled,
395+
setButtonDisabled
396+
);
385397
}
386398

387399
// Build Inspector Control Panels, which will appear in the Sidebar when editing the Block.
@@ -401,7 +413,11 @@ function convertKitGutenbergRegisterBlock(block) {
401413
block,
402414
props
403415
);
404-
return editBlockWithPanelsAndPreview(panels, preview);
416+
return editBlockWithPanelsAndPreview(
417+
panels,
418+
preview,
419+
blockProps
420+
);
405421
}
406422

407423
// If no settings have been defined for this block, render the block with a notice
@@ -416,7 +432,11 @@ function convertKitGutenbergRegisterBlock(block) {
416432
block.name,
417433
block.gutenberg_help_description
418434
);
419-
return editBlockWithPanelsAndPreview(panels, preview);
435+
return editBlockWithPanelsAndPreview(
436+
panels,
437+
preview,
438+
blockProps
439+
);
420440
}
421441

422442
// If no render_callback is defined, render the block.
@@ -443,7 +463,11 @@ function convertKitGutenbergRegisterBlock(block) {
443463
template,
444464
})
445465
);
446-
return editBlockWithPanelsAndPreview(panels, preview);
466+
return editBlockWithPanelsAndPreview(
467+
panels,
468+
preview,
469+
blockProps
470+
);
447471
}
448472

449473
// Use the block's PHP's render() function by calling the ServerSideRender component.
@@ -455,7 +479,7 @@ function convertKitGutenbergRegisterBlock(block) {
455479
// apply styles with i.e. convertkit-block.name.
456480
className: 'convertkit-ssr-' + block.name,
457481
});
458-
return editBlockWithPanelsAndPreview(panels, preview);
482+
return editBlockWithPanelsAndPreview(panels, preview, blockProps);
459483
};
460484

461485
/**
@@ -464,19 +488,20 @@ function convertKitGutenbergRegisterBlock(block) {
464488
*
465489
* @since 3.0.0
466490
*
467-
* @param {Object} panels Block panels.
468-
* @param {Object} preview Block preview.
469-
* @return {Object} Block settings sidebar elements.
491+
* @param {Object} panels Block panels.
492+
* @param {Object} preview Block preview.
493+
* @param {Object} blockProps Block properties.
494+
* @return {Object} Block settings sidebar elements.
470495
*/
471-
const editBlockWithPanelsAndPreview = function (panels, preview) {
472-
return el(
473-
// Sidebar Panel with Fields.
474-
Fragment,
475-
{},
496+
const editBlockWithPanelsAndPreview = function (
497+
panels,
498+
preview,
499+
blockProps
500+
) {
501+
return el('div', blockProps, [
476502
el(InspectorControls, {}, panels),
477-
// Block Preview.
478-
preview
479-
);
503+
preview,
504+
]);
480505
};
481506

482507
/**
@@ -488,7 +513,10 @@ function convertKitGutenbergRegisterBlock(block) {
488513
*/
489514
const saveBlock = function () {
490515
if (typeof block.gutenberg_template !== 'undefined') {
491-
return el('div', {}, el(InnerBlocks.Content));
516+
// Use useBlockProps.save() to preserve styling classes and attributes
517+
// from block supports (colors, typography, spacing, etc.)
518+
const blockProps = useBlockProps.save();
519+
return el('div', blockProps, el(InnerBlocks.Content));
492520
}
493521

494522
// Deliberate; preview in the editor is determined by the return statement in `edit` above.
@@ -504,13 +532,18 @@ function convertKitGutenbergRegisterBlock(block) {
504532
*
505533
* @since 2.2.5
506534
*
507-
* @param {Object} props Block properties.
508-
* @return {Object} Notice.
535+
* @param {Object} props Block properties.
536+
* @param {Object} blockProps Block properties.
537+
* @param {boolean} buttonDisabled Whether the refresh button is disabled (true) or enabled (false)
538+
* @param {Function} setButtonDisabled Function to enable or disable the refresh button.
539+
* @return {Object} Notice.
509540
*/
510-
const DisplayNoticeWithLink = function (props) {
511-
// useState to toggle the refresh button's disabled state.
512-
const [buttonDisabled, setButtonDisabled] = useState(false);
513-
541+
const DisplayNoticeWithLink = function (
542+
props,
543+
blockProps,
544+
buttonDisabled,
545+
setButtonDisabled
546+
) {
514547
// Holds the array of elements to display in the notice component.
515548
let elements;
516549

@@ -541,13 +574,19 @@ function convertKitGutenbergRegisterBlock(block) {
541574
// Return the element.
542575
return el(
543576
'div',
544-
{
545-
// convertkit-no-content class allows resources/backend/css/gutenberg.css
546-
// to apply styling/branding to the block.
547-
className:
548-
'convertkit-' + block.name + ' convertkit-no-content',
549-
},
550-
elements
577+
blockProps,
578+
el(
579+
'div',
580+
{
581+
// convertkit-no-content class allows resources/backend/css/gutenberg.css
582+
// to apply styling/branding to the block.
583+
className:
584+
'convertkit-' +
585+
block.name +
586+
' convertkit-no-content',
587+
},
588+
elements
589+
)
551590
);
552591
};
553592

@@ -851,7 +890,7 @@ function convertKitGutenbergRegisterBlock(block) {
851890
},
852891

853892
// Editor.
854-
edit: editBlock,
893+
edit: EditBlock,
855894

856895
// Output.
857896
save: saveBlock,

0 commit comments

Comments
 (0)