Search code examples
javascripttinymcekeyboard-shortcutswysiwyg

TinyMce Shorcut: change background color of the selected text with keybaord shorcut


I am using custom keyboard in my TinyMce editor like editor.addShortcut('ctrl+45', 'format', 'Bold'); (or indent, RemoveFormat) But I couldn't find a way to create a keyboard shortcut to change background color of the selected text.


Solution

  • Have you tried this?

    editor.addShortcut('ctrl+45', 'desc',['HiliteColor', false, '#FF99CC']);
    

    Also you can create a TinyMce plugin:

    1. go to your TinyMCE folder and open note.html
    2. In the list of plugins add background_color_plug
    3. open the folder plugins and create in it a folder background_color_plug and in this filder create a file plugin.js in which you should copy paste this:

      tinymce.PluginManager.add('background_color_plug', function (editor, url) {
      
      editor.addCommand('background_color_command', function () {
          var node = tinymce.activeEditor.selection.getNode();
          var color = tinymce.activeEditor.dom.getStyle(node, 'background-color', true);
          if (color =="rgb(255, 153, 204)") {
                  newcolor = "transparent";
          }
          else{
                  newcolor ="#FF99CC";
          }
              tinymce.activeEditor.execCommand('HiliteColor', false, newcolor);
      });  
      
      editor.addShortcut('ctrl+45', 'background_color_desc', 'background_color_command');
      });