Allow only certain formats in tinymce 4 Modern theme?

有些话、适合烂在心里 提交于 2019-12-22 08:49:31

问题


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?


回答1:


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.




回答2:


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"
});



回答3:


I think this is what you need:

http://www.tinymce.com/wiki.php/Configuration:block_formats

Like:

block_formats: "Paragraph=p;Heading 1=h1;Heading 2=h2;Heading 3=h3;Heading 4=h4"




回答4:


For Tinymce 4x, try the following:

tinymce.init({
    selector: "textarea",
    block_formats: 'Paragraph=p;Heading 1=h1;Heading 2=h2;Heading 3=h3;Heading 4=h4;Heading 5=h5;Heading 6=h6;Preformatted=pre',
});

https://www.tiny.cloud/docs-4x/configure/content-formatting/#block_formats

ben.hamelin's answer didn't work for me in v4.8.1.



来源:https://stackoverflow.com/questions/16048398/allow-only-certain-formats-in-tinymce-4-modern-theme

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!