Problem/Motivation

On Drupal 10.2.2, when adding a Media element via the Insert Media button, once "Insert selected" is pressed, the window scrolls to the bottom of the page. This happens with any Wysiwyg using CKEditor 5 with the Insert Media button enabled.

This behavior is disruptive to content authors/editors as it interrupts the flow of adding content.

Steps to reproduce

  • Ensure you're on an Drupal 10.2.2 environment.
  • On one of the options under "Text formats and editors", add CKEditor 5 as the Text editor and ensure that the Insert Media button is available under the "Active toolbar".
  • Enable this Text format on the body or other text field of any content type.
  • Create a node of this content type, and in the above field, add a media item using the Insert Media button (I found this behavior with Images and Remote Videos but it may exist for other Media types as well).
  • After choosing an Image or other Media, after clicking "Insert selected", you should see the window scroll to the bottom of the page.

Proposed resolution

Rather than scrolling to the bottom of the page, the window should stay in place after the Media is inserted in order to allow for content authors to continue the flow of adding content.

When a Table is inserted, for instance, the window stays in place, so the Insert Media button should function the same.

Observation

At first I thought this was a Claro issue, but when I switched to a different Admin theme (in this case switching it to the custom frontend theme), the issue still existed.

Issue fork drupal-3415961

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

morganlyndel created an issue. See original summary.

cilefen’s picture

Component: base system » ckeditor5.module
Issue tags: -ckeditor5, -CKEditor in core
wim leers’s picture

Title: Using the Insert Media button causes the window to scroll to the bottom of the page » [drupalMedia] Using the Insert Media button causes the window to scroll to the bottom of the page
Status: Active » Postponed (maintainer needs more info)
Issue tags: +Needs tests, +JavaScript, +Accessibility

I cannot reproduce this. In which browser is this happening?

gouthamraon’s picture

Issue tags: -JavaScript +JavaScript

We are also facing the same issue.

After upgrading site to 10.2.2, we are seeing this issue.

@wim-leers : I tried this in Chrome and Firefox, both i'm seeing this issue.

gouthamraon’s picture

Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new674 bytes

Created a patch for the issue mentioned, please review.

wim leers’s picture

Status: Needs review » Needs work
Issue tags: +Needs manual testing, +Needs accessibility review
Related issues: +#3410598: CKEditor 5: scroll jump after dialog close

Thank you very much for pushing this forward, @gouthamraon! 😊

AFAICT this is a duplicate of #3410598: CKEditor 5: scroll jump after dialog close. But this now has a patch (thanks @gouthamraon!). But that issue is more general: it reports a similar problem with all dialogs. (The GIF there demonstrates this problem using the https://www.drupal.org/project/ckeditor_templates contrib module.)

I think the patch in #5 also has an impact when using the media library dialog in non-CKEditor 5 contexts 😅 So I think right now this is fixing one problem and introducing another.

Or … is this actually fixing a bug elsewhere in core too?

IOW: we need to test the #5 patch when using a media field.


Conclusion:

  1. either this should be marked as a duplicate of #3410598: CKEditor 5: scroll jump after dialog close, because we should not change the media library dialog
  2. or this is the correct fix, and it improves the media library dialog's accessibility in non-CKEditor 5 contexts too, and #3410598: CKEditor 5: scroll jump after dialog close is really just reporting a bug in the ckeditor_templates module

Manual testing will tell! 😊

gouthamraon’s picture

@wim-leers :I have verified the #5 patch with the media field and it's working fine.

Version: 10.2.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

edwardsay’s picture

Unfortunately, patch #5 does not work for me. It affects the "Insert selected" button, but not the close X button of the dialog itself. So when I close dialog the page jumps to the top of the CKE5 editor.

The JS solution from https://www.drupal.org/project/drupal/issues/3410598 works for drupalMedia CKE plugin too, though.

vselivanov’s picture

I had exactly the same issue with scroll and patch #5 fixed it.
Thank you, @gouthamraon!

anand.panure’s picture

We are facing the same issue with drupal 10.2.3 and CKEditor5, the focus moves to the bottom of the page after adding a media inside the CkEditor and also attaching an image from the Imagery tab for my banner.

The patch #5, is helpful but it shifts your focus to the "Insert Media" button. So, if it's a large page and you use sticky toolbar of CKEditor, the focus moves to the toolbar (which you can say is to the top of the page.)

Thanks,
Anand Panure

francismak’s picture

Hi,

We are having the same issue, and the patch #5 is working, thx @gouthamraon.

The "jumping/scrolling" was caused by trying to get the parent element of the dialog.
L1126 of core/misc/ajax.js

Original issue:
Focus state bug on text field AJAX calls

It introduced a new thing "preCommandsFocusedElementSelector". So if refocus back to "Insert Media" button is a problem as mentioned in #11, maybe able to make sure of this new command.

Ideally, it should focus the newly created media inside the CKEditor. However, the element doesn't exist before clicking the button.
And there is nothing to focus if user click 'x' to close the dialog in 3410598

Instead of focus, I wonder if there is a way to "do nothing".

kumkum29’s picture

As @anand.panure says, we are facing the same issue on D9.5.11 + ckeditor5. I need to update Drupal to 10.2, but I need to resolve this issue before that.

Is there a quick fix to resolve this problem and keep the position of the inserted media ?

The mentionned fix in #5 already exists in the media_library of core (in D9.5.11).

Thanks.

UPDATE : After a new test, the proposed temporary solution in this post https://www.drupal.org/project/drupal/issues/3410598 (by a Drupal Behavior) seems to work. It's not a perfect solution, but for the moment the wysiwyg interface is easier to use for the editors...

n.ghunaim’s picture

n.ghunaim’s picture

grimreaper made their first commit to this issue’s fork.

grimreaper’s picture

Component: ckeditor5.module » media system

Same problem When closing a media modal in Layout Builder.

For example when a block content type has a media entity reference field.

The focus it set to the top of the page.

grimreaper’s picture

StatusFileSize
new1.07 KB

Patch for Composer usage.

rajab natshah’s picture

Thanks, Florent, for the MR and the static patch file.
Hoping that this issue gets committed to Drupal Core

duttonma’s picture

StatusFileSize
new675 bytes

Patch updated for 10.5.x

almunnings’s picture

Was getting the submit button focused at the bottom of the page after inserting media.
MR works nicely for me.

rajab natshah’s picture

Version: 11.x-dev » main
attheshow’s picture

Patch #21 is working for us on Drupal 11.3.8. Thanks everyone!

mgifford’s picture

Status: Needs work » Needs review

I assume this needs to be reviewed again since the patch is working again.

littlecoding’s picture

I have not been able to test to confirm this to my satisfaction, but this may be only a symptom of a nested modals issue in core.

When the nested modals issue happens, the focus tracking is broken, and the focus is not returned or changed to that of an open modal. The focus is moved to a top level HTML element (most often the body tag). When the focus is on an element outside of a modal or a page level form element (like a WYSIWYG editor field), the browser can jump or scroll to the top or bottom of the page. Going to the top or bottom of the page and a visual scroll effect are dependent on the styling and behaviours of the page/site.

Does anyone have the time to test and confirm or dismiss this theory? I have come across a few issues in the queue that would be able to be closed as duplicates of the nested modal, and the efforts can be moved to having the nested modal issue fixed and committed.

kentr’s picture

Status: Needs review » Needs work
Issue tags: -Needs manual testing

Removing Needs manual testing. AFAICT from the comments, it's been manually-tested many times.

But it still needs tests (#3).

mgifford’s picture

I think that this test will suffice - https://github.com/mgifford/drupal-core-lab/blob/main/reports/issues/341...

It is AI generated so just leaving it there if folks want to reference it. I am terrible at writing tests.