Skip to content

Commit

Permalink
feat(SLB-313 SLB-308): gutenberg cta block and its graphql schema
Browse files Browse the repository at this point in the history
  • Loading branch information
chindris committed Apr 11, 2024
1 parent 184d6d5 commit ab8d76a
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 10 deletions.
93 changes: 93 additions & 0 deletions packages/drupal/gutenberg_blocks/src/blocks/cta.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import {
// @ts-ignore
__experimentalLinkControl as LinkControl,
RichText,
} from 'wordpress__block-editor';
import { registerBlockType } from 'wordpress__blocks';
import { ToggleControl } from 'wordpress__components';
import { compose, withState } from 'wordpress__compose';

// @ts-ignore
const { t: __ } = Drupal;

// @ts-ignore
const { setPlainTextAttribute } = silverbackGutenbergUtils;

// @ts-ignore
registerBlockType('custom/cta', {
title: 'CTA',
icon: 'admin-links',
category: 'common',
attributes: {
url: {
type: 'string',
},
text: {
type: 'string',
},
// To have an easier integration with entity usage, we also retrieve and
// store the uuid of internal links.
uuid: {
type: 'string',
},
openInNewTab: {
type: 'boolean',
},
},
// @ts-ignore
edit: compose(withState({}))((props) => {
return (
<div>
<RichText
identifier="text"
className={`button`}
tagName="p"
value={props.attributes.text}
allowedFormats={[]}
// @ts-ignore
disableLineBreaks={true}
placeholder={__('Link text')}
keepPlaceholderOnFocus={true}
style={{
cursor: 'text',
}}
onChange={(text: string) => {
setPlainTextAttribute(props, 'text', text);
}}
/>
<LinkControl
value={{
url: props.attributes.url,
openInNewTab: props.attributes.openInNewTab,
}}
settings={{}}
// @ts-ignore
onChange={(link) => {
props.setAttributes({
url: link.url,
uuid: link.id,
});
}}
/>
<ToggleControl
label={__('Open in new tab')}
help={
props.attributes.openInNewTab
? __('Opens in a new tab.')
: __('Opens in the same tab.')
}
checked={props.attributes.openInNewTab}
onChange={(openInNewTab) => {
props.setAttributes({
openInNewTab,
});
}}
/>
</div>
);
}),

save: () => {
return null;
},
});
1 change: 1 addition & 0 deletions packages/drupal/gutenberg_blocks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ import './blocks/hero';
import './blocks/content';
import './blocks/heading';
import './blocks/form';
import './blocks/cta';
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ default:
<blockquote class="wp-block-quote"><p>Quote</p><cite>Citation</cite></blockquote>
<!-- /wp:quote -->
<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->
<!-- wp:custom/cta {"url":"/en/drupal","text":"Internal CTA","uuid":"f1f827c9-ed06-4e7d-b89d-a169e70a459c"} /-->
<!-- wp:custom/cta {"url":"https://www.google.com","text":"External CTA","uuid":"https://www.google.com","openInNewTab":true} /-->
<!-- /wp:custom/content -->
format: gutenberg
summary: ''
Expand Down Expand Up @@ -155,9 +155,9 @@ translations:
<blockquote class="wp-block-quote"><p>Quote DE</p><cite>Citation DE</cite></blockquote>
<!-- /wp:quote -->
<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->
<!-- wp:custom/cta {"url":"/en/drupal","text":"Internal CTA DE","uuid":"f1f827c9-ed06-4e7d-b89d-a169e70a459c"} /-->
<!-- wp:custom/cta {"url":"https://www.example.com","text":"External CTA DE","uuid":"https://www.example.com","openInNewTab":true} /-->
<!-- /wp:custom/content -->
format: gutenberg
summary: ''
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ default:
<!-- wp:custom/heading -->
<h2 class="wp-block-custom-heading"></h2>
<!-- /wp:custom/heading -->
<!-- wp:custom/cta /-->
<!-- /wp:custom/content -->
format: gutenberg
summary: ''
12 changes: 11 additions & 1 deletion packages/schema/src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,17 @@ type Hero {
@webformIdToUrl(id: "$")
}

union PageContent @resolveEditorBlockType = BlockMarkup | BlockMedia | BlockForm
union PageContent @resolveEditorBlockType =
BlockMarkup
| BlockMedia
| BlockForm
| BlockCta

type BlockCta @type(id: "custom/cta") {
url: Url @resolveEditorBlockAttribute(key: "url")
text: String @resolveEditorBlockAttribute(key: "text")
openInNewTab: Boolean @resolveEditorBlockAttribute(key: "openInNewTab")
}

type BlockForm @type(id: "custom/form") {
url: Url @resolveEditorBlockAttribute(key: "formId") @webformIdToUrl(id: "$")
Expand Down
27 changes: 24 additions & 3 deletions tests/schema/specs/blocks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ test('Blocks', async () => {
... on BlockForm {
url
}
... on BlockCta {
url
text
openInNewTab
}
}
}
{
Expand Down Expand Up @@ -96,9 +101,19 @@ test('Blocks', async () => {
<h3 class="wp-block-custom-heading">Heading 3</h3>
<blockquote class="wp-block-quote"><p>Quote</p><cite>Citation</cite></blockquote>
<p></p>
",
",
},
{
"__typename": "BlockCta",
"openInNewTab": null,
"text": "Internal CTA",
"url": "/en/drupal",
},
{
"__typename": "BlockCta",
"openInNewTab": true,
"text": "External CTA",
"url": "https://www.google.com",
},
],
"hero": {
Expand Down Expand Up @@ -136,6 +151,12 @@ test('Blocks', async () => {
<h2 class="wp-block-custom-heading"></h2>
",
},
{
"__typename": "BlockCta",
"openInNewTab": null,
"text": null,
"url": null,
},
],
"hero": {
"__typename": "Hero",
Expand Down

0 comments on commit ab8d76a

Please sign in to comment.