Skip to content

Commit 52cb97d

Browse files
committed
feat: add variations
1 parent 798fe7c commit 52cb97d

File tree

5 files changed

+397
-60
lines changed

5 files changed

+397
-60
lines changed

includes/gutenberg/feedzy-rss-feeds-loop-block.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function register_block() {
7171
'feedzy-rss-feeds-loop-editor-script',
7272
'feedzyData',
7373
array(
74+
'imagepath' => esc_url( FEEDZY_ABSURL . 'img/' ),
7475
'defaultImage' => esc_url( FEEDZY_ABSURL . 'img/feedzy.svg' ),
7576
'isPro' => feedzy_is_pro(),
7677
)

js/FeedzyLoop/edit.js

Lines changed: 48 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
/* eslint-disable @wordpress/no-unsafe-wp-apis */
12
/**
23
* WordPress dependencies.
34
*/
45
import { __ } from '@wordpress/i18n';
56

6-
import { serialize } from '@wordpress/blocks';
7+
import {
8+
store as blocksStore,
9+
createBlocksFromInnerBlocksTemplate,
10+
serialize,
11+
} from '@wordpress/blocks';
712

813
import {
914
store as blockEditorStore,
15+
__experimentalBlockVariationPicker as BlockVariationPicker,
1016
useBlockProps,
1117
BlockControls,
1218
InnerBlocks,
@@ -21,7 +27,7 @@ import {
2127
ToolbarGroup,
2228
} from '@wordpress/components';
2329

24-
import { useSelect } from '@wordpress/data';
30+
import { useDispatch, useSelect } from '@wordpress/data';
2531

2632
import { useState } from '@wordpress/element';
2733

@@ -30,14 +36,19 @@ import ServerSideRender from '@wordpress/server-side-render';
3036
/**
3137
* Internal dependencies.
3238
*/
39+
import metadata from './block.json';
3340
import Placeholder from './placeholder';
3441
import ConditionsControl from '../Conditions/ConditionsControl';
3542

43+
const { name } = metadata;
44+
3645
const Edit = ({ attributes, setAttributes, clientId }) => {
3746
const blockProps = useBlockProps();
3847

3948
const [isEditing, setIsEditing] = useState(!attributes?.feed?.source);
4049

50+
const { replaceInnerBlocks, selectBlock } = useDispatch(blockEditorStore);
51+
4152
const isSelected = useSelect(
4253
(select) => {
4354
const { isBlockSelected, hasSelectedInnerBlock } =
@@ -60,6 +71,21 @@ const Edit = ({ attributes, setAttributes, clientId }) => {
6071
[clientId]
6172
);
6273

74+
const hasInnerBlocks = useSelect(
75+
(select) => 0 < select(blockEditorStore).getBlocks(clientId).length,
76+
[clientId]
77+
);
78+
79+
const variations = useSelect((select) => {
80+
const { getBlockVariations } = select(blocksStore);
81+
return getBlockVariations(name, 'block');
82+
}, []);
83+
84+
const defaultVariation = useSelect((select) => {
85+
const { getDefaultBlockVariation } = select(blocksStore);
86+
return getDefaultBlockVariation(name, 'block');
87+
}, []);
88+
6389
const onSaveFeed = () => {
6490
setIsEditing(false);
6591
};
@@ -245,63 +271,26 @@ const Edit = ({ attributes, setAttributes, clientId }) => {
245271
</InspectorControls>
246272

247273
<div {...blockProps}>
248-
<InnerBlocks
249-
template={[
250-
[
251-
'core/group',
252-
{
253-
layout: {
254-
type: 'constrained',
255-
},
256-
style: {
257-
spacing: {
258-
padding: {
259-
top: 'var:preset|spacing|30',
260-
bottom: 'var:preset|spacing|30',
261-
left: 'var:preset|spacing|30',
262-
right: 'var:preset|spacing|30',
263-
},
264-
margin: {
265-
top: 'var:preset|spacing|30',
266-
bottom: 'var:preset|spacing|30',
267-
},
268-
},
269-
},
270-
},
271-
[
272-
[
273-
'core/image',
274-
{
275-
url: window.feedzyData.defaultImage,
276-
alt: '{{feedzy_title}}',
277-
href: '{{feedzy_url}}',
278-
},
279-
],
280-
[
281-
'core/paragraph',
282-
{
283-
content:
284-
'<a href="{{feedzy_url}}">{{feedzy_title}}</a>',
285-
},
286-
],
287-
[
288-
'core/paragraph',
289-
{
290-
content: '{{feedzy_meta}}',
291-
fontSize: 'medium',
292-
},
293-
],
294-
[
295-
'core/paragraph',
296-
{
297-
content: '{{feedzy_description}}',
298-
fontSize: 'small',
299-
},
300-
],
301-
],
302-
],
303-
]}
304-
/>
274+
{hasInnerBlocks ? (
275+
<InnerBlocks />
276+
) : (
277+
<BlockVariationPicker
278+
variations={variations}
279+
onSelect={(nextVariation = defaultVariation) => {
280+
if (nextVariation) {
281+
setAttributes(nextVariation.attributes);
282+
replaceInnerBlocks(
283+
clientId,
284+
createBlocksFromInnerBlocksTemplate(
285+
nextVariation.innerBlocks
286+
),
287+
true
288+
);
289+
}
290+
selectBlock(clientId);
291+
}}
292+
/>
293+
)}
305294
</div>
306295
</>
307296
);

js/FeedzyLoop/index.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import { __ } from '@wordpress/i18n';
55

6-
import { registerBlockType } from '@wordpress/blocks';
6+
import { createBlock, registerBlockType } from '@wordpress/blocks';
77

88
import { InnerBlocks } from '@wordpress/block-editor';
99

@@ -14,12 +14,48 @@ import './editor.scss';
1414
import './style.scss';
1515
import './extension';
1616
import metadata from './block.json';
17+
import variations from './variations';
1718
import edit from './edit';
1819

1920
const { name } = metadata;
2021

2122
registerBlockType(name, {
2223
...metadata,
24+
variations,
25+
transforms: {
26+
from: [
27+
{
28+
type: 'block',
29+
blocks: ['core/rss'],
30+
transform: (attributes) => {
31+
const { feedURL } = attributes;
32+
33+
if (feedURL) {
34+
return createBlock(name, {
35+
feed: { type: 'url', source: [feedURL] },
36+
});
37+
}
38+
39+
return createBlock(name);
40+
},
41+
},
42+
{
43+
type: 'block',
44+
blocks: ['feedzy-rss-feeds/feedzy-block'],
45+
transform: (attributes) => {
46+
const { feeds } = attributes;
47+
48+
if (feeds) {
49+
return createBlock(name, {
50+
feed: { type: 'url', source: [feeds] },
51+
});
52+
}
53+
54+
return createBlock(name);
55+
},
56+
},
57+
],
58+
},
2359
edit,
2460
save: () => {
2561
return <InnerBlocks.Content />;

js/FeedzyLoop/style.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.wp-block-feedzy-rss-feeds-loop {
22
display: grid;
3+
gap: 24px;
34

45
@media (min-width: 782px) {
56
@for $i from 2 through 5 {
@@ -18,4 +19,8 @@
1819
}
1920

2021
grid-template-columns: repeat(1, 1fr);
22+
23+
.wp-block-image.is-style-rounded img {
24+
border-radius: 9999px;
25+
}
2126
}

0 commit comments

Comments
 (0)