Stylesheet generator + CKE5 usage

Created on 10 June 2024, 5 months ago
Updated 2 September 2024, 2 months ago

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:

  1. From *.ui_styles.yml, get a flat list of all style options (or use the plugin manager).
  2. From *.info.yml, get default libraries where the provider is the theme
  3. From *.libraries.yml, get the CSS files of those libraries
  4. Open each CSS file with a CSS parser, and extract every selector containing a style option as a class (so, prefixes with dot)
  5. Add .ck-content (with a space) before each selector and copy it with its related properties in a new file.
  6. In *.info.yml, add the file to ckeditor5-stylesheets if not already there:
    ckeditor5-stylesheets:
      - whatever.css
    
✨ Feature request
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡«πŸ‡·France pdureau Paris

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @pdureau
  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

    For the record, example in UI Suite Bootstrap starter kit: https://git.drupalcode.org/project/ui_suite_bootstrap/-/blob/5.0.x/start...

  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·
  • πŸ‡ΊπŸ‡ΈUnited States thejimbirch Cape Cod, Massachusetts

    But that doesn’t take into account the overrides set in the UI, right?

  • πŸ‡ΊπŸ‡ΈUnited States thejimbirch Cape Cod, Massachusetts

    Adding 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.

  • First commit to issue fork.
  • πŸ‡«πŸ‡·France pdureau Paris
  • Pipeline finished with Success
    5 months ago
    Total: 234s
    #210765
  • Status changed to Needs work 4 months ago
  • πŸ‡«πŸ‡·France pdureau Paris

    Tested. The first commit is going in the good direction.

    Feedbacks:

    Move code to ui_styles_ckedtitor5 sub module

    This 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:

      css:
        theme:
          /libraries/dsfr/dist/dsfr.min.css: { minified: true }
          /libraries/dsfr/dist/utility/icons/icons.min.css: { minified: true }
          /libraries/dsfr/dist/utility/colors/colors.min.css: { minified: true }
    

    This is working but not acceptable:

      css:
        theme:
          ../../../libraries/dsfr/dist/dsfr.min.css: { minified: true }
          ../../../libraries/dsfr/dist/utility/icons/icons.min.css: { minified: true }
          ../../../libraries/dsfr/dist/utility/colors/colors.min.css: { minified: true }
    

    Let's manage

    Sabberworm dependency

    https://github.com/MyIntervals/PHP-CSS-Parser

    It is OK to have such dependency, but;

    • if would be better to replace it by a simpler, less-capable, single file, parser
    • if we keep it, we need to add it to composer.json

    Wrong addition info.yml

    The drush command is creating the file css/ckeditor5.css but add this to info.yml:

    stylesheets:
      - ckeditor5.css

    $themeInformationFileContent['stylesheets'] = [\sprintf('%s.css', $options['generated-file-name'] ?? 'ckeditor5')];

    2 errors:

    • not the right property, ckeditor5-stylesheets is expected
    • not the right path, css/ is missing

    Anyway, if we move to an HTTP controller, we will not need to add this property to YAML ourselves.

    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 an other issue: unexpected changes in info.yml file, which is fully loaded and rewritten:

    --- a/ui_suite_dsfr.info.yml
    +++ b/ui_suite_dsfr.info.yml
    @@ -1,44 +1,40 @@
    -name: UI Suite DSFR
    +name: 'UI Suite DSFR'
     type: theme
    -description: "A site-builder friendly theme implementing DSFR (https://www.systeme-de-design.gouv.fr/) using the UI Suite."
    -core_version_requirement: ^9 || ^10
    -base theme: false
    +description: 'A site-builder friendly theme implementing DSFR (https://www.systeme-de-design.gouv.fr/) using the UI Suite.'
    +core_version_requirement: '^9 || ^10'
    +'base theme': false
    
  • First commit to issue fork.
  • Pipeline finished with Success
    4 months ago
    Total: 363s
    #227926
  • Pipeline finished with Canceled
    4 months ago
    Total: 122s
    #227950
  • Pipeline finished with Success
    4 months ago
    Total: 271s
    #227953
  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

    Hi,

    Review done with @pdureau. We put some comments in the MR.

  • Pipeline finished with Success
    4 months ago
    Total: 210s
    #233944
  • Assigned to Grimreaper
  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·
  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

    Hi,

    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

  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

    cross-linking the issue I found in the library: https://github.com/MyIntervals/PHP-CSS-Parser/pull/202

  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·
  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

    Tests added, blocked by ✨ More compact inline CSS Active .

    TODO: try to create a patch for Sabberworm bug.

  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

    I 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

  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·
  • Issue was unassigned.
  • Status changed to Fixed 3 months ago
  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

    Merged!

    Thanks everyone!

  • πŸ‡ΊπŸ‡ΈUnited States thejimbirch Cape Cod, Massachusetts

    Amazing, thank you @Grimreaper!!!!

  • πŸ‡ΊπŸ‡ΎUruguay rpayanm

    Hi @Grimreaper, thank you!
    I tried this but I was unable to get it to work

    I added this in my custom_theme.info.yml:

    ckeditor5-stylesheets:
      - /ui_styles/stylesheet.css?prefix=.ck-content
    

    I got this error on Firefox:

    The page isn’t redirecting properly
    

    And ended up with this URL:

    http://example.com/ui_styles/stylesheet.css?q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&q=ui_styles/stylesheet.css&prefix=.ck-content
    

    If I change the URL in ui_styles/ui_styles.routing.yml from /ui_styles/stylesheet.css to /ui_styles/test and visit that URL: http://example.com/ui_styles/test?prefix=.ck-content I get the generated CSS right.

    What I am doing wrong?

  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

    Hello,

    @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.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024