admin管理员组

文章数量:1418380

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 mon request.

For example.

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

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

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 mon request.

For example.

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

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

Share Improve this question asked Dec 13, 2012 at 22:25 dardubdardub 3,1955 gold badges31 silver badges33 bronze badges 2
  • 1,2 ,3, bonus this is documentation you looked for ? – zb' Commented Dec 13, 2012 at 23:32
  • As stated in the title this is for wysihtml5 text editor. I need to be able to highlight the text and add a custom class. It's more plicated than just adding a class though. – dardub Commented Dec 15, 2012 at 1:02
Add a ment  | 

1 Answer 1

Reset to default 5

Define new mand, my example is based on ForeColor:

(function(wysihtml5) {
    
  wysihtml5.mands.setClass = {
    exec: function(poser, mand, 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.mands.formatInline.exec(poser, mand, element, newclass, REG_EXP);
    },

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

usage:

HTML:

<div id="uxToolbar">
   <button data-wysihtml5-mand="setClass" data-wysihtml5-mand-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

本文标签: javascriptadd custom class to wysihtml5 text editorStack Overflow