From 393db51f023591f9cf7eaf4eb1b07111a222f36f Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Fri, 16 Feb 2024 13:34:44 +0800 Subject: [PATCH] Allow editing of image block alt and title attributes in content only mode (#58998) * Add content only controls for alt and title to image block toolbar * Switch to plain `Dropdown` component to ensure correct semantics inside popover * Iterate on copy and translator comment * Iterate again Co-authored-by: talldan Co-authored-by: glendaviesnz Co-authored-by: kevin940726 Co-authored-by: SaxonF --- packages/block-library/src/image/editor.scss | 5 + packages/block-library/src/image/image.js | 110 +++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/packages/block-library/src/image/editor.scss b/packages/block-library/src/image/editor.scss index ded3768dfa7d38..d3fafcfcab4eca 100644 --- a/packages/block-library/src/image/editor.scss +++ b/packages/block-library/src/image/editor.scss @@ -181,3 +181,8 @@ figure.wp-block-image:not(.wp-block) { padding-right: 0; } } + +.wp-block-image__toolbar_content_textarea { + // Corresponds to the size of the textarea in the block inspector. + width: 250px; +} diff --git a/packages/block-library/src/image/image.js b/packages/block-library/src/image/image.js index 32226085b85a5f..a0d481681ee934 100644 --- a/packages/block-library/src/image/image.js +++ b/packages/block-library/src/image/image.js @@ -10,6 +10,7 @@ import { TextControl, ToolbarButton, ToolbarGroup, + Dropdown, __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, __experimentalUseCustomUnits as useCustomUnits, @@ -30,6 +31,7 @@ import { } from '@wordpress/block-editor'; import { useEffect, useMemo, useState, useRef } from '@wordpress/element'; import { __, _x, sprintf, isRTL } from '@wordpress/i18n'; +import { DOWN } from '@wordpress/keycodes'; import { getFilename } from '@wordpress/url'; import { switchToBlockType } from '@wordpress/blocks'; import { crop, overlayText, upload } from '@wordpress/icons'; @@ -177,6 +179,7 @@ export default function Image( { const [ externalBlob, setExternalBlob ] = useState(); const clientWidth = useClientWidth( containerRef, [ align ] ); const hasNonContentControls = blockEditingMode === 'default'; + const isContentOnlyMode = blockEditingMode === 'contentOnly'; const isResizable = allowResize && hasNonContentControls && @@ -514,6 +517,113 @@ export default function Image( { ) } + { isContentOnlyMode && ( + // Add some extra controls for content attributes when content only mode is active. + // With content only mode active, the inspector is hidden, so users need another way + // to edit these attributes. + + ( + { + if ( ! isOpen && event.keyCode === DOWN ) { + event.preventDefault(); + onToggle(); + } + } } + > + { _x( + 'Alt', + 'Alternative text for an image. Block toolbar label, a low character count is preferred.' + ) } + + ) } + renderContent={ () => ( + + { __( + 'Connected to a custom field' + ) } + + ) : ( + <> + + { __( + 'Describe the purpose of the image.' + ) } + +
+ { __( + 'Leave empty if decorative.' + ) } + + ) + } + __nextHasNoMarginBottom + /> + ) } + /> + ( + { + if ( ! isOpen && event.keyCode === DOWN ) { + event.preventDefault(); + onToggle(); + } + } } + > + { __( 'Title' ) } + + ) } + renderContent={ () => ( + + { __( + 'Connected to a custom field' + ) } + + ) : ( + <> + { __( + 'Describe the role of this image on the page.' + ) } + + { __( + '(Note: many devices and browsers do not display this text.)' + ) } + + + ) + } + /> + ) } + /> +
+ ) }