Adding a class to a UL in the RTE
# help-with-umbraco
d
I'm trying to figure out if there is solution that allows me to style *some *bulleted lists with a different background colour. There are basically two styles of bulleted list, and I need to toggle between them. The
<ul />
needs a class to do that. Using an RTE stylesheet I don't appear to be able to achieve this. I might end up adding a block inside my RTE, which I don't love. I'm sure this used to be possible by setting the stylesheet selector as
ul.green
but this wraps an additional
<ul />
around my original one. Just
.green
puts ``s inside my ``s. TIA
m
Could you add a CustomClass field to your document type and work from there? div.green ul....?
d
I appreciate your input. You could do that. I had in mind a settings model on my RTE block with a
useAlternativeListStyle
property. However it's not intuitive and styles all lists in the block in the same way. I'd love to find a solution within the RTE UI but I don't know as one exists.
j
I used to do this kind of thing with the Tiny MCE templates plugin, here's a link with how to do it in v13: https://our.umbraco.com/forum/using-umbraco-and-getting-started/111198-showing-templates-in-tinymce#comment-349641
Or do you feel this is too unintuitive and the option should be in the list options?
l
This would require users to know how to write html to make it a proper list with list items. At which point having it in a class dropdown is sort of pointless as it'd be less work for them to use the UI to build the list then just go into the markup and add the class themselves
d
Good point - I'll take another look
d
Hi @Dean Leigh . Thanks for going to the effort of capturing that journey. I think having
ul.red
as the selector is what should be the solution. However in your example it creates no
<li />
tags, and it you apply it to a list then it adds an extra
<ul />
wrapping the existing one. As @Lamont points out, this then requires some HTML coding. If they're doing that then they might as well add the class themselves. Adding the option to the list type would be πŸ’ͺ🏻, but I don't see an option in the Advanced List plugin to allow that. I think we're probably stuck with workarounds, but it was worth posting in case someone had a solution.
d
I think trying to add to their list would be complex as they have the nice drop down UI but making a new button possibly less so. I'm investigating
j
Did you try this @David Peck (Peck Tech) ?
d
I did take a look at this @Jason and it seems to be for adding chunks of HTML.
@David Peck (Peck Tech) I am trying to recreate something I already have working in v13 where I can add lang spans but caonnot remember for the life of me how we done it as it was an upgrade from v8. The code in appsettings is:
Copy code
"RichTextEditor": {
        "CustomConfig": {
          "style_formats": "[{\"title\": \"Headers\", \"items\": [{ \"title\": \"Header 2 (H2)\",\"block\": \"h2\"},{\"title\": \"Header 3 (H3)\",\"block\": \"h3\"}]},{\"title\": \"Languages\",\"items\": [{\"title\": \"English\",\"inline\": \"span\",\"attributes\": { \"lang\": \"en\" }},{\"title\": \"Japanese\",\"inline\": \"span\",\"attributes\": { \"lang\": \"ja\" }},{\"title\": \"Chinese\",\"inline\": \"span\",\"attributes\": { \"lang\": \"zh-Hans\" }}]}]"
        }
      }
j
Yep, but as pointed out, both the ul and li are what's needed, hence the HTML template suggestion. IIRC it's pretty straightforward to add a custom button to just drop a bit of HTML straight in, but I've not done that for a while and don't have an example.
Another solution, which now seems painfully obvious, is that I don't attempt to add a class to the UL at all. If I use a div as a container, then TinyMCE seems to play fairly nice. You can turn on and off the class for example. The only issue appears to be if you attempt to make an existing list green then it puts
<li><div class="green">foo</div></li>
but if you create the container first then you can add a UL in it. https://cdn.discordapp.com/attachments/1242820269517832203/1243236526335459371/image.png?ex=6650bdbe&is=664f6c3e&hm=f1e3ca186336677cd19877d566fe6ffc283711029fae7d29a375109f616412d0& https://cdn.discordapp.com/attachments/1242820269517832203/1243236526683459706/image.png?ex=6650bdbe&is=664f6c3e&hm=0c32de575ee3e1c029044c95e2b524855176a900a36cff98c76ee181c09d10b8&
A few less-than perfect but decent solutions here
d
@David Peck (Peck Tech) Would this do what you need?
Copy code
"RichTextEditor": {
    "CustomConfig": {
        "style_formats": "[{\"title\":\"Lists\",\"items\":[{\"title\":\"Alternative List\",\"selector\":\"ul\",\"classes\":\"alternative\"}]}]"
    }
}
I tend to group my formatting styles into 'Headings', 'Paragraphs', 'Images' etc, as it just makes for a clearer editing experience, like this:
Copy code
"RichTextEditor": {
    "CustomConfig": {
        "style_formats": "[{\"title\":\"Headers\",\"items\":[{\"title\":\"Heading 2\",\"block\":\"h2\"},{\"title\":\"Heading 3\",\"block\":\"h3\"},{\"title\":\"Heading 4\",\"block\":\"h4\"},{\"title\":\"Heading 5\",\"block\":\"h5\"}]},{\"title\":\"Paragraphs\",\"items\":[{\"title\":\"Intro\",\"block\":\"p\",\"classes\":\"intro\"}]},{\"title\":\"Lists\",\"items\":[{\"title\":\"Alternative List\",\"selector\":\"ul\",\"classes\":\"alternative\"}]}]"
    }
}
The key with ol/ul elements is to use
selector: ul
in the config, rather than
block: ul
(which, similar to Dean's suggestion above prevents
li
elements from being added as children). It does mean that the block you're wanting to style needs to be a
ul
before you can select the style (i.e. the content editor needs to make it a list using the list button first) which might be a showstopper for you, but it's the only way I can think of doing it without something custom or a little hacky, and it seems reasonable to me. πŸ™‚
d
That's where I was heading… for example I wrap sentences, words, characters in a 'span' to add lang=en.
j
That's a nice solution!
d
@Dan !!!! Whose the man? (it's you!) That worked great. @Dean Leigh - thanks for all your efforts @Jason - I'm using templates for something else in my project You all the bomb. I really appreciate your efforts.
Well done @Dan I did not think remove 'block' this works perfectly.
d
That this works suggests to me that there is something up with Umbraco's implementation.
d
If anyone needs a nicer way of creating the TinyMCE config for the formatting dropdown I was inspired to build a little helper tool: https://iomi.net/tinymce-umbraco/
b
It would be great if it also could deserialize from an existing configuration, modify it and get updated escaped json.
d
Not battle tested but I've updated to allow 2-way editing of the config. Ping me a message here if it breaks for you πŸ™‚
b
Something worked yes πŸ€— If it also worked with
formats
and
templates
properties under
CustomConfig
it would be even cooler 😎 https://cdn.discordapp.com/attachments/1242820269517832203/1252260990058496111/image.png?ex=6671926d&is=667040ed&hm=5bcb50058ca5141756764c486c9ce26c67ac6558acee93bdcbb8c1bab9110fdd&
d
There's quite a bit more stuff that this could generate tbh, even things like custom attributes or style attributes for each item, but I'm not sure I'm going to extend it much more tbh as the UI could start to get a bit unwieldy, but if you have an example config that demonstrates the kind of thing you're after I can at least have a think about it when I get a moment? I've not used
formats
but saw that documented (https://www.tiny.cloud/docs/tinymce/latest/user-formatting-options/#interactive-examples) β€” I hadn't actually tried out whether that works in Umbraco but good to know it does. Not sure what you mean by templates though?
b
Currently we have this configuration in a project:
Copy code
"RichTextEditor": {
   "CustomConfig": {
     "formats": "{\"cite\":{\"block\":\"cite\"}},\"checklist\":{\"block\":\"ul\",\"classes\":\"check-list\"},\"arrowlist\":{\"block\":\"ul\",\"classes\":\"arrow-list\"},\"numberlist\":{\"block\":\"ol\",\"classes\":\"number-list\"}",
     "style_formats": "[{\"title\":\"Paragraph\",\"format\":\"p\"},{\"title\":\"Blockquote\",\"format\":\"blockquote\"},{\"title\":\"Cite\",\"format\":\"cite\"},{\"title\":\"Code\",\"format\":\"code\"},{\"title\":\"Headers\",\"items\":[{\"title\":\"Heading h1\",\"block\":\"h1\"},{\"title\":\"Heading h2\",\"block\":\"h2\"},{\"title\":\"Heading h3\",\"block\":\"h3\"},{\"title\":\"Heading h4\",\"block\":\"h4\"},{\"title\":\"Heading h5\",\"block\":\"h5\"},{\"title\":\"Heading h6\",\"block\":\"h6\"}]},{\"title\":\"Checkmark list βœ“\",\"format\":\"checklist\"},{\"title\":\"Arrow list β†’\",\"format\":\"arrowlist\"},{\"title\":\"Number list βž€\",\"format\":\"numberlist\"}]",
     "templates": "[{\"title\":\"Checkmark list\",\"description\":\"Unordnered list with checkmark bullets\",\"url\":\"/App_Plugins/MCB.Umbraco.RTE/Templates/unordered-list-checkmark.html\"},{\"title\":\"Arrow list\",\"description\":\"Unordnered list with arrow bullets\",\"url\":\"/App_Plugins/MCB.Umbraco.RTE/Templates/unordered-list-arrow.html\"},{\"title\":\"Number list\",\"description\":\"Ordnered list with numbers\",\"url\":\"/App_Plugins/MCB.Umbraco.RTE/Templates/ordered-list-number.html\"}]"
   },
   "Commands": [
     {
       "Alias": "blockquote",
       "Name": "Blockquote",
       "Mode": "Selection"
     },
     {
       "Alias": "template",
       "Name": "Template",
       "Mode": "All"
     }
   ],
   "Plugins": [
     "template"
   ]
 }
The templates are used to insert predefined markup, in this case just
<ul>
and
<ol>
lists with a CSS class and 6 items by default, but it can be used to scaffold a table layout or other HTML markup. The formats are used to set format (class) on existing inserted lists.
85 Views