Score:0

CKeditor 5 generic block level styles

ru flag

Is it possible to define generic block level styles in CKeditor5, instead of targeting a specific HTML tag?

E.g. I can define this in editor.full_html.twig

settings:
  plugins:
    ckeditor5_style:
      styles:
        -
          label: 'looks like header 1'
          element: '<p class="h1">'
        -
          label: 'looks like header 2'
          element: '<p class="h2">'

But with this setting an editor can not style e.g. an <h3> to look like an .h2

Now I could add every single combination as style

settings:
  plugins:
    ckeditor5_style:
      styles:
        -
          label: 'P looks like header 1'
          element: '<p class="h1">'
        -
          label: 'H2 looks like header 1'
          element: '<h2 class="h1">'
        -
          label: 'H3 looks like header 1'
          element: '<h3 class="h1">'
        -
          label: 'H4 looks like header 1'
          element: '<h4 class="h1">'
        -
          label: '...(exaggerated example 300 lines later) H1 looks like header 6...'
          element: '<h1 class="h6">'

and I could live with the massive YAML config, but the styles combo in the admin UI gets equally polluted and flooded with so many disabled options that it becomes unusable.

Is there a way to define a style as "generic block level style"? E.g. a style .h1 that can be applied to any block tags like <p> and <h2> and <h3> and <h4> etc?

In CK4 a p.h4 was also available/mouse-clickable when the cursor was inside a <h3>. I don't know to to migrate that.

cn flag
I’ve also been looking into this the last few days - bottom line is the ckeditor styles plugin doesn’t support it, a tag name is required. It’s possible there’s another plugin out there which would do the trick, but even then it would need integrating with the Drupal style plugin to work. This is the data structure which mandates the element in case you’re interested: https://ckeditor.com/docs/ckeditor5/latest/api/module_style_style-StyleDefinition.html
Score:1
gb flag

Currently it seems that this feature is not included in ckeditor5.
Take a look at https://github.com/ckeditor/ckeditor5/issues/12771.

I would recommend to add the styles in the YAML config once and hide the disabled styles in the panel, like described in this feature request https://github.com/ckeditor/ckeditor5/issues/12770.

.ck.ck-style-panel .ck-style-grid .ck-style-grid__button.ck-disabled {
    display: none; /* Add this */
}
ru flag
I guess this is the best we can get for now. I'm leaving the question open though, maybe some future CKeditor updates will provide a better solution for this.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.