Make non editable bold

by Andrew Hunter

HTML

<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<textarea id="mytextarea">
  <p>
    I am editable.
  </p>
  <p>
    <span class="mceNonEditable">
      I am not editable, but I want to be wrapped by a 
      bold tag when the bold button is clicked. 
    </span>
  </p>
</textarea>

<br/>
<br/> When I click on the <b>B</b> button while the non editable span is selected in the editor, I would like the non editable span to be wrapped by a <b>b</b> tag.

JavaScript

tinymce.init({
  selector: '#mytextarea',
	plugins: 'textcolor noneditable',
  toolbar: 'forecolor backcolor | bold italic underline | variables',
  menubar: false,
  statusbar: false,
  // Tells Tinymce to treat any content with [] around it as noneditable
  noneditable_regexp: [/\[[^\[\]]*\]/g],
  setup: function (editor) {
  	var $ = tinymce.dom.DomQuery;
  	var nonEditableClass = editor.getParam('noneditable_noneditable_class', 'mceNonEditable');
    // Register a event before certain commands run that will turn contenteditable off temporarilly on noneditable fields
    editor.on('BeforeExecCommand', function (e) {
      // The commands we want to permit formatting noneditable items for
      var textFormatCommands = [
        'mceToggleFormat',
        'mceApplyTextcolor',
        'mceRemoveTextcolor'
      ];
      if (textFormatCommands.indexOf(e.command) !== -1) {
      	// Find all elements in the editor body that have the noneditable class on them
      	//  and turn contenteditable off  
        $(editor.getBody()).find('.' + nonEditableClass).attr('contenteditable', null);
      }
    });
    // Turn the contenteditable attribute back to false after the command has executed
    editor.on('ExecCommand', function (e) {
    	// Find all elements in the editor body that have the noneditable class on them
      //  and turn contenteditable back to false
      $(editor.getBody()).find('.' + nonEditableClass).attr('contenteditable', false);
    });
    // Register a button to add noneditable variables to the content
    editor.addButton('variables', {
      type: 'listbox',
      text: 'Variables',
      icon: false,
      values: [
      	{ text: 'First Name', value: 'first_name' },
        { text: 'Last Name', value: 'last_name' }
      ],
      onselect: function (e) {
        editor.insertContent('[' + this.value() + ']');
      }
    });
  },
  init_instance_callback: function (editor) {
	  
  	/* 
    	The following two hacks fix some weirdness with...