-
Notifications
You must be signed in to change notification settings - Fork 138
Enhance admin media UI with dominant color support #1719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Added inline CSS and JavaScript to display dominant colors and transparency in the WordPress admin area for attachment previews. This enhancement allows the media library to utilize dominant color and transparency data, providing visual feedback on image properties. Updated version to 1.1.2 to reflect these changes.
|
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 Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @[email protected]. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Update version numbers and associated comments from 1.1.2 to 1.1.3 for consistency across the plugin files. This change ensures that the codebase reflects the correct version for recent updates or bug fixes included in this release.
Corrected inconsistent indentation and spacing to improve code readability and maintainability in hooks.php. These changes ensure a cleaner code style without altering the functionality or logic.
Corrected inconsistent indentation and spacing to improve code readability and maintainability in hooks.php. These changes ensure a cleaner code style without altering the functionality or logic.
This commit specifies return types for several functions in the dominant-color-images plugin. The `dominant_color_admin_inline_style` and `dominant_color_admin_script` functions now explicitly return void, and the `dominant_color_prepare_attachment_for_js` function now returns an array. Adding these return types improves code clarity and aligns with current PHP best practices.
The @return void annotations were removed from the PHPDoc comments in two functions within the hooks.php file. These annotations were redundant, as the functions' return types are already declared with "void" in the function signatures. This cleanup enhances the readability and maintains consistency across the codebase.
Updated the function docblock type hints for arrays to use generics, enhancing code clarity and consistency. This change does not affect functionality but improves the readability and maintenance of the codebase.
Updated the function docblock type hints for arrays to use generics, enhancing code clarity and consistency. This change does not affect functionality but improves the readability and maintenance of the codebase.
joemcgill
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @pbearne. This looks mostly good to go for me. Left a few suggestions
| $response['hasTransparencyClass'] = ''; | ||
| if ( isset( $meta['has_transparency'] ) ) { | ||
| $response['hasTransparency'] = $meta['has_transparency']; | ||
| $response['hasTransparencyClass'] = $meta['has_transparency'] ? 'has-transparency' : 'not-transparent'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be inferred on the front end and not returned as part of the response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I belive we need this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine to create the class on the server and pass it as part of the response, but just to clarify what I meant, in the script that gets output in dominant_color_admin_script(), you could do something like this (untested):
<script>
(function() {
var s = jQuery( '#tmpl-attachment' )
var transparencyClassName = 'true' === data.hasTransparency ? 'has-transparency' : 'not-transparent';
var n = s[0].innerText.replace( '{{ data.orientation }}"', '{{ data.orientation }} {{ transparencyClassName }}" data-dominant-color="{{ data.dominantColor }}" data-has-transparency="{{ data.hasTransparency }}" style="--dominant-color: #{{ data.dominantColor }};"')
s.replaceWith( '<script type="text/html" id="tmpl-attachment">' + n );
}());
</script>Also, it looks like only the has-transparency class is ever referenced by the custom CSS that gets added here, so if could be simplified to:
var transparencyClassName = 'true' === data.hasTransparency ? 'has-transparency' : '';
or if this remains in PHP:
$response['hasTransparencyClass'] = $meta['has_transparency'] ? 'has-transparency' : '';There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made the change to use JS for the class
Updated the dominant color and transparency properties in hooks.php to use proper sanitization functions. This change helps prevent potential issues with unsanitized data affecting the media library display. Also, adjusted version references for an upcoming release.
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Reorganize the logic for assigning the transparency class name by moving it from the server-side to the client-side script. This change improves code clarity by handling conditional class name assignments directly within the client-side context, thereby simplifying the server-side response handling.
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
|
I'm seeing the dominant color on the frontend: <img data-dominant-color="070606" data-has-transparency="false" style="--dominant-color: #070606;" fetchpriority="high" decoding="async" width="1024" height="403" src="http://localhost:8888/wp-content/uploads/2024/12/Screenshot-2024-12-09-12.43.02-1024x403.png" alt="" class="wp-image-402 not-transparent" srcset="http://localhost:8888/wp-content/uploads/2024/12/Screenshot-2024-12-09-12.43.02-1024x403.png 1024w, http://localhost:8888/wp-content/uploads/2024/12/Screenshot-2024-12-09-12.43.02-300x118.png 300w, http://localhost:8888/wp-content/uploads/2024/12/Screenshot-2024-12-09-12.43.02-768x302.png 768w, http://localhost:8888/wp-content/uploads/2024/12/Screenshot-2024-12-09-12.43.02.png 1294w" sizes="(max-width: 1024px) 100vw, 1024px">But on the Media screen and in the Media modal, I'm not seeing anything added: <div class="not-transparent attachment-preview js--select-attachment type-image subtype-png landscape" data-dominant-color="" data-has-transparency="false" style="--dominant-color: #;">Note the dominant color is missing from the underlying data. In looking at the Ajax response, I see: {
"id": 402,
"title": "Screenshot 2024-12-09 12.43.02",
"filename": "Screenshot-2024-12-09-12.43.02.png",
"url": "http:\/\/localhost:8888\/wp-content\/uploads\/2024\/12\/Screenshot-2024-12-09-12.43.02.png",
"link": "http:\/\/localhost:8888\/screenshot-2024-12-09-12-43-02\/",
"alt": "",
"author": "1",
"description": "",
"caption": "",
"name": "screenshot-2024-12-09-12-43-02",
"status": "inherit",
"uploadedTo": 0,
"date": 1734116876000,
"modified": 1734116876000,
"menuOrder": 0,
"mime": "image\/png",
"type": "image",
"subtype": "png",
"icon": "http:\/\/localhost:8888\/wp-includes\/images\/media\/default.svg",
"dateFormatted": "December 13, 2024",
"nonces": {
"update": "6ec61bcfb7",
"delete": "92edbf45a7",
"edit": "dccdfde8bb"
},
"editLink": "http:\/\/localhost:8888\/wp-admin\/post.php?post=402&action=edit",
"meta": false,
"authorName": "admin",
"authorLink": "http:\/\/localhost:8888\/wp-admin\/profile.php",
"filesizeInBytes": 92142,
"filesizeHumanReadable": "90 KB",
"context": "",
"height": 509,
"width": 1294,
"orientation": "landscape",
"sizes": {
"thumbnail": {
"height": 150,
"width": 150,
"url": "http:\/\/localhost:8888\/wp-content\/uploads\/2024\/12\/Screenshot-2024-12-09-12.43.02-150x150.png",
"orientation": "landscape"
},
"medium": {
"height": 118,
"width": 300,
"url": "http:\/\/localhost:8888\/wp-content\/uploads\/2024\/12\/Screenshot-2024-12-09-12.43.02-300x118.png",
"orientation": "landscape"
},
"large": {
"height": 238,
"width": 604,
"url": "http:\/\/localhost:8888\/wp-content\/uploads\/2024\/12\/Screenshot-2024-12-09-12.43.02-1024x403.png",
"orientation": "landscape"
},
"full": {
"url": "http:\/\/localhost:8888\/wp-content\/uploads\/2024\/12\/Screenshot-2024-12-09-12.43.02.png",
"height": 509,
"width": 1294,
"orientation": "landscape"
}
},
"compat": {
"item": "",
"meta": ""
},
"dominantColor": "",
"hasTransparency": false
} |
|
In looking at the underlying attachment meta, the {
"dominant_color": "070606",
"has_transparency": false
} |
joemcgill
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good to me. Thanks @pbearne!


Added inline CSS and JavaScript to display dominant colors and transparency in the WordPress admin area for attachment previews. This enhancement allows the media library to utilize dominant color and transparency data, providing visual feedback on image properties. Updated version to 1.1.2 to reflect these changes.
Summary
Fixes #
Relevant technical choices