Make WordPress Core

Opened 8 months ago

Closed 6 weeks ago

Last modified 6 weeks ago

#60548 closed defect (bug) (fixed)

Image editor: improve the browsePopup function

Reported by: afercia's profile afercia Owned by: joedolson's profile joedolson
Milestone: 6.7 Priority: normal
Severity: normal Version: 6.3
Component: Media Keywords: has-screenshots has-patch has-testing-info commit
Focuses: accessibility, javascript Cc:

Description

While auditing all the remaining jQuery deprecations still to address in core, I noticed a couple things that offer room for improvements in the browsePopup function of the core image editor.

  1. The browsePopup function uses the window.event property, which is deprecated and should not be used. This is the global event. Instead, the event should be passed as a function parameter. Aside: instead of using inline events, it could have been better to consider a more modern approach.
  2. When using the keyboard to navigate the items in the 'Image rotation' dropdown menu, the page scrolls. See attached animated GIF. There's some code in place to prevent page scrolling you may need to reduce your viewport height.

See [55919] / #50523

Attachments (3)

scroll.gif (1.2 MB) - added by afercia 8 months ago.
scroll-fix.gif (404.3 KB) - added by deepakvijayan 8 months ago.
after applying the fixes
60548.2.diff (4.2 KB) - added by joedolson 6 weeks ago.
Update PR to include JS changes & pass event.

Download all attachments as: .zip

Change History (24)

@afercia
8 months ago

#1 @afercia
8 months ago

Some more details:

  • The event used here, here, and here is the gloabl window.event, which is deprecated.
  • The preventDefault() used here doesn't actually prevent the page scrolling default action because the event in use is keyup which fires when a key is released. At that point the page scroll already occurred when pressing the key.
  • As a minor improvement, I'd suggest to rename the $target variable as that's not a jQuery object wrapping a DOM object. It is either false or the value returned by jQuery get() which is the DOM object. Traditionally, the core JavaScript prefixes variable names with $ as a convention for jQuery objects while this is a 'native' DOM object.
Last edited 8 months ago by afercia (previous) (diff)

#2 @deepakvijayan
8 months ago

Adjusting the event handling from onkeyup to onkeydown and passing the event object (event) as a parameter to the browsePopup function should help resolve the issues right?

Using onkeydown would indeed prevent the page from scrolling down as well.

For example inside image-edit.php if we change the following

<button type="button" class="imgedit-rleft button" onkeyup="imageEdit.browsePopup(this)" onclick="imageEdit.rotate( 90, <?php echo "$post_id, '$nonce'"; ?>, this)" onblur="imageEdit.monitorPopup()"><?php esc_html_e( 'Rotate 90&deg; left' ); ?></button>

to

<button type="button" class="imgedit-rleft button" onkeydown="imageEdit.browsePopup(event, this)" onclick="imageEdit.rotate( 90, <?php echo "$post_id, '$nonce'"; ?>, this)" onblur="imageEdit.monitorPopup()"><?php esc_html_e( 'Rotate 90&deg; left' ); ?></button>

similarly for all the buttons should help.

Also browsePopup could receive the event attribute like below

browsePopup: function (event, el) {
...

Also, we won't have to worry about depending on the global window.event.

I did test this out on my local environment and seems to be working fine.

@deepakvijayan
8 months ago

after applying the fixes

#3 @afercia
8 months ago

Adjusting the event handling from onkeyup to onkeydown and passing the event object (event) as a parameter to the browsePopup function should help resolve the issues right?

yes, either that or we would need to add a keydown event for the only purpose to prevent scrolling. It depends whether keeping the interaction with the menu on keyup is desired or not. I'd defer to @joedolson for a decision about the preferred interaction.

#4 @joedolson
8 months ago

  • Owner set to joedolson
  • Status changed from new to accepted

Re: "Aside: instead of using inline events, it could have been better to consider a more modern approach. "

I didn't see any reason to have everything else in inline events with just this event being executed differently from the rest of the image editor, nor did I see it as worthwhile to refactor the entire image editor. So, I pretty much stand by that approach in this case, even though it's far from ideal.

This ticket was mentioned in Slack in #core-media by joedolson. View the logs.


7 months ago

#6 @joedolson
7 months ago

  • Milestone changed from Awaiting Review to 6.6

This ticket was mentioned in Slack in #core-media by antpb. View the logs.


4 months ago

This ticket was mentioned in Slack in #accessibility by rcreators. View the logs.


4 months ago

#9 @nirajgirixd
4 months ago

I haven't seen any PR raised for this issue. If it is still valid, shall I raise a PR for the fix?

cc: @afercia @joedolson

Last edited 4 months ago by nirajgirixd (previous) (diff)

#10 @afercia
4 months ago

@nirajgirixd sure please do feel free to go ahead, thanks!

This ticket was mentioned in Slack in #accessibility by sabernhardt. View the logs.


4 months ago

#12 @sabernhardt
4 months ago

  • Keywords needs-patch added
  • Milestone changed from 6.6 to 6.7

This ticket was mentioned in PR #6856 on WordPress/wordpress-develop by @nirajgirixd.


4 months ago
#13

  • Keywords has-patch added; needs-patch removed

## Description

This PR addresses the scrolling issue encountered when using the keyboard to navigate different options in the image editor.

## Changes Made

  • Replaced onkeyup event with onkeydown event.

## Why?

When using the keyboard to navigate the items in the 'Image rotation' dropdown menu, the page scrolls.

## Screencast

https://github.com/WordPress/wordpress-develop/assets/51747980/fe1f8e04-0a3d-462a-8d10-00ca4ecdaae7

---

## Trac ticket: https://core.trac.wordpress.org/ticket/60548

This ticket was mentioned in Slack in #accessibility by joedolson. View the logs.


8 weeks ago

@joedolson
6 weeks ago

Update PR to include JS changes & pass event.

#15 @joedolson
6 weeks ago

  • Keywords needs-testing added

The updated patch incorporates the changes suggested by @afercia and @deepakvijayan for passing events and updating formatting.

Tested, works as expected for me: marking as needs testing.

Testing instructions:

Navigate to the media and edit an image.
Navigate the 'Image Rotation' menu using the tab and arrow keys. Observe that there is no unexpected shift of viewport position while browsing.

#16 @joedolson
6 weeks ago

  • Keywords has-testing-info added

This ticket was mentioned in Slack in #core-media by joedolson. View the logs.


6 weeks ago

#18 @antpb
6 weeks ago

  • Keywords commit added; needs-testing removed

This is working good in my testing. Validated the before and after scroll event viewport behavior. Code looks good. Let's ship it :)

#20 @joedolson
6 weeks ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 58946:

Media: Fix viewport scrolling and code style in image rotation.

Change browsePopup to use onkeydown, pass the event parameter from the calling control, and adjust variable naming style.

The browsePopup method used for the image rotation menu used onkeyup to trigger events, which prevented capturing browser scroll actions with arrows occurring on onkeydown.

Props afercia, deepakvijayan, nirajgirixd, joedolson, antpb.
Fixes #60548.

@joedolson commented on PR #7262:


6 weeks ago
#21

In r58946

Note: See TracTickets for help on using tickets.