From 912b7d03348a0f2d747102177492be5c74eca72a Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Wed, 10 Feb 2021 16:40:45 -1000 Subject: [PATCH] Add image block duotone --- lib/blocks.php | 2 +- packages/block-library/src/image/block.json | 3 ++ packages/block-library/src/image/edit.js | 2 + packages/block-library/src/image/image.js | 27 +++++++++++++ packages/block-library/src/image/index.php | 45 +++++++++++++++++++++ packages/block-library/src/image/save.js | 2 + 6 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 packages/block-library/src/image/index.php diff --git a/lib/blocks.php b/lib/blocks.php index 8fbc900d7ecb19..c7ec3e3c04d52b 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -26,7 +26,6 @@ function gutenberg_reregister_core_block_types() { 'group', 'heading', 'html', - 'image', 'list', 'media-text', 'missing', @@ -55,6 +54,7 @@ function gutenberg_reregister_core_block_types() { 'calendar.php' => 'core/calendar', 'categories.php' => 'core/categories', 'cover.php' => 'core/cover', + 'image.php' => 'core/image', 'latest-comments.php' => 'core/latest-comments', 'latest-posts.php' => 'core/latest-posts', 'navigation.php' => 'core/navigation', diff --git a/packages/block-library/src/image/block.json b/packages/block-library/src/image/block.json index 3952230ded6a31..dd6d93f0f43c0d 100644 --- a/packages/block-library/src/image/block.json +++ b/packages/block-library/src/image/block.json @@ -68,6 +68,9 @@ "source": "attribute", "selector": "figure > a", "attribute": "target" + }, + "duotone": { + "type": "object" } }, "supports": { diff --git a/packages/block-library/src/image/edit.js b/packages/block-library/src/image/edit.js index 15477e3e1eaca9..70e6be6279e656 100644 --- a/packages/block-library/src/image/edit.js +++ b/packages/block-library/src/image/edit.js @@ -91,6 +91,7 @@ export function ImageEdit( { width, height, sizeSlug, + duotone, } = attributes; const altRef = useRef(); @@ -275,6 +276,7 @@ export function ImageEdit( { ); const classes = classnames( className, { + [ duotone?.id ]: duotone, 'is-transient': isBlobURL( url ), 'is-resized': !! width || !! height, 'is-focused': isSelected, diff --git a/packages/block-library/src/image/image.js b/packages/block-library/src/image/image.js index 543a5580216523..d7b0b7bb8a36fd 100644 --- a/packages/block-library/src/image/image.js +++ b/packages/block-library/src/image/image.js @@ -29,6 +29,9 @@ import { MediaReplaceFlow, store as blockEditorStore, BlockAlignmentControl, + __experimentalUseEditorFeature as useEditorFeature, + __experimentalDuotoneToolbar as DuotoneToolbar, + __experimentalDuotoneFilter as DuotoneFilter, } from '@wordpress/block-editor'; import { useEffect, useState, useRef } from '@wordpress/element'; import { __, sprintf, isRTL } from '@wordpress/i18n'; @@ -78,6 +81,7 @@ export default function Image( { height, linkTarget, sizeSlug, + duotone, }, setAttributes, isSelected, @@ -270,6 +274,10 @@ export default function Image( { } ); } + function onDuotoneChange( newDuotone ) { + setAttributes( { duotone: newDuotone } ); + } + useEffect( () => { if ( ! isSelected ) { setIsEditingImage( false ); @@ -279,6 +287,10 @@ export default function Image( { const canEditImage = id && naturalWidth && naturalHeight && imageEditing; const allowCrop = ! multiImageSelection && canEditImage && ! isEditingImage; + const duotonePalette = useEditorFeature( 'color.duotone' ); + + const colorPalette = useEditorFeature( 'color.palette' ); + const controls = ( <> @@ -306,6 +318,14 @@ export default function Image( { label={ __( 'Crop' ) } /> ) } + { ! isEditingImage && ( + + ) } { externalBlob && ( { controls } { img } + { duotone && ( + + ) } { ( ! RichText.isEmpty( caption ) || isSelected ) && ( 'render_block_core_image', + ) + ); +} +add_action( 'init', 'register_block_core_image' ); diff --git a/packages/block-library/src/image/save.js b/packages/block-library/src/image/save.js index ccf816c121e909..9e20327117a89f 100644 --- a/packages/block-library/src/image/save.js +++ b/packages/block-library/src/image/save.js @@ -24,11 +24,13 @@ export default function save( { attributes } ) { linkTarget, sizeSlug, title, + duotone, } = attributes; const newRel = isEmpty( rel ) ? undefined : rel; const classes = classnames( { + [ duotone?.id ]: duotone, [ `align${ align }` ]: align, [ `size-${ sizeSlug }` ]: sizeSlug, 'is-resized': width || height,