Problem/Motivation
CKEditor 5 is not provided by an iframe like it is in CKEditor 4. So, stylesheets applied via ckeditor5-stylesheets are loaded like any other CSS file but are applied to the entire page instead of on the iframe.
that's why we need to prefix the selectors:
/* Prefix the h1 with .ck-content to keep the style in the editor, aget a flat list of all style options.nd avoid polluting the page with unwanted h1 styles */
.ck-content h1 {
color: blue;
}And that's explain why UI Styles are not available by default in Ckedtor5 previews.
Source: https://www.drupal.org/docs/core-modules-and-themes/core-modules/ckedito...
Proposed resolution
A command drush with a Drupal theme (ckeditor5-stylesheets doesn't work in modules) machine name as only parameter:
- From
*.ui_styles.yml, get a flat list of all style options (or use the plugin manager). - From
*.info.yml, get default libraries where the provider is the theme - From
*.libraries.yml, get the CSS files of those libraries - Open each CSS file with a CSS parser, and extract every selector containing a style option as a class (so, prefixes with dot)
- Add
.ck-content(with a space) before each selector and copy it with its related properties in a new file. - In
*.info.yml, add the file tockeditor5-stylesheetsif not already there:
ckeditor5-stylesheets: - whatever.css
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | sabberworm-php-css-parser-202.patch | 2.36 KB | grimreaper |
Issue fork ui_styles-3453784
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
Comment #2
grimreaperFor the record, example in UI Suite Bootstrap starter kit: https://git.drupalcode.org/project/ui_suite_bootstrap/-/blob/5.0.x/start...
Comment #3
grimreaperComment #4
thejimbirch commentedBut that doesn’t take into account the overrides set in the UI, right?
Comment #5
thejimbirch commentedAdding some notes from Slack:
Grimreaper
:drupalfr: 15 days ago
Hi,
Have you checked the example in the starterkit https://git.drupalcode.org/project/ui_suite_bootstrap/-/blob/5.0.x/start...…]tstrap_starterkit/assets/scss/ckeditor5.scss?ref_type=heads ?
thejimbirch
15 days ago
Yes. I just commented this on the ticket also. This pulls in the default bootstrap files from the vendor folder. But does it pull in the settings changed in the UI?
Grimreaper
:drupalfr: 15 days ago
no, it is static. I don't know if it is possible to conditionnally add cke5 stylesheets with a hook/event.
I already not found way to dynamically remove ones from parent theme. So, it will be the same for adding.
thejimbirch
15 days ago
Would it be possible to write the UI variables to a file and hardcore the cke5 style sheet in the theme?
Pierre (pdureau)
15 days ago
Would it be possible to write the UI variables to a file and hardcore the cke5 style sheet in the theme?
Because CSS variables management & mode/themes management are both part of the same module UI Skins, such a feature may be doable.
Comment #7
pdureau commentedComment #9
pdureau commentedTested. The first commit is going in the good direction.
Move code to
ui_styles_ckedtitor5sub moduleThis logic is related to Ckeditor5 and doesn't belong to main
ui_styles.Errors when absolute path in libraries
Current code doesn't work with path absolute from Drupal HTTP root:
This is working but not acceptable:
Sabberworm dependency
https://github.com/MyIntervals/PHP-CSS-Parser
It is OK to have such dependency, but;
HTTP controller instead of drush command
The HTTP controller is returning a CSS file. The response must be cached.
In order to keep track of the change instead of expecting theme wonder to run the commands regularly.
Also, this may fix 2 other issues in
info.ymlfile:Unexpected formatting changes
Which is fully loaded and rewritten:
Wrong property addition
The drush command is creating the file
css/ckeditor5.cssbut add this to info.yml:$themeInformationFileContent['stylesheets'] = [\sprintf('%s.css', $options['generated-file-name'] ?? 'ckeditor5')];2 errors:
ckeditor5-stylesheetsis expectedcss/is missingComment #11
grimreaperHi,
Review done with @pdureau. We put some comments in the MR.
Comment #12
grimreaperComment #13
grimreaperHi,
Refactoring done. Now:
- even more generic controller and service
- handle external files and local files outside the theme folder
- handle styles plugins overrides
- handle libraries extends/overrides, etc.
- handle CSS variables
- fix CSS content not in correct loop
TODO:
- cleanup
- ui skins CSS variables integration?
- tests
Comment #14
grimreapercross-linking the issue I found in the library: https://github.com/MyIntervals/PHP-CSS-Parser/pull/202
Comment #15
grimreaperTODO:
- tests
- https://github.com/MyIntervals/PHP-CSS-Parser/pull/202
Comment #16
grimreaperTests added, blocked by #3466681: More compact inline CSS.
TODO: try to create a patch for Sabberworm bug.
Comment #17
grimreaperI have created a patch with only one commit, rebased against main of php css parser, and only with the effective change, not the tests to avoid conflicts.
https://github.com/FlorentTorregrosa/PHP-CSS-Parser/pull/1
Comment #18
grimreaperComment #20
grimreaperMerged!
Thanks everyone!
Comment #21
thejimbirch commentedAmazing, thank you @Grimreaper!!!!
Comment #22
rpayanmHi @Grimreaper, thank you!
I tried this but I was unable to get it to work
I added this in my custom_theme.info.yml:
I got this error on Firefox:
And ended up with this URL:
If I change the URL in
ui_styles/ui_styles.routing.ymlfrom/ui_styles/stylesheet.cssto/ui_styles/testand visit that URL:http://example.com/ui_styles/test?prefix=.ck-contentI get the generated CSS right.What I am doing wrong?
Comment #23
grimreaperHello,
@thejimbirch, thanks, but I was not alone to work on it ;)
@rpayanm, with this change there is no more need to add a ckeditor5-stylesheets entry in your theme.
The CSS from http://example.com/ui_styles/test?prefix=.ck-content should be loaded automatically when you have at least one of the two CKE5 plugins of ui_styles_ckeditor5 enabled in your text format.
Also for the redirection problem, check with another browser to see if it is specific to Firefox, otherwise check your webserver configuration why there is this redirection. I didn't got one when developing the feature.