Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
cb96657
Fix typo
kebbet Feb 14, 2024
cfffe29
Adds `data-choose` attribute for media modal
kebbet Feb 14, 2024
ff3ef3a
Adds docblocks, removes wrapping function.
kebbet Feb 14, 2024
44ca10d
Adds since tag, follow coding standards I guess.
kebbet Feb 14, 2024
ed9df2c
Store selected elements in vars.
kebbet Feb 14, 2024
54a0565
Updates docblock, move `frame` into initializing function
kebbet Feb 14, 2024
e584698
Reduce calls to .attr(). Remove `classes` var
kebbet Feb 14, 2024
233f89b
Adds space before the opening brackets
kebbet Feb 14, 2024
8ffc136
Reuse existing attribute, rename selector (in PHP)
kebbet Feb 14, 2024
5a97e40
Address review comments from @afercia
kebbet Feb 14, 2024
a3020b6
Merge branch 'WordPress:trunk' into 54370-site-icon-post-review
kebbet Feb 15, 2024
c930802
JS-fixes
kebbet Feb 15, 2024
c695e3c
Focus loss and fixed button ID for triggering modal.
kebbet Feb 15, 2024
564cdbd
Removes unnecessary setting of focus
kebbet Feb 15, 2024
cb268c4
Coding standard fixes.
kebbet Feb 15, 2024
1d696f7
Merge branch 'WordPress:trunk' into 54370-site-icon-post-review
kebbet Feb 16, 2024
edb1546
Updates docblocks, combine var-statements, start jQuery objects with …
kebbet Feb 16, 2024
8269ea3
Merge branch 'WordPress:trunk' into 54370-site-icon-post-review
kebbet Feb 16, 2024
10fe004
Accessibility: adds better alternative text to the images
kebbet Feb 16, 2024
71f353f
CS-issue with to many tabs.
kebbet Feb 16, 2024
18e9c1e
CS-issue
kebbet Feb 16, 2024
142fec6
Missing semicolons.
kebbet Feb 16, 2024
63f3885
Handle alt-attribute on remove
kebbet Feb 16, 2024
d7b30c0
Remove debug log.
kebbet Feb 16, 2024
f540014
Remove IIFE-sections, adjusts indents. Move var to function scope
kebbet Feb 19, 2024
84a7d4b
Merge branch 'WordPress:trunk' into 54370-site-icon-post-review
kebbet Feb 19, 2024
b5c43ed
Temp reset
kebbet Feb 19, 2024
e79e952
Merge branch '54370-site-icon-post-review' of https://github.com/kebb…
kebbet Feb 19, 2024
07c9089
Remove IIFE-sections, adjusts indents. Move var to function scope
kebbet Feb 19, 2024
ebfd54b
Merge branch 'WordPress:trunk' into 54370-site-icon-post-review
kebbet Feb 20, 2024
1f309ac
Adds prefix to alt text for each preview image.
kebbet Feb 20, 2024
869eae7
Address feedback from @afercia
kebbet Feb 20, 2024
94efea9
CS-fixes
kebbet Feb 20, 2024
8d5030a
CS-fixes
kebbet Feb 20, 2024
09641b7
Update src/js/_enqueues/admin/site-icon.js
kebbet Feb 20, 2024
a2bafb7
Address feedback from @aaronjorbin
kebbet Feb 20, 2024
2e177f0
fix
kebbet Feb 20, 2024
17a1618
Merge branch 'trunk' of github.com:WordPress/wordpress-develop into 5…
aaronjorbin Feb 20, 2024
d32112c
Start wiring in new UI
aaronjorbin Feb 20, 2024
399c5ff
Update text for description
aaronjorbin Feb 20, 2024
80fa6d0
Ensure unused dependecies aren't enqueued and translations are proper…
aaronjorbin Feb 21, 2024
cd760fb
Set IDs so JS works
aaronjorbin Feb 21, 2024
348e4aa
Minor tweaks to comments
aaronjorbin Feb 21, 2024
d2700c7
Ensure icons can be hidden
aaronjorbin Feb 21, 2024
b78cd5c
use less generic class names
aaronjorbin Feb 21, 2024
9e0220f
Fix specificity in preview
aaronjorbin Feb 21, 2024
9eacf2c
Use unitless for 0
aaronjorbin Feb 21, 2024
22a00f0
Use unitless for 0
aaronjorbin Feb 21, 2024
0a7c74b
lowercase colors
aaronjorbin Feb 21, 2024
10642d3
lowercase colors
aaronjorbin Feb 21, 2024
2d986fd
lowercase colors
aaronjorbin Feb 21, 2024
d75fb0d
Use unitless for 0
aaronjorbin Feb 21, 2024
138f269
Use unitless for 0
aaronjorbin Feb 21, 2024
952bb3f
lowercase colors
aaronjorbin Feb 21, 2024
4ed3128
Use unitless for 0
aaronjorbin Feb 21, 2024
5ffd471
Use unitless for 0
aaronjorbin Feb 21, 2024
7b67b64
Fix indentation
aaronjorbin Feb 21, 2024
f023335
Merge branch 'trunk' of github.com:WordPress/wordpress-develop into 5…
aaronjorbin Feb 21, 2024
254a288
Update Title in preview as it chagnes
aaronjorbin Feb 22, 2024
57f3d4a
Update background for preview with the new image
aaronjorbin Feb 22, 2024
6564f9b
Remove float which was used in old UI as it is interfering with the p…
aaronjorbin Feb 22, 2024
69416b4
Merge branch 'trunk' of github.com:WordPress/wordpress-develop into 5…
aaronjorbin Feb 22, 2024
b76e500
Restore alt text for browser and remove svg from screen reader
aaronjorbin Feb 22, 2024
16a3a8c
Use <code> tag for size info.
kebbet Feb 22, 2024
91fb8a8
Tidy CSS and add border to preview field.
kebbet Feb 22, 2024
9b68ef5
use prettier to format CSS and JS
aaronjorbin Feb 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
307 changes: 210 additions & 97 deletions src/js/_enqueues/admin/site-icon.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
(function($) {
var frame;

function calculateImageSelectOptions ( attachment ) {
var realWidth = attachment.get( 'width' ),
/**
* Handle the site icon setting in options-general.php.
*
* @since 6.5.0
* @output wp-admin/js/site-icon.js
*/

/* global jQuery, wp */

( function ( $ ) {
var $chooseButton = $( '#choose-from-library-button' ),
$iconPreview = $( '#site-icon-preview' ),
$browserIconPreview = $( '#browser-icon-preview' ),
$appIconPreview = $( '#app-icon-preview' ),
$hiddenDataField = $( '#site_icon_hidden_field' ),
$removeButton = $( '#js-remove-site-icon' ),
frame;

/**
* Calculate image selection options based on the attachment dimensions.
*
* @since 6.5.0
*
* @param {Object} attachment The attachment object representing the image.
* @return {Object} The image selection options.
*/
function calculateImageSelectOptions( attachment ) {
var realWidth = attachment.get( 'width' ),
realHeight = attachment.get( 'height' ),
xInit = 512,
yInit = 512,
ratio = xInit / yInit,
xImg = xInit,
yImg = yInit,
x1, y1, imgSelectOptions;
xImg = xInit,
yImg = yInit,
x1,
y1,
imgSelectOptions;

if ( realWidth / realHeight > ratio ) {
yInit = realHeight;
Expand All @@ -35,104 +60,192 @@
x1: x1,
y1: y1,
x2: xInit + x1,
y2: yInit + y1
y2: yInit + y1,
};

return imgSelectOptions;
}

$( function() {
// Build the choose from library frame.
$( '#choose-from-library-link' ).on( 'click', function() {
var $el = $(this);

// Create the media frame.
frame = wp.media({
button: {
// Set the text of the button.
text: $el.data('update'),
// Don't close, we might need to crop.
close: false
},
states: [
new wp.media.controller.Library({
title: $el.data( 'choose' ),
library: wp.media.query({ type: 'image' }),
date: false,
suggestedWidth: $el.data( 'size' ),
suggestedHeight: $el.data( 'size' )
}),
new wp.media.controller.SiteIconCropper({
control: {
params: {
width: $el.data( 'size' ),
height: $el.data( 'size' )
}
/**
* Initializes the media frame for selecting or cropping an image.
*
* @since 6.5.0
*/
$chooseButton.on( 'click', function () {
var $el = $( this );

// Create the media frame.
frame = wp.media( {
button: {
// Set the text of the button.
text: $el.data( 'update' ),

// Don't close, we might need to crop.
close: false,
},
states: [
new wp.media.controller.Library( {
title: $el.data( 'choose-text' ),
library: wp.media.query( { type: 'image' } ),
date: false,
suggestedWidth: $el.data( 'size' ),
suggestedHeight: $el.data( 'size' ),
} ),
new wp.media.controller.SiteIconCropper( {
control: {
params: {
width: $el.data( 'size' ),
height: $el.data( 'size' ),
},
imgSelectOptions: calculateImageSelectOptions
})
]
});

frame.on( 'cropped', function( attachment) {
$( '#site_icon_hidden_field' ).val(attachment.id);
switchToUpdate(attachment.url);
},
imgSelectOptions: calculateImageSelectOptions,
} ),
],
} );

frame.on( 'cropped', function ( attachment ) {
$hiddenDataField.val( attachment.id );
switchToUpdate( attachment );
frame.close();

// Start over with a frame that is so fresh and so clean clean.
frame = null;
} );

// When an image is selected, run a callback.
frame.on( 'select', function () {
// Grab the selected attachment.
var attachment = frame.state().get( 'selection' ).first();

if (
attachment.attributes.height === $el.data( 'size' ) &&
$el.data( 'size' ) === attachment.attributes.width
) {
switchToUpdate( attachment.attributes );
frame.close();
// Start over with a frame that is so fresh and so clean clean.
frame = null;
});

// When an image is selected, run a callback.
frame.on( 'select', function() {
// Grab the selected attachment.
var attachment = frame.state().get('selection').first();

if ( attachment.attributes.height === $el.data('size') && $el.data('size') === attachment.attributes.width ) {
// Set the value of the hidden input to the attachment id.
$( '#site_icon_hidden_field').val(attachment.id);
switchToUpdate(attachment.attributes.url);
frame.close();
} else {
frame.setState( 'cropper' );
}
});

frame.open();
});
});

function switchToUpdate( url ){
// Set site-icon-img src to the url and remove the hidden class.
$( '#site-icon-preview').find('img').not('.browser-preview').each( function(i, img ){
$(img).attr('src', url );
});
$( '#site-icon-preview' ).removeClass( 'hidden' );
// Remove hidden class from remove.
$( '#js-remove-site-icon' ).removeClass( 'hidden' );
// If the button is not in the update state, swap the classes.
if( $( '#choose-from-library-link' ).attr( 'data-state' ) !== '1' ){
var classes = $( '#choose-from-library-link' ).attr( 'class' );
$( '#choose-from-library-link' ).attr( 'class', $( '#choose-from-library-link' ).attr('data-alt-classes') );
$( '#choose-from-library-link' ).attr( 'data-alt-classes', classes );
$( '#choose-from-library-link' ).attr( 'data-state', '1' );

// Set the value of the hidden input to the attachment id.
$hiddenDataField.val( attachment.id );
} else {
frame.setState( 'cropper' );
}
} );

frame.open();
} );

/**
* Update the UI when a site icon is selected.
*
* @since 6.5.0
*
* @param {array} attributes The attributes for the attachment.
*/
function switchToUpdate( attributes ) {
var i18nAppAlternativeString, i18nBrowserAlternativeString;

if ( attributes.alt ) {
i18nAppAlternativeString = wp.i18n.sprintf(
/* translators: %s: The selected image alt text. */
wp.i18n.__( 'App icon preview: Current image: %s' ),
attributes.alt
);
i18nBrowserAlternativeString = wp.i18n.sprintf(
/* translators: %s: The selected image alt text. */
wp.i18n.__( 'Browser icon preview: Current image: %s' ),
attributes.alt
);
} else {
i18nAppAlternativeString = wp.i18n.sprintf(
/* Translators: %s: The selected image filename. */
wp.i18n.__(
'App icon preview: The current image has no alternative text. The file name is: %s'
),
attributes.filename
);
i18nBrowserAlternativeString = wp.i18n.sprintf(
/* Translators: %s: The selected image filename. */
wp.i18n.__(
'Browser icon preview: The current image has no alternative text. The file name is: %s'
),
attributes.filename
);
}

// swap the text of the button
$( '#choose-from-library-link' ).text( $( '#choose-from-library-link' ).attr( 'data-update-text' ) );
// Set site-icon-img src and alternative text to app icon preview.
$appIconPreview.attr( {
src: attributes.url,
alt: i18nAppAlternativeString,
} );

// Set site-icon-img src and alternative text to browser preview.
$browserIconPreview.attr( {
src: attributes.url,
alt: i18nBrowserAlternativeString,
} );

// Remove hidden class from icon preview div and remove button.
$iconPreview.removeClass( 'hidden' );
$removeButton.removeClass( 'hidden' );

// Set the global CSS variable for --site-icon-url to the selected image URL.
document.documentElement.style.setProperty(
'--site-icon-url',
'url(' + attributes.url + ')'
);

// If the choose button is not in the update state, swap the classes.
if ( $chooseButton.attr( 'data-state' ) !== '1' ) {
$chooseButton.attr( {
class: $chooseButton.attr( 'data-alt-classes' ),
'data-alt-classes': $chooseButton.attr( 'class' ),
'data-state': '1',
} );
}

// Swap the text of the choose button.
$chooseButton.text( $chooseButton.attr( 'data-update-text' ) );
}

$( '#js-remove-site-icon' ).on( 'click', function() {
$( '#site_icon_hidden_field' ).val( 'false' );
$( '#site-icon-preview' ).toggleClass( 'hidden' );
/**
* Handles the click event of the remove button.
*
* @since 6.5.0
*/
$removeButton.on( 'click', function () {
$hiddenDataField.val( 'false' );
$( this ).toggleClass( 'hidden' );

var classes = $( '#choose-from-library-link' ).attr( 'class' );
$( '#choose-from-library-link' ).attr( 'class', $( '#choose-from-library-link' ).attr( 'data-alt-classes' ) );
$( '#choose-from-library-link' ).attr( 'data-alt-classes', classes );

// Swap the text of the button.
$( '#choose-from-library-link' ).text( $( '#choose-from-library-link' ).attr( 'data-choose-text' ) );
// Set the state of the button so it can be changed on new icon.
$( '#choose-from-library-link' ).attr( 'data-state', '');
});
}(jQuery));
$iconPreview.toggleClass( 'hidden' );
$browserIconPreview.attr( {
src: '',
alt: '',
} );
$appIconPreview.attr( {
src: '',
alt: '',
} );

/**
* Resets state to the button, for correct visual style and state.
* Updates the text of the button.
* Sets focus state to the button.
*/
$chooseButton
.attr( {
class: $chooseButton.attr( 'data-alt-classes' ),
'data-alt-classes': $chooseButton.attr( 'class' ),
'data-state': '',
} )
.text( $chooseButton.attr( 'data-choose-text' ) )
.trigger( 'focus' );
} );

/**
* Update the site icon preview when the site title changes.
*
* @since 6.5.0
*/
$( '#blogname' ).on( 'input', function () {
$( '#site-icon-preview-site-title' ).text( $( this ).val() );
} );
} )( jQuery );
14 changes: 7 additions & 7 deletions src/wp-admin/css/forms.css
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ ul#add-to-blog-users {
outline: 2px solid transparent;
}

.button-add-site-icon{
.button-add-site-icon {
width: 100%;
cursor: pointer;
text-align: center;
Expand All @@ -801,23 +801,23 @@ ul#add-to-blog-users {
}

.button-add-site-icon:focus,
.button-add-site-icon:hover{
background: white;
.button-add-site-icon:hover {
background: #fff;
}

.site-icon-section .favicon-preview{
.site-icon-section .favicon-preview {
float: left;
}
.site-icon-section .app-icon-preview{
.site-icon-section .app-icon-preview {
float: left;
margin: 0 20px;
}

.site-icon-section .site-icon-preview img{
.site-icon-section .site-icon-preview img {
max-width: 100%;
}

.button-ad-site-icon:focus{
.button-add-site-icon:focus {
background-color: #fff;
border-color: #3582c4;
border-style: solid;
Expand Down
Loading