Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow only certain formats in tinymce 4 Modern theme?

Tags:

tinymce

Is there a 'modern theme' (in other words, tinymce 4) equivalent of the theme_advanced_blockformats option?

theme_advanced_blockformats allows you to limit the set of available formats by adding the following to tinymce.init():

tinyMCE.init({
    ...
    theme_advanced_blockformats : "p,div,h1,h2,h3,h4,h5,h6,blockquote,dt,dd,code,samp"
});

(TinyMCE theme advanced block formats)

I know that it's possible to explicitly specify which formats are available by passing an option to tinymce.init(), like so:

tinyMCE.init({
    ...
    formats :
            bold : {inline : 'span', 'classes' : 'bold'},
            italic : {inline : 'span', 'classes' : 'italic'},
            underline : {inline : 'span', 'classes' : 'underline', exact : true},
    }
});

(TinyMCE formats)

Unfortunately, this wants a lot of detail about the way that each format is implemented that I don't have.

Any words of advice?

like image 897
Bacon Avatar asked Mar 25 '23 07:03

Bacon


2 Answers

This is as of latest TinyMCE release (4.1.3). Although the "block_formats" setting documents this functionality, I could only get this to work using the following:

   tinymce.init({
    selector: "textarea",
      style_formats: [
       {title: 'Paragraph', block: 'p'},
       {title: 'Heading 2', block: 'h2'},
       {title: 'Heading 3', block: 'h3'},
       {title: 'Heading 4', block: 'h4'},
    ],

 });

This is a simple example of the Tinymce documented custom formats syntax.

like image 166
ben.hamelin Avatar answered Apr 07 '23 00:04

ben.hamelin


The documentation is a bit spotty right now but you can control what is a valid block as well as define default attributes for blocks using valid elements. Declaring allowed blocks and default styles are now up to the TinyMCE core rather than the theme. valid_elements declares allowed blocks and extended_valid_elements declares default attributes for allowed blocks.

tinymce.init({
  selector: "textarea",
  valid_elements : "a[href|target=_blank],strong/b,div[align],br",
  extended_valid_elements: "img[class=myclass|!src|border:0|alt|title|width|height]",
  invalid_elements: "strong,b,em,i"
});
like image 29
Archonic Avatar answered Apr 07 '23 02:04

Archonic