| 1 |
import { registerBlockType } from '@wordpress/blocks' |
| 2 |
import { useDispatch } from '@wordpress/data' |
| 3 |
import { useEffect } from '@wordpress/element' |
| 4 |
import { __, _x, sprintf } from '@wordpress/i18n' |
| 5 |
import { |
| 6 |
Icon, |
| 7 |
gallery, |
| 8 |
postAuthor, |
| 9 |
mapMarker, |
| 10 |
button, |
| 11 |
cover, |
| 12 |
overlayText, |
| 13 |
} from '@wordpress/icons' |
| 14 |
import { brandBlockIcon } from '@library/components/icons' |
| 15 |
import { setModalVisibility } from '@library/util/general' |
| 16 |
import metadata from './block.json' |
| 17 |
|
| 18 |
export const openModal = (source) => setModalVisibility(source, 'open') |
| 19 |
|
| 20 |
registerBlockType(metadata, { |
| 21 |
icon: brandBlockIcon, |
| 22 |
category: 'extendify', |
| 23 |
example: { |
| 24 |
attributes: { |
| 25 |
preview: window.extendifyData.asset_path + '/preview.png', |
| 26 |
}, |
| 27 |
}, |
| 28 |
variations: [ |
| 29 |
{ |
| 30 |
name: 'gallery', |
| 31 |
icon: <Icon icon={gallery} />, |
| 32 |
category: 'extendify', |
| 33 |
attributes: { search: 'gallery' }, |
| 34 |
title: __('Gallery Patterns', 'extendify'), |
| 35 |
description: __('Add gallery patterns and layouts.', 'extendify'), |
| 36 |
keywords: [__('slideshow', 'extendify'), __('images', 'extendify')], |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: 'team', |
| 40 |
icon: <Icon icon={postAuthor} />, |
| 41 |
category: 'extendify', |
| 42 |
attributes: { search: 'team' }, |
| 43 |
title: __('Team Patterns', 'extendify'), |
| 44 |
description: __('Add team patterns and layouts.', 'extendify'), |
| 45 |
keywords: [ |
| 46 |
_x('crew', 'As in team', 'extendify'), |
| 47 |
__('colleagues', 'extendify'), |
| 48 |
__('members', 'extendify'), |
| 49 |
], |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: 'hero', |
| 53 |
icon: <Icon icon={cover} />, |
| 54 |
category: 'extendify', |
| 55 |
attributes: { search: 'hero' }, |
| 56 |
title: _x( |
| 57 |
'Hero Patterns', |
| 58 |
'Hero being a hero/top section of a webpage', |
| 59 |
'extendify', |
| 60 |
), |
| 61 |
description: __('Add hero patterns and layouts.', 'extendify'), |
| 62 |
keywords: [__('heading', 'extendify'), __('headline', 'extendify')], |
| 63 |
}, |
| 64 |
{ |
| 65 |
name: 'text', |
| 66 |
icon: <Icon icon={overlayText} />, |
| 67 |
category: 'extendify', |
| 68 |
attributes: { search: 'text' }, |
| 69 |
title: _x( |
| 70 |
'Text Patterns', |
| 71 |
'Relating to patterns that feature text only', |
| 72 |
'extendify', |
| 73 |
), |
| 74 |
description: __('Add text patterns and layouts.', 'extendify'), |
| 75 |
keywords: [__('simple', 'extendify'), __('paragraph', 'extendify')], |
| 76 |
}, |
| 77 |
{ |
| 78 |
name: 'about', |
| 79 |
icon: <Icon icon={mapMarker} />, |
| 80 |
category: 'extendify', |
| 81 |
attributes: { search: 'about' }, |
| 82 |
title: _x( |
| 83 |
'About Page Patterns', |
| 84 |
'Add patterns relating to an about us page', |
| 85 |
'extendify', |
| 86 |
), |
| 87 |
description: __('About patterns and layouts.', 'extendify'), |
| 88 |
keywords: [__('who we are', 'extendify'), __('team', 'extendify')], |
| 89 |
}, |
| 90 |
{ |
| 91 |
name: 'call-to-action', |
| 92 |
icon: <Icon icon={button} />, |
| 93 |
category: 'extendify', |
| 94 |
attributes: { search: 'call-to-action' }, |
| 95 |
title: __('Call to Action Patterns', 'extendify'), |
| 96 |
description: __( |
| 97 |
'Add call to action patterns and layouts.', |
| 98 |
'extendify', |
| 99 |
), |
| 100 |
keywords: [ |
| 101 |
_x('cta', 'Initialism for call to action', 'extendify'), |
| 102 |
__('callout', 'extendify'), |
| 103 |
__('buttons', 'extendify'), |
| 104 |
], |
| 105 |
}, |
| 106 |
], |
| 107 |
edit: function Edit({ clientId, attributes }) { |
| 108 |
const { removeBlock } = useDispatch('core/block-editor') |
| 109 |
useEffect(() => { |
| 110 |
if (attributes.preview) { |
| 111 |
return |
| 112 |
} |
| 113 |
if (attributes.search) { |
| 114 |
addTermToSearchParams(attributes.search) |
| 115 |
} |
| 116 |
openModal('library-block') |
| 117 |
removeBlock(clientId) |
| 118 |
}, [clientId, attributes, removeBlock]) |
| 119 |
return ( |
| 120 |
<img |
| 121 |
style={{ display: 'block', maxWidth: '100%' }} |
| 122 |
src={attributes.preview} |
| 123 |
alt={sprintf( |
| 124 |
__('%s Pattern Library', 'extendify'), |
| 125 |
'Extendify', |
| 126 |
)} |
| 127 |
/> |
| 128 |
) |
| 129 |
}, |
| 130 |
}) |
| 131 |
|
| 132 |
const addTermToSearchParams = (term) => { |
| 133 |
const params = new URLSearchParams(window.location.search) |
| 134 |
params.append('ext-patternType', term) |
| 135 |
window.history.replaceState( |
| 136 |
null, |
| 137 |
null, |
| 138 |
window.location.pathname + '?' + params.toString(), |
| 139 |
) |
| 140 |
} |
| 141 |
|