-
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.
- Loading branch information
POK
authored and
POK
committed
Oct 22, 2024
1 parent
546784b
commit 8666e1a
Showing
16 changed files
with
308 additions
and
45 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 2, | ||
"name": "carkeek-blocks/button-extension", | ||
"version": "1.0.12", | ||
"editorScript": "file:./index.js" | ||
} |
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 @@ | ||
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-components', 'wp-compose', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => '322f66c417eb1ec9e5b5'); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-server-side-render'), 'version' => '95b4ea85249aee3c6ad5'); | ||
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-server-side-render'), 'version' => 'fb80865e344304036de3'); |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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,7 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 2, | ||
"name": "carkeek-blocks/button-extension", | ||
"version": "1.0.12", | ||
"editorScript": "file:./index.js" | ||
} |
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,9 @@ | ||
.wp-block-buttons > .wp-block-button.wp-block-button__link--icon { | ||
display: flex; | ||
align-items: center; | ||
padding-top: 0; | ||
padding-bottom: 0; | ||
& .wp-block-button__link { | ||
border: none; | ||
} | ||
} |
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,179 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { useBlockProps, InspectorControls, __experimentalUseColorProps as useColorProps } from '@wordpress/block-editor'; | ||
import { PanelBody, ToggleControl } from '@wordpress/components'; | ||
import { useMergeRefs, useRefEffect } from '@wordpress/compose'; | ||
import { useState, useRef } from '@wordpress/element'; | ||
|
||
import Inspector from '../icon-block/inspector'; | ||
import "./editor.scss"; | ||
|
||
/** | ||
* Add the attributes needed for button icons. | ||
* | ||
* @since 0.1.0 | ||
* @param {Object} settings | ||
*/ | ||
function addAttributes( settings ) { | ||
if ( 'core/button' !== settings.name ) { | ||
return settings; | ||
} | ||
|
||
// Add the block visibility attributes. | ||
const iconAttributes = { | ||
addIcon: { | ||
type: 'boolean', | ||
default: false | ||
}, | ||
icon: { | ||
type: 'string', | ||
}, | ||
variation: { | ||
type: 'string', | ||
}, | ||
iconPositionLeft: { | ||
type: 'boolean', | ||
default: false, | ||
} | ||
}; | ||
|
||
const newSettings = { | ||
...settings, | ||
edit(props) { | ||
const { attributes, setAttributes } = props; | ||
const { icon, variation, iconPositionLeft } = attributes; | ||
const colorProps = useColorProps( attributes ); | ||
let iconDiv = ''; | ||
let iconDivLeft = ''; | ||
let buttonClass = ''; | ||
if ( icon ) { | ||
const iconStyle = 'fa-' + icon + ' fa-' + variation; | ||
if (iconPositionLeft) { | ||
iconDivLeft = <i className={ iconStyle }></i>; | ||
|
||
} else { | ||
iconDiv = <i className={ iconStyle }></i>; | ||
} | ||
|
||
buttonClass = 'wp-block-button__link wp-block-button__link--icon'; | ||
} | ||
const blockProps = useBlockProps( | ||
{ className: buttonClass } | ||
); | ||
return ( | ||
<div | ||
{ ...blockProps } | ||
style = { colorProps.style } | ||
> | ||
|
||
{iconDivLeft} | ||
|
||
{settings.edit(props)} | ||
|
||
{iconDiv} | ||
|
||
</div> | ||
|
||
) | ||
}, | ||
attributes: { | ||
...settings.attributes, | ||
...iconAttributes, | ||
}, | ||
}; | ||
|
||
return newSettings; | ||
} | ||
|
||
addFilter( | ||
'blocks.registerBlockType', | ||
'enable-button-icons/add-attributes', | ||
addAttributes | ||
); | ||
|
||
/** | ||
* Filter the BlockEdit object and add icon inspector controls to button blocks. | ||
* | ||
* @since 0.1.0 | ||
* @param {Object} BlockEdit | ||
*/ | ||
function addInspectorControls( BlockEdit ) { | ||
return ( props ) => { | ||
if ( props.name !== 'core/button' ) { | ||
return <BlockEdit { ...props } />; | ||
} | ||
|
||
const { attributes, setAttributes } = props; | ||
const { addIcon, icon, variation, iconPositionLeft } = attributes; | ||
|
||
return ( | ||
<> | ||
<BlockEdit { ...props } /> | ||
<InspectorControls> | ||
<PanelBody title={ __( 'Icon settings', 'enable-button-icons' ) }> | ||
<ToggleControl | ||
label={ __( 'Add icon', 'enable-button-icons' ) } | ||
checked={ addIcon } | ||
onChange={ ( value ) => setAttributes( { addIcon: value } ) } | ||
/> | ||
{ addIcon && ( | ||
<ToggleControl | ||
label={ __( 'Place Icon Left', 'enable-button-icons' ) } | ||
checked={ iconPositionLeft } | ||
onChange={ ( value ) => setAttributes( { iconPositionLeft: value } ) } | ||
/> | ||
)} | ||
</PanelBody> | ||
</InspectorControls> | ||
|
||
{ addIcon && ( | ||
<Inspector { ...props } /> | ||
)} | ||
</> | ||
); | ||
}; | ||
} | ||
|
||
addFilter( | ||
'editor.BlockEdit', | ||
'enable-button-icons/add-inspector-controls', | ||
addInspectorControls | ||
); | ||
|
||
/** | ||
* Add icon and position classes in the Editor. | ||
* | ||
* @since 0.1.0 | ||
* @param {Object} BlockListBlock | ||
*/ | ||
function addClasses( BlockListBlock ) { | ||
return ( props ) => { | ||
const { name, attributes } = props; | ||
|
||
if ( 'core/button' !== name || ! attributes?.icon ) { | ||
return <BlockListBlock { ...props } />; | ||
} | ||
console.log( 'attributes', attributes ); | ||
const classes = classnames( props?.className, { | ||
[ `fa-${ attributes?.icon }` ]: attributes?.icon, | ||
[ `fa-${ attributes?.variation }` ]: attributes?.variation, | ||
'has-icon-position__left': attributes?.iconPositionLeft, | ||
} ); | ||
|
||
return <BlockListBlock { ...props } className={ classes } />; | ||
}; | ||
} | ||
|
||
// addFilter( | ||
// 'editor.BlockListBlock', | ||
// 'enable-button-icons/add-classes', | ||
// addClasses | ||
// ); |
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
Oops, something went wrong.