Skip to content

Format Library: Add Replace button to inline image#70414

Closed
Sukhendu2002 wants to merge 3 commits intoWordPress:trunkfrom
Sukhendu2002:fix/inline-image-replacement
Closed

Format Library: Add Replace button to inline image#70414
Sukhendu2002 wants to merge 3 commits intoWordPress:trunkfrom
Sukhendu2002:fix/inline-image-replacement

Conversation

@Sukhendu2002
Copy link
Contributor

@Sukhendu2002 Sukhendu2002 commented Jun 13, 2025

What?

Closes: #25909

This PR adds a "Replace" button to the inline image format popover in the Gutenberg editor. Currently, once an inline image is inserted, there's no easy way to replace it with another image without removing it completely and inserting a new one. This enhancement improves the user experience by making image replacement more discoverable and intuitive, similar to how image blocks work.

Testing Instructions

  1. Create a new post or page in the Gutenberg editor
  2. Insert a paragraph block and type some text
  3. Select "Inline Image" from the format options
  4. Select an image from the Media Library and insert it into your text
  5. Click on the inserted inline image to select it
  6. In the popover that appears, verify that there is a "Replace" button
  7. Click the "Replace" button and select a different image from the Media Library

Screenshots or screencast

Screen.Recording.2025-06-13.at.5.01.50.PM.mov

@Sukhendu2002 Sukhendu2002 changed the title Format Library: Add Replace button to inline image format Format Library: Add Replace button to inline image Jun 13, 2025
@Sukhendu2002 Sukhendu2002 marked this pull request as ready for review June 13, 2025 13:03
@Sukhendu2002 Sukhendu2002 requested a review from ellatrix as a code owner June 13, 2025 13:03
@github-actions
Copy link

github-actions bot commented Jun 13, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Sukhendu2002 <sukhendu2002@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: annezazu <annezazu@git.wordpress.org>
Co-authored-by: melchoyce <melchoyce@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@t-hamano t-hamano added Needs Design Feedback Needs general design feedback. [Package] Format library /packages/format-library labels Jun 14, 2025
Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I think this PR is a great enhancement.

One improvement would be to provide a value prop to the MediaUpload component, which can indicate which image is currently selected when the media dialog is opened.

Diff example
diff --git a/packages/format-library/src/image/index.js b/packages/format-library/src/image/index.js
index 24fa29c291..9c14c0daf5 100644
--- a/packages/format-library/src/image/index.js
+++ b/packages/format-library/src/image/index.js
@@ -43,7 +43,7 @@ export const image = {
 };
 
 function InlineUI( { value, onChange, activeObjectAttributes, contentRef } ) {
-       const { style, alt } = activeObjectAttributes;
+       const { style, alt, className } = activeObjectAttributes;
        const width = style?.replace( /\D/g, '' );
 
        const [ editedValues, setEditedValues ] = useState( {
@@ -51,6 +51,8 @@ function InlineUI( { value, onChange, activeObjectAttributes, contentRef } ) {
                alt: alt || '',
        } );
 
+       const imageId = className?.replace( /wp-image-(\d+)/, '$1' );
+
        useEffect( () => {
                setEditedValues( {
                        width: style?.replace( /\D/g, '' ),
@@ -168,6 +170,7 @@ function InlineUI( { value, onChange, activeObjectAttributes, contentRef } ) {
                                                        <MediaUpload
                                                                allowedTypes={ ALLOWED_MEDIA_TYPES }
                                                                onSelect={ handleImageReplace }
+                                                               value={ imageId }
                                                                render={ ( { open } ) => (
                                                                        <Button
                                                                                variant="secondary"

Comment on lines +54 to +59
useEffect( () => {
setEditedValues( {
width: style?.replace( /\D/g, '' ),
alt: alt || '',
} );
}, [ style, alt ] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this useEffect hook?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ensures that after replacing an image, the form fields properly reset to match the new image's properties.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, I think we should update it within handleImageReplace rather than using a hook.

@t-hamano t-hamano added the [Type] Enhancement A suggestion for improvement. label Jun 14, 2025
@t-hamano t-hamano requested a review from Mamaduka June 14, 2025 02:32
@Sukhendu2002 Sukhendu2002 requested a review from t-hamano June 16, 2025 07:14
@Mamaduka
Copy link
Member

Currently, once an inline image is inserted, there's no easy way to replace it with another image without removing it completely and inserting a new one.

This isn't entirely accurate. You can replace the image, but it may be less intuitive. I've even shared a screencast in the issue - #25909 (comment).

@t-hamano
Copy link
Contributor

I didn't know that we could replace the image by clicking the menu item in the More options. Perhaps we should discuss the new UX a bit more.

@Mamaduka
Copy link
Member

I didn't know that we could replace the image by clicking the menu item in the More options. Perhaps we should discuss the new UX a bit more.

100%. I think that if we can make the current option more intuitive and obvious, that could also resolve the issue.

P.S. I think the proposed "Replace" button also breaks the current Modal/Popover design pattern; typically, they render a "Cancel" button alongside the "Submit/Apply" button.

Example

CleanShot 2025-06-27 at 11 21 36

@Mamaduka
Copy link
Member

Mamaduka commented Jul 3, 2025

@Sukhendu2002, see the recent updates in the issue - #25909 (comment). Let us know if you would like to continue working on this enhancement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Design Feedback Needs general design feedback. [Package] Format library /packages/format-library [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inline Images: Can't replace image

3 participants