-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLANET-4924 Re-enable the native buttons block
- Loading branch information
1 parent
15f7b66
commit 84c0179
Showing
5 changed files
with
302 additions
and
5 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
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,273 @@ | ||
/** | ||
* This file is copy of core button block edit.js (https://github.com/WordPress/gutenberg/blob/master/packages/block-library/src/button/edit.js), with customize changes. | ||
* Customize changes(PLANET-4924) : | ||
* - Added `p4_button_text_colors` and `p4_button_bg_colors` custom P4 button colors. | ||
* - Remove the BorderPanel control(button borderRadius). | ||
*/ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useCallback, useState } from '@wordpress/element'; | ||
import { compose } from '@wordpress/compose'; | ||
import { | ||
KeyboardShortcuts, | ||
PanelBody, | ||
RangeControl, | ||
TextControl, | ||
ToggleControl, | ||
withFallbackStyles, | ||
ToolbarButton, | ||
ToolbarGroup, | ||
Popover, | ||
} from '@wordpress/components'; | ||
import { | ||
BlockControls, | ||
__experimentalUseGradient, | ||
ContrastChecker, | ||
InspectorControls, | ||
__experimentalPanelColorGradientSettings as PanelColorGradientSettings, | ||
RichText, | ||
withColors, | ||
__experimentalLinkControl as LinkControl, | ||
} from '@wordpress/block-editor'; | ||
import { rawShortcut, displayShortcut } from '@wordpress/keycodes'; | ||
import { link } from '@wordpress/icons'; | ||
|
||
const { getComputedStyle } = window; | ||
|
||
const p4_button_text_colors = [ | ||
{ name: 'dark-shade-black', color: '#1a1a1a' }, | ||
{ name: 'white', color: '#ffffff' }, | ||
]; | ||
|
||
const p4_button_bg_colors = [ | ||
{ name: 'orange', color: '#f36d3a' }, | ||
{ name: 'aquamarine', color: '#68dfde' }, | ||
{ name: 'white', color: '#ffffff' }, | ||
]; | ||
|
||
const applyFallbackStyles = withFallbackStyles( ( node, ownProps ) => { | ||
const { textColor, backgroundColor } = ownProps; | ||
const backgroundColorValue = backgroundColor && backgroundColor.color; | ||
const textColorValue = textColor && textColor.color; | ||
//avoid the use of querySelector if textColor color is known and verify if node is available. | ||
const textNode = | ||
! textColorValue && node | ||
? node.querySelector( '[contenteditable="true"]' ) | ||
: null; | ||
return { | ||
fallbackBackgroundColor: | ||
backgroundColorValue || ! node | ||
? undefined | ||
: getComputedStyle( node ).backgroundColor, | ||
fallbackTextColor: | ||
textColorValue || ! textNode | ||
? undefined | ||
: getComputedStyle( textNode ).color, | ||
}; | ||
} ); | ||
|
||
const NEW_TAB_REL = 'noreferrer noopener'; | ||
|
||
function URLPicker( { | ||
isSelected, | ||
url, | ||
setAttributes, | ||
opensInNewTab, | ||
onToggleOpenInNewTab, | ||
} ) { | ||
const [ isURLPickerOpen, setIsURLPickerOpen ] = useState( false ); | ||
const openLinkControl = () => { | ||
setIsURLPickerOpen( true ); | ||
}; | ||
const linkControl = isURLPickerOpen && ( | ||
<Popover | ||
position="bottom center" | ||
onClose={ () => setIsURLPickerOpen( false ) } | ||
> | ||
<LinkControl | ||
className="wp-block-navigation-link__inline-link-input" | ||
value={ { url, opensInNewTab } } | ||
onChange={ ( { | ||
url: newURL = '', | ||
opensInNewTab: newOpensInNewTab, | ||
} ) => { | ||
setAttributes( { url: newURL } ); | ||
|
||
if ( opensInNewTab !== newOpensInNewTab ) { | ||
onToggleOpenInNewTab( newOpensInNewTab ); | ||
} | ||
} } | ||
/> | ||
</Popover> | ||
); | ||
return ( | ||
<> | ||
<BlockControls> | ||
<ToolbarGroup> | ||
<ToolbarButton | ||
name="link" | ||
icon="admin-links" | ||
title={ __( 'Link' ) } | ||
shortcut={ displayShortcut.primary( 'k' ) } | ||
onClick={ openLinkControl } | ||
/> | ||
</ToolbarGroup> | ||
</BlockControls> | ||
{ isSelected && ( | ||
<KeyboardShortcuts | ||
bindGlobal | ||
shortcuts={ { | ||
[ rawShortcut.primary( 'k' ) ]: openLinkControl, | ||
} } | ||
/> | ||
) } | ||
{ linkControl } | ||
</> | ||
); | ||
} | ||
|
||
function ButtonEdit( { | ||
attributes, | ||
backgroundColor, | ||
textColor, | ||
setBackgroundColor, | ||
setTextColor, | ||
fallbackBackgroundColor, | ||
fallbackTextColor, | ||
setAttributes, | ||
className, | ||
isSelected, | ||
} ) { | ||
const { | ||
borderRadius, | ||
linkTarget, | ||
placeholder, | ||
rel, | ||
text, | ||
url, | ||
} = attributes; | ||
const onSetLinkRel = useCallback( | ||
( value ) => { | ||
setAttributes( { rel: value } ); | ||
}, | ||
[ setAttributes ] | ||
); | ||
|
||
const onToggleOpenInNewTab = useCallback( | ||
( value ) => { | ||
const newLinkTarget = value ? '_blank' : undefined; | ||
|
||
let updatedRel = rel; | ||
if ( newLinkTarget && ! rel ) { | ||
updatedRel = NEW_TAB_REL; | ||
} else if ( ! newLinkTarget && rel === NEW_TAB_REL ) { | ||
updatedRel = undefined; | ||
} | ||
|
||
setAttributes( { | ||
linkTarget: newLinkTarget, | ||
rel: updatedRel, | ||
} ); | ||
}, | ||
[ rel, setAttributes ] | ||
); | ||
const { | ||
gradientClass, | ||
gradientValue, | ||
setGradient, | ||
} = __experimentalUseGradient(); | ||
|
||
return ( | ||
<div className={ className }> | ||
<RichText | ||
placeholder={ placeholder || __( 'Add text…' ) } | ||
value={ text } | ||
onChange={ ( value ) => setAttributes( { text: value } ) } | ||
withoutInteractiveFormatting | ||
className={ classnames( 'wp-block-button__link', { | ||
'has-background': backgroundColor.color || gradientValue, | ||
[ backgroundColor.class ]: | ||
! gradientValue && backgroundColor.class, | ||
'has-text-color': textColor.color, | ||
[ textColor.class ]: textColor.class, | ||
[ gradientClass ]: gradientClass, | ||
'no-border-radius': borderRadius === 0, | ||
} ) } | ||
style={ { | ||
...( ! backgroundColor.color && gradientValue | ||
? { background: gradientValue } | ||
: { backgroundColor: backgroundColor.color } ), | ||
color: textColor.color, | ||
borderRadius: borderRadius | ||
? borderRadius + 'px' | ||
: undefined, | ||
} } | ||
/> | ||
<URLPicker | ||
url={ url } | ||
setAttributes={ setAttributes } | ||
isSelected={ isSelected } | ||
opensInNewTab={ linkTarget === '_blank' } | ||
onToggleOpenInNewTab={ onToggleOpenInNewTab } | ||
/> | ||
<InspectorControls> | ||
<PanelColorGradientSettings | ||
title={ __( 'Background & Text Color' ) } | ||
settings={ [ | ||
{ | ||
colors: p4_button_text_colors, | ||
colorValue: textColor.color, | ||
onColorChange: setTextColor, | ||
label: __( 'Text color' ), | ||
}, | ||
{ | ||
colors: p4_button_bg_colors, | ||
colorValue: backgroundColor.color, | ||
onColorChange: setBackgroundColor, | ||
gradientValue, | ||
onGradientChange: setGradient, | ||
label: __( 'Background' ), | ||
}, | ||
] } | ||
> | ||
<ContrastChecker | ||
{ ...{ | ||
// Text is considered large if font size is greater or equal to 18pt or 24px, | ||
// currently that's not the case for button. | ||
isLargeText: false, | ||
textColor: textColor.color, | ||
backgroundColor: backgroundColor.color, | ||
fallbackBackgroundColor, | ||
fallbackTextColor, | ||
} } | ||
/> | ||
</PanelColorGradientSettings> | ||
<PanelBody title={ __( 'Link settings' ) }> | ||
<ToggleControl | ||
label={ __( 'Open in new tab' ) } | ||
onChange={ onToggleOpenInNewTab } | ||
checked={ linkTarget === '_blank' } | ||
/> | ||
<TextControl | ||
label={ __( 'Link rel' ) } | ||
value={ rel || '' } | ||
onChange={ onSetLinkRel } | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
</div> | ||
); | ||
} | ||
|
||
export default compose( [ | ||
withColors( 'backgroundColor', { textColor: 'color' } ), | ||
applyFallbackStyles, | ||
] )( ButtonEdit ); |
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