5
votes

By default TinyMCE (4) has a "Paragraph ▼" dropdown, and if you click on it you get a list of formatting options ("Paragraph", "Heading 1", etc.).

I'd like to be able to do two things. First, I want to change the options and their names (eg. to "Normal" and "Heading"), and I found the block_formats option which does exactly that:

block_formats: 'Normal=p;Heading=h1'

However, I'm stuck on thing #2: adding classes to the generated elements. Instead of plain <h1> elements, when someone picks "Heading" I want to generate a <h1 class="heading">.

I thought maybe this would work:

block_formats: 'Normal=p;Heading=h1.heading'

... but it doesn't, and I haven't been able to find any other option that would let me do this. Then again, the TinyMCE documentation isn't always the easiest place to find answers, which is why I came here.

Does anyone know how I configure TinyMCE to have a "Paragraph ▼" dropdown with customized names AND custom classes on the generated elements?

1

1 Answers

1
votes

I never did find a way to do this, so what I wound up doing instead was remove the block format drop-down entirely and replace it with the (custom) format dropdown. In other words I:

  1. removed the formatselect from the toolbar1 config (removing the un-configurable normal formatting dropdown)
  2. added the custom format dropdown (styleselect) to the toolbar1 config
  3. defined a style_formats config entry with my custom styles

The style_formats config looked like this:

style_formats: [
    {
        title: 'Header',
        inline: 'span',
        classes: 'someClass',
        styles: {someStyle: '5px'}
    },
], // next style would go here

There were only two downsides of this approach. First, the dropdown now says "Formats", and I don't seem to be able to configure that anywhere. However I do have a single formatting dropdown, with only the options I want, and those options add the desired classes to the formatted text, so the dropdown's name isn't a big deal.

The second issue was that TinyMCE uses an <iframe>, which prevents it from using our stylesheet. I could have written a stylesheet for TinyMCE and then appended it to the <iframe> (or used some TinyMCE mechanism, if there is one) ... but I'm lazy so I just used style: entries for each custom format to define the style.