Restrict keyboard shortcuts in TinyMCE editor

后端 未结 5 684
慢半拍i
慢半拍i 2021-02-08 16:38

Trying to find where to disable individual keyboard shortcuts in the jQuery version of TinyMCE editor. Currently the list of allowable shortcuts is:

  • ctrl+z
5条回答
  •  情书的邮戳
    2021-02-08 16:52

    Disable Tested in Firefox

    This should help get you started. You might need to actually add empty shortcuts for ctrl+u and ctrl+i to disable it in other browsers, but this code has been tested to disable the actions in Firefox. Just run after the initialization of tinyMCE has run (I tested mine in Firebug):

    for(var i in tinyMCE.editors){
      var editor = tinyMCE.editors[i];
      for(var s in editor.shortcuts){
        var shortcut = editor.shortcuts[s];
        // Remove all shortcuts except Bold (66), Redo (89), Undo (90)
        if(!(s == "ctrl,,,66" || s == "ctrl,,,89" || s == "ctrl,,,90")){
           // This completely removes the shortcuts
           delete editor.shortcuts[s];
    
           // You could use this instead, which just disables it, but still keeps
           // browser functionality (like CMD+U = show source in FF Mac) from interrupting the flow
           // shortcut.func = function(){  };
        }
      }
    }
    

    Background

    It appears to be defined around line 2294 of jscripts/tiny_mce/classes/Editor.js (From the full development download).

    Also, they are stored in an array in the Editor.shortcuts variable. They keys are setup with special chars then the keycode, like this: ctrl,,,90.

    But from what I can tell, it seems that many browser implement their own versions of ctrl+b, ctrl+i, and ctrl+u and that only Gecko browsers do not:

    // Add default shortcuts for gecko
    if (isGecko) {
        t.addShortcut('ctrl+b', t.getLang('bold_desc'), 'Bold');
        t.addShortcut('ctrl+i', t.getLang('italic_desc'), 'Italic');
        t.addShortcut('ctrl+u', t.getLang('underline_desc'), 'Underline');
    }
    

    But if you look around there, you can see how they enable it.

    Additionally, look into the Editor.addShortcut method. You might be able to override the default behavior.

提交回复
热议问题