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

XWIKI-17664: When adding an annotation the focus should be inside WYSIWYG by default #3694

Merged
merged 12 commits into from
Jan 15, 2025

Conversation

Sereza7
Copy link
Contributor

@Sereza7 Sereza7 commented Nov 26, 2024

Jira URL

https://jira.xwiki.org/browse/XWIKI-17664

Changes

Description

  • Added a comment to explain a line of code that doesn't work with CKEditor anymore
  • Added a CKE config change so that it gets focus when loaded.

Clarifications

  • The legacy way was just a textarea for the annotation edition. I left this in for backwards compatibility. However, in parallel I added a CKEditor config change that would focus the field once it's loaded.
  • Comments explain why I went with this solution precisely.

Screenshots & Video

On the video below, we can see a basic use case and experimentation with adjacent UIs (namely inline editing and comment edition).

2024-11-26.14-56-41.mp4

Executed Tests

Manual tests, see above.
I doubt the changes would change anything to tests since it's only a minor CKEditor config update (and the docker tests pretty much don't care about the focus state).
Built changes with mvn clean install -f xwiki-platform-core/xwiki-platform-annotation/xwiki-platform-annotation-ui and then successfully passed the docker test suite: mvn clean install -f xwiki-platform-core/xwiki-platform-annotation/xwiki-platform-annotation-test/xwiki-platform-annotation-test-docker.

Expected merging strategy

  • Prefers squash: Yes
  • Backport on branches:
    • 16.10.X

…IWYG by default

* Added a comment to explain a line of code that doesn't work with CKEditor anymore
* Added a CKE config change so that it gets focus when loaded.
@Sereza7
Copy link
Contributor Author

Sereza7 commented Nov 27, 2024

Assigning surli to this ticket since he reviewed and merged #3239 which was a very similar solution.

// If the WYSIWYG editor used is CKEditor, we make sure its input gets focused as soon as it's loaded
// Note that this CKE config is easier than having to handle custom eventListeners.
// As of 17.0.0, this does not interfere with the inline editor, so this solution is okay.
if(typeof CKEDITOR !== 'undefined') {
Copy link
Member

Choose a reason for hiding this comment

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

@mflorea I need your input there, is that the proper way to detect CKEditor?

Copy link
Contributor Author

@Sereza7 Sereza7 Dec 3, 2024

Choose a reason for hiding this comment

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

Note that this is the same as the way we used at 3b2b6f3 . If this is not a correct way, we would need to fix that place too.

According to tests on my local instance this works but I cannot attest to the stability of this solution.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm actually in the link you provided @mflorea made some comments about this check and about startupFocus.
Feels like it could be better to put that in an internal library that we would reuse where needed, and so if we need or want to change the way to perform this, we wouldn't need to check everywhere.
wdyt? does it worth it?

Copy link
Member

Choose a reason for hiding this comment

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

As I said on my comment for 3b2b6f3 , I think it's wrong to hard-code the CKEditor usage. The solution should be generic, for any WYSIWYG editor:

  • either by setting some attribute on the text area that is enhanced by CKEditor, before the WYSIWYG editor is loaded, and having some code in a CKEditor plugin that reacts to that attribute
  • or by triggering some generic event that a CKEditor plugin can listen to and react
  • or, if the previous two options are not possible, introduce some generic WYSIWYG editor JavaScript API that we implement for now in a CKEditor plugin, but this requires more work

@Sereza7 Sereza7 marked this pull request as draft December 3, 2024 12:41
@Sereza7
Copy link
Contributor Author

Sereza7 commented Dec 17, 2024

In the last few commits, I implemented a new solution:

  • Before calling the displayEdit on the field, the velocity template sets the variable $startupFocus to true.
  • The CKEditor default template in EditSheet checks this value, and update the textarea that will be replaced by adding it an appropriate data-xxx attribute.
  • The extension to the CKEditor methods extend and inline are now sensitive to the presence of this data-xxx attribute and change the CKEditor config accordingly.

I added comments in the annotation and comment module templates to explain what is this weird assignation. The comments refer to CKEditor for the sake of clarity because as of now it's the only implementation of an editor using this config. The extra cost for other editors is only setting a boolean that won't be used.

@mflorea Does this implementation look okay to you?


Note that I included a fix on the code that was not in the PR initially. We want to keep things consistent, IMO it makes sense to include it in this very PR since it's not a lot of lines.


Here is a quick demo of the updated changes:

2024-12-17.17-01-54.mp4

Note that in order to make it work, I had to disable query caching in my browser, it got me stuck for a while (I was refreshing the page cache with Ctrl+Shift+R but it wasn't enough).

@Sereza7 Sereza7 marked this pull request as ready for review December 17, 2024 16:01
@Sereza7 Sereza7 requested review from surli and mflorea December 17, 2024 16:01
@@ -356,6 +356,8 @@

#macro(displayAnnotationProperty $annotatedDocument $annotationObject $annotationProperty $mode)
#if ($mode == 'edit' || $mode == 'create')
## This startupFocus parameter is used by the CKEditor WYSIWYG editor.
#set ($startupFocus = true)
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if we shouldn't make his a bit more generic / extensible to prepare the ground for other configuration parameters we may need in the future:

Suggested change
#set ($startupFocus = true)
#set ($wysiwygEditorConfig = {
'startupFocus': true
})

This will make the configuration more explicit, and it will reduce the risk of naming conflicts with other entries from the script context. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good 👍
I added changes related to this in 359f650

Note that using the standard XWiki config for intellij idea, it auto formatted the two xmls into a wrong style. I had to deactivate the feature and fix it by hand in b7d3008 .

@Sereza7 Sereza7 requested a review from mflorea January 3, 2025 10:23

var config = {
filebrowserUploadUrl: uploadDisabled ? '' : getUploadURL(sourceDocument, 'filebrowser'),
startupFocus,
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't it be startupFocus: startupFocus? it's a key: value config object no?

Copy link
Member

Choose a reason for hiding this comment

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

It's called "shorthand property name" see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#property_definitions (syntax sugar to reduce boilerplate).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't know this notation well enough to use it either ^^'
#3694 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

ok thanks for the information, I'm not sure I'd have changed it since IMO it's not helping much reading but that's fine :)

Copy link
Member

@mflorea mflorea left a comment

Choose a reason for hiding this comment

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

Looks good, with just a small indentation issue.

@@ -72,8 +72,10 @@

<label for="$name">$services.localization.render('core.viewers.comments.add.comment.label')</label>
#initRequiredSkinExtensions()
## This startupFocus parameter is used by the CKEditor WYSIWYG editor.
#set ($startupFocus = true)
## This startupFocus parameter is used by the CKEditor WYSIWYG editor.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
## This startupFocus parameter is used by the CKEditor WYSIWYG editor.
## This startupFocus parameter is used by the CKEditor WYSIWYG editor.

Wrong indentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Fixed in 27ead97 👍

@surli surli merged commit 957d0e5 into xwiki:master Jan 15, 2025
github-actions bot pushed a commit that referenced this pull request Jan 15, 2025
…IWYG by default (#3694)

* Added a comment to explain a line of code that doesn't work with CKEditor anymore
* Added a CKE config change so that it gets focus when loaded.
* Implemented a solution that does not assume there's CKEditor
* Fixed codestyle
---------

Co-authored-by: Marius Dumitru Florea <[email protected]>
(cherry picked from commit 957d0e5)
surli pushed a commit that referenced this pull request Jan 15, 2025
…IWYG by default (#3694)

* Added a comment to explain a line of code that doesn't work with CKEditor anymore
* Added a CKE config change so that it gets focus when loaded.
* Implemented a solution that does not assume there's CKEditor
* Fixed codestyle
---------

Co-authored-by: Marius Dumitru Florea <[email protected]>
(cherry picked from commit 957d0e5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants