Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add custom class to wysihtml5 text editor

I'd like to be able to add a button that adds my own custom class. I don't see this in the documentation anywhere but seems like a common request.

For example.

Highlighting "Some Text" and pressing the button "Custom Class" will add

<p class="wysiwyg-custom-class">Some Text</p>

like image 742
dardub Avatar asked Dec 13 '12 22:12

dardub


1 Answers

Define new command, my example is based on ForeColor:

(function(wysihtml5) {
    
  wysihtml5.commands.setClass = {
    exec: function(composer, command, element_class) {
        element_class=element_class.split(/:/);
        element=element_class[0];
        newclass=element_class[1];
      var REG_EXP = new RegExp(newclass,'g');
    //register custom class
      wysihtml5ParserRules['classes'][newclass]=1;

      return wysihtml5.commands.formatInline.exec(composer, command, element, newclass, REG_EXP);
    },

    state: function(composer, command, element_class) {
        element_class=element_class.split(/:/);
        element=element_class[0];
        newclass=element_class[1];
        var REG_EXP = new RegExp(newclass,'g');
      return wysihtml5.commands.formatInline.state(composer, command, element, newclass, REG_EXP);
    }
  };
})(wysihtml5);

usage:

HTML:

<div id="uxToolbar">
   <button data-wysihtml5-command="setClass" data-wysihtml5-command-value="span:my-wysihtml5-custom-class" type="button" title="View HTML" tabindex="-1" class="btn btn-mini">
       My class
   </button>
</div>

so as you can see value is from two parts: element:class

DEMO

like image 138
zb' Avatar answered Nov 05 '22 17:11

zb'