-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #226 from AmazeeLabs/feat/SLB-306--info-grid
feat(SLB-306): Info Grid
- Loading branch information
Showing
22 changed files
with
988 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
node_modules | ||
.idea/inspectionProfiles | ||
.idea/git_toolbox_prj.xml | ||
build-storybook.log | ||
storybook-static | ||
coverage | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/drupal/gutenberg_blocks/src/blocks/info-grid-item.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { InnerBlocks, InspectorControls } from 'wordpress__block-editor'; | ||
import { registerBlockType } from 'wordpress__blocks'; | ||
import { PanelBody, SelectControl } from 'wordpress__components'; | ||
|
||
import { | ||
iconImagePreview, | ||
Icons, | ||
limitedIconListOption, | ||
} from '../utils/icon-list'; | ||
|
||
// @ts-ignore | ||
const { t: __ } = Drupal; | ||
|
||
registerBlockType('custom/info-grid-item', { | ||
title: __('Info Grid Item'), | ||
icon: 'align-wide', | ||
category: 'layout', | ||
parent: ['custom/info-grid'], | ||
attributes: { | ||
icon: { | ||
type: 'string', | ||
default: '', | ||
}, | ||
}, | ||
edit: (props) => { | ||
const { setAttributes } = props; | ||
const iconPreview = iconImagePreview(props.attributes.icon as string); | ||
|
||
return ( | ||
<> | ||
<InspectorControls> | ||
<PanelBody title={__('Select an icon')}> | ||
<SelectControl | ||
value={props.attributes.icon as string} | ||
options={limitedIconListOption([ | ||
Icons.EMAIL, | ||
Icons.PHONE, | ||
Icons.LIFE_RING, | ||
])} | ||
onChange={(icon: string) => { | ||
setAttributes({ icon }); | ||
}} | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
|
||
<div className={'container-wrapper'}> | ||
<div className={'container-label'}>{__('Info Grid Item')}</div> | ||
<div className={'info-grid-icon'} style={{ maxWidth: '50px' }}> | ||
{iconPreview && ( | ||
<img src={iconPreview} alt={props.attributes.icon as string} /> | ||
)} | ||
</div> | ||
<InnerBlocks | ||
templateLock={false} | ||
allowedBlocks={['custom/heading', 'core/paragraph', 'custom/cta']} | ||
/> | ||
</div> | ||
</> | ||
); | ||
}, | ||
save: () => <InnerBlocks.Content />, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { InnerBlocks } from 'wordpress__block-editor'; | ||
import { registerBlockType } from 'wordpress__blocks'; | ||
import { useSelect } from 'wordpress__data'; | ||
|
||
// @ts-ignore | ||
const { t: __ } = Drupal; | ||
|
||
const MAX_BLOCKS: number = 3; | ||
|
||
registerBlockType('custom/info-grid', { | ||
title: __('Info Grid'), | ||
icon: 'editor-insertmore', | ||
category: 'layout', | ||
attributes: {}, | ||
edit: (props) => { | ||
/* eslint-disable-next-line */ | ||
const { blockCount } = useSelect((select) => ({ | ||
blockCount: select('core/block-editor').getBlockCount(props.clientId), | ||
})); | ||
|
||
return ( | ||
<div className={'container-wrapper'}> | ||
<div className={'container-label'}>{__('Info Grid')}</div> | ||
<InnerBlocks | ||
templateLock={false} | ||
renderAppender={() => { | ||
if (blockCount >= MAX_BLOCKS) { | ||
return null; | ||
} else { | ||
return <InnerBlocks.DefaultBlockAppender />; | ||
} | ||
}} | ||
allowedBlocks={['custom/info-grid-item']} | ||
template={[]} | ||
/> | ||
</div> | ||
); | ||
}, | ||
save: () => <InnerBlocks.Content />, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
packages/drupal/gutenberg_blocks/src/utils/icon-list.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// @ts-ignore | ||
const { t: __ } = Drupal; | ||
|
||
// HOW TO ADD A NEW ICON. | ||
// | ||
// 1. Add a new icon to the Icons enum. | ||
// 2. Add a new icon to the allIconListOptions function. | ||
// 3. Add icon image to the ICON_IMAGE_PATH. | ||
// 4. Add a new icon to the iconImagePreview function. | ||
// | ||
// Your new icon is now available for selection, it will be displayed everywhere all icons are used. | ||
// Or you can use the limitedIconListOption function to limit the available icons in a specific context. | ||
// | ||
// NOTE: search this file for "*" to find the places where you need to add your new icon. | ||
|
||
// *1. Available icons. | ||
export enum Icons { | ||
EMAIL = 'EMAIL', | ||
PHONE = 'PHONE', | ||
LIFE_RING = 'LIFE_RING', | ||
} | ||
|
||
// Single icon type. | ||
export type Icon = { | ||
label: any; | ||
value: string; | ||
}; | ||
|
||
// *3. Icon image path. | ||
const ICON_IMAGE_PATH = '/modules/custom/gutenberg_blocks/images/icons/'; | ||
|
||
/** | ||
* A list of all icons. | ||
* | ||
* @param addDefault | ||
* If you want to add a default option. | ||
* @param defaultLabel | ||
* The default label. | ||
* @param defaultValue | ||
* The default value. | ||
*/ | ||
export const allIconListOptions = ( | ||
addDefault: boolean = true, | ||
defaultLabel: string = __('Select an icon'), | ||
defaultValue: string = '', | ||
): Icon[] => { | ||
// Empty array of icons. | ||
let allIcons: Icon[] = []; | ||
|
||
// *2. The list of all icons. | ||
allIcons = [ | ||
{ | ||
label: __('Email'), | ||
value: Icons.EMAIL, | ||
}, | ||
{ | ||
label: __('Telephone'), | ||
value: Icons.PHONE, | ||
}, | ||
{ | ||
label: __('Life Ring'), | ||
value: Icons.LIFE_RING, | ||
}, | ||
]; | ||
|
||
// If using default add to first place. | ||
if (addDefault) { | ||
allIcons.unshift({ | ||
label: defaultLabel, | ||
value: defaultValue, | ||
}); | ||
} | ||
|
||
return allIcons; | ||
}; | ||
|
||
/** | ||
* Creates an icon image preview. | ||
* | ||
* @param icon | ||
* The icon to be displayed. | ||
*/ | ||
export const iconImagePreview = (icon: string): string => { | ||
let iconFileName = ''; | ||
|
||
switch (icon) { | ||
case Icons.EMAIL: | ||
iconFileName = 'email.svg'; | ||
break; | ||
case Icons.PHONE: | ||
iconFileName = 'phone.svg'; | ||
break; | ||
case Icons.LIFE_RING: | ||
iconFileName = 'life-ring.svg'; | ||
break; | ||
} | ||
|
||
if (!iconFileName) { | ||
return ''; | ||
} | ||
|
||
return ICON_IMAGE_PATH + iconFileName; | ||
}; | ||
|
||
/** | ||
* Limited icon list option. | ||
* | ||
* @param icons | ||
* The list of icons to be displayed. | ||
* @param addDefault | ||
* If you want to add a default option. | ||
* @param defaultLabel | ||
* The default label. | ||
* @param defaultValue | ||
* The default value. | ||
*/ | ||
export const limitedIconListOption = ( | ||
icons: Icons[], | ||
addDefault: boolean = true, | ||
defaultLabel: string = __('Select an icon'), | ||
defaultValue: string = '', | ||
): Icon[] => { | ||
if (addDefault) { | ||
icons.unshift(defaultValue as Icons); | ||
} | ||
|
||
return allIconListOptions(addDefault, defaultLabel, defaultValue).filter( | ||
(icon) => icons.includes(icon.value as Icons), | ||
); | ||
}; |
Oops, something went wrong.