Skip to content
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

Fix (image): Allow closing insert via url dialog window by enter key #17633

Merged
merged 7 commits into from
Dec 17, 2024

Conversation

l-lejman
Copy link
Contributor

Suggested merge commit message (convention)

Fix (image): Insert image via url dialog can be closed by "enter" button when input is focused. Closes #16902.


I tried to add better unit tests for this case but submitting form by "enter" button works different in browser than in unit test environment. I left comment to prevent confusion why unit test has no "enter" click.

Copy link
Contributor

@f1ames f1ames left a comment

Choose a reason for hiding this comment

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

The approach for this fix went into a good direction, to have a form in place so enter could be handled 👍 However, the changes you applied affects DialogView component globally (see review comments) which is not what we want IMHO.

The right approach would be to modify to ImageInsertViaUrlUI to have the form element rendered into the dialog and then proper submit handling. There are other plugins (like MediaEmbed) which already have this so you may take a look how it's done there.

@@ -260,7 +260,7 @@ export default class DialogView extends /* #__PURE__ */ DraggableViewMixin( View
},
children: [
{
tag: 'div',
tag: 'form',
Copy link
Contributor

Choose a reason for hiding this comment

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

DialogView is a "generic" component reused by CKEditor 5 in multiple places by multiple plugins. This change would affect all of the dialogs so this is not the right place for such change if we only want to adjust the behavior of a specific plugins dialog.

@@ -186,6 +191,7 @@ describe( 'ImageInsertViaUrlUI', () => {

testSubmit( 'accept button', () => acceptButton.fire( 'execute' ) );

// Can not be checked by simulating enter click because of unit test limitations
Copy link
Contributor

Choose a reason for hiding this comment

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

It's a good approach to describe "not so obvious" code, however this could use some more details to be clear. You could describe that we fire form submit event to simulate what browser does on enter press instead of trying to trigger enter key event on a button directly.

@l-lejman l-lejman changed the title Fix (image): Add form tag and submit button to insert via url dialog window to allow closing it by enter key Fix (image): Add form tag and submit button to insert via url dialog window to allow closing it by enter key. Dec 13, 2024
@l-lejman l-lejman changed the title Fix (image): Add form tag and submit button to insert via url dialog window to allow closing it by enter key. Fix (image): Allow closing insert via url dialog window by enter key. Dec 16, 2024
@l-lejman l-lejman changed the title Fix (image): Allow closing insert via url dialog window by enter key. Fix (image): Allow closing insert via url dialog window by enter key Dec 16, 2024
@l-lejman l-lejman requested a review from f1ames December 16, 2024 16:22
Copy link
Contributor

@f1ames f1ames left a comment

Choose a reason for hiding this comment

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

First thing, always make sure that CI is green when passing a PR for a review. Here, there is one job failing (cc is not 100% after recent changes).

Second thing, as for:

The approach for this fix went into a good direction, to have a form in place so enter could be handled 👍 However, the changes you applied affects DialogView component globally (see review comments) which is not what we want IMHO.

The right approach would be to modify to ImageInsertViaUrlUI to have the form element rendered into the dialog and then proper submit handling. There are other plugins (like MediaEmbed) which already have this so you may take a look how it's done there.

Well, you solved it by adding a flag, but it's unnecessary and makes the generic dialog more complex. As each plugin has a full control over it's views and can render anything there, there is no reason for lifting this logic higher in components hierarchy.
As suggested before, we already have plugins doing this. You may take a look on MediaEmbed plugin and its UI (here is the view definition) and see how "Image insert via URL" view can be adjusted (here).

Comment on lines 194 to 197
/**
* Browsers handle clicking Enter for form submitting by its own but unit test can not, it is known limitation.
* We fire a form submit event to simulate the behavior that occurs when pressing Enter in the browser.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/**
* Browsers handle clicking Enter for form submitting by its own but unit test can not, it is known limitation.
* We fire a form submit event to simulate the behavior that occurs when pressing Enter in the browser.
*/
/**
* Browsers handle pressing Enter on forms natively by submitting it. We fire a form submit event to simulate that behavior.
*/

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it could be a bit shorter as above. And a "nitpick", enter is pressed not clicked (usually at least 😄).

@f1ames
Copy link
Contributor

f1ames commented Dec 17, 2024

Just wanted to add something here. The funny thing is that we already have a test which checks "enter submit" behavior, and it happily passes.

This is because, the tests simulates "submit via enter" by sending a submit event on a top dialog view element (which is a div):

testSubmit( 'form submit (enter key)', () => {
const form = dialog.view.contentView.children.get( 0 );
form.fire( 'submit' );
} );

And then we have submit handler attached to this view which catches this event and runs the callback:

if ( !this._formView ) {
this._formView = this._createInsertUrlView();
this._formView.on( 'submit', () => this._handleSave() );
}

However, browser natively will not trigger the submit event when there is no form element 🤷 🙈

Copy link
Contributor

@f1ames f1ames left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@f1ames f1ames merged commit aa1ea78 into master Dec 17, 2024
9 checks passed
@f1ames f1ames deleted the ck/16902 branch December 17, 2024 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent behavior for URL fields
2 participants