Problem/Motivation
I'd like users of my site to be easily able to add predefined CSS classes to images, in the same way that CKEditor 5 allows them to easily add predefined classes to text. But it isn't clear how to configure this.
The CKEditor 5 config page for a text format allows the Styles plugin to be used to add arbitrary CSS classes to specific HTML tags - these are displayed as a drop-down list which the user can select from. e.g. I can add "a.special-offer|Special Offer" to provide the user with a "Special Offer" style, which will add the "special-offer" class to a selected <a> tag in the text.
However, this does not work for images.
The Styles plugin will allow me to add an entry such as "img.feature|Featured Image" as a style, and this will appear as an option to the user in the Styles drop down. However, when the user selects an image, the Styles dropdown becomes greyed out, and there's a separate, pop-up toolbar with image-specific functions. This makes it impossible to use the styles drop-down to add the required style. (I imagine this is the expected behaviour.)
I presume that in order to allow the user to apply my custom style to an image, I'll need to add an option button to the image pop-up toolbar. How do I do that?
The CKEditor documentation for Image Styles says that custom CSS styles can be added. And it is clear that this is different from adding text Styles. These pages explain how to achieve this at the CKEditor config level. But what would I need to do in Drupal terms?
I can see that in core/modules/ckeditor5/ckeditor5.ckeditor5.yml there's some config relating to the imagestyle toolbar. I am happy to forgo a GUI interface, but even so, I imagine that what I want to do cannot be achieved simply by editing this file.
I looked at https://www.drupal.org/docs/core-modules-and-themes/core-modules/ckedito..., but it doesn't cover this kind of issue. I also looked at https://www.drupal.org/docs/core-modules-and-themes/core-modules/ckedito..., but wasn't sure it that was the right route to go down.
What are the steps for customising the imagestyle toolbar? Do I need to create a new plugin? Or can can I simply adjust some configuration for an existing plugin - as I can for styles for tags other than <img>?
Steps to reproduce
- Go to the configuration page for the Basic HTML text format.
- Ensure the Style button is in the Active Toolbar.
- Under CKEditor 5 Plugin Settings > Style, add a line: img.feature|Featured Image
- Click outside the text box.
- Click Save Configuration.
- Edit a page on the site with a rich text field. Ensure the field is set to use Basic HTML text format.
- Add some text to the field.
- Select some text, then click on the Styles dropdown. Note that you have the option to select the "Featured Image" style, even though this is not relevant for text (Doing so will have no effect).
- Insert an image into the field.
- Click on the image to select it.
- Note that the Styles dropdown is now greyed out, making it impossible to select the "Featured Image" style, and instead, a pop-up toolbar is shown with image-related options.
Proposed resolution
❌ Provide some documentation which explains how to add custom buttons to the image pop-up toolbar, including a button which adds a specified class to the image.
✅ It would be helpful if the Styles Plugin could warn users when they attempt to define a style for an <img> tag that this will not be effective.
❌ Or, better still, extend the Styles Plugin so that defining a style for an <img> tag causes a suitable button to appear in the image pop-up toolbar.
Remaining tasks
Review.
User interface changes
A helpful validation error message now appears immediately:
API changes
None.
Data model changes
None.
Release notes snippet
TBD
Comment | File | Size | Author |
---|---|---|---|
#17 | Screenshot 2023-11-13 at 10.54.21 AM.png | 360.93 KB | Wim Leers |
Issue fork drupal-3347721
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:
- 3347721-style-validate-unsupported changes, plain diff MR !5364
Comments
Comment #2
Wim LeersThe same limitation existed in CKEditor 4: #2642808: [upstream] CKEditor Style for <img> not working because it is an "image" widget. That's why it was not a blocker for #3222797: Upgrade path from CKEditor 4's StylesCombo to CKEditor 5's Style.
Note the sibling issue #3280124: [PP-1] [upstream] Consider allowing styles for non-HTML5 tags for CKEditor 5 and #3117172: Allow CKEditor styles for <drupal-media> for Drupal core's Media module.
💯 — this is a great action to take today!
Let's do that here! I'll ask in the upcoming CKEditor 5 meeting later today how we can get a reliable list of elements that the current
Style
plugin actually supports.Comment #4
Wim LeersJust discussed with @Reinmar & Witek. Witek indicates that list of tags that are supported by
Style
is actually one of the things their customers are missing in their official docs. So: 2 birds, 1 stone — they'll work on getting us this list soonish so that we can add the necessary validation and ship that to users in9.5.x
and10.0.x
🤓👍Comment #5
dabley CreditAttribution: dabley as a volunteer commented@Wim-Leers, thanks for your input on this. My comment about warning about the use of an unsupported style tag was really just an aside, and not the main point of the issue I raised. (It did occur to me that perhaps I should not create confusion by mentioning this.) Your change to the title rather shifts attention away from my main concern.
Thanks for pointing out the related issues. It is sad to see that despite 7 years of effort, there are no solutions, only partially successful workarounds. Nobody seems to have given much consideration to customising the image widget so as to include a styles dropdown. I wonder if that's because it is just too difficult?
Comment #7
Wim Leers@witeksocha from CKSource investigated this: https://github.com/ckeditor/ckeditor5/issues/13763
I think this should be implemented as validation? OTOH, doing so could cause existing configuration to be treated as invalid, even if in the future it will work, but for now it won't, until the upstream bugs are fixed … tricky 🤔
Comment #8
lauriiiCould we implement it just as a warning in the UI instead of schema validation that prevents saving?
Comment #9
Wim LeersYes, but how?
Comment #10
Wim Leers#3326261: [Style] Add tests for inability to apply styles to <div>, <ul>, <ol>, <table> etc. in CKEditor 5 — and <a> allows applying it to all elements proves that one of the examples in the issue summary now does work:
a.special-offer|Special Offer
. Butimg.feature|Featured Image
indeed does not yet work (upstream issue: https://github.com/ckeditor/ckeditor5/issues/13778), nor doesdrupal-media.features|Features media
(#3117172-34: Allow CKEditor styles for <drupal-media>).So I think this is still relevant.
@lauriii, what do you think about limiting the scope of this issue to
<img>
and<drupal-media>
for now? And perhaps showing a warning message that links to the relevant upstream issue, asking the site builder to go and vote on those issues 🤓Comment #11
smustgrave CreditAttribution: smustgrave at Mobomo commentedJust my 2 cents but I think any warnings that core can do around ckeditor would be extremely useful.
Comment #12
lauriiiThe proposal seems fine 👍 We can expand the scope of the warning if we discover other use cases that are not compatible.
Comment #13
Wim LeersJust closed #3398786: CKEditor 5 - styles for Images not picking up as a duplicate of this. This is clearly becoming increasingly important!
Comment #16
Wim LeersJust pushed a test.
Comment #17
Wim LeersComment #18
Wim LeersNow passing tests 👍
P.S.: please vote on https://github.com/ckeditor/ckeditor5/issues/13778 — that's how they prioritize things: based on demand!
Comment #19
smustgrave CreditAttribution: smustgrave at Mobomo commentedRan the test-only feature and got
tag is not yet supported by the Style plugin.'
- 'settings.plugins.ckeditor5_style.styles.1.element' => 'Unfortunately, the
<br>
tag is not yet supported by the Style plugin.'-)
+Array &0 ()
Tested manually following the steps in the issue summary and am now seeing the warning message.
Think this will be very helpful for many people
Comment #23
lauriiiCommitted e8d2c22 and pushed to 11.x. Also cherry-picked to 10.2.x. Thanks!